219 lines
7.5 KiB
Rust
219 lines
7.5 KiB
Rust
// file: ks-store/src/postgres/query/table_diagnostics_queries.rs
|
|
// version: 7
|
|
|
|
//! Read-only PostgreSQL diagnostics for known Solana store tables.
|
|
|
|
use sqlx::Row; // rust-rules: trait-import
|
|
|
|
pub(crate) 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(crate) async fn load_table_statistics(
|
|
pool: &sqlx::PgPool,
|
|
table_name: &str,
|
|
) -> ks_core::Result<crate::StoreResourceStatistics> {
|
|
return match table_name {
|
|
crate::RAW_TRANSACTIONS_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_raw_transactions_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::TRANSACTION_OBSERVATIONS_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_obs_transaction_observations_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::ACCOUNT_OBSERVATIONS_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_obs_account_observations_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::CORE_TRANSACTIONS_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_core_transactions_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::CORE_ACCOUNT_KEYS_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_core_account_keys_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::CORE_INSTRUCTIONS_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_core_instructions_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::CORE_INNER_INSTRUCTIONS_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_core_inner_instructions_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::CORE_LOGS_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_core_logs_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::CORE_BALANCE_CHANGES_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_core_balance_changes_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::CORE_RETURN_DATA_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_core_return_data_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::CORE_ACCOUNT_STATES_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_core_account_states_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::PROCESSING_LEDGER_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_ops_processing_ledger_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::DECODE_EVENTS_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_decode_events_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::DECODE_COVERAGE_DECLARATIONS_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_decode_coverage_declarations_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::DECODE_COVERAGE_OBSERVATIONS_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_decode_coverage_observations_sql(),
|
|
table_name,
|
|
)
|
|
.await
|
|
},
|
|
crate::MATERIALIZED_OUTPUTS_TABLE_NAME => {
|
|
load_table_statistics_from_sql(
|
|
pool,
|
|
crate::table_stats_k_sol_mat_outputs_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::StoreResourceStatistics> {
|
|
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::StoreResourceStatistics {
|
|
record_count: row_count,
|
|
min_slot,
|
|
max_slot,
|
|
latest_created_at,
|
|
});
|
|
}
|