Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
perf: add partial index on plan_check_run for RUNNING status (#17889)
Danny Xudc57ae2eaf28
diff --git a/backend/migrator/migration/3.11/0021##plan_check_run_status_index.sql b/backend/migrator/migration/3.11/0021##plan_check_run_status_index.sqlnew file mode 100644index 0000000000..55c9d5a02a--- /dev/null+++ b/backend/migrator/migration/3.11/0021##plan_check_run_status_index.sql@@ -0,0 +1,31 @@+-- Add partial index on plan_check_run for active statuses+--+-- This migration adds a partial index to optimize queries filtering for active plan check runs.+--+-- Background:+-- Plan check runs go through state transitions: RUNNING -> (DONE|FAILED|CANCELED)+-- Once in a terminal state (DONE, FAILED, CANCELED), status never changes again.+-- Most plan check runs are in terminal states, but the scheduler frequently queries for RUNNING checks.+--+-- Problem:+-- The plan check scheduler polls every 5 seconds with:+-- "SELECT ... FROM plan_check_run WHERE status = 'RUNNING' ORDER BY id ASC"+-- This was doing sequential scans with high cost because there was no index on status.+--+-- Solution:+-- Use a partial index that only covers RUNNING status, following the same pattern as task_run.+-- This is more efficient than a full index because:+-- - Smaller index size (only active checks, not all historical completed checks)+-- - Faster index maintenance (terminal states don't consume index space)+-- - Better cache efficiency (smaller index fits in memory)+-- - Matches the query pattern (scheduler filters for RUNNING status)+--+-- Example query optimized:+-- SELECT plan_check_run.id, ... FROM plan_check_run+-- WHERE plan_check_run.status = 'RUNNING'+-- ORDER BY plan_check_run.id ASC+--+-- The (status, id) index also helps with the ORDER BY id ASC clause.++CREATE INDEX idx_plan_check_run_active_status ON plan_check_run(status, id)+WHERE status = 'RUNNING';diff --git a/backend/migrator/migration/LATEST.sql b/backend/migrator/migration/LATEST.sqlindex a64a39c115..45c83dc4b6 100644--- a/backend/migrator/migration/LATEST.sql+++ b/backend/migrator/migration/LATEST.sql@@ -295,6 +295,8 @@ CREATE TABLE plan_check_run (CREATE INDEX idx_plan_check_run_plan_id ON plan_check_run (plan_id);+CREATE INDEX idx_plan_check_run_active_status ON plan_check_run(status, id) WHERE status = 'RUNNING';+ALTER SEQUENCE plan_check_run_id_seq RESTART WITH 101;-- Plan related ENDdiff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.goindex 93ef621a7e..5a5ab4de33 100644--- a/backend/migrator/migrator_test.go+++ b/backend/migrator/migrator_test.go@@ -12,7 +12,7 @@ import (func TestLatestVersion(t *testing.T) {files, err := getSortedVersionedFiles()require.NoError(t, err)- require.Equal(t, semver.MustParse("3.11.20"), *files[len(files)-1].version)+ require.Equal(t, semver.MustParse("3.11.21"), *files[len(files)-1].version)}func TestVersionUnique(t *testing.T) {