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 Background Worker Coordination

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

Overview

Architecture pattern for managing replication worker processes through a supervisor (manager) that spawns, monitors, and restarts background workers (apply and sync) using shared memory coordination and exponential backoff for crash recovery.

Description

pglogical uses PostgreSQL's background worker framework to run long-lived replication processes. The architecture has three worker types:

  • Manager worker (one per database): Supervises all subscriptions. Scans catalog for enabled subscriptions, spawns apply workers, kills workers for disabled subscriptions, and restarts crashed workers with exponential backoff.
  • Apply worker (one per subscription): Connects to the provider, reads the replication stream, decodes messages, and applies changes. Runs continuously for the lifetime of the subscription.
  • Sync worker (one per table being synchronized): Performs initial data copy for individual tables. Exits after the table is synchronized and hands off to the apply worker.

Workers coordinate through shared memory (PGLogicalContext), which contains an array of worker slots protected by an LWLock. Each slot records the worker type, database OID, subscription ID, crash timestamp, and running state. The manager acquires the lock to find and manage workers; apply workers attach to their slot on startup.

Crash recovery uses exponential backoff: a crashed worker is not immediately restarted. The manager waits MIN_SLEEP (5s) after the first crash, then doubles the wait up to MAX_SLEEP (180s), starting from INITIAL_SLEEP (10s).

Usage

Background worker coordination is relevant when:

  • Understanding the pglogical process architecture
  • Debugging worker crashes or startup failures
  • Configuring worker pool sizing (max_worker_processes)
  • Monitoring replication worker health
  • Tuning crash recovery behavior

Theoretical Basis

Worker lifecycle:

Manager Main Loop:
    while not SIGTERM:
        manage_apply_workers()
            for each subscription:
                if enabled and no running worker:
                    register new apply worker
                if disabled and worker running:
                    kill worker
                if crashed:
                    if backoff_elapsed:
                        restart worker
        WaitLatch(timeout=10s)

Apply Worker:
    attach_to_slot()
    connect_to_provider()
    apply_work()  // main loop
    on_exit: record crash timestamp

Sync Worker:
    attach_to_slot()
    pglogical_sync_table()
    pglogical_sync_worker_finish()
    proc_exit(0)

Exponential backoff formula:

Failed to parse (syntax error): {\displaystyle \text{wait}(n) = \min(\text{INITIAL\_SLEEP} \times 2^{n-1}, \text{MAX\_SLEEP}) }

Where n is the crash count. The minimum wait is MIN_SLEEP (5s).

Shared memory coordination:

The PGLogicalContext structure in shared memory contains:

  • An LWLock for exclusive access
  • An array of PGLogicalWorker slots, each with:
    • worker_type: APPLY, SYNC, or MANAGER
    • dboid: database OID
    • worker.apply.subid: subscription ID
    • crashed_at: timestamp of last crash
    • proc: PostgreSQL process pointer (NULL if not running)

Practical Guide

Configuring Worker Pool

-- In postgresql.conf
shared_preload_libraries = 'pglogical'  -- Must be preloaded
max_worker_processes = 10                -- Must be >= 4 (manager + apply + sync + buffer)

Monitoring Workers

-- Check subscription status (shows worker running state)
SELECT * FROM pglogical.show_subscription_status();

-- Worker processes visible in pg_stat_activity
SELECT pid, application_name, state, backend_type
FROM pg_stat_activity
WHERE backend_type = 'bgworker';

Key Considerations

  • shared_preload_libraries = 'pglogical' is required for worker infrastructure
  • Each subscription needs one apply worker; each syncing table needs one sync worker
  • The manager sleeps for 10 seconds between scans
  • A crashed worker's slot is reused on restart (not freed immediately)
  • Sync workers are transient: they exit after table synchronization completes

Related Pages

Implemented By

Uses Heuristic

Page Connections

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