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 "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)
 
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 114 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().

115 {
116  int nsubs = 0;
117  Relation rel;
118  ScanKeyData scankey;
119  SysScanDesc scan;
120  HeapTuple tup;
121 
123 
124  ScanKeyInit(&scankey,
126  BTEqualStrategyNumber, F_OIDEQ,
127  ObjectIdGetDatum(dbid));
128 
129  scan = systable_beginscan(rel, InvalidOid, false,
130  NULL, 1, &scankey);
131 
132  while (HeapTupleIsValid(tup = systable_getnext(scan)))
133  nsubs++;
134 
135  systable_endscan(scan);
136 
137  heap_close(rel, NoLock);
138 
139  return nsubs;
140 }
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 146 of file pg_subscription.c.

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

Referenced by reread_subscription().

147 {
148  pfree(sub->name);
149  pfree(sub->conninfo);
150  pfree(sub->slotname);
152  pfree(sub);
153 }
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 179 of file pg_subscription.c.

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

Referenced by getObjectDescription(), and getObjectIdentityParts().

180 {
181  HeapTuple tup;
182  char *subname;
183  Form_pg_subscription subform;
184 
186 
187  if (!HeapTupleIsValid(tup))
188  elog(ERROR, "cache lookup failed for subscription %u", subid);
189 
190  subform = (Form_pg_subscription) GETSTRUCT(tup);
191  subname = pstrdup(NameStr(subform->subname));
192 
193  ReleaseSysCache(tup);
194 
195  return subname;
196 }
#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:152
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
#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 162 of file pg_subscription.c.

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

Referenced by get_object_address_unqualified().

163 {
164  Oid oid;
165 
167  CStringGetDatum(subname));
168  if (!OidIsValid(oid) && !missing_ok)
169  ereport(ERROR,
170  (errcode(ERRCODE_UNDEFINED_OBJECT),
171  errmsg("subscription \"%s\" does not exist", subname)));
172  return oid;
173 }
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:181
#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:76
int errmsg(const char *fmt,...)
Definition: elog.c:797
Subscription* GetSubscription ( Oid  subid,
bool  missing_ok 
)

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

46 {
47  HeapTuple tup;
48  Subscription *sub;
49  Form_pg_subscription subform;
50  Datum datum;
51  bool isnull;
52 
54 
55  if (!HeapTupleIsValid(tup))
56  {
57  if (missing_ok)
58  return NULL;
59 
60  elog(ERROR, "cache lookup failed for subscription %u", subid);
61  }
62 
63  subform = (Form_pg_subscription) GETSTRUCT(tup);
64 
65  sub = (Subscription *) palloc(sizeof(Subscription));
66  sub->oid = subid;
67  sub->dbid = subform->subdbid;
68  sub->name = pstrdup(NameStr(subform->subname));
69  sub->owner = subform->subowner;
70  sub->enabled = subform->subenabled;
71 
72  /* Get conninfo */
74  tup,
76  &isnull);
77  Assert(!isnull);
78  sub->conninfo = TextDatumGetCString(datum);
79 
80  /* Get slotname */
82  tup,
84  &isnull);
85  Assert(!isnull);
86  sub->slotname = pstrdup(NameStr(*DatumGetName(datum)));
87 
88  /* Get synccommit */
90  tup,
92  &isnull);
93  Assert(!isnull);
94  sub->synccommit = TextDatumGetCString(datum);
95 
96  /* Get publications */
98  tup,
100  &isnull);
101  Assert(!isnull);
103 
104  ReleaseSysCache(tup);
105 
106  return sub;
107 }
#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:152
#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:1116
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1278
#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 456 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().

457 {
458  List *res = NIL;
459  Relation rel;
460  HeapTuple tup;
461  int nkeys = 0;
462  ScanKeyData skey[2];
463  SysScanDesc scan;
464 
466 
467  ScanKeyInit(&skey[nkeys++],
469  BTEqualStrategyNumber, F_OIDEQ,
470  ObjectIdGetDatum(subid));
471 
472  ScanKeyInit(&skey[nkeys++],
474  BTEqualStrategyNumber, F_CHARNE,
476 
477  scan = systable_beginscan(rel, InvalidOid, false,
478  NULL, nkeys, skey);
479 
480  while (HeapTupleIsValid(tup = systable_getnext(scan)))
481  {
483  SubscriptionRelState *relstate;
484 
485  subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
486 
487  relstate = (SubscriptionRelState *)palloc(sizeof(SubscriptionRelState));
488  relstate->relid = subrel->srrelid;
489  relstate->state = subrel->srsubstate;
490  relstate->lsn = subrel->srsublsn;
491 
492  res = lappend(res, relstate);
493  }
494 
495  /* Cleanup */
496  systable_endscan(scan);
498 
499  return res;
500 }
#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 409 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().

410 {
411  List *res = NIL;
412  Relation rel;
413  HeapTuple tup;
414  int nkeys = 0;
415  ScanKeyData skey[2];
416  SysScanDesc scan;
417 
419 
420  ScanKeyInit(&skey[nkeys++],
422  BTEqualStrategyNumber, F_OIDEQ,
423  ObjectIdGetDatum(subid));
424 
425  scan = systable_beginscan(rel, InvalidOid, false,
426  NULL, nkeys, skey);
427 
428  while (HeapTupleIsValid(tup = systable_getnext(scan)))
429  {
431  SubscriptionRelState *relstate;
432 
433  subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
434 
435  relstate = (SubscriptionRelState *)palloc(sizeof(SubscriptionRelState));
436  relstate->relid = subrel->srrelid;
437  relstate->state = subrel->srsubstate;
438  relstate->lsn = subrel->srsublsn;
439 
440  res = lappend(res, relstate);
441  }
442 
443  /* Cleanup */
444  systable_endscan(scan);
446 
447  return res;
448 }
#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 310 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(), and LogicalRepSyncTableStart().

312 {
313  Relation rel;
314  HeapTuple tup;
315  char substate;
316  bool isnull;
317  Datum d;
318 
320 
321  /* Try finding the mapping. */
323  ObjectIdGetDatum(relid),
324  ObjectIdGetDatum(subid));
325 
326  if (!HeapTupleIsValid(tup))
327  {
328  if (missing_ok)
329  {
331  *sublsn = InvalidXLogRecPtr;
332  return SUBREL_STATE_UNKNOWN;
333  }
334 
335  elog(ERROR, "subscription table %u in subscription %u does not exist",
336  relid, subid);
337  }
338 
339  /* Get the state. */
342  Assert(!isnull);
343  substate = DatumGetChar(d);
346  if (isnull)
347  *sublsn = InvalidXLogRecPtr;
348  else
349  *sublsn = DatumGetLSN(d);
350 
351  /* Cleanup */
352  ReleaseSysCache(tup);
354 
355  return substate;
356 }
#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:1116
#define DatumGetChar(X)
Definition: postgres.h:415
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1278
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:154
void RemoveSubscriptionRel ( Oid  subid,
Oid  relid 
)

Definition at line 363 of file pg_subscription.c.

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

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

364 {
365  Relation rel;
366  HeapScanDesc scan;
367  ScanKeyData skey[2];
368  HeapTuple tup;
369  int nkeys = 0;
370 
372 
373  if (OidIsValid(subid))
374  {
375  ScanKeyInit(&skey[nkeys++],
378  F_OIDEQ,
379  ObjectIdGetDatum(subid));
380  }
381 
382  if (OidIsValid(relid))
383  {
384  ScanKeyInit(&skey[nkeys++],
387  F_OIDEQ,
388  ObjectIdGetDatum(relid));
389  }
390 
391  /* Do the search and delete what we found. */
392  scan = heap_beginscan_catalog(rel, nkeys, skey);
394  {
395  simple_heap_delete(rel, &tup->t_self);
396  }
397  heap_endscan(scan);
398 
400 }
#define Anum_pg_subscription_rel_srsubid
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1578
#define SubscriptionRelRelationId
#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 simple_heap_delete(Relation relation, ItemPointer tid)
Definition: heapam.c:3398
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 
)

Definition at line 231 of file pg_subscription.c.

References 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, InvalidXLogRecPtr, LSNGetDatum, Natts_pg_subscription_rel, NoLock, ObjectIdGetDatum, RelationGetDescr, RowExclusiveLock, SearchSysCacheCopy2, SUBSCRIPTIONRELMAP, SubscriptionRelRelationId, HeapTupleData::t_self, and values.

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

233 {
234  Relation rel;
235  HeapTuple tup;
236  Oid subrelid;
237  bool nulls[Natts_pg_subscription_rel];
239 
241 
242  /* Try finding existing mapping. */
244  ObjectIdGetDatum(relid),
245  ObjectIdGetDatum(subid));
246 
247  /*
248  * If the record for given table does not exist yet create new
249  * record, otherwise update the existing one.
250  */
251  if (!HeapTupleIsValid(tup))
252  {
253  /* Form the tuple. */
254  memset(values, 0, sizeof(values));
255  memset(nulls, false, sizeof(nulls));
259  if (sublsn != InvalidXLogRecPtr)
260  values[Anum_pg_subscription_rel_srsublsn - 1] = LSNGetDatum(sublsn);
261  else
262  nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
263 
264  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
265 
266  /* Insert tuple into catalog. */
267  subrelid = CatalogTupleInsert(rel, tup);
268 
269  heap_freetuple(tup);
270  }
271  else
272  {
273  bool replaces[Natts_pg_subscription_rel];
274 
275  /* Update the tuple. */
276  memset(values, 0, sizeof(values));
277  memset(nulls, false, sizeof(nulls));
278  memset(replaces, false, sizeof(replaces));
279 
280  replaces[Anum_pg_subscription_rel_srsubstate - 1] = true;
282 
283  replaces[Anum_pg_subscription_rel_srsublsn - 1] = true;
284  if (sublsn != InvalidXLogRecPtr)
285  values[Anum_pg_subscription_rel_srsublsn - 1] = LSNGetDatum(sublsn);
286  else
287  nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
288 
289  tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
290  replaces);
291 
292  /* Update the catalog. */
293  CatalogTupleUpdate(rel, &tup->t_self, tup);
294 
295  subrelid = HeapTupleGetOid(tup);
296  }
297 
298  /* Cleanup. */
299  heap_close(rel, NoLock);
300 
301  return subrelid;
302 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define Anum_pg_subscription_rel_srsubid
#define RelationGetDescr(relation)
Definition: rel.h:429
#define SubscriptionRelRelationId
#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:1374
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 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
#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:163
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:793
#define Anum_pg_subscription_rel_srrelid
static List * textarray_to_stringlist ( ArrayType textarray)
static

Definition at line 204 of file pg_subscription.c.

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

Referenced by GetSubscription().

205 {
206  Datum *elems;
207  int nelems, i;
208  List *res = NIL;
209 
210  deconstruct_array(textarray,
211  TEXTOID, -1, false, 'i',
212  &elems, NULL, &nelems);
213 
214  if (nelems == 0)
215  return NIL;
216 
217  for (i = 0; i < nelems; i++)
218  res = lappend(res, makeString(TextDatumGetCString(elems[i])));
219 
220  return res;
221 }
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