Re: BUG #19524: NaN handling in btree_gist's float4/float8 opclasses
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Bill Kim <billkimjh@gmail.com>
Cc: Daniel Gustafsson <daniel@yesql.se>, pgsql-bugs@lists.postgresql.org
Date: 2026-06-29T23:08:42Z
Lists: pgsql-bugs
Attachments
- v2-0001-btree_gist-fix-NaN-handling-in-float4-float8-opcl.patch (text/x-diff) patch v2-0001
I studied this and decided that it only partially fixes the problem. Yes, we've got to fix the comparison functions, but we also have to fix the penalty and distance functions. As things stand, the penalty functions will probably return NaN for any input including a NaN, which gistpenalty will clamp to zero, which we do not want because it'll basically break all decisions about where to put things, leading to a seriously inefficient index. (Maybe gistpenalty should do something else with a NaN, but I'm hesitant to touch that right now.) The penalty logic is also under the misapprehension that multiplying everything by 0.49 keeps it from having to worry about overflows; that won't fix things for infinities. The distance functions will do the wrong things for NaN as well, probably breaking any KNN search that happens across a NaN index entry. So I fixed all that, and then was dismayed to find that the new penalty logic wasn't reached at all in the regression tests, per code coverage testing. We don't compute any penalties during GiST index build unless it's a multicolumn index, so most of the per-datatype tests aren't reaching that. It didn't seem like adding more index entries partway through the test would fit very well into the structure of float[48].sql, so instead I added simple tests of two-column float indexes to improve the coverage. Also, I thought we should test this logic by adding NaN (and infinities for good measure) to the initial data load, rather than creating a new small table which would only exercise the logic very minimally. At the moment I'm leaning to back-patching this. We'd have to release-note the need to reindex any btree_gist indexes containing NaNs, but we've done similar things many times before. regards, tom lane
Commits
-
btree_gist: fix NaN handling in float4/float8 opclasses.
- d569ccd40858 16 (unreleased) landed
- d215d2cc2ae1 17 (unreleased) landed
- a47005f0b11d 19 (unreleased) landed
- 98dd4406f7a4 15 (unreleased) landed
- 7d3448961da3 master landed
- 255bce44884e 14 (unreleased) landed
- 1e1d07792e08 18 (unreleased) landed