pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pg_createsubscriber: allow duplicate subscription names
8+ messages / 4 participants
[nested] [flat]

* pg_createsubscriber: allow duplicate subscription names
@ 2026-08-05 07:29 Chao Li <li.evan.chao@gmail.com>
  2026-08-06 07:09 ` RE: pg_createsubscriber: allow duplicate subscription names Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Chao Li @ 2026-08-05 07:29 UTC (permalink / raw)
  To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; +Cc: Amit Kapila <amit.kapila16@gmail.com>; Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>

Hi,

This is follow-up work to patch [1], which fixed a bug in PG19 by allowing pg_createsubscriber to accept duplicate publication names, since publication names are unique per database.

In the review thread, Amit suggested allowing duplicate subscription names as well, because subscription names are also unique per database. However, that required more work because when slot names are not specified, subscription names are used as slot names, and slot names must be unique per cluster. For that reason, the change was deferred to v20.

Here it is. The code changes are actually small. When slot names are specified, we can allow duplicate subscription names. Doc and test changes are included. See the attached patch for details.

[1] https://www.postgresql.org/message-id/B08A7C89-B3DE-4C1D-A671-32AD8BAB7E22%40gmail.com

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

v1-0001-pg_createsubscriber-Allow-duplicate-subscription-.patch (application/octet-stream, 5.4K) download From cf6c42de2558b86bcee54c0b629385b8141f3054 Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" <lic@highgo.com> Date: Wed, 5 Aug 2026 15:11:13 +0800 Subject: [PATCH v1] pg_createsubscriber: Allow duplicate subscription names Subscription names are database-local, so the same name can be used in different databases. However, pg_createsubscriber rejected duplicate --subscription values unconditionally. Allow duplicate subscription names when distinct replication slot names are specified explicitly. Continue to reject them when --replication-slot is omitted, because subscription names are then reused as replication slot names, which must be unique within a cluster. Add tests and update the documentation. Suggested-by: Amit Kapila <amit.kapila16@gmail.com> Author: Chao Li <lic@highgo.com> Reviewed-by: Discussion: https://postgr.es/m/ --- doc/src/sgml/ref/pg_createsubscriber.sgml | 4 +++- src/bin/pg_basebackup/pg_createsubscriber.c | 19 +++++++++------ .../t/040_pg_createsubscriber.pl | 24 +++++++++++++++---- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/doc/src/sgml/ref/pg_createsubscriber.sgml b/doc/src/sgml/ref/pg_createsubscriber.sgml index 193f60628bc..60d7c8e2a2d 100644 --- a/doc/src/sgml/ref/pg_createsubscriber.sgml +++ b/doc/src/sgml/ref/pg_createsubscriber.sgml @@ -362,7 +362,9 @@ PostgreSQL documentation is reported. The order of the multiple subscription name switches must match the order of database switches. If this option is not specified, a generated name is assigned to the subscription name. This option cannot - be used together with <option>--all</option>. + be used together with <option>--all</option>. The same subscription name + can be used in different databases only when distinct replication slot + names are specified with <option>--replication-slot</option>. </para> </listitem> </varlistentry> diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c index 20b354aed56..dcf8422dd92 100644 --- a/src/bin/pg_basebackup/pg_createsubscriber.c +++ b/src/bin/pg_basebackup/pg_createsubscriber.c @@ -2262,6 +2262,7 @@ main(int argc, char **argv) int c; int option_index; + bool duplicate_sub_name = false; char *pub_base_conninfo; char *sub_base_conninfo; @@ -2394,13 +2395,10 @@ main(int argc, char **argv) pg_fatal("replication slot \"%s\" specified more than once for --replication-slot", optarg); break; case 4: - if (!simple_string_list_member(&opt.sub_names, optarg)) - { - simple_string_list_append(&opt.sub_names, optarg); - num_subs++; - } - else - pg_fatal("subscription \"%s\" specified more than once for --subscription", optarg); + if (simple_string_list_member(&opt.sub_names, optarg)) + duplicate_sub_name = true; + simple_string_list_append(&opt.sub_names, optarg); + num_subs++; break; case 5: if (!simple_string_list_member(&opt.objecttypes_to_clean, optarg)) @@ -2587,6 +2585,13 @@ main(int argc, char **argv) num_replslots, num_dbs); exit(1); } + if (duplicate_sub_name && num_replslots == 0) + { + pg_log_error("duplicate subscription names require distinct replication slot names"); + pg_log_error_detail("When replication slot names are not specified, subscription names are used, but replication slot names must be unique within a cluster."); + pg_log_error_hint("Specify a unique --replication-slot name for each database."); + exit(1); + } /* Verify the object types specified for removal from the subscriber */ for (SimpleStringListCell *cell = opt.objecttypes_to_clean.head; cell; cell = cell->next) diff --git a/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl b/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl index 9252d1c3c5c..82fb4394bea 100644 --- a/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl +++ b/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl @@ -91,6 +91,19 @@ command_fails( '--database' => 'pg2', ], 'wrong number of subscription names'); +command_fails_like( + [ + 'pg_createsubscriber', + '--verbose', + '--pgdata' => $datadir, + '--publisher-server' => 'port=5432', + '--subscription' => 'bar1', + '--subscription' => 'bar1', + '--database' => 'pg1', + '--database' => 'pg2', + ], + qr/duplicate subscription names require distinct replication slot names/, + 'duplicate subscription names without replication slot names'); command_fails( [ 'pg_createsubscriber', @@ -334,8 +347,9 @@ is($node_s->safe_psql($db1, "SELECT COUNT(*) FROM pg_publication"), $node_s->stop; -# dry run mode on node S. Use the same publication name for different -# databases, since publication names are database-local. +# dry run mode on node S. Use the same publication and subscription names for +# different databases, since both names are database-local. Distinct replication +# slot names are required because replication slots are cluster-global. command_ok( [ 'pg_createsubscriber', @@ -348,8 +362,10 @@ command_ok( '--subscriber-port' => $node_s->port, '--publication' => 'same_pub', '--publication' => 'same_pub', - '--subscription' => 'sub1', - '--subscription' => 'sub2', + '--subscription' => 'same_sub', + '--subscription' => 'same_sub', + '--replication-slot' => 'slot1', + '--replication-slot' => 'slot2', '--database' => $db1, '--database' => $db2, '--logdir' => $logdir, -- 2.50.1 (Apple Git-155) =
^ permalink raw reply [nested|flat] 8+ messages in thread

* RE: pg_createsubscriber: allow duplicate subscription names
  2026-08-05 07:29 pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
@ 2026-08-06 07:09 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
  2026-08-06 08:36   ` Re: pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Hayato Kuroda (Fujitsu) @ 2026-08-06 07:09 UTC (permalink / raw)
  To: Chao Li <li.evan.chao@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; +Cc: Amit Kapila <amit.kapila16@gmail.com>

Dear Chao,

Thanks for working on it. I checked your patch and briefly looks good.

```
+	if (duplicate_sub_name && num_replslots == 0)
+	{
+		pg_log_error("duplicate subscription names require distinct replication slot names");
+		pg_log_error_detail("When replication slot names are not specified, subscription names are used, but replication slot names must be unique within a cluster.");
+		pg_log_error_hint("Specify a unique --replication-slot name for each database.");
+		exit(1);
+	}
```

Not sure pg_log_error_detail() is helpful here. Can we remove?
Also I feel we do not have to clarify "distinct/unique" for replication slot names,
it has already been described other lines.

Best regards,
Hayato Kuroda
FUJITSU LIMITED







^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: pg_createsubscriber: allow duplicate subscription names
  2026-08-05 07:29 pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  2026-08-06 07:09 ` RE: pg_createsubscriber: allow duplicate subscription names Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-08-06 08:36   ` Chao Li <li.evan.chao@gmail.com>
  2026-08-07 00:24     ` Re: pg_createsubscriber: allow duplicate subscription names Peter Smith <smithpb2250@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Chao Li @ 2026-08-06 08:36 UTC (permalink / raw)
  To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Amit Kapila <amit.kapila16@gmail.com>



> On Aug 6, 2026, at 15:09, Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com> wrote:
> 
> Dear Chao,
> 
> Thanks for working on it. I checked your patch and briefly looks good.

Thank you very much for reviewing the patch.

> 
> ```
> + if (duplicate_sub_name && num_replslots == 0)
> + {
> + pg_log_error("duplicate subscription names require distinct replication slot names");
> + pg_log_error_detail("When replication slot names are not specified, subscription names are used, but replication slot names must be unique within a cluster.");
> + pg_log_error_hint("Specify a unique --replication-slot name for each database.");
> + exit(1);
> + }
> ```
> 
> Not sure pg_log_error_detail() is helpful here. Can we remove?

I thought to provide more info, maybe too verbose.

> Also I feel we do not have to clarify "distinct/unique" for replication slot names,
> it has already been described other lines.

Okay, removed.

PFA v2 - addressed Hayato-san’s comments.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

v2-0001-pg_createsubscriber-Allow-duplicate-subscription-.patch (application/octet-stream, 5.3K) download From b15a4f47e1c9da3a5e9d3db244b975a40d3583d9 Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" <lic@highgo.com> Date: Wed, 5 Aug 2026 15:11:13 +0800 Subject: [PATCH v2] pg_createsubscriber: Allow duplicate subscription names Subscription names are database-local, so the same name can be used in different databases. However, pg_createsubscriber rejected duplicate --subscription values unconditionally. Allow duplicate subscription names when distinct replication slot names are specified explicitly. Continue to reject them when --replication-slot is omitted, because subscription names are then reused as replication slot names, which must be unique within a cluster. Add tests and update the documentation. Suggested-by: Amit Kapila <amit.kapila16@gmail.com> Author: Chao Li <lic@highgo.com> Reviewed-by: Hayato Kuroda <kuroda.hayato@fujitsu.com> Discussion: https://postgr.es/m/2B037930-9B7F-41D3-98B3-E50FFD94C01A@gmail.com --- doc/src/sgml/ref/pg_createsubscriber.sgml | 4 +++- src/bin/pg_basebackup/pg_createsubscriber.c | 18 ++++++++------ .../t/040_pg_createsubscriber.pl | 24 +++++++++++++++---- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/doc/src/sgml/ref/pg_createsubscriber.sgml b/doc/src/sgml/ref/pg_createsubscriber.sgml index 193f60628bc..323d8e1db19 100644 --- a/doc/src/sgml/ref/pg_createsubscriber.sgml +++ b/doc/src/sgml/ref/pg_createsubscriber.sgml @@ -362,7 +362,9 @@ PostgreSQL documentation is reported. The order of the multiple subscription name switches must match the order of database switches. If this option is not specified, a generated name is assigned to the subscription name. This option cannot - be used together with <option>--all</option>. + be used together with <option>--all</option>. The same subscription name + can be used in different databases only when replication slot names are + specified with <option>--replication-slot</option>. </para> </listitem> </varlistentry> diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c index 20b354aed56..164912ee6f7 100644 --- a/src/bin/pg_basebackup/pg_createsubscriber.c +++ b/src/bin/pg_basebackup/pg_createsubscriber.c @@ -2262,6 +2262,7 @@ main(int argc, char **argv) int c; int option_index; + bool duplicate_sub_name = false; char *pub_base_conninfo; char *sub_base_conninfo; @@ -2394,13 +2395,10 @@ main(int argc, char **argv) pg_fatal("replication slot \"%s\" specified more than once for --replication-slot", optarg); break; case 4: - if (!simple_string_list_member(&opt.sub_names, optarg)) - { - simple_string_list_append(&opt.sub_names, optarg); - num_subs++; - } - else - pg_fatal("subscription \"%s\" specified more than once for --subscription", optarg); + if (simple_string_list_member(&opt.sub_names, optarg)) + duplicate_sub_name = true; + simple_string_list_append(&opt.sub_names, optarg); + num_subs++; break; case 5: if (!simple_string_list_member(&opt.objecttypes_to_clean, optarg)) @@ -2587,6 +2585,12 @@ main(int argc, char **argv) num_replslots, num_dbs); exit(1); } + if (duplicate_sub_name && num_replslots == 0) + { + pg_log_error("duplicate subscription names require replication slot names"); + pg_log_error_hint("Specify --replication-slot for each database."); + exit(1); + } /* Verify the object types specified for removal from the subscriber */ for (SimpleStringListCell *cell = opt.objecttypes_to_clean.head; cell; cell = cell->next) diff --git a/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl b/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl index 9252d1c3c5c..7b381637574 100644 --- a/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl +++ b/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl @@ -91,6 +91,19 @@ command_fails( '--database' => 'pg2', ], 'wrong number of subscription names'); +command_fails_like( + [ + 'pg_createsubscriber', + '--verbose', + '--pgdata' => $datadir, + '--publisher-server' => 'port=5432', + '--subscription' => 'bar1', + '--subscription' => 'bar1', + '--database' => 'pg1', + '--database' => 'pg2', + ], + qr/duplicate subscription names require replication slot names/, + 'duplicate subscription names without replication slot names'); command_fails( [ 'pg_createsubscriber', @@ -334,8 +347,9 @@ is($node_s->safe_psql($db1, "SELECT COUNT(*) FROM pg_publication"), $node_s->stop; -# dry run mode on node S. Use the same publication name for different -# databases, since publication names are database-local. +# dry run mode on node S. Use the same publication and subscription names for +# different databases, since both names are database-local. Replication slot +# names are specified because replication slots are cluster-global. command_ok( [ 'pg_createsubscriber', @@ -348,8 +362,10 @@ command_ok( '--subscriber-port' => $node_s->port, '--publication' => 'same_pub', '--publication' => 'same_pub', - '--subscription' => 'sub1', - '--subscription' => 'sub2', + '--subscription' => 'same_sub', + '--subscription' => 'same_sub', + '--replication-slot' => 'slot1', + '--replication-slot' => 'slot2', '--database' => $db1, '--database' => $db2, '--logdir' => $logdir, -- 2.50.1 (Apple Git-155) =
^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: pg_createsubscriber: allow duplicate subscription names
  2026-08-05 07:29 pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  2026-08-06 07:09 ` RE: pg_createsubscriber: allow duplicate subscription names Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
  2026-08-06 08:36   ` Re: pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
@ 2026-08-07 00:24     ` Peter Smith <smithpb2250@gmail.com>
  2026-08-07 02:55       ` Re: pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Peter Smith @ 2026-08-07 00:24 UTC (permalink / raw)
  To: Chao Li <li.evan.chao@gmail.com>; +Cc: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Amit Kapila <amit.kapila16@gmail.com>

Some minor review comments for patch v2. Nothing functional.

======
doc/src/sgml/ref/pg_createsubscriber.sgml

1.
        is reported.  The order of the multiple subscription name switches must
        match the order of database switches.  If this option is not specified,
        a generated name is assigned to the subscription name. This
option cannot
-       be used together with <option>--all</option>.
+       be used together with <option>--all</option>.  The same
subscription name
+       can be used in different databases only when replication slot names are
+       specified with <option>--replication-slot</option>.
       </para>

The new sentence LGTM. But my AI is pointing out that now the "-all"
sentence sits wedged between the parts describing names (order of
names, generated names, same names). It recommends moving the "--all"
sentence like below:

SUGGESTION
The subscription name to set up the logical replication. This option
cannot be used together with --all. Multiple subscriptions can be
specified by writing multiple --subscription switches. The number of
subscription names must match the number of specified databases,
otherwise an error is reported. The order of the multiple subscription
name switches must match the order of database switches. If this
option is not specified, a generated name is assigned to the
subscription name. The same subscription name can be used in
different databases only when replication slot names are specified
with --replication-slot.


======
src/bin/pg_basebackup/pg_createsubscriber.c

2.
  int option_index;
+ bool duplicate_sub_name = false;

Should it be plural?

/duplicate_sub_name/duplicate_sub_names/

~~~

3.
+ if (simple_string_list_member(&opt.sub_names, optarg))
+ duplicate_sub_name = true;

Having found at least one duplicate, you don't really need to keep checking.

SUGGESTION
if (!duplicate_sub_name)
  duplicate_sub_name = simple_string_list_member(&opt.sub_names, optarg);

Anyway, the extra checking is cheap, so feel free to ignore this comment.

~~~

4.
if (num_replslots > 0 && num_replslots != num_dbs)
{
pg_log_error("wrong number of replication slot names specified");
pg_log_error_detail("The number of specified replication slot names
(%d) must match the number of specified database names (%d).",
num_replslots, num_dbs);
exit(1);
}
if (duplicate_sub_name && num_replslots == 0)
{
pg_log_error("duplicate subscription names require replication slot names");
pg_log_error_hint("Specify --replication-slot for each database.");
exit(1);
}

The code LGTM, but would it be tidier to avoid multiple
`num_replslots` checks by combining as a single if/else?

SUGGESTION
if (num_replslots == 0)
{
  if (duplicate_sub_name)
  {
    pg_log_error ...
  }
}
else
{
  if (num_replslots != num_dbs)
  {
    pg_log_error ...
  }
}

======
Kind Regards,
Peter Smith.
Fujitsu Australia






^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: pg_createsubscriber: allow duplicate subscription names
  2026-08-05 07:29 pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  2026-08-06 07:09 ` RE: pg_createsubscriber: allow duplicate subscription names Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
  2026-08-06 08:36   ` Re: pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  2026-08-07 00:24     ` Re: pg_createsubscriber: allow duplicate subscription names Peter Smith <smithpb2250@gmail.com>
@ 2026-08-07 02:55       ` Chao Li <li.evan.chao@gmail.com>
  2026-08-07 07:03         ` Re: pg_createsubscriber: allow duplicate subscription names Peter Smith <smithpb2250@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Chao Li @ 2026-08-07 02:55 UTC (permalink / raw)
  To: Peter Smith <smithpb2250@gmail.com>; +Cc: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Amit Kapila <amit.kapila16@gmail.com>



> On Aug 7, 2026, at 08:24, Peter Smith <smithpb2250@gmail.com> wrote:
> 
> Some minor review comments for patch v2. Nothing functional.

Thank you very much for reviewing.

> 
> ======
> doc/src/sgml/ref/pg_createsubscriber.sgml
> 
> 1.
>        is reported.  The order of the multiple subscription name switches must
>        match the order of database switches.  If this option is not specified,
>        a generated name is assigned to the subscription name. This
> option cannot
> -       be used together with <option>--all</option>.
> +       be used together with <option>--all</option>.  The same
> subscription name
> +       can be used in different databases only when replication slot names are
> +       specified with <option>--replication-slot</option>.
>       </para>
> 
> The new sentence LGTM. But my AI is pointing out that now the "-all"
> sentence sits wedged between the parts describing names (order of
> names, generated names, same names). It recommends moving the "--all"
> sentence like below:
> 
> SUGGESTION
> The subscription name to set up the logical replication. This option
> cannot be used together with --all. Multiple subscriptions can be
> specified by writing multiple --subscription switches. The number of
> subscription names must match the number of specified databases,
> otherwise an error is reported. The order of the multiple subscription
> name switches must match the order of database switches. If this
> option is not specified, a generated name is assigned to the
> subscription name. The same subscription name can be used in
> different databases only when replication slot names are specified
> with --replication-slot.
> 

Accepted.

> 
> ======
> src/bin/pg_basebackup/pg_createsubscriber.c
> 
> 2.
>  int option_index;
> + bool duplicate_sub_name = false;
> 
> Should it be plural?
> 
> /duplicate_sub_name/duplicate_sub_names/
> 

Accepted.

> ~~~
> 
> 3.
> + if (simple_string_list_member(&opt.sub_names, optarg))
> + duplicate_sub_name = true;
> 
> Having found at least one duplicate, you don't really need to keep checking.
> 
> SUGGESTION
> if (!duplicate_sub_name)
>  duplicate_sub_name = simple_string_list_member(&opt.sub_names, optarg);
> 
> Anyway, the extra checking is cheap, so feel free to ignore this comment.
> 

Okay, why not.

> ~~~
> 
> 4.
> if (num_replslots > 0 && num_replslots != num_dbs)
> {
> pg_log_error("wrong number of replication slot names specified");
> pg_log_error_detail("The number of specified replication slot names
> (%d) must match the number of specified database names (%d).",
> num_replslots, num_dbs);
> exit(1);
> }
> if (duplicate_sub_name && num_replslots == 0)
> {
> pg_log_error("duplicate subscription names require replication slot names");
> pg_log_error_hint("Specify --replication-slot for each database.");
> exit(1);
> }
> 
> The code LGTM, but would it be tidier to avoid multiple
> `num_replslots` checks by combining as a single if/else?
> 
> SUGGESTION
> if (num_replslots == 0)
> {
>  if (duplicate_sub_name)
>  {
>    pg_log_error ...
>  }
> }
> else
> {
>  if (num_replslots != num_dbs)
>  {
>    pg_log_error ...
>  }
> }
> 

Accepted.

PFA v3: addressed Peter’s comments.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

v3-0001-pg_createsubscriber-Allow-duplicate-subscription-.patch (application/octet-stream, 6.1K) download From 52524a8e63a34c1aebbbdd598f9322409946151f Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" <lic@highgo.com> Date: Wed, 5 Aug 2026 15:11:13 +0800 Subject: [PATCH v3] pg_createsubscriber: Allow duplicate subscription names Subscription names are database-local, so the same name can be used in different databases. However, pg_createsubscriber rejected duplicate --subscription values unconditionally. Allow duplicate subscription names when distinct replication slot names are specified explicitly. Continue to reject them when --replication-slot is omitted, because subscription names are then reused as replication slot names, which must be unique within a cluster. Add tests and update the documentation. Suggested-by: Amit Kapila <amit.kapila16@gmail.com> Author: Chao Li <lic@highgo.com> Reviewed-by: Hayato Kuroda <kuroda.hayato@fujitsu.com> Reviewed-by: Peter Smith <smithpb2250@gmail.com> Discussion: https://postgr.es/m/2B037930-9B7F-41D3-98B3-E50FFD94C01A@gmail.com --- doc/src/sgml/ref/pg_createsubscriber.sgml | 9 ++++--- src/bin/pg_basebackup/pg_createsubscriber.c | 23 +++++++++++------- .../t/040_pg_createsubscriber.pl | 24 +++++++++++++++---- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/doc/src/sgml/ref/pg_createsubscriber.sgml b/doc/src/sgml/ref/pg_createsubscriber.sgml index 193f60628bc..78a2596f72e 100644 --- a/doc/src/sgml/ref/pg_createsubscriber.sgml +++ b/doc/src/sgml/ref/pg_createsubscriber.sgml @@ -355,14 +355,17 @@ PostgreSQL documentation <term><option>--subscription=<replaceable class="parameter">name</replaceable></option></term> <listitem> <para> - The subscription name to set up the logical replication. Multiple + The subscription name to set up the logical replication. This option + cannot be used together with <option>--all</option>. Multiple subscriptions can be specified by writing multiple <option>--subscription</option> switches. The number of subscription names must match the number of specified databases, otherwise an error is reported. The order of the multiple subscription name switches must match the order of database switches. If this option is not specified, - a generated name is assigned to the subscription name. This option cannot - be used together with <option>--all</option>. + a generated name is assigned to the subscription name. The same + subscription name can be used in different databases only when + replication slot names are specified with + <option>--replication-slot</option>. </para> </listitem> </varlistentry> diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c index 20b354aed56..5d834edbe72 100644 --- a/src/bin/pg_basebackup/pg_createsubscriber.c +++ b/src/bin/pg_basebackup/pg_createsubscriber.c @@ -2262,6 +2262,7 @@ main(int argc, char **argv) int c; int option_index; + bool duplicate_sub_names = false; char *pub_base_conninfo; char *sub_base_conninfo; @@ -2394,13 +2395,10 @@ main(int argc, char **argv) pg_fatal("replication slot \"%s\" specified more than once for --replication-slot", optarg); break; case 4: - if (!simple_string_list_member(&opt.sub_names, optarg)) - { - simple_string_list_append(&opt.sub_names, optarg); - num_subs++; - } - else - pg_fatal("subscription \"%s\" specified more than once for --subscription", optarg); + if (!duplicate_sub_names) + duplicate_sub_names = simple_string_list_member(&opt.sub_names, optarg); + simple_string_list_append(&opt.sub_names, optarg); + num_subs++; break; case 5: if (!simple_string_list_member(&opt.objecttypes_to_clean, optarg)) @@ -2580,7 +2578,16 @@ main(int argc, char **argv) num_subs, num_dbs); exit(1); } - if (num_replslots > 0 && num_replslots != num_dbs) + if (num_replslots == 0) + { + if (duplicate_sub_names) + { + pg_log_error("duplicate subscription names require replication slot names"); + pg_log_error_hint("Specify --replication-slot for each database."); + exit(1); + } + } + else if (num_replslots != num_dbs) { pg_log_error("wrong number of replication slot names specified"); pg_log_error_detail("The number of specified replication slot names (%d) must match the number of specified database names (%d).", diff --git a/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl b/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl index 9252d1c3c5c..7b381637574 100644 --- a/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl +++ b/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl @@ -91,6 +91,19 @@ command_fails( '--database' => 'pg2', ], 'wrong number of subscription names'); +command_fails_like( + [ + 'pg_createsubscriber', + '--verbose', + '--pgdata' => $datadir, + '--publisher-server' => 'port=5432', + '--subscription' => 'bar1', + '--subscription' => 'bar1', + '--database' => 'pg1', + '--database' => 'pg2', + ], + qr/duplicate subscription names require replication slot names/, + 'duplicate subscription names without replication slot names'); command_fails( [ 'pg_createsubscriber', @@ -334,8 +347,9 @@ is($node_s->safe_psql($db1, "SELECT COUNT(*) FROM pg_publication"), $node_s->stop; -# dry run mode on node S. Use the same publication name for different -# databases, since publication names are database-local. +# dry run mode on node S. Use the same publication and subscription names for +# different databases, since both names are database-local. Replication slot +# names are specified because replication slots are cluster-global. command_ok( [ 'pg_createsubscriber', @@ -348,8 +362,10 @@ command_ok( '--subscriber-port' => $node_s->port, '--publication' => 'same_pub', '--publication' => 'same_pub', - '--subscription' => 'sub1', - '--subscription' => 'sub2', + '--subscription' => 'same_sub', + '--subscription' => 'same_sub', + '--replication-slot' => 'slot1', + '--replication-slot' => 'slot2', '--database' => $db1, '--database' => $db2, '--logdir' => $logdir, -- 2.50.1 (Apple Git-155) =
^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: pg_createsubscriber: allow duplicate subscription names
  2026-08-05 07:29 pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  2026-08-06 07:09 ` RE: pg_createsubscriber: allow duplicate subscription names Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
  2026-08-06 08:36   ` Re: pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  2026-08-07 00:24     ` Re: pg_createsubscriber: allow duplicate subscription names Peter Smith <smithpb2250@gmail.com>
  2026-08-07 02:55       ` Re: pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
@ 2026-08-07 07:03         ` Peter Smith <smithpb2250@gmail.com>
  2026-09-05 03:24           ` Re: pg_createsubscriber: allow duplicate subscription names shihao zhong <zhong950419@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Peter Smith @ 2026-08-07 07:03 UTC (permalink / raw)
  To: Chao Li <li.evan.chao@gmail.com>; +Cc: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Amit Kapila <amit.kapila16@gmail.com>

Patch v3 LGTM

======
Kind Regards,
Peter Smith.
Fujitsu Australia





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: pg_createsubscriber: allow duplicate subscription names
  2026-08-05 07:29 pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  2026-08-06 07:09 ` RE: pg_createsubscriber: allow duplicate subscription names Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
  2026-08-06 08:36   ` Re: pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  2026-08-07 00:24     ` Re: pg_createsubscriber: allow duplicate subscription names Peter Smith <smithpb2250@gmail.com>
  2026-08-07 02:55       ` Re: pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  2026-08-07 07:03         ` Re: pg_createsubscriber: allow duplicate subscription names Peter Smith <smithpb2250@gmail.com>
@ 2026-09-05 03:24           ` shihao zhong <zhong950419@gmail.com>
  2026-09-07 03:34             ` Re: pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: shihao zhong @ 2026-09-05 03:24 UTC (permalink / raw)
  To: Peter Smith <smithpb2250@gmail.com>; +Cc: Chao Li <li.evan.chao@gmail.com>; Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Amit Kapila <amit.kapila16@gmail.com>

Hi Chao,

I reviewed v3 and also ran a real (non --dry-run) pg_createsubscriber
with the same --subscription name for two databases plus distinct
--replication-slot names. Both subscriptions were created in their
respective databases, the slots were created on the publisher, and
subsequent inserts replicated correctly. 040_pg_createsubscriber
passes as well.

v3 LGTM and -> Ready for commiter.

Thanks,
Shihao

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: pg_createsubscriber: allow duplicate subscription names
  2026-08-05 07:29 pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  2026-08-06 07:09 ` RE: pg_createsubscriber: allow duplicate subscription names Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
  2026-08-06 08:36   ` Re: pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  2026-08-07 00:24     ` Re: pg_createsubscriber: allow duplicate subscription names Peter Smith <smithpb2250@gmail.com>
  2026-08-07 02:55       ` Re: pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
  2026-08-07 07:03         ` Re: pg_createsubscriber: allow duplicate subscription names Peter Smith <smithpb2250@gmail.com>
  2026-09-05 03:24           ` Re: pg_createsubscriber: allow duplicate subscription names shihao zhong <zhong950419@gmail.com>
@ 2026-09-07 03:34             ` Chao Li <li.evan.chao@gmail.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Chao Li @ 2026-09-07 03:34 UTC (permalink / raw)
  To: shihao zhong <zhong950419@gmail.com>; +Cc: Peter Smith <smithpb2250@gmail.com>; Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Amit Kapila <amit.kapila16@gmail.com>



> On Sep 5, 2026, at 11:24, shihao zhong <zhong950419@gmail.com> wrote:
> 
> Hi Chao,
> 
> I reviewed v3 and also ran a real (non --dry-run) pg_createsubscriber
> with the same --subscription name for two databases plus distinct
> --replication-slot names. Both subscriptions were created in their
> respective databases, the slots were created on the publisher, and
> subsequent inserts replicated correctly. 040_pg_createsubscriber
> passes as well.
> 
> v3 LGTM and -> Ready for commiter.
> 
> Thanks,
> Shihao

Thanks a lot for the review.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/










^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2026-09-07 03:34 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-05 07:29 pg_createsubscriber: allow duplicate subscription names Chao Li <li.evan.chao@gmail.com>
2026-08-06 07:09 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-08-06 08:36   ` Chao Li <li.evan.chao@gmail.com>
2026-08-07 00:24     ` Peter Smith <smithpb2250@gmail.com>
2026-08-07 02:55       ` Chao Li <li.evan.chao@gmail.com>
2026-08-07 07:03         ` Peter Smith <smithpb2250@gmail.com>
2026-09-05 03:24           ` shihao zhong <zhong950419@gmail.com>
2026-09-07 03:34             ` Chao Li <li.evan.chao@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox