PostgreSQL obtain and release LOCK inside stored function

僤鯓⒐⒋嵵緔 提交于 2019-12-12 09:36:51

问题


I have a function that needs to perform a long update on multiple large tables. During the update 2-3 tables at a time need to be locked in EXCLUSIVE mode.

Since not all the tables need to be locked at the same time, ideally I'd want to LOCK only those tables I'm updating at the time, and then remove the lock once I'm done.

Eg.

-- Lock first pair of tables
LOCK TABLE tbl1_a IN EXCLUSIVE MODE;
LOCK TABLE tbl1_b IN EXCLUSIVE MODE;

-- Perform the update on tbl1_a and tbl1_b

-- Release the locks on tbl1_a and tbl1_b
--  HOW???

-- Proceed to the next pair of tables
LOCK TABLE tbl2_a IN EXCLUSIVE MODE;
LOCK TABLE tbl2_b IN EXCLUSIVE MODE;

Unfortunately, there's no the equivalent of UNLOCK statement in plpgsql. The normal way to remove LOCK is to COMMIT the transaction, but that is not possible inside a function.

Is there any solution for this? Some way to explicitly release the lock before function is done? Or run some kind of sub-transaction (perhaps by running each update in a separate function)?

UPDATE

I accepted that there is no solution. I'll write each update into a separate function and coordinate from outside the db. Thanks everyone.


回答1:


There is no way. Functions in Postgres are atomic (always inside a transaction) and locks are released at the end of a transaction. And there are no autonomous transactions (yet).

You might be able to work around this with advisory locks. But those are not the same thing. All competing transactions have to play along. Concurrent access that is not aware of advisory locks will spoil the party.

Code example on dba.SE:

  • Postgres UPDATE ... LIMIT 1

Or you might get somewhere with "cheating" autonomous transactions with dblink:

  • How do I do large non-blocking updates in PostgreSQL?
  • Does Postgres support nested or autonomous transactions?

Or you re-assess your problem and split it up into a couple of separate transactions.




回答2:


Not possible. From documentation: Once acquired, a lock is normally held till end of transaction. But if a lock is acquired after establishing a savepoint, the lock is released immediately if the savepoint is rolled back to. This is consistent with the principle that ROLLBACK cancels all effects of the commands since the savepoint. The same holds for locks acquired within a PL/pgSQL exception block: an error escape from the block releases locks acquired within it.

http://www.postgresql.org/docs/9.3/static/explicit-locking.html




回答3:


In pg11 you now have PROCEDUREs which let you release locks via COMMIT. I just converted a bunch of parallel executed functions running ALTER TABLE ... ADD FOREIGN KEY ... with lots of deadlock problems and it worked nicely.

https://www.postgresql.org/docs/current/sql-createprocedure.html



来源:https://stackoverflow.com/questions/28994567/postgresql-obtain-and-release-lock-inside-stored-function

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