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)
 
LogicalRepRelMapEntrylogicalrep_rel_open (LogicalRepRelId remoteid, LOCKMODE lockmode)
 
void logicalrep_rel_close (LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
 
static void logicalrep_typmap_free_entry (LogicalRepTyp *entry)
 
void logicalrep_typmap_update (LogicalRepTyp *remotetyp)
 
char * logicalrep_typmap_gettypname (Oid remoteid)
 
static void logicalrep_partmap_invalidate_cb (Datum arg, Oid reloid)
 
static void logicalrep_partmap_init (void)
 
LogicalRepRelMapEntrylogicalrep_partition_open (LogicalRepRelMapEntry *root, Relation partrel, AttrMap *map)
 

Variables

static MemoryContext LogicalRepRelMapContext = NULL
 
static HTABLogicalRepRelMap = NULL
 
static HTABLogicalRepTypMap = 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 629 of file relation.c.

References LogicalRepRelation::attkeys, LogicalRepRelation::attnames, AttrMap::attnums, LogicalRepRelMapEntry::attrmap, LogicalRepRelation::atttyps, bms_copy(), HASH_ENTER, hash_search(), i, LogicalRepRelMapEntry::localrel, LogicalRepRelMapEntry::localreloid, LogicalRepRelMapEntry::localrelvalid, logicalrep_partmap_init(), make_attrmap(), AttrMap::maplen, MemoryContextSwitchTo(), LogicalRepRelation::natts, LogicalRepRelation::nspname, palloc(), LogicalRepPartMapEntry::partoid, pstrdup(), RelationGetRelid, LogicalRepPartMapEntry::relmapentry, LogicalRepRelation::relname, LogicalRepRelation::remoteid, LogicalRepRelMapEntry::remoterel, LogicalRepRelation::replident, and LogicalRepRelMapEntry::updatable.

Referenced by apply_handle_tuple_routing().

631 {
632  LogicalRepRelMapEntry *entry;
633  LogicalRepPartMapEntry *part_entry;
634  LogicalRepRelation *remoterel = &root->remoterel;
635  Oid partOid = RelationGetRelid(partrel);
636  AttrMap *attrmap = root->attrmap;
637  bool found;
638  int i;
639  MemoryContext oldctx;
640 
641  if (LogicalRepPartMap == NULL)
643 
644  /* Search for existing entry. */
646  (void *) &partOid,
647  HASH_ENTER, &found);
648 
649  if (found)
650  return &part_entry->relmapentry;
651 
652  memset(part_entry, 0, sizeof(LogicalRepPartMapEntry));
653 
654  /* Switch to longer-lived context. */
656 
657  part_entry->partoid = partOid;
658 
659  /* Remote relation is used 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  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  entry->attrmap->attnums[attno] = attrmap->attnums[root_attno - 1];
699  }
700  }
701  else
702  entry->attrmap = attrmap;
703 
704  entry->updatable = root->updatable;
705 
706  entry->localrelvalid = true;
707 
708  /* state and statelsn are left set to 0. */
709  MemoryContextSwitchTo(oldctx);
710 
711  return entry;
712 }
static void logicalrep_partmap_init(void)
Definition: relation.c:596
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
char * pstrdup(const char *in)
Definition: mcxt.c:1187
int maplen
Definition: attmap.h:37
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
unsigned int Oid
Definition: postgres_ext.h:31
Bitmapset * attkeys
Definition: logicalproto.h:98
Definition: attmap.h:34
LogicalRepRelId remoteid
Definition: logicalproto.h:90
LogicalRepRelation remoterel
static HTAB * LogicalRepPartMap
Definition: relation.c:51
LogicalRepRelMapEntry relmapentry
Definition: relation.c:55
AttrNumber * attnums
Definition: attmap.h:36
AttrMap * make_attrmap(int maplen)
Definition: attmap.c:41
static MemoryContext LogicalRepPartMapContext
Definition: relation.c:50
void * palloc(Size size)
Definition: mcxt.c:950
int i
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:457

◆ logicalrep_partmap_init()

static void logicalrep_partmap_init ( void  )
static

Definition at line 596 of file relation.c.

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(), and LogicalRepPartMapContext.

Referenced by logicalrep_partition_open().

597 {
598  HASHCTL ctl;
599 
603  "LogicalRepPartMapContext",
605 
606  /* Initialize the relation hash table. */
607  ctl.keysize = sizeof(Oid); /* partition OID */
608  ctl.entrysize = sizeof(LogicalRepPartMapEntry);
610 
611  LogicalRepPartMap = hash_create("logicalrep partition map cache", 64, &ctl,
613 
614  /* Watch for invalidation events. */
616  (Datum) 0);
617 }
#define AllocSetContextCreate
Definition: memutils.h:170
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
MemoryContext hcxt
Definition: hsearch.h:86
struct LogicalRepPartMapEntry LogicalRepPartMapEntry
Size entrysize
Definition: hsearch.h:76
unsigned int Oid
Definition: postgres_ext.h:31
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1476
static HTAB * LogicalRepPartMap
Definition: relation.c:51
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
#define HASH_BLOBS
Definition: hsearch.h:97
uintptr_t Datum
Definition: postgres.h:367
Size keysize
Definition: hsearch.h:75
static void logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:555
static MemoryContext LogicalRepPartMapContext
Definition: relation.c:50
MemoryContext CacheMemoryContext
Definition: mcxt.c:47

◆ logicalrep_partmap_invalidate_cb()

static void logicalrep_partmap_invalidate_cb ( Datum  arg,
Oid  reloid 
)
static

Definition at line 555 of file relation.c.

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

Referenced by logicalrep_partmap_init().

556 {
557  LogicalRepRelMapEntry *entry;
558 
559  /* Just to be sure. */
560  if (LogicalRepPartMap == NULL)
561  return;
562 
563  if (reloid != InvalidOid)
564  {
566 
568 
569  /* TODO, use inverse lookup hashtable? */
570  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
571  {
572  if (entry->localreloid == reloid)
573  {
574  entry->localrelvalid = false;
575  hash_seq_term(&status);
576  break;
577  }
578  }
579  }
580  else
581  {
582  /* invalidate all cache entries */
584 
586 
587  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
588  entry->localrelvalid = false;
589  }
590 }
static HTAB * LogicalRepPartMap
Definition: relation.c:51
#define InvalidOid
Definition: postgres_ext.h:36
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1436
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1426
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
void hash_seq_term(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1512

◆ logicalrep_rel_att_by_name()

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

Definition at line 216 of file relation.c.

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

Referenced by logicalrep_rel_open().

217 {
218  int i;
219 
220  for (i = 0; i < remoterel->natts; i++)
221  {
222  if (strcmp(remoterel->attnames[i], attname) == 0)
223  return i;
224  }
225 
226  return -1;
227 }
NameData attname
Definition: pg_attribute.h:41
int i

◆ logicalrep_rel_close()

void logicalrep_rel_close ( LogicalRepRelMapEntry rel,
LOCKMODE  lockmode 
)

Definition at line 447 of file relation.c.

References LogicalRepRelMapEntry::localrel, and table_close().

Referenced by apply_handle_delete(), apply_handle_insert(), apply_handle_truncate(), apply_handle_update(), and copy_table().

448 {
449  table_close(rel->localrel, lockmode);
450  rel->localrel = NULL;
451 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167

◆ logicalrep_rel_open()

LogicalRepRelMapEntry* logicalrep_rel_open ( LogicalRepRelId  remoteid,
LOCKMODE  lockmode 
)

Definition at line 272 of file relation.c.

References LogicalRepRelation::attkeys, attnum, AttrMap::attnums, LogicalRepRelMapEntry::attrmap, AttrNumberGetAttrOffset, AttrNumberIsForUserDefinedAttr, bms_add_range(), bms_del_member(), bms_free(), bms_is_member(), bms_next_member(), CheckSubscriptionRelkind(), elog, ereport, errcode(), errmsg(), ERROR, FirstLowInvalidHeapAttributeNumber, GetSubscriptionRelState(), HASH_FIND, hash_search(), i, INDEX_ATTR_BITMAP_IDENTITY_KEY, INDEX_ATTR_BITMAP_PRIMARY_KEY, LogicalRepRelMapEntry::localrel, LogicalRepRelMapEntry::localreloid, LogicalRepRelMapEntry::localrelvalid, logicalrep_rel_att_by_name(), logicalrep_relmap_init(), logicalrep_report_missing_attrs(), make_attrmap(), makeRangeVar(), MemoryContextSwitchTo(), MySubscription, NameStr, TupleDescData::natts, LogicalRepRelation::natts, NoLock, LogicalRepRelation::nspname, Subscription::oid, OidIsValid, RangeVarGetRelid, RelationData::rd_rel, RelationGetDescr, RelationGetIndexAttrBitmap(), LogicalRepRelation::relname, LogicalRepRelMapEntry::remoterel, LogicalRepRelation::replident, LogicalRepRelMapEntry::state, LogicalRepRelMapEntry::statelsn, table_close(), table_open(), try_table_open(), TupleDescAttr, and LogicalRepRelMapEntry::updatable.

Referenced by apply_handle_delete(), apply_handle_insert(), apply_handle_truncate(), apply_handle_update(), and copy_table().

273 {
274  LogicalRepRelMapEntry *entry;
275  bool found;
276  LogicalRepRelation *remoterel;
277 
278  if (LogicalRepRelMap == NULL)
280 
281  /* Search for existing entry. */
282  entry = hash_search(LogicalRepRelMap, (void *) &remoteid,
283  HASH_FIND, &found);
284 
285  if (!found)
286  elog(ERROR, "no relation map entry for remote relation ID %u",
287  remoteid);
288 
289  remoterel = &entry->remoterel;
290 
291  /* Ensure we don't leak a relcache refcount. */
292  if (entry->localrel)
293  elog(ERROR, "remote relation ID %u is already open", remoteid);
294 
295  /*
296  * When opening and locking a relation, pending invalidation messages are
297  * processed which can invalidate the relation. Hence, if the entry is
298  * currently considered valid, try to open the local relation by OID and
299  * see if invalidation ensues.
300  */
301  if (entry->localrelvalid)
302  {
303  entry->localrel = try_table_open(entry->localreloid, lockmode);
304  if (!entry->localrel)
305  {
306  /* Table was renamed or dropped. */
307  entry->localrelvalid = false;
308  }
309  else if (!entry->localrelvalid)
310  {
311  /* Note we release the no-longer-useful lock here. */
312  table_close(entry->localrel, lockmode);
313  entry->localrel = NULL;
314  }
315  }
316 
317  /*
318  * If the entry has been marked invalid since we last had lock on it,
319  * re-open the local relation by name and rebuild all derived data.
320  */
321  if (!entry->localrelvalid)
322  {
323  Oid relid;
324  Bitmapset *idkey;
325  TupleDesc desc;
326  MemoryContext oldctx;
327  int i;
328  Bitmapset *missingatts;
329 
330  /* Try to find and lock the relation by name. */
331  relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
332  remoterel->relname, -1),
333  lockmode, true);
334  if (!OidIsValid(relid))
335  ereport(ERROR,
336  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
337  errmsg("logical replication target relation \"%s.%s\" does not exist",
338  remoterel->nspname, remoterel->relname)));
339  entry->localrel = table_open(relid, NoLock);
340  entry->localreloid = relid;
341 
342  /* Check for supported relkind. */
343  CheckSubscriptionRelkind(entry->localrel->rd_rel->relkind,
344  remoterel->nspname, remoterel->relname);
345 
346  /*
347  * Build the mapping of local attribute numbers to remote attribute
348  * numbers and validate that we don't miss any replicated columns as
349  * that would result in potentially unwanted data loss.
350  */
351  desc = RelationGetDescr(entry->localrel);
353  entry->attrmap = make_attrmap(desc->natts);
354  MemoryContextSwitchTo(oldctx);
355 
356  /* check and report missing attrs, if any */
357  missingatts = bms_add_range(NULL, 0, remoterel->natts - 1);
358  for (i = 0; i < desc->natts; i++)
359  {
360  int attnum;
361  Form_pg_attribute attr = TupleDescAttr(desc, i);
362 
363  if (attr->attisdropped || attr->attgenerated)
364  {
365  entry->attrmap->attnums[i] = -1;
366  continue;
367  }
368 
369  attnum = logicalrep_rel_att_by_name(remoterel,
370  NameStr(attr->attname));
371 
372  entry->attrmap->attnums[i] = attnum;
373  if (attnum >= 0)
374  missingatts = bms_del_member(missingatts, attnum);
375  }
376 
377  logicalrep_report_missing_attrs(remoterel, missingatts);
378 
379  /* be tidy */
380  bms_free(missingatts);
381 
382  /*
383  * Check that replica identity matches. We allow for stricter replica
384  * identity (fewer columns) on subscriber as that will not stop us
385  * from finding unique tuple. IE, if publisher has identity
386  * (id,timestamp) and subscriber just (id) this will not be a problem,
387  * but in the opposite scenario it will.
388  *
389  * Don't throw any error here just mark the relation entry as not
390  * updatable, as replica identity is only for updates and deletes but
391  * inserts can be replicated even without it.
392  */
393  entry->updatable = true;
394  idkey = RelationGetIndexAttrBitmap(entry->localrel,
396  /* fallback to PK if no replica identity */
397  if (idkey == NULL)
398  {
399  idkey = RelationGetIndexAttrBitmap(entry->localrel,
401 
402  /*
403  * If no replica identity index and no PK, the published table
404  * must have replica identity FULL.
405  */
406  if (idkey == NULL && remoterel->replident != REPLICA_IDENTITY_FULL)
407  entry->updatable = false;
408  }
409 
410  i = -1;
411  while ((i = bms_next_member(idkey, i)) >= 0)
412  {
413  int attnum = i + FirstLowInvalidHeapAttributeNumber;
414 
415  if (!AttrNumberIsForUserDefinedAttr(attnum))
416  ereport(ERROR,
417  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
418  errmsg("logical replication target relation \"%s.%s\" uses "
419  "system columns in REPLICA IDENTITY index",
420  remoterel->nspname, remoterel->relname)));
421 
422  attnum = AttrNumberGetAttrOffset(attnum);
423 
424  if (entry->attrmap->attnums[attnum] < 0 ||
425  !bms_is_member(entry->attrmap->attnums[attnum], remoterel->attkeys))
426  {
427  entry->updatable = false;
428  break;
429  }
430  }
431 
432  entry->localrelvalid = true;
433  }
434 
435  if (entry->state != SUBREL_STATE_READY)
437  entry->localreloid,
438  &entry->statelsn);
439 
440  return entry;
441 }
Subscription * MySubscription
Definition: worker.c:161
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define RelationGetDescr(relation)
Definition: rel.h:483
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:78
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
Definition: attnum.h:41
int errcode(int sqlerrcode)
Definition: elog.c:704
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
Form_pg_class rd_rel
Definition: rel.h:110
unsigned int Oid
Definition: postgres_ext.h:31
Relation try_table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:69
#define OidIsValid(objectId)
Definition: c.h:710
Bitmapset * attkeys
Definition: logicalproto.h:98
static void logicalrep_report_missing_attrs(LogicalRepRelation *remoterel, Bitmapset *missingatts)
Definition: relation.c:233
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition: bitmapset.c:834
#define ERROR
Definition: elog.h:45
static int logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
Definition: relation.c:216
LogicalRepRelation remoterel
#define AttrNumberGetAttrOffset(attNum)
Definition: attnum.h:51
#define NoLock
Definition: lockdefs.h:34
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:34
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
void CheckSubscriptionRelkind(char relkind, const char *nspname, const char *relname)
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
int16 attnum
Definition: pg_attribute.h:83
#define ereport(elevel,...)
Definition: elog.h:155
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
AttrNumber * attnums
Definition: attmap.h:36
AttrMap * make_attrmap(int maplen)
Definition: attmap.c:41
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define elog(elevel,...)
Definition: elog.h:228
int i
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition: relcache.c:4956
#define NameStr(name)
Definition: c.h:681
static void logicalrep_relmap_init(void)
Definition: relation.c:103
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:773
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:422
static HTAB * LogicalRepRelMap
Definition: relation.c:36

◆ logicalrep_relmap_free_entry()

static void logicalrep_relmap_free_entry ( LogicalRepRelMapEntry entry)
static

Definition at line 139 of file relation.c.

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

Referenced by logicalrep_relmap_update().

140 {
141  LogicalRepRelation *remoterel;
142 
143  remoterel = &entry->remoterel;
144 
145  pfree(remoterel->nspname);
146  pfree(remoterel->relname);
147 
148  if (remoterel->natts > 0)
149  {
150  int i;
151 
152  for (i = 0; i < remoterel->natts; i++)
153  pfree(remoterel->attnames[i]);
154 
155  pfree(remoterel->attnames);
156  pfree(remoterel->atttyps);
157  }
158  bms_free(remoterel->attkeys);
159 
160  if (entry->attrmap)
161  pfree(entry->attrmap);
162 }
Bitmapset * attkeys
Definition: logicalproto.h:98
void pfree(void *pointer)
Definition: mcxt.c:1057
LogicalRepRelation remoterel
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
int i

◆ logicalrep_relmap_init()

static void logicalrep_relmap_init ( void  )
static

Definition at line 103 of file relation.c.

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(), and LogicalRepRelMapContext.

Referenced by logicalrep_rel_open(), logicalrep_relmap_update(), and logicalrep_typmap_update().

104 {
105  HASHCTL ctl;
106 
110  "LogicalRepRelMapContext",
112 
113  /* Initialize the relation hash table. */
114  ctl.keysize = sizeof(LogicalRepRelId);
115  ctl.entrysize = sizeof(LogicalRepRelMapEntry);
117 
118  LogicalRepRelMap = hash_create("logicalrep relation map cache", 128, &ctl,
120 
121  /* Initialize the type hash table. */
122  ctl.keysize = sizeof(Oid);
123  ctl.entrysize = sizeof(LogicalRepTyp);
125 
126  /* This will usually be small. */
127  LogicalRepTypMap = hash_create("logicalrep type map cache", 2, &ctl,
129 
130  /* Watch for invalidation events. */
132  (Datum) 0);
133 }
#define AllocSetContextCreate
Definition: memutils.h:170
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
MemoryContext hcxt
Definition: hsearch.h:86
Size entrysize
Definition: hsearch.h:76
unsigned int Oid
Definition: postgres_ext.h:31
static HTAB * LogicalRepTypMap
Definition: relation.c:37
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1476
static void logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:62
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:34
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
struct LogicalRepTyp LogicalRepTyp
#define HASH_BLOBS
Definition: hsearch.h:97
struct LogicalRepRelMapEntry LogicalRepRelMapEntry
uintptr_t Datum
Definition: postgres.h:367
Size keysize
Definition: hsearch.h:75
static HTAB * LogicalRepRelMap
Definition: relation.c:36
uint32 LogicalRepRelId
Definition: logicalproto.h:84
MemoryContext CacheMemoryContext
Definition: mcxt.c:47

◆ logicalrep_relmap_invalidate_cb()

static void logicalrep_relmap_invalidate_cb ( Datum  arg,
Oid  reloid 
)
static

Definition at line 62 of file relation.c.

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

Referenced by logicalrep_relmap_init().

63 {
64  LogicalRepRelMapEntry *entry;
65 
66  /* Just to be sure. */
67  if (LogicalRepRelMap == NULL)
68  return;
69 
70  if (reloid != InvalidOid)
71  {
73 
75 
76  /* TODO, use inverse lookup hashtable? */
77  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
78  {
79  if (entry->localreloid == reloid)
80  {
81  entry->localrelvalid = false;
82  hash_seq_term(&status);
83  break;
84  }
85  }
86  }
87  else
88  {
89  /* invalidate all cache entries */
91 
93 
94  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
95  entry->localrelvalid = false;
96  }
97 }
#define InvalidOid
Definition: postgres_ext.h:36
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1436
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1426
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
void hash_seq_term(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1512
static HTAB * LogicalRepRelMap
Definition: relation.c:36

◆ logicalrep_relmap_update()

void logicalrep_relmap_update ( LogicalRepRelation remoterel)

Definition at line 171 of file relation.c.

References LogicalRepRelation::attkeys, LogicalRepRelation::attnames, LogicalRepRelation::atttyps, bms_copy(), HASH_ENTER, hash_search(), i, logicalrep_relmap_free_entry(), logicalrep_relmap_init(), MemoryContextSwitchTo(), LogicalRepRelation::natts, LogicalRepRelation::nspname, palloc(), pstrdup(), LogicalRepRelation::relname, LogicalRepRelation::remoteid, LogicalRepRelMapEntry::remoterel, and LogicalRepRelation::replident.

Referenced by apply_handle_relation(), and copy_table().

172 {
173  MemoryContext oldctx;
174  LogicalRepRelMapEntry *entry;
175  bool found;
176  int i;
177 
178  if (LogicalRepRelMap == NULL)
180 
181  /*
182  * HASH_ENTER returns the existing entry if present or creates a new one.
183  */
184  entry = hash_search(LogicalRepRelMap, (void *) &remoterel->remoteid,
185  HASH_ENTER, &found);
186 
187  if (found)
189 
190  memset(entry, 0, sizeof(LogicalRepRelMapEntry));
191 
192  /* Make cached copy of the data */
194  entry->remoterel.remoteid = remoterel->remoteid;
195  entry->remoterel.nspname = pstrdup(remoterel->nspname);
196  entry->remoterel.relname = pstrdup(remoterel->relname);
197  entry->remoterel.natts = remoterel->natts;
198  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
199  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
200  for (i = 0; i < remoterel->natts; i++)
201  {
202  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
203  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
204  }
205  entry->remoterel.replident = remoterel->replident;
206  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
207  MemoryContextSwitchTo(oldctx);
208 }
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:139
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
char * pstrdup(const char *in)
Definition: mcxt.c:1187
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
unsigned int Oid
Definition: postgres_ext.h:31
Bitmapset * attkeys
Definition: logicalproto.h:98
LogicalRepRelId remoteid
Definition: logicalproto.h:90
LogicalRepRelation remoterel
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:34
void * palloc(Size size)
Definition: mcxt.c:950
int i
static void logicalrep_relmap_init(void)
Definition: relation.c:103
static HTAB * LogicalRepRelMap
Definition: relation.c:36

◆ logicalrep_report_missing_attrs()

static void logicalrep_report_missing_attrs ( LogicalRepRelation remoterel,
Bitmapset missingatts 
)
static

Definition at line 233 of file relation.c.

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

235 {
236  if (!bms_is_empty(missingatts))
237  {
238  StringInfoData missingattsbuf;
239  int missingattcnt = 0;
240  int i;
241 
242  initStringInfo(&missingattsbuf);
243 
244  while ((i = bms_first_member(missingatts)) >= 0)
245  {
246  missingattcnt++;
247  if (missingattcnt == 1)
248  appendStringInfo(&missingattsbuf, _("\"%s\""),
249  remoterel->attnames[i]);
250  else
251  appendStringInfo(&missingattsbuf, _(", \"%s\""),
252  remoterel->attnames[i]);
253  }
254 
255  ereport(ERROR,
256  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
257  errmsg_plural("logical replication target relation \"%s.%s\" is missing replicated column: %s",
258  "logical replication target relation \"%s.%s\" is missing replicated columns: %s",
259  missingattcnt,
260  remoterel->nspname,
261  remoterel->relname,
262  missingattsbuf.data)));
263  }
264 }
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:996
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1025
int errcode(int sqlerrcode)
Definition: elog.c:704
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ERROR
Definition: elog.h:45
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define ereport(elevel,...)
Definition: elog.h:155
int i
#define _(x)
Definition: elog.c:89

◆ logicalrep_typmap_free_entry()

static void logicalrep_typmap_free_entry ( LogicalRepTyp entry)
static

Definition at line 457 of file relation.c.

References LogicalRepTyp::nspname, pfree(), and LogicalRepTyp::typname.

Referenced by logicalrep_typmap_update().

458 {
459  pfree(entry->nspname);
460  pfree(entry->typname);
461 }
void pfree(void *pointer)
Definition: mcxt.c:1057

◆ logicalrep_typmap_gettypname()

char* logicalrep_typmap_gettypname ( Oid  remoteid)

Definition at line 501 of file relation.c.

References Assert, FirstGenbkiObjectId, format_type_be(), get_typisdefined(), HASH_FIND, hash_search(), LogicalRepTyp::nspname, OidIsValid, psprintf(), LogicalRepTyp::remoteid, and LogicalRepTyp::typname.

Referenced by slot_store_error_callback().

502 {
503  LogicalRepTyp *entry;
504  bool found;
505 
506  /* Internal types are mapped directly. */
507  if (remoteid < FirstGenbkiObjectId)
508  {
509  if (!get_typisdefined(remoteid))
510  {
511  /*
512  * This can be caused by having a publisher with a higher
513  * PostgreSQL major version than the subscriber.
514  */
515  return psprintf("unrecognized %u", remoteid);
516  }
517 
518  return format_type_be(remoteid);
519  }
520 
521  if (LogicalRepTypMap == NULL)
522  {
523  /*
524  * If the typemap is not initialized yet, we cannot possibly attempt
525  * to search the hash table; but there's no way we know the type
526  * locally yet, since we haven't received a message about this type,
527  * so this is the best we can do.
528  */
529  return psprintf("unrecognized %u", remoteid);
530  }
531 
532  /* search the mapping */
533  entry = hash_search(LogicalRepTypMap, (void *) &remoteid,
534  HASH_FIND, &found);
535  if (!found)
536  return psprintf("unrecognized %u", remoteid);
537 
538  Assert(OidIsValid(entry->remoteid));
539  return psprintf("%s.%s", entry->nspname, entry->typname);
540 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
bool get_typisdefined(Oid typid)
Definition: lsyscache.c:2120
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
static HTAB * LogicalRepTypMap
Definition: relation.c:37
#define OidIsValid(objectId)
Definition: c.h:710
#define Assert(condition)
Definition: c.h:804
#define FirstGenbkiObjectId
Definition: transam.h:188

◆ logicalrep_typmap_update()

void logicalrep_typmap_update ( LogicalRepTyp remotetyp)

Definition at line 467 of file relation.c.

References HASH_ENTER, hash_search(), logicalrep_relmap_init(), logicalrep_typmap_free_entry(), MemoryContextSwitchTo(), LogicalRepTyp::nspname, pstrdup(), LogicalRepTyp::remoteid, and LogicalRepTyp::typname.

Referenced by apply_handle_type().

468 {
469  MemoryContext oldctx;
470  LogicalRepTyp *entry;
471  bool found;
472 
473  if (LogicalRepTypMap == NULL)
475 
476  /*
477  * HASH_ENTER returns the existing entry if present or creates a new one.
478  */
479  entry = hash_search(LogicalRepTypMap, (void *) &remotetyp->remoteid,
480  HASH_ENTER, &found);
481 
482  if (found)
484 
485  /* Make cached copy of the data */
486  entry->remoteid = remotetyp->remoteid;
488  entry->nspname = pstrdup(remotetyp->nspname);
489  entry->typname = pstrdup(remotetyp->typname);
490  MemoryContextSwitchTo(oldctx);
491 }
char * pstrdup(const char *in)
Definition: mcxt.c:1187
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
static HTAB * LogicalRepTypMap
Definition: relation.c:37
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:34
static void logicalrep_relmap_init(void)
Definition: relation.c:103
static void logicalrep_typmap_free_entry(LogicalRepTyp *entry)
Definition: relation.c:457

Variable Documentation

◆ LogicalRepPartMap

HTAB* LogicalRepPartMap = NULL
static

Definition at line 51 of file relation.c.

◆ LogicalRepPartMapContext

MemoryContext LogicalRepPartMapContext = NULL
static

Definition at line 50 of file relation.c.

Referenced by logicalrep_partmap_init().

◆ LogicalRepRelMap

HTAB* LogicalRepRelMap = NULL
static

Definition at line 36 of file relation.c.

◆ LogicalRepRelMapContext

MemoryContext LogicalRepRelMapContext = NULL
static

Definition at line 34 of file relation.c.

Referenced by logicalrep_relmap_init().

◆ LogicalRepTypMap

HTAB* LogicalRepTypMap = NULL
static

Definition at line 37 of file relation.c.