BUG #19597: getQuadrant: impossible case is reachable
The following bug has been logged on the website:
Bug reference: 19597
Logged by: Michael Malis
Email address: malis@pgrust.com
PostgreSQL version: 18.3
Operating system: Debian 18.3-1.pgdg13+1
Description:
getQuadrant has four arms covering the quadrants and an elog(ERROR,
"getQuadrant: impossible case") fallthrough. The arms are exhaustive only if
the above/below/horizontal predicates are exhaustive. They are not: the
three comparisons are written in three algebraically different forms, and
near a power-of-two boundary — where the gap between adjacent doubles
doubles — adding EPSILON rounds up on one side and vanishes on the other. A
point can then be neither above, below, nor level with the centroid, and the
"impossible" branch executes.
Reproducer (runnable against stock PostgreSQL 18.3)
---------------------------------------------------
CREATE TABLE gq(p point);
-- 4000 identical points: their mean (the quad centroid) is exactly this
value,
-- which is nextafter(2^34, -inf)
INSERT INTO gq SELECT point('17179869183.999998','17179869183.999998')
FROM generate_series(1,4000);
CREATE INDEX gqx ON gq USING spgist(p);
-- probe at 2^34, one representable step away
INSERT INTO gq SELECT point('17179869184.0','17179869184.0')
FROM generate_series(1,400);
ERROR: getQuadrant: impossible case
The identical-point fill is what forces the computed centroid onto the
chosen value — spg_quad_picksplit uses the mean of the split set, so the
centroid cannot simply be inserted.
Expected vs. actual
-------------------
- Expected: the insert succeeds, or fails with a meaningful user-facing
error. A branch labelled "impossible case" should not be reachable from
well-formed finite input.
- Actual: ERROR: getQuadrant: impossible case. The backend survives (this is
a catchable error, not a crash), but the index operation fails and the
message is an internal invariant leaking to the user.
Mechanism, with file:line into the 18.3 source
----------------------------------------------
src/backend/access/spgist/spgquadtreeproc.c, getQuadrant (function at 55):
57: if ((SPTEST(point_above, tst, centroid) || SPTEST(point_horiz, tst,
centroid)) && ...
63: if (SPTEST(point_below, tst, centroid) && ...
68: if ((SPTEST(point_below, tst, centroid) || SPTEST(point_horiz, tst,
centroid)) && ...
73: if (SPTEST(point_above, tst, centroid) && SPTEST(point_left, tst,
centroid))
77: elog(ERROR, "getQuadrant: impossible case");
point_above/point_below/point_horiz reduce to FPgt/FPlt/FPeq on the y
coordinate. src/include/utils/geo_decls.h:
41: #define EPSILON 1.0E-06
47: FPeq(A,B) { return A == B || fabs(A - B) <= EPSILON; }
59: FPlt(A,B) { return A + EPSILON < B; }
71: FPgt(A,B) { return A > B + EPSILON; }
FPeq tests the rounded difference; FPlt and FPgt test rounded sums. Over
exact reals these partition the line; in floating point they do not. Just
below 2^34 the spacing between doubles is 2^-19 ≈ 1.907e-6 (> EPSILON, so
adding EPSILON rounds up a step); just above it is 2^-18 ≈ 3.815e-6 (>
2·EPSILON, so adding EPSILON rounds back down). For a = 2^34, b =
nextafter(2^34, -inf) all three are false — verified on 18.3's own
arithmetic:
expression value
----------- --------------------------------------------
a - b 1.9073486328125e-06 (> EPSILON ⇒ FPeq false)
a > b + eps false (b + eps rounds up to exactly a)
a + eps < b false (a + eps rounds back to a)
With the y trichotomy failing, no arm matches regardless of the x result,
and line 77 executes.
Hi,
On Sun, 2 Aug 2026 at 23:42, PG Bug reporting form <noreply@postgresql.org>
wrote:
The following bug has been logged on the website:
Bug reference: 19597
Logged by: Michael Malis
Email address: malis@pgrust.com
PostgreSQL version: 18.3
Operating system: Debian 18.3-1.pgdg13+1
Description:getQuadrant has four arms covering the quadrants and an elog(ERROR,
"getQuadrant: impossible case") fallthrough. The arms are exhaustive only
if
the above/below/horizontal predicates are exhaustive. They are not: the
three comparisons are written in three algebraically different forms, and
near a power-of-two boundary — where the gap between adjacent doubles
doubles — adding EPSILON rounds up on one side and vanishes on the other. A
point can then be neither above, below, nor level with the centroid, and
the
"impossible" branch executes.Reproducer (runnable against stock PostgreSQL 18.3)
---------------------------------------------------
CREATE TABLE gq(p point);
-- 4000 identical points: their mean (the quad centroid) is exactly
this
value,
-- which is nextafter(2^34, -inf)
INSERT INTO gq SELECT point('17179869183.999998','17179869183.999998')
FROM generate_series(1,4000);
CREATE INDEX gqx ON gq USING spgist(p);
-- probe at 2^34, one representable step away
INSERT INTO gq SELECT point('17179869184.0','17179869184.0')
FROM generate_series(1,400);
ERROR: getQuadrant: impossible caseThe identical-point fill is what forces the computed centroid onto the
chosen value — spg_quad_picksplit uses the mean of the split set, so the
centroid cannot simply be inserted.Expected vs. actual
-------------------
- Expected: the insert succeeds, or fails with a meaningful user-facing
error. A branch labelled "impossible case" should not be reachable from
well-formed finite input.
- Actual: ERROR: getQuadrant: impossible case. The backend survives (this
is
a catchable error, not a crash), but the index operation fails and the
message is an internal invariant leaking to the user.Mechanism, with file:line into the 18.3 source
----------------------------------------------
src/backend/access/spgist/spgquadtreeproc.c, getQuadrant (function at 55):
57: if ((SPTEST(point_above, tst, centroid) || SPTEST(point_horiz,
tst,
centroid)) && ...
63: if (SPTEST(point_below, tst, centroid) && ...
68: if ((SPTEST(point_below, tst, centroid) || SPTEST(point_horiz,
tst,
centroid)) && ...
73: if (SPTEST(point_above, tst, centroid) && SPTEST(point_left, tst,
centroid))
77: elog(ERROR, "getQuadrant: impossible case");point_above/point_below/point_horiz reduce to FPgt/FPlt/FPeq on the y
coordinate. src/include/utils/geo_decls.h:
41: #define EPSILON 1.0E-06
47: FPeq(A,B) { return A == B || fabs(A - B) <= EPSILON; }
59: FPlt(A,B) { return A + EPSILON < B; }
71: FPgt(A,B) { return A > B + EPSILON; }FPeq tests the rounded difference; FPlt and FPgt test rounded sums. Over
exact reals these partition the line; in floating point they do not. Just
below 2^34 the spacing between doubles is 2^-19 ≈ 1.907e-6 (> EPSILON, so
adding EPSILON rounds up a step); just above it is 2^-18 ≈ 3.815e-6 (>
2·EPSILON, so adding EPSILON rounds back down). For a = 2^34, b =
nextafter(2^34, -inf) all three are false — verified on 18.3's own
arithmetic:expression value
----------- --------------------------------------------
a - b 1.9073486328125e-06 (> EPSILON ⇒ FPeq false)
a > b + eps false (b + eps rounds up to exactly a)
a + eps < b false (a + eps rounds back to a)With the y trichotomy failing, no arm matches regardless of the x result,
and line 77 executes.
Thanks for the detailed report and analysis. I reproduced the issue on
current master.
The three fuzzy comparisons are indeed not exhaustive near this
floating-point
boundary, allowing getQuadrant() to reach its "impossible case". The
attached
patch leaves the existing quadrant tests unchanged and falls back to exact
comparisons only when none of them match.
Thoughts?
Regards,
Ayush
Hi, Ayush!
Thanks for the patch.
The approach looks right to me: keep the fuzzy arms, fall back to exact
comparisons for the finite gap, and leave the
elog for NaN.
Here is a suggested v2 on top of your patch.
1. Write the exact fallback as y-then-x checks, which I find a bit easier
to match to the quadrant diagram and the axis tie-breaking rule.
2. Add a short note in the getQuadrant() header about why the fallback
exists.
3. Add a regress case.
пн, 3 авг. 2026 г. в 01:31, Ayush Tiwari <ayushtiwari.slg01@gmail.com>:
Hi,
On Sun, 2 Aug 2026 at 23:42, PG Bug reporting form <noreply@postgresql.org>
wrote:The following bug has been logged on the website:
Bug reference: 19597
Logged by: Michael Malis
Email address: malis@pgrust.com
PostgreSQL version: 18.3
Operating system: Debian 18.3-1.pgdg13+1
Description:getQuadrant has four arms covering the quadrants and an elog(ERROR,
"getQuadrant: impossible case") fallthrough. The arms are exhaustive only
if
the above/below/horizontal predicates are exhaustive. They are not: the
three comparisons are written in three algebraically different forms, and
near a power-of-two boundary — where the gap between adjacent doubles
doubles — adding EPSILON rounds up on one side and vanishes on the other.
A
point can then be neither above, below, nor level with the centroid, and
the
"impossible" branch executes.Reproducer (runnable against stock PostgreSQL 18.3)
---------------------------------------------------
CREATE TABLE gq(p point);
-- 4000 identical points: their mean (the quad centroid) is exactly
this
value,
-- which is nextafter(2^34, -inf)
INSERT INTO gq SELECT point('17179869183.999998','17179869183.999998')
FROM generate_series(1,4000);
CREATE INDEX gqx ON gq USING spgist(p);
-- probe at 2^34, one representable step away
INSERT INTO gq SELECT point('17179869184.0','17179869184.0')
FROM generate_series(1,400);
ERROR: getQuadrant: impossible caseThe identical-point fill is what forces the computed centroid onto the
chosen value — spg_quad_picksplit uses the mean of the split set, so the
centroid cannot simply be inserted.Expected vs. actual
-------------------
- Expected: the insert succeeds, or fails with a meaningful user-facing
error. A branch labelled "impossible case" should not be reachable from
well-formed finite input.
- Actual: ERROR: getQuadrant: impossible case. The backend survives (this
is
a catchable error, not a crash), but the index operation fails and the
message is an internal invariant leaking to the user.Mechanism, with file:line into the 18.3 source
----------------------------------------------
src/backend/access/spgist/spgquadtreeproc.c, getQuadrant (function at 55):
57: if ((SPTEST(point_above, tst, centroid) || SPTEST(point_horiz,
tst,
centroid)) && ...
63: if (SPTEST(point_below, tst, centroid) && ...
68: if ((SPTEST(point_below, tst, centroid) || SPTEST(point_horiz,
tst,
centroid)) && ...
73: if (SPTEST(point_above, tst, centroid) && SPTEST(point_left, tst,
centroid))
77: elog(ERROR, "getQuadrant: impossible case");point_above/point_below/point_horiz reduce to FPgt/FPlt/FPeq on the y
coordinate. src/include/utils/geo_decls.h:
41: #define EPSILON 1.0E-06
47: FPeq(A,B) { return A == B || fabs(A - B) <= EPSILON; }
59: FPlt(A,B) { return A + EPSILON < B; }
71: FPgt(A,B) { return A > B + EPSILON; }FPeq tests the rounded difference; FPlt and FPgt test rounded sums. Over
exact reals these partition the line; in floating point they do not. Just
below 2^34 the spacing between doubles is 2^-19 ≈ 1.907e-6 (> EPSILON, so
adding EPSILON rounds up a step); just above it is 2^-18 ≈ 3.815e-6 (>
2·EPSILON, so adding EPSILON rounds back down). For a = 2^34, b =
nextafter(2^34, -inf) all three are false — verified on 18.3's own
arithmetic:expression value
----------- --------------------------------------------
a - b 1.9073486328125e-06 (> EPSILON ⇒ FPeq false)
a > b + eps false (b + eps rounds up to exactly a)
a + eps < b false (a + eps rounds back to a)With the y trichotomy failing, no arm matches regardless of the x result,
and line 77 executes.Thanks for the detailed report and analysis. I reproduced the issue on
current master.The three fuzzy comparisons are indeed not exhaustive near this
floating-point
boundary, allowing getQuadrant() to reach its "impossible case". The
attached
patch leaves the existing quadrant tests unchanged and falls back to exact
comparisons only when none of them match.Thoughts?
Regards,
Ayush
--
Regards,
Rachitskiy Andrey
The following review has been posted through the commitfest application:
make installcheck-world: tested, passed
Implements feature: tested, passed
Spec compliant: not tested
Documentation: not tested
Hello,
I have reviewed this patch and it looks good to me.
I have reproduced it on master branch and applying patch has fixed the bug.
I have neither tested SQL compliance nor documentation: I think these steps are not needed for this bug fix.
Regards
Pierre Forstmann.
On Mon, Aug 3, 2026 at 4:22 AM Andrey Rachitskiy <pl0h0yp1@gmail.com> wrote:
The approach looks right to me: keep the fuzzy arms, fall back to exact comparisons for the finite gap, and leave the
elog for NaN.Here is a suggested v2 on top of your patch.
Note: "on top of" implies applying both -- v1 and then v2. v2 is
independent, and CI applies only the latest.
"not mutually exhaustive near some power-of-two boundaries"
The reporter stumbled across something interesting, but you actually
don't need to be near a boundary:
drop table if exists g2;
create table g2(p point);
insert into g2 select point(10000000000 + i* 0.001, 5)
from generate_series(1,3000) i;
create index on g2 using spgist(p);
ERROR: getQuadrant: impossible case
Note also that this doesn't need a separate insert. I found on master
and REL_18_STABLE that the reporter's case didn't need one either --
the error happened for me already when building the index.
What I think is happening (from rubber-ducking with Claude):
picksplit() calculates the centroid as the mean of a page of points.
For this binade [2^33, 2^34), where both 17179869183.999998 and
10000000000 are found, the error happens if any point on that page is
exactly 1 ulp (unit in the last place) from the centroid, or 1.9e-6.
This is because this ulp is between EPSILON and 2*EPSILON. A point
exactly 1 ulp from the centroid is then more than EPSILON away, so
FPeq is false. But centroid +/- EPSILON rounds onto that point, so
FPgt and FPlt are false too. Other binades have similar gaps, but it's
probably harder to use them to reach this elog from SQL.
1. Write the exact fallback as y-then-x checks, which I find a bit easier to match to the quadrant diagram and the axis tie-breaking rule.
+ if (tst->y > centroid->y)
+ return (tst->x >= centroid->x) ? 1 : 4;
+ if (tst->y < centroid->y)
+ return (tst->x >= centroid->x) ? 2 : 3;
+ if (tst->y == centroid->y)
+ return (tst->x >= centroid->x) ? 1 : 3;
I preferred the style of Ayush's patch. -- this looks really different
from the coding for the fuzzy case. Let's make the exact stanza
similar to the fuzzy stanza, including testing y before x.
--
John Naylor
Amazon Web Services
Hi,
On Tue, 29 Sept 2026 at 19:09, John Naylor <johncnaylorls@gmail.com> wrote:
On Mon, Aug 3, 2026 at 4:22 AM Andrey Rachitskiy <pl0h0yp1@gmail.com> wrote:
The approach looks right to me: keep the fuzzy arms, fall back to exact comparisons for the finite gap, and leave the
elog for NaN.Here is a suggested v2 on top of your patch.
Note: "on top of" implies applying both -- v1 and then v2. v2 is
independent, and CI applies only the latest."not mutually exhaustive near some power-of-two boundaries"
The reporter stumbled across something interesting, but you actually
don't need to be near a boundary:drop table if exists g2;
create table g2(p point);
insert into g2 select point(10000000000 + i* 0.001, 5)
from generate_series(1,3000) i;
create index on g2 using spgist(p);
ERROR: getQuadrant: impossible caseNote also that this doesn't need a separate insert. I found on master
and REL_18_STABLE that the reporter's case didn't need one either --
the error happened for me already when building the index.What I think is happening (from rubber-ducking with Claude):
picksplit() calculates the centroid as the mean of a page of points.
For this binade [2^33, 2^34), where both 17179869183.999998 and
10000000000 are found, the error happens if any point on that page is
exactly 1 ulp (unit in the last place) from the centroid, or 1.9e-6.
This is because this ulp is between EPSILON and 2*EPSILON. A point
exactly 1 ulp from the centroid is then more than EPSILON away, so
FPeq is false. But centroid +/- EPSILON rounds onto that point, so
FPgt and FPlt are false too. Other binades have similar gaps, but it's
probably harder to use them to reach this elog from SQL.1. Write the exact fallback as y-then-x checks, which I find a bit easier to match to the quadrant diagram and the axis tie-breaking rule.
+ if (tst->y > centroid->y) + return (tst->x >= centroid->x) ? 1 : 4; + if (tst->y < centroid->y) + return (tst->x >= centroid->x) ? 2 : 3; + if (tst->y == centroid->y) + return (tst->x >= centroid->x) ? 1 : 3;I preferred the style of Ayush's patch. -- this looks really different
from the coding for the fuzzy case. Let's make the exact stanza
similar to the fuzzy stanza, including testing y before x.
Thanks a lot for the review, John!
Attaching v3 with updated test case and earlier fuzzy comparisons.
Regards,
Ayush
On Tue, Sep 29, 2026 at 9:59 PM Ayush Tiwari
<ayushtiwari.slg01@gmail.com> wrote:
Thanks a lot for the review, John!
Attaching v3 with updated test case and earlier fuzzy comparisons.
In the attached v4, 0001 is like v3 but with a smaller test case with
non-huge numbers (back to using a separate insert):
create table spgist_quad_fp_tbl(p point);
insert into spgist_quad_fp_tbl select point(i, 5) from
generate_series(1, 300) i;
create index spgist_quad_fp_idx on spgist_quad_fp_tbl using spgist(p);
insert into spgist_quad_fp_tbl values (point(3, 5 + 1e-6::float8));
Unfortunately, we're not out of the woods yet. With v3, it's possible
to create indexes that were not possible before, but we can still get
the wrong answer when searching. Some LLM-assisted review and fuzzing
found this test case:
-- same table as above
insert into spgist_quad_fp_tbl values (point(114 - 5e-7, 5 + 1e-6::float8));
set enable_indexscan = off;
select count(*) from spgist_quad_fp_tbl
where p <@ box(point(113.999999, 5.0000004), point(113.9999999, 5.0000016));
count
-------
1
(1 row)
reset enable_indexscan;
set enable_seqscan = off;
select count(*) from spgist_quad_fp_tbl
where p <@ box(point(113.999999, 5.0000004), point(113.9999999, 5.0000016));
count
-------
0
(1 row)
reset all;
v4 0002 fixes this case (note for those following along: needs a
reindex between 0001 and 0002 for it to give the right answer). I've
left 0002 how Claude wrote it for now, but it can probably just be
squashed into 0001. I'm not yet sure if this series fixes all the bugs
in this path, so I'll hold off on polishing and committing just yet.
I may have found 3 other possible bugs involving SP-Gist in the course
of investigating this, so I will share those in separate threads when
I return to this topic.
--
John Naylor
Amazon Web Services
Attachments:
t253283_7v4-0001-Fix-reachable-impossible-case-in-SP-GiST-quad-tre.patchtext/x-patch; charset=US-ASCII; name=v4-0001-Fix-reachable-impossible-case-in-SP-GiST-quad-tre.patchDownload+25-1
v4-0002-Decide-each-axis-separately-in-getQuadrant-fallba.patchtext/x-patch; charset=US-ASCII; name=v4-0002-Decide-each-axis-separately-in-getQuadrant-fallba.patchDownload+55-14
On Thu, Oct 8, 2026 at 2:10 PM John Naylor <johncnaylorls@gmail.com> wrote:
v4 0002 fixes this case (note for those following along: needs a
reindex between 0001 and 0002 for it to give the right answer). I've
left 0002 how Claude wrote it for now, but it can probably just be
squashed into 0001. I'm not yet sure if this series fixes all the bugs
in this path, so I'll hold off on polishing and committing just yet.
Further fuzzing didn't find any more bugs (in this path), so pushed
after simplifying some comments.
--
John Naylor
Amazon Web Services
Hi,
On Wed, 14 Oct 2026 at 17:33, John Naylor <johncnaylorls@gmail.com> wrote:
On Thu, Oct 8, 2026 at 2:10 PM John Naylor <johncnaylorls@gmail.com> wrote:
v4 0002 fixes this case (note for those following along: needs a
reindex between 0001 and 0002 for it to give the right answer). I've
left 0002 how Claude wrote it for now, but it can probably just be
squashed into 0001. I'm not yet sure if this series fixes all the bugs
in this path, so I'll hold off on polishing and committing just yet.Further fuzzing didn't find any more bugs (in this path), so pushed
after simplifying some comments.
Thanks a lot for taking care of this, John!
Regards,
Ayush