Module A-12·40 min read

The difference between junior and senior is not SQL knowledge — it is having the right runbooks before the incident.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module 12 — Production Operations: Monitoring, Migration, and the Runbooks That Matter

What this module covers: The difference between a junior and a senior database engineer is not SQL knowledge — it is having the right runbooks before the incident, the right monitoring before the alert, and the right migration plan before the deployment. This module covers the operational stack: what to measure and how, connection pooling with PgBouncer, backup and restore, zero-downtime migration patterns, and the five runbooks that every team running Postgres in production needs to have written down before they need them.


The Monitoring Stack

You cannot operate what you cannot observe. Postgres exposes an exceptional amount of internal state through pg_stat_* views. The challenge is not access — it is knowing which metrics to collect, what thresholds to alert on, and what to do when they fire.

The Essential Views

sql

idle in transaction connections are the most dangerous. They hold open MVCC snapshots, pin OldestXmin, and block autovacuum from reclaiming dead tuples (Module 2 and 4). Any connection idle in transaction for more than 30 seconds deserves investigation.

sql
sql
sql
sql
sql

pg_stat_statements: Query Performance Over Time

sql
sql

Alerting Thresholds

MetricWarningCriticalAction
Connections used (% of max_connections)> 70%> 85%Check for connection leaks; increase PgBouncer pool
idle in transaction connections> 5> 20Kill offending connections; fix application code
Replication lag (bytes)> 100MB> 500MBInvestigate standby I/O; reduce write load
Dead tuple % on any table> 20%> 40%Run manual VACUUM; tune autovacuum for that table
XID age on any table> 1B> 1.5BRun VACUUM FREEZE immediately
Cache hit ratio< 99%< 95%Increase shared_buffers; optimize query working set
pg_wal directory size> 60% of partition> 80%Check inactive replication slots; increase disk
Long-running queries> 30s> 5minIdentify and kill; add timeout: statement_timeout
Lock wait time> 5s> 30sFind blocking query; investigate lock contention
ini

idle_in_transaction_session_timeout is one of the most important production settings that most teams don't have set. It automatically terminates connections that have been idle-in-transaction for too long — eliminating the entire class of "someone left a transaction open in their REPL and the table hasn't vacuumed in 6 hours" incidents.


Connection Pooling: PgBouncer

Every Postgres connection is a full OS process (Module 0). Above a few hundred connections, fork overhead and memory pressure become significant. PgBouncer is the standard connection pooler — a lightweight proxy that multiplexes many application connections onto a smaller number of Postgres connections.

PgBouncer Pooling Modes

Session mode:

  • One Postgres connection is assigned to an application connection for the entire session
  • Prepared statements, advisory locks, and SET parameters work correctly
  • Pool size = max simultaneous sessions ≈ similar to no pooler for connection count
  • Use when: you need prepared statements or session-level state

Transaction mode:

  • A Postgres connection is held only during a transaction; returned to the pool after COMMIT/ROLLBACK
  • One Postgres connection can serve many sequential application transactions
  • Breaks: prepared statements (not persisted across transactions), advisory locks, SET parameters, LISTEN/NOTIFY
  • Use when: high-throughput OLTP with short transactions and no prepared statements
  • Most PgBouncer deployments use this mode

Statement mode:

  • A Postgres connection is held only for a single statement
  • Breaks: multi-statement transactions (each statement gets its own connection → no transaction atomicity)
  • Rarely used in practice
ini

Sizing the Pool

The optimal number of Postgres connections is not "as many as possible" — it is the number that saturates your hardware without thrashing.

A well-known formula (from PgBouncer's own documentation):

optimal_pool_size ≈ (num_cores × 2) + num_effective_spindles

For a 16-core server with NVMe (treated as 1 effective spindle, by convention, even though it has no physical spindles):

optimal ≈ (16 × 2) + 1 = 33 connections

More connections than this means CPU context switching and memory pressure outweigh the benefit of additional concurrency. PgBouncer's default_pool_size = 30–50 for a modern server is a reasonable starting point.

sql

cl_waiting > 0 consistently means your pool is undersized for the workload. Either increase default_pool_size (requires more Postgres connections) or reduce application connection count.


Backup Strategies

pg_dump: Logical Backup

bash

Advantages: portable across Postgres versions, table-selective, works through logical replication boundaries.
Disadvantages: slow for large databases (must serialize every row), creates significant I/O load on the source, cannot be used for PITR.

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.