0

What's the syntax used to write a Postgresql event trigger in Python? This

CREATE OR REPLACE FUNCTION log_schema_changes()
RETURNS EVENT_TRIGGER
 LANGUAGE plpython3u 
 AS $$ 
import plpy
plyp.warning(TD)
$$; 


CREATE EVENT TRIGGER log_ddl_changes
ON ddl_command_start
WHEN TAG IN ('CREATE TABLE', 'ALTER TABLE', 'DROP TABLE', 'CREATE INDEX', 'ALTER INDEX', 'DROP INDEX')
EXECUTE FUNCTION log_schema_changes();

generates the error message: ERROR: trigger functions can only be called as triggers

Note: version info psql (15.6 (Ubuntu 15.6-1.pgdg20.04+1), server 15.7 (Ubuntu 15.7-1.pgdg22.04+1))

5
  • perhaps your search_path is different from audit? So there is one create function, but in CREATE EVENT TRIGGER you call another function?
    – Melkij
    Commented Jul 16 at 14:06
  • Which of the two statements causes the error? Commented Jul 17 at 3:31
  • @Melkij yes, that was a typo
    – gerardw
    Commented Jul 31 at 17:52
  • @LaurenzAlbe The first part: CREATE OR REPLACE FUNCTION log_schema_changes() RETURNS EVENT_TRIGGER LANGUAGE plpython3u AS $$ import plpy plyp.warning(TD) $$;
    – gerardw
    Commented Jul 31 at 17:53
  • Hm, yes, I can reproduce the error. No idea what causes it; I know little about PL/Python. Commented Jul 31 at 18:58

0

Your Answer

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

Browse other questions tagged or ask your own question.