error: ALTER TYPE … ADD cannot run inside a transaction block

后端 未结 4 1392
温柔的废话
温柔的废话 2021-02-12 11:22

I am trying to add new type value to my existing types in PostgreSQL. But I get the following error

error: ALTER TYPE ... ADD cannot run inside a transact

4条回答
  •  渐次进展
    2021-02-12 12:14

    The reason is given in the following comment in AlterEnum in src/backend/commands/typecmds.c:

    /*
     * Ordinarily we disallow adding values within transaction blocks,
     * because we can't cope with enum OID values getting into indexes and
     * then having their defining pg_enum entries go away.  However, it's
     * okay if the enum type was created in the current transaction, since
     * then there can be no such indexes that wouldn't themselves go away
     * on rollback.  (We support this case because pg_dump
     * --binary-upgrade needs it.)
    

    Note that this restriction has been removed in commit 212fab99; the commit message reads:

    To prevent possibly breaking indexes on enum columns, we must keep
    uncommitted enum values from getting stored in tables, unless we
    can be sure that any such column is new in the current transaction.
    
    Formerly, we enforced this by disallowing ALTER TYPE ... ADD VALUE
    from being executed at all in a transaction block, unless the target
    enum type had been created in the current transaction.  This patch
    removes that restriction, and instead insists that an uncommitted enum
    value can't be referenced unless it belongs to an enum type created
    in the same transaction as the value.  Per discussion, this should be
    a bit less onerous.  It does require each function that could possibly
    return a new enum value to SQL operations to check this restriction,
    but there aren't so many of those that this seems unmaintainable.
    

    So you might want to upgrade to PostgreSQL v12 some time soon :^)

提交回复
热议问题