| From: | PG Bug reporting form <noreply(at)postgresql(dot)org> |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Cc: | 798604270(at)qq(dot)com |
| Subject: | BUG #19938: BRIN index on box silently drops ordinary matching rows when a NaN box shares the same page range |
| Date: | 2026-11-25 06:40:19 |
| Message-ID: | 19938-fbb30b17957430dd@postgresql.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
The following bug has been logged on the website:
Bug reference: 19938
Logged by: Chi Zhang
Email address: 798604270(at)qq(dot)com
PostgreSQL version: 19.0
Operating system: Ubuntu 24.04
Description:
Hi,
I found that a BRIN index on a box column (brin/box_inclusion_ops) silently
returns wrong results—omitting completely ordinary, finite matching rows—for
overlap (&&) queries (and similarly containment/contained-by queries)
whenever a box containing a NaN coordinate resides in the same BRIN page
range.
This is the test case:
```
CREATE TEMP TABLE tb_brin (id int, b box);
INSERT INTO tb_brin VALUES (1, box(point('NaN', 0), point(1, 1))), (2,
box(point(0, 0), point(1, 1)));
CREATE INDEX tb_brin_idx ON tb_brin USING brin (b);
-- Sequential scan returns row 2:
SET enable_seqscan = on; SET enable_bitmapscan = off;
SELECT id FROM tb_brin WHERE b && box(point(0, 0), point(2, 2));
-- BRIN bitmap index scan silently returns 0 rows:
SET enable_seqscan = off; SET enable_bitmapscan = on;
SELECT id FROM tb_brin WHERE b && box(point(0, 0), point(2, 2));
```
I can reproduce this on PostgreSQL 20devel (master, commit b4cc5e924b7a).
The root cause might be that brin/box_inclusion_ops
(postgres/src/include/catalog/pg_amproc.dat:2015-2030) uses bound_box
(boxes_bound_box, postgres/src/backend/utils/adt/geo_ops.c:4414-4428) as
PROCNUM_MERGE and defines no PROCNUM_MERGEABLE function. When
brin_inclusion_add_value()
(postgres/src/backend/access/brin/brin_inclusion.c:138-238) summarizes a
page range containing box(point('NaN', 0), point(1, 1)), boxes_bound_box()
uses float8_max() (which orders NaN above finite numbers), permanently
setting the page-range INCLUSION_UNION summary's high.x to NaN even after
merging finite boxes like (1,1),(0,0). During query evaluation,
brin_inclusion_consistent()
(postgres/src/backend/access/brin/brin_inclusion.c:348-356) tests
RTOverlapStrategyNumber (&&) by calling box_overlap(unionval, query) →
box_ov() (postgres/src/backend/utils/adt/geo_ops.c:578-584). Because
box_ov() uses raw IEEE-754 comparisons (FPle(query->low.x, unionval->high.x)
→ 0 <= NaN is false), brin_inclusion_consistent() returns false and prunes
the entire page range, dropping all finite matching rows in that range.
| From | Date | Subject | |
|---|---|---|---|
| Next Message | PG Bug reporting form | 2026-11-25 07:39:07 | BUG #19939: In a read-only transaction, a plain SELECT ... INTO is refused, but the same statement under EXPLAIN |
| Previous Message | PG Bug reporting form | 2026-11-25 06:14:51 | BUG #19937: Assertion failure (SIGABRT) in GiST KNN scan (computeDistance) on box with NaN coordinate |