# Set A Statement Timeout Threshold For A Session

The `statement_timeout` variable is used to tell the PostgreSQL server that you want it to terminate statements (queries and transactions) that run past the specified threshold. This is a great way to [prevent runaway queries](https://blog.crunchydata.com/blog/control-runaway-postgres-queries-with-statement-timeout) in a production environment.

You can set this threshold with a `set` statement. It can take an integer argument of milliseconds. Here I set it to a timeout of 1 minute.

```sql
> set statement_timeout = 60000;
SET
> show statement_timeout;
 statement_timeout
-------------------
 1min
(1 row)
```

This will set the `statement_timeout` for the duration of the session. It won't effect other sessions.

You can also set the threshold with a string argument which allows you to include a unit of time. Here I set it to 30 seconds.

```sql
> set statement_timeout = '30s';
SET
> show statement_timeout;
 statement_timeout
-------------------
 30s
(1 row)
```

Now that the `statement_timeout` is set to `30s`, I can run a query that I know will exceed that threshold ([`pg_sleep`](https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-DELAY)).

```sql
> select pg_sleep(31);
ERROR:  canceling statement due to statement timeout
Time: 30001.997 ms (00:30.002)
```

After 30 seconds have passed, the Postgres server will interrupt the query.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/databases/postgres/commands/set-a-statement-timeout-threshold-for-a-session.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
