Working on a simple "Hey, I'm called" trigger

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 ...

Get PostgreSQL Server Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.