diff --git a/README.md b/README.md index b12c361..a0f6ff7 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ smart people at [Hashrocket](http://hashrocket.com/). - [Count Records By Type](postgres/count-records-by-type.md) - [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md) - [Generate Series Of Numbers](postgres/generate-series-of-numbers.md) +- [Limit Execution Time Of Statements](postgres/limit-execution-time-of-statements.md) - [String Contains Another String](postgres/string-contains-another-string.md) - [Timestamp Functions](postgres/timestamp-functions.md) - [Toggling The Pager In PSQL](postgres/toggling-the-pager-in-psql.md) diff --git a/postgres/limit-execution-time-of-statements.md b/postgres/limit-execution-time-of-statements.md new file mode 100644 index 0000000..96bcc7f --- /dev/null +++ b/postgres/limit-execution-time-of-statements.md @@ -0,0 +1,26 @@ +# Limit Execution Time Of Statements + +You can limit the amount of time that postgres will execute a statement +by setting a hard timeout. By default the timeout is 0 (see `show +statement_timeout;`) which means statements will be given as much time as +they need. + +If you do want to limit your statements, to say, 1 second, you can set the +execution time like so + +```sql +> set statement_timeout = '1s'; +SET +> show statement_timeout; + statement_timeout +------------------- + 1s +(1 row) +``` + +Any queries taking longer than 1 second will be aborted with the following +message output + +``` +ERROR: canceling statement due to statement timeout +```