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 "nodes/makefuncs.h"
26 #include "utils/builtins.h"
27 #include "utils/inval.h"
28 #include "utils/lsyscache.h"
29 #include "utils/memutils.h"
30 #include "utils/syscache.h"
31 
33 
36 
37 static void logicalrep_typmap_invalidate_cb(Datum arg, int cacheid,
38  uint32 hashvalue);
39 
40 /*
41  * Relcache invalidation callback for our relation map cache.
42  */
43 static void
45 {
46  LogicalRepRelMapEntry *entry;
47 
48  /* Just to be sure. */
49  if (LogicalRepRelMap == NULL)
50  return;
51 
52  if (reloid != InvalidOid)
53  {
55 
56  hash_seq_init(&status, LogicalRepRelMap);
57 
58  /* TODO, use inverse lookup hashtable? */
59  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
60  {
61  if (entry->localreloid == reloid)
62  {
63  entry->localreloid = InvalidOid;
64  hash_seq_term(&status);
65  break;
66  }
67  }
68  }
69  else
70  {
71  /* invalidate all cache entries */
73 
74  hash_seq_init(&status, LogicalRepRelMap);
75 
76  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
77  entry->localreloid = InvalidOid;
78  }
79 }
80 
81 /*
82  * Initialize the relation map cache.
83  */
84 static void
86 {
87  HASHCTL ctl;
88 
89  if (!LogicalRepRelMapContext)
90  LogicalRepRelMapContext =
92  "LogicalRepRelMapContext",
94 
95  /* Initialize the relation hash table. */
96  MemSet(&ctl, 0, sizeof(ctl));
97  ctl.keysize = sizeof(LogicalRepRelId);
98  ctl.entrysize = sizeof(LogicalRepRelMapEntry);
100 
101  LogicalRepRelMap = hash_create("logicalrep relation map cache", 128, &ctl,
103 
104  /* Initialize the type hash table. */
105  MemSet(&ctl, 0, sizeof(ctl));
106  ctl.keysize = sizeof(Oid);
107  ctl.entrysize = sizeof(LogicalRepTyp);
109 
110  /* This will usually be small. */
111  LogicalRepTypMap = hash_create("logicalrep type map cache", 2, &ctl,
113 
114  /* Watch for invalidation events. */
116  (Datum) 0);
118  (Datum) 0);
119 }
120 
121 /*
122  * Free the entry of a relation map cache.
123  */
124 static void
126 {
127  LogicalRepRelation *remoterel;
128 
129  remoterel = &entry->remoterel;
130 
131  pfree(remoterel->nspname);
132  pfree(remoterel->relname);
133 
134  if (remoterel->natts > 0)
135  {
136  int i;
137 
138  for (i = 0; i < remoterel->natts; i++)
139  pfree(remoterel->attnames[i]);
140 
141  pfree(remoterel->attnames);
142  pfree(remoterel->atttyps);
143  }
144  remoterel->attnames = NULL;
145  remoterel->atttyps = NULL;
146 
147  bms_free(remoterel->attkeys);
148  remoterel->attkeys = NULL;
149 
150  if (entry->attrmap)
151  pfree(entry->attrmap);
152 
153  entry->attrmap = NULL;
154  remoterel->natts = 0;
155  entry->localreloid = InvalidOid;
156  entry->localrel = NULL;
157 }
158 
159 /*
160  * Add new entry or update existing entry in the relation map cache.
161  *
162  * Called when new relation mapping is sent by the publisher to update
163  * our expected view of incoming data from said publisher.
164  */
165 void
167 {
168  MemoryContext oldctx;
169  LogicalRepRelMapEntry *entry;
170  bool found;
171  int i;
172 
173  if (LogicalRepRelMap == NULL)
175 
176  /*
177  * HASH_ENTER returns the existing entry if present or creates a new one.
178  */
179  entry = hash_search(LogicalRepRelMap, (void *) &remoterel->remoteid,
180  HASH_ENTER, &found);
181 
182  if (found)
184 
185  /* Make cached copy of the data */
186  oldctx = MemoryContextSwitchTo(LogicalRepRelMapContext);
187  entry->remoterel.remoteid = remoterel->remoteid;
188  entry->remoterel.nspname = pstrdup(remoterel->nspname);
189  entry->remoterel.relname = pstrdup(remoterel->relname);
190  entry->remoterel.natts = remoterel->natts;
191  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
192  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
193  for (i = 0; i < remoterel->natts; i++)
194  {
195  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
196  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
197  }
198  entry->remoterel.replident = remoterel->replident;
199  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
200  entry->attrmap = NULL;
201  entry->localreloid = InvalidOid;
202  MemoryContextSwitchTo(oldctx);
203 }
204 
205 /*
206  * Find attribute index in TupleDesc struct by attribute name.
207  *
208  * Returns -1 if not found.
209  */
210 static int
211 logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
212 {
213  int i;
214 
215  for (i = 0; i < remoterel->natts; i++)
216  {
217  if (strcmp(remoterel->attnames[i], attname) == 0)
218  return i;
219  }
220 
221  return -1;
222 }
223 
224 /*
225  * Open the local relation associated with the remote one.
226  *
227  * Optionally rebuilds the Relcache mapping if it was invalidated
228  * by local DDL.
229  */
232 {
233  LogicalRepRelMapEntry *entry;
234  bool found;
235 
236  if (LogicalRepRelMap == NULL)
238 
239  /* Search for existing entry. */
240  entry = hash_search(LogicalRepRelMap, (void *) &remoteid,
241  HASH_FIND, &found);
242 
243  if (!found)
244  elog(ERROR, "no relation map entry for remote relation ID %u",
245  remoteid);
246 
247  /* Need to update the local cache? */
248  if (!OidIsValid(entry->localreloid))
249  {
250  Oid relid;
251  int i;
252  int found;
253  Bitmapset *idkey;
254  TupleDesc desc;
255  LogicalRepRelation *remoterel;
256  MemoryContext oldctx;
257  remoterel = &entry->remoterel;
258 
259  /* Try to find and lock the relation by name. */
260  relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
261  remoterel->relname, -1),
262  lockmode, true);
263  if (!OidIsValid(relid))
264  ereport(ERROR,
265  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
266  errmsg("logical replication target relation \"%s.%s\" does not exist",
267  remoterel->nspname, remoterel->relname)));
268  entry->localrel = heap_open(relid, NoLock);
269 
270  /*
271  * We currently only support writing to regular and partitioned
272  * tables.
273  */
274  if (entry->localrel->rd_rel->relkind != RELKIND_RELATION)
275  ereport(ERROR,
276  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
277  errmsg("logical replication target relation \"%s.%s\" is not a table",
278  remoterel->nspname, remoterel->relname)));
279 
280  /*
281  * Build the mapping of local attribute numbers to remote attribute
282  * numbers and validate that we don't miss any replicated columns
283  * as that would result in potentially unwanted data loss.
284  */
285  desc = RelationGetDescr(entry->localrel);
286  oldctx = MemoryContextSwitchTo(LogicalRepRelMapContext);
287  entry->attrmap = palloc(desc->natts * sizeof(int));
288  MemoryContextSwitchTo(oldctx);
289 
290  found = 0;
291  for (i = 0; i < desc->natts; i++)
292  {
293  int attnum = logicalrep_rel_att_by_name(remoterel,
294  NameStr(desc->attrs[i]->attname));
295  entry->attrmap[i] = attnum;
296  if (attnum >= 0)
297  found++;
298  }
299 
300  /* TODO, detail message with names of missing columns */
301  if (found < remoterel->natts)
302  ereport(ERROR,
303  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
304  errmsg("logical replication target relation \"%s.%s\" is missing "
305  "some replicated columns",
306  remoterel->nspname, remoterel->relname)));
307 
308  /*
309  * Check that replica identity matches. We allow for stricter replica
310  * identity (fewer columns) on subscriber as that will not stop us
311  * from finding unique tuple. IE, if publisher has identity
312  * (id,timestamp) and subscriber just (id) this will not be a problem,
313  * but in the opposite scenario it will.
314  *
315  * Don't throw any error here just mark the relation entry as not
316  * updatable, as replica identity is only for updates and deletes
317  * but inserts can be replicated even without it.
318  */
319  entry->updatable = true;
320  idkey = RelationGetIndexAttrBitmap(entry->localrel,
322  /* fallback to PK if no replica identity */
323  if (idkey == NULL)
324  {
325  idkey = RelationGetIndexAttrBitmap(entry->localrel,
327  /*
328  * If no replica identity index and no PK, the published table
329  * must have replica identity FULL.
330  */
331  if (idkey == NULL && remoterel->replident != REPLICA_IDENTITY_FULL)
332  entry->updatable = false;
333  }
334 
335  i = -1;
336  while ((i = bms_next_member(idkey, i)) >= 0)
337  {
338  int attnum = i + FirstLowInvalidHeapAttributeNumber;
339 
340  if (!AttrNumberIsForUserDefinedAttr(attnum))
341  ereport(ERROR,
342  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
343  errmsg("logical replication target relation \"%s.%s\" uses "
344  "system columns in REPLICA IDENTITY index",
345  remoterel->nspname, remoterel->relname)));
346 
347  attnum = AttrNumberGetAttrOffset(attnum);
348 
349  if (!bms_is_member(entry->attrmap[attnum], remoterel->attkeys))
350  {
351  entry->updatable = false;
352  break;
353  }
354  }
355 
356  entry->localreloid = relid;
357  }
358  else
359  entry->localrel = heap_open(entry->localreloid, lockmode);
360 
361  if (entry->state != SUBREL_STATE_READY)
363  entry->localreloid,
364  &entry->statelsn,
365  true);
366 
367  return entry;
368 }
369 
370 /*
371  * Close the previously opened logical relation.
372  */
373 void
375 {
376  heap_close(rel->localrel, lockmode);
377  rel->localrel = NULL;
378 }
379 
380 
381 /*
382  * Type cache invalidation callback for our type map cache.
383  */
384 static void
386 {
388  LogicalRepTyp *entry;
389 
390  /* Just to be sure. */
391  if (LogicalRepTypMap == NULL)
392  return;
393 
394  /* invalidate all cache entries */
395  hash_seq_init(&status, LogicalRepTypMap);
396 
397  while ((entry = (LogicalRepTyp *) hash_seq_search(&status)) != NULL)
398  entry->typoid = InvalidOid;
399 }
400 
401 /*
402  * Free the type map cache entry data.
403  */
404 static void
406 {
407  pfree(entry->nspname);
408  pfree(entry->typname);
409 
410  entry->typoid = InvalidOid;
411 }
412 
413 /*
414  * Add new entry or update existing entry in the type map cache.
415  */
416 void
418 {
419  MemoryContext oldctx;
420  LogicalRepTyp *entry;
421  bool found;
422 
423  if (LogicalRepTypMap == NULL)
425 
426  /*
427  * HASH_ENTER returns the existing entry if present or creates a new one.
428  */
429  entry = hash_search(LogicalRepTypMap, (void *) &remotetyp->remoteid,
430  HASH_ENTER, &found);
431 
432  if (found)
434 
435  /* Make cached copy of the data */
436  entry->remoteid = remotetyp->remoteid;
437  oldctx = MemoryContextSwitchTo(LogicalRepRelMapContext);
438  entry->nspname = pstrdup(remotetyp->nspname);
439  entry->typname = pstrdup(remotetyp->typname);
440  MemoryContextSwitchTo(oldctx);
441  entry->typoid = InvalidOid;
442 }
443 
444 /*
445  * Fetch type info from the cache.
446  */
447 Oid
449 {
450  LogicalRepTyp *entry;
451  bool found;
452  Oid nspoid;
453 
454  /* Internal types are mapped directly. */
455  if (remoteid < FirstNormalObjectId)
456  {
457  if (!get_typisdefined(remoteid))
458  ereport(ERROR,
459  (errmsg("builtin type %u not found", remoteid),
460  errhint("This can be caused by having publisher with "
461  "higher major version than subscriber")));
462  return remoteid;
463  }
464 
465  if (LogicalRepTypMap == NULL)
467 
468  /* Try finding the mapping. */
469  entry = hash_search(LogicalRepTypMap, (void *) &remoteid,
470  HASH_FIND, &found);
471 
472  if (!found)
473  elog(ERROR, "no type map entry for remote type %u",
474  remoteid);
475 
476  /* Found and mapped, return the oid. */
477  if (OidIsValid(entry->typoid))
478  return entry->typoid;
479 
480  /* Otherwise, try to map to local type. */
481  nspoid = LookupExplicitNamespace(entry->nspname, true);
482  if (OidIsValid(nspoid))
484  PointerGetDatum(entry->typname),
485  ObjectIdGetDatum(nspoid));
486  else
487  entry->typoid = InvalidOid;
488 
489  if (!OidIsValid(entry->typoid))
490  ereport(ERROR,
491  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
492  errmsg("data type \"%s.%s\" required for logical replication does not exist",
493  entry->nspname, entry->typname)));
494 
495  return entry->typoid;
496 }
Subscription * MySubscription
Definition: worker.c:109
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:125
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2743
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:58
#define HASH_ELEM
Definition: hsearch.h:87
MemoryContext hcxt
Definition: hsearch.h:78
#define RelationGetDescr(relation)
Definition: rel.h:429
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:1891
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:857
void logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
Definition: relation.c:374
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
#define heap_close(r, l)
Definition: heapam.h:97
Oid logicalrep_typmap_getid(Oid remoteid)
Definition: relation.c:448
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
static HTAB * LogicalRepTypMap
Definition: relation.c:35
#define OidIsValid(objectId)
Definition: c.h:538
Bitmapset * attkeys
Definition: logicalproto.h:50
int natts
Definition: tupdesc.h:73
#define FirstNormalObjectId
Definition: transam.h:94
#define GetSysCacheOid2(cacheId, key1, key2)
Definition: syscache.h:181
LogicalRepRelId remoteid
Definition: logicalproto.h:43
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1405
void logicalrep_relmap_update(LogicalRepRelation *remoterel)
Definition: relation.c:166
Definition: dynahash.c:193
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:211
LogicalRepRelation remoterel
static void logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:44
#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:32
unsigned int uint32
Definition: c.h:268
LogicalRepRelMapEntry * logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
Definition: relation.c:231
#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:385
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:1381
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
uintptr_t Datum
Definition: postgres.h:372
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
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:1353
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn, bool missing_ok)
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1343
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:4836
#define NameStr(name)
Definition: c.h:499
void * arg
#define elog
Definition: elog.h:219
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
#define RELKIND_RELATION
Definition: pg_class.h:160
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:405
void hash_seq_term(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1429
char * nspname
Definition: logicalproto.h:57
void logicalrep_typmap_update(LogicalRepTyp *remotetyp)
Definition: relation.c:417
static HTAB * LogicalRepRelMap
Definition: relation.c:34
static void logicalrep_relmap_init()
Definition: relation.c:85
uint32 LogicalRepRelId
Definition: logicalproto.h:37
MemoryContext CacheMemoryContext
Definition: mcxt.c:46