Search code examples
sqlpostgresqlperformancelimit

limit query time in the client side of PostgreSQL


I'm trying to query the PostgreSQL database, but this is a public server and I really don't want to waste a lot of CPU for a long time.

So I wonder if there is some way to limit my query time for a specific duration, for example, 3/5/10 minutes.

I assume that there is syntax like limit but not for results amount but for query duration.


Solution

  • Set statement_timeout, then your query will be terminated with an error if it exceeds that limit.

    According to this documentation:

    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.