1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
--
-- Test GiST indexes.
--
-- There are other tests to test different GiST opclasses. This is for
-- testing GiST code itself. Vacuuming in particular.
create table gist_point_tbl(id int4, p point);
create index gist_pointidx on gist_point_tbl using gist(p);
-- Verify the fillfactor and buffering options
create index gist_pointidx2 on gist_point_tbl using gist(p) with (buffering = on, fillfactor=50);
create index gist_pointidx3 on gist_point_tbl using gist(p) with (buffering = off);
create index gist_pointidx4 on gist_point_tbl using gist(p) with (buffering = auto);
drop index gist_pointidx2, gist_pointidx3, gist_pointidx4;
-- Make sure bad values are refused
create index gist_pointidx5 on gist_point_tbl using gist(p) with (buffering = invalid_value);
ERROR: invalid value for enum option "buffering": invalid_value
DETAIL: Valid values are "on", "off", and "auto".
create index gist_pointidx5 on gist_point_tbl using gist(p) with (fillfactor=9);
ERROR: value 9 out of bounds for option "fillfactor"
DETAIL: Valid values are between "10" and "100".
create index gist_pointidx5 on gist_point_tbl using gist(p) with (fillfactor=101);
ERROR: value 101 out of bounds for option "fillfactor"
DETAIL: Valid values are between "10" and "100".
-- Insert enough data to create a tree that's a couple of levels deep.
insert into gist_point_tbl (id, p)
select g, point(g*10, g*10) from generate_series(1, 10000) g;
insert into gist_point_tbl (id, p)
select g+100000, point(g*10+1, g*10+1) from generate_series(1, 10000) g;
-- To test vacuum, delete some entries from all over the index.
delete from gist_point_tbl where id % 2 = 1;
-- And also delete some concentration of values.
delete from gist_point_tbl where id > 5000;
vacuum analyze gist_point_tbl;
-- rebuild the index with a different fillfactor
alter index gist_pointidx SET (fillfactor = 40);
reindex index gist_pointidx;
--
-- Test Index-only plans on GiST indexes
--
create table gist_tbl (b box, p point, c circle);
insert into gist_tbl
select box(point(0.05*i, 0.05*i), point(0.05*i, 0.05*i)),
point(0.05*i, 0.05*i),
circle(point(0.05*i, 0.05*i), 1.0)
from generate_series(0,10000) as i;
vacuum analyze gist_tbl;
set enable_seqscan=off;
set enable_bitmapscan=off;
set enable_indexonlyscan=on;
-- Test index-only scan with point opclass
create index gist_tbl_point_index on gist_tbl using gist (p);
-- check that the planner chooses an index-only scan
explain (costs off)
select p from gist_tbl where p <@ box(point(0,0), point(0.5, 0.5));
QUERY PLAN
--------------------------------------------------------
Index Only Scan using gist_tbl_point_index on gist_tbl
Index Cond: (p <@ '(0.5,0.5),(0,0)'::box)
(2 rows)
-- execute the same
select p from gist_tbl where p <@ box(point(0,0), point(0.5, 0.5));
p
-------------
(0,0)
(0.05,0.05)
(0.1,0.1)
(0.15,0.15)
(0.2,0.2)
(0.25,0.25)
(0.3,0.3)
(0.35,0.35)
(0.4,0.4)
(0.45,0.45)
(0.5,0.5)
(11 rows)
-- Also test an index-only knn-search
explain (costs off)
select p from gist_tbl where p <@ box(point(0,0), point(0.5, 0.5))
order by p <-> point(0.201, 0.201);
QUERY PLAN
--------------------------------------------------------
Index Only Scan using gist_tbl_point_index on gist_tbl
Index Cond: (p <@ '(0.5,0.5),(0,0)'::box)
Order By: (p <-> '(0.201,0.201)'::point)
(3 rows)
select p from gist_tbl where p <@ box(point(0,0), point(0.5, 0.5))
order by p <-> point(0.201, 0.201);
p
-------------
(0.2,0.2)
(0.25,0.25)
(0.15,0.15)
(0.3,0.3)
(0.1,0.1)
(0.35,0.35)
(0.05,0.05)
(0.4,0.4)
(0,0)
(0.45,0.45)
(0.5,0.5)
(11 rows)
-- Check commuted case as well
explain (costs off)
select p from gist_tbl where p <@ box(point(0,0), point(0.5, 0.5))
order by point(0.101, 0.101) <-> p;
QUERY PLAN
--------------------------------------------------------
Index Only Scan using gist_tbl_point_index on gist_tbl
Index Cond: (p <@ '(0.5,0.5),(0,0)'::box)
Order By: (p <-> '(0.101,0.101)'::point)
(3 rows)
select p from gist_tbl where p <@ box(point(0,0), point(0.5, 0.5))
order by point(0.101, 0.101) <-> p;
p
-------------
(0.1,0.1)
(0.15,0.15)
(0.05,0.05)
(0.2,0.2)
(0,0)
(0.25,0.25)
(0.3,0.3)
(0.35,0.35)
(0.4,0.4)
(0.45,0.45)
(0.5,0.5)
(11 rows)
-- Check case with multiple rescans (bug #14641)
explain (costs off)
select p from
(values (box(point(0,0), point(0.5,0.5))),
(box(point(0.5,0.5), point(0.75,0.75))),
(box(point(0.8,0.8), point(1.0,1.0)))) as v(bb)
cross join lateral
(select p from gist_tbl where p <@ bb order by p <-> bb[0] limit 2) ss;
QUERY PLAN
--------------------------------------------------------------------
Nested Loop
-> Values Scan on "*VALUES*"
-> Limit
-> Index Only Scan using gist_tbl_point_index on gist_tbl
Index Cond: (p <@ "*VALUES*".column1)
Order By: (p <-> ("*VALUES*".column1)[0])
(6 rows)
select p from
(values (box(point(0,0), point(0.5,0.5))),
(box(point(0.5,0.5), point(0.75,0.75))),
(box(point(0.8,0.8), point(1.0,1.0)))) as v(bb)
cross join lateral
(select p from gist_tbl where p <@ bb order by p <-> bb[0] limit 2) ss;
p
-------------
(0.5,0.5)
(0.45,0.45)
(0.75,0.75)
(0.7,0.7)
(1,1)
(0.95,0.95)
(6 rows)
drop index gist_tbl_point_index;
-- Test index-only scan with box opclass
create index gist_tbl_box_index on gist_tbl using gist (b);
-- check that the planner chooses an index-only scan
explain (costs off)
select b from gist_tbl where b <@ box(point(5,5), point(6,6));
QUERY PLAN
------------------------------------------------------
Index Only Scan using gist_tbl_box_index on gist_tbl
Index Cond: (b <@ '(6,6),(5,5)'::box)
(2 rows)
-- execute the same
select b from gist_tbl where b <@ box(point(5,5), point(6,6));
b
-------------------------
(5,5),(5,5)
(5.05,5.05),(5.05,5.05)
(5.1,5.1),(5.1,5.1)
(5.15,5.15),(5.15,5.15)
(5.2,5.2),(5.2,5.2)
(5.25,5.25),(5.25,5.25)
(5.3,5.3),(5.3,5.3)
(5.35,5.35),(5.35,5.35)
(5.4,5.4),(5.4,5.4)
(5.45,5.45),(5.45,5.45)
(5.5,5.5),(5.5,5.5)
(5.55,5.55),(5.55,5.55)
(5.6,5.6),(5.6,5.6)
(5.65,5.65),(5.65,5.65)
(5.7,5.7),(5.7,5.7)
(5.75,5.75),(5.75,5.75)
(5.8,5.8),(5.8,5.8)
(5.85,5.85),(5.85,5.85)
(5.9,5.9),(5.9,5.9)
(5.95,5.95),(5.95,5.95)
(6,6),(6,6)
(21 rows)
-- Also test an index-only knn-search
explain (costs off)
select b from gist_tbl where b <@ box(point(5,5), point(6,6))
order by b <-> point(5.2, 5.91);
QUERY PLAN
------------------------------------------------------
Index Only Scan using gist_tbl_box_index on gist_tbl
Index Cond: (b <@ '(6,6),(5,5)'::box)
Order By: (b <-> '(5.2,5.91)'::point)
(3 rows)
select b from gist_tbl where b <@ box(point(5,5), point(6,6))
order by b <-> point(5.2, 5.91);
b
-------------------------
(5.55,5.55),(5.55,5.55)
(5.6,5.6),(5.6,5.6)
(5.5,5.5),(5.5,5.5)
(5.65,5.65),(5.65,5.65)
(5.45,5.45),(5.45,5.45)
(5.7,5.7),(5.7,5.7)
(5.4,5.4),(5.4,5.4)
(5.75,5.75),(5.75,5.75)
(5.35,5.35),(5.35,5.35)
(5.8,5.8),(5.8,5.8)
(5.3,5.3),(5.3,5.3)
(5.85,5.85),(5.85,5.85)
(5.25,5.25),(5.25,5.25)
(5.9,5.9),(5.9,5.9)
(5.2,5.2),(5.2,5.2)
(5.95,5.95),(5.95,5.95)
(5.15,5.15),(5.15,5.15)
(6,6),(6,6)
(5.1,5.1),(5.1,5.1)
(5.05,5.05),(5.05,5.05)
(5,5),(5,5)
(21 rows)
-- Check commuted case as well
explain (costs off)
select b from gist_tbl where b <@ box(point(5,5), point(6,6))
order by point(5.2, 5.91) <-> b;
QUERY PLAN
------------------------------------------------------
Index Only Scan using gist_tbl_box_index on gist_tbl
Index Cond: (b <@ '(6,6),(5,5)'::box)
Order By: (b <-> '(5.2,5.91)'::point)
(3 rows)
select b from gist_tbl where b <@ box(point(5,5), point(6,6))
order by point(5.2, 5.91) <-> b;
b
-------------------------
(5.55,5.55),(5.55,5.55)
(5.6,5.6),(5.6,5.6)
(5.5,5.5),(5.5,5.5)
(5.65,5.65),(5.65,5.65)
(5.45,5.45),(5.45,5.45)
(5.7,5.7),(5.7,5.7)
(5.4,5.4),(5.4,5.4)
(5.75,5.75),(5.75,5.75)
(5.35,5.35),(5.35,5.35)
(5.8,5.8),(5.8,5.8)
(5.3,5.3),(5.3,5.3)
(5.85,5.85),(5.85,5.85)
(5.25,5.25),(5.25,5.25)
(5.9,5.9),(5.9,5.9)
(5.2,5.2),(5.2,5.2)
(5.95,5.95),(5.95,5.95)
(5.15,5.15),(5.15,5.15)
(6,6),(6,6)
(5.1,5.1),(5.1,5.1)
(5.05,5.05),(5.05,5.05)
(5,5),(5,5)
(21 rows)
drop index gist_tbl_box_index;
-- Test that an index-only scan is not chosen, when the query involves the
-- circle column (the circle opclass does not support index-only scans).
create index gist_tbl_multi_index on gist_tbl using gist (p, c);
explain (costs off)
select p, c from gist_tbl
where p <@ box(point(5,5), point(6, 6));
QUERY PLAN
---------------------------------------------------
Index Scan using gist_tbl_multi_index on gist_tbl
Index Cond: (p <@ '(6,6),(5,5)'::box)
(2 rows)
-- execute the same
select b, p from gist_tbl
where b <@ box(point(4.5, 4.5), point(5.5, 5.5))
and p <@ box(point(5,5), point(6, 6));
b | p
-------------------------+-------------
(5,5),(5,5) | (5,5)
(5.05,5.05),(5.05,5.05) | (5.05,5.05)
(5.1,5.1),(5.1,5.1) | (5.1,5.1)
(5.15,5.15),(5.15,5.15) | (5.15,5.15)
(5.2,5.2),(5.2,5.2) | (5.2,5.2)
(5.25,5.25),(5.25,5.25) | (5.25,5.25)
(5.3,5.3),(5.3,5.3) | (5.3,5.3)
(5.35,5.35),(5.35,5.35) | (5.35,5.35)
(5.4,5.4),(5.4,5.4) | (5.4,5.4)
(5.45,5.45),(5.45,5.45) | (5.45,5.45)
(5.5,5.5),(5.5,5.5) | (5.5,5.5)
(11 rows)
drop index gist_tbl_multi_index;
-- Test that we don't try to return the value of a non-returnable
-- column in an index-only scan. (This isn't GIST-specific, but
-- it only applies to index AMs that can return some columns and not
-- others, so GIST with appropriate opclasses is a convenient test case.)
create index gist_tbl_multi_index on gist_tbl using gist (circle(p,1), p);
explain (verbose, costs off)
select circle(p,1) from gist_tbl
where p <@ box(point(5, 5), point(5.3, 5.3));
QUERY PLAN
---------------------------------------------------------------
Index Only Scan using gist_tbl_multi_index on public.gist_tbl
Output: circle(p, '1'::double precision)
Index Cond: (gist_tbl.p <@ '(5.3,5.3),(5,5)'::box)
(3 rows)
select circle(p,1) from gist_tbl
where p <@ box(point(5, 5), point(5.3, 5.3));
circle
-----------------
<(5,5),1>
<(5.05,5.05),1>
<(5.1,5.1),1>
<(5.15,5.15),1>
<(5.2,5.2),1>
<(5.25,5.25),1>
<(5.3,5.3),1>
(7 rows)
-- Similarly, test that index rechecks involving a non-returnable column
-- are done correctly.
explain (verbose, costs off)
select p from gist_tbl where circle(p,1) @> circle(point(0,0),0.95);
QUERY PLAN
---------------------------------------------------------------------------------------
Index Only Scan using gist_tbl_multi_index on public.gist_tbl
Output: p
Index Cond: ((circle(gist_tbl.p, '1'::double precision)) @> '<(0,0),0.95>'::circle)
(3 rows)
select p from gist_tbl where circle(p,1) @> circle(point(0,0),0.95);
p
-------
(0,0)
(1 row)
-- Also check that use_physical_tlist doesn't trigger in such cases.
explain (verbose, costs off)
select count(*) from gist_tbl;
QUERY PLAN
---------------------------------------------------------------------
Aggregate
Output: count(*)
-> Index Only Scan using gist_tbl_multi_index on public.gist_tbl
(3 rows)
select count(*) from gist_tbl;
count
-------
10001
(1 row)
-- This case isn't supported, but it should at least EXPLAIN correctly.
explain (verbose, costs off)
select p from gist_tbl order by circle(p,1) <-> point(0,0) limit 1;
QUERY PLAN
------------------------------------------------------------------------------------
Limit
Output: p, ((circle(p, '1'::double precision) <-> '(0,0)'::point))
-> Index Only Scan using gist_tbl_multi_index on public.gist_tbl
Output: p, (circle(p, '1'::double precision) <-> '(0,0)'::point)
Order By: ((circle(gist_tbl.p, '1'::double precision)) <-> '(0,0)'::point)
(5 rows)
select p from gist_tbl order by circle(p,1) <-> point(0,0) limit 1;
ERROR: lossy distance functions are not supported in index-only scans
-- Clean up
reset enable_seqscan;
reset enable_bitmapscan;
reset enable_indexonlyscan;
drop table gist_tbl;
|