Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3c29b19

Browse files
committed
Fix gist_box_same and gist_point_consistent to handle fuzziness correctly.
While there's considerable doubt that we want fuzzy behavior in thegeometric operators at all (let alone as currently implemented), nobody isstepping forward to redesign that stuff. In the meantime it behooves usto make sure that index searches agree with the behavior of the underlyingoperators. This patch fixes two problems in this area.First, gist_box_same was using fuzzy equality, but it really needs to useexact equality to prevent not-quite-identical upper index keys from beingtreated as identical, which for example would prevent an existing upperkey from being extended by an amount less than epsilon. This would resultin inconsistent indexes. (The next release notes will need to recommendthat 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-pagecomparisons in ~= searches, when it needs to use fuzzy comparisons toensure it finds all matches; and it used fuzzy comparisons for point <@ boxsearches, 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
1 parent381d4b7 commit3c29b19

File tree

3 files changed

+112
-15
lines changed

3 files changed

+112
-15
lines changed

‎src/backend/access/gist/gistproc.c

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,12 @@ gist_box_picksplit(PG_FUNCTION_ARGS)
838838
/*
839839
* Equality method
840840
*
841-
* This is used for both boxes and points.
841+
* This is used for boxes, points, circles, and polygons, all of which store
842+
* boxes as GiST index entries.
843+
*
844+
* Returns true only when boxes are exactly the same. We can't use fuzzy
845+
* comparisons here without breaking index consistency; therefore, this isn't
846+
* equivalent to box_same().
842847
*/
843848
Datum
844849
gist_box_same(PG_FUNCTION_ARGS)
@@ -848,11 +853,10 @@ gist_box_same(PG_FUNCTION_ARGS)
848853
bool*result= (bool*)PG_GETARG_POINTER(2);
849854

850855
if (b1&&b2)
851-
*result=DatumGetBool(DirectFunctionCall2(box_same,
852-
PointerGetDatum(b1),
853-
PointerGetDatum(b2)));
856+
*result= (b1->low.x==b2->low.x&&b1->low.y==b2->low.y&&
857+
b1->high.x==b2->high.x&&b1->high.y==b2->high.y);
854858
else
855-
*result= (b1==NULL&&b2==NULL) ? TRUE : FALSE;
859+
*result= (b1==NULL&&b2==NULL);
856860
PG_RETURN_POINTER(result);
857861
}
858862

@@ -1296,13 +1300,16 @@ gist_point_consistent_internal(StrategyNumber strategy,
12961300
caseRTSameStrategyNumber:
12971301
if (isLeaf)
12981302
{
1299-
result=FPeq(key->low.x,query->x)
1300-
&&FPeq(key->low.y,query->y);
1303+
/* key.high must equal key.low, so we can disregard it */
1304+
result= (FPeq(key->low.x,query->x)&&
1305+
FPeq(key->low.y,query->y));
13011306
}
13021307
else
13031308
{
1304-
result= (query->x <=key->high.x&&query->x >=key->low.x&&
1305-
query->y <=key->high.y&&query->y >=key->low.y);
1309+
result= (FPle(query->x,key->high.x)&&
1310+
FPge(query->x,key->low.x)&&
1311+
FPle(query->y,key->high.y)&&
1312+
FPge(query->y,key->low.y));
13061313
}
13071314
break;
13081315
default:
@@ -1337,12 +1344,31 @@ gist_point_consistent(PG_FUNCTION_ARGS)
13371344
*recheck= false;
13381345
break;
13391346
caseBoxStrategyNumberGroup:
1340-
result=DatumGetBool(DirectFunctionCall5(
1341-
gist_box_consistent,
1342-
PointerGetDatum(entry),
1343-
PG_GETARG_DATUM(1),
1344-
Int16GetDatum(RTOverlapStrategyNumber),
1345-
0,PointerGetDatum(recheck)));
1347+
{
1348+
/*
1349+
* The only operator in this group is point <@ box (on_pb), so
1350+
* we needn't examine strategy again.
1351+
*
1352+
* For historical reasons, on_pb uses exact rather than fuzzy
1353+
* comparisons. We could use box_overlap when at an internal
1354+
* page, but that would lead to possibly visiting child pages
1355+
* uselessly, because box_overlap uses fuzzy comparisons.
1356+
* Instead we write a non-fuzzy overlap test. The same code
1357+
* will also serve for leaf-page tests, since leaf keys have
1358+
* high == low.
1359+
*/
1360+
BOX*query,
1361+
*key;
1362+
1363+
query=PG_GETARG_BOX_P(1);
1364+
key=DatumGetBoxP(entry->key);
1365+
1366+
result= (key->high.x >=query->low.x&&
1367+
key->low.x <=query->high.x&&
1368+
key->high.y >=query->low.y&&
1369+
key->low.y <=query->high.y);
1370+
*recheck= false;
1371+
}
13461372
break;
13471373
casePolygonStrategyNumberGroup:
13481374
{

‎src/test/regress/expected/point.out

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,53 @@ SELECT '' AS three, p1.f1 AS point1, p2.f1 AS point2, (p1.f1 <-> p2.f1) AS dista
245245
| (5.1,34.5) | (10,10) | 24.9851956166046
246246
(3 rows)
247247

248+
-- Test that GiST indexes provide same behavior as sequential scan
249+
CREATE TEMP TABLE point_gist_tbl(f1 point);
250+
INSERT INTO point_gist_tbl SELECT '(0,0)' FROM generate_series(0,1000);
251+
CREATE INDEX point_gist_tbl_index ON point_gist_tbl USING gist (f1);
252+
INSERT INTO point_gist_tbl VALUES ('(0.0000009,0.0000009)');
253+
SET enable_seqscan TO true;
254+
SET enable_indexscan TO false;
255+
SET enable_bitmapscan TO false;
256+
SELECT COUNT(*) FROM point_gist_tbl WHERE f1 ~= '(0.0000009,0.0000009)'::point;
257+
count
258+
-------
259+
1002
260+
(1 row)
261+
262+
SELECT COUNT(*) FROM point_gist_tbl WHERE f1 <@ '(0.0000009,0.0000009),(0.0000009,0.0000009)'::box;
263+
count
264+
-------
265+
1
266+
(1 row)
267+
268+
SELECT COUNT(*) FROM point_gist_tbl WHERE f1 ~= '(0.0000018,0.0000018)'::point;
269+
count
270+
-------
271+
1
272+
(1 row)
273+
274+
SET enable_seqscan TO false;
275+
SET enable_indexscan TO true;
276+
SET enable_bitmapscan TO true;
277+
SELECT COUNT(*) FROM point_gist_tbl WHERE f1 ~= '(0.0000009,0.0000009)'::point;
278+
count
279+
-------
280+
1002
281+
(1 row)
282+
283+
SELECT COUNT(*) FROM point_gist_tbl WHERE f1 <@ '(0.0000009,0.0000009),(0.0000009,0.0000009)'::box;
284+
count
285+
-------
286+
1
287+
(1 row)
288+
289+
SELECT COUNT(*) FROM point_gist_tbl WHERE f1 ~= '(0.0000018,0.0000018)'::point;
290+
count
291+
-------
292+
1
293+
(1 row)
294+
295+
RESET enable_seqscan;
296+
RESET enable_indexscan;
297+
RESET enable_bitmapscan;

‎src/test/regress/sql/point.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,24 @@ SELECT '' AS three, p1.f1 AS point1, p2.f1 AS point2, (p1.f1 <-> p2.f1) AS dista
8080
FROM POINT_TBL p1, POINT_TBL p2
8181
WHERE (p1.f1<->p2.f1)>3andp1.f1<<p2.f1andp1.f1>^p2.f1
8282
ORDER BY distance;
83+
84+
-- Test that GiST indexes provide same behavior as sequential scan
85+
CREATE TEMP TABLE point_gist_tbl(f1point);
86+
INSERT INTO point_gist_tblSELECT'(0,0)'FROM generate_series(0,1000);
87+
CREATEINDEXpoint_gist_tbl_indexON point_gist_tbl USING gist (f1);
88+
INSERT INTO point_gist_tblVALUES ('(0.0000009,0.0000009)');
89+
SET enable_seqscan TO true;
90+
SET enable_indexscan TO false;
91+
SET enable_bitmapscan TO false;
92+
SELECTCOUNT(*)FROM point_gist_tblWHERE f1 ~='(0.0000009,0.0000009)'::point;
93+
SELECTCOUNT(*)FROM point_gist_tblWHERE f1<@'(0.0000009,0.0000009),(0.0000009,0.0000009)'::box;
94+
SELECTCOUNT(*)FROM point_gist_tblWHERE f1 ~='(0.0000018,0.0000018)'::point;
95+
SET enable_seqscan TO false;
96+
SET enable_indexscan TO true;
97+
SET enable_bitmapscan TO true;
98+
SELECTCOUNT(*)FROM point_gist_tblWHERE f1 ~='(0.0000009,0.0000009)'::point;
99+
SELECTCOUNT(*)FROM point_gist_tblWHERE f1<@'(0.0000009,0.0000009),(0.0000009,0.0000009)'::box;
100+
SELECTCOUNT(*)FROM point_gist_tblWHERE f1 ~='(0.0000018,0.0000018)'::point;
101+
RESET enable_seqscan;
102+
RESET enable_indexscan;
103+
RESET enable_bitmapscan;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp