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