Skip to content

Commit

Permalink
adds index to support efficient querying of Watch API
Browse files Browse the repository at this point in the history
we identified Watch API was causing a full table scan on
large postgres-backed SpiceDB, overloading the database.

This commit adds an index that reduces the execution time
of relationship changes to be emitted via Watch API by
more than three orders of magnitude on tables with several
gigabytes worth of data.
  • Loading branch information
vroldanbet committed Nov 26, 2024
1 parent 7ff8fb1 commit 8ed00e6
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package migrations

import (
"context"
"fmt"

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

const addWatchAPIIndexToRelationTupleTable = `CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_watch_index ON relation_tuple (created_xid);`

func init() {
if err := DatabaseMigrations.Register("add-watch-api-index-to-relation-tuple-table", "add-metadata-to-transaction-table",
func(ctx context.Context, conn *pgx.Conn) error {
if _, err := conn.Exec(ctx, addWatchAPIIndexToRelationTupleTable); err != nil {
return fmt.Errorf("failed to add watch API index to relation tuple table: %w", err)
}
return nil
},
noTxMigration); err != nil {
panic("failed to register migration: " + err.Error())
}
}

0 comments on commit 8ed00e6

Please sign in to comment.