]> granicus.if.org Git - postgresql/commit
Fix GiST index build for NaN values in geometric types.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 14 Jul 2016 22:46:00 +0000 (18:46 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 14 Jul 2016 22:46:00 +0000 (18:46 -0400)
commit57dba87a72278468bc4bf5d025090a713a96fb1a
treed2c11601b8aa2dd4926d20066fc32231b795a8d1
parent7d70bf97b2b3d946c6f2eec7896a2f794921cb72
Fix GiST index build for NaN values in geometric types.

GiST index build could go into an infinite loop when presented with boxes
(or points, circles or polygons) containing NaN component values.  This
happened essentially because the code assumed that x == x is true for any
"double" value x; but it's not true for NaNs.  The looping behavior was not
the only problem though: we also attempted to sort the items using simple
double comparisons.  Since NaNs violate the trichotomy law, qsort could
(in principle at least) get arbitrarily confused and mess up the sorting of
ordinary values as well as NaNs.  And we based splitting choices on box size
calculations that could produce NaNs, again resulting in undesirable
behavior.

To fix, replace all comparisons of doubles in this logic with
float8_cmp_internal, which is NaN-aware and is careful to sort NaNs
consistently, higher than any non-NaN.  Also rearrange the box size
calculation to not produce NaNs; instead it should produce an infinity
for a box with NaN on one side and not-NaN on the other.

I don't by any means claim that this solves all problems with NaNs in
geometric values, but it should at least make GiST index insertion work
reliably with such data.  It's likely that the index search side of things
still needs some work, and probably regular geometric operations too.
But with this patch we're laying down a convention for how such cases
ought to behave.

Per bug #14238 from Guang-Dih Lei.  Back-patch to 9.2; the code used before
commit 7f3bd86843e5aad8 is quite different and doesn't lock up on my simple
test case, nor on the submitter's dataset.

Report: <20160708151747.1426.60150@wrigleys.postgresql.org>
Discussion: <28685.1468246504@sss.pgh.pa.us>
src/backend/access/gist/gistproc.c
src/backend/utils/adt/float.c
src/include/utils/builtins.h