PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
subscriptioncmds.c File Reference
Include dependency graph for subscriptioncmds.c:

Go to the source code of this file.

Functions

static Listfetch_table_list (WalReceiverConn *wrconn, List *publications)
 
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 Datum publicationListToArray (List *publist)
 
ObjectAddress CreateSubscription (CreateSubscriptionStmt *stmt, bool isTopLevel)
 
static void AlterSubscription_refresh (Subscription *sub, bool copy_data)
 
ObjectAddress AlterSubscription (AlterSubscriptionStmt *stmt)
 
void DropSubscription (DropSubscriptionStmt *stmt, bool isTopLevel)
 
static void AlterSubscriptionOwner_internal (Relation rel, HeapTuple tup, Oid newOwnerId)
 
ObjectAddress AlterSubscriptionOwner (const char *name, Oid newOwnerId)
 
void AlterSubscriptionOwner_oid (Oid subid, Oid newOwnerId)
 

Function Documentation

ObjectAddress AlterSubscription ( AlterSubscriptionStmt stmt)

Definition at line 615 of file subscriptioncmds.c.

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

Referenced by ProcessUtilitySlow().

616 {
617  Relation rel;
618  ObjectAddress myself;
619  bool nulls[Natts_pg_subscription];
620  bool replaces[Natts_pg_subscription];
622  HeapTuple tup;
623  Oid subid;
624  bool update_tuple = false;
625  Subscription *sub;
626 
628 
629  /* Fetch the existing tuple. */
631  CStringGetDatum(stmt->subname));
632 
633  if (!HeapTupleIsValid(tup))
634  ereport(ERROR,
635  (errcode(ERRCODE_UNDEFINED_OBJECT),
636  errmsg("subscription \"%s\" does not exist",
637  stmt->subname)));
638 
639  /* must be owner */
642  stmt->subname);
643 
644  subid = HeapTupleGetOid(tup);
645  sub = GetSubscription(subid, false);
646 
647  /* Lock the subscription so nobody else can do anything with it. */
649 
650  /* Form a new tuple. */
651  memset(values, 0, sizeof(values));
652  memset(nulls, false, sizeof(nulls));
653  memset(replaces, false, sizeof(replaces));
654 
655  switch (stmt->kind)
656  {
658  {
659  char *slotname;
660  bool slotname_given;
661  char *synchronous_commit;
662 
664  NULL, &slotname_given, &slotname,
665  NULL, &synchronous_commit, NULL);
666 
667  if (slotname_given)
668  {
669  if (sub->enabled && !slotname)
670  ereport(ERROR,
671  (errcode(ERRCODE_SYNTAX_ERROR),
672  errmsg("cannot set slot_name = NONE for enabled subscription")));
673 
674  if (slotname)
677  else
678  nulls[Anum_pg_subscription_subslotname - 1] = true;
679  replaces[Anum_pg_subscription_subslotname - 1] = true;
680  }
681 
682  if (synchronous_commit)
683  {
685  CStringGetTextDatum(synchronous_commit);
686  replaces[Anum_pg_subscription_subsynccommit - 1] = true;
687  }
688 
689  update_tuple = true;
690  break;
691  }
692 
694  {
695  bool enabled,
696  enabled_given;
697 
699  &enabled_given, &enabled, NULL,
700  NULL, NULL, NULL, NULL, NULL);
701  Assert(enabled_given);
702 
703  if (!sub->slotname && enabled)
704  ereport(ERROR,
705  (errcode(ERRCODE_SYNTAX_ERROR),
706  errmsg("cannot enable subscription that does not have a slot name")));
707 
708  values[Anum_pg_subscription_subenabled - 1] =
709  BoolGetDatum(enabled);
710  replaces[Anum_pg_subscription_subenabled - 1] = true;
711 
712  if (enabled)
714 
715  update_tuple = true;
716  break;
717  }
718 
720  /* Load the library providing us libpq calls. */
721  load_file("libpqwalreceiver", false);
722  /* Check the connection info string. */
724 
727  replaces[Anum_pg_subscription_subconninfo - 1] = true;
728  update_tuple = true;
729  break;
730 
732  {
733  bool copy_data;
734  bool refresh;
735 
737  NULL, NULL, NULL, &copy_data,
738  NULL, &refresh);
739 
742  replaces[Anum_pg_subscription_subpublications - 1] = true;
743 
744  update_tuple = true;
745 
746  /* Refresh if user asked us to. */
747  if (refresh)
748  {
749  if (!sub->enabled)
750  ereport(ERROR,
751  (errcode(ERRCODE_SYNTAX_ERROR),
752  errmsg("ALTER SUBSCRIPTION with refresh is not allowed for disabled subscriptions"),
753  errhint("Use ALTER SUBSCRIPTION ... SET PUBLICATION ... WITH (refresh = false).")));
754 
755  /* Make sure refresh sees the new list of publications. */
756  sub->publications = stmt->publication;
757 
758  AlterSubscription_refresh(sub, copy_data);
759  }
760 
761  break;
762  }
763 
765  {
766  bool copy_data;
767 
768  if (!sub->enabled)
769  ereport(ERROR,
770  (errcode(ERRCODE_SYNTAX_ERROR),
771  errmsg("ALTER SUBSCRIPTION ... REFRESH is not allowed for disabled subscriptions")));
772 
774  NULL, NULL, NULL, &copy_data,
775  NULL, NULL);
776 
777  AlterSubscription_refresh(sub, copy_data);
778 
779  break;
780  }
781 
782  default:
783  elog(ERROR, "unrecognized ALTER SUBSCRIPTION kind %d",
784  stmt->kind);
785  }
786 
787  /* Update the catalog if needed. */
788  if (update_tuple)
789  {
790  tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
791  replaces);
792 
793  CatalogTupleUpdate(rel, &tup->t_self, tup);
794 
795  heap_freetuple(tup);
796  }
797 
799 
801 
803 
804  return myself;
805 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
int errhint(const char *fmt,...)
Definition: elog.c:987
#define Anum_pg_subscription_subpublications
#define RelationGetDescr(relation)
Definition: rel.h:428
Oid GetUserId(void)
Definition: miscinit.c:284
#define walrcv_check_conninfo(conninfo)
Definition: walreceiver.h:244
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:1372
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:3412
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
int synchronous_commit
Definition: xact.c:82
#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:77
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 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 load_file(const char *filename, bool restricted)
Definition: dfmgr.c:137
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:163
#define AccessExclusiveLock
Definition: lockdefs.h:45
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
void ApplyLauncherWakeupAtCommit(void)
Definition: launcher.c:776
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:167
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:791
#define Anum_pg_subscription_subsynccommit
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:5110
static void AlterSubscription_refresh ( Subscription sub,
bool  copy_data 
)
static

Definition at line 503 of file subscriptioncmds.c.

References AccessShareLock, CheckSubscriptionRelkind(), Subscription::conninfo, ereport, errmsg(), ERROR, fetch_table_list(), get_namespace_name(), get_rel_name(), get_rel_relkind(), GetSubscriptionRelations(), InvalidXLogRecPtr, lfirst, list_length(), load_file(), logicalrep_worker_stop(), Subscription::name, NOTICE, Subscription::oid, oid_cmp(), palloc(), Subscription::publications, qsort, quote_identifier(), RangeVarGetRelid, SubscriptionRelState::relid, RangeVar::relname, RemoveSubscriptionRel(), RangeVar::schemaname, SetSubscriptionRelState(), SUBREL_STATE_INIT, SUBREL_STATE_READY, walrcv_connect, walrcv_disconnect, and wrconn.

Referenced by AlterSubscription().

504 {
505  char *err;
506  List *pubrel_names;
507  List *subrel_states;
508  Oid *subrel_local_oids;
509  Oid *pubrel_local_oids;
510  ListCell *lc;
511  int off;
512 
513  /* Load the library providing us libpq calls. */
514  load_file("libpqwalreceiver", false);
515 
516  /* Try to connect to the publisher. */
517  wrconn = walrcv_connect(sub->conninfo, true, sub->name, &err);
518  if (!wrconn)
519  ereport(ERROR,
520  (errmsg("could not connect to the publisher: %s", err)));
521 
522  /* Get the table list from publisher. */
523  pubrel_names = fetch_table_list(wrconn, sub->publications);
524 
525  /* We are done with the remote side, close connection. */
527 
528  /* Get local table list. */
529  subrel_states = GetSubscriptionRelations(sub->oid);
530 
531  /*
532  * Build qsorted array of local table oids for faster lookup. This can
533  * potentially contain all tables in the database so speed of lookup is
534  * important.
535  */
536  subrel_local_oids = palloc(list_length(subrel_states) * sizeof(Oid));
537  off = 0;
538  foreach(lc, subrel_states)
539  {
541 
542  subrel_local_oids[off++] = relstate->relid;
543  }
544  qsort(subrel_local_oids, list_length(subrel_states),
545  sizeof(Oid), oid_cmp);
546 
547  /*
548  * Walk over the remote tables and try to match them to locally known
549  * tables. If the table is not known locally create a new state for it.
550  *
551  * Also builds array of local oids of remote tables for the next step.
552  */
553  off = 0;
554  pubrel_local_oids = palloc(list_length(pubrel_names) * sizeof(Oid));
555 
556  foreach(lc, pubrel_names)
557  {
558  RangeVar *rv = (RangeVar *) lfirst(lc);
559  Oid relid;
560 
561  relid = RangeVarGetRelid(rv, AccessShareLock, false);
562 
563  /* Check for supported relkind. */
565  rv->schemaname, rv->relname);
566 
567  pubrel_local_oids[off++] = relid;
568 
569  if (!bsearch(&relid, subrel_local_oids,
570  list_length(subrel_states), sizeof(Oid), oid_cmp))
571  {
572  SetSubscriptionRelState(sub->oid, relid,
574  InvalidXLogRecPtr, false);
575  ereport(NOTICE,
576  (errmsg("added subscription for table %s.%s",
578  quote_identifier(rv->relname))));
579  }
580  }
581 
582  /*
583  * Next remove state for tables we should not care about anymore using the
584  * data we collected above
585  */
586  qsort(pubrel_local_oids, list_length(pubrel_names),
587  sizeof(Oid), oid_cmp);
588 
589  for (off = 0; off < list_length(subrel_states); off++)
590  {
591  Oid relid = subrel_local_oids[off];
592 
593  if (!bsearch(&relid, pubrel_local_oids,
594  list_length(pubrel_names), sizeof(Oid), oid_cmp))
595  {
596  char *namespace;
597 
598  RemoveSubscriptionRel(sub->oid, relid);
599 
600  logicalrep_worker_stop(sub->oid, relid);
601 
602  namespace = get_namespace_name(get_rel_namespace(relid));
603  ereport(NOTICE,
604  (errmsg("removed subscription for table %s.%s",
605  quote_identifier(namespace),
606  quote_identifier(get_rel_name(relid)))));
607  }
608  }
609 }
#define SUBREL_STATE_INIT
static List * fetch_table_list(WalReceiverConn *wrconn, List *publications)
WalReceiverConn * wrconn
Definition: worker.c:110
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void RemoveSubscriptionRel(Oid subid, Oid relid)
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10419
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:53
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1801
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:1750
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
char * schemaname
Definition: primnodes.h:67
char * relname
Definition: primnodes.h:68
#define ERROR
Definition: elog.h:43
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3033
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:415
List * GetSubscriptionRelations(Oid subid)
List * publications
void CheckSubscriptionRelkind(char relkind, const char *nspname, const char *relname)
#define ereport(elevel, rest)
Definition: elog.h:122
Oid SetSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn, bool update_only)
#define NOTICE
Definition: elog.h:37
#define lfirst(lc)
Definition: pg_list.h:106
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:137
int oid_cmp(const void *p1, const void *p2)
Definition: oid.c:336
static int list_length(const List *l)
Definition: pg_list.h:89
#define walrcv_disconnect(conn)
Definition: walreceiver.h:264
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define qsort(a, b, c, d)
Definition: port.h:443
Definition: pg_list.h:45
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1726
#define SUBREL_STATE_READY
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:242
ObjectAddress AlterSubscriptionOwner ( const char *  name,
Oid  newOwnerId 
)

Definition at line 1029 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().

1030 {
1031  Oid subid;
1032  HeapTuple tup;
1033  Relation rel;
1034  ObjectAddress address;
1035 
1037 
1040 
1041  if (!HeapTupleIsValid(tup))
1042  ereport(ERROR,
1043  (errcode(ERRCODE_UNDEFINED_OBJECT),
1044  errmsg("subscription \"%s\" does not exist", name)));
1045 
1046  subid = HeapTupleGetOid(tup);
1047 
1048  AlterSubscriptionOwner_internal(rel, tup, newOwnerId);
1049 
1050  ObjectAddressSet(address, SubscriptionRelationId, subid);
1051 
1052  heap_freetuple(tup);
1053 
1055 
1056  return address;
1057 }
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:1372
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:77
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:167
static void AlterSubscriptionOwner_internal ( Relation  rel,
HeapTuple  tup,
Oid  newOwnerId 
)
static

Definition at line 992 of file subscriptioncmds.c.

References ACL_KIND_SUBSCRIPTION, aclcheck_error(), ACLCHECK_NOT_OWNER, CatalogTupleUpdate(), changeDependencyOnOwner(), ereport, errcode(), errhint(), errmsg(), ERROR, GETSTRUCT, GetUserId(), HeapTupleGetOid, InvokeObjectPostAlterHook, NameStr, pg_subscription_ownercheck(), SubscriptionRelationId, superuser_arg(), and HeapTupleData::t_self.

Referenced by AlterSubscriptionOwner(), and AlterSubscriptionOwner_oid().

993 {
995 
996  form = (Form_pg_subscription) GETSTRUCT(tup);
997 
998  if (form->subowner == newOwnerId)
999  return;
1000 
1003  NameStr(form->subname));
1004 
1005  /* New owner must be a superuser */
1006  if (!superuser_arg(newOwnerId))
1007  ereport(ERROR,
1008  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1009  errmsg("permission denied to change owner of subscription \"%s\"",
1010  NameStr(form->subname)),
1011  errhint("The owner of a subscription must be a superuser.")));
1012 
1013  form->subowner = newOwnerId;
1014  CatalogTupleUpdate(rel, &tup->t_self, tup);
1015 
1016  /* Update owner dependency reference */
1018  HeapTupleGetOid(tup),
1019  newOwnerId);
1020 
1022  HeapTupleGetOid(tup), 0);
1023 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Oid GetUserId(void)
Definition: miscinit.c:284
int errcode(int sqlerrcode)
Definition: elog.c:575
FormData_pg_subscription * Form_pg_subscription
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
Definition: pg_shdepend.c:304
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define SubscriptionRelationId
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:163
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:499
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
bool pg_subscription_ownercheck(Oid sub_oid, Oid roleid)
Definition: aclchk.c:5110
void AlterSubscriptionOwner_oid ( Oid  subid,
Oid  newOwnerId 
)

Definition at line 1063 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().

1064 {
1065  HeapTuple tup;
1066  Relation rel;
1067 
1069 
1071 
1072  if (!HeapTupleIsValid(tup))
1073  ereport(ERROR,
1074  (errcode(ERRCODE_UNDEFINED_OBJECT),
1075  errmsg("subscription with OID %u does not exist", subid)));
1076 
1077  AlterSubscriptionOwner_internal(rel, tup, newOwnerId);
1078 
1079  heap_freetuple(tup);
1080 
1082 }
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:1372
#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:165
int errmsg(const char *fmt,...)
Definition: elog.c:797
ObjectAddress CreateSubscription ( CreateSubscriptionStmt stmt,
bool  isTopLevel 
)

Definition at line 304 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, Anum_pg_subscription_subsynccommit, ApplyLauncherWakeupAtCommit(), Assert, BoolGetDatum, CatalogTupleInsert(), CheckSubscriptionRelkind(), connect, CreateSubscriptionStmt::conninfo, CRS_NOEXPORT_SNAPSHOT, CStringGetDatum, CStringGetTextDatum, DirectFunctionCall1, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, fetch_table_list(), get_rel_relkind(), 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, RangeVar::relname, replorigin_create(), RowExclusiveLock, RangeVar::schemaname, SetSubscriptionRelState(), snprintf(), CreateSubscriptionStmt::subname, SUBREL_STATE_INIT, SUBREL_STATE_READY, SUBSCRIPTIONNAME, SubscriptionRelationId, superuser(), synchronous_commit, values, walrcv_check_conninfo, walrcv_connect, walrcv_create_slot, walrcv_disconnect, WARNING, and wrconn.

Referenced by ProcessUtilitySlow().

305 {
306  Relation rel;
307  ObjectAddress myself;
308  Oid subid;
309  bool nulls[Natts_pg_subscription];
311  Oid owner = GetUserId();
312  HeapTuple tup;
313  bool connect;
314  bool enabled_given;
315  bool enabled;
316  bool copy_data;
317  char *synchronous_commit;
318  char *conninfo;
319  char *slotname;
320  bool slotname_given;
321  char originname[NAMEDATALEN];
322  bool create_slot;
323  List *publications;
324 
325  /*
326  * Parse and check options.
327  *
328  * Connection and publication should not be specified here.
329  */
330  parse_subscription_options(stmt->options, &connect, &enabled_given,
331  &enabled, &create_slot, &slotname_given,
332  &slotname, &copy_data, &synchronous_commit,
333  NULL);
334 
335  /*
336  * Since creating a replication slot is not transactional, rolling back
337  * the transaction leaves the created replication slot. So we cannot run
338  * CREATE SUBSCRIPTION inside a transaction block if creating a
339  * replication slot.
340  */
341  if (create_slot)
342  PreventTransactionChain(isTopLevel, "CREATE SUBSCRIPTION ... WITH (create_slot = true)");
343 
344  if (!superuser())
345  ereport(ERROR,
346  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
347  (errmsg("must be superuser to create subscriptions"))));
348 
350 
351  /* Check if name is used */
353  CStringGetDatum(stmt->subname));
354  if (OidIsValid(subid))
355  {
356  ereport(ERROR,
358  errmsg("subscription \"%s\" already exists",
359  stmt->subname)));
360  }
361 
362  if (!slotname_given && slotname == NULL)
363  slotname = stmt->subname;
364 
365  /* The default for synchronous_commit of subscriptions is off. */
366  if (synchronous_commit == NULL)
367  synchronous_commit = "off";
368 
369  conninfo = stmt->conninfo;
370  publications = stmt->publication;
371 
372  /* Load the library providing us libpq calls. */
373  load_file("libpqwalreceiver", false);
374 
375  /* Check the connection info string. */
376  walrcv_check_conninfo(conninfo);
377 
378  /* Everything ok, form a new tuple. */
379  memset(values, 0, sizeof(values));
380  memset(nulls, false, sizeof(nulls));
381 
383  values[Anum_pg_subscription_subname - 1] =
385  values[Anum_pg_subscription_subowner - 1] = ObjectIdGetDatum(owner);
386  values[Anum_pg_subscription_subenabled - 1] = BoolGetDatum(enabled);
388  CStringGetTextDatum(conninfo);
389  if (slotname)
392  else
393  nulls[Anum_pg_subscription_subslotname - 1] = true;
395  CStringGetTextDatum(synchronous_commit);
397  publicationListToArray(publications);
398 
399  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
400 
401  /* Insert tuple into catalog. */
402  subid = CatalogTupleInsert(rel, tup);
403  heap_freetuple(tup);
404 
406 
407  snprintf(originname, sizeof(originname), "pg_%u", subid);
408  replorigin_create(originname);
409 
410  /*
411  * Connect to remote side to execute requested commands and fetch table
412  * info.
413  */
414  if (connect)
415  {
416  XLogRecPtr lsn;
417  char *err;
419  List *tables;
420  ListCell *lc;
421  char table_state;
422 
423  /* Try to connect to the publisher. */
424  wrconn = walrcv_connect(conninfo, true, stmt->subname, &err);
425  if (!wrconn)
426  ereport(ERROR,
427  (errmsg("could not connect to the publisher: %s", err)));
428 
429  PG_TRY();
430  {
431  /*
432  * Set sync state based on if we were asked to do data copy or
433  * not.
434  */
435  table_state = copy_data ? SUBREL_STATE_INIT : SUBREL_STATE_READY;
436 
437  /*
438  * Get the table list from publisher and build local table status
439  * info.
440  */
441  tables = fetch_table_list(wrconn, publications);
442  foreach(lc, tables)
443  {
444  RangeVar *rv = (RangeVar *) lfirst(lc);
445  Oid relid;
446 
447  relid = RangeVarGetRelid(rv, AccessShareLock, false);
448 
449  /* Check for supported relkind. */
451  rv->schemaname, rv->relname);
452 
453  SetSubscriptionRelState(subid, relid, table_state,
454  InvalidXLogRecPtr, false);
455  }
456 
457  /*
458  * If requested, create permanent slot for the subscription. We
459  * won't use the initial snapshot for anything, so no need to
460  * export it.
461  */
462  if (create_slot)
463  {
464  Assert(slotname);
465 
466  walrcv_create_slot(wrconn, slotname, false,
467  CRS_NOEXPORT_SNAPSHOT, &lsn);
468  ereport(NOTICE,
469  (errmsg("created replication slot \"%s\" on publisher",
470  slotname)));
471  }
472  }
473  PG_CATCH();
474  {
475  /* Close the connection in case of failure. */
476  walrcv_disconnect(wrconn);
477  PG_RE_THROW();
478  }
479  PG_END_TRY();
480 
481  /* And we are done with the remote side. */
482  walrcv_disconnect(wrconn);
483  }
484  else
486  (errmsg("tables were not subscribed, you will have to run "
487  "ALTER SUBSCRIPTION ... REFRESH PUBLICATION to "
488  "subscribe the tables")));
489 
491 
492  if (enabled)
494 
496 
498 
499  return myself;
500 }
#define connect(s, name, namelen)
Definition: win32.h:373
#define SUBREL_STATE_INIT
static List * fetch_table_list(WalReceiverConn *wrconn, List *publications)
WalReceiverConn * wrconn
Definition: worker.c:110
#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:428
Oid GetUserId(void)
Definition: miscinit.c:284
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:53
#define walrcv_check_conninfo(conninfo)
Definition: walreceiver.h:244
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1801
#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:1372
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:185
char * schemaname
Definition: primnodes.h:67
#define NAMEDATALEN
char * relname
Definition: primnodes.h:68
#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
void CheckSubscriptionRelkind(char relkind, const char *nspname, const char *relname)
RepOriginId replorigin_create(char *roname)
Definition: origin.c:235
int synchronous_commit
Definition: xact.c:82
#define ereport(elevel, rest)
Definition: elog.h:122
#define Anum_pg_subscription_subslotname
#define Anum_pg_subscription_subdbid
#define WARNING
Definition: elog.h:40
Oid SetSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn, bool update_only)
uintptr_t Datum
Definition: postgres.h:372
Oid MyDatabaseId
Definition: globals.c:77
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#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 Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:137
#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:163
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CStringGetTextDatum(s)
Definition: builtins.h:91
void ApplyLauncherWakeupAtCommit(void)
Definition: launcher.c:776
#define PG_TRY()
Definition: elog.h:284
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
Definition: pg_list.h:45
#define SUBREL_STATE_READY
#define Anum_pg_subscription_subsynccommit
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:300
void PreventTransactionChain(bool isTopLevel, const char *stmtType)
Definition: xact.c:3155
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:242
void DropSubscription ( DropSubscriptionStmt stmt,
bool  isTopLevel 
)

Definition at line 811 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(), ereport, WalRcvExecResult::err, errcode(), errdetail(), errhint(), 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(), quote_identifier(), 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().

812 {
813  Relation rel;
814  ObjectAddress myself;
815  HeapTuple tup;
816  Oid subid;
817  Datum datum;
818  bool isnull;
819  char *subname;
820  char *conninfo;
821  char *slotname;
822  char originname[NAMEDATALEN];
823  char *err = NULL;
824  RepOriginId originid;
826  StringInfoData cmd;
827 
828  /*
829  * Lock pg_subscription with AccessExclusiveLock to ensure that the
830  * launcher doesn't restart new worker during dropping the subscription
831  */
833 
835  CStringGetDatum(stmt->subname));
836 
837  if (!HeapTupleIsValid(tup))
838  {
839  heap_close(rel, NoLock);
840 
841  if (!stmt->missing_ok)
842  ereport(ERROR,
843  (errcode(ERRCODE_UNDEFINED_OBJECT),
844  errmsg("subscription \"%s\" does not exist",
845  stmt->subname)));
846  else
847  ereport(NOTICE,
848  (errmsg("subscription \"%s\" does not exist, skipping",
849  stmt->subname)));
850 
851  return;
852  }
853 
854  subid = HeapTupleGetOid(tup);
855 
856  /* must be owner */
857  if (!pg_subscription_ownercheck(subid, GetUserId()))
859  stmt->subname);
860 
861  /* DROP hook for the subscription being removed */
863 
864  /*
865  * Lock the subscription so nobody else can do anything with it (including
866  * the replication workers).
867  */
869 
870  /* Get subname */
871  datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
873  Assert(!isnull);
874  subname = pstrdup(NameStr(*DatumGetName(datum)));
875 
876  /* Get conninfo */
877  datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
879  Assert(!isnull);
880  conninfo = TextDatumGetCString(datum);
881 
882  /* Get slotname */
883  datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
885  if (!isnull)
886  slotname = pstrdup(NameStr(*DatumGetName(datum)));
887  else
888  slotname = NULL;
889 
890  /*
891  * Since dropping a replication slot is not transactional, the replication
892  * slot stays dropped even if the transaction rolls back. So we cannot
893  * run DROP SUBSCRIPTION inside a transaction block if dropping the
894  * replication slot.
895  *
896  * XXX The command name should really be something like "DROP SUBSCRIPTION
897  * of a subscription that is associated with a replication slot", but we
898  * don't have the proper facilities for that.
899  */
900  if (slotname)
901  PreventTransactionChain(isTopLevel, "DROP SUBSCRIPTION");
902 
903 
905  EventTriggerSQLDropAddObject(&myself, true, true);
906 
907  /* Remove the tuple from catalog. */
908  CatalogTupleDelete(rel, &tup->t_self);
909 
910  ReleaseSysCache(tup);
911 
912  /* Clean up dependencies */
914 
915  /* Remove any associated relation synchronization states. */
917 
918  /* Kill the apply worker so that the slot becomes accessible. */
920 
921  /* Remove the origin tracking if exists. */
922  snprintf(originname, sizeof(originname), "pg_%u", subid);
923  originid = replorigin_by_name(originname, true);
924  if (originid != InvalidRepOriginId)
925  replorigin_drop(originid);
926 
927  /*
928  * If there is no slot associated with the subscription, we can finish
929  * here.
930  */
931  if (!slotname)
932  {
933  heap_close(rel, NoLock);
934  return;
935  }
936 
937  /*
938  * Otherwise drop the replication slot at the publisher node using the
939  * replication connection.
940  */
941  load_file("libpqwalreceiver", false);
942 
943  initStringInfo(&cmd);
944  appendStringInfo(&cmd, "DROP_REPLICATION_SLOT %s", quote_identifier(slotname));
945 
946  wrconn = walrcv_connect(conninfo, true, subname, &err);
947  if (wrconn == NULL)
948  ereport(ERROR,
949  (errmsg("could not connect to publisher when attempting to "
950  "drop the replication slot \"%s\"", slotname),
951  errdetail("The error was: %s", err),
952  errhint("Use ALTER SUBSCRIPTION ... SET (slot_name = NONE) "
953  "to disassociate the subscription from the slot.")));
954 
955  PG_TRY();
956  {
957  WalRcvExecResult *res;
958 
959  res = walrcv_exec(wrconn, cmd.data, 0, NULL);
960 
961  if (res->status != WALRCV_OK_COMMAND)
962  ereport(ERROR,
963  (errmsg("could not drop the replication slot \"%s\" on publisher",
964  slotname),
965  errdetail("The error was: %s", res->err)));
966  else
967  ereport(NOTICE,
968  (errmsg("dropped replication slot \"%s\" on publisher",
969  slotname)));
970 
971  walrcv_clear_result(res);
972  }
973  PG_CATCH();
974  {
975  /* Close the connection in case of failure */
976  walrcv_disconnect(wrconn);
977  PG_RE_THROW();
978  }
979  PG_END_TRY();
980 
981  walrcv_disconnect(wrconn);
982 
983  pfree(cmd.data);
984 
985  heap_close(rel, NoLock);
986 }
WalReceiverConn * wrconn
Definition: worker.c:110
int errhint(const char *fmt,...)
Definition: elog.c:987
void RemoveSubscriptionRel(Oid subid, Oid relid)
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10419
Oid GetUserId(void)
Definition: miscinit.c:284
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:78
#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:415
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:46
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:1117
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1279
Oid MyDatabaseId
Definition: globals.c:77
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:137
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:45
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:3155
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:158
static List * fetch_table_list ( WalReceiverConn wrconn,
List publications 
)
static

Definition at line 1089 of file subscriptioncmds.c.

References appendStringInfo(), appendStringInfoString(), Assert, StringInfoData::data, ereport, WalRcvExecResult::err, errmsg(), ERROR, ExecClearTuple(), ExecDropSingleTupleTableSlot(), initStringInfo(), lappend(), lfirst, list_length(), makeRangeVar(), MakeSingleTupleTableSlot(), NIL, pfree(), pstrdup(), quote_literal_cstr(), slot_getattr(), WalRcvExecResult::status, strVal, TextDatumGetCString, TEXTOID, WalRcvExecResult::tupledesc, WalRcvExecResult::tuplestore, tuplestore_gettupleslot(), walrcv_clear_result(), walrcv_exec, and WALRCV_OK_TUPLES.

Referenced by AlterSubscription_refresh(), and CreateSubscription().

1090 {
1091  WalRcvExecResult *res;
1092  StringInfoData cmd;
1093  TupleTableSlot *slot;
1094  Oid tableRow[2] = {TEXTOID, TEXTOID};
1095  ListCell *lc;
1096  bool first;
1097  List *tablelist = NIL;
1098 
1099  Assert(list_length(publications) > 0);
1100 
1101  initStringInfo(&cmd);
1102  appendStringInfo(&cmd, "SELECT DISTINCT t.schemaname, t.tablename\n"
1103  " FROM pg_catalog.pg_publication_tables t\n"
1104  " WHERE t.pubname IN (");
1105  first = true;
1106  foreach(lc, publications)
1107  {
1108  char *pubname = strVal(lfirst(lc));
1109 
1110  if (first)
1111  first = false;
1112  else
1113  appendStringInfoString(&cmd, ", ");
1114 
1115  appendStringInfo(&cmd, "%s", quote_literal_cstr(pubname));
1116  }
1117  appendStringInfoString(&cmd, ")");
1118 
1119  res = walrcv_exec(wrconn, cmd.data, 2, tableRow);
1120  pfree(cmd.data);
1121 
1122  if (res->status != WALRCV_OK_TUPLES)
1123  ereport(ERROR,
1124  (errmsg("could not receive list of replicated tables from the publisher: %s",
1125  res->err)));
1126 
1127  /* Process tables. */
1128  slot = MakeSingleTupleTableSlot(res->tupledesc);
1129  while (tuplestore_gettupleslot(res->tuplestore, true, false, slot))
1130  {
1131  char *nspname;
1132  char *relname;
1133  bool isnull;
1134  RangeVar *rv;
1135 
1136  nspname = TextDatumGetCString(slot_getattr(slot, 1, &isnull));
1137  Assert(!isnull);
1138  relname = TextDatumGetCString(slot_getattr(slot, 2, &isnull));
1139  Assert(!isnull);
1140 
1141  rv = makeRangeVar(pstrdup(nspname), pstrdup(relname), -1);
1142  tablelist = lappend(tablelist, rv);
1143 
1144  ExecClearTuple(slot);
1145  }
1147 
1148  walrcv_clear_result(res);
1149 
1150  return tablelist;
1151 }
#define NIL
Definition: pg_list.h:69
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:102
#define TEXTOID
Definition: pg_type.h:324
char * pstrdup(const char *in)
Definition: mcxt.c:1077
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
#define strVal(v)
Definition: value.h:54
unsigned int Oid
Definition: postgres_ext.h:31
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:78
TupleDesc tupledesc
Definition: walreceiver.h:190
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:216
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc)
Definition: execTuples.c:199
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define TextDatumGetCString(d)
Definition: builtins.h:92
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1078
Tuplestorestate * tuplestore
Definition: walreceiver.h:189
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
WalRcvExecStatus status
Definition: walreceiver.h:187
static int list_length(const List *l)
Definition: pg_list.h:89
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: heaptuple.c:1141
Definition: pg_list.h:45
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:419
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:262
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

Definition at line 64 of file subscriptioncmds.c.

References Assert, connect, defGetBoolean(), defGetString(), DefElem::defname, ereport, errcode(), errmsg(), ERROR, GUC_ACTION_SET, lfirst, NULL, PGC_BACKEND, PGC_S_TEST, and set_config_option().

Referenced by AlterSubscription(), and CreateSubscription().

69 {
70  ListCell *lc;
71  bool connect_given = false;
72  bool create_slot_given = false;
73  bool copy_data_given = false;
74  bool refresh_given = false;
75 
76  /* If connect is specified, the others also need to be. */
77  Assert(!connect || (enabled && create_slot && copy_data));
78 
79  if (connect)
80  *connect = true;
81  if (enabled)
82  {
83  *enabled_given = false;
84  *enabled = true;
85  }
86  if (create_slot)
87  *create_slot = true;
88  if (slot_name)
89  {
90  *slot_name_given = false;
91  *slot_name = NULL;
92  }
93  if (copy_data)
94  *copy_data = true;
97  if (refresh)
98  *refresh = true;
99 
100  /* Parse options */
101  foreach(lc, options)
102  {
103  DefElem *defel = (DefElem *) lfirst(lc);
104 
105  if (strcmp(defel->defname, "connect") == 0 && connect)
106  {
107  if (connect_given)
108  ereport(ERROR,
109  (errcode(ERRCODE_SYNTAX_ERROR),
110  errmsg("conflicting or redundant options")));
111 
112  connect_given = true;
113  *connect = defGetBoolean(defel);
114  }
115  else if (strcmp(defel->defname, "enabled") == 0 && enabled)
116  {
117  if (*enabled_given)
118  ereport(ERROR,
119  (errcode(ERRCODE_SYNTAX_ERROR),
120  errmsg("conflicting or redundant options")));
121 
122  *enabled_given = true;
123  *enabled = defGetBoolean(defel);
124  }
125  else if (strcmp(defel->defname, "create_slot") == 0 && create_slot)
126  {
127  if (create_slot_given)
128  ereport(ERROR,
129  (errcode(ERRCODE_SYNTAX_ERROR),
130  errmsg("conflicting or redundant options")));
131 
132  create_slot_given = true;
133  *create_slot = defGetBoolean(defel);
134  }
135  else if (strcmp(defel->defname, "slot_name") == 0 && slot_name)
136  {
137  if (*slot_name_given)
138  ereport(ERROR,
139  (errcode(ERRCODE_SYNTAX_ERROR),
140  errmsg("conflicting or redundant options")));
141 
142  *slot_name_given = true;
143  *slot_name = defGetString(defel);
144 
145  /* Setting slot_name = NONE is treated as no slot name. */
146  if (strcmp(*slot_name, "none") == 0)
147  *slot_name = NULL;
148  }
149  else if (strcmp(defel->defname, "copy_data") == 0 && copy_data)
150  {
151  if (copy_data_given)
152  ereport(ERROR,
153  (errcode(ERRCODE_SYNTAX_ERROR),
154  errmsg("conflicting or redundant options")));
155 
156  copy_data_given = true;
157  *copy_data = defGetBoolean(defel);
158  }
159  else if (strcmp(defel->defname, "synchronous_commit") == 0 &&
161  {
162  if (*synchronous_commit)
163  ereport(ERROR,
164  (errcode(ERRCODE_SYNTAX_ERROR),
165  errmsg("conflicting or redundant options")));
166 
168 
169  /* Test if the given value is valid for synchronous_commit GUC. */
170  (void) set_config_option("synchronous_commit", *synchronous_commit,
172  false, 0, false);
173  }
174  else if (strcmp(defel->defname, "refresh") == 0 && refresh)
175  {
176  if (refresh_given)
177  ereport(ERROR,
178  (errcode(ERRCODE_SYNTAX_ERROR),
179  errmsg("conflicting or redundant options")));
180 
181  refresh_given = true;
182  *refresh = defGetBoolean(defel);
183  }
184  else
185  ereport(ERROR,
186  (errcode(ERRCODE_SYNTAX_ERROR),
187  errmsg("unrecognized subscription parameter: %s", defel->defname)));
188  }
189 
190  /*
191  * We've been explicitly asked to not connect, that requires some
192  * additional processing.
193  */
194  if (connect && !*connect)
195  {
196  /* Check for incompatible options from the user. */
197  if (enabled && *enabled_given && *enabled)
198  ereport(ERROR,
199  (errcode(ERRCODE_SYNTAX_ERROR),
200  errmsg("connect = false and enabled = true are mutually exclusive options")));
201 
202  if (create_slot && create_slot_given && *create_slot)
203  ereport(ERROR,
204  (errcode(ERRCODE_SYNTAX_ERROR),
205  errmsg("connect = false and create_slot = true are mutually exclusive options")));
206 
207  if (copy_data && copy_data_given && *copy_data)
208  ereport(ERROR,
209  (errcode(ERRCODE_SYNTAX_ERROR),
210  errmsg("connect = false and copy_data = true are mutually exclusive options")));
211 
212  /* Change the defaults of other options. */
213  *enabled = false;
214  *create_slot = false;
215  *copy_data = false;
216  }
217 
218  /*
219  * Do additional checking for disallowed combination when slot_name = NONE
220  * was used.
221  */
222  if (slot_name && *slot_name_given && !*slot_name)
223  {
224  if (enabled && *enabled_given && *enabled)
225  ereport(ERROR,
226  (errcode(ERRCODE_SYNTAX_ERROR),
227  errmsg("slot_name = NONE and enabled = true are mutually exclusive options")));
228 
229  if (create_slot && create_slot_given && *create_slot)
230  ereport(ERROR,
231  (errcode(ERRCODE_SYNTAX_ERROR),
232  errmsg("slot_name = NONE and create_slot = true are mutually exclusive options")));
233 
234  if (enabled && !*enabled_given && *enabled)
235  ereport(ERROR,
236  (errcode(ERRCODE_SYNTAX_ERROR),
237  errmsg("subscription with slot_name = NONE must also set enabled = false")));
238 
239  if (create_slot && !create_slot_given && *create_slot)
240  ereport(ERROR,
241  (errcode(ERRCODE_SYNTAX_ERROR),
242  errmsg("subscription with slot_name = NONE must also set create_slot = false")));
243  }
244 }
#define connect(s, name, namelen)
Definition: win32.h:373
int errcode(int sqlerrcode)
Definition: elog.c:575
bool defGetBoolean(DefElem *def)
Definition: define.c:111
#define ERROR
Definition: elog.h:43
char * defGetString(DefElem *def)
Definition: define.c:49
int synchronous_commit
Definition: xact.c:82
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:719
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:5908
static Datum publicationListToArray ( List publist)
static

Definition at line 250 of file subscriptioncmds.c.

References ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE, ALLOCSET_DEFAULT_MINSIZE, AllocSetContextCreate(), construct_array(), CStringGetTextDatum, CurrentMemoryContext, ereport, errcode(), errmsg(), ERROR, lfirst, list_length(), MemoryContextDelete(), MemoryContextSwitchTo(), name, palloc(), PointerGetDatum, strVal, and TEXTOID.

Referenced by AlterSubscription(), and CreateSubscription().

251 {
252  ArrayType *arr;
253  Datum *datums;
254  int j = 0;
255  ListCell *cell;
256  MemoryContext memcxt;
257  MemoryContext oldcxt;
258 
259  /* Create memory context for temporary allocations. */
261  "publicationListToArray to array",
265  oldcxt = MemoryContextSwitchTo(memcxt);
266 
267  datums = palloc(sizeof(text *) * list_length(publist));
268  foreach(cell, publist)
269  {
270  char *name = strVal(lfirst(cell));
271  ListCell *pcell;
272 
273  /* Check for duplicates. */
274  foreach(pcell, publist)
275  {
276  char *pname = strVal(lfirst(pcell));
277 
278  if (name == pname)
279  break;
280 
281  if (strcmp(name, pname) == 0)
282  ereport(ERROR,
283  (errcode(ERRCODE_SYNTAX_ERROR),
284  errmsg("publication name \"%s\" used more than once",
285  pname)));
286  }
287 
288  datums[j++] = CStringGetTextDatum(name);
289  }
290 
291  MemoryContextSwitchTo(oldcxt);
292 
293  arr = construct_array(datums, list_length(publist),
294  TEXTOID, -1, false, 'i');
295  MemoryContextDelete(memcxt);
296 
297  return PointerGetDatum(arr);
298 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
#define TEXTOID
Definition: pg_type.h:324
#define PointerGetDatum(X)
Definition: postgres.h:562
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ALLOCSET_DEFAULT_MINSIZE
Definition: memutils.h:162
#define ERROR
Definition: elog.h:43
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
uintptr_t Datum
Definition: postgres.h:372
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
const char * name
Definition: encode.c:521
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ALLOCSET_DEFAULT_INITSIZE
Definition: memutils.h:163
#define CStringGetTextDatum(s)
Definition: builtins.h:91
Definition: c.h:439
#define ALLOCSET_DEFAULT_MAXSIZE
Definition: memutils.h:164