Age Owner Branch data TLA Line data Source code
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 : :
164 tgl@sss.pgh.pa.us 26 :CBC 2 : PG_MODULE_MAGIC_EXT(
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 : : */
35 : : static void
4979 rhaas@postgresql.org 36 : 15 : strcpy_quoted(StringInfo r, const char *s, const char q)
37 : : {
38 [ - + ]: 15 : appendStringInfoCharMacro(r, q);
39 [ + + ]: 70 : while (*s)
40 : : {
41 [ - + ]: 55 : if (*s == q)
4979 rhaas@postgresql.org 42 [ # # ]:UBC 0 : appendStringInfoCharMacro(r, q);
4979 rhaas@postgresql.org 43 [ - + ]:CBC 55 : appendStringInfoCharMacro(r, *s);
44 : 55 : s++;
45 : : }
46 [ - + ]: 15 : appendStringInfoCharMacro(r, q);
47 : 15 : }
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 : : */
56 : 2 : PG_FUNCTION_INFO_V1(triggered_change_notification);
57 : :
58 : : Datum
59 : 5 : triggered_change_notification(PG_FUNCTION_ARGS)
60 : : {
61 : 5 : 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 : 5 : StringInfo payload = makeStringInfo();
70 : : bool foundPK;
71 : :
72 : : List *indexoidlist;
73 : : ListCell *indexoidscan;
74 : :
75 : : /* make sure it's called as a trigger */
76 [ + - - + ]: 5 : if (!CALLED_AS_TRIGGER(fcinfo))
4979 rhaas@postgresql.org 77 [ # # ]:UBC 0 : ereport(ERROR,
78 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
79 : : errmsg("triggered_change_notification: must be called as trigger")));
80 : :
81 : : /* and that it's called after the change */
4979 rhaas@postgresql.org 82 [ - + ]:CBC 5 : if (!TRIGGER_FIRED_AFTER(trigdata->tg_event))
4979 rhaas@postgresql.org 83 [ # # ]:UBC 0 : ereport(ERROR,
84 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
85 : : errmsg("triggered_change_notification: must be called after the change")));
86 : :
87 : : /* and that it's called for each row */
4979 rhaas@postgresql.org 88 [ - + ]:CBC 5 : if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
4979 rhaas@postgresql.org 89 [ # # ]:UBC 0 : ereport(ERROR,
90 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
91 : : errmsg("triggered_change_notification: must be called for each row")));
92 : :
4979 rhaas@postgresql.org 93 [ + + ]:CBC 5 : if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
94 : 2 : operation = 'I';
95 [ + + ]: 3 : else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
96 : 1 : operation = 'U';
97 [ + - ]: 2 : else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
98 : 2 : operation = 'D';
99 : : else
100 : : {
4979 rhaas@postgresql.org 101 [ # # ]:UBC 0 : elog(ERROR, "triggered_change_notification: trigger fired by unrecognized operation");
102 : : operation = 'X'; /* silence compiler warning */
103 : : }
104 : :
4979 rhaas@postgresql.org 105 :CBC 5 : trigger = trigdata->tg_trigger;
106 : 5 : nargs = trigger->tgnargs;
107 [ - + ]: 5 : if (nargs > 1)
4979 rhaas@postgresql.org 108 [ # # ]:UBC 0 : ereport(ERROR,
109 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
110 : : errmsg("triggered_change_notification: must not be called with more than one parameter")));
111 : :
4979 rhaas@postgresql.org 112 [ - + ]:CBC 5 : if (nargs == 0)
4979 rhaas@postgresql.org 113 :UBC 0 : channel = "tcn";
114 : : else
4979 rhaas@postgresql.org 115 :CBC 5 : channel = trigger->tgargs[0];
116 : :
117 : : /* get tuple data */
118 : 5 : trigtuple = trigdata->tg_trigtuple;
119 : 5 : rel = trigdata->tg_relation;
120 : 5 : tupdesc = rel->rd_att;
121 : :
122 : 5 : foundPK = false;
123 : :
124 : : /*
125 : : * Get the list of index OIDs for the table from the relcache, and look up
126 : : * each one in the pg_index syscache until we find one marked primary key
127 : : * (hopefully there isn't more than one such).
128 : : */
129 : 5 : indexoidlist = RelationGetIndexList(rel);
130 : :
131 [ + - + - : 5 : foreach(indexoidscan, indexoidlist)
+ - ]
132 : : {
133 : 5 : Oid indexoid = lfirst_oid(indexoidscan);
134 : : HeapTuple indexTuple;
135 : : Form_pg_index index;
136 : :
137 : 5 : indexTuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexoid));
2999 tgl@sss.pgh.pa.us 138 [ - + ]: 5 : if (!HeapTupleIsValid(indexTuple)) /* should not happen */
4979 rhaas@postgresql.org 139 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for index %u", indexoid);
4979 rhaas@postgresql.org 140 :CBC 5 : index = (Form_pg_index) GETSTRUCT(indexTuple);
141 : : /* we're only interested if it is the primary key and valid */
2445 peter_e@gmx.net 142 [ + - + - ]: 5 : if (index->indisprimary && index->indisvalid)
143 : : {
2690 tgl@sss.pgh.pa.us 144 : 5 : int indnkeyatts = index->indnkeyatts;
145 : :
2709 teodor@sigaev.ru 146 [ + - ]: 5 : if (indnkeyatts > 0)
147 : : {
148 : : int i;
149 : :
4979 rhaas@postgresql.org 150 : 5 : foundPK = true;
151 : :
152 : 5 : strcpy_quoted(payload, RelationGetRelationName(rel), '"');
153 [ - + ]: 5 : appendStringInfoCharMacro(payload, ',');
154 [ - + ]: 5 : appendStringInfoCharMacro(payload, operation);
155 : :
2709 teodor@sigaev.ru 156 [ + + ]: 10 : for (i = 0; i < indnkeyatts; i++)
157 : : {
4979 rhaas@postgresql.org 158 : 5 : int colno = index->indkey.values[i];
2939 andres@anarazel.de 159 : 5 : Form_pg_attribute attr = TupleDescAttr(tupdesc, colno - 1);
160 : :
4979 rhaas@postgresql.org 161 [ - + ]: 5 : appendStringInfoCharMacro(payload, ',');
2939 andres@anarazel.de 162 : 5 : strcpy_quoted(payload, NameStr(attr->attname), '"');
4979 rhaas@postgresql.org 163 [ - + ]: 5 : appendStringInfoCharMacro(payload, '=');
164 : 5 : strcpy_quoted(payload, SPI_getvalue(trigtuple, tupdesc, colno), '\'');
165 : : }
166 : :
167 : 5 : Async_Notify(channel, payload->data);
168 : : }
169 : 5 : ReleaseSysCache(indexTuple);
170 : 5 : break;
171 : : }
4979 rhaas@postgresql.org 172 :UBC 0 : ReleaseSysCache(indexTuple);
173 : : }
174 : :
4979 rhaas@postgresql.org 175 :CBC 5 : list_free(indexoidlist);
176 : :
177 [ - + ]: 5 : if (!foundPK)
4979 rhaas@postgresql.org 178 [ # # ]:UBC 0 : ereport(ERROR,
179 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
180 : : errmsg("triggered_change_notification: must be called on a table with a primary key")));
181 : :
2999 tgl@sss.pgh.pa.us 182 :CBC 5 : return PointerGetDatum(NULL); /* after trigger; value doesn't matter */
183 : : }
|