PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_subscription.c File Reference
#include "postgres.h"
#include "miscadmin.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/indexing.h"
#include "catalog/pg_type.h"
#include "catalog/pg_subscription.h"
#include "catalog/pg_subscription_rel.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)
 
Oid SetSubscriptionRelState (Oid subid, Oid relid, char state, XLogRecPtr sublsn, bool update_only)
 
char GetSubscriptionRelState (Oid subid, Oid relid, XLogRecPtr *sublsn, bool missing_ok)
 
void RemoveSubscriptionRel (Oid subid, Oid relid)
 
ListGetSubscriptionRelations (Oid subid)
 
ListGetSubscriptionNotReadyRelations (Oid subid)
 

Function Documentation

int CountDBSubscriptions ( Oid  dbid)

Definition at line 118 of file pg_subscription.c.

References Anum_pg_subscription_subdbid, BTEqualStrategyNumber, heap_close, heap_open(), HeapTupleIsValid, InvalidOid, NoLock, NULL, ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), SubscriptionRelationId, systable_beginscan(), systable_endscan(), and systable_getnext().

Referenced by dropdb().

119 {
120  int nsubs = 0;
121  Relation rel;
122  ScanKeyData scankey;
123  SysScanDesc scan;
124  HeapTuple tup;
125 
127 
128  ScanKeyInit(&scankey,
130  BTEqualStrategyNumber, F_OIDEQ,
131  ObjectIdGetDatum(dbid));
132 
133  scan = systable_beginscan(rel, InvalidOid, false,
134  NULL, 1, &scankey);
135 
136  while (HeapTupleIsValid(tup = systable_getnext(scan)))
137  nsubs++;
138 
139  systable_endscan(scan);
140 
141  heap_close(rel, NoLock);
142 
143  return nsubs;
144 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define heap_close(r, l)
Definition: heapam.h:97
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define SubscriptionRelationId
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
#define Anum_pg_subscription_subdbid
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
void FreeSubscription ( Subscription sub)

Definition at line 150 of file pg_subscription.c.

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

Referenced by maybe_reread_subscription().

151 {
152  pfree(sub->name);
153  pfree(sub->conninfo);
154  if (sub->slotname)
155  pfree(sub->slotname);
157  pfree(sub);
158 }
void list_free_deep(List *list)
Definition: list.c:1147
void pfree(void *pointer)
Definition: mcxt.c:950
List * publications
char* get_subscription_name ( Oid  subid)

Definition at line 184 of file pg_subscription.c.

References elog, ERROR, GETSTRUCT, HeapTupleIsValid, NameStr, ObjectIdGetDatum, pstrdup(), ReleaseSysCache(), SearchSysCache1, 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  elog(ERROR, "cache lookup failed for subscription %u", subid);
194 
195  subform = (Form_pg_subscription) GETSTRUCT(tup);
196  subname = pstrdup(NameStr(subform->subname));
197 
198  ReleaseSysCache(tup);
199 
200  return subname;
201 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
char * pstrdup(const char *in)
Definition: mcxt.c:1077
FormData_pg_subscription * Form_pg_subscription
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
Oid get_subscription_oid ( const char *  subname,
bool  missing_ok 
)

Definition at line 167 of file pg_subscription.c.

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

Referenced by get_object_address_unqualified().

168 {
169  Oid oid;
170 
172  CStringGetDatum(subname));
173  if (!OidIsValid(oid) && !missing_ok)
174  ereport(ERROR,
175  (errcode(ERRCODE_UNDEFINED_OBJECT),
176  errmsg("subscription \"%s\" does not exist", subname)));
177  return oid;
178 }
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define GetSysCacheOid2(cacheId, key1, key2)
Definition: syscache.h:185
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:584
#define ereport(elevel, rest)
Definition: elog.h:122
Oid MyDatabaseId
Definition: globals.c:77
int errmsg(const char *fmt,...)
Definition: elog.c:797
Subscription* GetSubscription ( Oid  subid,
bool  missing_ok 
)

Definition at line 47 of file pg_subscription.c.

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

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

48 {
49  HeapTuple tup;
50  Subscription *sub;
51  Form_pg_subscription subform;
52  Datum datum;
53  bool isnull;
54 
56 
57  if (!HeapTupleIsValid(tup))
58  {
59  if (missing_ok)
60  return NULL;
61 
62  elog(ERROR, "cache lookup failed for subscription %u", subid);
63  }
64 
65  subform = (Form_pg_subscription) GETSTRUCT(tup);
66 
67  sub = (Subscription *) palloc(sizeof(Subscription));
68  sub->oid = subid;
69  sub->dbid = subform->subdbid;
70  sub->name = pstrdup(NameStr(subform->subname));
71  sub->owner = subform->subowner;
72  sub->enabled = subform->subenabled;
73 
74  /* Get conninfo */
76  tup,
78  &isnull);
79  Assert(!isnull);
80  sub->conninfo = TextDatumGetCString(datum);
81 
82  /* Get slotname */
84  tup,
86  &isnull);
87  if (!isnull)
88  sub->slotname = pstrdup(NameStr(*DatumGetName(datum)));
89  else
90  sub->slotname = NULL;
91 
92  /* Get synccommit */
94  tup,
96  &isnull);
97  Assert(!isnull);
98  sub->synccommit = TextDatumGetCString(datum);
99 
100  /* Get publications */
102  tup,
104  &isnull);
105  Assert(!isnull);
107 
108  ReleaseSysCache(tup);
109 
110  return sub;
111 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define Anum_pg_subscription_subpublications
char * pstrdup(const char *in)
Definition: mcxt.c:1077
FormData_pg_subscription * Form_pg_subscription
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
#define DatumGetName(X)
Definition: postgres.h:591
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
static List * textarray_to_stringlist(ArrayType *textarray)
List * publications
#define Anum_pg_subscription_subslotname
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1279
#define Anum_pg_subscription_subconninfo
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void * palloc(Size size)
Definition: mcxt.c:849
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
#define Anum_pg_subscription_subsynccommit
#define DatumGetArrayTypeP(X)
Definition: array.h:242
List* GetSubscriptionNotReadyRelations ( Oid  subid)

Definition at line 469 of file pg_subscription.c.

References AccessShareLock, Anum_pg_subscription_rel_srsubid, Anum_pg_subscription_rel_srsubstate, BTEqualStrategyNumber, CharGetDatum, GETSTRUCT, heap_close, heap_open(), HeapTupleIsValid, InvalidOid, lappend(), SubscriptionRelState::lsn, NIL, NULL, ObjectIdGetDatum, palloc(), SubscriptionRelState::relid, ScanKeyInit(), SubscriptionRelState::state, SUBREL_STATE_READY, SubscriptionRelRelationId, systable_beginscan(), systable_endscan(), and systable_getnext().

Referenced by process_syncing_tables_for_apply().

470 {
471  List *res = NIL;
472  Relation rel;
473  HeapTuple tup;
474  int nkeys = 0;
475  ScanKeyData skey[2];
476  SysScanDesc scan;
477 
479 
480  ScanKeyInit(&skey[nkeys++],
482  BTEqualStrategyNumber, F_OIDEQ,
483  ObjectIdGetDatum(subid));
484 
485  ScanKeyInit(&skey[nkeys++],
487  BTEqualStrategyNumber, F_CHARNE,
489 
490  scan = systable_beginscan(rel, InvalidOid, false,
491  NULL, nkeys, skey);
492 
493  while (HeapTupleIsValid(tup = systable_getnext(scan)))
494  {
496  SubscriptionRelState *relstate;
497 
498  subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
499 
500  relstate = (SubscriptionRelState *) palloc(sizeof(SubscriptionRelState));
501  relstate->relid = subrel->srrelid;
502  relstate->state = subrel->srsubstate;
503  relstate->lsn = subrel->srsublsn;
504 
505  res = lappend(res, relstate);
506  }
507 
508  /* Cleanup */
509  systable_endscan(scan);
511 
512  return res;
513 }
#define NIL
Definition: pg_list.h:69
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define Anum_pg_subscription_rel_srsubid
#define SubscriptionRelRelationId
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
List * lappend(List *list, void *datum)
Definition: list.c:128
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
FormData_pg_subscription_rel * Form_pg_subscription_rel
#define CharGetDatum(X)
Definition: postgres.h:422
void * palloc(Size size)
Definition: mcxt.c:849
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define Anum_pg_subscription_rel_srsubstate
Definition: pg_list.h:45
#define SUBREL_STATE_READY
#define BTEqualStrategyNumber
Definition: stratnum.h:31
List* GetSubscriptionRelations ( Oid  subid)

Definition at line 422 of file pg_subscription.c.

References AccessShareLock, Anum_pg_subscription_rel_srsubid, BTEqualStrategyNumber, GETSTRUCT, heap_close, heap_open(), HeapTupleIsValid, InvalidOid, lappend(), SubscriptionRelState::lsn, NIL, NULL, ObjectIdGetDatum, palloc(), SubscriptionRelState::relid, ScanKeyInit(), SubscriptionRelState::state, SubscriptionRelRelationId, systable_beginscan(), systable_endscan(), and systable_getnext().

Referenced by AlterSubscription_refresh().

423 {
424  List *res = NIL;
425  Relation rel;
426  HeapTuple tup;
427  int nkeys = 0;
428  ScanKeyData skey[2];
429  SysScanDesc scan;
430 
432 
433  ScanKeyInit(&skey[nkeys++],
435  BTEqualStrategyNumber, F_OIDEQ,
436  ObjectIdGetDatum(subid));
437 
438  scan = systable_beginscan(rel, InvalidOid, false,
439  NULL, nkeys, skey);
440 
441  while (HeapTupleIsValid(tup = systable_getnext(scan)))
442  {
444  SubscriptionRelState *relstate;
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;
451  relstate->lsn = subrel->srsublsn;
452 
453  res = lappend(res, relstate);
454  }
455 
456  /* Cleanup */
457  systable_endscan(scan);
459 
460  return res;
461 }
#define NIL
Definition: pg_list.h:69
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define Anum_pg_subscription_rel_srsubid
#define SubscriptionRelRelationId
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
List * lappend(List *list, void *datum)
Definition: list.c:128
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
FormData_pg_subscription_rel * Form_pg_subscription_rel
void * palloc(Size size)
Definition: mcxt.c:849
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Definition: pg_list.h:45
#define BTEqualStrategyNumber
Definition: stratnum.h:31
char GetSubscriptionRelState ( Oid  subid,
Oid  relid,
XLogRecPtr sublsn,
bool  missing_ok 
)

Definition at line 323 of file pg_subscription.c.

References AccessShareLock, Anum_pg_subscription_rel_srsublsn, Anum_pg_subscription_rel_srsubstate, Assert, DatumGetChar, DatumGetLSN, elog, ERROR, heap_close, heap_open(), HeapTupleIsValid, InvalidXLogRecPtr, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache2, SUBREL_STATE_UNKNOWN, SUBSCRIPTIONRELMAP, SubscriptionRelRelationId, and SysCacheGetAttr().

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

325 {
326  Relation rel;
327  HeapTuple tup;
328  char substate;
329  bool isnull;
330  Datum d;
331 
333 
334  /* Try finding the mapping. */
336  ObjectIdGetDatum(relid),
337  ObjectIdGetDatum(subid));
338 
339  if (!HeapTupleIsValid(tup))
340  {
341  if (missing_ok)
342  {
344  *sublsn = InvalidXLogRecPtr;
345  return SUBREL_STATE_UNKNOWN;
346  }
347 
348  elog(ERROR, "subscription table %u in subscription %u does not exist",
349  relid, subid);
350  }
351 
352  /* Get the state. */
355  Assert(!isnull);
356  substate = DatumGetChar(d);
359  if (isnull)
360  *sublsn = InvalidXLogRecPtr;
361  else
362  *sublsn = DatumGetLSN(d);
363 
364  /* Cleanup */
365  ReleaseSysCache(tup);
367 
368  return substate;
369 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define SubscriptionRelRelationId
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define SUBREL_STATE_UNKNOWN
#define Anum_pg_subscription_rel_srsublsn
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define DatumGetChar(X)
Definition: postgres.h:415
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1279
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define DatumGetLSN(X)
Definition: pg_lsn.h:21
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:675
#define Anum_pg_subscription_rel_srsubstate
#define elog
Definition: elog.h:219
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:158
void RemoveSubscriptionRel ( Oid  subid,
Oid  relid 
)

Definition at line 376 of file pg_subscription.c.

References Anum_pg_subscription_rel_srrelid, Anum_pg_subscription_rel_srsubid, BTEqualStrategyNumber, CatalogTupleDelete(), ForwardScanDirection, heap_beginscan_catalog(), heap_close, heap_endscan(), heap_getnext(), heap_open(), HeapTupleIsValid, ObjectIdGetDatum, OidIsValid, RowExclusiveLock, ScanKeyInit(), SubscriptionRelRelationId, and HeapTupleData::t_self.

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

377 {
378  Relation rel;
379  HeapScanDesc scan;
380  ScanKeyData skey[2];
381  HeapTuple tup;
382  int nkeys = 0;
383 
385 
386  if (OidIsValid(subid))
387  {
388  ScanKeyInit(&skey[nkeys++],
391  F_OIDEQ,
392  ObjectIdGetDatum(subid));
393  }
394 
395  if (OidIsValid(relid))
396  {
397  ScanKeyInit(&skey[nkeys++],
400  F_OIDEQ,
401  ObjectIdGetDatum(relid));
402  }
403 
404  /* Do the search and delete what we found. */
405  scan = heap_beginscan_catalog(rel, nkeys, skey);
407  {
408  CatalogTupleDelete(rel, &tup->t_self);
409  }
410  heap_endscan(scan);
411 
413 }
#define Anum_pg_subscription_rel_srsubid
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1578
#define SubscriptionRelRelationId
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
#define heap_close(r, l)
Definition: heapam.h:97
#define OidIsValid(objectId)
Definition: c.h:538
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
HeapScanDesc heap_beginscan_catalog(Relation relation, int nkeys, ScanKey key)
Definition: heapam.c:1399
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1794
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define Anum_pg_subscription_rel_srrelid
Oid SetSubscriptionRelState ( Oid  subid,
Oid  relid,
char  state,
XLogRecPtr  sublsn,
bool  update_only 
)

Definition at line 242 of file pg_subscription.c.

References AccessShareLock, Anum_pg_subscription_rel_srrelid, Anum_pg_subscription_rel_srsubid, Anum_pg_subscription_rel_srsublsn, Anum_pg_subscription_rel_srsubstate, CatalogTupleInsert(), CatalogTupleUpdate(), CharGetDatum, heap_close, heap_form_tuple(), heap_freetuple(), heap_modify_tuple(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, InvalidOid, InvalidXLogRecPtr, LockSharedObject(), LSNGetDatum, Natts_pg_subscription_rel, NoLock, ObjectIdGetDatum, RelationGetDescr, RowExclusiveLock, SearchSysCacheCopy2, SubscriptionRelationId, SUBSCRIPTIONRELMAP, SubscriptionRelRelationId, HeapTupleData::t_self, and values.

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

244 {
245  Relation rel;
246  HeapTuple tup;
247  Oid subrelid = InvalidOid;
248  bool nulls[Natts_pg_subscription_rel];
250 
252 
254 
255  /* Try finding existing mapping. */
257  ObjectIdGetDatum(relid),
258  ObjectIdGetDatum(subid));
259 
260  /*
261  * If the record for given table does not exist yet create new record,
262  * otherwise update the existing one.
263  */
264  if (!HeapTupleIsValid(tup) && !update_only)
265  {
266  /* Form the tuple. */
267  memset(values, 0, sizeof(values));
268  memset(nulls, false, sizeof(nulls));
272  if (sublsn != InvalidXLogRecPtr)
273  values[Anum_pg_subscription_rel_srsublsn - 1] = LSNGetDatum(sublsn);
274  else
275  nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
276 
277  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
278 
279  /* Insert tuple into catalog. */
280  subrelid = CatalogTupleInsert(rel, tup);
281 
282  heap_freetuple(tup);
283  }
284  else if (HeapTupleIsValid(tup))
285  {
286  bool replaces[Natts_pg_subscription_rel];
287 
288  /* Update the tuple. */
289  memset(values, 0, sizeof(values));
290  memset(nulls, false, sizeof(nulls));
291  memset(replaces, false, sizeof(replaces));
292 
293  replaces[Anum_pg_subscription_rel_srsubstate - 1] = true;
295 
296  replaces[Anum_pg_subscription_rel_srsublsn - 1] = true;
297  if (sublsn != InvalidXLogRecPtr)
298  values[Anum_pg_subscription_rel_srsublsn - 1] = LSNGetDatum(sublsn);
299  else
300  nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
301 
302  tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
303  replaces);
304 
305  /* Update the catalog. */
306  CatalogTupleUpdate(rel, &tup->t_self, tup);
307 
308  subrelid = HeapTupleGetOid(tup);
309  }
310 
311  /* Cleanup. */
312  heap_close(rel, NoLock);
313 
314  return subrelid;
315 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define Anum_pg_subscription_rel_srsubid
#define RelationGetDescr(relation)
Definition: rel.h:428
#define SubscriptionRelRelationId
#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:692
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1372
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:162
ItemPointerData t_self
Definition: htup.h:65
#define SubscriptionRelationId
#define NoLock
Definition: lockdefs.h:34
#define Anum_pg_subscription_rel_srsublsn
#define RowExclusiveLock
Definition: lockdefs.h:38
#define Natts_pg_subscription_rel
uintptr_t Datum
Definition: postgres.h:372
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:871
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
Definition: regguts.h:298
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
#define CharGetDatum(X)
Definition: postgres.h:422
static Datum values[MAXATTR]
Definition: bootstrap.c:163
#define Anum_pg_subscription_rel_srsubstate
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:167
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:791
#define Anum_pg_subscription_rel_srrelid
static List * textarray_to_stringlist ( ArrayType textarray)
static

Definition at line 209 of file pg_subscription.c.

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

Referenced by GetSubscription().

210 {
211  Datum *elems;
212  int nelems,
213  i;
214  List *res = NIL;
215 
216  deconstruct_array(textarray,
217  TEXTOID, -1, false, 'i',
218  &elems, NULL, &nelems);
219 
220  if (nelems == 0)
221  return NIL;
222 
223  for (i = 0; i < nelems; i++)
224  res = lappend(res, makeString(TextDatumGetCString(elems[i])));
225 
226  return res;
227 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
#define TEXTOID
Definition: pg_type.h:324
List * lappend(List *list, void *datum)
Definition: list.c:128
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
#define NULL
Definition: c.h:229
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
int i
Definition: pg_list.h:45