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"
25#include "catalog/pg_type.h"
26#include "foreign/foreign.h"
27#include "miscadmin.h"
28#include "storage/lmgr.h"
29#include "storage/lock.h"
30#include "utils/acl.h"
31#include "utils/array.h"
32#include "utils/builtins.h"
33#include "utils/fmgroids.h"
34#include "utils/lsyscache.h"
35#include "utils/memutils.h"
36#include "utils/pg_lsn.h"
37#include "utils/rel.h"
38#include "utils/syscache.h"
39
41
42/*
43 * Add a comma-separated list of publication names to the 'dest' string.
44 *
45 * If quote_literal is true, the returned list can be used to construct an SQL
46 * command, thus no translation is applied. Otherwise, the string can be used
47 * to create a user-facing message, so translatable quote marks are added.
48 */
49void
51{
52 ListCell *lc;
53 bool first = true;
54
55 Assert(publications != NIL);
56
57 foreach(lc, publications)
58 {
59 char *pubname = strVal(lfirst(lc));
60
61 if (quote_literal)
62 {
63 if (!first)
64 appendStringInfoString(dest, ", ");
66 }
67 else
68 {
69 if (first)
70 appendStringInfo(dest, _("\"%s\""), pubname);
71 else
72 appendStringInfo(dest, _(", \"%s\""), pubname);
73 }
74
75 first = false;
76 }
77}
78
79/*
80 * Fetch the subscription from the syscache.
81 *
82 * If conninfo_needed is true, conninfo will be constructed, possibly
83 * encountering errors in ForeignServerConnectionString(). Callers not
84 * expecting such errors should pass false, in which case conninfo will be
85 * NULL.
86 */
88GetSubscription(Oid subid, bool missing_ok, bool conninfo_needed,
90{
92 Subscription *sub;
94 Datum datum;
95 bool isnull;
96 MemoryContext cxt;
98
100
102
103 if (!HeapTupleIsValid(tup))
104 {
105 if (missing_ok)
106 return NULL;
107
108 elog(ERROR, "cache lookup failed for subscription %u", subid);
109 }
110
111 cxt = AllocSetContextCreate(CurrentMemoryContext, "subscription",
114
116
118 sub->cxt = cxt;
119 sub->oid = subid;
120 sub->dbid = subform->subdbid;
121 sub->skiplsn = subform->subskiplsn;
122 sub->name = pstrdup(NameStr(subform->subname));
123 sub->owner = subform->subowner;
124 sub->enabled = subform->subenabled;
125 sub->binary = subform->subbinary;
126 sub->stream = subform->substream;
127 sub->twophasestate = subform->subtwophasestate;
128 sub->disableonerr = subform->subdisableonerr;
129 sub->passwordrequired = subform->subpasswordrequired;
130 sub->runasowner = subform->subrunasowner;
131 sub->failover = subform->subfailover;
132 sub->retaindeadtuples = subform->subretaindeadtuples;
133 sub->maxretention = subform->submaxretention;
134 sub->retentionactive = subform->subretentionactive;
135
136 if (conninfo_needed)
137 {
138 if (OidIsValid(subform->subserver))
139 {
141 ForeignServer *server;
142
143 server = GetForeignServer(subform->subserver);
144
146 {
147 /* recheck ACL if requested */
149 subform->subserver,
150 subform->subowner, ACL_USAGE);
151
152 if (aclresult != ACLCHECK_OK)
155 errmsg("subscription owner \"%s\" does not have permission on foreign server \"%s\"",
156 GetUserNameFromId(subform->subowner, false),
157 server->servername)));
158 }
159
161 server);
162 }
163 else
164 {
166 tup,
168 sub->conninfo = TextDatumGetCString(datum);
169 }
170 }
171
172 /* Get slotname */
174 tup,
176 &isnull);
177 if (!isnull)
178 sub->slotname = pstrdup(NameStr(*DatumGetName(datum)));
179 else
180 sub->slotname = NULL;
181
182 /* Get synccommit */
184 tup,
186 sub->synccommit = TextDatumGetCString(datum);
187
188 /* Get walrcvtimeout */
190 tup,
193
194 /* Get publications */
196 tup,
199
200 /* Get origin */
202 tup,
204 sub->origin = TextDatumGetCString(datum);
205
206 /* Is the subscription owner a superuser? */
208
210
212
213 return sub;
214}
215
216/*
217 * Return number of subscriptions defined in given database.
218 * Used by dropdb() to check if database can indeed be dropped.
219 */
220int
222{
223 int nsubs = 0;
224 Relation rel;
226 SysScanDesc scan;
228
230
234 ObjectIdGetDatum(dbid));
235
236 scan = systable_beginscan(rel, InvalidOid, false,
237 NULL, 1, &scankey);
238
239 while (HeapTupleIsValid(tup = systable_getnext(scan)))
240 nsubs++;
241
242 systable_endscan(scan);
243
244 table_close(rel, NoLock);
245
246 return nsubs;
247}
248
249/*
250 * Disable the given subscription.
251 */
252void
254{
255 Relation rel;
256 bool nulls[Natts_pg_subscription];
260
261 /* Look up the subscription in the catalog */
264
265 if (!HeapTupleIsValid(tup))
266 elog(ERROR, "cache lookup failed for subscription %u", subid);
267
269
270 /* Form a new tuple. */
271 memset(values, 0, sizeof(values));
272 memset(nulls, false, sizeof(nulls));
273 memset(replaces, false, sizeof(replaces));
274
275 /* Set the subscription to disabled. */
278
279 /* Update the catalog */
281 replaces);
282 CatalogTupleUpdate(rel, &tup->t_self, tup);
284
285 table_close(rel, NoLock);
286}
287
288/*
289 * Convert text array to list of strings.
290 *
291 * Note: the resulting list of strings is pallocated here.
292 */
293static List *
295{
296 Datum *elems;
297 int nelems,
298 i;
299 List *res = NIL;
300
302
303 if (nelems == 0)
304 return NIL;
305
306 for (i = 0; i < nelems; i++)
307 res = lappend(res, makeString(TextDatumGetCString(elems[i])));
308
309 return res;
310}
311
312/*
313 * Add new state record for a subscription table.
314 *
315 * If retain_lock is true, then don't release the locks taken in this function.
316 * We normally release the locks at the end of transaction but in binary-upgrade
317 * mode, we expect to release those immediately.
318 */
319void
322{
323 Relation rel;
325 bool nulls[Natts_pg_subscription_rel];
327
329
331
332 /* Try finding existing mapping. */
334 ObjectIdGetDatum(relid),
335 ObjectIdGetDatum(subid));
337 elog(ERROR, "subscription relation %u in subscription %u already exists",
338 relid, subid);
339
340 /* Form the tuple. */
341 memset(values, 0, sizeof(values));
342 memset(nulls, false, sizeof(nulls));
348 else
349 nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
350
352
353 /* Insert tuple into catalog. */
355
357
358 /* Cleanup. */
359 if (retain_lock)
360 {
361 table_close(rel, NoLock);
362 }
363 else
364 {
367 }
368}
369
370/*
371 * Update the state of a subscription table.
372 */
373void
376{
377 Relation rel;
379 bool nulls[Natts_pg_subscription_rel];
382
383 if (already_locked)
384 {
385#ifdef USE_ASSERT_CHECKING
386 LOCKTAG tag;
387
389 RowExclusiveLock, true));
391 Assert(LockHeldByMe(&tag, AccessShareLock, true));
392#endif
393
395 }
396 else
397 {
400 }
401
402 /* Try finding existing mapping. */
404 ObjectIdGetDatum(relid),
405 ObjectIdGetDatum(subid));
406 if (!HeapTupleIsValid(tup))
407 elog(ERROR, "subscription relation %u in subscription %u does not exist",
408 relid, subid);
409
410 /* Update the tuple. */
411 memset(values, 0, sizeof(values));
412 memset(nulls, false, sizeof(nulls));
413 memset(replaces, false, sizeof(replaces));
414
417
421 else
422 nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
423
425 replaces);
426
427 /* Update the catalog. */
428 CatalogTupleUpdate(rel, &tup->t_self, tup);
429
430 /* Cleanup. */
431 table_close(rel, NoLock);
432}
433
434/*
435 * Get state of subscription table.
436 *
437 * Returns SUBREL_STATE_UNKNOWN when the table is not in the subscription.
438 */
439char
441{
443 char substate;
444 bool isnull;
445 Datum d;
446 Relation rel;
447
448 /*
449 * This is to avoid the race condition with AlterSubscription which tries
450 * to remove this relstate.
451 */
453
454 /* Try finding the mapping. */
456 ObjectIdGetDatum(relid),
457 ObjectIdGetDatum(subid));
458
459 if (!HeapTupleIsValid(tup))
460 {
464 }
465
466 /* Get the state. */
468
469 /* Get the LSN */
472 if (isnull)
474 else
475 *sublsn = DatumGetLSN(d);
476
477 /* Cleanup */
479
481
482 return substate;
483}
484
485/*
486 * Drop subscription relation mapping. These can be for a particular
487 * subscription, or for a particular relation, or both.
488 */
489void
491{
492 Relation rel;
493 TableScanDesc scan;
494 ScanKeyData skey[2];
496 int nkeys = 0;
497
499
500 if (OidIsValid(subid))
501 {
502 ScanKeyInit(&skey[nkeys++],
505 F_OIDEQ,
506 ObjectIdGetDatum(subid));
507 }
508
509 if (OidIsValid(relid))
510 {
511 ScanKeyInit(&skey[nkeys++],
514 F_OIDEQ,
515 ObjectIdGetDatum(relid));
516 }
517
518 /* Do the search and delete what we found. */
519 scan = table_beginscan_catalog(rel, nkeys, skey);
521 {
523
525
526 /*
527 * We don't allow to drop the relation mapping when the table
528 * synchronization is in progress unless the caller updates the
529 * corresponding subscription as well. This is to ensure that we don't
530 * leave tablesync slots or origins in the system when the
531 * corresponding table is dropped. For sequences, however, it's ok to
532 * drop them since no separate slots or origins are created during
533 * synchronization.
534 */
535 if (!OidIsValid(subid) &&
536 subrel->srsubstate != SUBREL_STATE_READY &&
538 {
541 errmsg("could not drop relation mapping for subscription \"%s\"",
542 get_subscription_name(subrel->srsubid, false)),
543 errdetail("Table synchronization for relation \"%s\" is in progress and is in state \"%c\".",
544 get_rel_name(relid), subrel->srsubstate),
545
546 /*
547 * translator: first %s is a SQL ALTER command and second %s is a
548 * SQL DROP command
549 */
550 errhint("Use %s to enable subscription if not already enabled or use %s to drop the subscription.",
551 "ALTER SUBSCRIPTION ... ENABLE",
552 "DROP SUBSCRIPTION ...")));
553 }
554
555 CatalogTupleDelete(rel, &tup->t_self);
556 }
557 table_endscan(scan);
558
560}
561
562/*
563 * Does the subscription have any tables?
564 *
565 * Use this function only to know true/false, and when you have no need for the
566 * List returned by GetSubscriptionRelations.
567 */
568bool
570{
571 Relation rel;
572 ScanKeyData skey[1];
573 SysScanDesc scan;
575 bool has_subtables = false;
576
578
579 ScanKeyInit(&skey[0],
582 ObjectIdGetDatum(subid));
583
584 scan = systable_beginscan(rel, InvalidOid, false,
585 NULL, 1, skey);
586
587 while (HeapTupleIsValid(tup = systable_getnext(scan)))
588 {
590 char relkind;
591
593 relkind = get_rel_relkind(subrel->srrelid);
594
595 if (relkind == RELKIND_RELATION ||
596 relkind == RELKIND_PARTITIONED_TABLE)
597 {
598 has_subtables = true;
599 break;
600 }
601 }
602
603 /* Cleanup */
604 systable_endscan(scan);
606
607 return has_subtables;
608}
609
610/*
611 * Get the relations for the subscription.
612 *
613 * If not_ready is true, return only the relations that are not in a ready
614 * state, otherwise return all the relations of the subscription. The
615 * returned list is palloc'ed in the current memory context.
616 */
617List *
618GetSubscriptionRelations(Oid subid, bool tables, bool sequences,
619 bool not_ready)
620{
621 List *res = NIL;
622 Relation rel;
624 int nkeys = 0;
625 ScanKeyData skey[2];
626 SysScanDesc scan;
627
628 /* One or both of 'tables' and 'sequences' must be true. */
629 Assert(tables || sequences);
630
632
633 ScanKeyInit(&skey[nkeys++],
636 ObjectIdGetDatum(subid));
637
638 if (not_ready)
639 ScanKeyInit(&skey[nkeys++],
643
644 scan = systable_beginscan(rel, InvalidOid, false,
645 NULL, nkeys, skey);
646
647 while (HeapTupleIsValid(tup = systable_getnext(scan)))
648 {
650 SubscriptionRelState *relstate;
651 Datum d;
652 bool isnull;
653 char relkind;
654
656
657 /* Relation is either a sequence or a table */
658 relkind = get_rel_relkind(subrel->srrelid);
659 Assert(relkind == RELKIND_SEQUENCE || relkind == RELKIND_RELATION ||
660 relkind == RELKIND_PARTITIONED_TABLE);
661
662 /* Skip sequences if they were not requested */
663 if ((relkind == RELKIND_SEQUENCE) && !sequences)
664 continue;
665
666 /* Skip tables if they were not requested */
667 if ((relkind == RELKIND_RELATION ||
668 relkind == RELKIND_PARTITIONED_TABLE) && !tables)
669 continue;
670
672 relstate->relid = subrel->srrelid;
673 relstate->state = subrel->srsubstate;
676 if (isnull)
677 relstate->lsn = InvalidXLogRecPtr;
678 else
679 relstate->lsn = DatumGetLSN(d);
680
681 res = lappend(res, relstate);
682 }
683
684 /* Cleanup */
685 systable_endscan(scan);
687
688 return res;
689}
690
691/*
692 * Update the dead tuple retention status for the given subscription.
693 */
694void
696{
697 Relation rel;
698 bool nulls[Natts_pg_subscription];
702
703 /* Look up the subscription in the catalog */
706
707 if (!HeapTupleIsValid(tup))
708 elog(ERROR, "cache lookup failed for subscription %u", subid);
709
711
712 /* Form a new tuple. */
713 memset(values, 0, sizeof(values));
714 memset(nulls, false, sizeof(nulls));
715 memset(replaces, false, sizeof(replaces));
716
717 /* Set the subscription to disabled. */
720
721 /* Update the catalog */
723 replaces);
724 CatalogTupleUpdate(rel, &tup->t_self, tup);
726
727 table_close(rel, NoLock);
728}
AclResult
Definition acl.h:183
@ ACLCHECK_OK
Definition acl.h:184
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition aclchk.c:3880
#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:190
#define TextDatumGetCString(d)
Definition builtins.h:99
#define NameStr(name)
Definition c.h:891
#define Assert(condition)
Definition c.h:999
#define OidIsValid(objectId)
Definition c.h:914
int errcode(int sqlerrcode)
Definition elog.c:875
#define _(x)
Definition elog.c:96
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
#define palloc_object(type)
Definition fe_memutils.h:89
#define palloc0_object(type)
Definition fe_memutils.h:90
char * ForeignServerConnectionString(Oid userid, ForeignServer *server)
Definition foreign.c:202
ForeignServer * GetForeignServer(Oid serverid)
Definition foreign.c:114
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:604
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:515
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:1435
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition heaptuple.c:1118
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1025
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1372
#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 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:640
#define NoLock
Definition lockdefs.h:34
#define AccessShareLock
Definition lockdefs.h:36
#define RowExclusiveLock
Definition lockdefs.h:38
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition locktag.h:162
char * get_rel_name(Oid relid)
Definition lsyscache.c:2234
char get_rel_relkind(Oid relid)
Definition lsyscache.c:2309
char * get_subscription_name(Oid subid, bool missing_ok)
Definition lsyscache.c:4048
char * pstrdup(const char *in)
Definition mcxt.c:1910
MemoryContext CurrentMemoryContext
Definition mcxt.c:161
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition memutils.h:170
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition miscinit.c:990
static char * errmsg
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
#define ACL_USAGE
Definition parsenodes.h:84
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)
Subscription * GetSubscription(Oid subid, bool missing_ok, bool conninfo_needed, bool conninfo_aclcheck)
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)
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:393
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
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:542
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 appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
char * servername
Definition foreign.h:40
Definition pg_list.h:54
MemoryContext cxt
XLogRecPtr skiplsn
bool superuser_arg(Oid roleid)
Definition superuser.c:57
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache2(SysCacheIdentifier cacheId, Datum key1, Datum key2)
Definition syscache.c:231
Datum SysCacheGetAttrNotNull(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition syscache.c:626
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:221
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:596
#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:1061
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