pgcoalesce: Merge Concurrent Postgres Reads
Résumé
Pixel’s DEV Community announcement introduces pgcoalesce, an experimental Postgres wire-protocol proxy that merges concurrent, same-shape primary-key reads into one query while fetching every result live from Postgres. It covers the safety model, benchmarks against PgBouncer, and a connection-string-only setup, noting the project has no production users yet.
DEV Community
pgcoalesce: a Postgres proxy that merges concurrent point reads
pgcoalesce is a Postgres wire-protocol proxy that holds concurrent primary-key reads of the same shape for a couple of milliseconds, runs them as one query, and fails closed when it cannot prove that is safe. It is not a cache: every answer is read from Postgres at that moment. The only application change is the connection string. Status: 0.3.0, experimental — benchmarked on one machine, no production users yet. License: Apache-2.0. Repository: github.com/kobima6813/pgcoalesce. This write-up covers how the proxy works, the numbers we measured against PgBouncer, the safety model, and how it sits next to other tools in this space. We have not published a benchmark against any product except PgBouncer. Competitor features and prices are from their public docs, checked on 15 November 2026. The problem Serverless, edge and API backends often fire many tiny reads at the same moment: SELECT * FROM users WHERE id = $1
Each statement is cheap on its own. What adds up is Postgres's per-query overhead: bind, plan or lock a cached plan, snapshot, execute, and return a result, once per client. A pooler such as PgBouncer multiplexes connections, but it still runs every statement as its own query. Under a few thousand identical-shaped point reads per second, that per-query work saturates a small instance, not the data pages themselves. Application batchers such as DataLoader solve this inside one request, and only if you change the code. A read cache can absorb the load, but then the answer may be stale. The gap we wanted: merge concurrent point reads across clients, keep every answer live from Postgres, and leave the application unchanged. How it works pgcoalesce sits in front of Postgres like a pooler. When several clients, in the same result-affecting context, issue the same shape of point read with different keys, it waits for an adaptive window (about 2 ms by default; roughly 0 when there is no partner) and then runs one statement: SELECT k.ord, x.
FROM unnest($1::int8[]) WITH ORDINALITY k(v, ord)
CROSS JOIN LATERAL (
) x
Each client receives exactly its own rows, in the format it asked for (text or binary, simple or extended protocol). Writes, unrecognised SQL, and anything that is not a catalog-verified point read pass through unchanged. At low load the proxy almost never waits; a request with no partner is forwarded immediately. At higher load the window opens just enough to find partners, so the latency cost tracks the configured cap: about +1.8 ms at p50 with a 2 ms window, about +3.4 ms with 5 ms. A second path recognises the PostgREST / Supabase transaction template (BEGIN; set_config(...); one point SELECT; COMMIT) and merges whole transactions only when the fingerprint — role, claims, headers, path and watched GUCs — is identical. Benchmarks All figures in this section are from one box, on 15 November 2026. Unless noted, they are medians of three runs, and a single run moves around by roughly ±20%. Rig. Postgres 17.12 in a cgroup limited to 1 CPU and 256 MB (shared_buffers=64MB), 1 million row users table (172 MB, against 64 MB of shared_buffers and the 256 MB limit). 500 client connections, open-loop load, pgx v5. Baseline: PgBouncer 1.24.1 in transaction mode with pool_size=4 (pool_size=20 was worse). Database CPU is measured from the cgroup. The 5,000 req/s table and the lower-rate table are medians of three runs. Runs are short: 10,000 requests in about 2 seconds at 5,000 req/s, and 4 seconds at the lower rates. Raw data is in results/. The 5,000 req/s prepared / uniform / 2 ms cell was measured again in two later sessions, one of them on the code that includes the pre-launch fixes: 18.4 and 18.3 µs per request, against 60.2 and 59.2 µs for PgBouncer. Database CPU per request at 5,000 reads/s Driver mode Keys PgBouncer pgcoalesce 2 ms pgcoalesce 5 ms p50 / p99 (ms): PgBouncer → 2 ms → 5 ms Named prepared, binary Uniform 57.8 µs 17.5 µs (−70%) 12.3 µs (−79%) 0.59 / 1.7 → 2...

Powered by Readybot.io*