SP-GiST quad_point_ops KNN scan can omit a closer point
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
PostgreSQL server version: PostgreSQL 19.0, REL_19_0, commit 5c839a2a08b9b87a349be97371fac6c5a2873b10
psql version: PostgreSQL 19.0
Platform: Ubuntu 24.04 LTS, Linux x86_64, kernel 7.0.0-38-generic, glibc 2.39; GCC 13.3.0
Build options: --without-readline --without-zlib --without-icu --without-llvm
Cluster setup: Run as the postgres operating-system user. Set PGDATA to a new empty data directory and SOCKET to a local socket directory, then run:
export PGDATA=/path/to/pgdata
export SOCKET=/path/to/socket
mkdir -p "$SOCKET"
initdb -D "$PGDATA" --no-locale -E UTF8 --auth-local=trust --auth-host=reject
Start the server with:
pg_ctl -D "$PGDATA" -l "$PGDATA/server.log" -o "-c listen_addresses= -c unix_socket_directories=$SOCKET -c port=55488 -c max_connections=10 -c shared_buffers=32MB -c wal_level=logical -c max_replication_slots=10 -c max_wal_senders=10" -w start
Connect to the postgres database with PGHOST=$SOCKET PGPORT=55488 psql -X -U postgres -d postgres. The replication-related settings do not affect this case.
Summary
A KNN query using the default SP-GiST point operator class can omit a closer point and return a farther point in the top-k result. The result differs from sorting the same rows by the same distance expression.
Reproduction
Run this self-contained script in psql. It creates only temporary tables and rolls back its transaction.
BEGIN;
CREATE TEMP TABLE points (id integer, p point) ON COMMIT DROP;
INSERT INTO points
SELECT (i - 1) * 2 + CASE WHEN sign > 0 THEN 2 ELSE 1 END,
point(sign * (0.000002::float8 + i * 0.000000001::float8), 0::float8)
FROM generate_series(1, 400) AS g(i)
CROSS JOIN (VALUES (-1.0::float8), (1.0::float8)) AS s(sign)
ORDER BY i, sign;
CREATE INDEX points_spg ON points USING spgist (p);
INSERT INTO points VALUES
(9001, point(-0.00000095367431640625::float8, 0::float8)),
(9002, point(-0.000001430511474609375::float8, 0::float8)),
(9003, point(-0.0000012::float8, 0::float8)),
(9004, point(-0.00000125::float8, 0::float8));
ANALYZE points;
SET enable_seqscan = off;
SET enable_bitmapscan = off;
SET enable_indexscan = on;
EXPLAIN (COSTS OFF)
SELECT id, p <-> point(-0.0000012::float8, 0::float8) AS distance
FROM points
ORDER BY p <-> point(-0.0000012::float8, 0::float8)
LIMIT 10;
CREATE TEMP TABLE index_result AS
SELECT id, p <-> point(-0.0000012::float8, 0::float8) AS distance
FROM points
ORDER BY p <-> point(-0.0000012::float8, 0::float8)
LIMIT 10;
SET enable_indexscan = off;
SET enable_indexonlyscan = off;
SET enable_seqscan = on;
EXPLAIN (COSTS OFF)
SELECT id, p <-> point(-0.0000012::float8, 0::float8) AS distance
FROM points
ORDER BY p <-> point(-0.0000012::float8, 0::float8)
LIMIT 10;
CREATE TEMP TABLE seq_result AS
SELECT id, p <-> point(-0.0000012::float8, 0::float8) AS distance
FROM points
ORDER BY p <-> point(-0.0000012::float8, 0::float8)
LIMIT 10;
SELECT 'index' AS path, id, distance FROM index_result ORDER BY distance;
SELECT 'seq' AS path, id, distance FROM seq_result ORDER BY distance;
SELECT count(*) AS symmetric_difference
FROM (
(SELECT * FROM index_result EXCEPT ALL SELECT * FROM seq_result)
UNION ALL
(SELECT * FROM seq_result EXCEPT ALL SELECT * FROM index_result)
) AS differences;
ROLLBACK;
Observed result
The planner used an SP-GiST Index Scan with Order By: (p <-> '(-1.2e-06,0)'::point). The index top-10 IDs were {9003,9004,9002,1,3,5,7,9,11,13}; the sequential top-10 IDs were {9003,9004,9002,9001,1,3,5,7,9,11}. The index omitted id 9001, whose distance is 2.4632568359374995e-7, and returned id 13 at distance 8.070000000000001e-7 instead. symmetric_difference was 2. There is no distance tie at the LIMIT boundary.
Expected result
The SP-GiST KNN result should contain the same nearest rows as sorting the same finite point values by the supported <-> distance operator. PostgreSQL 19 documents quad_point_ops as supporting <-> KNN searches: https://www.postgresql.org/docs/19/spgist.html
The behavior reproduced in two independent psql sessions on PostgreSQL 18.7 (REL_18_7, commit f5c41f6681c910b3f69951bf6fb97c6d74605d25) and two independent psql sessions on PostgreSQL 19.0 (REL_19_0, commit 5c839a2a08b9b87a349be97371fac6c5a2873b10).
On PostgreSQL 18.7 (REL_18_7, commit f5c41f6681c910b3f69951bf6fb97c6d74605d25), a direct read of the root inner tuple in a logged scratch index showed a prefix of (-9.3127753303964754e-9, 0). The x coordinate of id 9001 differs from that split coordinate by 9.4436154107585355e-7, within EPSILON (1.0e-6). For the query point (-1.2e-6, 0), the quadrant-1 bounding box has a distance lower bound of 1.1906872246696034e-6, while id 9001's actual distance is 2.4632568359374995e-7. This root-prefix measurement was made on 18.7; the same SQL result was separately reproduced on 19.0, but its root page was not decoded. EPSILON and the fuzzy comparison definitions are in src/include/utils/geo_decls.h: https://github.com/postgres/postgres/blob/5c839a2a08b9b87a349be97371fac6c5a2873b10/src/include/utils/geo_decls.h#L39-L69
Relevant source location: getQuadrant() and getQuadrantArea() in src/backend/access/spgist/spgquadtreeproc.c. The routing and bounding-box observations above are consistent with the wrong top-k result. The exact executor queue/pruning event was not instrumented. Source at the tested 19.0 commit: https://github.com/postgres/postgres/blob/5c839a2a08b9b87a349be97371fac6c5a2873b10/src/backend/access/spgist/spgquadtreeproc.c#L83-L150
The PostgreSQL 18.7 root-tuple measurement shows that the exact-comparison fallback for the no-quadrant-matches case was not entered: the fuzzy comparisons already selected a quadrant. The same SQL result is present on 19.0, but its root page was not decoded. The fallback change is described here: /messages/by-id/E1xGx7l-00000001aV9-3OFJ@gemulon.postgresql.org
BUG #19641 reports incorrect equality lookup results for text in an SP-GiST index with a non-deterministic collation. This report concerns KNN ordering over point values with the default quad_point_ops class: /messages/by-id/19641-fbd0937eac4bf27e@postgresql.org
On Thu, Nov 26, 2026 at 12:57 AM <wangyang25@otcaix.iscas.ac.cn> wrote:
A KNN query using the default SP-GiST point operator class can omit a closer point and return a farther point in the top-k result. The result differs from sorting the same rows by the same distance expression.
This is the only readable part of the report -- the rest is
gibberish. From the above I can tell that I found this same bug among
others last month.
I have time to come back to this, so I'm now separating out rounding
bugs from other bugs, since some fixes for the latter need to take
rounding also into account. I'll share the pure-rounding fixes in a
separate email, then come back to this.
--
John Naylor
Amazon Web Services