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

634 {
635  LogicalRepRelMapEntry *entry;
636  LogicalRepPartMapEntry *part_entry;
637  LogicalRepRelation *remoterel = &root->remoterel;
638  Oid partOid = RelationGetRelid(partrel);
639  AttrMap *attrmap = root->attrmap;
640  bool found;
641  int i;
642  MemoryContext oldctx;
643 
644  if (LogicalRepPartMap == NULL)
646 
647  /* Search for existing entry. */
649  (void *) &partOid,
650  HASH_ENTER, &found);
651 
652  if (found)
653  return &part_entry->relmapentry;
654 
655  memset(part_entry, 0, sizeof(LogicalRepPartMapEntry));
656 
657  /* Switch to longer-lived context. */
659 
660  part_entry->partoid = partOid;
661 
662  /* Remote relation is used as-is from the root entry. */
663  entry = &part_entry->relmapentry;
664  entry->remoterel.remoteid = remoterel->remoteid;
665  entry->remoterel.nspname = pstrdup(remoterel->nspname);
666  entry->remoterel.relname = pstrdup(remoterel->relname);
667  entry->remoterel.natts = remoterel->natts;
668  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
669  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
670  for (i = 0; i < remoterel->natts; i++)
671  {
672  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
673  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
674  }
675  entry->remoterel.replident = remoterel->replident;
676  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
677 
678  entry->localrel = partrel;
679  entry->localreloid = partOid;
680 
681  /*
682  * If the partition's attributes don't match the root relation's, we'll
683  * need to make a new attrmap which maps partition attribute numbers to
684  * remoterel's, instead of the original which maps root relation's
685  * attribute numbers to remoterel's.
686  *
687  * Note that 'map' which comes from the tuple routing data structure
688  * contains 1-based attribute numbers (of the parent relation). However,
689  * the map in 'entry', a logical replication data structure, contains
690  * 0-based attribute numbers (of the remote relation).
691  */
692  if (map)
693  {
694  AttrNumber attno;
695 
696  entry->attrmap = make_attrmap(map->maplen);
697  for (attno = 0; attno < entry->attrmap->maplen; attno++)
698  {
699  AttrNumber root_attno = map->attnums[attno];
700 
701  entry->attrmap->attnums[attno] = attrmap->attnums[root_attno - 1];
702  }
703  }
704  else
705  entry->attrmap = attrmap;
706 
707  entry->updatable = root->updatable;
708 
709  entry->localrelvalid = true;
710 
711  /* state and statelsn are left set to 0. */
712  MemoryContextSwitchTo(oldctx);
713 
714  return entry;
715 }
static void logicalrep_partmap_init(void)
Definition: relation.c:598
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:919
unsigned int Oid
Definition: postgres_ext.h:31
Bitmapset * attkeys
Definition: logicalproto.h:71
Definition: attmap.h:34
LogicalRepRelId remoteid
Definition: logicalproto.h:63
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:456

◆ logicalrep_partmap_init()

static void logicalrep_partmap_init ( void  )
static

Definition at line 598 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(), LogicalRepPartMapContext, and MemSet.

Referenced by logicalrep_partition_open().

599 {
600  HASHCTL ctl;
601 
605  "LogicalRepPartMapContext",
607 
608  /* Initialize the relation hash table. */
609  MemSet(&ctl, 0, sizeof(ctl));
610  ctl.keysize = sizeof(Oid); /* partition OID */
611  ctl.entrysize = sizeof(LogicalRepPartMapEntry);
613 
614  LogicalRepPartMap = hash_create("logicalrep partition map cache", 64, &ctl,
616 
617  /* Watch for invalidation events. */
619  (Datum) 0);
620 }
#define AllocSetContextCreate
Definition: memutils.h:170
#define HASH_CONTEXT
Definition: hsearch.h:91
#define HASH_ELEM
Definition: hsearch.h:85
MemoryContext hcxt
Definition: hsearch.h:77
struct LogicalRepPartMapEntry LogicalRepPartMapEntry
Size entrysize
Definition: hsearch.h:72
#define MemSet(start, val, len)
Definition: c.h:950
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
#define HASH_BLOBS
Definition: hsearch.h:86
uintptr_t Datum
Definition: postgres.h:367
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:326
Size keysize
Definition: hsearch.h:71
static void logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:557
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 557 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().

558 {
559  LogicalRepRelMapEntry *entry;
560 
561  /* Just to be sure. */
562  if (LogicalRepPartMap == NULL)
563  return;
564 
565  if (reloid != InvalidOid)
566  {
568 
570 
571  /* TODO, use inverse lookup hashtable? */
572  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
573  {
574  if (entry->localreloid == reloid)
575  {
576  entry->localrelvalid = false;
577  hash_seq_term(&status);
578  break;
579  }
580  }
581  }
582  else
583  {
584  /* invalidate all cache entries */
586 
588 
589  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
590  entry->localrelvalid = false;
591  }
592 }
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:1401
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1391
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:1477

◆ logicalrep_rel_att_by_name()

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

Definition at line 218 of file relation.c.

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

Referenced by logicalrep_rel_open().

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

◆ logicalrep_rel_close()

void logicalrep_rel_close ( LogicalRepRelMapEntry rel,
LOCKMODE  lockmode 
)

Definition at line 449 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().

450 {
451  table_close(rel->localrel, lockmode);
452  rel->localrel = NULL;
453 }
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 274 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, LogicalRepRelation::natts, TupleDescData::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().

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

142 {
143  LogicalRepRelation *remoterel;
144 
145  remoterel = &entry->remoterel;
146 
147  pfree(remoterel->nspname);
148  pfree(remoterel->relname);
149 
150  if (remoterel->natts > 0)
151  {
152  int i;
153 
154  for (i = 0; i < remoterel->natts; i++)
155  pfree(remoterel->attnames[i]);
156 
157  pfree(remoterel->attnames);
158  pfree(remoterel->atttyps);
159  }
160  bms_free(remoterel->attkeys);
161 
162  if (entry->attrmap)
163  pfree(entry->attrmap);
164 }
Bitmapset * attkeys
Definition: logicalproto.h:71
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(), LogicalRepRelMapContext, and MemSet.

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  MemSet(&ctl, 0, sizeof(ctl));
115  ctl.keysize = sizeof(LogicalRepRelId);
116  ctl.entrysize = sizeof(LogicalRepRelMapEntry);
118 
119  LogicalRepRelMap = hash_create("logicalrep relation map cache", 128, &ctl,
121 
122  /* Initialize the type hash table. */
123  MemSet(&ctl, 0, sizeof(ctl));
124  ctl.keysize = sizeof(Oid);
125  ctl.entrysize = sizeof(LogicalRepTyp);
127 
128  /* This will usually be small. */
129  LogicalRepTypMap = hash_create("logicalrep type map cache", 2, &ctl,
131 
132  /* Watch for invalidation events. */
134  (Datum) 0);
135 }
#define AllocSetContextCreate
Definition: memutils.h:170
#define HASH_CONTEXT
Definition: hsearch.h:91
#define HASH_ELEM
Definition: hsearch.h:85
MemoryContext hcxt
Definition: hsearch.h:77
Size entrysize
Definition: hsearch.h:72
#define MemSet(start, val, len)
Definition: c.h:950
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
struct LogicalRepTyp LogicalRepTyp
#define HASH_BLOBS
Definition: hsearch.h:86
struct LogicalRepRelMapEntry LogicalRepRelMapEntry
uintptr_t Datum
Definition: postgres.h:367
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:326
Size keysize
Definition: hsearch.h:71
static HTAB * LogicalRepRelMap
Definition: relation.c:36
uint32 LogicalRepRelId
Definition: logicalproto.h:57
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:1401
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1391
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:1477
static HTAB * LogicalRepRelMap
Definition: relation.c:36

◆ logicalrep_relmap_update()

void logicalrep_relmap_update ( LogicalRepRelation remoterel)

Definition at line 173 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().

174 {
175  MemoryContext oldctx;
176  LogicalRepRelMapEntry *entry;
177  bool found;
178  int i;
179 
180  if (LogicalRepRelMap == NULL)
182 
183  /*
184  * HASH_ENTER returns the existing entry if present or creates a new one.
185  */
186  entry = hash_search(LogicalRepRelMap, (void *) &remoterel->remoteid,
187  HASH_ENTER, &found);
188 
189  if (found)
191 
192  memset(entry, 0, sizeof(LogicalRepRelMapEntry));
193 
194  /* Make cached copy of the data */
196  entry->remoterel.remoteid = remoterel->remoteid;
197  entry->remoterel.nspname = pstrdup(remoterel->nspname);
198  entry->remoterel.relname = pstrdup(remoterel->relname);
199  entry->remoterel.natts = remoterel->natts;
200  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
201  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
202  for (i = 0; i < remoterel->natts; i++)
203  {
204  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
205  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
206  }
207  entry->remoterel.replident = remoterel->replident;
208  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
209  MemoryContextSwitchTo(oldctx);
210 }
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:141
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:919
unsigned int Oid
Definition: postgres_ext.h:31
Bitmapset * attkeys
Definition: logicalproto.h:71
LogicalRepRelId remoteid
Definition: logicalproto.h:63
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 235 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().

237 {
238  if (!bms_is_empty(missingatts))
239  {
240  StringInfoData missingattsbuf;
241  int missingattcnt = 0;
242  int i;
243 
244  initStringInfo(&missingattsbuf);
245 
246  while ((i = bms_first_member(missingatts)) >= 0)
247  {
248  missingattcnt++;
249  if (missingattcnt == 1)
250  appendStringInfo(&missingattsbuf, _("\"%s\""),
251  remoterel->attnames[i]);
252  else
253  appendStringInfo(&missingattsbuf, _(", \"%s\""),
254  remoterel->attnames[i]);
255  }
256 
257  ereport(ERROR,
258  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
259  errmsg_plural("logical replication target relation \"%s.%s\" is missing replicated column: %s",
260  "logical replication target relation \"%s.%s\" is missing replicated columns: %s",
261  missingattcnt,
262  remoterel->nspname,
263  remoterel->relname,
264  missingattsbuf.data)));
265  }
266 }
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:931
int errcode(int sqlerrcode)
Definition: elog.c:610
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ERROR
Definition: elog.h:43
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:144
int i
#define _(x)
Definition: elog.c:88

◆ logicalrep_typmap_free_entry()

static void logicalrep_typmap_free_entry ( LogicalRepTyp entry)
static

Definition at line 459 of file relation.c.

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

Referenced by logicalrep_typmap_update().

460 {
461  pfree(entry->nspname);
462  pfree(entry->typname);
463 }
char * typname
Definition: logicalproto.h:79
void pfree(void *pointer)
Definition: mcxt.c:1057
char * nspname
Definition: logicalproto.h:78

◆ logicalrep_typmap_gettypname()

char* logicalrep_typmap_gettypname ( Oid  remoteid)

Definition at line 503 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().

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

◆ logicalrep_typmap_update()

void logicalrep_typmap_update ( LogicalRepTyp remotetyp)

Definition at line 469 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().

470 {
471  MemoryContext oldctx;
472  LogicalRepTyp *entry;
473  bool found;
474 
475  if (LogicalRepTypMap == NULL)
477 
478  /*
479  * HASH_ENTER returns the existing entry if present or creates a new one.
480  */
481  entry = hash_search(LogicalRepTypMap, (void *) &remotetyp->remoteid,
482  HASH_ENTER, &found);
483 
484  if (found)
486 
487  /* Make cached copy of the data */
488  entry->remoteid = remotetyp->remoteid;
490  entry->nspname = pstrdup(remotetyp->nspname);
491  entry->typname = pstrdup(remotetyp->typname);
492  MemoryContextSwitchTo(oldctx);
493 }
char * typname
Definition: logicalproto.h:79
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:919
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:459
char * nspname
Definition: logicalproto.h:78

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.