BRIN Indexes
index
BRIN
Introduction
BRIN stands for Block Range Index.
BRIN is designed for handling very large tables
in which certain columns have some natural correlation with their
physical location within the table.
A 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.
BRIN 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 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 BRIN 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.
The specific data that a BRIN 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.
The size of the block range is determined at index creation time by
the 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 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.
Built-in Operator Classes
The core PostgreSQL distribution
includes the BRIN operator classes shown in
.
The minmax>
operator classes store the minimum and the maximum values appearing
in the indexed column within the range.
Built-in BRIN Operator Classes
Name
Indexed Data Type
Indexable Operators
bytea_minmax_ops
bytea
<
<=
=
>=
>
char_minmax_ops
"char"
<
<=
=
>=
>
name_minmax_ops
name
<
<=
=
>=
>
int8_minmax_ops
bigint
<
<=
=
>=
>
int2_minmax_ops
smallint
<
<=
=
>=
>
int4_minmax_ops
integer
<
<=
=
>=
>
text_minmax_ops
text
<
<=
=
>=
>
oid_minmax_ops
oid
<
<=
=
>=
>
tid_minmax_ops
tid
<
<=
=
>=
>
float4_minmax_ops
real
<
<=
=
>=
>
float8_minmax_ops
double precision
<
<=
=
>=
>
abstime_minmax_ops
abstime
<
<=
=
>=
>
reltime_minmax_ops
reltime
<
<=
=
>=
>
macaddr_minmax_ops
macaddr
<
<=
=
>=
>
inet_minmax_ops
inet
<
<=
=
>=
>
bpchar_minmax_ops
character
<
<=
=
>=
>
date_minmax_ops
date
<
<=
=
>=
>
time_minmax_ops
time without time zone
<
<=
=
>=
>
timestamp_minmax_ops
timestamp without time zone
<
<=
=
>=
>
timestamptz_minmax_ops
timestamp with time zone
<
<=
=
>=
>
interval_minmax_ops
interval
<
<=
=
>=
>
timetz_minmax_ops
time with time zone
<
<=
=
>=
>
bit_minmax_ops
bit
<
<=
=
>=
>
varbit_minmax_ops
bit varying
<
<=
=
>=
>
numeric_minmax_ops
numeric
<
<=
=
>=
>
uuid_minmax_ops
uuid
<
<=
=
>=
>
pg_lsn_minmax_ops
pg_lsn
<
<=
=
>=
>
Extensibility
The BRIN interface has a high level of abstraction,
requiring the access method implementer only to implement the semantics
of the data type being accessed. The BRIN layer
itself takes care of concurrency, logging and searching the index structure.
All it takes to get a BRIN 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, BRIN combines
extensibility with generality, code reuse, and a clean interface.
There are four methods that an operator class for BRIN
must provide:
BrinOpcInfo *opcInfo(Oid type_oid)>
Returns internal information about the indexed columns' summary data.
The return value must point to a palloc'd BrinOpcInfo>,
which has this definition:
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;
BrinOpcInfo>.oi_opaque> can be used by the
operator class routines to pass information between support procedures
during an index scan.
bool consistent(BrinDesc *bdesc, BrinValues *column,
ScanKey key)
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.
bool addValue(BrinDesc *bdesc, BrinValues *column,
Datum newval, bool isnull)
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, true is
returned.
bool unionTuples(BrinDesc *bdesc, BrinValues *a,
BrinValues *b)
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.
To implement these methods in a generic way, the operator class
defines its own internal support functions.
(For instance, 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.