
PostgreSQL Temporal Tables for Real‑Time CDC: Reducing Latency and Cost
Learn how PostgreSQL temporal tables cut CDC latency and storage spend while keeping SLOs intact – a proven pattern for fast‑moving data pipelines.
PostgreSQL Temporal Tables for Real‑Time CDC: Reducing Latency and Cost
PostgreSQL temporal tables for real‑time CDC: a production incident revealed that our change‑data‑capture latency spiked to 45 seconds, violating a 5‑second SLO and causing downstream alerts to miss market‑moving events. The root cause was a growing history table that accumulated millions of rows without pruning, forcing Debezium to scan the entire WAL on each poll.\n\n## Why temporal tables solve CDC latency spikes\nTemporal tables (system‑versioned tables) keep a compact history of row changes inside the same table, exposing FOR SYSTEM_TIME AS OF queries. By storing only the delta and indexing the valid_from timestamp, the CDC engine can read the newest version directly, avoiding full‑table scans.\n\n## How to enable system‑versioned tables in PostgreSQL 15+\nsql\nCREATE TABLE orders (\n order_id BIGINT PRIMARY KEY,\n status TEXT NOT NULL,\n amount NUMERIC(10,2),\n created_at TIMESTAMPTZ NOT NULL DEFAULT now()\n);\n\nALTER TABLE orders\n ADD PERIOD FOR SYSTEM_TIME valid_period;\n\nCREATE TABLE orders_history (\n LIKE orders INCLUDING ALL,\n valid_from TIMESTAMPTZ NOT NULL,\n valid_to TIMESTAMPTZ NOT NULL\n);\n\nCREATE TRIGGER orders_versioning\nAFTER INSERT OR UPDATE OR DELETE ON orders\nFOR EACH ROW EXECUTE FUNCTION versioning_trigger('orders_history', 'valid_period');\n\nThe trigger copies the old row into orders_history with timestamps, while the main table always reflects the latest state.\n\n## Integrating with Debezium and dbt\nOur Kafka‑Debezium‑dbt stack already captures the orders WAL. Switching to the temporal model required only a connector config change: set snapshot.mode=never and include.schema.changes=true. Debezium now streams only the latest row version, cutting the poll interval from 30 seconds to sub‑second.\n\n## Cost impact and storage savings\nBefore the change the orders_history table grew at ~150 GB/month, incurring S3 egress charges when exported to the lakehouse. After pruning history older than 90 days (using a daily DELETE FROM orders_history WHERE valid_to < now() - interval '90 days') storage fell to 45 GB/month – a 70 % reduction.\n\n## Monitoring and SLO enforcement\nWe added a Prometheus metric cdc_poll_latency_seconds to the Debezium connector and set an alert at 5 seconds. After deployment the 99th‑percentile latency stabilized at 0.8 seconds, keeping our downstream alerting pipeline within the agreed SLO.\n\n## Lessons learned\n- Temporal tables simplify CDC logic; no external changelog tables are needed.\n- Keep history windows short; older data can be archived to cold storage with a separate ETL job.\n- Validate connector settings after schema changes – a stale snapshot.mode caused a brief regression in our test environment.\n\nFor more details on versioning triggers, see the official PostgreSQL docs and our open‑source implementation in the versioning_trigger function.\n\n---\n\nInternal link: Dynamic filtering in Trino\nProject link: Kafka‑Debezium‑dbt