Home / Software Development / PostgreSQL Query Performance Tuning Prompt
Software DevelopmentPostgreSQL Query Performance Tuning Prompt
Diagnose a slow PostgreSQL query from the schema and EXPLAIN output, and get index and rewrite suggestions with the trade-offs.
Query tuning needs evidence. A model that sees only the SQL can guess; a model that also sees the table definitions and the EXPLAIN ANALYZE output can point at the actual bottleneck.
This prompt asks for your schema, row counts and plan, and requires the model to explain each suggestion and its cost. Indexes speed up reads but slow down writes, so every recommendation should come with that trade-off.
The prompt
Act as a senior PostgreSQL database administrator.
I will give you a slow query, the relevant table definitions with approximate row counts, and the EXPLAIN (ANALYZE, BUFFERS) output.
Please:
1. Identify the main bottlenecks, quoting the plan nodes that show them.
2. Suggest a rewritten query if one would help, and explain what changes.
3. Suggest indexes with CREATE INDEX statements. For each, explain which part of the query it helps and the write-performance cost.
4. List what I should check before and after the change.
Rules:
- Do not guess about data you cannot see; ask for it or state your assumption.
- Keep the rewritten query semantically identical, or flag any difference.
Schema and row counts:
{SCHEMA}
Query:
{QUERY}
EXPLAIN (ANALYZE, BUFFERS) output:
{PLAN} What to replace
| Variable | What to put there |
|---|---|
{SCHEMA} | CREATE TABLE statements plus approximate row counts and existing indexes. |
{QUERY} | The exact slow query, with real parameter values where possible. |
{PLAN} | Output of EXPLAIN (ANALYZE, BUFFERS) from a representative environment. |
How to use it
- Run EXPLAIN (ANALYZE, BUFFERS) on the slow query in a safe environment with realistic data.
- Paste schema, query and plan into the prompt.
- Read the bottleneck explanation first. If it does not match what you see in the plan, correct the model.
- Test each suggested index on a staging copy and compare plans before and after.
- Deploy index changes carefully; for large tables consider building them without blocking writes.
Worked example
This example was written for this page to show the format. Results from your own model and your own material will differ, so run the prompt yourself before relying on it.
Input
SELECT c.name, SUM(o.total) FROM customers c JOIN orders o ON o.customer_id = c.id WHERE o.created_at >= '2026-01-01' AND LOWER(c.email) LIKE '%@example.com' GROUP BY c.name;
Output
Bottlenecks (illustrative, without a real plan): wrapping the column in LOWER() with a leading wildcard prevents ordinary index use on customers.email, and GROUP BY c.name merges different customers who share a name. Rewrite: SELECT c.id, c.name, SUM(o.total) AS total_spent FROM customers c JOIN orders o ON o.customer_id = c.id WHERE o.created_at >= DATE '2026-01-01' AND c.email ILIKE '%@example.com' GROUP BY c.id, c.name; Indexes to test: - CREATE INDEX CONCURRENTLY idx_orders_customer_created ON orders (customer_id, created_at) INCLUDE (total); helps the join and the date filter; adds write cost on orders. - A trigram index (pg_trgm) on customers.email if suffix searches are common; large on disk and slower to update. Before and after: compare EXPLAIN (ANALYZE, BUFFERS) output and run the query several times, since caching affects timings.
Variations
| Situation | What to change |
|---|---|
| Query without a plan yet | Ask the model to tell you which EXPLAIN options to run and what to look for first. |
| Reporting query on a replica | Add "Read-only workload; materialized views are acceptable" to allow precomputation. |
| Write-heavy table | Add "Minimize new indexes; this table has heavy inserts" and ask for the cheapest effective option. |
| Other database | Replace PostgreSQL with your engine and ask for its equivalent of EXPLAIN ANALYZE. |
Common mistakes
- Sharing only the query. Without the plan and row counts the advice is a guess. Include both.
- Adding every suggested index. Each index costs storage and write speed. Add one at a time and measure.
- Testing on tiny data. Plans change with table size. Use data volumes close to production.
- Ignoring semantic changes. A faster rewrite that returns different rows is a bug. Compare results before and after.
Frequently asked questions
Is it safe to run EXPLAIN ANALYZE in production?
It executes the query, so avoid it on statements that modify data or are very expensive. Use a replica or staging copy when possible.
Can the model see my database?
No. It only knows what you paste, so remove sensitive values and use sample data where needed.
Why mention write cost?
Indexes are a trade-off. Naming the cost helps you decide whether a read improvement is worth it.