HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlCritical

Is it possible to limit timeout on Postgres server?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgreslimittimeoutpossibleserver

Problem

I set connection and command timeout to 10 minutes in my application (client side).

Than my application runs a simple query: SELECT pg_sleep(65)

On some servers it works fine, but other servers close the connection after 60 seconds.

Could this be some sort of PostgreSQL server configuration which limits timeouts and ignores my client settings?

Solution

Yes, it's possible

There are two settings mentioned in the docs (idle_in_transaction_session_timeout is new to version 9.6x)

  • statement_timeout (integer)




Abort any statement that takes more than the specified number of milliseconds, starting from the time the command arrives at the server from the client. If log_min_error_statement is set to ERROR or lower, the statement that timed out will also be logged. A value of zero (the default) turns this off.


Setting statement_timeout in postgresql.conf is not recommended because it would affect all sessions.

  • idle_in_transaction_session_timeout (integer)




Terminate any session with an open transaction that has been idle for longer than the specified duration in milliseconds. This allows any locks held by that session to be released and the connection slot to be reused; it also allows tuples visible only to this transaction to be vacuumed. See Section 24.1 for more details about this.


The default value of 0 disables this feature.

It's important to not set the statement_timeout in postgresql.conf unless you want amusement.

Here is an example of it working

SET statement_timeout = 10000;
SET
test=# SELECT pg_sleep(15);
ERROR:  canceling statement due to statement timeout

Code Snippets

SET statement_timeout = 10000;
SET
test=# SELECT pg_sleep(15);
ERROR:  canceling statement due to statement timeout

Context

StackExchange Database Administrators Q#164419, answer score: 71

Revisions (0)

No revisions yet.