14

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

David S
  • 12,967
  • 12
  • 55
  • 93
  • This feels like one of those issues where you've decided on a solution to the real problem and you're asking for help with that solution. What's the background? Why do you need that information? What problem are you trying to solve with it? – Craig Ringer Sep 27 '12 at 00:02
  • 3
    @CraigRinger. You're right. I don't really want to write a 500 word essay of a question saying what I'm doing and all of the requirements, timelines, past experiences, etc that went into the solution/design that I've settled on. I was just trying to get a question answered about one tiny piece of it. – David S Sep 27 '12 at 00:35

3 Answers3

16

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;
dvasdekis
  • 304
  • 2
  • 8
  • Why not `fcesig := substring(substring(stack from 'unction (.*)') from 'function (.*?) line'); ` ... then you get the name of the function that is calling get_curr_fx_name(), which is what you want, isn't it? – Tom Warfield Feb 06 '21 at 13:14
  • ... pulling from the second line of the call stack, instead of the first? – Tom Warfield Feb 06 '21 at 13:20
7

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.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • I knew about TG_NAME and TG_ARGV. They are part of the reason that I thought there might be something else like it for a "normal" (i.e. non-trigger) function. But, I think I'm probably out of luck. :\ – David S Sep 27 '12 at 00:39
  • Lol, current_query() in a function returns "SELECT current_query()" for me, but this is Greenplum so it might be broken. – PhilHibbs Jul 27 '17 at 14:06
  • @PhilHibbs: that is because when issuing the query "SELECT current_query()" then the current query is "SELECT current_query()". – Matty Jan 23 '18 at 13:30
7

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

Pavel Stehule
  • 42,331
  • 5
  • 91
  • 94