Fix: Use relname instead of tablename in pg_stat_user_tables query + add error handling

This commit is contained in:
cawcenter
2025-12-16 11:33:35 -05:00
parent 89f895b3a8
commit b1acc50c7f

View File

@@ -162,20 +162,28 @@ export async function getDatabaseStats(): Promise<{
"SELECT pg_size_pretty(pg_database_size(current_database())) as size" "SELECT pg_size_pretty(pg_database_size(current_database())) as size"
); );
// Get table stats // Get table stats (handle case where no tables exist yet)
const { rows: tableRows } = await pool.query<{ let tableRows: Array<{ table: string; row_count: string; table_size: string }> = [];
try {
const result = await pool.query<{
table: string; table: string;
row_count: string; row_count: string;
table_size: string; table_size: string;
}>( }>(
`SELECT `SELECT
schemaname || '.' || tablename as table, schemaname || '.' || relname as table,
n_live_tup as row_count, n_live_tup as row_count,
pg_size_pretty(pg_total_relation_size(schemaname || '.' || tablename)) as table_size pg_size_pretty(pg_total_relation_size(schemaname || '.' || relname)) as table_size
FROM pg_stat_user_tables FROM pg_stat_user_tables
ORDER BY n_live_tup DESC ORDER BY n_live_tup DESC
LIMIT 20` 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 { return {
databaseSize: sizeRows[0]?.size || 'Unknown', databaseSize: sizeRows[0]?.size || 'Unknown',