PostgreSQL Source Code  git master
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-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  * 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/xact.h"
20 #include "catalog/catalog.h"
21 #include "catalog/dependency.h"
22 #include "catalog/indexing.h"
23 #include "catalog/namespace.h"
24 #include "catalog/objectaccess.h"
25 #include "catalog/objectaddress.h"
28 #include "catalog/pg_type.h"
29 #include "commands/defrem.h"
30 #include "commands/event_trigger.h"
32 #include "executor/executor.h"
33 #include "miscadmin.h"
34 #include "nodes/makefuncs.h"
36 #include "replication/origin.h"
37 #include "replication/slot.h"
39 #include "replication/walsender.h"
41 #include "storage/lmgr.h"
42 #include "utils/acl.h"
43 #include "utils/builtins.h"
44 #include "utils/guc.h"
45 #include "utils/lsyscache.h"
46 #include "utils/memutils.h"
47 #include "utils/syscache.h"
48 
49 /*
50  * Options that can be specified by the user in CREATE/ALTER SUBSCRIPTION
51  * command.
52  */
53 #define SUBOPT_CONNECT 0x00000001
54 #define SUBOPT_ENABLED 0x00000002
55 #define SUBOPT_CREATE_SLOT 0x00000004
56 #define SUBOPT_SLOT_NAME 0x00000008
57 #define SUBOPT_COPY_DATA 0x00000010
58 #define SUBOPT_SYNCHRONOUS_COMMIT 0x00000020
59 #define SUBOPT_REFRESH 0x00000040
60 #define SUBOPT_BINARY 0x00000080
61 #define SUBOPT_STREAMING 0x00000100
62 #define SUBOPT_TWOPHASE_COMMIT 0x00000200
63 
64 /* check if the 'val' has 'bits' set */
65 #define IsSet(val, bits) (((val) & (bits)) == (bits))
66 
67 /*
68  * Structure to hold a bitmap representing the user-provided CREATE/ALTER
69  * SUBSCRIPTION command options and the parsed/default values of each of them.
70  */
71 typedef struct SubOpts
72 {
74  char *slot_name;
76  bool connect;
77  bool enabled;
79  bool copy_data;
80  bool refresh;
81  bool binary;
82  bool streaming;
83  bool twophase;
84 } SubOpts;
85 
86 static List *fetch_table_list(WalReceiverConn *wrconn, List *publications);
87 static void check_duplicates_in_publist(List *publist, Datum *datums);
88 static List *merge_publications(List *oldpublist, List *newpublist, bool addpub, const char *subname);
89 static void ReportSlotConnectionError(List *rstates, Oid subid, char *slotname, char *err);
90 
91 
92 /*
93  * Common option parsing function for CREATE and ALTER SUBSCRIPTION commands.
94  *
95  * Since not all options can be specified in both commands, this function
96  * will report an error if mutually exclusive options are specified.
97  *
98  * Caller is expected to have cleared 'opts'.
99  */
100 static void
102  bits32 supported_opts, SubOpts *opts)
103 {
104  ListCell *lc;
105 
106  /* caller must expect some option */
107  Assert(supported_opts != 0);
108 
109  /* If connect option is supported, these others also need to be. */
110  Assert(!IsSet(supported_opts, SUBOPT_CONNECT) ||
111  IsSet(supported_opts, SUBOPT_ENABLED | SUBOPT_CREATE_SLOT |
113 
114  /* Set default values for the boolean supported options. */
115  if (IsSet(supported_opts, SUBOPT_CONNECT))
116  opts->connect = true;
117  if (IsSet(supported_opts, SUBOPT_ENABLED))
118  opts->enabled = true;
119  if (IsSet(supported_opts, SUBOPT_CREATE_SLOT))
120  opts->create_slot = true;
121  if (IsSet(supported_opts, SUBOPT_COPY_DATA))
122  opts->copy_data = true;
123  if (IsSet(supported_opts, SUBOPT_REFRESH))
124  opts->refresh = true;
125  if (IsSet(supported_opts, SUBOPT_BINARY))
126  opts->binary = false;
127  if (IsSet(supported_opts, SUBOPT_STREAMING))
128  opts->streaming = false;
129  if (IsSet(supported_opts, SUBOPT_TWOPHASE_COMMIT))
130  opts->twophase = false;
131 
132  /* Parse options */
133  foreach(lc, stmt_options)
134  {
135  DefElem *defel = (DefElem *) lfirst(lc);
136 
137  if (IsSet(supported_opts, SUBOPT_CONNECT) &&
138  strcmp(defel->defname, "connect") == 0)
139  {
140  if (IsSet(opts->specified_opts, SUBOPT_CONNECT))
141  errorConflictingDefElem(defel, pstate);
142 
144  opts->connect = defGetBoolean(defel);
145  }
146  else if (IsSet(supported_opts, SUBOPT_ENABLED) &&
147  strcmp(defel->defname, "enabled") == 0)
148  {
149  if (IsSet(opts->specified_opts, SUBOPT_ENABLED))
150  errorConflictingDefElem(defel, pstate);
151 
153  opts->enabled = defGetBoolean(defel);
154  }
155  else if (IsSet(supported_opts, SUBOPT_CREATE_SLOT) &&
156  strcmp(defel->defname, "create_slot") == 0)
157  {
159  errorConflictingDefElem(defel, pstate);
160 
162  opts->create_slot = defGetBoolean(defel);
163  }
164  else if (IsSet(supported_opts, SUBOPT_SLOT_NAME) &&
165  strcmp(defel->defname, "slot_name") == 0)
166  {
168  errorConflictingDefElem(defel, pstate);
169 
171  opts->slot_name = defGetString(defel);
172 
173  /* Setting slot_name = NONE is treated as no slot name. */
174  if (strcmp(opts->slot_name, "none") == 0)
175  opts->slot_name = NULL;
176  else
178  }
179  else if (IsSet(supported_opts, SUBOPT_COPY_DATA) &&
180  strcmp(defel->defname, "copy_data") == 0)
181  {
183  errorConflictingDefElem(defel, pstate);
184 
186  opts->copy_data = defGetBoolean(defel);
187  }
188  else if (IsSet(supported_opts, SUBOPT_SYNCHRONOUS_COMMIT) &&
189  strcmp(defel->defname, "synchronous_commit") == 0)
190  {
192  errorConflictingDefElem(defel, pstate);
193 
195  opts->synchronous_commit = defGetString(defel);
196 
197  /* Test if the given value is valid for synchronous_commit GUC. */
198  (void) set_config_option("synchronous_commit", opts->synchronous_commit,
200  false, 0, false);
201  }
202  else if (IsSet(supported_opts, SUBOPT_REFRESH) &&
203  strcmp(defel->defname, "refresh") == 0)
204  {
205  if (IsSet(opts->specified_opts, SUBOPT_REFRESH))
206  errorConflictingDefElem(defel, pstate);
207 
209  opts->refresh = defGetBoolean(defel);
210  }
211  else if (IsSet(supported_opts, SUBOPT_BINARY) &&
212  strcmp(defel->defname, "binary") == 0)
213  {
214  if (IsSet(opts->specified_opts, SUBOPT_BINARY))
215  errorConflictingDefElem(defel, pstate);
216 
217  opts->specified_opts |= SUBOPT_BINARY;
218  opts->binary = defGetBoolean(defel);
219  }
220  else if (IsSet(supported_opts, SUBOPT_STREAMING) &&
221  strcmp(defel->defname, "streaming") == 0)
222  {
224  errorConflictingDefElem(defel, pstate);
225 
227  opts->streaming = defGetBoolean(defel);
228  }
229  else if (strcmp(defel->defname, "two_phase") == 0)
230  {
231  /*
232  * Do not allow toggling of two_phase option. Doing so could cause
233  * missing of transactions and lead to an inconsistent replica.
234  * See comments atop worker.c
235  *
236  * Note: Unsupported twophase indicates that this call originated
237  * from AlterSubscription.
238  */
239  if (!IsSet(supported_opts, SUBOPT_TWOPHASE_COMMIT))
240  ereport(ERROR,
241  (errcode(ERRCODE_SYNTAX_ERROR),
242  errmsg("unrecognized subscription parameter: \"%s\"", defel->defname)));
243 
245  errorConflictingDefElem(defel, pstate);
246 
248  opts->twophase = defGetBoolean(defel);
249  }
250  else
251  ereport(ERROR,
252  (errcode(ERRCODE_SYNTAX_ERROR),
253  errmsg("unrecognized subscription parameter: \"%s\"", defel->defname)));
254  }
255 
256  /*
257  * We've been explicitly asked to not connect, that requires some
258  * additional processing.
259  */
260  if (!opts->connect && IsSet(supported_opts, SUBOPT_CONNECT))
261  {
262  /* Check for incompatible options from the user. */
263  if (opts->enabled &&
264  IsSet(supported_opts, SUBOPT_ENABLED) &&
266  ereport(ERROR,
267  (errcode(ERRCODE_SYNTAX_ERROR),
268  /*- translator: both %s are strings of the form "option = value" */
269  errmsg("%s and %s are mutually exclusive options",
270  "connect = false", "enabled = true")));
271 
272  if (opts->create_slot &&
273  IsSet(supported_opts, SUBOPT_CREATE_SLOT) &&
275  ereport(ERROR,
276  (errcode(ERRCODE_SYNTAX_ERROR),
277  errmsg("%s and %s are mutually exclusive options",
278  "connect = false", "create_slot = true")));
279 
280  if (opts->copy_data &&
281  IsSet(supported_opts, SUBOPT_COPY_DATA) &&
283  ereport(ERROR,
284  (errcode(ERRCODE_SYNTAX_ERROR),
285  errmsg("%s and %s are mutually exclusive options",
286  "connect = false", "copy_data = true")));
287 
288  /* Change the defaults of other options. */
289  opts->enabled = false;
290  opts->create_slot = false;
291  opts->copy_data = false;
292  }
293 
294  /*
295  * Do additional checking for disallowed combination when slot_name = NONE
296  * was used.
297  */
298  if (!opts->slot_name &&
299  IsSet(supported_opts, SUBOPT_SLOT_NAME) &&
301  {
302  if (opts->enabled &&
303  IsSet(supported_opts, SUBOPT_ENABLED) &&
305  ereport(ERROR,
306  (errcode(ERRCODE_SYNTAX_ERROR),
307  /*- translator: both %s are strings of the form "option = value" */
308  errmsg("%s and %s are mutually exclusive options",
309  "slot_name = NONE", "enabled = true")));
310 
311  if (opts->create_slot &&
312  IsSet(supported_opts, SUBOPT_CREATE_SLOT) &&
314  ereport(ERROR,
315  (errcode(ERRCODE_SYNTAX_ERROR),
316  /*- translator: both %s are strings of the form "option = value" */
317  errmsg("%s and %s are mutually exclusive options",
318  "slot_name = NONE", "create_slot = true")));
319 
320  if (opts->enabled &&
321  IsSet(supported_opts, SUBOPT_ENABLED) &&
323  ereport(ERROR,
324  (errcode(ERRCODE_SYNTAX_ERROR),
325  /*- translator: both %s are strings of the form "option = value" */
326  errmsg("subscription with %s must also set %s",
327  "slot_name = NONE", "enabled = false")));
328 
329  if (opts->create_slot &&
330  IsSet(supported_opts, SUBOPT_CREATE_SLOT) &&
332  ereport(ERROR,
333  (errcode(ERRCODE_SYNTAX_ERROR),
334  /*- translator: both %s are strings of the form "option = value" */
335  errmsg("subscription with %s must also set %s",
336  "slot_name = NONE", "create_slot = false")));
337  }
338 }
339 
340 /*
341  * Auxiliary function to build a text array out of a list of String nodes.
342  */
343 static Datum
345 {
346  ArrayType *arr;
347  Datum *datums;
348  MemoryContext memcxt;
349  MemoryContext oldcxt;
350 
351  /* Create memory context for temporary allocations. */
353  "publicationListToArray to array",
355  oldcxt = MemoryContextSwitchTo(memcxt);
356 
357  datums = (Datum *) palloc(sizeof(Datum) * list_length(publist));
358 
359  check_duplicates_in_publist(publist, datums);
360 
361  MemoryContextSwitchTo(oldcxt);
362 
363  arr = construct_array(datums, list_length(publist),
364  TEXTOID, -1, false, TYPALIGN_INT);
365 
366  MemoryContextDelete(memcxt);
367 
368  return PointerGetDatum(arr);
369 }
370 
371 /*
372  * Create new subscription.
373  */
376  bool isTopLevel)
377 {
378  Relation rel;
379  ObjectAddress myself;
380  Oid subid;
381  bool nulls[Natts_pg_subscription];
382  Datum values[Natts_pg_subscription];
383  Oid owner = GetUserId();
384  HeapTuple tup;
385  char *conninfo;
386  char originname[NAMEDATALEN];
387  List *publications;
388  bits32 supported_opts;
389  SubOpts opts = {0};
390 
391  /*
392  * Parse and check options.
393  *
394  * Connection and publication should not be specified here.
395  */
396  supported_opts = (SUBOPT_CONNECT | SUBOPT_ENABLED | SUBOPT_CREATE_SLOT |
400  parse_subscription_options(pstate, stmt->options, supported_opts, &opts);
401 
402  /*
403  * Since creating a replication slot is not transactional, rolling back
404  * the transaction leaves the created replication slot. So we cannot run
405  * CREATE SUBSCRIPTION inside a transaction block if creating a
406  * replication slot.
407  */
408  if (opts.create_slot)
409  PreventInTransactionBlock(isTopLevel, "CREATE SUBSCRIPTION ... WITH (create_slot = true)");
410 
411  if (!superuser())
412  ereport(ERROR,
413  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
414  errmsg("must be superuser to create subscriptions")));
415 
416  /*
417  * If built with appropriate switch, whine when regression-testing
418  * conventions for subscription names are violated.
419  */
420 #ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
421  if (strncmp(stmt->subname, "regress_", 8) != 0)
422  elog(WARNING, "subscriptions created by regression test cases should have names starting with \"regress_\"");
423 #endif
424 
425  rel = table_open(SubscriptionRelationId, RowExclusiveLock);
426 
427  /* Check if name is used */
428  subid = GetSysCacheOid2(SUBSCRIPTIONNAME, Anum_pg_subscription_oid,
430  if (OidIsValid(subid))
431  {
432  ereport(ERROR,
434  errmsg("subscription \"%s\" already exists",
435  stmt->subname)));
436  }
437 
438  if (!IsSet(opts.specified_opts, SUBOPT_SLOT_NAME) &&
439  opts.slot_name == NULL)
440  opts.slot_name = stmt->subname;
441 
442  /* The default for synchronous_commit of subscriptions is off. */
443  if (opts.synchronous_commit == NULL)
444  opts.synchronous_commit = "off";
445 
446  conninfo = stmt->conninfo;
447  publications = stmt->publication;
448 
449  /* Load the library providing us libpq calls. */
450  load_file("libpqwalreceiver", false);
451 
452  /* Check the connection info string. */
453  walrcv_check_conninfo(conninfo);
454 
455  /* Everything ok, form a new tuple. */
456  memset(values, 0, sizeof(values));
457  memset(nulls, false, sizeof(nulls));
458 
459  subid = GetNewOidWithIndex(rel, SubscriptionObjectIndexId,
460  Anum_pg_subscription_oid);
461  values[Anum_pg_subscription_oid - 1] = ObjectIdGetDatum(subid);
462  values[Anum_pg_subscription_subdbid - 1] = ObjectIdGetDatum(MyDatabaseId);
463  values[Anum_pg_subscription_subname - 1] =
465  values[Anum_pg_subscription_subowner - 1] = ObjectIdGetDatum(owner);
466  values[Anum_pg_subscription_subenabled - 1] = BoolGetDatum(opts.enabled);
467  values[Anum_pg_subscription_subbinary - 1] = BoolGetDatum(opts.binary);
468  values[Anum_pg_subscription_substream - 1] = BoolGetDatum(opts.streaming);
469  values[Anum_pg_subscription_subtwophasestate - 1] =
470  CharGetDatum(opts.twophase ?
473  values[Anum_pg_subscription_subconninfo - 1] =
474  CStringGetTextDatum(conninfo);
475  if (opts.slot_name)
476  values[Anum_pg_subscription_subslotname - 1] =
478  else
479  nulls[Anum_pg_subscription_subslotname - 1] = true;
480  values[Anum_pg_subscription_subsynccommit - 1] =
482  values[Anum_pg_subscription_subpublications - 1] =
483  publicationListToArray(publications);
484 
485  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
486 
487  /* Insert tuple into catalog. */
488  CatalogTupleInsert(rel, tup);
489  heap_freetuple(tup);
490 
491  recordDependencyOnOwner(SubscriptionRelationId, subid, owner);
492 
493  snprintf(originname, sizeof(originname), "pg_%u", subid);
494  replorigin_create(originname);
495 
496  /*
497  * Connect to remote side to execute requested commands and fetch table
498  * info.
499  */
500  if (opts.connect)
501  {
502  char *err;
504  List *tables;
505  ListCell *lc;
506  char table_state;
507 
508  /* Try to connect to the publisher. */
509  wrconn = walrcv_connect(conninfo, true, stmt->subname, &err);
510  if (!wrconn)
511  ereport(ERROR,
512  (errcode(ERRCODE_CONNECTION_FAILURE),
513  errmsg("could not connect to the publisher: %s", err)));
514 
515  PG_TRY();
516  {
517  /*
518  * Set sync state based on if we were asked to do data copy or
519  * not.
520  */
521  table_state = opts.copy_data ? SUBREL_STATE_INIT : SUBREL_STATE_READY;
522 
523  /*
524  * Get the table list from publisher and build local table status
525  * info.
526  */
527  tables = fetch_table_list(wrconn, publications);
528  foreach(lc, tables)
529  {
530  RangeVar *rv = (RangeVar *) lfirst(lc);
531  Oid relid;
532 
533  relid = RangeVarGetRelid(rv, AccessShareLock, false);
534 
535  /* Check for supported relkind. */
537  rv->schemaname, rv->relname);
538 
539  AddSubscriptionRelState(subid, relid, table_state,
541  }
542 
543  /*
544  * If requested, create permanent slot for the subscription. We
545  * won't use the initial snapshot for anything, so no need to
546  * export it.
547  */
548  if (opts.create_slot)
549  {
550  bool twophase_enabled = false;
551 
552  Assert(opts.slot_name);
553 
554  /*
555  * Even if two_phase is set, don't create the slot with
556  * two-phase enabled. Will enable it once all the tables are
557  * synced and ready. This avoids race-conditions like prepared
558  * transactions being skipped due to changes not being applied
559  * due to checks in should_apply_changes_for_rel() when
560  * tablesync for the corresponding tables are in progress. See
561  * comments atop worker.c.
562  *
563  * Note that if tables were specified but copy_data is false
564  * then it is safe to enable two_phase up-front because those
565  * tables are already initially in READY state. When the
566  * subscription has no tables, we leave the twophase state as
567  * PENDING, to allow ALTER SUBSCRIPTION ... REFRESH
568  * PUBLICATION to work.
569  */
570  if (opts.twophase && !opts.copy_data && tables != NIL)
571  twophase_enabled = true;
572 
573  walrcv_create_slot(wrconn, opts.slot_name, false, twophase_enabled,
574  CRS_NOEXPORT_SNAPSHOT, NULL);
575 
576  if (twophase_enabled)
578 
579  ereport(NOTICE,
580  (errmsg("created replication slot \"%s\" on publisher",
581  opts.slot_name)));
582  }
583  }
584  PG_FINALLY();
585  {
586  walrcv_disconnect(wrconn);
587  }
588  PG_END_TRY();
589  }
590  else
592  /* translator: %s is an SQL ALTER statement */
593  (errmsg("tables were not subscribed, you will have to run %s to subscribe the tables",
594  "ALTER SUBSCRIPTION ... REFRESH PUBLICATION")));
595 
597 
598  if (opts.enabled)
600 
601  ObjectAddressSet(myself, SubscriptionRelationId, subid);
602 
603  InvokeObjectPostCreateHook(SubscriptionRelationId, subid, 0);
604 
605  return myself;
606 }
607 
608 static void
610 {
611  char *err;
612  List *pubrel_names;
613  List *subrel_states;
614  Oid *subrel_local_oids;
615  Oid *pubrel_local_oids;
616  ListCell *lc;
617  int off;
618  int remove_rel_len;
619  Relation rel = NULL;
620  typedef struct SubRemoveRels
621  {
622  Oid relid;
623  char state;
624  } SubRemoveRels;
625  SubRemoveRels *sub_remove_rels;
627 
628  /* Load the library providing us libpq calls. */
629  load_file("libpqwalreceiver", false);
630 
631  /* Try to connect to the publisher. */
632  wrconn = walrcv_connect(sub->conninfo, true, sub->name, &err);
633  if (!wrconn)
634  ereport(ERROR,
635  (errcode(ERRCODE_CONNECTION_FAILURE),
636  errmsg("could not connect to the publisher: %s", err)));
637 
638  PG_TRY();
639  {
640  /* Get the table list from publisher. */
641  pubrel_names = fetch_table_list(wrconn, sub->publications);
642 
643  /* Get local table list. */
644  subrel_states = GetSubscriptionRelations(sub->oid);
645 
646  /*
647  * Build qsorted array of local table oids for faster lookup. This can
648  * potentially contain all tables in the database so speed of lookup
649  * is important.
650  */
651  subrel_local_oids = palloc(list_length(subrel_states) * sizeof(Oid));
652  off = 0;
653  foreach(lc, subrel_states)
654  {
656 
657  subrel_local_oids[off++] = relstate->relid;
658  }
659  qsort(subrel_local_oids, list_length(subrel_states),
660  sizeof(Oid), oid_cmp);
661 
662  /*
663  * Rels that we want to remove from subscription and drop any slots
664  * and origins corresponding to them.
665  */
666  sub_remove_rels = palloc(list_length(subrel_states) * sizeof(SubRemoveRels));
667 
668  /*
669  * Walk over the remote tables and try to match them to locally known
670  * tables. If the table is not known locally create a new state for
671  * it.
672  *
673  * Also builds array of local oids of remote tables for the next step.
674  */
675  off = 0;
676  pubrel_local_oids = palloc(list_length(pubrel_names) * sizeof(Oid));
677 
678  foreach(lc, pubrel_names)
679  {
680  RangeVar *rv = (RangeVar *) lfirst(lc);
681  Oid relid;
682 
683  relid = RangeVarGetRelid(rv, AccessShareLock, false);
684 
685  /* Check for supported relkind. */
687  rv->schemaname, rv->relname);
688 
689  pubrel_local_oids[off++] = relid;
690 
691  if (!bsearch(&relid, subrel_local_oids,
692  list_length(subrel_states), sizeof(Oid), oid_cmp))
693  {
694  AddSubscriptionRelState(sub->oid, relid,
695  copy_data ? SUBREL_STATE_INIT : SUBREL_STATE_READY,
697  ereport(DEBUG1,
698  (errmsg_internal("table \"%s.%s\" added to subscription \"%s\"",
699  rv->schemaname, rv->relname, sub->name)));
700  }
701  }
702 
703  /*
704  * Next remove state for tables we should not care about anymore using
705  * the data we collected above
706  */
707  qsort(pubrel_local_oids, list_length(pubrel_names),
708  sizeof(Oid), oid_cmp);
709 
710  remove_rel_len = 0;
711  for (off = 0; off < list_length(subrel_states); off++)
712  {
713  Oid relid = subrel_local_oids[off];
714 
715  if (!bsearch(&relid, pubrel_local_oids,
716  list_length(pubrel_names), sizeof(Oid), oid_cmp))
717  {
718  char state;
719  XLogRecPtr statelsn;
720 
721  /*
722  * Lock pg_subscription_rel with AccessExclusiveLock to
723  * prevent any race conditions with the apply worker
724  * re-launching workers at the same time this code is trying
725  * to remove those tables.
726  *
727  * Even if new worker for this particular rel is restarted it
728  * won't be able to make any progress as we hold exclusive
729  * lock on subscription_rel till the transaction end. It will
730  * simply exit as there is no corresponding rel entry.
731  *
732  * This locking also ensures that the state of rels won't
733  * change till we are done with this refresh operation.
734  */
735  if (!rel)
736  rel = table_open(SubscriptionRelRelationId, AccessExclusiveLock);
737 
738  /* Last known rel state. */
739  state = GetSubscriptionRelState(sub->oid, relid, &statelsn);
740 
741  sub_remove_rels[remove_rel_len].relid = relid;
742  sub_remove_rels[remove_rel_len++].state = state;
743 
744  RemoveSubscriptionRel(sub->oid, relid);
745 
746  logicalrep_worker_stop(sub->oid, relid);
747 
748  /*
749  * For READY state, we would have already dropped the
750  * tablesync origin.
751  */
752  if (state != SUBREL_STATE_READY)
753  {
754  char originname[NAMEDATALEN];
755 
756  /*
757  * Drop the tablesync's origin tracking if exists.
758  *
759  * It is possible that the origin is not yet created for
760  * tablesync worker, this can happen for the states before
761  * SUBREL_STATE_FINISHEDCOPY. The apply worker can also
762  * concurrently try to drop the origin and by this time
763  * the origin might be already removed. For these reasons,
764  * passing missing_ok = true.
765  */
766  ReplicationOriginNameForTablesync(sub->oid, relid, originname,
767  sizeof(originname));
768  replorigin_drop_by_name(originname, true, false);
769  }
770 
771  ereport(DEBUG1,
772  (errmsg_internal("table \"%s.%s\" removed from subscription \"%s\"",
774  get_rel_name(relid),
775  sub->name)));
776  }
777  }
778 
779  /*
780  * Drop the tablesync slots associated with removed tables. This has
781  * to be at the end because otherwise if there is an error while doing
782  * the database operations we won't be able to rollback dropped slots.
783  */
784  for (off = 0; off < remove_rel_len; off++)
785  {
786  if (sub_remove_rels[off].state != SUBREL_STATE_READY &&
787  sub_remove_rels[off].state != SUBREL_STATE_SYNCDONE)
788  {
789  char syncslotname[NAMEDATALEN] = {0};
790 
791  /*
792  * For READY/SYNCDONE states we know the tablesync slot has
793  * already been dropped by the tablesync worker.
794  *
795  * For other states, there is no certainty, maybe the slot
796  * does not exist yet. Also, if we fail after removing some of
797  * the slots, next time, it will again try to drop already
798  * dropped slots and fail. For these reasons, we allow
799  * missing_ok = true for the drop.
800  */
801  ReplicationSlotNameForTablesync(sub->oid, sub_remove_rels[off].relid,
802  syncslotname, sizeof(syncslotname));
803  ReplicationSlotDropAtPubNode(wrconn, syncslotname, true);
804  }
805  }
806  }
807  PG_FINALLY();
808  {
809  walrcv_disconnect(wrconn);
810  }
811  PG_END_TRY();
812 
813  if (rel)
814  table_close(rel, NoLock);
815 }
816 
817 /*
818  * Alter the existing subscription.
819  */
822  bool isTopLevel)
823 {
824  Relation rel;
825  ObjectAddress myself;
826  bool nulls[Natts_pg_subscription];
827  bool replaces[Natts_pg_subscription];
828  Datum values[Natts_pg_subscription];
829  HeapTuple tup;
830  Oid subid;
831  bool update_tuple = false;
832  Subscription *sub;
834  bits32 supported_opts;
835  SubOpts opts = {0};
836 
837  rel = table_open(SubscriptionRelationId, RowExclusiveLock);
838 
839  /* Fetch the existing tuple. */
841  CStringGetDatum(stmt->subname));
842 
843  if (!HeapTupleIsValid(tup))
844  ereport(ERROR,
845  (errcode(ERRCODE_UNDEFINED_OBJECT),
846  errmsg("subscription \"%s\" does not exist",
847  stmt->subname)));
848 
849  form = (Form_pg_subscription) GETSTRUCT(tup);
850  subid = form->oid;
851 
852  /* must be owner */
853  if (!pg_subscription_ownercheck(subid, GetUserId()))
855  stmt->subname);
856 
857  sub = GetSubscription(subid, false);
858 
859  /* Lock the subscription so nobody else can do anything with it. */
860  LockSharedObject(SubscriptionRelationId, subid, 0, AccessExclusiveLock);
861 
862  /* Form a new tuple. */
863  memset(values, 0, sizeof(values));
864  memset(nulls, false, sizeof(nulls));
865  memset(replaces, false, sizeof(replaces));
866 
867  switch (stmt->kind)
868  {
870  {
871  supported_opts = (SUBOPT_SLOT_NAME |
874 
875  parse_subscription_options(pstate, stmt->options,
876  supported_opts, &opts);
877 
879  {
880  /*
881  * The subscription must be disabled to allow slot_name as
882  * 'none', otherwise, the apply worker will repeatedly try
883  * to stream the data using that slot_name which neither
884  * exists on the publisher nor the user will be allowed to
885  * create it.
886  */
887  if (sub->enabled && !opts.slot_name)
888  ereport(ERROR,
889  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
890  errmsg("cannot set %s for enabled subscription",
891  "slot_name = NONE")));
892 
893  if (opts.slot_name)
894  values[Anum_pg_subscription_subslotname - 1] =
896  else
897  nulls[Anum_pg_subscription_subslotname - 1] = true;
898  replaces[Anum_pg_subscription_subslotname - 1] = true;
899  }
900 
901  if (opts.synchronous_commit)
902  {
903  values[Anum_pg_subscription_subsynccommit - 1] =
905  replaces[Anum_pg_subscription_subsynccommit - 1] = true;
906  }
907 
909  {
910  values[Anum_pg_subscription_subbinary - 1] =
911  BoolGetDatum(opts.binary);
912  replaces[Anum_pg_subscription_subbinary - 1] = true;
913  }
914 
916  {
917  values[Anum_pg_subscription_substream - 1] =
918  BoolGetDatum(opts.streaming);
919  replaces[Anum_pg_subscription_substream - 1] = true;
920  }
921 
922  update_tuple = true;
923  break;
924  }
925 
927  {
928  parse_subscription_options(pstate, stmt->options,
929  SUBOPT_ENABLED, &opts);
931 
932  if (!sub->slotname && opts.enabled)
933  ereport(ERROR,
934  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
935  errmsg("cannot enable subscription that does not have a slot name")));
936 
937  values[Anum_pg_subscription_subenabled - 1] =
938  BoolGetDatum(opts.enabled);
939  replaces[Anum_pg_subscription_subenabled - 1] = true;
940 
941  if (opts.enabled)
943 
944  update_tuple = true;
945  break;
946  }
947 
949  /* Load the library providing us libpq calls. */
950  load_file("libpqwalreceiver", false);
951  /* Check the connection info string. */
953 
954  values[Anum_pg_subscription_subconninfo - 1] =
956  replaces[Anum_pg_subscription_subconninfo - 1] = true;
957  update_tuple = true;
958  break;
959 
961  {
962  supported_opts = SUBOPT_COPY_DATA | SUBOPT_REFRESH;
963  parse_subscription_options(pstate, stmt->options,
964  supported_opts, &opts);
965 
966  values[Anum_pg_subscription_subpublications - 1] =
968  replaces[Anum_pg_subscription_subpublications - 1] = true;
969 
970  update_tuple = true;
971 
972  /* Refresh if user asked us to. */
973  if (opts.refresh)
974  {
975  if (!sub->enabled)
976  ereport(ERROR,
977  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
978  errmsg("ALTER SUBSCRIPTION with refresh is not allowed for disabled subscriptions"),
979  errhint("Use ALTER SUBSCRIPTION ... SET PUBLICATION ... WITH (refresh = false).")));
980 
981  /*
982  * See ALTER_SUBSCRIPTION_REFRESH for details why this is
983  * not allowed.
984  */
986  ereport(ERROR,
987  (errcode(ERRCODE_SYNTAX_ERROR),
988  errmsg("ALTER SUBSCRIPTION with refresh and copy_data is not allowed when two_phase is enabled"),
989  errhint("Use ALTER SUBSCRIPTION ...SET PUBLICATION with refresh = false, or with copy_data = false"
990  ", or use DROP/CREATE SUBSCRIPTION.")));
991 
992  PreventInTransactionBlock(isTopLevel, "ALTER SUBSCRIPTION with refresh");
993 
994  /* Make sure refresh sees the new list of publications. */
995  sub->publications = stmt->publication;
996 
998  }
999 
1000  break;
1001  }
1002 
1005  {
1006  List *publist;
1007  bool isadd = stmt->kind == ALTER_SUBSCRIPTION_ADD_PUBLICATION;
1008 
1009  supported_opts = SUBOPT_REFRESH;
1010  if (isadd)
1011  supported_opts |= SUBOPT_COPY_DATA;
1012 
1013  parse_subscription_options(pstate, stmt->options,
1014  supported_opts, &opts);
1015 
1016  publist = merge_publications(sub->publications, stmt->publication, isadd, stmt->subname);
1017  values[Anum_pg_subscription_subpublications - 1] =
1018  publicationListToArray(publist);
1019  replaces[Anum_pg_subscription_subpublications - 1] = true;
1020 
1021  update_tuple = true;
1022 
1023  /* Refresh if user asked us to. */
1024  if (opts.refresh)
1025  {
1026  if (!sub->enabled)
1027  ereport(ERROR,
1028  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1029  errmsg("ALTER SUBSCRIPTION with refresh is not allowed for disabled subscriptions"),
1030  errhint("Use ALTER SUBSCRIPTION ... SET PUBLICATION ... WITH (refresh = false).")));
1031 
1032  /*
1033  * See ALTER_SUBSCRIPTION_REFRESH for details why this is
1034  * not allowed.
1035  */
1037  ereport(ERROR,
1038  (errcode(ERRCODE_SYNTAX_ERROR),
1039  errmsg("ALTER SUBSCRIPTION with refresh and copy_data is not allowed when two_phase is enabled"),
1040  errhint("Use ALTER SUBSCRIPTION ...SET PUBLICATION with refresh = false, or with copy_data = false"
1041  ", or use DROP/CREATE SUBSCRIPTION.")));
1042 
1043  PreventInTransactionBlock(isTopLevel, "ALTER SUBSCRIPTION with refresh");
1044 
1045  /* Only refresh the added/dropped list of publications. */
1046  sub->publications = stmt->publication;
1047 
1049  }
1050 
1051  break;
1052  }
1053 
1055  {
1056  if (!sub->enabled)
1057  ereport(ERROR,
1058  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1059  errmsg("ALTER SUBSCRIPTION ... REFRESH is not allowed for disabled subscriptions")));
1060 
1061  parse_subscription_options(pstate, stmt->options,
1062  SUBOPT_COPY_DATA, &opts);
1063 
1064  /*
1065  * The subscription option "two_phase" requires that
1066  * replication has passed the initial table synchronization
1067  * phase before the two_phase becomes properly enabled.
1068  *
1069  * But, having reached this two-phase commit "enabled" state
1070  * we must not allow any subsequent table initialization to
1071  * occur. So the ALTER SUBSCRIPTION ... REFRESH is disallowed
1072  * when the user had requested two_phase = on mode.
1073  *
1074  * The exception to this restriction is when copy_data =
1075  * false, because when copy_data is false the tablesync will
1076  * start already in READY state and will exit directly without
1077  * doing anything.
1078  *
1079  * For more details see comments atop worker.c.
1080  */
1082  ereport(ERROR,
1083  (errcode(ERRCODE_SYNTAX_ERROR),
1084  errmsg("ALTER SUBSCRIPTION ... REFRESH with copy_data is not allowed when two_phase is enabled"),
1085  errhint("Use ALTER SUBSCRIPTION ... REFRESH with copy_data = false"
1086  ", or use DROP/CREATE SUBSCRIPTION.")));
1087 
1088  PreventInTransactionBlock(isTopLevel, "ALTER SUBSCRIPTION ... REFRESH");
1089 
1091 
1092  break;
1093  }
1094 
1095  default:
1096  elog(ERROR, "unrecognized ALTER SUBSCRIPTION kind %d",
1097  stmt->kind);
1098  }
1099 
1100  /* Update the catalog if needed. */
1101  if (update_tuple)
1102  {
1103  tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
1104  replaces);
1105 
1106  CatalogTupleUpdate(rel, &tup->t_self, tup);
1107 
1108  heap_freetuple(tup);
1109  }
1110 
1112 
1113  ObjectAddressSet(myself, SubscriptionRelationId, subid);
1114 
1115  InvokeObjectPostAlterHook(SubscriptionRelationId, subid, 0);
1116 
1117  return myself;
1118 }
1119 
1120 /*
1121  * Drop a subscription
1122  */
1123 void
1125 {
1126  Relation rel;
1127  ObjectAddress myself;
1128  HeapTuple tup;
1129  Oid subid;
1130  Datum datum;
1131  bool isnull;
1132  char *subname;
1133  char *conninfo;
1134  char *slotname;
1135  List *subworkers;
1136  ListCell *lc;
1137  char originname[NAMEDATALEN];
1138  char *err = NULL;
1140  Form_pg_subscription form;
1141  List *rstates;
1142 
1143  /*
1144  * Lock pg_subscription with AccessExclusiveLock to ensure that the
1145  * launcher doesn't restart new worker during dropping the subscription
1146  */
1147  rel = table_open(SubscriptionRelationId, AccessExclusiveLock);
1148 
1150  CStringGetDatum(stmt->subname));
1151 
1152  if (!HeapTupleIsValid(tup))
1153  {
1154  table_close(rel, NoLock);
1155 
1156  if (!stmt->missing_ok)
1157  ereport(ERROR,
1158  (errcode(ERRCODE_UNDEFINED_OBJECT),
1159  errmsg("subscription \"%s\" does not exist",
1160  stmt->subname)));
1161  else
1162  ereport(NOTICE,
1163  (errmsg("subscription \"%s\" does not exist, skipping",
1164  stmt->subname)));
1165 
1166  return;
1167  }
1168 
1169  form = (Form_pg_subscription) GETSTRUCT(tup);
1170  subid = form->oid;
1171 
1172  /* must be owner */
1173  if (!pg_subscription_ownercheck(subid, GetUserId()))
1175  stmt->subname);
1176 
1177  /* DROP hook for the subscription being removed */
1178  InvokeObjectDropHook(SubscriptionRelationId, subid, 0);
1179 
1180  /*
1181  * Lock the subscription so nobody else can do anything with it (including
1182  * the replication workers).
1183  */
1184  LockSharedObject(SubscriptionRelationId, subid, 0, AccessExclusiveLock);
1185 
1186  /* Get subname */
1187  datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
1188  Anum_pg_subscription_subname, &isnull);
1189  Assert(!isnull);
1190  subname = pstrdup(NameStr(*DatumGetName(datum)));
1191 
1192  /* Get conninfo */
1193  datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
1194  Anum_pg_subscription_subconninfo, &isnull);
1195  Assert(!isnull);
1196  conninfo = TextDatumGetCString(datum);
1197 
1198  /* Get slotname */
1199  datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
1200  Anum_pg_subscription_subslotname, &isnull);
1201  if (!isnull)
1202  slotname = pstrdup(NameStr(*DatumGetName(datum)));
1203  else
1204  slotname = NULL;
1205 
1206  /*
1207  * Since dropping a replication slot is not transactional, the replication
1208  * slot stays dropped even if the transaction rolls back. So we cannot
1209  * run DROP SUBSCRIPTION inside a transaction block if dropping the
1210  * replication slot.
1211  *
1212  * XXX The command name should really be something like "DROP SUBSCRIPTION
1213  * of a subscription that is associated with a replication slot", but we
1214  * don't have the proper facilities for that.
1215  */
1216  if (slotname)
1217  PreventInTransactionBlock(isTopLevel, "DROP SUBSCRIPTION");
1218 
1219  ObjectAddressSet(myself, SubscriptionRelationId, subid);
1220  EventTriggerSQLDropAddObject(&myself, true, true);
1221 
1222  /* Remove the tuple from catalog. */
1223  CatalogTupleDelete(rel, &tup->t_self);
1224 
1225  ReleaseSysCache(tup);
1226 
1227  /*
1228  * Stop all the subscription workers immediately.
1229  *
1230  * This is necessary if we are dropping the replication slot, so that the
1231  * slot becomes accessible.
1232  *
1233  * It is also necessary if the subscription is disabled and was disabled
1234  * in the same transaction. Then the workers haven't seen the disabling
1235  * yet and will still be running, leading to hangs later when we want to
1236  * drop the replication origin. If the subscription was disabled before
1237  * this transaction, then there shouldn't be any workers left, so this
1238  * won't make a difference.
1239  *
1240  * New workers won't be started because we hold an exclusive lock on the
1241  * subscription till the end of the transaction.
1242  */
1243  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1244  subworkers = logicalrep_workers_find(subid, false);
1245  LWLockRelease(LogicalRepWorkerLock);
1246  foreach(lc, subworkers)
1247  {
1249 
1251  }
1252  list_free(subworkers);
1253 
1254  /*
1255  * Cleanup of tablesync replication origins.
1256  *
1257  * Any READY-state relations would already have dealt with clean-ups.
1258  *
1259  * Note that the state can't change because we have already stopped both
1260  * the apply and tablesync workers and they can't restart because of
1261  * exclusive lock on the subscription.
1262  */
1263  rstates = GetSubscriptionNotReadyRelations(subid);
1264  foreach(lc, rstates)
1265  {
1267  Oid relid = rstate->relid;
1268 
1269  /* Only cleanup resources of tablesync workers */
1270  if (!OidIsValid(relid))
1271  continue;
1272 
1273  /*
1274  * Drop the tablesync's origin tracking if exists.
1275  *
1276  * It is possible that the origin is not yet created for tablesync
1277  * worker so passing missing_ok = true. This can happen for the states
1278  * before SUBREL_STATE_FINISHEDCOPY.
1279  */
1280  ReplicationOriginNameForTablesync(subid, relid, originname,
1281  sizeof(originname));
1282  replorigin_drop_by_name(originname, true, false);
1283  }
1284 
1285  /* Clean up dependencies */
1286  deleteSharedDependencyRecordsFor(SubscriptionRelationId, subid, 0);
1287 
1288  /* Remove any associated relation synchronization states. */
1290 
1291  /* Remove the origin tracking if exists. */
1292  snprintf(originname, sizeof(originname), "pg_%u", subid);
1293  replorigin_drop_by_name(originname, true, false);
1294 
1295  /*
1296  * If there is no slot associated with the subscription, we can finish
1297  * here.
1298  */
1299  if (!slotname && rstates == NIL)
1300  {
1301  table_close(rel, NoLock);
1302  return;
1303  }
1304 
1305  /*
1306  * Try to acquire the connection necessary for dropping slots.
1307  *
1308  * Note: If the slotname is NONE/NULL then we allow the command to finish
1309  * and users need to manually cleanup the apply and tablesync worker slots
1310  * later.
1311  *
1312  * This has to be at the end because otherwise if there is an error while
1313  * doing the database operations we won't be able to rollback dropped
1314  * slot.
1315  */
1316  load_file("libpqwalreceiver", false);
1317 
1318  wrconn = walrcv_connect(conninfo, true, subname, &err);
1319  if (wrconn == NULL)
1320  {
1321  if (!slotname)
1322  {
1323  /* be tidy */
1324  list_free(rstates);
1325  table_close(rel, NoLock);
1326  return;
1327  }
1328  else
1329  {
1330  ReportSlotConnectionError(rstates, subid, slotname, err);
1331  }
1332  }
1333 
1334  PG_TRY();
1335  {
1336  foreach(lc, rstates)
1337  {
1339  Oid relid = rstate->relid;
1340 
1341  /* Only cleanup resources of tablesync workers */
1342  if (!OidIsValid(relid))
1343  continue;
1344 
1345  /*
1346  * Drop the tablesync slots associated with removed tables.
1347  *
1348  * For SYNCDONE/READY states, the tablesync slot is known to have
1349  * already been dropped by the tablesync worker.
1350  *
1351  * For other states, there is no certainty, maybe the slot does
1352  * not exist yet. Also, if we fail after removing some of the
1353  * slots, next time, it will again try to drop already dropped
1354  * slots and fail. For these reasons, we allow missing_ok = true
1355  * for the drop.
1356  */
1357  if (rstate->state != SUBREL_STATE_SYNCDONE)
1358  {
1359  char syncslotname[NAMEDATALEN] = {0};
1360 
1361  ReplicationSlotNameForTablesync(subid, relid, syncslotname,
1362  sizeof(syncslotname));
1363  ReplicationSlotDropAtPubNode(wrconn, syncslotname, true);
1364  }
1365  }
1366 
1367  list_free(rstates);
1368 
1369  /*
1370  * If there is a slot associated with the subscription, then drop the
1371  * replication slot at the publisher.
1372  */
1373  if (slotname)
1374  ReplicationSlotDropAtPubNode(wrconn, slotname, false);
1375 
1376  }
1377  PG_FINALLY();
1378  {
1379  walrcv_disconnect(wrconn);
1380  }
1381  PG_END_TRY();
1382 
1383  table_close(rel, NoLock);
1384 }
1385 
1386 /*
1387  * Drop the replication slot at the publisher node using the replication
1388  * connection.
1389  *
1390  * missing_ok - if true then only issue a LOG message if the slot doesn't
1391  * exist.
1392  */
1393 void
1394 ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missing_ok)
1395 {
1396  StringInfoData cmd;
1397 
1398  Assert(wrconn);
1399 
1400  load_file("libpqwalreceiver", false);
1401 
1402  initStringInfo(&cmd);
1403  appendStringInfo(&cmd, "DROP_REPLICATION_SLOT %s WAIT", quote_identifier(slotname));
1404 
1405  PG_TRY();
1406  {
1407  WalRcvExecResult *res;
1408 
1409  res = walrcv_exec(wrconn, cmd.data, 0, NULL);
1410 
1411  if (res->status == WALRCV_OK_COMMAND)
1412  {
1413  /* NOTICE. Success. */
1414  ereport(NOTICE,
1415  (errmsg("dropped replication slot \"%s\" on publisher",
1416  slotname)));
1417  }
1418  else if (res->status == WALRCV_ERROR &&
1419  missing_ok &&
1420  res->sqlstate == ERRCODE_UNDEFINED_OBJECT)
1421  {
1422  /* LOG. Error, but missing_ok = true. */
1423  ereport(LOG,
1424  (errmsg("could not drop replication slot \"%s\" on publisher: %s",
1425  slotname, res->err)));
1426  }
1427  else
1428  {
1429  /* ERROR. */
1430  ereport(ERROR,
1431  (errcode(ERRCODE_CONNECTION_FAILURE),
1432  errmsg("could not drop replication slot \"%s\" on publisher: %s",
1433  slotname, res->err)));
1434  }
1435 
1436  walrcv_clear_result(res);
1437  }
1438  PG_FINALLY();
1439  {
1440  pfree(cmd.data);
1441  }
1442  PG_END_TRY();
1443 }
1444 
1445 /*
1446  * Internal workhorse for changing a subscription owner
1447  */
1448 static void
1450 {
1451  Form_pg_subscription form;
1452 
1453  form = (Form_pg_subscription) GETSTRUCT(tup);
1454 
1455  if (form->subowner == newOwnerId)
1456  return;
1457 
1458  if (!pg_subscription_ownercheck(form->oid, GetUserId()))
1460  NameStr(form->subname));
1461 
1462  /* New owner must be a superuser */
1463  if (!superuser_arg(newOwnerId))
1464  ereport(ERROR,
1465  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1466  errmsg("permission denied to change owner of subscription \"%s\"",
1467  NameStr(form->subname)),
1468  errhint("The owner of a subscription must be a superuser.")));
1469 
1470  form->subowner = newOwnerId;
1471  CatalogTupleUpdate(rel, &tup->t_self, tup);
1472 
1473  /* Update owner dependency reference */
1474  changeDependencyOnOwner(SubscriptionRelationId,
1475  form->oid,
1476  newOwnerId);
1477 
1478  InvokeObjectPostAlterHook(SubscriptionRelationId,
1479  form->oid, 0);
1480 }
1481 
1482 /*
1483  * Change subscription owner -- by name
1484  */
1486 AlterSubscriptionOwner(const char *name, Oid newOwnerId)
1487 {
1488  Oid subid;
1489  HeapTuple tup;
1490  Relation rel;
1491  ObjectAddress address;
1492  Form_pg_subscription form;
1493 
1494  rel = table_open(SubscriptionRelationId, RowExclusiveLock);
1495 
1497  CStringGetDatum(name));
1498 
1499  if (!HeapTupleIsValid(tup))
1500  ereport(ERROR,
1501  (errcode(ERRCODE_UNDEFINED_OBJECT),
1502  errmsg("subscription \"%s\" does not exist", name)));
1503 
1504  form = (Form_pg_subscription) GETSTRUCT(tup);
1505  subid = form->oid;
1506 
1507  AlterSubscriptionOwner_internal(rel, tup, newOwnerId);
1508 
1509  ObjectAddressSet(address, SubscriptionRelationId, subid);
1510 
1511  heap_freetuple(tup);
1512 
1514 
1515  return address;
1516 }
1517 
1518 /*
1519  * Change subscription owner -- by OID
1520  */
1521 void
1523 {
1524  HeapTuple tup;
1525  Relation rel;
1526 
1527  rel = table_open(SubscriptionRelationId, RowExclusiveLock);
1528 
1530 
1531  if (!HeapTupleIsValid(tup))
1532  ereport(ERROR,
1533  (errcode(ERRCODE_UNDEFINED_OBJECT),
1534  errmsg("subscription with OID %u does not exist", subid)));
1535 
1536  AlterSubscriptionOwner_internal(rel, tup, newOwnerId);
1537 
1538  heap_freetuple(tup);
1539 
1541 }
1542 
1543 /*
1544  * Get the list of tables which belong to specified publications on the
1545  * publisher connection.
1546  */
1547 static List *
1549 {
1550  WalRcvExecResult *res;
1551  StringInfoData cmd;
1552  TupleTableSlot *slot;
1553  Oid tableRow[2] = {TEXTOID, TEXTOID};
1554  ListCell *lc;
1555  bool first;
1556  List *tablelist = NIL;
1557 
1558  Assert(list_length(publications) > 0);
1559 
1560  initStringInfo(&cmd);
1561  appendStringInfoString(&cmd, "SELECT DISTINCT t.schemaname, t.tablename\n"
1562  " FROM pg_catalog.pg_publication_tables t\n"
1563  " WHERE t.pubname IN (");
1564  first = true;
1565  foreach(lc, publications)
1566  {
1567  char *pubname = strVal(lfirst(lc));
1568 
1569  if (first)
1570  first = false;
1571  else
1572  appendStringInfoString(&cmd, ", ");
1573 
1575  }
1576  appendStringInfoChar(&cmd, ')');
1577 
1578  res = walrcv_exec(wrconn, cmd.data, 2, tableRow);
1579  pfree(cmd.data);
1580 
1581  if (res->status != WALRCV_OK_TUPLES)
1582  ereport(ERROR,
1583  (errcode(ERRCODE_CONNECTION_FAILURE),
1584  errmsg("could not receive list of replicated tables from the publisher: %s",
1585  res->err)));
1586 
1587  /* Process tables. */
1589  while (tuplestore_gettupleslot(res->tuplestore, true, false, slot))
1590  {
1591  char *nspname;
1592  char *relname;
1593  bool isnull;
1594  RangeVar *rv;
1595 
1596  nspname = TextDatumGetCString(slot_getattr(slot, 1, &isnull));
1597  Assert(!isnull);
1598  relname = TextDatumGetCString(slot_getattr(slot, 2, &isnull));
1599  Assert(!isnull);
1600 
1601  rv = makeRangeVar(nspname, relname, -1);
1602  tablelist = lappend(tablelist, rv);
1603 
1604  ExecClearTuple(slot);
1605  }
1607 
1608  walrcv_clear_result(res);
1609 
1610  return tablelist;
1611 }
1612 
1613 /*
1614  * This is to report the connection failure while dropping replication slots.
1615  * Here, we report the WARNING for all tablesync slots so that user can drop
1616  * them manually, if required.
1617  */
1618 static void
1619 ReportSlotConnectionError(List *rstates, Oid subid, char *slotname, char *err)
1620 {
1621  ListCell *lc;
1622 
1623  foreach(lc, rstates)
1624  {
1626  Oid relid = rstate->relid;
1627 
1628  /* Only cleanup resources of tablesync workers */
1629  if (!OidIsValid(relid))
1630  continue;
1631 
1632  /*
1633  * Caller needs to ensure that relstate doesn't change underneath us.
1634  * See DropSubscription where we get the relstates.
1635  */
1636  if (rstate->state != SUBREL_STATE_SYNCDONE)
1637  {
1638  char syncslotname[NAMEDATALEN] = {0};
1639 
1640  ReplicationSlotNameForTablesync(subid, relid, syncslotname,
1641  sizeof(syncslotname));
1642  elog(WARNING, "could not drop tablesync replication slot \"%s\"",
1643  syncslotname);
1644  }
1645  }
1646 
1647  ereport(ERROR,
1648  (errcode(ERRCODE_CONNECTION_FAILURE),
1649  errmsg("could not connect to publisher when attempting to "
1650  "drop replication slot \"%s\": %s", slotname, err),
1651  /* translator: %s is an SQL ALTER command */
1652  errhint("Use %s to disassociate the subscription from the slot.",
1653  "ALTER SUBSCRIPTION ... SET (slot_name = NONE)")));
1654 }
1655 
1656 /*
1657  * Check for duplicates in the given list of publications and error out if
1658  * found one. Add publications to datums as text datums, if datums is not
1659  * NULL.
1660  */
1661 static void
1663 {
1664  ListCell *cell;
1665  int j = 0;
1666 
1667  foreach(cell, publist)
1668  {
1669  char *name = strVal(lfirst(cell));
1670  ListCell *pcell;
1671 
1672  foreach(pcell, publist)
1673  {
1674  char *pname = strVal(lfirst(pcell));
1675 
1676  if (pcell == cell)
1677  break;
1678 
1679  if (strcmp(name, pname) == 0)
1680  ereport(ERROR,
1682  errmsg("publication name \"%s\" used more than once",
1683  pname)));
1684  }
1685 
1686  if (datums)
1687  datums[j++] = CStringGetTextDatum(name);
1688  }
1689 }
1690 
1691 /*
1692  * Merge current subscription's publications and user-specified publications
1693  * from ADD/DROP PUBLICATIONS.
1694  *
1695  * If addpub is true, we will add the list of publications into oldpublist.
1696  * Otherwise, we will delete the list of publications from oldpublist. The
1697  * returned list is a copy, oldpublist itself is not changed.
1698  *
1699  * subname is the subscription name, for error messages.
1700  */
1701 static List *
1702 merge_publications(List *oldpublist, List *newpublist, bool addpub, const char *subname)
1703 {
1704  ListCell *lc;
1705 
1706  oldpublist = list_copy(oldpublist);
1707 
1708  check_duplicates_in_publist(newpublist, NULL);
1709 
1710  foreach(lc, newpublist)
1711  {
1712  char *name = strVal(lfirst(lc));
1713  ListCell *lc2;
1714  bool found = false;
1715 
1716  foreach(lc2, oldpublist)
1717  {
1718  char *pubname = strVal(lfirst(lc2));
1719 
1720  if (strcmp(name, pubname) == 0)
1721  {
1722  found = true;
1723  if (addpub)
1724  ereport(ERROR,
1726  errmsg("publication \"%s\" is already in subscription \"%s\"",
1727  name, subname)));
1728  else
1729  oldpublist = foreach_delete_current(oldpublist, lc2);
1730 
1731  break;
1732  }
1733  }
1734 
1735  if (addpub && !found)
1736  oldpublist = lappend(oldpublist, makeString(name));
1737  else if (!addpub && !found)
1738  ereport(ERROR,
1739  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1740  errmsg("publication \"%s\" is not in subscription \"%s\"",
1741  name, subname)));
1742  }
1743 
1744  /*
1745  * XXX Probably no strong reason for this, but for now it's to make ALTER
1746  * SUBSCRIPTION ... DROP PUBLICATION consistent with SET PUBLICATION.
1747  */
1748  if (!oldpublist)
1749  ereport(ERROR,
1750  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1751  errmsg("cannot drop all the publications from a subscription")));
1752 
1753  return oldpublist;
1754 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
static List * fetch_table_list(WalReceiverConn *wrconn, List *publications)
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:381
void DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
List * logicalrep_workers_find(Oid subid, bool only_running)
Definition: launcher.c:243
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define DEBUG1
Definition: elog.h:25
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:102
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:151
void RemoveSubscriptionRel(Oid subid, Oid relid)
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11332
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
bool copy_data
void ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missing_ok)
#define RelationGetDescr(relation)
Definition: rel.h:503
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1238
Oid GetUserId(void)
Definition: miscinit.c:478
#define LOGICALREP_TWOPHASE_STATE_DISABLED
#define PointerGetDatum(X)
Definition: postgres.h:600
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:79
#define walrcv_check_conninfo(conninfo)
Definition: walreceiver.h:406
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1974
char * pstrdup(const char *in)
Definition: mcxt.c:1299
bool create_slot
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:1923
bits32 specified_opts
#define InvokeObjectDropHook(classId, objectId, subId)
Definition: objectaccess.h:160
void replorigin_drop_by_name(const char *name, bool missing_ok, bool nowait)
Definition: origin.c:414
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3318
#define AccessShareLock
Definition: lockdefs.h:36
static void ReportSlotConnectionError(List *rstates, Oid subid, char *slotname, char *err)
List * list_copy(const List *oldlist)
Definition: list.c:1418
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:698
bool superuser(void)
Definition: superuser.c:46
#define SUBOPT_ENABLED
RepOriginId replorigin_create(const char *roname)
Definition: origin.c:240
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
static Datum publicationListToArray(List *publist)
#define SUBOPT_CREATE_SLOT
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
FormData_pg_subscription * Form_pg_subscription
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:163
#define SUBOPT_SYNCHRONOUS_COMMIT
#define LOG
Definition: elog.h:26
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
NameData relname
Definition: pg_class.h:38
unsigned int Oid
Definition: postgres_ext.h:31
NameData subname
static void parse_subscription_options(ParseState *pstate, List *stmt_options, bits32 supported_opts, SubOpts *opts)
#define SUBOPT_STREAMING
#define OidIsValid(objectId)
Definition: c.h:710
static WalReceiverConn * wrconn
Definition: walreceiver.c:94
char * schemaname
Definition: primnodes.h:67
#define LOGICALREP_TWOPHASE_STATE_ENABLED
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:369
#define NAMEDATALEN
#define DatumGetName(X)
Definition: postgres.h:629
char * relname
Definition: primnodes.h:68
Subscription * GetSubscription(Oid subid, bool missing_ok)
bool ReplicationSlotValidateName(const char *name, int elevel)
Definition: slot.c:172
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
Definition: pg_shdepend.c:311
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3308
bool defGetBoolean(DefElem *def)
Definition: define.c:111
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:436
#define SUBOPT_TWOPHASE_COMMIT
void pfree(void *pointer)
Definition: mcxt.c:1169
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
TupleDesc tupledesc
Definition: walreceiver.h:220
bool streaming
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define SUBOPT_CONNECT
char * defGetString(DefElem *def)
Definition: define.c:49
#define SUBOPT_BINARY
ItemPointerData t_self
Definition: htup.h:65
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
void AddSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn)
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define NoLock
Definition: lockdefs.h:34
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
AlterSubscriptionType kind
Definition: parsenodes.h:3674
#define SUBOPT_REFRESH
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:436
#define RowExclusiveLock
Definition: lockdefs.h:38
List * GetSubscriptionRelations(Oid subid)
#define LOGICALREP_TWOPHASE_STATE_PENDING
#define CStringGetDatum(X)
Definition: postgres.h:622
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3379
char * slot_name
void CheckSubscriptionRelkind(char relkind, const char *nspname, const char *relname)
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
ObjectAddress AlterSubscriptionOwner(const char *name, Oid newOwnerId)
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:175
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
void deleteSharedDependencyRecordsFor(Oid classId, Oid objectId, int32 objectSubId)
Definition: pg_shdepend.c:997
static AmcheckOptions opts
Definition: pg_amcheck.c:111
List * lappend(List *list, void *datum)
Definition: list.c:336
static void check_duplicates_in_publist(List *publist, Datum *datums)
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define WARNING
Definition: elog.h:40
ObjectAddress AlterSubscription(ParseState *pstate, AlterSubscriptionStmt *stmt, bool isTopLevel)
#define PG_FINALLY()
Definition: elog.h:330
#define TextDatumGetCString(d)
Definition: builtins.h:83
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
void UpdateTwoPhaseState(Oid suboid, char new_state)
Definition: tablesync.c:1255
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
Oid MyDatabaseId
Definition: globals.c:88
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1017
#define BoolGetDatum(X)
Definition: postgres.h:446
#define InvalidOid
Definition: postgres_ext.h:36
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:381
#define ereport(elevel,...)
Definition: elog.h:157
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
Definition: define.c:355
struct SubOpts SubOpts
static void AlterSubscriptionOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define NOTICE
Definition: elog.h:37
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1078
uint32 bits32
Definition: c.h:450
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Tuplestorestate * tuplestore
Definition: walreceiver.h:219
#define SUBOPT_COPY_DATA
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
WalRcvExecStatus status
Definition: walreceiver.h:216
Definition: regguts.h:317
ObjectAddress CreateSubscription(ParseState *pstate, CreateSubscriptionStmt *stmt, bool isTopLevel)
#define GetSysCacheOid2(cacheId, oidcol, key1, key2)
Definition: syscache.h:195
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:146
int oid_cmp(const void *p1, const void *p2)
Definition: oid.c:336
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
static int list_length(const List *l)
Definition: pg_list.h:149
void EventTriggerSQLDropAddObject(const ObjectAddress *object, bool original, bool normal)
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1138
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
void ReplicationOriginNameForTablesync(Oid suboid, Oid relid, char *originname, int szorgname)
Definition: tablesync.c:905
#define walrcv_disconnect(conn)
Definition: walreceiver.h:432
#define SUBOPT_SLOT_NAME
const char * name
Definition: encode.c:515
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define CharGetDatum(X)
Definition: postgres.h:460
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:175
#define AccessExclusiveLock
Definition: lockdefs.h:45
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
void list_free(List *list)
Definition: list.c:1391
#define elog(elevel,...)
Definition: elog.h:232
void AlterSubscriptionOwner_oid(Oid subid, Oid newOwnerId)
#define NameStr(name)
Definition: c.h:681
#define CStringGetTextDatum(s)
Definition: builtins.h:82
char * defname
Definition: parsenodes.h:746
#define qsort(a, b, c, d)
Definition: port.h:504
void ApplyLauncherWakeupAtCommit(void)
Definition: launcher.c:780
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:177
#define PG_TRY()
Definition: elog.h:313
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
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:7295
Definition: pg_list.h:50
List * GetSubscriptionNotReadyRelations(Oid subid)
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1899
#define snprintf
Definition: port.h:216
#define IsSet(val, bits)
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:422
#define PG_END_TRY()
Definition: elog.h:338
char * synchronous_commit
static void AlterSubscription_refresh(Subscription *sub, bool copy_data)
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:430
static List * merge_publications(List *oldpublist, List *newpublist, bool addpub, const char *subname)
void ReplicationSlotNameForTablesync(Oid suboid, Oid relid, char *syncslotname, int szslot)
Definition: tablesync.c:892
bool pg_subscription_ownercheck(Oid sub_oid, Oid roleid)
Definition: aclchk.c:5382
#define walrcv_create_slot(conn, slotname, temporary, two_phase, snapshot_action, lsn)
Definition: walreceiver.h:426
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:404