fix(objects): use live PG row estimates and refresh stale stats
This commit is contained in:
3 files changed
+219
-13
No files matched your search
@@ -7343,22 +7343,47 @@ pub async fn get_custom_type_details(pool: &Pool, schema: &str, name: &str) -> R
|
||||
})
|
||||
}
|
||||
|
||||
/// Row/size estimates for the object browser, per schema.
|
||||
///
|
||||
/// `pg_class.reltuples` only moves when ANALYZE (or autovacuum) rewrites it, so a
|
||||
/// table that was written to — or never analyzed at all — keeps reporting its
|
||||
/// last known count, often `0` (#10461). `pg_stat_user_tables.n_live_tup` is the
|
||||
/// statistics collector's live estimate: it tracks DML within seconds and still
|
||||
/// avoids a `COUNT(*)` scan, which is what the UI promises in its column hint.
|
||||
const POSTGRES_OBJECT_STATISTICS_SQL: &str = "SELECT c.relname, \
|
||||
GREATEST(COALESCE(s.n_live_tup, c.reltuples), 0)::bigint AS estimated_rows, \
|
||||
pg_catalog.pg_total_relation_size(c.oid)::bigint AS total_bytes \
|
||||
FROM pg_catalog.pg_class c \
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
|
||||
LEFT JOIN pg_catalog.pg_stat_user_tables s ON s.relid = c.oid \
|
||||
WHERE n.nspname = $1 AND c.relkind IN ('r','m','f','p') \
|
||||
ORDER BY c.relname";
|
||||
|
||||
/// Some Postgres-compatible engines do not expose `pg_stat_user_tables`; fall
|
||||
/// back to the ANALYZE-time estimate rather than dropping the columns entirely.
|
||||
const POSTGRES_OBJECT_STATISTICS_FALLBACK_SQL: &str = "SELECT c.relname, \
|
||||
GREATEST(c.reltuples, 0)::bigint AS estimated_rows, \
|
||||
pg_catalog.pg_total_relation_size(c.oid)::bigint AS total_bytes \
|
||||
FROM pg_catalog.pg_class c \
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
|
||||
WHERE n.nspname = $1 AND c.relkind IN ('r','m','f','p') \
|
||||
ORDER BY c.relname";
|
||||
|
||||
pub async fn list_object_statistics(pool: &Pool, schema: &str) -> Result<Vec<ObjectStatistics>, String> {
|
||||
let schema = if schema.is_empty() { "public" } else { schema };
|
||||
let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;
|
||||
let rows = postgres_query_cached(
|
||||
&client,
|
||||
"SELECT c.relname, \
|
||||
GREATEST(c.reltuples, 0)::bigint AS estimated_rows, \
|
||||
pg_catalog.pg_total_relation_size(c.oid)::bigint AS total_bytes \
|
||||
FROM pg_catalog.pg_class c \
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
|
||||
WHERE n.nspname = $1 AND c.relkind IN ('r','m','f','p') \
|
||||
ORDER BY c.relname",
|
||||
&[&schema],
|
||||
)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rows = match postgres_query_cached(&client, POSTGRES_OBJECT_STATISTICS_SQL, &[&schema]).await {
|
||||
Ok(rows) => rows,
|
||||
Err(error) => {
|
||||
log::warn!(
|
||||
"[postgres][object-statistics] live tuple estimate unavailable, falling back to reltuples: {}",
|
||||
pg_error_to_string(error)
|
||||
);
|
||||
postgres_query_cached(&client, POSTGRES_OBJECT_STATISTICS_FALLBACK_SQL, &[&schema])
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
};
|
||||
Ok(rows
|
||||
.iter()
|
||||
.map(|row| ObjectStatistics {
|
||||
@@ -15594,4 +15619,22 @@ mod tests {
|
||||
// `IN` must not match the start of `INTO`.
|
||||
assert_eq!(parse_pg_partition_bound("FOR VALUES INTO (1)"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn object_statistics_prefers_live_tuples_and_keeps_a_reltuples_fallback() {
|
||||
// #10461: `reltuples` lags behind DML, so the live estimate must win when
|
||||
// `pg_stat_user_tables` is available...
|
||||
assert!(POSTGRES_OBJECT_STATISTICS_SQL.contains("pg_catalog.pg_stat_user_tables"));
|
||||
assert!(POSTGRES_OBJECT_STATISTICS_SQL.contains("COALESCE(s.n_live_tup, c.reltuples)"));
|
||||
// ...while an engine without that view still reports counts.
|
||||
assert!(!POSTGRES_OBJECT_STATISTICS_FALLBACK_SQL.contains("pg_stat_user_tables"));
|
||||
assert!(POSTGRES_OBJECT_STATISTICS_FALLBACK_SQL.contains("GREATEST(c.reltuples, 0)"));
|
||||
for sql in [POSTGRES_OBJECT_STATISTICS_SQL, POSTGRES_OBJECT_STATISTICS_FALLBACK_SQL] {
|
||||
// Both queries must keep the same projection so the row mapping stays valid.
|
||||
assert!(sql.contains("AS estimated_rows"));
|
||||
assert!(sql.contains("AS total_bytes"));
|
||||
assert!(sql.contains("c.relkind IN ('r','m','f','p')"));
|
||||
assert!(sql.contains("WHERE n.nspname = $1"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user