Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "[EASY] Update used DB tables list (#3266)" #3275

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions crates/autopilot/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,22 @@ impl Postgres {
let metrics = Metrics::get();

// update table row metrics
let mut ex = self.pool.acquire().await?;
for table in database::all_tables(&mut ex).await? {
if database::LARGE_TABLES.iter().any(|t| *t == table) {
let count = estimate_rows_in_table(&mut ex, table).await?;
metrics.table_rows.with_label_values(&[table]).set(count);
} else {
let count = count_rows_in_table(&mut ex, table).await?;
metrics.table_rows.with_label_values(&[table]).set(count);
}
for &table in database::TABLES {
let mut ex = self.pool.acquire().await?;
let count = count_rows_in_table(&mut ex, table).await?;
metrics.table_rows.with_label_values(&[table]).set(count);
}

// update table row metrics
for &table in database::LARGE_TABLES {
let mut ex = self.pool.acquire().await?;
let count = estimate_rows_in_table(&mut ex, table).await?;
metrics.table_rows.with_label_values(&[table]).set(count);
}

// update unused app data metric
{
let mut ex = self.pool.acquire().await?;
let count = count_unused_app_data(&mut ex).await?;
metrics.unused_app_data.set(count);
}
Expand Down
1 change: 0 additions & 1 deletion crates/database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ hex = { workspace = true }
sqlx = { workspace = true }
strum = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["sync"] }

[dev-dependencies]
maplit = { workspace = true }
Expand Down
56 changes: 31 additions & 25 deletions crates/database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ pub mod trades;

use {
byte_array::ByteArray,
sqlx::{Executor, PgConnection, PgPool},
tokio::sync::OnceCell,
sqlx::{Executor, PgPool},
};

// Design:
Expand All @@ -49,36 +48,43 @@ use {

pub type PgTransaction<'a> = sqlx::Transaction<'a, sqlx::Postgres>;

/// The names of potentially big volume tables we use in the db.
pub const LARGE_TABLES: &[&str] = &["auction_prices", "order_events"];

pub async fn all_tables(ex: &mut PgConnection) -> sqlx::Result<&'static Vec<String>> {
static TABLES: OnceCell<Vec<String>> = OnceCell::const_new();

TABLES
.get_or_try_init(|| async {
#[derive(sqlx::FromRow, Debug)]
struct TableName(String);
/// The names of tables we use in the db.
pub const TABLES: &[&str] = &[
"orders",
"trades",
"invalidations",
"last_indexed_blocks",
"quotes",
"settlements",
"presignature_events",
"order_quotes",
"solver_competitions",
"auctions",
"competition_auctions",
"onchain_placed_orders",
"ethflow_orders",
"order_execution",
"interactions",
"ethflow_refunds",
"settlement_scores",
"settlement_observations",
"auction_prices",
"auction_participants",
"app_data",
"jit_orders",
];

const QUERY: &str = "SELECT tablename FROM pg_tables WHERE schemaname = 'public' AND \
tablename NOT LIKE '%flyway%'";

let table_names: Vec<String> = sqlx::query_as(QUERY)
.fetch_all(ex)
.await?
.into_iter()
.map(|TableName(name)| name)
.collect();
/// The names of potentially big volume tables we use in the db.
pub const LARGE_TABLES: &[&str] = &["order_events"];

Ok(table_names)
})
.await
pub fn all_tables() -> impl Iterator<Item = &'static str> {
TABLES.iter().copied().chain(LARGE_TABLES.iter().copied())
}

/// Delete all data in the database. Only used by tests.
#[allow(non_snake_case)]
pub async fn clear_DANGER_(ex: &mut PgTransaction<'_>) -> sqlx::Result<()> {
for table in all_tables(ex).await? {
for table in all_tables() {
ex.execute(format!("TRUNCATE {table};").as_str()).await?;
}
Ok(())
Expand Down
Loading