Fix: Use relname instead of tablename in pg_stat_user_tables query + add error handling
This commit is contained in:
@@ -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<{
|
||||
// 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 || '.' || tablename as table,
|
||||
schemaname || '.' || relname as table,
|
||||
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
|
||||
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',
|
||||
|
||||
Reference in New Issue
Block a user