Operations
What to know when running PgCache in production: how it behaves across restarts, how its replication slot affects your origin database, how to warm a cold cache during a cutover, and how it fits into a highly-available setup.
Restart and Recovery
PgCache’s cache is ephemeral. The cache database is dropped and recreated every time PgCache starts, so after any restart the cache begins empty and repopulates as queries arrive. There is no on-disk cache state to preserve, migrate, or corrupt.
What this means operationally:
- A restart never serves stale data. The cache is rebuilt from scratch and resynchronized with the origin via logical replication, so a restart cannot resurrect outdated rows.
- The cache is cold immediately after a restart. Until queries repopulate it, requests are forwarded to the origin and the cache hit ratio climbs from zero back to its steady state. See Cache warm-up to pre-populate on startup.
Connection Failures
Losing a connection to the origin does not always cost the cache.
| Failure | What PgCache does | Cache afterwards |
|---|---|---|
| The replication connection drops, once or repeatedly | Forwards every query to the origin, reconnects to its slot and resumes the stream | Intact |
| The replication slot is dropped, or advanced by something else | Restarts the cache subsystem | Cold |
| The cache’s own origin connection drops (for example, every PgCache backend is terminated, or the origin restarts) | Restarts the cache subsystem, since a schema change may have gone unseen | Cold |
Client connections stay up through a cache restart: queries are forwarded to the origin until the cache is serving again. pgcache.cache.restarts_total counts these restarts.
The Replication Slot
PgCache keeps the cache consistent by consuming a logical replication slot on the origin. The slot’s lifecycle directly affects origin disk usage, so it’s worth understanding.
- On startup, PgCache creates the replication slot if it does not already exist, and recreates its publication.
- On a stop signal (SIGTERM, SIGINT, SIGHUP or SIGQUIT), PgCache drops both the slot and the publication before it exits. The origin can then reclaim any WAL the slot was retaining. That covers
docker stop,docker compose down, a Kubernetes pod stop andsystemctl stop. PgCache logs the signal and the process that sent it. A second signal while it is shutting down makes it exit at once, without finishing the cleanup. - On a startup that fails after the slot was created (for example, the cache database is unreachable), PgCache drops the slot and the publication before it exits.
- On a crash or hard kill (SIGKILL, OOM, instance termination, a stop grace period that runs out), the shutdown cleanup does not run, so the replication slot is left behind on the origin.
PgCache 0.9.1 and earlier cleaned up only on SIGINT: Ctrl-C, and the stop signal the Docker image declares, so docker stop was already clean. Under those versions SIGTERM (systemctl stop, kill, a Compose stop_signal override) leaves the slot behind; send SIGINT instead.
The schema-change event triggers and the pgcache schema are never removed at shutdown: every PgCache on the database shares them. See Removing PgCache from an origin.
Why a leftover slot matters
A logical replication slot pins WAL on the origin — PostgreSQL will not remove WAL segments the slot has not yet confirmed. While PgCache is running and consuming the slot, the retained WAL is bounded by replication lag. But if PgCache crashes and stays down, the orphaned slot keeps pinning WAL, and the origin’s disk usage grows unbounded until either:
- PgCache restarts and resumes consuming the slot (it reuses the existing slot by name), or
- the slot is dropped manually on the origin.
Mitigation:
-
Monitor
pgcache.cdc.lag_bytes, and watch the slot directly on the origin:SELECT slot_name, active, pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) AS retained_bytes FROM pg_replication_slots; -
Alert on a slot that is
active = falsewith growingretained_bytes— that means PgCache is down while its slot is still pinning WAL on the origin. -
When you permanently decommission a PgCache instance (rather than restart it), remove its slot from the origin so the WAL can be reclaimed. See the next section.
Removing PgCache from an origin
pgcache --cleanup (in the Docker image, the cleanup command) drops the slot and publication of an instance that is no longer running, using the same origin and CDC name settings the instance ran with:
docker run --rm pgcache/pgcache cleanup \
--upstream postgres://user:password@your-db-host:5432/myapppgcache cleanup: origin user@your-db-host:5432/myapp
dropped replication slot pgcache_slot
dropped publication pgcache_pub
kept schema-change event triggers and schema pgcache (shared by every pgcache on this database; --purge removes them)
Cleanup finished.
It removes nothing and exits with status 1 while a PgCache is still streaming from the slot. Add --purge to also drop the event triggers and the pgcache schema; do that only when no other PgCache uses the database, since each one refuses to start without them.
The same by hand:
SELECT pg_drop_replication_slot('your_slot_name');
DROP PUBLICATION IF EXISTS your_publication_name;
-- Only when no PgCache uses this database any more:
DROP EVENT TRIGGER IF EXISTS pgcache_ddl_command_end;
DROP EVENT TRIGGER IF EXISTS pgcache_sql_drop;
DROP SCHEMA IF EXISTS pgcache CASCADE;Cache Warm-Up
By default (admission_threshold = 1) a query is cached the first time it is seen, so the cache warms naturally as traffic arrives — the only cost is one forwarded request per distinct query while the cache is cold.
For a planned cutover where you want hot paths cached before you shift traffic, pre-populate them on startup with pinned queries:
# Pre-populate and protect these at startup
pinned_queries = [
"SELECT * FROM settings",
"SELECT * FROM products WHERE active = true",
]
# Shorthand to pin entire tables (expands to SELECT * FROM <table>)
pinned_tables = ["categories", "feature_flags"]
Pinned queries are populated at startup, protected from eviction, and automatically re-admitted after a CDC invalidation. See Configuration for the full reference. PgCache does not currently seed the cache automatically from pg_stat_statements or query logs.
High Availability
PgCache is a stateless proxy in front of your origin database — all durable data lives on the origin, and the cache holds only a consistent, reconstructable copy.
- Each instance is independent. The cache is neither shared between instances nor persisted, so an instance can be replaced at any time; its replacement warms from scratch.
- On instance failure, route traffic back to the origin (or to another PgCache instance) while it restarts. Because the cache is consistent and ephemeral, failover never risks serving stale data.
- Running multiple instances for redundancy is supported. Give each instance a distinct
slot_nameandpublication_nameso their replication slots don’t collide on the origin.
For where PgCache sits relative to PgBouncer or RDS Proxy, see Connection Pooler Compatibility.