I am trying to intercept a CREATE TABLE by an event trigger in PostgreSQL, to forbid the creation of table that does not comply to some naming rules. My code is as follow:
CREATE OR REPLACE FUNCTION e_ddl_create_table_func()
RETURNS event_trigger
LANGUAGE plpgsql
AS $$
DECLARE
obj record;
BEGIN
FOR obj IN SELECT *
FROM pg_event_trigger_ddl_commands()
WHERE command_tag in ('CREATE TABLE')
LOOP
if NOT obj.object_identity LIKE 't?_%' ESCAPE '?'
THEN
raise EXCEPTION 'The table name must begin with t_';
end if;
END LOOP;
END;
$$;
CREATE EVENT TRIGGER trg_create_table ON ddl_command_end
WHEN TAG IN ('CREATE TABLE')
EXECUTE PROCEDURE e_ddl_create_table_func();
When I try with:
CREATE TABLE t_toto3 (i INT)
I have systematically the following error:
ERROR: The table name must begin with t_
CONTEXT: fonction PL/pgSQL e_ddl_create_table_func(), ligne 11 à RAISE
What am I missing ?