How to perform action after SQL statement

by Milosz Galazka on December 28, 2015

Sometimes I want to monitor and log particular PostgreSQL statements or execute other kind of external action.
In order to achieve this, I am using simple notify mechanism and slightly modified sample application found in database documentation.

In this example I will print username and date added columns to standard error output after INSERT statement into users table.

This is simpler than it sounds.

Trigger

Create trigger to send a notification event after particular PostgreSQL statement, include key of the record to keep payload minimal as it must be shorter than 8000 bytes.