PostgreSQL Source Code  git master
logicalrelation.h File Reference
Include dependency graph for logicalrelation.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  LogicalRepRelMapEntry
 

Typedefs

typedef struct LogicalRepRelMapEntry LogicalRepRelMapEntry
 

Functions

void logicalrep_relmap_update (LogicalRepRelation *remoterel)
 
LogicalRepRelMapEntrylogicalrep_rel_open (LogicalRepRelId remoteid, LOCKMODE lockmode)
 
LogicalRepRelMapEntrylogicalrep_partition_open (LogicalRepRelMapEntry *root, Relation partrel, AttrMap *map)
 
void logicalrep_rel_close (LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
 

Typedef Documentation

◆ LogicalRepRelMapEntry

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