PostgreSQL Source Code  git master
relation.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/table.h"
#include "catalog/namespace.h"
#include "catalog/pg_am_d.h"
#include "catalog/pg_subscription_rel.h"
#include "executor/executor.h"
#include "nodes/makefuncs.h"
#include "replication/logicalrelation.h"
#include "replication/worker_internal.h"
#include "utils/inval.h"
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 872 of file relation.c.

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

748 {
749  List *idxlist = RelationGetIndexList(localrel);
750  ListCell *lc;
751 
752  foreach(lc, idxlist)
753  {
754  Oid idxoid = lfirst_oid(lc);
755  bool isUsableIdx;
756  Relation idxRel;
757  IndexInfo *idxInfo;
758 
759  idxRel = index_open(idxoid, AccessShareLock);
760  idxInfo = BuildIndexInfo(idxRel);
761  isUsableIdx = IsIndexUsableForReplicaIdentityFull(idxInfo, attrmap);
762  index_close(idxRel, AccessShareLock);
763 
764  /* Return the first eligible index found */
765  if (isUsableIdx)
766  return idxoid;
767  }
768 
769  return InvalidOid;
770 }
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:2426
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132
#define AccessShareLock
Definition: lockdefs.h:36
#define lfirst_oid(lc)
Definition: pg_list.h:174
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4740
bool IsIndexUsableForReplicaIdentityFull(IndexInfo *indexInfo, AttrMap *attrmap)
Definition: relation.c:808
Definition: pg_list.h:54

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

Referenced by FindLogicalRepLocalIndex().

◆ GetRelationIdentityOrPK()

Oid GetRelationIdentityOrPK ( Relation  rel)

Definition at line 855 of file relation.c.

856 {
857  Oid idxoid;
858 
859  idxoid = RelationGetReplicaIndex(rel);
860 
861  if (!OidIsValid(idxoid))
862  idxoid = RelationGetPrimaryKeyIndex(rel);
863 
864  return idxoid;
865 }
Oid RelationGetPrimaryKeyIndex(Relation relation)
Definition: relcache.c:4950
Oid RelationGetReplicaIndex(Relation relation)
Definition: relcache.c:4971

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

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

◆ IsIndexUsableForReplicaIdentityFull()

bool IsIndexUsableForReplicaIdentityFull ( IndexInfo indexInfo,
AttrMap attrmap 
)

Definition at line 808 of file relation.c.

809 {
810  AttrNumber keycol;
811 
812  /* Ensure that the index access method has a valid equal strategy */
814  return false;
815 
816  /* The index must not be a partial index */
817  if (indexInfo->ii_Predicate != NIL)
818  return false;
819 
820  Assert(indexInfo->ii_NumIndexAttrs >= 1);
821 
822  /* The leftmost index field must not be an expression */
823  keycol = indexInfo->ii_IndexAttrNumbers[0];
824  if (!AttributeNumberIsValid(keycol))
825  return false;
826 
827  /*
828  * And the leftmost index field must reference the remote relation column.
829  * This is because if it doesn't, the sequential scan is favorable over
830  * index scan in most cases.
831  */
832  if (attrmap->maplen <= AttrNumberGetAttrOffset(keycol) ||
833  attrmap->attnums[AttrNumberGetAttrOffset(keycol)] < 0)
834  return false;
835 
836 #ifdef USE_ASSERT_CHECKING
837  {
838  IndexAmRoutine *amroutine;
839 
840  /* The given index access method must implement amgettuple. */
841  amroutine = GetIndexAmRoutineByAmId(indexInfo->ii_Am, false);
842  Assert(amroutine->amgettuple != NULL);
843  }
844 #endif
845 
846  return true;
847 }
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:275
int ii_NumIndexAttrs
Definition: execnodes.h:177
Oid ii_Am
Definition: execnodes.h:200
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 = &part_entry->relmapentry;
661  entry->remoterel.remoteid = remoterel->remoteid;
662  entry->remoterel.nspname = pstrdup(remoterel->nspname);
663  entry->remoterel.relname = pstrdup(remoterel->relname);
664  entry->remoterel.natts = remoterel->natts;
665  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
666  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
667  for (i = 0; i < remoterel->natts; i++)
668  {
669  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
670  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
671  }
672  entry->remoterel.replident = remoterel->replident;
673  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
674  }
675 
676  entry->localrel = partrel;
677  entry->localreloid = partOid;
678 
679  /*
680  * If the partition's attributes don't match the root relation's, we'll
681  * need to make a new attrmap which maps partition attribute numbers to
682  * remoterel's, instead of the original which maps root relation's
683  * attribute numbers to remoterel's.
684  *
685  * Note that 'map' which comes from the tuple routing data structure
686  * contains 1-based attribute numbers (of the parent relation). However,
687  * the map in 'entry', a logical replication data structure, contains
688  * 0-based attribute numbers (of the remote relation).
689  */
690  if (map)
691  {
692  AttrNumber attno;
693 
694  entry->attrmap = make_attrmap(map->maplen);
695  for (attno = 0; attno < entry->attrmap->maplen; attno++)
696  {
697  AttrNumber root_attno = map->attnums[attno];
698 
699  /* 0 means it's a dropped attribute. See comments atop AttrMap. */
700  if (root_attno == 0)
701  entry->attrmap->attnums[attno] = -1;
702  else
703  entry->attrmap->attnums[attno] = attrmap->attnums[root_attno - 1];
704  }
705  }
706  else
707  {
708  /* Lacking copy_attmap, do this the hard way. */
709  entry->attrmap = make_attrmap(attrmap->maplen);
710  memcpy(entry->attrmap->attnums, attrmap->attnums,
711  attrmap->maplen * sizeof(AttrNumber));
712  }
713 
714  /* Set if the table's replica identity is enough to apply update/delete. */
716 
717  /* state and statelsn are left set to 0. */
718  MemoryContextSwitchTo(oldctx);
719 
720  /*
721  * Finding a usable index is an infrequent task. It occurs when an
722  * operation is first performed on the relation, or after invalidation of
723  * the relation cache entry (such as ANALYZE or CREATE/DROP index on the
724  * relation).
725  *
726  * We also prefer to run this code on the oldctx so that we do not leak
727  * anything in the LogicalRepPartMapContext (hence CacheMemoryContext).
728  */
729  entry->localindexoid = FindLogicalRepLocalIndex(partrel, remoterel,
730  entry->attrmap);
731 
732  entry->localrelvalid = true;
733 
734  return entry;
735 }
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:872
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_init()

static void logicalrep_partmap_init ( void  )
static

Definition at line 568 of file relation.c.

569 {
570  HASHCTL ctl;
571 
575  "LogicalRepPartMapContext",
577 
578  /* Initialize the relation hash table. */
579  ctl.keysize = sizeof(Oid); /* partition OID */
580  ctl.entrysize = sizeof(LogicalRepPartMapEntry);
582 
583  LogicalRepPartMap = hash_create("logicalrep partition map cache", 64, &ctl,
585 
586  /* Watch for invalidation events. */
588  (Datum) 0);
589 }
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:350
#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:1561
MemoryContext CacheMemoryContext
Definition: mcxt.c:144
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
uintptr_t Datum
Definition: postgres.h:64
struct LogicalRepPartMapEntry LogicalRepPartMapEntry
static void logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:493
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
MemoryContext hcxt
Definition: hsearch.h:86

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CacheMemoryContext, CacheRegisterRelcacheCallback(), HASHCTL::entrysize, HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, HASHCTL::hcxt, HASHCTL::keysize, 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 493 of file relation.c.

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

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 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 }
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_att_by_name()

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

Definition at line 210 of file relation.c.

211 {
212  int i;
213 
214  for (i = 0; i < remoterel->natts; i++)
215  {
216  if (strcmp(remoterel->attnames[i], attname) == 0)
217  return i;
218  }
219 
220  return -1;
221 }
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 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_mark_updatable()

static void logicalrep_rel_mark_updatable ( LogicalRepRelMapEntry entry)
static

Definition at line 275 of file relation.c.

276 {
277  Bitmapset *idkey;
278  LogicalRepRelation *remoterel = &entry->remoterel;
279  int i;
280 
281  entry->updatable = true;
282 
283  idkey = RelationGetIndexAttrBitmap(entry->localrel,
285  /* fallback to PK if no replica identity */
286  if (idkey == NULL)
287  {
288  idkey = RelationGetIndexAttrBitmap(entry->localrel,
290 
291  /*
292  * If no replica identity index and no PK, the published table must
293  * have replica identity FULL.
294  */
295  if (idkey == NULL && remoterel->replident != REPLICA_IDENTITY_FULL)
296  entry->updatable = false;
297  }
298 
299  i = -1;
300  while ((i = bms_next_member(idkey, i)) >= 0)
301  {
303 
305  ereport(ERROR,
306  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
307  errmsg("logical replication target relation \"%s.%s\" uses "
308  "system columns in REPLICA IDENTITY index",
309  remoterel->nspname, remoterel->relname)));
310 
312 
313  if (entry->attrmap->attnums[attnum] < 0 ||
314  !bms_is_member(entry->attrmap->attnums[attnum], remoterel->attkeys))
315  {
316  entry->updatable = false;
317  break;
318  }
319  }
320 }
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
Definition: attnum.h:41
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1106
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:460
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
int16 attnum
Definition: pg_attribute.h:74
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition: relcache.c:5198
@ INDEX_ATTR_BITMAP_PRIMARY_KEY
Definition: relcache.h:63
@ INDEX_ATTR_BITMAP_IDENTITY_KEY
Definition: relcache.h:64
#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 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
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
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
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 133 of file relation.c.

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

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

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

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CacheMemoryContext, CacheRegisterRelcacheCallback(), HASHCTL::entrysize, HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, HASHCTL::hcxt, HASHCTL::keysize, 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 65 of file relation.c.

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

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

◆ logicalrep_report_missing_attrs()

static void logicalrep_report_missing_attrs ( LogicalRepRelation remoterel,
Bitmapset missingatts 
)
static

Definition at line 227 of file relation.c.

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

Referenced by logicalrep_partition_open(), and logicalrep_partmap_init().

◆ LogicalRepRelMap

HTAB* LogicalRepRelMap = NULL
static

◆ LogicalRepRelMapContext

MemoryContext LogicalRepRelMapContext = NULL
static