---
title: "PostgreSQL Stories: From slow query to fast—via stats"
description: "A simple LIMIT 1 join between a ~100k-row postgres_dbs table and a ~90M-row events table ran for hours."
url: "https://swe.quest/articles/postgresql-stories-from-slow-query-to-fast-via-stats-ac7001ab0fde8224"
canonical: "https://render.com/blog/postgresql-slow-query-to-fast-via-stats"
source: "Render"
published: "2024-05-14"
difficulty: "medium"
kind: "Case study"
reading_minutes: 13
tags: ["postgres","query-engines","performance","debugging"]
---

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

A post on [Render](https://swe.quest/sources/render.md), published 2024-05-14, 13 min read, Case study. Original: [https://render.com/blog/postgresql-slow-query-to-fast-via-stats](https://render.com/blog/postgresql-slow-query-to-fast-via-stats)

## Summary

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.

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)
- [Debugging](https://swe.quest/tags/debugging.md) (Engineering practices)

## Prerequisites

- SQL joins ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- B-tree indexes ([easy posts on Storage engines](https://swe.quest/tags/storage-engines.md?difficulty=easy))
- PostgreSQL EXPLAIN ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))

## Keywords

PostgreSQL, EXPLAIN ANALYZE, CREATE STATISTICS, pg\_stats, PEV2, merge join, hash join, Render

## Related posts

- [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.
- [pg\_stat\_ch: a PostgreSQL extension that exports every metric to ClickHouse](https://swe.quest/articles/pg-stat-ch-a-postgresql-extension-that-exports-every-metric-to-clickhouse-44a394f2889686fc.md): ClickHouse · 2026-02-13 · Hard · 16 min read · PostgreSQL, Observability, Performance engineering, Concurrency, Analytics & OLAP
  pg\_stat\_ch is an open-source (Apache 2.0) PostgreSQL 16–18 extension that captures every statement execution, including failed queries and DDL, as a fixed-size ~4.6KB event with 45 fields and streams it to ClickHouse for APM-style analysis.
- [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.
- [Postgres Support Recap: Investigating Postgres Query Performance](https://swe.quest/articles/postgres-support-recap-investigating-postgres-query-performance-82d2d3f876d71410.md): Neon · 2024-02-29 · Medium · 9 min read · PostgreSQL, Performance engineering, SQL, Observability
  Three starting points for diagnosing slow Postgres queries: use pg\_stat\_statements to find long-running queries by mean execution time and then EXPLAIN ANALYZE them, check the cache hit ratio (in Neon via the neon\_stat\_file\_cache view, since Neon extends shared buffers with a local file cache), and look for table and index bloat caused by MVCC dead tuples.
- [The side quest that freed 10% of our database](https://swe.quest/articles/the-side-quest-that-freed-10-of-our-database-649662623bb94a1e.md): incident.io · 2026-11-24 · Medium · 13 min read · PostgreSQL, Query engines, Performance engineering, SQL
  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.

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)
