问题
I would like to lock a table for writing during a period of time, while leaving it available for reading.
Is that possible ?
Ideally I would like to lock the table with a predicate (for example prevent writing rows "where country = france").
回答1:
If you really want to lock against such inserts, i.e. the query should hang and only continue when you allow it, you would have to place a SHARE lock on the table and keep the transaction open.
This is usually not a good idea.
If you want to prevent any such inserts, i.e. throw an error when such an insert is attempted, create a BEFORE INSERT trigger that throws an exception if the NEW row satisfies the condition.
回答2:
You can use FOR SHARE lock, which blocks other transactions from performing like UPDATE and DELETE, while allowing SELECT FOR SHARE. (Read the docs for details: https://www.postgresql.org/docs/9.4/explicit-locking.html [13.3.2])
For example, there are 2 processes accessing table user_table, in the following sequence:
- Process A:
BEGIN; - Process A:
SELECT username FROM user_table WHERE country = france FOR SHARE; Process B:
SELECT * FROM user_table FOR SHARE;(In here, process B can still read all the rows of the table)Process B:
UPDATE user_table SET username = 'test' WHERE country = france;(In here, process B is blocked and is waiting for process A to finish its transaction)
来源:https://stackoverflow.com/questions/58453621/how-to-lock-a-table-for-writing