Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Neon 10 min read How-to mediumAdept

How and when to use btree_gist

Postgres's btree_gist extension lets a single GiST index combine one-dimensional B-Tree-style columns (like dates) with spatial PostGIS geometries, so queries filtering on both space and time can use one index scan. Using 38.6 million UK police crime records loaded via xsv and COPY, then transformed from WGS84 to British National Grid, a count of crimes within 1km of Stratford during the 2012 Olympics takes about 8 seconds unindexed. A GiST spatial index cuts that to about 32ms via a bitmap scan. A combined gist(location, month) index brings it to about 8ms with a plain Index Scan, roughly 1000x faster than no index.

Assumes: Postgres indexesPostGIS basicsEXPLAIN ANALYZE

Related posts

  • The side quest that freed 10% of our database

    incident.io mediumAdept

    A single lateral-join query answering "who's on call for this rotation layer right now" consumed 34% of incident.io's Postgres replica CPU. It was using a GiST exclusion-constraint index that lacked organisation_id and start_at, so every lookup read a layer's full history. Adding a five-column btree index did nothing on its own, because the planner assumed the four ID columns were independent and kept choosing the GiST index. Rewriting the predicate as a row-value comparison with a matching ORDER BY, which only btree indexes can serve, forced the new index and cut per-call time from 44ms to 0.26ms, buffers by 115x and CPU share to 0.3%.

  • ClickHouse and PostgreSQL - a Match Made in Data Heaven - part 1

    ClickHouse mediumAdept

    ClickHouse can query PostgreSQL data in place through the postgresql table function and the PostgreSQL table engine. Simple WHERE clauses are pushed down to Postgres, while aggregation, joins, sorting and LIMIT run in ClickHouse. On the 28M-row UK house price dataset, a low-selectivity filter makes Postgres choose a parallel sequential scan over the index, taking about 28s, and querying through ClickHouse gives similar times because Postgres still does the scan. Queries using ClickHouse-specific functions such as toYear are not pushed down and can run much slower. Rewriting joins with conditional aggregates like medianIf, or copying the data into local MergeTree tables, is what delivers the large speedups.

  • pg_search is Available on Neon

    Neon mediumAdept

    ParadeDB's pg_search extension, now available on Neon in AWS regions, adds a Tantivy-powered BM25 inverted index to Postgres, bringing relevance ranking, fuzzy matching, faceting, highlighting and JSON-aware search that native tsvector/GIN full-text search lacks. The BM25 index is a covering index across multiple columns, so filters and aggregations are pushed into a single index scan, and it is MVCC-integrated so new data is searchable on commit without ETL. On a 10-million-row log dataset, it built faster than a GIN index (1.63 vs 2.60 min) but was larger (647 vs 176 MB). It beat a GIN/B-tree-tuned Postgres on every query tested, with the largest gains on ranked top-N, count and filtered queries, e.g. 81 ms vs 38.8 s for BM25-ranked top-10.

  • Simplifying Time-Based Queries with Range Columns

    Supabase mediumAdept

    Postgres range types such as tstzrange replace separate start_at/end_at columns with a single range value, which makes time-window queries and overlap prevention simpler. The && operator finds overlapping ranges directly, and an exclusion constraint using GiST rejects overlapping reservations at the database level. With the btree_gist extension, the constraint can be scoped per resource, for example `exclude using gist (table_id with =, duration with &&)`. That combination yields an overlap-proof reservations table without application-side checks.

  • Hosting Postgres with GeoLite2: a practical guide to IP geolocation, data loading, and updates

    Railway mediumAdept

    Loading MaxMind's GeoLite2 CSVs into Postgres pays off over the MMDB binary format when you need SQL joins, batch geolocation of many IPs, queries over the ranges themselves, or one copy shared across services; for simple single-IP lookups MMDB is easier. Three deployment options are covered: a docker-entrypoint-initdb.d script that downloads and COPYs the data, a custom image with the data baked in, and a scheduled loader service for updates. Lookups use the cidr `>>=` operator backed by a GiST inet_ops index, which keeps queries under 10ms. Recommended practice is weekly refreshes to match MaxMind's Tuesday releases, loading into new tables and swapping them via renames in a transaction, with roughly 500-600 MB of storage for all three databases and a ~200 MB City index that should fit in shared_buffers.

The quest for software engineering knowledge.

Look:
Layout: