Skip to content

avoidAlterEnumAddValue

Group correctness · Since 0.25.0
Diagnostic lint/avoidAlterEnumAddValue

Sources: inspired by pgfence/alter-enum-add-value

Description

ALTER TYPE ... ADD VALUE cannot run inside a transaction block in older Postgres versions.

In Postgres versions before 12, ALTER TYPE ... ADD VALUE cannot be executed inside a transaction block at all. On Postgres 12+, the operation is fast (metadata-only), but the new enum value cannot be used in the same transaction until it is committed.

Examples

Invalid

alter type my_enum add value 'new_value';
code-block.sql:1:1 lint/avoidAlterEnumAddValue ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  ! ALTER TYPE ... ADD VALUE cannot be used in a transaction block before Postgres 12.

  > 1 │ alter type my_enum add value 'new_value';
      │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2 │ 

  i On Postgres 12+, the operation is fast but the new value cannot be used in the same transaction until committed.

Valid

alter type my_enum rename value 'old_value' to 'new_value';

How to configure

{
  "linter": {
    "rules": {
      "avoidAlterEnumAddValue": "error"
    }
  }
}

How to suppress

Suppress this diagnostic with a comment:

-- pgls-ignore avoidAlterEnumAddValue