Skip to content

Commit cf2f440

Browse files
committed
store: Create postponed indexes on subgraph restart
Replace the IndexList-based `recreate_invalid_indexes` call in `start_subgraph()` with a call to `create_postponed_indexes()`. This uses `IF NOT EXISTS` and `CONCURRENTLY` to safely create any missing postponed indexes on every restart, acting as a safety net. Remove the now-unused `IndexList::recreate_invalid_indexes` method.
1 parent 33094d4 commit cf2f440

2 files changed

Lines changed: 5 additions & 67 deletions

File tree

store/postgres/src/deployment_store.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,19 +1627,15 @@ impl DeploymentStore {
16271627
.await?;
16281628
}
16291629

1630-
let mut conn = self.pool.get_permitted().await?;
1631-
if ENV_VARS.postpone_attribute_index_creation {
1632-
// Check if all indexes are valid and recreate them if they
1633-
// aren't.
1634-
IndexList::load(&mut conn, &dst)
1635-
.await?
1636-
.recreate_invalid_indexes(&mut conn, &dst)
1637-
.await?;
1638-
}
1630+
// Create any indexes whose creation was postponed when the
1631+
// deployment was first created. Using `IF NOT EXISTS` and
1632+
// `CONCURRENTLY` makes this safe to call on every restart.
1633+
self.create_postponed_indexes(site.cheap_clone()).await?;
16391634

16401635
// Make sure the block pointer is set. This is important for newly
16411636
// deployed subgraphs so that we respect the 'startBlock' setting
16421637
// the first time the subgraph is started
1638+
let mut conn = self.pool.get_permitted().await?;
16431639
conn.transaction(|conn| {
16441640
crate::deployment::initialize_block_ptr(conn, &dst.site).scope_boxed()
16451641
})

store/postgres/src/relational/index.rs

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::collections::HashMap;
44
use std::fmt::{Display, Write};
55

66
use diesel::sql_query;
7-
use diesel::sql_types::{Bool, Text};
87
use diesel_async::RunQueryDsl;
98
use graph::components::store::StoreError;
109
use graph::itertools::Itertools;
@@ -947,63 +946,6 @@ impl IndexList {
947946

948947
iter
949948
}
950-
951-
pub async fn recreate_invalid_indexes(
952-
&self,
953-
conn: &mut AsyncPgConnection,
954-
layout: &Layout,
955-
) -> Result<(), StoreError> {
956-
#[derive(QueryableByName, Debug)]
957-
struct IndexInfo {
958-
#[diesel(sql_type = Bool)]
959-
isvalid: bool,
960-
}
961-
962-
let namespace = &layout.catalog.site.namespace;
963-
let creat = layout.index_creator(true, true);
964-
for table in layout.tables.values() {
965-
let idxs = self
966-
.indexes_for_table(table)
967-
.filter(|idx| idx.to_postpone());
968-
for idx in idxs {
969-
if let Some(index_name) = idx.name() {
970-
let table_name = table.name.clone();
971-
let query = r#"
972-
SELECT x.indisvalid AS isvalid
973-
FROM pg_index x
974-
JOIN pg_class c ON c.oid = x.indrelid
975-
JOIN pg_class i ON i.oid = x.indexrelid
976-
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
977-
WHERE (c.relkind = ANY (ARRAY ['r'::"char", 'm'::"char", 'p'::"char"]))
978-
AND (i.relkind = ANY (ARRAY ['i'::"char", 'I'::"char"]))
979-
AND (n.nspname = $1)
980-
AND (c.relname = $2)
981-
AND (i.relname = $3);"#;
982-
let ii_vec = sql_query(query)
983-
.bind::<Text, _>(namespace.to_string())
984-
.bind::<Text, _>(table_name)
985-
.bind::<Text, _>(index_name.clone())
986-
.get_results::<IndexInfo>(conn)
987-
.await?
988-
.into_iter()
989-
.collect::<Vec<IndexInfo>>();
990-
assert!(ii_vec.len() <= 1);
991-
if ii_vec.is_empty() || !ii_vec[0].isvalid {
992-
// if a bad index exist lets first drop it
993-
if !ii_vec.is_empty() {
994-
let drop_query =
995-
sql_query(format!("DROP INDEX {}.{};", namespace, index_name));
996-
drop_query.execute(conn).await?;
997-
}
998-
// We are creating concurrently, which can't be done
999-
// in a transaction
1000-
IndexCreator::execute(&creat, conn, idx).await?;
1001-
}
1002-
}
1003-
}
1004-
}
1005-
Ok(())
1006-
}
1007949
}
1008950

1009951
#[cfg(test)]

0 commit comments

Comments
 (0)