On Fri, Jul 17, 2026 at 9:41 AM Peter Smith <[email protected]> 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


Reply via email to