Blog / PostgreSQL

Never Miss a Change Again: How to Create PostgreSQL Triggers for Automated Database Logging

Are you tired of manually keeping track of changes to your database tables? Do you wish there was an easier way to log updates and keep a record of all the changes that have been made? If so, you're in luck! In this tutorial, we'll show you how to create a PostgreSQL trigger that automatically logs changes to a specific column in your table.

Let's say you have a table called leads, which contains information about potential customers. You want to keep track of changes to the status column, which indicates whether a lead has been automatically assigned to a salesperson or not. Every time the status column is updated, you want to create a new record in a separate status_log table, which contains the lead_id, current time, and new value of status.

With a PostgreSQL trigger, you can automate this process and save yourself the hassle of manually tracking changes. In this tutorial, we'll walk you through the steps of creating a trigger function in PostgreSQL and using it to log changes to your database. Even if you're a junior developer or new to PostgreSQL, you'll find this tutorial easy to follow and understand.

So, let's get started and learn how to create a PostgreSQL trigger that automatically logs changes to your database!

Create a function

You can create a trigger function in PostgreSQL that executes whenever the status column in the leads table is updated. This function will insert a new record into the status_log table with the lead_id, current time, and new value of status.

Here's an example of how to create the trigger function:

CREATE OR REPLACE FUNCTION log_lead_status_changes()
RETURNS TRIGGER AS $$
BEGIN
  IF NEW.status <> OLD.status THEN -- only log changes
    INSERT INTO status_log (lead_id, time, new_status)
      VALUES (NEW.id, now(), NEW.status);
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

In this example, the log_lead_status_changes function checks whether the status column has changed by comparing the NEW and OLD values of the row. If the value has changed, it inserts a new record into the status_log table with the lead_id, current time, and new value of status. Finally, it returns the NEW row to indicate that the trigger function has completed successfully.

Create Trigger

To create the trigger that calls this function, you can use the following SQL statement:

CREATE TRIGGER lead_status_change
AFTER UPDATE OF status ON leads
FOR EACH ROW
EXECUTE FUNCTION log_lead_status_changes();

This creates a trigger called lead_status_change that fires AFTER an update to the status column of the leads table, FOR EACH ROW of the updated table. The trigger calls the log_lead_status_changes function that you defined earlier.

With this trigger in place, every time the status column is updated in the leads table, a new record will be inserted into the status_log table with the lead_id, current time, and new value of status.

Now you can write a simple queue in your backend that will check the status_log table and send notifications of changes. And then remove the record from the log table.