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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
<!-- doc/src/sgml/brin.sgml -->
<chapter id="BRIN">
<title>BRIN Indexes</title>
<indexterm>
<primary>index</primary>
<secondary>BRIN</secondary>
</indexterm>
<sect1 id="brin-intro">
<title>Introduction</title>
<para>
<acronym>BRIN</acronym> stands for Block Range Index.
<acronym>BRIN</acronym> is designed for handling very large tables
in which certain columns have some natural correlation with their
physical location within the table.
A <firstterm>block range</> is a group of pages that are physically
adjacent in the table; for each block range, some summary info is stored
by the index.
For example, a table storing a store's sale orders might have
a date column on which each order was placed, and most of the time
the entries for earlier orders will appear earlier in the table as well;
a table storing a ZIP code column might have all codes for a city
grouped together naturally.
</para>
<para>
<acronym>BRIN</acronym> indexes can satisfy queries via regular bitmap
index scans, and will return all tuples in all pages within each range if
the summary info stored by the index is <firstterm>consistent</> with the
query conditions.
The query executor is in charge of rechecking these tuples and discarding
those that do not match the query conditions — in other words, these
indexes are lossy.
Because a <acronym>BRIN</acronym> index is very small, scanning the index
adds little overhead compared to a sequential scan, but may avoid scanning
large parts of the table that are known not to contain matching tuples.
</para>
<para>
The specific data that a <acronym>BRIN</acronym> index will store,
as well as the specific queries that the index will be able to satisfy,
depend on the operator class selected for each column of the index.
Data types having a linear sort order can have operator classes that
store the minimum and maximum value within each block range, for instance;
geometrical types might store the bounding box for all the objects
in the block range.
</para>
<para>
The size of the block range is determined at index creation time by
the <literal>pages_per_range</> storage parameter. The number of index
entries will be equal to the size of the relation in pages divided by
the selected value for <literal>pages_per_range</>. Therefore, the smaller
the number, the larger the index becomes (because of the need to
store more index entries), but at the same time the summary data stored can
be more precise and more data blocks can be skipped during an index scan.
</para>
</sect1>
<sect1 id="brin-builtin-opclasses">
<title>Built-in Operator Classes</title>
<para>
The core <productname>PostgreSQL</productname> distribution
includes the <acronym>BRIN</acronym> operator classes shown in
<xref linkend="brin-builtin-opclasses-table">.
</para>
<para>
The <firstterm>minmax</>
operator classes store the minimum and the maximum values appearing
in the indexed column within the range. The <firstterm>inclusion</>
operator classes store a value which includes the values in the indexed
column within the range.
</para>
<table id="brin-builtin-opclasses-table">
<title>Built-in <acronym>BRIN</acronym> Operator Classes</title>
<tgroup cols="3">
<thead>
<row>
<entry>Name</entry>
<entry>Indexed Data Type</entry>
<entry>Indexable Operators</entry>
</row>
</thead>
<tbody>
<row>
<entry><literal>bytea_minmax_ops</literal></entry>
<entry><type>bytea</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>char_minmax_ops</literal></entry>
<entry><type>"char"</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>name_minmax_ops</literal></entry>
<entry><type>name</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>int8_minmax_ops</literal></entry>
<entry><type>bigint</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>int2_minmax_ops</literal></entry>
<entry><type>smallint</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>int4_minmax_ops</literal></entry>
<entry><type>integer</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>text_minmax_ops</literal></entry>
<entry><type>text</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>oid_minmax_ops</literal></entry>
<entry><type>oid</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>tid_minmax_ops</literal></entry>
<entry><type>tid</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>float4_minmax_ops</literal></entry>
<entry><type>real</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>float8_minmax_ops</literal></entry>
<entry><type>double precision</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>abstime_minmax_ops</literal></entry>
<entry><type>abstime</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>reltime_minmax_ops</literal></entry>
<entry><type>reltime</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>macaddr_minmax_ops</literal></entry>
<entry><type>macaddr</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>inet_minmax_ops</literal></entry>
<entry><type>inet</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>inet_inclusion_ops</literal></entry>
<entry><type>inet</type></entry>
<entry>
<literal>&&</>
<literal>>></>
<literal>>>=</>
<literal><<</literal>
<literal><<=</literal>
<literal>=</literal>
</entry>
</row>
<row>
<entry><literal>bpchar_minmax_ops</literal></entry>
<entry><type>character</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>date_minmax_ops</literal></entry>
<entry><type>date</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>time_minmax_ops</literal></entry>
<entry><type>time without time zone</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>timestamp_minmax_ops</literal></entry>
<entry><type>timestamp without time zone</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>timestamptz_minmax_ops</literal></entry>
<entry><type>timestamp with time zone</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>interval_minmax_ops</literal></entry>
<entry><type>interval</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>timetz_minmax_ops</literal></entry>
<entry><type>time with time zone</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>bit_minmax_ops</literal></entry>
<entry><type>bit</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>varbit_minmax_ops</literal></entry>
<entry><type>bit varying</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>numeric_minmax_ops</literal></entry>
<entry><type>numeric</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>uuid_minmax_ops</literal></entry>
<entry><type>uuid</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>range_inclusion_ops</></entry>
<entry><type>any range type</type></entry>
<entry>
<literal>&&</>
<literal>&></>
<literal>&<</>
<literal>>></>
<literal><<</>
<literal><@</>
<literal>=</>
<literal>@></>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>pg_lsn_minmax_ops</literal></entry>
<entry><type>pg_lsn</type></entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>=</literal>
<literal>>=</literal>
<literal>></literal>
</entry>
</row>
<row>
<entry><literal>box_inclusion_ops</></entry>
<entry><type>box</type></entry>
<entry>
<literal>&&</>
<literal>&></>
<literal>&<</>
<literal>>></>
<literal><<</>
<literal><@</>
<literal>~=</>
<literal>@></>
<literal>&>|</>
<literal>|&<</>
<literal>>>|</>
<literal>|<<</literal>
</entry>
</row>
</tbody>
</tgroup>
</table>
</sect1>
<sect1 id="brin-extensibility">
<title>Extensibility</title>
<para>
The <acronym>BRIN</acronym> interface has a high level of abstraction,
requiring the access method implementer only to implement the semantics
of the data type being accessed. The <acronym>BRIN</acronym> layer
itself takes care of concurrency, logging and searching the index structure.
</para>
<para>
All it takes to get a <acronym>BRIN</acronym> access method working is to
implement a few user-defined methods, which define the behavior of
summary values stored in the index and the way they interact with
scan keys.
In short, <acronym>BRIN</acronym> combines
extensibility with generality, code reuse, and a clean interface.
</para>
<para>
There are four methods that an operator class for <acronym>BRIN</acronym>
must provide:
<variablelist>
<varlistentry>
<term><function>BrinOpcInfo *opcInfo(Oid type_oid)</></term>
<listitem>
<para>
Returns internal information about the indexed columns' summary data.
The return value must point to a palloc'd <structname>BrinOpcInfo</>,
which has this definition:
<programlisting>
typedef struct BrinOpcInfo
{
/* Number of columns stored in an index column of this opclass */
uint16 oi_nstored;
/* Opaque pointer for the opclass' private use */
void *oi_opaque;
/* Type cache entries of the stored columns */
TypeCacheEntry *oi_typcache[FLEXIBLE_ARRAY_MEMBER];
} BrinOpcInfo;
</programlisting>
<structname>BrinOpcInfo</>.<structfield>oi_opaque</> can be used by the
operator class routines to pass information between support procedures
during an index scan.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function>bool consistent(BrinDesc *bdesc, BrinValues *column,
ScanKey key)</function></term>
<listitem>
<para>
Returns whether the ScanKey is consistent with the given indexed
values for a range.
The attribute number to use is passed as part of the scan key.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function>bool addValue(BrinDesc *bdesc, BrinValues *column,
Datum newval, bool isnull)</function></term>
<listitem>
<para>
Given an index tuple and an indexed value, modifies the indicated
attribute of the tuple so that it additionally represents the new value.
If any modification was done to the tuple, <literal>true</literal> is
returned.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function>bool unionTuples(BrinDesc *bdesc, BrinValues *a,
BrinValues *b)</function></term>
<listitem>
<para>
Consolidates two index tuples. Given two index tuples, modifies the
indicated attribute of the first of them so that it represents both tuples.
The second tuple is not modified.
</para>
</listitem>
</varlistentry>
</variablelist>
To implement these methods in a generic way, the operator class
defines its own internal support functions.
(For instance, <quote>min/max</> operator classes implements
support functions for the four inequality operators for the data type.)
Additionally, the operator class must supply appropriate
operator entries,
to enable the optimizer to use the index when those operators are
used in queries.
</para>
</sect1>
</chapter>
|