Highlights

Contents

Part VI
06

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.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1OLTP vs OLAP2Batch processing3Stream processing4Event time, windows,watermarks5ETL, ELT, warehouses, andlakes6CDC and materializedviews7Checkpoints andexactly-once8Spark and Flink9Case study: score toreport

#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.

CDC READS THE COMMIT LOG, SO DELETES ARRIVE AS CHANGES TOOCDC READS THE COMMIT LOG, SO DELETES ARRIVE AS CHANGES TOOsourceins 41upd 41del 39ins 42change log with positionsderived targeta nightly copy would miss row 39 silently; CDC sees "del 39"
Figure 17.21Illustrative log-based CDC path. The capture contract identifies committed operations and their replay positions.

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.

SNAPSHOT AT POSITION P, THEN REPLAY ONLY CHANGES AFTER P: NO GAP, NO OVERLAPSNAPSHOT AT POSITION P, THEN REPLAY ONLY CHANGES AFTER P: NO GAP, NO OVERLAPsnapshot = all changes ≤ Preplay changes > PPthe source must retain changes after P while the snapshot loads
Figure 17.22Illustrative consistent capture relationship. The source snapshot and stream boundary are compatible by protocol.

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 −4+6=2-4+6=2. 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.

AN UPDATE 4 → 6 ADDS +2; A LATER DELETE REMOVES 6, SO THE CONSUMER MUST REMEMBER ITAN UPDATE 4 → 6 ADDS +2; A LATER DELETE REMOVES 6, SO THE CONSUMER MUST REMEMBER ITstoredkey k: 4after imagekey k: 6derived change−4 + 6 = +2delete laterremove 6keep the last contribution per key, or capture before-images
Figure 17.23Computed illustrative CDC projection update. The delete rule requires the retained prior contribution.

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.

RAW 2 + 4 + 1 BECOMES THE VIEW ROW red = 7, STAMPED WITH ITS SOURCE POSITIONRAW 2 + 4 + 1 BECOMES THE VIEW ROW red = 7, STAMPED WITH ITS SOURCE POSITIONa: red 2c: red 4d: red 1b: blue 3team_totalsred = 7blue = 3as of pos 842, transform v7a fast read hides the upstream work that keeps it current
Figure 17.24Computed illustrative materialized totals. Metadata explains which source history and transformation produced them.

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.