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

Environment:2ndQuadrant Pglogical PostgreSQL Server

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

Overview

PostgreSQL 9.4+ server environment with logical replication extensions (pglogical 2.4.8), requiring `wal_level=logical`, background worker slots, and replication slot configuration.

Description

This environment provides the PostgreSQL server runtime required by the pglogical logical replication extension. pglogical is a compiled C extension that must be installed via `PGXS` (`make && make install`) against a PostgreSQL server development header set. The extension must be loaded at server startup via `shared_preload_libraries`. The server requires configuration of `wal_level = 'logical'`, replication slots, WAL senders, and background worker processes. pglogical supports PostgreSQL 9.4 through 19+ via per-version compatibility shims (`compat94` through `compat19`). PostgreSQL 9.5+ is required for replication origin filtering and conflict detection. The `track_commit_timestamp` setting must be enabled for timestamp-based conflict resolution strategies (`last_update_wins`, `first_update_wins`).

Usage

Use this environment for any pglogical operation — creating nodes, subscriptions, replication sets, applying changes, syncing data, or running the `pglogical_create_subscriber` CLI tool. Every pglogical implementation requires this environment as a prerequisite. The extension must be installed on both provider and subscriber nodes.

System Requirements

Category Requirement Notes
OS Linux (any modern distribution) Also supports Windows and macOS for development; production deployments typically use Linux
Software PostgreSQL 9.4+ (provider & subscriber) PostgreSQL 9.5+ required for conflict detection and replication origin filtering
Software PostgreSQL development headers Required for building the extension via PGXS (`postgresql-server-dev-*` package on Debian/Ubuntu)
Software libpq Required for client-side connections (`pglogical_create_subscriber` tool)
Hardware Sufficient `max_worker_processes` One worker per database on provider, one per node on subscriber, plus supervisor
Disk Writable temp directory For schema sync dumps; defaults to `/tmp` or `$TMPDIR`

Dependencies

System Packages

  • `postgresql-server-dev-9.x` (matching PG version, provides PGXS and headers)
  • `libpq-dev` (libpq headers for frontend tool)
  • `gcc` or compatible C compiler
  • `make`

PostgreSQL Configuration (postgresql.conf)

  • `wal_level = 'logical'` — mandatory for logical decoding
  • `max_worker_processes = 10` — one per database on provider, one per node on subscriber
  • `max_replication_slots = 10` — one per node on provider
  • `max_wal_senders = 10` — one per node on provider
  • `shared_preload_libraries = 'pglogical'` — must load at server startup
  • `track_commit_timestamp = on` — optional; required for `last_update_wins` / `first_update_wins` conflict resolution (PG 9.5+)

pglogical GUCs

  • `pglogical.conflict_resolution` — conflict resolution strategy (`error`, `apply_remote`, `keep_local`, `last_update_wins`, `first_update_wins`)
  • `pglogical.conflict_log_level` — log level for conflict reports (default: `LOG`)
  • `pglogical.synchronous_commit` — pglogical-specific synchronous commit (default: `false`)
  • `pglogical.use_spi` — use SPI instead of heap API for applying changes (default: `false`)
  • `pglogical.batch_inserts` — batch insert mechanism (default: `true`)
  • `pglogical.temp_directory` — directory for sync dump storage (default: `$TMPDIR` or `/tmp`)
  • `pglogical.extra_connection_options` — extra libpq connection options for peer connections
  • `pglogical.subscription_owner` — role that owns subscriptions

Credentials

The following connection parameters are used in DSN strings (not environment variables, but connection string components):

  • Provider DSN: Connection string to the provider node (e.g., `host=provider dbname=mydb user=replication_user password=...`)
  • Subscriber DSN: Connection string to the subscriber node
  • Replication user must have `REPLICATION` attribute and appropriate table permissions

Quick Install

# Install build dependencies (Debian/Ubuntu)
apt-get install postgresql-server-dev-15 libpq-dev gcc make

# Build and install pglogical extension
cd /path/to/pglogical
make
make install

# Configure postgresql.conf (add these lines):
#   wal_level = 'logical'
#   max_worker_processes = 10
#   max_replication_slots = 10
#   max_wal_senders = 10
#   shared_preload_libraries = 'pglogical'
#   track_commit_timestamp = on   # optional, for timestamp-based conflict resolution

# Restart PostgreSQL, then in each database:
psql -d mydb -c "CREATE EXTENSION pglogical;"

Code Evidence

Extension must be in `shared_preload_libraries` — from `pglogical.c:819-820`:

if (!process_shared_preload_libraries_in_progress)
    elog(ERROR, "pglogical is not in shared_preload_libraries");

Version check — from `pglogical.h:61-65`:

#define PGLOGICAL_VERSION "2.4.8"
#define PGLOGICAL_VERSION_NUM 20408

#define PGLOGICAL_MIN_PROTO_VERSION_NUM 1
#define PGLOGICAL_MAX_PROTO_VERSION_NUM 1

PG version conditional for replication origins — from `pglogical.h:71-73`:

#if PG_VERSION_NUM >= 90500
#define HAVE_REPLICATION_ORIGINS
#endif

Worker process limit check — 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")));

Conflict resolver GUC validation requiring `track_commit_timestamp` — from `pglogical_conflict.c:745-756`:

/*
 * Only allow PGLOGICAL_RESOLVE_APPLY_REMOTE when track_commit_timestamp
 * is off, because there is no way to know where the local tuple
 * originated from.
 */
if (!track_commit_timestamp &&
    *newval != PGLOGICAL_RESOLVE_APPLY_REMOTE &&
    *newval != PGLOGICAL_RESOLVE_ERROR)
{
    GUC_check_errdetail("track_commit_timestamp is off");
    return false;
}

SPI mode requires `error` conflict resolution — from `pglogical_apply.c:1940-1946`:

if (pglogical_use_spi)
{
    if (pglogical_conflict_resolver != PGLOGICAL_RESOLVE_ERROR)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("pglogical.use_spi can only be used when "
                        "pglogical.conflict_resolution is set to 'error'")));

Common Errors

Error Message Cause Solution
`pglogical is not in shared_preload_libraries` Extension not loaded at startup Add `pglogical` to `shared_preload_libraries` in `postgresql.conf` and restart server
`worker registration failed, you might want to increase max_worker_processes setting` Too few worker slots Increase `max_worker_processes` (one per database on provider, one per node on subscriber) and restart
`could not find REPLICA IDENTITY index for table` Table lacks primary key or replica identity Add `PRIMARY KEY` or set `ALTER TABLE ... REPLICA IDENTITY USING INDEX ...`
`pglogical.use_spi can only be used when pglogical.conflict_resolution is set to 'error'` SPI mode incompatible with conflict detection Set `pglogical.conflict_resolution = 'error'` when using SPI, or disable SPI
`track_commit_timestamp is off` (GUC check error) Timestamp-based conflict resolution without `track_commit_timestamp` Set `track_commit_timestamp = on` or use `apply_remote`/`error` resolver
`cannot apply conflicting row` Conflict detected with `pglogical.conflict_resolution = 'error'` Manually resolve conflict, then resume replication
`subscriber initialization failed during nonrecoverable step` Sync interrupted at a non-recoverable state Retry subscription setup from scratch
`user cannot replicate into relation with row level security enabled` RLS enabled on target table Disable RLS on replicated tables or exclude from replication

Compatibility Notes

  • PostgreSQL 9.4: Minimum supported version. No replication origins, no `track_commit_timestamp`. Conflict resolution limited to `apply_remote` or `error`. Uses `pglogical_origin` auxiliary extension. Includes `pglogical_dump` submodule.
  • PostgreSQL 9.5+: Enables replication origins (`HAVE_REPLICATION_ORIGINS`), conflict detection, and `track_commit_timestamp` support.
  • PostgreSQL 9.6 - 19: Each version has a dedicated `compatNN/` directory with version-specific macros. PG 12+ changes heap API (`table_open` vs `heap_open`). PG 15+ uses `shmem_request_hook` for shared memory allocation. PG 19+ uses `libpq/libpq-be-fe.h` wrapping for `libpqsrv_PGresult`.
  • Postgres-XL: Supported as subscriber only (9.5+). Defaults `pglogical.conflict_resolution` to `error` and `pglogical.use_spi` to `true`. XCP builds use `#ifdef XCP` conditionals.
  • Multiple upstreams: Only one `UNIQUE` index should be present on downstream replicated tables. Conflict resolution uses one index at a time; conflicting rows may ERROR if a row satisfies `PRIMARY KEY` but violates another `UNIQUE` constraint.
  • REPLICA IDENTITY FULL: Not supported. `UPDATE`s and `DELETE`s require a `PRIMARY KEY` or explicit `REPLICA IDENTITY USING INDEX`.
  • Windows: Uses `GetTempPath` for temp directory instead of `$TMPDIR`. Some GUC variables are not exported as `PGDLLIMPORT`, requiring `GetConfigOptionByName` workarounds.

Related Pages

Page Connections

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