Re: BUG #19705: One NaN box makes a BRIN box_inclusion_ops index omit unrelated rows

From: Greg Burd <greg(at)burd(dot)me>
To: Manu <manuelreyesbravo(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org, shihao zhong <zhong950419(at)gmail(dot)com>, John Naylor <johncnaylorls(at)gmail(dot)com>, Tomas Vondra <tomas(at)vondra(dot)me>, Heikki Linnakangas <heikki(dot)linnakangas(at)iki(dot)fi>, Kirill Reshke <reshkekirill(at)gmail(dot)com>, Andrey Borodin <x4mmm(at)yandex-team(dot)ru>, kehan5800(at)gmail(dot)com
Subject: Re: BUG #19705: One NaN box makes a BRIN box_inclusion_ops index omit unrelated rows
Date: 2026-12-04 19:52:48
Message-ID: 179641037197.1853587.17462786960454762039@burd.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Thu, Oct 22, 2026 at 12:22 AM Manu <manuelreyesbravo(at)gmail(dot)com> wrote:

Hi Shihao, Manu,

Thanks, Manu, for tracing the polygon failure to point_inside(); it
settled which side was wrong.

I have a patch on -hackers that touches the same GiST distance code as
v6-0002 and v6-0004 [1]. It makes <->(point, box) exact (it picked the
wrong edge for points near an edge's line) and has GiST and SP-GiST use
the operator's function. The two patches agree on the NaN rule, and I'd
like them to fit together before either goes in.

The rule is v6's: a key that only bounds the rows below it, if it has a
NaN, gets distance 0, because NaN sorts last and is no bound. My v2 [2]
moves that into one function, point_box_distance_bound(), which GiST and
SP-GiST both use. So it covers SP-GiST too, which v6 does not: there a
NaN key still gets NaN and the rows under it come back late. In my
index-versus-seq-scan comparison, v6 leaves 36 failures, all SP-GiST;
with both patches 4 remain, the all-NaN polygon case from
point_inside().

There is one difference. For a box_ops leaf with a NaN, v6-0004 asks
the operator, which is right, but the operator itself returns an
arbitrary number there, 0.5 for (0.5,0.5) and ((0,0),(1,NaN)), depending
on which comparisons the NaN fails. My patch makes it NaN, as <->
between points already is, so the leaf and the seq scan agree on NaN.
With both applied, v6's first gist_nan_knn_tbl query returns 0, 0.5,
0.5, 0.707, 1.5: the '(1,NaN),(0,0)' row moves from 0.5 to the end, in
both scans. No other test changes.

Stacked on v6, the conflict is textual and only in gistproc.c. The
resolution keeps v6's union-key and consistent changes, uses my two
functions for the distances, and drops distance_has_nan() and the
dist_bp() call in gist_box_distance(). Your p1 query, my infinite point
case from October 20, and bug #19937 [3] all match the seq scan.

Shihao, which order do you prefer: mine first and you rebase 0002 and
0004 on it, or the reverse? Either is fine with me. Mine changes the
operator's result for NaN boxes, so I'd keep it to master; for the back
branches v6-0004 as it stands is the fix.

> SP-GiST poly_ops still fails with v6: the same rows in an SP-GiST
> index raise the error for (0.5,0.5), (1e308,1e308), (Infinity,0),
> (-Infinity,-Infinity) and (Infinity,Infinity).

My patch fixes these: with it, alone or on top of v6, your p1 rows in an
SP-GiST index return 0 for all five points, as the seq scan does. The
SP-GiST bound was NaN for the NaN key, and now it is 0.

best.

-greg

[1] https://postgr.es/m/179631667918.2040837.8991557170201867655@burd.me
[2] https://postgr.es/m/179640348693.708287.16076171982731144440@burd.me
[3] https://postgr.es/m/19937-59d2a563ceecb0ab@postgresql.org

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Aman Ganapathy 2026-12-04 17:43:11 Re: BUG #19968: ALTER COLUMN TYPE fails with internal errors if a dependent object is dropped concurrently