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"
22 #include "nodes/makefuncs.h"
25 #include "utils/builtins.h"
26 #include "utils/inval.h"
27 #include "utils/lsyscache.h"
28 #include "utils/memutils.h"
29 #include "utils/syscache.h"
30 
32 
35 
36 static void logicalrep_typmap_invalidate_cb(Datum arg, int cacheid,
37  uint32 hashvalue);
38 
39 /*
40  * Relcache invalidation callback for our relation map cache.
41  */
42 static void
44 {
45  LogicalRepRelMapEntry *entry;
46 
47  /* Just to be sure. */
48  if (LogicalRepRelMap == NULL)
49  return;
50 
51  if (reloid != InvalidOid)
52  {
54 
55  hash_seq_init(&status, LogicalRepRelMap);
56 
57  /* TODO, use inverse lookup hashtable? */
58  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
59  {
60  if (entry->localreloid == reloid)
61  {
62  entry->localreloid = InvalidOid;
63  hash_seq_term(&status);
64  break;
65  }
66  }
67  }
68  else
69  {
70  /* invalidate all cache entries */
72 
73  hash_seq_init(&status, LogicalRepRelMap);
74 
75  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
76  entry->localreloid = InvalidOid;
77  }
78 }
79 
80 /*
81  * Initialize the relation map cache.
82  */
83 static void
85 {
86  HASHCTL ctl;
87 
88  if (!LogicalRepRelMapContext)
89  LogicalRepRelMapContext =
91  "LogicalRepRelMapContext",
93 
94  /* Initialize the relation hash table. */
95  MemSet(&ctl, 0, sizeof(ctl));
96  ctl.keysize = sizeof(LogicalRepRelId);
97  ctl.entrysize = sizeof(LogicalRepRelMapEntry);
99 
100  LogicalRepRelMap = hash_create("logicalrep relation map cache", 128, &ctl,
102 
103  /* Initialize the type hash table. */
104  MemSet(&ctl, 0, sizeof(ctl));
105  ctl.keysize = sizeof(Oid);
106  ctl.entrysize = sizeof(LogicalRepTyp);
108 
109  /* This will usually be small. */
110  LogicalRepTypMap = hash_create("logicalrep type map cache", 2, &ctl,
112 
113  /* Watch for invalidation events. */
115  (Datum) 0);
117  (Datum) 0);
118 }
119 
120 /*
121  * Free the entry of a relation map cache.
122  */
123 static void
125 {
126  LogicalRepRelation *remoterel;
127 
128  remoterel = &entry->remoterel;
129 
130  pfree(remoterel->nspname);
131  pfree(remoterel->relname);
132 
133  if (remoterel->natts > 0)
134  {
135  int i;
136 
137  for (i = 0; i < remoterel->natts; i++)
138  pfree(remoterel->attnames[i]);
139 
140  pfree(remoterel->attnames);
141  pfree(remoterel->atttyps);
142  }
143  remoterel->attnames = NULL;
144  remoterel->atttyps = NULL;
145 
146  bms_free(remoterel->attkeys);
147  remoterel->attkeys = NULL;
148 
149  if (entry->attrmap)
150  pfree(entry->attrmap);
151 
152  entry->attrmap = NULL;
153  remoterel->natts = 0;
154  entry->localreloid = InvalidOid;
155  entry->localrel = NULL;
156 }
157 
158 /*
159  * Add new entry or update existing entry in the relation map cache.
160  *
161  * Called when new relation mapping is sent by the publisher to update
162  * our expected view of incoming data from said publisher.
163  */
164 void
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 */
185  oldctx = MemoryContextSwitchTo(LogicalRepRelMapContext);
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 }
203 
204 /*
205  * Find attribute index in TupleDesc struct by attribute name.
206  *
207  * Returns -1 if not found.
208  */
209 static int
210 logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
211 {
212  int i;
213 
214  for (i = 0; i < remoterel->natts; i++)
215  {
216  if (strcmp(remoterel->attnames[i], attname) == 0)
217  return i;
218  }
219 
220  return -1;
221 }
222 
223 /*
224  * Open the local relation associated with the remote one.
225  *
226  * Optionally rebuilds the Relcache mapping if it was invalidated
227  * by local DDL.
228  */
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);
285  oldctx = MemoryContextSwitchTo(LogicalRepRelMapContext);
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 }
362 
363 /*
364  * Close the previously opened logical relation.
365  */
366 void
368 {
369  heap_close(rel->localrel, lockmode);
370  rel->localrel = NULL;
371 }
372 
373 
374 /*
375  * Type cache invalidation callback for our type map cache.
376  */
377 static void
379 {
381  LogicalRepTyp *entry;
382 
383  /* Just to be sure. */
384  if (LogicalRepTypMap == NULL)
385  return;
386 
387  /* invalidate all cache entries */
388  hash_seq_init(&status, LogicalRepTypMap);
389 
390  while ((entry = (LogicalRepTyp *) hash_seq_search(&status)) != NULL)
391  entry->typoid = InvalidOid;
392 }
393 
394 /*
395  * Free the type map cache entry data.
396  */
397 static void
399 {
400  pfree(entry->nspname);
401  pfree(entry->typname);
402 
403  entry->typoid = InvalidOid;
404 }
405 
406 /*
407  * Add new entry or update existing entry in the type map cache.
408  */
409 void
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;
430  oldctx = MemoryContextSwitchTo(LogicalRepRelMapContext);
431  entry->nspname = pstrdup(remotetyp->nspname);
432  entry->typname = pstrdup(remotetyp->typname);
433  MemoryContextSwitchTo(oldctx);
434  entry->typoid = InvalidOid;
435 }
436 
437 /*
438  * Fetch type info from the cache.
439  */
440 Oid
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 }
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:124
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2687
int errhint(const char *fmt,...)
Definition: elog.c:987
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:110
#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:425
int LOCKMODE
Definition: lockdefs.h:26
#define PointerGetDatum(X)
Definition: postgres.h:564
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:53
char * pstrdup(const char *in)
Definition: mcxt.c:1165
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
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:853
void logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
Definition: relation.c:367
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
#define heap_close(r, l)
Definition: heapam.h:97
Oid logicalrep_typmap_getid(Oid remoteid)
Definition: relation.c:441
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
static HTAB * LogicalRepTypMap
Definition: relation.c:34
#define OidIsValid(objectId)
Definition: c.h:534
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:178
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:165
Definition: dynahash.c:193
void pfree(void *pointer)
Definition: mcxt.c:992
#define REPLICA_IDENTITY_FULL
Definition: pg_class.h:179
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
static int logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
Definition: relation.c:210
LogicalRepRelation remoterel
static void logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:43
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
#define AttrNumberGetAttrOffset(attNum)
Definition: attnum.h:51
#define NoLock
Definition: lockdefs.h:34
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:31
unsigned int uint32
Definition: c.h:265
LogicalRepRelMapEntry * logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
Definition: relation.c:230
#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:378
struct LogicalRepRelMapEntry LogicalRepRelMapEntry
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
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:374
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:200
#define NULL
Definition: c.h:226
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1353
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1343
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
void * arg
#define elog
Definition: elog.h:219
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:222
#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 void logicalrep_typmap_free_entry(LogicalRepTyp *entry)
Definition: relation.c:398
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:410
static HTAB * LogicalRepRelMap
Definition: relation.c:33
static void logicalrep_relmap_init()
Definition: relation.c:84
uint32 LogicalRepRelId
Definition: logicalproto.h:37
MemoryContext CacheMemoryContext
Definition: mcxt.c:46