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
|
--
-- Test GIN indexes.
--
-- There are other tests to test different GIN opclasses. This is for testing
-- GIN itself.
-- Create and populate a test table with a GIN index.
create table gin_test_tbl(i int4[]) with (autovacuum_enabled = off);
create index gin_test_idx on gin_test_tbl using gin (i)
with (fastupdate = on, gin_pending_list_limit = 4096);
insert into gin_test_tbl select array[1, 2, g] from generate_series(1, 20000) g;
insert into gin_test_tbl select array[1, 3, g] from generate_series(1, 1000) g;
select gin_clean_pending_list('gin_test_idx')>10 as many; -- flush the fastupdate buffers
many
------
t
(1 row)
insert into gin_test_tbl select array[3, 1, g] from generate_series(1, 1000) g;
vacuum gin_test_tbl; -- flush the fastupdate buffers
select gin_clean_pending_list('gin_test_idx'); -- nothing to flush
gin_clean_pending_list
------------------------
0
(1 row)
-- Test vacuuming
delete from gin_test_tbl where i @> array[2];
vacuum gin_test_tbl;
-- Disable fastupdate, and do more insertions. With fastupdate enabled, most
-- insertions (by flushing the list pages) cause page splits. Without
-- fastupdate, we get more churn in the GIN data leaf pages, and exercise the
-- recompression codepaths.
alter index gin_test_idx set (fastupdate = off);
insert into gin_test_tbl select array[1, 2, g] from generate_series(1, 1000) g;
insert into gin_test_tbl select array[1, 3, g] from generate_series(1, 1000) g;
delete from gin_test_tbl where i @> array[2];
vacuum gin_test_tbl;
-- Test for "rare && frequent" searches
explain (costs off)
select count(*) from gin_test_tbl where i @> array[1, 999];
QUERY PLAN
-------------------------------------------------------
Aggregate
-> Bitmap Heap Scan on gin_test_tbl
Recheck Cond: (i @> '{1,999}'::integer[])
-> Bitmap Index Scan on gin_test_idx
Index Cond: (i @> '{1,999}'::integer[])
(5 rows)
select count(*) from gin_test_tbl where i @> array[1, 999];
count
-------
3
(1 row)
-- Very weak test for gin_fuzzy_search_limit
set gin_fuzzy_search_limit = 1000;
explain (costs off)
select count(*) > 0 as ok from gin_test_tbl where i @> array[1];
QUERY PLAN
---------------------------------------------------
Aggregate
-> Bitmap Heap Scan on gin_test_tbl
Recheck Cond: (i @> '{1}'::integer[])
-> Bitmap Index Scan on gin_test_idx
Index Cond: (i @> '{1}'::integer[])
(5 rows)
select count(*) > 0 as ok from gin_test_tbl where i @> array[1];
ok
----
t
(1 row)
reset gin_fuzzy_search_limit;
-- Test optimization of empty queries
create temp table t_gin_test_tbl(i int4[], j int4[]);
create index on t_gin_test_tbl using gin (i, j);
insert into t_gin_test_tbl
values
(null, null),
('{}', null),
('{1}', null),
('{1,2}', null),
(null, '{}'),
(null, '{10}'),
('{1,2}', '{10}'),
('{2}', '{10}'),
('{1,3}', '{}'),
('{1,1}', '{10}');
set enable_seqscan = off;
explain (costs off)
select * from t_gin_test_tbl where array[0] <@ i;
QUERY PLAN
---------------------------------------------------
Bitmap Heap Scan on t_gin_test_tbl
Recheck Cond: ('{0}'::integer[] <@ i)
-> Bitmap Index Scan on t_gin_test_tbl_i_j_idx
Index Cond: (i @> '{0}'::integer[])
(4 rows)
select * from t_gin_test_tbl where array[0] <@ i;
i | j
---+---
(0 rows)
select * from t_gin_test_tbl where array[0] <@ i and '{}'::int4[] <@ j;
i | j
---+---
(0 rows)
explain (costs off)
select * from t_gin_test_tbl where i @> '{}';
QUERY PLAN
---------------------------------------------------
Bitmap Heap Scan on t_gin_test_tbl
Recheck Cond: (i @> '{}'::integer[])
-> Bitmap Index Scan on t_gin_test_tbl_i_j_idx
Index Cond: (i @> '{}'::integer[])
(4 rows)
select * from t_gin_test_tbl where i @> '{}';
i | j
-------+------
{} |
{1} |
{1,2} |
{1,2} | {10}
{2} | {10}
{1,3} | {}
{1,1} | {10}
(7 rows)
create function explain_query_json(query_sql text)
returns table (explain_line json)
language plpgsql as
$$
begin
set enable_seqscan = off;
set enable_bitmapscan = on;
return query execute 'EXPLAIN (ANALYZE, FORMAT json) ' || query_sql;
end;
$$;
create function execute_text_query_index(query_sql text)
returns setof text
language plpgsql
as
$$
begin
set enable_seqscan = off;
set enable_bitmapscan = on;
return query execute query_sql;
end;
$$;
create function execute_text_query_heap(query_sql text)
returns setof text
language plpgsql
as
$$
begin
set enable_seqscan = on;
set enable_bitmapscan = off;
return query execute query_sql;
end;
$$;
-- check number of rows returned by index and removed by recheck
select
query,
js->0->'Plan'->'Plans'->0->'Actual Rows' as "return by index",
js->0->'Plan'->'Rows Removed by Index Recheck' as "removed by recheck",
(res_index = res_heap) as "match"
from
(values
($$ i @> '{}' $$),
($$ j @> '{}' $$),
($$ i @> '{}' and j @> '{}' $$),
($$ i @> '{1}' $$),
($$ i @> '{1}' and j @> '{}' $$),
($$ i @> '{1}' and i @> '{}' and j @> '{}' $$),
($$ j @> '{10}' $$),
($$ j @> '{10}' and i @> '{}' $$),
($$ j @> '{10}' and j @> '{}' and i @> '{}' $$),
($$ i @> '{1}' and j @> '{10}' $$)
) q(query),
lateral explain_query_json($$select * from t_gin_test_tbl where $$ || query) js,
lateral execute_text_query_index($$select string_agg((i, j)::text, ' ') from t_gin_test_tbl where $$ || query) res_index,
lateral execute_text_query_heap($$select string_agg((i, j)::text, ' ') from t_gin_test_tbl where $$ || query) res_heap;
query | return by index | removed by recheck | match
-------------------------------------------+-----------------+--------------------+-------
i @> '{}' | 7 | 0 | t
j @> '{}' | 6 | 0 | t
i @> '{}' and j @> '{}' | 4 | 0 | t
i @> '{1}' | 5 | 0 | t
i @> '{1}' and j @> '{}' | 3 | 0 | t
i @> '{1}' and i @> '{}' and j @> '{}' | 3 | 0 | t
j @> '{10}' | 4 | 0 | t
j @> '{10}' and i @> '{}' | 3 | 0 | t
j @> '{10}' and j @> '{}' and i @> '{}' | 3 | 0 | t
i @> '{1}' and j @> '{10}' | 2 | 0 | t
(10 rows)
reset enable_seqscan;
reset enable_bitmapscan;
-- re-purpose t_gin_test_tbl to test scans involving posting trees
insert into t_gin_test_tbl select array[1, g, g/10], array[2, g, g/10]
from generate_series(1, 20000) g;
select gin_clean_pending_list('t_gin_test_tbl_i_j_idx') is not null;
?column?
----------
t
(1 row)
analyze t_gin_test_tbl;
set enable_seqscan = off;
set enable_bitmapscan = on;
explain (costs off)
select count(*) from t_gin_test_tbl where j @> array[50];
QUERY PLAN
---------------------------------------------------------
Aggregate
-> Bitmap Heap Scan on t_gin_test_tbl
Recheck Cond: (j @> '{50}'::integer[])
-> Bitmap Index Scan on t_gin_test_tbl_i_j_idx
Index Cond: (j @> '{50}'::integer[])
(5 rows)
select count(*) from t_gin_test_tbl where j @> array[50];
count
-------
11
(1 row)
explain (costs off)
select count(*) from t_gin_test_tbl where j @> array[2];
QUERY PLAN
---------------------------------------------------------
Aggregate
-> Bitmap Heap Scan on t_gin_test_tbl
Recheck Cond: (j @> '{2}'::integer[])
-> Bitmap Index Scan on t_gin_test_tbl_i_j_idx
Index Cond: (j @> '{2}'::integer[])
(5 rows)
select count(*) from t_gin_test_tbl where j @> array[2];
count
-------
20000
(1 row)
explain (costs off)
select count(*) from t_gin_test_tbl where j @> '{}'::int[];
QUERY PLAN
---------------------------------------------------------
Aggregate
-> Bitmap Heap Scan on t_gin_test_tbl
Recheck Cond: (j @> '{}'::integer[])
-> Bitmap Index Scan on t_gin_test_tbl_i_j_idx
Index Cond: (j @> '{}'::integer[])
(5 rows)
select count(*) from t_gin_test_tbl where j @> '{}'::int[];
count
-------
20006
(1 row)
-- test vacuuming of posting trees
delete from t_gin_test_tbl where j @> array[2];
vacuum t_gin_test_tbl;
select count(*) from t_gin_test_tbl where j @> array[50];
count
-------
0
(1 row)
select count(*) from t_gin_test_tbl where j @> array[2];
count
-------
0
(1 row)
select count(*) from t_gin_test_tbl where j @> '{}'::int[];
count
-------
6
(1 row)
reset enable_seqscan;
reset enable_bitmapscan;
drop table t_gin_test_tbl;
|