PostgreSQL Source Code  git master
subscriptioncmds.h File Reference
Include dependency graph for subscriptioncmds.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ObjectAddress CreateSubscription (CreateSubscriptionStmt *stmt, bool isTopLevel)
 
ObjectAddress AlterSubscription (AlterSubscriptionStmt *stmt)
 
void DropSubscription (DropSubscriptionStmt *stmt, bool isTopLevel)
 
ObjectAddress AlterSubscriptionOwner (const char *name, Oid newOwnerId)
 
void AlterSubscriptionOwner_oid (Oid subid, Oid newOwnerId)
 

Function Documentation

◆ AlterSubscription()

ObjectAddress AlterSubscription ( AlterSubscriptionStmt stmt)

Definition at line 635 of file subscriptioncmds.c.

References AccessExclusiveLock, aclcheck_error(), ACLCHECK_NOT_OWNER, ALTER_SUBSCRIPTION_CONNECTION, ALTER_SUBSCRIPTION_ENABLED, ALTER_SUBSCRIPTION_OPTIONS, ALTER_SUBSCRIPTION_PUBLICATION, ALTER_SUBSCRIPTION_REFRESH, AlterSubscription_refresh(), ApplyLauncherWakeupAtCommit(), Assert, BoolGetDatum, CatalogTupleUpdate(), AlterSubscriptionStmt::conninfo, CStringGetDatum, CStringGetTextDatum, DirectFunctionCall1, elog, Subscription::enabled, ereport, errcode(), errhint(), errmsg(), ERROR, GETSTRUCT, GetSubscription(), GetUserId(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, InvokeObjectPostAlterHook, AlterSubscriptionStmt::kind, load_file(), LockSharedObject(), MyDatabaseId, namein(), OBJECT_SUBSCRIPTION, ObjectAddressSet, AlterSubscriptionStmt::options, parse_subscription_options(), pg_subscription_ownercheck(), AlterSubscriptionStmt::publication, publicationListToArray(), Subscription::publications, RelationGetDescr, RowExclusiveLock, SearchSysCacheCopy2, Subscription::slotname, AlterSubscriptionStmt::subname, SUBSCRIPTIONNAME, synchronous_commit, HeapTupleData::t_self, table_close(), table_open(), values, and walrcv_check_conninfo.

Referenced by ProcessUtilitySlow().

636 {
637  Relation rel;
638  ObjectAddress myself;
639  bool nulls[Natts_pg_subscription];
640  bool replaces[Natts_pg_subscription];
641  Datum values[Natts_pg_subscription];
642  HeapTuple tup;
643  Oid subid;
644  bool update_tuple = false;
645  Subscription *sub;
647 
648  rel = table_open(SubscriptionRelationId, RowExclusiveLock);
649 
650  /* Fetch the existing tuple. */
652  CStringGetDatum(stmt->subname));
653 
654  if (!HeapTupleIsValid(tup))
655  ereport(ERROR,
656  (errcode(ERRCODE_UNDEFINED_OBJECT),
657  errmsg("subscription \"%s\" does not exist",
658  stmt->subname)));
659 
660  form = (Form_pg_subscription) GETSTRUCT(tup);
661  subid = form->oid;
662 
663  /* must be owner */
664  if (!pg_subscription_ownercheck(subid, GetUserId()))
666  stmt->subname);
667 
668  sub = GetSubscription(subid, false);
669 
670  /* Lock the subscription so nobody else can do anything with it. */
671  LockSharedObject(SubscriptionRelationId, subid, 0, AccessExclusiveLock);
672 
673  /* Form a new tuple. */
674  memset(values, 0, sizeof(values));
675  memset(nulls, false, sizeof(nulls));
676  memset(replaces, false, sizeof(replaces));
677 
678  switch (stmt->kind)
679  {
681  {
682  char *slotname;
683  bool slotname_given;
684  char *synchronous_commit;
685 
686  parse_subscription_options(stmt->options, NULL, NULL, NULL,
687  NULL, &slotname_given, &slotname,
688  NULL, &synchronous_commit, NULL);
689 
690  if (slotname_given)
691  {
692  if (sub->enabled && !slotname)
693  ereport(ERROR,
694  (errcode(ERRCODE_SYNTAX_ERROR),
695  errmsg("cannot set %s for enabled subscription",
696  "slot_name = NONE")));
697 
698  if (slotname)
699  values[Anum_pg_subscription_subslotname - 1] =
701  else
702  nulls[Anum_pg_subscription_subslotname - 1] = true;
703  replaces[Anum_pg_subscription_subslotname - 1] = true;
704  }
705 
706  if (synchronous_commit)
707  {
708  values[Anum_pg_subscription_subsynccommit - 1] =
709  CStringGetTextDatum(synchronous_commit);
710  replaces[Anum_pg_subscription_subsynccommit - 1] = true;
711  }
712 
713  update_tuple = true;
714  break;
715  }
716 
718  {
719  bool enabled,
720  enabled_given;
721 
723  &enabled_given, &enabled, NULL,
724  NULL, NULL, NULL, NULL, NULL);
725  Assert(enabled_given);
726 
727  if (!sub->slotname && enabled)
728  ereport(ERROR,
729  (errcode(ERRCODE_SYNTAX_ERROR),
730  errmsg("cannot enable subscription that does not have a slot name")));
731 
732  values[Anum_pg_subscription_subenabled - 1] =
733  BoolGetDatum(enabled);
734  replaces[Anum_pg_subscription_subenabled - 1] = true;
735 
736  if (enabled)
738 
739  update_tuple = true;
740  break;
741  }
742 
744  /* Load the library providing us libpq calls. */
745  load_file("libpqwalreceiver", false);
746  /* Check the connection info string. */
748 
749  values[Anum_pg_subscription_subconninfo - 1] =
751  replaces[Anum_pg_subscription_subconninfo - 1] = true;
752  update_tuple = true;
753  break;
754 
756  {
757  bool copy_data;
758  bool refresh;
759 
760  parse_subscription_options(stmt->options, NULL, NULL, NULL,
761  NULL, NULL, NULL, &copy_data,
762  NULL, &refresh);
763 
764  values[Anum_pg_subscription_subpublications - 1] =
766  replaces[Anum_pg_subscription_subpublications - 1] = true;
767 
768  update_tuple = true;
769 
770  /* Refresh if user asked us to. */
771  if (refresh)
772  {
773  if (!sub->enabled)
774  ereport(ERROR,
775  (errcode(ERRCODE_SYNTAX_ERROR),
776  errmsg("ALTER SUBSCRIPTION with refresh is not allowed for disabled subscriptions"),
777  errhint("Use ALTER SUBSCRIPTION ... SET PUBLICATION ... WITH (refresh = false).")));
778 
779  /* Make sure refresh sees the new list of publications. */
780  sub->publications = stmt->publication;
781 
782  AlterSubscription_refresh(sub, copy_data);
783  }
784 
785  break;
786  }
787 
789  {
790  bool copy_data;
791 
792  if (!sub->enabled)
793  ereport(ERROR,
794  (errcode(ERRCODE_SYNTAX_ERROR),
795  errmsg("ALTER SUBSCRIPTION ... REFRESH is not allowed for disabled subscriptions")));
796 
797  parse_subscription_options(stmt->options, NULL, NULL, NULL,
798  NULL, NULL, NULL, &copy_data,
799  NULL, NULL);
800 
801  AlterSubscription_refresh(sub, copy_data);
802 
803  break;
804  }
805 
806  default:
807  elog(ERROR, "unrecognized ALTER SUBSCRIPTION kind %d",
808  stmt->kind);
809  }
810 
811  /* Update the catalog if needed. */
812  if (update_tuple)
813  {
814  tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
815  replaces);
816 
817  CatalogTupleUpdate(rel, &tup->t_self, tup);
818 
819  heap_freetuple(tup);
820  }
821 
823 
824  ObjectAddressSet(myself, SubscriptionRelationId, subid);
825 
826  InvokeObjectPostAlterHook(SubscriptionRelationId, subid, 0);
827 
828  return myself;
829 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
int errhint(const char *fmt,...)
Definition: elog.c:974
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define RelationGetDescr(relation)
Definition: rel.h:442
Oid GetUserId(void)
Definition: miscinit.c:380
#define walrcv_check_conninfo(conninfo)
Definition: walreceiver.h:257
int errcode(int sqlerrcode)
Definition: elog.c:570
static Datum publicationListToArray(List *publist)
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:617
FormData_pg_subscription * Form_pg_subscription
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
Subscription * GetSubscription(Oid subid, bool missing_ok)
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3353
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
AlterSubscriptionType kind
Definition: parsenodes.h:3521
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:578
List * publications
int synchronous_commit
Definition: xact.c:83
#define ereport(elevel, rest)
Definition: elog.h:141
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:163
uintptr_t Datum
Definition: postgres.h:367
Oid MyDatabaseId
Definition: globals.c:85
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1004
#define BoolGetDatum(X)
Definition: postgres.h:402
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:146
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:224
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define AccessExclusiveLock
Definition: lockdefs.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define CStringGetTextDatum(s)
Definition: builtins.h:83
void ApplyLauncherWakeupAtCommit(void)
Definition: launcher.c:955
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:176
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
static void parse_subscription_options(List *options, bool *connect, bool *enabled_given, bool *enabled, bool *create_slot, bool *slot_name_given, char **slot_name, bool *copy_data, char **synchronous_commit, bool *refresh)
static void AlterSubscription_refresh(Subscription *sub, bool copy_data)
bool pg_subscription_ownercheck(Oid sub_oid, Oid roleid)
Definition: aclchk.c:5319

◆ AlterSubscriptionOwner()

ObjectAddress AlterSubscriptionOwner ( const char *  name,
Oid  newOwnerId 
)

Definition at line 1082 of file subscriptioncmds.c.

References AlterSubscriptionOwner_internal(), CStringGetDatum, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, heap_freetuple(), HeapTupleIsValid, MyDatabaseId, ObjectAddressSet, RowExclusiveLock, SearchSysCacheCopy2, SUBSCRIPTIONNAME, table_close(), and table_open().

Referenced by ExecAlterOwnerStmt().

1083 {
1084  Oid subid;
1085  HeapTuple tup;
1086  Relation rel;
1087  ObjectAddress address;
1088  Form_pg_subscription form;
1089 
1090  rel = table_open(SubscriptionRelationId, RowExclusiveLock);
1091 
1094 
1095  if (!HeapTupleIsValid(tup))
1096  ereport(ERROR,
1097  (errcode(ERRCODE_UNDEFINED_OBJECT),
1098  errmsg("subscription \"%s\" does not exist", name)));
1099 
1100  form = (Form_pg_subscription) GETSTRUCT(tup);
1101  subid = form->oid;
1102 
1103  AlterSubscriptionOwner_internal(rel, tup, newOwnerId);
1104 
1105  ObjectAddressSet(address, SubscriptionRelationId, subid);
1106 
1107  heap_freetuple(tup);
1108 
1110 
1111  return address;
1112 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int errcode(int sqlerrcode)
Definition: elog.c:570
FormData_pg_subscription * Form_pg_subscription
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
Oid MyDatabaseId
Definition: globals.c:85
static void AlterSubscriptionOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
const char * name
Definition: encode.c:521
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:176
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ AlterSubscriptionOwner_oid()

void AlterSubscriptionOwner_oid ( Oid  subid,
Oid  newOwnerId 
)

Definition at line 1118 of file subscriptioncmds.c.

References AlterSubscriptionOwner_internal(), ereport, errcode(), errmsg(), ERROR, heap_freetuple(), HeapTupleIsValid, ObjectIdGetDatum, RowExclusiveLock, SearchSysCacheCopy1, SUBSCRIPTIONOID, table_close(), and table_open().

Referenced by shdepReassignOwned().

1119 {
1120  HeapTuple tup;
1121  Relation rel;
1122 
1123  rel = table_open(SubscriptionRelationId, RowExclusiveLock);
1124 
1126 
1127  if (!HeapTupleIsValid(tup))
1128  ereport(ERROR,
1129  (errcode(ERRCODE_UNDEFINED_OBJECT),
1130  errmsg("subscription with OID %u does not exist", subid)));
1131 
1132  AlterSubscriptionOwner_internal(rel, tup, newOwnerId);
1133 
1134  heap_freetuple(tup);
1135 
1137 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
int errcode(int sqlerrcode)
Definition: elog.c:570
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:141
static void AlterSubscriptionOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
int errmsg(const char *fmt,...)
Definition: elog.c:784
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ CreateSubscription()

ObjectAddress CreateSubscription ( CreateSubscriptionStmt stmt,
bool  isTopLevel 
)

Definition at line 315 of file subscriptioncmds.c.

References AccessShareLock, AddSubscriptionRelState(), ApplyLauncherWakeupAtCommit(), Assert, BoolGetDatum, CatalogTupleInsert(), CheckSubscriptionRelkind(), connect, CreateSubscriptionStmt::conninfo, create_slot, CRS_NOEXPORT_SNAPSHOT, CStringGetDatum, CStringGetTextDatum, DirectFunctionCall1, elog, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, fetch_table_list(), get_rel_relkind(), GetNewOidWithIndex(), GetSysCacheOid2, GetUserId(), heap_form_tuple(), heap_freetuple(), InvalidXLogRecPtr, InvokeObjectPostCreateHook, lfirst, load_file(), MyDatabaseId, NAMEDATALEN, namein(), NOTICE, ObjectAddressSet, ObjectIdGetDatum, OidIsValid, CreateSubscriptionStmt::options, parse_subscription_options(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PreventInTransactionBlock(), CreateSubscriptionStmt::publication, publicationListToArray(), RangeVarGetRelid, recordDependencyOnOwner(), RelationGetDescr, RangeVar::relname, replorigin_create(), RowExclusiveLock, RangeVar::schemaname, snprintf, CreateSubscriptionStmt::subname, SUBSCRIPTIONNAME, SubscriptionObjectIndexId, superuser(), synchronous_commit, table_close(), table_open(), values, walrcv_check_conninfo, walrcv_connect, walrcv_create_slot, walrcv_disconnect, WARNING, and wrconn.

Referenced by ProcessUtilitySlow().

316 {
317  Relation rel;
318  ObjectAddress myself;
319  Oid subid;
320  bool nulls[Natts_pg_subscription];
321  Datum values[Natts_pg_subscription];
322  Oid owner = GetUserId();
323  HeapTuple tup;
324  bool connect;
325  bool enabled_given;
326  bool enabled;
327  bool copy_data;
328  char *synchronous_commit;
329  char *conninfo;
330  char *slotname;
331  bool slotname_given;
332  char originname[NAMEDATALEN];
333  bool create_slot;
334  List *publications;
335 
336  /*
337  * Parse and check options.
338  *
339  * Connection and publication should not be specified here.
340  */
341  parse_subscription_options(stmt->options, &connect, &enabled_given,
342  &enabled, &create_slot, &slotname_given,
343  &slotname, &copy_data, &synchronous_commit,
344  NULL);
345 
346  /*
347  * Since creating a replication slot is not transactional, rolling back
348  * the transaction leaves the created replication slot. So we cannot run
349  * CREATE SUBSCRIPTION inside a transaction block if creating a
350  * replication slot.
351  */
352  if (create_slot)
353  PreventInTransactionBlock(isTopLevel, "CREATE SUBSCRIPTION ... WITH (create_slot = true)");
354 
355  if (!superuser())
356  ereport(ERROR,
357  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
358  (errmsg("must be superuser to create subscriptions"))));
359 
360  /*
361  * If built with appropriate switch, whine when regression-testing
362  * conventions for subscription names are violated.
363  */
364 #ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
365  if (strncmp(stmt->subname, "regress_", 8) != 0)
366  elog(WARNING, "subscriptions created by regression test cases should have names starting with \"regress_\"");
367 #endif
368 
369  rel = table_open(SubscriptionRelationId, RowExclusiveLock);
370 
371  /* Check if name is used */
372  subid = GetSysCacheOid2(SUBSCRIPTIONNAME, Anum_pg_subscription_oid,
374  if (OidIsValid(subid))
375  {
376  ereport(ERROR,
378  errmsg("subscription \"%s\" already exists",
379  stmt->subname)));
380  }
381 
382  if (!slotname_given && slotname == NULL)
383  slotname = stmt->subname;
384 
385  /* The default for synchronous_commit of subscriptions is off. */
386  if (synchronous_commit == NULL)
387  synchronous_commit = "off";
388 
389  conninfo = stmt->conninfo;
390  publications = stmt->publication;
391 
392  /* Load the library providing us libpq calls. */
393  load_file("libpqwalreceiver", false);
394 
395  /* Check the connection info string. */
396  walrcv_check_conninfo(conninfo);
397 
398  /* Everything ok, form a new tuple. */
399  memset(values, 0, sizeof(values));
400  memset(nulls, false, sizeof(nulls));
401 
403  Anum_pg_subscription_oid);
404  values[Anum_pg_subscription_oid - 1] = ObjectIdGetDatum(subid);
405  values[Anum_pg_subscription_subdbid - 1] = ObjectIdGetDatum(MyDatabaseId);
406  values[Anum_pg_subscription_subname - 1] =
408  values[Anum_pg_subscription_subowner - 1] = ObjectIdGetDatum(owner);
409  values[Anum_pg_subscription_subenabled - 1] = BoolGetDatum(enabled);
410  values[Anum_pg_subscription_subconninfo - 1] =
411  CStringGetTextDatum(conninfo);
412  if (slotname)
413  values[Anum_pg_subscription_subslotname - 1] =
415  else
416  nulls[Anum_pg_subscription_subslotname - 1] = true;
417  values[Anum_pg_subscription_subsynccommit - 1] =
418  CStringGetTextDatum(synchronous_commit);
419  values[Anum_pg_subscription_subpublications - 1] =
420  publicationListToArray(publications);
421 
422  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
423 
424  /* Insert tuple into catalog. */
425  CatalogTupleInsert(rel, tup);
426  heap_freetuple(tup);
427 
428  recordDependencyOnOwner(SubscriptionRelationId, subid, owner);
429 
430  snprintf(originname, sizeof(originname), "pg_%u", subid);
431  replorigin_create(originname);
432 
433  /*
434  * Connect to remote side to execute requested commands and fetch table
435  * info.
436  */
437  if (connect)
438  {
439  XLogRecPtr lsn;
440  char *err;
442  List *tables;
443  ListCell *lc;
444  char table_state;
445 
446  /* Try to connect to the publisher. */
447  wrconn = walrcv_connect(conninfo, true, stmt->subname, &err);
448  if (!wrconn)
449  ereport(ERROR,
450  (errmsg("could not connect to the publisher: %s", err)));
451 
452  PG_TRY();
453  {
454  /*
455  * Set sync state based on if we were asked to do data copy or
456  * not.
457  */
458  table_state = copy_data ? SUBREL_STATE_INIT : SUBREL_STATE_READY;
459 
460  /*
461  * Get the table list from publisher and build local table status
462  * info.
463  */
464  tables = fetch_table_list(wrconn, publications);
465  foreach(lc, tables)
466  {
467  RangeVar *rv = (RangeVar *) lfirst(lc);
468  Oid relid;
469 
470  relid = RangeVarGetRelid(rv, AccessShareLock, false);
471 
472  /* Check for supported relkind. */
474  rv->schemaname, rv->relname);
475 
476  AddSubscriptionRelState(subid, relid, table_state,
478  }
479 
480  /*
481  * If requested, create permanent slot for the subscription. We
482  * won't use the initial snapshot for anything, so no need to
483  * export it.
484  */
485  if (create_slot)
486  {
487  Assert(slotname);
488 
489  walrcv_create_slot(wrconn, slotname, false,
490  CRS_NOEXPORT_SNAPSHOT, &lsn);
491  ereport(NOTICE,
492  (errmsg("created replication slot \"%s\" on publisher",
493  slotname)));
494  }
495  }
496  PG_CATCH();
497  {
498  /* Close the connection in case of failure. */
499  walrcv_disconnect(wrconn);
500  PG_RE_THROW();
501  }
502  PG_END_TRY();
503 
504  /* And we are done with the remote side. */
505  walrcv_disconnect(wrconn);
506  }
507  else
509  /* translator: %s is an SQL ALTER statement */
510  (errmsg("tables were not subscribed, you will have to run %s to subscribe the tables",
511  "ALTER SUBSCRIPTION ... REFRESH PUBLICATION")));
512 
514 
515  if (enabled)
517 
518  ObjectAddressSet(myself, SubscriptionRelationId, subid);
519 
520  InvokeObjectPostCreateHook(SubscriptionRelationId, subid, 0);
521 
522  return myself;
523 }
static List * fetch_table_list(WalReceiverConn *wrconn, List *publications)
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:323
WalReceiverConn * wrconn
Definition: worker.c:100
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define RelationGetDescr(relation)
Definition: rel.h:442
Oid GetUserId(void)
Definition: miscinit.c:380
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:63
#define walrcv_check_conninfo(conninfo)
Definition: walreceiver.h:257
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1805
#define AccessShareLock
Definition: lockdefs.h:36
static bool create_slot
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
static Datum publicationListToArray(List *publist)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define connect(s, name, namelen)
Definition: win32_port.h:444
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:617
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:165
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define walrcv_create_slot(conn, slotname, temporary, snapshot_action, lsn)
Definition: walreceiver.h:277
#define OidIsValid(objectId)
Definition: c.h:638
char * schemaname
Definition: primnodes.h:67
#define NAMEDATALEN
char * relname
Definition: primnodes.h:68
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
void AddSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn)
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:578
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3328
void CheckSubscriptionRelkind(char relkind, const char *nspname, const char *relname)
RepOriginId replorigin_create(char *roname)
Definition: origin.c:243
int synchronous_commit
Definition: xact.c:83
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
uintptr_t Datum
Definition: postgres.h:367
Oid MyDatabaseId
Definition: globals.c:85
#define BoolGetDatum(X)
Definition: postgres.h:402
#define NOTICE
Definition: elog.h:37
#define PG_CATCH()
Definition: elog.h:310
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
#define GetSysCacheOid2(cacheId, oidcol, key1, key2)
Definition: syscache.h:194
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:146
#define PG_RE_THROW()
Definition: elog.h:331
#define walrcv_disconnect(conn)
Definition: walreceiver.h:281
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define CStringGetTextDatum(s)
Definition: builtins.h:83
void ApplyLauncherWakeupAtCommit(void)
Definition: launcher.c:955
#define PG_TRY()
Definition: elog.h:301
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:33
Definition: pg_list.h:50
#define snprintf
Definition: port.h:192
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:183
static void parse_subscription_options(List *options, bool *connect, bool *enabled_given, bool *enabled, bool *create_slot, bool *slot_name_given, char **slot_name, bool *copy_data, char **synchronous_commit, bool *refresh)
#define PG_END_TRY()
Definition: elog.h:317
#define SubscriptionObjectIndexId
Definition: indexing.h:361
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:255

◆ DropSubscription()

void DropSubscription ( DropSubscriptionStmt stmt,
bool  isTopLevel 
)

Definition at line 835 of file subscriptioncmds.c.

References AccessExclusiveLock, aclcheck_error(), ACLCHECK_NOT_OWNER, appendStringInfo(), Assert, CatalogTupleDelete(), CStringGetDatum, StringInfoData::data, DatumGetName, deleteSharedDependencyRecordsFor(), ereport, WalRcvExecResult::err, errcode(), errdetail(), errhint(), errmsg(), ERROR, EventTriggerSQLDropAddObject(), GETSTRUCT, GetUserId(), HeapTupleIsValid, initStringInfo(), InvalidOid, InvalidRepOriginId, InvokeObjectDropHook, lfirst, list_free(), load_file(), LockSharedObject(), logicalrep_worker_stop(), logicalrep_workers_find(), LW_SHARED, LWLockAcquire(), LWLockRelease(), DropSubscriptionStmt::missing_ok, MyDatabaseId, NAMEDATALEN, NameStr, NoLock, NOTICE, OBJECT_SUBSCRIPTION, ObjectAddressSet, pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, pg_subscription_ownercheck(), PG_TRY, PreventInTransactionBlock(), pstrdup(), quote_identifier(), ReleaseSysCache(), LogicalRepWorker::relid, RemoveSubscriptionRel(), replorigin_by_name(), replorigin_drop(), SearchSysCache2(), snprintf, WalRcvExecResult::status, LogicalRepWorker::subid, subname, DropSubscriptionStmt::subname, SUBSCRIPTIONNAME, SUBSCRIPTIONOID, SysCacheGetAttr(), HeapTupleData::t_self, table_close(), table_open(), TextDatumGetCString, walrcv_clear_result(), walrcv_connect, walrcv_disconnect, walrcv_exec, WALRCV_OK_COMMAND, and wrconn.

Referenced by ProcessUtilitySlow().

836 {
837  Relation rel;
838  ObjectAddress myself;
839  HeapTuple tup;
840  Oid subid;
841  Datum datum;
842  bool isnull;
843  char *subname;
844  char *conninfo;
845  char *slotname;
846  List *subworkers;
847  ListCell *lc;
848  char originname[NAMEDATALEN];
849  char *err = NULL;
850  RepOriginId originid;
851  WalReceiverConn *wrconn = NULL;
852  StringInfoData cmd;
854 
855  /*
856  * Lock pg_subscription with AccessExclusiveLock to ensure that the
857  * launcher doesn't restart new worker during dropping the subscription
858  */
859  rel = table_open(SubscriptionRelationId, AccessExclusiveLock);
860 
862  CStringGetDatum(stmt->subname));
863 
864  if (!HeapTupleIsValid(tup))
865  {
866  table_close(rel, NoLock);
867 
868  if (!stmt->missing_ok)
869  ereport(ERROR,
870  (errcode(ERRCODE_UNDEFINED_OBJECT),
871  errmsg("subscription \"%s\" does not exist",
872  stmt->subname)));
873  else
874  ereport(NOTICE,
875  (errmsg("subscription \"%s\" does not exist, skipping",
876  stmt->subname)));
877 
878  return;
879  }
880 
881  form = (Form_pg_subscription) GETSTRUCT(tup);
882  subid = form->oid;
883 
884  /* must be owner */
885  if (!pg_subscription_ownercheck(subid, GetUserId()))
887  stmt->subname);
888 
889  /* DROP hook for the subscription being removed */
890  InvokeObjectDropHook(SubscriptionRelationId, subid, 0);
891 
892  /*
893  * Lock the subscription so nobody else can do anything with it (including
894  * the replication workers).
895  */
896  LockSharedObject(SubscriptionRelationId, subid, 0, AccessExclusiveLock);
897 
898  /* Get subname */
899  datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
900  Anum_pg_subscription_subname, &isnull);
901  Assert(!isnull);
902  subname = pstrdup(NameStr(*DatumGetName(datum)));
903 
904  /* Get conninfo */
905  datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
906  Anum_pg_subscription_subconninfo, &isnull);
907  Assert(!isnull);
908  conninfo = TextDatumGetCString(datum);
909 
910  /* Get slotname */
911  datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
912  Anum_pg_subscription_subslotname, &isnull);
913  if (!isnull)
914  slotname = pstrdup(NameStr(*DatumGetName(datum)));
915  else
916  slotname = NULL;
917 
918  /*
919  * Since dropping a replication slot is not transactional, the replication
920  * slot stays dropped even if the transaction rolls back. So we cannot
921  * run DROP SUBSCRIPTION inside a transaction block if dropping the
922  * replication slot.
923  *
924  * XXX The command name should really be something like "DROP SUBSCRIPTION
925  * of a subscription that is associated with a replication slot", but we
926  * don't have the proper facilities for that.
927  */
928  if (slotname)
929  PreventInTransactionBlock(isTopLevel, "DROP SUBSCRIPTION");
930 
931 
932  ObjectAddressSet(myself, SubscriptionRelationId, subid);
933  EventTriggerSQLDropAddObject(&myself, true, true);
934 
935  /* Remove the tuple from catalog. */
936  CatalogTupleDelete(rel, &tup->t_self);
937 
938  ReleaseSysCache(tup);
939 
940  /*
941  * Stop all the subscription workers immediately.
942  *
943  * This is necessary if we are dropping the replication slot, so that the
944  * slot becomes accessible.
945  *
946  * It is also necessary if the subscription is disabled and was disabled
947  * in the same transaction. Then the workers haven't seen the disabling
948  * yet and will still be running, leading to hangs later when we want to
949  * drop the replication origin. If the subscription was disabled before
950  * this transaction, then there shouldn't be any workers left, so this
951  * won't make a difference.
952  *
953  * New workers won't be started because we hold an exclusive lock on the
954  * subscription till the end of the transaction.
955  */
956  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
957  subworkers = logicalrep_workers_find(subid, false);
958  LWLockRelease(LogicalRepWorkerLock);
959  foreach(lc, subworkers)
960  {
962 
964  }
965  list_free(subworkers);
966 
967  /* Clean up dependencies */
968  deleteSharedDependencyRecordsFor(SubscriptionRelationId, subid, 0);
969 
970  /* Remove any associated relation synchronization states. */
972 
973  /* Remove the origin tracking if exists. */
974  snprintf(originname, sizeof(originname), "pg_%u", subid);
975  originid = replorigin_by_name(originname, true);
976  if (originid != InvalidRepOriginId)
977  replorigin_drop(originid, false);
978 
979  /*
980  * If there is no slot associated with the subscription, we can finish
981  * here.
982  */
983  if (!slotname)
984  {
985  table_close(rel, NoLock);
986  return;
987  }
988 
989  /*
990  * Otherwise drop the replication slot at the publisher node using the
991  * replication connection.
992  */
993  load_file("libpqwalreceiver", false);
994 
995  initStringInfo(&cmd);
996  appendStringInfo(&cmd, "DROP_REPLICATION_SLOT %s WAIT", quote_identifier(slotname));
997 
998  wrconn = walrcv_connect(conninfo, true, subname, &err);
999  if (wrconn == NULL)
1000  ereport(ERROR,
1001  (errmsg("could not connect to publisher when attempting to "
1002  "drop the replication slot \"%s\"", slotname),
1003  errdetail("The error was: %s", err),
1004  /* translator: %s is an SQL ALTER command */
1005  errhint("Use %s to disassociate the subscription from the slot.",
1006  "ALTER SUBSCRIPTION ... SET (slot_name = NONE)")));
1007 
1008  PG_TRY();
1009  {
1010  WalRcvExecResult *res;
1011 
1012  res = walrcv_exec(wrconn, cmd.data, 0, NULL);
1013 
1014  if (res->status != WALRCV_OK_COMMAND)
1015  ereport(ERROR,
1016  (errmsg("could not drop the replication slot \"%s\" on publisher",
1017  slotname),
1018  errdetail("The error was: %s", res->err)));
1019  else
1020  ereport(NOTICE,
1021  (errmsg("dropped replication slot \"%s\" on publisher",
1022  slotname)));
1023 
1024  walrcv_clear_result(res);
1025  }
1026  PG_CATCH();
1027  {
1028  /* Close the connection in case of failure */
1029  walrcv_disconnect(wrconn);
1030  PG_RE_THROW();
1031  }
1032  PG_END_TRY();
1033 
1034  walrcv_disconnect(wrconn);
1035 
1036  pfree(cmd.data);
1037 
1038  table_close(rel, NoLock);
1039 }
WalReceiverConn * wrconn
Definition: worker.c:100
List * logicalrep_workers_find(Oid subid, bool only_running)
Definition: launcher.c:271
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
int errhint(const char *fmt,...)
Definition: elog.c:974
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
void RemoveSubscriptionRel(Oid subid, Oid relid)
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10628
Oid GetUserId(void)
Definition: miscinit.c:380
char * pstrdup(const char *in)
Definition: mcxt.c:1161
void replorigin_drop(RepOriginId roident, bool nowait)
Definition: origin.c:335
#define InvokeObjectDropHook(classId, objectId, subId)
Definition: objectaccess.h:154
uint16 RepOriginId
Definition: xlogdefs.h:58
int errcode(int sqlerrcode)
Definition: elog.c:570
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:269
FormData_pg_subscription * Form_pg_subscription
unsigned int Oid
Definition: postgres_ext.h:31
NameData subname
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:212
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define NAMEDATALEN
#define DatumGetName(X)
Definition: postgres.h:585
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3353
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:285
void pfree(void *pointer)
Definition: mcxt.c:1031
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define NoLock
Definition: lockdefs.h:34
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:464
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define CStringGetDatum(X)
Definition: postgres.h:578
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3328
#define ereport(elevel, rest)
Definition: elog.h:141
void deleteSharedDependencyRecordsFor(Oid classId, Oid objectId, int32 objectSubId)
Definition: pg_shdepend.c:908
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define TextDatumGetCString(d)
Definition: builtins.h:84
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1385
Oid MyDatabaseId
Definition: globals.c:85
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1004
#define InvalidOid
Definition: postgres_ext.h:36
#define NOTICE
Definition: elog.h:37
#define PG_CATCH()
Definition: elog.h:310
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
WalRcvExecStatus status
Definition: walreceiver.h:195
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:146
void EventTriggerSQLDropAddObject(const ObjectAddress *object, bool original, bool normal)
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1135
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
#define PG_RE_THROW()
Definition: elog.h:331
#define walrcv_disconnect(conn)
Definition: walreceiver.h:281
#define InvalidRepOriginId
Definition: origin.h:33
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define AccessExclusiveLock
Definition: lockdefs.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:784
void list_free(List *list)
Definition: list.c:1373
#define NameStr(name)
Definition: c.h:609
#define PG_TRY()
Definition: elog.h:301
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define snprintf
Definition: port.h:192
#define PG_END_TRY()
Definition: elog.h:317
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:279
bool pg_subscription_ownercheck(Oid sub_oid, Oid roleid)
Definition: aclchk.c:5319
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:255