Skip to content
Closed
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
20 changes: 20 additions & 0 deletions crates/storage-postgres/src/data/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,26 @@ impl PostgresEngine {
.map_err(|e| StorageError::Internal(e.to_string()))?;
}

// Index on base-table key columns to support efficient cascade deletes
// from `delete_index_row_multi`, whose predicate is
// `WHERE base_pk = $1 [AND base_sk_<type> = $n ...]`. The PK constraint
// and ordering index both lead with `pk`, so neither can serve this
// lookup — without this index PostgreSQL falls back to a Seq Scan.
let mut cascade_cols = vec!["base_pk".to_owned()];
for (i, &(_, sk_type)) in base_sks.iter().enumerate() {
let col = if i == 0 {
format!("base_{}", sk_column(sk_type))
} else {
format!("base_{}", sk_column_n(i, sk_type))
};
cascade_cols.push(col);
}
let cascade_idx = format!("CREATE INDEX ON {idx_table} ({})", cascade_cols.join(", "));
sqlx::query(&cascade_idx)
.execute(&mut **tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;

Ok(())
}

Expand Down
Loading