Re: Introducing find_all_inheritors_ordered()

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

Re: Introducing find_all_inheritors_ordered()

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
On Mon, 07 Sep 2026, Chao Li  wrote:
> My understanding is that PG doesn’t intend to support cyclic inheritance. The code explicitly rejects it:

To me it seems like that it discourages it and tries to prevent it
where it's possible to do so with reasonable effort, but it ends up
trying to support it because it's a valid scenario that can happen in
some workloads (as long as that also doesn't require too much effort).

> A cycle can cause it to exceed the stack depth limit, so it is reasonable to fail the command when a cycle is detected.

Or it might succeed in other cases.

Here's a repro with an isolation test. Perm 1 fails on master, works
with the patch. Perm 2 works on master, fails with the patch.
We could improve this by only using the new function in the enforced
direction, I don't have a better idea currently. With that, the
patched behavior would be the same as on master.

setup
{
  CREATE TABLE r (x int CONSTRAINT cc CHECK (x > 0));
  CREATE TABLE a (x int CONSTRAINT cc CHECK (x > 0));
  CREATE TABLE b (x int CONSTRAINT cc CHECK (x > 0));
  CREATE TABLE c (x int CONSTRAINT cc CHECK (x > 0));
  CREATE TABLE d (x int CONSTRAINT cc CHECK (x > 0));
  ALTER TABLE a INHERIT r;
  ALTER TABLE b INHERIT a;
  ALTER TABLE d INHERIT c;
}

teardown
{
  DROP TABLE IF EXISTS r, a, b, c, d CASCADE;
}

session s1
step s1b  { BEGIN; }
step s1i  { ALTER TABLE c INHERIT b; }
step s1c  { COMMIT; }

session s2
step s2b  { BEGIN; }
step s2i  { ALTER TABLE a INHERIT d; }
step s2c  { COMMIT; }

session s3
step s3pre { ALTER TABLE r ALTER CONSTRAINT cc NOT ENFORCED;
             ALTER TABLE c ALTER CONSTRAINT cc NOT ENFORCED; }
step s3e   { SELECT inhrelid::regclass::text AS child,
                    inhparent::regclass::text AS parent
             FROM pg_inherits
             WHERE inhrelid::regclass::text IN ('r','a','b','c','d')
             ORDER BY 1, 2; }
step s3not { ALTER TABLE r ALTER CONSTRAINT cc NOT ENFORCED; }
step s3enf { ALTER TABLE r ALTER CONSTRAINT cc ENFORCED; }
step s3s   { SELECT conrelid::regclass::text AS rel, conenforced
             FROM pg_constraint
             WHERE conname = 'cc'
               AND conrelid::regclass::text IN ('r','a','b','c','d')
             ORDER BY 1; }

permutation s1b s2b s1i s2i s1c s2c s3e s3not s3s
permutation s1b s2b s3pre s1i s2i s1c s2c s3e s3enf s3s


Re: Introducing find_all_inheritors_ordered()

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Zsolt Parragi  writes:
> On Mon, 07 Sep 2026, Chao Li  wrote:
>> My understanding is that PG doesn’t intend to support cyclic inheritance. The code explicitly rejects it:

> To me it seems like that it discourages it and tries to prevent it
> where it's possible to do so with reasonable effort, but it ends up
> trying to support it because it's a valid scenario that can happen in
> some workloads (as long as that also doesn't require too much effort).

Why is it a valid scenario, and how would we reach it?  It's going to
cause tons of problems if it can happen, so I'd rather put effort into
blocking it than making some parts of the system cope.

I have a different concern about the patch as it stands: the
data-gathering part is unnecessarily duplicative of
find_all_inheritors.  It does its best to look like it's doing
something different, but actually it computes exactly the same list
of relation OIDs and the same number-of-parents data.  And indeed
it had better be doing the same things in the same order, because
otherwise we'd be risking deadlock failures instead of simple
blocking when two processes are acquiring exclusive locks on
overlapping inheritance trees.  (Note the comment about "we need to be
sure all backends lock children in the same order to avoid needless
deadlocks" in find_inheritance_children.)  I don't like having two
independent implementations that are invisibly tied like that: if
they diverge, we might not notice until somebody makes a bug report
and somebody else figures out what's causing the deadlock.  So IMO
those two functions need to be revised to use a common data-gathering
step.

			regards, tom lane


Re: Introducing find_all_inheritors_ordered()

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


> On Sep 8, 2026, at 06:16, Zsolt Parragi  wrote:
> 
> On Mon, 07 Sep 2026, Chao Li  wrote:
>> My understanding is that PG doesn’t intend to support cyclic inheritance. The code explicitly rejects it:
> 
> To me it seems like that it discourages it and tries to prevent it
> where it's possible to do so with reasonable effort, but it ends up
> trying to support it because it's a valid scenario that can happen in
> some workloads (as long as that also doesn't require too much effort).

I think an important problem is that, in your earlier example, two users independently execute ALTER TABLE … INHERIT, but the commands together create an inheritance cycle without either user being notified. Even if PG currently tolerates this, I think preventing it would be preferable.

As Tom pointed out, cycles can also cause problems elsewhere in the system. So I agree that we should investigate whether we can prevent their creation at a reasonable cost.

> 
>> A cycle can cause it to exceed the stack depth limit, so it is reasonable to fail the command when a cycle is detected.
> 
> Or it might succeed in other cases.
> 
> Here's a repro with an isolation test. Perm 1 fails on master, works
> with the patch. Perm 2 works on master, fails with the patch.
> We could improve this by only using the new function in the enforced
> direction, I don't have a better idea currently. With that, the
> patched behavior would be the same as on master.
> 

This is a valid point. ENFORCED doesn’t need to check parents’ enforceability, so we can still use find_all_inheritors() to tolerate potential cycles and use find_all_inheritors_ordered() only for NOT ENFORCED. This preserves master’s success/failure outcomes in your test, while replacing the stack-depth error with an explicit cycle error. I have updated the code in v3.

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






Re: Introducing find_all_inheritors_ordered()

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
On Tue, 08 Sep 2026, Chao Li  wrote:
> > Why is it a valid scenario, and how would we reach it? It's going to
> > cause tons of problems if it can happen, so I'd rather put effort into
> > blocking it than making some parts of the system cope.
>
> +1
>
> I can look into if we can prevent concurrent ALTER TABLE ... INHERIT commands from creating cycles at a reasonable cost, and start a separate discussion.

+1 for that, if it is doable, I didn't investigate it further than the
comment's claim that it is too difficult.

I didn't want to say that I think it is a real-usable scenario, I
meant valid as currently reachable (as showcased by the isolation
tester spec I posted), that probably wasn't the best word to describe
it.


FAQ