How to Diagnose Slow Queries in PostgreSQL

Galaxy Glossary

How do I find and fix slow queries in PostgreSQL?

Identify, analyze, and optimize slow PostgreSQL queries with logging, statistics, and EXPLAIN.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

What makes a PostgreSQL query slow?<\/h2>\nLarge table scans, missing indexes, bloated tables, or bad JOIN orders force PostgreSQL to read more pages than needed, increasing latency.<\/p>\n\n

How can I find my slowest queries quickly?<\/h2>\nEnable the pg_stat_statements<\/code> extension, then query it to rank statements by total or mean execution time.This avoids digging through log files.<\/p>\n\n

Query example<\/h3>\n

SELECT query, total_exec_time, mean_exec_time\nFROM pg_stat_statements\nORDER BY mean_exec_time DESC\nLIMIT 10;<\/code><\/p>\n\n

When should I use EXPLAIN ANALYZE?<\/h2>\n

Use EXPLAIN ANALYZE<\/code> after spotting a slow statement to see the actual execution plan, row counts, and timing for each step.<\/p>\n\n

EXPLAIN output tip<\/h3>\n

Focus on nodes with high actual time\/rows<\/em> or on Sequential Scans where Index Scans are expected.<\/p>\n\n

How do I log only slow statements?<\/h2>\n

Set log_min_duration_statement = 500<\/code> (ms) and reload.PostgreSQL will now log queries taking longer than 0.5 s without flooding logs.<\/p>\n\n

Which parameters speed up diagnosis?<\/h2>\n

Enable auto_explain.log_min_duration = 500<\/code>, auto_explain.log_analyze = on<\/code>, and auto_explain.log_buffers = on<\/code> for automatic plan capture.<\/p>\n\n

What quick fixes usually help?<\/h2>\nCreate missing indexes, rewrite correlated sub-queries as JOINs, add WHERE filters earlier, and run VACUUM ANALYZE<\/code> to refresh statistics.<\/p>.

Why How to Diagnose Slow Queries in PostgreSQL is important

How to Diagnose Slow Queries in PostgreSQL Example Usage


EXPLAIN ANALYZE\nSELECT c.name, SUM(oi.quantity) AS items_bought\nFROM Customers c\nJOIN Orders o ON o.customer_id = c.id\nJOIN OrderItems oi ON oi.order_id = o.id\nGROUP BY c.name\nORDER BY items_bought DESC\nLIMIT 5;

How to Diagnose Slow Queries in PostgreSQL Syntax


-- Enable statistics module\nCREATE EXTENSION IF NOT EXISTS pg_stat_statements;\n\n-- Inspect top slow queries\nSELECT query, calls, total_exec_time, mean_exec_time\nFROM pg_stat_statements\nORDER BY mean_exec_time DESC\nLIMIT 20;\n\n-- Get a plan with timing\nEXPLAIN (ANALYZE, BUFFERS) \nSELECT o.id, SUM(oi.quantity * p.price) AS order_total\nFROM Orders o\nJOIN OrderItems oi ON oi.order_id = o.id\nJOIN Products p ON p.id = oi.product_id\nWHERE o.order_date >= CURRENT_DATE - INTERVAL '7 days'\nGROUP BY o.id;\n\n-- Log statements slower than 500 ms\nALTER SYSTEM SET log_min_duration_statement = 500;\nSELECT pg_reload_conf();

Common Mistakes

Frequently Asked Questions (FAQs)

Does pg_stat_statements slow my database?

The extension adds negligible overhead (≈1–2%) and is safe for production when track_activity_query_size is large enough.

Can I clear statistics without restarting?

Yes. Run SELECT pg_stat_statements_reset(); to zero out counters and start fresh.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.