feat(pg): support composite types in schema sync and SDL dump by h3n4l · Pull Request #20911 · bytebase/bytebase · GitHub
Skip to content

feat(pg): support composite types in schema sync and SDL dump - #20911

Merged
h3n4l merged 7 commits into
mainfrom
claude/byt-9668-cf7383
Jul 15, 2026
Merged

h3n4l merged 7 commits into
mainfrom
claude/byt-9668-cf7383

Conversation

@h3n4l

@h3n4l h3n4l commented Jul 15, 2026

Copy link
Copy Markdown
Member

What

Adds PostgreSQL composite type (CREATE TYPE ... AS) support to schema sync and all schema definition dump paths. Fixes the loader half of #20434 (Linear BYT-9668).

Why

SchemaMetadata modeled enum types but not composite types, so SDLMigration reconstructed source schemas that reference composite types without ever creating them. omni's dependency-aware LoadSDL then rejected the dump — ERROR: type "..." does not exist (SQLSTATE 42704) — on the database's own synced schema, which users cannot fix by editing their files.

How

  • Proto: new CompositeTypeMetadata / CompositeTypeAttribute messages (store + v1), composite_types field 17 on SchemaMetadata. Additive only; attributes carry type, collation, and comment. Per-concept message design is recorded in docs/adr/0001-per-concept-type-metadata.md.
  • Sync: getCompositeTypes in the PG driver — standalone composites only (pg_class.relkind = 'c', table/view row types excluded), dropped attributes excluded, extension-owned types marked skip_dump, zero-attribute types supported. Attribute types are rendered by format_type and always schema-qualified for user-defined types, so stored strings are deterministic regardless of search_path.
  • Dump: composite types emitted in all four definition paths (GetDatabaseDefinition, GetSchemaDefinition, SDL format, multi-file), after enums and before sequences/tables, topologically sorted so referenced composites precede dependents (PostgreSQL forbids containment cycles, so the sort cannot loop). Type and attribute comments emitted.
  • Docs: corrected the stale declarative-mode limitations list in action/README.md — the parser already supported these statements; the gap was metadata fidelity.

Known limitation (deliberate): a composite attribute typed with a table/view row type is not reordered relative to tables in plain dumps — that requires pg_depend-grade dependency data (cf. pg_dump_sort.c). The SDL flow is unaffected since LoadSDL resolves dependencies itself.

Sync Schema diff generation and the walk-through/query-span catalog loaders follow in separate PRs.

Testing

  • TestSyncCompositeTypesWithTestcontainer — pins exact synced metadata (qualification, collation, comments, empty types, row-type exclusion) against real PostgreSQL.
  • TestGetDatabaseDefinitionWithTestcontainer/composite_types — full round-trip: DDL → sync → dump → replay on a fresh database → sync → metadata equality. Includes nested/cross-schema/array/empty composites and adversarial names (dependent sorts alphabetically before its dependency).
  • TestGetDatabaseDefinitionSDLFormat_CompositeTypes / TestGetMultiFileDatabaseDefinition_CompositeTypes — emission order, skip_dump exclusion, and loadability through DiffSDLMigration (the exact path that failed in Can't use SDL or Sync Schema due to "does not exist" errors #20434).
  • Unit tests for the qualified-name parser and topological sort.
  • Full lint (0 issues), backend build, frontend type-check, and existing suites for db/pg, schema/pg, api/v1 all pass.

🤖 Generated with Claude Code

@h3n4l
h3n4l requested review from a team, RainbowDashy, d-bytebase and ecmadao as code owners July 15, 2026 03:08
@cla-bot cla-bot Bot added the cla-signed label Jul 15, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 313736b31d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread backend/plugin/schema/pg/get_database_definition.go
Comment thread backend/plugin/schema/pg/get_database_definition.go Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1315b90520

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread backend/plugin/schema/pg/get_database_definition.go

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3d34560c77

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread backend/plugin/db/pg/sync.go Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 54a49ab681

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread backend/plugin/schema/pg/get_database_definition.go
Comment thread action/README.md

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 389926a710

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread backend/plugin/schema/pg/get_database_definition.go
h3n4l and others added 6 commits July 15, 2026 14:52
PostgreSQL composite types (CREATE TYPE ... AS) were not modeled in
SchemaMetadata, so the SDL flow reconstructed source schemas without
their CREATE TYPE statements and omni's LoadSDL rejected the unresolved
references (SQLSTATE 42704), failing on the database's own synced
schema. Fixes the loader half of #20434.

- Add CompositeTypeMetadata / CompositeTypeAttribute to the store and
  v1 protos (attributes carry type, collation, and comment; attribute
  types are always schema-qualified for determinism)
- Sync standalone composite types (pg_class relkind 'c') with
  extension-owned types marked skip_dump, mirroring enums
- Emit composite types in all four definition paths, ordered enums
  first and composites topologically so referenced composites precede
  their dependents
- Correct the stale action/README.md declarative-mode limitations list
- Record the per-concept type-metadata decision in docs/adr/0001 and
  the Composite Type term in CONTEXT.md

Sync Schema diff support and the walk-through/query-span catalog
loaders follow in separate PRs.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- Order types.sql before table files in multi-file output so naive
  in-order consumption sees types before the tables using them
- Extract collectCompositeTypes/writeCompositeTypesSection shared by
  the three single-buffer dump paths (quality gate duplication)
- Build the composite type sync query once at package init (S2077)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Filter system schemas on the Go side via pgparser.IsSystemSchema instead
of interpolating the schema list into the SQL text. relkind = 'c' already
keeps system-schema rows near zero, so the SQL-side filter was only a
minor optimization.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
A bare collation name cannot resolve when the collation lives outside
pg_catalog (the dump header pins search_path) and can resolve to the
wrong same-named collation in another schema. Store the collation as an
emit-ready identifier reference instead — quoted as needed and
schema-qualified outside pg_catalog (e.g. `"C"`, `locale.en_us`) — and
emit it verbatim after COLLATE. The field format is settled now, before
the first release ships it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Composite type attributes and table columns may use extension-provided
types, so extensions.sql must precede the per-schema files for in-order
consumption, matching the single-file emission order.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…tput

Extensions may install into non-public schemas (CREATE EXTENSION ...
WITH SCHEMA), so schema files must come first. Final in-order layout
matches the single-file emission: schemas, extensions, types,
sequences, tables.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@h3n4l
h3n4l force-pushed the claude/byt-9668-cf7383 branch from b512e2f to 8d0bd52 Compare July 15, 2026 07:12
@github-actions

github-actions Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8d0bd52c4b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread backend/plugin/schema/pg/get_database_definition.go
Comment thread proto/v1/v1/database_service.proto Outdated
The flag is sync-computed bookkeeping (extension ownership from
pg_depend), not user-configurable, and nothing consumes it over the
API. Keep it in the store proto where dump and diff logic lives.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 87828c0144

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread backend/plugin/db/pg/sync.go
@h3n4l
h3n4l merged commit b29445f into main Jul 15, 2026
21 checks passed
@h3n4l
h3n4l deleted the claude/byt-9668-cf7383 branch July 15, 2026 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants