Branch data Line data Source code
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"
22 : : #include "catalog/pg_foreign_server.h"
23 : : #include "catalog/pg_subscription.h"
24 : : #include "catalog/pg_subscription_rel.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 : :
40 : : static List *textarray_to_stringlist(ArrayType *textarray);
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 : : */
49 : : void
50 : 581 : GetPublicationsStr(List *publications, StringInfo dest, bool quote_literal)
51 : : {
52 : : ListCell *lc;
53 : 581 : bool first = true;
54 : :
55 : : Assert(publications != NIL);
56 : :
57 [ + - + + : 1480 : foreach(lc, publications)
+ + ]
58 : : {
59 : 899 : char *pubname = strVal(lfirst(lc));
60 : :
61 [ + + ]: 899 : if (quote_literal)
62 : : {
63 [ + + ]: 888 : if (!first)
64 : 317 : appendStringInfoString(dest, ", ");
65 : 888 : appendStringInfoString(dest, quote_literal_cstr(pubname));
66 : : }
67 : : else
68 : : {
69 [ + + ]: 11 : if (first)
70 : 10 : appendStringInfo(dest, _("\"%s\""), pubname);
71 : : else
72 : 1 : appendStringInfo(dest, _(", \"%s\""), pubname);
73 : : }
74 : :
75 : 899 : first = false;
76 : : }
77 : 581 : }
78 : :
79 : : /*
80 : : * Fetch the subscription from the syscache.
81 : : *
82 : : * Callers that need conninfo must call SubscriptionConninfo().
83 : : */
84 : : Subscription *
85 : 1269 : GetSubscription(Oid subid, bool missing_ok)
86 : : {
87 : : HeapTuple tup;
88 : : Subscription *sub;
89 : : Form_pg_subscription subform;
90 : : Datum datum;
91 : : bool isnull;
92 : : MemoryContext cxt;
93 : : MemoryContext oldcxt;
94 : :
95 : 1269 : tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
96 : :
97 [ + + ]: 1269 : if (!HeapTupleIsValid(tup))
98 : : {
99 [ + - ]: 75 : if (missing_ok)
100 : 75 : return NULL;
101 : :
102 [ # # ]: 0 : elog(ERROR, "cache lookup failed for subscription %u", subid);
103 : : }
104 : :
105 : 1194 : cxt = AllocSetContextCreate(CurrentMemoryContext, "subscription",
106 : : ALLOCSET_SMALL_SIZES);
107 : 1194 : oldcxt = MemoryContextSwitchTo(cxt);
108 : :
109 : 1194 : subform = (Form_pg_subscription) GETSTRUCT(tup);
110 : :
111 : : /*
112 : : * It's only safe to access subscriptions from another database from the
113 : : * launcher process, which does not call GetSubscription().
114 : : */
115 : : Assert(subform->subdbid == MyDatabaseId);
116 : :
117 : 1194 : sub = palloc0_object(Subscription);
118 : 1194 : sub->cxt = cxt;
119 : 1194 : sub->oid = subid;
120 : 1194 : sub->dbid = subform->subdbid;
121 : 1194 : sub->skiplsn = subform->subskiplsn;
122 : 1194 : sub->name = pstrdup(NameStr(subform->subname));
123 : 1194 : sub->owner = subform->subowner;
124 : 1194 : sub->enabled = subform->subenabled;
125 : 1194 : sub->binary = subform->subbinary;
126 : 1194 : sub->stream = subform->substream;
127 : 1194 : sub->twophasestate = subform->subtwophasestate;
128 : 1194 : sub->disableonerr = subform->subdisableonerr;
129 : 1194 : sub->passwordrequired = subform->subpasswordrequired;
130 : 1194 : sub->runasowner = subform->subrunasowner;
131 : 1194 : sub->failover = subform->subfailover;
132 : 1194 : sub->retaindeadtuples = subform->subretaindeadtuples;
133 : 1194 : sub->maxretention = subform->submaxretention;
134 : 1194 : sub->retentionactive = subform->subretentionactive;
135 : 1194 : sub->conflictlogrelid = subform->subconflictlogrelid;
136 : :
137 : : /* Get slotname */
138 : 1194 : datum = SysCacheGetAttr(SUBSCRIPTIONOID,
139 : : tup,
140 : : Anum_pg_subscription_subslotname,
141 : : &isnull);
142 [ + + ]: 1194 : if (!isnull)
143 : 1150 : sub->slotname = pstrdup(NameStr(*DatumGetName(datum)));
144 : : else
145 : 44 : sub->slotname = NULL;
146 : :
147 : : /* Get synccommit */
148 : 1194 : datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID,
149 : : tup,
150 : : Anum_pg_subscription_subsynccommit);
151 : 1194 : sub->synccommit = TextDatumGetCString(datum);
152 : :
153 : : /* Get walrcvtimeout */
154 : 1194 : datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID,
155 : : tup,
156 : : Anum_pg_subscription_subwalrcvtimeout);
157 : 1194 : sub->walrcvtimeout = TextDatumGetCString(datum);
158 : :
159 : : /* Get publications */
160 : 1194 : datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID,
161 : : tup,
162 : : Anum_pg_subscription_subpublications);
163 : 1194 : sub->publications = textarray_to_stringlist(DatumGetArrayTypeP(datum));
164 : :
165 : : /* Get origin */
166 : 1194 : datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID,
167 : : tup,
168 : : Anum_pg_subscription_suborigin);
169 : 1194 : sub->origin = TextDatumGetCString(datum);
170 : :
171 : : /* Get conflict log destination */
172 : 1194 : datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID,
173 : : tup,
174 : : Anum_pg_subscription_subconflictlogdest);
175 : 1194 : sub->conflictlogdest = TextDatumGetCString(datum);
176 : :
177 : : /* Is the subscription owner a superuser? */
178 : 1194 : sub->ownersuperuser = superuser_arg(sub->owner);
179 : :
180 : 1194 : ReleaseSysCache(tup);
181 : :
182 : 1194 : MemoryContextSwitchTo(oldcxt);
183 : :
184 : 1194 : return sub;
185 : : }
186 : :
187 : : /*
188 : : * Generate the connection string for a subscription.
189 : : *
190 : : * This is deliberately separate from GetSubscription() because resolving
191 : : * conninfo for a server-based subscription has its own error paths (foreign
192 : : * server USAGE, user mapping, ForeignServerConnectionString()). Keeping it
193 : : * separate lets a caller load the subscription and decide whether a
194 : : * connection is actually needed, and check things such as whether the
195 : : * subscription is enabled, before risking those errors. Callers that never
196 : : * connect thus never hit them, which matters during restore.
197 : : */
198 : : char *
199 : 822 : SubscriptionConninfo(Subscription *sub)
200 : : {
201 : : HeapTuple tup;
202 : : Form_pg_subscription subform;
203 : : Datum datum;
204 : : char *conninfo;
205 : :
206 : 822 : tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(sub->oid));
207 [ - + ]: 822 : if (!HeapTupleIsValid(tup))
208 [ # # ]: 0 : elog(ERROR, "cache lookup failed for subscription %u", sub->oid);
209 : :
210 : 822 : subform = (Form_pg_subscription) GETSTRUCT(tup);
211 : :
212 [ + + ]: 822 : if (OidIsValid(subform->subserver))
213 : : {
214 : : ForeignServer *server;
215 : : AclResult aclresult;
216 : :
217 : 10 : server = GetForeignServer(subform->subserver);
218 : :
219 : 10 : aclresult = object_aclcheck(ForeignServerRelationId,
220 : : subform->subserver,
221 : : sub->owner, ACL_USAGE);
222 [ + + ]: 10 : if (aclresult != ACLCHECK_OK)
223 [ + - ]: 4 : ereport(ERROR,
224 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
225 : : errmsg("subscription owner \"%s\" does not have permission on foreign server \"%s\"",
226 : : GetUserNameFromId(sub->owner, false),
227 : : server->servername)));
228 : :
229 : 6 : conninfo = ForeignServerConnectionString(sub->owner, server);
230 : : }
231 : : else
232 : : {
233 : 812 : datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID, tup,
234 : : Anum_pg_subscription_subconninfo);
235 : 812 : conninfo = TextDatumGetCString(datum);
236 : : }
237 : :
238 : 818 : ReleaseSysCache(tup);
239 : :
240 : 818 : return conninfo;
241 : : }
242 : :
243 : : /*
244 : : * Return number of subscriptions defined in given database.
245 : : * Used by dropdb() to check if database can indeed be dropped.
246 : : */
247 : : int
248 : 68 : CountDBSubscriptions(Oid dbid)
249 : : {
250 : 68 : int nsubs = 0;
251 : : Relation rel;
252 : : ScanKeyData scankey;
253 : : SysScanDesc scan;
254 : : HeapTuple tup;
255 : :
256 : 68 : rel = table_open(SubscriptionRelationId, RowExclusiveLock);
257 : :
258 : 68 : ScanKeyInit(&scankey,
259 : : Anum_pg_subscription_subdbid,
260 : : BTEqualStrategyNumber, F_OIDEQ,
261 : : ObjectIdGetDatum(dbid));
262 : :
263 : 68 : scan = systable_beginscan(rel, InvalidOid, false,
264 : : NULL, 1, &scankey);
265 : :
266 [ - + ]: 68 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
267 : 0 : nsubs++;
268 : :
269 : 68 : systable_endscan(scan);
270 : :
271 : 68 : table_close(rel, NoLock);
272 : :
273 : 68 : return nsubs;
274 : : }
275 : :
276 : : /*
277 : : * Disable the given subscription.
278 : : */
279 : : void
280 : 4 : DisableSubscription(Oid subid)
281 : : {
282 : : Relation rel;
283 : : bool nulls[Natts_pg_subscription];
284 : : bool replaces[Natts_pg_subscription];
285 : : Datum values[Natts_pg_subscription];
286 : : HeapTuple tup;
287 : :
288 : : /* Look up the subscription in the catalog */
289 : 4 : rel = table_open(SubscriptionRelationId, RowExclusiveLock);
290 : 4 : tup = SearchSysCacheCopy1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
291 : :
292 [ - + ]: 4 : if (!HeapTupleIsValid(tup))
293 [ # # ]: 0 : elog(ERROR, "cache lookup failed for subscription %u", subid);
294 : :
295 : : /* Must only modify subscriptions belonging to the current database. */
296 : : Assert(((Form_pg_subscription) GETSTRUCT(tup))->subdbid == MyDatabaseId);
297 : :
298 : 4 : LockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
299 : :
300 : : /* Form a new tuple. */
301 : 4 : memset(values, 0, sizeof(values));
302 : 4 : memset(nulls, false, sizeof(nulls));
303 : 4 : memset(replaces, false, sizeof(replaces));
304 : :
305 : : /* Set the subscription to disabled. */
306 : 4 : values[Anum_pg_subscription_subenabled - 1] = BoolGetDatum(false);
307 : 4 : replaces[Anum_pg_subscription_subenabled - 1] = true;
308 : :
309 : : /* Update the catalog */
310 : 4 : tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
311 : : replaces);
312 : 4 : CatalogTupleUpdate(rel, &tup->t_self, tup);
313 : 4 : heap_freetuple(tup);
314 : :
315 : 4 : table_close(rel, NoLock);
316 : 4 : }
317 : :
318 : : /*
319 : : * Convert text array to list of strings.
320 : : *
321 : : * Note: the resulting list of strings is pallocated here.
322 : : */
323 : : static List *
324 : 1194 : textarray_to_stringlist(ArrayType *textarray)
325 : : {
326 : : Datum *elems;
327 : : int nelems,
328 : : i;
329 : 1194 : List *res = NIL;
330 : :
331 : 1194 : deconstruct_array_builtin(textarray, TEXTOID, &elems, NULL, &nelems);
332 : :
333 [ - + ]: 1194 : if (nelems == 0)
334 : 0 : return NIL;
335 : :
336 [ + + ]: 2799 : for (i = 0; i < nelems; i++)
337 : 1605 : res = lappend(res, makeString(TextDatumGetCString(elems[i])));
338 : :
339 : 1194 : return res;
340 : : }
341 : :
342 : : /*
343 : : * Add new state record for a subscription table.
344 : : *
345 : : * If retain_lock is true, then don't release the locks taken in this function.
346 : : * We normally release the locks at the end of transaction but in binary-upgrade
347 : : * mode, we expect to release those immediately.
348 : : */
349 : : void
350 : 243 : AddSubscriptionRelState(Oid subid, Oid relid, char state,
351 : : XLogRecPtr sublsn, bool retain_lock)
352 : : {
353 : : Relation rel;
354 : : HeapTuple tup;
355 : : bool nulls[Natts_pg_subscription_rel];
356 : : Datum values[Natts_pg_subscription_rel];
357 : :
358 : 243 : LockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
359 : :
360 : 243 : rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
361 : :
362 : : /* Try finding existing mapping. */
363 : 243 : tup = SearchSysCacheCopy2(SUBSCRIPTIONRELMAP,
364 : : ObjectIdGetDatum(relid),
365 : : ObjectIdGetDatum(subid));
366 [ - + ]: 243 : if (HeapTupleIsValid(tup))
367 [ # # ]: 0 : elog(ERROR, "subscription relation %u in subscription %u already exists",
368 : : relid, subid);
369 : :
370 : : /* Form the tuple. */
371 : 243 : memset(values, 0, sizeof(values));
372 : 243 : memset(nulls, false, sizeof(nulls));
373 : 243 : values[Anum_pg_subscription_rel_srsubid - 1] = ObjectIdGetDatum(subid);
374 : 243 : values[Anum_pg_subscription_rel_srrelid - 1] = ObjectIdGetDatum(relid);
375 : 243 : values[Anum_pg_subscription_rel_srsubstate - 1] = CharGetDatum(state);
376 [ + + ]: 243 : if (XLogRecPtrIsValid(sublsn))
377 : 2 : values[Anum_pg_subscription_rel_srsublsn - 1] = LSNGetDatum(sublsn);
378 : : else
379 : 241 : nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
380 : :
381 : 243 : tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
382 : :
383 : : /* Insert tuple into catalog. */
384 : 243 : CatalogTupleInsert(rel, tup);
385 : :
386 : 243 : heap_freetuple(tup);
387 : :
388 : : /* Cleanup. */
389 [ + + ]: 243 : if (retain_lock)
390 : : {
391 : 240 : table_close(rel, NoLock);
392 : : }
393 : : else
394 : : {
395 : 3 : table_close(rel, RowExclusiveLock);
396 : 3 : UnlockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
397 : : }
398 : 243 : }
399 : :
400 : : /*
401 : : * Update the state of a subscription table.
402 : : */
403 : : void
404 : 890 : UpdateSubscriptionRelState(Oid subid, Oid relid, char state,
405 : : XLogRecPtr sublsn, bool already_locked)
406 : : {
407 : : Relation rel;
408 : : HeapTuple tup;
409 : : bool nulls[Natts_pg_subscription_rel];
410 : : Datum values[Natts_pg_subscription_rel];
411 : : bool replaces[Natts_pg_subscription_rel];
412 : :
413 [ + + ]: 890 : if (already_locked)
414 : : {
415 : : #ifdef USE_ASSERT_CHECKING
416 : : LOCKTAG tag;
417 : :
418 : : Assert(CheckRelationOidLockedByMe(SubscriptionRelRelationId,
419 : : RowExclusiveLock, true));
420 : : SET_LOCKTAG_OBJECT(tag, InvalidOid, SubscriptionRelationId, subid, 0);
421 : : Assert(LockHeldByMe(&tag, AccessShareLock, true));
422 : : #endif
423 : :
424 : 232 : rel = table_open(SubscriptionRelRelationId, NoLock);
425 : : }
426 : : else
427 : : {
428 : 658 : LockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
429 : 658 : rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
430 : : }
431 : :
432 : : /* Try finding existing mapping. */
433 : 890 : tup = SearchSysCacheCopy2(SUBSCRIPTIONRELMAP,
434 : : ObjectIdGetDatum(relid),
435 : : ObjectIdGetDatum(subid));
436 [ - + ]: 890 : if (!HeapTupleIsValid(tup))
437 [ # # ]: 0 : elog(ERROR, "subscription relation %u in subscription %u does not exist",
438 : : relid, subid);
439 : :
440 : : /* Update the tuple. */
441 : 890 : memset(values, 0, sizeof(values));
442 : 890 : memset(nulls, false, sizeof(nulls));
443 : 890 : memset(replaces, false, sizeof(replaces));
444 : :
445 : 890 : replaces[Anum_pg_subscription_rel_srsubstate - 1] = true;
446 : 890 : values[Anum_pg_subscription_rel_srsubstate - 1] = CharGetDatum(state);
447 : :
448 : 890 : replaces[Anum_pg_subscription_rel_srsublsn - 1] = true;
449 [ + + ]: 890 : if (XLogRecPtrIsValid(sublsn))
450 : 438 : values[Anum_pg_subscription_rel_srsublsn - 1] = LSNGetDatum(sublsn);
451 : : else
452 : 452 : nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
453 : :
454 : 890 : tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
455 : : replaces);
456 : :
457 : : /* Update the catalog. */
458 : 890 : CatalogTupleUpdate(rel, &tup->t_self, tup);
459 : :
460 : : /* Cleanup. */
461 : 890 : table_close(rel, NoLock);
462 : 890 : }
463 : :
464 : : /*
465 : : * Get state of subscription table.
466 : : *
467 : : * Returns SUBREL_STATE_UNKNOWN when the table is not in the subscription.
468 : : */
469 : : char
470 : 1417 : GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
471 : : {
472 : : HeapTuple tup;
473 : : char substate;
474 : : bool isnull;
475 : : Datum d;
476 : : Relation rel;
477 : :
478 : : /*
479 : : * This is to avoid the race condition with AlterSubscription which tries
480 : : * to remove this relstate.
481 : : */
482 : 1417 : rel = table_open(SubscriptionRelRelationId, AccessShareLock);
483 : :
484 : : /* Try finding the mapping. */
485 : 1417 : tup = SearchSysCache2(SUBSCRIPTIONRELMAP,
486 : : ObjectIdGetDatum(relid),
487 : : ObjectIdGetDatum(subid));
488 : :
489 [ + + ]: 1417 : if (!HeapTupleIsValid(tup))
490 : : {
491 : 35 : table_close(rel, AccessShareLock);
492 : 35 : *sublsn = InvalidXLogRecPtr;
493 : 35 : return SUBREL_STATE_UNKNOWN;
494 : : }
495 : :
496 : : /* Get the state. */
497 : 1382 : substate = ((Form_pg_subscription_rel) GETSTRUCT(tup))->srsubstate;
498 : :
499 : : /* Get the LSN */
500 : 1382 : d = SysCacheGetAttr(SUBSCRIPTIONRELMAP, tup,
501 : : Anum_pg_subscription_rel_srsublsn, &isnull);
502 [ + + ]: 1382 : if (isnull)
503 : 730 : *sublsn = InvalidXLogRecPtr;
504 : : else
505 : 652 : *sublsn = DatumGetLSN(d);
506 : :
507 : : /* Cleanup */
508 : 1382 : ReleaseSysCache(tup);
509 : :
510 : 1382 : table_close(rel, AccessShareLock);
511 : :
512 : 1382 : return substate;
513 : : }
514 : :
515 : : /*
516 : : * Drop subscription relation mapping. These can be for a particular
517 : : * subscription, or for a particular relation, or both.
518 : : */
519 : : void
520 : 34880 : RemoveSubscriptionRel(Oid subid, Oid relid)
521 : : {
522 : : Relation rel;
523 : : SysScanDesc scan;
524 : : ScanKeyData skey[2];
525 : : HeapTuple tup;
526 : 34880 : int nkeys = 0;
527 : :
528 : 34880 : rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
529 : :
530 [ + + ]: 34880 : if (OidIsValid(relid))
531 : : {
532 : 34694 : ScanKeyInit(&skey[nkeys++],
533 : : Anum_pg_subscription_rel_srrelid,
534 : : BTEqualStrategyNumber,
535 : : F_OIDEQ,
536 : : ObjectIdGetDatum(relid));
537 : : }
538 : :
539 [ + + ]: 34880 : if (OidIsValid(subid))
540 : : {
541 : 212 : ScanKeyInit(&skey[nkeys++],
542 : : Anum_pg_subscription_rel_srsubid,
543 : : BTEqualStrategyNumber,
544 : : F_OIDEQ,
545 : : ObjectIdGetDatum(subid));
546 : : }
547 : :
548 : : /*
549 : : * Do the search and delete what we found. Use the (srrelid, srsubid)
550 : : * index when relid is given; otherwise (DROP SUBSCRIPTION) there is no
551 : : * suitable index, so fall back to a heap scan.
552 : : */
553 : 34880 : scan = systable_beginscan(rel, SubscriptionRelSrrelidSrsubidIndexId,
554 : : OidIsValid(relid), NULL, nkeys, skey);
555 [ + + ]: 35023 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
556 : : {
557 : : Form_pg_subscription_rel subrel;
558 : :
559 : 143 : subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
560 : :
561 : : /*
562 : : * We don't allow to drop the relation mapping when the table
563 : : * synchronization is in progress unless the caller updates the
564 : : * corresponding subscription as well. This is to ensure that we don't
565 : : * leave tablesync slots or origins in the system when the
566 : : * corresponding table is dropped. For sequences, however, it's ok to
567 : : * drop them since no separate slots or origins are created during
568 : : * synchronization.
569 : : */
570 [ + + ]: 143 : if (!OidIsValid(subid) &&
571 [ - + - - ]: 19 : subrel->srsubstate != SUBREL_STATE_READY &&
572 : 0 : get_rel_relkind(subrel->srrelid) != RELKIND_SEQUENCE)
573 : : {
574 [ # # ]: 0 : ereport(ERROR,
575 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
576 : : errmsg("could not drop relation mapping for subscription \"%s\"",
577 : : get_subscription_name(subrel->srsubid, false)),
578 : : errdetail("Table synchronization for relation \"%s\" is in progress and is in state \"%c\".",
579 : : get_rel_name(relid), subrel->srsubstate),
580 : :
581 : : /*
582 : : * translator: first %s is a SQL ALTER command and second %s is a
583 : : * SQL DROP command
584 : : */
585 : : errhint("Use %s to enable subscription if not already enabled or use %s to drop the subscription.",
586 : : "ALTER SUBSCRIPTION ... ENABLE",
587 : : "DROP SUBSCRIPTION ...")));
588 : : }
589 : :
590 : 143 : CatalogTupleDelete(rel, &tup->t_self);
591 : : }
592 : 34880 : systable_endscan(scan);
593 : :
594 : 34880 : table_close(rel, RowExclusiveLock);
595 : 34880 : }
596 : :
597 : : /*
598 : : * Does the subscription have any tables?
599 : : *
600 : : * Use this function only to know true/false, and when you have no need for the
601 : : * List returned by GetSubscriptionRelations.
602 : : */
603 : : bool
604 : 374 : HasSubscriptionTables(Oid subid)
605 : : {
606 : : Relation rel;
607 : : ScanKeyData skey[1];
608 : : SysScanDesc scan;
609 : : HeapTuple tup;
610 : 374 : bool has_subtables = false;
611 : :
612 : 374 : rel = table_open(SubscriptionRelRelationId, AccessShareLock);
613 : :
614 : 374 : ScanKeyInit(&skey[0],
615 : : Anum_pg_subscription_rel_srsubid,
616 : : BTEqualStrategyNumber, F_OIDEQ,
617 : : ObjectIdGetDatum(subid));
618 : :
619 : 374 : scan = systable_beginscan(rel, InvalidOid, false,
620 : : NULL, 1, skey);
621 : :
622 [ + + ]: 465 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
623 : : {
624 : : Form_pg_subscription_rel subrel;
625 : : char relkind;
626 : :
627 : 437 : subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
628 : 437 : relkind = get_rel_relkind(subrel->srrelid);
629 : :
630 [ + + + + ]: 437 : if (relkind == RELKIND_RELATION ||
631 : : relkind == RELKIND_PARTITIONED_TABLE)
632 : : {
633 : 346 : has_subtables = true;
634 : 346 : break;
635 : : }
636 : : }
637 : :
638 : : /* Cleanup */
639 : 374 : systable_endscan(scan);
640 : 374 : table_close(rel, AccessShareLock);
641 : :
642 : 374 : return has_subtables;
643 : : }
644 : :
645 : : /*
646 : : * Get the relations for the subscription.
647 : : *
648 : : * If not_ready is true, return only the relations that are not in a ready
649 : : * state, otherwise return all the relations of the subscription. The
650 : : * returned list is palloc'ed in the current memory context.
651 : : */
652 : : List *
653 : 1375 : GetSubscriptionRelations(Oid subid, bool tables, bool sequences,
654 : : bool not_ready)
655 : : {
656 : 1375 : List *res = NIL;
657 : : Relation rel;
658 : : HeapTuple tup;
659 : 1375 : int nkeys = 0;
660 : : ScanKeyData skey[2];
661 : : SysScanDesc scan;
662 : :
663 : : /* One or both of 'tables' and 'sequences' must be true. */
664 : : Assert(tables || sequences);
665 : :
666 : 1375 : rel = table_open(SubscriptionRelRelationId, AccessShareLock);
667 : :
668 : 1375 : ScanKeyInit(&skey[nkeys++],
669 : : Anum_pg_subscription_rel_srsubid,
670 : : BTEqualStrategyNumber, F_OIDEQ,
671 : : ObjectIdGetDatum(subid));
672 : :
673 [ + + ]: 1375 : if (not_ready)
674 : 1325 : ScanKeyInit(&skey[nkeys++],
675 : : Anum_pg_subscription_rel_srsubstate,
676 : : BTEqualStrategyNumber, F_CHARNE,
677 : : CharGetDatum(SUBREL_STATE_READY));
678 : :
679 : 1375 : scan = systable_beginscan(rel, InvalidOid, false,
680 : : NULL, nkeys, skey);
681 : :
682 [ + + ]: 3385 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
683 : : {
684 : : Form_pg_subscription_rel subrel;
685 : : SubscriptionRelState *relstate;
686 : : Datum d;
687 : : bool isnull;
688 : : char relkind;
689 : :
690 : 2010 : subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
691 : :
692 : 2010 : relkind = get_rel_relkind(subrel->srrelid);
693 : :
694 : : /* The relation may have been dropped concurrently. */
695 [ - + ]: 2010 : if (relkind == '\0')
696 : 0 : continue;
697 : :
698 : : /*
699 : : * The relation must be either a sequence or a table. Anything else
700 : : * indicates an unexpected relation kind for a subscription relation.
701 : : */
702 [ + + ]: 2010 : if (relkind == RELKIND_SEQUENCE)
703 : : {
704 : : /* Skip sequences if they were not requested */
705 [ - + ]: 78 : if (!sequences)
706 : 0 : continue;
707 : : }
708 [ + + + - ]: 1932 : else if (relkind == RELKIND_RELATION ||
709 : : relkind == RELKIND_PARTITIONED_TABLE)
710 : : {
711 : : /* Skip tables if they were not requested */
712 [ - + ]: 1932 : if (!tables)
713 : 0 : continue;
714 : : }
715 : : else
716 [ # # ]: 0 : elog(ERROR, "unexpected relkind \"%c\" for relation %u in subscription %u",
717 : : relkind, subrel->srrelid, subid);
718 : :
719 : 2010 : relstate = palloc_object(SubscriptionRelState);
720 : 2010 : relstate->relid = subrel->srrelid;
721 : 2010 : relstate->state = subrel->srsubstate;
722 : 2010 : d = SysCacheGetAttr(SUBSCRIPTIONRELMAP, tup,
723 : : Anum_pg_subscription_rel_srsublsn, &isnull);
724 [ + + ]: 2010 : if (isnull)
725 : 1633 : relstate->lsn = InvalidXLogRecPtr;
726 : : else
727 : 377 : relstate->lsn = DatumGetLSN(d);
728 : :
729 : 2010 : res = lappend(res, relstate);
730 : : }
731 : :
732 : : /* Cleanup */
733 : 1375 : systable_endscan(scan);
734 : 1375 : table_close(rel, AccessShareLock);
735 : :
736 : 1375 : return res;
737 : : }
738 : :
739 : : /*
740 : : * Update the dead tuple retention status for the given subscription.
741 : : */
742 : : void
743 : 2 : UpdateDeadTupleRetentionStatus(Oid subid, bool active)
744 : : {
745 : : Relation rel;
746 : : bool nulls[Natts_pg_subscription];
747 : : bool replaces[Natts_pg_subscription];
748 : : Datum values[Natts_pg_subscription];
749 : : HeapTuple tup;
750 : :
751 : : /* Look up the subscription in the catalog */
752 : 2 : rel = table_open(SubscriptionRelationId, RowExclusiveLock);
753 : 2 : tup = SearchSysCacheCopy1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
754 : :
755 [ - + ]: 2 : if (!HeapTupleIsValid(tup))
756 [ # # ]: 0 : elog(ERROR, "cache lookup failed for subscription %u", subid);
757 : :
758 : : /* Must only modify subscriptions belonging to the current database. */
759 : : Assert(((Form_pg_subscription) GETSTRUCT(tup))->subdbid == MyDatabaseId);
760 : :
761 : 2 : LockSharedObject(SubscriptionRelationId, subid, 0, AccessShareLock);
762 : :
763 : : /* Form a new tuple. */
764 : 2 : memset(values, 0, sizeof(values));
765 : 2 : memset(nulls, false, sizeof(nulls));
766 : 2 : memset(replaces, false, sizeof(replaces));
767 : :
768 : : /* Set the subscription to disabled. */
769 : 2 : values[Anum_pg_subscription_subretentionactive - 1] = BoolGetDatum(active);
770 : 2 : replaces[Anum_pg_subscription_subretentionactive - 1] = true;
771 : :
772 : : /* Update the catalog */
773 : 2 : tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
774 : : replaces);
775 : 2 : CatalogTupleUpdate(rel, &tup->t_self, tup);
776 : 2 : heap_freetuple(tup);
777 : :
778 : 2 : table_close(rel, NoLock);
779 : 2 : }
|