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
- 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?