Skip to content

Commit

Permalink
adds new index to help support DeleteRelationships with limit
Browse files Browse the repository at this point in the history
For folks running schema migrations, which typically involve
deleting relationships of one of the relations affected
in their schema, we recommend using DeleteRelationship
API with limit enabled not to overload the database in
situations where relations have millions of relationships.

DeleteRelationships with limits is fundamentally different
in that it has to do a SELECT first because Postgres does not
support DELETE ... LIMIT statements.

That particular SELECT statement was found to be unindexed
and be extremely slow in SpiceDB clusters with millions of relationships.
This commit adds a new index to support that access pattern
and make deletes+limit faster and less impactful overall.

⚠️This index uses the xid8 type and also requires Postgres 15
for the query planner to do the right thing.
  • Loading branch information
vroldanbet committed Sep 29, 2023
1 parent 13f6d3f commit 84250fd
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package migrations

import (
"context"
"fmt"

"github.com/jackc/pgx/v5"
)

const createAliveRelByResourceRelationSubjectIndex = `CREATE INDEX CONCURRENTLY
IF NOT EXISTS ix_relation_tuple_alive_by_resource_rel_subject
ON relation_tuple (namespace, relation, userset_namespace)
WHERE deleted_xid = '9223372036854775807'::xid8;`

func init() {
if err := DatabaseMigrations.Register("add-rel-by-alive-resource-relation-subject", "add-tuned-gc-index",
func(ctx context.Context, conn *pgx.Conn) error {
if _, err := conn.Exec(ctx, createAliveRelByResourceRelationSubjectIndex); err != nil {
return fmt.Errorf("failed to create index for alive relationships by resource/relation/subject: %w", err)
}
return nil
},
noTxMigration); err != nil {
panic("failed to register migration: " + err.Error())
}
}

0 comments on commit 84250fd

Please sign in to comment.