]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix selectivity estimation integer overflow in contrib/intarray
authorDavid Rowley <drowley@postgresql.org>
Sun, 4 Jan 2026 07:34:22 +0000 (20:34 +1300)
committerDavid Rowley <drowley@postgresql.org>
Sun, 4 Jan 2026 07:34:22 +0000 (20:34 +1300)
commitc89510431af2efe447d3f069d1f6ea417efdf62e
tree6e75ff7c6f546dff0cd85d0d9109de3299c2ea0b
parent176fd835008a4aeecd3295533d7d7d026bed00d8
Fix selectivity estimation integer overflow in contrib/intarray

This fixes a poorly written integer comparison function which was
performing subtraction in an attempt to return a negative value when
a < b and a positive value when a > b, and 0 when the values were equal.
Unfortunately that didn't always work correctly due to two's complement
having the INT_MIN 1 further from zero than INT_MAX.  This could result
in an overflow and cause the comparison function to return an incorrect
result, which would result in the binary search failing to find the
value being searched for.

This could cause poor selectivity estimates when the statistics stored
the value of INT_MAX (2147483647) and the value being searched for was
large enough to result in the binary search doing a comparison with that
INT_MAX value.

Author: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: David Rowley <dgrowleyml@gmail.com>
Discussion: https://postgr.es/m/CAEoWx2ng1Ot5LoKbVU-Dh---dFTUZWJRH8wv2chBu29fnNDMaQ@mail.gmail.com
Backpatch-through: 14
contrib/intarray/_int_selfuncs.c