Start CDC replay before deferred index creation

Open
#10 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

  • #11 by @tbarbugli — closed without merging

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
go

Research direction

Begin by tracing the existing COPY, CDC replay, index and constraint finalization, and durable resume-state paths; use PR #9 as the referenced entry point for out-of-order durable receipts. Done requires coordinated replay and deferred-index pipelines, safe crash recovery, preserved transaction ordering, and end-to-end proof that cutover data and indexes match.

Written by the indexing model from the issue text.

Description

Problem

pgmigrate currently captures CDC throughout schema restore, COPY, index/constraint creation, and target vacuuming, but does not start applying it until all of that work finishes. On large migrations, secondary index builds can therefore produce a very large CDC backlog and a long catch-up phase even though the copied tables are already usable for replay.

Proposed flow

capture CDC ─────────────────────────────────────────────>
pre-data → COPY → replay-critical indexes → CDC replay ──>
                                        └→ deferred indexes concurrently

The first iteration should start replay after all tables finish COPY, but before secondary index and deferred schema work completes.

1. Classify indexes before COPY

Inventory and persist index definitions early, dividing them into:

  • Replay-critical: primary-key and replica-identity indexes required for efficient UPDATE/DELETE replay.
  • Deferred: secondary indexes, non-identity unique indexes, and other post-data objects.

Partitioned indexes need leaf indexes built and validated before their parent index is attached.

2. Establish a replay-ready barrier

After all COPY transactions commit:

  1. Build replay-critical indexes using normal CREATE INDEX while replay is still stopped.
  2. Attach required primary-key and replica-identity constraints.
  3. Run ANALYZE on copied tables.
  4. Persist a replay-ready marker.

Replay must not begin without the key indexes, otherwise indexed updates and deletes can degrade into table scans.

3. Run replay and finalization concurrently

After the replay-ready barrier, supervise two pipelines together:

  • CDC replay/catch-up.
  • Deferred schema and index finalization.

A failure in either pipeline cancels the other. Reaching the initial CDC boundary is not sufficient to enter follow; required schema finalization must also be complete.

4. Make deferred index creation replay-safe

Once replay is active:

  • Use CREATE INDEX CONCURRENTLY for deferred indexes.
  • Use a small, independent index worker pool.
  • Persist pending, building, valid, and completed state.
  • On restart, identify owned invalid indexes and safely remove/retry them.
  • Never run an ordinary long CREATE INDEX against a table receiving replay.
5. Handle constraints deliberately
  • Primary-key and replica-identity constraints must exist before replay.
  • Foreign keys can be restored as NOT VALID.
  • Do not validate foreign keys while out-of-order replay may expose temporary referential inconsistencies.
  • Validate at a canonical replay boundary or during final drain.
  • Preserve the existing replica-role behavior for triggers and rules.
6. Prioritize replay over background work
  • Reserve target connections for replay.
  • Default deferred index concurrency to one.
  • Stop admitting new index builds when CDC lag exceeds a threshold.
  • Resume index work when lag recovers.
  • Treat vacuum and additional analysis as lower-priority background work.
7. Persist independent resume state

Track these milestones independently:

  • COPY complete.
  • Replay-critical indexes complete.
  • Replay started.
  • Deferred indexes complete.
  • Constraints validated.
  • Initial CDC boundary reached.

Resume must reconstruct both concurrent pipelines from durable state. follow and cutover remain gated on all required milestones.

Follow-up: per-table COPY handoff

If COPY itself remains the dominant source of backlog, allow each table to become replay-ready independently:

table A: COPY → key index → replay-ready
table B: COPY ─────────→ key index → replay-ready

A transaction is runnable only when every relation it touches is ready. This requires readiness-aware, disk-bounded scheduling and the out-of-order durable receipt mechanism from PR #9. Crashes during the COPY phase should continue resetting the whole base copy so the exported-snapshot correctness model is preserved.

Acceptance criteria

  • Target apply progress advances before deferred secondary indexes finish.
  • No normal long-running index build blocks active replay.
  • Crash recovery safely reconciles interrupted concurrent index builds.
  • Multi-table transactions remain atomic and ordered per relation.
  • Partitioned, keyless, foreign-key, unique-index, and update-heavy workloads remain correct.
  • follow begins only after both catch-up and required schema finalization complete.
  • End-to-end tests prove source/target data and final index definitions match after cutover.
  • Benchmarks show a material reduction in peak CDC backlog and catch-up duration.
Dominant language
Go
Stars
6
Forks
1
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from GetStream/pgmigrate

All issues in GetStream/pgmigrate

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.