Implementation:2ndQuadrant Pglogical Relation Open
| Knowledge Sources | |
|---|---|
| Domains | Database, Replication |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Relation cache function that maps remote relation IDs to local relations by looking up the remote-to-local relation mapping in a hash table cache, opening the local table, and computing attribute mappings.
Description
The pglogical_relation_open() function is the entry point for the subscriber-side relation cache. When the apply worker receives a protocol message referencing a remote relation ID, it calls this function to obtain a PGLogicalRelation structure containing:
- The opened local relation (Relation handle)
- The schema and table names
- An attribute map (attmap) mapping remote column positions to local column positions
- Trigger descriptor information (which triggers to fire)
The function looks up the remote ID in the PGLogicalRelationHash hash table. If the entry exists but the local relation hasn't been opened yet (no reloid), it opens the table by name, computes the attribute mapping from the cached remote column names to local column positions, and records whether the table has replica triggers.
The relation cache is populated when REL messages are received from the provider and is maintained in a hash table keyed by remote relation ID.
Usage
This is an internal function called by the apply worker's message processing loop. Users do not call it directly, but should understand it when:
- Debugging relation mapping failures
- Understanding how remote columns are mapped to local columns
- Troubleshooting "cache lookup failed for remote relation" errors
Code Reference
Source Location
- Repository: pglogical
- File: pglogical_relcache.c
- Lines: L63-100 (pglogical_relation_open); L1-60 (cache initialization)
Signature
PGLogicalRelation *pglogical_relation_open(uint32 remoteid, LOCKMODE lockmode);
/* Related cache management: */
void pglogical_relcache_init(void);
Import
#include "pglogical_relcache.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| remoteid | uint32 | Yes Remote relation ID (from protocol message) | |
| lockmode | LOCKMODE | Yes Lock mode for opening the local relation |
Outputs
| Name | Type | Description |
|---|---|---|
| return value | PGLogicalRelation* | Relation structure with open handle, attmap, trigger info |
| attmap | int[] | Mapping from remote column indices to local column positions |
| hasTriggers | bool | Whether the table has replica triggers to fire |
Usage Examples
Relation Cache Flow (Internal)
// Abstract cache lookup logic (NOT real implementation)
PGLogicalRelation *pglogical_relation_open(uint32 remoteid, LOCKMODE lockmode) {
// 1. Look up in hash table
entry = hash_search(PGLogicalRelationHash, &remoteid, HASH_FIND, &found);
if (!found)
elog(ERROR, "cache lookup failed for remote relation %u", remoteid);
// 2. Open local relation if not yet opened
if (!OidIsValid(entry->reloid)) {
rv = makeRangeVar(entry->nspname, entry->relname, -1);
entry->rel = table_openrv(rv, lockmode);
// 3. Compute attribute mapping
desc = RelationGetDescr(entry->rel);
for (i = 0; i < entry->natts; i++)
entry->attmap[i] = tupdesc_get_att_by_name(desc, entry->attnames[i]);
entry->reloid = RelationGetRelid(entry->rel);
// 4. Cache trigger info
entry->hasTriggers = check_replica_triggers(entry->rel);
}
return entry;
}