A new C function `get_partition_root`.
A new C function `get_partition_root`.
This patch introduces a new C function `get_partition_root`. WHY? 1. It is the C equivalent of the already-existing SQL function `pg_partition_root` 2. Some code is not interested in the partition "ancestors", yet it still had to declare/fetch/free an ancestors list to obtain the root relid at llast_oid(ancestors). 3. The logical replication FOR TABLES IN SCHEMA EXCEPT (...) patches [1] (still in development) have many more code fragments like this where the logic wants only the partition root OID -- This function would simplify all those. ~~~ PSA v1 Passes make check-world. Thoughts? ====== [1] https://www.postgresql.org/message-id/flat/CABdArM5sw4Q1ZU8HGdo4BSc1A_%2B8xtUNq17j6wcir%3DyMUy19Cg%40mail.gmail.com Kind Regards, Peter Smith. Fujitsu Australia
Re: A new C function `get_partition_root`.
От:
shveta malik <shveta.malik@gmail.com>
Дата:
On Fri, Jul 17, 2026 at 9:41 AM Peter Smith wrote: > > This patch introduces a new C function `get_partition_root`. > > WHY? > > 1. It is the C equivalent of the already-existing SQL function > `pg_partition_root` > > 2. Some code is not interested in the partition "ancestors", yet it > still had to declare/fetch/free an ancestors list to obtain the root > relid at llast_oid(ancestors). > > 3. The logical replication FOR TABLES IN SCHEMA EXCEPT (...) patches > [1] (still in development) have many more code fragments like this > where the logic wants only the partition root OID -- This function > would simplify all those. > I agree that the patch you mentioned will benefit from such a function. So before we commit that patch ( FOR TABLES IN SCHEMA EXCEPT), this function addition can be considered. A few comments: 1) + * Note: This should only be called when it is known that the relation is a + * partition (see function get_partition_ancestors). The comment does not match the actual flow through pg_partition_root(). This function can also be reached when the input relation is not a partition but a partitioned table (the root itself). We should update the comment to reflect the behavior enforced by check_rel_can_be_partition() in pg_partition_root(). 2) Looking only at get_partition_root(), it doesn't seem to be self-contained in validating that the caller has supplied a valid input. Since this internal function is expected to be used more widely (in the thread you referenced), I would expect it to handle all input cases consistently: --Return NULL if the input relation is neither a partition nor a partitioned table. Or may be Assert will be better? --Return the same relation if the input is already the partitioned table (the root). --Return the root partitioned table if the input is a partition. The second and third cases currently work as expected. But the first and second cases are not distinguishable. Both a regular table and a partitioned table results in the input relation's OID being returned by get_partition_root (verified by pg_partition_root() by bypassing check_rel_can_be_partition()). I think get_partition_root needs some improvement for validaitng and distinguishing above cases. thanks Shveta
Re: A new C function `get_partition_root`.
От:
Chao Li <li.evan.chao@gmail.com>
Дата:
> On Jul 30, 2026, at 08:04, Peter Smith wrote: > > On Wed, Jul 29, 2026 at 7:18 PM shveta malik wrote: >> >> On Wed, Jul 29, 2026 at 2:28 PM Peter Smith wrote: > ... >>> BTW, although I think using sanity Assert was correct, just in case >>> there is some unanticipated way to reach the C function with a bad >>> relid, I've changed to use an errlog(ERROR). >>> Thoughts? >> >> I don't immediately see any such possibility. I feel Assert is better. >> > > OK. PSA v4, which is the same as v3, but uses Assert instead of elog. > > ====== > Kind Regards, > Peter Smith. > Fujitsu Australia > I just reviewed v4 and got a doubt: ``` + /* Sanity check: The root must be a partitioned table */ + Assert(RELKIND_HAS_PARTITIONS(get_rel_relkind(root_relid))); ``` Looking into get_partition_ancestors(), it returns NIL in two cases: 1) No more parent 2) detach_pending is true Case 1 is an expected case, I doubt case 2 may fire the Assert. Say, partitioned table p has a leaf partition p1, now p1 is being detached. get_partition_ancestors(p1) may return NIL because of detach_pending, so root_relid is set to p1, but p1 is not a partitioned table, then this Assert is fired. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: A new C function `get_partition_root`.
От:
Chao Li <li.evan.chao@gmail.com>
Дата:
> On Aug 3, 2026, at 11:59, Peter Smith wrote:
>
> On Thu, Jul 30, 2026 at 3:46 PM Chao Li wrote:
>>
>>
>>
>>> On Jul 30, 2026, at 08:04, Peter Smith wrote:
>>>
>>> On Wed, Jul 29, 2026 at 7:18 PM shveta malik wrote:
>>>>
>>>> On Wed, Jul 29, 2026 at 2:28 PM Peter Smith wrote:
>>> ...
>>>>> BTW, although I think using sanity Assert was correct, just in case
>>>>> there is some unanticipated way to reach the C function with a bad
>>>>> relid, I've changed to use an errlog(ERROR).
>>>>> Thoughts?
>>>>
>>>> I don't immediately see any such possibility. I feel Assert is better.
>>>>
>>>
>>> OK. PSA v4, which is the same as v3, but uses Assert instead of elog.
>>>
>>> ======
>>> Kind Regards,
>>> Peter Smith.
>>> Fujitsu Australia
>>>
>>
>>
>> I just reviewed v4 and got a doubt:
>> ```
>> + /* Sanity check: The root must be a partitioned table */
>> + Assert(RELKIND_HAS_PARTITIONS(get_rel_relkind(root_relid)));
>> ```
>>
>> Looking into get_partition_ancestors(), it returns NIL in two cases:
>>
>> 1) No more parent
>> 2) detach_pending is true
>>
>> Case 1 is an expected case, I doubt case 2 may fire the Assert.
>>
>> Say, partitioned table p has a leaf partition p1, now p1 is being detached. get_partition_ancestors(p1) may return NIL because of detach_pending, so root_relid is set to p1, but p1 is not a partitioned table, then this Assert is fired.
>>
>
> Hi Chao-San.
>
> Thanks for reporting that issue.
>
> I've dealt with that now by exposing the `detach_pending` so now the
> `get_partition_root` can know whether a detach was the cause of
> ancestors == NIL.
>
> I also added another flag `even_if_detached` to `get_partition_root`
> so callers can decide what to do if a detach is in progress. That's
> analogous to other code that has a similar parameter.
>
> It seems ok using Shveta's 3 sessions example for testing.
>
Thanks for updating the patch.
Given the comment:
```
+ * Note: This should only be called when it is known that the relation is a
+ * partition or partitioned table.
```
Does it make sense to add an Assert for that, like:
```
Assert(get_rel_relispartition(relid) ||
RELKIND_HAS_PARTITIONS(get_rel_relkind(relid)));
```
Then, maybe we don’t need the final sanity check assert.
Otherwise v5 looks good to me. The new parameter even_if_detached matches the existing get_partition_parent().
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Re: A new C function `get_partition_root`.
On Mon, Aug 3, 2026 at 8:15 PM shveta malik wrote: > > On Mon, Aug 3, 2026 at 2:34 PM Álvaro Herrera wrote: > > > > Hi, > > > > I'd say this looks okay, but why do you need get_partition_root_guts() > > exposed in partition.h? In fact, it's not clear to me why you need a > > second routine at all. Why isn't enough to have just get_partition_root()? > > > > I think to avoid performing the validation twice in > pg_partition_root(): first via check_rel_can_be_partition(), and then > again in get_partition_root (see [1]), get_partition_root_guts() is > introduced and exposed in partition.h. > > [1]: > + /* Validate relid is member of a partition tree */ > + Assert(get_rel_relispartition(relid) || > + RELKIND_HAS_PARTITIONS(get_rel_relkind(relid))); > PSA patch v7, which has the following changes: 1. The wrapped function get_partition_root_guts() is removed. It previously existed in v6 only to avoid a double validation when called from the SQL pg_partition_root() function. Now, we chose not to care about double validation because the inner validation is Assert code, so it only happens in DEBUG build anyhow. 3. The status validation function check_rel_can_be_partition() is now common code so that it can be called from all places. 3. Similar validation (checking if relid can be part of a partition tree) is now also done for the get_partition_ancestors() C function. (Note: Existing code calls this function with RELKIND_INDEX/ RELKIND_PARTITIONED_INDEX, so the assertion is slightly different) ====== Kind Regards, Peter Smith. Fujitsu Australia