PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
subscriptioncmds.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * subscriptioncmds.c
4 * subscription catalog manipulation functions
5 *
6 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * IDENTIFICATION
10 * src/backend/commands/subscriptioncmds.c
11 *
12 *-------------------------------------------------------------------------
13 */
14
15#include "postgres.h"
16
17#include "access/htup_details.h"
18#include "access/table.h"
19#include "access/twophase.h"
20#include "access/xact.h"
21#include "catalog/catalog.h"
22#include "catalog/dependency.h"
23#include "catalog/indexing.h"
24#include "catalog/namespace.h"
27#include "catalog/pg_authid_d.h"
28#include "catalog/pg_database_d.h"
31#include "catalog/pg_type.h"
32#include "commands/dbcommands.h"
33#include "commands/defrem.h"
36#include "executor/executor.h"
37#include "miscadmin.h"
38#include "nodes/makefuncs.h"
39#include "pgstat.h"
42#include "replication/origin.h"
43#include "replication/slot.h"
47#include "storage/lmgr.h"
48#include "utils/acl.h"
49#include "utils/builtins.h"
50#include "utils/guc.h"
51#include "utils/lsyscache.h"
52#include "utils/memutils.h"
53#include "utils/pg_lsn.h"
54#include "utils/syscache.h"
55
56/*
57 * Options that can be specified by the user in CREATE/ALTER SUBSCRIPTION
58 * command.
59 */
60#define SUBOPT_CONNECT 0x00000001
61#define SUBOPT_ENABLED 0x00000002
62#define SUBOPT_CREATE_SLOT 0x00000004
63#define SUBOPT_SLOT_NAME 0x00000008
64#define SUBOPT_COPY_DATA 0x00000010
65#define SUBOPT_SYNCHRONOUS_COMMIT 0x00000020
66#define SUBOPT_REFRESH 0x00000040
67#define SUBOPT_BINARY 0x00000080
68#define SUBOPT_STREAMING 0x00000100
69#define SUBOPT_TWOPHASE_COMMIT 0x00000200
70#define SUBOPT_DISABLE_ON_ERR 0x00000400
71#define SUBOPT_PASSWORD_REQUIRED 0x00000800
72#define SUBOPT_RUN_AS_OWNER 0x00001000
73#define SUBOPT_FAILOVER 0x00002000
74#define SUBOPT_LSN 0x00004000
75#define SUBOPT_ORIGIN 0x00008000
76
77/* check if the 'val' has 'bits' set */
78#define IsSet(val, bits) (((val) & (bits)) == (bits))
79
80/*
81 * Structure to hold a bitmap representing the user-provided CREATE/ALTER
82 * SUBSCRIPTION command options and the parsed/default values of each of them.
83 */
84typedef struct SubOpts
85{
87 char *slot_name;
89 bool connect;
90 bool enabled;
93 bool refresh;
94 bool binary;
101 char *origin;
104
105static List *fetch_table_list(WalReceiverConn *wrconn, List *publications);
107 List *publications, bool copydata,
108 char *origin, Oid *subrel_local_oids,
109 int subrel_count, char *subname);
110static void check_duplicates_in_publist(List *publist, Datum *datums);
111static List *merge_publications(List *oldpublist, List *newpublist, bool addpub, const char *subname);
112static void ReportSlotConnectionError(List *rstates, Oid subid, char *slotname, char *err);
113static void CheckAlterSubOption(Subscription *sub, const char *option,
114 bool slot_needs_update, bool isTopLevel);
115
116
117/*
118 * Common option parsing function for CREATE and ALTER SUBSCRIPTION commands.
119 *
120 * Since not all options can be specified in both commands, this function
121 * will report an error if mutually exclusive options are specified.
122 */
123static void
125 bits32 supported_opts, SubOpts *opts)
126{
127 ListCell *lc;
128
129 /* Start out with cleared opts. */
130 memset(opts, 0, sizeof(SubOpts));
131
132 /* caller must expect some option */
133 Assert(supported_opts != 0);
134
135 /* If connect option is supported, these others also need to be. */
136 Assert(!IsSet(supported_opts, SUBOPT_CONNECT) ||
137 IsSet(supported_opts, SUBOPT_ENABLED | SUBOPT_CREATE_SLOT |
139
140 /* Set default values for the supported options. */
141 if (IsSet(supported_opts, SUBOPT_CONNECT))
142 opts->connect = true;
143 if (IsSet(supported_opts, SUBOPT_ENABLED))
144 opts->enabled = true;
145 if (IsSet(supported_opts, SUBOPT_CREATE_SLOT))
146 opts->create_slot = true;
147 if (IsSet(supported_opts, SUBOPT_COPY_DATA))
148 opts->copy_data = true;
149 if (IsSet(supported_opts, SUBOPT_REFRESH))
150 opts->refresh = true;
151 if (IsSet(supported_opts, SUBOPT_BINARY))
152 opts->binary = false;
153 if (IsSet(supported_opts, SUBOPT_STREAMING))
154 opts->streaming = LOGICALREP_STREAM_PARALLEL;
155 if (IsSet(supported_opts, SUBOPT_TWOPHASE_COMMIT))
156 opts->twophase = false;
157 if (IsSet(supported_opts, SUBOPT_DISABLE_ON_ERR))
158 opts->disableonerr = false;
159 if (IsSet(supported_opts, SUBOPT_PASSWORD_REQUIRED))
160 opts->passwordrequired = true;
161 if (IsSet(supported_opts, SUBOPT_RUN_AS_OWNER))
162 opts->runasowner = false;
163 if (IsSet(supported_opts, SUBOPT_FAILOVER))
164 opts->failover = false;
165 if (IsSet(supported_opts, SUBOPT_ORIGIN))
166 opts->origin = pstrdup(LOGICALREP_ORIGIN_ANY);
167
168 /* Parse options */
169 foreach(lc, stmt_options)
170 {
171 DefElem *defel = (DefElem *) lfirst(lc);
172
173 if (IsSet(supported_opts, SUBOPT_CONNECT) &&
174 strcmp(defel->defname, "connect") == 0)
175 {
176 if (IsSet(opts->specified_opts, SUBOPT_CONNECT))
177 errorConflictingDefElem(defel, pstate);
178
179 opts->specified_opts |= SUBOPT_CONNECT;
180 opts->connect = defGetBoolean(defel);
181 }
182 else if (IsSet(supported_opts, SUBOPT_ENABLED) &&
183 strcmp(defel->defname, "enabled") == 0)
184 {
185 if (IsSet(opts->specified_opts, SUBOPT_ENABLED))
186 errorConflictingDefElem(defel, pstate);
187
188 opts->specified_opts |= SUBOPT_ENABLED;
189 opts->enabled = defGetBoolean(defel);
190 }
191 else if (IsSet(supported_opts, SUBOPT_CREATE_SLOT) &&
192 strcmp(defel->defname, "create_slot") == 0)
193 {
194 if (IsSet(opts->specified_opts, SUBOPT_CREATE_SLOT))
195 errorConflictingDefElem(defel, pstate);
196
197 opts->specified_opts |= SUBOPT_CREATE_SLOT;
198 opts->create_slot = defGetBoolean(defel);
199 }
200 else if (IsSet(supported_opts, SUBOPT_SLOT_NAME) &&
201 strcmp(defel->defname, "slot_name") == 0)
202 {
203 if (IsSet(opts->specified_opts, SUBOPT_SLOT_NAME))
204 errorConflictingDefElem(defel, pstate);
205
206 opts->specified_opts |= SUBOPT_SLOT_NAME;
207 opts->slot_name = defGetString(defel);
208
209 /* Setting slot_name = NONE is treated as no slot name. */
210 if (strcmp(opts->slot_name, "none") == 0)
211 opts->slot_name = NULL;
212 else
214 }
215 else if (IsSet(supported_opts, SUBOPT_COPY_DATA) &&
216 strcmp(defel->defname, "copy_data") == 0)
217 {
218 if (IsSet(opts->specified_opts, SUBOPT_COPY_DATA))
219 errorConflictingDefElem(defel, pstate);
220
221 opts->specified_opts |= SUBOPT_COPY_DATA;
222 opts->copy_data = defGetBoolean(defel);
223 }
224 else if (IsSet(supported_opts, SUBOPT_SYNCHRONOUS_COMMIT) &&
225 strcmp(defel->defname, "synchronous_commit") == 0)
226 {
227 if (IsSet(opts->specified_opts, SUBOPT_SYNCHRONOUS_COMMIT))
228 errorConflictingDefElem(defel, pstate);
229
230 opts->specified_opts |= SUBOPT_SYNCHRONOUS_COMMIT;
231 opts->synchronous_commit = defGetString(defel);
232
233 /* Test if the given value is valid for synchronous_commit GUC. */
234 (void) set_config_option("synchronous_commit", opts->synchronous_commit,
236 false, 0, false);
237 }
238 else if (IsSet(supported_opts, SUBOPT_REFRESH) &&
239 strcmp(defel->defname, "refresh") == 0)
240 {
241 if (IsSet(opts->specified_opts, SUBOPT_REFRESH))
242 errorConflictingDefElem(defel, pstate);
243
244 opts->specified_opts |= SUBOPT_REFRESH;
245 opts->refresh = defGetBoolean(defel);
246 }
247 else if (IsSet(supported_opts, SUBOPT_BINARY) &&
248 strcmp(defel->defname, "binary") == 0)
249 {
250 if (IsSet(opts->specified_opts, SUBOPT_BINARY))
251 errorConflictingDefElem(defel, pstate);
252
253 opts->specified_opts |= SUBOPT_BINARY;
254 opts->binary = defGetBoolean(defel);
255 }
256 else if (IsSet(supported_opts, SUBOPT_STREAMING) &&
257 strcmp(defel->defname, "streaming") == 0)
258 {
259 if (IsSet(opts->specified_opts, SUBOPT_STREAMING))
260 errorConflictingDefElem(defel, pstate);
261
262 opts->specified_opts |= SUBOPT_STREAMING;
263 opts->streaming = defGetStreamingMode(defel);
264 }
265 else if (IsSet(supported_opts, SUBOPT_TWOPHASE_COMMIT) &&
266 strcmp(defel->defname, "two_phase") == 0)
267 {
268 if (IsSet(opts->specified_opts, SUBOPT_TWOPHASE_COMMIT))
269 errorConflictingDefElem(defel, pstate);
270
271 opts->specified_opts |= SUBOPT_TWOPHASE_COMMIT;
272 opts->twophase = defGetBoolean(defel);
273 }
274 else if (IsSet(supported_opts, SUBOPT_DISABLE_ON_ERR) &&
275 strcmp(defel->defname, "disable_on_error") == 0)
276 {
277 if (IsSet(opts->specified_opts, SUBOPT_DISABLE_ON_ERR))
278 errorConflictingDefElem(defel, pstate);
279
280 opts->specified_opts |= SUBOPT_DISABLE_ON_ERR;
281 opts->disableonerr = defGetBoolean(defel);
282 }
283 else if (IsSet(supported_opts, SUBOPT_PASSWORD_REQUIRED) &&
284 strcmp(defel->defname, "password_required") == 0)
285 {
286 if (IsSet(opts->specified_opts, SUBOPT_PASSWORD_REQUIRED))
287 errorConflictingDefElem(defel, pstate);
288
289 opts->specified_opts |= SUBOPT_PASSWORD_REQUIRED;
290 opts->passwordrequired = defGetBoolean(defel);
291 }
292 else if (IsSet(supported_opts, SUBOPT_RUN_AS_OWNER) &&
293 strcmp(defel->defname, "run_as_owner") == 0)
294 {
295 if (IsSet(opts->specified_opts, SUBOPT_RUN_AS_OWNER))
296 errorConflictingDefElem(defel, pstate);
297
298 opts->specified_opts |= SUBOPT_RUN_AS_OWNER;
299 opts->runasowner = defGetBoolean(defel);
300 }
301 else if (IsSet(supported_opts, SUBOPT_FAILOVER) &&
302 strcmp(defel->defname, "failover") == 0)
303 {
304 if (IsSet(opts->specified_opts, SUBOPT_FAILOVER))
305 errorConflictingDefElem(defel, pstate);
306
307 opts->specified_opts |= SUBOPT_FAILOVER;
308 opts->failover = defGetBoolean(defel);
309 }
310 else if (IsSet(supported_opts, SUBOPT_ORIGIN) &&
311 strcmp(defel->defname, "origin") == 0)
312 {
313 if (IsSet(opts->specified_opts, SUBOPT_ORIGIN))
314 errorConflictingDefElem(defel, pstate);
315
316 opts->specified_opts |= SUBOPT_ORIGIN;
317 pfree(opts->origin);
318
319 /*
320 * Even though the "origin" parameter allows only "none" and "any"
321 * values, it is implemented as a string type so that the
322 * parameter can be extended in future versions to support
323 * filtering using origin names specified by the user.
324 */
325 opts->origin = defGetString(defel);
326
327 if ((pg_strcasecmp(opts->origin, LOGICALREP_ORIGIN_NONE) != 0) &&
328 (pg_strcasecmp(opts->origin, LOGICALREP_ORIGIN_ANY) != 0))
330 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
331 errmsg("unrecognized origin value: \"%s\"", opts->origin));
332 }
333 else if (IsSet(supported_opts, SUBOPT_LSN) &&
334 strcmp(defel->defname, "lsn") == 0)
335 {
336 char *lsn_str = defGetString(defel);
337 XLogRecPtr lsn;
338
339 if (IsSet(opts->specified_opts, SUBOPT_LSN))
340 errorConflictingDefElem(defel, pstate);
341
342 /* Setting lsn = NONE is treated as resetting LSN */
343 if (strcmp(lsn_str, "none") == 0)
344 lsn = InvalidXLogRecPtr;
345 else
346 {
347 /* Parse the argument as LSN */
349 CStringGetDatum(lsn_str)));
350
351 if (XLogRecPtrIsInvalid(lsn))
353 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
354 errmsg("invalid WAL location (LSN): %s", lsn_str)));
355 }
356
357 opts->specified_opts |= SUBOPT_LSN;
358 opts->lsn = lsn;
359 }
360 else
362 (errcode(ERRCODE_SYNTAX_ERROR),
363 errmsg("unrecognized subscription parameter: \"%s\"", defel->defname)));
364 }
365
366 /*
367 * We've been explicitly asked to not connect, that requires some
368 * additional processing.
369 */
370 if (!opts->connect && IsSet(supported_opts, SUBOPT_CONNECT))
371 {
372 /* Check for incompatible options from the user. */
373 if (opts->enabled &&
374 IsSet(opts->specified_opts, SUBOPT_ENABLED))
376 (errcode(ERRCODE_SYNTAX_ERROR),
377 /*- translator: both %s are strings of the form "option = value" */
378 errmsg("%s and %s are mutually exclusive options",
379 "connect = false", "enabled = true")));
380
381 if (opts->create_slot &&
382 IsSet(opts->specified_opts, SUBOPT_CREATE_SLOT))
384 (errcode(ERRCODE_SYNTAX_ERROR),
385 errmsg("%s and %s are mutually exclusive options",
386 "connect = false", "create_slot = true")));
387
388 if (opts->copy_data &&
389 IsSet(opts->specified_opts, SUBOPT_COPY_DATA))
391 (errcode(ERRCODE_SYNTAX_ERROR),
392 errmsg("%s and %s are mutually exclusive options",
393 "connect = false", "copy_data = true")));
394
395 /* Change the defaults of other options. */
396 opts->enabled = false;
397 opts->create_slot = false;
398 opts->copy_data = false;
399 }
400
401 /*
402 * Do additional checking for disallowed combination when slot_name = NONE
403 * was used.
404 */
405 if (!opts->slot_name &&
406 IsSet(opts->specified_opts, SUBOPT_SLOT_NAME))
407 {
408 if (opts->enabled)
409 {
410 if (IsSet(opts->specified_opts, SUBOPT_ENABLED))
412 (errcode(ERRCODE_SYNTAX_ERROR),
413 /*- translator: both %s are strings of the form "option = value" */
414 errmsg("%s and %s are mutually exclusive options",
415 "slot_name = NONE", "enabled = true")));
416 else
418 (errcode(ERRCODE_SYNTAX_ERROR),
419 /*- translator: both %s are strings of the form "option = value" */
420 errmsg("subscription with %s must also set %s",
421 "slot_name = NONE", "enabled = false")));
422 }
423
424 if (opts->create_slot)
425 {
426 if (IsSet(opts->specified_opts, SUBOPT_CREATE_SLOT))
428 (errcode(ERRCODE_SYNTAX_ERROR),
429 /*- translator: both %s are strings of the form "option = value" */
430 errmsg("%s and %s are mutually exclusive options",
431 "slot_name = NONE", "create_slot = true")));
432 else
434 (errcode(ERRCODE_SYNTAX_ERROR),
435 /*- translator: both %s are strings of the form "option = value" */
436 errmsg("subscription with %s must also set %s",
437 "slot_name = NONE", "create_slot = false")));
438 }
439 }
440}
441
442/*
443 * Check that the specified publications are present on the publisher.
444 */
445static void
447{
449 StringInfo cmd;
450 TupleTableSlot *slot;
451 List *publicationsCopy = NIL;
452 Oid tableRow[1] = {TEXTOID};
453
454 cmd = makeStringInfo();
455 appendStringInfoString(cmd, "SELECT t.pubname FROM\n"
456 " pg_catalog.pg_publication t WHERE\n"
457 " t.pubname IN (");
458 GetPublicationsStr(publications, cmd, true);
459 appendStringInfoChar(cmd, ')');
460
461 res = walrcv_exec(wrconn, cmd->data, 1, tableRow);
463
464 if (res->status != WALRCV_OK_TUPLES)
466 errmsg("could not receive list of publications from the publisher: %s",
467 res->err));
468
469 publicationsCopy = list_copy(publications);
470
471 /* Process publication(s). */
473 while (tuplestore_gettupleslot(res->tuplestore, true, false, slot))
474 {
475 char *pubname;
476 bool isnull;
477
478 pubname = TextDatumGetCString(slot_getattr(slot, 1, &isnull));
479 Assert(!isnull);
480
481 /* Delete the publication present in publisher from the list. */
482 publicationsCopy = list_delete(publicationsCopy, makeString(pubname));
483 ExecClearTuple(slot);
484 }
485
487
489
490 if (list_length(publicationsCopy))
491 {
492 /* Prepare the list of non-existent publication(s) for error message. */
493 StringInfo pubnames = makeStringInfo();
494
495 GetPublicationsStr(publicationsCopy, pubnames, false);
497 errcode(ERRCODE_UNDEFINED_OBJECT),
498 errmsg_plural("publication %s does not exist on the publisher",
499 "publications %s do not exist on the publisher",
500 list_length(publicationsCopy),
501 pubnames->data));
502 }
503}
504
505/*
506 * Auxiliary function to build a text array out of a list of String nodes.
507 */
508static Datum
510{
511 ArrayType *arr;
512 Datum *datums;
513 MemoryContext memcxt;
514 MemoryContext oldcxt;
515
516 /* Create memory context for temporary allocations. */
518 "publicationListToArray to array",
520 oldcxt = MemoryContextSwitchTo(memcxt);
521
522 datums = (Datum *) palloc(sizeof(Datum) * list_length(publist));
523
524 check_duplicates_in_publist(publist, datums);
525
526 MemoryContextSwitchTo(oldcxt);
527
528 arr = construct_array_builtin(datums, list_length(publist), TEXTOID);
529
530 MemoryContextDelete(memcxt);
531
532 return PointerGetDatum(arr);
533}
534
535/*
536 * Create new subscription.
537 */
540 bool isTopLevel)
541{
542 Relation rel;
543 ObjectAddress myself;
544 Oid subid;
545 bool nulls[Natts_pg_subscription];
546 Datum values[Natts_pg_subscription];
547 Oid owner = GetUserId();
548 HeapTuple tup;
549 char *conninfo;
550 char originname[NAMEDATALEN];
551 List *publications;
552 bits32 supported_opts;
553 SubOpts opts = {0};
554 AclResult aclresult;
555
556 /*
557 * Parse and check options.
558 *
559 * Connection and publication should not be specified here.
560 */
561 supported_opts = (SUBOPT_CONNECT | SUBOPT_ENABLED | SUBOPT_CREATE_SLOT |
567 parse_subscription_options(pstate, stmt->options, supported_opts, &opts);
568
569 /*
570 * Since creating a replication slot is not transactional, rolling back
571 * the transaction leaves the created replication slot. So we cannot run
572 * CREATE SUBSCRIPTION inside a transaction block if creating a
573 * replication slot.
574 */
575 if (opts.create_slot)
576 PreventInTransactionBlock(isTopLevel, "CREATE SUBSCRIPTION ... WITH (create_slot = true)");
577
578 /*
579 * We don't want to allow unprivileged users to be able to trigger
580 * attempts to access arbitrary network destinations, so require the user
581 * to have been specifically authorized to create subscriptions.
582 */
583 if (!has_privs_of_role(owner, ROLE_PG_CREATE_SUBSCRIPTION))
585 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
586 errmsg("permission denied to create subscription"),
587 errdetail("Only roles with privileges of the \"%s\" role may create subscriptions.",
588 "pg_create_subscription")));
589
590 /*
591 * Since a subscription is a database object, we also check for CREATE
592 * permission on the database.
593 */
594 aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId,
595 owner, ACL_CREATE);
596 if (aclresult != ACLCHECK_OK)
599
600 /*
601 * Non-superusers are required to set a password for authentication, and
602 * that password must be used by the target server, but the superuser can
603 * exempt a subscription from this requirement.
604 */
605 if (!opts.passwordrequired && !superuser_arg(owner))
607 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
608 errmsg("password_required=false is superuser-only"),
609 errhint("Subscriptions with the password_required option set to false may only be created or modified by the superuser.")));
610
611 /*
612 * If built with appropriate switch, whine when regression-testing
613 * conventions for subscription names are violated.
614 */
615#ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
616 if (strncmp(stmt->subname, "regress_", 8) != 0)
617 elog(WARNING, "subscriptions created by regression test cases should have names starting with \"regress_\"");
618#endif
619
620 rel = table_open(SubscriptionRelationId, RowExclusiveLock);
621
622 /* Check if name is used */
623 subid = GetSysCacheOid2(SUBSCRIPTIONNAME, Anum_pg_subscription_oid,
625 if (OidIsValid(subid))
626 {
629 errmsg("subscription \"%s\" already exists",
630 stmt->subname)));
631 }
632
633 if (!IsSet(opts.specified_opts, SUBOPT_SLOT_NAME) &&
634 opts.slot_name == NULL)
635 opts.slot_name = stmt->subname;
636
637 /* The default for synchronous_commit of subscriptions is off. */
638 if (opts.synchronous_commit == NULL)
639 opts.synchronous_commit = "off";
640
641 conninfo = stmt->conninfo;
642 publications = stmt->publication;
643
644 /* Load the library providing us libpq calls. */
645 load_file("libpqwalreceiver", false);
646
647 /* Check the connection info string. */
648 walrcv_check_conninfo(conninfo, opts.passwordrequired && !superuser());
649
650 /* Everything ok, form a new tuple. */
651 memset(values, 0, sizeof(values));
652 memset(nulls, false, sizeof(nulls));
653
654 subid = GetNewOidWithIndex(rel, SubscriptionObjectIndexId,
655 Anum_pg_subscription_oid);
656 values[Anum_pg_subscription_oid - 1] = ObjectIdGetDatum(subid);
657 values[Anum_pg_subscription_subdbid - 1] = ObjectIdGetDatum(MyDatabaseId);
658 values[Anum_pg_subscription_subskiplsn - 1] = LSNGetDatum(InvalidXLogRecPtr);
659 values[Anum_pg_subscription_subname - 1] =
661 values[Anum_pg_subscription_subowner - 1] = ObjectIdGetDatum(owner);
662 values[Anum_pg_subscription_subenabled - 1] = BoolGetDatum(opts.enabled);
663 values[Anum_pg_subscription_subbinary - 1] = BoolGetDatum(opts.binary);
664 values[Anum_pg_subscription_substream - 1] = CharGetDatum(opts.streaming);
665 values[Anum_pg_subscription_subtwophasestate - 1] =
666 CharGetDatum(opts.twophase ?
667 LOGICALREP_TWOPHASE_STATE_PENDING :
668 LOGICALREP_TWOPHASE_STATE_DISABLED);
669 values[Anum_pg_subscription_subdisableonerr - 1] = BoolGetDatum(opts.disableonerr);
670 values[Anum_pg_subscription_subpasswordrequired - 1] = BoolGetDatum(opts.passwordrequired);
671 values[Anum_pg_subscription_subrunasowner - 1] = BoolGetDatum(opts.runasowner);
672 values[Anum_pg_subscription_subfailover - 1] = BoolGetDatum(opts.failover);
673 values[Anum_pg_subscription_subconninfo - 1] =
674 CStringGetTextDatum(conninfo);
675 if (opts.slot_name)
676 values[Anum_pg_subscription_subslotname - 1] =
678 else
679 nulls[Anum_pg_subscription_subslotname - 1] = true;
680 values[Anum_pg_subscription_subsynccommit - 1] =
681 CStringGetTextDatum(opts.synchronous_commit);
682 values[Anum_pg_subscription_subpublications - 1] =
683 publicationListToArray(publications);
684 values[Anum_pg_subscription_suborigin - 1] =
686
687 tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
688
689 /* Insert tuple into catalog. */
690 CatalogTupleInsert(rel, tup);
691 heap_freetuple(tup);
692
693 recordDependencyOnOwner(SubscriptionRelationId, subid, owner);
694
695 ReplicationOriginNameForLogicalRep(subid, InvalidOid, originname, sizeof(originname));
696 replorigin_create(originname);
697
698 /*
699 * Connect to remote side to execute requested commands and fetch table
700 * info.
701 */
702 if (opts.connect)
703 {
704 char *err;
706 List *tables;
707 ListCell *lc;
708 char table_state;
709 bool must_use_password;
710
711 /* Try to connect to the publisher. */
712 must_use_password = !superuser_arg(owner) && opts.passwordrequired;
713 wrconn = walrcv_connect(conninfo, true, true, must_use_password,
714 stmt->subname, &err);
715 if (!wrconn)
717 (errcode(ERRCODE_CONNECTION_FAILURE),
718 errmsg("subscription \"%s\" could not connect to the publisher: %s",
719 stmt->subname, err)));
720
721 PG_TRY();
722 {
723 check_publications(wrconn, publications);
724 check_publications_origin(wrconn, publications, opts.copy_data,
725 opts.origin, NULL, 0, stmt->subname);
726
727 /*
728 * Set sync state based on if we were asked to do data copy or
729 * not.
730 */
731 table_state = opts.copy_data ? SUBREL_STATE_INIT : SUBREL_STATE_READY;
732
733 /*
734 * Get the table list from publisher and build local table status
735 * info.
736 */
737 tables = fetch_table_list(wrconn, publications);
738 foreach(lc, tables)
739 {
740 RangeVar *rv = (RangeVar *) lfirst(lc);
741 Oid relid;
742
743 relid = RangeVarGetRelid(rv, AccessShareLock, false);
744
745 /* Check for supported relkind. */
747 rv->schemaname, rv->relname);
748
749 AddSubscriptionRelState(subid, relid, table_state,
750 InvalidXLogRecPtr, true);
751 }
752
753 /*
754 * If requested, create permanent slot for the subscription. We
755 * won't use the initial snapshot for anything, so no need to
756 * export it.
757 */
758 if (opts.create_slot)
759 {
760 bool twophase_enabled = false;
761
762 Assert(opts.slot_name);
763
764 /*
765 * Even if two_phase is set, don't create the slot with
766 * two-phase enabled. Will enable it once all the tables are
767 * synced and ready. This avoids race-conditions like prepared
768 * transactions being skipped due to changes not being applied
769 * due to checks in should_apply_changes_for_rel() when
770 * tablesync for the corresponding tables are in progress. See
771 * comments atop worker.c.
772 *
773 * Note that if tables were specified but copy_data is false
774 * then it is safe to enable two_phase up-front because those
775 * tables are already initially in READY state. When the
776 * subscription has no tables, we leave the twophase state as
777 * PENDING, to allow ALTER SUBSCRIPTION ... REFRESH
778 * PUBLICATION to work.
779 */
780 if (opts.twophase && !opts.copy_data && tables != NIL)
781 twophase_enabled = true;
782
783 walrcv_create_slot(wrconn, opts.slot_name, false, twophase_enabled,
784 opts.failover, CRS_NOEXPORT_SNAPSHOT, NULL);
785
786 if (twophase_enabled)
787 UpdateTwoPhaseState(subid, LOGICALREP_TWOPHASE_STATE_ENABLED);
788
790 (errmsg("created replication slot \"%s\" on publisher",
791 opts.slot_name)));
792 }
793 }
794 PG_FINALLY();
795 {
797 }
798 PG_END_TRY();
799 }
800 else
802 (errmsg("subscription was created, but is not connected"),
803 errhint("To initiate replication, you must manually create the replication slot, enable the subscription, and refresh the subscription.")));
804
806
808
809 if (opts.enabled)
811
812 ObjectAddressSet(myself, SubscriptionRelationId, subid);
813
814 InvokeObjectPostCreateHook(SubscriptionRelationId, subid, 0);
815
816 return myself;
817}
818
819static void
821 List *validate_publications)
822{
823 char *err;
824 List *pubrel_names;
825 List *subrel_states;
826 Oid *subrel_local_oids;
827 Oid *pubrel_local_oids;
828 ListCell *lc;
829 int off;
830 int remove_rel_len;
831 int subrel_count;
832 Relation rel = NULL;
833 typedef struct SubRemoveRels
834 {
835 Oid relid;
836 char state;
837 } SubRemoveRels;
838 SubRemoveRels *sub_remove_rels;
840 bool must_use_password;
841
842 /* Load the library providing us libpq calls. */
843 load_file("libpqwalreceiver", false);
844
845 /* Try to connect to the publisher. */
846 must_use_password = sub->passwordrequired && !sub->ownersuperuser;
847 wrconn = walrcv_connect(sub->conninfo, true, true, must_use_password,
848 sub->name, &err);
849 if (!wrconn)
851 (errcode(ERRCODE_CONNECTION_FAILURE),
852 errmsg("subscription \"%s\" could not connect to the publisher: %s",
853 sub->name, err)));
854
855 PG_TRY();
856 {
857 if (validate_publications)
858 check_publications(wrconn, validate_publications);
859
860 /* Get the table list from publisher. */
861 pubrel_names = fetch_table_list(wrconn, sub->publications);
862
863 /* Get local table list. */
864 subrel_states = GetSubscriptionRelations(sub->oid, false);
865 subrel_count = list_length(subrel_states);
866
867 /*
868 * Build qsorted array of local table oids for faster lookup. This can
869 * potentially contain all tables in the database so speed of lookup
870 * is important.
871 */
872 subrel_local_oids = palloc(subrel_count * sizeof(Oid));
873 off = 0;
874 foreach(lc, subrel_states)
875 {
877
878 subrel_local_oids[off++] = relstate->relid;
879 }
880 qsort(subrel_local_oids, subrel_count,
881 sizeof(Oid), oid_cmp);
882
884 sub->origin, subrel_local_oids,
885 subrel_count, sub->name);
886
887 /*
888 * Rels that we want to remove from subscription and drop any slots
889 * and origins corresponding to them.
890 */
891 sub_remove_rels = palloc(subrel_count * sizeof(SubRemoveRels));
892
893 /*
894 * Walk over the remote tables and try to match them to locally known
895 * tables. If the table is not known locally create a new state for
896 * it.
897 *
898 * Also builds array of local oids of remote tables for the next step.
899 */
900 off = 0;
901 pubrel_local_oids = palloc(list_length(pubrel_names) * sizeof(Oid));
902
903 foreach(lc, pubrel_names)
904 {
905 RangeVar *rv = (RangeVar *) lfirst(lc);
906 Oid relid;
907
908 relid = RangeVarGetRelid(rv, AccessShareLock, false);
909
910 /* Check for supported relkind. */
912 rv->schemaname, rv->relname);
913
914 pubrel_local_oids[off++] = relid;
915
916 if (!bsearch(&relid, subrel_local_oids,
917 subrel_count, sizeof(Oid), oid_cmp))
918 {
919 AddSubscriptionRelState(sub->oid, relid,
920 copy_data ? SUBREL_STATE_INIT : SUBREL_STATE_READY,
921 InvalidXLogRecPtr, true);
923 (errmsg_internal("table \"%s.%s\" added to subscription \"%s\"",
924 rv->schemaname, rv->relname, sub->name)));
925 }
926 }
927
928 /*
929 * Next remove state for tables we should not care about anymore using
930 * the data we collected above
931 */
932 qsort(pubrel_local_oids, list_length(pubrel_names),
933 sizeof(Oid), oid_cmp);
934
935 remove_rel_len = 0;
936 for (off = 0; off < subrel_count; off++)
937 {
938 Oid relid = subrel_local_oids[off];
939
940 if (!bsearch(&relid, pubrel_local_oids,
941 list_length(pubrel_names), sizeof(Oid), oid_cmp))
942 {
943 char state;
944 XLogRecPtr statelsn;
945
946 /*
947 * Lock pg_subscription_rel with AccessExclusiveLock to
948 * prevent any race conditions with the apply worker
949 * re-launching workers at the same time this code is trying
950 * to remove those tables.
951 *
952 * Even if new worker for this particular rel is restarted it
953 * won't be able to make any progress as we hold exclusive
954 * lock on pg_subscription_rel till the transaction end. It
955 * will simply exit as there is no corresponding rel entry.
956 *
957 * This locking also ensures that the state of rels won't
958 * change till we are done with this refresh operation.
959 */
960 if (!rel)
961 rel = table_open(SubscriptionRelRelationId, AccessExclusiveLock);
962
963 /* Last known rel state. */
964 state = GetSubscriptionRelState(sub->oid, relid, &statelsn);
965
966 sub_remove_rels[remove_rel_len].relid = relid;
967 sub_remove_rels[remove_rel_len++].state = state;
968
969 RemoveSubscriptionRel(sub->oid, relid);
970
971 logicalrep_worker_stop(sub->oid, relid);
972
973 /*
974 * For READY state, we would have already dropped the
975 * tablesync origin.
976 */
977 if (state != SUBREL_STATE_READY)
978 {
979 char originname[NAMEDATALEN];
980
981 /*
982 * Drop the tablesync's origin tracking if exists.
983 *
984 * It is possible that the origin is not yet created for
985 * tablesync worker, this can happen for the states before
986 * SUBREL_STATE_FINISHEDCOPY. The tablesync worker or
987 * apply worker can also concurrently try to drop the
988 * origin and by this time the origin might be already
989 * removed. For these reasons, passing missing_ok = true.
990 */
991 ReplicationOriginNameForLogicalRep(sub->oid, relid, originname,
992 sizeof(originname));
993 replorigin_drop_by_name(originname, true, false);
994 }
995
997 (errmsg_internal("table \"%s.%s\" removed from subscription \"%s\"",
999 get_rel_name(relid),
1000 sub->name)));
1001 }
1002 }
1003
1004 /*
1005 * Drop the tablesync slots associated with removed tables. This has
1006 * to be at the end because otherwise if there is an error while doing
1007 * the database operations we won't be able to rollback dropped slots.
1008 */
1009 for (off = 0; off < remove_rel_len; off++)
1010 {
1011 if (sub_remove_rels[off].state != SUBREL_STATE_READY &&
1012 sub_remove_rels[off].state != SUBREL_STATE_SYNCDONE)
1013 {
1014 char syncslotname[NAMEDATALEN] = {0};
1015
1016 /*
1017 * For READY/SYNCDONE states we know the tablesync slot has
1018 * already been dropped by the tablesync worker.
1019 *
1020 * For other states, there is no certainty, maybe the slot
1021 * does not exist yet. Also, if we fail after removing some of
1022 * the slots, next time, it will again try to drop already
1023 * dropped slots and fail. For these reasons, we allow
1024 * missing_ok = true for the drop.
1025 */
1026 ReplicationSlotNameForTablesync(sub->oid, sub_remove_rels[off].relid,
1027 syncslotname, sizeof(syncslotname));
1028 ReplicationSlotDropAtPubNode(wrconn, syncslotname, true);
1029 }
1030 }
1031 }
1032 PG_FINALLY();
1033 {
1035 }
1036 PG_END_TRY();
1037
1038 if (rel)
1039 table_close(rel, NoLock);
1040}
1041
1042/*
1043 * Common checks for altering failover and two_phase options.
1044 */
1045static void
1047 bool slot_needs_update, bool isTopLevel)
1048{
1049 /*
1050 * The checks in this function are required only for failover and
1051 * two_phase options.
1052 */
1053 Assert(strcmp(option, "failover") == 0 ||
1054 strcmp(option, "two_phase") == 0);
1055
1056 /*
1057 * Do not allow changing the option if the subscription is enabled. This
1058 * is because both failover and two_phase options of the slot on the
1059 * publisher cannot be modified if the slot is currently acquired by the
1060 * existing walsender.
1061 *
1062 * Note that two_phase is enabled (aka changed from 'false' to 'true') on
1063 * the publisher by the existing walsender, so we could have allowed that
1064 * even when the subscription is enabled. But we kept this restriction for
1065 * the sake of consistency and simplicity.
1066 */
1067 if (sub->enabled)
1068 ereport(ERROR,
1069 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1070 errmsg("cannot set option \"%s\" for enabled subscription",
1071 option)));
1072
1073 if (slot_needs_update)
1074 {
1075 StringInfoData cmd;
1076
1077 /*
1078 * A valid slot must be associated with the subscription for us to
1079 * modify any of the slot's properties.
1080 */
1081 if (!sub->slotname)
1082 ereport(ERROR,
1083 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1084 errmsg("cannot set option \"%s\" for a subscription that does not have a slot name",
1085 option)));
1086
1087 /* The changed option of the slot can't be rolled back. */
1088 initStringInfo(&cmd);
1089 appendStringInfo(&cmd, "ALTER SUBSCRIPTION ... SET (%s)", option);
1090
1091 PreventInTransactionBlock(isTopLevel, cmd.data);
1092 pfree(cmd.data);
1093 }
1094}
1095
1096/*
1097 * Alter the existing subscription.
1098 */
1101 bool isTopLevel)
1102{
1103 Relation rel;
1104 ObjectAddress myself;
1105 bool nulls[Natts_pg_subscription];
1106 bool replaces[Natts_pg_subscription];
1107 Datum values[Natts_pg_subscription];
1108 HeapTuple tup;
1109 Oid subid;
1110 bool update_tuple = false;
1111 bool update_failover = false;
1112 bool update_two_phase = false;
1113 Subscription *sub;
1115 bits32 supported_opts;
1116 SubOpts opts = {0};
1117
1118 rel = table_open(SubscriptionRelationId, RowExclusiveLock);
1119
1120 /* Fetch the existing tuple. */
1121 tup = SearchSysCacheCopy2(SUBSCRIPTIONNAME, MyDatabaseId,
1122 CStringGetDatum(stmt->subname));
1123
1124 if (!HeapTupleIsValid(tup))
1125 ereport(ERROR,
1126 (errcode(ERRCODE_UNDEFINED_OBJECT),
1127 errmsg("subscription \"%s\" does not exist",
1128 stmt->subname)));
1129
1130 form = (Form_pg_subscription) GETSTRUCT(tup);
1131 subid = form->oid;
1132
1133 /* must be owner */
1134 if (!object_ownercheck(SubscriptionRelationId, subid, GetUserId()))
1136 stmt->subname);
1137
1138 sub = GetSubscription(subid, false);
1139
1140 /*
1141 * Don't allow non-superuser modification of a subscription with
1142 * password_required=false.
1143 */
1144 if (!sub->passwordrequired && !superuser())
1145 ereport(ERROR,
1146 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1147 errmsg("password_required=false is superuser-only"),
1148 errhint("Subscriptions with the password_required option set to false may only be created or modified by the superuser.")));
1149
1150 /* Lock the subscription so nobody else can do anything with it. */
1151 LockSharedObject(SubscriptionRelationId, subid, 0, AccessExclusiveLock);
1152
1153 /* Form a new tuple. */
1154 memset(values, 0, sizeof(values));
1155 memset(nulls, false, sizeof(nulls));
1156 memset(replaces, false, sizeof(replaces));
1157
1158 switch (stmt->kind)
1159 {
1161 {
1162 supported_opts = (SUBOPT_SLOT_NAME |
1169
1170 parse_subscription_options(pstate, stmt->options,
1171 supported_opts, &opts);
1172
1173 if (IsSet(opts.specified_opts, SUBOPT_SLOT_NAME))
1174 {
1175 /*
1176 * The subscription must be disabled to allow slot_name as
1177 * 'none', otherwise, the apply worker will repeatedly try
1178 * to stream the data using that slot_name which neither
1179 * exists on the publisher nor the user will be allowed to
1180 * create it.
1181 */
1182 if (sub->enabled && !opts.slot_name)
1183 ereport(ERROR,
1184 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1185 errmsg("cannot set %s for enabled subscription",
1186 "slot_name = NONE")));
1187
1188 if (opts.slot_name)
1189 values[Anum_pg_subscription_subslotname - 1] =
1191 else
1192 nulls[Anum_pg_subscription_subslotname - 1] = true;
1193 replaces[Anum_pg_subscription_subslotname - 1] = true;
1194 }
1195
1196 if (opts.synchronous_commit)
1197 {
1198 values[Anum_pg_subscription_subsynccommit - 1] =
1199 CStringGetTextDatum(opts.synchronous_commit);
1200 replaces[Anum_pg_subscription_subsynccommit - 1] = true;
1201 }
1202
1203 if (IsSet(opts.specified_opts, SUBOPT_BINARY))
1204 {
1205 values[Anum_pg_subscription_subbinary - 1] =
1206 BoolGetDatum(opts.binary);
1207 replaces[Anum_pg_subscription_subbinary - 1] = true;
1208 }
1209
1210 if (IsSet(opts.specified_opts, SUBOPT_STREAMING))
1211 {
1212 values[Anum_pg_subscription_substream - 1] =
1213 CharGetDatum(opts.streaming);
1214 replaces[Anum_pg_subscription_substream - 1] = true;
1215 }
1216
1217 if (IsSet(opts.specified_opts, SUBOPT_DISABLE_ON_ERR))
1218 {
1219 values[Anum_pg_subscription_subdisableonerr - 1]
1220 = BoolGetDatum(opts.disableonerr);
1221 replaces[Anum_pg_subscription_subdisableonerr - 1]
1222 = true;
1223 }
1224
1225 if (IsSet(opts.specified_opts, SUBOPT_PASSWORD_REQUIRED))
1226 {
1227 /* Non-superuser may not disable password_required. */
1228 if (!opts.passwordrequired && !superuser())
1229 ereport(ERROR,
1230 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1231 errmsg("password_required=false is superuser-only"),
1232 errhint("Subscriptions with the password_required option set to false may only be created or modified by the superuser.")));
1233
1234 values[Anum_pg_subscription_subpasswordrequired - 1]
1235 = BoolGetDatum(opts.passwordrequired);
1236 replaces[Anum_pg_subscription_subpasswordrequired - 1]
1237 = true;
1238 }
1239
1240 if (IsSet(opts.specified_opts, SUBOPT_RUN_AS_OWNER))
1241 {
1242 values[Anum_pg_subscription_subrunasowner - 1] =
1243 BoolGetDatum(opts.runasowner);
1244 replaces[Anum_pg_subscription_subrunasowner - 1] = true;
1245 }
1246
1247 if (IsSet(opts.specified_opts, SUBOPT_TWOPHASE_COMMIT))
1248 {
1249 /*
1250 * We need to update both the slot and the subscription
1251 * for the two_phase option. We can enable the two_phase
1252 * option for a slot only once the initial data
1253 * synchronization is done. This is to avoid missing some
1254 * data as explained in comments atop worker.c.
1255 */
1256 update_two_phase = !opts.twophase;
1257
1258 CheckAlterSubOption(sub, "two_phase", update_two_phase,
1259 isTopLevel);
1260
1261 /*
1262 * Modifying the two_phase slot option requires a slot
1263 * lookup by slot name, so changing the slot name at the
1264 * same time is not allowed.
1265 */
1266 if (update_two_phase &&
1267 IsSet(opts.specified_opts, SUBOPT_SLOT_NAME))
1268 ereport(ERROR,
1269 (errcode(ERRCODE_SYNTAX_ERROR),
1270 errmsg("slot_name and two_phase cannot be altered at the same time")));
1271
1272 /*
1273 * Note that workers may still survive even if the
1274 * subscription has been disabled.
1275 *
1276 * Ensure workers have already been exited to avoid
1277 * getting prepared transactions while we are disabling
1278 * the two_phase option. Otherwise, the changes of an
1279 * already prepared transaction can be replicated again
1280 * along with its corresponding commit, leading to
1281 * duplicate data or errors.
1282 */
1283 if (logicalrep_workers_find(subid, true, true))
1284 ereport(ERROR,
1285 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1286 errmsg("cannot alter two_phase when logical replication worker is still running"),
1287 errhint("Try again after some time.")));
1288
1289 /*
1290 * two_phase cannot be disabled if there are any
1291 * uncommitted prepared transactions present otherwise it
1292 * can lead to duplicate data or errors as explained in
1293 * the comment above.
1294 */
1295 if (update_two_phase &&
1296 sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED &&
1297 LookupGXactBySubid(subid))
1298 ereport(ERROR,
1299 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1300 errmsg("cannot disable two_phase when prepared transactions are present"),
1301 errhint("Resolve these transactions and try again.")));
1302
1303 /* Change system catalog accordingly */
1304 values[Anum_pg_subscription_subtwophasestate - 1] =
1305 CharGetDatum(opts.twophase ?
1306 LOGICALREP_TWOPHASE_STATE_PENDING :
1307 LOGICALREP_TWOPHASE_STATE_DISABLED);
1308 replaces[Anum_pg_subscription_subtwophasestate - 1] = true;
1309 }
1310
1311 if (IsSet(opts.specified_opts, SUBOPT_FAILOVER))
1312 {
1313 /*
1314 * Similar to the two_phase case above, we need to update
1315 * the failover option for both the slot and the
1316 * subscription.
1317 */
1318 update_failover = true;
1319
1320 CheckAlterSubOption(sub, "failover", update_failover,
1321 isTopLevel);
1322
1323 values[Anum_pg_subscription_subfailover - 1] =
1324 BoolGetDatum(opts.failover);
1325 replaces[Anum_pg_subscription_subfailover - 1] = true;
1326 }
1327
1328 if (IsSet(opts.specified_opts, SUBOPT_ORIGIN))
1329 {
1330 values[Anum_pg_subscription_suborigin - 1] =
1331 CStringGetTextDatum(opts.origin);
1332 replaces[Anum_pg_subscription_suborigin - 1] = true;
1333 }
1334
1335 update_tuple = true;
1336 break;
1337 }
1338
1340 {
1341 parse_subscription_options(pstate, stmt->options,
1343 Assert(IsSet(opts.specified_opts, SUBOPT_ENABLED));
1344
1345 if (!sub->slotname && opts.enabled)
1346 ereport(ERROR,
1347 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1348 errmsg("cannot enable subscription that does not have a slot name")));
1349
1350 values[Anum_pg_subscription_subenabled - 1] =
1351 BoolGetDatum(opts.enabled);
1352 replaces[Anum_pg_subscription_subenabled - 1] = true;
1353
1354 if (opts.enabled)
1356
1357 update_tuple = true;
1358 break;
1359 }
1360
1362 /* Load the library providing us libpq calls. */
1363 load_file("libpqwalreceiver", false);
1364 /* Check the connection info string. */
1365 walrcv_check_conninfo(stmt->conninfo,
1366 sub->passwordrequired && !sub->ownersuperuser);
1367
1368 values[Anum_pg_subscription_subconninfo - 1] =
1369 CStringGetTextDatum(stmt->conninfo);
1370 replaces[Anum_pg_subscription_subconninfo - 1] = true;
1371 update_tuple = true;
1372 break;
1373
1375 {
1376 supported_opts = SUBOPT_COPY_DATA | SUBOPT_REFRESH;
1377 parse_subscription_options(pstate, stmt->options,
1378 supported_opts, &opts);
1379
1380 values[Anum_pg_subscription_subpublications - 1] =
1381 publicationListToArray(stmt->publication);
1382 replaces[Anum_pg_subscription_subpublications - 1] = true;
1383
1384 update_tuple = true;
1385
1386 /* Refresh if user asked us to. */
1387 if (opts.refresh)
1388 {
1389 if (!sub->enabled)
1390 ereport(ERROR,
1391 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1392 errmsg("ALTER SUBSCRIPTION with refresh is not allowed for disabled subscriptions"),
1393 errhint("Use ALTER SUBSCRIPTION ... SET PUBLICATION ... WITH (refresh = false).")));
1394
1395 /*
1396 * See ALTER_SUBSCRIPTION_REFRESH for details why this is
1397 * not allowed.
1398 */
1399 if (sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED && opts.copy_data)
1400 ereport(ERROR,
1401 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1402 errmsg("ALTER SUBSCRIPTION with refresh and copy_data is not allowed when two_phase is enabled"),
1403 errhint("Use ALTER SUBSCRIPTION ... SET PUBLICATION with refresh = false, or with copy_data = false, or use DROP/CREATE SUBSCRIPTION.")));
1404
1405 PreventInTransactionBlock(isTopLevel, "ALTER SUBSCRIPTION with refresh");
1406
1407 /* Make sure refresh sees the new list of publications. */
1408 sub->publications = stmt->publication;
1409
1410 AlterSubscription_refresh(sub, opts.copy_data,
1411 stmt->publication);
1412 }
1413
1414 break;
1415 }
1416
1419 {
1420 List *publist;
1421 bool isadd = stmt->kind == ALTER_SUBSCRIPTION_ADD_PUBLICATION;
1422
1423 supported_opts = SUBOPT_REFRESH | SUBOPT_COPY_DATA;
1424 parse_subscription_options(pstate, stmt->options,
1425 supported_opts, &opts);
1426
1427 publist = merge_publications(sub->publications, stmt->publication, isadd, stmt->subname);
1428 values[Anum_pg_subscription_subpublications - 1] =
1429 publicationListToArray(publist);
1430 replaces[Anum_pg_subscription_subpublications - 1] = true;
1431
1432 update_tuple = true;
1433
1434 /* Refresh if user asked us to. */
1435 if (opts.refresh)
1436 {
1437 /* We only need to validate user specified publications. */
1438 List *validate_publications = (isadd) ? stmt->publication : NULL;
1439
1440 if (!sub->enabled)
1441 ereport(ERROR,
1442 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1443 errmsg("ALTER SUBSCRIPTION with refresh is not allowed for disabled subscriptions"),
1444 /* translator: %s is an SQL ALTER command */
1445 errhint("Use %s instead.",
1446 isadd ?
1447 "ALTER SUBSCRIPTION ... ADD PUBLICATION ... WITH (refresh = false)" :
1448 "ALTER SUBSCRIPTION ... DROP PUBLICATION ... WITH (refresh = false)")));
1449
1450 /*
1451 * See ALTER_SUBSCRIPTION_REFRESH for details why this is
1452 * not allowed.
1453 */
1454 if (sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED && opts.copy_data)
1455 ereport(ERROR,
1456 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1457 errmsg("ALTER SUBSCRIPTION with refresh and copy_data is not allowed when two_phase is enabled"),
1458 /* translator: %s is an SQL ALTER command */
1459 errhint("Use %s with refresh = false, or with copy_data = false, or use DROP/CREATE SUBSCRIPTION.",
1460 isadd ?
1461 "ALTER SUBSCRIPTION ... ADD PUBLICATION" :
1462 "ALTER SUBSCRIPTION ... DROP PUBLICATION")));
1463
1464 PreventInTransactionBlock(isTopLevel, "ALTER SUBSCRIPTION with refresh");
1465
1466 /* Refresh the new list of publications. */
1467 sub->publications = publist;
1468
1469 AlterSubscription_refresh(sub, opts.copy_data,
1470 validate_publications);
1471 }
1472
1473 break;
1474 }
1475
1477 {
1478 if (!sub->enabled)
1479 ereport(ERROR,
1480 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1481 errmsg("ALTER SUBSCRIPTION ... REFRESH is not allowed for disabled subscriptions")));
1482
1483 parse_subscription_options(pstate, stmt->options,
1485
1486 /*
1487 * The subscription option "two_phase" requires that
1488 * replication has passed the initial table synchronization
1489 * phase before the two_phase becomes properly enabled.
1490 *
1491 * But, having reached this two-phase commit "enabled" state
1492 * we must not allow any subsequent table initialization to
1493 * occur. So the ALTER SUBSCRIPTION ... REFRESH is disallowed
1494 * when the user had requested two_phase = on mode.
1495 *
1496 * The exception to this restriction is when copy_data =
1497 * false, because when copy_data is false the tablesync will
1498 * start already in READY state and will exit directly without
1499 * doing anything.
1500 *
1501 * For more details see comments atop worker.c.
1502 */
1503 if (sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED && opts.copy_data)
1504 ereport(ERROR,
1505 (errcode(ERRCODE_SYNTAX_ERROR),
1506 errmsg("ALTER SUBSCRIPTION ... REFRESH with copy_data is not allowed when two_phase is enabled"),
1507 errhint("Use ALTER SUBSCRIPTION ... REFRESH with copy_data = false, or use DROP/CREATE SUBSCRIPTION.")));
1508
1509 PreventInTransactionBlock(isTopLevel, "ALTER SUBSCRIPTION ... REFRESH");
1510
1511 AlterSubscription_refresh(sub, opts.copy_data, NULL);
1512
1513 break;
1514 }
1515
1517 {
1518 parse_subscription_options(pstate, stmt->options, SUBOPT_LSN, &opts);
1519
1520 /* ALTER SUBSCRIPTION ... SKIP supports only LSN option */
1521 Assert(IsSet(opts.specified_opts, SUBOPT_LSN));
1522
1523 /*
1524 * If the user sets subskiplsn, we do a sanity check to make
1525 * sure that the specified LSN is a probable value.
1526 */
1527 if (!XLogRecPtrIsInvalid(opts.lsn))
1528 {
1529 RepOriginId originid;
1530 char originname[NAMEDATALEN];
1531 XLogRecPtr remote_lsn;
1532
1534 originname, sizeof(originname));
1535 originid = replorigin_by_name(originname, false);
1536 remote_lsn = replorigin_get_progress(originid, false);
1537
1538 /* Check the given LSN is at least a future LSN */
1539 if (!XLogRecPtrIsInvalid(remote_lsn) && opts.lsn < remote_lsn)
1540 ereport(ERROR,
1541 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1542 errmsg("skip WAL location (LSN %X/%X) must be greater than origin LSN %X/%X",
1543 LSN_FORMAT_ARGS(opts.lsn),
1544 LSN_FORMAT_ARGS(remote_lsn))));
1545 }
1546
1547 values[Anum_pg_subscription_subskiplsn - 1] = LSNGetDatum(opts.lsn);
1548 replaces[Anum_pg_subscription_subskiplsn - 1] = true;
1549
1550 update_tuple = true;
1551 break;
1552 }
1553
1554 default:
1555 elog(ERROR, "unrecognized ALTER SUBSCRIPTION kind %d",
1556 stmt->kind);
1557 }
1558
1559 /* Update the catalog if needed. */
1560 if (update_tuple)
1561 {
1562 tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
1563 replaces);
1564
1565 CatalogTupleUpdate(rel, &tup->t_self, tup);
1566
1567 heap_freetuple(tup);
1568 }
1569
1570 /*
1571 * Try to acquire the connection necessary for altering the slot, if
1572 * needed.
1573 *
1574 * This has to be at the end because otherwise if there is an error while
1575 * doing the database operations we won't be able to rollback altered
1576 * slot.
1577 */
1578 if (update_failover || update_two_phase)
1579 {
1580 bool must_use_password;
1581 char *err;
1583
1584 /* Load the library providing us libpq calls. */
1585 load_file("libpqwalreceiver", false);
1586
1587 /* Try to connect to the publisher. */
1588 must_use_password = sub->passwordrequired && !sub->ownersuperuser;
1589 wrconn = walrcv_connect(sub->conninfo, true, true, must_use_password,
1590 sub->name, &err);
1591 if (!wrconn)
1592 ereport(ERROR,
1593 (errcode(ERRCODE_CONNECTION_FAILURE),
1594 errmsg("subscription \"%s\" could not connect to the publisher: %s",
1595 sub->name, err)));
1596
1597 PG_TRY();
1598 {
1600 update_failover ? &opts.failover : NULL,
1601 update_two_phase ? &opts.twophase : NULL);
1602 }
1603 PG_FINALLY();
1604 {
1606 }
1607 PG_END_TRY();
1608 }
1609
1611
1612 ObjectAddressSet(myself, SubscriptionRelationId, subid);
1613
1614 InvokeObjectPostAlterHook(SubscriptionRelationId, subid, 0);
1615
1616 /* Wake up related replication workers to handle this change quickly. */
1618
1619 return myself;
1620}
1621
1622/*
1623 * Drop a subscription
1624 */
1625void
1627{
1628 Relation rel;
1629 ObjectAddress myself;
1630 HeapTuple tup;
1631 Oid subid;
1632 Oid subowner;
1633 Datum datum;
1634 bool isnull;
1635 char *subname;
1636 char *conninfo;
1637 char *slotname;
1638 List *subworkers;
1639 ListCell *lc;
1640 char originname[NAMEDATALEN];
1641 char *err = NULL;
1644 List *rstates;
1645 bool must_use_password;
1646
1647 /*
1648 * Lock pg_subscription with AccessExclusiveLock to ensure that the
1649 * launcher doesn't restart new worker during dropping the subscription
1650 */
1651 rel = table_open(SubscriptionRelationId, AccessExclusiveLock);
1652
1653 tup = SearchSysCache2(SUBSCRIPTIONNAME, MyDatabaseId,
1654 CStringGetDatum(stmt->subname));
1655
1656 if (!HeapTupleIsValid(tup))
1657 {
1658 table_close(rel, NoLock);
1659
1660 if (!stmt->missing_ok)
1661 ereport(ERROR,
1662 (errcode(ERRCODE_UNDEFINED_OBJECT),
1663 errmsg("subscription \"%s\" does not exist",
1664 stmt->subname)));
1665 else
1667 (errmsg("subscription \"%s\" does not exist, skipping",
1668 stmt->subname)));
1669
1670 return;
1671 }
1672
1673 form = (Form_pg_subscription) GETSTRUCT(tup);
1674 subid = form->oid;
1675 subowner = form->subowner;
1676 must_use_password = !superuser_arg(subowner) && form->subpasswordrequired;
1677
1678 /* must be owner */
1679 if (!object_ownercheck(SubscriptionRelationId, subid, GetUserId()))
1681 stmt->subname);
1682
1683 /* DROP hook for the subscription being removed */
1684 InvokeObjectDropHook(SubscriptionRelationId, subid, 0);
1685
1686 /*
1687 * Lock the subscription so nobody else can do anything with it (including
1688 * the replication workers).
1689 */
1690 LockSharedObject(SubscriptionRelationId, subid, 0, AccessExclusiveLock);
1691
1692 /* Get subname */
1693 datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID, tup,
1694 Anum_pg_subscription_subname);
1695 subname = pstrdup(NameStr(*DatumGetName(datum)));
1696
1697 /* Get conninfo */
1698 datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID, tup,
1699 Anum_pg_subscription_subconninfo);
1700 conninfo = TextDatumGetCString(datum);
1701
1702 /* Get slotname */
1703 datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
1704 Anum_pg_subscription_subslotname, &isnull);
1705 if (!isnull)
1706 slotname = pstrdup(NameStr(*DatumGetName(datum)));
1707 else
1708 slotname = NULL;
1709
1710 /*
1711 * Since dropping a replication slot is not transactional, the replication
1712 * slot stays dropped even if the transaction rolls back. So we cannot
1713 * run DROP SUBSCRIPTION inside a transaction block if dropping the
1714 * replication slot. Also, in this case, we report a message for dropping
1715 * the subscription to the cumulative stats system.
1716 *
1717 * XXX The command name should really be something like "DROP SUBSCRIPTION
1718 * of a subscription that is associated with a replication slot", but we
1719 * don't have the proper facilities for that.
1720 */
1721 if (slotname)
1722 PreventInTransactionBlock(isTopLevel, "DROP SUBSCRIPTION");
1723
1724 ObjectAddressSet(myself, SubscriptionRelationId, subid);
1725 EventTriggerSQLDropAddObject(&myself, true, true);
1726
1727 /* Remove the tuple from catalog. */
1728 CatalogTupleDelete(rel, &tup->t_self);
1729
1730 ReleaseSysCache(tup);
1731
1732 /*
1733 * Stop all the subscription workers immediately.
1734 *
1735 * This is necessary if we are dropping the replication slot, so that the
1736 * slot becomes accessible.
1737 *
1738 * It is also necessary if the subscription is disabled and was disabled
1739 * in the same transaction. Then the workers haven't seen the disabling
1740 * yet and will still be running, leading to hangs later when we want to
1741 * drop the replication origin. If the subscription was disabled before
1742 * this transaction, then there shouldn't be any workers left, so this
1743 * won't make a difference.
1744 *
1745 * New workers won't be started because we hold an exclusive lock on the
1746 * subscription till the end of the transaction.
1747 */
1748 subworkers = logicalrep_workers_find(subid, false, true);
1749 foreach(lc, subworkers)
1750 {
1752
1754 }
1755 list_free(subworkers);
1756
1757 /*
1758 * Remove the no-longer-useful entry in the launcher's table of apply
1759 * worker start times.
1760 *
1761 * If this transaction rolls back, the launcher might restart a failed
1762 * apply worker before wal_retrieve_retry_interval milliseconds have
1763 * elapsed, but that's pretty harmless.
1764 */
1766
1767 /*
1768 * Cleanup of tablesync replication origins.
1769 *
1770 * Any READY-state relations would already have dealt with clean-ups.
1771 *
1772 * Note that the state can't change because we have already stopped both
1773 * the apply and tablesync workers and they can't restart because of
1774 * exclusive lock on the subscription.
1775 */
1776 rstates = GetSubscriptionRelations(subid, true);
1777 foreach(lc, rstates)
1778 {
1780 Oid relid = rstate->relid;
1781
1782 /* Only cleanup resources of tablesync workers */
1783 if (!OidIsValid(relid))
1784 continue;
1785
1786 /*
1787 * Drop the tablesync's origin tracking if exists.
1788 *
1789 * It is possible that the origin is not yet created for tablesync
1790 * worker so passing missing_ok = true. This can happen for the states
1791 * before SUBREL_STATE_FINISHEDCOPY.
1792 */
1793 ReplicationOriginNameForLogicalRep(subid, relid, originname,
1794 sizeof(originname));
1795 replorigin_drop_by_name(originname, true, false);
1796 }
1797
1798 /* Clean up dependencies */
1799 deleteSharedDependencyRecordsFor(SubscriptionRelationId, subid, 0);
1800
1801 /* Remove any associated relation synchronization states. */
1803
1804 /* Remove the origin tracking if exists. */
1805 ReplicationOriginNameForLogicalRep(subid, InvalidOid, originname, sizeof(originname));
1806 replorigin_drop_by_name(originname, true, false);
1807
1808 /*
1809 * Tell the cumulative stats system that the subscription is getting
1810 * dropped.
1811 */
1813
1814 /*
1815 * If there is no slot associated with the subscription, we can finish
1816 * here.
1817 */
1818 if (!slotname && rstates == NIL)
1819 {
1820 table_close(rel, NoLock);
1821 return;
1822 }
1823
1824 /*
1825 * Try to acquire the connection necessary for dropping slots.
1826 *
1827 * Note: If the slotname is NONE/NULL then we allow the command to finish
1828 * and users need to manually cleanup the apply and tablesync worker slots
1829 * later.
1830 *
1831 * This has to be at the end because otherwise if there is an error while
1832 * doing the database operations we won't be able to rollback dropped
1833 * slot.
1834 */
1835 load_file("libpqwalreceiver", false);
1836
1837 wrconn = walrcv_connect(conninfo, true, true, must_use_password,
1838 subname, &err);
1839 if (wrconn == NULL)
1840 {
1841 if (!slotname)
1842 {
1843 /* be tidy */
1844 list_free(rstates);
1845 table_close(rel, NoLock);
1846 return;
1847 }
1848 else
1849 {
1850 ReportSlotConnectionError(rstates, subid, slotname, err);
1851 }
1852 }
1853
1854 PG_TRY();
1855 {
1856 foreach(lc, rstates)
1857 {
1859 Oid relid = rstate->relid;
1860
1861 /* Only cleanup resources of tablesync workers */
1862 if (!OidIsValid(relid))
1863 continue;
1864
1865 /*
1866 * Drop the tablesync slots associated with removed tables.
1867 *
1868 * For SYNCDONE/READY states, the tablesync slot is known to have
1869 * already been dropped by the tablesync worker.
1870 *
1871 * For other states, there is no certainty, maybe the slot does
1872 * not exist yet. Also, if we fail after removing some of the
1873 * slots, next time, it will again try to drop already dropped
1874 * slots and fail. For these reasons, we allow missing_ok = true
1875 * for the drop.
1876 */
1877 if (rstate->state != SUBREL_STATE_SYNCDONE)
1878 {
1879 char syncslotname[NAMEDATALEN] = {0};
1880
1881 ReplicationSlotNameForTablesync(subid, relid, syncslotname,
1882 sizeof(syncslotname));
1883 ReplicationSlotDropAtPubNode(wrconn, syncslotname, true);
1884 }
1885 }
1886
1887 list_free(rstates);
1888
1889 /*
1890 * If there is a slot associated with the subscription, then drop the
1891 * replication slot at the publisher.
1892 */
1893 if (slotname)
1894 ReplicationSlotDropAtPubNode(wrconn, slotname, false);
1895 }
1896 PG_FINALLY();
1897 {
1899 }
1900 PG_END_TRY();
1901
1902 table_close(rel, NoLock);
1903}
1904
1905/*
1906 * Drop the replication slot at the publisher node using the replication
1907 * connection.
1908 *
1909 * missing_ok - if true then only issue a LOG message if the slot doesn't
1910 * exist.
1911 */
1912void
1913ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missing_ok)
1914{
1915 StringInfoData cmd;
1916
1917 Assert(wrconn);
1918
1919 load_file("libpqwalreceiver", false);
1920
1921 initStringInfo(&cmd);
1922 appendStringInfo(&cmd, "DROP_REPLICATION_SLOT %s WAIT", quote_identifier(slotname));
1923
1924 PG_TRY();
1925 {
1927
1928 res = walrcv_exec(wrconn, cmd.data, 0, NULL);
1929
1930 if (res->status == WALRCV_OK_COMMAND)
1931 {
1932 /* NOTICE. Success. */
1934 (errmsg("dropped replication slot \"%s\" on publisher",
1935 slotname)));
1936 }
1937 else if (res->status == WALRCV_ERROR &&
1938 missing_ok &&
1939 res->sqlstate == ERRCODE_UNDEFINED_OBJECT)
1940 {
1941 /* LOG. Error, but missing_ok = true. */
1942 ereport(LOG,
1943 (errmsg("could not drop replication slot \"%s\" on publisher: %s",
1944 slotname, res->err)));
1945 }
1946 else
1947 {
1948 /* ERROR. */
1949 ereport(ERROR,
1950 (errcode(ERRCODE_CONNECTION_FAILURE),
1951 errmsg("could not drop replication slot \"%s\" on publisher: %s",
1952 slotname, res->err)));
1953 }
1954
1956 }
1957 PG_FINALLY();
1958 {
1959 pfree(cmd.data);
1960 }
1961 PG_END_TRY();
1962}
1963
1964/*
1965 * Internal workhorse for changing a subscription owner
1966 */
1967static void
1969{
1971 AclResult aclresult;
1972
1973 form = (Form_pg_subscription) GETSTRUCT(tup);
1974
1975 if (form->subowner == newOwnerId)
1976 return;
1977
1978 if (!object_ownercheck(SubscriptionRelationId, form->oid, GetUserId()))
1980 NameStr(form->subname));
1981
1982 /*
1983 * Don't allow non-superuser modification of a subscription with
1984 * password_required=false.
1985 */
1986 if (!form->subpasswordrequired && !superuser())
1987 ereport(ERROR,
1988 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1989 errmsg("password_required=false is superuser-only"),
1990 errhint("Subscriptions with the password_required option set to false may only be created or modified by the superuser.")));
1991
1992 /* Must be able to become new owner */
1993 check_can_set_role(GetUserId(), newOwnerId);
1994
1995 /*
1996 * current owner must have CREATE on database
1997 *
1998 * This is consistent with how ALTER SCHEMA ... OWNER TO works, but some
1999 * other object types behave differently (e.g. you can't give a table to a
2000 * user who lacks CREATE privileges on a schema).
2001 */
2002 aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId,
2004 if (aclresult != ACLCHECK_OK)
2007
2008 form->subowner = newOwnerId;
2009 CatalogTupleUpdate(rel, &tup->t_self, tup);
2010
2011 /* Update owner dependency reference */
2012 changeDependencyOnOwner(SubscriptionRelationId,
2013 form->oid,
2014 newOwnerId);
2015
2016 InvokeObjectPostAlterHook(SubscriptionRelationId,
2017 form->oid, 0);
2018
2019 /* Wake up related background processes to handle this change quickly. */
2022}
2023
2024/*
2025 * Change subscription owner -- by name
2026 */
2028AlterSubscriptionOwner(const char *name, Oid newOwnerId)
2029{
2030 Oid subid;
2031 HeapTuple tup;
2032 Relation rel;
2033 ObjectAddress address;
2035
2036 rel = table_open(SubscriptionRelationId, RowExclusiveLock);
2037
2038 tup = SearchSysCacheCopy2(SUBSCRIPTIONNAME, MyDatabaseId,
2040
2041 if (!HeapTupleIsValid(tup))
2042 ereport(ERROR,
2043 (errcode(ERRCODE_UNDEFINED_OBJECT),
2044 errmsg("subscription \"%s\" does not exist", name)));
2045
2046 form = (Form_pg_subscription) GETSTRUCT(tup);
2047 subid = form->oid;
2048
2049 AlterSubscriptionOwner_internal(rel, tup, newOwnerId);
2050
2051 ObjectAddressSet(address, SubscriptionRelationId, subid);
2052
2053 heap_freetuple(tup);
2054
2056
2057 return address;
2058}
2059
2060/*
2061 * Change subscription owner -- by OID
2062 */
2063void
2065{
2066 HeapTuple tup;
2067 Relation rel;
2068
2069 rel = table_open(SubscriptionRelationId, RowExclusiveLock);
2070
2071 tup = SearchSysCacheCopy1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
2072
2073 if (!HeapTupleIsValid(tup))
2074 ereport(ERROR,
2075 (errcode(ERRCODE_UNDEFINED_OBJECT),
2076 errmsg("subscription with OID %u does not exist", subid)));
2077
2078 AlterSubscriptionOwner_internal(rel, tup, newOwnerId);
2079
2080 heap_freetuple(tup);
2081
2083}
2084
2085/*
2086 * Check and log a warning if the publisher has subscribed to the same table
2087 * from some other publisher. This check is required only if "copy_data = true"
2088 * and "origin = none" for CREATE SUBSCRIPTION and
2089 * ALTER SUBSCRIPTION ... REFRESH statements to notify the user that data
2090 * having origin might have been copied.
2091 *
2092 * This check need not be performed on the tables that are already added
2093 * because incremental sync for those tables will happen through WAL and the
2094 * origin of the data can be identified from the WAL records.
2095 *
2096 * subrel_local_oids contains the list of relation oids that are already
2097 * present on the subscriber.
2098 */
2099static void
2101 bool copydata, char *origin, Oid *subrel_local_oids,
2102 int subrel_count, char *subname)
2103{
2105 StringInfoData cmd;
2106 TupleTableSlot *slot;
2107 Oid tableRow[1] = {TEXTOID};
2108 List *publist = NIL;
2109 int i;
2110
2111 if (!copydata || !origin ||
2112 (pg_strcasecmp(origin, LOGICALREP_ORIGIN_NONE) != 0))
2113 return;
2114
2115 initStringInfo(&cmd);
2117 "SELECT DISTINCT P.pubname AS pubname\n"
2118 "FROM pg_publication P,\n"
2119 " LATERAL pg_get_publication_tables(P.pubname) GPT\n"
2120 " JOIN pg_subscription_rel PS ON (GPT.relid = PS.srrelid),\n"
2121 " pg_class C JOIN pg_namespace N ON (N.oid = C.relnamespace)\n"
2122 "WHERE C.oid = GPT.relid AND P.pubname IN (");
2123 GetPublicationsStr(publications, &cmd, true);
2124 appendStringInfoString(&cmd, ")\n");
2125
2126 /*
2127 * In case of ALTER SUBSCRIPTION ... REFRESH, subrel_local_oids contains
2128 * the list of relation oids that are already present on the subscriber.
2129 * This check should be skipped for these tables.
2130 */
2131 for (i = 0; i < subrel_count; i++)
2132 {
2133 Oid relid = subrel_local_oids[i];
2134 char *schemaname = get_namespace_name(get_rel_namespace(relid));
2135 char *tablename = get_rel_name(relid);
2136
2137 appendStringInfo(&cmd, "AND NOT (N.nspname = '%s' AND C.relname = '%s')\n",
2138 schemaname, tablename);
2139 }
2140
2141 res = walrcv_exec(wrconn, cmd.data, 1, tableRow);
2142 pfree(cmd.data);
2143
2144 if (res->status != WALRCV_OK_TUPLES)
2145 ereport(ERROR,
2146 (errcode(ERRCODE_CONNECTION_FAILURE),
2147 errmsg("could not receive list of replicated tables from the publisher: %s",
2148 res->err)));
2149
2150 /* Process tables. */
2151 slot = MakeSingleTupleTableSlot(res->tupledesc, &TTSOpsMinimalTuple);
2152 while (tuplestore_gettupleslot(res->tuplestore, true, false, slot))
2153 {
2154 char *pubname;
2155 bool isnull;
2156
2157 pubname = TextDatumGetCString(slot_getattr(slot, 1, &isnull));
2158 Assert(!isnull);
2159
2160 ExecClearTuple(slot);
2161 publist = list_append_unique(publist, makeString(pubname));
2162 }
2163
2164 /*
2165 * Log a warning if the publisher has subscribed to the same table from
2166 * some other publisher. We cannot know the origin of data during the
2167 * initial sync. Data origins can be found only from the WAL by looking at
2168 * the origin id.
2169 *
2170 * XXX: For simplicity, we don't check whether the table has any data or
2171 * not. If the table doesn't have any data then we don't need to
2172 * distinguish between data having origin and data not having origin so we
2173 * can avoid logging a warning in that case.
2174 */
2175 if (publist)
2176 {
2177 StringInfo pubnames = makeStringInfo();
2178
2179 /* Prepare the list of publication(s) for warning message. */
2180 GetPublicationsStr(publist, pubnames, false);
2182 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2183 errmsg("subscription \"%s\" requested copy_data with origin = NONE but might copy data that had a different origin",
2184 subname),
2185 errdetail_plural("The subscription being created subscribes to a publication (%s) that contains tables that are written to by other subscriptions.",
2186 "The subscription being created subscribes to publications (%s) that contain tables that are written to by other subscriptions.",
2187 list_length(publist), pubnames->data),
2188 errhint("Verify that initial data copied from the publisher tables did not come from other origins."));
2189 }
2190
2192
2194}
2195
2196/*
2197 * Get the list of tables which belong to specified publications on the
2198 * publisher connection.
2199 *
2200 * Note that we don't support the case where the column list is different for
2201 * the same table in different publications to avoid sending unwanted column
2202 * information for some of the rows. This can happen when both the column
2203 * list and row filter are specified for different publications.
2204 */
2205static List *
2207{
2209 StringInfoData cmd;
2210 TupleTableSlot *slot;
2211 Oid tableRow[3] = {TEXTOID, TEXTOID, InvalidOid};
2212 List *tablelist = NIL;
2214 bool check_columnlist = (server_version >= 150000);
2215 StringInfo pub_names = makeStringInfo();
2216
2217 initStringInfo(&cmd);
2218
2219 /* Build the pub_names comma-separated string. */
2220 GetPublicationsStr(publications, pub_names, true);
2221
2222 /* Get the list of tables from the publisher. */
2223 if (server_version >= 160000)
2224 {
2225 tableRow[2] = INT2VECTOROID;
2226
2227 /*
2228 * From version 16, we allowed passing multiple publications to the
2229 * function pg_get_publication_tables. This helped to filter out the
2230 * partition table whose ancestor is also published in this
2231 * publication array.
2232 *
2233 * Join pg_get_publication_tables with pg_publication to exclude
2234 * non-existing publications.
2235 *
2236 * Note that attrs are always stored in sorted order so we don't need
2237 * to worry if different publications have specified them in a
2238 * different order. See pub_collist_validate.
2239 */
2240 appendStringInfo(&cmd, "SELECT DISTINCT n.nspname, c.relname, gpt.attrs\n"
2241 " FROM pg_class c\n"
2242 " JOIN pg_namespace n ON n.oid = c.relnamespace\n"
2243 " JOIN ( SELECT (pg_get_publication_tables(VARIADIC array_agg(pubname::text))).*\n"
2244 " FROM pg_publication\n"
2245 " WHERE pubname IN ( %s )) AS gpt\n"
2246 " ON gpt.relid = c.oid\n",
2247 pub_names->data);
2248 }
2249 else
2250 {
2251 tableRow[2] = NAMEARRAYOID;
2252 appendStringInfoString(&cmd, "SELECT DISTINCT t.schemaname, t.tablename \n");
2253
2254 /* Get column lists for each relation if the publisher supports it */
2255 if (check_columnlist)
2256 appendStringInfoString(&cmd, ", t.attnames\n");
2257
2258 appendStringInfo(&cmd, "FROM pg_catalog.pg_publication_tables t\n"
2259 " WHERE t.pubname IN ( %s )",
2260 pub_names->data);
2261 }
2262
2263 destroyStringInfo(pub_names);
2264
2265 res = walrcv_exec(wrconn, cmd.data, check_columnlist ? 3 : 2, tableRow);
2266 pfree(cmd.data);
2267
2268 if (res->status != WALRCV_OK_TUPLES)
2269 ereport(ERROR,
2270 (errcode(ERRCODE_CONNECTION_FAILURE),
2271 errmsg("could not receive list of replicated tables from the publisher: %s",
2272 res->err)));
2273
2274 /* Process tables. */
2275 slot = MakeSingleTupleTableSlot(res->tupledesc, &TTSOpsMinimalTuple);
2276 while (tuplestore_gettupleslot(res->tuplestore, true, false, slot))
2277 {
2278 char *nspname;
2279 char *relname;
2280 bool isnull;
2281 RangeVar *rv;
2282
2283 nspname = TextDatumGetCString(slot_getattr(slot, 1, &isnull));
2284 Assert(!isnull);
2285 relname = TextDatumGetCString(slot_getattr(slot, 2, &isnull));
2286 Assert(!isnull);
2287
2288 rv = makeRangeVar(nspname, relname, -1);
2289
2290 if (check_columnlist && list_member(tablelist, rv))
2291 ereport(ERROR,
2292 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2293 errmsg("cannot use different column lists for table \"%s.%s\" in different publications",
2294 nspname, relname));
2295 else
2296 tablelist = lappend(tablelist, rv);
2297
2298 ExecClearTuple(slot);
2299 }
2301
2303
2304 return tablelist;
2305}
2306
2307/*
2308 * This is to report the connection failure while dropping replication slots.
2309 * Here, we report the WARNING for all tablesync slots so that user can drop
2310 * them manually, if required.
2311 */
2312static void
2313ReportSlotConnectionError(List *rstates, Oid subid, char *slotname, char *err)
2314{
2315 ListCell *lc;
2316
2317 foreach(lc, rstates)
2318 {
2320 Oid relid = rstate->relid;
2321
2322 /* Only cleanup resources of tablesync workers */
2323 if (!OidIsValid(relid))
2324 continue;
2325
2326 /*
2327 * Caller needs to ensure that relstate doesn't change underneath us.
2328 * See DropSubscription where we get the relstates.
2329 */
2330 if (rstate->state != SUBREL_STATE_SYNCDONE)
2331 {
2332 char syncslotname[NAMEDATALEN] = {0};
2333
2334 ReplicationSlotNameForTablesync(subid, relid, syncslotname,
2335 sizeof(syncslotname));
2336 elog(WARNING, "could not drop tablesync replication slot \"%s\"",
2337 syncslotname);
2338 }
2339 }
2340
2341 ereport(ERROR,
2342 (errcode(ERRCODE_CONNECTION_FAILURE),
2343 errmsg("could not connect to publisher when attempting to drop replication slot \"%s\": %s",
2344 slotname, err),
2345 /* translator: %s is an SQL ALTER command */
2346 errhint("Use %s to disable the subscription, and then use %s to disassociate it from the slot.",
2347 "ALTER SUBSCRIPTION ... DISABLE",
2348 "ALTER SUBSCRIPTION ... SET (slot_name = NONE)")));
2349}
2350
2351/*
2352 * Check for duplicates in the given list of publications and error out if
2353 * found one. Add publications to datums as text datums, if datums is not
2354 * NULL.
2355 */
2356static void
2358{
2359 ListCell *cell;
2360 int j = 0;
2361
2362 foreach(cell, publist)
2363 {
2364 char *name = strVal(lfirst(cell));
2365 ListCell *pcell;
2366
2367 foreach(pcell, publist)
2368 {
2369 char *pname = strVal(lfirst(pcell));
2370
2371 if (pcell == cell)
2372 break;
2373
2374 if (strcmp(name, pname) == 0)
2375 ereport(ERROR,
2377 errmsg("publication name \"%s\" used more than once",
2378 pname)));
2379 }
2380
2381 if (datums)
2382 datums[j++] = CStringGetTextDatum(name);
2383 }
2384}
2385
2386/*
2387 * Merge current subscription's publications and user-specified publications
2388 * from ADD/DROP PUBLICATIONS.
2389 *
2390 * If addpub is true, we will add the list of publications into oldpublist.
2391 * Otherwise, we will delete the list of publications from oldpublist. The
2392 * returned list is a copy, oldpublist itself is not changed.
2393 *
2394 * subname is the subscription name, for error messages.
2395 */
2396static List *
2397merge_publications(List *oldpublist, List *newpublist, bool addpub, const char *subname)
2398{
2399 ListCell *lc;
2400
2401 oldpublist = list_copy(oldpublist);
2402
2403 check_duplicates_in_publist(newpublist, NULL);
2404
2405 foreach(lc, newpublist)
2406 {
2407 char *name = strVal(lfirst(lc));
2408 ListCell *lc2;
2409 bool found = false;
2410
2411 foreach(lc2, oldpublist)
2412 {
2413 char *pubname = strVal(lfirst(lc2));
2414
2415 if (strcmp(name, pubname) == 0)
2416 {
2417 found = true;
2418 if (addpub)
2419 ereport(ERROR,
2421 errmsg("publication \"%s\" is already in subscription \"%s\"",
2422 name, subname)));
2423 else
2424 oldpublist = foreach_delete_current(oldpublist, lc2);
2425
2426 break;
2427 }
2428 }
2429
2430 if (addpub && !found)
2431 oldpublist = lappend(oldpublist, makeString(name));
2432 else if (!addpub && !found)
2433 ereport(ERROR,
2434 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2435 errmsg("publication \"%s\" is not in subscription \"%s\"",
2436 name, subname)));
2437 }
2438
2439 /*
2440 * XXX Probably no strong reason for this, but for now it's to make ALTER
2441 * SUBSCRIPTION ... DROP PUBLICATION consistent with SET PUBLICATION.
2442 */
2443 if (!oldpublist)
2444 ereport(ERROR,
2445 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2446 errmsg("cannot drop all the publications from a subscription")));
2447
2448 return oldpublist;
2449}
2450
2451/*
2452 * Extract the streaming mode value from a DefElem. This is like
2453 * defGetBoolean() but also accepts the special value of "parallel".
2454 */
2455char
2457{
2458 /*
2459 * If no parameter value given, assume "true" is meant.
2460 */
2461 if (!def->arg)
2462 return LOGICALREP_STREAM_ON;
2463
2464 /*
2465 * Allow 0, 1, "false", "true", "off", "on" or "parallel".
2466 */
2467 switch (nodeTag(def->arg))
2468 {
2469 case T_Integer:
2470 switch (intVal(def->arg))
2471 {
2472 case 0:
2473 return LOGICALREP_STREAM_OFF;
2474 case 1:
2475 return LOGICALREP_STREAM_ON;
2476 default:
2477 /* otherwise, error out below */
2478 break;
2479 }
2480 break;
2481 default:
2482 {
2483 char *sval = defGetString(def);
2484
2485 /*
2486 * The set of strings accepted here should match up with the
2487 * grammar's opt_boolean_or_string production.
2488 */
2489 if (pg_strcasecmp(sval, "false") == 0 ||
2490 pg_strcasecmp(sval, "off") == 0)
2491 return LOGICALREP_STREAM_OFF;
2492 if (pg_strcasecmp(sval, "true") == 0 ||
2493 pg_strcasecmp(sval, "on") == 0)
2494 return LOGICALREP_STREAM_ON;
2495 if (pg_strcasecmp(sval, "parallel") == 0)
2496 return LOGICALREP_STREAM_PARALLEL;
2497 }
2498 break;
2499 }
2500
2501 ereport(ERROR,
2502 (errcode(ERRCODE_SYNTAX_ERROR),
2503 errmsg("%s requires a Boolean value or \"parallel\"",
2504 def->defname)));
2505 return LOGICALREP_STREAM_OFF; /* keep compiler quiet */
2506}
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:5268
void check_can_set_role(Oid member, Oid role)
Definition: acl.c:5325
AclResult
Definition: acl.h:182
@ ACLCHECK_OK
Definition: acl.h:183
@ ACLCHECK_NOT_OWNER
Definition: acl.h:185
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2622
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:3810
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4064
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3381
void LogicalRepWorkersWakeupAtCommit(Oid subid)
Definition: worker.c:5102
void ReplicationOriginNameForLogicalRep(Oid suboid, Oid relid, char *originname, Size szoriginname)
Definition: worker.c:426
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define NameStr(name)
Definition: c.h:700
#define Assert(condition)
Definition: c.h:812
uint32 bits32
Definition: c.h:494
#define OidIsValid(objectId)
Definition: c.h:729
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:419
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3187
char * defGetString(DefElem *def)
Definition: define.c:35
bool defGetBoolean(DefElem *def)
Definition: define.c:94
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
Definition: define.c:371
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:134
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1180
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1295
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define LOG
Definition: elog.h:31
#define PG_TRY(...)
Definition: elog.h:371
#define WARNING
Definition: elog.h:36
#define PG_END_TRY(...)
Definition: elog.h:396
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define NOTICE
Definition: elog.h:35
#define PG_FINALLY(...)
Definition: elog.h:388
#define ereport(elevel,...)
Definition: elog.h:149
void err(int eval, const char *fmt,...)
Definition: err.c:43
void EventTriggerSQLDropAddObject(const ObjectAddress *object, bool original, bool normal)
void CheckSubscriptionRelkind(char relkind, const char *nspname, const char *relname)
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1425
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1441
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:86
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
Oid MyDatabaseId
Definition: globals.c:93
int set_config_option(const char *name, const char *value, GucContext context, GucSource source, GucAction action, bool changeVal, int elevel, bool is_reload)
Definition: guc.c:3342
@ GUC_ACTION_SET
Definition: guc.h:199
@ PGC_S_TEST
Definition: guc.h:121
@ PGC_BACKEND
Definition: guc.h:73
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
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
#define stmt
Definition: indent_codes.h:59
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:313
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:233
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:365
int j
Definition: isn.c:73
int i
Definition: isn.c:72
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
List * logicalrep_workers_find(Oid subid, bool only_running, bool acquire_lock)
Definition: launcher.c:266
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:606
void ApplyLauncherWakeupAtCommit(void)
Definition: launcher.c:1102
void ApplyLauncherForgetWorkerStartTime(Oid subid)
Definition: launcher.c:1072
List * lappend(List *list, void *datum)
Definition: list.c:339
List * list_delete(List *list, void *datum)
Definition: list.c:853
List * list_append_unique(List *list, void *datum)
Definition: list.c:1343
List * list_copy(const List *oldlist)
Definition: list.c:1573
void list_free(List *list)
Definition: list.c:1546
bool list_member(const List *list, const void *datum)
Definition: list.c:661
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1072
#define NoLock
Definition: lockdefs.h:34
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define AccessShareLock
Definition: lockdefs.h:36
#define RowExclusiveLock
Definition: lockdefs.h:38
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1928
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:2003
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:1952
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3366
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:424
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
Oid GetUserId(void)
Definition: miscinit.c:517
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:80
#define nodeTag(nodeptr)
Definition: nodes.h:133
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:173
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:197
#define InvokeObjectDropHook(classId, objectId, subId)
Definition: objectaccess.h:182
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
int oid_cmp(const void *p1, const void *p2)
Definition: oid.c:258
RepOriginId replorigin_by_name(const char *roname, bool missing_ok)
Definition: origin.c:225
RepOriginId replorigin_create(const char *roname)
Definition: origin.c:256
XLogRecPtr replorigin_get_progress(RepOriginId node, bool flush)
Definition: origin.c:1018
void replorigin_drop_by_name(const char *name, bool missing_ok, bool nowait)
Definition: origin.c:415
@ ALTER_SUBSCRIPTION_ENABLED
Definition: parsenodes.h:4239
@ ALTER_SUBSCRIPTION_DROP_PUBLICATION
Definition: parsenodes.h:4237
@ ALTER_SUBSCRIPTION_SET_PUBLICATION
Definition: parsenodes.h:4235
@ ALTER_SUBSCRIPTION_REFRESH
Definition: parsenodes.h:4238
@ ALTER_SUBSCRIPTION_SKIP
Definition: parsenodes.h:4240
@ ALTER_SUBSCRIPTION_OPTIONS
Definition: parsenodes.h:4233
@ ALTER_SUBSCRIPTION_CONNECTION
Definition: parsenodes.h:4234
@ ALTER_SUBSCRIPTION_ADD_PUBLICATION
Definition: parsenodes.h:4236
@ OBJECT_DATABASE
Definition: parsenodes.h:2277
@ OBJECT_SUBSCRIPTION
Definition: parsenodes.h:2306
#define ACL_CREATE
Definition: parsenodes.h:85
static AmcheckOptions opts
Definition: pg_amcheck.c:112
NameData relname
Definition: pg_class.h:38
#define NAMEDATALEN
static int server_version
Definition: pg_dumpall.c:110
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define foreach_delete_current(lst, var_or_cell)
Definition: pg_list.h:391
Datum pg_lsn_in(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:63
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:28
static XLogRecPtr DatumGetLSN(Datum X)
Definition: pg_lsn.h:22
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
Definition: pg_shdepend.c:316
void deleteSharedDependencyRecordsFor(Oid classId, Oid objectId, int32 objectSubId)
Definition: pg_shdepend.c:1047
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:168
List * GetSubscriptionRelations(Oid subid, bool not_ready)
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)
Subscription * GetSubscription(Oid subid, bool missing_ok)
NameData subname
FormData_pg_subscription * Form_pg_subscription
void pgstat_drop_subscription(Oid subid)
void pgstat_create_subscription(Oid subid)
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define qsort(a, b, c, d)
Definition: port.h:447
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Name DatumGetName(Datum X)
Definition: postgres.h:360
uintptr_t Datum
Definition: postgres.h:64
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
static Datum CharGetDatum(char X)
Definition: postgres.h:122
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
MemoryContextSwitchTo(old_ctx)
#define RelationGetDescr(relation)
Definition: rel.h:531
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:12870
bool ReplicationSlotValidateName(const char *name, int elevel)
Definition: slot.c:252
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:30
void destroyStringInfo(StringInfo str)
Definition: stringinfo.c:358
StringInfo makeStringInfo(void)
Definition: stringinfo.c:38
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:94
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:179
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:191
void initStringInfo(StringInfo str)
Definition: stringinfo.c:56
char * defname
Definition: parsenodes.h:817
Node * arg
Definition: parsenodes.h:818
ItemPointerData t_self
Definition: htup.h:65
Definition: pg_list.h:54
char * relname
Definition: primnodes.h:82
char * schemaname
Definition: primnodes.h:79
bool runasowner
bool copy_data
bits32 specified_opts
bool disableonerr
bool create_slot
char * synchronous_commit
char streaming
char * origin
char * slot_name
XLogRecPtr lsn
bool passwordrequired
Definition: regguts.h:323
struct SubOpts SubOpts
void DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
#define SUBOPT_STREAMING
char defGetStreamingMode(DefElem *def)
#define SUBOPT_CREATE_SLOT
#define SUBOPT_PASSWORD_REQUIRED
ObjectAddress CreateSubscription(ParseState *pstate, CreateSubscriptionStmt *stmt, bool isTopLevel)
#define SUBOPT_SYNCHRONOUS_COMMIT
#define SUBOPT_ENABLED
static void check_duplicates_in_publist(List *publist, Datum *datums)
static void CheckAlterSubOption(Subscription *sub, const char *option, bool slot_needs_update, bool isTopLevel)
#define SUBOPT_ORIGIN
static Datum publicationListToArray(List *publist)
#define SUBOPT_FAILOVER
static void parse_subscription_options(ParseState *pstate, List *stmt_options, bits32 supported_opts, SubOpts *opts)
static void check_publications(WalReceiverConn *wrconn, List *publications)
#define SUBOPT_RUN_AS_OWNER
#define SUBOPT_SLOT_NAME
#define SUBOPT_COPY_DATA
#define SUBOPT_TWOPHASE_COMMIT
static void AlterSubscription_refresh(Subscription *sub, bool copy_data, List *validate_publications)
static void ReportSlotConnectionError(List *rstates, Oid subid, char *slotname, char *err)
#define SUBOPT_DISABLE_ON_ERR
static void AlterSubscriptionOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
ObjectAddress AlterSubscriptionOwner(const char *name, Oid newOwnerId)
static void check_publications_origin(WalReceiverConn *wrconn, List *publications, bool copydata, char *origin, Oid *subrel_local_oids, int subrel_count, char *subname)
void ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missing_ok)
void AlterSubscriptionOwner_oid(Oid subid, Oid newOwnerId)
#define SUBOPT_LSN
static List * merge_publications(List *oldpublist, List *newpublist, bool addpub, const char *subname)
#define SUBOPT_BINARY
#define IsSet(val, bits)
#define SUBOPT_REFRESH
static List * fetch_table_list(WalReceiverConn *wrconn, List *publications)
#define SUBOPT_CONNECT
ObjectAddress AlterSubscription(ParseState *pstate, AlterSubscriptionStmt *stmt, bool isTopLevel)
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
bool superuser(void)
Definition: superuser.c:46
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:600
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:232
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:631
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:91
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:93
#define GetSysCacheOid2(cacheId, oidcol, key1, key2)
Definition: syscache.h:111
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
void ReplicationSlotNameForTablesync(Oid suboid, Oid relid, char *syncslotname, Size szslot)
Definition: tablesync.c:1274
void UpdateTwoPhaseState(Oid suboid, char new_state)
Definition: tablesync.c:1764
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1130
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:395
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:454
bool LookupGXactBySubid(Oid subid)
Definition: twophase.c:2813
String * makeString(char *str)
Definition: value.c:63
#define intVal(v)
Definition: value.h:79
#define strVal(v)
Definition: value.h:82
const char * name
static WalReceiverConn * wrconn
Definition: walreceiver.c:92
#define walrcv_connect(conninfo, replication, logical, must_use_password, appname, err)
Definition: walreceiver.h:435
@ WALRCV_OK_COMMAND
Definition: walreceiver.h:205
@ WALRCV_ERROR
Definition: walreceiver.h:204
@ WALRCV_OK_TUPLES
Definition: walreceiver.h:207
#define walrcv_create_slot(conn, slotname, temporary, two_phase, failover, snapshot_action, lsn)
Definition: walreceiver.h:459
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:471
#define walrcv_server_version(conn)
Definition: walreceiver.h:447
#define walrcv_check_conninfo(conninfo, must_use_password)
Definition: walreceiver.h:437
#define walrcv_alter_slot(conn, slotname, failover, two_phase)
Definition: walreceiver.h:461
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:465
#define walrcv_disconnect(conn)
Definition: walreceiver.h:467
@ CRS_NOEXPORT_SNAPSHOT
Definition: walsender.h:23
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3640
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint16 RepOriginId
Definition: xlogdefs.h:65
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28