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)
 
void logicalrep_rel_close (LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
 
void logicalrep_typmap_update (LogicalRepTyp *remotetyp)
 
Oid logicalrep_typmap_getid (Oid remoteid)
 

Typedef Documentation

◆ LogicalRepRelMapEntry

Function Documentation

◆ logicalrep_rel_close()

void logicalrep_rel_close ( LogicalRepRelMapEntry rel,
LOCKMODE  lockmode 
)

Definition at line 372 of file relation.c.

References heap_close, and LogicalRepRelMapEntry::localrel.

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

373 {
374  heap_close(rel->localrel, lockmode);
375  rel->localrel = NULL;
376 }
#define heap_close(r, l)
Definition: heapam.h:97

◆ logicalrep_rel_open()

LogicalRepRelMapEntry* logicalrep_rel_open ( LogicalRepRelId  remoteid,
LOCKMODE  lockmode 
)

Definition at line 223 of file relation.c.

References LogicalRepRelation::attkeys, LogicalRepRelMapEntry::attrmap, AttrNumberGetAttrOffset, AttrNumberIsForUserDefinedAttr, bms_is_member(), bms_next_member(), CheckSubscriptionRelkind(), elog, ereport, errcode(), errmsg(), ERROR, FirstLowInvalidHeapAttributeNumber, GetSubscriptionRelState(), HASH_FIND, hash_search(), heap_open(), i, INDEX_ATTR_BITMAP_IDENTITY_KEY, INDEX_ATTR_BITMAP_PRIMARY_KEY, LogicalRepRelMapEntry::localrel, LogicalRepRelMapEntry::localreloid, logicalrep_rel_att_by_name(), logicalrep_relmap_init(), makeRangeVar(), MemoryContextSwitchTo(), MySubscription, NameStr, tupleDesc::natts, NoLock, LogicalRepRelation::nspname, Subscription::oid, OidIsValid, palloc(), RangeVarGetRelid, RelationData::rd_rel, RelationGetDescr, RelationGetIndexAttrBitmap(), LogicalRepRelation::relname, LogicalRepRelMapEntry::remoterel, REPLICA_IDENTITY_FULL, LogicalRepRelation::replident, LogicalRepRelMapEntry::state, LogicalRepRelMapEntry::statelsn, SUBREL_STATE_READY, TupleDescAttr, and LogicalRepRelMapEntry::updatable.

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

224 {
225  LogicalRepRelMapEntry *entry;
226  bool found;
227 
228  if (LogicalRepRelMap == NULL)
230 
231  /* Search for existing entry. */
232  entry = hash_search(LogicalRepRelMap, (void *) &remoteid,
233  HASH_FIND, &found);
234 
235  if (!found)
236  elog(ERROR, "no relation map entry for remote relation ID %u",
237  remoteid);
238 
239  /* Need to update the local cache? */
240  if (!OidIsValid(entry->localreloid))
241  {
242  Oid relid;
243  int i;
244  int found;
245  Bitmapset *idkey;
246  TupleDesc desc;
247  LogicalRepRelation *remoterel;
248  MemoryContext oldctx;
249 
250  remoterel = &entry->remoterel;
251 
252  /* Try to find and lock the relation by name. */
253  relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
254  remoterel->relname, -1),
255  lockmode, true);
256  if (!OidIsValid(relid))
257  ereport(ERROR,
258  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
259  errmsg("logical replication target relation \"%s.%s\" does not exist",
260  remoterel->nspname, remoterel->relname)));
261  entry->localrel = heap_open(relid, NoLock);
262 
263  /* Check for supported relkind. */
264  CheckSubscriptionRelkind(entry->localrel->rd_rel->relkind,
265  remoterel->nspname, remoterel->relname);
266 
267  /*
268  * Build the mapping of local attribute numbers to remote attribute
269  * numbers and validate that we don't miss any replicated columns as
270  * that would result in potentially unwanted data loss.
271  */
272  desc = RelationGetDescr(entry->localrel);
274  entry->attrmap = palloc(desc->natts * sizeof(int));
275  MemoryContextSwitchTo(oldctx);
276 
277  found = 0;
278  for (i = 0; i < desc->natts; i++)
279  {
280  int attnum;
281  Form_pg_attribute attr = TupleDescAttr(desc, i);
282 
283  if (attr->attisdropped)
284  {
285  entry->attrmap[i] = -1;
286  continue;
287  }
288 
289  attnum = logicalrep_rel_att_by_name(remoterel,
290  NameStr(attr->attname));
291 
292  entry->attrmap[i] = attnum;
293  if (attnum >= 0)
294  found++;
295  }
296 
297  /* TODO, detail message with names of missing columns */
298  if (found < remoterel->natts)
299  ereport(ERROR,
300  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
301  errmsg("logical replication target relation \"%s.%s\" is missing "
302  "some replicated columns",
303  remoterel->nspname, remoterel->relname)));
304 
305  /*
306  * Check that replica identity matches. We allow for stricter replica
307  * identity (fewer columns) on subscriber as that will not stop us
308  * from finding unique tuple. IE, if publisher has identity
309  * (id,timestamp) and subscriber just (id) this will not be a problem,
310  * but in the opposite scenario it will.
311  *
312  * Don't throw any error here just mark the relation entry as not
313  * updatable, as replica identity is only for updates and deletes but
314  * inserts can be replicated even without it.
315  */
316  entry->updatable = true;
317  idkey = RelationGetIndexAttrBitmap(entry->localrel,
319  /* fallback to PK if no replica identity */
320  if (idkey == NULL)
321  {
322  idkey = RelationGetIndexAttrBitmap(entry->localrel,
324 
325  /*
326  * If no replica identity index and no PK, the published table
327  * must have replica identity FULL.
328  */
329  if (idkey == NULL && remoterel->replident != REPLICA_IDENTITY_FULL)
330  entry->updatable = false;
331  }
332 
333  i = -1;
334  while ((i = bms_next_member(idkey, i)) >= 0)
335  {
336  int attnum = i + FirstLowInvalidHeapAttributeNumber;
337 
338  if (!AttrNumberIsForUserDefinedAttr(attnum))
339  ereport(ERROR,
340  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
341  errmsg("logical replication target relation \"%s.%s\" uses "
342  "system columns in REPLICA IDENTITY index",
343  remoterel->nspname, remoterel->relname)));
344 
345  attnum = AttrNumberGetAttrOffset(attnum);
346 
347  if (!bms_is_member(entry->attrmap[attnum], remoterel->attkeys))
348  {
349  entry->updatable = false;
350  break;
351  }
352  }
353 
354  entry->localreloid = relid;
355  }
356  else
357  entry->localrel = heap_open(entry->localreloid, lockmode);
358 
359  if (entry->state != SUBREL_STATE_READY)
361  entry->localreloid,
362  &entry->statelsn,
363  true);
364 
365  return entry;
366 }
Subscription * MySubscription
Definition: worker.c:112
#define RelationGetDescr(relation)
Definition: rel.h:437
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:53
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:937
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
Definition: attnum.h:41
int errcode(int sqlerrcode)
Definition: elog.c:575
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:576
Bitmapset * attkeys
Definition: logicalproto.h:52
int natts
Definition: tupdesc.h:79
#define REPLICA_IDENTITY_FULL
Definition: pg_class.h:179
#define ERROR
Definition: elog.h:43
static int logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
Definition: relation.c:203
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:187
void CheckSubscriptionRelkind(char relkind, const char *nspname, const char *relname)
#define ereport(elevel, rest)
Definition: elog.h:122
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn, bool missing_ok)
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition: relcache.c:4862
#define NameStr(name)
Definition: c.h:547
#define elog
Definition: elog.h:219
static void logicalrep_relmap_init(void)
Definition: relation.c:86
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
#define SUBREL_STATE_READY
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:421
static HTAB * LogicalRepRelMap
Definition: relation.c:35

◆ logicalrep_relmap_update()

void logicalrep_relmap_update ( LogicalRepRelation remoterel)

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

159 {
160  MemoryContext oldctx;
161  LogicalRepRelMapEntry *entry;
162  bool found;
163  int i;
164 
165  if (LogicalRepRelMap == NULL)
167 
168  /*
169  * HASH_ENTER returns the existing entry if present or creates a new one.
170  */
171  entry = hash_search(LogicalRepRelMap, (void *) &remoterel->remoteid,
172  HASH_ENTER, &found);
173 
174  if (found)
176 
177  memset(entry, 0, sizeof(LogicalRepRelMapEntry));
178 
179  /* Make cached copy of the data */
181  entry->remoterel.remoteid = remoterel->remoteid;
182  entry->remoterel.nspname = pstrdup(remoterel->nspname);
183  entry->remoterel.relname = pstrdup(remoterel->relname);
184  entry->remoterel.natts = remoterel->natts;
185  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
186  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
187  for (i = 0; i < remoterel->natts; i++)
188  {
189  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
190  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
191  }
192  entry->remoterel.replident = remoterel->replident;
193  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
194  MemoryContextSwitchTo(oldctx);
195 }
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:126
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
char * pstrdup(const char *in)
Definition: mcxt.c:1076
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
unsigned int Oid
Definition: postgres_ext.h:31
Bitmapset * attkeys
Definition: logicalproto.h:52
LogicalRepRelId remoteid
Definition: logicalproto.h:45
LogicalRepRelation remoterel
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:33
void * palloc(Size size)
Definition: mcxt.c:848
int i
static void logicalrep_relmap_init(void)
Definition: relation.c:86
static HTAB * LogicalRepRelMap
Definition: relation.c:35

◆ logicalrep_typmap_getid()

Oid logicalrep_typmap_getid ( Oid  remoteid)

Definition at line 446 of file relation.c.

References elog, ereport, errcode(), errhint(), errmsg(), ERROR, FirstNormalObjectId, get_typisdefined(), GetSysCacheOid2, HASH_FIND, hash_search(), InvalidOid, logicalrep_relmap_init(), LookupExplicitNamespace(), LogicalRepTyp::nspname, ObjectIdGetDatum, OidIsValid, PointerGetDatum, TYPENAMENSP, LogicalRepTyp::typname, and LogicalRepTyp::typoid.

Referenced by slot_store_error_callback().

447 {
448  LogicalRepTyp *entry;
449  bool found;
450  Oid nspoid;
451 
452  /* Internal types are mapped directly. */
453  if (remoteid < FirstNormalObjectId)
454  {
455  if (!get_typisdefined(remoteid))
456  ereport(ERROR,
457  (errmsg("built-in type %u not found", remoteid),
458  errhint("This can be caused by having a publisher with a higher PostgreSQL major version than the subscriber.")));
459  return remoteid;
460  }
461 
462  if (LogicalRepTypMap == NULL)
464 
465  /* Try finding the mapping. */
466  entry = hash_search(LogicalRepTypMap, (void *) &remoteid,
467  HASH_FIND, &found);
468 
469  if (!found)
470  elog(ERROR, "no type map entry for remote type %u",
471  remoteid);
472 
473  /* Found and mapped, return the oid. */
474  if (OidIsValid(entry->typoid))
475  return entry->typoid;
476 
477  /* Otherwise, try to map to local type. */
478  nspoid = LookupExplicitNamespace(entry->nspname, true);
479  if (OidIsValid(nspoid))
481  PointerGetDatum(entry->typname),
482  ObjectIdGetDatum(nspoid));
483  else
484  entry->typoid = InvalidOid;
485 
486  if (!OidIsValid(entry->typoid))
487  ereport(ERROR,
488  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
489  errmsg("data type \"%s.%s\" required for logical replication does not exist",
490  entry->nspname, entry->typname)));
491 
492  return entry->typoid;
493 }
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2853
int errhint(const char *fmt,...)
Definition: elog.c:987
char * typname
Definition: logicalproto.h:60
#define PointerGetDatum(X)
Definition: postgres.h:562
bool get_typisdefined(Oid typid)
Definition: lsyscache.c:1923
int errcode(int sqlerrcode)
Definition: elog.c:575
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
unsigned int Oid
Definition: postgres_ext.h:31
static HTAB * LogicalRepTypMap
Definition: relation.c:36
#define OidIsValid(objectId)
Definition: c.h:576
#define FirstNormalObjectId
Definition: transam.h:94
#define GetSysCacheOid2(cacheId, key1, key2)
Definition: syscache.h:193
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvalidOid
Definition: postgres_ext.h:36
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
static void logicalrep_relmap_init(void)
Definition: relation.c:86
char * nspname
Definition: logicalproto.h:59

◆ logicalrep_typmap_update()

void logicalrep_typmap_update ( LogicalRepTyp remotetyp)

Definition at line 415 of file relation.c.

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

Referenced by apply_handle_type().

416 {
417  MemoryContext oldctx;
418  LogicalRepTyp *entry;
419  bool found;
420 
421  if (LogicalRepTypMap == NULL)
423 
424  /*
425  * HASH_ENTER returns the existing entry if present or creates a new one.
426  */
427  entry = hash_search(LogicalRepTypMap, (void *) &remotetyp->remoteid,
428  HASH_ENTER, &found);
429 
430  if (found)
432 
433  /* Make cached copy of the data */
434  entry->remoteid = remotetyp->remoteid;
436  entry->nspname = pstrdup(remotetyp->nspname);
437  entry->typname = pstrdup(remotetyp->typname);
438  MemoryContextSwitchTo(oldctx);
439  entry->typoid = InvalidOid;
440 }
char * typname
Definition: logicalproto.h:60
char * pstrdup(const char *in)
Definition: mcxt.c:1076
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
static HTAB * LogicalRepTypMap
Definition: relation.c:36
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:33
#define InvalidOid
Definition: postgres_ext.h:36
static void logicalrep_relmap_init(void)
Definition: relation.c:86
static void logicalrep_typmap_free_entry(LogicalRepTyp *entry)
Definition: relation.c:403
char * nspname
Definition: logicalproto.h:59