Skip to content

BRIN indexes for very large PostgreSQL tables

Use BRIN indexes on append-only PostgreSQL tables — when they outperform btree, how to size pages_per_range, summarisation, and the access patterns BRIN cannot help.

BRIN — Block Range INdex — stores a summary (min/max, bloom filter, multi-range) for ranges of pages instead of a per-row entry. On an append-only table where new rows arrive in roughly time-correlated order, a BRIN index on the time column is dozens to hundreds of times smaller than a btree and gives competitive query performance. This article covers when BRIN fits, how to tune pages_per_range, the maintenance pattern, and the access shapes BRIN cannot accelerate.

How to verify

Confirm BRIN indexes exist and check correlation:

psql -d app -c "\d events"
psql -d app -c "SELECT correlation FROM pg_stats
                WHERE tablename = 'events' AND attname = 'created_at';"
psql -d app -c "SELECT pg_size_pretty(pg_relation_size(indexrelid)) AS size,
                       indexrelname
                FROM pg_stat_user_indexes
                WHERE relname = 'events';"
psql -d app -c "EXPLAIN (ANALYZE, BUFFERS)
                SELECT count(*) FROM events
                WHERE created_at >= '2026-05-01' AND created_at < '2026-06-01';"

pg_stats.correlation close to ±1 means the column’s order on disk matches its values — the precondition for BRIN to be effective. Correlation near 0 means rows are scattered and BRIN will be useless.

What’s happening

A btree index has one entry per row, sorted. Lookup is O(log N), but storage is O(N) — for a 5 billion row table, that’s hundreds of GB of index. BRIN stores one entry per block range (default 128 blocks, configurable via pages_per_range). For each range it keeps a summary: for the standard brin_minmax_ops, the minimum and maximum value seen in that range. A query with WHERE created_at >= X scans the summary; ranges whose max is below X are skipped, the rest are scanned exhaustively.

BRIN works when the data is physically clustered — autoincrementing IDs and timestamps in an append-only table satisfy this naturally. It does not work when rows are inserted out of order, when the table is HOT-updated heavily (UPDATEs can move rows), or when the query column is uncorrelated with disk order (foreign keys, tenant IDs).

PG 14 added multi-minmax (brin_minmax_multi_ops) and bloom filter (brin_bloom_ops) variants. Multi-minmax keeps several non-overlapping min/max ranges per block range, useful when a few outlier rows would otherwise inflate the summary. Bloom is good for equality lookups on a clustered but unsorted column (think batch-loaded IDs in groups).

The procedure

  1. Confirm correlation. A BRIN on a column with correlation 0.1 is a waste of disk and a slow query. Check pg_stats first:

    SELECT tablename, attname, correlation
    FROM pg_stats
    WHERE tablename = 'events'
    ORDER BY abs(correlation) DESC;

    For BRIN, you want |correlation| > 0.9 ideally, > 0.7 acceptable.

  2. Create the index:

    CREATE INDEX events_created_at_brin
      ON events USING BRIN (created_at)
      WITH (pages_per_range = 128, autosummarize = on);

    pages_per_range = 128 is the default. Smaller (32, 64) gives tighter summaries — better pruning, larger index. Larger (256, 512) gives smaller index — but each range might cover days of data. For monthly time-range queries on hourly inserts, 128 is a fine default.

  3. For tables with HOT updates or any non-append churn, add a covering btree:

    CREATE INDEX events_id_btree ON events (id);

    BRIN is the secondary index; the btree on the primary key still anchors point lookups.

  4. Summarise after a bulk load:

    SELECT brin_summarize_new_values('events_created_at_brin'::regclass);

    New pages that haven’t been summarised yet are excluded from BRIN scans. Autovacuum’s autosummarize = on (PG 10+, default) does this automatically on insert, but a bulk COPY may outpace it — manual summarisation closes the gap.

  5. Combine with partitioning. BRIN on a partitioned table indexes each partition separately. The combination is powerful: partition pruning narrows to the right month, BRIN narrows within that month to the right blocks. See declarative partitioning.

  6. For PG 14+, try the multi-minmax variant if your data has outliers:

    CREATE INDEX events_created_at_brin
      ON events USING BRIN (created_at brin_minmax_multi_ops)
      WITH (values_per_range = 32);

    Useful when a few rows have wildly out-of-band timestamps (clock skew, late-arriving backfills) that would otherwise inflate every range’s max.

Common pitfalls

  • BRIN on an uncorrelated column produces a query plan that looks fine in EXPLAIN but reads almost the whole heap. Always check correlation first.
  • VACUUM doesn’t summarise new pages — brin_summarize_new_values is on autovacuum’s tail, not its main loop. Schedule it after big loads.
  • A botched re-cluster (CLUSTER table USING btree_idx) changes the on-disk order and breaks BRIN correlation. Don’t re-cluster without re-evaluating the BRIN.
  • BRIN doesn’t help ORDER BY — you still need a btree for index-only sorts.
  • The bloom variant has a fixed false-positive rate; if your equality lookups are skewed, btree may still win.

In the engagements we run, BRIN is the default secondary index on any append-only table where the primary access pattern is a time-range scan — events, metrics, logs, audit tables. It’s typically 1/100th the size of the equivalent btree and the planner handles it well. We pair BRIN with partitioning by month and a btree on the primary key. For the operating model behind a Postgres footprint at this scale, see managed operations.