I have a function that should perform a long update on several large tables. During the update, every 2-3 tables must be locked in EXCLUSIVE mode.
Since not all tables should be locked at the same time, ideally, I would like to LOCK only those tables that I update at that time, and then remove the lock as soon as we finish.
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 is no equivalent to the UNLOCK statement in plpgsql. The usual way to delete a LOCK is a COMMIT transaction, but this is not possible in a function.
Is there any solution for this? Is there some way to explicitly release the lock before executing the function? Or run some kind of sub-transaction (perhaps by running each update in a separate function)?
UPDATE
I agreed that there is no solution. I will write each update in a separate function and coordinate from outside db. Thanks to everyone.
sql plpgsql postgresql
panta82
source share