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

Graphman new command: analyze #3170

Merged
merged 8 commits into from
Jan 21, 2022
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
12 changes: 12 additions & 0 deletions node/src/bin/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,13 @@ pub enum StatsCommand {
/// The name of a table to fully count
table: Option<String>,
},
/// Perform a SQL ANALYZE in a Entity table
Analyze {
/// The id of the deployment
id: String,
/// The name of the Entity to ANALYZE
entity: String,
},
}

impl From<Opt> for config::Opt {
Expand Down Expand Up @@ -695,6 +702,11 @@ async fn main() {
commands::stats::account_like(ctx.pools(), clear, table)
}
Show { nsp, table } => commands::stats::show(ctx.pools(), nsp, table),
Analyze { id, entity } => {
let store = ctx.store();
let subgraph_store = store.subgraph_store();
commands::stats::analyze(subgraph_store, id, entity).await
}
}
}
};
Expand Down
23 changes: 23 additions & 0 deletions node/src/manager/commands/stats.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use std::collections::HashMap;
use std::sync::Arc;

use diesel::r2d2::ConnectionManager;
use diesel::r2d2::PooledConnection;
use diesel::sql_query;
use diesel::sql_types::{Integer, Text};
use diesel::PgConnection;
use diesel::RunQueryDsl;
use graph::components::store::EntityType;
use graph::prelude::anyhow;
use graph::prelude::anyhow::bail;
use graph::prelude::DeploymentHash;
use graph_store_postgres::command_support::catalog::Site;
use graph_store_postgres::command_support::{catalog as store_catalog, SqlName};
use graph_store_postgres::connection_pool::ConnectionPool;
use graph_store_postgres::Shard;
use graph_store_postgres::SubgraphStore;
use graph_store_postgres::PRIMARY_SHARD;

fn parse_table_name(table: &str) -> Result<(&str, SqlName), anyhow::Error> {
Expand Down Expand Up @@ -151,3 +155,22 @@ pub fn show(

Ok(())
}

pub async fn analyze(
store: Arc<SubgraphStore>,
hash: String,
entity_name: String,
) -> Result<(), anyhow::Error> {
println!("Running ANALYZE for {entity_name} entity");
let entity_type = EntityType::new(entity_name);
let deployment_hash = DeploymentHash::new(hash).map_err(|malformed_hash| {
anyhow!(
"Subgraph hash must be a valid IPFS hash: {}",
malformed_hash
)
})?;
store
.analyze(&deployment_hash, entity_type)
.await
.map_err(|e| anyhow!(e))
}
18 changes: 18 additions & 0 deletions store/postgres/src/deployment_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,24 @@ impl DeploymentStore {
})
.await
}

/// Runs the SQL `ANALYZE` command in a table.
pub(crate) async fn analyze(
&self,
site: Arc<Site>,
entity_type: EntityType,
) -> Result<(), StoreError> {
let store = self.clone();
self.with_conn(move |conn, _| {
let layout = store.layout(conn, site)?;
let table = layout.table_for_entity(&entity_type)?;
let table_name = &table.qualified_name;
let sql = format!("analyze {table_name}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if diesel provided analyze in their pg module, perhaps we can add there someday 🙂

conn.execute(&sql)?;
Ok(())
})
.await
}
}

/// Methods that back the trait `graph::components::Store`, but have small
Expand Down
11 changes: 10 additions & 1 deletion store/postgres/src/subgraph_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use graph::{
components::{
server::index_node::VersionInfo,
store::{
self, DeploymentLocator, EnsLookup as EnsLookupTrait,
self, DeploymentLocator, EnsLookup as EnsLookupTrait, EntityType,
WritableStore as WritableStoreTrait,
},
},
Expand Down Expand Up @@ -947,6 +947,15 @@ impl SubgraphStoreInner {
)
.await;
}

pub async fn analyze(
&self,
id: &DeploymentHash,
entity_type: EntityType,
) -> Result<(), StoreError> {
let (store, site) = self.store(&id)?;
store.analyze(site, entity_type).await
}
}

struct EnsLookup {
Expand Down