Skip to content

perf(gsi): add index on (base_pk, base_sk_*) for cascade deletes#116

Closed
parsnips wants to merge 1 commit into
ExtendDB:mainfrom
parsnips:index-for-delete-cascade
Closed

perf(gsi): add index on (base_pk, base_sk_*) for cascade deletes#116
parsnips wants to merge 1 commit into
ExtendDB:mainfrom
parsnips:index-for-delete-cascade

Conversation

@parsnips
Copy link
Copy Markdown

@parsnips parsnips commented May 22, 2026

What

Cascade-delete from base-table writes runs
DELETE FROM <gsi> WHERE base_pk = $1 [AND base_sk_<type> = $n ...] against every GSI table. Neither the PK constraint nor the existing ordering index can serve this lookup because both lead with pk, so PostgreSQL falls back to a Seq Scan. In a recent benchmark this missing index accounted for ~74% of total DB time.

Create an index that mirrors the cascade-delete predicate at GSI table creation time. Column order matches the PK constraint minus the leading pk, so for a single-bytes base SK this produces (base_pk, base_sk_b). The index is created unconditionally — HASH-only base tables still benefit from (base_pk) alone.

Only applies to GSI tables created after this change; existing deployments need a backfill migration to pick up the index.

Why

GSI Ops should be fast.

Closes #115

Testing done

Manually applied and ran an internal benchmark and plan.

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace)
  • Code is formatted (cargo fmt --check)
  • (no new warnings) Clippy is clean (cargo clippy -- -W clippy::pedantic)
  • I have added or updated tests for new functionality
  • I have updated documentation if behavior changed
  • Breaking changes are noted below (if any)

By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.

Cascade-delete from base-table writes runs
`DELETE FROM <gsi> WHERE base_pk = $1 [AND base_sk_<type> = $n ...]`
against every GSI table. Neither the PK constraint nor the existing
ordering index can serve this lookup because both lead with `pk`, so
PostgreSQL falls back to a Seq Scan. In a recent benchmark this missing
index accounted for ~74% of total DB time.

Create an index that mirrors the cascade-delete predicate at GSI table
creation time. Column order matches the PK constraint minus the leading
`pk`, so for a single-bytes base SK this produces `(base_pk, base_sk_b)`.
The index is created unconditionally — HASH-only base tables still
benefit from `(base_pk)` alone.

Only applies to GSI tables created after this change; existing
deployments need a backfill migration to pick up the index.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@parsnips
Copy link
Copy Markdown
Author

looks like #127 will fix this.

@parsnips parsnips closed this May 25, 2026
@parsnips parsnips deleted the index-for-delete-cascade branch May 25, 2026 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] GSI missing index for efficient propagation

1 participant