postgresql - How to manually lock and unlock a row? -
i trying circumvent double-writes locking , unlocking rows.
i have table personnel , user tries edit row, want lock row.
so
begin; // guess have lock somehow here select * personnel id = 12; commit; and once edit been made, want submit update in same style:
begin; //unlocking update personnel set ... id = 12; commit; in time between, when user tries edit same row, message.
locks released @ end of transaction. need keep transaction open uphold lock , in single transaction:
begin; select * personnel id = 12 for update nowait; -- row level locking commit; begin; update personnel set ... id = 12; commit; alternatively can use less restrictive for share clause.
details in manual on select in chapter "the locking clause".
concurrent reads allowed. quoting chapter "row-level locks" in manual:
in addition table-level locks, there row-level locks, can exclusive or shared locks. exclusive row-level lock on specific row automatically acquired when row updated or deleted. lock held until transaction commits or rolls back, table-level locks. row-level locks not affect data querying; block writers same row.
to have concurrent attempts same fail immediately, use nowait option - all competing queries.
Comments
Post a Comment