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

Implementation:2ndQuadrant Pglogical Sync Table

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

Overview

Internal function that synchronizes an individual table by obtaining a replication slot snapshot, copying table data via COPY, and coordinating handoff to the apply worker.

Description

The pglogical_sync_table() function handles per-table synchronization, used when adding a new table to an existing subscription or re-synchronizing a table that fell behind. It obtains a fresh snapshot from the replication slot, copies the table data using the COPY protocol, and transitions the table's sync status to SYNCWAIT. The apply worker then processes the CATCHUP and SYNCDONE states.

The pglogical_sync_main() function is the entry point for the sync worker background process. It receives the worker slot index, loads the subscription, identifies the table to sync, and calls pglogical_sync_table().

After the table is copied, pglogical_sync_worker_finish() signals the apply worker and exits.

Usage

This is an internal function called by sync workers. Users trigger it via:

  • pglogical.alter_subscription_synchronize() — sync all tables or a specific table
  • pglogical.alter_subscription_resynchronize_table() — re-sync a specific table

Code Reference

Source Location

  • Repository: pglogical
  • File: pglogical_sync.c
  • Lines: L1077-1159 (pglogical_sync_table); L1199-1270 (pglogical_sync_main); L1161-1197 (pglogical_sync_worker_finish)

Signature

char pglogical_sync_table(PGLogicalSubscription *sub,
                          RangeVar *table,
                          XLogRecPtr *status_lsn);

void pglogical_sync_main(Datum main_arg);

void pglogical_sync_worker_finish(void);

Import

/* Internal C function, called by sync worker background process. */

I/O Contract

Inputs

Name Type Required Description
sub PGLogicalSubscription* Yes Subscription context with origin DSN
table RangeVar* Yes Schema-qualified table name
status_lsn XLogRecPtr* Yes Output: LSN for sync status tracking
main_arg Datum Yes pglogical_sync_main: worker slot index

Outputs

Name Type Description
return value char Sync status (SYNC_STATUS_SYNCWAIT on success, or existing status if already synced)
*status_lsn XLogRecPtr LSN of the sync snapshot for apply worker handoff
table data rows Data copied via COPY protocol
sync status catalog Table sync status advanced to SYNC_STATUS_DATA then SYNC_STATUS_SYNCWAIT

Usage Examples

Triggering Table Sync (User API)

-- Synchronize a specific table
SELECT pglogical.alter_subscription_synchronize(
    subscription_name := 'my_sub',
    relation := 'public.new_table'
);

-- Re-synchronize a table (full re-copy)
SELECT pglogical.alter_subscription_resynchronize_table(
    subscription_name := 'my_sub',
    relation := 'public.corrupted_table'
);

-- Wait for the table sync to complete
SELECT pglogical.wait_for_table_sync_complete('my_sub', 'public.new_table');

Related Pages

Implements Principle

Uses Heuristic

Requires Environment

Page Connections

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