Heuristic:2ndQuadrant Pglogical Relation Metadata Caching
| Knowledge Sources | |
|---|---|
| Domains | Replication, Optimization, Database |
| Last Updated | 2026-10-13 03:51 GMT |
Overview
pglogical caches remote-to-local relation metadata (attribute mappings, trigger info) in a hash table to avoid repeated catalog lookups during high-throughput change application.
Description
pglogical maintains an in-memory hash table (`PGLogicalRelationHash`) that maps remote relation IDs to local relation metadata, including schema/table names, attribute name arrays, attribute mapping arrays, and trigger presence flags. When a change message arrives from the provider, pglogical looks up the remote relation ID in this cache. If the local relation OID is invalid (first access or after invalidation), pglogical opens the local relation, builds the attribute mapping, and caches the result. The cache is invalidated by PostgreSQL's standard cache invalidation mechanism (`CacheInvalidateRelcache` callbacks). This avoids repeated `table_open` and `TupleDesc` lookups for every row change, which is critical for high-volume replication throughput.
Usage
This caching is automatic and transparent. However, when debugging replication issues, be aware that stale cache entries can cause attribute mapping mismatches after schema changes on the provider. If replication fails with "cache lookup failed for remote relation", the cache may need to be refreshed by restarting the apply worker. The initial hash table size is 128 entries.
The Insight (Rule of Thumb)
- Action: No action needed — caching is automatic. For debugging, restart the apply worker to flush the relation cache after schema changes.
- Value: Initial hash table size: 128 entries (`PGLOGICALRELATIONHASH_INITIAL_SIZE`).
- Trade-off: Stale cache entries after schema changes on the provider can cause attribute mapping errors until the apply worker is restarted.
- Compatibility: Cache invalidation relies on PostgreSQL's standard `InvalidateCaches` mechanism.
Reasoning
During high-throughput replication, the apply worker processes thousands of row changes per second. Each change references a remote relation ID that must be mapped to a local relation and its attribute layout. Without caching, every row change would require catalog lookups (`table_open`, `TupleDesc` inspection, attribute name resolution), which is extremely expensive. The hash table provides O(1) lookup after the first access. The cache is properly invalidated by registering with PostgreSQL's cache invalidation framework, but edge cases during schema changes can leave stale entries.
Code Evidence
Hash table initialization — from `pglogical_relcache.c:31-32`:
#define PGLOGICALRELATIONHASH_INITIAL_SIZE 128
static HTAB *PGLogicalRelationHash = NULL;
Relation open with cache lookup — from `pglogical_relcache.c:63-94`:
PGLogicalRelation *
pglogical_relation_open(uint32 remoteid, LOCKMODE lockmode)
{
PGLogicalRelation *entry;
bool found;
if (PGLogicalRelationHash == NULL)
pglogical_relcache_init();
/* Search for existing entry. */
entry = hash_search(PGLogicalRelationHash, (void *) &remoteid,
HASH_FIND, &found);
if (!found)
elog(ERROR, "cache lookup failed for remote relation %u",
remoteid);
/* Need to update the local cache? */
if (!OidIsValid(entry->reloid))
{
RangeVar *rv = makeNode(RangeVar);
TupleDesc desc;
rv->schemaname = (char *) entry->nspname;
rv->relname = (char *) entry->relname;
entry->rel = table_openrv(rv, lockmode);
desc = RelationGetDescr(entry->rel);
for (int i = 0; i < entry->natts; i++)
entry->attmap[i] = tupdesc_get_att_by_name(desc, entry->attnames[i]);
entry->reloid = RelationGetRelid(entry->rel);
}
return entry;
}
Cache entry freeing — from `pglogical_relcache.c:38-60`:
static void
relcache_free_entry(PGLogicalRelation *entry)
{
pfree(entry->nspname);
pfree(entry->relname);
if (entry->natts > 0)
{
int i;
for (i = 0; i < entry->natts; i++)
pfree(entry->attnames[i]);
pfree(entry->attnames);
}
if (entry->attmap)
pfree(entry->attmap);
entry->natts = 0;
entry->reloid = InvalidOid;
entry->rel = NULL;
}