PostgreSQL Source Code  git master
relation.c File Reference
Include dependency graph for relation.c:

Go to the source code of this file.

Data Structures

struct  LogicalRepPartMapEntry
 

Typedefs

typedef struct LogicalRepPartMapEntry LogicalRepPartMapEntry
 

Functions

static Oid FindLogicalRepLocalIndex (Relation localrel, LogicalRepRelation *remoterel, AttrMap *attrMap)
 
static void logicalrep_relmap_invalidate_cb (Datum arg, Oid reloid)
 
static void logicalrep_relmap_init (void)
 
static void logicalrep_relmap_free_entry (LogicalRepRelMapEntry *entry)
 
void logicalrep_relmap_update (LogicalRepRelation *remoterel)
 
static int logicalrep_rel_att_by_name (LogicalRepRelation *remoterel, const char *attname)
 
static void logicalrep_report_missing_attrs (LogicalRepRelation *remoterel, Bitmapset *missingatts)
 
static void logicalrep_rel_mark_updatable (LogicalRepRelMapEntry *entry)
 
LogicalRepRelMapEntrylogicalrep_rel_open (LogicalRepRelId remoteid, LOCKMODE lockmode)
 
void logicalrep_rel_close (LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
 
static void logicalrep_partmap_invalidate_cb (Datum arg, Oid reloid)
 
void logicalrep_partmap_reset_relmap (LogicalRepRelation *remoterel)
 
static void logicalrep_partmap_init (void)
 
LogicalRepRelMapEntrylogicalrep_partition_open (LogicalRepRelMapEntry *root, Relation partrel, AttrMap *map)
 
static Oid FindUsableIndexForReplicaIdentityFull (Relation localrel, AttrMap *attrmap)
 
bool IsIndexUsableForReplicaIdentityFull (IndexInfo *indexInfo, AttrMap *attrmap)
 
Oid GetRelationIdentityOrPK (Relation rel)
 

Variables

static MemoryContext LogicalRepRelMapContext = NULL
 
static HTABLogicalRepRelMap = NULL
 
static MemoryContext LogicalRepPartMapContext = NULL
 
static HTABLogicalRepPartMap = NULL
 

Typedef Documentation

◆ LogicalRepPartMapEntry

Function Documentation

◆ FindLogicalRepLocalIndex()

static Oid FindLogicalRepLocalIndex ( Relation  localrel,
LogicalRepRelation remoterel,
AttrMap attrMap 
)
static

Definition at line 868 of file relation.c.

870 {
871  Oid idxoid;
872 
873  /*
874  * We never need index oid for partitioned tables, always rely on leaf
875  * partition's index.
876  */
877  if (localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
878  return InvalidOid;
879 
880  /*
881  * Simple case, we already have a primary key or a replica identity index.
882  */
883  idxoid = GetRelationIdentityOrPK(localrel);
884  if (OidIsValid(idxoid))
885  return idxoid;
886 
887  if (remoterel->replident == REPLICA_IDENTITY_FULL)
888  {
889  /*
890  * We are looking for one more opportunity for using an index. If
891  * there are any indexes defined on the local relation, try to pick a
892  * suitable index.
893  *
894  * The index selection safely assumes that all the columns are going
895  * to be available for the index scan given that remote relation has
896  * replica identity full.
897  *
898  * Note that we are not using the planner to find the cheapest method
899  * to scan the relation as that would require us to either use lower
900  * level planner functions which would be a maintenance burden in the
901  * long run or use the full-fledged planner which could cause
902  * overhead.
903  */
904  return FindUsableIndexForReplicaIdentityFull(localrel, attrMap);
905  }
906 
907  return InvalidOid;
908 }
#define OidIsValid(objectId)
Definition: c.h:775
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
Oid GetRelationIdentityOrPK(Relation rel)
Definition: relation.c:851
static Oid FindUsableIndexForReplicaIdentityFull(Relation localrel, AttrMap *attrmap)
Definition: relation.c:745
Form_pg_class rd_rel
Definition: rel.h:111

References FindUsableIndexForReplicaIdentityFull(), GetRelationIdentityOrPK(), InvalidOid, OidIsValid, RelationData::rd_rel, and LogicalRepRelation::replident.

Referenced by logicalrep_partition_open(), and logicalrep_rel_open().

◆ FindUsableIndexForReplicaIdentityFull()

static Oid FindUsableIndexForReplicaIdentityFull ( Relation  localrel,
AttrMap attrmap 
)
static

Definition at line 745 of file relation.c.

746 {
747  List *idxlist = RelationGetIndexList(localrel);
748 
749  foreach_oid(idxoid, idxlist)
750  {
751  bool isUsableIdx;
752  Relation idxRel;
753  IndexInfo *idxInfo;
754 
755  idxRel = index_open(idxoid, AccessShareLock);
756  idxInfo = BuildIndexInfo(idxRel);
757  isUsableIdx = IsIndexUsableForReplicaIdentityFull(idxInfo, attrmap);
758  index_close(idxRel, AccessShareLock);
759 
760  /* Return the first eligible index found */
761  if (isUsableIdx)
762  return idxoid;
763  }
764 
765  return InvalidOid;
766 }
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:2404
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
#define AccessShareLock
Definition: lockdefs.h:36
#define foreach_oid(var, lst)
Definition: pg_list.h:471
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4801
bool IsIndexUsableForReplicaIdentityFull(IndexInfo *indexInfo, AttrMap *attrmap)
Definition: relation.c:804
Definition: pg_list.h:54

References AccessShareLock, BuildIndexInfo(), foreach_oid, index_close(), index_open(), InvalidOid, IsIndexUsableForReplicaIdentityFull(), and RelationGetIndexList().

Referenced by FindLogicalRepLocalIndex().

◆ GetRelationIdentityOrPK()

Oid GetRelationIdentityOrPK ( Relation  rel)

Definition at line 851 of file relation.c.

852 {
853  Oid idxoid;
854 
855  idxoid = RelationGetReplicaIndex(rel);
856 
857  if (!OidIsValid(idxoid))
858  idxoid = RelationGetPrimaryKeyIndex(rel);
859 
860  return idxoid;
861 }
Oid RelationGetPrimaryKeyIndex(Relation relation)
Definition: relcache.c:4992
Oid RelationGetReplicaIndex(Relation relation)
Definition: relcache.c:5013

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

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

◆ IsIndexUsableForReplicaIdentityFull()

bool IsIndexUsableForReplicaIdentityFull ( IndexInfo indexInfo,
AttrMap attrmap 
)

Definition at line 804 of file relation.c.

805 {
806  AttrNumber keycol;
807 
808  /* Ensure that the index access method has a valid equal strategy */
810  return false;
811 
812  /* The index must not be a partial index */
813  if (indexInfo->ii_Predicate != NIL)
814  return false;
815 
816  Assert(indexInfo->ii_NumIndexAttrs >= 1);
817 
818  /* The leftmost index field must not be an expression */
819  keycol = indexInfo->ii_IndexAttrNumbers[0];
820  if (!AttributeNumberIsValid(keycol))
821  return false;
822 
823  /*
824  * And the leftmost index field must reference the remote relation column.
825  * This is because if it doesn't, the sequential scan is favorable over
826  * index scan in most cases.
827  */
828  if (attrmap->maplen <= AttrNumberGetAttrOffset(keycol) ||
829  attrmap->attnums[AttrNumberGetAttrOffset(keycol)] < 0)
830  return false;
831 
832 #ifdef USE_ASSERT_CHECKING
833  {
834  IndexAmRoutine *amroutine;
835 
836  /* The given index access method must implement amgettuple. */
837  amroutine = GetIndexAmRoutineByAmId(indexInfo->ii_Am, false);
838  Assert(amroutine->amgettuple != NULL);
839  }
840 #endif
841 
842  return true;
843 }
IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition: amapi.c:56
int16 AttrNumber
Definition: attnum.h:21
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
#define AttrNumberGetAttrOffset(attNum)
Definition: attnum.h:51
#define Assert(condition)
Definition: c.h:858
StrategyNumber get_equal_strategy_number_for_am(Oid am)
#define NIL
Definition: pg_list.h:68
#define InvalidStrategy
Definition: stratnum.h:24
int maplen
Definition: attmap.h:37
AttrNumber * attnums
Definition: attmap.h:36
amgettuple_function amgettuple
Definition: amapi.h:282
int ii_NumIndexAttrs
Definition: execnodes.h:184
Oid ii_Am
Definition: execnodes.h:206
AttrNumber ii_IndexAttrNumbers[INDEX_MAX_KEYS]
Definition: execnodes.h:186
List * ii_Predicate
Definition: execnodes.h:189

References IndexAmRoutine::amgettuple, Assert, AttrMap::attnums, AttributeNumberIsValid, AttrNumberGetAttrOffset, get_equal_strategy_number_for_am(), GetIndexAmRoutineByAmId(), IndexInfo::ii_Am, IndexInfo::ii_IndexAttrNumbers, IndexInfo::ii_NumIndexAttrs, IndexInfo::ii_Predicate, InvalidStrategy, AttrMap::maplen, and NIL.

Referenced by FindReplTupleInLocalRel(), and FindUsableIndexForReplicaIdentityFull().

◆ logicalrep_partition_open()

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

Definition at line 602 of file relation.c.

604 {
605  LogicalRepRelMapEntry *entry;
606  LogicalRepPartMapEntry *part_entry;
607  LogicalRepRelation *remoterel = &root->remoterel;
608  Oid partOid = RelationGetRelid(partrel);
609  AttrMap *attrmap = root->attrmap;
610  bool found;
611  MemoryContext oldctx;
612 
613  if (LogicalRepPartMap == NULL)
615 
616  /* Search for existing entry. */
618  &partOid,
619  HASH_ENTER, &found);
620 
621  entry = &part_entry->relmapentry;
622 
623  /*
624  * We must always overwrite entry->localrel with the latest partition
625  * Relation pointer, because the Relation pointed to by the old value may
626  * have been cleared after the caller would have closed the partition
627  * relation after the last use of this entry. Note that localrelvalid is
628  * only updated by the relcache invalidation callback, so it may still be
629  * true irrespective of whether the Relation pointed to by localrel has
630  * been cleared or not.
631  */
632  if (found && entry->localrelvalid)
633  {
634  entry->localrel = partrel;
635  return entry;
636  }
637 
638  /* Switch to longer-lived context. */
640 
641  if (!found)
642  {
643  memset(part_entry, 0, sizeof(LogicalRepPartMapEntry));
644  part_entry->partoid = partOid;
645  }
646 
647  /* Release the no-longer-useful attrmap, if any. */
648  if (entry->attrmap)
649  {
650  free_attrmap(entry->attrmap);
651  entry->attrmap = NULL;
652  }
653 
654  if (!entry->remoterel.remoteid)
655  {
656  int i;
657 
658  /* Remote relation is copied as-is from the root entry. */
659  entry->remoterel.remoteid = remoterel->remoteid;
660  entry->remoterel.nspname = pstrdup(remoterel->nspname);
661  entry->remoterel.relname = pstrdup(remoterel->relname);
662  entry->remoterel.natts = remoterel->natts;
663  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
664  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
665  for (i = 0; i < remoterel->natts; i++)
666  {
667  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
668  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
669  }
670  entry->remoterel.replident = remoterel->replident;
671  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
672  }
673 
674  entry->localrel = partrel;
675  entry->localreloid = partOid;
676 
677  /*
678  * If the partition's attributes don't match the root relation's, we'll
679  * need to make a new attrmap which maps partition attribute numbers to
680  * remoterel's, instead of the original which maps root relation's
681  * attribute numbers to remoterel's.
682  *
683  * Note that 'map' which comes from the tuple routing data structure
684  * contains 1-based attribute numbers (of the parent relation). However,
685  * the map in 'entry', a logical replication data structure, contains
686  * 0-based attribute numbers (of the remote relation).
687  */
688  if (map)
689  {
690  AttrNumber attno;
691 
692  entry->attrmap = make_attrmap(map->maplen);
693  for (attno = 0; attno < entry->attrmap->maplen; attno++)
694  {
695  AttrNumber root_attno = map->attnums[attno];
696 
697  /* 0 means it's a dropped attribute. See comments atop AttrMap. */
698  if (root_attno == 0)
699  entry->attrmap->attnums[attno] = -1;
700  else
701  entry->attrmap->attnums[attno] = attrmap->attnums[root_attno - 1];
702  }
703  }
704  else
705  {
706  /* Lacking copy_attmap, do this the hard way. */
707  entry->attrmap = make_attrmap(attrmap->maplen);
708  memcpy(entry->attrmap->attnums, attrmap->attnums,
709  attrmap->maplen * sizeof(AttrNumber));
710  }
711 
712  /* Set if the table's replica identity is enough to apply update/delete. */
714 
715  /* state and statelsn are left set to 0. */
716  MemoryContextSwitchTo(oldctx);
717 
718  /*
719  * Finding a usable index is an infrequent task. It occurs when an
720  * operation is first performed on the relation, or after invalidation of
721  * the relation cache entry (such as ANALYZE or CREATE/DROP index on the
722  * relation).
723  *
724  * We also prefer to run this code on the oldctx so that we do not leak
725  * anything in the LogicalRepPartMapContext (hence CacheMemoryContext).
726  */
727  entry->localindexoid = FindLogicalRepLocalIndex(partrel, remoterel,
728  entry->attrmap);
729 
730  entry->localrelvalid = true;
731 
732  return entry;
733 }
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:955
@ HASH_ENTER
Definition: hsearch.h:114
int i
Definition: isn.c:73
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void * palloc(Size size)
Definition: mcxt.c:1317
MemoryContextSwitchTo(old_ctx)
tree ctl root
Definition: radixtree.h:1886
#define RelationGetRelid(relation)
Definition: rel.h:505
static MemoryContext LogicalRepPartMapContext
Definition: relation.c:49
static void logicalrep_partmap_init(void)
Definition: relation.c:567
static HTAB * LogicalRepPartMap
Definition: relation.c:50
static void logicalrep_rel_mark_updatable(LogicalRepRelMapEntry *entry)
Definition: relation.c:274
static Oid FindLogicalRepLocalIndex(Relation localrel, LogicalRepRelation *remoterel, AttrMap *attrMap)
Definition: relation.c:868
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_init()

static void logicalrep_partmap_init ( void  )
static

Definition at line 567 of file relation.c.

568 {
569  HASHCTL ctl;
570 
574  "LogicalRepPartMapContext",
576 
577  /* Initialize the relation hash table. */
578  ctl.keysize = sizeof(Oid); /* partition OID */
579  ctl.entrysize = sizeof(LogicalRepPartMapEntry);
581 
582  LogicalRepPartMap = hash_create("logicalrep partition map cache", 64, &ctl,
584 
585  /* Watch for invalidation events. */
587  (Datum) 0);
588 }
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1558
MemoryContext CacheMemoryContext
Definition: mcxt.c:152
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
uintptr_t Datum
Definition: postgres.h:64
tree ctl
Definition: radixtree.h:1853
struct LogicalRepPartMapEntry LogicalRepPartMapEntry
static void logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:492

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CacheMemoryContext, CacheRegisterRelcacheCallback(), ctl, HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, logicalrep_partmap_invalidate_cb(), LogicalRepPartMap, and LogicalRepPartMapContext.

Referenced by logicalrep_partition_open().

◆ logicalrep_partmap_invalidate_cb()

static void logicalrep_partmap_invalidate_cb ( Datum  arg,
Oid  reloid 
)
static

Definition at line 492 of file relation.c.

493 {
494  LogicalRepPartMapEntry *entry;
495 
496  /* Just to be sure. */
497  if (LogicalRepPartMap == NULL)
498  return;
499 
500  if (reloid != InvalidOid)
501  {
502  HASH_SEQ_STATUS status;
503 
505 
506  /* TODO, use inverse lookup hashtable? */
507  while ((entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
508  {
509  if (entry->relmapentry.localreloid == reloid)
510  {
511  entry->relmapentry.localrelvalid = false;
512  hash_seq_term(&status);
513  break;
514  }
515  }
516  }
517  else
518  {
519  /* invalidate all cache entries */
520  HASH_SEQ_STATUS status;
521 
523 
524  while ((entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
525  entry->relmapentry.localrelvalid = false;
526  }
527 }
void hash_seq_term(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1471
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1395
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1385

References hash_seq_init(), hash_seq_search(), hash_seq_term(), InvalidOid, LogicalRepRelMapEntry::localreloid, LogicalRepRelMapEntry::localrelvalid, LogicalRepPartMap, and LogicalRepPartMapEntry::relmapentry.

Referenced by logicalrep_partmap_init().

◆ logicalrep_partmap_reset_relmap()

void logicalrep_partmap_reset_relmap ( LogicalRepRelation remoterel)

Definition at line 540 of file relation.c.

541 {
542  HASH_SEQ_STATUS status;
543  LogicalRepPartMapEntry *part_entry;
544  LogicalRepRelMapEntry *entry;
545 
546  if (LogicalRepPartMap == NULL)
547  return;
548 
550  while ((part_entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
551  {
552  entry = &part_entry->relmapentry;
553 
554  if (entry->remoterel.remoteid != remoterel->remoteid)
555  continue;
556 
558 
559  memset(entry, 0, sizeof(LogicalRepRelMapEntry));
560  }
561 }
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_att_by_name()

static int logicalrep_rel_att_by_name ( LogicalRepRelation remoterel,
const char *  attname 
)
static

Definition at line 209 of file relation.c.

210 {
211  int i;
212 
213  for (i = 0; i < remoterel->natts; i++)
214  {
215  if (strcmp(remoterel->attnames[i], attname) == 0)
216  return i;
217  }
218 
219  return -1;
220 }
NameData attname
Definition: pg_attribute.h:41

References attname, LogicalRepRelation::attnames, i, and LogicalRepRelation::natts.

Referenced by logicalrep_rel_open().

◆ logicalrep_rel_close()

void logicalrep_rel_close ( LogicalRepRelMapEntry rel,
LOCKMODE  lockmode 
)

Definition at line 473 of file relation.c.

474 {
475  table_close(rel->localrel, lockmode);
476  rel->localrel = NULL;
477 }
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_mark_updatable()

static void logicalrep_rel_mark_updatable ( LogicalRepRelMapEntry entry)
static

Definition at line 274 of file relation.c.

275 {
276  Bitmapset *idkey;
277  LogicalRepRelation *remoterel = &entry->remoterel;
278  int i;
279 
280  entry->updatable = true;
281 
282  idkey = RelationGetIndexAttrBitmap(entry->localrel,
284  /* fallback to PK if no replica identity */
285  if (idkey == NULL)
286  {
287  idkey = RelationGetIndexAttrBitmap(entry->localrel,
289 
290  /*
291  * If no replica identity index and no PK, the published table must
292  * have replica identity FULL.
293  */
294  if (idkey == NULL && remoterel->replident != REPLICA_IDENTITY_FULL)
295  entry->updatable = false;
296  }
297 
298  i = -1;
299  while ((i = bms_next_member(idkey, i)) >= 0)
300  {
302 
304  ereport(ERROR,
305  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
306  errmsg("logical replication target relation \"%s.%s\" uses "
307  "system columns in REPLICA IDENTITY index",
308  remoterel->nspname, remoterel->relname)));
309 
311 
312  if (entry->attrmap->attnums[attnum] < 0 ||
313  !bms_is_member(entry->attrmap->attnums[attnum], remoterel->attkeys))
314  {
315  entry->updatable = false;
316  break;
317  }
318  }
319 }
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
Definition: attnum.h:41
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
int errcode(int sqlerrcode)
Definition: elog.c:855
int errmsg(const char *fmt,...)
Definition: elog.c:1068
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
int16 attnum
Definition: pg_attribute.h:74
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition: relcache.c:5244
@ INDEX_ATTR_BITMAP_PRIMARY_KEY
Definition: relcache.h:62
@ INDEX_ATTR_BITMAP_IDENTITY_KEY
Definition: relcache.h:63
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

References LogicalRepRelation::attkeys, attnum, AttrMap::attnums, LogicalRepRelMapEntry::attrmap, AttrNumberGetAttrOffset, AttrNumberIsForUserDefinedAttr, bms_is_member(), bms_next_member(), ereport, errcode(), errmsg(), ERROR, FirstLowInvalidHeapAttributeNumber, i, INDEX_ATTR_BITMAP_IDENTITY_KEY, INDEX_ATTR_BITMAP_PRIMARY_KEY, LogicalRepRelMapEntry::localrel, LogicalRepRelation::nspname, RelationGetIndexAttrBitmap(), LogicalRepRelation::relname, LogicalRepRelMapEntry::remoterel, LogicalRepRelation::replident, and LogicalRepRelMapEntry::updatable.

Referenced by logicalrep_partition_open(), and logicalrep_rel_open().

◆ logicalrep_rel_open()

LogicalRepRelMapEntry* logicalrep_rel_open ( LogicalRepRelId  remoteid,
LOCKMODE  lockmode 
)

Definition at line 327 of file relation.c.

328 {
329  LogicalRepRelMapEntry *entry;
330  bool found;
331  LogicalRepRelation *remoterel;
332 
333  if (LogicalRepRelMap == NULL)
335 
336  /* Search for existing entry. */
337  entry = hash_search(LogicalRepRelMap, &remoteid,
338  HASH_FIND, &found);
339 
340  if (!found)
341  elog(ERROR, "no relation map entry for remote relation ID %u",
342  remoteid);
343 
344  remoterel = &entry->remoterel;
345 
346  /* Ensure we don't leak a relcache refcount. */
347  if (entry->localrel)
348  elog(ERROR, "remote relation ID %u is already open", remoteid);
349 
350  /*
351  * When opening and locking a relation, pending invalidation messages are
352  * processed which can invalidate the relation. Hence, if the entry is
353  * currently considered valid, try to open the local relation by OID and
354  * see if invalidation ensues.
355  */
356  if (entry->localrelvalid)
357  {
358  entry->localrel = try_table_open(entry->localreloid, lockmode);
359  if (!entry->localrel)
360  {
361  /* Table was renamed or dropped. */
362  entry->localrelvalid = false;
363  }
364  else if (!entry->localrelvalid)
365  {
366  /* Note we release the no-longer-useful lock here. */
367  table_close(entry->localrel, lockmode);
368  entry->localrel = NULL;
369  }
370  }
371 
372  /*
373  * If the entry has been marked invalid since we last had lock on it,
374  * re-open the local relation by name and rebuild all derived data.
375  */
376  if (!entry->localrelvalid)
377  {
378  Oid relid;
379  TupleDesc desc;
380  MemoryContext oldctx;
381  int i;
382  Bitmapset *missingatts;
383 
384  /* Release the no-longer-useful attrmap, if any. */
385  if (entry->attrmap)
386  {
387  free_attrmap(entry->attrmap);
388  entry->attrmap = NULL;
389  }
390 
391  /* Try to find and lock the relation by name. */
392  relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
393  remoterel->relname, -1),
394  lockmode, true);
395  if (!OidIsValid(relid))
396  ereport(ERROR,
397  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
398  errmsg("logical replication target relation \"%s.%s\" does not exist",
399  remoterel->nspname, remoterel->relname)));
400  entry->localrel = table_open(relid, NoLock);
401  entry->localreloid = relid;
402 
403  /* Check for supported relkind. */
404  CheckSubscriptionRelkind(entry->localrel->rd_rel->relkind,
405  remoterel->nspname, remoterel->relname);
406 
407  /*
408  * Build the mapping of local attribute numbers to remote attribute
409  * numbers and validate that we don't miss any replicated columns as
410  * that would result in potentially unwanted data loss.
411  */
412  desc = RelationGetDescr(entry->localrel);
414  entry->attrmap = make_attrmap(desc->natts);
415  MemoryContextSwitchTo(oldctx);
416 
417  /* check and report missing attrs, if any */
418  missingatts = bms_add_range(NULL, 0, remoterel->natts - 1);
419  for (i = 0; i < desc->natts; i++)
420  {
421  int attnum;
422  Form_pg_attribute attr = TupleDescAttr(desc, i);
423 
424  if (attr->attisdropped || attr->attgenerated)
425  {
426  entry->attrmap->attnums[i] = -1;
427  continue;
428  }
429 
431  NameStr(attr->attname));
432 
433  entry->attrmap->attnums[i] = attnum;
434  if (attnum >= 0)
435  missingatts = bms_del_member(missingatts, attnum);
436  }
437 
438  logicalrep_report_missing_attrs(remoterel, missingatts);
439 
440  /* be tidy */
441  bms_free(missingatts);
442 
443  /*
444  * Set if the table's replica identity is enough to apply
445  * update/delete.
446  */
448 
449  /*
450  * Finding a usable index is an infrequent task. It occurs when an
451  * operation is first performed on the relation, or after invalidation
452  * of the relation cache entry (such as ANALYZE or CREATE/DROP index
453  * on the relation).
454  */
455  entry->localindexoid = FindLogicalRepLocalIndex(entry->localrel, remoterel,
456  entry->attrmap);
457 
458  entry->localrelvalid = true;
459  }
460 
461  if (entry->state != SUBREL_STATE_READY)
463  entry->localreloid,
464  &entry->statelsn);
465 
466  return entry;
467 }
Subscription * MySubscription
Definition: worker.c:298
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition: bitmapset.c:1019
#define NameStr(name)
Definition: c.h:746
#define elog(elevel,...)
Definition: elog.h:224
void CheckSubscriptionRelkind(char relkind, 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:424
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:80
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
#define RelationGetDescr(relation)
Definition: rel.h:531
static void logicalrep_report_missing_attrs(LogicalRepRelation *remoterel, Bitmapset *missingatts)
Definition: relation.c:226
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:209
Relation try_table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:60
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References attnum, AttrMap::attnums, LogicalRepRelMapEntry::attrmap, 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_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::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_free_entry()

static void logicalrep_relmap_free_entry ( LogicalRepRelMapEntry entry)
static

Definition at line 132 of file relation.c.

133 {
134  LogicalRepRelation *remoterel;
135 
136  remoterel = &entry->remoterel;
137 
138  pfree(remoterel->nspname);
139  pfree(remoterel->relname);
140 
141  if (remoterel->natts > 0)
142  {
143  int i;
144 
145  for (i = 0; i < remoterel->natts; i++)
146  pfree(remoterel->attnames[i]);
147 
148  pfree(remoterel->attnames);
149  pfree(remoterel->atttyps);
150  }
151  bms_free(remoterel->attkeys);
152 
153  if (entry->attrmap)
154  free_attrmap(entry->attrmap);
155 }
void pfree(void *pointer)
Definition: mcxt.c:1521

References LogicalRepRelation::attkeys, LogicalRepRelation::attnames, LogicalRepRelMapEntry::attrmap, LogicalRepRelation::atttyps, bms_free(), free_attrmap(), i, LogicalRepRelation::natts, LogicalRepRelation::nspname, pfree(), LogicalRepRelation::relname, and LogicalRepRelMapEntry::remoterel.

Referenced by logicalrep_partmap_reset_relmap(), and logicalrep_relmap_update().

◆ logicalrep_relmap_init()

static void logicalrep_relmap_init ( void  )
static

Definition at line 105 of file relation.c.

106 {
107  HASHCTL ctl;
108 
112  "LogicalRepRelMapContext",
114 
115  /* Initialize the relation hash table. */
116  ctl.keysize = sizeof(LogicalRepRelId);
117  ctl.entrysize = sizeof(LogicalRepRelMapEntry);
119 
120  LogicalRepRelMap = hash_create("logicalrep relation map cache", 128, &ctl,
122 
123  /* Watch for invalidation events. */
125  (Datum) 0);
126 }
uint32 LogicalRepRelId
Definition: logicalproto.h:101
struct LogicalRepRelMapEntry LogicalRepRelMapEntry
static void logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:64

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CacheMemoryContext, CacheRegisterRelcacheCallback(), ctl, HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, logicalrep_relmap_invalidate_cb(), LogicalRepRelMap, and LogicalRepRelMapContext.

Referenced by logicalrep_rel_open(), and logicalrep_relmap_update().

◆ logicalrep_relmap_invalidate_cb()

static void logicalrep_relmap_invalidate_cb ( Datum  arg,
Oid  reloid 
)
static

Definition at line 64 of file relation.c.

65 {
66  LogicalRepRelMapEntry *entry;
67 
68  /* Just to be sure. */
69  if (LogicalRepRelMap == NULL)
70  return;
71 
72  if (reloid != InvalidOid)
73  {
74  HASH_SEQ_STATUS status;
75 
77 
78  /* TODO, use inverse lookup hashtable? */
79  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
80  {
81  if (entry->localreloid == reloid)
82  {
83  entry->localrelvalid = false;
84  hash_seq_term(&status);
85  break;
86  }
87  }
88  }
89  else
90  {
91  /* invalidate all cache entries */
92  HASH_SEQ_STATUS status;
93 
95 
96  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
97  entry->localrelvalid = false;
98  }
99 }

References hash_seq_init(), hash_seq_search(), hash_seq_term(), InvalidOid, LogicalRepRelMapEntry::localreloid, LogicalRepRelMapEntry::localrelvalid, and LogicalRepRelMap.

Referenced by logicalrep_relmap_init().

◆ logicalrep_relmap_update()

void logicalrep_relmap_update ( LogicalRepRelation remoterel)

Definition at line 164 of file relation.c.

165 {
166  MemoryContext oldctx;
167  LogicalRepRelMapEntry *entry;
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  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
200  MemoryContextSwitchTo(oldctx);
201 }

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::relname, LogicalRepRelation::remoteid, LogicalRepRelMapEntry::remoterel, and LogicalRepRelation::replident.

Referenced by apply_handle_relation(), and copy_table().

◆ logicalrep_report_missing_attrs()

static void logicalrep_report_missing_attrs ( LogicalRepRelation remoterel,
Bitmapset missingatts 
)
static

Definition at line 226 of file relation.c.

228 {
229  if (!bms_is_empty(missingatts))
230  {
231  StringInfoData missingattsbuf;
232  int missingattcnt = 0;
233  int i;
234 
235  initStringInfo(&missingattsbuf);
236 
237  i = -1;
238  while ((i = bms_next_member(missingatts, i)) >= 0)
239  {
240  missingattcnt++;
241  if (missingattcnt == 1)
242  appendStringInfo(&missingattsbuf, _("\"%s\""),
243  remoterel->attnames[i]);
244  else
245  appendStringInfo(&missingattsbuf, _(", \"%s\""),
246  remoterel->attnames[i]);
247  }
248 
249  ereport(ERROR,
250  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
251  errmsg_plural("logical replication target relation \"%s.%s\" is missing replicated column: %s",
252  "logical replication target relation \"%s.%s\" is missing replicated columns: %s",
253  missingattcnt,
254  remoterel->nspname,
255  remoterel->relname,
256  missingattsbuf.data)));
257  }
258 }
#define bms_is_empty(a)
Definition: bitmapset.h:118
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1178
#define _(x)
Definition: elog.c:90
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:97
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

References _, appendStringInfo(), LogicalRepRelation::attnames, bms_is_empty, bms_next_member(), StringInfoData::data, ereport, errcode(), errmsg_plural(), ERROR, i, initStringInfo(), LogicalRepRelation::nspname, and LogicalRepRelation::relname.

Referenced by logicalrep_rel_open().

Variable Documentation

◆ LogicalRepPartMap

HTAB* LogicalRepPartMap = NULL
static

◆ LogicalRepPartMapContext

MemoryContext LogicalRepPartMapContext = NULL
static

Definition at line 49 of file relation.c.

Referenced by logicalrep_partition_open(), and logicalrep_partmap_init().

◆ LogicalRepRelMap

HTAB* LogicalRepRelMap = NULL
static

◆ LogicalRepRelMapContext

MemoryContext LogicalRepRelMapContext = NULL
static