Repository navigation
fix(tool/postgres-list-database-stats): return NULL size for databases without CONNECT privilege - #4456
Conversation
…s without CONNECT privilege pg_database_size raises "permission denied for database" for a database the role cannot CONNECT to unless it has the privileges of pg_read_all_stats, so a single database with CONNECT revoked from PUBLIC made every call fail, even when filtered to another database. Compute the size only when that check passes and return NULL otherwise, as postgres-list-tablespaces does for tablespace size, and sort NULL sizes last for order_by = 'size'. Fixes googleapis#4455
There was a problem hiding this comment.
Code Review
This pull request updates the PostgreSQL database stats tool to safely retrieve database sizes by checking for CONNECT privileges or the pg_read_all_stats role, returning NULL if unauthorized, and ensures NULL values are sorted last when ordering by size. The reviewer noted that directly checking the pg_read_all_stats role by name can cause runtime errors on PostgreSQL versions older than 10 or compatible databases where the role does not exist, and suggested a safer query that checks pg_roles first.
| CASE | ||
| WHEN has_database_privilege(s.datid, 'CONNECT') OR pg_has_role('pg_read_all_stats', 'USAGE') | ||
| THEN pg_database_size(s.datid) | ||
| END AS database_size_bytes |
There was a problem hiding this comment.
Directly calling pg_has_role('pg_read_all_stats', 'USAGE') with a hardcoded role name string will cause the entire query to fail with ERROR: role "pg_read_all_stats" does not exist on PostgreSQL versions older than 10, or on PostgreSQL-compatible databases (such as CockroachDB, Amazon Redshift, or older custom setups) that do not define the pg_read_all_stats role.
By checking the existence of the role in pg_roles first and passing its oid to pg_has_role, we ensure the query is completely safe and backward-compatible, preventing runtime errors on those environments.
| CASE | |
| WHEN has_database_privilege(s.datid, 'CONNECT') OR pg_has_role('pg_read_all_stats', 'USAGE') | |
| THEN pg_database_size(s.datid) | |
| END AS database_size_bytes | |
| CASE | |
| WHEN has_database_privilege(s.datid, 'CONNECT') | |
| OR EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'pg_read_all_stats' AND pg_has_role(oid, 'USAGE')) | |
| THEN pg_database_size(s.datid) | |
| END AS database_size_bytes |
Description
postgres-list-database-statscallspg_database_size(s.datid)for every row ofpg_stat_database.pg_database_sizeraisespermission denied for database <name>when the role has neither CONNECT on that database nor the privileges ofpg_read_all_stats, so a single database with CONNECT revoked from PUBLIC makes every call fail for a non-superuser, including calls filtered to a different database withdatabase_name.This computes the size only when that same check passes and returns NULL otherwise, the way
postgres-list-tablespacesalready guardspg_tablespace_sizewithhas_tablespace_privilege. It also addsNULLS LASTto theorder_by = 'size'sort key so databases without a visible size don't sort first.Verified with psql on PostgreSQL 16.15, running the tool's statement verbatim (PREPARE/EXECUTE) as a non-superuser role, with one extra database that has CONNECT revoked from PUBLIC:
ERROR: permission denied for database secret_db, with and without adatabase_namefilter;database_size_bytesis NULL only for that database, thedatabase_namefilter returns just the requested database, andorder_by = 'size'lists it last; withpg_read_all_statsgranted, or as a superuser, sizes are returned for all databases.Tests: I did not add an integration case. The CI roles appear to be superusers or members of each test database's owner role (
setUpDatabasegrants the owner role tocurrent_user), so a CONNECT-revoked database would not reproduce the error there. The existingRunPostgresListDatabaseStatsTestcases still run the changed statement. I couldn't run the Go tests locally (no Go 1.27 toolchain in my environment). Happy to add a test if you have a preferred way to run the tool as a restricted role.Security: no change to parameter binding or to what the tool can read; it only avoids calling
pg_database_sizewhere PostgreSQL would refuse it.PR Checklist
!if this involves a breaking change (not breaking)🛠️ Fixes #4455