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?

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

1 Answer 1

13

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.

answered Jan 30, 2013 at 9:41
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. Commented Jan 30, 2013 at 9:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.