0

What are the equal/closest alternatives in Postgres for Oracle DBMS_UTILITY.FORMAT_CALL_STACK and DBMS_UTILITY.FORMAT_ERROR_STACK ?

1 Answer 1

2

You can use GET DIAGNOSTICS or GET STACKED DIAGNOSTICS statement and read a PG_CONTEXT field.

CREATE OR REPLACE FUNCTION outer_func() RETURNS integer AS $$
BEGIN
  RETURN inner_func();
END;
$$ LANGUAGE plpgsql;

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;

SELECT outer_func();

NOTICE:  --- Call Stack ---
PL/pgSQL function inner_func() line 5 at GET DIAGNOSTICS
PL/pgSQL function outer_func() line 3 at RETURN
CONTEXT:  PL/pgSQL function outer_func() line 3 at RETURN
outer_func
------------
        1
(1 row)
Sign up to request clarification or add additional context in comments.

2 Comments

Can I use the same for DBMS_UTILITY.FORMAT_ERROR_STACK as well?
Postgres itself has not DBMS_UTILITY package. You can try to install orafce extension, and that extension has emulation of this package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.