When a Neighboring Dev Query Fills Your Production Disk: A PostgreSQL CDC WAL Saturation Postmortem
Activating Change Data Capture (CDC) via PostgreSQL logical replication (wal_level = logical) is normally low-overhead—provided your CDC consumers actively read and advance their replication slots.
So when an operations team alerted us that a production database host had abruptly climbed to 91% disk saturation (2.89 TB), every finger pointed immediately at our new CDC producer.
Yet our CDC consumer metrics looked completely healthy: offsets were advancing every few seconds, lag was near zero, and confirmed_flush_lsn was current. Why was PostgreSQL refusing to recycle gigabytes of Write-Ahead Log (pg_wal / pg_xlog) files?
The culprit wasn’t our CDC consumer at all—it was a forgotten UPDATE query stuck idle in transaction for 11–13 days on a co-located dev/test database sharing the same physical PostgreSQL instance.
The Architecture Blindspot: Cluster-Wide WAL and xmin Horizons
In architecture diagrams, our production OLTP database appeared as an isolated logical box, and our integration credentials were scoped strictly to that single production database. Without cluster-wide host visibility, we didn’t initially know that the production database and several *_test / development databases lived inside the same physical PostgreSQL instance.
In PostgreSQL, WAL retention and MVCC snapshot horizons (xmin) operate at the physical instance level—not per database:
Even when the production CDC replication slot has safely flushed every record downstream, PostgreSQL cannot recycle or delete WAL segments or dead tuples older than the oldest open transaction (backend_xmin / catalog_xmin) anywhere on the cluster.
One forgotten connection on a completely different database held the entire server’s storage hostage.
Emergency Diagnosis and Zero-Loss Recovery
Working alongside infrastructure DBAs with cluster-level privileges, we ran two diagnostic queries to isolate the lock and replication state:
-- 1. Inspect long-running idle transactions pinning xmin
SELECT
pid,
datname,
usename,
state,
xact_start,
state_change,
LEFT(query, 120) AS query_snippet
FROM pg_stat_activity
WHERE state = 'idle in transaction'
AND xact_start IS NOT NULL
ORDER BY xact_start ASC;
-- 2. Inspect logical replication slot WAL retention
SELECT
slot_name,
database,
active,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained_wal,
xmin,
catalog_xmin
FROM pg_replication_slots;
That immediately surfaced the 11–13-day-old idle in transaction sessions sitting inside the co-located test database. Once we verified the stuck PIDs were abandoned client sessions:
- Terminated Zombie Sessions: Executed
SELECT pg_terminate_backend(<pid>);on the stalled sessions. - Triggered Immediate WAL Recycling: Executed
CHECKPOINT;so PostgreSQL reclaimed all unpinned historical WAL segments right away.
Disk utilization dropped from 91% back below 50% within minutes—with zero production downtime and zero CDC data loss.
Five Guardrails We Adopted Afterward
- Strict Production Instance Isolation: Never host
PRDdatabases inside the same PostgreSQL cluster asDEV/TESTdatabases, especially oncewal_level = logicalis enabled. - Enforce Transaction & Connection Timeouts: Set
idle_in_transaction_session_timeoutat the database/role level (and use connection poolers like PgBouncer) so abandoned application transactions auto-abort within minutes instead of weeks. - Cross-Tenant Architecture Review Before Enabling CDC: Require host-level topology verification (checking what else lives on the physical server) before activating logical replication slots.
- Proactive Replication &
xminAge Alerts: Alert onpg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)and oldestxact_startage long before disk usage hits reactive 90% alerts. - Least-Privilege Monitoring Views: When application teams or data engineers only receive single-database access, provision a sanitized read-only telemetry feed exposing
pg_replication_slotsretained bytes and oldest activexminage across the host.
Key Takeaway
In PostgreSQL logical replication, a caught-up CDC consumer does not guarantee your WAL directory is free to shrink. Because xmin horizons and pg_wal segments are cluster-wide resources, a single untimed idle in transaction session in a neighboring test database can fill a multi-terabyte production disk just as fast as a dead consumer.
Co-authored with Gemini 3.8 Flash.