Implementation:2ndQuadrant Pglogical Sync Subscription
| Knowledge Sources | |
|---|---|
| Domains | Database, Replication, Data_Transfer |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Internal function that performs full subscription synchronization by creating a replication slot, exporting a consistent snapshot, dumping schema via pg_dump, copying table data via COPY, and transitioning through the multi-phase sync state machine.
Description
The pglogical_sync_subscription() function orchestrates the initial synchronization of a new subscription. It creates a logical replication slot on the provider (which exports a consistent snapshot), uses pg_dump in custom format to copy the schema (excluding pglogical schemas), restores the schema via pg_restore (pre_data section first, then post_data for constraints/indexes), copies table data using the COPY protocol with the exported snapshot, and coordinates the handoff to the apply worker.
The sync state machine transitions through: INIT → STRUCTURE (dump/restore schema) → DATA (COPY table data) → SYNCWAIT (signal apply worker) → CATCHUP (apply catches up) → SYNCDONE → READY (streaming begins).
The helper functions dump_structure(), restore_structure(), and exec_cmd() manage the external binary execution (pg_dump, pg_restore).
Usage
This is an internal function called by the sync worker. It is not user-callable directly. Users trigger it by creating a subscription with synchronize_structure=true and/or synchronize_data=true, or by calling pglogical.alter_subscription_synchronize().
Code Reference
Source Location
- Repository: pglogical
- File: pglogical_sync.c
- Lines: L862-1075 (pglogical_sync_subscription); L161-229 (dump_structure); L231-293 (restore_structure); L114-143 (exec_cmd)
Signature
void pglogical_sync_subscription(PGLogicalSubscription *sub);
static void dump_structure(PGLogicalSubscription *sub,
const char *destfile,
const char *snapshot);
static void restore_structure(PGLogicalSubscription *sub,
const char *srcfile,
const char *section);
static int exec_cmd(const char *cmd, char *cmdargv[]);
Import
/* Internal C function, called by sync worker.
Not user-callable. */
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| sub | PGLogicalSubscription* | Yes | Subscription with origin DSN and slot name |
| destfile | const char* | Yes dump_structure: temp file path for pg_dump output | |
| snapshot | const char* | Yes dump_structure: exported snapshot ID for consistent dump | |
| srcfile | const char* | Yes restore_structure: dump file path to restore from | |
| section | const char* | Yes restore_structure: pg_restore section ("pre_data", "post_data") | |
| cmd | const char* | Yes exec_cmd: full path to executable | |
| cmdargv | char*[] | Yes exec_cmd: argument array (NULL-terminated) |
Outputs
| Name | Type | Description |
|---|---|---|
| sync status | catalog | Transitions through INIT → STRUCTURE → DATA → SYNCWAIT → CATCHUP → SYNCDONE → READY |
| schema | database objects | Tables, sequences, constraints created on subscriber (pre_data + post_data) |
| table data | rows | Table data copied via COPY protocol |
| replication origin | origin record | Replication origin advanced to sync point |
Usage Examples
Triggering Subscription Sync (User API)
-- Create subscription with full sync (triggers pglogical_sync_subscription internally)
SELECT pglogical.create_subscription(
subscription_name := 'my_sub',
provider_dsn := 'host=provider.example.com dbname=mydb',
replication_sets := ARRAY['default'],
synchronize_structure := true,
synchronize_data := true,
...
);
-- Re-sync a subscription later
SELECT pglogical.alter_subscription_synchronize('my_sub', 'public.my_table');
Schema Dump Command (Internal)
# Internal command built by dump_structure():
pg_dump -Fc -s --snapshot=<snapshot_id> \
--exclude-schema=pglogical \
--file=<destfile> \
--dbname=<provider_dsn>
Schema Restore Command (Internal)
# Internal command built by restore_structure():
pg_restore --section=pre_data --dbname=<subscriber_dsn> <dumpfile>
# ... then after data copy ...
pg_restore --section=post_data --dbname=<subscriber_dsn> <dumpfile>
Related Pages
Implements Principle
Uses Heuristic
- Heuristic:2ndQuadrant_Pglogical_Temp_Directory_Configuration
- Heuristic:2ndQuadrant_Pglogical_Snapshot_Consistency_for_Sync