DEV Community

Dmitry Romanoff
Dmitry Romanoff

Posted on

PostgreSQL parameter statement_timeout

The statement_timeout is configuration parameter of PostgreSQL.

It sets the length of time before a statement automatically time out.

Usage examples.

#1 Example. How to cancel long running SELECT query using the PostgreSQL parameter statement_timeout?

postgres=# select version();
                                                           version
-----------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
(1 row)


postgres=# show statement_timeout;
 statement_timeout
-------------------
 0
(1 row)

postgres=# set statement_timeout='2s';
SET

postgres=# SELECT * FROM generate_series(1,50000000000);
ERROR:  canceling statement due to statement timeout
Enter fullscreen mode Exit fullscreen mode

#2 Example. How to cancel long running UPDATE query using the PostgreSQL parameter statement_timeout?

postgres=# create table abc(a bigint);
CREATE TABLE

postgres=# set statement_timeout=0;
SET

postgres=# insert into abc SELECT * FROM generate_series(1,10000000);
INSERT 0 10000000

postgres=# set statement_timeout='2s';
SET

postgres=# update abc set a=a+2;
ERROR:  canceling statement due to statement timeout
Enter fullscreen mode Exit fullscreen mode

#3 Example. How to cancel long running DELETE query using the PostgreSQL parameter statement_timeout?

postgres=# show statement_timeout;
 statement_timeout
-------------------
 2s
(1 row)

postgres=# select count(1) from abc;
  count
----------
 10000000
(1 row)

postgres=# delete from abc;
ERROR:  canceling statement due to statement timeout
postgres=# select count(1) from abc;
  count
----------
 10000000
(1 row)

postgres=#
Enter fullscreen mode Exit fullscreen mode

#4 Example. How to cancel long running INSERT query using the PostgreSQL parameter statement_timeout?

postgres=# select count(1) from abc;
  count
----------
 10000000
(1 row)

postgres=# insert into abc select * FROM generate_series(1,10000000);
ERROR:  canceling statement due to statement timeout
postgres=# select count(1) from abc;
  count
----------
 10000000
(1 row)
Enter fullscreen mode Exit fullscreen mode

#5 Example. How to cancel long running CREATE INDEX query using the PostgreSQL parameter statement_timeout?

postgres=# show statement_timeout;
 statement_timeout
-------------------
 2s
(1 row)

postgres=# create index abc_idx on abc(a);
ERROR:  canceling statement due to statement timeout
postgres=#
Enter fullscreen mode Exit fullscreen mode

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry đź•’

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay