Sub topic 4
Adding and Renaming Enum Values
Assume you know basic CREATE TABLE, INSERT, SELECT, and ALTER TYPE syntax. You should also know that PostgreSQL transactions group several statements into one all-or-nothing operation.
An enum type stores one value from a fixed, ordered list of labels. PostgreSQL lets you extend that list and rename existing labels. These operations change the type definition, so they need more care than updating an ordinary table row.
Adding a value
Use ALTER TYPE ... ADD VALUE to add a label:
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'
);
ALTER TYPE order_status ADD VALUE 'refunded' AFTER 'delivered';
INSERT INTO orders (status)
VALUES ('refunded');
SELECT id, status
FROM orders
ORDER BY id;The new label becomes valid for columns that use order_status. Existing rows do not change. In this example, existing delivered rows remain delivered, while new rows can use refunded.
You can place a value before or after another value:
ALTER TYPE order_status ADD VALUE 'processing' AFTER 'pending';
ALTER TYPE order_status ADD VALUE 'failed' BEFORE 'cancelled';If you do not specify BEFORE or AFTER, PostgreSQL adds the value at the end.
For repeatable deployment scripts, use IF NOT EXISTS:
ALTER TYPE order_status
ADD VALUE IF NOT EXISTS 'refunded' AFTER 'delivered';If the label already exists, PostgreSQL emits a notice and does not add it again. This prevents a migration from failing when part of it has already run.
Transaction behavior for new values
A migration tool often wraps each migration in a transaction. That matters when you add an enum value and then try to use it immediately.
When ADD VALUE runs inside a transaction block, PostgreSQL does not allow the new label to be used until that transaction commits. The type definition changes within the transaction, but the new label is not yet usable by later statements in that same transaction.
BEGIN;
ALTER TYPE order_status ADD VALUE IF NOT EXISTS 'refunded' AFTER 'delivered';
-- Do not insert 'refunded' here in the same transaction.
COMMIT;
INSERT INTO orders (status) VALUES ('refunded');A migration that must add and use a value can use two migrations:
- Add the enum value and commit.
- In a later migration, insert or update rows using that value.
Some PostgreSQL versions have special behavior when a type and its enum value are created in the same transaction. Do not rely on that exception for an existing production type. The safe general rule is to commit an added value before using it.
The position of a label affects comparisons and sorting. Adding a label in the middle can therefore change the result of queries that compare enum values.
Renaming a value
Use RENAME VALUE when the meaning stays the same but the label needs a new spelling:
ALTER TYPE order_status
RENAME VALUE 'cancelled' TO 'canceled';This changes the enum label stored in rows of orders. It does not require an update statement for every row. Queries and inserts must use the new spelling after the rename:
SELECT id, status
FROM orders
WHERE status = 'canceled';
INSERT INTO orders (status)
VALUES ('canceled');The enum's position does not change. If cancelled was the last label, canceled remains the last label. Comparisons still use the original enum ordering.
Renaming is a database compatibility change. Existing application code that sends 'cancelled' will fail after the rename, so coordinate the database migration with the application deployment. A common approach is to deploy application code that can handle the new spelling, rename the value, and then remove support for the old spelling.
You can check whether a label exists before planning a migration by reading PostgreSQL's catalog:
SELECT enumlabel, enumsortorder
FROM pg_enum
WHERE enumtypid = 'order_status'::regtype
ORDER BY enumsortorder;enumlabel is the text of each label. enumsortorder records its ordering within the enum type.
Limits and migration choices
Enum labels are identifiers internally, so each label is limited by PostgreSQL's identifier length, normally 63 bytes. A label can contain spaces or punctuation when written as a quoted SQL string, but simple, stable labels are easier to use in application code.
PostgreSQL supports adding and renaming enum values directly. It does not provide a direct command to remove one value. If a value must disappear, you need a replacement-type migration that converts the column and recreates the type. This usually requires checking existing rows, defaults, indexes, and application code first.
Use an enum when the set is small, stable, and part of the database schema. Use a lookup table instead when users may add values, values need descriptions or other metadata, or values must be removed frequently.
Exercise
A migration runs ALTER TYPE order_status ADD VALUE 'refunded'; and then inserts an order with status 'refunded' before committing. What should you change?
Show answer
Commit the transaction after adding the value, then use the new value in a later transaction or migration.