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

Variables

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

Typedef Documentation

◆ LogicalRepPartMapEntry

Function Documentation

◆ logicalrep_partition_open()

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

Definition at line 585 of file relation.c.

587 {
588  LogicalRepRelMapEntry *entry;
589  LogicalRepPartMapEntry *part_entry;
590  LogicalRepRelation *remoterel = &root->remoterel;
591  Oid partOid = RelationGetRelid(partrel);
592  AttrMap *attrmap = root->attrmap;
593  bool found;
594  MemoryContext oldctx;
595 
596  if (LogicalRepPartMap == NULL)
598 
599  /* Search for existing entry. */
601  (void *) &partOid,
602  HASH_ENTER, &found);
603 
604  entry = &part_entry->relmapentry;
605 
606  /*
607  * We must always overwrite entry->localrel with the latest partition
608  * Relation pointer, because the Relation pointed to by the old value may
609  * have been cleared after the caller would have closed the partition
610  * relation after the last use of this entry. Note that localrelvalid is
611  * only updated by the relcache invalidation callback, so it may still be
612  * true irrespective of whether the Relation pointed to by localrel has
613  * been cleared or not.
614  */
615  if (found && entry->localrelvalid)
616  {
617  entry->localrel = partrel;
618  return entry;
619  }
620 
621  /* Switch to longer-lived context. */
623 
624  if (!found)
625  {
626  memset(part_entry, 0, sizeof(LogicalRepPartMapEntry));
627  part_entry->partoid = partOid;
628  }
629 
630  /* Release the no-longer-useful attrmap, if any. */
631  if (entry->attrmap)
632  {
633  free_attrmap(entry->attrmap);
634  entry->attrmap = NULL;
635  }
636 
637  if (!entry->remoterel.remoteid)
638  {
639  int i;
640 
641  /* Remote relation is copied as-is from the root entry. */
642  entry = &part_entry->relmapentry;
643  entry->remoterel.remoteid = remoterel->remoteid;
644  entry->remoterel.nspname = pstrdup(remoterel->nspname);
645  entry->remoterel.relname = pstrdup(remoterel->relname);
646  entry->remoterel.natts = remoterel->natts;
647  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
648  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
649  for (i = 0; i < remoterel->natts; i++)
650  {
651  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
652  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
653  }
654  entry->remoterel.replident = remoterel->replident;
655  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
656  }
657 
658  entry->localrel = partrel;
659  entry->localreloid = partOid;
660 
661  /*
662  * If the partition's attributes don't match the root relation's, we'll
663  * need to make a new attrmap which maps partition attribute numbers to
664  * remoterel's, instead of the original which maps root relation's
665  * attribute numbers to remoterel's.
666  *
667  * Note that 'map' which comes from the tuple routing data structure
668  * contains 1-based attribute numbers (of the parent relation). However,
669  * the map in 'entry', a logical replication data structure, contains
670  * 0-based attribute numbers (of the remote relation).
671  */
672  if (map)
673  {
674  AttrNumber attno;
675 
676  entry->attrmap = make_attrmap(map->maplen);
677  for (attno = 0; attno < entry->attrmap->maplen; attno++)
678  {
679  AttrNumber root_attno = map->attnums[attno];
680 
681  /* 0 means it's a dropped attribute. See comments atop AttrMap. */
682  if (root_attno == 0)
683  entry->attrmap->attnums[attno] = -1;
684  else
685  entry->attrmap->attnums[attno] = attrmap->attnums[root_attno - 1];
686  }
687  }
688  else
689  {
690  /* Lacking copy_attmap, do this the hard way. */
691  entry->attrmap = make_attrmap(attrmap->maplen);
692  memcpy(entry->attrmap->attnums, attrmap->attnums,
693  attrmap->maplen * sizeof(AttrNumber));
694  }
695 
696  /* Set if the table's replica identity is enough to apply update/delete. */
698 
699  entry->localrelvalid = true;
700 
701  /* state and statelsn are left set to 0. */
702  MemoryContextSwitchTo(oldctx);
703 
704  return entry;
705 }
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:1483
void * palloc(Size size)
Definition: mcxt.c:1199
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:135
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetRelid(relation)
Definition: rel.h:501
static MemoryContext LogicalRepPartMapContext
Definition: relation.c:45
static void logicalrep_partmap_init(void)
Definition: relation.c:550
static HTAB * LogicalRepPartMap
Definition: relation.c:46
static void logicalrep_rel_mark_updatable(LogicalRepRelMapEntry *entry)
Definition: relation.c:266
Definition: attmap.h:35
int maplen
Definition: attmap.h:37
AttrNumber * attnums
Definition: attmap.h:36
LogicalRepRelMapEntry relmapentry
Definition: relation.c:50
LogicalRepRelation remoterel
LogicalRepRelId remoteid
Definition: logicalproto.h:102
Bitmapset * attkeys
Definition: logicalproto.h:110

References LogicalRepRelation::attkeys, LogicalRepRelation::attnames, AttrMap::attnums, LogicalRepRelMapEntry::attrmap, LogicalRepRelation::atttyps, bms_copy(), free_attrmap(), HASH_ENTER, hash_search(), i, 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 550 of file relation.c.

551 {
552  HASHCTL ctl;
553 
557  "LogicalRepPartMapContext",
559 
560  /* Initialize the relation hash table. */
561  ctl.keysize = sizeof(Oid); /* partition OID */
562  ctl.entrysize = sizeof(LogicalRepPartMapEntry);
564 
565  LogicalRepPartMap = hash_create("logicalrep partition map cache", 64, &ctl,
567 
568  /* Watch for invalidation events. */
570  (Datum) 0);
571 }
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:133
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
uintptr_t Datum
Definition: postgres.h:412
struct LogicalRepPartMapEntry LogicalRepPartMapEntry
static void logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:475
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 475 of file relation.c.

476 {
477  LogicalRepPartMapEntry *entry;
478 
479  /* Just to be sure. */
480  if (LogicalRepPartMap == NULL)
481  return;
482 
483  if (reloid != InvalidOid)
484  {
486 
488 
489  /* TODO, use inverse lookup hashtable? */
490  while ((entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
491  {
492  if (entry->relmapentry.localreloid == reloid)
493  {
494  entry->relmapentry.localrelvalid = false;
496  break;
497  }
498  }
499  }
500  else
501  {
502  /* invalidate all cache entries */
504 
506 
507  while ((entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
508  entry->relmapentry.localrelvalid = false;
509  }
510 }
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
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define InvalidOid
Definition: postgres_ext.h:36

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

Referenced by logicalrep_partmap_init().

◆ logicalrep_partmap_reset_relmap()

void logicalrep_partmap_reset_relmap ( LogicalRepRelation remoterel)

Definition at line 523 of file relation.c.

524 {
526  LogicalRepPartMapEntry *part_entry;
527  LogicalRepRelMapEntry *entry;
528 
529  if (LogicalRepPartMap == NULL)
530  return;
531 
533  while ((part_entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
534  {
535  entry = &part_entry->relmapentry;
536 
537  if (entry->remoterel.remoteid != remoterel->remoteid)
538  continue;
539 
541 
542  memset(entry, 0, sizeof(LogicalRepRelMapEntry));
543  }
544 }
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:125

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

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

203 {
204  int i;
205 
206  for (i = 0; i < remoterel->natts; i++)
207  {
208  if (strcmp(remoterel->attnames[i], attname) == 0)
209  return i;
210  }
211 
212  return -1;
213 }
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 456 of file relation.c.

457 {
458  table_close(rel->localrel, lockmode);
459  rel->localrel = NULL;
460 }
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 266 of file relation.c.

267 {
268  Bitmapset *idkey;
269  LogicalRepRelation *remoterel = &entry->remoterel;
270  int i;
271 
272  entry->updatable = true;
273 
274  idkey = RelationGetIndexAttrBitmap(entry->localrel,
276  /* fallback to PK if no replica identity */
277  if (idkey == NULL)
278  {
279  idkey = RelationGetIndexAttrBitmap(entry->localrel,
281 
282  /*
283  * If no replica identity index and no PK, the published table must
284  * have replica identity FULL.
285  */
286  if (idkey == NULL && remoterel->replident != REPLICA_IDENTITY_FULL)
287  entry->updatable = false;
288  }
289 
290  i = -1;
291  while ((i = bms_next_member(idkey, i)) >= 0)
292  {
294 
296  ereport(ERROR,
297  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
298  errmsg("logical replication target relation \"%s.%s\" uses "
299  "system columns in REPLICA IDENTITY index",
300  remoterel->nspname, remoterel->relname)));
301 
303 
304  if (entry->attrmap->attnums[attnum] < 0 ||
305  !bms_is_member(entry->attrmap->attnums[attnum], remoterel->attkeys))
306  {
307  entry->updatable = false;
308  break;
309  }
310  }
311 }
#define AttrNumberGetAttrOffset(attNum)
Definition: attnum.h:51
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
Definition: attnum.h:41
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1047
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:428
int errcode(int sqlerrcode)
Definition: elog.c:735
int errmsg(const char *fmt,...)
Definition: elog.c:946
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
int16 attnum
Definition: pg_attribute.h:83
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition: relcache.c:5168
@ INDEX_ATTR_BITMAP_PRIMARY_KEY
Definition: relcache.h:61
@ INDEX_ATTR_BITMAP_IDENTITY_KEY
Definition: relcache.h:62
#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 319 of file relation.c.

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

126 {
127  LogicalRepRelation *remoterel;
128 
129  remoterel = &entry->remoterel;
130 
131  pfree(remoterel->nspname);
132  pfree(remoterel->relname);
133 
134  if (remoterel->natts > 0)
135  {
136  int i;
137 
138  for (i = 0; i < remoterel->natts; i++)
139  pfree(remoterel->attnames[i]);
140 
141  pfree(remoterel->attnames);
142  pfree(remoterel->atttyps);
143  }
144  bms_free(remoterel->attkeys);
145 
146  if (entry->attrmap)
147  free_attrmap(entry->attrmap);
148 }
void pfree(void *pointer)
Definition: mcxt.c:1306

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

99 {
100  HASHCTL ctl;
101 
105  "LogicalRepRelMapContext",
107 
108  /* Initialize the relation hash table. */
109  ctl.keysize = sizeof(LogicalRepRelId);
110  ctl.entrysize = sizeof(LogicalRepRelMapEntry);
112 
113  LogicalRepRelMap = hash_create("logicalrep relation map cache", 128, &ctl,
115 
116  /* Watch for invalidation events. */
118  (Datum) 0);
119 }
uint32 LogicalRepRelId
Definition: logicalproto.h:96
struct LogicalRepRelMapEntry LogicalRepRelMapEntry
static void logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:57

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

58 {
59  LogicalRepRelMapEntry *entry;
60 
61  /* Just to be sure. */
62  if (LogicalRepRelMap == NULL)
63  return;
64 
65  if (reloid != InvalidOid)
66  {
68 
70 
71  /* TODO, use inverse lookup hashtable? */
72  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
73  {
74  if (entry->localreloid == reloid)
75  {
76  entry->localrelvalid = false;
78  break;
79  }
80  }
81  }
82  else
83  {
84  /* invalidate all cache entries */
86 
88 
89  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
90  entry->localrelvalid = false;
91  }
92 }

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

Referenced by logicalrep_relmap_init().

◆ logicalrep_relmap_update()

void logicalrep_relmap_update ( LogicalRepRelation remoterel)

Definition at line 157 of file relation.c.

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

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

221 {
222  if (!bms_is_empty(missingatts))
223  {
224  StringInfoData missingattsbuf;
225  int missingattcnt = 0;
226  int i;
227 
228  initStringInfo(&missingattsbuf);
229 
230  while ((i = bms_first_member(missingatts)) >= 0)
231  {
232  missingattcnt++;
233  if (missingattcnt == 1)
234  appendStringInfo(&missingattsbuf, _("\"%s\""),
235  remoterel->attnames[i]);
236  else
237  appendStringInfo(&missingattsbuf, _(", \"%s\""),
238  remoterel->attnames[i]);
239  }
240 
241  ereport(ERROR,
242  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
243  errmsg_plural("logical replication target relation \"%s.%s\" is missing replicated column: %s",
244  "logical replication target relation \"%s.%s\" is missing replicated columns: %s",
245  missingattcnt,
246  remoterel->nspname,
247  remoterel->relname,
248  missingattsbuf.data)));
249  }
250 }
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:704
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:1000
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1056
#define _(x)
Definition: elog.c:90
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

References _, appendStringInfo(), LogicalRepRelation::attnames, bms_first_member(), bms_is_empty(), 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 45 of file relation.c.

Referenced by logicalrep_partition_open(), and logicalrep_partmap_init().

◆ LogicalRepRelMap

HTAB* LogicalRepRelMap = NULL
static

◆ LogicalRepRelMapContext

MemoryContext LogicalRepRelMapContext = NULL
static