Laurenz Albe wrote:
> Here is a trivial example:
>
>   BEGIN;
>
>   SELECT value FROM tab WHERE id = 42 FOR NO KEY UPDATE;
>
>   /* calculate a new value in the application */
>
>   UPDATE tab SET value = 'new_value' WHERE is = 42;
>
>   COMMIT;

I have used a variation of Laurenz' example to port a pessimistic locking
application from DB2 to PostgreSQL. The application was CICS and had
ENQ before the SELECT ... FOR READ ONLY and DEQ after the UPDATE,
basically a critical section for replicas of this application.
The ENQ is emulated in PG with pg_advisory_lock() and the DEQ by
pg_advisory_unlock().
In db2 pessimistic lockiing, FOR READ ONLY is blocked by an UPDATE lock:
writers block readers in db2, not like PG. I used SELECT ... FOR SHARE
so the SELECT waits on the write (UPDATE) transaction to end.

et voila, pessimistic locking. The "problem":  FOR SHARE
is a WAL write and absolutly bombs the logging ... so it is correct but
READ ONLY is treated as  a WAL writer in PG.

Regards,
Dave Sharpe


Reply via email to