From 3c29b196b0ce46662cb9bb7a1f91079fbacbcabb Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 8 Feb 2013 18:03:17 -0500 Subject: Fix gist_box_same and gist_point_consistent to handle fuzziness correctly. While there's considerable doubt that we want fuzzy behavior in the geometric operators at all (let alone as currently implemented), nobody is stepping forward to redesign that stuff. In the meantime it behooves us to make sure that index searches agree with the behavior of the underlying operators. This patch fixes two problems in this area. First, gist_box_same was using fuzzy equality, but it really needs to use exact equality to prevent not-quite-identical upper index keys from being treated as identical, which for example would prevent an existing upper key from being extended by an amount less than epsilon. This would result in inconsistent indexes. (The next release notes will need to recommend that users reindex GiST indexes on boxes, polygons, circles, and points, since all four opclasses use gist_box_same.) Second, gist_point_consistent used exact comparisons for upper-page comparisons in ~= searches, when it needs to use fuzzy comparisons to ensure it finds all matches; and it used fuzzy comparisons for point <@ box searches, when it needs to use exact comparisons because that's what the <@ operator (rather inconsistently) does. The added regression test cases illustrate all three misbehaviors. Back-patch to all active branches. (8.4 did not have GiST point_ops, but it still seems prudent to apply the gist_box_same patch to it.) Alexander Korotkov, reviewed by Noah Misch --- src/test/regress/expected/point.out | 50 +++++++++++++++++++++++++++++++++++++ src/test/regress/sql/point.sql | 21 ++++++++++++++++ 2 files changed, 71 insertions(+) (limited to 'src/test') diff --git a/src/test/regress/expected/point.out b/src/test/regress/expected/point.out index 7929229b16..bfc0962749 100644 --- a/src/test/regress/expected/point.out +++ b/src/test/regress/expected/point.out @@ -245,3 +245,53 @@ SELECT '' AS three, p1.f1 AS point1, p2.f1 AS point2, (p1.f1 <-> p2.f1) AS dista | (5.1,34.5) | (10,10) | 24.9851956166046 (3 rows) +-- Test that GiST indexes provide same behavior as sequential scan +CREATE TEMP TABLE point_gist_tbl(f1 point); +INSERT INTO point_gist_tbl SELECT '(0,0)' FROM generate_series(0,1000); +CREATE INDEX point_gist_tbl_index ON point_gist_tbl USING gist (f1); +INSERT INTO point_gist_tbl VALUES ('(0.0000009,0.0000009)'); +SET enable_seqscan TO true; +SET enable_indexscan TO false; +SET enable_bitmapscan TO false; +SELECT COUNT(*) FROM point_gist_tbl WHERE f1 ~= '(0.0000009,0.0000009)'::point; + count +------- + 1002 +(1 row) + +SELECT COUNT(*) FROM point_gist_tbl WHERE f1 <@ '(0.0000009,0.0000009),(0.0000009,0.0000009)'::box; + count +------- + 1 +(1 row) + +SELECT COUNT(*) FROM point_gist_tbl WHERE f1 ~= '(0.0000018,0.0000018)'::point; + count +------- + 1 +(1 row) + +SET enable_seqscan TO false; +SET enable_indexscan TO true; +SET enable_bitmapscan TO true; +SELECT COUNT(*) FROM point_gist_tbl WHERE f1 ~= '(0.0000009,0.0000009)'::point; + count +------- + 1002 +(1 row) + +SELECT COUNT(*) FROM point_gist_tbl WHERE f1 <@ '(0.0000009,0.0000009),(0.0000009,0.0000009)'::box; + count +------- + 1 +(1 row) + +SELECT COUNT(*) FROM point_gist_tbl WHERE f1 ~= '(0.0000018,0.0000018)'::point; + count +------- + 1 +(1 row) + +RESET enable_seqscan; +RESET enable_indexscan; +RESET enable_bitmapscan; diff --git a/src/test/regress/sql/point.sql b/src/test/regress/sql/point.sql index 1b62b10d40..63a803a809 100644 --- a/src/test/regress/sql/point.sql +++ b/src/test/regress/sql/point.sql @@ -80,3 +80,24 @@ SELECT '' AS three, p1.f1 AS point1, p2.f1 AS point2, (p1.f1 <-> p2.f1) AS dista FROM POINT_TBL p1, POINT_TBL p2 WHERE (p1.f1 <-> p2.f1) > 3 and p1.f1 << p2.f1 and p1.f1 >^ p2.f1 ORDER BY distance; + +-- Test that GiST indexes provide same behavior as sequential scan +CREATE TEMP TABLE point_gist_tbl(f1 point); +INSERT INTO point_gist_tbl SELECT '(0,0)' FROM generate_series(0,1000); +CREATE INDEX point_gist_tbl_index ON point_gist_tbl USING gist (f1); +INSERT INTO point_gist_tbl VALUES ('(0.0000009,0.0000009)'); +SET enable_seqscan TO true; +SET enable_indexscan TO false; +SET enable_bitmapscan TO false; +SELECT COUNT(*) FROM point_gist_tbl WHERE f1 ~= '(0.0000009,0.0000009)'::point; +SELECT COUNT(*) FROM point_gist_tbl WHERE f1 <@ '(0.0000009,0.0000009),(0.0000009,0.0000009)'::box; +SELECT COUNT(*) FROM point_gist_tbl WHERE f1 ~= '(0.0000018,0.0000018)'::point; +SET enable_seqscan TO false; +SET enable_indexscan TO true; +SET enable_bitmapscan TO true; +SELECT COUNT(*) FROM point_gist_tbl WHERE f1 ~= '(0.0000009,0.0000009)'::point; +SELECT COUNT(*) FROM point_gist_tbl WHERE f1 <@ '(0.0000009,0.0000009),(0.0000009,0.0000009)'::box; +SELECT COUNT(*) FROM point_gist_tbl WHERE f1 ~= '(0.0000018,0.0000018)'::point; +RESET enable_seqscan; +RESET enable_indexscan; +RESET enable_bitmapscan; -- cgit v1.2.3