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

Heuristic:2ndQuadrant Pglogical Worker Process Sizing

From Leeroopedia
Knowledge Sources
Domains Replication, Infrastructure, Optimization
Last Updated 2026-10-13 03:51 GMT

Overview

Size `max_worker_processes` to account for pglogical's supervisor, manager (one per database), apply workers (one per subscription), and sync workers (one per table during initial sync), or risk worker registration failures.

Description

pglogical uses PostgreSQL's background worker infrastructure extensively. The worker hierarchy is:

  • Supervisor: One global worker, started by `_PG_init()`, registered with `BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION`.
  • Manager workers: One per database with pglogical installed. The supervisor starts managers for every connectable database — even those without pglogical — because it cannot switch databases within a single worker. Unused managers exit immediately.
  • Apply workers: One per subscription (per node).
  • Sync workers: One per table during initial data synchronization, spawned by the manager.

The `max_worker_processes` PostgreSQL setting must be set high enough to accommodate all these workers. The supervisor's behavior of starting workers for every database can consume all available worker slots, causing `worker registration failed` errors even when `max_worker_processes` appears sufficient.

Usage

Set `max_worker_processes` in `postgresql.conf` based on:

  • Provider node: One worker per database with pglogical.
  • Subscriber node: One worker per node (subscription), plus one per table during initial sync, plus one manager per database.
  • Rule of thumb: `max_worker_processes = 10` is the README's recommended minimum.

If you see `worker registration failed, you might want to increase max_worker_processes setting`, increase the value and restart PostgreSQL.

The Insight (Rule of Thumb)

  • Action: Set `max_worker_processes` in `postgresql.conf` (requires restart).
  • Value: Minimum 10 (README recommendation). Calculate: supervisor (1) + number of databases with pglogical + number of subscriptions + concurrent sync workers.
  • Trade-off: Each worker process consumes shared memory and a process slot. Over-provisioning wastes resources; under-provisioning causes replication failures.
  • Compatibility: Workers are allocated from the shared `max_worker_processes` pool, shared with other extensions and PostgreSQL's parallel query workers.

Reasoning

The supervisor starts a manager worker for every connectable database to check if pglogical is installed there. This is necessary because background workers cannot switch databases (`UseBackgroundConnection` is per-database). Workers for databases without pglogical exit immediately, but during the startup window they consume worker slots. If `max_worker_processes` is set too low, legitimate apply or sync workers may fail to register. The shared memory allocation for worker tracking (`worker_shmem_size(nworkers)`) is sized based on `max_worker_processes` at startup.

Code Evidence

Supervisor startup logic — from `pglogical.c:596-606`:

/*
 * Note that we start workers that are not necessary here. We do this because
 * we need to check every individual database to check if there is pglogical
 * node setup and it's not possible to switch connections to different
 * databases within one background worker. The workers that won't find any
 * pglogical node setup will exit immediately during startup.
 * This behavior can cause issue where we consume all the allowed workers and
 * eventually error out even though the max_worker_processes is set high enough
 * to satisfy the actual needed worker count.
 */

Worker registration failure — from `pglogical_worker.c:183-185`:

ereport(ERROR,
        (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
         errmsg("worker registration failed, you might want to increase max_worker_processes setting")));

Shared memory sized by `max_worker_processes` — from `pglogical_worker.c:696-710`:

nworkers = atoi(GetConfigOptionByName("max_worker_processes", NULL, false));

PGLogicalCtx = ShmemInitStruct("pglogical_context",
                               worker_shmem_size(nworkers), &found);

if (!found)
{
    PGLogicalCtx->lock = &(GetNamedLWLockTranche("pglogical"))->lock;
    PGLogicalCtx->supervisor = NULL;
    PGLogicalCtx->subscriptions_changed = false;
    PGLogicalCtx->total_workers = nworkers;
    memset(PGLogicalCtx->workers, 0,
           sizeof(PGLogicalWorker) * PGLogicalCtx->total_workers);
}

Supervisor worker registration — from `pglogical.c:923-935`:

bgw.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
bgw.bgw_start_time = BgWorkerStart_RecoveryFinished;
bgw.bgw_restart_time = 5;
RegisterBackgroundWorker(&bgw);

Related Pages

Page Connections

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