在执行查询之前检查用户

check user before execute queries

我想在执行某些查询之前检查用户。我做了类似的事情:

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

有办法吗?

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

ALTER TYPE ... ADD VALUE (the form that adds a new value to an enum type) cannot be executed inside a transaction block.

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.
*/

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