diff --git a/internal/plan/rewrite.go b/internal/plan/rewrite.go index ae299858..6efd0b26 100644 --- a/internal/plan/rewrite.go +++ b/internal/plan/rewrite.go @@ -440,18 +440,24 @@ func generateIndexSQL(index *ir.Index, isConcurrent bool) string { var columnParts []string for _, col := range index.Columns { part := col.Name - if col.Direction != "" && col.Direction != "ASC" { - part += " " + col.Direction - } if col.Operator != "" { part += " " + col.Operator } + if col.Direction != "" && col.Direction != "ASC" { + part += " " + col.Direction + } columnParts = append(columnParts, part) } sql.WriteString(joinStrings(columnParts, ", ")) sql.WriteString(")") + if len(index.IncludeColumns) > 0 { + sql.WriteString(" INCLUDE (") + sql.WriteString(strings.Join(index.IncludeColumns, ", ")) + sql.WriteString(")") + } + if index.NullsNotDistinct && index.Type == ir.IndexTypeUnique { sql.WriteString(" NULLS NOT DISTINCT") } diff --git a/testdata/diff/online/add_composite_index/diff.sql b/testdata/diff/online/add_composite_index/diff.sql index 2419bade..68155862 100644 --- a/testdata/diff/online/add_composite_index/diff.sql +++ b/testdata/diff/online/add_composite_index/diff.sql @@ -1 +1,3 @@ +CREATE INDEX IF NOT EXISTS idx_users_email_include ON users (email) INCLUDE (username); + CREATE INDEX IF NOT EXISTS idx_users_email_status ON users (email, status DESC); diff --git a/testdata/diff/online/add_composite_index/new.sql b/testdata/diff/online/add_composite_index/new.sql index e2dd8fdb..1882697a 100644 --- a/testdata/diff/online/add_composite_index/new.sql +++ b/testdata/diff/online/add_composite_index/new.sql @@ -6,4 +6,6 @@ CREATE TABLE public.users ( status text ); -CREATE INDEX idx_users_email_status ON public.users (email, status DESC); \ No newline at end of file +CREATE INDEX idx_users_email_status ON public.users (email, status DESC); + +CREATE INDEX idx_users_email_include ON public.users (email) INCLUDE (username); \ No newline at end of file diff --git a/testdata/diff/online/add_composite_index/plan.json b/testdata/diff/online/add_composite_index/plan.json index aff9eabc..bdff1f5c 100644 --- a/testdata/diff/online/add_composite_index/plan.json +++ b/testdata/diff/online/add_composite_index/plan.json @@ -6,6 +6,30 @@ "hash": "4174f5cda975543aad9cf30ba2b3a2bd5ace1e79185c119db587df0795195c33" }, "groups": [ + { + "steps": [ + { + "sql": "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_users_email_include ON users (email) INCLUDE (username);", + "type": "table.index", + "operation": "create", + "path": "public.users.idx_users_email_include" + } + ] + }, + { + "steps": [ + { + "sql": "SELECT \n COALESCE(i.indisvalid, false) as done,\n CASE \n WHEN p.blocks_total > 0 THEN p.blocks_done * 100 / p.blocks_total\n ELSE 0\n END as progress\nFROM pg_class c\nLEFT JOIN pg_index i ON c.oid = i.indexrelid\nLEFT JOIN pg_stat_progress_create_index p ON c.oid = p.index_relid\nWHERE c.relname = 'idx_users_email_include';", + "directive": { + "type": "wait", + "message": "Creating index idx_users_email_include" + }, + "type": "table.index", + "operation": "create", + "path": "public.users.idx_users_email_include" + } + ] + }, { "steps": [ { diff --git a/testdata/diff/online/add_composite_index/plan.sql b/testdata/diff/online/add_composite_index/plan.sql index 037529e6..9d42d6f1 100644 --- a/testdata/diff/online/add_composite_index/plan.sql +++ b/testdata/diff/online/add_composite_index/plan.sql @@ -1,3 +1,17 @@ +CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_users_email_include ON users (email) INCLUDE (username); + +-- pgschema:wait +SELECT + COALESCE(i.indisvalid, false) as done, + CASE + WHEN p.blocks_total > 0 THEN p.blocks_done * 100 / p.blocks_total + ELSE 0 + END as progress +FROM pg_class c +LEFT JOIN pg_index i ON c.oid = i.indexrelid +LEFT JOIN pg_stat_progress_create_index p ON c.oid = p.index_relid +WHERE c.relname = 'idx_users_email_include'; + CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_users_email_status ON users (email, status DESC); -- pgschema:wait diff --git a/testdata/diff/online/add_composite_index/plan.txt b/testdata/diff/online/add_composite_index/plan.txt index 2b0d7b11..0f82758e 100644 --- a/testdata/diff/online/add_composite_index/plan.txt +++ b/testdata/diff/online/add_composite_index/plan.txt @@ -5,13 +5,14 @@ Summary by type: Tables: ~ users + + idx_users_email_include (index) + idx_users_email_status (index) DDL to be executed: -------------------------------------------------- -- Transaction Group #1 -CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_users_email_status ON users (email, status DESC); +CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_users_email_include ON users (email) INCLUDE (username); -- Transaction Group #2 -- pgschema:wait @@ -24,4 +25,20 @@ SELECT FROM pg_class c LEFT JOIN pg_index i ON c.oid = i.indexrelid LEFT JOIN pg_stat_progress_create_index p ON c.oid = p.index_relid -WHERE c.relname = 'idx_users_email_status'; +WHERE c.relname = 'idx_users_email_include'; + +-- Transaction Group #3 +CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_users_email_status ON users (email, status DESC); + +-- Transaction Group #4 +-- pgschema:wait +SELECT + COALESCE(i.indisvalid, false) as done, + CASE + WHEN p.blocks_total > 0 THEN p.blocks_done * 100 / p.blocks_total + ELSE 0 + END as progress +FROM pg_class c +LEFT JOIN pg_index i ON c.oid = i.indexrelid +LEFT JOIN pg_stat_progress_create_index p ON c.oid = p.index_relid +WHERE c.relname = 'idx_users_email_status'; \ No newline at end of file