PL/pgsql locking policy

Started by Richard Embersonabout 24 years ago2 messagesgeneral
Jump to latest
#1Richard Emberson
emberson@phc.net

If you have a PL/pgsql procedure that first reads from a given table to
verify a condition and
then if the condition is true write to that same table ... and at the
same time you want other
processes to be able to simply read from the table, what is the best
locking policy within the procedure?

While ACCESS EXCLUSIVE MODE prevents others from reading.
allowing other to read means that two process might be concurrently
reading (verifying) within
the procedure, reach the same condition value, and then both attempt to
write data.

Is it better to simply have a helper table, a table created just for
this one procedure, the only mechanism
where writing to the table actually occurs, and use this helper table as
the locking mechanism.

Richard

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Richard Emberson (#1)
Re: PL/pgsql locking policy

Richard Emberson <emberson@phc.net> writes:

If you have a PL/pgsql procedure that first reads from a given table to
verify a condition and
then if the condition is true write to that same table ... and at the
same time you want other
processes to be able to simply read from the table, what is the best
locking policy within the procedure?

You could use EXCLUSIVE lock mode, which blocks everything except SELECT.

regards, tom lane