Fix creation of partition descriptor during concurrent detach+drop
authorAlvaro Herrera <[email protected]>
Mon, 12 Aug 2024 22:17:56 +0000 (18:17 -0400)
committerAlvaro Herrera <[email protected]>
Mon, 12 Aug 2024 22:17:56 +0000 (18:17 -0400)
If a partition undergoes DETACH CONCURRENTLY immediately followed by
DROP, this could cause a problem for a concurrent transaction
recomputing the partition descriptor when running a prepared statement,
because it tries to dereference a pointer to a tuple that's not found in
a catalog scan.

The existing retry logic added in commit dbca3469ebf8 is sufficient to
cope with the overall problem, provided we don't try to dereference a
non-existant heap tuple.

Arguably, the code in RelationBuildPartitionDesc() has been wrong all
along, since no check was added in commit 898e5e3290a7 against receiving
a NULL tuple from the catalog scan; that bug has only become
user-visible with DETACH CONCURRENTLY which was added in branch 14.
Therefore, even though there's no known mechanism to cause a crash
because of this, backpatch the addition of such a check to all supported
branches.  In branches prior to 14, this would cause the code to fail
with a "missing relpartbound for relation XYZ" error instead of
crashing; that's okay, because there are no reports of such behavior
anyway.

Author: Kuntal Ghosh <[email protected]>
Reviewed-by: Junwang Zhao <[email protected]>
Reviewed-by: Tender Wang <[email protected]>
Discussion: https://postgr.es/m/18559-b48286d2eacd9a4e@postgresql.org

src/backend/partitioning/partdesc.c

index ae193681b968bcfd8a40751dc71f10aac438a303..ae52432f7bfe763f6cf60acae3617aaefd1a9c08 100644 (file)
@@ -130,8 +130,6 @@ RelationBuildPartitionDesc(Relation rel)
            Relation    pg_class;
            SysScanDesc scan;
            ScanKeyData key[1];
-           Datum       datum;
-           bool        isnull;
 
            pg_class = table_open(RelationRelationId, AccessShareLock);
            ScanKeyInit(&key[0],
@@ -141,10 +139,16 @@ RelationBuildPartitionDesc(Relation rel)
            scan = systable_beginscan(pg_class, ClassOidIndexId, true,
                                      NULL, 1, key);
            tuple = systable_getnext(scan);
-           datum = heap_getattr(tuple, Anum_pg_class_relpartbound,
-                                RelationGetDescr(pg_class), &isnull);
-           if (!isnull)
-               boundspec = stringToNode(TextDatumGetCString(datum));
+           if (HeapTupleIsValid(tuple))
+           {
+               Datum       datum;
+               bool        isnull;
+
+               datum = heap_getattr(tuple, Anum_pg_class_relpartbound,
+                                    RelationGetDescr(pg_class), &isnull);
+               if (!isnull)
+                   boundspec = stringToNode(TextDatumGetCString(datum));
+           }
            systable_endscan(scan);
            table_close(pg_class, AccessShareLock);
        }