You can lock a row in a Postgres table by using "FOR UPDATE" in a SELECT query. The row will stay locked until the transaction is over. For example:

BEGIN;

SELECT *
FROM users
WHERE id = 123
FOR UPDATE; -- ๐Ÿ‘ˆ

-- at this point, another process can't update user #123 until we're done

UPDATE users
SET name = 'John Smith'
WHERE id = 123;

COMMIT; -- we're done
Latest
Mastodon Mastodon