This page provides a complete guide for setting up PostgreSQL slow query logging, generating example logs, and analyzing them with IQToolkit Analyzer.
Edit your postgresql.conf (commonly at /etc/postgresql/*/main/postgresql.conf or /usr/local/var/postgres/postgresql.conf):
logging_collector = on
log_directory = 'log' # or an absolute path
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_min_duration_statement = 1000 # log queries slower than 1 second (adjust as needed)
log_statement = 'none'
log_duration = off
log_line_prefix = '%m [%p] %u@%d '
log_destination = 'stderr'
Restart PostgreSQL after making changes:
sudo systemctl restart postgresql
# or
pg_ctl restart
SET log_min_duration_statement = 1000;
Check the log_directory you set above. Common locations:
/var/log/postgresql//usr/local/var/postgres/log//opt/homebrew/var/postgresql@*/log/Run slow queries in your database to generate log entries. For example:
SELECT * FROM employees ORDER BY random() LIMIT 10000;
SELECT COUNT(*) FROM sales WHERE amount > 1000;
Copy the relevant log file to your project directory’s sample_logs/ folder (create it if it doesn’t exist). For example:
mkdir -p sample_logs
cp /var/log/postgresql/postgresql-2025-10-31_*.log ./sample_logs/
Sample log file:
sample_logs/postgresql-2025-10-31_122408.log.txt (plain text, multi-line queries supported)# With uv (recommended)
uv run python -m iqtoolkit_analyzer sample_logs/postgresql-2025-10-31_122408.log.txt
# Traditional approach
python -m iqtoolkit_analyzer sample_logs/postgresql-2025-10-31_122408.log.txt
# With uv (recommended)
uv run python -m iqtoolkit_analyzer sample_logs/postgresql-2025-10-31_122408.log.txt --verbose
# Traditional approach
python -m iqtoolkit_analyzer sample_logs/postgresql-2025-10-31_122408.log.txt --verbose
# With uv (recommended)
uv run python -m iqtoolkit_analyzer sample_logs/postgresql-2025-10-31_122408.log.txt --output reports/my_report.md
# Traditional approach
python -m iqtoolkit_analyzer sample_logs/postgresql-2025-10-31_122408.log.txt --output reports/my_report.md
Slow Query Doctor currently supports the following PostgreSQL log formats:
log_destination = 'csvlog')Ensure your logs include durations and statements for accurate analysis.
You can generate a Markdown report summarizing top slow queries, their durations, frequency, and AI-powered recommendations. See the main README for a detailed sample report.
log_min_duration_statement is set appropriately (e.g., 1000 for 1s)--verbose to get more details during analysis