pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

ALTER COLUMN TYPE omits USING clause for non-implicitly-castable built-in type changes (SQLSTATE 42804)

Open saosebastiao opened this issue 3 months ago • 0 comments

Summary

When a column's type changes between two built-in types that PostgreSQL cannot implicitly cast (e.g. text → integer), pgschema plan emits a bare ALTER TABLE ... ALTER COLUMN ... TYPE integer with no USING clause. PostgreSQL rejects this at apply time with SQLSTATE 42804, so pgschema apply (and apply-plan) fails on the change.

This is related to #190 / #209, but not covered by that fix: #209 adds the USING clause only when the target is a non-built-in type (e.g. an enum). The text → integer case has a built-in target and takes the fall-through path, so no USING clause is generated.

Version

  • pgschema 1.12.0 (also reproduced against current main by code inspection — see below)
  • PostgreSQL 18

Reproducer

Old (current DB) state:

CREATE TABLE nokia_cell_info (
  arfcn_dl text
);

New (desired) state:

CREATE TABLE nokia_cell_info (
  arfcn_dl integer
);

pgschema plan output (observed — BROKEN):

ALTER TABLE nokia_cell_info ALTER COLUMN arfcn_dl TYPE integer;

pgschema apply result:

ERROR: column "arfcn_dl" cannot be cast automatically to type integer (SQLSTATE 42804)
HINT:  You might need to specify "USING arfcn_dl::integer".

Expected plan output:

ALTER TABLE nokia_cell_info ALTER COLUMN arfcn_dl TYPE integer USING arfcn_dl::integer;

The same failure occurs for any non-binary-coercible built-in target, e.g. text/varchar → integer / numeric / boolean / uuid / date / json (the last is already noted as a known gap in the code comment cited below).

Root cause

In internal/diff/column.go, needsUsingClause(oldType, newType) returns false for any built-in → built-in change:

// For built-in → built-in types we assume an implicit cast is available.
return false

That assumption is false for text-like → numeric/boolean/uuid/etc. The function's own doc comment already acknowledges the gap:

For built-in → built-in conversions we still assume PostgreSQL provides an implicit cast in most cases; callers should be aware that some edge cases (e.g. certain text → json conversions) may still need manual adjustment.

Confirmed present in v1.12.0 and unchanged on main at the time of filing.

Design note

I understand a USING cast expression can't always be inferred safely — the user may need to choose the conversion semantics (trim, nullif, a lookup, etc.), and a blind col::newtype could change behavior. So "always emit USING" isn't right either.

A reasonable approach, mirroring the enum path from #209: for type changes that are not binary-coercible / not implicitly assignable, emit a documented default USING <col>::<newtype> (so the plan at least applies), with a way to override the cast expression (e.g. a per-column annotation / config) when the default is wrong. At minimum, the plan should not silently emit SQL that PostgreSQL is guaranteed to reject.

Workaround

Apply the ALTER ... TYPE ... USING <col>::<newtype> by hand; the desired-state file stays correct and a subsequent plan reports "No changes detected".

saosebastiao avatar Aug 11 '26 18:08 saosebastiao