Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:2ndQuadrant Pglogical Initial Data Synchronization

From Leeroopedia
Knowledge Sources
Domains Database, Replication, Data_Transfer
Last Updated 2026-10-13 00:00 GMT

Overview

Process of bootstrapping a subscriber with the initial state of the provider's data (schema and/or table contents) before transitioning to streaming replication, using a multi-phase state machine with consistent snapshots.

Description

When a new subscription is created, the subscriber must obtain an initial copy of the data before it can begin applying streaming changes. This is the initial synchronization phase. The challenge is to perform this copy consistently: the snapshot of the data must correspond to a specific WAL position, so that streaming replication can resume exactly where the initial copy left off, without gaps or overlaps.

Initial synchronization solves the "cold start" problem of logical replication: without an initial copy, the subscriber has no base data, and streaming changes would fail on the first INSERT that references rows that don't exist yet.

The synchronization process uses a multi-phase state machine with these states:

  • INIT: slot created, snapshot exported on provider
  • STRUCTURE: schema dumped via pg_dump and restored via pg_restore (pre_data section)
  • DATA: table data copied via COPY protocol using the exported snapshot
  • SYNCWAIT: sync worker signals apply worker that data copy is complete
  • CATCHUP: apply worker processes streamed changes up to the sync point
  • SYNCDONE: sync worker confirms apply has caught up
  • READY: normal streaming replication begins

For individual table re-synchronization, a per-table sync worker handles the COPY while the main apply worker continues processing other tables.

Usage

Use initial synchronization when:

  • Setting up a new subscriber from scratch (schema + data copy)
  • Adding a new table to an existing subscription (alter_subscription_synchronize)
  • Re-synchronizing a table that fell behind or was corrupted
  • The subscription was created with synchronize_structure and/or synchronize_data enabled

Skip initial sync when the subscriber was created via physical base backup (data already present), or when only schema is needed.

Theoretical Basis

The key insight is snapshot consistency: the initial data copy must use a snapshot that corresponds to a known WAL position, so that streaming replication can resume at that exact point.

Exported snapshot mechanism:

When a logical replication slot is created, PostgreSQL exports a consistent snapshot. All data copied using this snapshot will be consistent with the WAL position at which the slot was created.

1. Create replication slot on provider → exports snapshot S at LSN L₀
2. Copy schema using snapshot S (pg_dump --snapshot=S)
3. Copy data using snapshot S (COPY ... (SNAPSHOT S))
4. Begin streaming from LSN L₀
5. Apply changes until caught up to current WAL position
6. Transition to continuous streaming replication

State machine for subscription sync:

INIT→STRUCTURE→DATA→SYNCWAIT→CATCHUP→SYNCDONE→READY

The states SYNCWAIT, CATCHUP, and SYNCDONE implement a two-phase handoff between the sync worker and the apply worker, ensuring no changes are lost or duplicated during the transition.

Practical Guide

Synchronization Modes

  • Full sync (synchronize_structure=true, synchronize_data=true): Copy schema and data
  • Structure only (synchronize_structure=true, synchronize_data=false): Copy schema only
  • Data only (synchronize_data=true, synchronize_structure=false): Copy data into existing schema
  • No sync (both false): Start streaming immediately (requires pre-existing data)

Monitoring Sync Progress

-- Check subscription status (shows "initializing" during sync)
SELECT * FROM pglogical.show_subscription_status('my_subscription');

-- Block until sync is complete
SELECT pglogical.wait_for_subscription_sync_complete('my_subscription');

-- Wait for a specific table to sync
SELECT pglogical.wait_for_table_sync_complete('my_subscription', 'public.my_table');

Key Considerations

  • Schema sync uses pg_dump/pg_restore with custom format, excluding pglogical schemas
  • Data sync uses the COPY protocol for efficiency
  • Constraints and indexes are applied after data copy (post_data section of pg_restore)
  • pglogical.subscription_schema_restore=true connection parameter suppresses triggers during restore
  • Crashes during INIT or CATCHUP are recoverable; other states require re-setup

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment