Skip to content

Commit dde111c

Browse files
committed
store: Create postponed indexes non-concurrently for copy/graft
At the point where we create the postponed indexes during copying, nothing else is writing to the subgraph and we can't be blocking a writer with a normal 'create index'. Since concurrent index creation has to wait for all previous transactions in the database to finish, the concurrent creation can significantly slow down index creation and therefore how long the copy takes.
1 parent ccd65e7 commit dde111c

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

store/postgres/src/copy.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ impl Connection {
730730
&table.src.name.to_string(),
731731
&table.dst,
732732
true,
733-
true,
733+
false,
734734
)?;
735735

736736
for (_, sql) in arr {
@@ -748,7 +748,11 @@ impl Connection {
748748
.iter()
749749
.map(|c| c.name.to_string())
750750
.collect_vec();
751-
for sql in table.dst.create_postponed_indexes(orig_colums).into_iter() {
751+
for sql in table
752+
.dst
753+
.create_postponed_indexes(orig_colums, false)
754+
.into_iter()
755+
{
752756
let query = sql_query(sql);
753757
query.execute(conn)?;
754758
}

store/postgres/src/relational/ddl.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ impl Table {
269269
(method, index_expr)
270270
}
271271

272-
pub(crate) fn create_postponed_indexes(&self, skip_colums: Vec<String>) -> Vec<String> {
272+
pub(crate) fn create_postponed_indexes(
273+
&self,
274+
skip_colums: Vec<String>,
275+
concurrently: bool,
276+
) -> Vec<String> {
273277
let mut indexing_queries = vec![];
274278
let columns = self.columns_to_index();
275279

@@ -281,8 +285,9 @@ impl Table {
281285
&& column.name.as_str() != "id"
282286
&& !skip_colums.contains(&column.name.to_string())
283287
{
288+
let conc = if concurrently { "concurrently " } else { "" };
284289
let sql = format!(
285-
"create index concurrently if not exists attr_{table_index}_{column_index}_{table_name}_{column_name}\n on {qname} using {method}({index_expr});\n",
290+
"create index {conc}if not exists attr_{table_index}_{column_index}_{table_name}_{column_name}\n on {qname} using {method}({index_expr});\n",
286291
table_index = self.position,
287292
table_name = self.name,
288293
column_name = column.name,

store/postgres/src/relational/ddl_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn generate_postponed_indexes() {
158158
let layout = test_layout(THING_GQL);
159159
let table = layout.table(&SqlName::from("Scalar")).unwrap();
160160
let skip_colums = vec!["id".to_string()];
161-
let query_vec = table.create_postponed_indexes(skip_colums);
161+
let query_vec = table.create_postponed_indexes(skip_colums, true);
162162
assert!(query_vec.len() == 7);
163163
let queries = query_vec.join(" ");
164164
check_eqv(THING_POSTPONED_INDEXES, &queries)

0 commit comments

Comments
 (0)