BUG #19949: pg_partition_tree() returns dropped partition from cached OID list

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: imchifan(at)163(dot)com
Subject: BUG #19949: pg_partition_tree() returns dropped partition from cached OID list
Date: 2026-11-25 17:30:01
Message-ID: 19949-96d2cda049b9f558@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19949
Logged by: Qifan Liu
Email address: imchifan(at)163(dot)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.

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2026-11-25 17:34:05 BUG #19950: pg_terminate_backend rejects a positive bigint timeout as negative
Previous Message Kacper Kuras 2026-11-25 17:27:21 Re: BUG #18754: Logical replication cannot restart if client sent a copyDone