The first trigger we will work on simply sends a notice back to the database client each time the trigger is fired and provides some feedback on its firing conditions:
CREATE OR REPLACE FUNCTION notify_trigger() RETURNS TRIGGER AS $$ BEGIN RAISE NOTICE 'Hi, I got % invoked FOR % % % on %', TG_NAME, TG_LEVEL, TG_WHEN, TG_OP, TG_TABLE_NAME; END; $$ LANGUAGE plpgsql;
Next, we need a table to bind this function to the following line of code:
CREATE TABLE notify_test(i int);
Now we are ready to define the trigger. As this is a simple example, we define a trigger which is invoked on INSERT
and calls the function once on each row:
CREATE TRIGGER notify_insert_trigger AFTER INSERT ON notify_test FOR EACH ROW EXECUTE PROCEDURE notify_trigger();
Let's test this out:
postgres=# INSERT INTO...