tcn

Triggered change notifications

Overview

PackageVersionCategoryLicenseLanguage
tcn1.0FUNCPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
4920tcnNoYesNoYesYesNo-
Relatedpg_idkit pgx_ulid pg_uuidv7 permuteseq pg_hashids sequential_uuids topn quantile

Version

PG18PG17PG16PG15PG14
1.01.01.01.01.0

Install

Note: This is a built-in contrib extension of PostgreSQL

CREATE EXTENSION tcn;

Usage

tcn: triggered change notifications via LISTEN/NOTIFY

Provides a trigger function that sends NOTIFY events with information about changed rows, enabling asynchronous change tracking.

CREATE EXTENSION tcn;

Trigger Function

FunctionDescription
triggered_change_notification()Send NOTIFY on row changes with primary key info

Optional parameter: custom channel name (defaults to tcn).

Notification Payload Format

"table_name",operation,"column"='value',"column"='value'

Operations: I (INSERT), U (UPDATE), D (DELETE).

Examples

CREATE TABLE tcndata (
  a int NOT NULL,
  b date NOT NULL,
  c text,
  PRIMARY KEY (a, b)
);

-- Attach the trigger
CREATE TRIGGER tcndata_tcn
  AFTER INSERT OR UPDATE OR DELETE ON tcndata
  FOR EACH ROW
  EXECUTE FUNCTION triggered_change_notification();

-- Listen for notifications
LISTEN tcn;

-- Changes trigger notifications:
INSERT INTO tcndata VALUES (1, '2024-01-01', 'test');
-- Notification: "tcndata",I,"a"='1',"b"='2024-01-01'

UPDATE tcndata SET c = 'updated' WHERE a = 1;
-- Notification: "tcndata",U,"a"='1',"b"='2024-01-01'

DELETE FROM tcndata WHERE a = 1;
-- Notification: "tcndata",D,"a"='1',"b"='2024-01-01'

-- Use a custom channel name
CREATE TRIGGER my_trigger
  AFTER INSERT OR UPDATE OR DELETE ON my_table
  FOR EACH ROW
  EXECUTE FUNCTION triggered_change_notification('my_channel');

Last Modified 2026-03-12: add pg extension catalog (95749bf)