PostgreSQL Source Code  git master
pg_subscription_rel.h File Reference
#include "access/xlogdefs.h"
#include "catalog/genbki.h"
#include "catalog/pg_subscription_rel_d.h"
#include "nodes/pg_list.h"
Include dependency graph for pg_subscription_rel.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SubscriptionRelState
 

Typedefs

typedef FormData_pg_subscription_relForm_pg_subscription_rel
 
typedef struct SubscriptionRelState SubscriptionRelState
 

Functions

 CATALOG (pg_subscription_rel, 6102, SubscriptionRelRelationId)
 
 DECLARE_UNIQUE_INDEX_PKEY (pg_subscription_rel_srrelid_srsubid_index, 6117, SubscriptionRelSrrelidSrsubidIndexId, pg_subscription_rel, btree(srrelid oid_ops, srsubid oid_ops))
 
 MAKE_SYSCACHE (SUBSCRIPTIONRELMAP, pg_subscription_rel_srrelid_srsubid_index, 64)
 
void AddSubscriptionRelState (Oid subid, Oid relid, char state, XLogRecPtr sublsn, bool retain_lock)
 
void UpdateSubscriptionRelState (Oid subid, Oid relid, char state, XLogRecPtr sublsn)
 
char GetSubscriptionRelState (Oid subid, Oid relid, XLogRecPtr *sublsn)
 
void RemoveSubscriptionRel (Oid subid, Oid relid)
 
bool HasSubscriptionRelations (Oid subid)
 
ListGetSubscriptionRelations (Oid subid, bool not_ready)
 

Variables

 FormData_pg_subscription_rel
 

Typedef Documentation

◆ Form_pg_subscription_rel

◆ SubscriptionRelState

Function Documentation

◆ AddSubscriptionRelState()

void AddSubscriptionRelState ( Oid  subid,
Oid  relid,
char  state,
XLogRecPtr  sublsn,
bool  retain_lock 
)

Definition at line 236 of file pg_subscription.c.

238 {
239  Relation rel;
240  HeapTuple tup;
241  bool nulls[Natts_pg_subscription_rel];
242  Datum values[Natts_pg_subscription_rel];
243 
244  LockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
245 
246  rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
247 
248  /* Try finding existing mapping. */
249  tup = SearchSysCacheCopy2(SUBSCRIPTIONRELMAP,
250  ObjectIdGetDatum(relid),
251  ObjectIdGetDatum(subid));
252  if (HeapTupleIsValid(tup))
253  elog(ERROR, "subscription table %u in subscription %u already exists",
254  relid, subid);
255 
256  /* Form the tuple. */
257  memset(values, 0, sizeof(values));
258  memset(nulls, false, sizeof(nulls));
259  values[Anum_pg_subscription_rel_srsubid - 1] = ObjectIdGetDatum(subid);
260  values[Anum_pg_subscription_rel_srrelid - 1] = ObjectIdGetDatum(relid);
261  values[Anum_pg_subscription_rel_srsubstate - 1] = CharGetDatum(state);
262  if (sublsn != InvalidXLogRecPtr)
263  values[Anum_pg_subscription_rel_srsublsn - 1] = LSNGetDatum(sublsn);
264  else
265  nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
266 
267  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
268 
269  /* Insert tuple into catalog. */
270  CatalogTupleInsert(rel, tup);
271 
272  heap_freetuple(tup);
273 
274  /* Cleanup. */
275  if (retain_lock)
276  {
277  table_close(rel, NoLock);
278  }
279  else
280  {
282  UnlockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
283  }
284 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1434
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:233
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1045
void UnlockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1104
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
#define RowExclusiveLock
Definition: lockdefs.h:38
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:28
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum CharGetDatum(char X)
Definition: postgres.h:122
#define RelationGetDescr(relation)
Definition: rel.h:531
Definition: regguts.h:323
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:88
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References AccessShareLock, CatalogTupleInsert(), CharGetDatum(), elog, ERROR, heap_form_tuple(), heap_freetuple(), HeapTupleIsValid, InvalidXLogRecPtr, LockSharedObject(), LSNGetDatum(), NoLock, ObjectIdGetDatum(), RelationGetDescr, RowExclusiveLock, SearchSysCacheCopy2, table_close(), table_open(), UnlockSharedObject(), and values.

Referenced by AlterSubscription_refresh(), binary_upgrade_add_sub_rel_state(), and CreateSubscription().

◆ CATALOG()

CATALOG ( pg_subscription_rel  ,
6102  ,
SubscriptionRelRelationId   
)

Definition at line 31 of file pg_subscription_rel.h.

32 {
33  Oid srsubid BKI_LOOKUP(pg_subscription); /* Oid of subscription */
34  Oid srrelid BKI_LOOKUP(pg_class); /* Oid of relation */
35  char srsubstate; /* state of the relation in subscription */
36 
37  /*
38  * Although srsublsn is a fixed-width type, it is allowed to be NULL, so
39  * we prevent direct C code access to it just as for a varlena field.
40  */
41 #ifdef CATALOG_VARLEN /* variable-length fields start here */
42 
43  XLogRecPtr srsublsn BKI_FORCE_NULL; /* remote LSN of the state change
44  * used for synchronization
45  * coordination, or NULL if not
46  * valid */
47 #endif
#define BKI_LOOKUP(catalog)
Definition: genbki.h:46
#define BKI_FORCE_NULL
Definition: genbki.h:32
FormData_pg_subscription_rel
unsigned int Oid
Definition: postgres_ext.h:31
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References BKI_FORCE_NULL, and BKI_LOOKUP.

◆ DECLARE_UNIQUE_INDEX_PKEY()

DECLARE_UNIQUE_INDEX_PKEY ( pg_subscription_rel_srrelid_srsubid_index  ,
6117  ,
SubscriptionRelSrrelidSrsubidIndexId  ,
pg_subscription_rel  ,
btree(srrelid oid_ops, srsubid oid_ops)   
)

◆ GetSubscriptionRelations()

List* GetSubscriptionRelations ( Oid  subid,
bool  not_ready 
)

Definition at line 501 of file pg_subscription.c.

502 {
503  List *res = NIL;
504  Relation rel;
505  HeapTuple tup;
506  int nkeys = 0;
507  ScanKeyData skey[2];
508  SysScanDesc scan;
509 
510  rel = table_open(SubscriptionRelRelationId, AccessShareLock);
511 
512  ScanKeyInit(&skey[nkeys++],
513  Anum_pg_subscription_rel_srsubid,
514  BTEqualStrategyNumber, F_OIDEQ,
515  ObjectIdGetDatum(subid));
516 
517  if (not_ready)
518  ScanKeyInit(&skey[nkeys++],
519  Anum_pg_subscription_rel_srsubstate,
520  BTEqualStrategyNumber, F_CHARNE,
521  CharGetDatum(SUBREL_STATE_READY));
522 
523  scan = systable_beginscan(rel, InvalidOid, false,
524  NULL, nkeys, skey);
525 
526  while (HeapTupleIsValid(tup = systable_getnext(scan)))
527  {
529  SubscriptionRelState *relstate;
530  Datum d;
531  bool isnull;
532 
533  subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
534 
535  relstate = (SubscriptionRelState *) palloc(sizeof(SubscriptionRelState));
536  relstate->relid = subrel->srrelid;
537  relstate->state = subrel->srsubstate;
538  d = SysCacheGetAttr(SUBSCRIPTIONRELMAP, tup,
539  Anum_pg_subscription_rel_srsublsn, &isnull);
540  if (isnull)
541  relstate->lsn = InvalidXLogRecPtr;
542  else
543  relstate->lsn = DatumGetLSN(d);
544 
545  res = lappend(res, relstate);
546  }
547 
548  /* Cleanup */
549  systable_endscan(scan);
551 
552  return res;
553 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:596
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:503
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:384
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
List * lappend(List *list, void *datum)
Definition: list.c:339
void * palloc(Size size)
Definition: mcxt.c:1304
#define NIL
Definition: pg_list.h:68
static XLogRecPtr DatumGetLSN(Datum X)
Definition: pg_lsn.h:22
FormData_pg_subscription_rel * Form_pg_subscription_rel
#define InvalidOid
Definition: postgres_ext.h:36
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
Definition: pg_list.h:54
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:479

References AccessShareLock, BTEqualStrategyNumber, CharGetDatum(), DatumGetLSN(), GETSTRUCT, HeapTupleIsValid, InvalidOid, InvalidXLogRecPtr, lappend(), SubscriptionRelState::lsn, NIL, ObjectIdGetDatum(), palloc(), SubscriptionRelState::relid, res, ScanKeyInit(), SubscriptionRelState::state, SysCacheGetAttr(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by AlterSubscription_refresh(), DropSubscription(), and FetchTableStates().

◆ GetSubscriptionRelState()

char GetSubscriptionRelState ( Oid  subid,
Oid  relid,
XLogRecPtr sublsn 
)

Definition at line 341 of file pg_subscription.c.

342 {
343  HeapTuple tup;
344  char substate;
345  bool isnull;
346  Datum d;
347  Relation rel;
348 
349  /*
350  * This is to avoid the race condition with AlterSubscription which tries
351  * to remove this relstate.
352  */
353  rel = table_open(SubscriptionRelRelationId, AccessShareLock);
354 
355  /* Try finding the mapping. */
356  tup = SearchSysCache2(SUBSCRIPTIONRELMAP,
357  ObjectIdGetDatum(relid),
358  ObjectIdGetDatum(subid));
359 
360  if (!HeapTupleIsValid(tup))
361  {
363  *sublsn = InvalidXLogRecPtr;
364  return SUBREL_STATE_UNKNOWN;
365  }
366 
367  /* Get the state. */
368  substate = ((Form_pg_subscription_rel) GETSTRUCT(tup))->srsubstate;
369 
370  /* Get the LSN */
371  d = SysCacheGetAttr(SUBSCRIPTIONRELMAP, tup,
372  Anum_pg_subscription_rel_srsublsn, &isnull);
373  if (isnull)
374  *sublsn = InvalidXLogRecPtr;
375  else
376  *sublsn = DatumGetLSN(d);
377 
378  /* Cleanup */
379  ReleaseSysCache(tup);
380 
382 
383  return substate;
384 }
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:266
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:229

References AccessShareLock, DatumGetLSN(), GETSTRUCT, HeapTupleIsValid, InvalidXLogRecPtr, ObjectIdGetDatum(), ReleaseSysCache(), SearchSysCache2(), SysCacheGetAttr(), table_close(), and table_open().

Referenced by AlterSubscription_refresh(), logicalrep_rel_open(), LogicalRepSyncTableStart(), and wait_for_relation_state_change().

◆ HasSubscriptionRelations()

bool HasSubscriptionRelations ( Oid  subid)

Definition at line 466 of file pg_subscription.c.

467 {
468  Relation rel;
469  ScanKeyData skey[1];
470  SysScanDesc scan;
471  bool has_subrels;
472 
473  rel = table_open(SubscriptionRelRelationId, AccessShareLock);
474 
475  ScanKeyInit(&skey[0],
476  Anum_pg_subscription_rel_srsubid,
477  BTEqualStrategyNumber, F_OIDEQ,
478  ObjectIdGetDatum(subid));
479 
480  scan = systable_beginscan(rel, InvalidOid, false,
481  NULL, 1, skey);
482 
483  /* If even a single tuple exists then the subscription has tables. */
484  has_subrels = HeapTupleIsValid(systable_getnext(scan));
485 
486  /* Cleanup */
487  systable_endscan(scan);
489 
490  return has_subrels;
491 }

References AccessShareLock, BTEqualStrategyNumber, HeapTupleIsValid, InvalidOid, ObjectIdGetDatum(), ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by FetchTableStates().

◆ MAKE_SYSCACHE()

MAKE_SYSCACHE ( SUBSCRIPTIONRELMAP  ,
pg_subscription_rel_srrelid_srsubid_index  ,
64   
)

◆ RemoveSubscriptionRel()

void RemoveSubscriptionRel ( Oid  subid,
Oid  relid 
)

Definition at line 391 of file pg_subscription.c.

392 {
393  Relation rel;
394  TableScanDesc scan;
395  ScanKeyData skey[2];
396  HeapTuple tup;
397  int nkeys = 0;
398 
399  rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
400 
401  if (OidIsValid(subid))
402  {
403  ScanKeyInit(&skey[nkeys++],
404  Anum_pg_subscription_rel_srsubid,
406  F_OIDEQ,
407  ObjectIdGetDatum(subid));
408  }
409 
410  if (OidIsValid(relid))
411  {
412  ScanKeyInit(&skey[nkeys++],
413  Anum_pg_subscription_rel_srrelid,
415  F_OIDEQ,
416  ObjectIdGetDatum(relid));
417  }
418 
419  /* Do the search and delete what we found. */
420  scan = table_beginscan_catalog(rel, nkeys, skey);
422  {
424 
425  subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
426 
427  /*
428  * We don't allow to drop the relation mapping when the table
429  * synchronization is in progress unless the caller updates the
430  * corresponding subscription as well. This is to ensure that we don't
431  * leave tablesync slots or origins in the system when the
432  * corresponding table is dropped.
433  */
434  if (!OidIsValid(subid) && subrel->srsubstate != SUBREL_STATE_READY)
435  {
436  ereport(ERROR,
437  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
438  errmsg("could not drop relation mapping for subscription \"%s\"",
439  get_subscription_name(subrel->srsubid, false)),
440  errdetail("Table synchronization for relation \"%s\" is in progress and is in state \"%c\".",
441  get_rel_name(relid), subrel->srsubstate),
442 
443  /*
444  * translator: first %s is a SQL ALTER command and second %s is a
445  * SQL DROP command
446  */
447  errhint("Use %s to enable subscription if not already enabled or use %s to drop the subscription.",
448  "ALTER SUBSCRIPTION ... ENABLE",
449  "DROP SUBSCRIPTION ...")));
450  }
451 
452  CatalogTupleDelete(rel, &tup->t_self);
453  }
454  table_endscan(scan);
455 
457 }
#define OidIsValid(objectId)
Definition: c.h:762
int errdetail(const char *fmt,...)
Definition: elog.c:1205
int errhint(const char *fmt,...)
Definition: elog.c:1319
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ereport(elevel,...)
Definition: elog.h:149
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1082
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:365
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1906
char * get_subscription_name(Oid subid, bool missing_ok)
Definition: lsyscache.c:3651
@ ForwardScanDirection
Definition: sdir.h:28
ItemPointerData t_self
Definition: htup.h:65
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:1009

References BTEqualStrategyNumber, CatalogTupleDelete(), ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, ForwardScanDirection, get_rel_name(), get_subscription_name(), GETSTRUCT, heap_getnext(), HeapTupleIsValid, ObjectIdGetDatum(), OidIsValid, RowExclusiveLock, ScanKeyInit(), HeapTupleData::t_self, table_beginscan_catalog(), table_close(), table_endscan(), and table_open().

Referenced by AlterSubscription_refresh(), DropSubscription(), and heap_drop_with_catalog().

◆ UpdateSubscriptionRelState()

void UpdateSubscriptionRelState ( Oid  subid,
Oid  relid,
char  state,
XLogRecPtr  sublsn 
)

Definition at line 290 of file pg_subscription.c.

292 {
293  Relation rel;
294  HeapTuple tup;
295  bool nulls[Natts_pg_subscription_rel];
296  Datum values[Natts_pg_subscription_rel];
297  bool replaces[Natts_pg_subscription_rel];
298 
299  LockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
300 
301  rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
302 
303  /* Try finding existing mapping. */
304  tup = SearchSysCacheCopy2(SUBSCRIPTIONRELMAP,
305  ObjectIdGetDatum(relid),
306  ObjectIdGetDatum(subid));
307  if (!HeapTupleIsValid(tup))
308  elog(ERROR, "subscription table %u in subscription %u does not exist",
309  relid, subid);
310 
311  /* Update the tuple. */
312  memset(values, 0, sizeof(values));
313  memset(nulls, false, sizeof(nulls));
314  memset(replaces, false, sizeof(replaces));
315 
316  replaces[Anum_pg_subscription_rel_srsubstate - 1] = true;
317  values[Anum_pg_subscription_rel_srsubstate - 1] = CharGetDatum(state);
318 
319  replaces[Anum_pg_subscription_rel_srsublsn - 1] = true;
320  if (sublsn != InvalidXLogRecPtr)
321  values[Anum_pg_subscription_rel_srsublsn - 1] = LSNGetDatum(sublsn);
322  else
323  nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
324 
325  tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
326  replaces);
327 
328  /* Update the catalog. */
329  CatalogTupleUpdate(rel, &tup->t_self, tup);
330 
331  /* Cleanup. */
332  table_close(rel, NoLock);
333 }
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition: heaptuple.c:1209
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:313

References AccessShareLock, CatalogTupleUpdate(), CharGetDatum(), elog, ERROR, heap_modify_tuple(), HeapTupleIsValid, InvalidXLogRecPtr, LockSharedObject(), LSNGetDatum(), NoLock, ObjectIdGetDatum(), RelationGetDescr, RowExclusiveLock, SearchSysCacheCopy2, HeapTupleData::t_self, table_close(), table_open(), and values.

Referenced by LogicalRepSyncTableStart(), process_syncing_tables_for_apply(), and process_syncing_tables_for_sync().

Variable Documentation

◆ FormData_pg_subscription_rel

FormData_pg_subscription_rel

Definition at line 48 of file pg_subscription_rel.h.