Monday, January 30, 2023

Are you able to lock a table outside begin/commit statement

 Are you able to lock a table outside begin/commit oe end transaction control statement:


pgtst_db=# lock table pgtst_vacuum in exclusive mode;

ERROR:  LOCK TABLE can only be used in transaction blocks

pgtst_db=#


Observation: No. As per the postgresql guide as of 30Jan2023, its not possible to lock a table outside a transaction control block.


Note:

Also a commit between begin/end or commit closes the tx and releases any exclusive lock acquired manually like below...


Session 1:

pgtst_db=# begin work;
BEGIN
pgtst_db=*# lock table pgtst_vacuum in exclusive mode;
LOCK TABLE
pgtst_db=*# INSERT INTO pgtst_schema.pgtst_vacuum
pgtst_db-*# SELECT i, lpad('TST',mod(i,100),'CHK')
pgtst_db-*# FROM generate_series (20016,20020) s(i);
INSERT 0 5
pgtst_db=*# commit;
COMMIT
pgtst_db=#

Session 2:

pgtst_db=# begin work;
BEGIN
pgtst_db=*# lock table pgtst_vacuum in exclusive mode;
LOCK TABLE                                              <<<< this was acquied as soon as commit is issued. No need of 'commit work;' statement.
pgtst_db=*#

No comments:

Post a Comment

Troubleshooting the “Cannot Generate SSPI Context” Error After SQL Server Migration

  Introduction After a recent  SQL Server migration from 2012 to 2022 , our team encountered a classic authentication issue: The target prin...