Sub topic 13
Permissions and Query Text Visibility
Assume you know basic SELECT statements, can use psql, and understand that PostgreSQL roles control access. This chapter focuses on who can read pg_stat_statements, not on enabling the extension.
pg_stat_statements contains performance data that can reveal database structure and application behavior. A query may expose table names, column names, joins, and comments. Even when literal values are normalized, the query shape can be sensitive. PostgreSQL therefore limits what ordinary users can see.
What users can see
The view records the role that executed a statement in userid. It also records the database in dbid. PostgreSQL uses the connected role's privileges to decide which rows and query text the role may inspect.
By default:
- A user can see statistics for that user's own statements.
- A superuser can see statistics and query text for all users.
- A member of the predefined
pg_read_all_statsrole can see statistics and query text for all users. - Granting this role does not grant permission to read or modify application tables.
The exact result also depends on the database and the role used to connect. Run this query to identify the current session:
SELECT current_user,
session_user,
current_database();The following query is useful when checking whether visible rows belong to the current role:
SELECT userid::regrole AS executing_role,
dbid,
calls,
round(total_exec_time::numeric, 2) AS total_ms,
left(query, 100) AS query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 20;An ordinary analyst might receive only that analyst's statements. A privileged observer can compare activity across application roles.
Granting read access
A database administrator can grant the predefined monitoring role to a trusted observer:
GRANT pg_read_all_stats TO performance_analyst;The command must be run by a role allowed to grant membership, usually a superuser or an appropriately configured administrator. The analyst may then need to reconnect before the new membership is used.
This grant is broader than access to one view. It can allow the role to inspect statistics from other monitoring views, including activity from other sessions. Grant it only to people and service accounts that need that visibility.
A narrower operational choice is to have an administrator run approved reports and expose only aggregated results. For example, a report can return query IDs, call counts, and timing while omitting query text. This reduces the chance of revealing internal names or sensitive SQL structure.
What permissions do not solve
Permissions do not provide per-execution history. A user with broad read access sees cumulative counters grouped by query identity, not every parameter value or every execution event.
Permissions also do not make a query safe to run with EXPLAIN (ANALYZE). EXPLAIN ANALYZE executes the statement, so use it only with appropriate authorization and care, especially for writes.
Exercise
A performance analyst needs to compare queries run by several application roles. Which PostgreSQL privilege is the usual predefined choice, and what risk should the administrator consider?
Show answer
Grant pgreadall_stats to the trusted analyst role. The grant exposes statistics and query text from other users, so it can reveal database structure and application behavior.