pgsql: Require empty Bitmapsets to be represented as NULL. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Require empty Bitmapsets to be represented as NULL.
Date
Msg-id E1pXmJY-001Xy5-W5@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Require empty Bitmapsets to be represented as NULL.

When I designed the Bitmapset module, I set things up so that an empty
Bitmapset could be represented either by a NULL pointer, or by an
allocated object all of whose bits are zero.  I've recently come to
the conclusion that that was a bad idea and we should instead have a
convention like the longstanding invariant for Lists, whereby an empty
list is represented by NIL and nothing else.

To do this, we need to fix bms_intersect, bms_difference, and a couple
of other functions to check for having produced an empty result; but
then we can replace bms_is_empty(a) by a simple "a == NULL" test.

This is very likely a (marginal) win performance-wise, because we
call bms_is_empty many more times than those other functions put
together.  However, the real reason to do it is that we have various
places that have hand-implemented a rule about "this Bitmapset
variable must be exactly NULL if empty", so that they can use
checks-for-null in place of bms_is_empty calls in particularly hot
code paths.  That is a really fragile, mistake-prone way to do things,
and I'm surprised that we've seldom been bitten by it.  It's not well
documented at all which variables have this property, so you can't
readily tell which code might be violating those conventions.  By
making the convention universal, we can eliminate a subtle source of
bugs.

Patch by me; thanks to Nathan Bossart and Richard Guo for review.

Discussion: https://postgr.es/m/1159933.1677621588@sss.pgh.pa.us

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/00b41463c21615f9bf3927f207e37f9e215d32e6

Modified Files
--------------
src/backend/nodes/bitmapset.c | 70 ++++++++++++++++++++++++++++++++-----------
src/include/nodes/bitmapset.h | 10 +++----
2 files changed, 57 insertions(+), 23 deletions(-)


pgsql-committers by date:

Previous
From: Daniel Gustafsson
Date:
Subject: pgsql: Mark options as deprecated in usage output
Next
From: Thomas Munro
Date:
Subject: pgsql: Don't leak descriptors into subprograms.