# pg_brin_bins BRIN operator classes that store a bitmap of the value bins that appear in a block range. The bins operator classes split the value domain of the indexed column into 64 bins and record, with one bit per bin, which bins contain at least one value within each block range. This is based on the idea of *column imprints* (Sidirourgos and Kersten, "Column Imprints: A Secondary Index Structure", SIGMOD 2013). This extension may be effective when you have a large table in which a column's values are concentrated in many narrow bands, and rows from different bands are stored interleaved with each other. See https://www.postgresql.org/docs/devel/brin.html for BRIN itself. ## How it works - The range `[min_boundary_value, max_boundary_value)` is divided into 62 bins of equal width. - Bin 0 holds values below `min_boundary_value`, and bin 63 holds values at or above `max_boundary_value` (overflow bins). - Each block range is summarized as a 64-bit bitmap (8 bytes per range). - A query condition is converted into a range of bins, and a block range is skipped when none of those bins is set in its bitmap. ## Installation Simply run `make` at the top of the source tree, then make install as an appropriate user. The `PATH` environment variable should be set properly to point to a PostgreSQL set of binaries: ```sh $ make $ su $ make install ``` Connect to the database where you want to install pg_brin_bins and run `CREATE EXTENSION`: ```sql CREATE EXTENSION pg_brin_bins; ``` ## Usage The operator class name and both options are required. ```sql CREATE INDEX ON measurements USING brin (temperature float8_bins_ops (min_boundary_value = -30, max_boundary_value = 200)); ``` ### Choosing the boundaries Using percentiles keeps outliers in the overflow bins. With psql: ```sql SELECT percentile_disc(0.01) WITHIN GROUP (ORDER BY v) AS lo, percentile_disc(0.99) WITHIN GROUP (ORDER BY v) AS hi FROM t \gset CREATE INDEX ON t USING brin (v int8_bins_ops (min_boundary_value = :lo, max_boundary_value = :hi)); ``` For `date`, `timestamp` and `timestamptz`, the boundaries are given in the internal representation. Use `brin_bins_datetime_value()` to convert them: ```sql SELECT brin_bins_datetime_value(percentile_disc(0.01) WITHIN GROUP (ORDER BY ts)) AS lo, brin_bins_datetime_value(percentile_disc(0.99) WITHIN GROUP (ORDER BY ts)) AS hi FROM events \gset CREATE INDEX ON events USING brin (ts timestamptz_bins_ops (min_boundary_value = :lo, max_boundary_value = :hi)); ``` To use fixed dates, write the time zone explicitly: ```sql SELECT brin_bins_datetime_value(TIMESTAMPTZ '2026-01-01 00:00:00+00') AS lo, brin_bins_datetime_value(TIMESTAMPTZ '2027-01-01 00:00:00+00') AS hi \gset ``` ## Functions | Function | Description | |---|---| | `brin_bins_datetime_value(date)` | Days since 2000-01-01 | | `brin_bins_datetime_value(timestamp)` | Microseconds since 2000-01-01 00:00:00 (no time zone conversion) | | `brin_bins_datetime_value(timestamptz)` | Microseconds since 2000-01-01 00:00:00 UTC | ## Options | Option | Description | |---|---| | `min_boundary_value` | Lower boundary of the inner bins (required) | | `max_boundary_value` | Upper boundary of the inner bins (required) | `min_boundary_value` must be less than `max_boundary_value`. ## Supported types and operators | Operator family | Operator classes | |---|---| | `integer_bins_ops` | `int2_bins_ops`, `int4_bins_ops`, `int8_bins_ops` | | `float_bins_ops` | `float4_bins_ops`, `float8_bins_ops` | | `datetime_bins_ops` | `date_bins_ops`, `timestamp_bins_ops`, `timestamptz_bins_ops` | Supported operators: `<`, `<=`, `=`, `>=`, `>`. Cross-type comparisons within the same operator family (e.g. an `int8` column compared with an `int4` constant) are supported. ## Support version PostgreSQL 15 and higher version ## Limitation (future TODO) - The size of the bin is fixed (64) - supported data types are: int2 (smallint), int4 (int), int8 (bigint), float4 (real), float8 (double precision), date, timestamp, timestamp with timezone - Only equal-width bins are supported