Skip to content

Commit

Permalink
fix: counting rows in postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
frectonz committed Jun 24, 2024
1 parent a5a0579 commit c447998
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,25 +1122,34 @@ 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'
"#,
&[],
)
.await?
.into_iter()
.map(|r| RowCount {
name: r.get(0),
count: r.get::<usize, i64>(1) as i32,
count: 0,
})
.collect::<Vec<_>>();

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,
Expand All @@ -1152,7 +1161,7 @@ mod postgres {
indexes: indexes as i32,
triggers: triggers as i32,
views: views as i32,
counts,
counts: table_counts,
})
}

Expand All @@ -1161,7 +1170,7 @@ mod postgres {
.client
.query(
r#"
SELECT relname, n_live_tup
SELECT relname
FROM pg_stat_user_tables
WHERE schemaname = 'public'
"#,
Expand All @@ -1171,10 +1180,19 @@ mod postgres {
.into_iter()
.map(|r| RowCount {
name: r.get(0),
count: r.get::<usize, i64>(1) as i32,
count: 0,
})
.collect::<Vec<_>>();

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 })
Expand Down

0 comments on commit c447998

Please sign in to comment.