Most of the articles in this series are about making reasonable queries faster. This one is about queries that are wrong by construction — patterns that account for most production SQL performance incidents, not because the query is subtle, but because the pattern is near-universal and the fix is well-known to people who've seen it before. If you recognise these in your codebase, fixing them is almost always a straightforward win.
Each anti-pattern below maps to a specific plan signature or log symptom; cross-references to deeper treatment are inline.
1. N+1 queries — the ORM default
The single most common performance problem in applications talking to PostgreSQL. The pattern is one query to fetch a list of parent rows, plus one query per parent to fetch related data:
# One query — fetches 500 orders. orders = db . query ( " SELECT * FROM orders WHERE status = ' pending '" )
# Five hundred more queries — one per order. for order in orders : items = db .…