Heuristic:2ndQuadrant Pglogical Snapshot Consistency for Sync
| Knowledge Sources | |
|---|---|
| Domains | Replication, Database, Synchronization |
| Last Updated | 2026-10-13 03:51 GMT |
Overview
Initial data synchronization uses a consistent replication slot snapshot to ensure schema and data dumps match the replication start position, avoiding data loss or duplication during subscriber initialization.
Description
pglogical's initial sync process creates a logical replication slot on the provider, which establishes a consistent snapshot point. The schema is dumped using `pg_dump` and restored using `pg_restore` (or `pg_dump`'s `--section` flags) against this snapshot. Data is then copied using `COPY` operations that reference the same snapshot via the replication slot. The replication origin is advanced to the snapshot's LSN before live replication begins, ensuring no changes are lost and no changes are applied twice. The sync process is implemented as a multi-phase state machine (`INIT` → `STRUCTURE` → `DATA` → `CATCHUP` → `READY`) with crash recovery at each phase.
Usage
Use this heuristic when understanding or troubleshooting initial subscription setup. If sync fails during a non-recoverable step, the subscription must be recreated from scratch. The `pglogical.temp_directory` GUC must be set to a writable path for temporary dump files. Schema and data sync can be independently controlled via the subscription's `sync_kind` parameter.
The Insight (Rule of Thumb)
- Action: Ensure `pglogical.temp_directory` is set to a writable, sufficiently large directory before starting subscription sync.
- Value: Default is `$TMPDIR` or `/tmp`. Override with `pglogical.temp_directory = '/path/to/dir'` in `postgresql.conf` (SIGHUP).
- Trade-off: Dump files can be large; ensure adequate disk space. The temp directory must NOT be a `temp_tablespace` because PostgreSQL crash recovery deletes files there.
- Compatibility: Schema sync uses `pg_dump`/`pg_restore`; data sync uses `COPY` via the replication slot snapshot. Failover slots are supported on 2ndQuadrant PostgreSQL builds (2QPG9.6, 2QPG11).
Reasoning
The sync process must establish a consistent point-in-time view of the provider's data. It does this by creating a logical replication slot, which provides both a snapshot and a WAL position (LSN). Schema is dumped and restored first (pre-data section), then data is copied. The replication origin is advanced to the snapshot LSN so that when live replication starts, it begins from exactly where the snapshot left off. If sync fails at `SYNC_STATUS_INIT` or `SYNC_STATUS_CATCHUP`, the process can recover. Other states are non-recoverable and require full re-setup.
Code Evidence
Snapshot via replication slot — from `pglogical_sync.c:925-928`:
snapshot = ensure_replication_slot_snapshot(origin_conn,
origin_conn_repl,
sub->slot_name,
use_failover_slot, &lsn);
Temp file for schema dump — from `pglogical_sync.c:937-939`:
snprintf(tmpfile, MAXPGPATH, "%s/pglogical-%d.dump",
pglogical_temp_directory, MyProcPid);
canonicalize_path(tmpfile);
Multi-phase sync state machine — from `pglogical_sync.c:884-898`:
switch (status)
{
/* Already synced, nothing to do except cleanup. */
case SYNC_STATUS_READY:
MemoryContextDelete(myctx);
return;
/* We can recover from crashes during these. */
case SYNC_STATUS_INIT:
case SYNC_STATUS_CATCHUP:
break;
default:
elog(ERROR,
"subscriber %s initialization failed during nonrecoverable step (%c), please try the setup again",
sub->name, status);
break;
}
Replication origin advance to snapshot LSN — from `pglogical_sync.c:950-958`:
originid = ensure_replication_origin(sub->slot_name);
replorigin_advance(originid, lsn, XactLastCommitEnd, true, true);