Notes for v2 of "Compute the distance between a point and a box exactly" ======================================================================= 1. Reproducing the v1 bug ------------------------- A NaN point inserted after the build puts a NaN in the high corner of its leaf page's internal key. v1 returned NaN as that key's distance, which sorts after every number, so every row under the key came out after all the others. CREATE TABLE t (p point); INSERT INTO t SELECT point(x, y) FROM generate_series(0, 44) x, generate_series(0, 44) y; CREATE INDEX ON t USING gist (p); INSERT INTO t VALUES ('(1,NaN)'); SET enable_seqscan = off; SET enable_bitmapscan = off; SELECT rn, p, dist, prev FROM ( SELECT row_number() OVER () rn, p, dist, lag(dist) OVER () prev FROM (SELECT p, p <-> point '(0.5,0.5)' dist FROM t ORDER BY p <-> point '(0.5,0.5)') s) ss WHERE dist < prev OR rn <= 3; master: server crash, Assert("box->low.y <= box->high.y") in computeDistance(); "inconsistent point values" without assertions v1: rows 1-3 are (0,8), (1,8), (2,8) at 7.52 to 7.65; row 1930 is (0,0) at 0.707, after a row at 61.5 v2: rows 1-3 are (0,0), (0,1), (1,0) at 0.707; no row is closer than the one before it box.sql in v2 has this case, plus the same check for GiST and SP-GiST box_ops, where the NaN row's distance is NaN (the operator's) and it comes last. The test fails on v1 and crashes master. 2. Index scan versus seq scan ----------------------------- For each opclass, a 45x45 grid of rows plus special rows. For each query point, the distances from ORDER BY col <-> q, all rows, are compared between a seq scan and the index scan. Each check runs in its own session, so a crash costs one cell. Built with assertions. Opclasses: GiST point_ops, box_ops, poly_ops, circle_ops; SP-GiST quad_point_ops, kd_point_ops, box_ops, poly_ops. Layouts: no special row; one all-NaN row first; one partly-NaN row (e.g. '(1,NaN),(0,0)') first, last, or inserted after the build; 3000 partly-NaN rows; one infinite row first. Query points: (0.5,0.5), (99,99), (50.25,-3), (22.5,22.5), (Infinity,0), (1e300,1e300), (NaN,NaN), (1,NaN). 448 cells, 408 run. The other 40 are SP-GiST quad_point_ops with a NaN row, where CREATE INDEX itself fails with "getQuadrant: impossible case" on every build here; that is separate. Failures per opclass (any query point / finite query points only): master v1 v2 gist point 21 / 14 21 / 20 0 / 0 gist box 23 / 16 22 / 22 0 / 0 gist polygon 40 / 26 44 / 30 0 / 0 gist circle 21 / 14 25 / 25 0 / 0 spgist quad 0 / 0 0 / 0 0 / 0 spgist kd 6 / 5 6 / 5 0 / 0 spgist box 12 / 11 0 / 0 0 / 0 spgist polygon 18 / 4 44 / 30 4 / 4 total 141 162 4 crash 38 0 0 error 53 92 0 wrong order 50 70 4 The 4 left on v2 are SP-GiST poly_ops with a polygon whose vertices are all NaN, at four of the finite query points. point_inside() puts any point inside such a polygon, so its rechecked distance is 0 for a polygon that is nowhere. Master and CF 7370 fail the same 4. The point_inside() fix belongs on the BUG #19705 thread, where Shihao proposed a separate patch for it. 3. CF 7370 ---------- CF 7370 (BUG #19705) fixes NaN keys in GiST: union keys map NaN to infinity, and existing NaN keys match every search and get distance 0. That 0 is the same bound as point_box_distance_bound(). Applied on top of v6 of that series, this patch conflicts only textually in gistproc.c. The resolution keeps v6's union-key and consistent changes and uses the two functions here for the distances. With both applied the comparison above has the same 4 failures as v2 alone; v6 alone has 36, mostly SP-GiST, which v6 does not change. One expected row in v6's gist.out changes, because the NaN box's distance is now NaN rather than 0.5. 4. Testing ---------- Builds without warnings (gcc 16, cassert) and is pgindent clean. make check passes. Test-world has one failure, recovery/045_archive_restartpoint ("pg_ctl stop failed" while the archiver copies a segment), which fails the same way on unpatched master under the same parallel load and passes alone on both. The CF 7392 series applies on top of v2 without conflicts and passes its regress and isolation suites. CI on Linux (64- and 32-bit, meson and autoconf), Windows (VS and MinGW) and CompilerWarnings passed; the macOS job was cancelled by GitHub for lack of a runner, as on other branches.