9

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?

2
  • I suppose not. What did you try? Jan 30, 2013 at 9:04
  • @Nitesh i have edited my question
    – indago
    Jan 30, 2013 at 9:29

1 Answer 1

12

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.

1
  • 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
    Jan 30, 2013 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.