Skip to content

Commit 483b3b5

Browse files
graph, store, server: Expose table sizes in deployment indexing status
Signed-off-by: Maksim Dimitrov <[email protected]>
1 parent 9426ef6 commit 483b3b5

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

graph/src/data/subgraph/status.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,35 @@ impl IntoValue for ChainInfo {
9191
}
9292
}
9393

94+
#[derive(Debug, Default)]
95+
pub struct SubgraphSize {
96+
pub row_estimate: i64,
97+
pub table_bytes: i64,
98+
pub index_bytes: i64,
99+
pub toast_bytes: i64,
100+
pub total_bytes: i64,
101+
}
102+
103+
impl IntoValue for SubgraphSize {
104+
fn into_value(self) -> r::Value {
105+
let SubgraphSize {
106+
row_estimate,
107+
table_bytes,
108+
index_bytes,
109+
toast_bytes,
110+
total_bytes,
111+
} = self;
112+
object! {
113+
__typename: "SubgraphSize",
114+
rowEstimate: format!("{}", row_estimate),
115+
tableSize: format!("{}", table_bytes),
116+
indexSize: format!("{}", index_bytes),
117+
toastSize: format!("{}", toast_bytes),
118+
totalSize: format!("{}", total_bytes),
119+
}
120+
}
121+
}
122+
94123
#[derive(Debug)]
95124
pub struct Info {
96125
pub id: DeploymentId,
@@ -114,6 +143,9 @@ pub struct Info {
114143
pub node: Option<String>,
115144

116145
pub history_blocks: i32,
146+
147+
/// The size of all entity tables in a deployment's namespace in bytes.
148+
pub subgraph_size: SubgraphSize,
117149
}
118150

119151
impl IntoValue for Info {
@@ -130,6 +162,7 @@ impl IntoValue for Info {
130162
non_fatal_errors,
131163
synced,
132164
history_blocks,
165+
subgraph_size,
133166
} = self;
134167

135168
fn subgraph_error_to_value(subgraph_error: SubgraphError) -> r::Value {
@@ -173,6 +206,7 @@ impl IntoValue for Info {
173206
entityCount: format!("{}", entity_count),
174207
node: node,
175208
historyBlocks: history_blocks,
209+
subgraphSize: subgraph_size.into_value(),
176210
}
177211
}
178212
}

server/index-node/src/schema.graphql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ type SubgraphIndexingStatus {
7777
paused: Boolean
7878

7979
historyBlocks: Int!
80+
81+
"Total size of all tables a deployment's namespace in bytes."
82+
subgraphSize: SubgraphSize!
8083
}
8184

8285
interface ChainIndexingStatus {
@@ -206,3 +209,11 @@ type ApiVersion {
206209
"""
207210
version: String!
208211
}
212+
213+
type SubgraphSize {
214+
rowEstimate: BigInt!
215+
tableSize: BigInt!
216+
indexSize: BigInt!
217+
toastSize: BigInt!
218+
totalSize: BigInt!
219+
}

store/postgres/src/detail.rs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use diesel::prelude::{
77
ExpressionMethods, JoinOnDsl, NullableExpressionMethods, OptionalExtension, PgConnection,
88
QueryDsl, RunQueryDsl, SelectableHelper as _,
99
};
10+
use diesel::sql_types::{Array, BigInt, Integer};
1011
use diesel_derives::Associations;
1112
use git_testament::{git_testament, git_testament_macros};
1213
use graph::blockchain::BlockHash;
@@ -239,6 +240,7 @@ pub(crate) fn info_from_details(
239240
non_fatal: Vec<ErrorDetail>,
240241
sites: &[Arc<Site>],
241242
subgraph_history_blocks: i32,
243+
subgraph_size: status::SubgraphSize,
242244
) -> Result<status::Info, StoreError> {
243245
let DeploymentDetail {
244246
id,
@@ -301,6 +303,7 @@ pub(crate) fn info_from_details(
301303
entity_count,
302304
node: None,
303305
history_blocks: subgraph_history_blocks,
306+
subgraph_size,
304307
})
305308
}
306309

@@ -422,17 +425,87 @@ pub(crate) fn deployment_statuses(
422425
.collect()
423426
};
424427

428+
let mut deployment_sizes = deployment_sizes(conn, sites)?;
429+
425430
details_with_fatal_error
426431
.into_iter()
427432
.map(|(deployment, head, fatal)| {
428433
let detail = DeploymentDetail::from((deployment, head));
429434
let non_fatal = non_fatal_errors.remove(&detail.id).unwrap_or_default();
430435
let subgraph_history_blocks = history_blocks_map.remove(&detail.id).unwrap_or_default();
431-
info_from_details(detail, fatal, non_fatal, sites, subgraph_history_blocks)
436+
let table_sizes = deployment_sizes.remove(&detail.id).unwrap_or_default();
437+
info_from_details(
438+
detail,
439+
fatal,
440+
non_fatal,
441+
sites,
442+
subgraph_history_blocks,
443+
table_sizes,
444+
)
432445
})
433446
.collect()
434447
}
435448

449+
fn deployment_sizes(
450+
conn: &mut PgConnection,
451+
sites: &[Arc<Site>],
452+
) -> Result<HashMap<DeploymentId, status::SubgraphSize>, StoreError> {
453+
#[derive(QueryableByName)]
454+
struct SubgraphSizeRow {
455+
#[diesel(sql_type = Integer)]
456+
id: DeploymentId,
457+
#[diesel(sql_type = BigInt)]
458+
row_estimate: i64,
459+
#[diesel(sql_type = BigInt)]
460+
table_bytes: i64,
461+
#[diesel(sql_type = BigInt)]
462+
index_bytes: i64,
463+
#[diesel(sql_type = BigInt)]
464+
toast_bytes: i64,
465+
#[diesel(sql_type = BigInt)]
466+
total_bytes: i64,
467+
}
468+
469+
let mut query = String::from(
470+
r#"
471+
SELECT
472+
ds.id,
473+
ss.row_estimate::bigint,
474+
ss.table_bytes::bigint,
475+
ss.index_bytes::bigint,
476+
ss.toast_bytes::bigint,
477+
ss.total_bytes::bigint
478+
FROM deployment_schemas ds
479+
JOIN info.subgraph_sizes as ss on ss.name = ds.name
480+
"#,
481+
);
482+
483+
let rows = if sites.is_empty() {
484+
diesel::sql_query(query).load::<SubgraphSizeRow>(conn)?
485+
} else {
486+
query.push_str(" WHERE ds.id = ANY($1)");
487+
diesel::sql_query(query)
488+
.bind::<Array<Integer>, _>(sites.iter().map(|site| site.id).collect::<Vec<_>>())
489+
.load::<SubgraphSizeRow>(conn)?
490+
};
491+
492+
let mut sizes: HashMap<DeploymentId, status::SubgraphSize> = HashMap::new();
493+
for row in rows {
494+
sizes.insert(
495+
row.id,
496+
status::SubgraphSize {
497+
row_estimate: row.row_estimate,
498+
table_bytes: row.table_bytes,
499+
index_bytes: row.index_bytes,
500+
toast_bytes: row.toast_bytes,
501+
total_bytes: row.total_bytes,
502+
},
503+
);
504+
}
505+
506+
Ok(sizes)
507+
}
508+
436509
#[derive(Queryable, Selectable, Identifiable, Associations)]
437510
#[diesel(table_name = subgraph_manifest)]
438511
#[diesel(belongs_to(GraphNodeVersion))]

0 commit comments

Comments
 (0)