PostgreSQL Source Code git master
tcn.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * tcn.c
4 * triggered change notification support for PostgreSQL
5 *
6 * Portions Copyright (c) 2011-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * contrib/tcn/tcn.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#include "postgres.h"
17
18#include "access/htup_details.h"
19#include "commands/async.h"
20#include "commands/trigger.h"
21#include "executor/spi.h"
22#include "lib/stringinfo.h"
23#include "utils/rel.h"
24#include "utils/syscache.h"
25
27 .name = "tcn",
28 .version = PG_VERSION
29);
30
31/*
32 * Copy from s (for source) to r (for result), wrapping with q (quote)
33 * characters and doubling any quote characters found.
34 */
35static void
36strcpy_quoted(StringInfo r, const char *s, const char q)
37{
39 while (*s)
40 {
41 if (*s == q)
44 s++;
45 }
47}
48
49/*
50 * triggered_change_notification
51 *
52 * This trigger function will send a notification of data modification with
53 * primary key values. The channel will be "tcn" unless the trigger is
54 * created with a parameter, in which case that parameter will be used.
55 */
57
60{
61 TriggerData *trigdata = (TriggerData *) fcinfo->context;
62 Trigger *trigger;
63 int nargs;
64 HeapTuple trigtuple;
65 Relation rel;
66 TupleDesc tupdesc;
67 char *channel;
68 char operation;
69 StringInfoData payload;
70 bool foundPK;
71
72 List *indexoidlist;
73 ListCell *indexoidscan;
74
75 initStringInfo(&payload);
76 /* make sure it's called as a trigger */
77 if (!CALLED_AS_TRIGGER(fcinfo))
79 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
80 errmsg("triggered_change_notification: must be called as trigger")));
81
82 /* and that it's called after the change */
83 if (!TRIGGER_FIRED_AFTER(trigdata->tg_event))
85 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
86 errmsg("triggered_change_notification: must be called after the change")));
87
88 /* and that it's called for each row */
89 if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
91 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
92 errmsg("triggered_change_notification: must be called for each row")));
93
94 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
95 operation = 'I';
96 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
97 operation = 'U';
98 else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
99 operation = 'D';
100 else
101 {
102 elog(ERROR, "triggered_change_notification: trigger fired by unrecognized operation");
103 operation = 'X'; /* silence compiler warning */
104 }
105
106 trigger = trigdata->tg_trigger;
107 nargs = trigger->tgnargs;
108 if (nargs > 1)
110 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
111 errmsg("triggered_change_notification: must not be called with more than one parameter")));
112
113 if (nargs == 0)
114 channel = "tcn";
115 else
116 channel = trigger->tgargs[0];
117
118 /* get tuple data */
119 trigtuple = trigdata->tg_trigtuple;
120 rel = trigdata->tg_relation;
121 tupdesc = rel->rd_att;
122
123 foundPK = false;
124
125 /*
126 * Get the list of index OIDs for the table from the relcache, and look up
127 * each one in the pg_index syscache until we find one marked primary key
128 * (hopefully there isn't more than one such).
129 */
130 indexoidlist = RelationGetIndexList(rel);
131
132 foreach(indexoidscan, indexoidlist)
133 {
134 Oid indexoid = lfirst_oid(indexoidscan);
135 HeapTuple indexTuple;
137
138 indexTuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexoid));
139 if (!HeapTupleIsValid(indexTuple)) /* should not happen */
140 elog(ERROR, "cache lookup failed for index %u", indexoid);
141 index = (Form_pg_index) GETSTRUCT(indexTuple);
142 /* we're only interested if it is the primary key and valid */
143 if (index->indisprimary && index->indisvalid)
144 {
145 int indnkeyatts = index->indnkeyatts;
146
147 if (indnkeyatts > 0)
148 {
149 int i;
150
151 foundPK = true;
152
153 strcpy_quoted(&payload, RelationGetRelationName(rel), '"');
154 appendStringInfoCharMacro(&payload, ',');
155 appendStringInfoCharMacro(&payload, operation);
156
157 for (i = 0; i < indnkeyatts; i++)
158 {
159 int colno = index->indkey.values[i];
160 Form_pg_attribute attr = TupleDescAttr(tupdesc, colno - 1);
161
162 appendStringInfoCharMacro(&payload, ',');
163 strcpy_quoted(&payload, NameStr(attr->attname), '"');
164 appendStringInfoCharMacro(&payload, '=');
165 strcpy_quoted(&payload, SPI_getvalue(trigtuple, tupdesc, colno), '\'');
166 }
167
168 Async_Notify(channel, payload.data);
169 }
170 ReleaseSysCache(indexTuple);
171 break;
172 }
173 ReleaseSysCache(indexTuple);
174 }
175
176 list_free(indexoidlist);
177
178 if (!foundPK)
180 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
181 errmsg("triggered_change_notification: must be called on a table with a primary key")));
182
183 return PointerGetDatum(NULL); /* after trigger; value doesn't matter */
184}
void Async_Notify(const char *channel, const char *payload)
Definition: async.c:590
#define NameStr(name)
Definition: c.h:756
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
int i
Definition: isn.c:77
void list_free(List *list)
Definition: list.c:1546
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
FormData_pg_index * Form_pg_index
Definition: pg_index.h:70
#define lfirst_oid(lc)
Definition: pg_list.h:174
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
uint64_t Datum
Definition: postgres.h:70
unsigned int Oid
Definition: postgres_ext.h:32
#define RelationGetRelationName(relation)
Definition: rel.h:549
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4836
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:1220
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:231
Definition: pg_list.h:54
TupleDesc rd_att
Definition: rel.h:112
Relation tg_relation
Definition: trigger.h:35
TriggerEvent tg_event
Definition: trigger.h:34
Trigger * tg_trigger
Definition: trigger.h:38
HeapTuple tg_trigtuple
Definition: trigger.h:36
int16 tgnargs
Definition: reltrigger.h:38
Definition: type.h:96
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220
static void strcpy_quoted(StringInfo r, const char *s, const char q)
Definition: tcn.c:36
Datum triggered_change_notification(PG_FUNCTION_ARGS)
Definition: tcn.c:59
PG_MODULE_MAGIC_EXT(.name="tcn",.version=PG_VERSION)
PG_FUNCTION_INFO_V1(triggered_change_notification)
#define TRIGGER_FIRED_BY_DELETE(event)
Definition: trigger.h:113
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:26
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:122
#define TRIGGER_FIRED_AFTER(event)
Definition: trigger.h:131
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:110
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:116
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160
const char * name