PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

ObjectAddress AlterSubscription ( AlterSubscriptionStmt stmt)

Definition at line 545 of file subscriptioncmds.c.

References ACL_KIND_SUBSCRIPTION, aclcheck_error(), ACLCHECK_NOT_OWNER, ALTER_SUBSCRIPTION_CONNECTION, ALTER_SUBSCRIPTION_ENABLED, ALTER_SUBSCRIPTION_OPTIONS, ALTER_SUBSCRIPTION_PUBLICATION, ALTER_SUBSCRIPTION_PUBLICATION_REFRESH, ALTER_SUBSCRIPTION_REFRESH, AlterSubscription_refresh(), Anum_pg_subscription_subconninfo, Anum_pg_subscription_subenabled, Anum_pg_subscription_subpublications, Anum_pg_subscription_subslotname, Assert, BoolGetDatum, CatalogTupleUpdate(), AlterSubscriptionStmt::conninfo, CStringGetDatum, CStringGetTextDatum, DirectFunctionCall1, elog, ereport, errcode(), errmsg(), ERROR, GetSubscription(), GetUserId(), heap_close, heap_freetuple(), heap_modify_tuple(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, InvokeObjectPostAlterHook, AlterSubscriptionStmt::kind, MyDatabaseId, namein(), Natts_pg_subscription, NULL, ObjectAddressSet, AlterSubscriptionStmt::options, parse_subscription_options(), pg_subscription_ownercheck(), AlterSubscriptionStmt::publication, publicationListToArray(), Subscription::publications, RelationGetDescr, RowExclusiveLock, SearchSysCacheCopy2, AlterSubscriptionStmt::subname, SUBSCRIPTIONNAME, SubscriptionRelationId, HeapTupleData::t_self, and values.

Referenced by ProcessUtilitySlow().

546 {
547  Relation rel;
548  ObjectAddress myself;
549  bool nulls[Natts_pg_subscription];
550  bool replaces[Natts_pg_subscription];
552  HeapTuple tup;
553  Oid subid;
554  bool update_tuple = false;
555 
557 
558  /* Fetch the existing tuple. */
560  CStringGetDatum(stmt->subname));
561 
562  if (!HeapTupleIsValid(tup))
563  ereport(ERROR,
564  (errcode(ERRCODE_UNDEFINED_OBJECT),
565  errmsg("subscription \"%s\" does not exist",
566  stmt->subname)));
567 
568  /* must be owner */
571  stmt->subname);
572 
573  subid = HeapTupleGetOid(tup);
574 
575  /* Form a new tuple. */
576  memset(values, 0, sizeof(values));
577  memset(nulls, false, sizeof(nulls));
578  memset(replaces, false, sizeof(replaces));
579 
580  switch (stmt->kind)
581  {
583  {
584  char *slot_name;
585 
587  NULL, &slot_name, NULL);
588 
591  replaces[Anum_pg_subscription_subslotname - 1] = true;
592 
593  update_tuple = true;
594  break;
595  }
596 
598  {
599  bool enabled,
600  enabled_given;
601 
603  &enabled_given, &enabled, NULL,
604  NULL, NULL);
605  Assert(enabled_given);
606 
607  values[Anum_pg_subscription_subenabled - 1] =
608  BoolGetDatum(enabled);
609  replaces[Anum_pg_subscription_subenabled - 1] = true;
610 
611  update_tuple = true;
612  break;
613  }
614 
618  replaces[Anum_pg_subscription_subconninfo - 1] = true;
619  update_tuple = true;
620  break;
621 
624  {
625  bool copy_data;
626  Subscription *sub = GetSubscription(subid, false);
627 
629  NULL, NULL, &copy_data);
630 
633  replaces[Anum_pg_subscription_subpublications - 1] = true;
634 
635  update_tuple = true;
636 
637  /* Refresh if user asked us to. */
639  {
640  /* Make sure refresh sees the new list of publications. */
641  sub->publications = stmt->publication;
642 
643  AlterSubscription_refresh(sub, copy_data);
644  }
645 
646  break;
647  }
648 
650  {
651  bool copy_data;
652  Subscription *sub = GetSubscription(subid, false);
653 
655  NULL, NULL, &copy_data);
656 
657  AlterSubscription_refresh(sub, copy_data);
658 
659  break;
660  }
661 
662  default:
663  elog(ERROR, "unrecognized ALTER SUBSCRIPTION kind %d",
664  stmt->kind);
665  }
666 
667  /* Update the catalog if needed. */
668  if (update_tuple)
669  {
670  tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
671  replaces);
672 
673  CatalogTupleUpdate(rel, &tup->t_self, tup);
674 
675  heap_freetuple(tup);
676  }
677 
679 
681 
683 
684  return myself;
685 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
#define Anum_pg_subscription_subpublications
#define RelationGetDescr(relation)
Definition: rel.h:429
Oid GetUserId(void)
Definition: miscinit.c:283
int errcode(int sqlerrcode)
Definition: elog.c:575
static Datum publicationListToArray(List *publist)
#define heap_close(r, l)
Definition: heapam.h:97
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
Subscription * GetSubscription(Oid subid, bool missing_ok)
#define ERROR
Definition: elog.h:43
#define Natts_pg_subscription
ItemPointerData t_self
Definition: htup.h:65
#define SubscriptionRelationId
AlterSubscriptionType kind
Definition: parsenodes.h:3361
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:584
List * publications
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:163
#define Anum_pg_subscription_subslotname
uintptr_t Datum
Definition: postgres.h:372
Oid MyDatabaseId
Definition: globals.c:76
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
static void parse_subscription_options(List *options, bool *connect, bool *enabled_given, bool *enabled, bool *create_slot, char **slot_name, bool *copy_data)
#define BoolGetDatum(X)
Definition: postgres.h:408
#define Anum_pg_subscription_subconninfo
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define Anum_pg_subscription_subenabled
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CStringGetTextDatum(s)
Definition: builtins.h:91
#define elog
Definition: elog.h:219
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:163
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:793
static void AlterSubscription_refresh(Subscription *sub, bool copy_data)
bool pg_subscription_ownercheck(Oid sub_oid, Oid roleid)
Definition: aclchk.c:5110
ObjectAddress AlterSubscriptionOwner ( const char *  name,
Oid  newOwnerId 
)

Definition at line 897 of file subscriptioncmds.c.

References AlterSubscriptionOwner_internal(), CStringGetDatum, ereport, errcode(), errmsg(), ERROR, heap_close, heap_freetuple(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, MyDatabaseId, ObjectAddressSet, RowExclusiveLock, SearchSysCacheCopy2, SUBSCRIPTIONNAME, and SubscriptionRelationId.

Referenced by ExecAlterOwnerStmt().

898 {
899  Oid subid;
900  HeapTuple tup;
901  Relation rel;
902  ObjectAddress address;
903 
905 
908 
909  if (!HeapTupleIsValid(tup))
910  ereport(ERROR,
911  (errcode(ERRCODE_UNDEFINED_OBJECT),
912  errmsg("subscription \"%s\" does not exist", name)));
913 
914  subid = HeapTupleGetOid(tup);
915 
916  AlterSubscriptionOwner_internal(rel, tup, newOwnerId);
917 
918  ObjectAddressSet(address, SubscriptionRelationId, subid);
919 
920  heap_freetuple(tup);
921 
923 
924  return address;
925 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define SubscriptionRelationId
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:584
#define ereport(elevel, rest)
Definition: elog.h:122
Oid MyDatabaseId
Definition: globals.c:76
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
static void AlterSubscriptionOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
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:797
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:163
void AlterSubscriptionOwner_oid ( Oid  subid,
Oid  newOwnerId 
)

Definition at line 931 of file subscriptioncmds.c.

References AlterSubscriptionOwner_internal(), ereport, errcode(), errmsg(), ERROR, heap_close, heap_freetuple(), heap_open(), HeapTupleIsValid, ObjectIdGetDatum, RowExclusiveLock, SearchSysCacheCopy1, SUBSCRIPTIONOID, and SubscriptionRelationId.

Referenced by shdepReassignOwned().

932 {
933  HeapTuple tup;
934  Relation rel;
935 
937 
939 
940  if (!HeapTupleIsValid(tup))
941  ereport(ERROR,
942  (errcode(ERRCODE_UNDEFINED_OBJECT),
943  errmsg("subscription with OID %u does not exist", subid)));
944 
945  AlterSubscriptionOwner_internal(rel, tup, newOwnerId);
946 
947  heap_freetuple(tup);
948 
950 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define SubscriptionRelationId
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:122
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
static void AlterSubscriptionOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:161
int errmsg(const char *fmt,...)
Definition: elog.c:797
ObjectAddress CreateSubscription ( CreateSubscriptionStmt stmt,
bool  isTopLevel 
)

Definition at line 259 of file subscriptioncmds.c.

References AccessShareLock, Anum_pg_subscription_subconninfo, Anum_pg_subscription_subdbid, Anum_pg_subscription_subenabled, Anum_pg_subscription_subname, Anum_pg_subscription_subowner, Anum_pg_subscription_subpublications, Anum_pg_subscription_subslotname, ApplyLauncherWakeupAtCommit(), BoolGetDatum, CatalogTupleInsert(), connect, CreateSubscriptionStmt::conninfo, CRS_NOEXPORT_SNAPSHOT, CStringGetDatum, CStringGetTextDatum, DirectFunctionCall1, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, fetch_table_list(), GetSysCacheOid2, GetUserId(), heap_close, heap_form_tuple(), heap_freetuple(), heap_open(), InvalidXLogRecPtr, InvokeObjectPostCreateHook, lfirst, load_file(), MyDatabaseId, NAMEDATALEN, namein(), Natts_pg_subscription, NOTICE, NULL, ObjectAddressSet, ObjectIdGetDatum, OidIsValid, CreateSubscriptionStmt::options, parse_subscription_options(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PreventTransactionChain(), CreateSubscriptionStmt::publication, publicationListToArray(), RangeVarGetRelid, recordDependencyOnOwner(), RelationGetDescr, replorigin_create(), RowExclusiveLock, SetSubscriptionRelState(), snprintf(), CreateSubscriptionStmt::subname, SUBREL_STATE_INIT, SUBREL_STATE_READY, SUBSCRIPTIONNAME, SubscriptionRelationId, superuser(), values, walrcv_check_conninfo, walrcv_connect, walrcv_create_slot, walrcv_disconnect, WARNING, and wrconn.

Referenced by ProcessUtilitySlow().

260 {
261  Relation rel;
262  ObjectAddress myself;
263  Oid subid;
264  bool nulls[Natts_pg_subscription];
266  Oid owner = GetUserId();
267  HeapTuple tup;
268  bool connect;
269  bool enabled_given;
270  bool enabled;
271  bool copy_data;
272  char *conninfo;
273  char *slotname;
274  char originname[NAMEDATALEN];
275  bool create_slot;
276  List *publications;
277 
278  /*
279  * Parse and check options.
280  * Connection and publication should not be specified here.
281  */
282  parse_subscription_options(stmt->options, &connect, &enabled_given,
283  &enabled, &create_slot, &slotname, &copy_data);
284 
285  /*
286  * Since creating a replication slot is not transactional, rolling back
287  * the transaction leaves the created replication slot. So we cannot run
288  * CREATE SUBSCRIPTION inside a transaction block if creating a
289  * replication slot.
290  */
291  if (create_slot)
292  PreventTransactionChain(isTopLevel, "CREATE SUBSCRIPTION ... CREATE SLOT");
293 
294  if (!superuser())
295  ereport(ERROR,
296  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
297  (errmsg("must be superuser to create subscriptions"))));
298 
300 
301  /* Check if name is used */
303  CStringGetDatum(stmt->subname));
304  if (OidIsValid(subid))
305  {
306  ereport(ERROR,
308  errmsg("subscription \"%s\" already exists",
309  stmt->subname)));
310  }
311 
312  if (slotname == NULL)
313  slotname = stmt->subname;
314 
315  conninfo = stmt->conninfo;
316  publications = stmt->publication;
317 
318  /* Load the library providing us libpq calls. */
319  load_file("libpqwalreceiver", false);
320 
321  /* Check the connection info string. */
322  walrcv_check_conninfo(conninfo);
323 
324  /* Everything ok, form a new tuple. */
325  memset(values, 0, sizeof(values));
326  memset(nulls, false, sizeof(nulls));
327 
329  values[Anum_pg_subscription_subname - 1] =
331  values[Anum_pg_subscription_subowner - 1] = ObjectIdGetDatum(owner);
332  values[Anum_pg_subscription_subenabled - 1] = BoolGetDatum(enabled);
334  CStringGetTextDatum(conninfo);
338  publicationListToArray(publications);
339 
340  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
341 
342  /* Insert tuple into catalog. */
343  subid = CatalogTupleInsert(rel, tup);
344  heap_freetuple(tup);
345 
347 
348  snprintf(originname, sizeof(originname), "pg_%u", subid);
349  replorigin_create(originname);
350 
351  /*
352  * Connect to remote side to execute requested commands and fetch table
353  * info.
354  */
355  if (connect)
356  {
357  XLogRecPtr lsn;
358  char *err;
360  List *tables;
361  ListCell *lc;
362  char table_state;
363 
364  /* Try to connect to the publisher. */
365  wrconn = walrcv_connect(conninfo, true, stmt->subname, &err);
366  if (!wrconn)
367  ereport(ERROR,
368  (errmsg("could not connect to the publisher: %s", err)));
369 
370  PG_TRY();
371  {
372  /*
373  * If requested, create permanent slot for the subscription.
374  * We won't use the initial snapshot for anything, so no need
375  * to export it.
376  */
377  if (create_slot)
378  {
379  walrcv_create_slot(wrconn, slotname, false,
380  CRS_NOEXPORT_SNAPSHOT, &lsn);
381  ereport(NOTICE,
382  (errmsg("created replication slot \"%s\" on publisher",
383  slotname)));
384  }
385 
386  /*
387  * Set sync state based on if we were asked to do data copy or
388  * not.
389  */
390  table_state = copy_data ? SUBREL_STATE_INIT : SUBREL_STATE_READY;
391 
392  /*
393  * Get the table list from publisher and build local table status
394  * info.
395  */
396  tables = fetch_table_list(wrconn, publications);
397  foreach (lc, tables)
398  {
399  RangeVar *rv = (RangeVar *) lfirst(lc);
400  Oid relid;
401 
402  relid = RangeVarGetRelid(rv, AccessShareLock, false);
403 
404  SetSubscriptionRelState(subid, relid, table_state,
406  }
407 
408  ereport(NOTICE,
409  (errmsg("synchronized table states")));
410  }
411  PG_CATCH();
412  {
413  /* Close the connection in case of failure. */
414  walrcv_disconnect(wrconn);
415  PG_RE_THROW();
416  }
417  PG_END_TRY();
418 
419  /* And we are done with the remote side. */
420  walrcv_disconnect(wrconn);
421  }
422  else
424  (errmsg("tables were not subscribed, you will have to run "
425  "ALTER SUBSCRIPTION ... REFRESH PUBLICATION to "
426  "subscribe the tables")));
427 
429 
431 
433 
435 
436  return myself;
437 }
#define connect(s, name, namelen)
Definition: win32.h:383
#define SUBREL_STATE_INIT
static List * fetch_table_list(WalReceiverConn *wrconn, List *publications)
WalReceiverConn * wrconn
Definition: worker.c:107
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
#define Anum_pg_subscription_subpublications
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define Anum_pg_subscription_subowner
#define RelationGetDescr(relation)
Definition: rel.h:429
Oid GetUserId(void)
Definition: miscinit.c:283
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:53
#define walrcv_check_conninfo(conninfo)
Definition: walreceiver.h:244
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static Datum publicationListToArray(List *publist)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
#define heap_close(r, l)
Definition: heapam.h:97
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:159
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
#define walrcv_create_slot(conn, slotname, temporary, snapshot_action, lsn)
Definition: walreceiver.h:260
#define OidIsValid(objectId)
Definition: c.h:538
#define GetSysCacheOid2(cacheId, key1, key2)
Definition: syscache.h:181
#define NAMEDATALEN
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:162
#define Natts_pg_subscription
#define Anum_pg_subscription_subname
#define SubscriptionRelationId
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:584
RepOriginId replorigin_create(char *roname)
Definition: origin.c:235
#define ereport(elevel, rest)
Definition: elog.h:122
#define Anum_pg_subscription_subslotname
#define Anum_pg_subscription_subdbid
#define WARNING
Definition: elog.h:40
uintptr_t Datum
Definition: postgres.h:372
Oid MyDatabaseId
Definition: globals.c:76
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
static void parse_subscription_options(List *options, bool *connect, bool *enabled_given, bool *enabled, bool *create_slot, char **slot_name, bool *copy_data)
#define BoolGetDatum(X)
Definition: postgres.h:408
#define NOTICE
Definition: elog.h:37
#define Anum_pg_subscription_subconninfo
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define lfirst(lc)
Definition: pg_list.h:106
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:133
#define PG_RE_THROW()
Definition: elog.h:314
#define walrcv_disconnect(conn)
Definition: walreceiver.h:264
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define Anum_pg_subscription_subenabled
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CStringGetTextDatum(s)
Definition: builtins.h:91
void ApplyLauncherWakeupAtCommit(void)
Definition: launcher.c:615
#define PG_TRY()
Definition: elog.h:284
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31
Definition: pg_list.h:45
Oid SetSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn)
#define SUBREL_STATE_READY
#define PG_END_TRY()
Definition: elog.h:300
void PreventTransactionChain(bool isTopLevel, const char *stmtType)
Definition: xact.c:3154
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:242
void DropSubscription ( DropSubscriptionStmt stmt,
bool  isTopLevel 
)

Definition at line 691 of file subscriptioncmds.c.

References AccessExclusiveLock, ACL_KIND_SUBSCRIPTION, aclcheck_error(), ACLCHECK_NOT_OWNER, Anum_pg_subscription_subconninfo, Anum_pg_subscription_subname, Anum_pg_subscription_subslotname, appendStringInfo(), Assert, CatalogTupleDelete(), CStringGetDatum, StringInfoData::data, DatumGetName, deleteSharedDependencyRecordsFor(), DropSubscriptionStmt::drop_slot, ereport, WalRcvExecResult::err, errcode(), errdetail(), errmsg(), ERROR, EventTriggerSQLDropAddObject(), GetUserId(), heap_close, heap_open(), HeapTupleGetOid, HeapTupleIsValid, initStringInfo(), InvalidOid, InvalidRepOriginId, InvokeObjectDropHook, load_file(), LockSharedObject(), logicalrep_worker_stop(), DropSubscriptionStmt::missing_ok, MyDatabaseId, NAMEDATALEN, NameStr, NoLock, NOTICE, NULL, ObjectAddressSet, pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, pg_subscription_ownercheck(), PG_TRY, PreventTransactionChain(), pstrdup(), ReleaseSysCache(), RemoveSubscriptionRel(), replorigin_by_name(), replorigin_drop(), SearchSysCache2, snprintf(), WalRcvExecResult::status, DropSubscriptionStmt::subname, SUBSCRIPTIONNAME, SUBSCRIPTIONOID, SubscriptionRelationId, SysCacheGetAttr(), HeapTupleData::t_self, TextDatumGetCString, walrcv_clear_result(), walrcv_connect, walrcv_disconnect, walrcv_exec, WALRCV_OK_COMMAND, and wrconn.

Referenced by ProcessUtilitySlow().

692 {
693  Relation rel;
694  ObjectAddress myself;
695  HeapTuple tup;
696  Oid subid;
697  Datum datum;
698  bool isnull;
699  char *subname;
700  char *conninfo;
701  char *slotname;
702  char originname[NAMEDATALEN];
703  char *err = NULL;
704  RepOriginId originid;
706  StringInfoData cmd;
707 
708  /*
709  * Since dropping a replication slot is not transactional, the replication
710  * slot stays dropped even if the transaction rolls back. So we cannot
711  * run DROP SUBSCRIPTION inside a transaction block if dropping the
712  * replication slot.
713  */
714  if (stmt->drop_slot)
715  PreventTransactionChain(isTopLevel, "DROP SUBSCRIPTION ... DROP SLOT");
716 
717  /*
718  * Lock pg_subscription with AccessExclusiveLock to ensure
719  * that the launcher doesn't restart new worker during dropping
720  * the subscription
721  */
723 
725  CStringGetDatum(stmt->subname));
726 
727  if (!HeapTupleIsValid(tup))
728  {
729  heap_close(rel, NoLock);
730 
731  if (!stmt->missing_ok)
732  ereport(ERROR,
733  (errcode(ERRCODE_UNDEFINED_OBJECT),
734  errmsg("subscription \"%s\" does not exist",
735  stmt->subname)));
736  else
737  ereport(NOTICE,
738  (errmsg("subscription \"%s\" does not exist, skipping",
739  stmt->subname)));
740 
741  return;
742  }
743 
744  subid = HeapTupleGetOid(tup);
745 
746  /* must be owner */
747  if (!pg_subscription_ownercheck(subid, GetUserId()))
749  stmt->subname);
750 
751  /* DROP hook for the subscription being removed */
753 
754  /*
755  * Lock the subscription so nobody else can do anything with it
756  * (including the replication workers).
757  */
759 
760  /* Get subname */
761  datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
763  Assert(!isnull);
764  subname = pstrdup(NameStr(*DatumGetName(datum)));
765 
766  /* Get conninfo */
767  datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
769  Assert(!isnull);
770  conninfo = pstrdup(TextDatumGetCString(datum));
771 
772  /* Get slotname */
773  datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
775  Assert(!isnull);
776  slotname = pstrdup(NameStr(*DatumGetName(datum)));
777 
779  EventTriggerSQLDropAddObject(&myself, true, true);
780 
781  /* Remove the tuple from catalog. */
782  CatalogTupleDelete(rel, &tup->t_self);
783 
784  ReleaseSysCache(tup);
785 
786  /* Clean up dependencies */
788 
789  /* Remove any associated relation synchronization states. */
791 
792  /* Kill the apply worker so that the slot becomes accessible. */
794 
795  /* Remove the origin tracking if exists. */
796  snprintf(originname, sizeof(originname), "pg_%u", subid);
797  originid = replorigin_by_name(originname, true);
798  if (originid != InvalidRepOriginId)
799  replorigin_drop(originid);
800 
801  /* If the user asked to not drop the slot, we are done mow.*/
802  if (!stmt->drop_slot)
803  {
804  heap_close(rel, NoLock);
805  return;
806  }
807 
808  /*
809  * Otherwise drop the replication slot at the publisher node using
810  * the replication connection.
811  */
812  load_file("libpqwalreceiver", false);
813 
814  initStringInfo(&cmd);
815  appendStringInfo(&cmd, "DROP_REPLICATION_SLOT \"%s\"", slotname);
816 
817  wrconn = walrcv_connect(conninfo, true, subname, &err);
818  if (wrconn == NULL)
819  ereport(ERROR,
820  (errmsg("could not connect to publisher when attempting to "
821  "drop the replication slot \"%s\"", slotname),
822  errdetail("The error was: %s", err)));
823 
824  PG_TRY();
825  {
826  WalRcvExecResult *res;
827  res = walrcv_exec(wrconn, cmd.data, 0, NULL);
828 
829  if (res->status != WALRCV_OK_COMMAND)
830  ereport(ERROR,
831  (errmsg("could not drop the replication slot \"%s\" on publisher",
832  slotname),
833  errdetail("The error was: %s", res->err)));
834  else
835  ereport(NOTICE,
836  (errmsg("dropped replication slot \"%s\" on publisher",
837  slotname)));
838 
839  walrcv_clear_result(res);
840  }
841  PG_CATCH();
842  {
843  /* Close the connection in case of failure */
844  walrcv_disconnect(wrconn);
845  PG_RE_THROW();
846  }
847  PG_END_TRY();
848 
849  walrcv_disconnect(wrconn);
850 
851  pfree(cmd.data);
852 
853  heap_close(rel, NoLock);
854 }
WalReceiverConn * wrconn
Definition: worker.c:107
void RemoveSubscriptionRel(Oid subid, Oid relid)
Oid GetUserId(void)
Definition: miscinit.c:283
char * pstrdup(const char *in)
Definition: mcxt.c:1077
#define InvokeObjectDropHook(classId, objectId, subId)
Definition: objectaccess.h:154
uint16 RepOriginId
Definition: xlogdefs.h:51
int errcode(int sqlerrcode)
Definition: elog.c:575
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:206
#define NAMEDATALEN
#define DatumGetName(X)
Definition: postgres.h:591
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:268
void pfree(void *pointer)
Definition: mcxt.c:950
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define ERROR
Definition: elog.h:43
#define Anum_pg_subscription_subname
ItemPointerData t_self
Definition: htup.h:65
#define SubscriptionRelationId
#define NoLock
Definition: lockdefs.h:34
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:328
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define CStringGetDatum(X)
Definition: postgres.h:584
#define ereport(elevel, rest)
Definition: elog.h:122
void deleteSharedDependencyRecordsFor(Oid classId, Oid objectId, int32 objectSubId)
Definition: pg_shdepend.c:823
#define Anum_pg_subscription_subslotname
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
void replorigin_drop(RepOriginId roident)
Definition: origin.c:327
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1278
Oid MyDatabaseId
Definition: globals.c:76
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:871
#define InvalidOid
Definition: postgres_ext.h:36
#define NOTICE
Definition: elog.h:37
#define Anum_pg_subscription_subconninfo
#define PG_CATCH()
Definition: elog.h:293
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
WalRcvExecStatus status
Definition: walreceiver.h:187
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:133
void EventTriggerSQLDropAddObject(const ObjectAddress *object, bool original, bool normal)
#define PG_RE_THROW()
Definition: elog.h:314
#define walrcv_disconnect(conn)
Definition: walreceiver.h:264
#define InvalidRepOriginId
Definition: origin.h:34
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define AccessExclusiveLock
Definition: lockdefs.h:46
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:499
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:262
void PreventTransactionChain(bool isTopLevel, const char *stmtType)
Definition: xact.c:3154
bool pg_subscription_ownercheck(Oid sub_oid, Oid roleid)
Definition: aclchk.c:5110
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:242
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:154