Getting name of the current function inside of the function with plpgsql

荒凉一梦 提交于 2019-12-19 14:55:31

问题


Is there anyway from within a plpgsql function that you can get the name of the function? Or even the OID of the function?

I know there are some "special" variables (such as FOUND) within plpgsql, but there doesn't seem to be any way of getting this. (Although, I've read where it seems to be possible if your function is written in C). It's not critical, but it would make something I'm doing a little nicer/less fragile.

I'm using PostgreSQL v. 9.1.5


回答1:


For triggers use TG_NAME to get the name of the trigger (not the trigger function) fired.

You also have current_query() to get the top level query executed by the application, the root cause for your function's execution. It won't show you any intermediate functions.

Otherwise, not really AFAIK and I did go looking for it a while ago when I wanted to print a "current function stack" for debugging. Others may know more.

UPDATE: In Pg 9.4 and above you can also use PG_CONTEXT to the call stack, but not just the current function name.




回答2:


As of Postgres 9.4, the below function will return its own name:

CREATE OR REPLACE FUNCTION your_schema.get_curr_fx_name()
RETURNS text AS  $$
DECLARE
  stack text; fcesig text;
BEGIN
  GET DIAGNOSTICS stack = PG_CONTEXT;
  fcesig := substring(stack from 'function (.*?) line');
  RETURN fcesig::regprocedure::text;
END;
$$ LANGUAGE plpgsql;



回答3:


Update: possibility to take call stack is available in PostgreSQL 9.4

No, there is no way how to get name of currently executed function in plpgsql function.

Some year ago I wrote functions for access to call stack - it is part of orafce. You can get last function from stack



来源:https://stackoverflow.com/questions/12611596/getting-name-of-the-current-function-inside-of-the-function-with-plpgsql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!