Skip to content

fix: drop a type after the columns of modified tables that use it (#710) - #711

Open
augustoribeiro wants to merge 2 commits into
pgplex:mainfrom
augustoribeiro:fix/issue-710-drop-type-after-column
Open

augustoribeiro wants to merge 2 commits into
pgplex:mainfrom
augustoribeiro:fix/issue-710-drop-type-after-column

Conversation

@augustoribeiro

@augustoribeiro augustoribeiro commented Nov 19, 2026 •

Copy link
Copy Markdown

A type dropped together with a column that uses it (or with that column changing to another type) was dropped in the drop phase, before the ALTER TABLE of the modify phase, and failed with SQLSTATE 2BP01. Such types, and whatever has to be dropped after them, now wait until the modified tables have been altered.

Fixes #710

…plex#710)

A type dropped together with a column that uses it (or with that column
changing to another type) was dropped in the drop phase, before the ALTER
TABLE of the modify phase, and failed with SQLSTATE 2BP01. Such types, and
whatever has to be dropped after them, now wait until the modified tables
have been altered.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@greptile-apps

greptile-apps Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 2/5

[Medium impact] The change affects generated database migration plans when altered tables still use dropped types.

The drop ordering needs fixes before merging so these valid schema changes can be applied.

Findings

  1. P1 Multirange columns still block drops ▶
  2. P1 Replaced ranges drop too early ▶
  3. P1 Reused type names block tables ▶

Summary

Defers type drops until after existing tables release their columns, while preserving the order of related function drops.

  • Type drops wait until changed tables stop using them.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Find types still used by changing columns] --> B[Drop other obsolete objects]
    B --> C[Create new objects]
    C --> D[Alter existing tables]
    D --> E[Drop delayed types and related functions]
    E --> F[Finish remaining changes]
Loading

Reviews (1) · Last reviewed commit: "fix: drop a type after the columns of mo..." · Reviewed by Greptile

Comment thread internal/diff/diff.go
Comment on lines +3354 to +3360
byKey := make(map[string]*ir.Type, len(droppedTypes))
for _, t := range droppedTypes {
byKey[typeGraphKey(t.Schema, t.Name)] = t
}
used := make(map[*ir.Type]bool)
mark := func(table *ir.Table, column *ir.Column) {
if t, ok := byKey[extractTypeName(column.DataType, table.Schema)]; ok {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Multirange columns still block drops. byKey contains only each range's name, but the inspector stores multirange columns under MultirangeTypeName. For example, removing a period_multirange column and its owning range period never marks period as held. The plan still emits DROP TYPE period before ALTER TABLE ... DROP COLUMN, causing the dependency error this PR fixes. Map multirange names, including their array forms, to their owning range before looking up columns.

Knowledge Base Used: Migration dependency ordering

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Same here: this combination already failed before the PR (the type drop ran before the column drop). Fixing it means deferring creates that reuse a dropped type's name, which is out of scope for this fix. Happy to open a follow-up issue.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That makes sense. This combination already failed before this PR, and handling creates that reuse a dropped type’s name would require broader phase reordering, so I agree it’s out of scope here. The updated lookup does cover multirange names (including arrays); I’ll withdraw this finding and leave the create-ordering case for a follow-up.

Comment thread internal/diff/diff.go
Comment on lines +3315 to +3319
for _, node := range held {
if _, ok := replacedOld[node.typeObj]; ok && node.typeObj != nil {
now = append(now, node)
} else {
afterTables = append(afterTables, node)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Replaced ranges drop too early. Moving a replaced range back into now breaks the order established by orderTypeAndFunctionDrops. For example, an old domain slot can wrap range span, with a table column using slot. If the desired schema removes that column and domain while changing span's subtype, both types enter held, but this branch moves only span back. The plan drops span while slot still depends on it and fails. Keep the column removal, dependent drops, and range replacement in dependency order rather than moving the range alone.

Knowledge Base Used: Migration planning and diffing

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This scenario isn't made worse here: before this PR the domain was dropped ahead of the column and failed the same way. Ordering it correctly needs the old range dropped after the modify phase but before its replacement is created, which the current phase layout can't express. I'd rather leave that for a follow-up than widen this fix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That’s a fair distinction. This dependency chain already failed before the PR because slot was dropped while the column still used it; the new ordering changes where it fails, but doesn’t introduce a newly failing scenario. Fixing it properly would require a phase that drops the old span after the table alteration and before creating its replacement, which is beyond this change’s scope. I’ll withdraw the comment and leave it for a follow-up.

Tip: You can customize Greptile's behavior for this repo with .greptile/rules.md and .greptile/config.json.

Comment thread internal/diff/diff.go

// Drop the types held back by generateDropSQL, now that the columns using
// them have been dropped or changed to another type (issue #710)
generateDropTypesAndFunctionsSQL(typeDropsAfterTables, nil, targetSchema, collector)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Reused type names block tables. The delayed drops run after every new table is created. If the old schema has enum priority and column tickets.priority, a valid desired schema can remove that column and enum and add table priority. The plan now tries to create table priority while the enum still owns that type name. PostgreSQL rejects the creation before the delayed drop can run. Defer creations that reuse these names until the columns and types have been removed.

Knowledge Base Used: Migration dependency ordering

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch — fixed in 84f8ecb. A dropped range is now also held for columns typed as its multirange (arrays included), and the drop_type case covers it.

…#710)

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dropping an enum together with the column that uses it plans DROP TYPE before DROP COLUMN (SQLSTATE 2BP01)

1 participant