REASSIGN OWNED vs. relisshared dep on !relisshared

Started by Noah Misch5 months ago15 messageshackers
Jump to latest
#1Noah Misch
noah@leadboat.com

v19 commit 8185bb5 added field pg_subscription.subserver, and v20 commit
a5918fd added field pg_subscription.subconflictlogrelid. Each involves a
dependency link from the relisshared pg_subscription to a !relisshared
catalog. REASSIGN OWNED, if run in a database other than
pg_subscription.subdbid, wrongly interprets those fields relative to the
current database. This causes an error or opens an unrelated object.

Perhaps one could fix this by having REASSIGN OWNED process only
pg_subscription rows where subdbid is the current database. Already, one must
run REASSIGN OWNED in each database for ordinary db-local objects like tables.
From a user's perspective, REASSIGN OWNED would reach subscriptions like it
reaches tables.

I'm nervous that we'll have other reasons to regret letting a shared object
depend on a non-shared object, but I've not come up with anything else
concrete.

Ops 4.8 wrote the attached test cases. (The subserver test patch has tests
for some other behaviors, which I'll post about elsewhere.)

Attachments:

test-subscription-server-v0.patchtext/plain; charset=us-asciiDownload+234-0
test-subconflictlogrelid-REASSIGN-OWNED-v0.patchtext/plain; charset=us-asciiDownload+75-0
#2Jeff Davis
pgsql@j-davis.com
In reply to: Noah Misch (#1)
Re: REASSIGN OWNED vs. relisshared dep on !relisshared

On Fri, 2026-07-10 at 12:25 -0700, Noah Misch wrote:

Perhaps one could fix this by having REASSIGN OWNED process only
pg_subscription rows where subdbid is the current database.

That was my first thought, as well. I will look into it.

I'm nervous that we'll have other reasons to regret letting a shared
object
depend on a non-shared object, but I've not come up with anything
else
concrete.

That's a reasonable concern. We discussed it here:

/messages/by-id/CAA4eK1LyHvRoNzZPpPQqo7a=5Wov8F-7+KDduy-9ymcRm=Batg@mail.gmail.com

Though the root of the problem might be that pg_subscription is shared
in the first place.

Perhaps we should just not record the dependency on the foreign server
at all, and if someone drops it, then the drop succeeds and the
connection fails later. That would be more like what would happen if
you revoke privileges, which also isn't tracked by the dependency
mechanism but has a similar result.

Regards,
Jeff Davis

#3Jeff Davis
pgsql@j-davis.com
In reply to: Jeff Davis (#2)
Re: REASSIGN OWNED vs. relisshared dep on !relisshared

On Fri, 2026-07-10 at 15:11 -0700, Jeff Davis wrote:

Perhaps we should just not record the dependency on the foreign
server
at all

Of course we have to be careful not to leave a dangling reference. That
should be read more as an idea than a proposal.

Regards,
Jeff Davis

#4Amit Kapila
amit.kapila16@gmail.com
In reply to: Jeff Davis (#2)
Re: REASSIGN OWNED vs. relisshared dep on !relisshared

On Sat, Jul 11, 2026 at 3:41 AM Jeff Davis <pgsql@j-davis.com> wrote:

On Fri, 2026-07-10 at 12:25 -0700, Noah Misch wrote:

Perhaps one could fix this by having REASSIGN OWNED process only
pg_subscription rows where subdbid is the current database.

That was my first thought, as well. I will look into it.

I'm nervous that we'll have other reasons to regret letting a shared
object
depend on a non-shared object, but I've not come up with anything
else
concrete.

That's a reasonable concern. We discussed it here:

/messages/by-id/CAA4eK1LyHvRoNzZPpPQqo7a=5Wov8F-7+KDduy-9ymcRm=Batg@mail.gmail.com

Though the root of the problem might be that pg_subscription is shared
in the first place.

Yeah, but actually a subscription is really tied to a specific
database; the catalog is shared only because the replication launcher
must see all subscriptions to start their workers, as the header
comment notes: "Technically, the subscriptions live inside the
database, so a shared catalog seems weird, but the replication
launcher process needs to access all of them to be able to start the
workers, so we have to put them in a shared, nailed catalog."

Note that the subscription commands already behave per-database in
code: CREATE SUBSCRIPTION stores subdbid = MyDatabaseId, and
ALTER/DROP SUBSCRIPTION and ALTER SUBSCRIPTION ... OWNER all look the
subscription up by (MyDatabaseId, subname) via the unique index on
(subdbid, subname), so they only ever act on subscriptions in the
current database.

Given that, and that REASSIGN OWNED is already documented as a
per-database operation "Because REASSIGN OWNED does not affect objects
within other databases, it is usually necessary to execute this
command in each database that contains objects owned by a role that is
to be removed.", processing only the pg_subscription rows whose
subdbid is the current database sounds like a fix (which is consistent
with other commands operating on a subscription) for this as mentioned
by both you and Noah.

--
With Regards,
Amit Kapila.

#5Dilip Kumar
dilipbalaut@gmail.com
In reply to: Amit Kapila (#4)
Re: REASSIGN OWNED vs. relisshared dep on !relisshared

On Sat, Jul 11, 2026 at 9:31 AM Amit Kapila <amit.kapila16@gmail.com> wrote:

On Sat, Jul 11, 2026 at 3:41 AM Jeff Davis <pgsql@j-davis.com> wrote:

On Fri, 2026-07-10 at 12:25 -0700, Noah Misch wrote:

Perhaps one could fix this by having REASSIGN OWNED process only
pg_subscription rows where subdbid is the current database.

That was my first thought, as well. I will look into it.

I'm nervous that we'll have other reasons to regret letting a shared
object
depend on a non-shared object, but I've not come up with anything
else
concrete.

That's a reasonable concern. We discussed it here:

/messages/by-id/CAA4eK1LyHvRoNzZPpPQqo7a=5Wov8F-7+KDduy-9ymcRm=Batg@mail.gmail.com

Though the root of the problem might be that pg_subscription is shared
in the first place.

Yeah, but actually a subscription is really tied to a specific
database; the catalog is shared only because the replication launcher
must see all subscriptions to start their workers, as the header
comment notes: "Technically, the subscriptions live inside the
database, so a shared catalog seems weird, but the replication
launcher process needs to access all of them to be able to start the
workers, so we have to put them in a shared, nailed catalog."

Note that the subscription commands already behave per-database in
code: CREATE SUBSCRIPTION stores subdbid = MyDatabaseId, and
ALTER/DROP SUBSCRIPTION and ALTER SUBSCRIPTION ... OWNER all look the
subscription up by (MyDatabaseId, subname) via the unique index on
(subdbid, subname), so they only ever act on subscriptions in the
current database.

Given that, and that REASSIGN OWNED is already documented as a
per-database operation "Because REASSIGN OWNED does not affect objects
within other databases, it is usually necessary to execute this
command in each database that contains objects owned by a role that is
to be removed.", processing only the pg_subscription rows whose
subdbid is the current database sounds like a fix (which is consistent
with other commands operating on a subscription) for this as mentioned
by both you and Noah.

PFA a patch for skipping subscriptions belonging to different databases.

--
Regards,
Dilip Kumar
Google

Attachments:

v1-0001-Do-not-reassign-subscriptions-belonging-to-other-.patchapplication/octet-stream; name=v1-0001-Do-not-reassign-subscriptions-belonging-to-other-.patchDownload+16-3
#6shveta malik
shveta.malik@gmail.com
In reply to: Dilip Kumar (#5)
Re: REASSIGN OWNED vs. relisshared dep on !relisshared

On Mon, Jul 13, 2026 at 5:04 PM Dilip Kumar <dilipbalaut@gmail.com> wrote:

On Sat, Jul 11, 2026 at 9:31 AM Amit Kapila <amit.kapila16@gmail.com> wrote:

On Sat, Jul 11, 2026 at 3:41 AM Jeff Davis <pgsql@j-davis.com> wrote:

On Fri, 2026-07-10 at 12:25 -0700, Noah Misch wrote:

Perhaps one could fix this by having REASSIGN OWNED process only
pg_subscription rows where subdbid is the current database.

That was my first thought, as well. I will look into it.

I'm nervous that we'll have other reasons to regret letting a shared
object
depend on a non-shared object, but I've not come up with anything
else
concrete.

That's a reasonable concern. We discussed it here:

/messages/by-id/CAA4eK1LyHvRoNzZPpPQqo7a=5Wov8F-7+KDduy-9ymcRm=Batg@mail.gmail.com

Though the root of the problem might be that pg_subscription is shared
in the first place.

Yeah, but actually a subscription is really tied to a specific
database; the catalog is shared only because the replication launcher
must see all subscriptions to start their workers, as the header
comment notes: "Technically, the subscriptions live inside the
database, so a shared catalog seems weird, but the replication
launcher process needs to access all of them to be able to start the
workers, so we have to put them in a shared, nailed catalog."

Note that the subscription commands already behave per-database in
code: CREATE SUBSCRIPTION stores subdbid = MyDatabaseId, and
ALTER/DROP SUBSCRIPTION and ALTER SUBSCRIPTION ... OWNER all look the
subscription up by (MyDatabaseId, subname) via the unique index on
(subdbid, subname), so they only ever act on subscriptions in the
current database.

Given that, and that REASSIGN OWNED is already documented as a
per-database operation "Because REASSIGN OWNED does not affect objects
within other databases, it is usually necessary to execute this
command in each database that contains objects owned by a role that is
to be removed.", processing only the pg_subscription rows whose
subdbid is the current database sounds like a fix (which is consistent
with other commands operating on a subscription) for this as mentioned
by both you and Noah.

PFA a patch for skipping subscriptions belonging to different databases.

The fix idea and the patch LGTM. One minor comment in the doc:

'and are reassigned when REASSIGN OWNED is executed in that database.'

when --> only when (to make it more clear)

thanks
Shveta

#7Jeff Davis
pgsql@j-davis.com
In reply to: Dilip Kumar (#5)
Re: REASSIGN OWNED vs. relisshared dep on !relisshared

On Mon, 2026-07-13 at 17:04 +0530, Dilip Kumar wrote:

PFA a patch for skipping subscriptions belonging to different
databases.

I suggest we add a few checks (or asserts) that subdbid==MyDatabaseId
when reading pg_subscription from anywhere other than the launcher.

Also, the REASSIGN OWNED path goes through
AlterSubscriptionOwner_oid(), whereas both the REASSIGN OWNED and ALTER
SUBSCRIPTION ... OWNER TO paths go through
AlterSubscriptionOwner_internal(). It would be more clear to add the
skipping logic in the former and an assert in the latter.

Other than that, LGTM.

We can also reconsider how we handle dependencies to address Noah's
concern, but this patch is good regardless.

Regards,
Jeff Davis

#8Dilip Kumar
dilipbalaut@gmail.com
In reply to: Jeff Davis (#7)
Re: REASSIGN OWNED vs. relisshared dep on !relisshared

On Wed, Jul 15, 2026 at 2:20 AM Jeff Davis <pgsql@j-davis.com> wrote:

On Mon, 2026-07-13 at 17:04 +0530, Dilip Kumar wrote:

PFA a patch for skipping subscriptions belonging to different
databases.

I suggest we add a few checks (or asserts) that subdbid==MyDatabaseId
when reading pg_subscription from anywhere other than the launcher.

Yeah, I've done that

Also, the REASSIGN OWNED path goes through
AlterSubscriptionOwner_oid(), whereas both the REASSIGN OWNED and ALTER
SUBSCRIPTION ... OWNER TO paths go through
AlterSubscriptionOwner_internal(). It would be more clear to add the
skipping logic in the former and an assert in the latter.

That makes sense; I've made the change.

Other than that, LGTM.

Thanks for the review

--
Regards,
Dilip Kumar
Google

Attachments:

v2-0001-Do-not-reassign-subscriptions-belonging-to-other-.patchapplication/octet-stream; name=v2-0001-Do-not-reassign-subscriptions-belonging-to-other-.patchDownload+34-4
#9Jeff Davis
pgsql@j-davis.com
In reply to: Dilip Kumar (#8)
Re: REASSIGN OWNED vs. relisshared dep on !relisshared

On Wed, 2026-07-15 at 20:53 +0530, Dilip Kumar wrote:

Thanks for the review

Looks good to me, thank you for the patch. I will commit this soon.

To address Noah's concern: is the concern about the dependency link, or
about having a database-specific oid in a shared catalog at all?

If the former, then we could do the same work manually outside of the
pg_depend mechanism.

If the latter, we could decompose pg_subscription into the columns
necessary for the launcher (shared catalog) and the database-specific
columns (unshared catalog). But that's more invasive so it might not be
v19 material.

Thoughts?

Regards,
Jeff Davis

#10Amit Kapila
amit.kapila16@gmail.com
In reply to: Jeff Davis (#9)
Re: REASSIGN OWNED vs. relisshared dep on !relisshared

On Thu, Jul 16, 2026 at 12:06 AM Jeff Davis <pgsql@j-davis.com> wrote:

On Wed, 2026-07-15 at 20:53 +0530, Dilip Kumar wrote:

Thanks for the review

Looks good to me, thank you for the patch. I will commit this soon.

Thanks.

To address Noah's concern: is the concern about the dependency link, or
about having a database-specific oid in a shared catalog at all?

If the former, then we could do the same work manually outside of the
pg_depend mechanism.

If the latter, we could decompose pg_subscription into the columns
necessary for the launcher (shared catalog) and the database-specific
columns (unshared catalog). But that's more invasive so it might not be
v19 material.

I feel it is the latter as the former doesn't scale well, since each
new db-specific column would need its own bespoke handling instead of
the dependency machinery handling it uniformly. I agree with your
judgement that it is not a good idea to tackle this in v19, especially
because splitting a shared, user-visible catalog is too
invasive to land safely this late in the cycle, whereas the
subdbid-scoping fix already resolves the actual bug at low risk,
leaving the decomposition as a structural cleanup better suited to
v20.

--
With Regards,
Amit Kapila.

#11Jeff Davis
pgsql@j-davis.com
In reply to: Amit Kapila (#10)
Re: REASSIGN OWNED vs. relisshared dep on !relisshared

On Sat, 2026-07-18 at 12:10 +0530, Amit Kapila wrote:

On Thu, Jul 16, 2026 at 12:06 AM Jeff Davis <pgsql@j-davis.com>

Looks good to me, thank you for the patch. I will commit this soon.

Committed with a smaller test. Thank you.

To address Noah's concern: is the concern about the dependency
link, or
about having a database-specific oid in a shared catalog at all?

...

I feel it is the latter as the former doesn't scale well, since each
new db-specific column would need its own bespoke handling instead of
the dependency machinery handling it uniformly. I agree with your
judgement that it is not a good idea to tackle this in v19,
especially
because splitting a shared, user-visible catalog is too
invasive to land safely this late in the cycle, whereas the
subdbid-scoping fix already resolves the actual bug at low risk,
leaving the decomposition as a structural cleanup better suited to
v20.

OK, should we go ahead and split the pg_subscription catalog for v20
then? Maybe pg_subscription (shared) and pg_subscription_db (per-
database)?

Regards,
Jeff Davis

#12Amit Kapila
amit.kapila16@gmail.com
In reply to: Jeff Davis (#11)
Re: REASSIGN OWNED vs. relisshared dep on !relisshared

On Sat, Jul 18, 2026 at 10:11 PM Jeff Davis <pgsql@j-davis.com> wrote:

On Sat, 2026-07-18 at 12:10 +0530, Amit Kapila wrote:

On Thu, Jul 16, 2026 at 12:06 AM Jeff Davis <pgsql@j-davis.com>

Looks good to me, thank you for the patch. I will commit this soon.

Committed with a smaller test. Thank you.

To address Noah's concern: is the concern about the dependency
link, or
about having a database-specific oid in a shared catalog at all?

...

I feel it is the latter as the former doesn't scale well, since each
new db-specific column would need its own bespoke handling instead of
the dependency machinery handling it uniformly. I agree with your
judgement that it is not a good idea to tackle this in v19,
especially
because splitting a shared, user-visible catalog is too
invasive to land safely this late in the cycle, whereas the
subdbid-scoping fix already resolves the actual bug at low risk,
leaving the decomposition as a structural cleanup better suited to
v20.

OK, should we go ahead and split the pg_subscription catalog for v20
then? Maybe pg_subscription (shared) and pg_subscription_db (per-
database)?

+1, though I am not sure of catalog names. We can brainstorm those
during development.

--
With Regards,
Amit Kapila.

#13Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Amit Kapila (#12)
RE: REASSIGN OWNED vs. relisshared dep on !relisshared

Dear Hackers,

OK, should we go ahead and split the pg_subscription catalog for v20
then? Maybe pg_subscription (shared) and pg_subscription_db (per-
database)?

+1, though I am not sure of catalog names. We can brainstorm those
during development.

+1. I cannot come up with better idea to resolve the entanglement. I briefly
checked the launcher code and it read 7 attributes [1]``` oid dbid name owner retaindeadtuples retentionactive enabled ``` at least. Not verified,
all of them (or except name) can be retained on the shared catalog.

If it's ok I can start a separated thread after analyzing bit more with the PoC.

[1]: ``` oid dbid name owner retaindeadtuples retentionactive enabled ```
```
oid
dbid
name
owner
retaindeadtuples
retentionactive
enabled
```

Best regards,
Hayato Kuroda
FUJITSU LIMITED

#14Noah Misch
noah@leadboat.com
In reply to: Amit Kapila (#12)
Re: REASSIGN OWNED vs. relisshared dep on !relisshared

On Sun, Jul 19, 2026 at 07:09:13AM +0530, Amit Kapila wrote:

On Sat, Jul 18, 2026 at 10:11 PM Jeff Davis <pgsql@j-davis.com> wrote:

On Sat, 2026-07-18 at 12:10 +0530, Amit Kapila wrote:

On Thu, Jul 16, 2026 at 12:06 AM Jeff Davis <pgsql@j-davis.com>

To address Noah's concern: is the concern about the dependency
link, or
about having a database-specific oid in a shared catalog at all?

...

I feel it is the latter as the former doesn't scale well, since each
new db-specific column would need its own bespoke handling instead of
the dependency machinery handling it uniformly. I agree with your
judgement that it is not a good idea to tackle this in v19,
especially
because splitting a shared, user-visible catalog is too
invasive to land safely this late in the cycle, whereas the
subdbid-scoping fix already resolves the actual bug at low risk,
leaving the decomposition as a structural cleanup better suited to
v20.

OK, should we go ahead and split the pg_subscription catalog for v20
then? Maybe pg_subscription (shared) and pg_subscription_db (per-
database)?

+1, though I am not sure of catalog names. We can brainstorm those
during development.

That works for me. I agree an untracked dependency would be worse than the
status quo and worse than splitting the catalog. I don't have a strong
objection to the status quo.

#15Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Noah Misch (#14)
RE: REASSIGN OWNED vs. relisshared dep on !relisshared

Dear Noah,

That works for me. I agree an untracked dependency would be worse than the
status quo and worse than splitting the catalog. I don't have a strong
objection to the status quo.

Thanks for the agreement. The implementation has already been done locally,
but I'm planning to start a new thread after the PG19 is stabilized once.

Best regards,
Hayato Kuroda
FUJITSU LIMITED