diff --git a/README.md b/README.md index f11c57f..2956b64 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_874 TILs and counting..._ +_875 TILs and counting..._ --- @@ -459,6 +459,7 @@ _874 TILs and counting..._ - [Prepare, Execute, And Deallocate Statements](postgres/prepare-execute-and-deallocate-statements.md) - [Pretty Print Data Sizes](postgres/pretty-print-data-sizes.md) - [Pretty Printing JSONB Rows](postgres/pretty-printing-jsonb-rows.md) +- [Prevent A Query From Running Too Long](postgres/prevent-a-query-from-running-too-long.md) - [Print The Query Buffer In psql](postgres/print-the-query-buffer-in-psql.md) - [Renaming A Sequence](postgres/renaming-a-sequence.md) - [Renaming A Table](postgres/renaming-a-table.md) diff --git a/postgres/prevent-a-query-from-running-too-long.md b/postgres/prevent-a-query-from-running-too-long.md new file mode 100644 index 0000000..3ad9d00 --- /dev/null +++ b/postgres/prevent-a-query-from-running-too-long.md @@ -0,0 +1,28 @@ +# Prevent A Query From Running Too Long + +A number of different factors can effect how long a query takes to run. +Certainly the size of a table and the complexity of the query play a big role. +Locking can really slow a query down by making it wait to get started. A series +of competing queries that induce table locking can grind things to a halt. + +If you don't want queries in a particular connection being allowed to wait or +run too long, you can set a timeout. + +```sql +set statement_timeout to '500'; +``` + +That will ensure that any statement run in that connection will be terminated +if it takes longer than 500ms. + +You can also specify a unit: + +```sql +set statement_timeout to '15s'; +``` + +That will enforce statement timeout of 15 seconds. + +See the +[docs](https://www.postgresql.org/docs/current/runtime-config-client.html) for +more details.