-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcn_json.c
191 lines (164 loc) · 5.33 KB
/
tcn_json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*-------------------------------------------------------------------------
*
* tcn_json.c
* triggered change notification support for PostgreSQL
*
* Portions Copyright (c) 2011-2015, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* contrib/tcn_json/tcn_json.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/htup_details.h"
#include "executor/spi.h"
#include "commands/async.h"
#include "commands/trigger.h"
#include "lib/stringinfo.h"
#include "utils/rel.h"
#include "utils/syscache.h"
PG_MODULE_MAGIC;
/*
* Copy from s (for source) to r (for result), wrapping with q (quote)
* characters and doubling any quote characters found.
*/
static void
strcpy_quoted(StringInfo r, const char *s, const char q)
{
appendStringInfoCharMacro(r, q);
while (*s)
{
if (*s == q)
appendStringInfoCharMacro(r, q);
appendStringInfoCharMacro(r, *s);
s++;
}
appendStringInfoCharMacro(r, q);
}
/*
* triggered_change_notification_json
*
* This trigger function will send a notification of data modification with
* primary key values. The channel will be "tcn_json" unless the trigger is
* created with a parameter, in which case that parameter will be used.
*/
PG_FUNCTION_INFO_V1(triggered_change_notification_json);
Datum
triggered_change_notification_json(PG_FUNCTION_ARGS)
{
TriggerData *trigdata = (TriggerData *) fcinfo->context;
Trigger *trigger;
int nargs;
HeapTuple trigtuple;
Relation rel;
TupleDesc tupdesc;
char *channel;
char operation;
StringInfo payload = makeStringInfo();
bool foundPK;
List *indexoidlist;
ListCell *indexoidscan;
/* make sure it's called as a trigger */
if (!CALLED_AS_TRIGGER(fcinfo))
ereport(ERROR,
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
errmsg("triggered_change_notification_json: must be called as trigger")));
/* and that it's called after the change */
if (!TRIGGER_FIRED_AFTER(trigdata->tg_event))
ereport(ERROR,
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
errmsg("triggered_change_notification_json: must be called after the change")));
/* and that it's called for each row */
if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
ereport(ERROR,
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
errmsg("triggered_change_notification_json: must be called for each row")));
if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
operation = 'I';
else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
operation = 'U';
else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
operation = 'D';
else
{
elog(ERROR, "triggered_change_notification_json: trigger fired by unrecognized operation");
operation = 'X'; /* silence compiler warning */
}
trigger = trigdata->tg_trigger;
nargs = trigger->tgnargs;
if (nargs > 2)
ereport(ERROR,
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
errmsg("triggered_change_notification_json: must not be called with more than 2 parameter")));
/* get tuple data */
trigtuple = trigdata->tg_trigtuple;
rel = trigdata->tg_relation;
tupdesc = rel->rd_att;
foundPK = false;
/*
* Get the list of index OIDs for the table from the relcache, and look up
* each one in the pg_index syscache until we find one marked primary key
* (hopefully there isn't more than one such).
*/
indexoidlist = RelationGetIndexList(rel);
if (nargs == 0){
channel = "tcn_json";
strcpy_quoted(payload, RelationGetRelationName(rel), '"');
}else if (nargs == 1){
channel = trigger->tgargs[0];
strcpy_quoted(payload, RelationGetRelationName(rel), '"');
}else if (nargs == 2){
channel = trigger->tgargs[0];
strcpy_quoted(payload, trigger->tgargs[1], '"');
}else{
channel = "tcn_json";
}
appendStringInfoCharMacro(payload, ',');
appendStringInfoCharMacro(payload, operation);
appendStringInfoCharMacro(payload, ',');
appendStringInfoCharMacro(payload, '{');
foreach(indexoidscan, indexoidlist)
{
Oid indexoid = lfirst_oid(indexoidscan);
HeapTuple indexTuple;
Form_pg_index index;
indexTuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexoid));
if (!HeapTupleIsValid(indexTuple)) /* should not happen */
elog(ERROR, "cache lookup failed for index %u", indexoid);
index = (Form_pg_index) GETSTRUCT(indexTuple);
/* we're only interested if it is the primary key and valid */
if (index->indisprimary && IndexIsValid(index))
{
int numatts = index->indnatts;
if (numatts > 0)
{
int i;
foundPK = true;
for (i = 0; i < numatts; i++)
{
int colno = index->indkey.values[i];
if(i > 0){
appendStringInfoCharMacro(payload, ',');
}
strcpy_quoted(payload, NameStr((tupdesc->attrs[colno - 1])->attname), '"');
appendStringInfoCharMacro(payload, ':');
strcpy_quoted(payload, SPI_getvalue(trigtuple, tupdesc, colno), '"');
}
appendStringInfoCharMacro(payload, '}');
Async_Notify(channel, payload->data);
}
ReleaseSysCache(indexTuple);
break;
}
ReleaseSysCache(indexTuple);
}
list_free(indexoidlist);
if (!foundPK)
ereport(ERROR,
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
errmsg("triggered_change_notification_json: must be called on a table with a primary key")));
return PointerGetDatum(NULL); /* after trigger; value doesn't matter */
}