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 "catalog/pg_type.h"
#include "catalog/pg_subscription.h"
#include "nodes/makefuncs.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/fmgroids.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)
 

Function Documentation

int CountDBSubscriptions ( Oid  dbid)

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

102 {
103  int nsubs = 0;
104  Relation rel;
105  ScanKeyData scankey;
106  SysScanDesc scan;
107  HeapTuple tup;
108 
110 
111  ScanKeyInit(&scankey,
113  BTEqualStrategyNumber, F_OIDEQ,
114  ObjectIdGetDatum(dbid));
115 
116  scan = systable_beginscan(rel, InvalidOid, false,
117  NULL, 1, &scankey);
118 
119  while (HeapTupleIsValid(tup = systable_getnext(scan)))
120  nsubs++;
121 
122  systable_endscan(scan);
123 
124  heap_close(rel, NoLock);
125 
126  return nsubs;
127 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:495
#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:324
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:412
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#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:1287
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
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 133 of file pg_subscription.c.

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

Referenced by reread_subscription().

134 {
135  pfree(sub->name);
136  pfree(sub->conninfo);
137  pfree(sub->slotname);
139  pfree(sub);
140 }
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 166 of file pg_subscription.c.

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

Referenced by getObjectDescription(), and getObjectIdentityParts().

167 {
168  HeapTuple tup;
169  char *subname;
170  Form_pg_subscription subform;
171 
173 
174  if (!HeapTupleIsValid(tup))
175  elog(ERROR, "cache lookup failed for subscription %u", subid);
176 
177  subform = (Form_pg_subscription) GETSTRUCT(tup);
178  subname = pstrdup(NameStr(subform->subname));
179 
180  ReleaseSysCache(tup);
181 
182  return subname;
183 }
#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:149
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NameStr(name)
Definition: c.h:495
#define elog
Definition: elog.h:219
Oid get_subscription_oid ( const char *  subname,
bool  missing_ok 
)

Definition at line 149 of file pg_subscription.c.

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

Referenced by get_object_address_unqualified().

150 {
151  Oid oid;
152 
154  CStringGetDatum(subname));
155  if (!OidIsValid(oid) && !missing_ok)
156  ereport(ERROR,
157  (errcode(ERRCODE_UNDEFINED_OBJECT),
158  errmsg("subscription \"%s\" does not exist", subname)));
159  return oid;
160 }
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
#define GetSysCacheOid2(cacheId, key1, key2)
Definition: syscache.h:178
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:586
#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 40 of file pg_subscription.c.

References Anum_pg_subscription_subconninfo, Anum_pg_subscription_subpublications, Anum_pg_subscription_subslotname, 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, SysCacheGetAttr(), textarray_to_stringlist(), and TextDatumGetCString.

Referenced by ApplyWorkerMain(), and reread_subscription().

41 {
42  HeapTuple tup;
43  Subscription *sub;
44  Form_pg_subscription subform;
45  Datum datum;
46  bool isnull;
47 
49 
50  if (!HeapTupleIsValid(tup))
51  {
52  if (missing_ok)
53  return NULL;
54 
55  elog(ERROR, "cache lookup failed for subscription %u", subid);
56  }
57 
58  subform = (Form_pg_subscription) GETSTRUCT(tup);
59 
60  sub = (Subscription *) palloc(sizeof(Subscription));
61  sub->oid = subid;
62  sub->dbid = subform->subdbid;
63  sub->name = pstrdup(NameStr(subform->subname));
64  sub->owner = subform->subowner;
65  sub->enabled = subform->subenabled;
66 
67  /* Get conninfo */
69  tup,
71  &isnull);
72  Assert(!isnull);
73  sub->conninfo = pstrdup(TextDatumGetCString(datum));
74 
75  /* Get slotname */
77  tup,
79  &isnull);
80  Assert(!isnull);
81  sub->slotname = pstrdup(NameStr(*DatumGetName(datum)));
82 
83  /* Get publications */
85  tup,
87  &isnull);
88  Assert(!isnull);
90 
91  ReleaseSysCache(tup);
92 
93  return sub;
94 }
#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:149
#define DatumGetName(X)
Definition: postgres.h:593
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#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:374
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1245
#define Anum_pg_subscription_subconninfo
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void * palloc(Size size)
Definition: mcxt.c:849
#define NameStr(name)
Definition: c.h:495
#define elog
Definition: elog.h:219
#define DatumGetArrayTypeP(X)
Definition: array.h:242
static List * textarray_to_stringlist ( ArrayType textarray)
static

Definition at line 191 of file pg_subscription.c.

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

Referenced by GetSubscription().

192 {
193  Datum *elems;
194  int nelems, i;
195  List *res = NIL;
196 
197  deconstruct_array(textarray,
198  TEXTOID, -1, false, 'i',
199  &elems, NULL, &nelems);
200 
201  if (nelems == 0)
202  return NIL;
203 
204  for (i = 0; i < nelems; i++)
205  res = lappend(res, makeString(pstrdup(TextDatumGetCString(elems[i]))));
206 
207  return res;
208 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
#define TEXTOID
Definition: pg_type.h:324
char * pstrdup(const char *in)
Definition: mcxt.c:1077
List * lappend(List *list, void *datum)
Definition: list.c:128
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:226
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