PostgreSQL Source Code  git master
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 (IndexInfo *indexInfo, AttrMap *attrmap)
 
Oid GetRelationIdentityOrPK (Relation rel)
 

Typedef Documentation

◆ LogicalRepRelMapEntry

Function Documentation

◆ GetRelationIdentityOrPK()

Oid GetRelationIdentityOrPK ( Relation  rel)

Definition at line 854 of file relation.c.

855 {
856  Oid idxoid;
857 
858  idxoid = RelationGetReplicaIndex(rel);
859 
860  if (!OidIsValid(idxoid))
861  idxoid = RelationGetPrimaryKeyIndex(rel);
862 
863  return idxoid;
864 }
#define OidIsValid(objectId)
Definition: c.h:764
unsigned int Oid
Definition: postgres_ext.h:31
Oid RelationGetPrimaryKeyIndex(Relation relation)
Definition: relcache.c:4983
Oid RelationGetReplicaIndex(Relation relation)
Definition: relcache.c:5004

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

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

◆ IsIndexUsableForReplicaIdentityFull()

bool IsIndexUsableForReplicaIdentityFull ( IndexInfo indexInfo,
AttrMap attrmap 
)

Definition at line 807 of file relation.c.

808 {
809  AttrNumber keycol;
810 
811  /* Ensure that the index access method has a valid equal strategy */
813  return false;
814 
815  /* The index must not be a partial index */
816  if (indexInfo->ii_Predicate != NIL)
817  return false;
818 
819  Assert(indexInfo->ii_NumIndexAttrs >= 1);
820 
821  /* The leftmost index field must not be an expression */
822  keycol = indexInfo->ii_IndexAttrNumbers[0];
823  if (!AttributeNumberIsValid(keycol))
824  return false;
825 
826  /*
827  * And the leftmost index field must reference the remote relation column.
828  * This is because if it doesn't, the sequential scan is favorable over
829  * index scan in most cases.
830  */
831  if (attrmap->maplen <= AttrNumberGetAttrOffset(keycol) ||
832  attrmap->attnums[AttrNumberGetAttrOffset(keycol)] < 0)
833  return false;
834 
835 #ifdef USE_ASSERT_CHECKING
836  {
837  IndexAmRoutine *amroutine;
838 
839  /* The given index access method must implement amgettuple. */
840  amroutine = GetIndexAmRoutineByAmId(indexInfo->ii_Am, false);
841  Assert(amroutine->amgettuple != NULL);
842  }
843 #endif
844 
845  return true;
846 }
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
StrategyNumber get_equal_strategy_number_for_am(Oid am)
Assert(fmt[strlen(fmt) - 1] !='\n')
#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:279
int ii_NumIndexAttrs
Definition: execnodes.h:177
Oid ii_Am
Definition: execnodes.h:199
AttrNumber ii_IndexAttrNumbers[INDEX_MAX_KEYS]
Definition: execnodes.h:179
List * ii_Predicate
Definition: execnodes.h:182

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 603 of file relation.c.

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

Referenced by apply_handle_tuple_routing().

◆ logicalrep_partmap_reset_relmap()

void logicalrep_partmap_reset_relmap ( LogicalRepRelation remoterel)

Definition at line 541 of file relation.c.

542 {
543  HASH_SEQ_STATUS status;
544  LogicalRepPartMapEntry *part_entry;
545  LogicalRepRelMapEntry *entry;
546 
547  if (LogicalRepPartMap == NULL)
548  return;
549 
551  while ((part_entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
552  {
553  entry = &part_entry->relmapentry;
554 
555  if (entry->remoterel.remoteid != remoterel->remoteid)
556  continue;
557 
559 
560  memset(entry, 0, sizeof(LogicalRepRelMapEntry));
561  }
562 }
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1431
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1421
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:133

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 474 of file relation.c.

475 {
476  table_close(rel->localrel, lockmode);
477  rel->localrel = NULL;
478 }
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 328 of file relation.c.

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

void logicalrep_relmap_update ( LogicalRepRelation remoterel)

Definition at line 165 of file relation.c.

166 {
167  MemoryContext oldctx;
168  LogicalRepRelMapEntry *entry;
169  bool found;
170  int i;
171 
172  if (LogicalRepRelMap == NULL)
174 
175  /*
176  * HASH_ENTER returns the existing entry if present or creates a new one.
177  */
178  entry = hash_search(LogicalRepRelMap, &remoterel->remoteid,
179  HASH_ENTER, &found);
180 
181  if (found)
183 
184  memset(entry, 0, sizeof(LogicalRepRelMapEntry));
185 
186  /* Make cached copy of the data */
188  entry->remoterel.remoteid = remoterel->remoteid;
189  entry->remoterel.nspname = pstrdup(remoterel->nspname);
190  entry->remoterel.relname = pstrdup(remoterel->relname);
191  entry->remoterel.natts = remoterel->natts;
192  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
193  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
194  for (i = 0; i < remoterel->natts; i++)
195  {
196  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
197  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
198  }
199  entry->remoterel.replident = remoterel->replident;
200  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
201  MemoryContextSwitchTo(oldctx);
202 }

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().