Connection pooling timeout safety

From CarlosCaPe/octorato · View interactive version

Connection pooling timeout safety is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Connection Pooling & Timeout Safety

How to use this skill

Skill file

Connection Pooling & Timeout Safety

Source: PostgreSQL Best Practices

-- "Connection management and pooling", "Server configuration"

What

Configuring connection pool sizing, statement timeouts, and idle transaction

timeouts to prevent resource exhaustion and runaway queries on Azure

Flexible Server.

Why

PostgreSQL forks a process per connection. Without pooling and timeout

guardrails:

These are infrastructure-level settings, but Data Engineers must understand

them because:

How

Server Parameters (Azure Portal or CLI)

-- Query timeout (milliseconds). 0 = no limit.
statement_timeout = 30000          -- 30 seconds for app queries
-- Long migrations may need temporary increase:
-- SET LOCAL statement_timeout = '5min';

-- Kill idle-in-transaction sessions (milliseconds)
idle_in_transaction_session_timeout = 60000   -- 60 seconds

-- Slow query logging threshold (milliseconds)
log_min_duration_statement = 250              -- log queries > 250ms

Application Pool Sizing (Node.js / knex)

// knexfile.js -- recommended pool configuration
pool: {
    min: 2,
    max: 10,
    acquireTimeoutMillis: 10000,   // fail fast if pool exhausted
    idleTimeoutMillis: 30000,      // release idle connections
    reapIntervalMillis: 1000       // check for idle connections
}

Pool sizing rule of thumb from Best Practices:

max_pool_size = (core_count * 2) + effective_spindle_count

For Azure B1ms (1 vCPU, no spindles): max = (1 * 2) + 1 = 3

For Azure D2s_v3 (2 vCPU): max = (2 * 2) + 1 = 5

Temporary timeout override for migrations

-- Inside a migration transaction
BEGIN;
    SET LOCAL statement_timeout = '5min';

    -- Long-running DDL (e.g., adding column with default)
    ALTER TABLE public."LargeTable"
        ADD COLUMN "IsActive" boolean NOT NULL DEFAULT true;

COMMIT;
-- statement_timeout reverts to server default after COMMIT

Check current settings

SHOW statement_timeout;
SHOW idle_in_transaction_session_timeout;
SHOW max_connections;

-- Active connections by state
SELECT state, COUNT(*)
FROM pg_stat_activity
GROUP BY state
ORDER BY COUNT(*) DESC;

Monitor connection usage

SELECT
    usename,
    application_name,
    state,
    query_start,
    NOW() - query_start AS duration,
    LEFT(query, 80) AS query_preview
FROM pg_stat_activity
WHERE state <> 'idle'
ORDER BY duration DESC;

Decision Matrix

| Scenario | statement_timeout | idle_in_transaction |

|----------|------------------|---------------------|

| Web API queries | 30s | 60s |

| Migration scripts | 5min (SET LOCAL) | 60s |

| Bulk data loads | 10min (SET LOCAL) | 60s |

| One-off admin queries | Session-level SET | Not critical |

| pg_cron jobs | Default (30s) | Default (60s) |

When to Use

Where We Applied It

Related Skills

References

-- "Connection management and pooling" section

-- "Server configuration" section

Gotchas

statements within it -- a DO block with 10 ALTERs is ONE statement

a transaction, it is equivalent to SET (session-level)

you cannot SET it beyond the tier limit

breaks SET and prepared statements -- use SET LOCAL instead

the transaction -- the application must handle reconnection


Category: Strategy | Origin: Best Practices

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.