Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

Поиск
Список
Период
Сортировка
Искать

Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Chao Li <li.evan.chao@gmail.com>
Дата:
Вложения:
Hi,

I tested the v19 new feature CREATE SUBSCRIPTION ... SERVER yesterday, and found an issue: once the old server becomes broken, the subscription cannot be recovered by switching it to a good server.

Here is a repro:
```
evantest=# CREATE EXTENSION postgres_fdw;
CREATE EXTENSION

# Create two servers
evantest=# CREATE SERVER old_srv FOREIGN DATA WRAPPER postgres_fdw
evantest-# OPTIONS (host '127.0.0.1', dbname 'postgres', port '5432');
CREATE SERVER
evantest=# CREATE SERVER new_srv FOREIGN DATA WRAPPER postgres_fdw
evantest-# OPTIONS (host '127.0.0.1', dbname 'postgres', port '5432');
CREATE SERVER
evantest=# CREATE USER MAPPING FOR CURRENT_USER SERVER old_srv
evantest-# OPTIONS (user 'dummy', password 'dummy');
CREATE USER MAPPING
evantest=# CREATE USER MAPPING FOR CURRENT_USER SERVER new_srv
evantest-# OPTIONS (user 'dummy', password 'dummy');
CREATE USER MAPPING

# Add old_server to a subscription
evantest=# CREATE SUBSCRIPTION sub_bug SERVER old_srv
evantest-# PUBLICATION pub_does_not_matter
evantest-# WITH (connect = false, slot_name = NONE);
WARNING:  subscription was created, but is not connected
HINT:  To initiate replication, you must manually create the replication slot, enable the subscription, and alter the subscription to refresh publications.
CREATE SUBSCRIPTION

# Break old_srv
evantest=# DROP USER MAPPING FOR CURRENT_USER SERVER old_srv;
DROP USER MAPPING

# Fail to switch to a good server because old_srv is broken
evantest=# ALTER SUBSCRIPTION sub_bug SERVER new_srv;
ERROR:  user mapping not found for user "chaol", server "old_srv"
evantest=# ALTER SUBSCRIPTION sub_bug CONNECTION 'host=127.0.0.1 dbname=postgres port=5432 user=dummy password=dummy';
ERROR:  user mapping not found for user "chaol", server "old_srv"
```

In AlterSubscription(), this comment made me think this is a bug:
```
	/*
	 * Skip ACL checks on the subscription's foreign server, if any. If
	 * changing the server (or replacing it with a raw connection), then the
	 * old one will be removed anyway. If changing something unrelated,
	 * there's no need to do an additional ACL check here; that will be done
	 * by the subscription worker anyway.
	 */
	sub = GetSubscription(subid, false, false);
```

The comment explicitly says to skip ACL checks on the old server because it will be removed anyway.

But after looking into GetSubscription(), I found that when the aclcheck parameter is false, it still calls ForeignServerConnectionString(). I think that is the root cause of the bug.

To fix this, I worked out a solution that stores the server OID in Subscription, and only calls ForeignServerConnectionString()lazily when sub->conninfo is actually needed. I also added a new test case to cover this scenario. Without the fix, the new test fails.
See attached patch for details.

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



RE: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Dear Chao,

> I tested the v19 new feature CREATE SUBSCRIPTION ... SERVER yesterday, and
> found an issue: once the old server becomes broken, the subscription cannot be
> recovered by switching it to a good server.

Thanks for testing. I could reproduce the same issue. In addition to yours, I found
DROP SUBSCRIPTION cannot be done anymore. To switch the connection or drop it,
I had to create the same user mapping must be created again.

```
postgres=# DROP SUBSCRIPTION sub_bug ;
ERROR:  user mapping not found for user "postgres", server "old_srv"
postgres=# CREATE USER MAPPING FOR CURRENT_USER SERVER old_srv
OPTIONS (user 'dummy', password 'dummy');
CREATE USER MAPPING
postgres=# DROP SUBSCRIPTION sub_bug ;
DROP SUBSCRIPTION
```

Before deep dive to your fix, I'm unclear why dropping the active USER MAPPING is
allowed. Personally, it should be avoided anyway. Do you know why it's not restricted?

Best regards,
Hayato Kuroda
FUJITSU LIMITED



Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Chao Li <li.evan.chao@gmail.com>
Дата:


> On Apr 22, 2026, at 20:35, Hayato Kuroda (Fujitsu)  wrote:
> 
> Dear Chao,
> 
>> I tested the v19 new feature CREATE SUBSCRIPTION ... SERVER yesterday, and
>> found an issue: once the old server becomes broken, the subscription cannot be
>> recovered by switching it to a good server.
> 
> Thanks for testing. I could reproduce the same issue. In addition to yours, I found
> DROP SUBSCRIPTION cannot be done anymore. To switch the connection or drop it,
> I had to create the same user mapping must be created again.
> 
> ```
> postgres=# DROP SUBSCRIPTION sub_bug ;
> ERROR:  user mapping not found for user "postgres", server "old_srv"
> postgres=# CREATE USER MAPPING FOR CURRENT_USER SERVER old_srv
> OPTIONS (user 'dummy', password 'dummy');
> CREATE USER MAPPING
> postgres=# DROP SUBSCRIPTION sub_bug ;
> DROP SUBSCRIPTION
> ```
> 
> Before deep dive to your fix, I'm unclear why dropping the active USER MAPPING is
> allowed. Personally, it should be avoided anyway. Do you know why it's not restricted?
> 
> Best regards,
> Hayato Kuroda
> FUJITSU LIMITED
> 

Hi Hayato-san,

There is an existing test case in subscription.sql:
```
-- fail, must connect but lacks USAGE on server, as well as user mapping
DROP SUBSCRIPTION regress_testsub6;
```

So, I guess that’s an intentional behavior. You have to fix the broken server or switch to a good one before dropping the subscription. That’s my understanding from the test cases.

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






Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Ajin Cherian <itsajin@gmail.com>
Дата:
On Wed, Apr 22, 2026 at 11:52 AM Chao Li  wrote:
>
> Hi,
>
> The comment explicitly says to skip ACL checks on the old server because it will be removed anyway.
>
> But after looking into GetSubscription(), I found that when the aclcheck parameter is false, it still calls ForeignServerConnectionString(). I think that is the root cause of the bug.
>
> To fix this, I worked out a solution that stores the server OID in Subscription, and only calls ForeignServerConnectionString()lazily when sub->conninfo is actually needed. I also added a new test case to cover this scenario. Without the fix, the new test fails.
> See attached patch for details.
>
Hi Li,

Thanks for the patch.
Some comments:

1.
             if (aclresult != ACLCHECK_OK)
                 ereport(ERROR,
-                        (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
-                         errmsg("subscription owner \"%s\" does not
have permission on foreign server \"%s\"",
-                                GetUserNameFromId(subform->subowner, false),
-                                server->servername)));
+                        errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+                        errmsg("subscription owner \"%s\" does not
have permission on foreign server \"%s\"",
+                               GetUserNameFromId(subform->subowner, false),
+                               server->servername));
+            sub->conninfo = ForeignServerConnectionString(subform->subowner,
+                                                          server);
         }

Add a new line before the call to ForeignServerConnectionString(),
also I think you should put the if condition within curly brackets
because it spans more than one line and might confuse developers while
adding new code.

2. I think you should add a comment in the function header above
GetSubscription() stating that if aclcheck is false, then the conninfo
will be set to null and users need to call GetSubscriptionConnInfo to
get the conninfo.

3.
 Datum
 test_fdw_connection(PG_FUNCTION_ARGS)
 {
+    GetUserMapping(PG_GETARG_OID(0), PG_GETARG_OID(1));
     PG_RETURN_TEXT_P(cstring_to_text("dbname=regress_doesnotexist
user=doesnotexist password=secret"));
 }

Add a comment above this change describing why it's required.

regards,
Ajin Cherian
Fujitsu Australia


Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Chao Li <li.evan.chao@gmail.com>
Дата:
Вложения:


> On Apr 29, 2026, at 12:44, Ajin Cherian  wrote:
> 
> On Wed, Apr 22, 2026 at 11:52 AM Chao Li  wrote:
>> 
>> Hi,
>> 
>> The comment explicitly says to skip ACL checks on the old server because it will be removed anyway.
>> 
>> But after looking into GetSubscription(), I found that when the aclcheck parameter is false, it still calls ForeignServerConnectionString(). I think that is the root cause of the bug.
>> 
>> To fix this, I worked out a solution that stores the server OID in Subscription, and only calls ForeignServerConnectionString()lazily when sub->conninfo is actually needed. I also added a new test case to cover this scenario. Without the fix, the new test fails.
>> See attached patch for details.
>> 
> Hi Li,
> 
> Thanks for the patch.

Thanks for your review.

> Some comments:
> 
> 1.
>             if (aclresult != ACLCHECK_OK)
>                 ereport(ERROR,
> -                        (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
> -                         errmsg("subscription owner \"%s\" does not
> have permission on foreign server \"%s\"",
> -                                GetUserNameFromId(subform->subowner, false),
> -                                server->servername)));
> +                        errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
> +                        errmsg("subscription owner \"%s\" does not
> have permission on foreign server \"%s\"",
> +                               GetUserNameFromId(subform->subowner, false),
> +                               server->servername));
> +            sub->conninfo = ForeignServerConnectionString(subform->subowner,
> +                                                          server);
>         }
> 
> Add a new line before the call to ForeignServerConnectionString(),

Okay.

> also I think you should put the if condition within curly brackets
> because it spans more than one line and might confuse developers while
> adding new code.

I don’t think curly brackets are needed as there is only one statement under the if though the statement spans multiple lines. There are plenty of examples in the existing code, for example:
```
	if (!wrconn)
		ereport(ERROR,
				errcode(ERRCODE_CONNECTION_FAILURE),
				errmsg("subscription \"%s\" could not connect to the publisher: %s",
					   sub->name, err));
```

> 
> 2. I think you should add a comment in the function header above
> GetSubscription() stating that if aclcheck is false, then the conninfo
> will be set to null and users need to call GetSubscriptionConnInfo to
> get the conninfo.

Updated the header comment.

> 
> 3.
> Datum
> test_fdw_connection(PG_FUNCTION_ARGS)
> {
> +    GetUserMapping(PG_GETARG_OID(0), PG_GETARG_OID(1));
>     PG_RETURN_TEXT_P(cstring_to_text("dbname=regress_doesnotexist
> user=doesnotexist password=secret"));
> }
> 
> Add a comment above this change describing why it's required.
> 

Added the comment.

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




Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Ajin Cherian <itsajin@gmail.com>
Дата:
On Thu, Apr 30, 2026 at 2:12 PM Chao Li  wrote:
>
>
>
> >
> > 2. I think you should add a comment in the function header above
> > GetSubscription() stating that if aclcheck is false, then the conninfo
> > will be set to null and users need to call GetSubscriptionConnInfo to
> > get the conninfo.
>
> Updated the header comment.

 /*
  * Fetch the subscription from the syscache.
+ *
+ * If missing_ok is false, throw an error if the subscription is not found.
+ * If true, return NULL in that case.
+ *
+ * If aclcheck is true, check whether the subscription owner has permission on
+ * the subscription's foreign server, and load the connection string from the
+ * foreign server. Later, GetSubscriptionConnInfo() should be called to get
+ * the connection string.
  */
 Subscription *
 GetSubscription(Oid subid, bool missing_ok, bool aclcheck)

I don't think this comment is right. If aclcheck is true, users need
not call GetSubscriptionConnInfo() to get the connection string, as it
is populated in this function itself. It is only if aclecheck is
false, do callers need to do that.
Isn't that the case? There are multiple places in the apply worker
where GetSubscription() is called with aclcheck is true and
GetSubscriptionConnInfo() is not called there.

regards,
Ajin Cherian
Fujitsu Australia


Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Hello

- server = GetForeignServer(subform->subserver);
+ server = GetForeignServer(sub->serverid);

Couldn't we also move this inside the if?

+/*
+ * Return the subscription's connection string, loading it into the
+ * subscription memory context if necessary.
+ *
+ * GetSubscription must be called earlier to set sub->serverid, because ACL
+ * checks are performed there.
+ */
+char *
+GetSubscriptionConnInfo(Subscription *sub)

This is related to Ajin's comment earlier, the part about ACL check
seems incorrect to me.


Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Chao Li <li.evan.chao@gmail.com>
Дата:


> On May 1, 2026, at 11:58, Ajin Cherian  wrote:
> 
> On Thu, Apr 30, 2026 at 2:12 PM Chao Li  wrote:
>> 
>> 
>> 
>>> 
>>> 2. I think you should add a comment in the function header above
>>> GetSubscription() stating that if aclcheck is false, then the conninfo
>>> will be set to null and users need to call GetSubscriptionConnInfo to
>>> get the conninfo.
>> 
>> Updated the header comment.
> 
> /*
>  * Fetch the subscription from the syscache.
> + *
> + * If missing_ok is false, throw an error if the subscription is not found.
> + * If true, return NULL in that case.
> + *
> + * If aclcheck is true, check whether the subscription owner has permission on
> + * the subscription's foreign server, and load the connection string from the
> + * foreign server. Later, GetSubscriptionConnInfo() should be called to get
> + * the connection string.
>  */
> Subscription *
> GetSubscription(Oid subid, bool missing_ok, bool aclcheck)
> 
> I don't think this comment is right. If aclcheck is true, users need
> not call GetSubscriptionConnInfo() to get the connection string, as it
> is populated in this function itself. It is only if aclecheck is
> false, do callers need to do that.
> Isn't that the case? There are multiple places in the apply worker
> where GetSubscription() is called with aclcheck is true and
> GetSubscriptionConnInfo() is not called there.
> 
> regards,
> Ajin Cherian
> Fujitsu Australia

Hi Ajin,

Thank you for the comment. After rereading that part, I agree the wording is not clear.

What I meant is that GetSubscriptionConnInfo() is a safe accessor, if sub->conninfo has already been resolved, it just returns it; otherwise, it resolves it on demand. So the intended usage is that callers can consistently use GetSubscriptionConnInfo() when they need the connection string.

I also missed doing a broader search for direct uses of sub->conninfo and replacing them with GetSubscriptionConnInfo() where appropriate. Sorry about that.

I will address both issues in v3.

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






Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Chao Li <li.evan.chao@gmail.com>
Дата:
Вложения:


> On May 6, 2026, at 04:53, Zsolt Parragi  wrote:
> 
> Hello
> 
> - server = GetForeignServer(subform->subserver);
> + server = GetForeignServer(sub->serverid);
> 
> Couldn't we also move this inside the if?

Ah, true. Both aclresult and server can be moved into the if.

> 
> +/*
> + * Return the subscription's connection string, loading it into the
> + * subscription memory context if necessary.
> + *
> + * GetSubscription must be called earlier to set sub->serverid, because ACL
> + * checks are performed there.
> + */
> +char *
> +GetSubscriptionConnInfo(Subscription *sub)
> 
> This is related to Ajin's comment earlier, the part about ACL check
> seems incorrect to me.

Yes, see my reply to Ajin in the previous email.

PFA v3 - addressed Ajin and Zsolt’s comments.

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




Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Jeff Davis <pgsql@j-davis.com>
Дата:
On Wed, 2026-04-22 at 12:35 +0000, Hayato Kuroda (Fujitsu) wrote:
> Before deep dive to your fix, I'm unclear why dropping the active
> USER MAPPING is
> allowed. Personally, it should be avoided anyway. Do you know why
> it's not restricted?

We don't record dependencies on user mappings because technically it's
not dependent on a specific user mapping. That is, GetUserMapping() can
fall back to the public user mapping.

Regards,
	Jeff Davis



Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Jeff Davis <pgsql@j-davis.com>
Дата:
Вложения:
On Tue, 2026-05-26 at 17:44 -0700, Amit Kapila wrote:
> > Yep, this part looks good now.

Thank you. I committed 0001 with one additional fix: make sure that
ALTER SUBSCRIPTION DISABLE doesn't try to construct the old conninfo.

I plan to commit 0002 soon.

> > 
> > For the reason you described in the commit message, catching the
> > error and later reporting it through ReportSlotConnectionError(), I
> > don't think this is over-engineered. I also think keeping the
> > subtransaction inside construct_subserver_conninfo() is reasonable,
> > because this error-capturing behavior seems specific to the DROP
> > SUBSCRIPTION path.

OK.

> > As for whether the HINT itself really helps, I would reserve my
> > opinion. As the test output shows:
> > ```
> > DROP SUBSCRIPTION regress_testsub6;
> > ERROR:  could not connect to publisher when attempting to drop
> > replication slot "dummy": user mapping not found for user
> > "regress_subscription_user3", server "test_server"
> > HINT:  Use ALTER SUBSCRIPTION ... DISABLE to disable the
> > subscription, and then use ALTER SUBSCRIPTION ... SET (slot_name =
> > NONE) to disassociate it from the slot.
> > ```
> > 
> > The error message already says that the problem is “user mapping
> > not found”, so fixing the user mapping could also be a solution.
> > So, the HINT is still useful, but it might not be the most direct
> > fix in some case.
> > 
> 
> Right, the hint doesn't sound to be a right solution for the problem
> reported.

The user might no longer have permissions on the server to create a new
user mapping, so if they want to drop the subscription, then they need
to set slot_name=NONE.

In other words, we can't delete the existing hint language; we can only
add new language about adding the user mapping back. I don't see a
great way to add that language without making it too long and
confusing, but I am open to suggestion.

v7-0003 is more than just an error message change, so I updated the
commit message. It handles the case where the drop path has
rstates!=NIL but slot_name=NONE: without 0003, the drop will fail while
trying to construct the conninfo (without the HINT); with 0003, the
drop will silently succeed without removing the tablesync slots (as it
does with a connection failure when subconninfo is set).

If the use of a subtransaction is fine there, then I think we should
proceed with 0003 and whatever hint message is agreeable.

Regards,
	Jeff Davis

Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Jeff Davis <pgsql@j-davis.com>
Дата:
On Wed, 2026-06-17 at 16:10 -0700, Jeff Davis wrote:
> v7-0003 is more than just an error message change, so I updated the
> commit message. It handles the case where the drop path has
> rstates!=NIL but slot_name=NONE: without 0003, the drop will fail
> while
> trying to construct the conninfo (without the HINT); with 0003, the
> drop will silently succeed without removing the tablesync slots (as
> it
> does with a connection failure when subconninfo is set).
> 
> If the use of a subtransaction is fine there, then I think we should
> proceed with 0003 and whatever hint message is agreeable.

0002 committed.

For 0003, I don't think we can absorb every kind of error that might
happen inside fdwconnection. libpqrcv_connect() doesn't attempt to do
so, so neither should construct_subserver_conninfo().

What 0003 is really trying to avoid is fairly normal kinds of errors
that can happen on a non-broken FDW that get in the way of dropping a
subscription. That includes a missing user mapping or a failed ACL
check. But aside from that, it's hard to think of other examples we'd
clearly want to absorb.

Options:

  * check for those two error codes explicitly
  * try to be more systematic about what kinds of errors should be
absorbed or not
  * add an escape hatch for users to turn off tablesync slots so that
DROP will always succeed
  * consider it an unimportant edge case and leave it the way it is
(with 0001 & 0002 already done), and close the open item

Thoughts?

Regards,
	Jeff Davis



Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Jeff Davis <pgsql@j-davis.com>
Дата:
On Fri, 2026-06-19 at 11:40 -0700, Jeff Davis wrote:
>   * add an escape hatch for users to turn off tablesync slots so that
> DROP will always succeed

It looks like SLOT_NAME=NONE is already supposed to be this escape
hatch, even for tablesync slots. From the docs:

"To proceed in this situation, first disable the subscription by
executing ALTER SUBSCRIPTION ... DISABLE, and then disassociate it from
the replication slot by executing ALTER SUBSCRIPTION ... SET (slot_name
= NONE). After that, DROP SUBSCRIPTION will no longer attempt any
actions on a remote host."

https://www.postgresql.org/docs/devel/sql-dropsubscription.html

But DropSubscription() only does the early-return if there are no
tablesync slots. If there are tablesync slots, it still tries to
contact the publisher, even if SLOT_NAME=NONE.

>   * consider it an unimportant edge case and leave it the way it is
> (with 0001 & 0002 already done), and close the open item

I plan to close this open item, and treat the above as a pre-existing
bug, which may require a backport.

Regards,
	Jeff Davis



RE: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Dear Jeff,

I went through the 0003 patch. Apart from HINT message, I found that error code
ERRCODE_CONNECTION_FAILURE is always used. Should we report the original errcode
of the root cause? Or it's OK because the observed outcome is the cannot-connect
error?

Another comment:


```
+               conninfo = ForeignServerConnectionString(subowner, server);
+               Assert(conninfo != NULL);
```

Do we have to consider the case that ForeignServerConnectionString() returns NULL
without any ERRORs? For the production build, the Assert() would be skipped and
the segmentation fault would happen in the `if (!conninfo)` part.
I checked the doc [1] but there are no specifications for it.

[1]: https://www.postgresql.org/docs/devel/sql-createforeigndatawrapper.html#:~:text=ereport(ERROR)%20function.-,CONNECTION%20connection_function,-connection_function%20is%20the

Best regards,
Hayato Kuroda
FUJITSU LIMITED

Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Tue, Jun 23, 2026 at 2:40 AM Jeff Davis  wrote:
>
> On Fri, 2026-06-19 at 11:40 -0700, Jeff Davis wrote:
> >   * add an escape hatch for users to turn off tablesync slots so that
> > DROP will always succeed
>
> It looks like SLOT_NAME=NONE is already supposed to be this escape
> hatch, even for tablesync slots. From the docs:
>
> "To proceed in this situation, first disable the subscription by
> executing ALTER SUBSCRIPTION ... DISABLE, and then disassociate it from
> the replication slot by executing ALTER SUBSCRIPTION ... SET (slot_name
> = NONE). After that, DROP SUBSCRIPTION will no longer attempt any
> actions on a remote host."
>
> https://www.postgresql.org/docs/devel/sql-dropsubscription.html
>
> But DropSubscription() only does the early-return if there are no
> tablesync slots. If there are tablesync slots, it still tries to
> contact the publisher, even if SLOT_NAME=NONE.
>

AFAICS, the corresponding DropSubscription() code is as follows:

if (!slotname && rstates == NIL)
{
table_close(rel, NoLock);
return;
}
....
if (OidIsValid(subserver))
conninfo = construct_subserver_conninfo(subserver, subowner, &err);
else
conninfo = subconninfo;

if (conninfo)
wrconn = walrcv_connect(conninfo, true, true, must_use_password,
subname, &err);

if (wrconn == NULL)
{
if (!slotname)
{
/* be tidy */
list_free(rstates);
table_close(rel, NoLock);
return;
}
else
{
ReportSlotConnectionError(rstates, subid, slotname, err);
}
}

So, if the connection is not successful then we simply return without
checking tablesync slots (when slotname is NONE) whereas with FDW
server connection, the ERROR will be raised from
construct_subserver_conninfo->ForeignServerConnectionString(). Isn't
that different from current situation?

> >   * consider it an unimportant edge case and leave it the way it is
> > (with 0001 & 0002 already done), and close the open item
>
> I plan to close this open item, and treat the above as a pre-existing
> bug, which may require a backport.
>

As per my understanding it is better to go with your fix idea even
though we don't have better ideas for HINT message. I see your point
of making HINT much longer as we may need to add something like (...
or re-create the user mapping/ACL and retry..), so we can keep the
current one as it is and if we see any real user complaint then we can
consider improving it in the future.

-- 
With Regards,
Amit Kapila.


Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Wed, Jun 24, 2026 at 7:48 AM Hayato Kuroda (Fujitsu)
 wrote:
>
> I went through the 0003 patch. Apart from HINT message, I found that error code
> ERRCODE_CONNECTION_FAILURE is always used. Should we report the original errcode
> of the root cause?
>

That could be marginally better but not sure if it is worth the
complexity. I have a few more points regarding 0003:
* Regarding the point: "we can't absorb every kind of error." In the
PG_CATCH, re-throw query-cancel / interrupt-class conditions (if those
are possible) instead of swallowing them, otherwise a SIGINT during
ForeignServerConnectionString() becomes a silent successful DROP (for
slot_name=NONE) or a mislabeled connection error.

*
+ if (aclresult != ACLCHECK_OK)
+ ereport(ERROR,
+ errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ errmsg("subscription owner \"%s\" does not have permission on
foreign server \"%s\"",
+    GetUserNameFromId(subowner, false),
+    server->servername));

I am not sure if this is a good idea as the outer catch will anyway
silence this.

-- 
With Regards,
Amit Kapila.


Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Jeff Davis <pgsql@j-davis.com>
Дата:
On Mon, 2026-06-29 at 10:07 +0530, Amit Kapila wrote:
> On Wed, Jun 24, 2026 at 7:48 AM Hayato Kuroda (Fujitsu)
>  wrote:
> > 
> > I went through the 0003 patch. Apart from HINT message, I found
> > that error code
> > ERRCODE_CONNECTION_FAILURE is always used. Should we report the
> > original errcode
> > of the root cause?
> > 
> 
> That could be marginally better but not sure if it is worth the
> complexity. I have a few more points regarding 0003:
> * Regarding the point: "we can't absorb every kind of error." In the
> PG_CATCH, re-throw query-cancel / interrupt-class conditions (if
> those
> are possible) instead of swallowing them, otherwise a SIGINT during
> ForeignServerConnectionString() becomes a silent successful DROP (for
> slot_name=NONE) or a mislabeled connection error.

The DROP SUBSCRIPTION documentation says that:

"To proceed in this situation, first disable the subscription by
executing ALTER SUBSCRIPTION ... DISABLE, and then disassociate it from
the replication slot by executing ALTER SUBSCRIPTION ... SET (slot_name
= NONE). After that, DROP SUBSCRIPTION will no longer attempt any
actions on a remote host."

But that stopped being true after commit ce0fdbfe97. There should be
some kind of escape such that we can force a DROP SUBSCRIPTION to
succeed without trying to connect to the remote host. And if we have
that escape, then we don't need to overcomplicate this by using a
subtransaction and trying to guess which errors are recoverable or not.

Regards,
	Jeff Davis



Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Jeff Davis <pgsql@j-davis.com>
Дата:
On Mon, 2026-06-29 at 09:57 +0530, Amit Kapila wrote:
> wrconn = walrcv_connect(conninfo, true, true, must_use_password,
> subname, &err);

...

> So, if the connection is not successful then we simply return without
> checking tablesync slots (when slotname is NONE) whereas with FDW
> server connection, the ERROR will be raised from
> construct_subserver_conninfo->ForeignServerConnectionString(). Isn't
> that different from current situation?

It's easier to cause an error in ForeignServerConnectionString(); but
walrcv_connect() can throw errors, too. Also, a network connection is a
side-effect that you may not want to have for whatever reason.

I think it's better to have an escape that prevents any connection to
the publisher, as the documentation says, to deal with these kinds of
edge cases.

Regards,
	Jeff Davis



Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Jeff Davis <pgsql@j-davis.com>
Дата:
On Wed, 2026-07-08 at 10:29 +0530, Amit Kapila wrote:
> Okay, I see your point and we can make it return early when slot_name
> is NONE. However, do we need to consider a case where users want to
> manually manage the subscription's main slot, so it uses
> slot_name=NONE? I mean users want to manually maintain the lifecycle
> of the slot and due to that even during the CREATE SUBSCRIPTION, it
> uses the specified slot_name instead of the default. But still
> tablesync slots need to be removed in such a case as those are
> internally created.

The problem is that we are overloading slot_name=NONE, and as you point
out, it can mean multiple things.

Can we just have DROP SUBSCRIPTION ... FORCE or DROP SUBSCRIPTION ...
NOCONNECT? That allows the user to clearly express their intent at the
time of the drop, rather than guessing from the state of the
subscription.

If it's too late to do that for 19, we should probably just make the
code match the documentation and don't try to connect if
slot_name=NONE. In most ordinary cases that would not cause a problem,
you'd have to have slot_name=NONE and also drop the subscription before
the table syncs finish.

Regards,
	Jeff Davis



Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Thu, Jul 9, 2026 at 12:50 AM Jeff Davis  wrote:
>
> On Wed, 2026-07-08 at 10:29 +0530, Amit Kapila wrote:
> > Okay, I see your point and we can make it return early when slot_name
> > is NONE. However, do we need to consider a case where users want to
> > manually manage the subscription's main slot, so it uses
> > slot_name=NONE? I mean users want to manually maintain the lifecycle
> > of the slot and due to that even during the CREATE SUBSCRIPTION, it
> > uses the specified slot_name instead of the default. But still
> > tablesync slots need to be removed in such a case as those are
> > internally created.
>
> The problem is that we are overloading slot_name=NONE, and as you point
> out, it can mean multiple things.
>
> Can we just have DROP SUBSCRIPTION ... FORCE or DROP SUBSCRIPTION ...
> NOCONNECT? That allows the user to clearly express their intent at the
> time of the drop, rather than guessing from the state of the
> subscription.
>

That sounds like an appropriate solution or we can use (connect =
false) option as well a way to indicate NOCONNECT.

> If it's too late to do that for 19,
>

I feel it is late for 19.

 we should probably just make the
> code match the documentation and don't try to connect if
> slot_name=NONE. In most ordinary cases that would not cause a problem,
> you'd have to have slot_name=NONE and also drop the subscription before
> the table syncs finish.
>

Yeah, we can do this but if we want to change this behaviour in the
next version, isn't it better to tweak the doc wording as:"After that,
DROP SUBSCRIPTION will not attempt to drop the subscription's own
replication slot. It may still connect to the publisher to drop
internally-created table synchronization slots if some table
synchronization was left unfinished; if the publisher is unreachable,
those slots (and the main slot, if it still exists) must be dropped
manually.". I think in most cases where we need this is where the
publisher is not available or the network is broken and in those
cases, the current code still works and won't leave any tablesync
slots.

-- 
With Regards,
Amit Kapila.


Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Sat, Jul 11, 2026 at 8:42 PM Jeff Davis  wrote:
>
> On Thu, 2026-07-09 at 12:47 +0530, Amit Kapila wrote:
> > That sounds like an appropriate solution or we can use (connect =
> > false) option as well a way to indicate NOCONNECT.
>
> That doesn't have a separate state in the catalog, so DROP SUBSCRIPTION
> doesn't know that it was specified.
>

I was imagining doing something what CREATE SUBSCRIPTION doc describes
for this option: "Specifies whether the CREATE SUBSCRIPTION command
should connect to the publisher at all.". I mean some sort of WITH
(connect = false) but on re-thinking, for DROP it is better to use a
clause like NOCONNECT instead of providing options. Anyway, we can
brainstorm on the exact syntax in a separate thread for this work.

> > Yeah, we can do this but if we want to change this behaviour in the
> > next version, isn't it better to tweak the doc wording as:"After
> > that,
> > DROP SUBSCRIPTION will not attempt to drop the subscription's own
> > replication slot. It may still connect to the publisher to drop
> > internally-created table synchronization slots if some table
> > synchronization was left unfinished; if the publisher is unreachable,
> > those slots (and the main slot, if it still exists) must be dropped
> > manually.".
>
> Right, we can make the documentation match the code instead of making
> the code match the documentation. I don't have a strong opinion which
> way to go. But the wording above is fine with me.
>

Fair enough. So, I'll commit this to HEAD and backbaptch till PG14.

> > I think in most cases where we need this is where the
> > publisher is not available or the network is broken and in those
> > cases, the current code still works and won't leave any tablesync
> > slots.
>
> If it can't connnect, then the tablesync slots could still be left
> behind, right?
>

Yes. I think I wanted to say in such situations "the DROP completes,
but the tablesync slots are left on the publisher for manual
cleanup.". Sorry for confusion.

-- 
With Regards,
Amit Kapila.


Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
Вложения:
On Mon, Jul 13, 2026 at 11:21 AM Amit Kapila  wrote:
>
> On Sat, Jul 11, 2026 at 8:42 PM Jeff Davis  wrote:
> >
> > On Thu, 2026-07-09 at 12:47 +0530, Amit Kapila wrote:
> > > That sounds like an appropriate solution or we can use (connect =
> > > false) option as well a way to indicate NOCONNECT.
> >
> > That doesn't have a separate state in the catalog, so DROP SUBSCRIPTION
> > doesn't know that it was specified.
> >
>
> I was imagining doing something what CREATE SUBSCRIPTION doc describes
> for this option: "Specifies whether the CREATE SUBSCRIPTION command
> should connect to the publisher at all.". I mean some sort of WITH
> (connect = false) but on re-thinking, for DROP it is better to use a
> clause like NOCONNECT instead of providing options. Anyway, we can
> brainstorm on the exact syntax in a separate thread for this work.
>
> > > Yeah, we can do this but if we want to change this behaviour in the
> > > next version, isn't it better to tweak the doc wording as:"After
> > > that,
> > > DROP SUBSCRIPTION will not attempt to drop the subscription's own
> > > replication slot. It may still connect to the publisher to drop
> > > internally-created table synchronization slots if some table
> > > synchronization was left unfinished; if the publisher is unreachable,
> > > those slots (and the main slot, if it still exists) must be dropped
> > > manually.".
> >
> > Right, we can make the documentation match the code instead of making
> > the code match the documentation. I don't have a strong opinion which
> > way to go. But the wording above is fine with me.
> >
>
> Fair enough. So, I'll commit this to HEAD and backbaptch till PG14.
>

Here are patches for versions HEAD-to-17 and 16-to-14. Unless there
are any comments or suggestions, I'll push these tomorrow.

-- 
With Regards,
Amit Kapila.

Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Nisha Moond <nisha.moond412@gmail.com>
Дата:
On Thu, Jul 16, 2026 at 2:47 PM Amit Kapila  wrote:
>
> On Mon, Jul 13, 2026 at 11:21 AM Amit Kapila  wrote:
> >
> > On Sat, Jul 11, 2026 at 8:42 PM Jeff Davis  wrote:
> > >
> > > On Thu, 2026-07-09 at 12:47 +0530, Amit Kapila wrote:
> > > > That sounds like an appropriate solution or we can use (connect =
> > > > false) option as well a way to indicate NOCONNECT.
> > >
> > > That doesn't have a separate state in the catalog, so DROP SUBSCRIPTION
> > > doesn't know that it was specified.
> > >
> >
> > I was imagining doing something what CREATE SUBSCRIPTION doc describes
> > for this option: "Specifies whether the CREATE SUBSCRIPTION command
> > should connect to the publisher at all.". I mean some sort of WITH
> > (connect = false) but on re-thinking, for DROP it is better to use a
> > clause like NOCONNECT instead of providing options. Anyway, we can
> > brainstorm on the exact syntax in a separate thread for this work.
> >
> > > > Yeah, we can do this but if we want to change this behaviour in the
> > > > next version, isn't it better to tweak the doc wording as:"After
> > > > that,
> > > > DROP SUBSCRIPTION will not attempt to drop the subscription's own
> > > > replication slot. It may still connect to the publisher to drop
> > > > internally-created table synchronization slots if some table
> > > > synchronization was left unfinished; if the publisher is unreachable,
> > > > those slots (and the main slot, if it still exists) must be dropped
> > > > manually.".
> > >
> > > Right, we can make the documentation match the code instead of making
> > > the code match the documentation. I don't have a strong opinion which
> > > way to go. But the wording above is fine with me.
> > >
> >
> > Fair enough. So, I'll commit this to HEAD and backbaptch till PG14.
> >
>
> Here are patches for versions HEAD-to-17 and 16-to-14. Unless there
> are any comments or suggestions, I'll push these tomorrow.
>

Thanks for the patches. I verified both on their respective branches
(master through PG14).
The patches apply cleanly, and the doc builds successfully on all branches.

--
Thanks,
Nisha


Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Thu, Jul 16, 2026 at 3:49 PM Nisha Moond  wrote:
>
> On Thu, Jul 16, 2026 at 2:47 PM Amit Kapila  wrote:
> >
> > On Mon, Jul 13, 2026 at 11:21 AM Amit Kapila  wrote:
> > >
> > > On Sat, Jul 11, 2026 at 8:42 PM Jeff Davis  wrote:
> > > >
> > > > On Thu, 2026-07-09 at 12:47 +0530, Amit Kapila wrote:
> > > > > That sounds like an appropriate solution or we can use (connect =
> > > > > false) option as well a way to indicate NOCONNECT.
> > > >
> > > > That doesn't have a separate state in the catalog, so DROP SUBSCRIPTION
> > > > doesn't know that it was specified.
> > > >
> > >
> > > I was imagining doing something what CREATE SUBSCRIPTION doc describes
> > > for this option: "Specifies whether the CREATE SUBSCRIPTION command
> > > should connect to the publisher at all.". I mean some sort of WITH
> > > (connect = false) but on re-thinking, for DROP it is better to use a
> > > clause like NOCONNECT instead of providing options. Anyway, we can
> > > brainstorm on the exact syntax in a separate thread for this work.
> > >
> > > > > Yeah, we can do this but if we want to change this behaviour in the
> > > > > next version, isn't it better to tweak the doc wording as:"After
> > > > > that,
> > > > > DROP SUBSCRIPTION will not attempt to drop the subscription's own
> > > > > replication slot. It may still connect to the publisher to drop
> > > > > internally-created table synchronization slots if some table
> > > > > synchronization was left unfinished; if the publisher is unreachable,
> > > > > those slots (and the main slot, if it still exists) must be dropped
> > > > > manually.".
> > > >
> > > > Right, we can make the documentation match the code instead of making
> > > > the code match the documentation. I don't have a strong opinion which
> > > > way to go. But the wording above is fine with me.
> > > >
> > >
> > > Fair enough. So, I'll commit this to HEAD and backbaptch till PG14.
> > >
> >
> > Here are patches for versions HEAD-to-17 and 16-to-14. Unless there
> > are any comments or suggestions, I'll push these tomorrow.
> >
>
> Thanks for the patches. I verified both on their respective branches
> (master through PG14).
> The patches apply cleanly, and the doc builds successfully on all branches.
>

Pushed. Sorry, forgot to add your name as reviewer.

-- 
With Regards,
Amit Kapila.


Re: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
Shlok Kyal <shlok.kyal.oss@gmail.com>
Дата:
Вложения:
Hi,

While testing another patch, I found a bug related to subscriptions
created using a foreign server.

Suppose a subscription is created using a foreign server, and the
subscription owner does not have permission to access that server. If
the owner executes ALTER SUBSCRIPTION ... REFRESH PUBLICATION, ADD
PUBLICATION, DROP PUBLICATION, or SET PUBLICATION, the command
can still connect to the publisher and fetch the updated relation
list.

This happens because AlterSubscription() calls:
GetSubscription(subid, false, orig_conninfo_needed, false)
with conninfo_aclcheck set to false, so the ACL check on the
associated foreign server is skipped.

The attached patch fixes this by enabling conninfo_aclcheck for
these ALTER SUBSCRIPTION operations, ensuring that permissions on
the foreign server are validated before connecting to the publisher.
Thoughts?

I am posting the patch in this thread since the related changes were made here.

Thanks,
Shlok Kyal

RE: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Вложения:
Dear Shlok,

> While testing another patch, I found a bug related to subscriptions
> created using a foreign server.

Good catch.

> This happens because AlterSubscription() calls:
> GetSubscription(subid, false, orig_conninfo_needed, false)
> with conninfo_aclcheck set to false, so the ACL check on the
> associated foreign server is skipped.

Agree. There is an ongoing discussion to weaken the validation at DDL phase [1],
but IIUC it does not match with this issue. REFRESH PUBLICATION command needs to
refer the conninfo to connect to the publisher, so validation is mandatory.

> The attached patch fixes this by enabling conninfo_aclcheck for
> these ALTER SUBSCRIPTION operations, ensuring that permissions on
> the foreign server are validated before connecting to the publisher.
> Thoughts?

I think for ADD/DROP/SET PUBLICATION commands, ACL check is not needed non-refresh
case. For these cases, backends won't directly connect to the publisher server:
apply worker will check privileges and raise the ERROR. Can you set
orig_conninfo_aclchk to true only for REFRESH case?

[1]: https://www.postgresql.org/message-id/e96efe16fb47fcca4ae0cd0157fb6bc662470712.camel%40j-davis.com

Best regards,
Hayato Kuroda
FUJITSU LIMITED

RE: Bug in ALTER SUBSCRIPTION ... SERVER / ... CONNECTION with broken old server

От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Dear Shlok,

> Thanks for reviewing the patch. I have updated the patch and attached
> the v2 patch.

Thanks for the update. According to others, different kinds of statements should be
different else-if statements. So we may have to be something like below:

```
	else if (stmt->kind == ALTER_SUBSCRIPTION_REFRESH_PUBLICATION)
	{
		...
	}
	else if (stmt->kind == ALTER_SUBSCRIPTION_ADD_PUBLICATION ||
			 stmt->kind == ALTER_SUBSCRIPTION_DROP_PUBLICATION ||
			 stmt->kind == ALTER_SUBSCRIPTION_SET_PUBLICATION)
	{
		if (IsSet(opts.specified_opts, SUBOPT_REFRESH) && opts.refresh)
			...
	}
```

Best regards,
Hayato Kuroda
FUJITSU LIMITED

FAQ