From c447998ec834a6f7effd2d586579d1ab49f1a41b Mon Sep 17 00:00:00 2001 From: frectonz Date: Tue, 25 Jun 2024 00:48:28 +0300 Subject: [PATCH] fix: counting rows in postgres --- src/main.rs | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1df3e41..90445fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1122,13 +1122,13 @@ mod postgres { .await? .get(0); - let mut counts = self + let mut table_counts = self .client .query( r#" - SELECT relname, n_live_tup + SELECT relname FROM pg_stat_user_tables - WHERE schemaname = 'public'; + WHERE schemaname = 'public' "#, &[], ) @@ -1136,11 +1136,20 @@ mod postgres { .into_iter() .map(|r| RowCount { name: r.get(0), - count: r.get::(1) as i32, + count: 0, }) .collect::>(); - counts.sort_by(|a, b| b.count.cmp(&a.count)); + for table in table_counts.iter_mut() { + let count: i64 = self + .client + .query_one(&format!(r#"SELECT count(*) FROM "{}""#, table.name), &[]) + .await? + .get(0); + table.count = count as i32; + } + + table_counts.sort_by(|a, b| b.count.cmp(&a.count)); Ok(responses::Overview { file_name, @@ -1152,7 +1161,7 @@ mod postgres { indexes: indexes as i32, triggers: triggers as i32, views: views as i32, - counts, + counts: table_counts, }) } @@ -1161,7 +1170,7 @@ mod postgres { .client .query( r#" - SELECT relname, n_live_tup + SELECT relname FROM pg_stat_user_tables WHERE schemaname = 'public' "#, @@ -1171,10 +1180,19 @@ mod postgres { .into_iter() .map(|r| RowCount { name: r.get(0), - count: r.get::(1) as i32, + count: 0, }) .collect::>(); + for table in tables.iter_mut() { + let count: i64 = self + .client + .query_one(&format!(r#"SELECT count(*) FROM "{}""#, table.name), &[]) + .await? + .get(0); + table.count = count as i32; + } + tables.sort_by_key(|r| r.count); Ok(responses::Tables { tables })