在PostgreSQL中可以触发知道调用哪个函数吗?

时间:2015-10-27 09:52:58

标签: postgresql plpgsql

我有一个触发器如下:

CREATE TRIGGER A
  AFTER INSERT OR UPDATE OR DELETE
  ON TableA
  FOR EACH ROW
  EXECUTE PROCEDURE func1();

和触发功能:

CREATE OR REPLACE FUNCTION func1()
  RETURNS trigger AS
$BODY$
begin
    insert into tableinfo values(...);
    return new;
end ;
$BODY$
  LANGUAGE plpgsql VOLATILE

现在我有许多从TableA插入/更新/删除的函数。 触发器中有没有办法知道哪个函数调用它?

类似的东西:

CREATE OR REPLACE FUNCTION func1()
  RETURNS trigger AS
$BODY$
begin
    if functionname='insertnewrecord' or functionname='deleterecord'  then 
        insert into tableinfo values(...);
         return new;
    else
       return NULL;
    end if;
end ;
$BODY$
  LANGUAGE plpgsql VOLATILE

可行吗?我可以根据调用它的函数来限制触发器做某事吗?

1 个答案:

答案 0 :(得分:2)

您可以使用get diagnostics CREATE OR REPLACE FUNCTION func1() RETURNS trigger LANGUAGE plpgsql AS $function$ declare stack text; begin get diagnostics stack = pg_context; raise notice E'--- Call Stack ---\n%',stack; ... return new; end; $function$; 获取来自调用堆栈的信息,如here所述。

类似的东西:

CREATE OR REPLACE FUNCTION insertnewrecord()
 RETURNS void
 LANGUAGE plpgsql
AS $function$
begin
  insert into TableA values(...);
end;
$function$

测试功能:

select insertnewrecord();
NOTICE:  --- Call Stack ---
PL/pgSQL function func1() line 6 at GET DIAGNOSTICS
SQL statement "insert into tbl values(10)"
PL/pgSQL function insertnewrecord() line 3 at SQL statement
CONTEXT:  SQL statement "insert into tbl values(10)"
PL/pgSQL function insertnewrecord() line 3 at SQL statement
 insertnewrecord 
-----------------

(1 row)

结果:

stack

所以你可以在func1()中分析location ~ \.m3u8 { root /mnt/hls; add_header Cache-Control no-cache; set $access 1; } location ~ \.ts { if ($access = "") { return 403; } root /mnt/hls; add_header Cache-Control no-cache; } 并根据它做出决定,虽然它不是很方便。