Skip to content

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

incident.io 13 min read Case study mediumAdept

The side quest that freed 10% of our database

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%.

Related posts

  • How and when to use btree_gist

    Neon mediumAdept

    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.

  • PostgreSQL Stories: From slow query to fast—via stats

    Render mediumAdept

    A simple LIMIT 1 join between a ~100k-row postgres_dbs table and a ~90M-row events table ran for hours. The cause was a merge join that scanned ~79M non-matching events sorted ahead of the 'dpg-' serviceId prefix. Adding manual lower and upper bounds on the indexed JSON expression brought the query down to milliseconds, but the real fault was that PostgreSQL only kept statistics on whole JSON objects, not on data->>'serviceId'. Running CREATE STATISTICS on that expression gave the planner accurate distributions, and the original query then finished in under a millisecond without rewrites.

  • Choosing the right Postgres indexes

    incident.io mediumAdept

    Postgres indexes pay off for frequently run queries that discard most rows they read, need results in a specific order, or must enforce uniqueness. In a 500k-row example, adding a B-tree index cuts a lookup from 215ms to 0.05ms. Put the most frequently filtered columns first in composite indexes, prefer reusable indexes over narrowly tailored ones, and use partial indexes (e.g. WHERE archived_at IS NULL) to keep them small. Missing indexes can be spotted with a Grafana panel on pg_stat_user_tables_seq_tup_read and confirmed with EXPLAIN ANALYZE.

  • Introducing Postgres Query Insights in ClickHouse Cloud

    ClickHouse mediumAdept

    Postgres Query Insights, now in preview for ClickHouse Cloud Managed Postgres, ranks every normalized query pattern by impact and aggregates per-execution counters (CPU, cache vs disk reads, temp spills, unlaunched parallel workers, WAL) to explain why a pattern is slow. A worked example traces a p99 regression to a sort spilling to disk, confirmed with EXPLAIN (ANALYZE, BUFFERS) and fixed with an index or work_mem tuning. Telemetry comes from pg_stat_ch, an Apache 2.0 extension that normalizes literals at parse-analyze time so values and PII never leave the database. It enqueues raw per-statement events via a non-blocking try-lock on a shared-memory ring buffer at about 3% overhead, dropping events under pressure rather than back-pressuring Postgres.

  • 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.

The quest for software engineering knowledge.

Look:
Layout: