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

530 {
531  LogicalRepRelMapEntry *entry;
532  LogicalRepPartMapEntry *part_entry;
533  LogicalRepRelation *remoterel = &root->remoterel;
534  Oid partOid = RelationGetRelid(partrel);
535  AttrMap *attrmap = root->attrmap;
536  bool found;
537  int i;
538  MemoryContext oldctx;
539 
540  if (LogicalRepPartMap == NULL)
542 
543  /* Search for existing entry. */
545  (void *) &partOid,
546  HASH_ENTER, &found);
547 
548  if (found)
549  return &part_entry->relmapentry;
550 
551  memset(part_entry, 0, sizeof(LogicalRepPartMapEntry));
552 
553  /* Switch to longer-lived context. */
555 
556  part_entry->partoid = partOid;
557 
558  /* Remote relation is copied as-is from the root entry. */
559  entry = &part_entry->relmapentry;
560  entry->remoterel.remoteid = remoterel->remoteid;
561  entry->remoterel.nspname = pstrdup(remoterel->nspname);
562  entry->remoterel.relname = pstrdup(remoterel->relname);
563  entry->remoterel.natts = remoterel->natts;
564  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
565  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
566  for (i = 0; i < remoterel->natts; i++)
567  {
568  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
569  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
570  }
571  entry->remoterel.replident = remoterel->replident;
572  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
573 
574  entry->localrel = partrel;
575  entry->localreloid = partOid;
576 
577  /*
578  * If the partition's attributes don't match the root relation's, we'll
579  * need to make a new attrmap which maps partition attribute numbers to
580  * remoterel's, instead of the original which maps root relation's
581  * attribute numbers to remoterel's.
582  *
583  * Note that 'map' which comes from the tuple routing data structure
584  * contains 1-based attribute numbers (of the parent relation). However,
585  * the map in 'entry', a logical replication data structure, contains
586  * 0-based attribute numbers (of the remote relation).
587  */
588  if (map)
589  {
590  AttrNumber attno;
591 
592  entry->attrmap = make_attrmap(map->maplen);
593  for (attno = 0; attno < entry->attrmap->maplen; attno++)
594  {
595  AttrNumber root_attno = map->attnums[attno];
596 
597  entry->attrmap->attnums[attno] = attrmap->attnums[root_attno - 1];
598  }
599  }
600  else
601  {
602  /* Lacking copy_attmap, do this the hard way. */
603  entry->attrmap = make_attrmap(attrmap->maplen);
604  memcpy(entry->attrmap->attnums, attrmap->attnums,
605  attrmap->maplen * sizeof(AttrNumber));
606  }
607 
608  entry->updatable = root->updatable;
609 
610  entry->localrelvalid = true;
611 
612  /* state and statelsn are left set to 0. */
613  MemoryContextSwitchTo(oldctx);
614 
615  return entry;
616 }
static void logicalrep_partmap_init(void)
Definition: relation.c:493
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
char * pstrdup(const char *in)
Definition: mcxt.c:1299
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:109
Definition: attmap.h:34
LogicalRepRelId remoteid
Definition: logicalproto.h:101
LogicalRepRelation remoterel
static HTAB * LogicalRepPartMap
Definition: relation.c:46
LogicalRepRelMapEntry relmapentry
Definition: relation.c:50
AttrNumber * attnums
Definition: attmap.h:36
AttrMap * make_attrmap(int maplen)
Definition: attmap.c:41
static MemoryContext LogicalRepPartMapContext
Definition: relation.c:45
void * palloc(Size size)
Definition: mcxt.c:1062
int i
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:477

◆ logicalrep_partmap_init()

static void logicalrep_partmap_init ( void  )
static

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

494 {
495  HASHCTL ctl;
496 
500  "LogicalRepPartMapContext",
502 
503  /* Initialize the relation hash table. */
504  ctl.keysize = sizeof(Oid); /* partition OID */
505  ctl.entrysize = sizeof(LogicalRepPartMapEntry);
507 
508  LogicalRepPartMap = hash_create("logicalrep partition map cache", 64, &ctl,
510 
511  /* Watch for invalidation events. */
513  (Datum) 0);
514 }
#define AllocSetContextCreate
Definition: memutils.h:173
#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:1540
static HTAB * LogicalRepPartMap
Definition: relation.c:46
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
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:411
Size keysize
Definition: hsearch.h:75
static void logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:452
static MemoryContext LogicalRepPartMapContext
Definition: relation.c:45
MemoryContext CacheMemoryContext
Definition: mcxt.c:51

◆ logicalrep_partmap_invalidate_cb()

static void logicalrep_partmap_invalidate_cb ( Datum  arg,
Oid  reloid 
)
static

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

453 {
454  LogicalRepRelMapEntry *entry;
455 
456  /* Just to be sure. */
457  if (LogicalRepPartMap == NULL)
458  return;
459 
460  if (reloid != InvalidOid)
461  {
463 
465 
466  /* TODO, use inverse lookup hashtable? */
467  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
468  {
469  if (entry->localreloid == reloid)
470  {
471  entry->localrelvalid = false;
472  hash_seq_term(&status);
473  break;
474  }
475  }
476  }
477  else
478  {
479  /* invalidate all cache entries */
481 
483 
484  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
485  entry->localrelvalid = false;
486  }
487 }
static HTAB * LogicalRepPartMap
Definition: relation.c:46
#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 202 of file relation.c.

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

Referenced by logicalrep_rel_open().

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
int i

◆ logicalrep_rel_close()

void logicalrep_rel_close ( LogicalRepRelMapEntry rel,
LOCKMODE  lockmode 
)

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

434 {
435  table_close(rel->localrel, lockmode);
436  rel->localrel = NULL;
437 }
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 258 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().

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

◆ logicalrep_relmap_free_entry()

static void logicalrep_relmap_free_entry ( LogicalRepRelMapEntry entry)
static

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

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  pfree(entry->attrmap);
148 }
Bitmapset * attkeys
Definition: logicalproto.h:109
void pfree(void *pointer)
Definition: mcxt.c:1169
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 98 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(), and logicalrep_relmap_update().

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 }
#define AllocSetContextCreate
Definition: memutils.h:173
#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
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1540
static void logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:57
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:30
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
#define HASH_BLOBS
Definition: hsearch.h:97
struct LogicalRepRelMapEntry LogicalRepRelMapEntry
uintptr_t Datum
Definition: postgres.h:411
Size keysize
Definition: hsearch.h:75
static HTAB * LogicalRepRelMap
Definition: relation.c:32
uint32 LogicalRepRelId
Definition: logicalproto.h:95
MemoryContext CacheMemoryContext
Definition: mcxt.c:51

◆ logicalrep_relmap_invalidate_cb()

static void logicalrep_relmap_invalidate_cb ( Datum  arg,
Oid  reloid 
)
static

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

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;
77  hash_seq_term(&status);
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 }
#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:32

◆ logicalrep_relmap_update()

void logicalrep_relmap_update ( LogicalRepRelation remoterel)

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

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 }
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:125
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
char * pstrdup(const char *in)
Definition: mcxt.c:1299
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:109
LogicalRepRelId remoteid
Definition: logicalproto.h:101
LogicalRepRelation remoterel
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:30
void * palloc(Size size)
Definition: mcxt.c:1062
int i
static void logicalrep_relmap_init(void)
Definition: relation.c:98
static HTAB * LogicalRepRelMap
Definition: relation.c:32

◆ logicalrep_report_missing_attrs()

static void logicalrep_report_missing_attrs ( LogicalRepRelation remoterel,
Bitmapset missingatts 
)
static

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

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 }
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:1019
int errcode(int sqlerrcode)
Definition: elog.c:698
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ERROR
Definition: elog.h:46
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:157
int i
#define _(x)
Definition: elog.c:89

Variable Documentation

◆ LogicalRepPartMap

HTAB* LogicalRepPartMap = NULL
static

Definition at line 46 of file relation.c.

◆ LogicalRepPartMapContext

MemoryContext LogicalRepPartMapContext = NULL
static

Definition at line 45 of file relation.c.

Referenced by logicalrep_partmap_init().

◆ LogicalRepRelMap

HTAB* LogicalRepRelMap = NULL
static

Definition at line 32 of file relation.c.

◆ LogicalRepRelMapContext

MemoryContext LogicalRepRelMapContext = NULL
static

Definition at line 30 of file relation.c.

Referenced by logicalrep_relmap_init().