From b1acc50c7fad85aae6e266b21f62376648c6a4c9 Mon Sep 17 00:00:00 2001 From: cawcenter Date: Tue, 16 Dec 2025 11:33:35 -0500 Subject: [PATCH] Fix: Use relname instead of tablename in pg_stat_user_tables query + add error handling --- src/lib/shim/pool.ts | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/lib/shim/pool.ts b/src/lib/shim/pool.ts index 0e26508..45f6070 100644 --- a/src/lib/shim/pool.ts +++ b/src/lib/shim/pool.ts @@ -162,20 +162,28 @@ export async function getDatabaseStats(): Promise<{ "SELECT pg_size_pretty(pg_database_size(current_database())) as size" ); - // Get table stats - const { rows: tableRows } = await pool.query<{ - table: string; - row_count: string; - table_size: string; - }>( - `SELECT - schemaname || '.' || tablename as table, - n_live_tup as row_count, - pg_size_pretty(pg_total_relation_size(schemaname || '.' || tablename)) as table_size - FROM pg_stat_user_tables - ORDER BY n_live_tup DESC - LIMIT 20` - ); + // Get table stats (handle case where no tables exist yet) + let tableRows: Array<{ table: string; row_count: string; table_size: string }> = []; + + try { + const result = await pool.query<{ + table: string; + row_count: string; + table_size: string; + }>( + `SELECT + schemaname || '.' || relname as table, + n_live_tup as row_count, + pg_size_pretty(pg_total_relation_size(schemaname || '.' || relname)) as table_size + FROM pg_stat_user_tables + ORDER BY n_live_tup DESC + LIMIT 20` + ); + tableRows = result.rows; + } catch (error) { + console.warn('[DB Stats] Could not fetch table stats:', error); + // Return empty array if tables don't exist yet + } return { databaseSize: sizeRows[0]?.size || 'Unknown',