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)
 
void logicalrep_typmap_update (LogicalRepTyp *remotetyp)
 
char * logicalrep_typmap_gettypname (Oid remoteid)
 

Typedef Documentation

◆ LogicalRepRelMapEntry

Function Documentation

◆ logicalrep_partition_open()

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

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

584 {
585  LogicalRepRelMapEntry *entry;
586  LogicalRepPartMapEntry *part_entry;
587  LogicalRepRelation *remoterel = &root->remoterel;
588  Oid partOid = RelationGetRelid(partrel);
589  AttrMap *attrmap = root->attrmap;
590  bool found;
591  int i;
592  MemoryContext oldctx;
593 
594  if (LogicalRepPartMap == NULL)
596 
597  /* Search for existing entry. */
599  (void *) &partOid,
600  HASH_ENTER, &found);
601 
602  if (found)
603  return &part_entry->relmapentry;
604 
605  memset(part_entry, 0, sizeof(LogicalRepPartMapEntry));
606 
607  /* Switch to longer-lived context. */
609 
610  part_entry->partoid = partOid;
611 
612  /* Remote relation is used as-is from the root entry. */
613  entry = &part_entry->relmapentry;
614  entry->remoterel.remoteid = remoterel->remoteid;
615  entry->remoterel.nspname = pstrdup(remoterel->nspname);
616  entry->remoterel.relname = pstrdup(remoterel->relname);
617  entry->remoterel.natts = remoterel->natts;
618  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
619  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
620  for (i = 0; i < remoterel->natts; i++)
621  {
622  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
623  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
624  }
625  entry->remoterel.replident = remoterel->replident;
626  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
627 
628  entry->localrel = partrel;
629  entry->localreloid = partOid;
630 
631  /*
632  * If the partition's attributes don't match the root relation's, we'll
633  * need to make a new attrmap which maps partition attribute numbers to
634  * remoterel's, instead the original which maps root relation's attribute
635  * numbers to remoterel's.
636  *
637  * Note that 'map' which comes from the tuple routing data structure
638  * contains 1-based attribute numbers (of the parent relation). However,
639  * the map in 'entry', a logical replication data structure, contains
640  * 0-based attribute numbers (of the remote relation).
641  */
642  if (map)
643  {
644  AttrNumber attno;
645 
646  entry->attrmap = make_attrmap(map->maplen);
647  for (attno = 0; attno < entry->attrmap->maplen; attno++)
648  {
649  AttrNumber root_attno = map->attnums[attno];
650 
651  entry->attrmap->attnums[attno] = attrmap->attnums[root_attno - 1];
652  }
653  }
654  else
655  entry->attrmap = attrmap;
656 
657  entry->updatable = root->updatable;
658 
659  /* state and statelsn are left set to 0. */
660  MemoryContextSwitchTo(oldctx);
661 
662  return entry;
663 }
static void logicalrep_partmap_init(void)
Definition: relation.c:548
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
char * pstrdup(const char *in)
Definition: mcxt.c:1186
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:908
unsigned int Oid
Definition: postgres_ext.h:31
Bitmapset * attkeys
Definition: logicalproto.h:53
Definition: attmap.h:34
LogicalRepRelId remoteid
Definition: logicalproto.h:45
LogicalRepRelation remoterel
static HTAB * LogicalRepPartMap
Definition: relation.c:50
LogicalRepRelMapEntry relmapentry
Definition: relation.c:54
AttrNumber * attnums
Definition: attmap.h:36
AttrMap * make_attrmap(int maplen)
Definition: attmap.c:41
static MemoryContext LogicalRepPartMapContext
Definition: relation.c:49
void * palloc(Size size)
Definition: mcxt.c:949
int i
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:456

◆ logicalrep_rel_close()

void logicalrep_rel_close ( LogicalRepRelMapEntry rel,
LOCKMODE  lockmode 
)

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

400 {
401  table_close(rel->localrel, lockmode);
402  rel->localrel = NULL;
403 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133

◆ logicalrep_rel_open()

LogicalRepRelMapEntry* logicalrep_rel_open ( LogicalRepRelId  remoteid,
LOCKMODE  lockmode 
)

Definition at line 237 of file relation.c.

References LogicalRepRelation::attkeys, attnum, AttrMap::attnums, LogicalRepRelMapEntry::attrmap, AttrNumberGetAttrOffset, AttrNumberIsForUserDefinedAttr, 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, InvalidOid, LogicalRepRelMapEntry::localrel, LogicalRepRelMapEntry::localreloid, logicalrep_rel_att_by_name(), logicalrep_relmap_init(), make_attrmap(), makeRangeVar(), MemoryContextSwitchTo(), MySubscription, NameStr, 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_open(), TupleDescAttr, and LogicalRepRelMapEntry::updatable.

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

238 {
239  LogicalRepRelMapEntry *entry;
240  bool found;
241  Oid relid = InvalidOid;
242  LogicalRepRelation *remoterel;
243 
244  if (LogicalRepRelMap == NULL)
246 
247  /* Search for existing entry. */
248  entry = hash_search(LogicalRepRelMap, (void *) &remoteid,
249  HASH_FIND, &found);
250 
251  if (!found)
252  elog(ERROR, "no relation map entry for remote relation ID %u",
253  remoteid);
254 
255  remoterel = &entry->remoterel;
256 
257  /*
258  * When opening and locking a relation, pending invalidation messages are
259  * processed which can invalidate the relation. We need to update the
260  * local cache both when we are first time accessing the relation and when
261  * the relation is invalidated (aka entry->localreloid is set InvalidOid).
262  */
263  if (!OidIsValid(entry->localreloid))
264  {
265  /* Try to find and lock the relation by name. */
266  relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
267  remoterel->relname, -1),
268  lockmode, true);
269  if (!OidIsValid(relid))
270  ereport(ERROR,
271  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
272  errmsg("logical replication target relation \"%s.%s\" does not exist",
273  remoterel->nspname, remoterel->relname)));
274  entry->localrel = table_open(relid, NoLock);
275 
276  }
277  else
278  {
279  relid = entry->localreloid;
280  entry->localrel = table_open(entry->localreloid, lockmode);
281  }
282 
283  if (!OidIsValid(entry->localreloid))
284  {
285  int found;
286  Bitmapset *idkey;
287  TupleDesc desc;
288  MemoryContext oldctx;
289  int i;
290 
291  /* Check for supported relkind. */
292  CheckSubscriptionRelkind(entry->localrel->rd_rel->relkind,
293  remoterel->nspname, remoterel->relname);
294 
295  /*
296  * Build the mapping of local attribute numbers to remote attribute
297  * numbers and validate that we don't miss any replicated columns as
298  * that would result in potentially unwanted data loss.
299  */
300  desc = RelationGetDescr(entry->localrel);
302  entry->attrmap = make_attrmap(desc->natts);
303  MemoryContextSwitchTo(oldctx);
304 
305  found = 0;
306  for (i = 0; i < desc->natts; i++)
307  {
308  int attnum;
309  Form_pg_attribute attr = TupleDescAttr(desc, i);
310 
311  if (attr->attisdropped || attr->attgenerated)
312  {
313  entry->attrmap->attnums[i] = -1;
314  continue;
315  }
316 
317  attnum = logicalrep_rel_att_by_name(remoterel,
318  NameStr(attr->attname));
319 
320  entry->attrmap->attnums[i] = attnum;
321  if (attnum >= 0)
322  found++;
323  }
324 
325  /* TODO, detail message with names of missing columns */
326  if (found < remoterel->natts)
327  ereport(ERROR,
328  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
329  errmsg("logical replication target relation \"%s.%s\" is missing "
330  "some replicated columns",
331  remoterel->nspname, remoterel->relname)));
332 
333  /*
334  * Check that replica identity matches. We allow for stricter replica
335  * identity (fewer columns) on subscriber as that will not stop us
336  * from finding unique tuple. IE, if publisher has identity
337  * (id,timestamp) and subscriber just (id) this will not be a problem,
338  * but in the opposite scenario it will.
339  *
340  * Don't throw any error here just mark the relation entry as not
341  * updatable, as replica identity is only for updates and deletes but
342  * inserts can be replicated even without it.
343  */
344  entry->updatable = true;
345  idkey = RelationGetIndexAttrBitmap(entry->localrel,
347  /* fallback to PK if no replica identity */
348  if (idkey == NULL)
349  {
350  idkey = RelationGetIndexAttrBitmap(entry->localrel,
352 
353  /*
354  * If no replica identity index and no PK, the published table
355  * must have replica identity FULL.
356  */
357  if (idkey == NULL && remoterel->replident != REPLICA_IDENTITY_FULL)
358  entry->updatable = false;
359  }
360 
361  i = -1;
362  while ((i = bms_next_member(idkey, i)) >= 0)
363  {
364  int attnum = i + FirstLowInvalidHeapAttributeNumber;
365 
366  if (!AttrNumberIsForUserDefinedAttr(attnum))
367  ereport(ERROR,
368  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
369  errmsg("logical replication target relation \"%s.%s\" uses "
370  "system columns in REPLICA IDENTITY index",
371  remoterel->nspname, remoterel->relname)));
372 
373  attnum = AttrNumberGetAttrOffset(attnum);
374 
375  if (entry->attrmap->attnums[attnum] < 0 ||
376  !bms_is_member(entry->attrmap->attnums[attnum], remoterel->attkeys))
377  {
378  entry->updatable = false;
379  break;
380  }
381  }
382 
383  entry->localreloid = relid;
384  }
385 
386  if (entry->state != SUBREL_STATE_READY)
388  entry->localreloid,
389  &entry->statelsn,
390  true);
391 
392  return entry;
393 }
Subscription * MySubscription
Definition: worker.c:107
#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:908
Form_pg_class rd_rel
Definition: rel.h:109
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:644
Bitmapset * attkeys
Definition: logicalproto.h:53
#define ERROR
Definition: elog.h:43
static int logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
Definition: relation.c:217
LogicalRepRelation remoterel
#define AttrNumberGetAttrOffset(attNum)
Definition: attnum.h:51
#define NoLock
Definition: lockdefs.h:34
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:33
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
void CheckSubscriptionRelkind(char relkind, const char *nspname, const char *relname)
#define InvalidOid
Definition: postgres_ext.h:36
int16 attnum
Definition: pg_attribute.h:79
#define ereport(elevel,...)
Definition: elog.h:144
AttrNumber * attnums
Definition: attmap.h:36
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn, bool missing_ok)
AttrMap * make_attrmap(int maplen)
Definition: attmap.c:41
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
int i
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition: relcache.c:4936
#define NameStr(name)
Definition: c.h:615
static void logicalrep_relmap_init(void)
Definition: relation.c:102
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:420
static HTAB * LogicalRepRelMap
Definition: relation.c:35

◆ logicalrep_relmap_update()

void logicalrep_relmap_update ( LogicalRepRelation remoterel)

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

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

◆ logicalrep_typmap_gettypname()

char* logicalrep_typmap_gettypname ( Oid  remoteid)

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

454 {
455  LogicalRepTyp *entry;
456  bool found;
457 
458  /* Internal types are mapped directly. */
459  if (remoteid < FirstGenbkiObjectId)
460  {
461  if (!get_typisdefined(remoteid))
462  {
463  /*
464  * This can be caused by having a publisher with a higher
465  * PostgreSQL major version than the subscriber.
466  */
467  return psprintf("unrecognized %u", remoteid);
468  }
469 
470  return format_type_be(remoteid);
471  }
472 
473  if (LogicalRepTypMap == NULL)
474  {
475  /*
476  * If the typemap is not initialized yet, we cannot possibly attempt
477  * to search the hash table; but there's no way we know the type
478  * locally yet, since we haven't received a message about this type,
479  * so this is the best we can do.
480  */
481  return psprintf("unrecognized %u", remoteid);
482  }
483 
484  /* search the mapping */
485  entry = hash_search(LogicalRepTypMap, (void *) &remoteid,
486  HASH_FIND, &found);
487  if (!found)
488  return psprintf("unrecognized %u", remoteid);
489 
490  Assert(OidIsValid(entry->remoteid));
491  return psprintf("%s.%s", entry->nspname, entry->typname);
492 }
char * typname
Definition: logicalproto.h:61
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:327
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:908
static HTAB * LogicalRepTypMap
Definition: relation.c:36
#define OidIsValid(objectId)
Definition: c.h:644
#define Assert(condition)
Definition: c.h:738
#define FirstGenbkiObjectId
Definition: transam.h:153
char * nspname
Definition: logicalproto.h:60

◆ logicalrep_typmap_update()

void logicalrep_typmap_update ( LogicalRepTyp remotetyp)

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

420 {
421  MemoryContext oldctx;
422  LogicalRepTyp *entry;
423  bool found;
424 
425  if (LogicalRepTypMap == NULL)
427 
428  /*
429  * HASH_ENTER returns the existing entry if present or creates a new one.
430  */
431  entry = hash_search(LogicalRepTypMap, (void *) &remotetyp->remoteid,
432  HASH_ENTER, &found);
433 
434  if (found)
436 
437  /* Make cached copy of the data */
438  entry->remoteid = remotetyp->remoteid;
440  entry->nspname = pstrdup(remotetyp->nspname);
441  entry->typname = pstrdup(remotetyp->typname);
442  MemoryContextSwitchTo(oldctx);
443 }
char * typname
Definition: logicalproto.h:61
char * pstrdup(const char *in)
Definition: mcxt.c:1186
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:908
static HTAB * LogicalRepTypMap
Definition: relation.c:36
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:33
static void logicalrep_relmap_init(void)
Definition: relation.c:102
static void logicalrep_typmap_free_entry(LogicalRepTyp *entry)
Definition: relation.c:409
char * nspname
Definition: logicalproto.h:60