---
title: "The side quest that freed 10% of our database"
description: "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."
url: "https://swe.quest/articles/the-side-quest-that-freed-10-of-our-database-649662623bb94a1e"
canonical: "https://incident.io/blog/the-side-quest-that-freed-10-of-our-database"
source: "incident.io"
published: "2026-11-24"
difficulty: "medium"
kind: "Case study"
reading_minutes: 13
tags: ["postgres","query-engines","performance","sql"]
---

# The side quest that freed 10% of our database

A post on [incident.io](https://swe.quest/sources/incident-io.md), published 2026-11-24, 13 min read, Case study. Original: [https://incident.io/blog/the-side-quest-that-freed-10-of-our-database](https://incident.io/blog/the-side-quest-that-freed-10-of-our-database)

## Summary

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

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Query engines](https://swe.quest/tags/query-engines.md) (Data & databases)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [SQL](https://swe.quest/tags/sql.md) (Languages)

## Prerequisites

- SQL query plans and EXPLAIN ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- btree indexes
- Postgres basics ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))

## Keywords

PostgreSQL, GiST, pg\_stat\_statements, CROSS JOIN LATERAL, incident.io

## Related posts

- [How and when to use btree\_gist](https://swe.quest/articles/how-and-when-to-use-btree-gist-a2bcb15ac54b481b.md): Neon · 2024-07-08 · Medium · 10 min read · PostgreSQL, Maps & geospatial, Performance engineering, SQL
  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.
- [PostgreSQL Stories: From slow query to fast—via stats](https://swe.quest/articles/postgresql-stories-from-slow-query-to-fast-via-stats-ac7001ab0fde8224.md): Render · 2024-05-14 · Medium · 13 min read · PostgreSQL, Query engines, Performance engineering, Debugging
  A simple LIMIT 1 join between a ~100k-row postgres\_dbs table and a ~90M-row events table ran for hours.
- [Choosing the right Postgres indexes](https://swe.quest/articles/choosing-the-right-postgres-indexes-8f3c9e0e9ee3e588.md): incident.io · 2024-10-25 · Medium · 9 min read · PostgreSQL, Performance engineering, SQL, Observability
  Postgres indexes pay off for frequently run queries that discard most rows they read, need results in a specific order, or must enforce uniqueness.
- [Introducing Postgres Query Insights in ClickHouse Cloud](https://swe.quest/articles/introducing-postgres-query-insights-in-clickhouse-cloud-a9697a827409b984.md): ClickHouse · 2026-05-07 · Medium · 7 min read · PostgreSQL, Observability, Performance engineering, Analytics & OLAP
  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.
- [ClickHouse and PostgreSQL - a Match Made in Data Heaven - part 1](https://swe.quest/articles/clickhouse-and-postgresql-a-match-made-in-data-heaven-part-1-7c7668b9b4913b70.md): ClickHouse · 2022-12-20 · Medium · 18 min read · Analytics & OLAP, PostgreSQL, Query engines, Performance engineering, SQL
  ClickHouse can query PostgreSQL data in place through the postgresql table function and the PostgreSQL table engine.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
