Set Statement Timeout to Avoid Runaway Queries

In YSQL establishing a statement timeout via the statement_timeout parameter restricts queries from exceeding a designated duration.

This timeout can be configured at the universe, database, user, or session level.

It’s advisable to initially set a timeout at the universe or database level, then subsequently adjust it for specific users or sessions requiring an extended execution time.

Example:

				
					yugabyte=# CREATE DATABASE some_database;
CREATE DATABASE

yugabyte=# ALTER DATABASE some_database SET statement_timeout = '10s';
ALTER DATABASE

yugabyte=# CREATE USER lucy WITH LOGIN;
CREATE ROLE

yugabyte=# \! ysqlsh -h 127.0.0.1 -d some_database -U lucy -c "SELECT pg_sleep(20)";
ERROR:  canceling statement due to statement timeout
				
			
We can override the statement_timeout database setting at the session level in order to run query longer as needed…
				
					yugabyte=# \! ysqlsh -h 127.0.0.1 -d some_database -U lucy -c "SET statement_timeout = '30s';" -c "SELECT pg_sleep(20);"
SET
 pg_sleep
----------

(1 row)
				
			

Have Fun!

Scoreboard at a little park in Knoxville, TN.