---
title: "PostgreSQL Stories: A simple query with a big problem"
description: "A seemingly simple indexed join with LIMIT 1 ran for over four hours on Render's staging PostgreSQL, holding a lock that blocked CREATE OR REPLACE VIEW migrations and stalled deploys."
url: "https://swe.quest/articles/postgresql-stories-a-simple-query-with-a-big-problem-6dd72fff3c9a1d47"
canonical: "https://render.com/blog/postgresql-simple-query-big-problem"
source: "Render"
published: "2024-04-03"
difficulty: "medium"
kind: "Incident"
reading_minutes: 10
tags: ["postgres","debugging","incident-response","schema-migrations"]
---

# PostgreSQL Stories: A simple query with a big problem

A post on [Render](https://swe.quest/sources/render.md), published 2024-04-03, 10 min read, Incident. Original: [https://render.com/blog/postgresql-simple-query-big-problem](https://render.com/blog/postgresql-simple-query-big-problem)

## Summary

A seemingly simple indexed join with LIMIT 1 ran for over four hours on Render's staging PostgreSQL, holding a lock that blocked CREATE OR REPLACE VIEW migrations and stalled deploys. The team traced it by watching oldest-transaction age, querying pg\_stat\_activity for long-running active queries, and using pg\_blocking\_pids() to map which backends blocked which migrations. The culprit was an engineer's ad-hoc psql query: pressing Ctrl+C in psql stopped reading results but left the server-side query running. Killing the backend fixed it immediately. Key lessons are to keep transactions short, because they block locks, autovacuum and risk XID wraparound, and to set application\_name and inspect usename so queries can be traced to their source.

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)
- [Debugging](https://swe.quest/tags/debugging.md) (Engineering practices)
- [Incidents & postmortems](https://swe.quest/tags/incident-response.md) (Reliability & operations)
- [Schema migrations](https://swe.quest/tags/schema-migrations.md) (Data & databases)

## Prerequisites

- PostgreSQL basics ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- database locks ([easy posts on Databases](https://swe.quest/tags/databases.md?difficulty=easy))
- SQL joins and indexes ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))

## Keywords

PostgreSQL, pg\_stat\_activity, pg\_blocking\_pids, psql, GORM, autovacuum

## Related posts

- [Incident Report: December 8th, 2025](https://swe.quest/articles/incident-report-december-8th-2025-0ad0c5a15c22753f.md): Railway · 2025-12-08 · Medium · 2 min read · PostgreSQL, Schema migrations, Incidents & postmortems
  A schema migration adding a nullable column to a ~1 billion row PostgreSQL table stalled behind locks held by a long-running query.
- [Incident Report: October 28th, 2025](https://swe.quest/articles/incident-report-october-28th-2025-842ec59ef90dc7e9.md): Railway · 2025-10-28 · Medium · 2 min read · PostgreSQL, Schema migrations, Incidents & postmortems, Databases
  A schema migration that added an indexed column to a ~1 billion-row Postgres table without \`CREATE INDEX CONCURRENTLY\` took an exclusive table lock, queuing nearly all backend API queries and taking down Railway's dashboard, CLI, Public API and GitHub webhook deploys for about 40 minutes.
- [The Postgres mistakes we all still make](https://swe.quest/articles/the-postgres-mistakes-we-all-still-make-63314d194ac77b85.md): Neon · 2026-11-28 · Medium · 15 min read · PostgreSQL, Performance engineering, Databases
  Five common Postgres mistakes all eventually show up as connection pressure: opening a connection per request, appending to JSONB columns, holding transactions open across external API calls, nightly bulk DELETEs, and unused indexes.
- [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.
- [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.

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)
