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)
 
void AddSubscriptionRelState (Oid subid, Oid relid, char state, XLogRecPtr sublsn)
 
void UpdateSubscriptionRelState (Oid subid, Oid relid, char state, XLogRecPtr sublsn)
 
char GetSubscriptionRelState (Oid subid, Oid relid, XLogRecPtr *sublsn, bool missing_ok)
 
void RemoveSubscriptionRel (Oid subid, Oid relid)
 
ListGetSubscriptionRelations (Oid subid)
 
ListGetSubscriptionNotReadyRelations (Oid subid)
 

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 
)

Definition at line 236 of file pg_subscription.c.

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

Referenced by AlterSubscription_refresh(), and CreateSubscription().

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. */
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  table_close(rel, NoLock);
276 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define RelationGetDescr(relation)
Definition: rel.h:482
#define AccessShareLock
Definition: lockdefs.h:36
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
uintptr_t Datum
Definition: postgres.h:367
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1002
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Definition: regguts.h:298
#define CharGetDatum(X)
Definition: postgres.h:416
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define elog(elevel,...)
Definition: elog.h:214
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:176
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221

◆ CATALOG()

CATALOG ( pg_subscription_rel  ,
6102  ,
SubscriptionRelRelationId   
)

Definition at line 31 of file pg_subscription_rel.h.

References BKI_FORCE_NULL.

32 {
33  Oid srsubid; /* Oid of subscription */
34  Oid srrelid; /* 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
unsigned int Oid
Definition: postgres_ext.h:31
FormData_pg_subscription_rel
#define BKI_FORCE_NULL
Definition: genbki.h:32
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ GetSubscriptionNotReadyRelations()

List* GetSubscriptionNotReadyRelations ( Oid  subid)

Definition at line 486 of file pg_subscription.c.

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

Referenced by process_syncing_tables_for_apply().

487 {
488  List *res = NIL;
489  Relation rel;
490  HeapTuple tup;
491  int nkeys = 0;
492  ScanKeyData skey[2];
493  SysScanDesc scan;
494 
495  rel = table_open(SubscriptionRelRelationId, AccessShareLock);
496 
497  ScanKeyInit(&skey[nkeys++],
498  Anum_pg_subscription_rel_srsubid,
499  BTEqualStrategyNumber, F_OIDEQ,
500  ObjectIdGetDatum(subid));
501 
502  ScanKeyInit(&skey[nkeys++],
503  Anum_pg_subscription_rel_srsubstate,
504  BTEqualStrategyNumber, F_CHARNE,
505  CharGetDatum(SUBREL_STATE_READY));
506 
507  scan = systable_beginscan(rel, InvalidOid, false,
508  NULL, nkeys, skey);
509 
510  while (HeapTupleIsValid(tup = systable_getnext(scan)))
511  {
513  SubscriptionRelState *relstate;
514  Datum d;
515  bool isnull;
516 
517  subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
518 
519  relstate = (SubscriptionRelState *) palloc(sizeof(SubscriptionRelState));
520  relstate->relid = subrel->srrelid;
521  relstate->state = subrel->srsubstate;
523  Anum_pg_subscription_rel_srsublsn, &isnull);
524  if (isnull)
525  relstate->lsn = InvalidXLogRecPtr;
526  else
527  relstate->lsn = DatumGetLSN(d);
528 
529  res = lappend(res, relstate);
530  }
531 
532  /* Cleanup */
533  systable_endscan(scan);
535 
536  return res;
537 }
#define NIL
Definition: pg_list.h:65
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define AccessShareLock
Definition: lockdefs.h:36
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
List * lappend(List *list, void *datum)
Definition: list.c:321
uintptr_t Datum
Definition: postgres.h:367
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
#define InvalidOid
Definition: postgres_ext.h:36
#define DatumGetLSN(X)
Definition: pg_lsn.h:21
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_subscription_rel * Form_pg_subscription_rel
#define CharGetDatum(X)
Definition: postgres.h:416
void * palloc(Size size)
Definition: mcxt.c:949
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ GetSubscriptionRelations()

List* GetSubscriptionRelations ( Oid  subid)

Definition at line 432 of file pg_subscription.c.

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

Referenced by AlterSubscription_refresh().

433 {
434  List *res = NIL;
435  Relation rel;
436  HeapTuple tup;
437  int nkeys = 0;
438  ScanKeyData skey[2];
439  SysScanDesc scan;
440 
441  rel = table_open(SubscriptionRelRelationId, AccessShareLock);
442 
443  ScanKeyInit(&skey[nkeys++],
444  Anum_pg_subscription_rel_srsubid,
445  BTEqualStrategyNumber, F_OIDEQ,
446  ObjectIdGetDatum(subid));
447 
448  scan = systable_beginscan(rel, InvalidOid, false,
449  NULL, nkeys, skey);
450 
451  while (HeapTupleIsValid(tup = systable_getnext(scan)))
452  {
454  SubscriptionRelState *relstate;
455  Datum d;
456  bool isnull;
457 
458  subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
459 
460  relstate = (SubscriptionRelState *) palloc(sizeof(SubscriptionRelState));
461  relstate->relid = subrel->srrelid;
462  relstate->state = subrel->srsubstate;
464  Anum_pg_subscription_rel_srsublsn, &isnull);
465  if (isnull)
466  relstate->lsn = InvalidXLogRecPtr;
467  else
468  relstate->lsn = DatumGetLSN(d);
469 
470  res = lappend(res, relstate);
471  }
472 
473  /* Cleanup */
474  systable_endscan(scan);
476 
477  return res;
478 }
#define NIL
Definition: pg_list.h:65
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define AccessShareLock
Definition: lockdefs.h:36
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
List * lappend(List *list, void *datum)
Definition: list.c:321
uintptr_t Datum
Definition: postgres.h:367
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
#define InvalidOid
Definition: postgres_ext.h:36
#define DatumGetLSN(X)
Definition: pg_lsn.h:21
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_subscription_rel * Form_pg_subscription_rel
void * palloc(Size size)
Definition: mcxt.c:949
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ GetSubscriptionRelState()

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

Definition at line 333 of file pg_subscription.c.

References AccessShareLock, Assert, DatumGetChar, DatumGetLSN, elog, ERROR, HeapTupleIsValid, InvalidXLogRecPtr, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache2(), SUBSCRIPTIONRELMAP, SysCacheGetAttr(), table_close(), and table_open().

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

335 {
336  Relation rel;
337  HeapTuple tup;
338  char substate;
339  bool isnull;
340  Datum d;
341 
342  rel = table_open(SubscriptionRelRelationId, AccessShareLock);
343 
344  /* Try finding the mapping. */
346  ObjectIdGetDatum(relid),
347  ObjectIdGetDatum(subid));
348 
349  if (!HeapTupleIsValid(tup))
350  {
351  if (missing_ok)
352  {
354  *sublsn = InvalidXLogRecPtr;
355  return SUBREL_STATE_UNKNOWN;
356  }
357 
358  elog(ERROR, "subscription table %u in subscription %u does not exist",
359  relid, subid);
360  }
361 
362  /* Get the state. */
364  Anum_pg_subscription_rel_srsubstate, &isnull);
365  Assert(!isnull);
366  substate = DatumGetChar(d);
368  Anum_pg_subscription_rel_srsublsn, &isnull);
369  if (isnull)
370  *sublsn = InvalidXLogRecPtr;
371  else
372  *sublsn = DatumGetLSN(d);
373 
374  /* Cleanup */
375  ReleaseSysCache(tup);
377 
378  return substate;
379 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define AccessShareLock
Definition: lockdefs.h:36
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define DatumGetChar(X)
Definition: postgres.h:409
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
#define DatumGetLSN(X)
Definition: pg_lsn.h:21
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:745
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1127
#define elog(elevel,...)
Definition: elog.h:214
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ RemoveSubscriptionRel()

void RemoveSubscriptionRel ( Oid  subid,
Oid  relid 
)

Definition at line 386 of file pg_subscription.c.

References BTEqualStrategyNumber, CatalogTupleDelete(), ForwardScanDirection, 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().

387 {
388  Relation rel;
389  TableScanDesc scan;
390  ScanKeyData skey[2];
391  HeapTuple tup;
392  int nkeys = 0;
393 
394  rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
395 
396  if (OidIsValid(subid))
397  {
398  ScanKeyInit(&skey[nkeys++],
399  Anum_pg_subscription_rel_srsubid,
401  F_OIDEQ,
402  ObjectIdGetDatum(subid));
403  }
404 
405  if (OidIsValid(relid))
406  {
407  ScanKeyInit(&skey[nkeys++],
408  Anum_pg_subscription_rel_srrelid,
410  F_OIDEQ,
411  ObjectIdGetDatum(relid));
412  }
413 
414  /* Do the search and delete what we found. */
415  scan = table_beginscan_catalog(rel, nkeys, skey);
417  {
418  CatalogTupleDelete(rel, &tup->t_self);
419  }
420  table_endscan(scan);
421 
423 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
#define OidIsValid(objectId)
Definition: c.h:651
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
ItemPointerData t_self
Definition: htup.h:65
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1286
#define RowExclusiveLock
Definition: lockdefs.h:38
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:863
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ UpdateSubscriptionRelState()

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

Definition at line 282 of file pg_subscription.c.

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

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

284 {
285  Relation rel;
286  HeapTuple tup;
287  bool nulls[Natts_pg_subscription_rel];
288  Datum values[Natts_pg_subscription_rel];
289  bool replaces[Natts_pg_subscription_rel];
290 
291  LockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
292 
293  rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
294 
295  /* Try finding existing mapping. */
297  ObjectIdGetDatum(relid),
298  ObjectIdGetDatum(subid));
299  if (!HeapTupleIsValid(tup))
300  elog(ERROR, "subscription table %u in subscription %u does not exist",
301  relid, subid);
302 
303  /* Update the tuple. */
304  memset(values, 0, sizeof(values));
305  memset(nulls, false, sizeof(nulls));
306  memset(replaces, false, sizeof(replaces));
307 
308  replaces[Anum_pg_subscription_rel_srsubstate - 1] = true;
309  values[Anum_pg_subscription_rel_srsubstate - 1] = CharGetDatum(state);
310 
311  replaces[Anum_pg_subscription_rel_srsublsn - 1] = true;
312  if (sublsn != InvalidXLogRecPtr)
313  values[Anum_pg_subscription_rel_srsublsn - 1] = LSNGetDatum(sublsn);
314  else
315  nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
316 
317  tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
318  replaces);
319 
320  /* Update the catalog. */
321  CatalogTupleUpdate(rel, &tup->t_self, tup);
322 
323  /* Cleanup. */
324  table_close(rel, NoLock);
325 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define RelationGetDescr(relation)
Definition: rel.h:482
#define AccessShareLock
Definition: lockdefs.h:36
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
uintptr_t Datum
Definition: postgres.h:367
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1002
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Definition: regguts.h:298
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
#define CharGetDatum(X)
Definition: postgres.h:416
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define elog(elevel,...)
Definition: elog.h:214
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:176
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113

Variable Documentation

◆ FormData_pg_subscription_rel

FormData_pg_subscription_rel

Definition at line 48 of file pg_subscription_rel.h.