0.5.1-pre.002
This commit is contained in:
197
ks-store/src/postgres/query/table_diagnostics_queries.rs
Normal file
197
ks-store/src/postgres/query/table_diagnostics_queries.rs
Normal file
@@ -0,0 +1,197 @@
|
||||
// file: ks-store/src/postgres/query/table_diagnostics_queries.rs
|
||||
// version: 3
|
||||
|
||||
//! Read-only PostgreSQL diagnostics for known Solana store tables.
|
||||
|
||||
use sqlx::Row; // rust-rules: trait-import
|
||||
|
||||
pub(in crate::postgres) async fn table_exists(
|
||||
pool: &sqlx::PgPool,
|
||||
table_name: &str,
|
||||
) -> ks_core::Result<bool> {
|
||||
let query_result =
|
||||
sqlx::query_scalar::<sqlx::Postgres, bool>("SELECT to_regclass($1)::text IS NOT NULL")
|
||||
.bind(table_name)
|
||||
.fetch_one(pool)
|
||||
.await;
|
||||
return match query_result {
|
||||
std::result::Result::Ok(value) => std::result::Result::Ok(value),
|
||||
std::result::Result::Err(error) => std::result::Result::Err(ks_core::Error::db(format!(
|
||||
"postgres table existence diagnostic failed for '{table_name}': {error}"
|
||||
))),
|
||||
};
|
||||
}
|
||||
|
||||
pub(in crate::postgres) async fn load_table_statistics(
|
||||
pool: &sqlx::PgPool,
|
||||
table_name: &str,
|
||||
) -> ks_core::Result<crate::PostgresTableStatistics> {
|
||||
return match table_name {
|
||||
crate::RAW_TRANSACTIONS_TABLE_NAME => {
|
||||
crate::postgres::query::table_diagnostics_queries::load_table_statistics_from_sql(
|
||||
pool,
|
||||
crate::postgres::migrations::table_stats_kb_sol_raw_transactions_sql(),
|
||||
table_name,
|
||||
)
|
||||
.await
|
||||
},
|
||||
crate::TRANSACTION_OBSERVATIONS_TABLE_NAME => {
|
||||
crate::postgres::query::table_diagnostics_queries::load_table_statistics_from_sql(
|
||||
pool,
|
||||
crate::postgres::migrations::table_stats_kb_sol_obs_transaction_observations_sql(),
|
||||
table_name,
|
||||
)
|
||||
.await
|
||||
},
|
||||
crate::CORE_TRANSACTIONS_TABLE_NAME => {
|
||||
crate::postgres::query::table_diagnostics_queries::load_table_statistics_from_sql(
|
||||
pool,
|
||||
crate::postgres::migrations::table_stats_kb_sol_core_transactions_sql(),
|
||||
table_name,
|
||||
)
|
||||
.await
|
||||
},
|
||||
crate::CORE_ACCOUNT_KEYS_TABLE_NAME => {
|
||||
crate::postgres::query::table_diagnostics_queries::load_table_statistics_from_sql(
|
||||
pool,
|
||||
crate::postgres::migrations::table_stats_kb_sol_core_account_keys_sql(),
|
||||
table_name,
|
||||
)
|
||||
.await
|
||||
},
|
||||
crate::CORE_INSTRUCTIONS_TABLE_NAME => {
|
||||
crate::postgres::query::table_diagnostics_queries::load_table_statistics_from_sql(
|
||||
pool,
|
||||
crate::postgres::migrations::table_stats_kb_sol_core_instructions_sql(),
|
||||
table_name,
|
||||
)
|
||||
.await
|
||||
},
|
||||
crate::CORE_INNER_INSTRUCTIONS_TABLE_NAME => {
|
||||
crate::postgres::query::table_diagnostics_queries::load_table_statistics_from_sql(
|
||||
pool,
|
||||
crate::postgres::migrations::table_stats_kb_sol_core_inner_instructions_sql(),
|
||||
table_name,
|
||||
)
|
||||
.await
|
||||
},
|
||||
crate::CORE_LOGS_TABLE_NAME => {
|
||||
crate::postgres::query::table_diagnostics_queries::load_table_statistics_from_sql(
|
||||
pool,
|
||||
crate::postgres::migrations::table_stats_kb_sol_core_logs_sql(),
|
||||
table_name,
|
||||
)
|
||||
.await
|
||||
},
|
||||
crate::CORE_BALANCE_CHANGES_TABLE_NAME => {
|
||||
crate::postgres::query::table_diagnostics_queries::load_table_statistics_from_sql(
|
||||
pool,
|
||||
crate::postgres::migrations::table_stats_kb_sol_core_balance_changes_sql(),
|
||||
table_name,
|
||||
)
|
||||
.await
|
||||
},
|
||||
crate::PROCESSING_LEDGER_TABLE_NAME => {
|
||||
crate::postgres::query::table_diagnostics_queries::load_table_statistics_from_sql(
|
||||
pool,
|
||||
crate::postgres::migrations::table_stats_kb_sol_ops_processing_ledger_sql(),
|
||||
table_name,
|
||||
)
|
||||
.await
|
||||
},
|
||||
crate::DECODE_EVENTS_TABLE_NAME => {
|
||||
crate::postgres::query::table_diagnostics_queries::load_table_statistics_from_sql(
|
||||
pool,
|
||||
crate::postgres::migrations::table_stats_kb_sol_decode_events_sql(),
|
||||
table_name,
|
||||
)
|
||||
.await
|
||||
},
|
||||
crate::DECODE_COVERAGE_DECLARATIONS_TABLE_NAME => {
|
||||
crate::postgres::query::table_diagnostics_queries::load_table_statistics_from_sql(
|
||||
pool,
|
||||
crate::postgres::migrations::table_stats_kb_sol_decode_coverage_declarations_sql(),
|
||||
table_name,
|
||||
)
|
||||
.await
|
||||
},
|
||||
crate::DECODE_COVERAGE_OBSERVATIONS_TABLE_NAME => {
|
||||
crate::postgres::query::table_diagnostics_queries::load_table_statistics_from_sql(
|
||||
pool,
|
||||
crate::postgres::migrations::table_stats_kb_sol_decode_coverage_observations_sql(),
|
||||
table_name,
|
||||
)
|
||||
.await
|
||||
},
|
||||
crate::MATERIALIZED_EVENTS_TABLE_NAME => {
|
||||
crate::postgres::query::table_diagnostics_queries::load_table_statistics_from_sql(
|
||||
pool,
|
||||
crate::postgres::migrations::table_stats_kb_sol_mat_events_sql(),
|
||||
table_name,
|
||||
)
|
||||
.await
|
||||
},
|
||||
_ => std::result::Result::Err(ks_core::Error::db(format!(
|
||||
"postgres table statistics are not supported for '{table_name}'"
|
||||
))),
|
||||
};
|
||||
}
|
||||
|
||||
async fn load_table_statistics_from_sql(
|
||||
pool: &sqlx::PgPool,
|
||||
sql: &'static str,
|
||||
table_name: &str,
|
||||
) -> ks_core::Result<crate::PostgresTableStatistics> {
|
||||
let query_result = sqlx::query(sql).fetch_one(pool).await;
|
||||
let row = match query_result {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => {
|
||||
return std::result::Result::Err(ks_core::Error::db(format!(
|
||||
"postgres table statistics failed for '{table_name}': {error}"
|
||||
)));
|
||||
},
|
||||
};
|
||||
let row_count_result = row.try_get::<i64, _>("row_count");
|
||||
let row_count = match row_count_result {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => {
|
||||
return std::result::Result::Err(ks_core::Error::db(format!(
|
||||
"postgres table row_count mapping failed for '{table_name}': {error}"
|
||||
)));
|
||||
},
|
||||
};
|
||||
let min_slot_result = row.try_get::<std::option::Option<i64>, _>("min_slot");
|
||||
let min_slot = match min_slot_result {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => {
|
||||
return std::result::Result::Err(ks_core::Error::db(format!(
|
||||
"postgres table min_slot mapping failed for '{table_name}': {error}"
|
||||
)));
|
||||
},
|
||||
};
|
||||
let max_slot_result = row.try_get::<std::option::Option<i64>, _>("max_slot");
|
||||
let max_slot = match max_slot_result {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => {
|
||||
return std::result::Result::Err(ks_core::Error::db(format!(
|
||||
"postgres table max_slot mapping failed for '{table_name}': {error}"
|
||||
)));
|
||||
},
|
||||
};
|
||||
let latest_created_at_result =
|
||||
row.try_get::<std::option::Option<std::string::String>, _>("latest_created_at");
|
||||
let latest_created_at = match latest_created_at_result {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => {
|
||||
return std::result::Result::Err(ks_core::Error::db(format!(
|
||||
"postgres table latest_created_at mapping failed for '{table_name}': {error}"
|
||||
)));
|
||||
},
|
||||
};
|
||||
return std::result::Result::Ok(crate::PostgresTableStatistics {
|
||||
row_count,
|
||||
min_slot,
|
||||
max_slot,
|
||||
latest_created_at,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user