Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(pg): install composite types in walk-through and query-span catalogs (#20913)
h3n4l584760129fd8
diff --git a/backend/plugin/parser/pg/query_span_composite_test.go b/backend/plugin/parser/pg/query_span_composite_test.gonew file mode 100644index 0000000000..521bb3b1dd--- /dev/null+++ b/backend/plugin/parser/pg/query_span_composite_test.go@@ -0,0 +1,183 @@+package pg++import (+ "context"+ "testing"++ "github.com/bytebase/omni/pg/ast"+ "github.com/bytebase/omni/pg/catalog"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+)++// TestLoaderInstallsCompositeTypes proves the query-span catalog loader+// installs composite types from metadata with real attributes, in dependency+// order (aa_nested sorts before its dependency zz_base alphabetically), and+// that tables typed with them install cleanly.+func TestLoaderInstallsCompositeTypes(t *testing.T) {+ meta := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ EnumTypes: []*storepb.EnumTypeMetadata{+ {Name: "status", Values: []string{"a", "b"}},+ },+ CompositeTypes: []*storepb.CompositeTypeMetadata{+ {+ // References zz_base only through an array suffix.+ Name: "aa_array_only",+ Attributes: []*storepb.CompositeTypeAttribute{+ {Name: "items", Type: "public.zz_base[]"},+ },+ },+ {+ Name: "aa_nested",+ Attributes: []*storepb.CompositeTypeAttribute{+ {Name: "home", Type: "public.zz_base"},+ {Name: "s", Type: "public.status"},+ },+ },+ {+ Name: "zz_base",+ Attributes: []*storepb.CompositeTypeAttribute{+ {Name: "street", Type: "text"},+ {Name: "city", Type: "character varying(50)"},+ },+ },+ },+ Tables: []*storepb.TableMetadata{+ {+ Name: "users",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer"},+ {Name: "home", Type: "public.zz_base", Nullable: true},+ },+ },+ },+ },+ },+ }++ cat := catalog.New()+ cat.SetSearchPath([]string{"public"})+ loader := newCatalogLoader(cat, meta)+ if err := loader.Load(context.Background()); err != nil {+ t.Fatalf("Load: %v", err)+ }++ base := cat.GetRelation("public", "zz_base")+ if base == nil {+ t.Fatal("composite type zz_base not installed")+ }+ if len(base.Columns) != 2 {+ t.Errorf("zz_base attribute count: got %d, want 2", len(base.Columns))+ }+ if base.Columns[0].Name != "street" {+ t.Errorf("zz_base first attribute: got %q, want street", base.Columns[0].Name)+ }+ nested := cat.GetRelation("public", "aa_nested")+ if nested == nil {+ t.Fatal("composite type aa_nested not installed")+ }+ if len(nested.Columns) != 2 {+ t.Errorf("aa_nested attribute count: got %d, want 2", len(nested.Columns))+ }+ arrayOnly := cat.GetRelation("public", "aa_array_only")+ if arrayOnly == nil {+ t.Fatal("composite type aa_array_only not installed")+ }+ if len(arrayOnly.Columns) != 1 || arrayOnly.Columns[0].Name != "items" {+ t.Errorf("aa_array_only must install with its real attribute, got %d columns", len(arrayOnly.Columns))+ }+ if cat.GetRelation("public", "users") == nil {+ t.Fatal("table with composite column not installed")+ }+}++// TestLoaderCompositeFallbackPreservesAttributeNames forces the pseudo+// fallback via a domain-typed attribute (domains are not loader objects) and+// asserts the fallback keeps the metadata attribute names.+func TestLoaderCompositeFallbackPreservesAttributeNames(t *testing.T) {+ meta := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ CompositeTypes: []*storepb.CompositeTypeMetadata{+ {+ Name: "with_domain",+ Attributes: []*storepb.CompositeTypeAttribute{+ {Name: "p", Type: "public.pos_int"},+ {Name: "note", Type: "text"},+ },+ },+ },+ },+ },+ }++ cat := catalog.New()+ cat.SetSearchPath([]string{"public"})+ loader := newCatalogLoader(cat, meta)+ if err := loader.Load(context.Background()); err != nil {+ t.Fatalf("Load: %v", err)+ }++ rel := cat.GetRelation("public", "with_domain")+ if rel == nil {+ t.Fatal("composite must fall back, not vanish")+ }+ if len(rel.Columns) != 2 {+ t.Fatalf("fallback must preserve attribute names, got %d columns", len(rel.Columns))+ }+ if rel.Columns[0].Name != "p" || rel.Columns[1].Name != "note" {+ t.Errorf("fallback attribute names: got %q, %q", rel.Columns[0].Name, rel.Columns[1].Name)+ }+}++// TestRangeVarFallbackSkipsCompositeTypes proves the query-span fallback does+// not expand a standalone composite type as a FROM source — PostgreSQL+// rejects composite types as table sources.+func TestRangeVarFallbackSkipsCompositeTypes(t *testing.T) {+ meta := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ CompositeTypes: []*storepb.CompositeTypeMetadata{+ {+ Name: "addr",+ Attributes: []*storepb.CompositeTypeAttribute{+ {Name: "street", Type: "text"},+ },+ },+ },+ Tables: []*storepb.TableMetadata{+ {+ Name: "t",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer"},+ },+ },+ },+ },+ },+ }++ cat := catalog.New()+ cat.SetSearchPath([]string{"public"})+ loader := newCatalogLoader(cat, meta)+ if err := loader.Load(context.Background()); err != nil {+ t.Fatalf("Load: %v", err)+ }++ e := &omniQuerySpanExtractor{+ cat: cat,+ searchPath: []string{"public"},+ defaultDatabase: "db",+ }+ if results := e.extractColumnsFromRangeVar(&ast.RangeVar{Schemaname: "public", Relname: "addr"}); results != nil {+ t.Errorf("composite type must not expand as a FROM source, got %d columns", len(results))+ }+ if results := e.extractColumnsFromRangeVar(&ast.RangeVar{Schemaname: "public", Relname: "t"}); len(results) != 1 {+ t.Errorf("real table must still expand, got %d columns", len(results))+ }+}diff --git a/backend/plugin/parser/pg/query_span_loader.go b/backend/plugin/parser/pg/query_span_loader.goindex 0272365635..900e5acda6 100644--- a/backend/plugin/parser/pg/query_span_loader.go+++ b/backend/plugin/parser/pg/query_span_loader.go@@ -55,6 +55,7 @@ type objectKind intconst (kindSchema objectKind = iotakindEnum+ kindCompositekindTablekindViewkindMatView@@ -71,11 +72,12 @@ type objectEntry struct {name string// Exactly one of the following is populated based on kind.- enumMeta *storepb.EnumTypeMetadata- tableMeta *storepb.TableMetadata- viewMeta *storepb.ViewMetadata- matViewMeta *storepb.MaterializedViewMetadata- funcMeta *storepb.FunctionMetadata+ enumMeta *storepb.EnumTypeMetadata+ compositeMeta *storepb.CompositeTypeMetadata+ tableMeta *storepb.TableMetadata+ viewMeta *storepb.ViewMetadata+ matViewMeta *storepb.MaterializedViewMetadata+ funcMeta *storepb.FunctionMetadata}// key returns a canonical kind-prefixed identifier used for dependency@@ -89,7 +91,7 @@ func (e *objectEntry) key() string {switch e.kind {case kindSchema:return "schema:" + e.schema- case kindEnum:+ case kindEnum, kindComposite:return "type:" + e.schema + "." + e.namecase kindTable, kindView, kindMatView:return "rel:" + e.schema + "." + e.name@@ -130,6 +132,8 @@ func kindLabel(k objectKind) string {return "0schema"case kindEnum:return "1enum"+ case kindComposite:+ return "1composite"case kindTable:return "2table"case kindView:@@ -205,6 +209,14 @@ func (l *catalogLoader) collectObjects() []*objectEntry {enumMeta: enum,})}+ for _, composite := range sm.CompositeTypes {+ out = append(out, &objectEntry{+ kind: kindComposite,+ schema: sm.Name,+ name: composite.Name,+ compositeMeta: composite,+ })+ }for _, tbl := range sm.Tables {out = append(out, &objectEntry{kind: kindTable,@@ -378,6 +390,18 @@ func buildDependencyEdges(objects []*objectEntry, index map[string]*objectEntry)}}}+ case kindComposite:+ for _, attribute := range obj.compositeMeta.Attributes {+ for _, ref := range extractUserTypeRefs(attribute.Type) {+ if k := typeRefKey(ref); index[k] != nil {+ deps = append(deps, k)+ }+ // An attribute may be typed with a table/view row type.+ if k := relationKey(ref.Schema, ref.Name); index[k] != nil {+ deps = append(deps, k)+ }+ }+ }case kindSchema, kindEnum:// Schemas have no outgoing edges (they are the top of the graph).// Enums depend only on their schema, already captured above.@@ -562,6 +586,12 @@ func (l *catalogLoader) installReal(obj *objectEntry) error {return errcase kindEnum:return l.cat.DefineEnum(buildCreateEnumStmt(obj.schema, obj.enumMeta))+ case kindComposite:+ stmt, err := buildCompositeTypeStmt(obj.schema, obj.compositeMeta)+ if err != nil {+ return err+ }+ return l.cat.DefineCompositeType(stmt)case kindTable:stmt, err := buildCreateStmt(obj.schema, obj.tableMeta)if err != nil {@@ -602,6 +632,27 @@ func (l *catalogLoader) installPseudo(obj *objectEntry) error {return errors.New("schema has no pseudo form")case kindEnum:return l.cat.DefineEnum(pseudoCreateEnumStmt(obj.schema, obj.name))+ case kindComposite:+ // Name-preserving, text-backed fallback when metadata attributes are+ // known, so (col).field access still resolves; the "_broken" shape+ // remains for composites with no attribute information at all.+ if len(obj.compositeMeta.GetAttributes()) > 0 {+ items := make([]ast.Node, 0, len(obj.compositeMeta.Attributes))+ for _, attribute := range obj.compositeMeta.Attributes {+ if attribute.Name == "" {+ continue+ }+ items = append(items, &ast.ColumnDef{+ Colname: attribute.Name,+ TypeName: pseudoTextTypeName(),+ })+ }+ return l.cat.DefineCompositeType(&ast.CompositeTypeStmt{+ Typevar: &ast.RangeVar{Schemaname: obj.schema, Relname: obj.name},+ Coldeflist: &ast.List{Items: items},+ })+ }+ return l.cat.DefineCompositeType(pseudoCompositeTypeStmt(obj.schema, obj.name))case kindTable:cols := columnNamesFromTableMetadata(obj.tableMeta)return l.cat.DefineRelation(pseudoCreateTableStmt(obj.schema, obj.name, cols), 'r')diff --git a/backend/plugin/parser/pg/query_span_loader_builders.go b/backend/plugin/parser/pg/query_span_loader_builders.goindex dc71d55dd0..988f75d961 100644--- a/backend/plugin/parser/pg/query_span_loader_builders.go+++ b/backend/plugin/parser/pg/query_span_loader_builders.go@@ -28,6 +28,34 @@ func buildCreateEnumStmt(schema string, enum *storepb.EnumTypeMetadata) *ast.Cre}}+// buildCompositeTypeStmt translates CompositeTypeMetadata + schema into a+// CompositeTypeStmt. Attribute types are parsed via typeNameFromString; an+// error on any attribute propagates so the loader can pseudo the whole type.+func buildCompositeTypeStmt(schema string, composite *storepb.CompositeTypeMetadata) (*ast.CompositeTypeStmt, error) {+ cols := make([]ast.Node, 0, len(composite.Attributes))+ for _, attribute := range composite.Attributes {+ if attribute.Name == "" {+ continue+ }+ if attribute.Type == "" {+ return nil, errors.Errorf("attribute %q: empty type", attribute.Name)+ }+ tn, err := typeNameFromString(attribute.Type)+ if err != nil {+ return nil, errors.Wrapf(err, "attribute %q", attribute.Name)+ }+ cols = append(cols, &ast.ColumnDef{+ Colname: attribute.Name,+ TypeName: tn,+ CollClause: qsCollateClauseFromReference(attribute.Collation),+ })+ }+ return &ast.CompositeTypeStmt{+ Typevar: &ast.RangeVar{Schemaname: schema, Relname: composite.Name},+ Coldeflist: &ast.List{Items: cols},+ }, nil+}+// buildCreateStmt translates TableMetadata into a CreateStmt with real// column types. Column types are parsed via typeNameFromString; an error on// any single column propagates back so the loader can pseudo the whole@@ -243,3 +271,21 @@ func splitTopLevelCommas(s string) []string {parts = append(parts, strings.TrimSpace(s[start:]))return parts}++// qsCollateClauseFromReference converts a stored emit-ready collation+// reference (quoted as needed, schema-qualified outside pg_catalog) into an+// AST collate clause. Returns nil for no collation.+func qsCollateClauseFromReference(reference string) *ast.CollateClause {+ if reference == "" {+ return nil+ }+ var items []ast.Node+ if schemaName, collationName, ok := splitQualifiedName(reference); ok {+ items = []ast.Node{&ast.String{Str: schemaName}, &ast.String{Str: collationName}}+ } else {+ name := strings.Trim(reference, `"`)+ name = strings.ReplaceAll(name, `""`, `"`)+ items = []ast.Node{&ast.String{Str: name}}+ }+ return &ast.CollateClause{Collname: &ast.List{Items: items}}+}diff --git a/backend/plugin/parser/pg/query_span_loader_pseudo.go b/backend/plugin/parser/pg/query_span_loader_pseudo.goindex 90a8b2fd50..17d4d4109f 100644--- a/backend/plugin/parser/pg/query_span_loader_pseudo.go+++ b/backend/plugin/parser/pg/query_span_loader_pseudo.go@@ -48,9 +48,10 @@ func pseudoCreateDomainStmt(schema, name string) *ast.CreateDomainStmt {}// pseudoCompositeTypeStmt builds a composite type with a single text field-// named "_broken". Without CompositeTypeMetadata in storepb we cannot-// preserve real field names, so (col).field access against a pseudo composite-// will fall through to extractFallbackColumns.+// named "_broken". It is the fallback when a composite type's real+// definition is absent from metadata or fails to install, so (col).field+// access against a pseudo composite will fall through to+// extractFallbackColumns.func pseudoCompositeTypeStmt(schema, name string) *ast.CompositeTypeStmt {return &ast.CompositeTypeStmt{Typevar: &ast.RangeVar{diff --git a/backend/plugin/parser/pg/query_span_loader_type_name.go b/backend/plugin/parser/pg/query_span_loader_type_name.goindex 8e85623c19..191502df5a 100644--- a/backend/plugin/parser/pg/query_span_loader_type_name.go+++ b/backend/plugin/parser/pg/query_span_loader_type_name.go@@ -91,6 +91,12 @@ func extractUserTypeRefs(typeStr string) []UserTypeRef {// the caller's job to split.func stripTypeModifiers(typeStr string) string {s := strings.TrimSpace(typeStr)+ // Array suffixes wrap the element type: "public.addr[]" refers to+ // public.addr. Quoted identifiers end with a quote, so a genuine name+ // containing "[]" is never clipped.+ for strings.HasSuffix(s, "[]") {+ s = strings.TrimSpace(strings.TrimSuffix(s, "[]"))+ }s = stripTimeZoneSuffix(s)s = stripParens(s)return strings.TrimSpace(s)diff --git a/backend/plugin/parser/pg/query_span_omni.go b/backend/plugin/parser/pg/query_span_omni.goindex 8c693a3a7a..1cf956a7db 100644--- a/backend/plugin/parser/pg/query_span_omni.go+++ b/backend/plugin/parser/pg/query_span_omni.go@@ -779,6 +779,11 @@ func (e *omniQuerySpanExtractor) resolveVar(queryStack []*catalog.Query, v *cataif rel == nil || rel.Schema == nil {return}+ // Standalone composite types are not FROM-able; a valid analysis+ // never produces such an RTE, so treat it as unresolved.+ if rel.RelKind == 'c' {+ return+ }colName := ""if colIdx >= 0 && colIdx < len(rte.ColNames) {colName = rte.ColNames[colIdx]@@ -1064,6 +1069,11 @@ func (e *omniQuerySpanExtractor) extractColumnsFromRangeVar(rv *ast.RangeVar) []if rel == nil {return nil}+ // Standalone composite types share the relation namespace but are not+ // FROM-able; PostgreSQL rejects them as table sources.+ if rel.RelKind == 'c' {+ return nil+ }var results []base.QuerySpanResultfor _, col := range rel.Columns {colSet := make(base.SourceColumnSet)diff --git a/backend/plugin/parser/pg/query_span_omni_plpgsql.go b/backend/plugin/parser/pg/query_span_omni_plpgsql.goindex b56613212a..1ed87bdfe3 100644--- a/backend/plugin/parser/pg/query_span_omni_plpgsql.go+++ b/backend/plugin/parser/pg/query_span_omni_plpgsql.go@@ -247,6 +247,11 @@ type plpgsqlAnalyzer struct {// cteMap holds CTE definitions for resolving column refs through CTEs.cteMap map[string]*ast.SelectStmtresolvingCTE map[cteResolveKey]bool+ // qualifiedFrom marks fromTables keys whose FROM item carried an+ // explicit schema qualifier, so fallback resolution does not probe the+ // search path for them. Reset per fallback statement; nested subquery+ // collection during one statement accumulates into the same scope.+ qualifiedFrom map[string]bool}type cteResolveKey struct {@@ -757,7 +762,10 @@ func (a *plpgsqlAnalyzer) fallbackExtractLineage(origSQL string, selStmt *ast.Sereturn nil}- // Collect table references from the FROM clause.+ // Collect table references from the FROM clause. The explicit-schema+ // markers are scoped to this statement — a previous fallback query's+ // qualified FROM must not pin a later query's unqualified reference.+ a.qualifiedFrom = make(map[string]bool)fromTables := a.collectFromTables(selStmt)var results []base.SourceColumnSet@@ -817,6 +825,41 @@ func (a *plpgsqlAnalyzer) collectFromTables(selStmt *ast.SelectStmt) map[string]return tables}+// resolveFallbackRelation resolves a textual fallback resource against the+// catalog. Unqualified FROM items default to "public" in collectTablesFromNode,+// so on a miss the extractor's search path is probed the way PostgreSQL+// would resolve the name.+// pushMarkerScope opens a nested explicit-qualification scope seeded from+// the current one, so markers collected inside a subquery or CTE apply to+// that scope's resolution but never leak outward. The returned restore+// function reinstates the caller's scope; the returned map is the nested+// scope for selective exports.+func (a *plpgsqlAnalyzer) pushMarkerScope() (func(), map[string]bool) {+ outer := a.qualifiedFrom+ nested := make(map[string]bool, len(outer))+ for k, v := range outer {+ nested[k] = v+ }+ a.qualifiedFrom = nested+ return func() { a.qualifiedFrom = outer }, nested+}++func (a *plpgsqlAnalyzer) resolveFallbackRelation(fromKey, schemaName, tableName string) *catalog.Relation {+ // collectTablesFromNode defaults unqualified FROM items to "public"; for+ // those (and only those — explicitly qualified items resolve exactly),+ // the configured search path decides resolution order, exactly like+ // PostgreSQL. The literal public lookup remains the last resort for+ // search paths that do not contain public.+ if schemaName == "public" && !a.qualifiedFrom[fromKey] {+ for _, pathSchema := range a.extractor.searchPath {+ if rel := a.extractor.cat.GetRelation(pathSchema, tableName); rel != nil {+ return rel+ }+ }+ }+ return a.extractor.cat.GetRelation(schemaName, tableName)+}+func (a *plpgsqlAnalyzer) collectTablesFromNode(node ast.Node, tables map[string]base.ColumnResource) {switch v := node.(type) {case *ast.RangeVar:@@ -829,29 +872,63 @@ func (a *plpgsqlAnalyzer) collectTablesFromNode(node ast.Node, tables map[stringSchema: schema,Table: v.Relname,}+ // The newest FROM item owns its key's qualification marker in this+ // scope: set it when explicitly qualified, clear any inherited one+ // when not — an inner unqualified FROM must not be pinned by a+ // same-named outer explicit FROM.+ if a.qualifiedFrom == nil {+ a.qualifiedFrom = make(map[string]bool)+ }tables[strings.ToLower(v.Relname)] = resource+ if v.Schemaname != "" {+ a.qualifiedFrom[strings.ToLower(v.Relname)] = true+ } else {+ delete(a.qualifiedFrom, strings.ToLower(v.Relname))+ }if v.Alias != nil && v.Alias.Aliasname != "" {tables[strings.ToLower(v.Alias.Aliasname)] = resource+ if v.Schemaname != "" {+ a.qualifiedFrom[strings.ToLower(v.Alias.Aliasname)] = true+ } else {+ delete(a.qualifiedFrom, strings.ToLower(v.Alias.Aliasname))+ }}case *ast.RangeSubselect:// For subquery aliases like (SELECT * FROM t) result, collect inner tables// and map them under the alias so column refs can be resolved.if v.Subquery != nil {if subSel, ok := v.Subquery.(*ast.SelectStmt); ok {+ restore, nested := a.pushMarkerScope()innerTables := a.collectFromTables(subSel)- // Add inner tables to the outer fromTables.+ restore()+ if a.qualifiedFrom == nil {+ a.qualifiedFrom = make(map[string]bool)+ }+ // Add inner tables to the outer fromTables. Every key this+ // branch writes takes ownership of its qualification marker:+ // set from the borrowed resource's inner marker, cleared+ // otherwise — never inherited from the outer scope.for k, res := range innerTables {if _, exists := tables[k]; !exists {tables[k] = res+ if nested[k] {+ a.qualifiedFrom[k] = true+ } else {+ delete(a.qualifiedFrom, k)+ }}}// If the subquery has an alias, also map all inner tables'// columns as accessible through the alias.if v.Alias != nil && v.Alias.Aliasname != "" {alias := strings.ToLower(v.Alias.Aliasname)- // Use the first inner table as the primary source for column lookup.- for _, res := range innerTables {+ for k, res := range innerTables {tables[alias] = res+ if nested[k] {+ a.qualifiedFrom[alias] = true+ } else {+ delete(a.qualifiedFrom, alias)+ }break}}@@ -898,6 +975,10 @@ func (a *plpgsqlAnalyzer) extractColumnRefsFromExpr(node ast.Node, fromTables macase *ast.SubLink:if v.Subselect != nil {if subSel, ok := v.Subselect.(*ast.SelectStmt); ok {+ // The subquery's qualification markers apply only within its+ // own resolution scope.+ restore, _ := a.pushMarkerScope()+ defer restore()subTables := a.collectFromTables(subSel)for k, val := range fromTables {if _, exists := subTables[k]; !exists {@@ -1007,13 +1088,21 @@ func (a *plpgsqlAnalyzer) resolveColumnRef(cr *ast.ColumnRef, fromTables map[strreturn}// Check if it's a column name in any FROM table.- for _, resource := range fromTables {- rel := a.extractor.cat.GetRelation(resource.Schema, resource.Table)- if rel != nil && relationHasColumn(rel, colName) {+ for fromKey, resource := range fromTables {+ rel := a.resolveFallbackRelation(fromKey, resource.Schema, resource.Table)+ // Standalone composite types are not FROM-able.+ if rel != nil && rel.RelKind != 'c' && relationHasColumn(rel, colName) {+ // Emit the schema the relation actually resolved to — an+ // unqualified item may have resolved through the search+ // path, not the public default.+ emitSchema, emitTable := resource.Schema, resource.Table+ if rel.Schema != nil {+ emitSchema, emitTable = rel.Schema.Name, rel.Name+ }result[base.ColumnResource{Database: resource.Database,- Schema: resource.Schema,- Table: resource.Table,+ Schema: emitSchema,+ Table: emitTable,Column: colName,}] = truereturn@@ -1021,13 +1110,17 @@ func (a *plpgsqlAnalyzer) resolveColumnRef(cr *ast.ColumnRef, fromTables map[str}// Check if it's a whole-row reference (table alias used as value, e.g., to_jsonb(t1)).if resource, ok := fromTables[strings.ToLower(colName)]; ok {- rel := a.extractor.cat.GetRelation(resource.Schema, resource.Table)- if rel != nil {+ rel := a.resolveFallbackRelation(strings.ToLower(colName), resource.Schema, resource.Table)+ if rel != nil && rel.RelKind != 'c' {+ emitSchema, emitTable := resource.Schema, resource.Table+ if rel.Schema != nil {+ emitSchema, emitTable = rel.Schema.Name, rel.Name+ }for _, col := range rel.Columns {result[base.ColumnResource{Database: resource.Database,- Schema: resource.Schema,- Table: resource.Table,+ Schema: emitSchema,+ Table: emitTable,Column: col.Name,}] = true}@@ -1046,10 +1139,21 @@ func (a *plpgsqlAnalyzer) resolveColumnRef(cr *ast.ColumnRef, fromTables map[strreturn}}+ // A known standalone composite type is not FROM-able; suppress+ // lineage for it (unknown relations keep the textual fallback);+ // a resolved relation supplies the schema it actually lives in.+ rel := a.resolveFallbackRelation(tableName, resource.Schema, resource.Table)+ if rel != nil && rel.RelKind == 'c' {+ return+ }+ emitSchema, emitTable := resource.Schema, resource.Table+ if rel != nil && rel.Schema != nil {+ emitSchema, emitTable = rel.Schema.Name, rel.Name+ }result[base.ColumnResource{Database: resource.Database,- Schema: resource.Schema,- Table: resource.Table,+ Schema: emitSchema,+ Table: emitTable,Column: colName,}] = true}@@ -1075,7 +1179,10 @@ func (a *plpgsqlAnalyzer) resolveThroughCTE(cteSel *ast.SelectStmt, colName stria.resolvingCTE[key] = truedefer delete(a.resolvingCTE, key)- // Build FROM tables for the CTE's query.+ // Build FROM tables for the CTE's query; its qualification markers are+ // scoped to this resolution.+ restoreMarkers, _ := a.pushMarkerScope()+ defer restoreMarkers()cteTables := a.collectFromTables(cteSel)// Include parent CTEs for nested CTE references.if a.cteMap != nil {diff --git a/backend/plugin/schema/pg/composite_type_walkthrough_test.go b/backend/plugin/schema/pg/composite_type_walkthrough_test.gonew file mode 100644index 0000000000..aeb538f16d--- /dev/null+++ b/backend/plugin/schema/pg/composite_type_walkthrough_test.go@@ -0,0 +1,281 @@+package pg++import (+ "context"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/omni/pg/catalog"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/model"+)++// compositeWalkThroughMetadata has an adversarial name pair (aa_nested sorts+// before its dependency zz_base) plus an enum-referencing composite and a+// table using one, so a successful load proves dependency-ordered install.+func compositeWalkThroughMetadata() *storepb.DatabaseSchemaMetadata {+ return &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ EnumTypes: []*storepb.EnumTypeMetadata{+ {Name: "status", Values: []string{"a", "b"}},+ },+ CompositeTypes: []*storepb.CompositeTypeMetadata{+ {+ // References zz_base only through an array suffix.+ Name: "aa_array_only",+ Attributes: []*storepb.CompositeTypeAttribute{+ {Name: "items", Type: "public.zz_base[]"},+ },+ },+ {+ Name: "aa_nested",+ Attributes: []*storepb.CompositeTypeAttribute{+ {Name: "home", Type: "public.zz_base"},+ {Name: "s", Type: "public.status"},+ },+ },+ {+ Name: "zz_base",+ Comment: "base address type",+ Attributes: []*storepb.CompositeTypeAttribute{+ {Name: "street", Type: "text", Collation: `"C"`, Comment: "street line"},+ {Name: "city", Type: "character varying(50)"},+ },+ },+ {+ Name: "ext_owned",+ SkipDump: true,+ Attributes: []*storepb.CompositeTypeAttribute{+ {Name: "x", Type: "integer"},+ },+ },+ },+ Tables: []*storepb.TableMetadata{+ {+ Name: "users",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer", Position: 1},+ {Name: "home", Type: "public.zz_base", Position: 2, Nullable: true},+ },+ },+ },+ },+ },+ }+}++func TestWalkThroughLoadsCompositeTypes(t *testing.T) {+ meta := compositeWalkThroughMetadata()++ cat := catalog.New()+ err := loadWalkThroughCatalog(context.Background(), cat, meta)+ require.NoError(t, err)++ require.NotNil(t, cat.GetRelation("public", "zz_base"), "composite type must be installed")+ require.NotNil(t, cat.GetRelation("public", "aa_nested"), "nested composite type must be installed")+ arrayOnly := cat.GetRelation("public", "aa_array_only")+ require.NotNil(t, arrayOnly, "array-only referencing composite must be installed")+ require.Len(t, arrayOnly.Columns, 1, "array-only composite must install with its real attribute")+ require.NotNil(t, cat.GetRelation("public", "users"), "table using a composite column must install")+}++func TestWalkThroughCompositeFallbackPreservesAttributeNames(t *testing.T) {+ // A domain-typed attribute cannot install (domains are not loader+ // objects), forcing the pseudo fallback — which must keep attribute+ // names so later DDL targeting them still resolves.+ meta := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ CompositeTypes: []*storepb.CompositeTypeMetadata{+ {+ Name: "with_domain",+ Attributes: []*storepb.CompositeTypeAttribute{+ {Name: "p", Type: "public.pos_int"},+ {Name: "note", Type: "text"},+ },+ },+ },+ },+ },+ }++ cat := catalog.New()+ err := loadWalkThroughCatalog(context.Background(), cat, meta)+ require.NoError(t, err)++ rel := cat.GetRelation("public", "with_domain")+ require.NotNil(t, rel, "composite must fall back, not vanish")+ require.Len(t, rel.Columns, 2, "fallback must preserve attribute names")+ require.Equal(t, "p", rel.Columns[0].Name)+ require.Equal(t, "note", rel.Columns[1].Name)++ // DDL targeting a preserved attribute must succeed against the fallback.+ catAfter := cat.Clone()+ results, err := catAfter.Exec(`ALTER TYPE public.with_domain DROP ATTRIBUTE note;`, &catalog.ExecOptions{ContinueOnError: true})+ require.NoError(t, err)+ for _, r := range results {+ require.NoError(t, r.Error, "DDL against fallback composite should succeed: %s", r.SQL)+ }++ // A rename keeps the attribute number, so the renamed attribute of a+ // degraded composite must also keep its real previous type.+ renameResults, err := catAfter.Exec(`ALTER TYPE public.with_domain RENAME ATTRIBUTE p TO q;`, &catalog.ExecOptions{ContinueOnError: true})+ require.NoError(t, err)+ for _, r := range renameResults {+ require.NoError(t, r.Error, "rename against fallback composite should succeed: %s", r.SQL)+ }++ // Applying the diff must not rewrite the untouched attribute to the+ // fallback's text type — unchanged attributes keep previous metadata.+ diff := catalog.Diff(cat, catAfter)+ require.False(t, diff.IsEmpty())+ newProto := applyDiffToMetadata(meta, cat, catAfter, diff)+ var applied *storepb.CompositeTypeMetadata+ for _, composite := range newProto.Schemas[0].CompositeTypes {+ if composite.Name == "with_domain" {+ applied = composite+ }+ }+ require.NotNil(t, applied)+ require.Len(t, applied.Attributes, 1, "dropped attribute must be removed")+ require.Equal(t, "q", applied.Attributes[0].Name, "rename must be reflected")+ require.Equal(t, "public.pos_int", applied.Attributes[0].Type,+ "renamed attribute must keep its real previous type, not the fallback text")+}++func TestWalkThroughDropReaddAttributeReadsCatalogType(t *testing.T) {+ // Dropping and re-adding an attribute with the same name assigns a new+ // attnum; the rebuilt metadata must take the catalog's new type, not+ // carry the stale previous metadata.+ meta := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ CompositeTypes: []*storepb.CompositeTypeMetadata{+ {+ Name: "with_domain",+ Attributes: []*storepb.CompositeTypeAttribute{+ {Name: "p", Type: "public.pos_int", Comment: "old comment"},+ {Name: "note", Type: "text"},+ },+ },+ },+ },+ },+ }++ cat := catalog.New()+ err := loadWalkThroughCatalog(context.Background(), cat, meta)+ require.NoError(t, err)++ catAfter := cat.Clone()+ results, err := catAfter.Exec(`+ ALTER TYPE public.with_domain DROP ATTRIBUTE p;+ ALTER TYPE public.with_domain ADD ATTRIBUTE p text;+ `, &catalog.ExecOptions{ContinueOnError: true})+ require.NoError(t, err)+ for _, r := range results {+ require.NoError(t, r.Error, "DDL should succeed: %s", r.SQL)+ }++ diff := catalog.Diff(cat, catAfter)+ require.False(t, diff.IsEmpty())+ newProto := applyDiffToMetadata(meta, cat, catAfter, diff)+ var applied *storepb.CompositeTypeMetadata+ for _, composite := range newProto.Schemas[0].CompositeTypes {+ if composite.Name == "with_domain" {+ applied = composite+ }+ }+ require.NotNil(t, applied)+ require.Len(t, applied.Attributes, 2)+ var p *storepb.CompositeTypeAttribute+ for _, attribute := range applied.Attributes {+ if attribute.Name == "p" {+ p = attribute+ }+ }+ require.NotNil(t, p)+ require.Equal(t, "text", p.Type, "re-added attribute must take the catalog's new type: %v", applied)+ require.Empty(t, p.Comment, "re-added attribute must not inherit the dropped attribute's comment")+}++func TestWalkThroughAppliesCompositeTypeChanges(t *testing.T) {+ meta := compositeWalkThroughMetadata()++ catBefore := catalog.New()+ err := loadWalkThroughCatalog(context.Background(), catBefore, meta)+ require.NoError(t, err)++ catAfter := catBefore.Clone()+ userSQL := `+ CREATE TYPE public.geo AS (lat numeric(9,6), lng numeric(9,6));+ CREATE TYPE public.geo_wrap AS (g geo, gs geo[]);+ CREATE SCHEMA "select";+ CREATE TYPE "select".kw AS (x int);+ CREATE TYPE public.kw_wrap AS (k "select".kw);+ CREATE TABLE public.places (id int, location public.geo);+ DROP TYPE public.aa_nested;+ ALTER TYPE public.zz_base ADD ATTRIBUTE zip text;+ ALTER TYPE public.ext_owned ADD ATTRIBUTE y integer;+ `+ results, err := catAfter.Exec(userSQL, &catalog.ExecOptions{ContinueOnError: true})+ require.NoError(t, err)+ for _, r := range results {+ require.NoError(t, r.Error, "DDL should succeed: %s", r.SQL)+ }++ diff := catalog.Diff(catBefore, catAfter)+ require.False(t, diff.IsEmpty())++ newProto := applyDiffToMetadata(meta, catBefore, catAfter, diff)+ newMeta := model.NewDatabaseMetadata(newProto, nil, nil, storepb.Engine_POSTGRES, true)++ publicSchema := newMeta.GetSchemaMetadata("public")+ require.NotNil(t, publicSchema)++ composites := make(map[string]*storepb.CompositeTypeMetadata)+ for _, composite := range publicSchema.GetProto().CompositeTypes {+ composites[composite.Name] = composite+ }+ require.Contains(t, composites, "geo", "created composite must be applied to metadata")+ require.Len(t, composites["geo"].Attributes, 2)+ require.Equal(t, "lat", composites["geo"].Attributes[0].Name)+ require.Equal(t, "numeric(9,6)", composites["geo"].Attributes[0].Type,+ "built-in attribute types must stay unqualified")++ // User-defined attribute types must come back schema-qualified even when+ // the DDL referenced them unqualified via the search path.+ require.Contains(t, composites, "geo_wrap")+ require.Equal(t, "public.geo", composites["geo_wrap"].Attributes[0].Type)+ require.Equal(t, "public.geo[]", composites["geo_wrap"].Attributes[1].Type)++ // A schema named like a reserved keyword must come back quoted.+ require.Contains(t, composites, "kw_wrap")+ require.Equal(t, `"select".kw`, composites["kw_wrap"].Attributes[0].Type)+ require.NotContains(t, composites, "aa_nested", "dropped composite must be removed from metadata")+ require.Contains(t, composites, "zz_base", "altered composite must remain")++ // The modified composite gains the new attribute while preserving the+ // type comment and surviving attributes' comments/collations.+ base := composites["zz_base"]+ require.Equal(t, "base address type", base.Comment, "type comment must survive modification")+ require.Len(t, base.Attributes, 3)+ require.Equal(t, "street", base.Attributes[0].Name)+ require.Equal(t, `"C"`, base.Attributes[0].Collation, "attribute collation must survive modification")+ require.Equal(t, "street line", base.Attributes[0].Comment, "attribute comment must survive modification")+ require.Equal(t, "zip", base.Attributes[2].Name, "added attribute must appear")++ // skip_dump survives modification (extension-owned types must stay+ // excluded from dumps).+ require.Contains(t, composites, "ext_owned")+ require.True(t, composites["ext_owned"].SkipDump, "skip_dump must survive modification")++ // Original metadata is not mutated.+ require.Len(t, meta.Schemas[0].CompositeTypes, 4)+}diff --git a/backend/plugin/schema/pg/walk_through_apply.go b/backend/plugin/schema/pg/walk_through_apply.goindex 40dba185da..e0b6a73f48 100644--- a/backend/plugin/schema/pg/walk_through_apply.go+++ b/backend/plugin/schema/pg/walk_through_apply.go@@ -7,6 +7,7 @@ import ("google.golang.org/protobuf/proto""github.com/bytebase/omni/pg/catalog"+ omniparser "github.com/bytebase/omni/pg/parser"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -102,6 +103,31 @@ func applyDiffToMetadata(original *storepb.DatabaseSchemaMetadata, catBefore, ca}}+ for _, ct := range diff.CompositeTypes {+ if ct.Action == catalog.DiffDrop {+ if sm := findSchema(result, ct.SchemaName); sm != nil {+ sm.CompositeTypes = removeCompositeTypeByName(sm.CompositeTypes, ct.Name)+ }+ continue+ }+ if ct.To == nil {+ continue+ }+ sm := findOrCreateSchema(result, ct.SchemaName)+ switch ct.Action {+ case catalog.DiffAdd:+ sm.CompositeTypes = append(sm.CompositeTypes, compositeTypeToProto(catAfter, ct.Name, nil, ct.To, nil))+ case catalog.DiffModify:+ for i, existing := range sm.CompositeTypes {+ if existing.Name == ct.Name {+ sm.CompositeTypes[i] = compositeTypeToProto(catAfter, ct.Name, ct.From, ct.To, existing)+ break+ }+ }+ default:+ }+ }+for _, f := range diff.Functions {isProcedure := f.To != nil && f.To.Kind == 'p'wasProcedure := f.From != nil && f.From.Kind == 'p'@@ -688,3 +714,167 @@ func removeFunctionByIdentity(funcs []*storepb.FunctionMetadata, identity string}return out}++// compositeTypeToProto builds metadata from the catalog relation, preserving+// the type comment and per-attribute comments/collations from the previous+// metadata (the catalog does not track comments, and it only keeps the bare+// collation name) for attributes that survive by name. Attributes unchanged+// between from and to keep their previous metadata verbatim, so a composite+// degraded to the text-backed pseudo fallback does not rewrite untouched+// attributes to text — mirroring the per-column granularity of table diffs.+//+// Within a degraded composite this is a deliberate trade-off: an attribute+// genuinely altered TO text is indistinguishable from an unchanged one (both+// read text in from and to), so that rare change is missed in favor of not+// corrupting every untouched attribute. Walk-through metadata is advisory;+// post-execution sync restores ground truth. The root fix is modeling the+// types the loader cannot install today (e.g. domains).+func compositeTypeToProto(cat *catalog.Catalog, name string, from, to *catalog.Relation, previous *storepb.CompositeTypeMetadata) *storepb.CompositeTypeMetadata {+ previousAttributes := make(map[string]*storepb.CompositeTypeAttribute)+ composite := &storepb.CompositeTypeMetadata{Name: name}+ if previous != nil {+ composite.Comment = previous.Comment+ composite.SkipDump = previous.SkipDump+ for _, attribute := range previous.Attributes {+ previousAttributes[attribute.Name] = attribute+ }+ }+ fromByAttNum := make(map[int16]*catalog.Column)+ if from != nil {+ for _, c := range from.Columns {+ if c != nil {+ fromByAttNum[c.AttNum] = c+ }+ }+ }+ for _, c := range to.Columns {+ if c == nil || c.Name == "" {+ continue+ }+ // The attribute number is the stable column identity: it survives+ // RENAME ATTRIBUTE and is never reused, so a dropped-and-re-added+ // attribute (same name, new attnum) correctly reads as a new column+ // while a renamed survivor still carries its previous metadata.+ fc := fromByAttNum[c.AttNum]+ if fc != nil &&+ fc.TypeOID == c.TypeOID && fc.TypeMod == c.TypeMod && fc.CollationName == c.CollationName {+ if prev := previousAttributes[fc.Name]; prev != nil {+ composite.Attributes = append(composite.Attributes, &storepb.CompositeTypeAttribute{+ Name: c.Name,+ Type: prev.Type,+ Collation: prev.Collation,+ Comment: prev.Comment,+ })+ continue+ }+ }+ attribute := &storepb.CompositeTypeAttribute{+ Name: c.Name,+ Type: qualifyWalkThroughAttributeType(cat, c.TypeOID, cat.FormatType(c.TypeOID, c.TypeMod)),+ }+ // Previous metadata is only consulted for the column identity that+ // survived (same attnum): a dropped-and-re-added attribute must not+ // inherit the old attribute's collation reference or comment.+ var surviving *storepb.CompositeTypeAttribute+ if fc != nil {+ surviving = previousAttributes[fc.Name]+ }+ if c.CollationName != "" {+ // The catalog keeps only the bare collation name, so a collation+ // introduced by walk-through DDL loses its schema qualifier here+ // (same limitation as below). Walk-through metadata is advisory —+ // the authoritative metadata is re-synced from the database after+ // execution, which stores the properly qualified reference.+ attribute.Collation = "\"" + c.CollationName + "\""+ // Substitute the previous emit-ready (possibly schema-qualified)+ // reference only when it names the same collation — a changed+ // collation must reflect the catalog's current value.+ if surviving != nil && collationReferenceBareName(surviving.Collation) == c.CollationName {+ attribute.Collation = surviving.Collation+ }+ }+ if surviving != nil {+ attribute.Comment = surviving.Comment+ }+ composite.Attributes = append(composite.Attributes, attribute)+ }+ return composite+}++func removeCompositeTypeByName(composites []*storepb.CompositeTypeMetadata, name string) []*storepb.CompositeTypeMetadata {+ out := make([]*storepb.CompositeTypeMetadata, 0, len(composites))+ for _, composite := range composites {+ if composite.Name != name {+ out = append(out, composite)+ }+ }+ return out+}++// collationReferenceBareName extracts the bare collation name from a stored+// emit-ready reference (e.g. `"C"` -> C, `locale.en_us` -> en_us) for+// comparison with the catalog's collation name. The comparison cannot see+// schema qualifiers — the omni catalog only records the bare collation name —+// so switching an attribute between two same-named collations in different+// schemas keeps the previous reference. Known, accepted limitation.+func collationReferenceBareName(reference string) string {+ if _, name, ok := parseQualifiedTypeIdent(reference); ok {+ return name+ }+ name := strings.Trim(reference, `"`)+ return strings.ReplaceAll(name, `""`, `"`)+}++// qualifyWalkThroughAttributeType enforces the CompositeTypeAttribute.Type+// contract: user-defined types are always schema-qualified. FormatType+// renders search-path-visible types unqualified, so the (element) type's+// namespace is resolved and prepended when missing. System-schema types come+// back unqualified from QueryPgNamespace and stay untouched.+func qualifyWalkThroughAttributeType(cat *catalog.Catalog, typeOID uint32, formatted string) string {+ t := cat.TypeByOID(typeOID)+ if t == nil {+ return formatted+ }+ element := t+ if t.Elem != 0 && t.Len == -1 {+ if el := cat.TypeByOID(t.Elem); el != nil {+ element = el+ }+ }+ schemaName := ""+ for _, row := range cat.QueryPgNamespace() {+ if row.OID == element.Namespace {+ schemaName = row.NspName+ break+ }+ }+ // QueryPgNamespace already omits builtin-OID schemas; the explicit check+ // keeps the invariant independent of the catalog's OID layout and covers+ // information_schema, mirroring the sync query's exclusions.+ if schemaName == "" || wtIsSystemSchema(schemaName) {+ return formatted+ }+ prefix := quoteWalkThroughIdent(schemaName) + "."+ quotedPrefix := `"` + strings.ReplaceAll(schemaName, `"`, `""`) + `".`+ if strings.HasPrefix(formatted, prefix) || strings.HasPrefix(formatted, quotedPrefix) {+ return formatted+ }+ return prefix + formatted+}++// quoteWalkThroughIdent mirrors quote_ident: quote when the name needs it,+// including reserved keywords.+func quoteWalkThroughIdent(name string) string {+ simple := name != ""+ for i, r := range name {+ if (r >= 'a' && r <= 'z') || r == '_' || (i > 0 && ((r >= '0' && r <= '9') || r == '$')) {+ continue+ }+ simple = false+ break+ }+ if simple && !omniparser.IsReservedKeyword(name) {+ return name+ }+ return `"` + strings.ReplaceAll(name, `"`, `""`) + `"`+}diff --git a/backend/plugin/schema/pg/walk_through_loader.go b/backend/plugin/schema/pg/walk_through_loader.goindex 7df2aa9d99..829335810f 100644--- a/backend/plugin/schema/pg/walk_through_loader.go+++ b/backend/plugin/schema/pg/walk_through_loader.go@@ -46,6 +46,7 @@ type wtObjectKind intconst (kindWTSchema wtObjectKind = iotakindWTEnum+ kindWTCompositekindWTSequencekindWTTablekindWTView@@ -62,13 +63,14 @@ type wtObjectEntry struct {name string// Exactly one of the following is set based on kind.- enumMeta *storepb.EnumTypeMetadata- seqMeta *storepb.SequenceMetadata- tableMeta *storepb.TableMetadata- viewMeta *storepb.ViewMetadata- matViewMeta *storepb.MaterializedViewMetadata- funcMeta *storepb.FunctionMetadata- idxMeta *storepb.IndexMetadata+ enumMeta *storepb.EnumTypeMetadata+ compositeMeta *storepb.CompositeTypeMetadata+ seqMeta *storepb.SequenceMetadata+ tableMeta *storepb.TableMetadata+ viewMeta *storepb.ViewMetadata+ matViewMeta *storepb.MaterializedViewMetadata+ funcMeta *storepb.FunctionMetadata+ idxMeta *storepb.IndexMetadata// For kindWTConstraint: the parent table metadata.constraintTable *storepb.TableMetadata// For kindWTConstraint, kindWTIndex: the parent relation name.@@ -79,7 +81,7 @@ func (e *wtObjectEntry) key() string {switch e.kind {case kindWTSchema:return "schema:" + e.schema- case kindWTEnum:+ case kindWTEnum, kindWTComposite:return "type:" + e.schema + "." + e.namecase kindWTSequence:return "seq:" + e.schema + "." + e.name@@ -122,6 +124,8 @@ func wtKindLabel(k wtObjectKind) string {return "0schema"case kindWTEnum:return "1enum"+ case kindWTComposite:+ return "1composite"case kindWTSequence:return "2seq"case kindWTTable:@@ -160,6 +164,14 @@ func wtCollectObjects(meta *storepb.DatabaseSchemaMetadata) []*wtObjectEntry {enumMeta: enum,})}+ for _, composite := range sm.CompositeTypes {+ out = append(out, &wtObjectEntry{+ kind: kindWTComposite,+ schema: sm.Name,+ name: composite.Name,+ compositeMeta: composite,+ })+ }for _, seq := range sm.Sequences {if wtIsIdentitySequence(seq, sm.Tables) {continue@@ -429,6 +441,18 @@ func wtBuildEdges(objects []*wtObjectEntry, index map[string]*wtObjectEntry) map}}}+ case kindWTComposite:+ for _, attribute := range obj.compositeMeta.Attributes {+ for _, ref := range wtExtractUserTypeRefs(attribute.Type) {+ if k := "type:" + ref.Schema + "." + ref.Name; index[k] != nil {+ deps = append(deps, k)+ }+ // An attribute may be typed with a table/view row type.+ if k := "rel:" + ref.Schema + "." + ref.Name; index[k] != nil {+ deps = append(deps, k)+ }+ }+ }default:// kindWTSchema, kindWTEnum, kindWTSequence have no additional deps.}@@ -566,6 +590,23 @@ func wtInstallOne(cat *catalog.Catalog, obj *wtObjectEntry) {if stmt, err := wtPseudoMatViewStmt(obj.schema, obj.name, cols); err == nil {_ = cat.ExecCreateTableAs(stmt)}+ case kindWTComposite:+ // Name-preserving, text-backed fallback (mirroring the table pseudo)+ // so later DDL targeting an existing attribute still resolves.+ items := make([]ast.Node, 0, len(obj.compositeMeta.Attributes))+ for _, attribute := range obj.compositeMeta.Attributes {+ if attribute.Name == "" {+ continue+ }+ items = append(items, &ast.ColumnDef{+ Colname: attribute.Name,+ TypeName: wtPseudoTextTypeName(),+ })+ }+ _ = cat.DefineCompositeType(&ast.CompositeTypeStmt{+ Typevar: &ast.RangeVar{Schemaname: obj.schema, Relname: obj.name},+ Coldeflist: &ast.List{Items: items},+ })default:// No pseudo form for schemas, enums, sequences, functions, indexes, constraints.}@@ -592,6 +633,27 @@ func wtInstallReal(cat *catalog.Catalog, obj *wtObjectEntry) error {Vals: &ast.List{Items: vals},})+ case kindWTComposite:+ cols := make([]ast.Node, 0, len(obj.compositeMeta.Attributes))+ for _, attribute := range obj.compositeMeta.Attributes {+ if attribute.Name == "" {+ continue+ }+ tn, err := wtTypeNameFromString(attribute.Type)+ if err != nil {+ return errors.Wrapf(err, "attribute %q", attribute.Name)+ }+ cols = append(cols, &ast.ColumnDef{+ Colname: attribute.Name,+ TypeName: tn,+ CollClause: collateClauseFromReference(attribute.Collation),+ })+ }+ return cat.DefineCompositeType(&ast.CompositeTypeStmt{+ Typevar: &ast.RangeVar{Schemaname: obj.schema, Relname: obj.name},+ Coldeflist: &ast.List{Items: cols},+ })+case kindWTSequence:return wtInstallSequence(cat, obj)@@ -1181,6 +1243,12 @@ func wtExtractUserTypeRefs(typeStr string) []wtUserTypeRef {func wtStripTypeModifiers(s string) string {s = strings.TrimSpace(s)+ // Array suffixes wrap the element type: "public.addr[]" refers to+ // public.addr. Quoted identifiers end with a quote, so a genuine name+ // containing "[]" is never clipped.+ for strings.HasSuffix(s, "[]") {+ s = strings.TrimSpace(strings.TrimSuffix(s, "[]"))+ }lower := strings.ToLower(s)for _, sfx := range []string{" with time zone", " without time zone"} {if strings.HasSuffix(lower, sfx) {@@ -1410,3 +1478,21 @@ func wtParseExpr(expr string) ast.Node {}return rt.Val}++// collateClauseFromReference converts a stored emit-ready collation reference+// (quoted as needed, schema-qualified outside pg_catalog, e.g. `"C"` or+// `locale.en_us`) into an AST collate clause. Returns nil for no collation.+func collateClauseFromReference(reference string) *ast.CollateClause {+ if reference == "" {+ return nil+ }+ var items []ast.Node+ if schemaName, collationName, ok := parseQualifiedTypeIdent(reference); ok {+ items = []ast.Node{&ast.String{Str: schemaName}, &ast.String{Str: collationName}}+ } else {+ name := strings.Trim(reference, `"`)+ name = strings.ReplaceAll(name, `""`, `"`)+ items = []ast.Node{&ast.String{Str: name}}+ }+ return &ast.CollateClause{Collname: &ast.List{Items: items}}+}