Skip to content

Latest commit

 

History

History
39 lines (30 loc) · 1.24 KB

set-statement-timeout-for-all-postgres-connections.md

File metadata and controls

39 lines (30 loc) · 1.24 KB

Set Statement Timeout For All Postgres Connections

The statement_timeout setting in PostgreSQL allows you to head off long running queries and migrations that could break your deploys and lock up your production tables.

This value can be set to a sensible default across all the connections your Rails app makes to PostgreSQL. To set it, open up your config/database.yml file and add a variables element to the default section.

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  variables:
    statement_timeout: 60000

That's 60 seconds in milliseconds. You can avoid the mental math by using a string argument with a unit such as s for seconds.

  variables:
    statement_timeout: '60s'

If you then execute a long running query, such as:

ActiveRecord::Base.connection.execute('select pg_sleep(62)')

It will terminate 2 seconds early because of the statement timeout.

source