Implementation:2ndQuadrant Pglogical Manage Apply Workers
| Knowledge Sources | |
|---|---|
| Domains | Database, Replication, Process_Management |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Internal supervisor function that manages apply worker lifecycle by scanning subscriptions, spawning workers for enabled subscriptions, killing workers for disabled ones, and restarting crashed workers with exponential backoff.
Description
The manage_apply_workers() function is the core of the pglogical manager worker. It runs in a loop (called from pglogical_manager_main()), scans all subscriptions for the local node, checks each against the pool of running workers, and takes action:
- If a subscription is enabled and has no running worker, a new apply worker is registered via pglogical_worker_register().
- If a subscription is disabled but has a running worker, the worker is killed.
- If a worker has crashed (crashed_at != 0), the manager applies exponential backoff before restarting.
The manager sleeps 10 seconds between scans, using WaitLatch to allow SIGTERM to interrupt.
Usage
This is an internal function called automatically by the manager background worker. Users do not call it directly, but should understand it when:
- Debugging worker startup or crash recovery
- Tuning max_worker_processes
- Understanding why a crashed worker does not restart immediately
Code Reference
Source Location
- Repository: pglogical
- File: pglogical_manager.c, pglogical_worker.c
- Lines: pglogical_manager.c:L45-190 (manage_apply_workers), L195-253 (manager_main); pglogical_worker.c:L107 (worker_register), L308 (worker_attach)
Signature
static bool manage_apply_workers(void);
void pglogical_manager_main(Datum main_arg);
void pglogical_worker_register(PGLogicalWorker *worker);
PGLogicalWorker *pglogical_worker_attach(int slot, PGLogicalWorkerType type);
Import
/* Internal C function, not user-callable.
Loaded automatically as a background worker by PostgreSQL. */
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| local node | catalog | Yes pglogical.local_node must exist | |
| subscriptions | catalog | Yes pglogical.subscription entries with enabled flag | |
| worker slots | shared memory | Yes PGLogicalContext worker array |
Outputs
| Name | Type | Description |
|---|---|---|
| return value | bool | true if all workers started, false if some are in backoff |
| apply workers | background processes | Registered or killed as needed |
| sync workers | background processes | Spawned for pending table syncs |
Usage Examples
Manager Behavior (Internal)
// Abstract manager loop (NOT real implementation)
void pglogical_manager_main(Datum main_arg) {
while (!got_SIGTERM) {
manage_apply_workers();
// Sleep 10 seconds, wake on SIGTERM
WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, 10000L);
ResetLatch(&MyProc->procLatch);
}
proc_exit(0);
}
Monitoring Worker Health
-- Check subscription status (shows if worker is running)
SELECT subscription_name, status
FROM pglogical.show_subscription_status();
-- Check background worker processes
SELECT pid, application_name, state
FROM pg_stat_activity
WHERE backend_type = 'bgworker';
Related Pages
Implements Principle
- Principle:2ndQuadrant_Pglogical_Background_Worker_Coordination
- Principle:2ndQuadrant_Pglogical_Subscription_Management