Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

Started by Fujii Masaoabout 2 months ago36 messages
#1Fujii Masao
masao.fujii@gmail.com

Hi,

In logical replication, any GUC settings specified in the CONNECTION clause of
CREATE SUBSCRIPTION are currently ignored. For example:

CREATE SUBSCRIPTION mysub
CONNECTION 'options=''-c wal_sender_timeout=1000'''
PUBLICATION ...

The wal_sender_timeout value here has no effect.

This is inconvenient when different logical replication walsenders need
different settings - e.g., a small wal_sender_timeout for walsender
connecting to a nearby subscriber and a larger one for walsender
connecting to a distant subscriber. Right now, it's not easy for users
to control such per-connection behavior.

The reason of thid limitation is that libpqrcv_connect() always overwrites
the options connection parameter as follows:

keys[++i] = "options";
vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c
extra_float_digits=3";

This wipes out any user-specified GUCs in the CONNECTION string.
Physical replication does not have this problem because it does not overwrite
options, so GUCs in primary_conninfo are honored.

To remove this restriction, how about switching to issuing SET commands for
datestyle, intervalstyle, and extra_float_digits after the connection
is established,
similar to what postgres_fdw does, instead of forcing them into options?
That would allow user-specified GUC settings in CREATE SUBSCRIPTION to
take effect.

This overwrite behavior was introduced in commit f3d4019da5d and chosen mainly
to avoid extra network round trips according to the discussion [1]/messages/by-id/CAFF0-CF=D7pc6st-3A9f1JnOt0qmc+BcBPVzD6fLYisKyAjkGA@mail.gmail.com.
While SET commands would add a round trip, it only happens at
connection startup,
which is infrequent - so the overhead seems negligible.

Thoughts?

Regards,

[1]: /messages/by-id/CAFF0-CF=D7pc6st-3A9f1JnOt0qmc+BcBPVzD6fLYisKyAjkGA@mail.gmail.com

--
Fujii Masao

#2Fujii Masao
masao.fujii@gmail.com
In reply to: Fujii Masao (#1)
1 attachment(s)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Wed, Nov 19, 2025 at 12:59 AM Fujii Masao <masao.fujii@gmail.com> wrote:

Hi,

In logical replication, any GUC settings specified in the CONNECTION clause of
CREATE SUBSCRIPTION are currently ignored. For example:

CREATE SUBSCRIPTION mysub
CONNECTION 'options=''-c wal_sender_timeout=1000'''
PUBLICATION ...

The wal_sender_timeout value here has no effect.

This is inconvenient when different logical replication walsenders need
different settings - e.g., a small wal_sender_timeout for walsender
connecting to a nearby subscriber and a larger one for walsender
connecting to a distant subscriber. Right now, it's not easy for users
to control such per-connection behavior.

The reason of thid limitation is that libpqrcv_connect() always overwrites
the options connection parameter as follows:

keys[++i] = "options";
vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c
extra_float_digits=3";

This wipes out any user-specified GUCs in the CONNECTION string.
Physical replication does not have this problem because it does not overwrite
options, so GUCs in primary_conninfo are honored.

To remove this restriction, how about switching to issuing SET commands for
datestyle, intervalstyle, and extra_float_digits after the connection
is established,
similar to what postgres_fdw does, instead of forcing them into options?
That would allow user-specified GUC settings in CREATE SUBSCRIPTION to
take effect.

This overwrite behavior was introduced in commit f3d4019da5d and chosen mainly
to avoid extra network round trips according to the discussion [1].
While SET commands would add a round trip, it only happens at
connection startup,
which is infrequent - so the overhead seems negligible.

Thoughts?

Attached is a patch implementing this idea.
I've also added it to the next CommitFest.

Regards,

--
Fujii Masao

Attachments:

v1-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchapplication/octet-stream; name=v1-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchDownload
From 7d458371f8d737357d4d1e6899dd81f916c2d70b Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 19 Nov 2025 08:49:46 +0900
Subject: [PATCH v1] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Previously, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were not used by the publisher's walsender.
For example, in:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

the wal_sender_timeout setting had no effect.
This contrasted with physical replication, where GUCs in primary_conninfo
are applied to the walsender.

This limitation made it difficult to tune logical replication connections
individually, for example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit removes the restriction by changing how logical replication
connections are established so that GUC settings in the CONNECTION string
are properly passed through to and uesd by the walsender. This enables
per-connection configuration for logical replication.
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 43 ++++++++++++++-----
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 239641bfbb6..d880309e2da 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -180,17 +180,6 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 			/* Tell the publisher to translate to our encoding */
 			keys[++i] = "client_encoding";
 			vals[i] = GetDatabaseEncodingName();
-
-			/*
-			 * Force assorted GUC parameters to settings that ensure that the
-			 * publisher will output data values in a form that is unambiguous
-			 * to the subscriber.  (We don't want to modify the subscriber's
-			 * GUC settings, since that might surprise user-defined code
-			 * running in the subscriber, such as triggers.)  This should
-			 * match what pg_dump does.
-			 */
-			keys[++i] = "options";
-			vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
 		}
 		else
 		{
@@ -256,6 +245,38 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 		PQclear(res);
 	}
 
+	/*
+	 * Force assorted GUC parameters to settings that ensure that the
+	 * publisher will output data values in a form that is unambiguous to the
+	 * subscriber.  (We don't want to modify the subscriber's GUC settings,
+	 * since that might surprise user-defined code running in the subscriber,
+	 * such as triggers.)  This should match what pg_dump does.
+	 */
+	if (replication && logical)
+	{
+		const char *params[] =
+		{"datestyle", "intervalstyle", "extra_float_digits", NULL};
+		const char *values[] = {"ISO", "postgres", "3", NULL};
+
+		for (int i = 0; params[i] != NULL; i++)
+		{
+			char		sql[100];
+			PGresult   *res;
+
+			sprintf(sql, "SET %s = %s", params[i], values[i]);
+			res = libpqsrv_exec(conn->streamConn, sql,
+								WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
+			if (PQresultStatus(res) != PGRES_COMMAND_OK)
+			{
+				PQclear(res);
+				*err = psprintf(_("could not set %s: %s"),
+								params[i], pchomp(PQerrorMessage(conn->streamConn)));
+				goto bad_connection;
+			}
+			PQclear(res);
+		}
+	}
+
 	conn->logical = logical;
 
 	return conn;
-- 
2.51.2

#3Fujii Masao
masao.fujii@gmail.com
In reply to: Fujii Masao (#2)
1 attachment(s)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Wed, Nov 19, 2025 at 3:22 PM Fujii Masao <masao.fujii@gmail.com> wrote:

Attached is a patch implementing this idea.
I've also added it to the next CommitFest.

I've fixed the compiler warning issue.
Attached is an updated version of the patch.

Regards,

--
Fujii Masao

Attachments:

v2-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchapplication/octet-stream; name=v2-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchDownload
From 5fc2ab0592d5516b40266df2c786780d49d20b80 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 19 Nov 2025 08:49:46 +0900
Subject: [PATCH v2] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Previously, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were not used by the publisher's walsender.
For example, in:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

the wal_sender_timeout setting had no effect.
This contrasted with physical replication, where GUCs in primary_conninfo
are applied to the walsender.

This limitation made it difficult to tune logical replication connections
individually, for example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit removes the restriction by changing how logical replication
connections are established so that GUC settings in the CONNECTION string
are properly passed through to and uesd by the walsender. This enables
per-connection configuration for logical replication.
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 43 ++++++++++++++-----
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 239641bfbb6..abc74a9230d 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -180,17 +180,6 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 			/* Tell the publisher to translate to our encoding */
 			keys[++i] = "client_encoding";
 			vals[i] = GetDatabaseEncodingName();
-
-			/*
-			 * Force assorted GUC parameters to settings that ensure that the
-			 * publisher will output data values in a form that is unambiguous
-			 * to the subscriber.  (We don't want to modify the subscriber's
-			 * GUC settings, since that might surprise user-defined code
-			 * running in the subscriber, such as triggers.)  This should
-			 * match what pg_dump does.
-			 */
-			keys[++i] = "options";
-			vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
 		}
 		else
 		{
@@ -256,6 +245,38 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 		PQclear(res);
 	}
 
+	/*
+	 * Force assorted GUC parameters to settings that ensure that the
+	 * publisher will output data values in a form that is unambiguous to the
+	 * subscriber.  (We don't want to modify the subscriber's GUC settings,
+	 * since that might surprise user-defined code running in the subscriber,
+	 * such as triggers.)  This should match what pg_dump does.
+	 */
+	if (replication && logical)
+	{
+		const char *params[] =
+		{"datestyle", "intervalstyle", "extra_float_digits", NULL};
+		const char *values[] = {"ISO", "postgres", "3", NULL};
+
+		for (int j = 0; params[j] != NULL; j++)
+		{
+			char		sql[100];
+			PGresult   *res;
+
+			sprintf(sql, "SET %s = %s", params[j], values[j]);
+			res = libpqsrv_exec(conn->streamConn, sql,
+								WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
+			if (PQresultStatus(res) != PGRES_COMMAND_OK)
+			{
+				PQclear(res);
+				*err = psprintf(_("could not set %s: %s"),
+								params[j], pchomp(PQerrorMessage(conn->streamConn)));
+				goto bad_connection;
+			}
+			PQclear(res);
+		}
+	}
+
 	conn->logical = logical;
 
 	return conn;
-- 
2.51.2

#4Chao Li
li.evan.chao@gmail.com
In reply to: Fujii Masao (#1)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Nov 18, 2025, at 23:59, Fujii Masao <masao.fujii@gmail.com> wrote:

Hi,

In logical replication, any GUC settings specified in the CONNECTION clause of
CREATE SUBSCRIPTION are currently ignored. For example:

CREATE SUBSCRIPTION mysub
CONNECTION 'options=''-c wal_sender_timeout=1000'''
PUBLICATION ...

The wal_sender_timeout value here has no effect.

This is inconvenient when different logical replication walsenders need
different settings - e.g., a small wal_sender_timeout for walsender
connecting to a nearby subscriber and a larger one for walsender
connecting to a distant subscriber. Right now, it's not easy for users
to control such per-connection behavior.

The reason of thid limitation is that libpqrcv_connect() always overwrites
the options connection parameter as follows:

keys[++i] = "options";
vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c
extra_float_digits=3";

This wipes out any user-specified GUCs in the CONNECTION string.
Physical replication does not have this problem because it does not overwrite
options, so GUCs in primary_conninfo are honored.

To remove this restriction, how about switching to issuing SET commands for
datestyle, intervalstyle, and extra_float_digits after the connection
is established,
similar to what postgres_fdw does, instead of forcing them into options?
That would allow user-specified GUC settings in CREATE SUBSCRIPTION to
take effect.

This overwrite behavior was introduced in commit f3d4019da5d and chosen mainly
to avoid extra network round trips according to the discussion [1].
While SET commands would add a round trip, it only happens at
connection startup,
which is infrequent - so the overhead seems negligible.

Thoughts?

Before this patch, all user specified options are silently discarded, now all user specified options expect the 3 will be kept, will that expose a hold where user badly specifies some option that may break logical replication? If that’s true, then we need to parse user specified options and do some verifications.

I just reviewed v2, and got some comments:

1.
```
+ char sql[100];
```

Hardcode 100 here doesn’t look good. If you decide to keep, I won’t have a strong objection.

2
```
+		const char *params[] =
+		{"datestyle", "intervalstyle", "extra_float_digits", NULL};
+		const char *values[] = {"ISO", "postgres", "3", NULL};
```

Nit: we don’t need to have a NULL terminator element. We can use lengthof() macro to get array length. lengthof() is defined in c.h.

3. To minimize the network round-trip, maybe we can combine the 3 set into a single statement?

4. The commit message:
```
This commit removes the restriction by changing how logical replication
connections are established so that GUC settings in the CONNECTION string
are properly passed through to and uesd by the walsender. This enables
```

This is a little bit inaccurate, all user specified settings expected the 3 ones being overwritten will be honored.

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

#5Fujii Masao
masao.fujii@gmail.com
In reply to: Chao Li (#4)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Thu, Nov 20, 2025 at 3:54 PM Chao Li <li.evan.chao@gmail.com> wrote:

Before this patch, all user specified options are silently discarded,

The GUC settings in CREATE SUBSCRIPTION were honored up through v14;
the behavior changed in commit f3d4019da5d, so some might view this
as a regression.

now all user specified options expect the 3 will be kept, will that expose a hold where user badly specifies some option that may break logical replication? If that’s true, then we need to parse user specified options and do some verifications.

Yeah, I agree that if certain GUCs can break logical replication,
we should enforce "safe" values, just as we currently do for datestyle.
And if any other GUCs can cause the issue, they could affect
postgres_fdw etc, so the fix would need to be broader.

I just reviewed v2, and got some comments:

Thanks for the review!

1.
```
+ char sql[100];
```

Hardcode 100 here doesn’t look good. If you decide to keep, I won’t have a strong objection.

I think hardcoding 100 here is sufficient, since the queries built on
that buffer are fixed and clearly fit within that limit.

2
```
+               const char *params[] =
+               {"datestyle", "intervalstyle", "extra_float_digits", NULL};
+               const char *values[] = {"ISO", "postgres", "3", NULL};
```

Nit: we don’t need to have a NULL terminator element. We can use lengthof() macro to get array length. lengthof() is defined in c.h.

Okay, I'll adjust the patch accordingly.

3. To minimize the network round-trip, maybe we can combine the 3 set into a single statement?

As for the extra network round trip, I still doubt it will matter
in practice given that it happens only at replication connection startup.

4. The commit message:
```
This commit removes the restriction by changing how logical replication
connections are established so that GUC settings in the CONNECTION string
are properly passed through to and uesd by the walsender. This enables
```

This is a little bit inaccurate, all user specified settings expected the 3 ones being overwritten will be honored.

Are you suggesting that, because datestyle and the other two parameters
specified in CONNECTION aren(t actually applied by the walsender,
the commit message should explicitly mention that not all parameters
from CONNECTION are used?

Regards,

--
Fujii Masao

#6Chao Li
li.evan.chao@gmail.com
In reply to: Fujii Masao (#5)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Nov 21, 2025, at 15:47, Fujii Masao <masao.fujii@gmail.com> wrote:

4. The commit message:
```
This commit removes the restriction by changing how logical replication
connections are established so that GUC settings in the CONNECTION string
are properly passed through to and uesd by the walsender. This enables
```

This is a little bit inaccurate, all user specified settings expected the 3 ones being overwritten will be honored.

Are you suggesting that, because datestyle and the other two parameters
specified in CONNECTION aren(t actually applied by the walsender,
the commit message should explicitly mention that not all parameters
from CONNECTION are used?

No, what I was thinking is that, we could combine the three set statement into one, like:

```
Set a = 1; set b = 2; set c = 3;
```
So that sends a single statement to publisher server, that reduces round-trip from 3 times to one time.

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

#7Fujii Masao
masao.fujii@gmail.com
In reply to: Chao Li (#6)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Fri, Nov 21, 2025 at 6:24 PM Chao Li <li.evan.chao@gmail.com> wrote:

No, what I was thinking is that, we could combine the three set statement into one, like:

```
Set a = 1; set b = 2; set c = 3;
```
So that sends a single statement to publisher server, that reduces round-trip from 3 times to one time.

I see the point about combining the three SET commands to reduce round trips,
but I think the current approach in the patch (i.e., issuing a separate
SET command for each parameter) is sufficient. I still don't think
the additional round trip during replication connection startup is
a real concern. This approach is also consistent with what postgres_fdw
and pg_dump already do.

Regards,

--
Fujii Masao

#8Chao Li
li.evan.chao@gmail.com
In reply to: Fujii Masao (#7)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Nov 22, 2025, at 00:14, Fujii Masao <masao.fujii@gmail.com> wrote:

On Fri, Nov 21, 2025 at 6:24 PM Chao Li <li.evan.chao@gmail.com> wrote:

No, what I was thinking is that, we could combine the three set statement into one, like:

```
Set a = 1; set b = 2; set c = 3;
```
So that sends a single statement to publisher server, that reduces round-trip from 3 times to one time.

I see the point about combining the three SET commands to reduce round trips,
but I think the current approach in the patch (i.e., issuing a separate
SET command for each parameter) is sufficient. I still don't think
the additional round trip during replication connection startup is
a real concern. This approach is also consistent with what postgres_fdw
and pg_dump already do.

No problem. I don’t have a strong option here. I just saw you mentioned overhead of round trip and thought to improve. But I agree that overhead is tiny, not a real concern.

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

#9Fujii Masao
masao.fujii@gmail.com
In reply to: Chao Li (#8)
1 attachment(s)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Sat, Nov 22, 2025 at 10:31 AM Chao Li <li.evan.chao@gmail.com> wrote:

On Nov 22, 2025, at 00:14, Fujii Masao <masao.fujii@gmail.com> wrote:

On Fri, Nov 21, 2025 at 6:24 PM Chao Li <li.evan.chao@gmail.com> wrote:

No, what I was thinking is that, we could combine the three set statement into one, like:

```
Set a = 1; set b = 2; set c = 3;
```
So that sends a single statement to publisher server, that reduces round-trip from 3 times to one time.

I see the point about combining the three SET commands to reduce round trips,
but I think the current approach in the patch (i.e., issuing a separate
SET command for each parameter) is sufficient. I still don't think
the additional round trip during replication connection startup is
a real concern. This approach is also consistent with what postgres_fdw
and pg_dump already do.

No problem. I don’t have a strong option here. I just saw you mentioned overhead of round trip and thought to improve. But I agree that overhead is tiny, not a real concern.

Okay, thanks!

I've updated the patch to use lengthof() as you suggested.
The revised version is attached.

Regards,

--
Fujii Masao

Attachments:

v3-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchapplication/octet-stream; name=v3-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchDownload
From 4c5407fd093f1cf77234f4af604f2355d907a61e Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Sat, 22 Nov 2025 22:35:22 +0900
Subject: [PATCH v3] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Previously, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were not used by the publisher's walsender.
For example, in:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

the wal_sender_timeout setting had no effect.
This contrasted with physical replication, where GUCs in primary_conninfo
are applied to the walsender.

This limitation made it difficult to tune logical replication connections
individually, for example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit removes the restriction by changing how logical replication
connections are established so that GUC settings in the CONNECTION string
are properly passed through to and uesd by the walsender. This enables
per-connection configuration for logical replication.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Chao Li <lic@highgo.com>
Discussion: https://postgr.es/m/CAHGQGwGYV+-abbKwdrM2UHUe-JYOFWmsrs6=QicyJO-j+-Widw@mail.gmail.com
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 43 ++++++++++++++-----
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 239641bfbb6..500632b462f 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -180,17 +180,6 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 			/* Tell the publisher to translate to our encoding */
 			keys[++i] = "client_encoding";
 			vals[i] = GetDatabaseEncodingName();
-
-			/*
-			 * Force assorted GUC parameters to settings that ensure that the
-			 * publisher will output data values in a form that is unambiguous
-			 * to the subscriber.  (We don't want to modify the subscriber's
-			 * GUC settings, since that might surprise user-defined code
-			 * running in the subscriber, such as triggers.)  This should
-			 * match what pg_dump does.
-			 */
-			keys[++i] = "options";
-			vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
 		}
 		else
 		{
@@ -256,6 +245,38 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 		PQclear(res);
 	}
 
+	/*
+	 * Force assorted GUC parameters to settings that ensure that the
+	 * publisher will output data values in a form that is unambiguous to the
+	 * subscriber.  (We don't want to modify the subscriber's GUC settings,
+	 * since that might surprise user-defined code running in the subscriber,
+	 * such as triggers.)  This should match what pg_dump does.
+	 */
+	if (replication && logical)
+	{
+		const char *params[] =
+		{"datestyle", "intervalstyle", "extra_float_digits"};
+		const char *values[] = {"ISO", "postgres", "3"};
+
+		for (int j = 0; j < lengthof(params); j++)
+		{
+			char		sql[100];
+			PGresult   *res;
+
+			sprintf(sql, "SET %s = %s", params[j], values[j]);
+			res = libpqsrv_exec(conn->streamConn, sql,
+								WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
+			if (PQresultStatus(res) != PGRES_COMMAND_OK)
+			{
+				PQclear(res);
+				*err = psprintf(_("could not set %s: %s"),
+								params[j], pchomp(PQerrorMessage(conn->streamConn)));
+				goto bad_connection;
+			}
+			PQclear(res);
+		}
+	}
+
 	conn->logical = logical;
 
 	return conn;
-- 
2.51.2

#10Chao Li
li.evan.chao@gmail.com
In reply to: Fujii Masao (#9)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Nov 22, 2025, at 22:14, Fujii Masao <masao.fujii@gmail.com> wrote:

On Sat, Nov 22, 2025 at 10:31 AM Chao Li <li.evan.chao@gmail.com> wrote:

On Nov 22, 2025, at 00:14, Fujii Masao <masao.fujii@gmail.com> wrote:

On Fri, Nov 21, 2025 at 6:24 PM Chao Li <li.evan.chao@gmail.com> wrote:

No, what I was thinking is that, we could combine the three set statement into one, like:

```
Set a = 1; set b = 2; set c = 3;
```
So that sends a single statement to publisher server, that reduces round-trip from 3 times to one time.

I see the point about combining the three SET commands to reduce round trips,
but I think the current approach in the patch (i.e., issuing a separate
SET command for each parameter) is sufficient. I still don't think
the additional round trip during replication connection startup is
a real concern. This approach is also consistent with what postgres_fdw
and pg_dump already do.

No problem. I don’t have a strong option here. I just saw you mentioned overhead of round trip and thought to improve. But I agree that overhead is tiny, not a real concern.

Okay, thanks!

I've updated the patch to use lengthof() as you suggested.
The revised version is attached.

Regards,

--
Fujii Masao
<v3-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patch>

V3 looks good to me.

now all user specified options expect the 3 will be kept, will that expose a hold where user badly specifies some option that may break logical replication? If that’s true, then we need to parse user specified options and do some verifications.

Yeah, I agree that if certain GUCs can break logical replication,
we should enforce "safe" values, just as we currently do for datestyle.
And if any other GUCs can cause the issue, they could affect
postgres_fdw etc, so the fix would need to be broader.

Just want to clarify if you mean you will handle this in a future patch?

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

#11Jelte Fennema-Nio
postgres@jeltef.nl
In reply to: Fujii Masao (#5)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Fri, Nov 21, 2025, 00:47 Fujii Masao <masao.fujii@gmail.com> wrote:

On Thu, Nov 20, 2025 at 3:54 PM Chao Li <li.evan.chao@gmail.com> wrote:

Before this patch, all user specified options are silently discarded,

The GUC settings in CREATE SUBSCRIPTION were honored up through v14;
the behavior changed in commit f3d4019da5d, so some might view this
as a regression.

FWIW, I definitely view it as a regression. I used this in citus to make
the logical replication sender of the shard rebalancer use a higher CPU
priority[1]https://github.com/citusdata/citus/blame/662b7248db2146d33a1a21227271b839355a970a/src/backend/distributed/replication/multi_logical_replication.c#L1510. I had no clue, until now, that that logic got completely
broken in PG15 (which we coincidentally added support for in the same
release).

I'm not entirely sure if it's worth a backpatch. This citus feature
probably isn't the most critical. So if that's the only usecase in the wild
that got broken, then that might be fine. But I at least wanted to share
that others (i.e. me) have used this feature.

[1]: https://github.com/citusdata/citus/blame/662b7248db2146d33a1a21227271b839355a970a/src/backend/distributed/replication/multi_logical_replication.c#L1510
https://github.com/citusdata/citus/blame/662b7248db2146d33a1a21227271b839355a970a/src/backend/distributed/replication/multi_logical_replication.c#L1510

Show quoted text
#12Fujii Masao
masao.fujii@gmail.com
In reply to: Chao Li (#10)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Mon, Nov 24, 2025 at 12:52 PM Chao Li <li.evan.chao@gmail.com> wrote:

V3 looks good to me.

Thanks for reviewing the patch!

Yeah, I agree that if certain GUCs can break logical replication,
we should enforce "safe" values, just as we currently do for datestyle.
And if any other GUCs can cause the issue, they could affect
postgres_fdw etc, so the fix would need to be broader.

Just want to clarify if you mean you will handle this in a future patch?

I don't currently know of any other parameters that must be forced for
logical replication or postgres_fdw. But if we identify any, I'm happy to
review a patch that adds the necessary handling.

Regards,

--
Fujii Masao

#13Fujii Masao
masao.fujii@gmail.com
In reply to: Jelte Fennema-Nio (#11)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Mon, Nov 24, 2025 at 2:55 PM Jelte Fennema-Nio <postgres@jeltef.nl> wrote:

On Fri, Nov 21, 2025, 00:47 Fujii Masao <masao.fujii@gmail.com> wrote:

On Thu, Nov 20, 2025 at 3:54 PM Chao Li <li.evan.chao@gmail.com> wrote:

Before this patch, all user specified options are silently discarded,

The GUC settings in CREATE SUBSCRIPTION were honored up through v14;
the behavior changed in commit f3d4019da5d, so some might view this
as a regression.

FWIW, I definitely view it as a regression. I used this in citus to make the logical replication sender of the shard rebalancer use a higher CPU priority[1]. I had no clue, until now, that that logic got completely broken in PG15 (which we coincidentally added support for in the same release).

Thanks for sharing this!

I'm not entirely sure if it's worth a backpatch. This citus feature probably isn't the most critical. So if that's the only usecase in the wild that got broken, then that might be fine. But I at least wanted to share that others (i.e. me) have used this feature.

I found the following description in logical replication docs, which makes me
start thinking that the patch would need to be backpatched.

-----------------
If the role does not trust all table owners, include options=-crow_security=off
in the connection string
https://www.postgresql.org/docs/devel/logical-replication-security.html
-----------------

Regards,

--
Fujii Masao

#14Fujii Masao
masao.fujii@gmail.com
In reply to: Fujii Masao (#13)
3 attachment(s)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Tue, Nov 25, 2025 at 8:32 PM Fujii Masao <masao.fujii@gmail.com> wrote:

I found the following description in logical replication docs, which makes me
start thinking that the patch would need to be backpatched.

I've prepared patches for the older branches as well and attached them.

Regards,

--
Fujii Masao

Attachments:

v4-0001-PG15-PG16-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txttext/plain; charset=US-ASCII; name=v4-0001-PG15-PG16-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txtDownload
From 159e0c886546f3b12ebd48b57d12c68493d5309e Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 26 Nov 2025 23:35:44 +0900
Subject: [PATCH v4] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Prior to v15, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were correctly passed through to
the publisher's walsender. For example:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

would cause wal_sender_timeout to take effect on the publisher's walsender.

However, commit f3d4019da5d changed the way logical replication
connections are established, forcing the publisher's relevant
GUC settings (datestyle, intervalstyle, extra_float_digits) to
override those provided in the CONNECTION string. As a result,
from v15 through v18, GUC settings in the CONNECTION string were
always ignored.

This regression prevented per-connection tuning of logical replication.
For example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit restores the intended behavior by ensuring that
GUC settings in the CONNECTION string are again passed through
and applied by the walsender, allowing per-connection configuration.

Backpatch to v15, where the regression was introduced.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Chao Li <lic@highgo.com>
Discussion: https://postgr.es/m/CAHGQGwGYV+-abbKwdrM2UHUe-JYOFWmsrs6=QicyJO-j+-Widw@mail.gmail.com
Backpatch-through: 15
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 43 ++++++++++++++-----
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 568024ec974..1270d162c88 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -170,17 +170,6 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 		/* Tell the publisher to translate to our encoding */
 		keys[++i] = "client_encoding";
 		vals[i] = GetDatabaseEncodingName();
-
-		/*
-		 * Force assorted GUC parameters to settings that ensure that the
-		 * publisher will output data values in a form that is unambiguous to
-		 * the subscriber.  (We don't want to modify the subscriber's GUC
-		 * settings, since that might surprise user-defined code running in
-		 * the subscriber, such as triggers.)  This should match what pg_dump
-		 * does.
-		 */
-		keys[++i] = "options";
-		vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
 	}
 	keys[++i] = NULL;
 	vals[i] = NULL;
@@ -263,6 +252,37 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 		PQclear(res);
 	}
 
+	/*
+	 * Force assorted GUC parameters to settings that ensure that the
+	 * publisher will output data values in a form that is unambiguous to the
+	 * subscriber.  (We don't want to modify the subscriber's GUC settings,
+	 * since that might surprise user-defined code running in the subscriber,
+	 * such as triggers.)  This should match what pg_dump does.
+	 */
+	if (logical)
+	{
+		const char *params[] =
+		{"datestyle", "intervalstyle", "extra_float_digits"};
+		const char *values[] = {"ISO", "postgres", "3"};
+
+		for (int j = 0; j < lengthof(params); j++)
+		{
+			char		sql[100];
+			PGresult   *res;
+
+			sprintf(sql, "SET %s = %s", params[j], values[j]);
+			res = libpqrcv_PQexec(conn->streamConn, sql);
+			if (PQresultStatus(res) != PGRES_COMMAND_OK)
+			{
+				PQclear(res);
+				*err = psprintf(_("could not set %s: %s"),
+								params[j], pchomp(PQerrorMessage(conn->streamConn)));
+				goto bad_connection;
+			}
+			PQclear(res);
+		}
+	}
+
 	conn->logical = logical;
 
 	return conn;
@@ -434,6 +454,7 @@ libpqrcv_identify_system(WalReceiverConn *conn, TimeLineID *primary_tli)
 						"the primary server: %s",
 						pchomp(PQerrorMessage(conn->streamConn)))));
 	}
+
 	/*
 	 * IDENTIFY_SYSTEM returns 3 columns in 9.3 and earlier, and 4 columns in
 	 * 9.4 and onwards.
-- 
2.51.2

v4-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchapplication/octet-stream; name=v4-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchDownload
From 947cf9d587333219791a257389b95eae01d76237 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 26 Nov 2025 23:35:44 +0900
Subject: [PATCH v4] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Prior to v15, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were correctly passed through to
the publisher's walsender. For example:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

would cause wal_sender_timeout to take effect on the publisher's walsender.

However, commit f3d4019da5d changed the way logical replication
connections are established, forcing the publisher's relevant
GUC settings (datestyle, intervalstyle, extra_float_digits) to
override those provided in the CONNECTION string. As a result,
from v15 through v18, GUC settings in the CONNECTION string were
always ignored.

This regression prevented per-connection tuning of logical replication.
For example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit restores the intended behavior by ensuring that
GUC settings in the CONNECTION string are again passed through
and applied by the walsender, allowing per-connection configuration.

Backpatch to v15, where the regression was introduced.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Chao Li <lic@highgo.com>
Discussion: https://postgr.es/m/CAHGQGwGYV+-abbKwdrM2UHUe-JYOFWmsrs6=QicyJO-j+-Widw@mail.gmail.com
Backpatch-through: 15
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 43 ++++++++++++++-----
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 239641bfbb6..500632b462f 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -180,17 +180,6 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 			/* Tell the publisher to translate to our encoding */
 			keys[++i] = "client_encoding";
 			vals[i] = GetDatabaseEncodingName();
-
-			/*
-			 * Force assorted GUC parameters to settings that ensure that the
-			 * publisher will output data values in a form that is unambiguous
-			 * to the subscriber.  (We don't want to modify the subscriber's
-			 * GUC settings, since that might surprise user-defined code
-			 * running in the subscriber, such as triggers.)  This should
-			 * match what pg_dump does.
-			 */
-			keys[++i] = "options";
-			vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
 		}
 		else
 		{
@@ -256,6 +245,38 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 		PQclear(res);
 	}
 
+	/*
+	 * Force assorted GUC parameters to settings that ensure that the
+	 * publisher will output data values in a form that is unambiguous to the
+	 * subscriber.  (We don't want to modify the subscriber's GUC settings,
+	 * since that might surprise user-defined code running in the subscriber,
+	 * such as triggers.)  This should match what pg_dump does.
+	 */
+	if (replication && logical)
+	{
+		const char *params[] =
+		{"datestyle", "intervalstyle", "extra_float_digits"};
+		const char *values[] = {"ISO", "postgres", "3"};
+
+		for (int j = 0; j < lengthof(params); j++)
+		{
+			char		sql[100];
+			PGresult   *res;
+
+			sprintf(sql, "SET %s = %s", params[j], values[j]);
+			res = libpqsrv_exec(conn->streamConn, sql,
+								WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
+			if (PQresultStatus(res) != PGRES_COMMAND_OK)
+			{
+				PQclear(res);
+				*err = psprintf(_("could not set %s: %s"),
+								params[j], pchomp(PQerrorMessage(conn->streamConn)));
+				goto bad_connection;
+			}
+			PQclear(res);
+		}
+	}
+
 	conn->logical = logical;
 
 	return conn;
-- 
2.51.2

v4-0001-PG17-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txttext/plain; charset=US-ASCII; name=v4-0001-PG17-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txtDownload
From 1a1c412e2d320d82d7a6927e5e6550c554a77b23 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 26 Nov 2025 23:35:44 +0900
Subject: [PATCH v4] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Prior to v15, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were correctly passed through to
the publisher's walsender. For example:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

would cause wal_sender_timeout to take effect on the publisher's walsender.

However, commit f3d4019da5d changed the way logical replication
connections are established, forcing the publisher's relevant
GUC settings (datestyle, intervalstyle, extra_float_digits) to
override those provided in the CONNECTION string. As a result,
from v15 through v18, GUC settings in the CONNECTION string were
always ignored.

This regression prevented per-connection tuning of logical replication.
For example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit restores the intended behavior by ensuring that
GUC settings in the CONNECTION string are again passed through
and applied by the walsender, allowing per-connection configuration.

Backpatch to v15, where the regression was introduced.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Chao Li <lic@highgo.com>
Discussion: https://postgr.es/m/CAHGQGwGYV+-abbKwdrM2UHUe-JYOFWmsrs6=QicyJO-j+-Widw@mail.gmail.com
Backpatch-through: 15
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 42 ++++++++++++++-----
 1 file changed, 31 insertions(+), 11 deletions(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 02f12f29219..02f3ee15c87 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -178,17 +178,6 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 			/* Tell the publisher to translate to our encoding */
 			keys[++i] = "client_encoding";
 			vals[i] = GetDatabaseEncodingName();
-
-			/*
-			 * Force assorted GUC parameters to settings that ensure that the
-			 * publisher will output data values in a form that is unambiguous
-			 * to the subscriber.  (We don't want to modify the subscriber's
-			 * GUC settings, since that might surprise user-defined code
-			 * running in the subscriber, such as triggers.)  This should
-			 * match what pg_dump does.
-			 */
-			keys[++i] = "options";
-			vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
 		}
 		else
 		{
@@ -289,6 +278,37 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 		PQclear(res);
 	}
 
+	/*
+	 * Force assorted GUC parameters to settings that ensure that the
+	 * publisher will output data values in a form that is unambiguous to the
+	 * subscriber.  (We don't want to modify the subscriber's GUC settings,
+	 * since that might surprise user-defined code running in the subscriber,
+	 * such as triggers.)  This should match what pg_dump does.
+	 */
+	if (replication && logical)
+	{
+		const char *params[] =
+		{"datestyle", "intervalstyle", "extra_float_digits"};
+		const char *values[] = {"ISO", "postgres", "3"};
+
+		for (int j = 0; j < lengthof(params); j++)
+		{
+			char		sql[100];
+			PGresult   *res;
+
+			sprintf(sql, "SET %s = %s", params[j], values[j]);
+			res = libpqrcv_PQexec(conn->streamConn, sql);
+			if (PQresultStatus(res) != PGRES_COMMAND_OK)
+			{
+				PQclear(res);
+				*err = psprintf(_("could not set %s: %s"),
+								params[j], pchomp(PQerrorMessage(conn->streamConn)));
+				goto bad_connection;
+			}
+			PQclear(res);
+		}
+	}
+
 	conn->logical = logical;
 
 	return conn;
-- 
2.51.2

#15Kirill Reshke
reshkekirill@gmail.com
In reply to: Fujii Masao (#9)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Sat, 22 Nov 2025 at 17:14, Fujii Masao <masao.fujii@gmail.com> wrote:

Okay, thanks!

I've updated the patch to use lengthof() as you suggested.
The revised version is attached.

Regards,

--
Fujii Masao

Looking at v3 raises two questions for me.

First is if we should have a doc notion of which variables ought to be
set to what.
Second, how do we actually test that subscription connection options
are applied on the subscriber side? Can we have TAP for this (is is
worth the troubles)?

--
Best regards,
Kirill Reshke

#16Fujii Masao
masao.fujii@gmail.com
In reply to: Kirill Reshke (#15)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Thu, Nov 27, 2025 at 2:37 AM Kirill Reshke <reshkekirill@gmail.com> wrote:

Looking at v3 raises two questions for me.

First is if we should have a doc notion of which variables ought to be
set to what.

Are you suggesting that we document which GUC parameters should be set,
and to what values, for logical replication? We already have a section on this
in logical-replication.sgml. Is that sufficient?

Second, how do we actually test that subscription connection options
are applied on the subscriber side? Can we have TAP for this (is is
worth the troubles)?

+1 on adding a test. One idea is to enable log_replication_commands via
the CONNECTION option and then check that the publisher’s log contains
the message "received replication command: IDENTIFY_SYSTEM".
There may be a cleaner way to test this, though.

Regards,

--
Fujii Masao

#17Fujii Masao
masao.fujii@gmail.com
In reply to: Fujii Masao (#16)
4 attachment(s)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Thu, Nov 27, 2025 at 11:46 AM Fujii Masao <masao.fujii@gmail.com> wrote:

On Thu, Nov 27, 2025 at 2:37 AM Kirill Reshke <reshkekirill@gmail.com> wrote:

Looking at v3 raises two questions for me.

First is if we should have a doc notion of which variables ought to be
set to what.

Are you suggesting that we document which GUC parameters should be set,
and to what values, for logical replication? We already have a section on this
in logical-replication.sgml. Is that sufficient?

Second, how do we actually test that subscription connection options
are applied on the subscriber side? Can we have TAP for this (is is
worth the troubles)?

+1 on adding a test. One idea is to enable log_replication_commands via
the CONNECTION option and then check that the publisher’s log contains
the message "received replication command: IDENTIFY_SYSTEM".
There may be a cleaner way to test this, though.

I've added the test and attached it as patch v4-0002.

The test enables log_disconnections via the CONNECTION string and then checks
that the publisher's log contains the expected disconnection message after
the logical replication connection is reestablished.

Regards,

--
Fujii Masao

Attachments:

v4-0001-PG15-PG16-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txttext/plain; charset=US-ASCII; name=v4-0001-PG15-PG16-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txtDownload
From 159e0c886546f3b12ebd48b57d12c68493d5309e Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 26 Nov 2025 23:35:44 +0900
Subject: [PATCH v4] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Prior to v15, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were correctly passed through to
the publisher's walsender. For example:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

would cause wal_sender_timeout to take effect on the publisher's walsender.

However, commit f3d4019da5d changed the way logical replication
connections are established, forcing the publisher's relevant
GUC settings (datestyle, intervalstyle, extra_float_digits) to
override those provided in the CONNECTION string. As a result,
from v15 through v18, GUC settings in the CONNECTION string were
always ignored.

This regression prevented per-connection tuning of logical replication.
For example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit restores the intended behavior by ensuring that
GUC settings in the CONNECTION string are again passed through
and applied by the walsender, allowing per-connection configuration.

Backpatch to v15, where the regression was introduced.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Chao Li <lic@highgo.com>
Discussion: https://postgr.es/m/CAHGQGwGYV+-abbKwdrM2UHUe-JYOFWmsrs6=QicyJO-j+-Widw@mail.gmail.com
Backpatch-through: 15
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 43 ++++++++++++++-----
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 568024ec974..1270d162c88 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -170,17 +170,6 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 		/* Tell the publisher to translate to our encoding */
 		keys[++i] = "client_encoding";
 		vals[i] = GetDatabaseEncodingName();
-
-		/*
-		 * Force assorted GUC parameters to settings that ensure that the
-		 * publisher will output data values in a form that is unambiguous to
-		 * the subscriber.  (We don't want to modify the subscriber's GUC
-		 * settings, since that might surprise user-defined code running in
-		 * the subscriber, such as triggers.)  This should match what pg_dump
-		 * does.
-		 */
-		keys[++i] = "options";
-		vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
 	}
 	keys[++i] = NULL;
 	vals[i] = NULL;
@@ -263,6 +252,37 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 		PQclear(res);
 	}
 
+	/*
+	 * Force assorted GUC parameters to settings that ensure that the
+	 * publisher will output data values in a form that is unambiguous to the
+	 * subscriber.  (We don't want to modify the subscriber's GUC settings,
+	 * since that might surprise user-defined code running in the subscriber,
+	 * such as triggers.)  This should match what pg_dump does.
+	 */
+	if (logical)
+	{
+		const char *params[] =
+		{"datestyle", "intervalstyle", "extra_float_digits"};
+		const char *values[] = {"ISO", "postgres", "3"};
+
+		for (int j = 0; j < lengthof(params); j++)
+		{
+			char		sql[100];
+			PGresult   *res;
+
+			sprintf(sql, "SET %s = %s", params[j], values[j]);
+			res = libpqrcv_PQexec(conn->streamConn, sql);
+			if (PQresultStatus(res) != PGRES_COMMAND_OK)
+			{
+				PQclear(res);
+				*err = psprintf(_("could not set %s: %s"),
+								params[j], pchomp(PQerrorMessage(conn->streamConn)));
+				goto bad_connection;
+			}
+			PQclear(res);
+		}
+	}
+
 	conn->logical = logical;
 
 	return conn;
@@ -434,6 +454,7 @@ libpqrcv_identify_system(WalReceiverConn *conn, TimeLineID *primary_tli)
 						"the primary server: %s",
 						pchomp(PQerrorMessage(conn->streamConn)))));
 	}
+
 	/*
 	 * IDENTIFY_SYSTEM returns 3 columns in 9.3 and earlier, and 4 columns in
 	 * 9.4 and onwards.
-- 
2.51.2

v4-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchapplication/octet-stream; name=v4-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchDownload
From 947cf9d587333219791a257389b95eae01d76237 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 26 Nov 2025 23:35:44 +0900
Subject: [PATCH v4] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Prior to v15, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were correctly passed through to
the publisher's walsender. For example:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

would cause wal_sender_timeout to take effect on the publisher's walsender.

However, commit f3d4019da5d changed the way logical replication
connections are established, forcing the publisher's relevant
GUC settings (datestyle, intervalstyle, extra_float_digits) to
override those provided in the CONNECTION string. As a result,
from v15 through v18, GUC settings in the CONNECTION string were
always ignored.

This regression prevented per-connection tuning of logical replication.
For example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit restores the intended behavior by ensuring that
GUC settings in the CONNECTION string are again passed through
and applied by the walsender, allowing per-connection configuration.

Backpatch to v15, where the regression was introduced.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Chao Li <lic@highgo.com>
Discussion: https://postgr.es/m/CAHGQGwGYV+-abbKwdrM2UHUe-JYOFWmsrs6=QicyJO-j+-Widw@mail.gmail.com
Backpatch-through: 15
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 43 ++++++++++++++-----
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 239641bfbb6..500632b462f 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -180,17 +180,6 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 			/* Tell the publisher to translate to our encoding */
 			keys[++i] = "client_encoding";
 			vals[i] = GetDatabaseEncodingName();
-
-			/*
-			 * Force assorted GUC parameters to settings that ensure that the
-			 * publisher will output data values in a form that is unambiguous
-			 * to the subscriber.  (We don't want to modify the subscriber's
-			 * GUC settings, since that might surprise user-defined code
-			 * running in the subscriber, such as triggers.)  This should
-			 * match what pg_dump does.
-			 */
-			keys[++i] = "options";
-			vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
 		}
 		else
 		{
@@ -256,6 +245,38 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 		PQclear(res);
 	}
 
+	/*
+	 * Force assorted GUC parameters to settings that ensure that the
+	 * publisher will output data values in a form that is unambiguous to the
+	 * subscriber.  (We don't want to modify the subscriber's GUC settings,
+	 * since that might surprise user-defined code running in the subscriber,
+	 * such as triggers.)  This should match what pg_dump does.
+	 */
+	if (replication && logical)
+	{
+		const char *params[] =
+		{"datestyle", "intervalstyle", "extra_float_digits"};
+		const char *values[] = {"ISO", "postgres", "3"};
+
+		for (int j = 0; j < lengthof(params); j++)
+		{
+			char		sql[100];
+			PGresult   *res;
+
+			sprintf(sql, "SET %s = %s", params[j], values[j]);
+			res = libpqsrv_exec(conn->streamConn, sql,
+								WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
+			if (PQresultStatus(res) != PGRES_COMMAND_OK)
+			{
+				PQclear(res);
+				*err = psprintf(_("could not set %s: %s"),
+								params[j], pchomp(PQerrorMessage(conn->streamConn)));
+				goto bad_connection;
+			}
+			PQclear(res);
+		}
+	}
+
 	conn->logical = logical;
 
 	return conn;
-- 
2.51.2

v4-0002-Add-TAP-test-for-GUC-settings-passed-via-CONNECTI.patchapplication/octet-stream; name=v4-0002-Add-TAP-test-for-GUC-settings-passed-via-CONNECTI.patchDownload
From b8ca4b8637140da82b13f40ae509787bb643fea7 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Thu, 27 Nov 2025 12:36:59 +0900
Subject: [PATCH v4 2/2] Add TAP test for GUC settings passed via CONNECTION in
 logical replication.

This commit adds a TAP test to verify that GUC settings provided in
the CONNECTION string of CREATE/ALTER SUBSCRIPTION are correctly
passed through to and applied by the publisher's walsender.
---
 src/test/subscription/t/001_rep_changes.pl | 28 +++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/test/subscription/t/001_rep_changes.pl b/src/test/subscription/t/001_rep_changes.pl
index 430c1246d14..9d08ae8b641 100644
--- a/src/test/subscription/t/001_rep_changes.pl
+++ b/src/test/subscription/t/001_rep_changes.pl
@@ -438,17 +438,34 @@ is( $result, qq(11.11|baz|1
 22.22|bar|2),
 	'update works with dropped subscriber column');
 
+# Verify that GUC settings supplied in the CONNECTION string take effect on
+# the publisher's walsender.  We do this by enabling log_disconnections in
+# the CONNECTION string later and checking that the publisher's log contains a
+# disconnection message.
+#
+# First, confirm that no such disconnection message appears before enabling
+# log_disconnections.
+$logfile = slurp_file($node_publisher->logfile, $log_location);
+unlike(
+	$logfile,
+	qr/disconnection: session time:/,
+	'log_disconnections has not been enabled yet');
+
 # check that change of connection string and/or publication list causes
 # restart of subscription workers. We check the state along with
 # application_name to ensure that the walsender is (re)started.
 #
 # Not all of these are registered as tests as we need to poll for a change
 # but the test suite will fail nonetheless when something goes wrong.
+#
+# Enable log_disconnections as the change of connection string,
+# which is also for the above mentioned test of GUC settings passed through
+# CONNECTION.
 my $oldpid = $node_publisher->safe_psql('postgres',
 	"SELECT pid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
 );
 $node_subscriber->safe_psql('postgres',
-	"ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr sslmode=disable'"
+	"ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr options=''-c log_disconnections=on'''"
 );
 $node_publisher->poll_query_until('postgres',
 	"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
@@ -552,6 +569,15 @@ $node_publisher->poll_query_until('postgres',
   or die
   "Timed out while waiting for apply to restart after renaming SUBSCRIPTION";
 
+# Check that the expected disconnection message appears,
+# which shows that log_disconnections=on from the CONNECTION string
+# was correctly passed through to and honored by the walsender.
+$logfile = slurp_file($node_publisher->logfile, $log_location);
+like(
+	$logfile,
+	qr/disconnection: session time:/,
+	'log_disconnections in CONNECTION string had effect on publisher\'s walsender');
+
 # check all the cleanup
 $node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_renamed");
 
-- 
2.51.2

v4-0001-PG17-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txttext/plain; charset=US-ASCII; name=v4-0001-PG17-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txtDownload
From 1a1c412e2d320d82d7a6927e5e6550c554a77b23 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 26 Nov 2025 23:35:44 +0900
Subject: [PATCH v4] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Prior to v15, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were correctly passed through to
the publisher's walsender. For example:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

would cause wal_sender_timeout to take effect on the publisher's walsender.

However, commit f3d4019da5d changed the way logical replication
connections are established, forcing the publisher's relevant
GUC settings (datestyle, intervalstyle, extra_float_digits) to
override those provided in the CONNECTION string. As a result,
from v15 through v18, GUC settings in the CONNECTION string were
always ignored.

This regression prevented per-connection tuning of logical replication.
For example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit restores the intended behavior by ensuring that
GUC settings in the CONNECTION string are again passed through
and applied by the walsender, allowing per-connection configuration.

Backpatch to v15, where the regression was introduced.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Chao Li <lic@highgo.com>
Discussion: https://postgr.es/m/CAHGQGwGYV+-abbKwdrM2UHUe-JYOFWmsrs6=QicyJO-j+-Widw@mail.gmail.com
Backpatch-through: 15
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 42 ++++++++++++++-----
 1 file changed, 31 insertions(+), 11 deletions(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 02f12f29219..02f3ee15c87 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -178,17 +178,6 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 			/* Tell the publisher to translate to our encoding */
 			keys[++i] = "client_encoding";
 			vals[i] = GetDatabaseEncodingName();
-
-			/*
-			 * Force assorted GUC parameters to settings that ensure that the
-			 * publisher will output data values in a form that is unambiguous
-			 * to the subscriber.  (We don't want to modify the subscriber's
-			 * GUC settings, since that might surprise user-defined code
-			 * running in the subscriber, such as triggers.)  This should
-			 * match what pg_dump does.
-			 */
-			keys[++i] = "options";
-			vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
 		}
 		else
 		{
@@ -289,6 +278,37 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 		PQclear(res);
 	}
 
+	/*
+	 * Force assorted GUC parameters to settings that ensure that the
+	 * publisher will output data values in a form that is unambiguous to the
+	 * subscriber.  (We don't want to modify the subscriber's GUC settings,
+	 * since that might surprise user-defined code running in the subscriber,
+	 * such as triggers.)  This should match what pg_dump does.
+	 */
+	if (replication && logical)
+	{
+		const char *params[] =
+		{"datestyle", "intervalstyle", "extra_float_digits"};
+		const char *values[] = {"ISO", "postgres", "3"};
+
+		for (int j = 0; j < lengthof(params); j++)
+		{
+			char		sql[100];
+			PGresult   *res;
+
+			sprintf(sql, "SET %s = %s", params[j], values[j]);
+			res = libpqrcv_PQexec(conn->streamConn, sql);
+			if (PQresultStatus(res) != PGRES_COMMAND_OK)
+			{
+				PQclear(res);
+				*err = psprintf(_("could not set %s: %s"),
+								params[j], pchomp(PQerrorMessage(conn->streamConn)));
+				goto bad_connection;
+			}
+			PQclear(res);
+		}
+	}
+
 	conn->logical = logical;
 
 	return conn;
-- 
2.51.2

#18Fujii Masao
masao.fujii@gmail.com
In reply to: Fujii Masao (#17)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Thu, Nov 27, 2025 at 2:17 PM Fujii Masao <masao.fujii@gmail.com> wrote:

I've added the test and attached it as patch v4-0002.

The test enables log_disconnections via the CONNECTION string and then checks
that the publisher's log contains the expected disconnection message after
the logical replication connection is reestablished.

Barring any objections, I will commit the v4-001 patch and backpatch it to v15.

Regards,

--
Fujii Masao

#19Kirill Reshke
reshkekirill@gmail.com
In reply to: Fujii Masao (#18)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Tue, 2 Dec 2025 at 11:02, Fujii Masao <masao.fujii@gmail.com> wrote:

On Thu, Nov 27, 2025 at 2:17 PM Fujii Masao <masao.fujii@gmail.com> wrote:

I've added the test and attached it as patch v4-0002.

The test enables log_disconnections via the CONNECTION string and then checks
that the publisher's log contains the expected disconnection message after
the logical replication connection is reestablished.

Barring any objections, I will commit the v4-001 patch and backpatch it to v15.

Regards,

--
Fujii Masao

Sure lgtm. I actually checked v4-0001, but did not got any objections

--
Best regards,
Kirill Reshke

#20Amit Kapila
amit.kapila16@gmail.com
In reply to: Fujii Masao (#1)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Tue, Nov 18, 2025 at 9:29 PM Fujii Masao <masao.fujii@gmail.com> wrote:

In logical replication, any GUC settings specified in the CONNECTION clause of
CREATE SUBSCRIPTION are currently ignored. For example:

CREATE SUBSCRIPTION mysub
CONNECTION 'options=''-c wal_sender_timeout=1000'''
PUBLICATION ...

The wal_sender_timeout value here has no effect.

This is inconvenient when different logical replication walsenders need
different settings - e.g., a small wal_sender_timeout for walsender
connecting to a nearby subscriber and a larger one for walsender
connecting to a distant subscriber. Right now, it's not easy for users
to control such per-connection behavior.

The reason of thid limitation is that libpqrcv_connect() always overwrites
the options connection parameter as follows:

keys[++i] = "options";
vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c
extra_float_digits=3";

This wipes out any user-specified GUCs in the CONNECTION string.
Physical replication does not have this problem because it does not overwrite
options, so GUCs in primary_conninfo are honored.

To remove this restriction, how about switching to issuing SET commands for
datestyle, intervalstyle, and extra_float_digits after the connection
is established,
similar to what postgres_fdw does, instead of forcing them into options?
That would allow user-specified GUC settings in CREATE SUBSCRIPTION to
take effect.

Is it possible that we append the predefined options to the options
given by the user to avoid extra round-trip?

--
With Regards,
Amit Kapila.

#21Fujii Masao
masao.fujii@gmail.com
In reply to: Amit Kapila (#20)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Tue, Dec 2, 2025 at 9:08 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

Is it possible that we append the predefined options to the options
given by the user to avoid extra round-trip?

One idea is to add a function, similar to libpqrcv_get_dbname_from_conninfo()
in libpqwalreceiver.c, that extracts the options string from the conninfo,
to append the required fixed settings, and then to use the combined string as
the value of the options parameter. Do you think implementing this is worthwhile
to avoid the extra round trip?

Regards,

--
Fujii Masao

#22Amit Kapila
amit.kapila16@gmail.com
In reply to: Fujii Masao (#21)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Tue, Dec 2, 2025 at 8:30 PM Fujii Masao <masao.fujii@gmail.com> wrote:

On Tue, Dec 2, 2025 at 9:08 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

Is it possible that we append the predefined options to the options
given by the user to avoid extra round-trip?

One idea is to add a function, similar to libpqrcv_get_dbname_from_conninfo()
in libpqwalreceiver.c, that extracts the options string from the conninfo,
to append the required fixed settings, and then to use the combined string as
the value of the options parameter.

Yes, but libpqrcv_get_dbname_from_conninfo() is an exposed function,
we can implement something internal for libpqwalreceiver.c like
conninfo_add_defaults() present in fe-connect.c.

Do you think implementing this is worthwhile
to avoid the extra round trip?

I think so. Today, we have three variables, tomorrow there could be
more such variables as well and apart from avoiding extra round trips,
the idea to append options to provided options (if any) sounds more
logical to me.

--
With Regards,
Amit Kapila.

#23Fujii Masao
masao.fujii@gmail.com
In reply to: Amit Kapila (#22)
3 attachment(s)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Wed, Dec 3, 2025 at 2:45 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

On Tue, Dec 2, 2025 at 8:30 PM Fujii Masao <masao.fujii@gmail.com> wrote:

On Tue, Dec 2, 2025 at 9:08 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

Is it possible that we append the predefined options to the options
given by the user to avoid extra round-trip?

One idea is to add a function, similar to libpqrcv_get_dbname_from_conninfo()
in libpqwalreceiver.c, that extracts the options string from the conninfo,
to append the required fixed settings, and then to use the combined string as
the value of the options parameter.

Yes, but libpqrcv_get_dbname_from_conninfo() is an exposed function,
we can implement something internal for libpqwalreceiver.c like
conninfo_add_defaults() present in fe-connect.c.

Do you think implementing this is worthwhile
to avoid the extra round trip?

I think so. Today, we have three variables, tomorrow there could be
more such variables as well and apart from avoiding extra round trips,
the idea to append options to provided options (if any) sounds more
logical to me.

OK, I've implemented the idea discussed upthread. The patch updates
libpqrcv_connect() so that, for logical replication, it extracts the options
string from the conninfo, appends the required fixed settings, and passes
the combined string as the options parameter to libpq.

For example, if the conninfo specifies -c wal_sender_timeout=10s,
the resulting options value becomes:

-c wal_sender_timeout=10s -c datestyle=ISO -c
intervalstyle=postgres -c extra_float_digits=3.

Patch attached.

Regards,

--
Fujii Masao

Attachments:

v5-0001-PG15-PG16-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txttext/plain; charset=US-ASCII; name=v5-0001-PG15-PG16-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txtDownload
From f8e0dcdea82d08d65e30224da2339f58de8c5fc3 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 3 Dec 2025 00:59:09 +0900
Subject: [PATCH v5] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Prior to v15, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were correctly passed through to
the publisher's walsender. For example:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

would cause wal_sender_timeout to take effect on the publisher's walsender.

However, commit f3d4019da5d changed the way logical replication
connections are established, forcing the publisher's relevant
GUC settings (datestyle, intervalstyle, extra_float_digits) to
override those provided in the CONNECTION string. As a result,
from v15 through v18, GUC settings in the CONNECTION string were
always ignored.

This regression prevented per-connection tuning of logical replication.
For example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit restores the intended behavior by ensuring that
GUC settings in the CONNECTION string are again passed through
and applied by the walsender, allowing per-connection configuration.

Backpatch to v15, where the regression was introduced.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Chao Li <lic@highgo.com>
Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Discussion: https://postgr.es/m/CAHGQGwGYV+-abbKwdrM2UHUe-JYOFWmsrs6=QicyJO-j+-Widw@mail.gmail.com
Backpatch-through: 15
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 61 ++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 568024ec974..743f951f330 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -57,6 +57,8 @@ static void libpqrcv_get_senderinfo(WalReceiverConn *conn,
 									char **sender_host, int *sender_port);
 static char *libpqrcv_identify_system(WalReceiverConn *conn,
 									  TimeLineID *primary_tli);
+static char *libpqrcv_get_option_from_conninfo(const char *connInfo,
+											   const char *keyword);
 static int	libpqrcv_server_version(WalReceiverConn *conn);
 static void libpqrcv_readtimelinehistoryfile(WalReceiverConn *conn,
 											 TimeLineID tli, char **filename,
@@ -136,6 +138,7 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 	const char *keys[6];
 	const char *vals[6];
 	int			i = 0;
+	char	   *options_val = NULL;
 
 	/*
 	 * Re-validate connection string. The validation already happened at DDL
@@ -167,6 +170,8 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 	vals[i] = appname;
 	if (logical)
 	{
+		char	   *opt = NULL;
+
 		/* Tell the publisher to translate to our encoding */
 		keys[++i] = "client_encoding";
 		vals[i] = GetDatabaseEncodingName();
@@ -179,8 +184,13 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 		 * the subscriber, such as triggers.)  This should match what pg_dump
 		 * does.
 		 */
+		opt = libpqrcv_get_option_from_conninfo(conninfo, "options");
+		options_val = psprintf("%s -c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3",
+							   (opt == NULL) ? "" : opt);
 		keys[++i] = "options";
-		vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
+		vals[i] = options_val;
+		if (opt != NULL)
+			pfree(opt);
 	}
 	keys[++i] = NULL;
 	vals[i] = NULL;
@@ -232,6 +242,9 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 			status = PQconnectPoll(conn->streamConn);
 	} while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED);
 
+	if (options_val != NULL)
+		pfree(options_val);
+
 	if (PQstatus(conn->streamConn) != CONNECTION_OK)
 		goto bad_connection_errmsg;
 
@@ -434,6 +447,7 @@ libpqrcv_identify_system(WalReceiverConn *conn, TimeLineID *primary_tli)
 						"the primary server: %s",
 						pchomp(PQerrorMessage(conn->streamConn)))));
 	}
+
 	/*
 	 * IDENTIFY_SYSTEM returns 3 columns in 9.3 and earlier, and 4 columns in
 	 * 9.4 and onwards.
@@ -466,6 +480,51 @@ libpqrcv_server_version(WalReceiverConn *conn)
 	return PQserverVersion(conn->streamConn);
 }
 
+/*
+ * Get the value of the option with the given keyword from the primary
+ * server's conninfo.
+ *
+ * If the option is not found in connInfo, return NULL value.
+ */
+static char *
+libpqrcv_get_option_from_conninfo(const char *connInfo, const char *keyword)
+{
+	PQconninfoOption *opts;
+	char	   *option = NULL;
+	char	   *err = NULL;
+
+	opts = PQconninfoParse(connInfo, &err);
+	if (opts == NULL)
+	{
+		/* The error string is malloc'd, so we must free it explicitly */
+		char	   *errcopy = err ? pstrdup(err) : "out of memory";
+
+		PQfreemem(err);
+		ereport(ERROR,
+				(errcode(ERRCODE_SYNTAX_ERROR),
+				 errmsg("invalid connection string syntax: %s", errcopy)));
+	}
+
+	for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
+	{
+		/*
+		 * If the same option appears multiple times, then the last one will
+		 * be returned
+		 */
+		if (strcmp(opt->keyword, keyword) == 0 && opt->val &&
+			*opt->val)
+		{
+			if (option)
+				pfree(option);
+
+			option = pstrdup(opt->val);
+		}
+	}
+
+	PQconninfoFree(opts);
+	return option;
+}
+
 /*
  * Start streaming WAL data from given streaming options.
  *
-- 
2.51.2

v5-0002-Add-TAP-test-for-GUC-settings-passed-via-CONNECTI.patchapplication/octet-stream; name=v5-0002-Add-TAP-test-for-GUC-settings-passed-via-CONNECTI.patchDownload
From ad7ac7f3d88b6178cb50e2b64982bf89589c3655 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Thu, 27 Nov 2025 12:36:59 +0900
Subject: [PATCH v5 2/2] Add TAP test for GUC settings passed via CONNECTION in
 logical replication.

This commit adds a TAP test to verify that GUC settings provided in
the CONNECTION string of CREATE/ALTER SUBSCRIPTION are correctly
passed through to and applied by the publisher's walsender.
---
 src/test/subscription/t/001_rep_changes.pl | 29 +++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/test/subscription/t/001_rep_changes.pl b/src/test/subscription/t/001_rep_changes.pl
index 430c1246d14..c044a1e63e1 100644
--- a/src/test/subscription/t/001_rep_changes.pl
+++ b/src/test/subscription/t/001_rep_changes.pl
@@ -438,17 +438,34 @@ is( $result, qq(11.11|baz|1
 22.22|bar|2),
 	'update works with dropped subscriber column');
 
+# Verify that GUC settings supplied in the CONNECTION string take effect on
+# the publisher's walsender.  We do this by enabling log_disconnections in
+# the CONNECTION string later and checking that the publisher's log contains a
+# disconnection message.
+#
+# First, confirm that no such disconnection message appears before enabling
+# log_disconnections.
+$logfile = slurp_file($node_publisher->logfile, $log_location);
+unlike(
+	$logfile,
+	qr/disconnection: session time:/,
+	'log_disconnections has not been enabled yet');
+
 # check that change of connection string and/or publication list causes
 # restart of subscription workers. We check the state along with
 # application_name to ensure that the walsender is (re)started.
 #
 # Not all of these are registered as tests as we need to poll for a change
 # but the test suite will fail nonetheless when something goes wrong.
+#
+# Enable log_disconnections as the change of connection string,
+# which is also for the above mentioned test of GUC settings passed through
+# CONNECTION.
 my $oldpid = $node_publisher->safe_psql('postgres',
 	"SELECT pid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
 );
 $node_subscriber->safe_psql('postgres',
-	"ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr sslmode=disable'"
+	"ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr options=''-c log_disconnections=on'''"
 );
 $node_publisher->poll_query_until('postgres',
 	"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
@@ -552,6 +569,16 @@ $node_publisher->poll_query_until('postgres',
   or die
   "Timed out while waiting for apply to restart after renaming SUBSCRIPTION";
 
+# Check that the expected disconnection message appears,
+# which shows that log_disconnections=on from the CONNECTION string
+# was correctly passed through to and honored by the walsender.
+$logfile = slurp_file($node_publisher->logfile, $log_location);
+like(
+	$logfile,
+	qr/disconnection: session time:/,
+	'log_disconnections in CONNECTION string had effect on publisher\'s walsender'
+);
+
 # check all the cleanup
 $node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_renamed");
 
-- 
2.51.2

v5-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchapplication/octet-stream; name=v5-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchDownload
From c3e510636e8b7a1cce0657d86d6a29097544ee99 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 3 Dec 2025 00:59:09 +0900
Subject: [PATCH v5 1/2] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Prior to v15, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were correctly passed through to
the publisher's walsender. For example:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

would cause wal_sender_timeout to take effect on the publisher's walsender.

However, commit f3d4019da5d changed the way logical replication
connections are established, forcing the publisher's relevant
GUC settings (datestyle, intervalstyle, extra_float_digits) to
override those provided in the CONNECTION string. As a result,
from v15 through v18, GUC settings in the CONNECTION string were
always ignored.

This regression prevented per-connection tuning of logical replication.
For example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit restores the intended behavior by ensuring that
GUC settings in the CONNECTION string are again passed through
and applied by the walsender, allowing per-connection configuration.

Backpatch to v15, where the regression was introduced.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Chao Li <lic@highgo.com>
Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Discussion: https://postgr.es/m/CAHGQGwGYV+-abbKwdrM2UHUe-JYOFWmsrs6=QicyJO-j+-Widw@mail.gmail.com
Backpatch-through: 15
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 43 +++++++++++++++----
 1 file changed, 34 insertions(+), 9 deletions(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 5ddc9e812e7..c067908da35 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -65,6 +65,8 @@ static void libpqrcv_get_senderinfo(WalReceiverConn *conn,
 static char *libpqrcv_identify_system(WalReceiverConn *conn,
 									  TimeLineID *primary_tli);
 static char *libpqrcv_get_dbname_from_conninfo(const char *connInfo);
+static char *libpqrcv_get_option_from_conninfo(const char *connInfo,
+											   const char *keyword);
 static int	libpqrcv_server_version(WalReceiverConn *conn);
 static void libpqrcv_readtimelinehistoryfile(WalReceiverConn *conn,
 											 TimeLineID tli, char **filename,
@@ -150,6 +152,7 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 	const char *keys[6];
 	const char *vals[6];
 	int			i = 0;
+	char	   *options_val = NULL;
 
 	/*
 	 * Re-validate connection string. The validation already happened at DDL
@@ -177,6 +180,8 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 
 		if (logical)
 		{
+			char	   *opt = NULL;
+
 			/* Tell the publisher to translate to our encoding */
 			keys[++i] = "client_encoding";
 			vals[i] = GetDatabaseEncodingName();
@@ -189,8 +194,13 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 			 * running in the subscriber, such as triggers.)  This should
 			 * match what pg_dump does.
 			 */
+			opt = libpqrcv_get_option_from_conninfo(conninfo, "options");
+			options_val = psprintf("%s -c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3",
+								   (opt == NULL) ? "" : opt);
 			keys[++i] = "options";
-			vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
+			vals[i] = options_val;
+			if (opt != NULL)
+				pfree(opt);
 		}
 		else
 		{
@@ -217,6 +227,9 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 								 /* expand_dbname = */ true,
 								WAIT_EVENT_LIBPQWALRECEIVER_CONNECT);
 
+	if (options_val != NULL)
+		pfree(options_val);
+
 	if (PQstatus(conn->streamConn) != CONNECTION_OK)
 		goto bad_connection_errmsg;
 
@@ -460,9 +473,21 @@ libpqrcv_server_version(WalReceiverConn *conn)
  */
 static char *
 libpqrcv_get_dbname_from_conninfo(const char *connInfo)
+{
+	return libpqrcv_get_option_from_conninfo(connInfo, "dbname");
+}
+
+/*
+ * Get the value of the option with the given keyword from the primary
+ * server's conninfo.
+ *
+ * If the option is not found in connInfo, return NULL value.
+ */
+static char *
+libpqrcv_get_option_from_conninfo(const char *connInfo, const char *keyword)
 {
 	PQconninfoOption *opts;
-	char	   *dbname = NULL;
+	char	   *option = NULL;
 	char	   *err = NULL;
 
 	opts = PQconninfoParse(connInfo, &err);
@@ -480,21 +505,21 @@ libpqrcv_get_dbname_from_conninfo(const char *connInfo)
 	for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
 	{
 		/*
-		 * If multiple dbnames are specified, then the last one will be
-		 * returned
+		 * If the same option appears multiple times, then the last one will
+		 * be returned
 		 */
-		if (strcmp(opt->keyword, "dbname") == 0 && opt->val &&
+		if (strcmp(opt->keyword, keyword) == 0 && opt->val &&
 			*opt->val)
 		{
-			if (dbname)
-				pfree(dbname);
+			if (option)
+				pfree(option);
 
-			dbname = pstrdup(opt->val);
+			option = pstrdup(opt->val);
 		}
 	}
 
 	PQconninfoFree(opts);
-	return dbname;
+	return option;
 }
 
 /*
-- 
2.51.2

#24Japin Li
japinli@hotmail.com
In reply to: Fujii Masao (#23)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Fri, 19 Dec 2025 at 16:55, Fujii Masao <masao.fujii@gmail.com> wrote:

On Wed, Dec 3, 2025 at 2:45 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

On Tue, Dec 2, 2025 at 8:30 PM Fujii Masao <masao.fujii@gmail.com> wrote:

On Tue, Dec 2, 2025 at 9:08 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

Is it possible that we append the predefined options to the options
given by the user to avoid extra round-trip?

One idea is to add a function, similar to libpqrcv_get_dbname_from_conninfo()
in libpqwalreceiver.c, that extracts the options string from the conninfo,
to append the required fixed settings, and then to use the combined string as
the value of the options parameter.

Yes, but libpqrcv_get_dbname_from_conninfo() is an exposed function,
we can implement something internal for libpqwalreceiver.c like
conninfo_add_defaults() present in fe-connect.c.

Do you think implementing this is worthwhile
to avoid the extra round trip?

I think so. Today, we have three variables, tomorrow there could be
more such variables as well and apart from avoiding extra round trips,
the idea to append options to provided options (if any) sounds more
logical to me.

OK, I've implemented the idea discussed upthread. The patch updates
libpqrcv_connect() so that, for logical replication, it extracts the options
string from the conninfo, appends the required fixed settings, and passes
the combined string as the options parameter to libpq.

For example, if the conninfo specifies -c wal_sender_timeout=10s,
the resulting options value becomes:

-c wal_sender_timeout=10s -c datestyle=ISO -c
intervalstyle=postgres -c extra_float_digits=3.

Patch attached.

Thanks for the patch — that was my oversight.

LGTM with one small suggestion:

The comment says: "If the option is not found in connInfo, return NULL value."

Since the parameter is named `keyword`, I'd suggest: "If the keyword is not found in connInfo, return NULL."

This keeps terminology consistent with the function signature.

--
Regards,
Japin Li
ChengDu WenWu Information Technology Co., Ltd.

#25Fujii Masao
masao.fujii@gmail.com
In reply to: Japin Li (#24)
3 attachment(s)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Fri, Dec 19, 2025 at 7:07 PM Japin Li <japinli@hotmail.com> wrote:

Thanks for the patch — that was my oversight.

LGTM with one small suggestion:

Thanks for the review!

The comment says: "If the option is not found in connInfo, return NULL value."

Since the parameter is named `keyword`, I'd suggest: "If the keyword is not found in connInfo, return NULL."

This keeps terminology consistent with the function signature.

I think "the option with the given keyword" is more precise than just
"the keyword".
That said, simply using "the option" also seems sufficient in this context...

Regarding 0002 patch, I found that it caused a CI failure, so I’ve updated
the patch to fix that. The revised patch is attached.

Regards,

--
Fujii Masao

Attachments:

v6-0001-PG15-PG16-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txttext/plain; charset=US-ASCII; name=v6-0001-PG15-PG16-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txtDownload
From a29e80e158b043a76f00383705ee9c27f3f864a5 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 3 Dec 2025 00:59:09 +0900
Subject: [PATCH v6 1/2] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Prior to v15, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were correctly passed through to
the publisher's walsender. For example:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

would cause wal_sender_timeout to take effect on the publisher's walsender.

However, commit f3d4019da5d changed the way logical replication
connections are established, forcing the publisher's relevant
GUC settings (datestyle, intervalstyle, extra_float_digits) to
override those provided in the CONNECTION string. As a result,
from v15 through v18, GUC settings in the CONNECTION string were
always ignored.

This regression prevented per-connection tuning of logical replication.
For example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit restores the intended behavior by ensuring that
GUC settings in the CONNECTION string are again passed through
and applied by the walsender, allowing per-connection configuration.

Backpatch to v15, where the regression was introduced.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Chao Li <lic@highgo.com>
Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Reviewed-by: Japin Li <japinli@hotmail.com>
Discussion: https://postgr.es/m/CAHGQGwGYV+-abbKwdrM2UHUe-JYOFWmsrs6=QicyJO-j+-Widw@mail.gmail.com
Backpatch-through: 15
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 61 ++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 568024ec974..743f951f330 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -57,6 +57,8 @@ static void libpqrcv_get_senderinfo(WalReceiverConn *conn,
 									char **sender_host, int *sender_port);
 static char *libpqrcv_identify_system(WalReceiverConn *conn,
 									  TimeLineID *primary_tli);
+static char *libpqrcv_get_option_from_conninfo(const char *connInfo,
+											   const char *keyword);
 static int	libpqrcv_server_version(WalReceiverConn *conn);
 static void libpqrcv_readtimelinehistoryfile(WalReceiverConn *conn,
 											 TimeLineID tli, char **filename,
@@ -136,6 +138,7 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 	const char *keys[6];
 	const char *vals[6];
 	int			i = 0;
+	char	   *options_val = NULL;
 
 	/*
 	 * Re-validate connection string. The validation already happened at DDL
@@ -167,6 +170,8 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 	vals[i] = appname;
 	if (logical)
 	{
+		char	   *opt = NULL;
+
 		/* Tell the publisher to translate to our encoding */
 		keys[++i] = "client_encoding";
 		vals[i] = GetDatabaseEncodingName();
@@ -179,8 +184,13 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 		 * the subscriber, such as triggers.)  This should match what pg_dump
 		 * does.
 		 */
+		opt = libpqrcv_get_option_from_conninfo(conninfo, "options");
+		options_val = psprintf("%s -c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3",
+							   (opt == NULL) ? "" : opt);
 		keys[++i] = "options";
-		vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
+		vals[i] = options_val;
+		if (opt != NULL)
+			pfree(opt);
 	}
 	keys[++i] = NULL;
 	vals[i] = NULL;
@@ -232,6 +242,9 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 			status = PQconnectPoll(conn->streamConn);
 	} while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED);
 
+	if (options_val != NULL)
+		pfree(options_val);
+
 	if (PQstatus(conn->streamConn) != CONNECTION_OK)
 		goto bad_connection_errmsg;
 
@@ -434,6 +447,7 @@ libpqrcv_identify_system(WalReceiverConn *conn, TimeLineID *primary_tli)
 						"the primary server: %s",
 						pchomp(PQerrorMessage(conn->streamConn)))));
 	}
+
 	/*
 	 * IDENTIFY_SYSTEM returns 3 columns in 9.3 and earlier, and 4 columns in
 	 * 9.4 and onwards.
@@ -466,6 +480,51 @@ libpqrcv_server_version(WalReceiverConn *conn)
 	return PQserverVersion(conn->streamConn);
 }
 
+/*
+ * Get the value of the option with the given keyword from the primary
+ * server's conninfo.
+ *
+ * If the option is not found in connInfo, return NULL value.
+ */
+static char *
+libpqrcv_get_option_from_conninfo(const char *connInfo, const char *keyword)
+{
+	PQconninfoOption *opts;
+	char	   *option = NULL;
+	char	   *err = NULL;
+
+	opts = PQconninfoParse(connInfo, &err);
+	if (opts == NULL)
+	{
+		/* The error string is malloc'd, so we must free it explicitly */
+		char	   *errcopy = err ? pstrdup(err) : "out of memory";
+
+		PQfreemem(err);
+		ereport(ERROR,
+				(errcode(ERRCODE_SYNTAX_ERROR),
+				 errmsg("invalid connection string syntax: %s", errcopy)));
+	}
+
+	for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
+	{
+		/*
+		 * If the same option appears multiple times, then the last one will
+		 * be returned
+		 */
+		if (strcmp(opt->keyword, keyword) == 0 && opt->val &&
+			*opt->val)
+		{
+			if (option)
+				pfree(option);
+
+			option = pstrdup(opt->val);
+		}
+	}
+
+	PQconninfoFree(opts);
+	return option;
+}
+
 /*
  * Start streaming WAL data from given streaming options.
  *
-- 
2.51.2

v6-0002-Add-TAP-test-for-GUC-settings-passed-via-CONNECTI.patchapplication/octet-stream; name=v6-0002-Add-TAP-test-for-GUC-settings-passed-via-CONNECTI.patchDownload
From 0e49936016a81eccf095cf710aa6c5ea1ca761cf Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Thu, 27 Nov 2025 12:36:59 +0900
Subject: [PATCH v6 2/2] Add TAP test for GUC settings passed via CONNECTION in
 logical replication.

This commit adds a TAP test to verify that GUC settings provided in
the CONNECTION string of CREATE/ALTER SUBSCRIPTION are correctly
passed through to and applied by the publisher's walsender.
---
 src/test/subscription/t/001_rep_changes.pl | 29 +++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/test/subscription/t/001_rep_changes.pl b/src/test/subscription/t/001_rep_changes.pl
index 430c1246d14..9db7e8219de 100644
--- a/src/test/subscription/t/001_rep_changes.pl
+++ b/src/test/subscription/t/001_rep_changes.pl
@@ -438,17 +438,34 @@ is( $result, qq(11.11|baz|1
 22.22|bar|2),
 	'update works with dropped subscriber column');
 
+# Verify that GUC settings supplied in the CONNECTION string take effect on
+# the publisher's walsender.  We do this by enabling log_statement_stats in
+# the CONNECTION string later and checking that the publisher's log contains a
+# QUERY STATISTICS message.
+#
+# First, confirm that no such QUERY STATISTICS message appears before enabling
+# log_statement_stats.
+$logfile = slurp_file($node_publisher->logfile, $log_location);
+unlike(
+	$logfile,
+	qr/QUERY STATISTICS/,
+	'log_statement_stats has not been enabled yet');
+
 # check that change of connection string and/or publication list causes
 # restart of subscription workers. We check the state along with
 # application_name to ensure that the walsender is (re)started.
 #
 # Not all of these are registered as tests as we need to poll for a change
 # but the test suite will fail nonetheless when something goes wrong.
+#
+# Enable log_statement_stats as the change of connection string,
+# which is also for the above mentioned test of GUC settings passed through
+# CONNECTION.
 my $oldpid = $node_publisher->safe_psql('postgres',
 	"SELECT pid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
 );
 $node_subscriber->safe_psql('postgres',
-	"ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr sslmode=disable'"
+	"ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr options=''-c log_statement_stats=on'''"
 );
 $node_publisher->poll_query_until('postgres',
 	"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
@@ -552,6 +569,16 @@ $node_publisher->poll_query_until('postgres',
   or die
   "Timed out while waiting for apply to restart after renaming SUBSCRIPTION";
 
+# Check that the expected QUERY STATISTICS message appears,
+# which shows that log_statement_stats=on from the CONNECTION string
+# was correctly passed through to and honored by the walsender.
+$logfile = slurp_file($node_publisher->logfile, $log_location);
+like(
+	$logfile,
+	qr/QUERY STATISTICS/,
+	'log_statement_stats in CONNECTION string had effect on publisher\'s walsender'
+);
+
 # check all the cleanup
 $node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_renamed");
 
-- 
2.51.2

v6-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchapplication/octet-stream; name=v6-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchDownload
From ca378d99f617b4418261fce74e4f5ff84570a7e7 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 3 Dec 2025 00:59:09 +0900
Subject: [PATCH v6 1/2] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Prior to v15, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were correctly passed through to
the publisher's walsender. For example:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

would cause wal_sender_timeout to take effect on the publisher's walsender.

However, commit f3d4019da5d changed the way logical replication
connections are established, forcing the publisher's relevant
GUC settings (datestyle, intervalstyle, extra_float_digits) to
override those provided in the CONNECTION string. As a result,
from v15 through v18, GUC settings in the CONNECTION string were
always ignored.

This regression prevented per-connection tuning of logical replication.
For example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit restores the intended behavior by ensuring that
GUC settings in the CONNECTION string are again passed through
and applied by the walsender, allowing per-connection configuration.

Backpatch to v15, where the regression was introduced.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Chao Li <lic@highgo.com>
Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Reviewed-by: Japin Li <japinli@hotmail.com>
Discussion: https://postgr.es/m/CAHGQGwGYV+-abbKwdrM2UHUe-JYOFWmsrs6=QicyJO-j+-Widw@mail.gmail.com
Backpatch-through: 15
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 43 +++++++++++++++----
 1 file changed, 34 insertions(+), 9 deletions(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 5ddc9e812e7..c067908da35 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -65,6 +65,8 @@ static void libpqrcv_get_senderinfo(WalReceiverConn *conn,
 static char *libpqrcv_identify_system(WalReceiverConn *conn,
 									  TimeLineID *primary_tli);
 static char *libpqrcv_get_dbname_from_conninfo(const char *connInfo);
+static char *libpqrcv_get_option_from_conninfo(const char *connInfo,
+											   const char *keyword);
 static int	libpqrcv_server_version(WalReceiverConn *conn);
 static void libpqrcv_readtimelinehistoryfile(WalReceiverConn *conn,
 											 TimeLineID tli, char **filename,
@@ -150,6 +152,7 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 	const char *keys[6];
 	const char *vals[6];
 	int			i = 0;
+	char	   *options_val = NULL;
 
 	/*
 	 * Re-validate connection string. The validation already happened at DDL
@@ -177,6 +180,8 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 
 		if (logical)
 		{
+			char	   *opt = NULL;
+
 			/* Tell the publisher to translate to our encoding */
 			keys[++i] = "client_encoding";
 			vals[i] = GetDatabaseEncodingName();
@@ -189,8 +194,13 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 			 * running in the subscriber, such as triggers.)  This should
 			 * match what pg_dump does.
 			 */
+			opt = libpqrcv_get_option_from_conninfo(conninfo, "options");
+			options_val = psprintf("%s -c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3",
+								   (opt == NULL) ? "" : opt);
 			keys[++i] = "options";
-			vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
+			vals[i] = options_val;
+			if (opt != NULL)
+				pfree(opt);
 		}
 		else
 		{
@@ -217,6 +227,9 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 								 /* expand_dbname = */ true,
 								WAIT_EVENT_LIBPQWALRECEIVER_CONNECT);
 
+	if (options_val != NULL)
+		pfree(options_val);
+
 	if (PQstatus(conn->streamConn) != CONNECTION_OK)
 		goto bad_connection_errmsg;
 
@@ -460,9 +473,21 @@ libpqrcv_server_version(WalReceiverConn *conn)
  */
 static char *
 libpqrcv_get_dbname_from_conninfo(const char *connInfo)
+{
+	return libpqrcv_get_option_from_conninfo(connInfo, "dbname");
+}
+
+/*
+ * Get the value of the option with the given keyword from the primary
+ * server's conninfo.
+ *
+ * If the option is not found in connInfo, return NULL value.
+ */
+static char *
+libpqrcv_get_option_from_conninfo(const char *connInfo, const char *keyword)
 {
 	PQconninfoOption *opts;
-	char	   *dbname = NULL;
+	char	   *option = NULL;
 	char	   *err = NULL;
 
 	opts = PQconninfoParse(connInfo, &err);
@@ -480,21 +505,21 @@ libpqrcv_get_dbname_from_conninfo(const char *connInfo)
 	for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
 	{
 		/*
-		 * If multiple dbnames are specified, then the last one will be
-		 * returned
+		 * If the same option appears multiple times, then the last one will
+		 * be returned
 		 */
-		if (strcmp(opt->keyword, "dbname") == 0 && opt->val &&
+		if (strcmp(opt->keyword, keyword) == 0 && opt->val &&
 			*opt->val)
 		{
-			if (dbname)
-				pfree(dbname);
+			if (option)
+				pfree(option);
 
-			dbname = pstrdup(opt->val);
+			option = pstrdup(opt->val);
 		}
 	}
 
 	PQconninfoFree(opts);
-	return dbname;
+	return option;
 }
 
 /*
-- 
2.51.2

#26Kirill Reshke
reshkekirill@gmail.com
In reply to: Fujii Masao (#25)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Fri, 19 Dec 2025 at 16:05, Fujii Masao <masao.fujii@gmail.com> wrote:

On Fri, Dec 19, 2025 at 7:07 PM Japin Li <japinli@hotmail.com> wrote:

Thanks for the patch — that was my oversight.

LGTM with one small suggestion:

Thanks for the review!

The comment says: "If the option is not found in connInfo, return NULL value."

Since the parameter is named `keyword`, I'd suggest: "If the keyword is not found in connInfo, return NULL."

This keeps terminology consistent with the function signature.

I think "the option with the given keyword" is more precise than just
"the keyword".
That said, simply using "the option" also seems sufficient in this context...

Regarding 0002 patch, I found that it caused a CI failure, so I’ve updated
the patch to fix that. The revised patch is attached.

Regards,

--
Fujii Masao

Hi!
I checked the new TAP test 0002 changes. I am wondering, why are
connection options validated so late in this test? I mean, we do
ALTER PUBLICATION, then we restart publisher, wait for catchup, check
alter publication, and etc, and only then we look if connection
options are indeed applied?

--
Best regards,
Kirill Reshke

#27Fujii Masao
masao.fujii@gmail.com
In reply to: Kirill Reshke (#26)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Fri, Dec 19, 2025 at 9:30 PM Kirill Reshke <reshkekirill@gmail.com> wrote:

I checked the new TAP test 0002 changes. I am wondering, why are
connection options validated so late in this test? I mean, we do
ALTER PUBLICATION, then we restart publisher, wait for catchup, check
alter publication, and etc, and only then we look if connection
options are indeed applied?

Are you suggesting testing whether the conninfo setting is applied earlier,
for example, right after both running ALTER SUBSCRIPTION CONNECTION and
confirming that the logical replication connection is re-established?
Yeah, that might be better and would also make the test easier to read.

Regards,

--
Fujii Masao

#28Kirill Reshke
reshkekirill@gmail.com
In reply to: Fujii Masao (#27)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Fri, 19 Dec 2025 at 19:42, Fujii Masao <masao.fujii@gmail.com> wrote:

On Fri, Dec 19, 2025 at 9:30 PM Kirill Reshke <reshkekirill@gmail.com> wrote:

I checked the new TAP test 0002 changes. I am wondering, why are
connection options validated so late in this test? I mean, we do
ALTER PUBLICATION, then we restart publisher, wait for catchup, check
alter publication, and etc, and only then we look if connection
options are indeed applied?

Are you suggesting testing whether the conninfo setting is applied earlier,
for example, right after both running ALTER SUBSCRIPTION CONNECTION and
confirming that the logical replication connection is re-established?
Yeah, that might be better and would also make the test easier to read.

Regards,

--
Fujii Masao

Yes, exactly

--
Best regards,
Kirill Reshke

#29Amit Kapila
amit.kapila16@gmail.com
In reply to: Fujii Masao (#23)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Fri, Dec 19, 2025 at 1:25 PM Fujii Masao <masao.fujii@gmail.com> wrote:

On Wed, Dec 3, 2025 at 2:45 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

On Tue, Dec 2, 2025 at 8:30 PM Fujii Masao <masao.fujii@gmail.com> wrote:

On Tue, Dec 2, 2025 at 9:08 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

Is it possible that we append the predefined options to the options
given by the user to avoid extra round-trip?

One idea is to add a function, similar to libpqrcv_get_dbname_from_conninfo()
in libpqwalreceiver.c, that extracts the options string from the conninfo,
to append the required fixed settings, and then to use the combined string as
the value of the options parameter.

Yes, but libpqrcv_get_dbname_from_conninfo() is an exposed function,
we can implement something internal for libpqwalreceiver.c like
conninfo_add_defaults() present in fe-connect.c.

Do you think implementing this is worthwhile
to avoid the extra round trip?

I think so. Today, we have three variables, tomorrow there could be
more such variables as well and apart from avoiding extra round trips,
the idea to append options to provided options (if any) sounds more
logical to me.

OK, I've implemented the idea discussed upthread. The patch updates
libpqrcv_connect() so that, for logical replication, it extracts the options
string from the conninfo, appends the required fixed settings, and passes
the combined string as the options parameter to libpq.

For example, if the conninfo specifies -c wal_sender_timeout=10s,
the resulting options value becomes:

-c wal_sender_timeout=10s -c datestyle=ISO -c
intervalstyle=postgres -c extra_float_digits=3.

Patch attached.

Thanks, the idea looks good now. One minor comment:
}
+
/*

This appears to be an unnecessary addition in the patch.

--
With Regards,
Amit Kapila.

#30Chao Li
li.evan.chao@gmail.com
In reply to: Fujii Masao (#25)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Dec 19, 2025, at 19:05, Fujii Masao <masao.fujii@gmail.com> wrote:

On Fri, Dec 19, 2025 at 7:07 PM Japin Li <japinli@hotmail.com> wrote:

Thanks for the patch — that was my oversight.

LGTM with one small suggestion:

Thanks for the review!

The comment says: "If the option is not found in connInfo, return NULL value."

Since the parameter is named `keyword`, I'd suggest: "If the keyword is not found in connInfo, return NULL."

This keeps terminology consistent with the function signature.

I think "the option with the given keyword" is more precise than just
"the keyword".
That said, simply using "the option" also seems sufficient in this context...

Regarding 0002 patch, I found that it caused a CI failure, so I’ve updated
the patch to fix that. The revised patch is attached.

Regards,

--
Fujii Masao
<v6-0001-PG15-PG16-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txt><v6-0002-Add-TAP-test-for-GUC-settings-passed-via-CONNECTI.patch><v6-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patch>

A few more comments on v6:

1 - 0001
```
+ if (opt != NULL)
+ pfree(opt);
```

From what I learned from previous reviews, pfree() is safe to handle NULL, we can omit the NULL check, which makes the code simpler. This comment applies to multiple places.

2. I still think we should verify options extracted from conninfo. In the 0002’s tap script, if I make the following change:
```
 $node_subscriber->safe_psql('postgres',
-       "ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr options=''-c log_statement_stats=on'''"
+       "ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr options=''-c log_statement_stats=on-c wal_sender_timeout=1000'’'"
```

Notice, there is no space between the second “-c” and “on”, meaning that a user passes an invalid options. Then the test will get stuck forever, which would never happen before this patch.

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

#31Japin Li
japinli@hotmail.com
In reply to: Chao Li (#30)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Tue, 23 Dec 2025 at 14:17, Chao Li <li.evan.chao@gmail.com> wrote:

On Dec 19, 2025, at 19:05, Fujii Masao <masao.fujii@gmail.com> wrote:

On Fri, Dec 19, 2025 at 7:07 PM Japin Li <japinli@hotmail.com> wrote:

Thanks for the patch — that was my oversight.

LGTM with one small suggestion:

Thanks for the review!

The comment says: "If the option is not found in connInfo, return NULL value."

Since the parameter is named `keyword`, I'd suggest: "If the keyword is not found in connInfo, return NULL."

This keeps terminology consistent with the function signature.

I think "the option with the given keyword" is more precise than just
"the keyword".
That said, simply using "the option" also seems sufficient in this context...

Regarding 0002 patch, I found that it caused a CI failure, so I’ve updated
the patch to fix that. The revised patch is attached.

Regards,

--
Fujii Masao
<v6-0001-PG15-PG16-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txt><v6-0002-Add-TAP-test-for-GUC-settings-passed-via-CONNECTI.patch><v6-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patch>

A few more comments on v6:

1 - 0001
```
+ if (opt != NULL)
+ pfree(opt);
```

From what I learned from previous reviews, pfree() is safe to handle NULL, we can omit the NULL check, which makes the code simpler. This comment applies to multiple places.

I think we cannot omit it here. We have two pfree()s, one for frontend and
one for backend. IIUC, the frontend pfree() is safe to handle NULL, but the
backend pfree() is not.

2. I still think we should verify options extracted from conninfo. In the 0002’s tap script, if I make the following change:
```
$node_subscriber->safe_psql('postgres',
-       "ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr options=''-c log_statement_stats=on'''"
+       "ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr options=''-c log_statement_stats=on-c wal_sender_timeout=1000'’'"
```

Notice, there is no space between the second “-c” and “on”, meaning that a user passes an invalid options. Then the test will get stuck forever, which would never happen before this patch.

How to verify the options? Checking for -c isn't enough because valid flags
can still carry invalid values. For example, -c log_statement_stats=aa is
syntactically correct but will fail at runtime.

--
Regards,
Japin Li
ChengDu WenWu Information Technology Co., Ltd.

#32Chao Li
li.evan.chao@gmail.com
In reply to: Japin Li (#31)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Dec 23, 2025, at 16:13, Japin Li <japinli@hotmail.com> wrote:

On Tue, 23 Dec 2025 at 14:17, Chao Li <li.evan.chao@gmail.com> wrote:

On Dec 19, 2025, at 19:05, Fujii Masao <masao.fujii@gmail.com> wrote:

On Fri, Dec 19, 2025 at 7:07 PM Japin Li <japinli@hotmail.com> wrote:

Thanks for the patch — that was my oversight.

LGTM with one small suggestion:

Thanks for the review!

The comment says: "If the option is not found in connInfo, return NULL value."

Since the parameter is named `keyword`, I'd suggest: "If the keyword is not found in connInfo, return NULL."

This keeps terminology consistent with the function signature.

I think "the option with the given keyword" is more precise than just
"the keyword".
That said, simply using "the option" also seems sufficient in this context...

Regarding 0002 patch, I found that it caused a CI failure, so I’ve updated
the patch to fix that. The revised patch is attached.

Regards,

--
Fujii Masao
<v6-0001-PG15-PG16-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txt><v6-0002-Add-TAP-test-for-GUC-settings-passed-via-CONNECTI.patch><v6-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patch>

A few more comments on v6:

1 - 0001
```
+ if (opt != NULL)
+ pfree(opt);
```

From what I learned from previous reviews, pfree() is safe to handle NULL, we can omit the NULL check, which makes the code simpler. This comment applies to multiple places.

I think we cannot omit it here. We have two pfree()s, one for frontend and
one for backend. IIUC, the frontend pfree() is safe to handle NULL, but the
backend pfree() is not.

Ahh… You are right. I just traced a backend pfree(), and it ends up calling GetMemoryChunkMethodID(const void *point), and the function doesn’t handle NULL pointer.

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

#33Fujii Masao
masao.fujii@gmail.com
In reply to: Kirill Reshke (#28)
3 attachment(s)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Sat, Dec 20, 2025 at 12:48 AM Kirill Reshke <reshkekirill@gmail.com> wrote:

On Fri, 19 Dec 2025 at 19:42, Fujii Masao <masao.fujii@gmail.com> wrote:

On Fri, Dec 19, 2025 at 9:30 PM Kirill Reshke <reshkekirill@gmail.com> wrote:

I checked the new TAP test 0002 changes. I am wondering, why are
connection options validated so late in this test? I mean, we do
ALTER PUBLICATION, then we restart publisher, wait for catchup, check
alter publication, and etc, and only then we look if connection
options are indeed applied?

Are you suggesting testing whether the conninfo setting is applied earlier,
for example, right after both running ALTER SUBSCRIPTION CONNECTION and
confirming that the logical replication connection is re-established?
Yeah, that might be better and would also make the test easier to read.

Regards,

--
Fujii Masao

Yes, exactly

OK, I've updated the 0002 patch accordingly.

Regards,

--
Fujii Masao

Attachments:

v7-0001-PG15-PG16-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txttext/plain; charset=US-ASCII; name=v7-0001-PG15-PG16-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.txtDownload
From 38798c9bc99f0499bea754f3d6a51d5244163360 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 3 Dec 2025 00:59:09 +0900
Subject: [PATCH v7 1/2] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Prior to v15, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were correctly passed through to
the publisher's walsender. For example:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

would cause wal_sender_timeout to take effect on the publisher's walsender.

However, commit f3d4019da5d changed the way logical replication
connections are established, forcing the publisher's relevant
GUC settings (datestyle, intervalstyle, extra_float_digits) to
override those provided in the CONNECTION string. As a result,
from v15 through v18, GUC settings in the CONNECTION string were
always ignored.

This regression prevented per-connection tuning of logical replication.
For example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit restores the intended behavior by ensuring that
GUC settings in the CONNECTION string are again passed through
and applied by the walsender, allowing per-connection configuration.

Backpatch to v15, where the regression was introduced.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Chao Li <lic@highgo.com>
Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Reviewed-by: Japin Li <japinli@hotmail.com>
Discussion: https://postgr.es/m/CAHGQGwGYV+-abbKwdrM2UHUe-JYOFWmsrs6=QicyJO-j+-Widw@mail.gmail.com
Backpatch-through: 15
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 61 ++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 568024ec974..743f951f330 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -57,6 +57,8 @@ static void libpqrcv_get_senderinfo(WalReceiverConn *conn,
 									char **sender_host, int *sender_port);
 static char *libpqrcv_identify_system(WalReceiverConn *conn,
 									  TimeLineID *primary_tli);
+static char *libpqrcv_get_option_from_conninfo(const char *connInfo,
+											   const char *keyword);
 static int	libpqrcv_server_version(WalReceiverConn *conn);
 static void libpqrcv_readtimelinehistoryfile(WalReceiverConn *conn,
 											 TimeLineID tli, char **filename,
@@ -136,6 +138,7 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 	const char *keys[6];
 	const char *vals[6];
 	int			i = 0;
+	char	   *options_val = NULL;
 
 	/*
 	 * Re-validate connection string. The validation already happened at DDL
@@ -167,6 +170,8 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 	vals[i] = appname;
 	if (logical)
 	{
+		char	   *opt = NULL;
+
 		/* Tell the publisher to translate to our encoding */
 		keys[++i] = "client_encoding";
 		vals[i] = GetDatabaseEncodingName();
@@ -179,8 +184,13 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 		 * the subscriber, such as triggers.)  This should match what pg_dump
 		 * does.
 		 */
+		opt = libpqrcv_get_option_from_conninfo(conninfo, "options");
+		options_val = psprintf("%s -c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3",
+							   (opt == NULL) ? "" : opt);
 		keys[++i] = "options";
-		vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
+		vals[i] = options_val;
+		if (opt != NULL)
+			pfree(opt);
 	}
 	keys[++i] = NULL;
 	vals[i] = NULL;
@@ -232,6 +242,9 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
 			status = PQconnectPoll(conn->streamConn);
 	} while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED);
 
+	if (options_val != NULL)
+		pfree(options_val);
+
 	if (PQstatus(conn->streamConn) != CONNECTION_OK)
 		goto bad_connection_errmsg;
 
@@ -434,6 +447,7 @@ libpqrcv_identify_system(WalReceiverConn *conn, TimeLineID *primary_tli)
 						"the primary server: %s",
 						pchomp(PQerrorMessage(conn->streamConn)))));
 	}
+
 	/*
 	 * IDENTIFY_SYSTEM returns 3 columns in 9.3 and earlier, and 4 columns in
 	 * 9.4 and onwards.
@@ -466,6 +480,51 @@ libpqrcv_server_version(WalReceiverConn *conn)
 	return PQserverVersion(conn->streamConn);
 }
 
+/*
+ * Get the value of the option with the given keyword from the primary
+ * server's conninfo.
+ *
+ * If the option is not found in connInfo, return NULL value.
+ */
+static char *
+libpqrcv_get_option_from_conninfo(const char *connInfo, const char *keyword)
+{
+	PQconninfoOption *opts;
+	char	   *option = NULL;
+	char	   *err = NULL;
+
+	opts = PQconninfoParse(connInfo, &err);
+	if (opts == NULL)
+	{
+		/* The error string is malloc'd, so we must free it explicitly */
+		char	   *errcopy = err ? pstrdup(err) : "out of memory";
+
+		PQfreemem(err);
+		ereport(ERROR,
+				(errcode(ERRCODE_SYNTAX_ERROR),
+				 errmsg("invalid connection string syntax: %s", errcopy)));
+	}
+
+	for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
+	{
+		/*
+		 * If the same option appears multiple times, then the last one will
+		 * be returned
+		 */
+		if (strcmp(opt->keyword, keyword) == 0 && opt->val &&
+			*opt->val)
+		{
+			if (option)
+				pfree(option);
+
+			option = pstrdup(opt->val);
+		}
+	}
+
+	PQconninfoFree(opts);
+	return option;
+}
+
 /*
  * Start streaming WAL data from given streaming options.
  *
-- 
2.51.2

v7-0002-Add-TAP-test-for-GUC-settings-passed-via-CONNECTI.patchapplication/octet-stream; name=v7-0002-Add-TAP-test-for-GUC-settings-passed-via-CONNECTI.patchDownload
From 32f36d5bf49c5ec8b60aa05cc0e070b686b459ed Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Thu, 27 Nov 2025 12:36:59 +0900
Subject: [PATCH v7 2/2] Add TAP test for GUC settings passed via CONNECTION in
 logical replication.

This commit adds a TAP test to verify that GUC settings provided in
the CONNECTION string of CREATE/ALTER SUBSCRIPTION are correctly
passed through to and applied by the publisher's walsender.
---
 src/test/subscription/t/001_rep_changes.pl | 29 +++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/test/subscription/t/001_rep_changes.pl b/src/test/subscription/t/001_rep_changes.pl
index ecb79e79474..4eab249d62e 100644
--- a/src/test/subscription/t/001_rep_changes.pl
+++ b/src/test/subscription/t/001_rep_changes.pl
@@ -438,17 +438,34 @@ is( $result, qq(11.11|baz|1
 22.22|bar|2),
 	'update works with dropped subscriber column');
 
+# Verify that GUC settings supplied in the CONNECTION string take effect on
+# the publisher's walsender.  We do this by enabling log_statement_stats in
+# the CONNECTION string later and checking that the publisher's log contains a
+# QUERY STATISTICS message.
+#
+# First, confirm that no such QUERY STATISTICS message appears before enabling
+# log_statement_stats.
+$logfile = slurp_file($node_publisher->logfile, $log_location);
+unlike(
+	$logfile,
+	qr/QUERY STATISTICS/,
+	'log_statement_stats has not been enabled yet');
+
 # check that change of connection string and/or publication list causes
 # restart of subscription workers. We check the state along with
 # application_name to ensure that the walsender is (re)started.
 #
 # Not all of these are registered as tests as we need to poll for a change
 # but the test suite will fail nonetheless when something goes wrong.
+#
+# Enable log_statement_stats as the change of connection string,
+# which is also for the above mentioned test of GUC settings passed through
+# CONNECTION.
 my $oldpid = $node_publisher->safe_psql('postgres',
 	"SELECT pid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
 );
 $node_subscriber->safe_psql('postgres',
-	"ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr sslmode=disable'"
+	"ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr options=''-c log_statement_stats=on'''"
 );
 $node_publisher->poll_query_until('postgres',
 	"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
@@ -456,6 +473,16 @@ $node_publisher->poll_query_until('postgres',
   or die
   "Timed out while waiting for apply to restart after changing CONNECTION";
 
+# Check that the expected QUERY STATISTICS message appears,
+# which shows that log_statement_stats=on from the CONNECTION string
+# was correctly passed through to and honored by the walsender.
+$logfile = slurp_file($node_publisher->logfile, $log_location);
+like(
+	$logfile,
+	qr/QUERY STATISTICS/,
+	'log_statement_stats in CONNECTION string had effect on publisher\'s walsender'
+);
+
 $oldpid = $node_publisher->safe_psql('postgres',
 	"SELECT pid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
 );
-- 
2.51.2

v7-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchapplication/octet-stream; name=v7-0001-Honor-GUC-settings-specified-in-CREATE-SUBSCRIPTI.patchDownload
From 84d9dfd3e7d49c318dfad78c31cd5ca620b31877 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 3 Dec 2025 00:59:09 +0900
Subject: [PATCH v7 1/2] Honor GUC settings specified in CREATE SUBSCRIPTION
 CONNECTION.

Prior to v15, GUC settings supplied in the CONNECTION clause of
CREATE SUBSCRIPTION were correctly passed through to
the publisher's walsender. For example:

        CREATE SUBSCRIPTION mysub
            CONNECTION 'options=''-c wal_sender_timeout=1000'''
            PUBLICATION ...

would cause wal_sender_timeout to take effect on the publisher's walsender.

However, commit f3d4019da5d changed the way logical replication
connections are established, forcing the publisher's relevant
GUC settings (datestyle, intervalstyle, extra_float_digits) to
override those provided in the CONNECTION string. As a result,
from v15 through v18, GUC settings in the CONNECTION string were
always ignored.

This regression prevented per-connection tuning of logical replication.
For example, using a shorter timeout for walsender connecting
to a nearby subscriber and a longer one for walsender connecting
to a remote subscriber.

This commit restores the intended behavior by ensuring that
GUC settings in the CONNECTION string are again passed through
and applied by the walsender, allowing per-connection configuration.

Backpatch to v15, where the regression was introduced.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Chao Li <lic@highgo.com>
Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Reviewed-by: Japin Li <japinli@hotmail.com>
Discussion: https://postgr.es/m/CAHGQGwGYV+-abbKwdrM2UHUe-JYOFWmsrs6=QicyJO-j+-Widw@mail.gmail.com
Backpatch-through: 15
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 43 +++++++++++++++----
 1 file changed, 34 insertions(+), 9 deletions(-)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 5ddc9e812e7..c067908da35 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -65,6 +65,8 @@ static void libpqrcv_get_senderinfo(WalReceiverConn *conn,
 static char *libpqrcv_identify_system(WalReceiverConn *conn,
 									  TimeLineID *primary_tli);
 static char *libpqrcv_get_dbname_from_conninfo(const char *connInfo);
+static char *libpqrcv_get_option_from_conninfo(const char *connInfo,
+											   const char *keyword);
 static int	libpqrcv_server_version(WalReceiverConn *conn);
 static void libpqrcv_readtimelinehistoryfile(WalReceiverConn *conn,
 											 TimeLineID tli, char **filename,
@@ -150,6 +152,7 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 	const char *keys[6];
 	const char *vals[6];
 	int			i = 0;
+	char	   *options_val = NULL;
 
 	/*
 	 * Re-validate connection string. The validation already happened at DDL
@@ -177,6 +180,8 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 
 		if (logical)
 		{
+			char	   *opt = NULL;
+
 			/* Tell the publisher to translate to our encoding */
 			keys[++i] = "client_encoding";
 			vals[i] = GetDatabaseEncodingName();
@@ -189,8 +194,13 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 			 * running in the subscriber, such as triggers.)  This should
 			 * match what pg_dump does.
 			 */
+			opt = libpqrcv_get_option_from_conninfo(conninfo, "options");
+			options_val = psprintf("%s -c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3",
+								   (opt == NULL) ? "" : opt);
 			keys[++i] = "options";
-			vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
+			vals[i] = options_val;
+			if (opt != NULL)
+				pfree(opt);
 		}
 		else
 		{
@@ -217,6 +227,9 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 								 /* expand_dbname = */ true,
 								WAIT_EVENT_LIBPQWALRECEIVER_CONNECT);
 
+	if (options_val != NULL)
+		pfree(options_val);
+
 	if (PQstatus(conn->streamConn) != CONNECTION_OK)
 		goto bad_connection_errmsg;
 
@@ -460,9 +473,21 @@ libpqrcv_server_version(WalReceiverConn *conn)
  */
 static char *
 libpqrcv_get_dbname_from_conninfo(const char *connInfo)
+{
+	return libpqrcv_get_option_from_conninfo(connInfo, "dbname");
+}
+
+/*
+ * Get the value of the option with the given keyword from the primary
+ * server's conninfo.
+ *
+ * If the option is not found in connInfo, return NULL value.
+ */
+static char *
+libpqrcv_get_option_from_conninfo(const char *connInfo, const char *keyword)
 {
 	PQconninfoOption *opts;
-	char	   *dbname = NULL;
+	char	   *option = NULL;
 	char	   *err = NULL;
 
 	opts = PQconninfoParse(connInfo, &err);
@@ -480,21 +505,21 @@ libpqrcv_get_dbname_from_conninfo(const char *connInfo)
 	for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
 	{
 		/*
-		 * If multiple dbnames are specified, then the last one will be
-		 * returned
+		 * If the same option appears multiple times, then the last one will
+		 * be returned
 		 */
-		if (strcmp(opt->keyword, "dbname") == 0 && opt->val &&
+		if (strcmp(opt->keyword, keyword) == 0 && opt->val &&
 			*opt->val)
 		{
-			if (dbname)
-				pfree(dbname);
+			if (option)
+				pfree(option);
 
-			dbname = pstrdup(opt->val);
+			option = pstrdup(opt->val);
 		}
 	}
 
 	PQconninfoFree(opts);
-	return dbname;
+	return option;
 }
 
 /*
-- 
2.51.2

#34Fujii Masao
masao.fujii@gmail.com
In reply to: Amit Kapila (#29)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Tue, Dec 23, 2025 at 2:05 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

Thanks, the idea looks good now. One minor comment:

Thanks for the review!

}
+
/*

This appears to be an unnecessary addition in the patch.

The added blank line is due to pgindent, so I didn't exclude it from the patch.
I'm not sure what the preferred practice is for handling pgindent changes
in older branches, but in general it seems better to keep the code clean
and pgindent-compliant even there. Thoughts?

Regards,

--
Fujii Masao

#35Amit Kapila
amit.kapila16@gmail.com
In reply to: Fujii Masao (#34)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Thu, Dec 25, 2025 at 12:31 PM Fujii Masao <masao.fujii@gmail.com> wrote:

On Tue, Dec 23, 2025 at 2:05 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

Thanks, the idea looks good now. One minor comment:

Thanks for the review!

}
+
/*

This appears to be an unnecessary addition in the patch.

The added blank line is due to pgindent, so I didn't exclude it from the patch.

Fair enough.

I'm not sure what the preferred practice is for handling pgindent changes
in older branches, but in general it seems better to keep the code clean
and pgindent-compliant even there.

+1.

--
With Regards,
Amit Kapila.

#36Fujii Masao
masao.fujii@gmail.com
In reply to: Fujii Masao (#33)
Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect

On Thu, Dec 25, 2025 at 4:00 PM Fujii Masao <masao.fujii@gmail.com> wrote:

OK, I've updated the 0002 patch accordingly.

I've pushed the patches. Thanks all!

Regards,

--
Fujii Masao