PostgreSQL Source Code  git master
pg_subscription.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/tableam.h"
#include "access/xact.h"
#include "catalog/indexing.h"
#include "catalog/pg_subscription.h"
#include "catalog/pg_subscription_rel.h"
#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "storage/lmgr.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/pg_lsn.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for pg_subscription.c:

Go to the source code of this file.

Functions

static Listtextarray_to_stringlist (ArrayType *textarray)
 
SubscriptionGetSubscription (Oid subid, bool missing_ok)
 
int CountDBSubscriptions (Oid dbid)
 
void FreeSubscription (Subscription *sub)
 
Oid get_subscription_oid (const char *subname, bool missing_ok)
 
char * get_subscription_name (Oid subid, bool missing_ok)
 
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)
 
void RemoveSubscriptionRel (Oid subid, Oid relid)
 
ListGetSubscriptionRelations (Oid subid)
 
ListGetSubscriptionNotReadyRelations (Oid subid)
 

Function Documentation

◆ AddSubscriptionRelState()

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

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

239 {
240  Relation rel;
241  HeapTuple tup;
242  bool nulls[Natts_pg_subscription_rel];
243  Datum values[Natts_pg_subscription_rel];
244 
245  LockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
246 
247  rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
248 
249  /* Try finding existing mapping. */
251  ObjectIdGetDatum(relid),
252  ObjectIdGetDatum(subid));
253  if (HeapTupleIsValid(tup))
254  elog(ERROR, "subscription table %u in subscription %u already exists",
255  relid, subid);
256 
257  /* Form the tuple. */
258  memset(values, 0, sizeof(values));
259  memset(nulls, false, sizeof(nulls));
260  values[Anum_pg_subscription_rel_srsubid - 1] = ObjectIdGetDatum(subid);
261  values[Anum_pg_subscription_rel_srrelid - 1] = ObjectIdGetDatum(relid);
262  values[Anum_pg_subscription_rel_srsubstate - 1] = CharGetDatum(state);
263  if (sublsn != InvalidXLogRecPtr)
264  values[Anum_pg_subscription_rel_srsublsn - 1] = LSNGetDatum(sublsn);
265  else
266  nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
267 
268  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
269 
270  /* Insert tuple into catalog. */
271  CatalogTupleInsert(rel, tup);
272 
273  heap_freetuple(tup);
274 
275  /* Cleanup. */
276  table_close(rel, NoLock);
277 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#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:1017
#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:165
#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

◆ CountDBSubscriptions()

int CountDBSubscriptions ( Oid  dbid)

Definition at line 115 of file pg_subscription.c.

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

Referenced by dropdb().

116 {
117  int nsubs = 0;
118  Relation rel;
119  ScanKeyData scankey;
120  SysScanDesc scan;
121  HeapTuple tup;
122 
123  rel = table_open(SubscriptionRelationId, RowExclusiveLock);
124 
125  ScanKeyInit(&scankey,
126  Anum_pg_subscription_subdbid,
127  BTEqualStrategyNumber, F_OIDEQ,
128  ObjectIdGetDatum(dbid));
129 
130  scan = systable_beginscan(rel, InvalidOid, false,
131  NULL, 1, &scankey);
132 
133  while (HeapTupleIsValid(tup = systable_getnext(scan)))
134  nsubs++;
135 
136  systable_endscan(scan);
137 
138  table_close(rel, NoLock);
139 
140  return nsubs;
141 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
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
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
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

◆ FreeSubscription()

void FreeSubscription ( Subscription sub)

Definition at line 147 of file pg_subscription.c.

References Subscription::conninfo, list_free_deep(), Subscription::name, pfree(), Subscription::publications, and Subscription::slotname.

Referenced by maybe_reread_subscription().

148 {
149  pfree(sub->name);
150  pfree(sub->conninfo);
151  if (sub->slotname)
152  pfree(sub->slotname);
154  pfree(sub);
155 }
void list_free_deep(List *list)
Definition: list.c:1390
void pfree(void *pointer)
Definition: mcxt.c:1057
List * publications

◆ get_subscription_name()

char* get_subscription_name ( Oid  subid,
bool  missing_ok 
)

Definition at line 184 of file pg_subscription.c.

References elog, ERROR, GETSTRUCT, HeapTupleIsValid, NameStr, ObjectIdGetDatum, pstrdup(), ReleaseSysCache(), SearchSysCache1(), subname, and SUBSCRIPTIONOID.

Referenced by getObjectDescription(), and getObjectIdentityParts().

185 {
186  HeapTuple tup;
187  char *subname;
188  Form_pg_subscription subform;
189 
191 
192  if (!HeapTupleIsValid(tup))
193  {
194  if (!missing_ok)
195  elog(ERROR, "cache lookup failed for subscription %u", subid);
196  return NULL;
197  }
198 
199  subform = (Form_pg_subscription) GETSTRUCT(tup);
200  subname = pstrdup(NameStr(subform->subname));
201 
202  ReleaseSysCache(tup);
203 
204  return subname;
205 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char * pstrdup(const char *in)
Definition: mcxt.c:1187
FormData_pg_subscription * Form_pg_subscription
NameData subname
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:214
#define NameStr(name)
Definition: c.h:623

◆ get_subscription_oid()

Oid get_subscription_oid ( const char *  subname,
bool  missing_ok 
)

Definition at line 164 of file pg_subscription.c.

References CStringGetDatum, ereport, errcode(), errmsg(), ERROR, GetSysCacheOid2, MyDatabaseId, OidIsValid, and SUBSCRIPTIONNAME.

Referenced by get_object_address_unqualified().

165 {
166  Oid oid;
167 
168  oid = GetSysCacheOid2(SUBSCRIPTIONNAME, Anum_pg_subscription_oid,
170  if (!OidIsValid(oid) && !missing_ok)
171  ereport(ERROR,
172  (errcode(ERRCODE_UNDEFINED_OBJECT),
173  errmsg("subscription \"%s\" does not exist", subname)));
174  return oid;
175 }
int errcode(int sqlerrcode)
Definition: elog.c:610
unsigned int Oid
Definition: postgres_ext.h:31
NameData subname
#define OidIsValid(objectId)
Definition: c.h:652
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:578
Oid MyDatabaseId
Definition: globals.c:85
#define ereport(elevel,...)
Definition: elog.h:144
#define GetSysCacheOid2(cacheId, oidcol, key1, key2)
Definition: syscache.h:194
int errmsg(const char *fmt,...)
Definition: elog.c:821

◆ GetSubscription()

Subscription* GetSubscription ( Oid  subid,
bool  missing_ok 
)

Definition at line 42 of file pg_subscription.c.

References Assert, Subscription::binary, Subscription::conninfo, DatumGetArrayTypeP, DatumGetName, Subscription::dbid, elog, Subscription::enabled, ERROR, GETSTRUCT, HeapTupleIsValid, Subscription::name, NameStr, ObjectIdGetDatum, Subscription::oid, Subscription::owner, palloc(), pstrdup(), Subscription::publications, ReleaseSysCache(), SearchSysCache1(), Subscription::slotname, Subscription::stream, SUBSCRIPTIONOID, Subscription::synccommit, SysCacheGetAttr(), textarray_to_stringlist(), and TextDatumGetCString.

Referenced by AlterSubscription(), ApplyWorkerMain(), and maybe_reread_subscription().

43 {
44  HeapTuple tup;
45  Subscription *sub;
46  Form_pg_subscription subform;
47  Datum datum;
48  bool isnull;
49 
51 
52  if (!HeapTupleIsValid(tup))
53  {
54  if (missing_ok)
55  return NULL;
56 
57  elog(ERROR, "cache lookup failed for subscription %u", subid);
58  }
59 
60  subform = (Form_pg_subscription) GETSTRUCT(tup);
61 
62  sub = (Subscription *) palloc(sizeof(Subscription));
63  sub->oid = subid;
64  sub->dbid = subform->subdbid;
65  sub->name = pstrdup(NameStr(subform->subname));
66  sub->owner = subform->subowner;
67  sub->enabled = subform->subenabled;
68  sub->binary = subform->subbinary;
69  sub->stream = subform->substream;
70 
71  /* Get conninfo */
73  tup,
74  Anum_pg_subscription_subconninfo,
75  &isnull);
76  Assert(!isnull);
77  sub->conninfo = TextDatumGetCString(datum);
78 
79  /* Get slotname */
81  tup,
82  Anum_pg_subscription_subslotname,
83  &isnull);
84  if (!isnull)
85  sub->slotname = pstrdup(NameStr(*DatumGetName(datum)));
86  else
87  sub->slotname = NULL;
88 
89  /* Get synccommit */
91  tup,
92  Anum_pg_subscription_subsynccommit,
93  &isnull);
94  Assert(!isnull);
95  sub->synccommit = TextDatumGetCString(datum);
96 
97  /* Get publications */
99  tup,
100  Anum_pg_subscription_subpublications,
101  &isnull);
102  Assert(!isnull);
104 
105  ReleaseSysCache(tup);
106 
107  return sub;
108 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char * pstrdup(const char *in)
Definition: mcxt.c:1187
FormData_pg_subscription * Form_pg_subscription
#define DatumGetName(X)
Definition: postgres.h:585
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
static List * textarray_to_stringlist(ArrayType *textarray)
List * publications
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
#define TextDatumGetCString(d)
Definition: builtins.h:87
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:746
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:214
#define NameStr(name)
Definition: c.h:623
#define DatumGetArrayTypeP(X)
Definition: array.h:249

◆ GetSubscriptionNotReadyRelations()

List* GetSubscriptionNotReadyRelations ( Oid  subid)

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

475 {
476  List *res = NIL;
477  Relation rel;
478  HeapTuple tup;
479  int nkeys = 0;
480  ScanKeyData skey[2];
481  SysScanDesc scan;
482 
483  rel = table_open(SubscriptionRelRelationId, AccessShareLock);
484 
485  ScanKeyInit(&skey[nkeys++],
486  Anum_pg_subscription_rel_srsubid,
487  BTEqualStrategyNumber, F_OIDEQ,
488  ObjectIdGetDatum(subid));
489 
490  ScanKeyInit(&skey[nkeys++],
491  Anum_pg_subscription_rel_srsubstate,
492  BTEqualStrategyNumber, F_CHARNE,
493  CharGetDatum(SUBREL_STATE_READY));
494 
495  scan = systable_beginscan(rel, InvalidOid, false,
496  NULL, nkeys, skey);
497 
498  while (HeapTupleIsValid(tup = systable_getnext(scan)))
499  {
501  SubscriptionRelState *relstate;
502  Datum d;
503  bool isnull;
504 
505  subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
506 
507  relstate = (SubscriptionRelState *) palloc(sizeof(SubscriptionRelState));
508  relstate->relid = subrel->srrelid;
509  relstate->state = subrel->srsubstate;
511  Anum_pg_subscription_rel_srsublsn, &isnull);
512  if (isnull)
513  relstate->lsn = InvalidXLogRecPtr;
514  else
515  relstate->lsn = DatumGetLSN(d);
516 
517  res = lappend(res, relstate);
518  }
519 
520  /* Cleanup */
521  systable_endscan(scan);
523 
524  return res;
525 }
#define NIL
Definition: pg_list.h:65
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
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:950
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 420 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().

421 {
422  List *res = NIL;
423  Relation rel;
424  HeapTuple tup;
425  int nkeys = 0;
426  ScanKeyData skey[2];
427  SysScanDesc scan;
428 
429  rel = table_open(SubscriptionRelRelationId, AccessShareLock);
430 
431  ScanKeyInit(&skey[nkeys++],
432  Anum_pg_subscription_rel_srsubid,
433  BTEqualStrategyNumber, F_OIDEQ,
434  ObjectIdGetDatum(subid));
435 
436  scan = systable_beginscan(rel, InvalidOid, false,
437  NULL, nkeys, skey);
438 
439  while (HeapTupleIsValid(tup = systable_getnext(scan)))
440  {
442  SubscriptionRelState *relstate;
443  Datum d;
444  bool isnull;
445 
446  subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
447 
448  relstate = (SubscriptionRelState *) palloc(sizeof(SubscriptionRelState));
449  relstate->relid = subrel->srrelid;
450  relstate->state = subrel->srsubstate;
452  Anum_pg_subscription_rel_srsublsn, &isnull);
453  if (isnull)
454  relstate->lsn = InvalidXLogRecPtr;
455  else
456  relstate->lsn = DatumGetLSN(d);
457 
458  res = lappend(res, relstate);
459  }
460 
461  /* Cleanup */
462  systable_endscan(scan);
464 
465  return res;
466 }
#define NIL
Definition: pg_list.h:65
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
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:950
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 
)

Definition at line 334 of file pg_subscription.c.

References DatumGetLSN, GETSTRUCT, HeapTupleIsValid, InvalidXLogRecPtr, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache2(), SUBSCRIPTIONRELMAP, and SysCacheGetAttr().

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

335 {
336  HeapTuple tup;
337  char substate;
338  bool isnull;
339  Datum d;
340 
341  /* Try finding the mapping. */
343  ObjectIdGetDatum(relid),
344  ObjectIdGetDatum(subid));
345 
346  if (!HeapTupleIsValid(tup))
347  {
348  *sublsn = InvalidXLogRecPtr;
349  return SUBREL_STATE_UNKNOWN;
350  }
351 
352  /* Get the state. */
353  substate = ((Form_pg_subscription_rel) GETSTRUCT(tup))->srsubstate;
354 
355  /* Get the LSN */
357  Anum_pg_subscription_rel_srsublsn, &isnull);
358  if (isnull)
359  *sublsn = InvalidXLogRecPtr;
360  else
361  *sublsn = DatumGetLSN(d);
362 
363  /* Cleanup */
364  ReleaseSysCache(tup);
365 
366  return substate;
367 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
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
FormData_pg_subscription_rel * Form_pg_subscription_rel
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1127

◆ RemoveSubscriptionRel()

void RemoveSubscriptionRel ( Oid  subid,
Oid  relid 
)

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

375 {
376  Relation rel;
377  TableScanDesc scan;
378  ScanKeyData skey[2];
379  HeapTuple tup;
380  int nkeys = 0;
381 
382  rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
383 
384  if (OidIsValid(subid))
385  {
386  ScanKeyInit(&skey[nkeys++],
387  Anum_pg_subscription_rel_srsubid,
389  F_OIDEQ,
390  ObjectIdGetDatum(subid));
391  }
392 
393  if (OidIsValid(relid))
394  {
395  ScanKeyInit(&skey[nkeys++],
396  Anum_pg_subscription_rel_srrelid,
398  F_OIDEQ,
399  ObjectIdGetDatum(relid));
400  }
401 
402  /* Do the search and delete what we found. */
403  scan = table_beginscan_catalog(rel, nkeys, skey);
405  {
406  CatalogTupleDelete(rel, &tup->t_self);
407  }
408  table_endscan(scan);
409 
411 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
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:652
#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

◆ textarray_to_stringlist()

static List * textarray_to_stringlist ( ArrayType textarray)
static

Definition at line 213 of file pg_subscription.c.

References deconstruct_array(), i, lappend(), makeString(), NIL, and TextDatumGetCString.

Referenced by GetSubscription().

214 {
215  Datum *elems;
216  int nelems,
217  i;
218  List *res = NIL;
219 
220  deconstruct_array(textarray,
221  TEXTOID, -1, false, TYPALIGN_INT,
222  &elems, NULL, &nelems);
223 
224  if (nelems == 0)
225  return NIL;
226 
227  for (i = 0; i < nelems; i++)
228  res = lappend(res, makeString(TextDatumGetCString(elems[i])));
229 
230  return res;
231 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
List * lappend(List *list, void *datum)
Definition: list.c:321
#define TextDatumGetCString(d)
Definition: builtins.h:87
uintptr_t Datum
Definition: postgres.h:367
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3483
int i
Definition: pg_list.h:50

◆ UpdateSubscriptionRelState()

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

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

285 {
286  Relation rel;
287  HeapTuple tup;
288  bool nulls[Natts_pg_subscription_rel];
289  Datum values[Natts_pg_subscription_rel];
290  bool replaces[Natts_pg_subscription_rel];
291 
292  LockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
293 
294  rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
295 
296  /* Try finding existing mapping. */
298  ObjectIdGetDatum(relid),
299  ObjectIdGetDatum(subid));
300  if (!HeapTupleIsValid(tup))
301  elog(ERROR, "subscription table %u in subscription %u does not exist",
302  relid, subid);
303 
304  /* Update the tuple. */
305  memset(values, 0, sizeof(values));
306  memset(nulls, false, sizeof(nulls));
307  memset(replaces, false, sizeof(replaces));
308 
309  replaces[Anum_pg_subscription_rel_srsubstate - 1] = true;
310  values[Anum_pg_subscription_rel_srsubstate - 1] = CharGetDatum(state);
311 
312  replaces[Anum_pg_subscription_rel_srsublsn - 1] = true;
313  if (sublsn != InvalidXLogRecPtr)
314  values[Anum_pg_subscription_rel_srsublsn - 1] = LSNGetDatum(sublsn);
315  else
316  nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
317 
318  tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
319  replaces);
320 
321  /* Update the catalog. */
322  CatalogTupleUpdate(rel, &tup->t_self, tup);
323 
324  /* Cleanup. */
325  table_close(rel, NoLock);
326 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#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:1017
#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:165
#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