risingwave_frontend/expr/function_impl/
pg_indexes_size.rs1use risingwave_expr::{ExprError, Result, capture_context, function};
16use thiserror_ext::AsReport;
17
18use super::context::{CATALOG_READER, DB_NAME};
19use crate::catalog::CatalogReader;
20
21#[function("pg_indexes_size(int4) -> int8")]
23fn pg_indexes_size(oid: i32) -> Result<i64> {
24 pg_indexes_size_impl_captured(oid)
25}
26
27#[capture_context(CATALOG_READER, DB_NAME)]
28fn pg_indexes_size_impl(catalog: &CatalogReader, db_name: &str, oid: i32) -> Result<i64> {
29 let catalog = catalog.read_guard();
30 let database = catalog
31 .get_database_by_name(db_name)
32 .map_err(|e| ExprError::InvalidParam {
33 name: "oid",
34 reason: e.to_report_string().into(),
35 })?;
36 let mut sum = 0;
37 for schema in database.iter_schemas() {
38 for index in schema.iter_index() {
39 if index.primary_table.id().table_id == oid as u32 {
40 if let Some(table_stats) = catalog
41 .table_stats()
42 .table_stats
43 .get(&index.primary_table.id().table_id)
44 {
45 sum += table_stats.total_key_size + table_stats.total_value_size;
46 }
47 }
48 }
49 }
50 Ok(sum)
51}