From e0e55ae772ad6ab7d87d5941be4699793cfb55a3 Mon Sep 17 00:00:00 2001 From: Baji Shaik Date: Fri, 4 Dec 2026 12:00:00 -0600 Subject: [PATCH v2] Validate the default slot name in CREATE SUBSCRIPTION. When slot_name is not given, CREATE SUBSCRIPTION uses the subscription name as the slot name without validating it, while an explicit slot_name is validated. With connect = false, a subscription such as "MySub" could be created whose slot can never be created on the publisher, and pg_dump writes that slot name explicitly, so the dump could not be restored. The same way, a subscription named pg_conflict_detection could use the reserved slot name, and one named "none" got a slot name that restores as slot_name = NONE. Validate the default slot name the same way as an explicit one, and reject "none". Author: Baji Shaik Reviewed-by: Masahiko Sawada Reviewed-by: Zsolt Parragi --- doc/src/sgml/ref/create_subscription.sgml | 4 +++- src/backend/commands/subscriptioncmds.c | 25 ++++++++++++++++++++++ src/test/regress/expected/subscription.out | 16 ++++++++++++++ src/test/regress/sql/subscription.sql | 8 +++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/ref/create_subscription.sgml b/doc/src/sgml/ref/create_subscription.sgml index 47c6b42b4c1..ad83c6b5e8b 100644 --- a/doc/src/sgml/ref/create_subscription.sgml +++ b/doc/src/sgml/ref/create_subscription.sgml @@ -183,7 +183,9 @@ CREATE SUBSCRIPTION subscription_name Name of the publisher's replication slot to use. The default is - to use the name of the subscription for the slot name. The name cannot + to use the name of the subscription for the slot name, so the + subscription name must then be a valid replication slot name and + cannot be none. The name cannot be pg_conflict_detection as it is reserved for the conflict detection. diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index abea2ecd276..145d29a1f1a 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -786,7 +786,32 @@ CreateSubscription(ParseState *pstate, CreateSubscriptionStmt *stmt, if (!IsSet(opts.specified_opts, SUBOPT_SLOT_NAME) && opts.slot_name == NULL) + { + int err_code; + char *err_msg = NULL; + char *err_hint = NULL; + + /* The default slot name must be valid, like an explicit one */ + if (!ReplicationSlotValidateNameInternal(stmt->subname, false, + &err_code, &err_msg, + &err_hint)) + ereport(ERROR, + errcode(err_code), + errmsg_internal("%s", err_msg), + errdetail("The subscription name is used as the replication slot name when slot_name is not specified."), + (err_hint != NULL) ? errhint_internal("%s", err_hint) : 0); + + /* slot_name = 'none' means no slot, so it can't be a slot's name */ + if (strcmp(stmt->subname, "none") == 0) + ereport(ERROR, + errcode(ERRCODE_INVALID_NAME), + errmsg("replication slot name \"%s\" cannot be used", + stmt->subname), + errdetail("The subscription name is used as the replication slot name when slot_name is not specified."), + errhint("Specify a different slot_name for the subscription.")); + opts.slot_name = stmt->subname; + } /* The default for synchronous_commit of subscriptions is off. */ if (opts.synchronous_commit == NULL) diff --git a/src/test/regress/expected/subscription.out b/src/test/regress/expected/subscription.out index e09c81b3b2a..89624b2b2b0 100644 --- a/src/test/regress/expected/subscription.out +++ b/src/test/regress/expected/subscription.out @@ -131,6 +131,22 @@ ALTER SUBSCRIPTION regress_testsub3 ENABLE; ERROR: cannot enable subscription that does not have a slot name ALTER SUBSCRIPTION regress_testsub3 REFRESH PUBLICATION; ERROR: ALTER SUBSCRIPTION ... REFRESH PUBLICATION is not allowed for disabled subscriptions +-- fail - the default slot name (the subscription name) must be a valid slot name +CREATE SUBSCRIPTION "regress_TestSub" CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false); +ERROR: replication slot name "regress_TestSub" contains invalid character +DETAIL: The subscription name is used as the replication slot name when slot_name is not specified. +HINT: Replication slot names may only contain lower case letters, numbers, and the underscore character. +-- these names can't start with "regress_", so hide the warning about that +SET client_min_messages = ERROR; +CREATE SUBSCRIPTION pg_conflict_detection CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false); +ERROR: replication slot name "pg_conflict_detection" is reserved +DETAIL: The subscription name is used as the replication slot name when slot_name is not specified. +HINT: The name "pg_conflict_detection" is reserved for the conflict detection slot. +CREATE SUBSCRIPTION none CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false); +ERROR: replication slot name "none" cannot be used +DETAIL: The subscription name is used as the replication slot name when slot_name is not specified. +HINT: Specify a different slot_name for the subscription. +RESET client_min_messages; -- fail - origin must be either none or any CREATE SUBSCRIPTION regress_testsub4 CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (slot_name = NONE, connect = false, origin = foo); ERROR: unrecognized origin value: "foo" diff --git a/src/test/regress/sql/subscription.sql b/src/test/regress/sql/subscription.sql index 4ccce5c56f5..07581d1898a 100644 --- a/src/test/regress/sql/subscription.sql +++ b/src/test/regress/sql/subscription.sql @@ -88,6 +88,14 @@ CREATE SUBSCRIPTION regress_testsub3 CONNECTION 'dbname=regress_doesnotexist' PU ALTER SUBSCRIPTION regress_testsub3 ENABLE; ALTER SUBSCRIPTION regress_testsub3 REFRESH PUBLICATION; +-- fail - the default slot name (the subscription name) must be a valid slot name +CREATE SUBSCRIPTION "regress_TestSub" CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false); +-- these names can't start with "regress_", so hide the warning about that +SET client_min_messages = ERROR; +CREATE SUBSCRIPTION pg_conflict_detection CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false); +CREATE SUBSCRIPTION none CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false); +RESET client_min_messages; + -- fail - origin must be either none or any CREATE SUBSCRIPTION regress_testsub4 CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (slot_name = NONE, connect = false, origin = foo); -- 2.54.0 (Apple Git-157)