From 90256698ba6e1e4cf16df15a70e8a3cca764a842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rold=C3=A1n=20Betancort?= Date: Thu, 28 Sep 2023 09:42:02 +0100 Subject: [PATCH] add tuned GC index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../zz_migration.0016_add_tuned_gc_index.go | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 internal/datastore/postgres/migrations/zz_migration.0016_add_tuned_gc_index.go diff --git a/internal/datastore/postgres/migrations/zz_migration.0016_add_tuned_gc_index.go b/internal/datastore/postgres/migrations/zz_migration.0016_add_tuned_gc_index.go new file mode 100644 index 0000000000..3ca0bcd7b2 --- /dev/null +++ b/internal/datastore/postgres/migrations/zz_migration.0016_add_tuned_gc_index.go @@ -0,0 +1,31 @@ +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) + } + return nil + }, + noTxMigration); err != nil { + panic("failed to register migration: " + err.Error()) + } +}