how to lock a table for writing

為{幸葍}努か 提交于 2019-12-11 06:03:33

问题


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:

  1. Process A: BEGIN;
  2. Process A: SELECT username FROM user_table WHERE country = france FOR SHARE;
  3. Process B: SELECT * FROM user_table FOR SHARE; (In here, process B can still read all the rows of the table)

  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!