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.
Code
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
SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.
FAQ
- What relations does the SQL touch?
- `sales` — referenced directly, no join needed.
Report a problem with this exampleOpens a pre-filled issue on GitHub.