PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
relation.c File Reference
Include dependency graph for relation.c:

Go to the source code of this file.

Functions

static void logicalrep_typmap_invalidate_cb (Datum arg, int cacheid, uint32 hashvalue)
 
static void logicalrep_relmap_invalidate_cb (Datum arg, Oid reloid)
 
static void logicalrep_relmap_init (void)
 
static void logicalrep_relmap_free_entry (LogicalRepRelMapEntry *entry)
 
void logicalrep_relmap_update (LogicalRepRelation *remoterel)
 
static int logicalrep_rel_att_by_name (LogicalRepRelation *remoterel, const char *attname)
 
LogicalRepRelMapEntrylogicalrep_rel_open (LogicalRepRelId remoteid, LOCKMODE lockmode)
 
void logicalrep_rel_close (LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
 
static void logicalrep_typmap_free_entry (LogicalRepTyp *entry)
 
void logicalrep_typmap_update (LogicalRepTyp *remotetyp)
 
Oid logicalrep_typmap_getid (Oid remoteid)
 

Variables

static MemoryContext LogicalRepRelMapContext = NULL
 
static HTABLogicalRepRelMap = NULL
 
static HTABLogicalRepTypMap = NULL
 

Function Documentation

static int logicalrep_rel_att_by_name ( LogicalRepRelation remoterel,
const char *  attname 
)
static

Definition at line 202 of file relation.c.

References LogicalRepRelation::attnames, i, and LogicalRepRelation::natts.

Referenced by logicalrep_rel_open().

203 {
204  int i;
205 
206  for (i = 0; i < remoterel->natts; i++)
207  {
208  if (strcmp(remoterel->attnames[i], attname) == 0)
209  return i;
210  }
211 
212  return -1;
213 }
int i
void logicalrep_rel_close ( LogicalRepRelMapEntry rel,
LOCKMODE  lockmode 
)

Definition at line 365 of file relation.c.

References heap_close, LogicalRepRelMapEntry::localrel, and NULL.

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

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

Definition at line 222 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, 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, NULL, Subscription::oid, OidIsValid, palloc(), RangeVarGetRelid, RelationData::rd_rel, RelationGetDescr, RelationGetIndexAttrBitmap(), RELKIND_RELATION, LogicalRepRelation::relname, LogicalRepRelMapEntry::remoterel, REPLICA_IDENTITY_FULL, LogicalRepRelation::replident, LogicalRepRelMapEntry::state, LogicalRepRelMapEntry::statelsn, SUBREL_STATE_READY, and LogicalRepRelMapEntry::updatable.

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

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

Definition at line 125 of file relation.c.

References LogicalRepRelation::attkeys, LogicalRepRelation::attnames, LogicalRepRelMapEntry::attrmap, LogicalRepRelation::atttyps, bms_free(), i, LogicalRepRelation::natts, LogicalRepRelation::nspname, pfree(), LogicalRepRelation::relname, and LogicalRepRelMapEntry::remoterel.

Referenced by logicalrep_relmap_update().

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  bms_free(remoterel->attkeys);
145 
146  if (entry->attrmap)
147  pfree(entry->attrmap);
148 }
Bitmapset * attkeys
Definition: logicalproto.h:50
void pfree(void *pointer)
Definition: mcxt.c:950
LogicalRepRelation remoterel
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
int i
static void logicalrep_relmap_init ( void  )
static

Definition at line 85 of file relation.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), CacheMemoryContext, CacheRegisterRelcacheCallback(), CacheRegisterSyscacheCallback(), HASHCTL::entrysize, HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, HASHCTL::hcxt, HASHCTL::keysize, logicalrep_relmap_invalidate_cb(), logicalrep_typmap_invalidate_cb(), LogicalRepRelMapContext, MemSet, and TYPEOID.

Referenced by logicalrep_rel_open(), logicalrep_relmap_update(), logicalrep_typmap_getid(), and logicalrep_typmap_update().

86 {
87  HASHCTL ctl;
88 
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 }
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
MemoryContext hcxt
Definition: hsearch.h:78
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:857
unsigned int Oid
Definition: postgres_ext.h:31
static HTAB * LogicalRepTypMap
Definition: relation.c:35
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1406
static void logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:44
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:32
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:376
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:1382
uintptr_t Datum
Definition: postgres.h:372
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
Size keysize
Definition: hsearch.h:72
static HTAB * LogicalRepRelMap
Definition: relation.c:34
uint32 LogicalRepRelId
Definition: logicalproto.h:37
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
static void logicalrep_relmap_invalidate_cb ( Datum  arg,
Oid  reloid 
)
static

Definition at line 44 of file relation.c.

References hash_seq_init(), hash_seq_search(), hash_seq_term(), InvalidOid, LogicalRepRelMapEntry::localreloid, NULL, and status().

Referenced by logicalrep_relmap_init().

45 {
46  LogicalRepRelMapEntry *entry;
47 
48  /* Just to be sure. */
49  if (LogicalRepRelMap == NULL)
50  return;
51 
52  if (reloid != InvalidOid)
53  {
55 
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 
75 
76  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
77  entry->localreloid = InvalidOid;
78  }
79 }
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
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
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
void hash_seq_term(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1429
static HTAB * LogicalRepRelMap
Definition: relation.c:34
void logicalrep_relmap_update ( LogicalRepRelation remoterel)

Definition at line 157 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, NULL, palloc(), pstrdup(), LogicalRepRelation::relname, LogicalRepRelation::remoteid, LogicalRepRelMapEntry::remoterel, and LogicalRepRelation::replident.

Referenced by apply_handle_relation(), and copy_table().

158 {
159  MemoryContext oldctx;
160  LogicalRepRelMapEntry *entry;
161  bool found;
162  int i;
163 
164  if (LogicalRepRelMap == NULL)
166 
167  /*
168  * HASH_ENTER returns the existing entry if present or creates a new one.
169  */
170  entry = hash_search(LogicalRepRelMap, (void *) &remoterel->remoteid,
171  HASH_ENTER, &found);
172 
173  if (found)
175 
176  memset(entry, 0, sizeof(LogicalRepRelMapEntry));
177 
178  /* Make cached copy of the data */
180  entry->remoterel.remoteid = remoterel->remoteid;
181  entry->remoterel.nspname = pstrdup(remoterel->nspname);
182  entry->remoterel.relname = pstrdup(remoterel->relname);
183  entry->remoterel.natts = remoterel->natts;
184  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
185  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
186  for (i = 0; i < remoterel->natts; i++)
187  {
188  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
189  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
190  }
191  entry->remoterel.replident = remoterel->replident;
192  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
193  MemoryContextSwitchTo(oldctx);
194 }
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:125
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
char * pstrdup(const char *in)
Definition: mcxt.c:1077
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:32
#define NULL
Definition: c.h:229
void * palloc(Size size)
Definition: mcxt.c:849
int i
static void logicalrep_relmap_init(void)
Definition: relation.c:85
static HTAB * LogicalRepRelMap
Definition: relation.c:34
static void logicalrep_typmap_free_entry ( LogicalRepTyp entry)
static

Definition at line 396 of file relation.c.

References InvalidOid, LogicalRepTyp::nspname, pfree(), LogicalRepTyp::typname, and LogicalRepTyp::typoid.

Referenced by logicalrep_typmap_update().

397 {
398  pfree(entry->nspname);
399  pfree(entry->typname);
400 
401  entry->typoid = InvalidOid;
402 }
char * typname
Definition: logicalproto.h:58
void pfree(void *pointer)
Definition: mcxt.c:950
#define InvalidOid
Definition: postgres_ext.h:36
char * nspname
Definition: logicalproto.h:57
Oid logicalrep_typmap_getid ( Oid  remoteid)

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

440 {
441  LogicalRepTyp *entry;
442  bool found;
443  Oid nspoid;
444 
445  /* Internal types are mapped directly. */
446  if (remoteid < FirstNormalObjectId)
447  {
448  if (!get_typisdefined(remoteid))
449  ereport(ERROR,
450  (errmsg("builtin type %u not found", remoteid),
451  errhint("This can be caused by having publisher with "
452  "higher major version than subscriber")));
453  return remoteid;
454  }
455 
456  if (LogicalRepTypMap == NULL)
458 
459  /* Try finding the mapping. */
460  entry = hash_search(LogicalRepTypMap, (void *) &remoteid,
461  HASH_FIND, &found);
462 
463  if (!found)
464  elog(ERROR, "no type map entry for remote type %u",
465  remoteid);
466 
467  /* Found and mapped, return the oid. */
468  if (OidIsValid(entry->typoid))
469  return entry->typoid;
470 
471  /* Otherwise, try to map to local type. */
472  nspoid = LookupExplicitNamespace(entry->nspname, true);
473  if (OidIsValid(nspoid))
475  PointerGetDatum(entry->typname),
476  ObjectIdGetDatum(nspoid));
477  else
478  entry->typoid = InvalidOid;
479 
480  if (!OidIsValid(entry->typoid))
481  ereport(ERROR,
482  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
483  errmsg("data type \"%s.%s\" required for logical replication does not exist",
484  entry->nspname, entry->typname)));
485 
486  return entry->typoid;
487 }
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2743
int errhint(const char *fmt,...)
Definition: elog.c:987
char * typname
Definition: logicalproto.h:58
#define PointerGetDatum(X)
Definition: postgres.h:562
bool get_typisdefined(Oid typid)
Definition: lsyscache.c:1923
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:35
#define OidIsValid(objectId)
Definition: c.h:538
#define FirstNormalObjectId
Definition: transam.h:94
#define GetSysCacheOid2(cacheId, key1, key2)
Definition: syscache.h:181
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#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:229
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
static void logicalrep_relmap_init(void)
Definition: relation.c:85
char * nspname
Definition: logicalproto.h:57
static void logicalrep_typmap_invalidate_cb ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)
static

Definition at line 376 of file relation.c.

References hash_seq_init(), hash_seq_search(), InvalidOid, NULL, status(), and LogicalRepTyp::typoid.

Referenced by logicalrep_relmap_init().

377 {
379  LogicalRepTyp *entry;
380 
381  /* Just to be sure. */
382  if (LogicalRepTypMap == NULL)
383  return;
384 
385  /* invalidate all cache entries */
387 
388  while ((entry = (LogicalRepTyp *) hash_seq_search(&status)) != NULL)
389  entry->typoid = InvalidOid;
390 }
static HTAB * LogicalRepTypMap
Definition: relation.c:35
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
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
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
void logicalrep_typmap_update ( LogicalRepTyp remotetyp)

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

409 {
410  MemoryContext oldctx;
411  LogicalRepTyp *entry;
412  bool found;
413 
414  if (LogicalRepTypMap == NULL)
416 
417  /*
418  * HASH_ENTER returns the existing entry if present or creates a new one.
419  */
420  entry = hash_search(LogicalRepTypMap, (void *) &remotetyp->remoteid,
421  HASH_ENTER, &found);
422 
423  if (found)
425 
426  /* Make cached copy of the data */
427  entry->remoteid = remotetyp->remoteid;
429  entry->nspname = pstrdup(remotetyp->nspname);
430  entry->typname = pstrdup(remotetyp->typname);
431  MemoryContextSwitchTo(oldctx);
432  entry->typoid = InvalidOid;
433 }
char * typname
Definition: logicalproto.h:58
char * pstrdup(const char *in)
Definition: mcxt.c:1077
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:35
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:32
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
static void logicalrep_relmap_init(void)
Definition: relation.c:85
static void logicalrep_typmap_free_entry(LogicalRepTyp *entry)
Definition: relation.c:396
char * nspname
Definition: logicalproto.h:57

Variable Documentation

HTAB* LogicalRepRelMap = NULL
static

Definition at line 34 of file relation.c.

MemoryContext LogicalRepRelMapContext = NULL
static

Definition at line 32 of file relation.c.

Referenced by logicalrep_relmap_init().

HTAB* LogicalRepTypMap = NULL
static

Definition at line 35 of file relation.c.