首页 > 解决方案 > 在执行查询之前检查用户

问题描述

我想在执行一些查询之前检查用户。我做这样的事情:

DO $do$
  BEGIN
  IF(current_user=$$usrA$$) THEN
    ALTER TYPE enum_to_change ADD VALUE $$myNewValue$$;
  ELSE
    SELECT $$ERROR$$; /*must crash here*/
  END IF;
END $do$;

它不起作用:

ERROR:  ALTER TYPE ... ADD cannot be executed from a function or multi-command string
CONTEXT:  SQL statement "ALTER TYPE e ADD VALUE 'myNewValue'"
PL/pgSQL function inline_code_block line 4 at SQL statement

有没有办法做到这一点 ?

标签: sqlpostgresql-9.1

解决方案


https://www.postgresql.org/docs/current/static/sql-altertype.html

ALTER TYPE ... ADD VALUE(向枚举类型添加新值的形式)不能在事务块内执行。

https://www.postgresql.org/message-id/3543.1317224437%40sss.pgh.pa.us

The comment beside the code says what it breaks:

case T_AlterEnumStmt: /* ALTER TYPE (enum) */

/*
* We disallow this in transaction blocks, because we can't cope
* with enum OID values getting into indexes and then having their
* defining pg_enum entries go away.
*/

看起来你做不到。这意味着行为...


推荐阅读