I have two tables. One is a log table; another contains, essentially, coupon codes that can only be used once.
The user needs to be able to redeem a coupon, which will insert a row into the log table and mark the coupon as used (by updating the used
column to true
).
Naturally, there’s an obvious race condition/security issue here.
I’ve done similar things in the past in the world of mySQL. In that world, I’d lock both tables globally, do the logic safe in the knowledge that this could only happen once at a time, and then unlock the tables once I was done.
Is there a better way in Postgres to do this? In particular, I’m concerned that the lock is global, but doesn’t have to be — I really only need to make sure no one else is trying to enter that particular code, so perhaps some row-level locking would work?