20

In a pgsql event trigger on tag ALTER TABLE, I wish to know which table is being altered.

The pg variables do not cover this, nor do the variables exposed by GET STACKED DIAGNOSTICS.

With variables available, is there any way within the trigger function itself to see the text of the SQL command responsible for initiating the function.

for example, if

ALTER TABLE base1 ADD COLUMN col1 int;

were responsible for calling the event trigger, is there any way within the event trigger to see then ALTER TABLE base1 ADD COLUMN col1 int text itself?

3
  • 13
    SELECT current_query(). But it'll only show the top level query, which isn't necessarily what fired the event trigger - say, if you did an ALTER TABLE with a PL/PgSQL function. Event triggers will be extended to add much more information about the command, but are currently very limited in 9.3/9.4 . May 6, 2014 at 7:21
  • @CraigRinger - thanks. looking forward to extensions!
    – cc young
    May 6, 2014 at 7:34
  • 3
    If you write an event trigger in C, you have also the parse tree of the command. postgresql.org/docs/9.3/static/event-trigger-interface.html Surely not the best approach to rely on that, but its something until this feature is extended.
    – pozs
    May 6, 2014 at 7:41

2 Answers 2

28

Starting from PostgreSQL 9.5, function pg_event_trigger_ddl_commands() is available for ddl_command_end event triggers. Using the TAG filter, it may be used for processing any ALTERed table. object_identity (or objid) may be used to solve the original problem of knowing which table has been ALTERed. As for getting the complete command, it is available, too, but it is of an internal type pg_ddl_command.

CREATE TABLE t (n INT);

CREATE FUNCTION notice_event() RETURNS event_trigger AS $$
DECLARE r RECORD;
BEGIN
    FOR r IN SELECT * FROM pg_event_trigger_ddl_commands() LOOP
        RAISE NOTICE 'caught % event on %', r.command_tag, r.object_identity;
    END LOOP;
END;
$$
LANGUAGE plpgsql;

CREATE EVENT TRIGGER tr_notice_alter_table
  ON ddl_command_end WHEN TAG IN ('ALTER TABLE')
  EXECUTE PROCEDURE notice_event();

ALTER TABLE t ADD c CHAR;

outputs: NOTICE: caught ALTER TABLE event on public.t

4
  • how to grab some more data than tg_tag and tg_event in case of ddl_command_start?
    – andilabs
    Apr 19, 2019 at 11:13
  • 3
    Is there a way to get the actual SQL that was executed? pg_event_trigger_ddl_commands() returns one column command but it seems the content is not accessible.
    – user330315
    Jul 11, 2019 at 7:17
  • 1
    @a_horse_with_no_name I think you can use current_query() to catch it.
    – Sotis
    Feb 3, 2022 at 14:54
  • So far the best answer I have found is that there isn't actually any way to use the command column. It appears to be something like an incomplete implementation according to this thread: postgresql.org/message-id/… Apr 19 at 17:48
0

I've found that the command column is basically a dead end, unless you are capable of making your own c-function to process the data. For everyone else, this solution seems to work:

  1. Install the pgddl extension https://github.com/lacanoid/pgddl
  2. Use something like SELECT ddlx_create(objid) FROM pg_event_trigger_ddl_commands() as the ddl text for each object.

I've tested it and it works just about exactly what I would hope for.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.