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
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?
-
I suppose not. What did you try?Nitesh Verma– Nitesh Verma2013年01月30日 09:04:30 +00:00Commented Jan 30, 2013 at 9:04
-
@Nitesh i have edited my questionindago– indago2013年01月30日 09:29:15 +00:00Commented Jan 30, 2013 at 9:29
1 Answer 1
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 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.
-
thanks for you answer,how can i implement the output? i need help in implementing this. All this triggers will be calling the same trigger function.indago– indago2013年01月30日 09:47:13 +00:00Commented Jan 30, 2013 at 9:47
Explore related questions
See similar questions with these tags.