patternsqlModerate
one trigger for multiple tables
Viewed 0 times
tablestriggeroneformultiple
Problem
i have a trigger in PostgreSql 9.1 that i want to fire on inserts of several tables. is it possible to have it affect all these tables instead of creating the trigger for all these tables?
i have 58 tables that i want to use the same insert trigger which is calling a trigger function, so i have been using
doing this for all those tables and then we have multiple schemas,so the workload is alot,can this be done in one query? then affect all tables in that schema or the whole database?
i have 58 tables that i want to use the same insert trigger which is calling a trigger function, so i have been using
create trigger tuweke
after insert on product
for each row execute procedure tuwekeAdjustextract();
create trigger tuweke
after insert on caneweightment
for each row execute procedure tuwekeAdjustextract();
...doing this for all those tables and then we have multiple schemas,so the workload is alot,can this be done in one query? then affect all tables in that schema or the whole database?
Solution
There is no option like that. A trigger belongs to a table and that is that. However, if all your triggers use the same procedure, you can easily generate a nice text output with all the
You can get all table names from a given schema with a query like this:
If now you select
instead of
CREATE TRIGGER statements. The only thing to do is to collect the desired table names and then prepend and appent the necessary parts to those.You can get all table names from a given schema with a query like this:
SELECT p.tablename
FROM pg_tables p
WHERE p.schemaname = 'public'
;If now you select
'CREATE TRIGGER tuweke
AFTER INSERT ON ' || p.tablename || '
FOR EACH ROW EXECUTE PROCEDURE tuwekeAdjustextract();
'instead of
p.tablename then you are ready. Copy the output and run it.Code Snippets
SELECT p.tablename
FROM pg_tables p
WHERE p.schemaname = 'public'
;'CREATE TRIGGER tuweke
AFTER INSERT ON ' || p.tablename || '
FOR EACH ROW EXECUTE PROCEDURE tuwekeAdjustextract();
'Context
StackExchange Database Administrators Q#33813, answer score: 11
Revisions (0)
No revisions yet.