Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually Release Postgres LOCK

I would like to know if there´s any command to unlock or release a lock made by the same transaction.

Pseudo-code

FUNCTION
    TRANSACTION
        LOOP
            TABLE LOCK
            table operations...
            "TABLE UNLOCK WANTED"
        END
    END OF TRANSACTION
END OF FUNCTION

The function query can take a while as the LOOP might be large, so I would like to be able to unlock before the transaction is fully finished.

like image 334
masize Avatar asked May 01 '26 02:05

masize


1 Answers

No, it isn't possible. Locks are held until end of transaction, no exceptions.

Thus, you need to either:

  • Use a nonstandard lock like an advisory lock, which requires everyone to check for it and respect it; or

  • Do your work in transactions that commit autonomously, before the outer transaction is done. In PostgreSQL, lack of autonomous subtransaction support means that you must use dblink for this.

like image 56
Craig Ringer Avatar answered May 02 '26 19:05

Craig Ringer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!