Introducing find_all_inheritors_ordered()

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, jian he <jian(dot)universality(at)gmail(dot)com>, Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>, Álvaro Herrera <alvherre(at)kurilemu(dot)de>
Subject: Introducing find_all_inheritors_ordered()
Date: 2026-07-30 08:35:00
Message-ID: 57100518-3FF1-48AD-B550-47B9A3E88688@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

This is follow-up work to patch [1], which fixed a bug when altering a CHECK constraint's enforceability. The fix was not very elegant. It had to use upward recursion to traverse all ancestors when deciding a child table's enforceability. This was because the existing function find_all_inheritors() returns a list of descendants without ensuring that parents precede their children.

This patch introduces a new function, find_all_inheritors_ordered(), which guarantees that every ancestor in the returned list appears before its descendants. With this new function, the original fix in commit 0cd17fdd3c0 is significantly simplified. The function could also potentially benefit other features that need to traverse inheritance trees in parent-before-child order.

This patch also strengthens an existing test by adding another level of inheritance. My first version of the implementation failed with the following case:
```
Root ———————————————> child
\ /
\ ——————> a —————> b /
```
(The diagram might not display well. Basically, “child" has parents “b" and “root", “b" has parent “a”, “a” has parent “root")

The current version uses Kahn's topological sorting algorithm, which handles this case correctly. Please see the attached patch for details.

[1] https://postgr.es/m/E74C57FA-1DD0-4C8E-8FB1-538034752592@gmail.com

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

Attachment Content-Type Size
v1-0001-Add-find_all_inheritors_ordered.patch application/octet-stream 16.3 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2026-07-30 08:45:22 Re: Fix hashchar() and hashcharextended() to not depend on char signedness
Previous Message Hannu Krosing 2026-07-30 08:24:47 Re: Direct Toast PoC