Vous pouvez RAISE NOTICE
sur pg_trigger_depth();
Voici un exemple où nous utilisons également TG_NAME
CREATE TABLE foo (id int primary key);
CREATE FUNCTION bar()
RETURNS trigger
AS $$
BEGIN
-- Debugging information
RAISE NOTICE '[%] TRIGGER DEPTH %', TG_NAME, pg_trigger_depth();
INSERT INTO foo VALUES (42);
RETURN null;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER bar
AFTER INSERT ON foo
FOR EACH ROW
EXECUTE PROCEDURE bar();
INSERT INTO foo VALUES (41);
L'exécution de cette donne
NOTICE: [bar] TRIGGER DEPTH 1
NOTICE: [bar] TRIGGER DEPTH 2
ERROR: duplicate key value violates unique constraint "foo_pkey"
DETAIL: Key (id)=(42) already exists.
CONTEXT: SQL statement "INSERT INTO foo VALUES (42)"
PL/pgSQL function bar() line 5 at SQL statement
SQL statement "INSERT INTO foo VALUES (42)"
PL/pgSQL function bar() line 5 at SQL statement
Bien que cela aidera à déboguer la situation making a system dependent on this behavior is not a good idea.