PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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 "catalog/indexing.h"
24#include "catalog/pg_type.h"
25#include "miscadmin.h"
26#include "storage/lmgr.h"
27#include "utils/array.h"
28#include "utils/builtins.h"
29#include "utils/fmgroids.h"
30#include "utils/lsyscache.h"
31#include "utils/pg_lsn.h"
32#include "utils/rel.h"
33#include "utils/syscache.h"
34
36
37/*
38 * Add a comma-separated list of publication names to the 'dest' string.
39 */
40void
42{
43 ListCell *lc;
44 bool first = true;
45
46 Assert(publications != NIL);
47
48 foreach(lc, publications)
49 {
50 char *pubname = strVal(lfirst(lc));
51
52 if (first)
53 first = false;
54 else
55 appendStringInfoString(dest, ", ");
56
57 if (quote_literal)
59 else
60 {
61 appendStringInfoChar(dest, '"');
62 appendStringInfoString(dest, pubname);
63 appendStringInfoChar(dest, '"');
64 }
65 }
66}
67
68/*
69 * Fetch the subscription from the syscache.
70 */
72GetSubscription(Oid subid, bool missing_ok)
73{
75 Subscription *sub;
77 Datum datum;
78 bool isnull;
79
81
83 {
84 if (missing_ok)
85 return NULL;
86
87 elog(ERROR, "cache lookup failed for subscription %u", subid);
88 }
89
91
93 sub->oid = subid;
94 sub->dbid = subform->subdbid;
95 sub->skiplsn = subform->subskiplsn;
96 sub->name = pstrdup(NameStr(subform->subname));
97 sub->owner = subform->subowner;
98 sub->enabled = subform->subenabled;
99 sub->binary = subform->subbinary;
100 sub->stream = subform->substream;
101 sub->twophasestate = subform->subtwophasestate;
102 sub->disableonerr = subform->subdisableonerr;
103 sub->passwordrequired = subform->subpasswordrequired;
104 sub->runasowner = subform->subrunasowner;
105 sub->failover = subform->subfailover;
106 sub->retaindeadtuples = subform->subretaindeadtuples;
107 sub->maxretention = subform->submaxretention;
108 sub->retentionactive = subform->subretentionactive;
109
110 /* Get conninfo */
112 tup,
114 sub->conninfo = TextDatumGetCString(datum);
115
116 /* Get slotname */
118 tup,
120 &isnull);
121 if (!isnull)
122 sub->slotname = pstrdup(NameStr(*DatumGetName(datum)));
123 else
124 sub->slotname = NULL;
125
126 /* Get synccommit */
128 tup,
130 sub->synccommit = TextDatumGetCString(datum);
131
132 /* Get walrcvtimeout */
134 tup,
137
138 /* Get publications */
140 tup,
143
144 /* Get origin */
146 tup,
148 sub->origin = TextDatumGetCString(datum);
149
150 /* Is the subscription owner a superuser? */
152
154
155 return sub;
156}
157
158/*
159 * Return number of subscriptions defined in given database.
160 * Used by dropdb() to check if database can indeed be dropped.
161 */
162int
164{
165 int nsubs = 0;
166 Relation rel;
168 SysScanDesc scan;
170
172
176 ObjectIdGetDatum(dbid));
177
178 scan = systable_beginscan(rel, InvalidOid, false,
179 NULL, 1, &scankey);
180
181 while (HeapTupleIsValid(tup = systable_getnext(scan)))
182 nsubs++;
183
184 systable_endscan(scan);
185
186 table_close(rel, NoLock);
187
188 return nsubs;
189}
190
191/*
192 * Free memory allocated by subscription struct.
193 */
194void
196{
197 pfree(sub->name);
198 pfree(sub->conninfo);
199 if (sub->slotname)
200 pfree(sub->slotname);
202 pfree(sub);
203}
204
205/*
206 * Disable the given subscription.
207 */
208void
210{
211 Relation rel;
212 bool nulls[Natts_pg_subscription];
216
217 /* Look up the subscription in the catalog */
220
221 if (!HeapTupleIsValid(tup))
222 elog(ERROR, "cache lookup failed for subscription %u", subid);
223
225
226 /* Form a new tuple. */
227 memset(values, 0, sizeof(values));
228 memset(nulls, false, sizeof(nulls));
229 memset(replaces, false, sizeof(replaces));
230
231 /* Set the subscription to disabled. */
234
235 /* Update the catalog */
237 replaces);
238 CatalogTupleUpdate(rel, &tup->t_self, tup);
240
241 table_close(rel, NoLock);
242}
243
244/*
245 * Convert text array to list of strings.
246 *
247 * Note: the resulting list of strings is pallocated here.
248 */
249static List *
251{
252 Datum *elems;
253 int nelems,
254 i;
255 List *res = NIL;
256
258
259 if (nelems == 0)
260 return NIL;
261
262 for (i = 0; i < nelems; i++)
263 res = lappend(res, makeString(TextDatumGetCString(elems[i])));
264
265 return res;
266}
267
268/*
269 * Add new state record for a subscription table.
270 *
271 * If retain_lock is true, then don't release the locks taken in this function.
272 * We normally release the locks at the end of transaction but in binary-upgrade
273 * mode, we expect to release those immediately.
274 */
275void
278{
279 Relation rel;
281 bool nulls[Natts_pg_subscription_rel];
283
285
287
288 /* Try finding existing mapping. */
290 ObjectIdGetDatum(relid),
291 ObjectIdGetDatum(subid));
293 elog(ERROR, "subscription relation %u in subscription %u already exists",
294 relid, subid);
295
296 /* Form the tuple. */
297 memset(values, 0, sizeof(values));
298 memset(nulls, false, sizeof(nulls));
304 else
305 nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
306
308
309 /* Insert tuple into catalog. */
311
313
314 /* Cleanup. */
315 if (retain_lock)
316 {
317 table_close(rel, NoLock);
318 }
319 else
320 {
323 }
324}
325
326/*
327 * Update the state of a subscription table.
328 */
329void
332{
333 Relation rel;
335 bool nulls[Natts_pg_subscription_rel];
338
339 if (already_locked)
340 {
341#ifdef USE_ASSERT_CHECKING
342 LOCKTAG tag;
343
345 RowExclusiveLock, true));
347 Assert(LockHeldByMe(&tag, AccessShareLock, true));
348#endif
349
351 }
352 else
353 {
356 }
357
358 /* Try finding existing mapping. */
360 ObjectIdGetDatum(relid),
361 ObjectIdGetDatum(subid));
362 if (!HeapTupleIsValid(tup))
363 elog(ERROR, "subscription relation %u in subscription %u does not exist",
364 relid, subid);
365
366 /* Update the tuple. */
367 memset(values, 0, sizeof(values));
368 memset(nulls, false, sizeof(nulls));
369 memset(replaces, false, sizeof(replaces));
370
373
377 else
378 nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
379
381 replaces);
382
383 /* Update the catalog. */
384 CatalogTupleUpdate(rel, &tup->t_self, tup);
385
386 /* Cleanup. */
387 table_close(rel, NoLock);
388}
389
390/*
391 * Get state of subscription table.
392 *
393 * Returns SUBREL_STATE_UNKNOWN when the table is not in the subscription.
394 */
395char
397{
399 char substate;
400 bool isnull;
401 Datum d;
402 Relation rel;
403
404 /*
405 * This is to avoid the race condition with AlterSubscription which tries
406 * to remove this relstate.
407 */
409
410 /* Try finding the mapping. */
412 ObjectIdGetDatum(relid),
413 ObjectIdGetDatum(subid));
414
415 if (!HeapTupleIsValid(tup))
416 {
420 }
421
422 /* Get the state. */
424
425 /* Get the LSN */
428 if (isnull)
430 else
431 *sublsn = DatumGetLSN(d);
432
433 /* Cleanup */
435
437
438 return substate;
439}
440
441/*
442 * Drop subscription relation mapping. These can be for a particular
443 * subscription, or for a particular relation, or both.
444 */
445void
447{
448 Relation rel;
449 TableScanDesc scan;
450 ScanKeyData skey[2];
452 int nkeys = 0;
453
455
456 if (OidIsValid(subid))
457 {
458 ScanKeyInit(&skey[nkeys++],
461 F_OIDEQ,
462 ObjectIdGetDatum(subid));
463 }
464
465 if (OidIsValid(relid))
466 {
467 ScanKeyInit(&skey[nkeys++],
470 F_OIDEQ,
471 ObjectIdGetDatum(relid));
472 }
473
474 /* Do the search and delete what we found. */
475 scan = table_beginscan_catalog(rel, nkeys, skey);
477 {
479
481
482 /*
483 * We don't allow to drop the relation mapping when the table
484 * synchronization is in progress unless the caller updates the
485 * corresponding subscription as well. This is to ensure that we don't
486 * leave tablesync slots or origins in the system when the
487 * corresponding table is dropped. For sequences, however, it's ok to
488 * drop them since no separate slots or origins are created during
489 * synchronization.
490 */
491 if (!OidIsValid(subid) &&
492 subrel->srsubstate != SUBREL_STATE_READY &&
494 {
497 errmsg("could not drop relation mapping for subscription \"%s\"",
498 get_subscription_name(subrel->srsubid, false)),
499 errdetail("Table synchronization for relation \"%s\" is in progress and is in state \"%c\".",
500 get_rel_name(relid), subrel->srsubstate),
501
502 /*
503 * translator: first %s is a SQL ALTER command and second %s is a
504 * SQL DROP command
505 */
506 errhint("Use %s to enable subscription if not already enabled or use %s to drop the subscription.",
507 "ALTER SUBSCRIPTION ... ENABLE",
508 "DROP SUBSCRIPTION ...")));
509 }
510
511 CatalogTupleDelete(rel, &tup->t_self);
512 }
513 table_endscan(scan);
514
516}
517
518/*
519 * Does the subscription have any tables?
520 *
521 * Use this function only to know true/false, and when you have no need for the
522 * List returned by GetSubscriptionRelations.
523 */
524bool
526{
527 Relation rel;
528 ScanKeyData skey[1];
529 SysScanDesc scan;
531 bool has_subtables = false;
532
534
535 ScanKeyInit(&skey[0],
538 ObjectIdGetDatum(subid));
539
540 scan = systable_beginscan(rel, InvalidOid, false,
541 NULL, 1, skey);
542
543 while (HeapTupleIsValid(tup = systable_getnext(scan)))
544 {
546 char relkind;
547
549 relkind = get_rel_relkind(subrel->srrelid);
550
551 if (relkind == RELKIND_RELATION ||
552 relkind == RELKIND_PARTITIONED_TABLE)
553 {
554 has_subtables = true;
555 break;
556 }
557 }
558
559 /* Cleanup */
560 systable_endscan(scan);
562
563 return has_subtables;
564}
565
566/*
567 * Get the relations for the subscription.
568 *
569 * If not_ready is true, return only the relations that are not in a ready
570 * state, otherwise return all the relations of the subscription. The
571 * returned list is palloc'ed in the current memory context.
572 */
573List *
574GetSubscriptionRelations(Oid subid, bool tables, bool sequences,
575 bool not_ready)
576{
577 List *res = NIL;
578 Relation rel;
580 int nkeys = 0;
581 ScanKeyData skey[2];
582 SysScanDesc scan;
583
584 /* One or both of 'tables' and 'sequences' must be true. */
585 Assert(tables || sequences);
586
588
589 ScanKeyInit(&skey[nkeys++],
592 ObjectIdGetDatum(subid));
593
594 if (not_ready)
595 ScanKeyInit(&skey[nkeys++],
599
600 scan = systable_beginscan(rel, InvalidOid, false,
601 NULL, nkeys, skey);
602
603 while (HeapTupleIsValid(tup = systable_getnext(scan)))
604 {
606 SubscriptionRelState *relstate;
607 Datum d;
608 bool isnull;
609 char relkind;
610
612
613 /* Relation is either a sequence or a table */
614 relkind = get_rel_relkind(subrel->srrelid);
615 Assert(relkind == RELKIND_SEQUENCE || relkind == RELKIND_RELATION ||
616 relkind == RELKIND_PARTITIONED_TABLE);
617
618 /* Skip sequences if they were not requested */
619 if ((relkind == RELKIND_SEQUENCE) && !sequences)
620 continue;
621
622 /* Skip tables if they were not requested */
623 if ((relkind == RELKIND_RELATION ||
624 relkind == RELKIND_PARTITIONED_TABLE) && !tables)
625 continue;
626
628 relstate->relid = subrel->srrelid;
629 relstate->state = subrel->srsubstate;
632 if (isnull)
633 relstate->lsn = InvalidXLogRecPtr;
634 else
635 relstate->lsn = DatumGetLSN(d);
636
637 res = lappend(res, relstate);
638 }
639
640 /* Cleanup */
641 systable_endscan(scan);
643
644 return res;
645}
646
647/*
648 * Update the dead tuple retention status for the given subscription.
649 */
650void
652{
653 Relation rel;
654 bool nulls[Natts_pg_subscription];
658
659 /* Look up the subscription in the catalog */
662
663 if (!HeapTupleIsValid(tup))
664 elog(ERROR, "cache lookup failed for subscription %u", subid);
665
667
668 /* Form a new tuple. */
669 memset(values, 0, sizeof(values));
670 memset(nulls, false, sizeof(nulls));
671 memset(replaces, false, sizeof(replaces));
672
673 /* Set the subscription to disabled. */
676
677 /* Update the catalog */
679 replaces);
680 CatalogTupleUpdate(rel, &tup->t_self, tup);
682
683 table_close(rel, NoLock);
684}
#define DatumGetArrayTypeP(X)
Definition array.h:261
void deconstruct_array_builtin(const ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
static Datum values[MAXATTR]
Definition bootstrap.c:147
#define TextDatumGetCString(d)
Definition builtins.h:99
#define NameStr(name)
Definition c.h:777
#define Assert(condition)
Definition c.h:885
#define OidIsValid(objectId)
Definition c.h:800
int errcode(int sqlerrcode)
Definition elog.c:874
int errmsg(const char *fmt,...)
Definition elog.c:1093
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
#define palloc_object(type)
Definition fe_memutils.h:74
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:603
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:514
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition heapam.c:1410
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition heaptuple.c:1210
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1435
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition indexing.c:313
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition indexing.c:233
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition indexing.c:365
int i
Definition isn.c:77
List * lappend(List *list, void *datum)
Definition list.c:339
void list_free_deep(List *list)
Definition list.c:1560
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition lmgr.c:1088
bool CheckRelationOidLockedByMe(Oid relid, LOCKMODE lockmode, bool orstronger)
Definition lmgr.c:351
void UnlockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition lmgr.c:1148
bool LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode, bool orstronger)
Definition lock.c:643
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition lock.h:264
#define NoLock
Definition lockdefs.h:34
#define AccessShareLock
Definition lockdefs.h:36
#define RowExclusiveLock
Definition lockdefs.h:38
char * get_rel_name(Oid relid)
Definition lsyscache.c:2078
char get_rel_relkind(Oid relid)
Definition lsyscache.c:2153
char * get_subscription_name(Oid subid, bool missing_ok)
Definition lsyscache.c:3847
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
static SequenceItem * sequences
Definition pg_dump.c:214
#define lfirst(lc)
Definition pg_list.h:172
#define NIL
Definition pg_list.h:68
static Datum LSNGetDatum(XLogRecPtr X)
Definition pg_lsn.h:31
static XLogRecPtr DatumGetLSN(Datum X)
Definition pg_lsn.h:25
void UpdateSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn, bool already_locked)
int CountDBSubscriptions(Oid dbid)
void FreeSubscription(Subscription *sub)
void DisableSubscription(Oid subid)
void RemoveSubscriptionRel(Oid subid, Oid relid)
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
void GetPublicationsStr(List *publications, StringInfo dest, bool quote_literal)
void AddSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn, bool retain_lock)
bool HasSubscriptionTables(Oid subid)
static List * textarray_to_stringlist(ArrayType *textarray)
void UpdateDeadTupleRetentionStatus(Oid subid, bool active)
Subscription * GetSubscription(Oid subid, bool missing_ok)
List * GetSubscriptionRelations(Oid subid, bool tables, bool sequences, bool not_ready)
END_CATALOG_STRUCT typedef FormData_pg_subscription * Form_pg_subscription
END_CATALOG_STRUCT typedef FormData_pg_subscription_rel * Form_pg_subscription_rel
static Name DatumGetName(Datum X)
Definition postgres.h:390
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
static Datum CharGetDatum(char X)
Definition postgres.h:132
#define InvalidOid
unsigned int Oid
static int fb(int x)
char * quote_literal_cstr(const char *rawstr)
Definition quote.c:101
Datum quote_literal(PG_FUNCTION_ARGS)
Definition quote.c:76
#define RelationGetDescr(relation)
Definition rel.h:540
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
@ ForwardScanDirection
Definition sdir.h:28
#define BTEqualStrategyNumber
Definition stratnum.h:31
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
void appendStringInfoChar(StringInfo str, char ch)
Definition stringinfo.c:242
Definition pg_list.h:54
XLogRecPtr skiplsn
bool superuser_arg(Oid roleid)
Definition superuser.c:57
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache2(SysCacheIdentifier cacheId, Datum key1, Datum key2)
Definition syscache.c:230
Datum SysCacheGetAttrNotNull(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition syscache.c:625
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:220
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:595
#define SearchSysCacheCopy1(cacheId, key1)
Definition syscache.h:91
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition syscache.h:93
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, ScanKeyData *key)
Definition tableam.c:113
static void table_endscan(TableScanDesc scan)
Definition tableam.h:1004
String * makeString(char *str)
Definition value.c:63
#define strVal(v)
Definition value.h:82
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29
uint64 XLogRecPtr
Definition xlogdefs.h:21
#define InvalidXLogRecPtr
Definition xlogdefs.h:28