Skip to main content

Change events

There are many ways to consume data changes from Postgres.

Polling

A simple way to pick up on changes for a low workload system is to poll for them. Just query on a loop and handle any new items.

LISTEN/NOTIFY

Alternatively, for a more reactive approach, you can LISTEN for changes in a Postgres client.

LISTEN channel_name;

And then NOTIFY:

-- Either
NOTIFY channel_name, 'something happened';

-- OR
SELECT pg_notify('channel_name', 'something happened');

Usually wrapped up inside a trigger to generate the notification automatically when data changes.

Logical replication

Postgres provides Logical replication. This allows you to subscribe to a publication of changes. This is more reliable than LISTEN/NOTIFY because it tolerates downtime and disconnection.

There are many ways to consume Logical replication, such as: