Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: add materialized view support to PostgreSQL walkthrough (#19248)
p0nyed23df1a7651
diff --git a/backend/plugin/schema/pg/testdata/walk_through.yaml b/backend/plugin/schema/pg/testdata/walk_through.yamlindex e41246db1c..96cf5490b3 100644--- a/backend/plugin/schema/pg/testdata/walk_through.yaml+++ b/backend/plugin/schema/pg/testdata/walk_through.yaml@@ -296,3 +296,26 @@: 2,\n \"nullable\": true,\n \"type\": \"varchar(20)\"\\n }\n ]\n }\n ]\n }\n ]\n}"advice: null+- statement: 'CREATE MATERIALIZED VIEW mv AS SELECT id, name FROM test;++ CREATE INDEX ON mv(id);'+ ignore_case_sensitive: false+ want: "{\n \"name\": \"postgres\",\n \"schemas\": [\n {\n \"name\": \"\+ public\",\n \"tables\": [\n {\n \"name\": \"test\",\n \+ \ \"columns\": [\n {\n \"name\": \"id\",\n \+ \ \"position\": 1,\n \"type\": \"int\"\n },\n\+ \ {\n \"name\": \"name\",\n \"position\"\+ : 2,\n \"nullable\": true,\n \"type\": \"varchar(20)\"\+ \n }\n ]\n }\n ],\n \"views\": [\n \+ \ {\n \"name\": \"v1\",\n \"definition\": \"SELECT id, name\+ \ FROM test\",\n \"dependencyColumns\": [\n {\n \+ \ \"schema\": \"public\",\n \"table\": \"test\",\n \+ \ \"column\": \"id\"\n },\n {\n \"schema\"\+ : \"public\",\n \"table\": \"test\",\n \"column\": \"\+ name\"\n }\n ]\n }\n ],\n \"materializedViews\"\+ : [\n {\n \"name\": \"mv\",\n \"definition\": \"SELECT\+ \ id, name FROM test\",\n \"indexes\": [\n {\n \+ \ \"name\": \"mv_id_idx\",\n \"expressions\": [\n \+ \ \"id\"\n ],\n \"type\": \"btree\"\n }\n\+ \ ]\n }\n ]\n }\n ]\n}"+ advice: nulldiff --git a/backend/plugin/schema/pg/walk_through.go b/backend/plugin/schema/pg/walk_through.goindex e226b31896..35db916876 100644--- a/backend/plugin/schema/pg/walk_through.go+++ b/backend/plugin/schema/pg/walk_through.go@@ -452,25 +452,27 @@ func (l *pgCatalogListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {return}l.currentLine = ctx.GetStart().GetLine()- // Extract relation (table) name+ // Extract relation (table/materialized view) namerelationExpr := ctx.Relation_expr()if relationExpr == nil || relationExpr.Qualified_name() == nil {return}- tableName := extractTableName(relationExpr.Qualified_name())+ relationName := extractTableName(relationExpr.Qualified_name())schemaName := extractSchemaName(relationExpr.Qualified_name())schema, err := getOrCreatePublicSchema(l.databaseState, schemaName, l.currentLine)if err != nil {l.advice = errreturn}- table := schema.GetTable(tableName)- if table == nil {+ // Try to find the relation as a table first, then as a materialized view+ table := schema.GetTable(relationName)+ matView := schema.GetMaterializedView(relationName)+ if table == nil && matView == nil {l.advice = &storepb.Advice{Status: storepb.Advice_ERROR,Code: code.TableNotExists.Int32(),- Title: fmt.Sprintf("Table `%s` does not exist", tableName),- Content: fmt.Sprintf("Table `%s` does not exist", tableName),+ Title: fmt.Sprintf("Table `%s` does not exist", relationName),+ Content: fmt.Sprintf("Table `%s` does not exist", relationName),StartPosition: &storepb.Position{Line: int32(l.currentLine)},}return@@ -500,8 +502,8 @@ func (l *pgCatalogListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {l.advice = &storepb.Advice{Status: storepb.Advice_ERROR,Code: code.IndexEmptyKeys.Int32(),- Title: fmt.Sprintf("Index %q in table %q has empty key", indexName, tableName),- Content: fmt.Sprintf("Index %q in table %q has empty key", indexName, tableName),+ Title: fmt.Sprintf("Index %q in table %q has empty key", indexName, relationName),+ Content: fmt.Sprintf("Index %q in table %q has empty key", indexName, relationName),StartPosition: &storepb.Position{Line: int32(l.currentLine)},}return@@ -510,41 +512,7 @@ func (l *pgCatalogListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {isUnique := ctx.Opt_unique() != nil && ctx.Opt_unique().UNIQUE() != nilwasAutoGenerated := indexName == ""if indexName == "" {- indexName = generateIndexName(tableName, columnList, isUnique)- }- // Check if index name already exists- if table.GetIndex(indexName) != nil {- if ifNotExists {- return- }- // If name was auto-generated, try with numeric suffix- if wasAutoGenerated {- indexName = generateUniqueIndexName(schema, tableName, columnList, isUnique)- } else {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.RelationExists.Int32(),- Title: fmt.Sprintf("Relation %q already exists in schema %q", indexName, schema.GetProto().Name),- Content: fmt.Sprintf("Relation %q already exists in schema %q", indexName, schema.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- }- // Check that all columns exist (skip expressions)- for _, colName := range columnList {- if colName != "expr" {- if table.GetColumn(colName) == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", colName, tableName),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", colName, tableName),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- }+ indexName = generateIndexName(relationName, columnList, isUnique)}// Determine index typeindexType := "btree" // default@@ -552,7 +520,7 @@ func (l *pgCatalogListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {method := pgparser.NormalizePostgreSQLName(ctx.Access_method_clause().Name())indexType = method}- // Create index+ // Create index - handle table and materialized view separatelyindex := &storepb.IndexMetadata{Name: indexName,Expressions: columnList,@@ -560,14 +528,79 @@ func (l *pgCatalogListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {Unique: isUnique,Primary: false,}- if err := table.CreateIndex(index); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},+ if table != nil {+ // Index on a table+ // Check if index name already exists+ if table.GetIndex(indexName) != nil {+ if ifNotExists {+ return+ }+ // If name was auto-generated, try with numeric suffix+ if wasAutoGenerated {+ indexName = generateUniqueIndexName(schema, relationName, columnList, isUnique)+ index.Name = indexName+ } else {+ l.advice = &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.RelationExists.Int32(),+ Title: fmt.Sprintf("Relation %q already exists in schema %q", indexName, schema.GetProto().Name),+ Content: fmt.Sprintf("Relation %q already exists in schema %q", indexName, schema.GetProto().Name),+ StartPosition: &storepb.Position{Line: int32(l.currentLine)},+ }+ return+ }+ }+ // Check that all columns exist (skip expressions)+ for _, colName := range columnList {+ if colName != "expr" {+ if table.GetColumn(colName) == nil {+ l.advice = &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.ColumnNotExists.Int32(),+ Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", colName, relationName),+ Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", colName, relationName),+ StartPosition: &storepb.Position{Line: int32(l.currentLine)},+ }+ return+ }+ }}+ if err := table.CreateIndex(index); err != nil {+ l.advice = &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.IndexExists.Int32(),+ Title: err.Error(),+ Content: err.Error(),+ StartPosition: &storepb.Position{Line: int32(l.currentLine)},+ }+ }+ } else {+ // Index on a materialized view+ // Check if index name already exists+ for _, existingIndex := range matView.Indexes {+ if existingIndex.Name == indexName {+ if ifNotExists {+ return+ }+ // If name was auto-generated, try with numeric suffix+ if wasAutoGenerated {+ indexName = generateUniqueIndexName(schema, relationName, columnList, isUnique)+ index.Name = indexName+ break+ }+ l.advice = &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.RelationExists.Int32(),+ Title: fmt.Sprintf("Relation %q already exists in schema %q", indexName, schema.GetProto().Name),+ Content: fmt.Sprintf("Relation %q already exists in schema %q", indexName, schema.GetProto().Name),+ StartPosition: &storepb.Position{Line: int32(l.currentLine)},+ }+ return+ }+ }+ // For materialized views, we don't have column metadata to validate+ // Just add the index to the materialized view+ matView.Indexes = append(matView.Indexes, index)}}@@ -626,6 +659,12 @@ func (l *pgCatalogListener) EnterAltertablestmt(ctx *parser.AltertablestmtContex}table := schema.GetTable(tableName)if table == nil {+ // Check if it's a materialized view - PostgreSQL allows some ALTER TABLE+ // commands on materialized views (e.g., OWNER TO). Skip processing for+ // materialized views since we don't track these changes in metadata.+ if schema.GetMaterializedView(tableName) != nil {+ return+ }l.advice = &storepb.Advice{Status: storepb.Advice_ERROR,Code: code.TableNotExists.Int32(),@@ -1185,6 +1224,16 @@ func (l *pgCatalogListener) EnterDropstmt(ctx *parser.DropstmtContext) {}}}+ } else if objType.MATERIALIZED() != nil && objType.VIEW() != nil {+ // DROP MATERIALIZED VIEW+ if ctx.Any_name_list() != nil {+ for _, anyName := range ctx.Any_name_list().AllAny_name() {+ if err := l.dropMaterializedView(anyName, ifExists); err != nil {+ l.advice = err+ return+ }+ }+ }} else if objType.VIEW() != nil {// DROP VIEWif ctx.Any_name_list() != nil {@@ -1350,6 +1399,42 @@ func (l *pgCatalogListener) dropView(anyName parser.IAny_nameContext, ifExists b}return nil}+func (l *pgCatalogListener) dropMaterializedView(anyName parser.IAny_nameContext, ifExists bool) *storepb.Advice {+ parts := pgparser.NormalizePostgreSQLAnyName(anyName)+ if len(parts) == 0 {+ return nil+ }+ var schemaName, viewName string+ switch len(parts) {+ case 1:+ viewName = parts[0]+ case 2:+ schemaName = parts[0]+ viewName = parts[1]+ default:+ return nil+ }+ schema, err := getOrCreatePublicSchema(l.databaseState, schemaName, l.currentLine)+ if err != nil {+ if ifExists {+ return nil+ }+ return err+ }+ // Try to drop the materialized view+ if dropErr := schema.DropMaterializedView(viewName); dropErr != nil {+ if !ifExists {+ return &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.ViewNotExists.Int32(),+ Title: dropErr.Error(),+ Content: dropErr.Error(),+ StartPosition: &storepb.Position{Line: int32(l.currentLine)},+ }+ }+ }+ return nil+}func (l *pgCatalogListener) dropIndex(anyName parser.IAny_nameContext, ifExists bool) *storepb.Advice {parts := pgparser.NormalizePostgreSQLAnyName(anyName)if len(parts) == 0 {@@ -1370,20 +1455,43 @@ func (l *pgCatalogListener) dropIndex(anyName parser.IAny_nameContext, ifExists}return err}- table, index, err := getIndexFromSchema(schema, indexName)- if err != nil {+ // Get the index - it could be on a table or materialized view+ index := schema.GetIndex(indexName)+ if index == nil {if ifExists {return nil}- return err+ return &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.IndexNotExists.Int32(),+ Title: fmt.Sprintf("Index %q does not exist in schema %q", indexName, schema.GetProto().Name),+ Content: fmt.Sprintf("Index %q does not exist in schema %q", indexName, schema.GetProto().Name),+ StartPosition: &storepb.Position{Line: int32(l.currentLine)},+ }+ }+ // Get the owner name (table or materialized view)+ ownerName := index.GetTableProto().Name+ // Try dropping from table first+ table := schema.GetTable(ownerName)+ if table != nil {+ if dropErr := table.DropIndex(indexName); dropErr != nil {+ return &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.IndexNotExists.Int32(),+ Title: dropErr.Error(),+ Content: dropErr.Error(),+ StartPosition: &storepb.Position{Line: int32(l.currentLine)},+ }+ }+ return nil}- // Drop the index from the table- if err := table.DropIndex(index.GetProto().Name); err != nil {+ // Try dropping from materialized view+ if dropErr := schema.DropMaterializedViewIndex(ownerName, indexName); dropErr != nil {return &storepb.Advice{Status: storepb.Advice_ERROR,Code: code.IndexNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),+ Title: dropErr.Error(),+ Content: dropErr.Error(),StartPosition: &storepb.Position{Line: int32(l.currentLine)},}}@@ -1650,6 +1758,72 @@ func (l *pgCatalogListener) EnterViewstmt(ctx *parser.ViewstmtContext) {}}+// ========================================+// CREATE MATERIALIZED VIEW handling+// ========================================+// EnterCreatematviewstmt handles CREATE MATERIALIZED VIEW statements.+func (l *pgCatalogListener) EnterCreatematviewstmt(ctx *parser.CreatematviewstmtContext) {+ if !isTopLevel(ctx.GetParent()) || l.advice != nil {+ return+ }+ l.currentLine = ctx.GetStart().GetLine()+ // Extract materialized view name from Create_mv_target+ if ctx.Create_mv_target() == nil || ctx.Create_mv_target().Qualified_name() == nil {+ return+ }+ viewName := extractTableName(ctx.Create_mv_target().Qualified_name())+ schemaName := extractSchemaName(ctx.Create_mv_target().Qualified_name())+ databaseName := extractDatabaseName(ctx.Create_mv_target().Qualified_name())+ // Check if accessing other database+ if databaseName != "" && l.databaseState.DatabaseName() != databaseName {+ l.advice = &storepb.Advice{+ Status: storepb.Advice_WARNING,+ Code: code.NotCurrentDatabase.Int32(),+ Title: fmt.Sprintf("Database %q is not the current database %q", databaseName, l.databaseState.DatabaseName()),+ Content: fmt.Sprintf("Database %q is not the current database %q", databaseName, l.databaseState.DatabaseName()),+ StartPosition: &storepb.Position{Line: int32(l.currentLine)},+ }+ return+ }+ schema, err := getOrCreatePublicSchema(l.databaseState, schemaName, l.currentLine)+ if err != nil {+ l.advice = err+ return+ }+ // Check IF NOT EXISTS clause+ ifNotExists := ctx.IF_P() != nil && ctx.NOT() != nil && ctx.EXISTS() != nil+ // Check if materialized view already exists+ if schema.GetMaterializedView(viewName) != nil {+ if ifNotExists {+ return+ }+ l.advice = &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.TableExists.Int32(),+ Title: fmt.Sprintf("Materialized view %q already exists in schema %q", viewName, schema.GetProto().Name),+ Content: fmt.Sprintf("Materialized view %q already exists in schema %q", viewName, schema.GetProto().Name),+ StartPosition: &storepb.Position{Line: int32(l.currentLine)},+ }+ return+ }+ // Get the view definition (the SELECT statement)+ definition := ""+ if ctx.Selectstmt() != nil {+ definition = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx.Selectstmt())+ }+ // Create the materialized view+ _, createErr := schema.CreateMaterializedView(viewName, definition)+ if createErr != nil {+ l.advice = &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.TableExists.Int32(),+ Title: createErr.Error(),+ Content: createErr.Error(),+ StartPosition: &storepb.Position{Line: int32(l.currentLine)},+ }+ }+}+// ========================================// Helper functions// ========================================diff --git a/backend/store/model/database.go b/backend/store/model/database.goindex 3a50108002..65cf0e10e6 100644--- a/backend/store/model/database.go+++ b/backend/store/model/database.go@@ -323,6 +323,20 @@ func (s *SchemaMetadata) GetIndex(name string) *IndexMetadata {return index}}+ // Also search in materialized views+ nameID := normalizeNameByCaseSensitivity(name, s.isObjectCaseSensitive)+ for _, mv := range s.internalMaterializedView {+ for _, idx := range mv.Indexes {+ idxID := normalizeNameByCaseSensitivity(idx.Name, s.isObjectCaseSensitive)+ if idxID == nameID {+ // Return a wrapper IndexMetadata for the materialized view index+ return &IndexMetadata{+ proto: idx,+ tableProto: &storepb.TableMetadata{Name: mv.Name},+ }+ }+ }+ }return nil}@@ -652,6 +666,92 @@ func (s *SchemaMetadata) RenameView(oldName string, newName string) error {return nil}+// CreateMaterializedView creates a new materialized view in the schema.+// Returns an error if the materialized view already exists.+func (s *SchemaMetadata) CreateMaterializedView(viewName string, definition string) (*storepb.MaterializedViewMetadata, error) {+ // Check if materialized view already exists+ if s.GetMaterializedView(viewName) != nil {+ return nil, errors.Errorf("materialized view %q already exists in schema %q", viewName, s.proto.Name)+ }++ // Create new materialized view proto+ newViewProto := &storepb.MaterializedViewMetadata{+ Name: viewName,+ Definition: definition,+ }++ // Add to proto's materialized view list+ s.proto.MaterializedViews = append(s.proto.MaterializedViews, newViewProto)++ // Add to internal map+ viewID := normalizeNameByCaseSensitivity(viewName, s.isObjectCaseSensitive)+ s.internalMaterializedView[viewID] = newViewProto++ return newViewProto, nil+}++// DropMaterializedView drops a materialized view from the schema.+// Returns an error if the materialized view does not exist.+func (s *SchemaMetadata) DropMaterializedView(viewName string) error {+ // Check if materialized view exists+ if s.GetMaterializedView(viewName) == nil {+ return errors.Errorf("materialized view %q does not exist in schema %q", viewName, s.proto.Name)+ }++ // Remove from internal map+ viewID := normalizeNameByCaseSensitivity(viewName, s.isObjectCaseSensitive)+ delete(s.internalMaterializedView, viewID)++ // Remove from proto's materialized view list+ newViews := make([]*storepb.MaterializedViewMetadata, 0, len(s.proto.MaterializedViews)-1)+ for _, view := range s.proto.MaterializedViews {+ if s.isObjectCaseSensitive {+ if view.Name != viewName {+ newViews = append(newViews, view)+ }+ } else {+ if !strings.EqualFold(view.Name, viewName) {+ newViews = append(newViews, view)+ }+ }+ }+ s.proto.MaterializedViews = newViews++ return nil+}++// DropMaterializedViewIndex drops an index from a materialized view.+func (s *SchemaMetadata) DropMaterializedViewIndex(viewName, indexName string) error {+ mv := s.GetMaterializedView(viewName)+ if mv == nil {+ return errors.Errorf("materialized view %q does not exist in schema %q", viewName, s.proto.Name)+ }++ // Remove from indexes+ newIndexes := make([]*storepb.IndexMetadata, 0, len(mv.Indexes))+ found := false+ for _, idx := range mv.Indexes {+ if s.isObjectCaseSensitive {+ if idx.Name != indexName {+ newIndexes = append(newIndexes, idx)+ } else {+ found = true+ }+ } else {+ if !strings.EqualFold(idx.Name, indexName) {+ newIndexes = append(newIndexes, idx)+ } else {+ found = true+ }+ }+ }+ if !found {+ return errors.Errorf("index %q does not exist on materialized view %q", indexName, viewName)+ }+ mv.Indexes = newIndexes+ return nil+}+// GetDependentViews returns all views that depend on the given table and column.// This is used to check if a column can be dropped or if a table can be dropped.func (s *SchemaMetadata) GetDependentViews(tableName string, columnName string) []string {