From e505d83aa56fc9293b6819e5adc6971df4734ce8 Mon Sep 17 00:00:00 2001 From: Ayush Tiwari Date: Mon, 3 Aug 2026 01:46:18 +0530 Subject: [PATCH] Fix reachable "impossible case" in SP-GiST quad tree The fuzzy point comparisons used by getQuadrant() are not mutually exhaustive near some power-of-two boundaries. Fall back to exact comparisons when none of the existing quadrant tests match. Reported-by: Michael Malis Discussion: https://postgr.es/m/19597-39c532e61d78dff6@postgresql.org Backpatch-through: 14 --- src/backend/access/spgist/spgquadtreeproc.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/backend/access/spgist/spgquadtreeproc.c b/src/backend/access/spgist/spgquadtreeproc.c index 946dabc4527..2566ab3b8db 100644 --- a/src/backend/access/spgist/spgquadtreeproc.c +++ b/src/backend/access/spgist/spgquadtreeproc.c @@ -76,6 +76,16 @@ getQuadrant(Point *centroid, Point *tst) SPTEST(point_left, tst, centroid)) return 4; + /* Fuzzy comparisons can leave gaps, so fall back to exact ones. */ + if (tst->x >= centroid->x && tst->y >= centroid->y) + return 1; + if (tst->x >= centroid->x && tst->y < centroid->y) + return 2; + if (tst->x < centroid->x && tst->y <= centroid->y) + return 3; + if (tst->x < centroid->x && tst->y > centroid->y) + return 4; + elog(ERROR, "getQuadrant: impossible case"); return 0; } -- 2.34.1