From 27a33e7c85843574b526e9773ab1a0a03f4f05e2 Mon Sep 17 00:00:00 2001 From: Brains Fatman Date: Mon, 22 Jun 2026 16:09:05 +0300 Subject: [PATCH] fix(tree): don't fail database listing when role lacks CONNECT on some DBs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pg_database_size() requires CONNECT on the target database (or membership in pg_read_all_stats). The database tree enumerated the whole cluster in a single query calling pg_database_size(datname) for every row, so the first inaccessible database aborted the entire SELECT — leaving the tree empty even for databases the role can access. Guard the size computation with has_database_privilege(datname, 'CONNECT') in both the databases-group and system-databases-group queries, so inaccessible databases yield a NULL size instead of erroring the whole statement. Behaviour is unchanged for privileged roles. Fixes #104 Co-Authored-By: Claude Opus 4.8 (1M context) --- src/providers/tree/loaders/ConnectionLoader.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/providers/tree/loaders/ConnectionLoader.ts b/src/providers/tree/loaders/ConnectionLoader.ts index 3376ec8..2ee8741 100644 --- a/src/providers/tree/loaders/ConnectionLoader.ts +++ b/src/providers/tree/loaders/ConnectionLoader.ts @@ -75,8 +75,11 @@ export class ConnectionLoader extends BaseLoader { const cacheKey = SchemaCache.buildKey(element.connectionId!, dbName, undefined, 'databases'); const dbResult = await (provider as any)._cache.getOrFetch(cacheKey, async () => { return await client.query(` - SELECT datname, pg_size_pretty(pg_database_size(datname)) as size - FROM pg_database + SELECT datname, + CASE WHEN has_database_privilege(datname, 'CONNECT') + THEN pg_size_pretty(pg_database_size(datname)) + END as size + FROM pg_database ORDER BY datname `); }); @@ -128,7 +131,10 @@ export class ConnectionLoader extends BaseLoader { case 'system-databases-group': { const systemDbResult = await client.query( - `SELECT datname, pg_size_pretty(pg_database_size(datname)) as size + `SELECT datname, + CASE WHEN has_database_privilege(datname, 'CONNECT') + THEN pg_size_pretty(pg_database_size(datname)) + END as size FROM pg_database WHERE datname = ANY($1) ORDER BY datname`,