Report
GiST point_ops/box_ops/circle_ops/poly_ops index scans skip finite rows when a NaN point or box shares a subtree: the page union gets a NaN high bound and the non-NaN-aware FPle/FPgt tests prune it
- Severity
- 38
- Blamed commit
- 1acf7572554515b99ef6e783750aaea8777524ec
On 12 branches
REL9_6_STABLE, REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
GiST indexes on point, box, circle and polygon columns return fewer rows than a sequential scan once the indexed data contains a NaN coordinate. The NaN row does not need to match the query: a NaN value poisons the bounding box of the internal page that covers it, and the search code then refuses to descend into that page, so the finite rows below it are silently skipped.
Reproduction
Run on a fresh database. Sorted index build is deterministic for these tables; the box count varies slightly between runs but is always below 1000.
CREATE TABLE gp AS
SELECT point(i % 40, i / 40) AS v FROM generate_series(0, 999) i;
INSERT INTO gp VALUES ('(NaN,NaN)');
CREATE INDEX gp_idx ON gp USING gist (v);
SET enable_seqscan = off; SET enable_bitmapscan = off;
SET enable_indexonlyscan = off;
SELECT count(*) FROM gp WHERE v >> '(5,5)'; -- 689
RESET enable_seqscan; RESET enable_bitmapscan;
RESET enable_indexonlyscan;
SET enable_indexscan = off; SET enable_bitmapscan = off;
SET enable_indexonlyscan = off;
SELECT count(*) FROM gp WHERE v >> '(5,5)'; -- 850
RESET enable_indexscan; RESET enable_bitmapscan;
RESET enable_indexonlyscan;
CREATE TABLE gb AS
SELECT box(point(i % 40, i / 40),
point(i % 40 + 0.5, i / 40 + 0.5)) AS v
FROM generate_series(0, 999) i;
INSERT INTO gb VALUES ('((NaN,NaN),(NaN,NaN))');
CREATE INDEX gb_idx ON gb USING gist (v);
SET enable_seqscan = off; SET enable_bitmapscan = off;
SET enable_indexonlyscan = off;
SELECT count(*) FROM gb WHERE v <@ '((0,0),(40,40))'::box;
-- 916 (varies, always < 1000)
RESET enable_seqscan; RESET enable_bitmapscan;
RESET enable_indexonlyscan;
SET enable_indexscan = off; SET enable_bitmapscan = off;
SET enable_indexonlyscan = off;
SELECT count(*) FROM gb WHERE v <@ '((0,0),(40,40))'::box;
-- 1000
Expected: the same count from the index scan and the seq scan (the NaN row never satisfies these operators in either plan). Actual: the index scan loses the finite rows stored under the page whose key became NaN.
The same mismatch shows up with circle_ops (<@ against a circle
returns fewer rows than a seq scan once one <(NaN,NaN),1> is
present), poly_ops (<@, and point <@ polygon on point_ops), and
point_ops |>>. One NaN coordinate is enough: a single '(NaN,3)'
point gives the same >> mismatch (689 vs 850). If the index is
built first and NaN rows are inserted afterwards (for example 30 rows
of '(NaN,NaN)' into an existing 1000-row point index), v ~= '(5,5)'
returns 0 rows through the index and 1 through a seq scan; which leaf
a NaN row lands on is chosen at random by gistchoose(), so that
variant is not deterministic with a single insert.
Dumping the root page with pageinspect shows the cause, an internal key with a NaN high corner next to finite children:
(v)=("(NaN,NaN),(33,2)")
Analysis
- The GiST union and picksplit code in gistproc.c (rt_box_union() and adjustBox(), introduced by 1acf7572554 and later moved to the float8_max()/float8_min()/float8_lt() helpers) orders NaN above every other value, so a union containing a NaN coordinate gets that NaN as its high.x/high.y bound (the low bound becomes NaN only when every member is NaN).
- The search side does not follow that convention. The consistent
functions use FPle(), FPge(), FPgt() and FPlt() from geo_decls.h,
whose header comment says they are "not NaN-aware and will give
FALSE for any case involving NaN inputs". For point_ops,
gist_point_consistent_internal() tests e.g. FPgt(key->high.x,
query->x) for
>>and FPle(query->y, key->high.y) for~=on non-leaf pages; with key->high.x or key->high.y NaN they return false and the whole subtree is pruned. rtree_internal_consistent() (used for box, circle and polygon through their bounding boxes) calls box_overlap() and box_contain() for&&,<@,@>and~=, which use the same FP comparisons and also fail for a NaN key. - The commit message of 1acf7572554 says "It's likely that the index search side of things still needs some work", which is the part left undone. The result is an inconsistency between index and heap evaluation: the operators evaluated on the heap never match the NaN row, but the index also hides unrelated finite rows.
- A related report exists for BRIN (box_inclusion_ops summary absorbs a NaN box, blame b0b7be61); this is the GiST counterpart with a different root cause and different code. SP-GiST quad_point_ops has its own NaN problem (8daeb5dd) and is not covered here.
Notes
Tested at master dc168a83874fbf5cee4afb76a623e1349a539724 (assert
build). KNN (<->) behaviour with NaN is a separate, already
reported problem and is not part of this report.
Branches
1acf7572554 is on every supported branch (REL_14_STABLE through master). Reproduced on master only; back branches not yet tested.
Related
- 01a214ea-95c2-79fe-995f-2b05121ecc29 (BRIN box_inclusion_ops counterpart; different code)
- 01a20c51-5020-7f12-9c1b-e79ca65a559e (GiST point_ops KNN with NaN; separate problem)
- 01a1d16b-747c-727a-8586-467b314febf8 (SP-GiST quad_point_ops NaN centroid; separate problem)