0

I tried two ways to get table name inside Trigger function. But both of them not working. My Postgresql version is 9.5.14

1)

CREATE OR REPLACE FUNCTION log_ddl2()
  RETURNS event_trigger AS $$
DECLARE
  audit_query TEXT;
  r RECORD;
BEGIN
  IF tg_tag = 'CREATE TABLE'
  THEN
    r := pg_event_trigger_ddl_commands();
    INSERT INTO ddl_history(object_name) VALUES(r.object_identity);
  END IF;
END;

$$ LANGUAGE plpgsql;

CREATE EVENT TRIGGER log_ddl_info2 ON ddl_command_end EXECUTE PROCEDURE log_ddl2();



CREATE TABLE mytable(
    id serial PRIMARY KEY, 
    first_name text,
    age integer,
    created_at timestamp(0) without time zone default now()
 );

Above way is giving an error:

pg_event_trigger_ddl_commands() returning set of rows
  1. Second way is using TG_TABLE_NAME::regclass::text. This also not working. It is giving TG_TABLE_NAME does not exists. Is there another possible way to get table name once it is created?

1 Answer 1

2

Try

SELECT * INTO r
FROM pg_event_trigger_ddl_commands()
WHERE object_type = 'table';

I am not sure if object_type is in lower case.

Sign up to request clarification or add additional context in comments.

3 Comments

It returned public.mytable_id_seq.
I have amended the answer.
Yes now it is working fine. Thank you very much!

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.