Sub topic 6
Removing an Enum Value by Replacing the Type
This chapter assumes that you know basic PostgreSQL DDL, such as CREATE TYPE, ALTER TABLE, and ALTER TYPE. It also assumes that you understand defaults, casts, and transactions.
PostgreSQL does not support removing one value from an enum type with a command such as ALTER TYPE ... DROP VALUE. Enum labels are part of the type definition, so removing a label requires replacing the type.
A safe replacement has this general shape:
- Decide what to do with rows that use the value.
- Create a replacement enum without that value.
- Change every dependent column to the replacement type.
- Drop the old type.
- Rename the replacement type to the original name.
Why PostgreSQL requires a replacement
Suppose an application no longer supports the cancelled order status:
CREATE TYPE order_status AS ENUM (
'pending',
'paid',
'shipped',
'delivered',
'cancelled'
);
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
status order_status NOT NULL DEFAULT 'pending'
);The following command is not valid PostgreSQL:
ALTER TYPE order_status DROP VALUE 'cancelled';Instead, create another enum containing only the labels that should remain:
CREATE TYPE order_status_new AS ENUM (
'pending',
'paid',
'shipped',
'delivered'
);The new type is independent of order_status, even though it has similar labels.
Resolve existing rows first
Before changing the column type, find rows that use the value being removed:
SELECT id, status
FROM orders
WHERE status = 'cancelled';You must choose a business rule for these rows. You might archive them, delete them, or change their status. The database cannot make that decision for you.
For example, this migration maps cancelled orders to pending:
UPDATE orders
SET status = 'pending'
WHERE status = 'cancelled';This is only an example. Mapping a cancelled order to a pending order may be incorrect for your application. In many systems, an archive table or a separate historical status is a better choice.
Convert the column through text
The old and new enum types are different types. PostgreSQL cannot directly cast one enum type to another. Both types can, however, be converted through their text labels.
The column has a default that uses the old enum type. Drop that default before changing the column type:
ALTER TABLE orders
ALTER COLUMN status DROP DEFAULT;
ALTER TABLE orders
ALTER COLUMN status TYPE order_status_new
USING status::text::order_status_new;
ALTER TABLE orders
ALTER COLUMN status SET DEFAULT 'pending';The USING expression controls how existing values are converted:
status::textconverts the old enum label to text.::order_status_newconverts that text to a label in the replacement enum.
The conversion fails if any row still contains a label that does not exist in order_status_new. That failure protects you from silently changing an unsupported value.
After the column has been converted, the old type is no longer needed for this table:
DROP TYPE order_status;
ALTER TYPE order_status_new RENAME TO order_status;The final type name is again order_status, so application queries do not need to change.
A complete migration
The following example shows the sequence in one transaction. The UPDATE statement represents a deliberate business decision and should be changed to match your application.
BEGIN;
CREATE TYPE order_status_new AS ENUM (
'pending',
'paid',
'shipped',
'delivered'
);
UPDATE orders
SET status = 'pending'
WHERE status = 'cancelled';
ALTER TABLE orders
ALTER COLUMN status DROP DEFAULT;
ALTER TABLE orders
ALTER COLUMN status TYPE order_status_new
USING status::text::order_status_new;
ALTER TABLE orders
ALTER COLUMN status SET DEFAULT 'pending';
DROP TYPE order_status;
ALTER TYPE order_status_new RENAME TO order_status;
COMMIT;A transaction makes the replacement appear as one atomic change to other sessions. If a statement fails, PostgreSQL can roll back the transaction instead of leaving the table halfway through the migration.
Dependencies and operational risks
An enum can be used by more than one table or column. You must convert every dependent column before dropping the old type. The same applies to enum columns in other schemas and to columns inside composite types.
Defaults often need special handling because a default expression can retain the old enum type. Drop each affected default before conversion and recreate it afterward.
Views, functions, generated expressions, casts, and application code may also refer to the enum. Inspect these dependencies before production work. A view that exposes an enum column might need to be recreated, depending on how PostgreSQL records its dependency.
The ALTER TABLE ... TYPE operation can lock the affected table. Run the migration during a suitable maintenance window, test it on a copy of production data, and measure how long it takes.
| Situation | Recommended action |
|---|---|
| A label is obsolete and no rows use it | Replace the enum after checking dependencies |
| Rows still use the label | Resolve or migrate those rows first |
| Labels change frequently | Consider a lookup table or a text column with a `CHECK` constraint |
| Several columns use the enum | Convert every dependent column before dropping the old type |
When replacement is the wrong approach
Replacing an enum is useful when the allowed labels are stable and the removed label truly must disappear. It is less attractive when labels change often, when administrators must add labels without deployments, or when labels need descriptions, translations, or other metadata.
A lookup table handles those requirements better. A foreign key can prevent invalid values, and rows in the lookup table can store names, display order, descriptions, and an active flag.
Exercise
Why must the status default be dropped before changing the column from order_status to order_status_new?
Show answer
The default expression is associated with the old enum type. Dropping it removes that dependency, allowing the column to change type. The migration then creates a new default using the replacement type.