SET LOCAL Statement Timeout Guard

SET LOCAL statement_timeout caps every statement inside the current transaction only, so pooled connections return to their default limit after COMMIT.

SqlResilience

Code

snippet
BEGIN;
-- SET LOCAL applies only inside the current transaction: cap reporting queries with a timeout
SET LOCAL statement_timeout = '2s';

SELECT region, SUM(revenue) AS revenue
FROM sales
GROUP BY region;

-- A timed-out statement receives: ERROR: canceling statement due to statement timeout
-- The transaction is then aborted; the app should ROLLBACK and retry or degrade
ROLLBACK;

-- Contrast: session-level SET affects every later statement on the connection and must be reset by hand
SET statement_timeout = '30s';
RESET statement_timeout;

What the code does

At its core this is a single SELECT statement against sales.

Breaking the statements into their working parts: SUM aggregation; a GROUP BY that collapses matching rows into one output row per group.

Using it

The sales table is the working surface here.

Key statements

SELECT region, SUM(revenue) AS revenue
FROM sales
GROUP BY region;
SET LOCAL statement_timeout = '2s';
SET statement_timeout = '30s';

Interactive Tools for This Topic

FAQ

What relations does the SQL touch?
`sales` — referenced directly, no join needed.