代码之家  ›  专栏  ›  技术社区  ›  Hemadri Dasari

错误:alter type…add无法在事务块内运行

  •  1
  • Hemadri Dasari  · 技术社区  · 7 年前

    我正在尝试向PostgreSQL中现有的类型添加新的类型值。但我得到以下错误

    错误:更改类型…添加不能在事务块内运行

    我用于向类型添加新值的查询是

    ALTER TYPE public.request_type ADD VALUE "Check";
    

    我实际上正在使用node pg migrate创建的migrations文件中运行上面的查询。

    在这里 public 是我的模式。

    知道为什么会失败吗?

    编辑:

    以下查询在pgadmin中执行时执行良好

    更改类型public.request_type add value“check”;
    

    但是当我通过node pg migrate migration运行上面的命令时,它失败并抛出上面的错误。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Laurenz Albe    7 年前

    原因见以下注释: AlterEnum 在里面 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.)
    

    请注意,此限制已在中删除 commit 212fab99 ;提交消息显示:

    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.
    

    所以您可能希望很快升级到PostgreSQL v12:^)