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
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
| 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
- Estimate peak concurrent queries you want running—not idle HTTP workers worldwide;
- Set pool size near that concurrency;
- Keep total backends under
max_connectionswith headroom for admin, migrations, ETL; - More backends than runnable CPU rarely increases throughput for heavy Fairfax reports—it increases stampede damage. Pair with
statement_timeoutand pool wait metrics.
End-to-end: one request through the pipeline
Here is how Parts 1–5 meet on a single GET /fairfax-paid-orders-style call:
- 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.
- 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).EXPLAINwould show intent; we are past that—execution will follow the chosen plan. - Transaction snapshot (Part 3). Explicitly or via autocommit, the statement runs under an isolation level. Read Committed takes a statement snapshot; a surrounding
REPEATABLE READtransaction 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. - Execute operators (Part 1). Nested loops, hash joins, filters on
city,status, andcreated_atrun as plan nodes. - Buffer / OS cache (Part 4). Heap and index pages are pinned from shared buffers or fetched through the OS page cache into the pool.
- 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. - 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_timeoutand 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
- Query planning — SQL → logical/physical plans, joins, indexes;
- Storage — B+ trees vs LSM, WAL, checkpoints;
- Transactions — MVCC, locks, isolation, 2PC;
- Caching — buffers, cache-aside, invalidation, stampedes;
- 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
- PostgreSQL Documentation: Connection Settings —
max_connections - PostgreSQL Documentation: Client Connection Defaults —
statement_timeout - PgBouncer: Features / pool modes
- PgBouncer: Configuration
About the author
I write about Applied AI, MCP, Azure AI Search, and RAG. Follow me on Dev.to and Hashnode for more.




