Implementation:2ndQuadrant Pglogical PG94 Compat
| Knowledge Sources | |
|---|---|
| Domains | Replication, Compatibility |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Concrete compatibility layer that backports replication origin tracking, commit timestamp support, named LWLocks, catalog tuple helpers, and background worker connection to PostgreSQL 9.4, enabling pglogical to run on the oldest supported server version.
Description
The compat94/pglogical_compat.c file provides full implementations of PostgreSQL internals that are missing in PG 9.4 but required by pglogical. PG 9.4 predates the built-in replication origin infrastructure, commit timestamp tracking, named LWLock tranches, and the CatalogTuple* convenience API. This file fills those gaps with standalone implementations stored in the pglogical_origin schema.
The replication origin API (replorigin_create, replorigin_by_name, replorigin_session_setup, replorigin_advance, etc.) is the largest and most significant portion. It maintains a pg_replication_origin catalog table with per-origin replay progress (remote LSN and local LSN), a session-level replication state for the current backend, and a transaction callback that advances progress at commit time.
Additional helpers include BackgroundWorkerInitializeConnectionByOid for connecting a background worker to a database by OID, strlist_to_textarray for converting C-string lists to PostgreSQL text arrays, GetNamedLWLockTranche/RequestNamedLWLockTranche for named LWLock allocation, and CatalogTupleInsert/CatalogTupleUpdate/CatalogTupleDelete for uniform catalog tuple manipulation.
Usage
This file is compiled and linked only when pglogical is built against PostgreSQL 9.4. It is not imported by user code directly; it provides symbols that the rest of pglogical calls as if they were native PostgreSQL functions. Import this module when you need to understand what infrastructure pglogical provides on PG 9.4 that the server itself lacks.
Code Reference
Source Location
- Repository: 2ndQuadrant_Pglogical
- File: compat94/pglogical_compat.c
- Lines: 1-669
Signature
/* Replication origin API (backported for PG 9.4) */
RepOriginId replorigin_create(char *roname);
void pgl_replorigin_drop(RepOriginId roident);
void replorigin_drop_by_name(char *name, bool missing_ok, bool nowait);
RepOriginId replorigin_by_name(char *name, bool missing_ok);
void replorigin_session_setup(RepOriginId node);
void replorigin_session_reset(void);
XLogRecPtr replorigin_session_get_progress(bool flush);
void replorigin_advance(RepOriginId node,
XLogRecPtr remote_commit,
XLogRecPtr local_commit,
bool go_backward, bool wal_log);
/* Background worker connection */
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid);
/* Utility helpers */
ArrayType *strlist_to_textarray(List *list);
LWLockPadded *GetNamedLWLockTranche(const char *tranche_name);
void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks);
/* Catalog tuple helpers */
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup);
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup);
void CatalogTupleDelete(Relation heapRel, ItemPointer tid);
/* Commit timestamp (stub) */
bool TransactionIdGetCommitTsData(TransactionId xid,
TimestampTz *ts, RepOriginId *nodeid);
Import
/* Not directly imported — compiled into pglogical extension when PG_VERSION_NUM < 90500 */
#include "pglogical_compat.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| roname | char * | Yes | Name for a new replication origin (replorigin_create) |
| roident | RepOriginId | Yes | Numeric origin identifier (pgl_replorigin_drop, replorigin_session_setup, replorigin_advance) |
| name | char * | Yes | Origin name to look up or drop (replorigin_by_name, replorigin_drop_by_name) |
| missing_ok | bool | Yes | If true, return InvalidOid instead of erroring when origin not found |
| node | RepOriginId | Yes | Origin ID to set up as the session origin |
| remote_commit | XLogRecPtr | Yes | Remote LSN of the commit being replayed (replorigin_advance) |
| local_commit | XLogRecPtr | Yes | Local LSN of the replayed commit (replorigin_advance) |
| flush | bool | Yes | If true, XLogFlush the local LSN before returning progress |
| dboid | Oid | Yes | Database OID to connect to (BackgroundWorkerInitializeConnectionByOid) |
| useroid | Oid | Yes | User OID for the connection |
| list | List * | Yes | List of C-strings to convert (strlist_to_textarray) |
| tranche_name | const char * | Yes | Name of the LWLock tranche |
| heapRel | Relation | Yes | Catalog relation for tuple operations |
| tup | HeapTuple | Yes | Tuple to insert/update |
| tid | ItemPointer | Yes | Tuple identifier to update/delete |
Outputs
| Name | Type | Description |
|---|---|---|
| replorigin_create returns | RepOriginId | Newly allocated 16-bit origin identifier |
| replorigin_by_name returns | RepOriginId | Origin ID found by name, or InvalidOid |
| replorigin_session_get_progress returns | XLogRecPtr | Remote LSN of last replayed commit |
| strlist_to_textarray returns | ArrayType * | TEXT array containing the input strings |
| GetNamedLWLockTranche returns | LWLockPadded * | Pointer to the named LWLock |
| CatalogTupleInsert returns | Oid | OID of the inserted tuple |
| Side effects | catalog rows | Replication origin rows inserted/deleted/updated in pglogical_origin.replication_origin |
| Side effects | session state | session_replication_state allocated in CacheMemoryContext |
Usage Examples
Replication Origin Lifecycle
/*
* Create, use, and drop a replication origin on PG 9.4.
* This is what pglogical's apply worker does internally.
*/
/* 1. Create a new origin for a provider node */
RepOriginId origin = replorigin_create("pglogical_provider_1");
/* 2. Set up the session to track replay progress */
replorigin_session_setup(origin);
/* 3. After applying a transaction, advance the origin */
replorigin_advance(origin,
remote_commit_lsn, /* where the provider's WAL is */
local_commit_lsn, /* where our replay landed */
false, /* don't go backward */
true); /* log to WAL */
/* 4. Query how far we've replayed */
XLogRecPtr progress = replorigin_session_get_progress(true /* flush */);
/* 5. Reset session when done */
replorigin_session_reset();
/* 6. Drop the origin when the subscription is removed */
pgl_replorigin_drop(origin);
Background Worker Connection
/*
* Connect a pglogical background worker to a target database by OID.
* Used by the apply worker and sync workers.
*/
BackgroundWorkerInitializeConnectionByOid(MyDatabaseId, MyUserId);
/* Worker is now connected and can run queries via SPI */
Catalog Tuple Helper
/*
* Insert a tuple into a catalog table, keeping indexes up to date.
* On PG 9.4 this is provided by pglogical's compat layer.
*/
CatalogTupleInsert(catalog_relation, new_tuple);