CDC and materialized views
We derive another representation from committed changes. Reading changed rows without a consistent capture position can miss or double data. We will trace CDC, snapshot handoff, keyed updates, and maintaining a materialized result.
#Section 21CDC follows accepted source changes
Polling a table’s updated_at field can miss equal timestamps, deletes, or changes committed around the polling boundary. Change data capture, or CDC, transfers committed data changes from a source into another processing path. A log-based implementation can interpret committed changes from the database’s write-ahead history.
The captured record needs identity, operation, source position, and enough before/after information for the intended transform. A delete must be a first-class change, not the absence of a row in the next scan. The consumer’s checkpoint records how far the target has durably applied those changes under its recovery protocol.
PostgreSQL logical decoding describes extraction from WAL and warns that changes can repeat after a crash. The generic consumer must therefore handle repeated effects safely. A source capture mechanism with ordered output does not make an unrelated target write exactly once automatically.
#Section 22Snapshot and stream need a handoff
A new warehouse begins with a full source copy, then follows CDC. If the snapshot ends and streaming begins at unrelated positions, changes between them can be lost or duplicated. A snapshot handoff relates a consistent source view to the exact point after which change capture supplies later changes.
The source can provide a snapshot tied to a log position, or another documented capture protocol can buffer changes during copying. Read the snapshot under that protocol, apply it, then consume the corresponding later change suffix. Stable keys and source versions handle any deliberate overlap. Guessing from copy start and end timestamps is not equivalent.
The copy can finish while the capture tail remains behind. Do not declare the new projection current solely because every snapshot row was written. Compare captured and applied positions under the handoff protocol, then publish the intended boundary. Deletes during copying must travel through the tail or another compatible reconciliation rule.
A slow CDC consumer can retain source log resources. That protects replay but can fill source storage. Monitor retained-log age and bytes, not only consumer process health. If the replay horizon is lost, restart from a fresh consistent snapshot under an explicit repair procedure rather than pretending the old checkpoint can resume.
#Section 23Upserts and deletes maintain keyed state
A source row’s value changes from 4 to 6. The analytical sum must remove 4 and add 6, not add another 6 beside the old contribution. An upsert inserts a missing keyed row or updates an existing one. A CDC projection needs a source key and version so retries and older changes do not overwrite newer state.
For the sum example, the net adjustment is . A delete retracts the prior stored contribution. The consumer may need the before image or a keyed record of the last contribution to compute that removal. If it discarded the old value, an incoming delete cannot reveal how much to subtract.
Not every aggregation supports easy deletion. A minimum can change when the smallest member is removed, requiring retained members or recomputation. An approximate sketch may not support arbitrary subtraction. Explain update and deletion behavior before claiming a streaming aggregate can maintain any query incrementally.
#Section 24Materialized views store prepared answers
The dashboard repeatedly asks for current runs by team. A materialized view stores a derived query result so readers can retrieve it without recomputing the full input each time. Maintenance can be batch refresh, incremental updates, or another engine-specific mechanism.
Our raw contributions produce red 7 and blue 3. A view keyed by team stores those totals, together with its progress or result version. A new red contribution updates red’s row. A correction retracts or recomputes according to the view’s supported maintenance rule. Read latency improves by moving work into view construction and update.
The view can lag or fail independently of source transactions. Define whether readers accept stale values, fall back to a bounded recomputation, or receive an unavailable response. A full-source fallback for every failed dashboard query can collapse the OLTP system that separation was meant to protect. Plan degradation rather than creating an accidental hot path.