pgsql: Fix broken Bitmapset optimization in DiscreteKnapsack() - Mailing list pgsql-committers

From David Rowley
Subject pgsql: Fix broken Bitmapset optimization in DiscreteKnapsack()
Date
Msg-id E1rQaCL-0023mm-M2@gemulon.postgresql.org
Whole thread Raw
List 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(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Fix plpgsql to allow new-style SQL CREATE FUNCTION as a SQL comm
Next
From: Jeff Davis
Date:
Subject: pgsql: Fix buildfarm error from commit 5c31669058.