Postgres Performance

Guide for investigating slow PostgreSQL queries

robusta-dev fc71ef0 1.0 KB Updated

File contents

PostgreSQL Performance Investigation

How to investigate slow PostgreSQL queries

1. Connect to PostgreSQL database

kubectl run temp-psql --image=postgres:15-alpine --rm -it --restart=Never --namespace=namespace-104b --env="PGPASSWORD=postgres123" -- psql -h postgres -U postgres -d userdb

2. Check for slow queries using pg_stat_statements

SELECT query, mean_exec_time, calls
FROM pg_stat_statements
WHERE query NOT LIKE '%pg_stat_statements%'
ORDER BY mean_exec_time DESC
LIMIT 10;

3. For any slow query, run EXPLAIN ANALYZE

EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'user1@example.com';

4. Check table structure for missing indexes

\d users

5. Look for Sequential Scans in EXPLAIN output

If you see "Seq Scan" on a large table, it indicates a missing index.

6. Create index if needed

CREATE INDEX idx_users_email ON users(email);

robusta-dev/holmesgpt/tree/main/tests/llm/fixtures/test_ask_holmes/104b_postgres_missing_index_pgstat/postgres-performance commit fc71ef0f10

Frequently asked questions

npx skillmds@latest add robusta-dev/postgres-performance