forked from eulerto/wal2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wal2json.c
1220 lines (1057 loc) · 34.8 KB
/
wal2json.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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* wal2json.c
* JSON output plugin for changeset extraction
*
* Copyright (c) 2013-2018, Euler Taveira de Oliveira
*
* IDENTIFICATION
* contrib/wal2json/wal2json.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "catalog/pg_type.h"
#include "replication/logical.h"
#include "utils/builtins.h"
#include "utils/json.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/pg_lsn.h"
#include "utils/rel.h"
#include "utils/syscache.h"
PG_MODULE_MAGIC;
extern void _PG_init(void);
extern void PGDLLEXPORT _PG_output_plugin_init(OutputPluginCallbacks *cb);
typedef struct
{
MemoryContext context;
bool include_xids; /* include transaction ids */
bool include_timestamp; /* include transaction timestamp */
bool include_schemas; /* qualify tables */
bool include_types; /* include data types */
bool include_type_oids; /* include data type oids */
bool include_typmod; /* include typmod in types */
bool include_not_null; /* include not-null constraints */
bool pretty_print; /* pretty-print JSON? */
bool write_in_chunks; /* write in chunks? */
List *filter_tables; /* filter out tables */
List *add_tables; /* add only these tables */
/*
* LSN pointing to the end of commit record + 1 (txn->end_lsn)
* It is useful for tools that wants a position to restart from.
*/
bool include_lsn; /* include LSNs */
uint64 nr_changes; /* # of passes in pg_decode_change() */
/* FIXME replace with txn->nentries */
/* pretty print */
char ht[2]; /* horizontal tab, if pretty print */
char nl[2]; /* new line, if pretty print */
char sp[2]; /* space, if pretty print */
} JsonDecodingData;
typedef struct SelectTable
{
char *schemaname;
char *tablename;
bool allschemas; /* true means any schema */
bool alltables; /* true means any table */
} SelectTable;
/* These must be available to pg_dlsym() */
static void pg_decode_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init);
static void pg_decode_shutdown(LogicalDecodingContext *ctx);
static void pg_decode_begin_txn(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn);
static void pg_decode_commit_txn(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, XLogRecPtr commit_lsn);
static void pg_decode_change(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, Relation rel,
ReorderBufferChange *change);
#if PG_VERSION_NUM >= 90600
static void pg_decode_message(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, XLogRecPtr lsn,
bool transactional, const char *prefix,
Size content_size, const char *content);
#endif
static bool parse_table_identifier(List *qualified_tables, char separator, List **select_tables);
static bool string_to_SelectTable(char *rawstring, char separator, List **select_tables);
void
_PG_init(void)
{
}
/* Specify output plugin callbacks */
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;
#if PG_VERSION_NUM >= 90600
cb->message_cb = pg_decode_message;
#endif
}
/* Initialize this plugin */
static void
pg_decode_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
{
ListCell *option;
JsonDecodingData *data;
SelectTable *t;
data = palloc0(sizeof(JsonDecodingData));
data->context = AllocSetContextCreate(TopMemoryContext,
"wal2json output context",
#if PG_VERSION_NUM >= 90600
ALLOCSET_DEFAULT_SIZES
#else
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE
#endif
);
data->include_xids = false;
data->include_timestamp = false;
data->include_schemas = true;
data->include_types = true;
data->include_type_oids = false;
data->include_typmod = true;
data->pretty_print = false;
data->write_in_chunks = false;
data->include_lsn = false;
data->include_not_null = false;
data->filter_tables = NIL;
/* pretty print */
strcpy(data->ht, "");
strcpy(data->nl, "");
strcpy(data->sp, "");
/* add all tables in all schemas by default */
t = palloc0(sizeof(SelectTable));
t->allschemas = true;
t->alltables = true;
data->add_tables = lappend(data->add_tables, t);
data->nr_changes = 0;
ctx->output_plugin_private = data;
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-xids") == 0)
{
/* If option does not provide a value, it means its value is true */
if (elem->arg == NULL)
{
elog(DEBUG1, "include-xids argument is null");
data->include_xids = true;
}
else if (!parse_bool(strVal(elem->arg), &data->include_xids))
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, "include-timestamp") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "include-timestamp argument is null");
data->include_timestamp = true;
}
else if (!parse_bool(strVal(elem->arg), &data->include_timestamp))
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, "include-schemas") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "include-schemas argument is null");
data->include_schemas = true;
}
else if (!parse_bool(strVal(elem->arg), &data->include_schemas))
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, "include-types") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "include-types argument is null");
data->include_types = true;
}
else if (!parse_bool(strVal(elem->arg), &data->include_types))
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, "include-type-oids") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "include-type-oids argument is null");
data->include_type_oids = true;
}
else if (!parse_bool(strVal(elem->arg), &data->include_type_oids))
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, "include-typmod") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "include-typmod argument is null");
data->include_typmod = true;
}
else if (!parse_bool(strVal(elem->arg), &data->include_typmod))
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, "include-not-null") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "include-not-null argument is null");
data->include_not_null = true;
}
else if (!parse_bool(strVal(elem->arg), &data->include_not_null))
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, "pretty-print") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "pretty-print argument is null");
data->pretty_print = true;
}
else if (!parse_bool(strVal(elem->arg), &data->pretty_print))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not parse value \"%s\" for parameter \"%s\"",
strVal(elem->arg), elem->defname)));
if (data->pretty_print)
{
strncpy(data->ht, "\t", 1);
strncpy(data->nl, "\n", 1);
strncpy(data->sp, " ", 1);
}
}
else if (strcmp(elem->defname, "write-in-chunks") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "write-in-chunks argument is null");
data->write_in_chunks = true;
}
else if (!parse_bool(strVal(elem->arg), &data->write_in_chunks))
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, "include-lsn") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "include-lsn argument is null");
data->include_lsn = true;
}
else if (!parse_bool(strVal(elem->arg), &data->include_lsn))
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, "include-unchanged-toast") == 0)
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("parameter \"%s\" was deprecated", elem->defname)));
}
else if (strcmp(elem->defname, "filter-tables") == 0)
{
char *rawstr;
if (elem->arg == NULL)
{
elog(DEBUG1, "filter-tables argument is null");
data->filter_tables = NIL;
}
rawstr = pstrdup(strVal(elem->arg));
if (!string_to_SelectTable(rawstr, ',', &data->filter_tables))
{
pfree(rawstr);
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("could not parse value \"%s\" for parameter \"%s\"",
strVal(elem->arg), elem->defname)));
}
pfree(rawstr);
}
else if (strcmp(elem->defname, "add-tables") == 0)
{
char *rawstr;
/*
* If this parameter is specified, remove 'all tables in all
* schemas' value from list.
*/
list_free_deep(data->add_tables);
data->add_tables = NIL;
if (elem->arg == NULL)
{
elog(DEBUG1, "add-tables argument is null");
data->add_tables = NIL;
}
else
{
rawstr = pstrdup(strVal(elem->arg));
if (!string_to_SelectTable(rawstr, ',', &data->add_tables))
{
pfree(rawstr);
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("could not parse value \"%s\" for parameter \"%s\"",
strVal(elem->arg), elem->defname)));
}
pfree(rawstr);
}
}
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
pg_decode_shutdown(LogicalDecodingContext *ctx)
{
JsonDecodingData *data = ctx->output_plugin_private;
/* cleanup our own resources via memory context reset */
MemoryContextDelete(data->context);
}
/* BEGIN callback */
static void
pg_decode_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
{
JsonDecodingData *data = ctx->output_plugin_private;
data->nr_changes = 0;
/* Transaction starts */
OutputPluginPrepareWrite(ctx, true);
appendStringInfo(ctx->out, "{%s", data->nl);
if (data->include_xids)
appendStringInfo(ctx->out, "%s\"xid\":%s%u,%s", data->ht, data->sp, txn->xid, data->nl);
if (data->include_lsn)
{
char *lsn_str = DatumGetCString(DirectFunctionCall1(pg_lsn_out, txn->end_lsn));
appendStringInfo(ctx->out, "%s\"nextlsn\":%s\"%s\",%s", data->ht, data->sp, lsn_str, data->nl);
pfree(lsn_str);
}
if (data->include_timestamp)
appendStringInfo(ctx->out, "%s\"timestamp\":%s\"%s\",%s", data->ht, data->sp, timestamptz_to_str(txn->commit_time), data->nl);
appendStringInfo(ctx->out, "%s\"change\":%s[", data->ht, data->sp);
if (data->write_in_chunks)
OutputPluginWrite(ctx, true);
}
/* COMMIT callback */
static void
pg_decode_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
XLogRecPtr commit_lsn)
{
JsonDecodingData *data = ctx->output_plugin_private;
if (txn->has_catalog_changes)
elog(DEBUG2, "txn has catalog changes: yes");
else
elog(DEBUG2, "txn has catalog changes: no");
elog(DEBUG2, "my change counter: %lu ; # of changes: %lu ; # of changes in memory: %lu", data->nr_changes, txn->nentries, txn->nentries_mem);
elog(DEBUG2, "# of subxacts: %d", txn->nsubtxns);
/* Transaction ends */
if (data->write_in_chunks)
OutputPluginPrepareWrite(ctx, true);
/* if we don't write in chunks, we need a newline here */
if (!data->write_in_chunks)
appendStringInfo(ctx->out, "%s", data->nl);
appendStringInfo(ctx->out, "%s]%s}", data->ht, data->nl);
OutputPluginWrite(ctx, true);
}
/*
* Accumulate tuple information and stores it at the end
*
* replident: is this tuple a replica identity?
* hasreplident: does this tuple has an associated replica identity?
*/
static void
tuple_to_stringinfo(LogicalDecodingContext *ctx, TupleDesc tupdesc, HeapTuple tuple, TupleDesc indexdesc, bool replident, bool hasreplident)
{
JsonDecodingData *data;
int natt;
StringInfoData colnames;
StringInfoData coltypes;
StringInfoData coltypeoids;
StringInfoData colnotnulls;
StringInfoData colvalues;
char comma[3] = "";
data = ctx->output_plugin_private;
initStringInfo(&colnames);
initStringInfo(&coltypes);
if (data->include_type_oids)
initStringInfo(&coltypeoids);
if (data->include_not_null)
initStringInfo(&colnotnulls);
initStringInfo(&colvalues);
/*
* If replident is true, it will output info about replica identity. In this
* case, there are special JSON objects for it. Otherwise, it will print new
* tuple data.
*/
if (replident)
{
appendStringInfo(&colnames, "%s%s%s\"oldkeys\":%s{%s", data->ht, data->ht, data->ht, data->sp, data->nl);
appendStringInfo(&colnames, "%s%s%s%s\"keynames\":%s[", data->ht, data->ht, data->ht, data->ht, data->sp);
appendStringInfo(&coltypes, "%s%s%s%s\"keytypes\":%s[", data->ht, data->ht, data->ht, data->ht, data->sp);
if (data->include_type_oids)
appendStringInfo(&coltypeoids, "%s%s%s%s\"keytypeoids\":%s[", data->ht, data->ht, data->ht, data->ht, data->sp);
appendStringInfo(&colvalues, "%s%s%s%s\"keyvalues\":%s[", data->ht, data->ht, data->ht, data->ht, data->sp);
}
else
{
appendStringInfo(&colnames, "%s%s%s\"columnnames\":%s[", data->ht, data->ht, data->ht, data->sp);
appendStringInfo(&coltypes, "%s%s%s\"columntypes\":%s[", data->ht, data->ht, data->ht, data->sp);
if (data->include_type_oids)
appendStringInfo(&coltypeoids, "%s%s%s\"columntypeoids\":%s[", data->ht, data->ht, data->ht, data->sp);
if (data->include_not_null)
appendStringInfo(&colnotnulls, "%s%s%s\"columnoptionals\":%s[", data->ht, data->ht, data->ht, data->sp);
appendStringInfo(&colvalues, "%s%s%s\"columnvalues\":%s[", data->ht, data->ht, data->ht, data->sp);
}
/* Print column information (name, type, value) */
for (natt = 0; natt < tupdesc->natts; natt++)
{
Form_pg_attribute attr; /* the attribute itself */
Oid typid; /* type of current attribute */
HeapTuple type_tuple; /* information about a type */
Oid typoutput; /* output function */
bool typisvarlena;
Datum origval; /* possibly toasted Datum */
Datum val; /* definitely detoasted Datum */
char *outputstr = NULL;
bool isnull; /* column is null? */
/*
* Commit d34a74dd064af959acd9040446925d9d53dff15b introduced
* TupleDescAttr() in back branches. If the version supports
* this macro, use it. Version 10 and later already support it.
*/
#if (PG_VERSION_NUM >= 90600 && PG_VERSION_NUM < 90605) || (PG_VERSION_NUM >= 90500 && PG_VERSION_NUM < 90509) || (PG_VERSION_NUM >= 90400 && PG_VERSION_NUM < 90414)
attr = tupdesc->attrs[natt];
#else
attr = TupleDescAttr(tupdesc, natt);
#endif
elog(DEBUG1, "attribute \"%s\" (%d/%d)", NameStr(attr->attname), natt, tupdesc->natts);
/* Do not print dropped or system columns */
if (attr->attisdropped || attr->attnum < 0)
continue;
/* Search indexed columns in whole heap tuple */
if (indexdesc != NULL)
{
int j;
bool found_col = false;
for (j = 0; j < indexdesc->natts; j++)
{
Form_pg_attribute iattr;
/* See explanation a few lines above. */
#if (PG_VERSION_NUM >= 90600 && PG_VERSION_NUM < 90605) || (PG_VERSION_NUM >= 90500 && PG_VERSION_NUM < 90509) || (PG_VERSION_NUM >= 90400 && PG_VERSION_NUM < 90414)
iattr = indexdesc->attrs[j];
#else
iattr = TupleDescAttr(indexdesc, j);
#endif
if (strcmp(NameStr(attr->attname), NameStr(iattr->attname)) == 0)
found_col = true;
}
/* Print only indexed columns */
if (!found_col)
continue;
}
typid = attr->atttypid;
/* Figure out type name */
type_tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
if (!HeapTupleIsValid(type_tuple))
elog(ERROR, "cache lookup failed for type %u", typid);
/* Get information needed for printing values of a type */
getTypeOutputInfo(typid, &typoutput, &typisvarlena);
/* Get Datum from tuple */
origval = heap_getattr(tuple, natt + 1, tupdesc, &isnull);
/* Skip nulls iif printing key/identity */
if (isnull && replident)
continue;
/* XXX Unchanged TOAST Datum does not need to be output */
if (!isnull && typisvarlena && VARATT_IS_EXTERNAL_ONDISK(origval))
{
elog(DEBUG1, "column \"%s\" has an unchanged TOAST", NameStr(attr->attname));
continue;
}
/* Accumulate each column info */
appendStringInfo(&colnames, "%s", comma);
escape_json(&colnames, NameStr(attr->attname));
if (data->include_types)
{
if (data->include_typmod)
{
char *type_str;
type_str = TextDatumGetCString(DirectFunctionCall2(format_type, attr->atttypid, attr->atttypmod));
appendStringInfo(&coltypes, "%s", comma);
escape_json(&coltypes, type_str);
pfree(type_str);
}
else
{
Form_pg_type type_form = (Form_pg_type) GETSTRUCT(type_tuple);
appendStringInfo(&coltypes, "%s", comma);
escape_json(&coltypes, NameStr(type_form->typname));
}
/* oldkeys doesn't print not-null constraints */
if (!replident && data->include_not_null)
{
if (attr->attnotnull)
appendStringInfo(&colnotnulls, "%sfalse", comma);
else
appendStringInfo(&colnotnulls, "%strue", comma);
}
}
if (data->include_type_oids)
appendStringInfo(&coltypeoids, "%s%u", comma, typid);
ReleaseSysCache(type_tuple);
if (isnull)
{
appendStringInfo(&colvalues, "%snull", comma);
}
else
{
if (typisvarlena)
val = PointerGetDatum(PG_DETOAST_DATUM(origval));
else
val = origval;
/* Finally got the value */
outputstr = OidOutputFunctionCall(typoutput, val);
/*
* Data types are printed with quotes unless they are number, true,
* false, null, an array or an object.
*
* The NaN and Infinity are not valid JSON symbols. Hence,
* regardless of sign they are represented as the string null.
*/
switch (typid)
{
case INT2OID:
case INT4OID:
case INT8OID:
case OIDOID:
case FLOAT4OID:
case FLOAT8OID:
case NUMERICOID:
if (pg_strncasecmp(outputstr, "NaN", 3) == 0 ||
pg_strncasecmp(outputstr, "Infinity", 8) == 0 ||
pg_strncasecmp(outputstr, "-Infinity", 9) == 0)
{
appendStringInfo(&colvalues, "%snull", comma);
elog(DEBUG1, "attribute \"%s\" is special: %s", NameStr(attr->attname), outputstr);
}
else if (strspn(outputstr, "0123456789+-eE.") == strlen(outputstr))
appendStringInfo(&colvalues, "%s%s", comma, outputstr);
else
elog(ERROR, "%s is not a number", outputstr);
break;
case BOOLOID:
if (strcmp(outputstr, "t") == 0)
appendStringInfo(&colvalues, "%strue", comma);
else
appendStringInfo(&colvalues, "%sfalse", comma);
break;
case BYTEAOID:
appendStringInfo(&colvalues, "%s", comma);
/* string is "\x54617069727573", start after "\x" */
escape_json(&colvalues, (outputstr + 2));
break;
default:
appendStringInfo(&colvalues, "%s", comma);
escape_json(&colvalues, outputstr);
break;
}
}
/* The first column does not have comma */
if (strcmp(comma, "") == 0)
snprintf(comma, 3, ",%s", data->sp);
}
/* Column info ends */
if (replident)
{
appendStringInfo(&colnames, "],%s", data->nl);
if (data->include_types)
appendStringInfo(&coltypes, "],%s", data->nl);
if (data->include_type_oids)
appendStringInfo(&coltypeoids, "],%s", data->nl);
appendStringInfo(&colvalues, "]%s", data->nl);
appendStringInfo(&colvalues, "%s%s%s}%s", data->ht, data->ht, data->ht, data->nl);
}
else
{
appendStringInfo(&colnames, "],%s", data->nl);
if (data->include_types)
appendStringInfo(&coltypes, "],%s", data->nl);
if (data->include_type_oids)
appendStringInfo(&coltypeoids, "],%s", data->nl);
if (data->include_not_null)
appendStringInfo(&colnotnulls, "],%s", data->nl);
if (hasreplident)
appendStringInfo(&colvalues, "],%s", data->nl);
else
appendStringInfo(&colvalues, "]%s", data->nl);
}
/* Print data */
appendStringInfoString(ctx->out, colnames.data);
if (data->include_types)
appendStringInfoString(ctx->out, coltypes.data);
if (data->include_type_oids)
appendStringInfoString(ctx->out, coltypeoids.data);
if (data->include_not_null)
appendStringInfoString(ctx->out, colnotnulls.data);
appendStringInfoString(ctx->out, colvalues.data);
pfree(colnames.data);
pfree(coltypes.data);
if (data->include_type_oids)
pfree(coltypeoids.data);
if (data->include_not_null)
pfree(colnotnulls.data);
pfree(colvalues.data);
}
/* Print columns information */
static void
columns_to_stringinfo(LogicalDecodingContext *ctx, TupleDesc tupdesc, HeapTuple tuple, bool hasreplident)
{
tuple_to_stringinfo(ctx, tupdesc, tuple, NULL, false, hasreplident);
}
/* Print replica identity information */
static void
identity_to_stringinfo(LogicalDecodingContext *ctx, TupleDesc tupdesc, HeapTuple tuple, TupleDesc indexdesc)
{
/* Last parameter does not matter */
tuple_to_stringinfo(ctx, tupdesc, tuple, indexdesc, true, false);
}
/* Callback for individual changed tuples */
static void
pg_decode_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
Relation relation, ReorderBufferChange *change)
{
JsonDecodingData *data;
Form_pg_class class_form;
TupleDesc tupdesc;
MemoryContext old;
Relation indexrel;
TupleDesc indexdesc;
char *schemaname;
char *tablename;
AssertVariableIsOfType(&pg_decode_change, LogicalDecodeChangeCB);
data = ctx->output_plugin_private;
class_form = RelationGetForm(relation);
tupdesc = RelationGetDescr(relation);
/* Avoid leaking memory by using and resetting our own context */
old = MemoryContextSwitchTo(data->context);
/* schema and table names are used for select tables */
schemaname = get_namespace_name(class_form->relnamespace);
tablename = NameStr(class_form->relname);
if (data->write_in_chunks)
OutputPluginPrepareWrite(ctx, true);
/* Make sure rd_replidindex is set */
RelationGetIndexList(relation);
/* Filter tables, if available */
if (list_length(data->filter_tables) > 0)
{
ListCell *lc;
foreach(lc, data->filter_tables)
{
SelectTable *t = lfirst(lc);
if (t->allschemas || strcmp(t->schemaname, schemaname) == 0)
{
if (t->alltables || strcmp(t->tablename, tablename) == 0)
{
elog(DEBUG2, "\"%s\".\"%s\" was filtered out",
((t->allschemas) ? "*" : t->schemaname),
((t->alltables) ? "*" : t->tablename));
return;
}
}
}
}
/* Add tables */
if (list_length(data->add_tables) > 0)
{
ListCell *lc;
bool skip = true;
/* all tables in all schemas are added by default */
foreach(lc, data->add_tables)
{
SelectTable *t = lfirst(lc);
if (t->allschemas || strcmp(t->schemaname, schemaname) == 0)
{
if (t->alltables || strcmp(t->tablename, tablename) == 0)
{
elog(DEBUG2, "\"%s\".\"%s\" was added",
((t->allschemas) ? "*" : t->schemaname),
((t->alltables) ? "*" : t->tablename));
skip = false;
}
}
}
/* table was not found */
if (skip)
return;
}
/* Sanity checks */
switch (change->action)
{
case REORDER_BUFFER_CHANGE_INSERT:
if (change->data.tp.newtuple == NULL)
{
elog(WARNING, "no tuple data for INSERT in table \"%s\"", NameStr(class_form->relname));
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
return;
}
break;
case REORDER_BUFFER_CHANGE_UPDATE:
/*
* Bail out iif:
* (i) doesn't have a pk and replica identity is not full;
* (ii) replica identity is nothing.
*/
if (!OidIsValid(relation->rd_replidindex) && relation->rd_rel->relreplident != REPLICA_IDENTITY_FULL)
{
/* FIXME this sentence is imprecise */
elog(WARNING, "table \"%s\" without primary key or replica identity is nothing", NameStr(class_form->relname));
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
return;
}
if (change->data.tp.newtuple == NULL)
{
elog(WARNING, "no tuple data for UPDATE in table \"%s\"", NameStr(class_form->relname));
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
return;
}
break;
case REORDER_BUFFER_CHANGE_DELETE:
/*
* Bail out iif:
* (i) doesn't have a pk and replica identity is not full;
* (ii) replica identity is nothing.
*/
if (!OidIsValid(relation->rd_replidindex) && relation->rd_rel->relreplident != REPLICA_IDENTITY_FULL)
{
/* FIXME this sentence is imprecise */
elog(WARNING, "table \"%s\" without primary key or replica identity is nothing", NameStr(class_form->relname));
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
return;
}
if (change->data.tp.oldtuple == NULL)
{
elog(WARNING, "no tuple data for DELETE in table \"%s\"", NameStr(class_form->relname));
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
return;
}
break;
default:
Assert(false);
}
/* Change counter */
data->nr_changes++;
/* if we don't write in chunks, we need a newline here */
if (!data->write_in_chunks)
appendStringInfo(ctx->out, "%s", data->nl);
appendStringInfo(ctx->out, "%s%s", data->ht, data->ht);
if (data->nr_changes > 1)
appendStringInfoChar(ctx->out, ',');
appendStringInfo(ctx->out, "{%s", data->nl);
/* Print change kind */
switch (change->action)
{
case REORDER_BUFFER_CHANGE_INSERT:
appendStringInfo(ctx->out, "%s%s%s\"kind\":%s\"insert\",%s", data->ht, data->ht, data->ht, data->sp, data->nl);
break;
case REORDER_BUFFER_CHANGE_UPDATE:
appendStringInfo(ctx->out, "%s%s%s\"kind\":%s\"update\",%s", data->ht, data->ht, data->ht, data->sp, data->nl);
break;
case REORDER_BUFFER_CHANGE_DELETE:
appendStringInfo(ctx->out, "%s%s%s\"kind\":%s\"delete\",%s", data->ht, data->ht, data->ht, data->sp, data->nl);
break;
default:
Assert(false);
}
/* Print table name (possibly) qualified */
if (data->include_schemas)
{
appendStringInfo(ctx->out, "%s%s%s\"schema\":%s", data->ht, data->ht, data->ht, data->sp);
escape_json(ctx->out, get_namespace_name(class_form->relnamespace));
appendStringInfo(ctx->out, ",%s", data->nl);
}
appendStringInfo(ctx->out, "%s%s%s\"table\":%s", data->ht, data->ht, data->ht, data->sp);
escape_json(ctx->out, NameStr(class_form->relname));
appendStringInfo(ctx->out, ",%s", data->nl);
switch (change->action)
{
case REORDER_BUFFER_CHANGE_INSERT:
/* Print the new tuple */
columns_to_stringinfo(ctx, tupdesc, &change->data.tp.newtuple->tuple, false);
break;
case REORDER_BUFFER_CHANGE_UPDATE:
/* Print the new tuple */
columns_to_stringinfo(ctx, tupdesc, &change->data.tp.newtuple->tuple, true);
/*
* The old tuple is available when:
* (i) pk changes;
* (ii) replica identity is full;
* (iii) replica identity is index and indexed column changes.
*
* FIXME if old tuple is not available we must get only the indexed
* columns (the whole tuple is printed).
*/
if (change->data.tp.oldtuple == NULL)
{
elog(DEBUG1, "old tuple is null");
indexrel = RelationIdGetRelation(relation->rd_replidindex);
if (indexrel != NULL)
{
indexdesc = RelationGetDescr(indexrel);
identity_to_stringinfo(ctx, tupdesc, &change->data.tp.newtuple->tuple, indexdesc);
RelationClose(indexrel);
}
else
{
identity_to_stringinfo(ctx, tupdesc, &change->data.tp.newtuple->tuple, NULL);
}
}
else
{
elog(DEBUG1, "old tuple is not null");
identity_to_stringinfo(ctx, tupdesc, &change->data.tp.oldtuple->tuple, NULL);
}
break;
case REORDER_BUFFER_CHANGE_DELETE:
/* Print the replica identity */
indexrel = RelationIdGetRelation(relation->rd_replidindex);
if (indexrel != NULL)
{
indexdesc = RelationGetDescr(indexrel);
identity_to_stringinfo(ctx, tupdesc, &change->data.tp.oldtuple->tuple, indexdesc);
RelationClose(indexrel);
}
else
{
identity_to_stringinfo(ctx, tupdesc, &change->data.tp.oldtuple->tuple, NULL);
}
if (change->data.tp.oldtuple == NULL)
elog(DEBUG1, "old tuple is null");
else
elog(DEBUG1, "old tuple is not null");
break;
default:
Assert(false);
}
appendStringInfo(ctx->out, "%s%s}", data->ht, data->ht);
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
if (data->write_in_chunks)
OutputPluginWrite(ctx, true);