BUG #19949: pg_partition_tree() returns dropped partition from cached OID list
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
The following bug has been logged on the website:
Bug reference: 19949
Logged by: Qifan Liu
Email address: imchifan@163.com
PostgreSQL version: 19.0
Operating system: Linux/amd64
Description:
Hello,
pg_partition_tree() can return a relid that no longer exists if a volatile
sibling expression drops a partition while the function is producing rows.
The missing regclass is displayed as its numeric OID.
Run the following with psql in any database as a role with CREATE privilege
on that database:
\set ON_ERROR_STOP on
CREATE SCHEMA p;
CREATE TABLE p.root(a int) PARTITION BY RANGE (a);
CREATE TABLE p.p1 PARTITION OF p.root FOR VALUES FROM (0) TO (10);
CREATE TABLE p.p2 PARTITION OF p.root FOR VALUES FROM (10) TO (20);
CREATE TEMP TABLE expected AS
SELECT 'p.p1'::regclass::oid AS dropped_oid;
CREATE FUNCTION p.drop_p1()
RETURNS int
LANGUAGE plpgsql VOLATILE
AS $$
BEGIN
IF to_regclass('p.p1') IS NOT NULL THEN
EXECUTE 'DROP TABLE p.p1';
END IF;
RETURN 1;
END
$$;
CREATE TEMP TABLE results AS
SELECT t.*, p.drop_p1() AS side_effect
FROM pg_partition_tree('p.root'::regclass) AS t;
SELECT count(*) AS total_rows,
count(*) FILTER (WHERE c.oid IS NULL) AS stale_rows,
count(*) FILTER (
WHERE c.oid IS NULL AND e.dropped_oid IS NOT NULL
) AS dropped_partition_rows,
to_regclass('p.p1') IS NULL AS partition_was_dropped
FROM results r
LEFT JOIN pg_class c ON c.oid = r.relid
LEFT JOIN expected e ON e.dropped_oid = r.relid;
TABLE results;
The aggregate query prints:
total_rows | stale_rows | dropped_partition_rows | partition_was_dropped
------------+------------+------------------------+-----------------------
3 | 1 | 1 | t
(1 row)
The results contain the root, p.p2, and a numeric relid for the dropped
p.p1. Thus pg_partition_tree() emits all three identifiers cached before the
drop even though one no longer has a pg_class row.
I expected pg_partition_tree() not to emit a cached relation identifier
after that relation ceased to exist, for example by revalidating identifiers
when it resumes after the volatile expression.