我可以在PostgreSQL中执行以下操作吗:

WHEN (my_session <> admin)
   CREATE OR REPLACE FUNCTION testing123 () RETURNS VOID AS
   ....



   ...
   //end of function here

换句话说,如果my_session <> adminTRUE,则将创建函数。

最佳答案

为此使用DO语句,如下所示:

DO
$$DECLARE
   issuper boolean;
BEGIN
   /* check if current user is superuser */
   SELECT usesuper INTO issuper
      FROM pg_catalog.pg_user
      WHERE usename = CURRENT_USER;
   IF issuper THEN
      CREATE OR REPLACE FUNCTION testing123() RETURNS void AS ...;
   END IF;
END;$$;

10-08 12:57