Skip to content

Commit

Permalink
WIP: high load unique index
Browse files Browse the repository at this point in the history
  • Loading branch information
agedemenli committed Jan 21, 2025
1 parent a9b35b5 commit 3b8f549
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pkg/migrations/op_set_unique.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package migrations
import (
"context"
"fmt"
"time"

"github.com/lib/pq"
"github.com/xataio/pgroll/pkg/db"
Expand All @@ -30,6 +31,11 @@ func (o *OpSetUnique) Start(ctx context.Context, conn db.DB, latestSchema string
return nil, fmt.Errorf("failed to add unique index: %w", err)
}

err := ensureIndexIsValid(ctx, conn, s.Name, o.Name)
if err != nil {
return nil, fmt.Errorf("failed to validate unique index: %w", err)
}

return table, nil
}

Expand Down Expand Up @@ -83,3 +89,43 @@ func addUniqueIndex(ctx context.Context, conn db.DB, table, column, name string)

return err
}

func ensureIndexIsValid(ctx context.Context, conn db.DB, schemaname string, indexname string) error {
invalid, err := isIndexInvalid(ctx, conn, schemaname, indexname)
if err != nil {
return err
}
for invalid {
time.Sleep(1 * time.Second)
invalid, err = isIndexInvalid(ctx, conn, schemaname, indexname)
if err != nil {
return err
}
}
return nil
}

func isIndexInvalid(ctx context.Context, conn db.DB, schemaname string, indexname string) (bool, error) {
var exists bool
rows, err := conn.QueryContext(ctx, `
SELECT EXISTS (
SELECT *
FROM pg_catalog.pg_index
WHERE indexrelid = $1::regclass
AND indisvalid = false)`,
fmt.Sprintf("%s.%s", pq.QuoteIdentifier(schemaname), pq.QuoteIdentifier(indexname)))
if err != nil {
return false, fmt.Errorf("getting invalid index with name %q: %w", indexname, err)
}
if err := db.ScanFirstValue(rows, &exists); err != nil {
return false, fmt.Errorf("scanning invalid index with name %q: %w", indexname, err)
}

if exists {
fmt.Printf("%s Invalid, waiting for a sec\n", fmt.Sprintf("%s.%s", pq.QuoteIdentifier(schemaname), pq.QuoteIdentifier(indexname)))
} else {
fmt.Println("VALID")
}

return exists, nil
}

0 comments on commit 3b8f549

Please sign in to comment.