pg_read_all_stats#
What is pg_read_all_stats?#
pg_read_all_stats is a built-in role that allows a user to read all PostgreSQL runtime statistics.
- In simple words:
π βI can see what the database is doing right now and what it has been doing.β
What pg_read_all_stats CAN do#
A role with this privilege can read all statistics views, even for objects it does not own.
Access to key system views
- Including (but not limited to):
Session & activity
pg_stat_activity
Active queries
Idle connections
Long-running queries
Blocked sessions
Table statistics
pg_stat_user_tables
pg_stat_all_tables
Sequential scans
Index scans
Dead tuples
Vacuum / analyze counts
Index statistics
pg_stat_user_indexes
pg_stat_all_indexes
Index usage
Index hit ratios
I/O and background writer
pg_stat_bgwriter
Checkpoints
Buffer writes
WAL activity
WAL & replication
pg_stat_replication
pg_stat_wal
Replication lag
Streaming status
Functions & statements (if enabled)
pg_stat_user_functions
pg_stat_statements
- What it CANNOT do
β Read table data
β Modify anything
β Kill queries
β Change config
β Access files
This role is read-only telemetry.
Why this role exists
- Without it:
You only see stats for objects you own
- With it:
You can monitor the entire cluster
- Essential for:
DBAs
SREs
Monitoring tools
Performance analysis
- Mental model
pg_read_all_stats = βDatabase CCTV cameraβ
- You see:
Who is doing what
When
How often
How expensive
But you cannot interfere.
Example
Without role
SELECT * FROM pg_stat_activity;
-- limited rows
Grant role
GRANT pg_read_all_stats TO monitor_user;
After grant
SELECT pid, usename, query, state
FROM pg_stat_activity;
β Full visibility
Relationship with other roles
Role |
Purpose |
|---|---|
pg_read_all_settings |
βοΈ Read config |
pg_read_all_stats |
π Read stats |
pg_monitor |
π Both + more |
pg_signal_backend |
πͺ Control sessions |
- Important:
pg_monitor includes pg_read_all_stats.
Security considerations
Risk |
Level |
|---|---|
Data exposure |
π’ None |
Query visibility |
π‘ Medium |
Business logic exposure |
π‘ Medium |
Server control |
π’ None |
- It can expose:
Query text
Table names
Access patterns
- Best practices
β Grant to monitoring users
β Grant to observability tools
β Use with pg_read_all_settings
β Avoid granting to app users
- Typical real-world users
Datadog / Prometheus agents
Grafana dashboards
DBA read-only accounts
Incident response teams
Summary
Feature |
Allowed |
|---|---|
Read stats |
β |
View all sessions |
β |
Read data |
β |
Modify server |
β |
Kill queries |
β |