1

Is there a special variable returning the name of a PostgreSQL function similar to special variable TG_NAME for triggers?

Rainer
  • 1,007
  • 1
  • 13
  • 26

1 Answers1

0

as of 9.4 I believe we have PG_CONTEXT:

https://www.postgresql.org/docs/current/static/plpgsql-control-structures.html

CREATE OR REPLACE FUNCTION inner_func() RETURNS integer AS $$
DECLARE
  stack text;
BEGIN
  GET DIAGNOSTICS stack = PG_CONTEXT;
  RAISE NOTICE E'--- Call Stack ---\n%', stack;
  RETURN 1;
END;
$$ LANGUAGE plpgsql;

for parsing the context for function name, use substring(stack from 'function (.*?) line') as in https://stackoverflow.com/a/32016935/5315974

Vao Tsun
  • 42,665
  • 8
  • 85
  • 115
  • Ok, it's a good workaround. Of course I can put this into a separate function, return second line, maybe parse out the function name before. And maybe sometimes there is a FC_NAME variable in PostgreSQL. – Rainer Jan 11 '18 at 20:08
  • this is the only way nowaday (btw quite recent - before the only solution was writing C function yourself) if you don't believe me - believe Erwin Brandstetter and Pavel Stehule (on the linked answer) :) First is a huge authority on SO, and the other is even recognised contributor https://www.postgresql.org/community/contributors/ – Vao Tsun Jan 11 '18 at 20:26