mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Set A Statement Timeout Threshold For A Session as a postgres til
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
||||
|
||||
_1086 TILs and counting..._
|
||||
_1087 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -589,6 +589,7 @@ _1086 TILs and counting..._
|
||||
- [Send A Command To psql](postgres/send-a-command-to-psql.md)
|
||||
- [Set Inclusion With hstore](postgres/set-inclusion-with-hstore.md)
|
||||
- [Set A Seed For The Random Number Generator](postgres/set-a-seed-for-the-random-number-generator.md)
|
||||
- [Set A Statement Timeout Threshold For A Session](postgres/set-a-statement-timeout-threshold-for-a-session.md)
|
||||
- [Sets With The Values Command](postgres/sets-with-the-values-command.md)
|
||||
- [Shorthand Absolute Value Operator](postgres/shorthand-absolute-value-operator.md)
|
||||
- [Show All Versions Of An Operator](postgres/show-all-versions-of-an-operator.md)
|
||||
|
||||
48
postgres/set-a-statement-timeout-threshold-for-a-session.md
Normal file
48
postgres/set-a-statement-timeout-threshold-for-a-session.md
Normal file
@@ -0,0 +1,48 @@
|
||||
# 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.
|
||||
Reference in New Issue
Block a user