From 3ad4c8615a7616c0d3f6bf6fe9bdd22d43c95813 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Mon, 19 Aug 2024 16:09:10 -0400 Subject: [PATCH] Avoid failure to open dropped detached partition MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit When a partition is detached and immediately dropped, a prepared statement could try to compute a new partition descriptor that includes it. This leads to this kind of error: ERROR: could not open relation with OID 457639 Avoid this by skipping the partition in expand_partitioned_rtentry if it doesn't exist. Noted by me while investigating bug #18559. Kuntal Gosh helped to identify the exact failure. Backpatch to 14, where DETACH CONCURRENTLY was introduced. Author: Álvaro Herrera Reviewed-by: Kuntal Ghosh Reviewed-by: Junwang Zhao Discussion: https://postgr.es/m/202408122233.bo4adt3vh5bi@alvherre.pgsql --- src/backend/optimizer/util/inherit.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/backend/optimizer/util/inherit.c b/src/backend/optimizer/util/inherit.c index 7ea758049ed..b7ed598a48a 100644 --- a/src/backend/optimizer/util/inherit.c +++ b/src/backend/optimizer/util/inherit.c @@ -378,8 +378,14 @@ expand_partitioned_rtentry(PlannerInfo *root, RelOptInfo *relinfo, Index childRTindex; RelOptInfo *childrelinfo; - /* Open rel, acquiring required locks */ - childrel = table_open(childOID, lockmode); + /* + * Open rel, acquiring required locks. If a partition was recently + * detached and subsequently dropped, then opening it will fail. In + * this case, behave as though the partition had been pruned. + */ + childrel = try_table_open(childOID, lockmode); + if (childrel == NULL) + continue; /* * Temporary partitions belonging to other sessions should have been -- 2.39.5