PostgreSQL Source Code  git master
pg_subscription.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_subscription.c
4  * replication subscriptions
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  * src/backend/catalog/pg_subscription.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 
15 #include "postgres.h"
16 
17 #include "access/genam.h"
18 #include "access/heapam.h"
19 #include "access/htup_details.h"
20 #include "access/tableam.h"
21 #include "access/xact.h"
22 #include "catalog/indexing.h"
25 #include "catalog/pg_type.h"
26 #include "miscadmin.h"
27 #include "nodes/makefuncs.h"
28 #include "storage/lmgr.h"
29 #include "utils/array.h"
30 #include "utils/builtins.h"
31 #include "utils/fmgroids.h"
32 #include "utils/pg_lsn.h"
33 #include "utils/rel.h"
34 #include "utils/syscache.h"
35 
36 static List *textarray_to_stringlist(ArrayType *textarray);
37 
38 /*
39  * Fetch the subscription from the syscache.
40  */
42 GetSubscription(Oid subid, bool missing_ok)
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 }
109 
110 /*
111  * Return number of subscriptions defined in given database.
112  * Used by dropdb() to check if database can indeed be dropped.
113  */
114 int
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 }
142 
143 /*
144  * Free memory allocated by subscription struct.
145  */
146 void
148 {
149  pfree(sub->name);
150  pfree(sub->conninfo);
151  if (sub->slotname)
152  pfree(sub->slotname);
154  pfree(sub);
155 }
156 
157 /*
158  * get_subscription_oid - given a subscription name, look up the OID
159  *
160  * If missing_ok is false, throw an error if name not found. If true, just
161  * return InvalidOid.
162  */
163 Oid
164 get_subscription_oid(const char *subname, bool missing_ok)
165 {
166  Oid oid;
167 
168  oid = GetSysCacheOid2(SUBSCRIPTIONNAME, Anum_pg_subscription_oid,
169  MyDatabaseId, CStringGetDatum(subname));
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 }
176 
177 /*
178  * get_subscription_name - given a subscription OID, look up the name
179  *
180  * If missing_ok is false, throw an error if name not found. If true, just
181  * return NULL.
182  */
183 char *
184 get_subscription_name(Oid subid, bool missing_ok)
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 }
206 
207 /*
208  * Convert text array to list of strings.
209  *
210  * Note: the resulting list of strings is pallocated here.
211  */
212 static List *
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 }
232 
233 /*
234  * Add new state record for a subscription table.
235  */
236 void
238  XLogRecPtr sublsn)
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 }
278 
279 /*
280  * Update the state of a subscription table.
281  */
282 void
284  XLogRecPtr sublsn)
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 }
327 
328 /*
329  * Get state of subscription table.
330  *
331  * Returns SUBREL_STATE_UNKNOWN when the table is not in the subscription.
332  */
333 char
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 }
368 
369 /*
370  * Drop subscription relation mapping. These can be for a particular
371  * subscription, or for a particular relation, or both.
372  */
373 void
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 }
412 
413 
414 /*
415  * Get all relations for subscription.
416  *
417  * Returned list is palloc'ed in current memory context.
418  */
419 List *
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 }
467 
468 /*
469  * Get all relations for subscription that are not in a ready state.
470  *
471  * Returned list is palloc'ed in current memory context.
472  */
473 List *
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 }
Value * makeString(char *str)
Definition: value.c:53
#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:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
void RemoveSubscriptionRel(Oid subid, Oid relid)
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
#define RelationGetDescr(relation)
Definition: rel.h:483
char * pstrdup(const char *in)
Definition: mcxt.c:1187
int CountDBSubscriptions(Oid dbid)
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:704
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:351
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
FormData_pg_subscription * Form_pg_subscription
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
NameData subname
#define OidIsValid(objectId)
Definition: c.h:698
void list_free_deep(List *list)
Definition: list.c:1405
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:383
char * get_subscription_name(Oid subid, bool missing_ok)
#define DatumGetName(X)
Definition: postgres.h:585
Subscription * GetSubscription(Oid subid, bool missing_ok)
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
static List * textarray_to_stringlist(ArrayType *textarray)
ItemPointerData t_self
Definition: htup.h:65
void AddSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn)
#define NoLock
Definition: lockdefs.h:34
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1316
#define RowExclusiveLock
Definition: lockdefs.h:38
List * GetSubscriptionRelations(Oid subid)
#define CStringGetDatum(X)
Definition: postgres.h:578
List * publications
List * lappend(List *list, void *datum)
Definition: list.c:336
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
#define TextDatumGetCString(d)
Definition: builtins.h:83
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
Oid MyDatabaseId
Definition: globals.c:86
void UpdateSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn)
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1017
#define InvalidOid
Definition: postgres_ext.h:36
#define DatumGetLSN(X)
Definition: pg_lsn.h:21
#define ereport(elevel,...)
Definition: elog.h:155
Oid get_subscription_oid(const char *subname, bool missing_ok)
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_subscription_rel * Form_pg_subscription_rel
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:792
Definition: regguts.h:298
#define GetSysCacheOid2(cacheId, oidcol, key1, key2)
Definition: syscache.h:195
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:302
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1138
#define CharGetDatum(X)
Definition: postgres.h:416
void FreeSubscription(Subscription *sub)
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3488
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:962
static Datum values[MAXATTR]
Definition: bootstrap.c:165
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define elog(elevel,...)
Definition: elog.h:228
int i
#define NameStr(name)
Definition: c.h:669
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:177
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
Definition: pg_list.h:50
List * GetSubscriptionNotReadyRelations(Oid subid)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:222
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define DatumGetArrayTypeP(X)
Definition: array.h:254