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)
 
char * logicalrep_typmap_gettypname (Oid remoteid)
 

Typedef Documentation

◆ LogicalRepRelMapEntry

Function Documentation

◆ logicalrep_rel_close()

void logicalrep_rel_close ( LogicalRepRelMapEntry rel,
LOCKMODE  lockmode 
)

Definition at line 368 of file relation.c.

References heap_close, and LogicalRepRelMapEntry::localrel.

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

369 {
370  heap_close(rel->localrel, lockmode);
371  rel->localrel = NULL;
372 }
#define heap_close(r, l)
Definition: heapam.h:97

◆ logicalrep_rel_open()

LogicalRepRelMapEntry* logicalrep_rel_open ( LogicalRepRelId  remoteid,
LOCKMODE  lockmode 
)

Definition at line 219 of file relation.c.

References LogicalRepRelation::attkeys, attnum, 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, LogicalRepRelation::replident, LogicalRepRelMapEntry::state, LogicalRepRelMapEntry::statelsn, TupleDescAttr, and LogicalRepRelMapEntry::updatable.

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

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

155 {
156  MemoryContext oldctx;
157  LogicalRepRelMapEntry *entry;
158  bool found;
159  int i;
160 
161  if (LogicalRepRelMap == NULL)
163 
164  /*
165  * HASH_ENTER returns the existing entry if present or creates a new one.
166  */
167  entry = hash_search(LogicalRepRelMap, (void *) &remoterel->remoteid,
168  HASH_ENTER, &found);
169 
170  if (found)
172 
173  memset(entry, 0, sizeof(LogicalRepRelMapEntry));
174 
175  /* Make cached copy of the data */
177  entry->remoterel.remoteid = remoterel->remoteid;
178  entry->remoterel.nspname = pstrdup(remoterel->nspname);
179  entry->remoterel.relname = pstrdup(remoterel->relname);
180  entry->remoterel.natts = remoterel->natts;
181  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
182  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
183  for (i = 0; i < remoterel->natts; i++)
184  {
185  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
186  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
187  }
188  entry->remoterel.replident = remoterel->replident;
189  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
190  MemoryContextSwitchTo(oldctx);
191 }
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:122
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:133
char * pstrdup(const char *in)
Definition: mcxt.c:1161
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
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:924
int i
static void logicalrep_relmap_init(void)
Definition: relation.c:84
static HTAB * LogicalRepRelMap
Definition: relation.c:35

◆ logicalrep_typmap_gettypname()

char* logicalrep_typmap_gettypname ( Oid  remoteid)

Definition at line 422 of file relation.c.

References Assert, FirstNormalObjectId, format_type_be(), get_typisdefined(), HASH_FIND, hash_search(), LogicalRepTyp::nspname, OidIsValid, psprintf(), LogicalRepTyp::remoteid, and LogicalRepTyp::typname.

Referenced by slot_store_error_callback().

423 {
424  LogicalRepTyp *entry;
425  bool found;
426 
427  /* Internal types are mapped directly. */
428  if (remoteid < FirstNormalObjectId)
429  {
430  if (!get_typisdefined(remoteid))
431  {
432  /*
433  * This can be caused by having a publisher with a higher
434  * PostgreSQL major version than the subscriber.
435  */
436  return psprintf("unrecognized %u", remoteid);
437  }
438 
439  return format_type_be(remoteid);
440  }
441 
442  if (LogicalRepTypMap == NULL)
443  {
444  /*
445  * If the typemap is not initialized yet, we cannot possibly attempt
446  * to search the hash table; but there's no way we know the type
447  * locally yet, since we haven't received a message about this type,
448  * so this is the best we can do.
449  */
450  return psprintf("unrecognized %u", remoteid);
451  }
452 
453  /* search the mapping */
454  entry = hash_search(LogicalRepTypMap, (void *) &remoteid,
455  HASH_FIND, &found);
456  if (!found)
457  return psprintf("unrecognized %u", remoteid);
458 
459  Assert(OidIsValid(entry->remoteid));
460  return psprintf("%s.%s", entry->nspname, entry->typname);
461 }
char * typname
Definition: logicalproto.h:60
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
bool get_typisdefined(Oid typid)
Definition: lsyscache.c:1927
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
static HTAB * LogicalRepTypMap
Definition: relation.c:36
#define OidIsValid(objectId)
Definition: c.h:605
#define FirstNormalObjectId
Definition: transam.h:94
#define Assert(condition)
Definition: c.h:699
char * nspname
Definition: logicalproto.h:59

◆ logicalrep_typmap_update()

void logicalrep_typmap_update ( LogicalRepTyp remotetyp)

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

389 {
390  MemoryContext oldctx;
391  LogicalRepTyp *entry;
392  bool found;
393 
394  if (LogicalRepTypMap == NULL)
396 
397  /*
398  * HASH_ENTER returns the existing entry if present or creates a new one.
399  */
400  entry = hash_search(LogicalRepTypMap, (void *) &remotetyp->remoteid,
401  HASH_ENTER, &found);
402 
403  if (found)
405 
406  /* Make cached copy of the data */
407  entry->remoteid = remotetyp->remoteid;
409  entry->nspname = pstrdup(remotetyp->nspname);
410  entry->typname = pstrdup(remotetyp->typname);
411  MemoryContextSwitchTo(oldctx);
412 }
char * typname
Definition: logicalproto.h:60
char * pstrdup(const char *in)
Definition: mcxt.c:1161
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
static HTAB * LogicalRepTypMap
Definition: relation.c:36
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:33
static void logicalrep_relmap_init(void)
Definition: relation.c:84
static void logicalrep_typmap_free_entry(LogicalRepTyp *entry)
Definition: relation.c:378
char * nspname
Definition: logicalproto.h:59