-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdecoder_json.c
603 lines (534 loc) · 18 KB
/
decoder_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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*-------------------------------------------------------------------------
*
* decoder_json.c
* Logical decoding output plugin generating JSON structures based
* on things decoded.
*
* Author, Ildus Kurbangaliev
*
* IDENTIFICATION
* decoder_json/decoder_json.c
*
* TODO:
* 1) add detail logs for unsupported types and other cases (replica identity)
* 2) use postgresql internal realization of json
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/genam.h"
#include "access/sysattr.h"
#include "catalog/pg_class.h"
#include "catalog/pg_type.h"
#include "nodes/parsenodes.h"
#include "replication/output_plugin.h"
#include "replication/logical.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/relcache.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
#include "utils/array.h"
#include <assert.h>
#include <jansson.h>
PG_MODULE_MAGIC;
/* These must be available to pg_dlsym() */
extern void _PG_init(void);
extern void _PG_output_plugin_init(OutputPluginCallbacks *cb);
/*
* Structure storing the plugin specifications and options.
*/
typedef struct
{
MemoryContext context;
bool include_transaction;
bool sort_keys;
} DecoderRawData;
typedef struct
{
char *name;
json_t *value;
} Pair;
static void decoder_json_startup(LogicalDecodingContext *ctx,
OutputPluginOptions *opt,
bool is_init);
static void decoder_json_shutdown(LogicalDecodingContext *ctx);
static void decoder_json_begin_txn(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn);
static void decoder_json_commit_txn(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn,
XLogRecPtr commit_lsn);
static void decoder_json_change(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, Relation rel,
ReorderBufferChange *change);
void
_PG_init(void)
{
/* other plugins can perform things here */
}
/* specify output plugin callbacks */
void
_PG_output_plugin_init(OutputPluginCallbacks *cb)
{
AssertVariableIsOfType(&_PG_output_plugin_init, LogicalOutputPluginInit);
cb->startup_cb = decoder_json_startup;
cb->begin_cb = decoder_json_begin_txn;
cb->change_cb = decoder_json_change;
cb->commit_cb = decoder_json_commit_txn;
cb->shutdown_cb = decoder_json_shutdown;
}
/* initialize this plugin */
static void
decoder_json_startup(LogicalDecodingContext *ctx,
OutputPluginOptions *opt,
bool is_init)
{
ListCell *option;
DecoderRawData *data;
json_set_alloc_funcs(palloc,pfree);
data = palloc(sizeof(DecoderRawData));
assert(data);
data->context = AllocSetContextCreate(ctx->context,
"Raw decoder context",
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
data->include_transaction = false;
data->sort_keys = false;
ctx->output_plugin_private = data;
/* Default output format */
opt->output_type = OUTPUT_PLUGIN_TEXTUAL_OUTPUT;
foreach(option, ctx->output_plugin_options)
{
DefElem *elem = lfirst(option);
Assert(elem->arg == NULL || IsA(elem->arg, String));
if (strcmp(elem->defname, "include_transaction") == 0)
{
/* if option does not provide a value, it means its value is true */
if (elem->arg == NULL)
data->include_transaction = true;
else if (!parse_bool(strVal(elem->arg), &data->include_transaction))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not parse value \"%s\" for parameter \"%s\"",
strVal(elem->arg), elem->defname)));
}
else if (strcmp(elem->defname, "sort_keys") == 0) {
/* if option does not provide a value, it means its value is true */
if (elem->arg == NULL)
data->sort_keys = true;
else if (!parse_bool(strVal(elem->arg), &data->sort_keys))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not parse value \"%s\" for parameter \"%s\"",
strVal(elem->arg), elem->defname)));
}
else
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("option \"%s\" = \"%s\" is unknown",
elem->defname,
elem->arg ? strVal(elem->arg) : "(null)")));
}
}
}
/* cleanup this plugin's resources */
static void
decoder_json_shutdown(LogicalDecodingContext *ctx)
{
DecoderRawData *data = ctx->output_plugin_private;
/* cleanup our own resources via memory context reset */
MemoryContextDelete(data->context);
}
/* BEGIN callback */
static void
decoder_json_begin_txn(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn)
{
DecoderRawData *data = ctx->output_plugin_private;
/* Write to the plugin only if there is */
if (data->include_transaction)
{
OutputPluginPrepareWrite(ctx, true);
appendStringInfoString(ctx->out, "begin");
OutputPluginWrite(ctx, true);
}
}
/* COMMIT callback */
static void
decoder_json_commit_txn(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn,
XLogRecPtr commit_lsn)
{
DecoderRawData *data = ctx->output_plugin_private;
/* Write to the plugin only if there is */
if (data->include_transaction)
{
OutputPluginPrepareWrite(ctx, true);
appendStringInfoString(ctx->out, "commit");
OutputPluginWrite(ctx, true);
}
}
/*
* Get a relation name.
*/
static char
*get_relname(Relation rel)
{
Form_pg_class class_form = RelationGetForm(rel);
return quote_qualified_identifier(
get_namespace_name(
get_rel_namespace(RelationGetRelid(rel))),
NameStr(class_form->relname));
}
static json_t *get_json_value(Datum, Oid, bool);
static json_t
*get_json_array(Datum array, Oid elmtype, int elmlen, bool elmbyval) {
Datum *elems;
int nelems, i;
ArrayType *arr = DatumGetArrayTypeP(array);
json_t *result = json_array();
if (ARR_NDIM(arr) != 1 || ARR_HASNULL(arr) || ARR_ELEMTYPE(arr) != elmtype) {
elog(ERROR, "expected 1-D array");
return NULL;
}
deconstruct_array(arr, elmtype, elmlen, elmbyval, 'i', &elems, NULL, &nelems);
for (i = 0; i < nelems; ++i) {
json_array_append_new(result, get_json_value(elems[i], elmtype, false));
}
pfree(elems);
return result;
}
/*
* Get json value for datum.
*/
static json_t
*get_json_value(Datum origval,
Oid typid,
bool isnull)
{
Oid typoutput;
bool typisvarlena;
Datum val;
/* Query output function */
getTypeOutputInfo(typid, &typoutput, &typisvarlena);
if (isnull)
return json_null();
else if (typisvarlena && VARATT_IS_EXTERNAL_ONDISK(origval))
return NULL; //unchanged-toast-datum
else if (!typisvarlena)
val = origval;
else
{
/* Definitely detoasted Datum */
val = PointerGetDatum(PG_DETOAST_DATUM(origval));
}
switch (typid) {
case BOOLOID:
return json_boolean(DatumGetBool(val));
case INT2OID:
return json_integer(DatumGetInt16(val));
case INT4OID:
return json_integer(DatumGetInt32(val));
case INT8OID:
case OIDOID:
return json_integer(DatumGetInt64(val));
case FLOAT4OID:
return json_real(DatumGetFloat4(val));
case FLOAT8OID:
return json_real(DatumGetFloat8(val));
case TIMESTAMPOID:
case TIMESTAMPTZOID:
return json_string(timestamptz_to_str(DatumGetTimestampTz(val)));
case NUMERICOID:
/* we send numeric as string for data safety */
case CHAROID:
case VARCHAROID:
case BPCHAROID:
case TEXTOID:
case JSONOID:
case XMLOID:
case UUIDOID:
return json_string(OidOutputFunctionCall(typoutput, val));
case 1015:
/* varchar array */
return get_json_array(val, VARCHAROID, -1, false);
case TEXTARRAYOID:
return get_json_array(val, TEXTOID, -1, false);
case INT2ARRAYOID:
return get_json_array(val, INT2OID, 2, true);
case INT4ARRAYOID:
return get_json_array(val, INT4OID, 4, true);
case FLOAT4ARRAYOID:
return get_json_array(val, FLOAT4OID, 4, true);
case OIDARRAYOID:
return get_json_array(val, OIDOID, 8, true);
default:
elog(ERROR, "Type %s is not supported, oid=%d", format_type_be(typid), typid);
}
return NULL;
}
/*
* Get attr:value pair
*/
static Pair
*get_pair(TupleDesc tupdesc,
HeapTuple tuple,
int natt)
{
Form_pg_attribute attr;
Datum origval;
bool isnull;
Pair *pair;
char *attname;
json_t *val;
attr = tupdesc->attrs[natt - 1];
/* Skip dropped columns and system columns */
if (attr->attisdropped || attr->attnum < 0)
return NULL;
/* Get attribute name */
attname = NameStr(attr->attname);
/* Get Datum from tuple */
origval = fastgetattr(tuple, natt, tupdesc, &isnull);
val = get_json_value(origval, attr->atttypid, isnull);
//elog(LOG, "We got json val for: %s=%s", attname, json_dumps(val, JSON_ENCODE_ANY));
if (val == NULL)
return NULL;
pair = palloc(sizeof(Pair));
assert(pair);
pair->value = val;
pair->name = attname;
return pair;
}
static json_t
*get_pairs(TupleDesc tupdesc,
HeapTuple tuple)
{
int natt;
json_t *pairs = json_object();
for (natt = 0; natt < tupdesc->natts; natt++) {
Pair *pair = get_pair(tupdesc, tuple, natt + 1);
if (pair == NULL)
continue;
json_object_set_new(pairs, pair->name, pair->value);
pfree(pair);
}
return pairs;
}
/*
* Generate a WHERE clause for UPDATE or DELETE.
*/
static json_t
*get_where_clause(Relation relation,
HeapTuple oldtuple,
HeapTuple newtuple)
{
TupleDesc tupdesc = RelationGetDescr(relation);
int natt;
json_t *allClauses = json_object();
Assert(relation->rd_rel->relreplident == REPLICA_IDENTITY_DEFAULT ||
relation->rd_rel->relreplident == REPLICA_IDENTITY_FULL ||
relation->rd_rel->relreplident == REPLICA_IDENTITY_INDEX);
RelationGetIndexList(relation);
/* Generate WHERE clause using new values of REPLICA IDENTITY */
if (OidIsValid(relation->rd_replidindex))
{
Relation indexRel;
int key;
/* Use all the values associated with the index */
indexRel = index_open(relation->rd_replidindex, ShareLock);
for (key = 0; key < indexRel->rd_index->indnatts; key++)
{
int relattr = indexRel->rd_index->indkey.values[key];
/*
* For a relation having REPLICA IDENTITY set at DEFAULT
* or INDEX, if one of the columns used for tuple selectivity
* is changed, the old tuple data is not NULL and need to
* be used for tuple selectivity. If no such columns are
* updated, old tuple data is NULL.
*/
Pair *pair = get_pair(tupdesc, oldtuple ? oldtuple : newtuple, relattr);
if (pair == NULL)
continue;
json_object_set_new(allClauses, pair->name, pair->value);
pfree(pair);
}
index_close(indexRel, NoLock);
return allClauses;
}
/* We need absolutely some values for tuple selectivity now */
Assert(oldtuple != NULL &&
relation->rd_rel->relreplident == REPLICA_IDENTITY_FULL);
/*
* Fallback to default case, use of old values and print WHERE clause
* using all the columns. This is actually the code path for FULL.
*/
for (natt = 0; natt < tupdesc->natts; natt++) {
Pair *pair = get_pair(tupdesc, oldtuple, natt + 1);
if (pair == NULL)
continue;
json_object_set_new(allClauses, pair->name, pair->value);
pfree(pair);
}
return allClauses;
}
static void
write_struct(StringInfo s,
int actionId,
Relation relation,
json_t *clause,
json_t *data,
bool sort_keys)
{
char *relname = get_relname(relation);
char *result;
size_t flags = JSON_COMPACT;
json_t *action = json_object();
if (sort_keys) flags |= JSON_SORT_KEYS;
/* Generate struct */
json_object_set_new(action, "a", json_integer(actionId));
json_object_set_new(action, "r", json_string(relname));
if (clause != NULL)
json_object_set(action, "c", clause);
if (data != NULL)
json_object_set(action, "d", data);
result = json_dumps(action, flags);
json_decref(action);
elog(LOG, "Struct:%s", result);
appendStringInfoString(s, result);
pfree(result);
}
/*
* Decode an INSERT entry
*/
static void
decoder_json_insert(StringInfo s,
Relation relation,
HeapTuple tuple,
bool sort_keys)
{
TupleDesc tupdesc = RelationGetDescr(relation);
json_t *data = get_pairs(tupdesc, tuple);
write_struct(s, 0, relation, NULL, data, sort_keys);
json_decref(data);
}
/*
* Decode a DELETE entry
* Append to output json structure like
* {"a": 2, "r": "public.table_name", "c": "some_clause"}
*/
static void
decoder_json_delete(StringInfo s,
Relation relation,
HeapTuple tuple,
bool sort_keys)
{
/*
* Here the same tuple is used as old and new values, selectivity will
* be properly reduced by relation uses DEFAULT or INDEX as REPLICA
* IDENTITY.
*/
json_t *clause = get_where_clause(relation, tuple, tuple);
write_struct(s, 2, relation, clause, NULL, sort_keys);
json_decref(clause);
}
/*
* Decode an UPDATE entry
*/
static void
decoder_json_update(StringInfo s,
Relation relation,
HeapTuple oldtuple,
HeapTuple newtuple,
bool sort_keys)
{
json_t *clause;
json_t *data;
TupleDesc tupdesc = RelationGetDescr(relation);
/* If there are no new values, simply leave as there is nothing to do */
if (newtuple == NULL)
return;
clause = get_where_clause(relation, oldtuple, newtuple);
data = get_pairs(tupdesc, newtuple);
write_struct(s, 1, relation, clause, data, sort_keys);
json_decref(clause);
json_decref(data);
}
/*
* Callback for individual changed tuples
*/
static void
decoder_json_change(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn,
Relation relation,
ReorderBufferChange *change)
{
DecoderRawData *data;
MemoryContext old;
char replident = relation->rd_rel->relreplident;
bool is_rel_non_selective;
data = ctx->output_plugin_private;
/* Avoid leaking memory by using and resetting our own context */
old = MemoryContextSwitchTo(data->context);
/*
* Determine if relation is selective enough for WHERE clause generation
* in UPDATE and DELETE cases. A non-selective relation uses REPLICA
* IDENTITY set as NOTHING, or DEFAULT without an available replica
* identity index.
*/
RelationGetIndexList(relation);
is_rel_non_selective = (replident == REPLICA_IDENTITY_NOTHING ||
(replident == REPLICA_IDENTITY_DEFAULT &&
!OidIsValid(relation->rd_replidindex)));
/* Decode entry depending on its type */
switch (change->action)
{
case REORDER_BUFFER_CHANGE_INSERT:
if (change->data.tp.newtuple != NULL)
{
OutputPluginPrepareWrite(ctx, true);
decoder_json_insert(ctx->out,
relation,
&change->data.tp.newtuple->tuple,
data->sort_keys);
OutputPluginWrite(ctx, true);
}
break;
case REORDER_BUFFER_CHANGE_UPDATE:
if (!is_rel_non_selective)
{
HeapTuple oldtuple = change->data.tp.oldtuple != NULL ?
&change->data.tp.oldtuple->tuple : NULL;
HeapTuple newtuple = change->data.tp.newtuple != NULL ?
&change->data.tp.newtuple->tuple : NULL;
OutputPluginPrepareWrite(ctx, true);
decoder_json_update(ctx->out,
relation,
oldtuple,
newtuple,
data->sort_keys);
OutputPluginWrite(ctx, true);
}
break;
case REORDER_BUFFER_CHANGE_DELETE:
if (!is_rel_non_selective)
{
OutputPluginPrepareWrite(ctx, true);
decoder_json_delete(ctx->out,
relation,
&change->data.tp.oldtuple->tuple,
data->sort_keys);
OutputPluginWrite(ctx, true);
}
break;
default:
/* Should not come here */
Assert(0);
break;
}
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
}