<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/when-the-postgres-query-planner-goes-rogue-planetscale-idrswdxnc" -->

---
title: When the Postgres query planner goes rogue — PlanetScale
description: A PlanetScale customer experienced a sudden CPU spike to 100% with no code or infra changes, traced to the Postgres query planner abruptly abandoning an index...
canonical: https://daily.dev/posts/when-the-postgres-query-planner-goes-rogue-planetscale-idrswdxnc
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: When the Postgres query planner goes rogue — PlanetScale | daily.dev
og:description: A PlanetScale customer experienced a sudden CPU spike to 100% with no code or infra changes, traced to the Postgres query planner abruptly abandoning an index...
og:url: https://daily.dev/posts/when-the-postgres-query-planner-goes-rogue-planetscale-idrswdxnc
og:image: https://api.daily.dev/og/posts/IDrSWdxnC.png
og:image:alt: When the Postgres query planner goes rogue — PlanetScale
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# When the Postgres query planner goes rogue — PlanetScale

**[PlanetScale](https://daily.dev/sources/planetscale)** · 4 min read · 26 upvotes · 1 comments

## Summary

A PlanetScale customer experienced a sudden CPU spike to 100% with no code or infra changes, traced to the Postgres query planner abruptly abandoning an index in favor of a full table scan, driving query latency from milliseconds to seconds. PlanetScale's Database Traffic Control was used to apply a resource budget that killed the offending query pattern in real time, restoring database health while the underlying stale statistics or query issue could be investigated. The piece explains how Postgres cost-based planning can shift as data grows even without schema or code changes, and outlines follow-up steps like EXPLAIN (ANALYZE, BUFFERS), running ANALYZE, or adjusting indexes.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://planetscale.com/blog/when-the-postgres-query-planner-goes-rogue>

## Questions this post answers

### Why would a Postgres query suddenly stop using an index and cause a CPU spike with no code or schema changes?

The Postgres query planner recalculates the optimal plan based on evolving table statistics, not just schema or code. As data grows, the planner can suddenly decide a sequential scan is cheaper than an index scan, causing a previously fast query to jump from millisecond latency to multi-second timeouts and spike CPU to 100%, even though nothing was deployed or changed.

_Anyone chasing a mystery Postgres CPU spike can find similar incident breakdowns curated on daily.dev._

### How can I stop a single bad query from taking down my entire Postgres database in production without redeploying?

Apply a resource budget targeting that exact query pattern and set it to enforcement mode, which immediately kills further executions of the offending query while the rest of traffic continues normally. This was done using PlanetScale's Database Traffic Control feature, which let a team recover a database within about 15 minutes without any code or infrastructure changes.

_Engineers building runbooks for database incidents can track tools like this via daily.dev._

### What steps should I take to permanently fix a query after a Postgres query planner regression causes a full table scan?

Compare the old and new execution plans using EXPLAIN (ANALYZE, BUFFERS) in a safe environment, then apply a longer-term fix depending on the cause: run ANALYZE to refresh stale table statistics, add or adjust indexes, or rewrite the query so Postgres can use an index effectively again.

_Developers debugging query regressions can follow practical Postgres troubleshooting writeups on daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@agustinbarrientos** · 0 upvotes

> I bet the bad plan showed up before CPU hit 100 percent. Once the database was pinned, even figuring out why got harder. Ten seconds per call is a brutal way to find out.

## Similar posts on daily.dev

- [Tuning Postgres for Analytics](https://daily.dev/posts/tuning-postgres-for-analytics-n8y7hadc4) · Planet PostgreSQL · 1 upvotes · 0 comments
- [Keeping a Postgres queue healthy — PlanetScale](https://daily.dev/posts/keeping-a-postgres-queue-healthy-planetscale-h5pvjk1vk) · Hacker News · 0 upvotes · 0 comments
- [Debug PostgreSQL query latency faster with EXPLAIN ANALYZE in Datadog Database Monitoring](https://daily.dev/posts/debug-postgresql-query-latency-faster-with-explain-analyze-in-datadog-database-monitoring-kwcnveyee) · Datadog · 40 upvotes · 0 comments
- [PostgreSQL Query Optimization: The Index That Made Our Fastest Query 40 Times Slower](https://daily.dev/posts/postgresql-query-optimization-the-index-that-made-our-fastest-query-40-times-slower-w73b0y9pb) · Medium · 1 upvotes · 0 comments

---

Tags: [#database](https://daily.dev/tags/database), [#postgresql](https://daily.dev/tags/postgresql), [#planetscale](https://daily.dev/tags/planetscale)

[View this post on daily.dev](https://daily.dev/posts/when-the-postgres-query-planner-goes-rogue-planetscale-idrswdxnc)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"When the Postgres query planner goes rogue — PlanetScale","url":"https://daily.dev/posts/when-the-postgres-query-planner-goes-rogue-planetscale-idrswdxnc","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/when-the-postgres-query-planner-goes-rogue-planetscale-idrswdxnc"},"datePublished":"2026-09-10T14:53:17.750Z","dateModified":"2026-10-15T02:22:15.981Z","description":"A PlanetScale customer experienced a sudden CPU spike to 100% with no code or infra changes, traced to the Postgres query planner abruptly abandoning an index...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/bfe400dfde5126c44de53fb646eb525f?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/bfe400dfde5126c44de53fb646eb525f?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"PlanetScale","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"PlanetScale","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/09515c29cafc45618b3f117eab4fccf8","url":"https://daily.dev/squads/planetscale"},"commentCount":1,"discussionUrl":"https://daily.dev/posts/when-the-postgres-query-planner-goes-rogue-planetscale-idrswdxnc","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":26},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"database,postgresql,planetscale","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"PlanetScale","item":"https://daily.dev/squads/planetscale"},{"@type":"ListItem","position":3,"name":"When the Postgres query planner goes rogue — PlanetScale"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/when-the-postgres-query-planner-goes-rogue-planetscale-idrswdxnc","comment":[{"@type":"Comment","text":"I bet the bad plan showed up before CPU hit 100 percent. Once the database was pinned, even figuring out why got harder. Ten seconds per call is a brutal way to find out.","datePublished":"2026-09-13T05:07:51.016Z","url":"https://daily.dev/posts/IDrSWdxnC#c-XOy0Av9VD","author":{"@type":"Person","name":"Agustin Barrientos","url":"https://daily.dev/agustinbarrientos","image":"https://media.daily.dev/image/upload/s--5ayxQnqn--/f_auto/v1788281802/avatars/avatar_wQYYVe5Tbj0NJ7C7qPoa8?_a=BAMAMicg0"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/when-the-postgres-query-planner-goes-rogue-planetscale-idrswdxnc#faq","mainEntity":[{"@type":"Question","name":"Why would a Postgres query suddenly stop using an index and cause a CPU spike with no code or schema changes?","acceptedAnswer":{"@type":"Answer","text":"The Postgres query planner recalculates the optimal plan based on evolving table statistics, not just schema or code. As data grows, the planner can suddenly decide a sequential scan is cheaper than an index scan, causing a previously fast query to jump from millisecond latency to multi-second timeouts and spike CPU to 100%, even though nothing was deployed or changed. Anyone chasing a mystery Postgres CPU spike can find similar incident breakdowns curated on daily.dev."}},{"@type":"Question","name":"How can I stop a single bad query from taking down my entire Postgres database in production without redeploying?","acceptedAnswer":{"@type":"Answer","text":"Apply a resource budget targeting that exact query pattern and set it to enforcement mode, which immediately kills further executions of the offending query while the rest of traffic continues normally. This was done using PlanetScale's Database Traffic Control feature, which let a team recover a database within about 15 minutes without any code or infrastructure changes. Engineers building runbooks for database incidents can track tools like this via daily.dev."}},{"@type":"Question","name":"What steps should I take to permanently fix a query after a Postgres query planner regression causes a full table scan?","acceptedAnswer":{"@type":"Answer","text":"Compare the old and new execution plans using EXPLAIN (ANALYZE, BUFFERS) in a safe environment, then apply a longer-term fix depending on the cause: run ANALYZE to refresh stale table statistics, add or adjust indexes, or rewrite the query so Postgres can use an index effectively again. Developers debugging query regressions can follow practical Postgres troubleshooting writeups on daily.dev."}}]}
```

