"SET LOCAL can only be used in transaction blocks" warning in PostgreSQL

1.3k views Asked by At

When setting lock_timeout 10 seconds locally on psql as shown below:

SET LOCAL lock_timeout = 10000;

I got the warning below on psql:

WARNING: SET LOCAL can only be used in transaction blocks

Then, SET LOCAL lock_timeout = 10000; doesn't apply to the following transaction at all so lock table person; waits to lock the table forever without timeout after 10 seconds as shown below:

postgres=# SET LOCAL lock_timeout = 10000;
WARNING:  SET LOCAL can only be used in transaction blocks
SET
postgres=# BEGIN;
BEGIN
postgres=*# LOCK TABLE person; # Waits to lock the table forever

So, how can I solve the warning then apply SET LOCAL lock_timeout = 10000; to the following transaction?

1

There are 1 answers

0
Super Kai - Kazuya Ito On

As the warning below says:

WARNING: SET LOCAL can only be used in transaction blocks

You need to use SET LOCAL in transaction after running BEGIN then it works properly as shown below:

postgres=# BEGIN;
BEGIN
postgres=*# SET LOCAL lock_timeout = 10000;
SET
postgres=*# LOCK TABLE person; # Waits to lock the table for 10 seconds
ERROR:  canceling statement due to lock timeout # Cancelled after 10 seconds
postgres=!#