CREATE SUBSCRIPTION ... SERVER vs. pg_dump, etc.

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

CREATE SUBSCRIPTION ... SERVER vs. pg_dump, etc.

От:
Noah Misch <noah@leadboat.com>
Дата:
Вложения:
An Opus 4.8 review of commit 8185bb5 found two pg_dump+restore failure
scenarios, visible in the attached test patch.  (The patch also tests a
REASSIGN OWNED finding, for which I started a distinct thread
postgr.es/m/flat/20260710192533.4f.noahmisch@microsoft.com).

Opus also emitted the attached report about these findings and others.  I
didn't examine the others closely.  Finding-19, about invalidation callbacks,
stood out as perhaps most exciting if true.

Re: CREATE SUBSCRIPTION ... SERVER vs. pg_dump, etc.

От:
Jeff Davis <pgsql@j-davis.com>
Дата:
Вложения:
On Fri, 2026-07-10 at 12:59 -0700, Noah Misch wrote:
> An Opus 4.8 review of commit 8185bb5 found two pg_dump+restore
> failure
> scenarios, visible in the attached test patch.  (The patch also tests
> a
> REASSIGN OWNED finding, for which I started a distinct thread
> postgr.es/m/flat/20260710192533.4f.noahmisch@microsoft.com).
> 
> Opus also emitted the attached report about these findings and
> others.  I
> didn't examine the others closely.  Finding-19, about invalidation
> callbacks,
> stood out as perhaps most exciting if true.

Patch attached.

Generating and validating the connection requires the subscription
owner to be set correctly, the foreign server ACLs to be set, and the
user mapping to exist. The checks at DDL time are were a convenient way
to catch errors, but end up being too strict because those things can
change before the connection is actually needed. In particular, pg_dump
does the DDL in parts (first creating the subscription, then changing
the owner), and we need the first part to succeed.

It would be nice to expand the pg_dump tests to cover this, but that
would require a test dependency on postgres_fdw (or some kind of built-
in test FDW), and I don't think we want that. So I just included SQL
tests.

I think there's a remaining bug involving retaindeadtuples
(228c3708685) where it still tries to connect during binary upgrade. 

That can be seen if you add a $publisher->stop to line 317 (right
before the pg_upgrade that's supposed to succeed) in
004_subscription.pl.

Regards,
	Jeff Davis

Re: CREATE SUBSCRIPTION ... SERVER vs. pg_dump, etc.

On Wed, 2026-07-29 at 14:30 -0700, Jeff Davis wrote:
> Proposal:
> 
>  * Form a conninfo if and only if a connection is immediately
>    required. That is, it's one of the DDL commands above, or a
>    logical worker.
> 
>  * Check USAGE on the server when a connection is formed or when
>    ALTER sets the server or when the subscription owner changes
>    (unless superuser changes the owner, in which case it may be part
>    of a multi-command DDL sequence during restore).
> 
>  * Check that the server's FDW supports a connection function when
>    DDL sets the subscription's server.
> 
>  * Check that a user mapping exists during DDL when the server or
>    owner changes, but demote the message to a WARNING, because it
>    may be part of a multi-command DDL sequence during restore.
>    - CREATE SUBSCRIPTION already issues WARNINGS during restore.
> 
>  * Ensure that none of the commands during restore need a connection.
>    - check_pub_rdt should happen at connection time, and only
>      opportunistically at DDL time if already forming a connection
> 
>  * Check walrcv_check_conninfo() before connecting, or during
>    CREATE/ALTER SUBSCRIPTION ... CONNECTION.
>    - If a connection is not needed for DDL, and it's a server-based
>      subscription, walrcv_check_conninfo() will be called only by
>      the logical worker when a connection is needed.
>    - That loses some convenience for interactive DDL, but avoids
>      false positive failures during restore.

Here's a consolidated series of commits.

Fujii, note that this includes a revert of your commit 1c9c358904.
Doing too much validation can cause problems for restore.

Amit, this series does not include the check_pub_rdt change to move it
to the worker.

Shlok Kyal, the change to detect when a refresh is happening is in my
patch 0006, the ACL check fix is in my patch 0007.

Hayato Kuroda, Amit already pushed the --no-subscriptions fix
(02decf9a9a). I didn't change the dump/restore order in this series,
because reducing the errors seems right for v19. We can reconsider it
for v20.

Regards,
	Jeff Davis

Re: CREATE SUBSCRIPTION ... SERVER vs. pg_dump, etc.

От:
Jeff Davis <pgsql@j-davis.com>
Дата:
On Fri, 2026-07-31 at 14:12 +0530, Amit Kapila wrote:
> However, I feel it is better to detect the same
> at DDL time whenever possible as well as it gives immediate,
> synchronous feedback for interactive CREATE/ALTER, whereas a
> worker-only failure just lands in the server log and the worker keeps
> restarting. Removing it would also mean enabling retain_dead_tuples
> no
> longer validates the publisher at all in the common interactive case.

I believe the only problem case is ALTER SUBSCRIPTION ... ENABLE,
right?

CREATE doesn't do the check when connect=false, so that's the same
behavior.

None of ALTER ... SERVER, ALTER ... CONNECTION, or ALTER ... SET
(retain_dead_tuples) are called by restore because it sets those things
with the CREATE statement.


If you'd still like ALTER SUBSCRIPTION ... ENABLE to do the convenience
check, then I think you could clarify the problem case in the comments:

+  /*
+   * During binary upgrade, we only recreate the catalog state and
must not
+   * connect to the publisher. The publisher's suitability for
+   * retain_dead_tuples is validated authoritatively by the apply
worker
+   * when it connects, so skip the opportunistic DDL-time check here.
+   */
+  if (IsBinaryUpgrade)
+    check_pub_rdt = false;

During any restore we must not connect to the publisher. It's only a
problem for binary upgrade because that's what issues the ENABLE.

But the overall logic is more like "restore must not create any
connections, therefore it must not issue any commands that set
check_pub_rdt". We can't detect an ordinary restore (because it's
treated the same as interactive SQL), so we just have to be sure not to
introduce check_pub_rdt cases in the ordinary restore path later.

Also, we need to integrate it with the series I posted because we must
not generate the conninfo if IsBinaryUpgrade. I expect yours will go in
first and I can rebase on that, so you don't need to make a change
here.

Regards,
	Jeff Davis



RE: CREATE SUBSCRIPTION ... SERVER vs. pg_dump, etc.

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

> OTOH, I am also fine if you prefer to remove the retain_dead_tuples
> check entirely from the ENABLE path and keep it in other existing
> paths as in attached. Actually, that will slightly simplify the code
> as well.

I also preferred the approach 2. I tested on PG19 and master, and confirmed
it could pass tests with the Jeff's reproducer. maybe_reread_subscription()
did not take care the parameter change, but it's ok because it cannot be
altered for the enabled subscription.

The patch LGTM.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

RE: CREATE SUBSCRIPTION ... SERVER vs. pg_dump, etc.

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

> New v4 series attached.

Thanks for updating the patch. While seeing 0001, I found that GetSubscription()
and SubscriptionConninfo() are called without acquiring the lock for the
subscription. Is there a possibility that DDL commands are executed concurrently
and the cache can be invalidated in-between, thus it causes inconsistent results?
I tested concurrent ALTER SUBSCRIPTION SET OWNER command in maybe_reread_subscription(),
but the cache is not invalidated thus changing the owner is not reflected.
Is it OK?

Best regards,
Hayato Kuroda
FUJITSU LIMITED

Re: CREATE SUBSCRIPTION ... SERVER vs. pg_dump, etc.

От:
Jeff Davis <pgsql@j-davis.com>
Дата:
On Tue, 2026-08-04 at 10:07 +0000, Hayato Kuroda (Fujitsu) wrote:
> Thanks for updating the patch. While seeing 0001, I found that
> GetSubscription()
> and SubscriptionConninfo() are called without acquiring the lock for
> the
> subscription. Is there a possibility that DDL commands are executed
> concurrently
> and the cache can be invalidated in-between, thus it causes
> inconsistent results?
> I tested concurrent ALTER SUBSCRIPTION SET OWNER command in
> maybe_reread_subscription(),
> but the cache is not invalidated thus changing the owner is not
> reflected.
> Is it OK?

Can you explain in more detail the problem case? Is it unique to
server-based subscriptions?

Looking at the code it seems that invalidations can be missed if they
come between the time the catalogs are read and the time that
MySubscriptionValid is set. But I think that's a pre-existing issue --
perhaps we should start a new thread about that?

Regards,
	Jeff Davis



Re: CREATE SUBSCRIPTION ... SERVER vs. pg_dump, etc.

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Вложения:
Jeff Davis  writes:
> On Wed, 2026-08-05 at 16:47 +0530, Amit Kapila wrote:
>> BTW, I had also looked at the overall patch series, the idea and
>> high-level code looks good to me. Though I haven't done a detailed
>> testing or review of the same but as Shlok and Kuroda-San seem to
>> have
>> reviewed/tested these patches, I think we can go-ahead with these
>> fixes.

> Thank you all, pushed.

Coverity thinks there is a hole in this logic:

/srv/coverity/git/pgsql-git/postgresql/src/backend/commands/subscriptioncmds.c: 875             in CreateSubscription()
869     	values[Anum_pg_subscription_submaxretention - 1] =
870     		Int32GetDatum(opts.maxretention);
871     	values[Anum_pg_subscription_subretentionactive - 1] =
872     		BoolGetDatum(opts.retaindeadtuples);
873     	values[Anum_pg_subscription_subserver - 1] = ObjectIdGetDatum(serverid);
874     	if (!OidIsValid(serverid))
>>>     CID 1699896:         Null pointer dereferences  (FORWARD_NULL)
>>>     Passing null pointer "conninfo" to "cstring_to_text", which dereferences it.
875     		values[Anum_pg_subscription_subconninfo - 1] =
876     			CStringGetTextDatum(conninfo);
877     	else
878     		nulls[Anum_pg_subscription_subconninfo - 1] = true;
879     	if (opts.slot_name)
880     		values[Anum_pg_subscription_subslotname - 1] =

AFAICS, it's right: if stmt->servername is set while opts.connect is
not, we'll arrive at this step with serverid filled in but conninfo
still NULL.  Even if there's some upstream reason why that combination
can't occur, this is pretty fragile-looking code.  It's far from clear
why serverid has anything to do with conninfo being available.

			regards, tom lane


FAQ