-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpg_json_decoding.c
273 lines (246 loc) · 7.35 KB
/
pg_json_decoding.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "postgres.h"
#include "catalog/namespace.h"
#include "replication/logical.h"
#include "common/base64.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/relcache.h"
#include "utils/syscache.h"
#include "utils/json.h"
#include "utils/builtins.h"
#include "utils/array.h"
PG_MODULE_MAGIC;
extern void _PG_init(void);
extern void _PG_output_plugin_init(OutputPluginCallbacks *cb);
typedef struct _JsonDecodingData {
MemoryContext context;
char *pubname;
Oid pubid;
bool puballtables;
} JsonDecodingData;
static void pg_decode_startup(
LogicalDecodingContext *ctx,
OutputPluginOptions *opt,
bool is_init
) {
JsonDecodingData *data;
ListCell *option;
data = palloc0(sizeof(JsonDecodingData));
data->pubname = NULL;
data->pubid = InvalidOid;
data->puballtables = false;
data->context = AllocSetContextCreate(
ctx->context,
"pg_json_decoding context",
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE
);
ctx->output_plugin_private = data;
opt->output_type = OUTPUT_PLUGIN_TEXTUAL_OUTPUT;
if (is_init) {
return;
}
foreach(option, ctx->output_plugin_options) {
DefElem *elem = lfirst(option);
Assert(elem->arg == NULL || IsA(elem->arg, String));
if (strcmp(elem->defname, "publication") == 0) {
data->pubname = strVal(elem->arg);
}
}
if (data->pubname == NULL) {
ereport(ERROR, (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("publication parameter missing")
));
}
}
static void pg_decode_shutdown(LogicalDecodingContext *ctx) {
JsonDecodingData *data = ctx->output_plugin_private;
MemoryContextDelete(data->context);
}
static void pg_decode_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn) {
OutputPluginPrepareWrite(ctx, true);
appendStringInfo(
ctx->out,
"{\"kind\":\"begin\",\"committed\":\"%ld\"}",
txn->commit_time + (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * USECS_PER_DAY
);
OutputPluginWrite(ctx, true);
}
static void pg_decode_commit_txn(
LogicalDecodingContext *ctx,
ReorderBufferTXN *txn,
XLogRecPtr commit_lsn
) {
OutputPluginPrepareWrite(ctx, true);
appendStringInfoString(ctx->out, "{\"kind\":\"commit\"}");
OutputPluginWrite(ctx, true);
}
static void tuple_to_json(StringInfo out, TupleDesc tupdesc, HeapTuple tuple) {
int i;
bool emit_comma;
emit_comma = false;
appendStringInfoChar(out, '{');
for (i = 0; i < tupdesc->natts; i++) {
Form_pg_attribute att;
Datum val;
bool isnull;
att = TupleDescAttr(tupdesc, i);
if (att->attisdropped || att->attnum < 0) {
continue;
}
#if (PG_VERSION_NUM >= 120000)
if (att->attgenerated) {
continue;
}
#endif
if (emit_comma) {
appendStringInfoChar(out, ',');
}
emit_comma = true;
escape_json(out, NameStr(att->attname));
appendStringInfoChar(out, ':');
val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
if (isnull) {
appendStringInfoString(out, "null");
} else if (att->attlen == -1 && VARATT_IS_EXTERNAL_ONDISK(val)) {
appendStringInfoString(out, "\"__unchanged_toast_datum__\"");
} else {
ArrayType *arr;
char *arrjsonstr;
// use array wrapper to specify value type for json conversion function
arr = construct_array(&val, 1, att->atttypid, att->attlen, true, 'd');
arrjsonstr = TextDatumGetCString(DirectFunctionCall1(array_to_json, PointerGetDatum(arr)));
appendBinaryStringInfo(
out,
// omit json array brackets
arrjsonstr + 1,
strlen(arrjsonstr + 1) - 1
);
pfree(arr);
pfree(arrjsonstr);
}
}
appendStringInfoChar(out, '}');
}
static void pg_decode_change(
LogicalDecodingContext *ctx,
ReorderBufferTXN *txn,
Relation relation,
ReorderBufferChange *change
) {
JsonDecodingData *data;
Form_pg_class class_form;
TupleDesc tupdesc;
MemoryContext old;
char *table_name;
data = ctx->output_plugin_private;
if (data->pubid == InvalidOid) {
data->pubid = get_publication_oid(data->pubname, false);
data->puballtables = GetPublication(data->pubid)->alltables;
}
if (!data->puballtables && !SearchSysCacheExists2(
PUBLICATIONRELMAP,
ObjectIdGetDatum(RelationGetRelid(relation)),
ObjectIdGetDatum(data->pubid)
)) {
return;
}
class_form = RelationGetForm(relation);
tupdesc = RelationGetDescr(relation);
table_name = NameStr(class_form->relname);
old = MemoryContextSwitchTo(data->context);
OutputPluginPrepareWrite(ctx, true);
appendStringInfoString(ctx->out, "{\"kind\":");
switch (change->action) {
case REORDER_BUFFER_CHANGE_INSERT:
appendStringInfoString(ctx->out, "\"insert\"");
break;
case REORDER_BUFFER_CHANGE_UPDATE:
appendStringInfoString(ctx->out, "\"update\"");
break;
case REORDER_BUFFER_CHANGE_DELETE:
appendStringInfoString(ctx->out, "\"delete\"");
break;
default:
appendStringInfoString(ctx->out, "\"unknown\"");
break;
}
appendStringInfoString(ctx->out, ",\"schema\":");
escape_json(ctx->out, get_namespace_name(
get_rel_namespace(RelationGetRelid(relation))
));
appendStringInfoString(ctx->out, ",\"table\":");
escape_json(ctx->out, table_name);
if (change->data.tp.oldtuple != NULL) {
appendStringInfoString(ctx->out, ",\"oldtuple\":");
tuple_to_json(ctx->out, tupdesc, &change->data.tp.oldtuple->tuple);
}
if (change->data.tp.newtuple != NULL) {
appendStringInfoString(ctx->out, ",\"newtuple\":");
tuple_to_json(ctx->out, tupdesc, &change->data.tp.newtuple->tuple);
}
appendStringInfoChar(ctx->out, '}');
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
OutputPluginWrite(ctx, true);
}
static void pg_decode_truncate(
LogicalDecodingContext *ctx,
ReorderBufferTXN *txn,
int nrelations,
Relation relations[],
ReorderBufferChange *change
) {
OutputPluginPrepareWrite(ctx, true);
appendStringInfoString(ctx->out, "{\"kind\":\"truncate\"}");
OutputPluginWrite(ctx, true);
}
static void pg_decode_message(
LogicalDecodingContext *ctx,
ReorderBufferTXN *txn,
XLogRecPtr lsn,
bool transactional,
const char *prefix,
Size sz,
const char *message
) {
JsonDecodingData *data;
MemoryContext old;
char *message_b64;
data = ctx->output_plugin_private;
old = MemoryContextSwitchTo(data->context);
OutputPluginPrepareWrite(ctx, true);
if (transactional) {
appendStringInfoString(ctx->out, "{\"kind\":\"xmessage\"");
} else {
appendStringInfoString(ctx->out, "{\"kind\":\"message\"");
}
appendStringInfoString(ctx->out, ",\"prefix\":");
escape_json(ctx->out, prefix);
message_b64 = palloc0(pg_b64_enc_len(sz) + 1);
pg_b64_encode(message, sz, message_b64
#if (PG_VERSION_NUM >= 130000)
,pg_b64_enc_len(sz) + 1
#endif
);
appendStringInfo(ctx->out, ",\"content\":\"%s\"}", message_b64);
pfree(message_b64);
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
OutputPluginWrite(ctx, true);
}
void _PG_output_plugin_init(OutputPluginCallbacks *cb) {
AssertVariableIsOfType(&_PG_output_plugin_init, LogicalOutputPluginInit);
cb->startup_cb = pg_decode_startup;
cb->begin_cb = pg_decode_begin_txn;
cb->change_cb = pg_decode_change;
cb->commit_cb = pg_decode_commit_txn;
cb->shutdown_cb = pg_decode_shutdown;
cb->message_cb = pg_decode_message;
cb->truncate_cb = pg_decode_truncate;
}
void _PG_init(void) {
}