DEV Community

Ali Ahmed
Ali Ahmed

Posted on Originally published at aliahmed.si

Removing an allowed value from a Postgres column: CHECK constraints, defaults, and why enums can't drop a value

To stop a column accepting a value, say a status of 'New', do three things in this order, in one transaction: move the existing rows to a value that stays, replace the CHECK constraint with one that leaves the old value out, and change the column default if it was the old value. Skip the first step and Postgres refuses the new constraint: "check constraint … is violated by some row". Skip the last and every insert that relies on the default fails, because the default now violates the check. If the column is an enum instead, there's no shortcut: PostgreSQL 17.5 answers alter type … drop value with "dropping an enum value is not implemented", so you either live with the extra value or rebuild the type. In a CRM I built, this is how "New" was retired as a prospect status, so a stale client can't create a prospect with no column on the board.

Retiring the status New in PostgreSQL 17.5. Wrong order: adding the new check while New rows exist fails with check constraint is violated by some row. Right order in one transaction: update New rows to Replied, add the check without New, set the default to Replied. Afterwards an insert using the default gets Replied, and a stale client inserting New is rejected by the check. Forgetting the default: inserts that rely on it violate the check. Enums: alter type drop value is not implemented; only renaming a value works.

The order matters more than the statements.

Why replace the check instead of loosening it?

The status list lived in a CHECK constraint on a text column:

before

status text not null default 'New'
  check (status in ('New', 'Contacted', 'Replied', 'Converted'))
Enter fullscreen mode Exit fullscreen mode

The product decision was that only people who reply are real leads, so prospects start as "Replied" and "New" disappears. The board had no "New" column any more, and the same app had already shown that a status without a column means invisible records. So the database has to refuse "New" outright, even from an older client that still sends it, rather than merely stop offering it in the UI.

What's the safe order?

0019_prospects_start_replied.sql

begin;
update prospects set status = 'Replied' where status = 'New';
alter table prospects drop constraint if exists prospects_status_check;
alter table prospects add constraint prospects_status_check
  check (status in ('Contacted', 'Replied', 'Converted'));
alter table prospects alter column status set default 'Replied';
commit;
Enter fullscreen mode Exit fullscreen mode

What each step protects against, from the test on PostgreSQL 17.5:

Step skipped or misordered What happens
Add the new check before moving rows check constraint "prospects_status_check" … is violated by some row, and nothing changes
Move rows and replace the check (correct) Rows 1 and 3 become Replied; inserting 'New' from a stale client is rejected
Leave the default as 'New' Every insert that omits status fails with the same constraint violation
Set the default to 'Replied' (correct) An insert without a status gets Replied

Running it in one transaction means there's never a moment when the table has no status check, and if any statement fails, none of them apply. Adding a constraint validates every row, which locks the table while it scans. On a large table, add it not valid first and run alter table … validate constraint separately, which only needs a lighter lock.

What about history rows that say "New"?

Leave them. In the CRM, a status-history table records each change with its old and new value, and past changes from "New" are true facts about the past. The constraint belongs on the current status, not on the history, and the migration only touched prospects. It's worth checking before a migration like this whether any other table stores the same values and whether it should change at all.

Can you remove a value from a Postgres enum?

Not directly:

PostgreSQL 17.5

alter type stage drop value 'New';   -- ERROR: dropping an enum value is not implemented
alter type stage rename value 'New' to 'Replied_old';   -- works
Enter fullscreen mode Exit fullscreen mode

Postgres can add and rename enum values but not remove them, because existing rows and indexes may still hold the value. The options are:

  • Rebuild the type: create a new enum without the value, move rows off the old value, convert the column with alter column … type new_type using status::text::new_type, then drop the old type. It rewrites the table.
  • Keep the value, refuse it: leave the enum alone and add a CHECK that excludes the retired value.
  • Don't use an enum for values that change: a text column with a CHECK constraint, as here, or a lookup table with a foreign key, can be changed with ordinary DDL.

Summary

  • Retire an allowed value by moving rows → replacing the check → changing the default, in one transaction.
  • Wrong order gives "violated by some row"; a stale default makes every default insert fail.
  • Constrain the current state, not the history.
  • Enums can't drop a value in PostgreSQL 17.5; prefer a CHECK or a lookup table for lists that change.

The same CRM's schema got foreign key indexes and an RLS policy that lets users delete their own rows, and its migrations taught why they should only be applied one way.


Written by Ali Ahmed (QuantumBreakz), AI engineer in Lahore · Original article

Top comments (0)