1/*-------------------------------------------------------------------------
3 * PostgreSQL logical replication relation mapping cache
5 * Copyright (c) 2016-2025, PostgreSQL Global Development Group
8 * src/backend/replication/logical/relation.c
11 * Routines in this file mainly have to do with mapping the properties
12 * of local replication target relations to the properties of their
15 *-------------------------------------------------------------------------
39 * Partition map (LogicalRepPartMap)
41 * When a partitioned table is used as replication target, replicated
42 * operations are actually performed on its leaf partitions, which requires
43 * the partitions to also be mapped to the remote relation. Parent's entry
44 * (LogicalRepRelMapEntry) cannot be used as-is for all partitions, because
45 * individual partitions may have different attribute numbers, which means
46 * attribute mappings to remote relation's attributes must be maintained
47 * separately for each partition.
61 * Relcache invalidation callback for our relation map cache.
68 /* Just to be sure. */
78 /* TODO, use inverse lookup hashtable? */
91 /* invalidate all cache entries */
102 * Initialize the relation map cache.
112 "LogicalRepRelMapContext",
115 /* Initialize the relation hash table. */
123 /* Watch for invalidation events. */
129 * Free the entry of a relation map cache.
141 if (remoterel->
natts > 0)
145 for (
i = 0;
i < remoterel->
natts;
i++)
158 * Add new entry or update existing entry in the relation map cache.
160 * Called when new relation mapping is sent by the publisher to update
161 * our expected view of incoming data from said publisher.
175 * HASH_ENTER returns the existing entry if present or creates a new one.
185 /* Make cached copy of the data */
193 for (
i = 0;
i < remoterel->
natts;
i++)
204 * Find attribute index in TupleDesc struct by attribute name.
206 * Returns -1 if not found.
213 for (
i = 0;
i < remoterel->
natts;
i++)
223 * Returns a comma-separated string of attribute names based on the provided
224 * relation and bitmap indicating which attributes to include.
250 * If attempting to replicate missing or generated columns, report an error.
251 * Prioritize 'missing' errors if both occur though the prioritization is
261 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
262 errmsg_plural(
"logical replication target relation \"%s.%s\" is missing replicated column: %s",
263 "logical replication target relation \"%s.%s\" is missing replicated columns: %s",
272 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
273 errmsg_plural(
"logical replication target relation \"%s.%s\" has incompatible generated column: %s",
274 "logical replication target relation \"%s.%s\" has incompatible generated columns: %s",
283 * Check if replica identity matches and mark the updatable flag.
285 * We allow for stricter replica identity (fewer columns) on subscriber as
286 * that will not stop us from finding unique tuple. IE, if publisher has
287 * identity (id,timestamp) and subscriber just (id) this will not be a
288 * problem, but in the opposite scenario it will.
290 * We just mark the relation entry as not updatable here if the local
291 * replica identity is found to be insufficient for applying
292 * updates/deletes (inserts don't care!) and leave it to
293 * check_relation_updatable() to throw the actual error if needed.
306 /* fallback to PK if no replica identity */
313 * If no replica identity index and no PK, the published table must
314 * have replica identity FULL.
316 if (idkey == NULL && remoterel->
replident != REPLICA_IDENTITY_FULL)
327 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
328 errmsg(
"logical replication target relation \"%s.%s\" uses "
329 "system columns in REPLICA IDENTITY index",
344 * Open the local relation associated with the remote one.
346 * Rebuilds the Relcache mapping if it was invalidated by local DDL.
358 /* Search for existing entry. */
363 elog(
ERROR,
"no relation map entry for remote relation ID %u",
368 /* Ensure we don't leak a relcache refcount. */
370 elog(
ERROR,
"remote relation ID %u is already open", remoteid);
373 * When opening and locking a relation, pending invalidation messages are
374 * processed which can invalidate the relation. Hence, if the entry is
375 * currently considered valid, try to open the local relation by OID and
376 * see if invalidation ensues.
383 /* Table was renamed or dropped. */
388 /* Note we release the no-longer-useful lock here. */
395 * If the entry has been marked invalid since we last had lock on it,
396 * re-open the local relation by name and rebuild all derived data.
407 /* Release the no-longer-useful attrmap, if any. */
414 /* Try to find and lock the relation by name. */
420 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
421 errmsg(
"logical replication target relation \"%s.%s\" does not exist",
426 /* Check for supported relkind. */
431 * Build the mapping of local attribute numbers to remote attribute
432 * numbers and validate that we don't miss any replicated columns as
433 * that would result in potentially unwanted data loss.
440 /* check and report missing attrs, if any */
447 if (attr->attisdropped)
459 /* Remember which subscriber columns are generated. */
460 if (attr->attgenerated)
475 * Set if the table's replica identity is enough to apply
481 * Finding a usable index is an infrequent task. It occurs when an
482 * operation is first performed on the relation, or after invalidation
483 * of the relation cache entry (such as ANALYZE or CREATE/DROP index
492 if (entry->
state != SUBREL_STATE_READY)
501 * Close the previously opened logical relation.
511 * Partition cache: look up partition LogicalRepRelMapEntry's
513 * Unlike relation map cache, this is keyed by partition OID, not remote
514 * relation OID, because we only have to use this cache in the case where
515 * partitions are not directly mapped to any remote relation, such as when
516 * replication is occurring with one of their ancestors as target.
520 * Relcache invalidation callback
527 /* Just to be sure. */
537 /* TODO, use inverse lookup hashtable? */
550 /* invalidate all cache entries */
561 * Reset the entries in the partition map that refer to remoterel.
563 * Called when new relation mapping is sent by the publisher to update our
564 * expected view of incoming data from said publisher.
566 * Note that we don't update the remoterel information in the entry here,
567 * we will update the information in logicalrep_partition_open to avoid
595 * Initialize the partition map cache.
605 "LogicalRepPartMapContext",
608 /* Initialize the relation hash table. */
609 ctl.keysize =
sizeof(
Oid);
/* partition OID */
616 /* Watch for invalidation events. */
622 * logicalrep_partition_open
624 * Returned entry reuses most of the values of the root table's entry, save
625 * the attribute map, which can be different for the partition. However,
626 * we must physically copy all the data, in case the root table's entry
627 * gets freed/rebuilt.
629 * Note there's no logicalrep_partition_close, because the caller closes the
630 * component relation.
647 /* Search for existing entry. */
655 * We must always overwrite entry->localrel with the latest partition
656 * Relation pointer, because the Relation pointed to by the old value may
657 * have been cleared after the caller would have closed the partition
658 * relation after the last use of this entry. Note that localrelvalid is
659 * only updated by the relcache invalidation callback, so it may still be
660 * true irrespective of whether the Relation pointed to by localrel has
661 * been cleared or not.
669 /* Switch to longer-lived context. */
678 /* Release the no-longer-useful attrmap, if any. */
689 /* Remote relation is copied as-is from the root entry. */
696 for (
i = 0;
i < remoterel->
natts;
i++)
709 * If the partition's attributes don't match the root relation's, we'll
710 * need to make a new attrmap which maps partition attribute numbers to
711 * remoterel's, instead of the original which maps root relation's
712 * attribute numbers to remoterel's.
714 * Note that 'map' which comes from the tuple routing data structure
715 * contains 1-based attribute numbers (of the parent relation). However,
716 * the map in 'entry', a logical replication data structure, contains
717 * 0-based attribute numbers (of the remote relation).
728 /* 0 means it's a dropped attribute. See comments atop AttrMap. */
737 /* Lacking copy_attmap, do this the hard way. */
743 /* Set if the table's replica identity is enough to apply update/delete. */
746 /* state and statelsn are left set to 0. */
750 * Finding a usable index is an infrequent task. It occurs when an
751 * operation is first performed on the relation, or after invalidation of
752 * the relation cache entry (such as ANALYZE or CREATE/DROP index on the
755 * We also prefer to run this code on the oldctx so that we do not leak
756 * anything in the LogicalRepPartMapContext (hence CacheMemoryContext).
767 * Returns the oid of an index that can be used by the apply worker to scan
770 * We expect to call this function when REPLICA IDENTITY FULL is defined for
771 * the remote relation.
773 * If no suitable index is found, returns InvalidOid.
789 /* Return the first eligible index found */
798 * Returns true if the index is usable for replica identity full.
800 * The index must have an equal strategy for each key column, be non-partial,
801 * and the leftmost field must be a column (not an expression) that references
802 * the remote relation column. These limitations help to keep the index scan
803 * similar to PK/RI index scans.
805 * attrmap is a map of local attributes to remote ones. We can consult this
806 * map to check whether the local index attribute has a corresponding remote
809 * Note that the limitations of index scans for replica identity full only
810 * adheres to a subset of the limitations of PK/RI. For example, we support
811 * columns that are marked as [NULL] or we are not interested in the [NOT
812 * DEFERRABLE] aspect of constraints here. It works for us because we always
813 * compare the tuples for non-PK/RI index scans. See
814 * RelationFindReplTupleByIndex().
816 * XXX: To support partial indexes, the required changes are likely to be larger.
817 * If none of the tuples satisfy the expression for the index scan, we fall-back
818 * to sequential execution, which might not be a good idea in some cases.
826 /* The index must not be a partial index */
834 Anum_pg_index_indclass));
836 /* Ensure that the index has a valid equal strategy for each key column */
837 for (
int i = 0;
i < idxrel->
rd_index->indnkeyatts;
i++)
847 * For indexes other than PK and REPLICA IDENTITY, we need to match the
848 * local and remote tuples. The equality routine tuples_equal() cannot
849 * accept a data type where the type cache cannot provide an equality
861 /* The leftmost index field must not be an expression */
862 keycol = idxrel->
rd_index->indkey.values[0];
867 * And the leftmost index field must reference the remote relation column.
868 * This is because if it doesn't, the sequential scan is favorable over
869 * index scan in most cases.
876 * The given index access method must implement "amgettuple", which will
877 * be used later to fetch the tuples. See RelationFindReplTupleByIndex().
886 * Return the OID of the replica identity index if one is defined;
887 * the OID of the PK if one exists and is not deferrable;
888 * otherwise, InvalidOid.
904 * Returns the index oid if we can use an index for subscriber. Otherwise,
905 * returns InvalidOid.
914 * We never need index oid for partitioned tables, always rely on leaf
917 if (localrel->
rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
921 * Simple case, we already have a primary key or a replica identity index.
927 if (remoterel->
replident == REPLICA_IDENTITY_FULL)
930 * We are looking for one more opportunity for using an index. If
931 * there are any indexes defined on the local relation, try to pick a
934 * The index selection safely assumes that all the columns are going
935 * to be available for the index scan given that remote relation has
936 * replica identity full.
938 * Note that we are not using the planner to find the cheapest method
939 * to scan the relation as that would require us to either use lower
940 * level planner functions which would be a maintenance burden in the
941 * long run or use the full-fledged planner which could cause
StrategyNumber IndexAmTranslateCompareType(CompareType cmptype, Oid amoid, Oid opfamily, bool missing_ok)
IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
void free_attrmap(AttrMap *map)
AttrMap * make_attrmap(int maplen)
#define AttributeNumberIsValid(attributeNumber)
#define AttrNumberGetAttrOffset(attNum)
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
Subscription * MySubscription
int bms_next_member(const Bitmapset *a, int prevbit)
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Bitmapset * bms_del_member(Bitmapset *a, int x)
void bms_free(Bitmapset *a)
int bms_num_members(const Bitmapset *a)
bool bms_is_member(int x, const Bitmapset *a)
Bitmapset * bms_add_member(Bitmapset *a, int x)
Bitmapset * bms_copy(const Bitmapset *a)
#define OidIsValid(objectId)
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
void * hash_seq_search(HASH_SEQ_STATUS *status)
void hash_seq_term(HASH_SEQ_STATUS *status)
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void CheckSubscriptionRelkind(char relkind, const char *nspname, const char *relname)
Assert(PointerIsAligned(start, uint64))
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
void index_close(Relation relation, LOCKMODE lockmode)
Relation index_open(Oid relationId, LOCKMODE lockmode)
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
struct LogicalRepRelMapEntry LogicalRepRelMapEntry
Oid get_opclass_family(Oid opclass)
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
char * pstrdup(const char *in)
void pfree(void *pointer)
MemoryContext CacheMemoryContext
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
#define RangeVarGetRelid(relation, lockmode, missing_ok)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
FormData_pg_attribute * Form_pg_attribute
#define foreach_oid(var, lst)
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
static Pointer DatumGetPointer(Datum X)
#define RelationGetRelid(relation)
#define RelationGetDescr(relation)
List * RelationGetIndexList(Relation relation)
Oid RelationGetPrimaryKeyIndex(Relation relation, bool deferrable_ok)
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Oid RelationGetReplicaIndex(Relation relation)
@ INDEX_ATTR_BITMAP_PRIMARY_KEY
@ INDEX_ATTR_BITMAP_IDENTITY_KEY
static MemoryContext LogicalRepPartMapContext
void logicalrep_partmap_reset_relmap(LogicalRepRelation *remoterel)
static void logicalrep_partmap_init(void)
static void logicalrep_report_missing_or_gen_attrs(LogicalRepRelation *remoterel, Bitmapset *missingatts, Bitmapset *generatedatts)
LogicalRepRelMapEntry * logicalrep_partition_open(LogicalRepRelMapEntry *root, Relation partrel, AttrMap *map)
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
struct LogicalRepPartMapEntry LogicalRepPartMapEntry
static char * logicalrep_get_attrs_str(LogicalRepRelation *remoterel, Bitmapset *atts)
bool IsIndexUsableForReplicaIdentityFull(Relation idxrel, AttrMap *attrmap)
static void logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid)
static HTAB * LogicalRepPartMap
static HTAB * LogicalRepRelMap
static void logicalrep_rel_mark_updatable(LogicalRepRelMapEntry *entry)
static MemoryContext LogicalRepRelMapContext
Oid GetRelationIdentityOrPK(Relation rel)
void logicalrep_relmap_update(LogicalRepRelation *remoterel)
static void logicalrep_relmap_init(void)
static int logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
static Oid FindUsableIndexForReplicaIdentityFull(Relation localrel, AttrMap *attrmap)
static void logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
void logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
LogicalRepRelMapEntry * logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
static Oid FindLogicalRepLocalIndex(Relation localrel, LogicalRepRelation *remoterel, AttrMap *attrMap)
void appendStringInfo(StringInfo str, const char *fmt,...)
void appendStringInfoString(StringInfo str, const char *s)
void initStringInfo(StringInfo str)
amgettuple_function amgettuple
LogicalRepRelMapEntry relmapentry
LogicalRepRelation remoterel
struct HeapTupleData * rd_indextuple
Oid values[FLEXIBLE_ARRAY_MEMBER]
#define FirstLowInvalidHeapAttributeNumber
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Relation try_table_open(Oid relationId, LOCKMODE lockmode)
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
#define TYPECACHE_EQ_OPR_FINFO