I am new to Postgres (9.0.5) and have written very few functions/triggers in general. I would like to set up a trigger on my widgets table such that every time a widget is inserted, updated or deleted, the entire record is passed to a stored procedure as an argument. The procedure can then inspect the record for various changes and decide what action to take. Here’s my best attempt so far, though I know I’m way off course:
CREATE OR REPLACE TRIGGER tg_widgets
ON TABLE widgets
AFTER EVERY INSERT, UPDATED, DELETE
DO run_widget_handler
CREATE OR REPLACE PROCEDURE run_widget_handler AS
# Definitition here
# If the widget's name was changed, do X
# Else if the widget's wow_factor was changed, do Y
# Else, do Z
I’m not worried about how to implement the definition of run_widget_handler, just looking for help and writing the trigger and passing the widget to the proc. Thanks in advance.
You don’t need to “pass” the row to the trigger function (btw: there is no “procedure” in PostgreSQL, only functions).
If the function is declared as “returns trigger”, you can automatically access the complete row as “new” or “old”.
More details in the manual: http://www.postgresql.org/docs/current/static/plpgsql-trigger.html