LoginSignup
5
9

More than 3 years have passed since last update.

PostgreSQL クライアントのタイムアウト設定について

Posted at

クライアントのタイムアウト設定について

PostgreSQLクライアントからSQLを実行した場合のタイムアウトの設定は以下の2つがあります。

  • statement_timeout
  • lock_timeout

statement_timeoutを設定すると、SQLが実行されてから指定した時間を経過すると処理を中断します。
lock_timeoutを設定すると、SQLがロックを取得しようと待っている間に、指定した時間を経過すると処理を中断します。
両方ともpostgresql.confに設定することもできますが、全てのセッションに影響を与えるため、セッションごとに設定することになります。

セッション中で両方のタイムアウトを設定することもできます。
その場合、statement_timeout > lock_timeoutで設定します。そうしないとlock_timeoutより必ず先にstatement_timeoutで処理が中断されることになります。

lock_timeout

lock_timeoutを設定してタイムアウト時の動作を確認してみます。
タイムアウトは10,000ms(10秒)に設定しています。

まず、test01テーブルをselectし、ACCESS SHAREロックを取得します。

postgres=# begin;
BEGIN
postgres=# select * from test01;
 id 
----
(0 rows)

別セッションで、lock_timeoutを設定し、test01テーブルをロックします。
ACCESS SHAREのロックがかかっており、ロックが取得できずタイムアウトとなります。

postgres=# set lock_timeout = 10000;
SET
postgres=# begin;
BEGIN
postgres=# lock table test01;
ERROR:  canceling statement due to lock timeout

ログにもERRORレベルで出力されます。

2020-08-24 23:24:01.972 CEST [4011] ERROR:  canceling statement due to lock timeout
2020-08-24 23:24:01.972 CEST [4011] STATEMENT:  lock table test01;

statement_timeout

statement_timeoutを設定してタイムアウト時の動作を確認してみます。
タイムアウトは10,000ms(10秒)に設定しています。

lock_timeoutを設定し、pg_sleep(30)で30秒間スリープします。
そうすると10秒経過後にタイムアウトします。

postgres=# set statement_timeout to 10000;
SET
postgres=# select pg_sleep(30);
ERROR:  canceling statement due to statement timeout

ログにもERRORレベルで出力されます。

2020-08-24 23:31:47.978 CEST [4011] ERROR:  canceling statement due to statement timeout
2020-08-24 23:31:47.978 CEST [4011] STATEMENT:  select pg_sleep(30);

参考

5
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
9