diff --git a/store/postgres/src/relational/index.rs b/store/postgres/src/relational/index.rs index 6013a5d9e68..4cc354b8e89 100644 --- a/store/postgres/src/relational/index.rs +++ b/store/postgres/src/relational/index.rs @@ -734,6 +734,16 @@ pub struct IndexList { pub(crate) indexes: HashMap>, } +pub fn load_indexes_from_table( + conn: &mut PgConnection, + table: &Arc, + schema_name: &str, +) -> Result, StoreError> { + let table_name = table.name.as_str(); + let indexes = catalog::indexes_for_table(conn, schema_name, table_name)?; + Ok(indexes.into_iter().map(CreateIndex::parse).collect()) +} + impl IndexList { pub fn load( conn: &mut PgConnection, @@ -746,10 +756,8 @@ impl IndexList { let schema_name = site.namespace.clone(); let layout = store.layout(conn, site)?; for (_, table) in &layout.tables { - let table_name = table.name.as_str(); - let indexes = catalog::indexes_for_table(conn, schema_name.as_str(), table_name)?; - let collect: Vec = indexes.into_iter().map(CreateIndex::parse).collect(); - list.indexes.insert(table_name.to_string(), collect); + let indexes = load_indexes_from_table(conn, table, schema_name.as_str())?; + list.indexes.insert(table.name.to_string(), indexes); } Ok(list) } diff --git a/store/postgres/src/relational/prune.rs b/store/postgres/src/relational/prune.rs index 10a9cff1626..d54f8106955 100644 --- a/store/postgres/src/relational/prune.rs +++ b/store/postgres/src/relational/prune.rs @@ -1,4 +1,4 @@ -use std::{fmt::Write, sync::Arc, time::Instant}; +use std::{collections::HashMap, fmt::Write, sync::Arc, time::Instant}; use diesel::{ connection::SimpleConnection, @@ -24,7 +24,10 @@ use crate::{ relational::{Table, VID_COLUMN}, }; -use super::{Catalog, Layout, Namespace}; +use super::{ + index::{load_indexes_from_table, IndexList}, + Catalog, Layout, Namespace, +}; // Additions to `Table` that are useful for pruning impl Table { @@ -94,9 +97,15 @@ impl TablePair { if catalog::table_exists(conn, dst_nsp.as_str(), &dst.name)? { writeln!(query, "truncate table {};", dst.qualified_name)?; } else { + let mut list = IndexList { + indexes: HashMap::new(), + }; + let indexes = load_indexes_from_table(conn, &src, dst_nsp.as_str())?; + list.indexes.insert(src.name.to_string(), indexes); + // In case of pruning we don't do delayed creation of indexes, // as the asumption is that there is not that much data inserted. - dst.as_ddl(schema, catalog, None, &mut query)?; + dst.as_ddl(schema, catalog, Some(&list), &mut query)?; } conn.batch_execute(&query)?;