Database Pipelines — Part 5/5

Parts 1–4 covered plans, storage, transactions, and caches. None of that helps if every app thread opens its own backend until max_connections collapses. This final part covers connection pooling, then stitches the series into one end-to-end request path. Examples use PostgreSQL and poolers such as PgBouncer.

This is Part 5 of 5 in the Database Pipelines series.

Why pool at all

Many clients through a pooler onto fewer database backends

Each PostgreSQL backend is a process with memory and scheduling cost. Unbounded connections waste RAM, increase context switches, and hit hard caps on managed instances. A pooler lends a bounded set of live backends to many clients.

The Fairfax SQL does not change; pooling changes whether one thousand concurrent callers create one thousand backends or fifty.

Placement:

  • External pooler (PgBouncer, cloud proxies): global cap in front of the database;
  • In-process pool (JDBC, SQLAlchemy, pgx): per instance only—N instances × pool size must still fit;
  • Both when fleets scale horizontally.

Pool modes

Session, transaction, and statement pooling tradeoffs

Mode Backend held Watch-outs
Session Whole client session Simplest semantics; weakest multiplexing
Transaction Until COMMIT / ROLLBACK Temp tables, session SET, some prepared-statement setups, LISTEN, cross-txn advisory locks
Statement Per statement Breaks multi-statement transactions

Web APIs often want transaction pooling when each request is a clean transaction. Session-sticky features need session pooling or a separate pool. Check driver + pooler docs before assuming ORMs survive transaction mode unchanged.

Sizing sketch

  1. Estimate peak concurrent queries you want running—not idle HTTP workers worldwide;
  2. Set pool size near that concurrency;
  3. Keep total backends under max_connections with headroom for admin, migrations, ETL;
  4. More backends than runnable CPU rarely increases throughput for heavy Fairfax reports—it increases stampede damage. Pair with statement_timeout and pool wait metrics.

End-to-end: one request through the pipeline

End-to-end: pool to plan to snapshot to execute to cache to storage/WAL to response

Here is how Parts 1–5 meet on a single GET /fairfax-paid-orders-style call:

  1. Pool / admission. The app borrows a backend from an in-process pool or PgBouncer. If the pool is exhausted, the request waits or fails fast instead of opening another OS connection to PostgreSQL. This is intentional admission control.
  2. SQL parse, bind, plan (Part 1). Text becomes a parse tree, names bind to customers / orders, the planner builds a logical plan and a physical plan (index vs seq scan, hash vs nested loop). EXPLAIN would show intent; we are past that—execution will follow the chosen plan.
  3. Transaction snapshot (Part 3). Explicitly or via autocommit, the statement runs under an isolation level. Read Committed takes a statement snapshot; a surrounding REPEATABLE READ transaction would freeze a longer view. Locks are taken only as the chosen operators and mode require—not “2PC” unless a transaction manager prepared multiple participants.
  4. Execute operators (Part 1). Nested loops, hash joins, filters on city, status, and created_at run as plan nodes.
  5. Buffer / OS cache (Part 4). Heap and index pages are pinned from shared buffers or fetched through the OS page cache into the pool.
  6. Storage / WAL (Part 2). Pure reads of the Fairfax report do not append commit records; they still read pages that earlier writes made durable via WAL and checkpoints. If this request had been a UPDATE … status = 'paid', the write path would log WAL before depending on that change for durability, dirty pages asynchronously, and acknowledge commit without flushing every page.
  7. Response. Rows return to the app; the pool releases the backend (end of transaction in transaction mode, or end of session in session mode). App-level cache-aside (Part 4) may store a TTL’d fragment—invalidated on later writes—without pretending to be the database.

That loop is the “database pipeline” in operational order: admit → plan → snapshot → execute → cache → store/WAL → respond.

Operational checklist

  • Prefer short transactions so transaction-mode pooling releases backends quickly.
  • Set statement_timeout and idle-in-transaction limits.
  • Monitor pool wait time separately from query time.
  • Serverless bursts need an external pooler or cloud pooling; per-instance pools alone will not save the primary.

Series close

  1. Query planning — SQL → logical/physical plans, joins, indexes;
  2. Storage — B+ trees vs LSM, WAL, checkpoints;
  3. Transactions — MVCC, locks, isolation, 2PC;
  4. Caching — buffers, cache-aside, invalidation, stampedes;
  5. Pooling — bounded backends and the end-to-end path above.

Write clear SQL, respect transactions, expect pages and caches to dominate runtime, and never attach unbounded clients straight to the engine.

Limits

No turnkey PgBouncer config, no single “pool size = cores” formula, and no deep MySQL ProxySQL tour. Size from measurement.

References

About the author

I write about Applied AI, MCP, Azure AI Search, and RAG. Follow me on Dev.to and Hashnode for more.