Fix bug in the new ResourceOwner implementation.
authorHeikki Linnakangas <[email protected]>
Wed, 8 Nov 2023 23:33:14 +0000 (01:33 +0200)
committerHeikki Linnakangas <[email protected]>
Wed, 8 Nov 2023 23:33:14 +0000 (01:33 +0200)
When the hash table is in use, ResoureOwnerSort() moves any elements
from the small fixed-size array to the hash table, and sorts it. When
the hash table is not in use, it sorts the elements in the small
fixed-size array directly. However, ResourceOwnerSort() and
ResourceOwnerReleaseAll() had different idea on when the hash table is
in use: ResourceOwnerSort() checked owner->nhash != 0, and
ResourceOwnerReleaseAll() checked owner->hash != NULL. If the hash
table was allocated but was currently empty, you hit an assertion
failure.

Reported-by: Alexander Lakhin <[email protected]>
Discussion: https://www.postgresql.org/message-id/be58d565-9e95-d417-4e47-f6bd408dea4b@gmail.com

src/backend/utils/resowner/resowner.c

index eecb3ade3d7c06969f0f5d501881ddc2644dd680..f096f3df20a72d047b4580429c37503519353635 100644 (file)
@@ -349,10 +349,13 @@ ResourceOwnerReleaseAll(ResourceOwner owner, ResourceReleasePhase phase,
    ResourceElem *items;
    uint32      nitems;
 
-   /* ResourceOwnerSort must've been called already */
+   /*
+    * ResourceOwnerSort must've been called already.  All the resources are
+    * either in the array or the hash.
+    */
    Assert(owner->releasing);
    Assert(owner->sorted);
-   if (!owner->hash)
+   if (owner->nhash == 0)
    {
        items = owner->arr;
        nitems = owner->narr;
@@ -393,7 +396,7 @@ ResourceOwnerReleaseAll(ResourceOwner owner, ResourceReleasePhase phase,
        kind->ReleaseResource(value);
        nitems--;
    }
-   if (!owner->hash)
+   if (owner->nhash == 0)
        owner->narr = nitems;
    else
        owner->nhash = nitems;