Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(pg): preserve INCLUDE clause in covering index export (#19313)

Vincent Huangc0908502dcf4
diff --git a/backend/plugin/schema/pg/get_database_definition.go b/backend/plugin/schema/pg/get_database_definition.go
index 74f658389d..b6b5c6cbba 100644
--- a/backend/plugin/schema/pg/get_database_definition.go
+++ b/backend/plugin/schema/pg/get_database_definition.go
@@ -3223,12 +3223,16 @@ func writeIndexInternal(out io.Writer, schema string, table string, index *store
return err
}
- // Write WHERE clause for partial indexes.
- // The WHERE clause is stored in the full index definition (from pg_get_indexdef).
+ // Write INCLUDE and WHERE clauses from the full index definition (from pg_get_indexdef).
+ // SQL syntax order: (keys) INCLUDE (cols) WHERE (predicate)
if index.Definition != "" {
comparer := &PostgreSQLIndexComparer{}
- whereClause := comparer.ExtractWhereClauseFromIndexDef(index.Definition)
- if whereClause != "" {
+ if includeClause := comparer.ExtractIncludeClauseFromIndexDef(index.Definition); includeClause != "" {
+ if _, err := fmt.Fprintf(out, " %s", includeClause); err != nil {
+ return err
+ }
+ }
+ if whereClause := comparer.ExtractWhereClauseFromIndexDef(index.Definition); whereClause != "" {
if _, err := fmt.Fprintf(out, " WHERE %s", whereClause); err != nil {
return err
}
diff --git a/backend/plugin/schema/pg/index_comparer.go b/backend/plugin/schema/pg/index_comparer.go
index 458b0c5f8d..a59527aace 100644
--- a/backend/plugin/schema/pg/index_comparer.go
+++ b/backend/plugin/schema/pg/index_comparer.go
@@ -141,6 +141,90 @@ func (*indexWhereExtractor) getTextFromContext(ctx antlr.ParserRuleContext) stri
return input.GetText(startIndex, stopIndex)
}
+// ExtractIncludeClauseFromIndexDef extracts the INCLUDE clause from a CREATE INDEX statement using ANTLR AST parsing.
+// Returns the full "INCLUDE (col1, col2, ...)" string, or empty if no INCLUDE clause exists.
+func (*PostgreSQLIndexComparer) ExtractIncludeClauseFromIndexDef(definition string) string {
+ if definition == "" {
+ return ""
+ }
+
+ inputStream := antlr.NewInputStream(definition)
+ lexer := pgparser.NewPostgreSQLLexer(inputStream)
+ stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
+ parser := pgparser.NewPostgreSQLParser(stream)
+ parser.RemoveErrorListeners()
+
+ tree := parser.Root()
+
+ extractor := &indexIncludeExtractor{}
+ extractor.Visit(tree)
+
+ return extractor.includeClause
+}
+
+// indexIncludeExtractor is a visitor that extracts INCLUDE clauses from CREATE INDEX statements.
+type indexIncludeExtractor struct {
+ *pgparser.BasePostgreSQLParserVisitor
+ includeClause string
+}
+
+// Visit implements the visitor pattern to walk the AST.
+func (e *indexIncludeExtractor) Visit(tree antlr.ParseTree) {
+ switch ctx := tree.(type) {
+ case *pgparser.IndexstmtContext:
+ e.visitIndexStmt(ctx)
+ default:
+ if tree != nil {
+ for i := 0; i < tree.GetChildCount(); i++ {
+ child := tree.GetChild(i)
+ if parseTree, ok := child.(antlr.ParseTree); ok {
+ e.Visit(parseTree)
+ }
+ }
+ }
+ }
+}
+
+func (e *indexIncludeExtractor) visitIndexStmt(ctx *pgparser.IndexstmtContext) {
+ if ctx == nil {
+ return
+ }
+
+ optInclude := ctx.Opt_include()
+ if optInclude == nil {
+ return
+ }
+
+ // Get the full text of the INCLUDE clause: "INCLUDE (col1, col2, ...)"
+ e.includeClause = e.getTextFromContext(optInclude)
+}
+
+func (*indexIncludeExtractor) getTextFromContext(ctx antlr.ParserRuleContext) string {
+ if ctx == nil {
+ return ""
+ }
+
+ start := ctx.GetStart()
+ stop := ctx.GetStop()
+ if start == nil || stop == nil {
+ return ""
+ }
+
+ input := start.GetInputStream()
+ if input == nil {
+ return ""
+ }
+
+ startIndex := start.GetStart()
+ stopIndex := stop.GetStop()
+
+ if startIndex < 0 || stopIndex < 0 || stopIndex < startIndex {
+ return ""
+ }
+
+ return input.GetText(startIndex, stopIndex)
+}
+
// IndexDefinition represents the parsed structure of a CREATE INDEX statement.
type IndexDefinition struct {
IndexName string
diff --git a/backend/plugin/schema/pg/index_comparer_test.go b/backend/plugin/schema/pg/index_comparer_test.go
index ffa5430124..8b240462e4 100644
--- a/backend/plugin/schema/pg/index_comparer_test.go
+++ b/backend/plugin/schema/pg/index_comparer_test.go
@@ -100,6 +100,51 @@ func TestPostgreSQLIndexComparer_CompareIndexWhereConditions(t *testing.T) {
}
}
+func TestPostgreSQLIndexComparer_ExtractIncludeClauseFromIndexDef(t *testing.T) {
+ comparer := &PostgreSQLIndexComparer{}
+
+ tests := []struct {
+ name string
+ indexDef string
+ expectedInclude string
+ }{
+ {
+ name: "Index with INCLUDE clause",
+ indexDef: "CREATE INDEX idx_covering ON products (price) INCLUDE (name, description)",
+ expectedInclude: "INCLUDE (name, description)",
+ },
+ {
+ name: "Index with INCLUDE and WHERE clause",
+ indexDef: "CREATE INDEX idx_covering ON orders (status) INCLUDE (total, customer_name) WHERE (status = 'active'::text)",
+ expectedInclude: "INCLUDE (total, customer_name)",
+ },
+ {
+ name: "Index without INCLUDE clause",
+ indexDef: "CREATE INDEX idx_simple ON users (email)",
+ expectedInclude: "",
+ },
+ {
+ name: "UNIQUE index with INCLUDE",
+ indexDef: "CREATE UNIQUE INDEX idx_unique_covering ON products (id) INCLUDE (name)",
+ expectedInclude: "INCLUDE (name)",
+ },
+ {
+ name: "Empty definition",
+ indexDef: "",
+ expectedInclude: "",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ result := comparer.ExtractIncludeClauseFromIndexDef(tt.indexDef)
+ if result != tt.expectedInclude {
+ t.Errorf("ExtractIncludeClauseFromIndexDef() = %q, want %q", result, tt.expectedInclude)
+ }
+ })
+ }
+}
+
func TestPostgreSQLIndexComparer_ParseIndexDefinition(t *testing.T) {
comparer := &PostgreSQLIndexComparer{}
diff --git a/backend/plugin/schema/pg/standalone_index_test.go b/backend/plugin/schema/pg/standalone_index_test.go
index 2ff24b1e4e..e2c2d84649 100644
--- a/backend/plugin/schema/pg/standalone_index_test.go
+++ b/backend/plugin/schema/pg/standalone_index_test.go
@@ -1,6 +1,7 @@
package pg
import (
+ "strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -632,3 +633,84 @@ func TestPartitionedTableIndexUsesOnOnly(t *testing.T) {
require.Contains(t, sdl, `CREATE INDEX "idx_orders_customer_id"`)
require.Contains(t, sdl, `ON ONLY "public"."orders"`)
}
+
+// TestCoveringIndexIncludeClauseInSDLOutput tests that covering index INCLUDE clauses
+// are preserved in SDL output (regression test for INCLUDE clause being dropped).
+func TestCoveringIndexIncludeClauseInSDLOutput(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "products",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer", Position: 1},
+ {Name: "name", Type: "text", Position: 2},
+ {Name: "description", Type: "text", Position: 3},
+ {Name: "price", Type: "numeric(10,2)", Position: 4},
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "idx_products_price_covering",
+ Expressions: []string{"price"},
+ Type: "btree",
+ Definition: "CREATE INDEX idx_products_price_covering ON public.products USING btree (price) INCLUDE (name, description);",
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ sdl, err := getSDLFormat(metadata)
+ require.NoError(t, err)
+
+ require.Contains(t, sdl, "INCLUDE (name, description)")
+ require.Contains(t, sdl, `ON "public"."products"`)
+ require.NotContains(t, sdl, "ON ONLY")
+}
+
+// TestCoveringIndexWithWhereClause tests that an index with both INCLUDE and WHERE
+// clauses preserves both in the correct SQL order.
+func TestCoveringIndexWithWhereClause(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer", Position: 1},
+ {Name: "status", Type: "text", Position: 2},
+ {Name: "total", Type: "numeric", Position: 3},
+ {Name: "customer_name", Type: "text", Position: 4},
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "idx_orders_active_covering",
+ Expressions: []string{"status"},
+ Type: "btree",
+ Definition: "CREATE INDEX idx_orders_active_covering ON public.orders USING btree (status) INCLUDE (total, customer_name) WHERE (status = 'active'::text);",
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ sdl, err := getSDLFormat(metadata)
+ require.NoError(t, err)
+
+ require.Contains(t, sdl, "INCLUDE (total, customer_name)")
+ require.Contains(t, sdl, "WHERE")
+ require.Contains(t, sdl, "active")
+
+ // Verify correct order: INCLUDE before WHERE
+ includePos := strings.Index(sdl, "INCLUDE")
+ wherePos := strings.Index(sdl, "WHERE")
+ require.Greater(t, wherePos, includePos, "INCLUDE should come before WHERE in SQL syntax")
+}