PostgreSQL cache invalidation: the late-fill race
Walk through a cache-aside race where an old read refills a deleted key after commit. Understand publication fencing, snapshots, rollback and request-local caches.
Read articleRead rows by primary key with SQL mget or RESP. PostgreSQL writes automatically invalidate affected cache entries.
Explicit SQL API · PostgreSQL 14–18 · Open source
Measured locally · 15 Sep 2026
839,678requests/s · RESP MGET
3.31× the prepared SQL throughput
in this single-key comparison
An eligible hit returns the stored row. A miss or bypass reads the source table. Your ordinary SELECT queries keep their existing path.
-- Attach your table once.
SELECT local_cache.attach_table('public.items');
-- Read rows by primary key.
SELECT local_cache.mget(
'public.items', ARRAY[42, 7]::bigint[]
);
Up to 1,024 keys per call. Order, duplicates, and NULL positions are preserved.
INSERT, UPDATE, and DELETE invalidate affected entries.
Allocate the shared row cache at PostgreSQL startup.
Ineligible reads use the source table under PostgreSQL visibility rules.
Node.js and Go. Prepared SQL, SQL mget, and RESP MGET.
One runner uses the same keys, batch sizes, connection counts, duration, and result checks. Compare throughput, latency, and PostgreSQL CPU and memory on your machine.
Run the comparisonChoosing a cache? Compare PostgreSQL pages, rows, materialized views, and Redis.
Clone the repository and start a disposable PostgreSQL server with sample rows. Docker builds the extension and keeps the demo separate from your databases.
git clone https://github.com/profundium/pg_local_cache.git
cd pg_local_cache
docker compose -f examples/compose.yaml up --build --wait
Read sample rows and check cache hits. Already cloned the repository? Run the last command from its root. For an existing server, use the installation guide.
Installing on an existing server requires a PostgreSQL restart.
Walk through a cache-aside race where an old read refills a deleted key after commit. Understand publication fencing, snapshots, rollback and request-local caches.
Read articleDesign a fair PostgreSQL row-cache comparison using prepared SQL, SQL mget and RESP MGET. Separate warm reads, misses, batch sizes, writes and client costs.
Read articleReplace N+1 primary-key queries while preserving duplicate IDs, input order, NULL positions and missing rows. Compare ANY, WITH ORDINALITY and SQL mget.
Read articleNo. PostgreSQL caches database pages. This extension separately caches serialized whole rows by primary key. See the read-path comparison.
No. Only explicit local_cache.mget calls use the SQL cache. Your existing queries keep PostgreSQL's normal execution path.
No. The optional RESP2 endpoint is limited. There is no general-purpose Redis command set, TTL, pub/sub, or distributed coordination. Compare the PostgreSQL and Redis cache-aside paths.
The invalidation guide includes a two-session test. The runnable example checks uncommitted writes, read-your-writes, rollback, and committed updates.