---
title: "Fix a top cause of slow queries in PostgreSQL (no slow query log needed)"
description: "PostgreSQL automatically indexes primary keys and UNIQUE columns but not the referencing column of a foreign key, so lookups on that side and ON DELETE CASCADE / SET NULL triggers fall back to full table scans."
url: "https://swe.quest/articles/fix-a-top-cause-of-slow-queries-in-postgresql-no-slow-query-log-needed-4d1a4c0df1aa9a0d"
canonical: "https://render.com/blog/postgresql-top-cause-slow-queries"
source: "Render"
published: "2024-12-12"
difficulty: "medium"
kind: "How-to"
reading_minutes: 7
tags: ["postgres","performance","sql"]
---

# Fix a top cause of slow queries in PostgreSQL (no slow query log needed)

A post on [Render](https://swe.quest/sources/render.md), published 2024-12-12, 7 min read, How-to. Original: [https://render.com/blog/postgresql-top-cause-slow-queries](https://render.com/blog/postgresql-top-cause-slow-queries)

## Summary

PostgreSQL automatically indexes primary keys and UNIQUE columns but not the referencing column of a foreign key, so lookups on that side and ON DELETE CASCADE / SET NULL triggers fall back to full table scans. In a simulated blog schema with 13 million comments, deleting a single user took 19 seconds, almost all of it in foreign-key triggers. Adding non-unique B-tree indexes on articles.author\_id, comments.author\_id and comments.article\_id cut the delete to 342 ms, a 55x speedup, with the comments-by-article trigger dropping from 18 s to 26 ms. The rule of thumb is to index foreign key columns in referencing tables, and it is mandatory when ON DELETE propagation is defined.

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)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [SQL](https://swe.quest/tags/sql.md) (Languages)

## Prerequisites

- SQL foreign keys ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- database indexes ([easy posts on Databases](https://swe.quest/tags/databases.md?difficulty=easy))
- EXPLAIN query plans

## Keywords

PostgreSQL, ON DELETE CASCADE, B-tree index, EXPLAIN ANALYZE, Render

## Related posts

- [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.
- [Postgres to ClickHouse: Data Modeling Tips](https://swe.quest/articles/postgres-to-clickhouse-data-modeling-tips-aec1f81e14af15d4.md): ClickHouse · 2024-08-28 · Medium · 9 min read · Analytics & OLAP, PostgreSQL, Event-driven architecture, SQL, Replication
  Postgres tables replicated to ClickHouse via PeerDB land in ReplacingMergeTree tables where updates and deletes arrive as versioned inserts (\_peerdb\_version, \_peerdb\_is\_deleted), so duplicates persist until background merges run.
- [The silent syntax difference in foreign keys between Postgres and MySQL](https://swe.quest/articles/the-silent-syntax-difference-in-foreign-keys-between-postgres-and-mysql-36df1bac8b9567ad.md): Neon · 2024-06-05 · Medium · 6 min read · MySQL, PostgreSQL, SQL, Schema migrations
  MySQL parses but silently ignores inline column-level \`REFERENCES\` clauses, so a foreign key written Postgres-style (\`customer\_id INT REFERENCES customers(customer\_id) ON DELETE CASCADE\`) creates no constraint, no index and no cascade behavior in MySQL.
- [LLM Rules for Postgres](https://swe.quest/articles/llm-rules-for-postgres-ad82e8ad652f6e5a.md): Neon · 2025-08-29 · Medium · 4 min read · PostgreSQL, Code review, Schema migrations, AI for coding, LLMs
  Six explicit Postgres rules, drawn from building the wispbit review bot, let an LLM code reviewer or coding agent catch common migration and query mistakes before production.
- [You can’t UPDATE what you can’t find: ClickHouse vs PostgreSQL](https://swe.quest/articles/you-can-t-update-what-you-can-t-find-clickhouse-vs-postgresql-6396a03839ca899c.md): ClickHouse · 2025-08-13 · Medium · 15 min read · Analytics & OLAP, PostgreSQL, Performance engineering, Databases, Storage engines
  ClickHouse 25.7's SQL-standard UPDATEs were benchmarked against PostgreSQL 17.5 on identical hardware using the 600M-row TPC-H lineitem table, with cold and hot runs.

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)
