PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
logicalrelation.h File Reference
Include dependency graph for logicalrelation.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  LogicalRepRelMapEntry
 

Typedefs

typedef struct LogicalRepRelMapEntry LogicalRepRelMapEntry
 

Functions

void logicalrep_relmap_update (LogicalRepRelation *remoterel)
 
void logicalrep_partmap_reset_relmap (LogicalRepRelation *remoterel)
 
LogicalRepRelMapEntrylogicalrep_rel_open (LogicalRepRelId remoteid, LOCKMODE lockmode)
 
LogicalRepRelMapEntrylogicalrep_partition_open (LogicalRepRelMapEntry *root, Relation partrel, AttrMap *map)
 
void logicalrep_rel_close (LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
 
bool IsIndexUsableForReplicaIdentityFull (Relation idxrel, AttrMap *attrmap)
 
Oid GetRelationIdentityOrPK (Relation rel)
 

Typedef Documentation

◆ LogicalRepRelMapEntry

Function Documentation

◆ GetRelationIdentityOrPK()

Oid GetRelationIdentityOrPK ( Relation  rel)

Definition at line 903 of file relation.c.

904{
905 Oid idxoid;
906
907 idxoid = RelationGetReplicaIndex(rel);
908
909 if (!OidIsValid(idxoid))
910 idxoid = RelationGetPrimaryKeyIndex(rel, false);
911
912 return idxoid;
913}
#define OidIsValid(objectId)
Definition: c.h:778
unsigned int Oid
Definition: postgres_ext.h:32
Oid RelationGetPrimaryKeyIndex(Relation relation, bool deferrable_ok)
Definition: relcache.c:5047
Oid RelationGetReplicaIndex(Relation relation)
Definition: relcache.c:5072

References OidIsValid, RelationGetPrimaryKeyIndex(), and RelationGetReplicaIndex().

Referenced by build_tuple_value_details(), check_relation_updatable(), FindLogicalRepLocalIndex(), FindReplTupleInLocalRel(), RelationFindDeletedTupleInfoByIndex(), and RelationFindReplTupleByIndex().

◆ IsIndexUsableForReplicaIdentityFull()

bool IsIndexUsableForReplicaIdentityFull ( Relation  idxrel,
AttrMap attrmap 
)

Definition at line 833 of file relation.c.

834{
835 AttrNumber keycol;
836 oidvector *indclass;
837
838 /* The index must not be a partial index */
839 if (!heap_attisnull(idxrel->rd_indextuple, Anum_pg_index_indpred, NULL))
840 return false;
841
842 Assert(idxrel->rd_index->indnatts >= 1);
843
844 indclass = (oidvector *) DatumGetPointer(SysCacheGetAttrNotNull(INDEXRELID,
845 idxrel->rd_indextuple,
846 Anum_pg_index_indclass));
847
848 /* Ensure that the index has a valid equal strategy for each key column */
849 for (int i = 0; i < idxrel->rd_index->indnkeyatts; i++)
850 {
851 Oid opfamily;
852
853 opfamily = get_opclass_family(indclass->values[i]);
854 if (IndexAmTranslateCompareType(COMPARE_EQ, idxrel->rd_rel->relam, opfamily, true) == InvalidStrategy)
855 return false;
856 }
857
858 /*
859 * For indexes other than PK and REPLICA IDENTITY, we need to match the
860 * local and remote tuples. The equality routine tuples_equal() cannot
861 * accept a data type where the type cache cannot provide an equality
862 * operator.
863 */
864 for (int i = 0; i < idxrel->rd_att->natts; i++)
865 {
866 TypeCacheEntry *typentry;
867
868 typentry = lookup_type_cache(TupleDescAttr(idxrel->rd_att, i)->atttypid, TYPECACHE_EQ_OPR_FINFO);
869 if (!OidIsValid(typentry->eq_opr_finfo.fn_oid))
870 return false;
871 }
872
873 /* The leftmost index field must not be an expression */
874 keycol = idxrel->rd_index->indkey.values[0];
875 if (!AttributeNumberIsValid(keycol))
876 return false;
877
878 /*
879 * And the leftmost index field must reference the remote relation column.
880 * This is because if it doesn't, the sequential scan is favorable over
881 * index scan in most cases.
882 */
883 if (attrmap->maplen <= AttrNumberGetAttrOffset(keycol) ||
884 attrmap->attnums[AttrNumberGetAttrOffset(keycol)] < 0)
885 return false;
886
887 /*
888 * The given index access method must implement "amgettuple", which will
889 * be used later to fetch the tuples. See RelationFindReplTupleByIndex().
890 */
891 if (GetIndexAmRoutineByAmId(idxrel->rd_rel->relam, false)->amgettuple == NULL)
892 return false;
893
894 return true;
895}
StrategyNumber IndexAmTranslateCompareType(CompareType cmptype, Oid amoid, Oid opfamily, bool missing_ok)
Definition: amapi.c:161
IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition: amapi.c:69
int16 AttrNumber
Definition: attnum.h:21
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
#define AttrNumberGetAttrOffset(attNum)
Definition: attnum.h:51
@ COMPARE_EQ
Definition: cmptype.h:36
Assert(PointerIsAligned(start, uint64))
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:456
int i
Definition: isn.c:77
Oid get_opclass_family(Oid opclass)
Definition: lsyscache.c:1309
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322
#define InvalidStrategy
Definition: stratnum.h:24
int maplen
Definition: attmap.h:37
AttrNumber * attnums
Definition: attmap.h:36
Oid fn_oid
Definition: fmgr.h:59
amgettuple_function amgettuple
Definition: amapi.h:311
struct HeapTupleData * rd_indextuple
Definition: rel.h:194
TupleDesc rd_att
Definition: rel.h:112
Form_pg_index rd_index
Definition: rel.h:192
Form_pg_class rd_rel
Definition: rel.h:111
FmgrInfo eq_opr_finfo
Definition: typcache.h:76
Definition: c.h:735
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:742
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:625
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:386
#define TYPECACHE_EQ_OPR_FINFO
Definition: typcache.h:143

References IndexAmRoutine::amgettuple, Assert(), AttrMap::attnums, AttributeNumberIsValid, AttrNumberGetAttrOffset, COMPARE_EQ, DatumGetPointer(), TypeCacheEntry::eq_opr_finfo, FmgrInfo::fn_oid, get_opclass_family(), GetIndexAmRoutineByAmId(), heap_attisnull(), i, IndexAmTranslateCompareType(), InvalidStrategy, lookup_type_cache(), AttrMap::maplen, TupleDescData::natts, OidIsValid, RelationData::rd_att, RelationData::rd_index, RelationData::rd_indextuple, RelationData::rd_rel, SysCacheGetAttrNotNull(), TupleDescAttr(), TYPECACHE_EQ_OPR_FINFO, and oidvector::values.

Referenced by FindReplTupleInLocalRel(), and FindUsableIndexForReplicaIdentityFull().

◆ logicalrep_partition_open()

LogicalRepRelMapEntry * logicalrep_partition_open ( LogicalRepRelMapEntry root,
Relation  partrel,
AttrMap map 
)

Definition at line 645 of file relation.c.

647{
649 LogicalRepPartMapEntry *part_entry;
650 LogicalRepRelation *remoterel = &root->remoterel;
651 Oid partOid = RelationGetRelid(partrel);
652 AttrMap *attrmap = root->attrmap;
653 bool found;
654 MemoryContext oldctx;
655
656 if (LogicalRepPartMap == NULL)
658
659 /* Search for existing entry. */
661 &partOid,
662 HASH_ENTER, &found);
663
664 entry = &part_entry->relmapentry;
665
666 /*
667 * We must always overwrite entry->localrel with the latest partition
668 * Relation pointer, because the Relation pointed to by the old value may
669 * have been cleared after the caller would have closed the partition
670 * relation after the last use of this entry. Note that localrelvalid is
671 * only updated by the relcache invalidation callback, so it may still be
672 * true irrespective of whether the Relation pointed to by localrel has
673 * been cleared or not.
674 */
675 if (found && entry->localrelvalid)
676 {
677 entry->localrel = partrel;
678 return entry;
679 }
680
681 /* Switch to longer-lived context. */
683
684 if (!found)
685 {
686 memset(part_entry, 0, sizeof(LogicalRepPartMapEntry));
687 part_entry->partoid = partOid;
688 }
689
690 /* Release the no-longer-useful attrmap, if any. */
691 if (entry->attrmap)
692 {
693 free_attrmap(entry->attrmap);
694 entry->attrmap = NULL;
695 }
696
697 if (!entry->remoterel.remoteid)
698 {
699 int i;
700
701 /* Remote relation is copied as-is from the root entry. */
702 entry->remoterel.remoteid = remoterel->remoteid;
703 entry->remoterel.nspname = pstrdup(remoterel->nspname);
704 entry->remoterel.relname = pstrdup(remoterel->relname);
705 entry->remoterel.natts = remoterel->natts;
706 entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
707 entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
708 for (i = 0; i < remoterel->natts; i++)
709 {
710 entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
711 entry->remoterel.atttyps[i] = remoterel->atttyps[i];
712 }
713 entry->remoterel.replident = remoterel->replident;
714 entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
715 }
716
717 entry->localrel = partrel;
718 entry->localreloid = partOid;
719
720 /*
721 * If the partition's attributes don't match the root relation's, we'll
722 * need to make a new attrmap which maps partition attribute numbers to
723 * remoterel's, instead of the original which maps root relation's
724 * attribute numbers to remoterel's.
725 *
726 * Note that 'map' which comes from the tuple routing data structure
727 * contains 1-based attribute numbers (of the parent relation). However,
728 * the map in 'entry', a logical replication data structure, contains
729 * 0-based attribute numbers (of the remote relation).
730 */
731 if (map)
732 {
733 AttrNumber attno;
734
735 entry->attrmap = make_attrmap(map->maplen);
736 for (attno = 0; attno < entry->attrmap->maplen; attno++)
737 {
738 AttrNumber root_attno = map->attnums[attno];
739
740 /* 0 means it's a dropped attribute. See comments atop AttrMap. */
741 if (root_attno == 0)
742 entry->attrmap->attnums[attno] = -1;
743 else
744 entry->attrmap->attnums[attno] = attrmap->attnums[root_attno - 1];
745 }
746 }
747 else
748 {
749 /* Lacking copy_attmap, do this the hard way. */
750 entry->attrmap = make_attrmap(attrmap->maplen);
751 memcpy(entry->attrmap->attnums, attrmap->attnums,
752 attrmap->maplen * sizeof(AttrNumber));
753 }
754
755 /* Set if the table's replica identity is enough to apply update/delete. */
757
758 /* state and statelsn are left set to 0. */
759 MemoryContextSwitchTo(oldctx);
760
761 /*
762 * Finding a usable index is an infrequent task. It occurs when an
763 * operation is first performed on the relation, or after invalidation of
764 * the relation cache entry (such as ANALYZE or CREATE/DROP index on the
765 * relation).
766 *
767 * We also prefer to run this code on the oldctx so that we do not leak
768 * anything in the LogicalRepPartMapContext (hence CacheMemoryContext).
769 */
770 entry->localindexoid = FindLogicalRepLocalIndex(partrel, remoterel,
771 entry->attrmap);
772
773 entry->localrelvalid = true;
774
775 return entry;
776}
void free_attrmap(AttrMap *map)
Definition: attmap.c:56
AttrMap * make_attrmap(int maplen)
Definition: attmap.c:40
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:122
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:952
@ HASH_ENTER
Definition: hsearch.h:114
char * pstrdup(const char *in)
Definition: mcxt.c:1759
void * palloc(Size size)
Definition: mcxt.c:1365
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
tree ctl root
Definition: radixtree.h:1857
#define RelationGetRelid(relation)
Definition: rel.h:515
static MemoryContext LogicalRepPartMapContext
Definition: relation.c:49
static void logicalrep_partmap_init(void)
Definition: relation.c:610
static HTAB * LogicalRepPartMap
Definition: relation.c:50
static void logicalrep_rel_mark_updatable(LogicalRepRelMapEntry *entry)
Definition: relation.c:307
static Oid FindLogicalRepLocalIndex(Relation localrel, LogicalRepRelation *remoterel, AttrMap *attrMap)
Definition: relation.c:920
Definition: attmap.h:35
LogicalRepRelMapEntry relmapentry
Definition: relation.c:54
LogicalRepRelation remoterel
LogicalRepRelId remoteid
Definition: logicalproto.h:107
Bitmapset * attkeys
Definition: logicalproto.h:115

References LogicalRepRelation::attkeys, LogicalRepRelation::attnames, AttrMap::attnums, LogicalRepRelMapEntry::attrmap, LogicalRepRelation::atttyps, bms_copy(), FindLogicalRepLocalIndex(), free_attrmap(), HASH_ENTER, hash_search(), i, LogicalRepRelMapEntry::localindexoid, LogicalRepRelMapEntry::localrel, LogicalRepRelMapEntry::localreloid, LogicalRepRelMapEntry::localrelvalid, logicalrep_partmap_init(), logicalrep_rel_mark_updatable(), LogicalRepPartMap, LogicalRepPartMapContext, make_attrmap(), AttrMap::maplen, MemoryContextSwitchTo(), LogicalRepRelation::natts, LogicalRepRelation::nspname, palloc(), LogicalRepPartMapEntry::partoid, pstrdup(), RelationGetRelid, LogicalRepPartMapEntry::relmapentry, LogicalRepRelation::relname, LogicalRepRelation::remoteid, LogicalRepRelMapEntry::remoterel, LogicalRepRelation::replident, and root.

Referenced by apply_handle_tuple_routing().

◆ logicalrep_partmap_reset_relmap()

void logicalrep_partmap_reset_relmap ( LogicalRepRelation remoterel)

Definition at line 583 of file relation.c.

584{
585 HASH_SEQ_STATUS status;
586 LogicalRepPartMapEntry *part_entry;
588
589 if (LogicalRepPartMap == NULL)
590 return;
591
593 while ((part_entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
594 {
595 entry = &part_entry->relmapentry;
596
597 if (entry->remoterel.remoteid != remoterel->remoteid)
598 continue;
599
601
602 memset(entry, 0, sizeof(LogicalRepRelMapEntry));
603 }
604}
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1415
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1380
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:132

References hash_seq_init(), hash_seq_search(), logicalrep_relmap_free_entry(), LogicalRepPartMap, LogicalRepPartMapEntry::relmapentry, LogicalRepRelation::remoteid, and LogicalRepRelMapEntry::remoterel.

Referenced by apply_handle_relation().

◆ logicalrep_rel_close()

void logicalrep_rel_close ( LogicalRepRelMapEntry rel,
LOCKMODE  lockmode 
)

Definition at line 516 of file relation.c.

517{
518 table_close(rel->localrel, lockmode);
519 rel->localrel = NULL;
520}
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126

References LogicalRepRelMapEntry::localrel, and table_close().

Referenced by apply_handle_delete(), apply_handle_insert(), apply_handle_truncate(), apply_handle_update(), and copy_table().

◆ logicalrep_rel_open()

LogicalRepRelMapEntry * logicalrep_rel_open ( LogicalRepRelId  remoteid,
LOCKMODE  lockmode 
)

Definition at line 360 of file relation.c.

361{
363 bool found;
364 LogicalRepRelation *remoterel;
365
366 if (LogicalRepRelMap == NULL)
368
369 /* Search for existing entry. */
370 entry = hash_search(LogicalRepRelMap, &remoteid,
371 HASH_FIND, &found);
372
373 if (!found)
374 elog(ERROR, "no relation map entry for remote relation ID %u",
375 remoteid);
376
377 remoterel = &entry->remoterel;
378
379 /* Ensure we don't leak a relcache refcount. */
380 if (entry->localrel)
381 elog(ERROR, "remote relation ID %u is already open", remoteid);
382
383 /*
384 * When opening and locking a relation, pending invalidation messages are
385 * processed which can invalidate the relation. Hence, if the entry is
386 * currently considered valid, try to open the local relation by OID and
387 * see if invalidation ensues.
388 */
389 if (entry->localrelvalid)
390 {
391 entry->localrel = try_table_open(entry->localreloid, lockmode);
392 if (!entry->localrel)
393 {
394 /* Table was renamed or dropped. */
395 entry->localrelvalid = false;
396 }
397 else if (!entry->localrelvalid)
398 {
399 /* Note we release the no-longer-useful lock here. */
400 table_close(entry->localrel, lockmode);
401 entry->localrel = NULL;
402 }
403 }
404
405 /*
406 * If the entry has been marked invalid since we last had lock on it,
407 * re-open the local relation by name and rebuild all derived data.
408 */
409 if (!entry->localrelvalid)
410 {
411 Oid relid;
412 TupleDesc desc;
413 MemoryContext oldctx;
414 int i;
415 Bitmapset *missingatts;
416 Bitmapset *generatedattrs = NULL;
417
418 /* Release the no-longer-useful attrmap, if any. */
419 if (entry->attrmap)
420 {
421 free_attrmap(entry->attrmap);
422 entry->attrmap = NULL;
423 }
424
425 /* Try to find and lock the relation by name. */
426 relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
427 remoterel->relname, -1),
428 lockmode, true);
429 if (!OidIsValid(relid))
431 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
432 errmsg("logical replication target relation \"%s.%s\" does not exist",
433 remoterel->nspname, remoterel->relname)));
434 entry->localrel = table_open(relid, NoLock);
435 entry->localreloid = relid;
436
437 /* Check for supported relkind. */
439 remoterel->relkind,
440 remoterel->nspname, remoterel->relname);
441
442 /*
443 * Build the mapping of local attribute numbers to remote attribute
444 * numbers and validate that we don't miss any replicated columns as
445 * that would result in potentially unwanted data loss.
446 */
447 desc = RelationGetDescr(entry->localrel);
449 entry->attrmap = make_attrmap(desc->natts);
450 MemoryContextSwitchTo(oldctx);
451
452 /* check and report missing attrs, if any */
453 missingatts = bms_add_range(NULL, 0, remoterel->natts - 1);
454 for (i = 0; i < desc->natts; i++)
455 {
456 int attnum;
457 Form_pg_attribute attr = TupleDescAttr(desc, i);
458
459 if (attr->attisdropped)
460 {
461 entry->attrmap->attnums[i] = -1;
462 continue;
463 }
464
466 NameStr(attr->attname));
467
468 entry->attrmap->attnums[i] = attnum;
469 if (attnum >= 0)
470 {
471 /* Remember which subscriber columns are generated. */
472 if (attr->attgenerated)
473 generatedattrs = bms_add_member(generatedattrs, attnum);
474
475 missingatts = bms_del_member(missingatts, attnum);
476 }
477 }
478
479 logicalrep_report_missing_or_gen_attrs(remoterel, missingatts,
480 generatedattrs);
481
482 /* be tidy */
483 bms_free(generatedattrs);
484 bms_free(missingatts);
485
486 /*
487 * Set if the table's replica identity is enough to apply
488 * update/delete.
489 */
491
492 /*
493 * Finding a usable index is an infrequent task. It occurs when an
494 * operation is first performed on the relation, or after invalidation
495 * of the relation cache entry (such as ANALYZE or CREATE/DROP index
496 * on the relation).
497 */
498 entry->localindexoid = FindLogicalRepLocalIndex(entry->localrel, remoterel,
499 entry->attrmap);
500
501 entry->localrelvalid = true;
502 }
503
504 if (entry->state != SUBREL_STATE_READY)
506 entry->localreloid,
507 &entry->statelsn);
508
509 return entry;
510}
Subscription * MySubscription
Definition: worker.c:479
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition: bitmapset.c:1019
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
#define NameStr(name)
Definition: c.h:755
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
void CheckSubscriptionRelkind(char localrelkind, char remoterelkind, const char *nspname, const char *relname)
@ HASH_FIND
Definition: hsearch.h:113
#define NoLock
Definition: lockdefs.h:34
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:473
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:98
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
#define RelationGetDescr(relation)
Definition: rel.h:541
static void logicalrep_report_missing_or_gen_attrs(LogicalRepRelation *remoterel, Bitmapset *missingatts, Bitmapset *generatedatts)
Definition: relation.c:266
static HTAB * LogicalRepRelMap
Definition: relation.c:36
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:34
static void logicalrep_relmap_init(void)
Definition: relation.c:105
static int logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
Definition: relation.c:220
Relation try_table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:60
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References attnum, AttrMap::attnums, LogicalRepRelMapEntry::attrmap, bms_add_member(), bms_add_range(), bms_del_member(), bms_free(), CheckSubscriptionRelkind(), elog, ereport, errcode(), errmsg(), ERROR, FindLogicalRepLocalIndex(), free_attrmap(), GetSubscriptionRelState(), HASH_FIND, hash_search(), i, LogicalRepRelMapEntry::localindexoid, LogicalRepRelMapEntry::localrel, LogicalRepRelMapEntry::localreloid, LogicalRepRelMapEntry::localrelvalid, logicalrep_rel_att_by_name(), logicalrep_rel_mark_updatable(), logicalrep_relmap_init(), logicalrep_report_missing_or_gen_attrs(), LogicalRepRelMap, LogicalRepRelMapContext, make_attrmap(), makeRangeVar(), MemoryContextSwitchTo(), MySubscription, NameStr, TupleDescData::natts, LogicalRepRelation::natts, NoLock, LogicalRepRelation::nspname, Subscription::oid, OidIsValid, RangeVarGetRelid, RelationData::rd_rel, RelationGetDescr, LogicalRepRelation::relkind, LogicalRepRelation::relname, LogicalRepRelMapEntry::remoterel, LogicalRepRelMapEntry::state, LogicalRepRelMapEntry::statelsn, table_close(), table_open(), try_table_open(), and TupleDescAttr().

Referenced by apply_handle_delete(), apply_handle_insert(), apply_handle_truncate(), apply_handle_update(), and copy_table().

◆ logicalrep_relmap_update()

void logicalrep_relmap_update ( LogicalRepRelation remoterel)

Definition at line 164 of file relation.c.

165{
166 MemoryContext oldctx;
168 bool found;
169 int i;
170
171 if (LogicalRepRelMap == NULL)
173
174 /*
175 * HASH_ENTER returns the existing entry if present or creates a new one.
176 */
177 entry = hash_search(LogicalRepRelMap, &remoterel->remoteid,
178 HASH_ENTER, &found);
179
180 if (found)
182
183 memset(entry, 0, sizeof(LogicalRepRelMapEntry));
184
185 /* Make cached copy of the data */
187 entry->remoterel.remoteid = remoterel->remoteid;
188 entry->remoterel.nspname = pstrdup(remoterel->nspname);
189 entry->remoterel.relname = pstrdup(remoterel->relname);
190 entry->remoterel.natts = remoterel->natts;
191 entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
192 entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
193 for (i = 0; i < remoterel->natts; i++)
194 {
195 entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
196 entry->remoterel.atttyps[i] = remoterel->atttyps[i];
197 }
198 entry->remoterel.replident = remoterel->replident;
199
200 /*
201 * XXX The walsender currently does not transmit the relkind of the remote
202 * relation when replicating changes. Since we support replicating only
203 * table changes at present, we default to initializing relkind as
204 * RELKIND_RELATION. This is needed in CheckSubscriptionRelkind() to check
205 * if the publisher and subscriber relation kinds are compatible.
206 */
207 entry->remoterel.relkind =
208 (remoterel->relkind == 0) ? RELKIND_RELATION : remoterel->relkind;
209
210 entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
211 MemoryContextSwitchTo(oldctx);
212}

References LogicalRepRelation::attkeys, LogicalRepRelation::attnames, LogicalRepRelation::atttyps, bms_copy(), HASH_ENTER, hash_search(), i, logicalrep_relmap_free_entry(), logicalrep_relmap_init(), LogicalRepRelMap, LogicalRepRelMapContext, MemoryContextSwitchTo(), LogicalRepRelation::natts, LogicalRepRelation::nspname, palloc(), pstrdup(), LogicalRepRelation::relkind, LogicalRepRelation::relname, LogicalRepRelation::remoteid, LogicalRepRelMapEntry::remoterel, and LogicalRepRelation::replident.

Referenced by apply_handle_relation(), and copy_table().