Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Postgres Difficulty: Advanced ClaudeChatGPTCursor

Postgres BRIN Index Design for Time-Series Tables Prompt

Decide whether a BRIN index will actually speed up your large, naturally-ordered table — and size pages_per_range correctly — instead of blindly reaching for a B-tree that bloats storage on append-only data.

Target user
DBAs and backend engineers managing large append-only or time-series Postgres tables
Difficulty
Advanced
Tools
Claude, ChatGPT, Cursor

The prompt

You are a senior PostgreSQL DBA who specializes in very large, append-mostly tables
(events, metrics, logs, ledgers). You reason from physical correlation and page layout,
not from index folklore, and you always verify a design choice with EXPLAIN and
pg_stats before recommending it.

I will paste:
- The table definition and approximate row count / on-disk size: [TABLE / SIZE]
- The candidate range column (usually a timestamp or bigint id): [RANGE COLUMN]
- The most important range queries you run against it: [QUERIES]
- Ingest pattern (append-only, updated in place, backfilled, bulk-loaded): [WRITE PATTERN]
- Postgres version: [VERSION]

Work through this in order:

1. **Test physical correlation first.** BRIN only helps when rows are stored in
   roughly the same order as the range column. Have me run
   `SELECT attname, correlation FROM pg_stats WHERE tablename = '...' AND attname = '...';`
   and interpret it: a correlation near 1.0 (or -1.0) means BRIN is viable; near 0
   means BRIN will scan most of the table and a B-tree or a CLUSTER/repack is needed first.

2. **Compare BRIN vs B-tree explicitly** for this table: index size (BRIN is often
   thousands of times smaller), maintenance cost on insert, and query selectivity.
   Say plainly when a B-tree is the better call despite the size cost.

3. **Choose pages_per_range** with reasoning: smaller values give finer granularity
   and better filtering but a larger index; relate the choice to the table's page count
   and typical query range. Give a concrete starting value and how to measure whether
   to lower it.

4. **Give the exact DDL** using `CREATE INDEX CONCURRENTLY ... USING brin (...) WITH (pages_per_range = N)`,
   plus any `autosummarize` guidance and when `brin_summarize_new_values()` is needed
   after bulk loads.

5. **Give a verification plan**: the exact `EXPLAIN (ANALYZE, BUFFERS)` for a
   representative range query, what a healthy BRIN plan looks like (Bitmap Heap Scan
   with lossy blocks, low shared reads), and the failure signature (recheck removing
   most rows) that means correlation is too low.

Output: (a) verdict — BRIN, B-tree, or fix ordering first; (b) the DDL; (c) the
verification commands in order; (d) one risk to watch.

Guardrails: never DROP the existing index until the replacement is proven on a
replica. Build all indexes CONCURRENTLY. If you recommend CLUSTER or a repack to fix
ordering, flag that it takes an ACCESS EXCLUSIVE lock (or requires pg_repack) and must
be scheduled, not run ad hoc on prod.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Why this prompt works

BRIN indexes are the right tool for a narrow but common shape — huge, naturally-ordered tables — and the wrong tool almost everywhere else. The usual failure is adopting BRIN because it is tiny, without checking whether the table is physically ordered on the range column. This prompt makes correlation the first gate, so the model never recommends BRIN for data that would force it into a near-sequential scan.

By forcing an explicit BRIN-vs-B-tree comparison and a reasoned pages_per_range, it turns a hand-wavy “use BRIN for time-series” into a sized, defensible design tied to the table’s real page count and query ranges. The verification step then proves the choice with an actual Bitmap Heap Scan plan rather than trusting theory, and the guardrails keep index swaps and any reordering safe on production.

Related prompts

More Postgres prompts & error guides

Browse every Postgres prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.