PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

Function Documentation

void logicalrep_rel_close ( LogicalRepRelMapEntry rel,
LOCKMODE  lockmode 
)

Definition at line 367 of file relation.c.

References heap_close, LogicalRepRelMapEntry::localrel, and NULL.

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

368 {
369  heap_close(rel->localrel, lockmode);
370  rel->localrel = NULL;
371 }
#define heap_close(r, l)
Definition: heapam.h:97
#define NULL
Definition: c.h:226
LogicalRepRelMapEntry* logicalrep_rel_open ( LogicalRepRelId  remoteid,
LOCKMODE  lockmode 
)

Definition at line 230 of file relation.c.

References LogicalRepRelation::attkeys, LogicalRepRelMapEntry::attrmap, AttrNumberGetAttrOffset, AttrNumberIsForUserDefinedAttr, tupleDesc::attrs, bms_is_member(), bms_next_member(), elog, ereport, errcode(), errmsg(), ERROR, FirstLowInvalidHeapAttributeNumber, 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(), NameStr, tupleDesc::natts, NoLock, LogicalRepRelation::nspname, NULL, OidIsValid, palloc(), RangeVarGetRelid, RelationData::rd_rel, RelationGetDescr, RelationGetIndexAttrBitmap(), RELKIND_RELATION, LogicalRepRelation::relname, LogicalRepRelMapEntry::remoterel, REPLICA_IDENTITY_FULL, LogicalRepRelation::replident, and LogicalRepRelMapEntry::updatable.

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

231 {
232  LogicalRepRelMapEntry *entry;
233  bool found;
234 
235  if (LogicalRepRelMap == NULL)
237 
238  /* Search for existing entry. */
239  entry = hash_search(LogicalRepRelMap, (void *) &remoteid,
240  HASH_FIND, &found);
241 
242  if (!found)
243  elog(ERROR, "no relation map entry for remote relation ID %u",
244  remoteid);
245 
246  /* Need to update the local cache? */
247  if (!OidIsValid(entry->localreloid))
248  {
249  Oid relid;
250  int i;
251  int found;
252  Bitmapset *idkey;
253  TupleDesc desc;
254  LogicalRepRelation *remoterel;
255  MemoryContext oldctx;
256  remoterel = &entry->remoterel;
257 
258  /* Try to find and lock the relation by name. */
259  relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
260  remoterel->relname, -1),
261  lockmode, true);
262  if (!OidIsValid(relid))
263  ereport(ERROR,
264  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
265  errmsg("logical replication target relation \"%s.%s\" does not exist",
266  remoterel->nspname, remoterel->relname)));
267  entry->localrel = heap_open(relid, NoLock);
268 
269  /*
270  * We currently only support writing to regular and partitioned
271  * tables.
272  */
273  if (entry->localrel->rd_rel->relkind != RELKIND_RELATION)
274  ereport(ERROR,
275  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
276  errmsg("logical replication target relation \"%s.%s\" is not a table",
277  remoterel->nspname, remoterel->relname)));
278 
279  /*
280  * Build the mapping of local attribute numbers to remote attribute
281  * numbers and validate that we don't miss any replicated columns
282  * as that would result in potentially unwanted data loss.
283  */
284  desc = RelationGetDescr(entry->localrel);
286  entry->attrmap = palloc(desc->natts * sizeof(int));
287  MemoryContextSwitchTo(oldctx);
288 
289  found = 0;
290  for (i = 0; i < desc->natts; i++)
291  {
292  int attnum = logicalrep_rel_att_by_name(remoterel,
293  NameStr(desc->attrs[i]->attname));
294  entry->attrmap[i] = attnum;
295  if (attnum >= 0)
296  found++;
297  }
298 
299  /* TODO, detail message with names of missing columns */
300  if (found < remoterel->natts)
301  ereport(ERROR,
302  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
303  errmsg("logical replication target relation \"%s.%s\" is missing "
304  "some replicated columns",
305  remoterel->nspname, remoterel->relname)));
306 
307  /*
308  * Check that replica identity matches. We allow for stricter replica
309  * identity (fewer columns) on subscriber as that will not stop us
310  * from finding unique tuple. IE, if publisher has identity
311  * (id,timestamp) and subscriber just (id) this will not be a problem,
312  * but in the opposite scenario it will.
313  *
314  * Don't throw any error here just mark the relation entry as not
315  * updatable, as replica identity is only for updates and deletes
316  * but inserts can be replicated even without it.
317  */
318  entry->updatable = true;
319  idkey = RelationGetIndexAttrBitmap(entry->localrel,
321  /* fallback to PK if no replica identity */
322  if (idkey == NULL)
323  {
324  idkey = RelationGetIndexAttrBitmap(entry->localrel,
326  /*
327  * If no replica identity index and no PK, the published table
328  * must have replica identity FULL.
329  */
330  if (idkey == NULL && remoterel->replident != REPLICA_IDENTITY_FULL)
331  entry->updatable = false;
332  }
333 
334  i = -1;
335  while ((i = bms_next_member(idkey, i)) >= 0)
336  {
337  int attnum = i + FirstLowInvalidHeapAttributeNumber;
338 
339  if (!AttrNumberIsForUserDefinedAttr(attnum))
340  ereport(ERROR,
341  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
342  errmsg("logical replication target relation \"%s.%s\" uses "
343  "system columns in REPLICA IDENTITY index",
344  remoterel->nspname, remoterel->relname)));
345 
346  attnum = AttrNumberGetAttrOffset(attnum);
347 
348  if (!bms_is_member(entry->attrmap[attnum], remoterel->attkeys))
349  {
350  entry->updatable = false;
351  break;
352  }
353  }
354 
355  entry->localreloid = relid;
356  }
357  else
358  entry->localrel = heap_open(entry->localreloid, lockmode);
359 
360  return entry;
361 }
#define RelationGetDescr(relation)
Definition: rel.h:425
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:53
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:907
Form_pg_attribute * attrs
Definition: tupdesc.h:74
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:885
Form_pg_class rd_rel
Definition: rel.h:113
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
Bitmapset * attkeys
Definition: logicalproto.h:50
int natts
Definition: tupdesc.h:73
#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:210
LogicalRepRelation remoterel
#define AttrNumberGetAttrOffset(attNum)
Definition: attnum.h:51
#define NoLock
Definition: lockdefs.h:34
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:31
#define ereport(elevel, rest)
Definition: elog.h:122
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define NULL
Definition: c.h:226
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition: relcache.c:4759
#define NameStr(name)
Definition: c.h:495
#define elog
Definition: elog.h:219
#define RELKIND_RELATION
Definition: pg_class.h:160
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:419
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:419
static HTAB * LogicalRepRelMap
Definition: relation.c:33
static void logicalrep_relmap_init()
Definition: relation.c:84
void logicalrep_relmap_update ( LogicalRepRelation remoterel)

Definition at line 165 of file relation.c.

References LogicalRepRelation::attkeys, LogicalRepRelation::attnames, LogicalRepRelMapEntry::attrmap, LogicalRepRelation::atttyps, bms_copy(), HASH_ENTER, hash_search(), i, InvalidOid, LogicalRepRelMapEntry::localreloid, logicalrep_relmap_free_entry(), logicalrep_relmap_init(), MemoryContextSwitchTo(), LogicalRepRelation::natts, LogicalRepRelation::nspname, NULL, palloc(), pstrdup(), LogicalRepRelation::relname, LogicalRepRelation::remoteid, LogicalRepRelMapEntry::remoterel, and LogicalRepRelation::replident.

Referenced by apply_handle_relation().

166 {
167  MemoryContext oldctx;
168  LogicalRepRelMapEntry *entry;
169  bool found;
170  int i;
171 
172  if (LogicalRepRelMap == NULL)
174 
175  /*
176  * HASH_ENTER returns the existing entry if present or creates a new one.
177  */
178  entry = hash_search(LogicalRepRelMap, (void *) &remoterel->remoteid,
179  HASH_ENTER, &found);
180 
181  if (found)
183 
184  /* Make cached copy of the data */
186  entry->remoterel.remoteid = remoterel->remoteid;
187  entry->remoterel.nspname = pstrdup(remoterel->nspname);
188  entry->remoterel.relname = pstrdup(remoterel->relname);
189  entry->remoterel.natts = remoterel->natts;
190  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
191  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
192  for (i = 0; i < remoterel->natts; i++)
193  {
194  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
195  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
196  }
197  entry->remoterel.replident = remoterel->replident;
198  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
199  entry->attrmap = NULL;
200  entry->localreloid = InvalidOid;
201  MemoryContextSwitchTo(oldctx);
202 }
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:124
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:110
char * pstrdup(const char *in)
Definition: mcxt.c:1165
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
unsigned int Oid
Definition: postgres_ext.h:31
Bitmapset * attkeys
Definition: logicalproto.h:50
LogicalRepRelId remoteid
Definition: logicalproto.h:43
LogicalRepRelation remoterel
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:31
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:226
void * palloc(Size size)
Definition: mcxt.c:891
int i
static HTAB * LogicalRepRelMap
Definition: relation.c:33
static void logicalrep_relmap_init()
Definition: relation.c:84
Oid logicalrep_typmap_getid ( Oid  remoteid)

Definition at line 441 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, NULL, ObjectIdGetDatum, OidIsValid, PointerGetDatum, TYPENAMENSP, LogicalRepTyp::typname, and LogicalRepTyp::typoid.

Referenced by slot_store_error_callback().

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

Definition at line 410 of file relation.c.

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

Referenced by apply_handle_type().

411 {
412  MemoryContext oldctx;
413  LogicalRepTyp *entry;
414  bool found;
415 
416  if (LogicalRepTypMap == NULL)
418 
419  /*
420  * HASH_ENTER returns the existing entry if present or creates a new one.
421  */
422  entry = hash_search(LogicalRepTypMap, (void *) &remotetyp->remoteid,
423  HASH_ENTER, &found);
424 
425  if (found)
427 
428  /* Make cached copy of the data */
429  entry->remoteid = remotetyp->remoteid;
431  entry->nspname = pstrdup(remotetyp->nspname);
432  entry->typname = pstrdup(remotetyp->typname);
433  MemoryContextSwitchTo(oldctx);
434  entry->typoid = InvalidOid;
435 }
char * typname
Definition: logicalproto.h:58
char * pstrdup(const char *in)
Definition: mcxt.c:1165
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
static HTAB * LogicalRepTypMap
Definition: relation.c:34
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:31
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:226
static void logicalrep_typmap_free_entry(LogicalRepTyp *entry)
Definition: relation.c:398
char * nspname
Definition: logicalproto.h:57
static void logicalrep_relmap_init()
Definition: relation.c:84