Enums

ALTER TYPE ... ADD VALUE on PostgreSQL 11 and earlier

PostgreSQL 11 and earlier

Lock mode EXCLUSIVE
Risk MEDIUM
Blocks writes, other DDL
pgfence rule alter-enum-add-value
ALTER TYPE order_status ADD VALUE 'refunded';

What this does

Before PostgreSQL 12, adding an enum value takes EXCLUSIVE on the type object, the same lock as PostgreSQL 12 and later (AddEnumLabel in pg_enum.c, verified on REL9_6, REL_10 and REL_11). No table is locked. The real hazard is that the statement cannot run inside a transaction block, and most migration runners wrap each migration in one.

Safe rewrite

On PostgreSQL 11 and earlier, run ADD VALUE outside a transaction block

-- Put ALTER TYPE ... ADD VALUE in its own migration and disable the runner transaction for it.
-- On PG12+: ADD VALUE may run inside a transaction, but the new value is not usable until it commits.

Read more

More in Enums