pg_createsubscriber: allow duplicate subscription names

Started by Chao Li4 days ago6 messageshackers
Jump to latest
#1Chao Li
li.evan.chao@gmail.com

Hi,

This is follow-up work to patch [1]/messages/by-id/B08A7C89-B3DE-4C1D-A671-32AD8BAB7E22@gmail.com, 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]: /messages/by-id/B08A7C89-B3DE-4C1D-A671-32AD8BAB7E22@gmail.com

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

Attachments:

v1-0001-pg_createsubscriber-Allow-duplicate-subscription-.patchapplication/octet-stream; name=v1-0001-pg_createsubscriber-Allow-duplicate-subscription-.patch; x-unix-mode=0644Download+35-15
#2Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Chao Li (#1)
RE: pg_createsubscriber: allow duplicate subscription names

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

#3Chao Li
li.evan.chao@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#2)
Re: pg_createsubscriber: allow duplicate subscription names

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-.patchapplication/octet-stream; name=v2-0001-pg_createsubscriber-Allow-duplicate-subscription-.patch; x-unix-mode=0644Download+34-15
#4Peter Smith
smithpb2250@gmail.com
In reply to: Chao Li (#3)
Re: pg_createsubscriber: allow duplicate subscription names

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

#5Chao Li
li.evan.chao@gmail.com
In reply to: Peter Smith (#4)
Re: pg_createsubscriber: allow duplicate subscription names

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-.patchapplication/octet-stream; name=v3-0001-pg_createsubscriber-Allow-duplicate-subscription-.patch; x-unix-mode=0644Download+41-18
#6Peter Smith
smithpb2250@gmail.com
In reply to: Chao Li (#5)
Re: pg_createsubscriber: allow duplicate subscription names

Patch v3 LGTM

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