Skip to content

Commit

Permalink
add tuned GC index
Browse files Browse the repository at this point in the history
the original GC index was rarely selected
when GC'ing relations with millions of relationships.
This caused a sequential scan that could load potentially
Gigabytes worth of data, and overload the database.

⚠️ Postgres 13 and 14 do not correctly select this new index
because the query planner struggles with the xid8 datatype.
Postgres 15 will be needed for this index to
actually work. For scenarios where relations aren't very wide,
older PG versions may have an acceptable query latency for GC,
but we strongly recommend moving to PG15.

This migration also deletes the old suboptimal index after
the new one is created.
  • Loading branch information
vroldanbet committed Oct 3, 2023
1 parent ea861b2 commit 1e81e68
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package migrations

import (
"context"
"fmt"

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

const createTunedGCIndex = `CREATE INDEX CONCURRENTLY
IF NOT EXISTS ix_gc_index
ON relation_tuple (deleted_xid DESC)
WHERE deleted_xid < '9223372036854775807'::xid8;`

const deleteSuboptimalGCIndex = `DROP INDEX CONCURRENTLY IF EXISTS ix_relation_tuple_by_deleted_xid`

func init() {
if err := DatabaseMigrations.Register("add-tuned-gc-index", "add-gc-covering-index",
func(ctx context.Context, conn *pgx.Conn) error {
if _, err := conn.Exec(ctx, createTunedGCIndex); err != nil {
return fmt.Errorf("failed to create new tuned GC Index: %w", err)
}
if _, err := conn.Exec(ctx, deleteSuboptimalGCIndex); err != nil {
return fmt.Errorf("failed to remove old GC Index: %w", err)
}
if _, err := conn.Exec(ctx, "ANALYZE relation_tuple"); err != nil {
return fmt.Errorf("failed to update relation_tuple table statistics after new index: %w", err)
}
return nil
},
noTxMigration); err != nil {
panic("failed to register migration: " + err.Error())
}
}

0 comments on commit 1e81e68

Please sign in to comment.