Skip to content

Commit 3c16d59

Browse files
feat(posts): add "PostgreSQL Debug Tips"
Post: 2026-03-23-postgresql-debug-tips.md
1 parent d945a1d commit 3c16d59

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
layout: post
3+
title: PostgreSQL Debug Tips
4+
date: 2026-03-23 17:15:04
5+
excerpt: Tips on how to debug PostgreSQL database.
6+
categories: postgresql debug database
7+
---
8+
9+
Tips on how to debug Postgres:
10+
11+
- [Explain Analyze](#explain-analyze)
12+
- [Slow Statements](#slow-statements)
13+
- [Session Activity](#session-activity)
14+
15+
## Explain Analyze
16+
17+
Check if the planner chose a bad plan for your query:
18+
19+
```sql
20+
EXPLAIN (ANALYZE, BUFFERS)
21+
SELECT ...
22+
```
23+
24+
`EXPLAIN ANALYZE` runs the statement and shows actual timings and `BUFFERS` helps identify heavy reads/hits.
25+
26+
## Slow Statements
27+
28+
Find slow statements:
29+
30+
```sql
31+
SELECT
32+
query,
33+
calls,
34+
total_exec_time,
35+
mean_exec_time,
36+
rows
37+
FROM pg_stat_statements
38+
ORDER BY total_exec_time DESC
39+
LIMIT 20;
40+
```
41+
42+
Depending on the queries, you can improve the query-plan, index, and/or infrastructure.
43+
44+
## Session Activity
45+
46+
Check what sessions are waiting on:
47+
48+
```sql
49+
SELECT
50+
pid,
51+
usename,
52+
state,
53+
wait_event_type,
54+
wait_event,
55+
query
56+
FROM pg_stat_activity
57+
WHERE state <> 'idle';
58+
```
59+
60+
- `Lock` (for `wait_event_type`) means there's a blocking/locking problem.
61+
- `IO` or buffer-related waits means there's a storage/cache issue.
62+
- Little waiting time but high runtime means there could be an expensive execution plan or CPU-heavy work.

0 commit comments

Comments
 (0)