PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
relation.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  * relation.c
3  * PostgreSQL logical replication
4  *
5  * Copyright (c) 2016-2017, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * src/backend/replication/logical/relation.c
9  *
10  * NOTES
11  * This file contains helper functions for logical replication relation
12  * mapping cache.
13  *
14  *-------------------------------------------------------------------------
15  */
16 
17 #include "postgres.h"
18 
19 #include "access/heapam.h"
20 #include "access/sysattr.h"
21 #include "catalog/namespace.h"
23 #include "executor/executor.h"
24 #include "nodes/makefuncs.h"
27 #include "utils/builtins.h"
28 #include "utils/inval.h"
29 #include "utils/lsyscache.h"
30 #include "utils/memutils.h"
31 #include "utils/syscache.h"
32 
34 
37 
38 static void logicalrep_typmap_invalidate_cb(Datum arg, int cacheid,
39  uint32 hashvalue);
40 
41 /*
42  * Relcache invalidation callback for our relation map cache.
43  */
44 static void
46 {
47  LogicalRepRelMapEntry *entry;
48 
49  /* Just to be sure. */
50  if (LogicalRepRelMap == NULL)
51  return;
52 
53  if (reloid != InvalidOid)
54  {
56 
57  hash_seq_init(&status, LogicalRepRelMap);
58 
59  /* TODO, use inverse lookup hashtable? */
60  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
61  {
62  if (entry->localreloid == reloid)
63  {
64  entry->localreloid = InvalidOid;
65  hash_seq_term(&status);
66  break;
67  }
68  }
69  }
70  else
71  {
72  /* invalidate all cache entries */
74 
75  hash_seq_init(&status, LogicalRepRelMap);
76 
77  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
78  entry->localreloid = InvalidOid;
79  }
80 }
81 
82 /*
83  * Initialize the relation map cache.
84  */
85 static void
87 {
88  HASHCTL ctl;
89 
90  if (!LogicalRepRelMapContext)
91  LogicalRepRelMapContext =
93  "LogicalRepRelMapContext",
95 
96  /* Initialize the relation hash table. */
97  MemSet(&ctl, 0, sizeof(ctl));
98  ctl.keysize = sizeof(LogicalRepRelId);
99  ctl.entrysize = sizeof(LogicalRepRelMapEntry);
101 
102  LogicalRepRelMap = hash_create("logicalrep relation map cache", 128, &ctl,
104 
105  /* Initialize the type hash table. */
106  MemSet(&ctl, 0, sizeof(ctl));
107  ctl.keysize = sizeof(Oid);
108  ctl.entrysize = sizeof(LogicalRepTyp);
110 
111  /* This will usually be small. */
112  LogicalRepTypMap = hash_create("logicalrep type map cache", 2, &ctl,
114 
115  /* Watch for invalidation events. */
117  (Datum) 0);
119  (Datum) 0);
120 }
121 
122 /*
123  * Free the entry of a relation map cache.
124  */
125 static void
127 {
128  LogicalRepRelation *remoterel;
129 
130  remoterel = &entry->remoterel;
131 
132  pfree(remoterel->nspname);
133  pfree(remoterel->relname);
134 
135  if (remoterel->natts > 0)
136  {
137  int i;
138 
139  for (i = 0; i < remoterel->natts; i++)
140  pfree(remoterel->attnames[i]);
141 
142  pfree(remoterel->attnames);
143  pfree(remoterel->atttyps);
144  }
145  bms_free(remoterel->attkeys);
146 
147  if (entry->attrmap)
148  pfree(entry->attrmap);
149 }
150 
151 /*
152  * Add new entry or update existing entry in the relation map cache.
153  *
154  * Called when new relation mapping is sent by the publisher to update
155  * our expected view of incoming data from said publisher.
156  */
157 void
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 */
180  oldctx = MemoryContextSwitchTo(LogicalRepRelMapContext);
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 }
196 
197 /*
198  * Find attribute index in TupleDesc struct by attribute name.
199  *
200  * Returns -1 if not found.
201  */
202 static int
203 logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
204 {
205  int i;
206 
207  for (i = 0; i < remoterel->natts; i++)
208  {
209  if (strcmp(remoterel->attnames[i], attname) == 0)
210  return i;
211  }
212 
213  return -1;
214 }
215 
216 /*
217  * Open the local relation associated with the remote one.
218  *
219  * Optionally rebuilds the Relcache mapping if it was invalidated
220  * by local DDL.
221  */
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);
273  oldctx = MemoryContextSwitchTo(LogicalRepRelMapContext);
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 
282  if (desc->attrs[i]->attisdropped)
283  {
284  entry->attrmap[i] = -1;
285  continue;
286  }
287 
288  attnum = logicalrep_rel_att_by_name(remoterel,
289  NameStr(desc->attrs[i]->attname));
290 
291  entry->attrmap[i] = attnum;
292  if (attnum >= 0)
293  found++;
294  }
295 
296  /* TODO, detail message with names of missing columns */
297  if (found < remoterel->natts)
298  ereport(ERROR,
299  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
300  errmsg("logical replication target relation \"%s.%s\" is missing "
301  "some replicated columns",
302  remoterel->nspname, remoterel->relname)));
303 
304  /*
305  * Check that replica identity matches. We allow for stricter replica
306  * identity (fewer columns) on subscriber as that will not stop us
307  * from finding unique tuple. IE, if publisher has identity
308  * (id,timestamp) and subscriber just (id) this will not be a problem,
309  * but in the opposite scenario it will.
310  *
311  * Don't throw any error here just mark the relation entry as not
312  * updatable, as replica identity is only for updates and deletes but
313  * inserts can be replicated even without it.
314  */
315  entry->updatable = true;
316  idkey = RelationGetIndexAttrBitmap(entry->localrel,
318  /* fallback to PK if no replica identity */
319  if (idkey == NULL)
320  {
321  idkey = RelationGetIndexAttrBitmap(entry->localrel,
323 
324  /*
325  * If no replica identity index and no PK, the published table
326  * must have replica identity FULL.
327  */
328  if (idkey == NULL && remoterel->replident != REPLICA_IDENTITY_FULL)
329  entry->updatable = false;
330  }
331 
332  i = -1;
333  while ((i = bms_next_member(idkey, i)) >= 0)
334  {
335  int attnum = i + FirstLowInvalidHeapAttributeNumber;
336 
337  if (!AttrNumberIsForUserDefinedAttr(attnum))
338  ereport(ERROR,
339  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
340  errmsg("logical replication target relation \"%s.%s\" uses "
341  "system columns in REPLICA IDENTITY index",
342  remoterel->nspname, remoterel->relname)));
343 
344  attnum = AttrNumberGetAttrOffset(attnum);
345 
346  if (!bms_is_member(entry->attrmap[attnum], remoterel->attkeys))
347  {
348  entry->updatable = false;
349  break;
350  }
351  }
352 
353  entry->localreloid = relid;
354  }
355  else
356  entry->localrel = heap_open(entry->localreloid, lockmode);
357 
358  if (entry->state != SUBREL_STATE_READY)
360  entry->localreloid,
361  &entry->statelsn,
362  true);
363 
364  return entry;
365 }
366 
367 /*
368  * Close the previously opened logical relation.
369  */
370 void
372 {
373  heap_close(rel->localrel, lockmode);
374  rel->localrel = NULL;
375 }
376 
377 
378 /*
379  * Type cache invalidation callback for our type map cache.
380  */
381 static void
383 {
385  LogicalRepTyp *entry;
386 
387  /* Just to be sure. */
388  if (LogicalRepTypMap == NULL)
389  return;
390 
391  /* invalidate all cache entries */
392  hash_seq_init(&status, LogicalRepTypMap);
393 
394  while ((entry = (LogicalRepTyp *) hash_seq_search(&status)) != NULL)
395  entry->typoid = InvalidOid;
396 }
397 
398 /*
399  * Free the type map cache entry data.
400  */
401 static void
403 {
404  pfree(entry->nspname);
405  pfree(entry->typname);
406 
407  entry->typoid = InvalidOid;
408 }
409 
410 /*
411  * Add new entry or update existing entry in the type map cache.
412  */
413 void
415 {
416  MemoryContext oldctx;
417  LogicalRepTyp *entry;
418  bool found;
419 
420  if (LogicalRepTypMap == NULL)
422 
423  /*
424  * HASH_ENTER returns the existing entry if present or creates a new one.
425  */
426  entry = hash_search(LogicalRepTypMap, (void *) &remotetyp->remoteid,
427  HASH_ENTER, &found);
428 
429  if (found)
431 
432  /* Make cached copy of the data */
433  entry->remoteid = remotetyp->remoteid;
434  oldctx = MemoryContextSwitchTo(LogicalRepRelMapContext);
435  entry->nspname = pstrdup(remotetyp->nspname);
436  entry->typname = pstrdup(remotetyp->typname);
437  MemoryContextSwitchTo(oldctx);
438  entry->typoid = InvalidOid;
439 }
440 
441 /*
442  * Fetch type info from the cache.
443  */
444 Oid
446 {
447  LogicalRepTyp *entry;
448  bool found;
449  Oid nspoid;
450 
451  /* Internal types are mapped directly. */
452  if (remoteid < FirstNormalObjectId)
453  {
454  if (!get_typisdefined(remoteid))
455  ereport(ERROR,
456  (errmsg("builtin type %u not found", remoteid),
457  errhint("This can be caused by having publisher with "
458  "higher major version than 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 }
Subscription * MySubscription
Definition: worker.c:112
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:126
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2853
int errhint(const char *fmt,...)
Definition: elog.c:987
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
#define HASH_CONTEXT
Definition: hsearch.h:93
char * typname
Definition: logicalproto.h:60
#define HASH_ELEM
Definition: hsearch.h:87
MemoryContext hcxt
Definition: hsearch.h:78
#define RelationGetDescr(relation)
Definition: rel.h:428
int LOCKMODE
Definition: lockdefs.h:26
#define PointerGetDatum(X)
Definition: postgres.h:562
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:53
char * pstrdup(const char *in)
Definition: mcxt.c:1077
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:937
Form_pg_attribute * attrs
Definition: tupdesc.h:74
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Size entrysize
Definition: hsearch.h:73
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
Definition: attnum.h:41
bool get_typisdefined(Oid typid)
Definition: lsyscache.c:1923
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:858
void logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
Definition: relation.c:371
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
#define heap_close(r, l)
Definition: heapam.h:97
Oid logicalrep_typmap_getid(Oid remoteid)
Definition: relation.c:445
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
static HTAB * LogicalRepTypMap
Definition: relation.c:36
#define OidIsValid(objectId)
Definition: c.h:538
Bitmapset * attkeys
Definition: logicalproto.h:52
int natts
Definition: tupdesc.h:73
#define FirstNormalObjectId
Definition: transam.h:94
#define GetSysCacheOid2(cacheId, key1, key2)
Definition: syscache.h:185
LogicalRepRelId remoteid
Definition: logicalproto.h:45
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1431
void logicalrep_relmap_update(LogicalRepRelation *remoterel)
Definition: relation.c:158
Definition: dynahash.c:208
void pfree(void *pointer)
Definition: mcxt.c:950
#define REPLICA_IDENTITY_FULL
Definition: pg_class.h:179
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
static int logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
Definition: relation.c:203
LogicalRepRelation remoterel
static void logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:45
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
#define AttrNumberGetAttrOffset(attNum)
Definition: attnum.h:51
#define NoLock
Definition: lockdefs.h:34
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:33
unsigned int uint32
Definition: c.h:268
void CheckSubscriptionRelkind(char relkind, const char *nspname, const char *relname)
LogicalRepRelMapEntry * logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
Definition: relation.c:223
#define ereport(elevel, rest)
Definition: elog.h:122
struct LogicalRepTyp LogicalRepTyp
#define HASH_BLOBS
Definition: hsearch.h:88
static void logicalrep_typmap_invalidate_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: relation.c:382
struct LogicalRepRelMapEntry LogicalRepRelMapEntry
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1389
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
uintptr_t Datum
Definition: postgres.h:372
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
Size keysize
Definition: hsearch.h:72
#define InvalidOid
Definition: postgres_ext.h:36
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
#define NULL
Definition: c.h:229
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1385
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn, bool missing_ok)
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1375
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition: relcache.c:4853
#define NameStr(name)
Definition: c.h:499
void * arg
#define elog
Definition: elog.h:219
static void logicalrep_relmap_init(void)
Definition: relation.c:86
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
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:419
static void logicalrep_typmap_free_entry(LogicalRepTyp *entry)
Definition: relation.c:402
void hash_seq_term(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1461
char * nspname
Definition: logicalproto.h:59
void logicalrep_typmap_update(LogicalRepTyp *remotetyp)
Definition: relation.c:414
static HTAB * LogicalRepRelMap
Definition: relation.c:35
uint32 LogicalRepRelId
Definition: logicalproto.h:39
MemoryContext CacheMemoryContext
Definition: mcxt.c:46