postgres.git / summary / log / commit / refs
commit b0d482b33ffd119c8d5619a10734a532ee910ea1
Author: John Naylor <john.naylor@postgresql.org>
Date: Fri Oct 30 05:31:39 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0
Check all four box corners in SP-GiST quad tree <@ search
When the centroid lies outside the query box, spg_quad_inner_consistent()
keeps only the quadrants that contain a corner of the box. It meant to
test all four corners, but the last step set x back to low.x instead of
y to low.y, so it tested (low.x, high.y) twice and never (high.x, low.y).
With exact comparisons the missing corner would not matter: a box that
does not contain the centroid lies on one side of it on at least one
axis, so (high.x, low.y) shares a quadrant with another corner, but
getQuadrant() compares with FPeq(), which tiebreaks to the lowest
numbered quadrant.
When a box edge lies within EPSILON of the centroid, the corner on that
edge counts as being on the centroid's line, and (high.x, low.y) may be
the only corner in its quadrant. That quadrant was then skipped. Fix
the missing corner test.
Discussion: https://postgr.es/m/CANWCAZZZpkwrX0kC4Jo6KM27J6dyBdCDir-PbjEYAS06r%3D4-ag%40mail.gmail.com
Backpatch-through: 14
src/backend/access/spgist/spgquadtreeproc.c | 2 +-
src/test/regress/expected/spgist.out | 9 +++++++++
src/test/regress/sql/spgist.sql | 5 +++++
3 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/src/backend/access/spgist/spgquadtreeproc.c b/src/backend/access/spgist/spgquadtreeproc.c
index e038c998472..485b3467489 100644
--- a/src/backend/access/spgist/spgquadtreeproc.c
+++ b/src/backend/access/spgist/spgquadtreeproc.c
@@ -393,7 +393,7 @@ spg_quad_inner_consistent(PG_FUNCTION_ARGS)
r |= 1 << getQuadrant(centroid, &p);
p = boxQuery->high;
r |= 1 << getQuadrant(centroid, &p);
- p.x = boxQuery->low.x;
+ p.y = boxQuery->low.y;
r |= 1 << getQuadrant(centroid, &p);
which &= r;
diff --git a/src/test/regress/expected/spgist.out b/src/test/regress/expected/spgist.out
index 064b0a69cae..9eebc506fd3 100644
--- a/src/test/regress/expected/spgist.out
+++ b/src/test/regress/expected/spgist.out
@@ -109,4 +109,13 @@ select count(*) from spgist_quad_fp_tbl
1
(1 row)
+-- test top of box within EPSILON of the centroid
+insert into spgist_quad_fp_tbl values (point(200, 5 - 5e-6));
+select count(*) from spgist_quad_fp_tbl
+ where p <@ box(point(0, 4.99999), point(400, 4.9999995));
+ count
+-------
+ 1
+(1 row)
+
reset enable_seqscan;
diff --git a/src/test/regress/sql/spgist.sql b/src/test/regress/sql/spgist.sql
index c7bfae347db..710f7699366 100644
--- a/src/test/regress/sql/spgist.sql
+++ b/src/test/regress/sql/spgist.sql
@@ -101,4 +101,9 @@ insert into spgist_quad_fp_tbl values (point(114 - 5e-7, 5 + 1e-6));
set enable_seqscan = off;
select count(*) from spgist_quad_fp_tbl
where p <@ box(point(113.999999, 5.0000004), point(113.9999999, 5.0000016));
+
+-- test top of box within EPSILON of the centroid
+insert into spgist_quad_fp_tbl values (point(200, 5 - 5e-6));
+select count(*) from spgist_quad_fp_tbl
+ where p <@ box(point(0, 4.99999), point(400, 4.9999995));
reset enable_seqscan;
[parent: bfa409cc1181]