pgsql: Fix broken Bitmapset optimization in DiscreteKnapsack()

From: David Rowley <drowley(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix broken Bitmapset optimization in DiscreteKnapsack()
Date: 2024-01-18 21:45:17
Message-ID: E1rQaCL-0023mm-M2@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix broken Bitmapset optimization in DiscreteKnapsack()

Some code in DiscreteKnapsack() attempted to zero all words in a
Bitmapset by performing bms_del_members() to delete all the members from
itself before replacing those members with members from another set.
When that code was written, this was a valid way to manipulate the set
in such a way to save from palloc having to be called to allocate a new
Bitmapset. However, 00b41463c modified Bitmapsets so that an empty set is
*always* represented as NULL and this breaks the optimization as the
Bitmapset code will always pfree the memory when the set becomes empty.

Since DiscreteKnapsack() has been coded to avoid as many unneeded
allocations as possible, it seems risky to not fix this. Here we add
bms_replace_members() to effectively perform an in-place copy of another
set, reusing the memory of the existing set, when possible.

This got broken in v16, but no backpatch for now as there've been no
complaints.

Reviewed-by: Richard Guo
Discussion: https://postgr.es/m/CAApHDvoTCBkBU2PJghNOFUiO0q=QP4WAWHi5sJP6_4=b2WodrA@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/4b310636439da432c473e0e85de661b3342bb745

Modified Files
--------------
src/backend/lib/knapsack.c | 5 +----
src/backend/nodes/bitmapset.c | 44 +++++++++++++++++++++++++++++++++++++++++++
src/include/nodes/bitmapset.h | 1 +
3 files changed, 46 insertions(+), 4 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Jeff Davis 2024-01-18 23:21:46 pgsql: Fix buildfarm error from commit 5c31669058.
Previous Message Tom Lane 2024-01-18 21:11:32 pgsql: Fix plpgsql to allow new-style SQL CREATE FUNCTION as a SQL comm