Avoid failure to open dropped detached partition
authorAlvaro Herrera <[email protected]>
Mon, 19 Aug 2024 20:09:10 +0000 (16:09 -0400)
committerAlvaro Herrera <[email protected]>
Mon, 19 Aug 2024 20:09:10 +0000 (16:09 -0400)
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 <[email protected]>
Reviewed-by: Kuntal Ghosh <[email protected]>
Reviewed-by: Junwang Zhao <[email protected]>
Discussion: https://postgr.es/m/202408122233[email protected]

src/backend/optimizer/util/inherit.c

index 4797312ae53eab74bbe6edcaa7ef6166bac24e7d..c5b906a9d43681a87eccdb6a7ddba8cac690cc34 100644 (file)
@@ -386,8 +386,17 @@ 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)
+       {
+           relinfo->live_parts = bms_del_member(relinfo->live_parts, i);
+           continue;
+       }
 
        /*
         * Temporary partitions belonging to other sessions should have been