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)
 
Oid GetRelationIdentityOrPK (Relation rel)
 

Typedef Documentation

◆ LogicalRepRelMapEntry

Function Documentation

◆ GetRelationIdentityOrPK()

Oid GetRelationIdentityOrPK ( Relation  rel)

Definition at line 857 of file relation.c.

858 {
859  Oid idxoid;
860 
861  idxoid = RelationGetReplicaIndex(rel);
862 
863  if (!OidIsValid(idxoid))
864  idxoid = RelationGetPrimaryKeyIndex(rel);
865 
866  return idxoid;
867 }
#define OidIsValid(objectId)
Definition: c.h:759
unsigned int Oid
Definition: postgres_ext.h:31
Oid RelationGetPrimaryKeyIndex(Relation relation)
Definition: relcache.c:4927
Oid RelationGetReplicaIndex(Relation relation)
Definition: relcache.c:4948

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

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

◆ IsIndexUsableForReplicaIdentityFull()

bool IsIndexUsableForReplicaIdentityFull ( IndexInfo indexInfo)

Definition at line 842 of file relation.c.

843 {
844  bool is_btree = (indexInfo->ii_Am == BTREE_AM_OID);
845  bool is_partial = (indexInfo->ii_Predicate != NIL);
846  bool is_only_on_expression = IsIndexOnlyOnExpression(indexInfo);
847 
848  return is_btree && !is_partial && !is_only_on_expression;
849 }
#define NIL
Definition: pg_list.h:68
static bool IsIndexOnlyOnExpression(IndexInfo *indexInfo)
Definition: relation.c:742
Oid ii_Am
Definition: execnodes.h:200
List * ii_Predicate
Definition: execnodes.h:182

References IndexInfo::ii_Am, IndexInfo::ii_Predicate, IsIndexOnlyOnExpression(), and NIL.

Referenced by FindUsableIndexForReplicaIdentityFull(), and RelationFindReplTupleByIndex().

◆ logicalrep_partition_open()

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

Definition at line 600 of file relation.c.

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

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

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

472 {
473  table_close(rel->localrel, lockmode);
474  rel->localrel = NULL;
475 }
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 325 of file relation.c.

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

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

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