forked from eulerto/wal2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wal2json.c
3096 lines (2684 loc) · 88.3 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-2022, Euler Taveira de Oliveira
*
* IDENTIFICATION
* contrib/wal2json/wal2json.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/sysattr.h"
#include "catalog/indexing.h"
#include "catalog/pg_attrdef.h"
#include "catalog/pg_type.h"
#include "replication/logical.h"
#if PG_VERSION_NUM >= 90500
#include "replication/origin.h"
#endif
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/guc.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"
#define WAL2JSON_FORMAT_VERSION 2
#define WAL2JSON_FORMAT_MIN_VERSION 1
PG_MODULE_MAGIC;
extern void _PG_init(void);
extern void PGDLLEXPORT _PG_output_plugin_init(OutputPluginCallbacks *cb);
typedef struct
{
bool insert;
bool update;
bool delete;
bool truncate;
} JsonAction;
typedef struct
{
MemoryContext context;
bool include_transaction; /* BEGIN and COMMIT objects (v2) */
bool include_xids; /* include transaction ids */
bool include_timestamp; /* include transaction timestamp */
bool include_origin; /* replication origin */
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_domain_data_type; /* include underlying data type of the domain */
bool include_column_positions; /* include column numbers */
bool include_not_null; /* include not-null constraints */
bool include_default; /* include default expressions */
bool include_pk; /* include primary key */
bool pretty_print; /* pretty-print JSON? */
bool write_in_chunks; /* write in chunks? (v1) */
bool skip_empty_xacts; /* skip empty transactions */
JsonAction actions; /* output only these actions */
List *filter_origins; /* filter out origins */
List *filter_tables; /* filter out tables */
List *add_tables; /* add only these tables */
List *filter_msg_prefixes; /* filter by message prefixes */
List *add_msg_prefixes; /* add only messages with these prefixes */
int format_version; /* support different formats */
/*
* 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;
/*
* Maintain a per-transaction structure to track whether the
* transaction have written any changes. This is required so that if user
* has requested to skip the empty transactions we can skip the empty streams
* even though the transaction has written some changes.
*/
typedef struct
{
bool xact_wrote_changes;
} JsonTxnData;
typedef enum
{
PGOUTPUTJSON_CHANGE,
PGOUTPUTJSON_IDENTITY,
PGOUTPUTJSON_PK
} PGOutputJsonKind;
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_output_begin(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn,
bool wrote_change);
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 >= 90500
static bool pg_filter_by_origin(LogicalDecodingContext *ctx, RepOriginId origin_id);
#endif
#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
#if PG_VERSION_NUM >= 110000
static void pg_decode_truncate(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, int n, Relation relations[],
ReorderBufferChange *change);
#endif
static void columns_to_stringinfo(LogicalDecodingContext *ctx, TupleDesc tupdesc, HeapTuple tuple, bool addcomma, Relation relation);
static void tuple_to_stringinfo(LogicalDecodingContext *ctx, TupleDesc tupdesc, HeapTuple tuple, Bitmapset *bs, bool replident, bool addcomma, Relation relation);
static void pk_to_stringinfo(LogicalDecodingContext *ctx, TupleDesc tupdesc, HeapTuple tuple, Bitmapset *bs, bool addcomma);
static void identity_to_stringinfo(LogicalDecodingContext *ctx, TupleDesc tupdesc, HeapTuple tuple, Bitmapset *bs);
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);
static bool split_string_to_list(char *rawstring, char separator, List **sl);
static bool split_string_to_oid_list(char *rawstring, char separator, List **sl);
static bool pg_filter_by_action(int change_type, JsonAction actions);
static bool pg_filter_by_table(List *filter_tables, char *schemaname, char *tablename);
static bool pg_add_by_table(List *add_tables, char *schemaname, char *tablename);
/* version 1 */
static void pg_decode_begin_txn_v1(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, bool wrote_change);
static void pg_decode_commit_txn_v1(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, XLogRecPtr commit_lsn);
static void pg_decode_change_v1(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, Relation rel,
ReorderBufferChange *change);
#if PG_VERSION_NUM >= 90600
static void pg_decode_message_v1(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, XLogRecPtr lsn,
bool transactional, const char *prefix,
Size content_size, const char *content);
#endif
#if PG_VERSION_NUM >= 110000
static void pg_decode_truncate_v1(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, int n, Relation relations[],
ReorderBufferChange *change);
#endif
/* version 2 */
static void pg_decode_begin_txn_v2(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, bool wrote_change);
static void pg_decode_commit_txn_v2(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, XLogRecPtr commit_lsn);
static void pg_decode_write_value(LogicalDecodingContext *ctx, Datum value, bool isnull, Oid typid);
static void pg_decode_write_tuple(LogicalDecodingContext *ctx, Relation relation, HeapTuple tuple, PGOutputJsonKind kind);
static void pg_decode_write_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change);
static void pg_decode_change_v2(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, Relation rel,
ReorderBufferChange *change);
#if PG_VERSION_NUM >= 90600
static void pg_decode_message_v2(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, XLogRecPtr lsn,
bool transactional, const char *prefix,
Size content_size, const char *content);
#endif
#if PG_VERSION_NUM >= 110000
static void pg_decode_truncate_v2(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn, int n, Relation relations[],
ReorderBufferChange *change);
#endif
/*
* Backward compatibility.
*
* This macro is only available in 9.6+.
*/
#if PG_VERSION_NUM < 90600
#ifdef USE_FLOAT8_BYVAL
#define UInt64GetDatum(X) ((Datum) (X))
#else
#define UInt64GetDatum(X) Int64GetDatum((int64) (X))
#endif
#endif
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 >= 90500
cb->filter_by_origin_cb = pg_filter_by_origin;
#endif
#if PG_VERSION_NUM >= 90600
cb->message_cb = pg_decode_message;
#endif
#if PG_VERSION_NUM >= 110000
cb->truncate_cb = pg_decode_truncate;
#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_transaction = true;
data->include_xids = false;
data->include_timestamp = false;
data->include_pk = false;
data->include_origin = false;
data->include_schemas = true;
data->include_types = true;
data->include_type_oids = false;
data->include_typmod = true;
data->include_domain_data_type = false;
data->include_column_positions = false;
data->pretty_print = false;
data->write_in_chunks = false;
data->include_lsn = false;
data->include_not_null = false;
data->include_default = false;
data->skip_empty_xacts = false;
data->filter_origins = NIL;
data->filter_tables = NIL;
data->filter_msg_prefixes = NIL;
data->add_msg_prefixes = NIL;
data->format_version = 1;
/* default actions */
if (WAL2JSON_FORMAT_VERSION == 1)
{
data->actions.insert = true;
data->actions.update = true;
data->actions.delete = true;
data->actions.truncate = false; /* backward compatibility */
}
else
{
data->actions.insert = true;
data->actions.update = true;
data->actions.delete = true;
data->actions.truncate = true;
}
/* pretty print */
data->ht[0] = '\0';
data->nl[0] = '\0';
data->sp[0] = '\0';
/* 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-transaction") == 0)
{
/* if option value is NULL then assume that 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, "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-pk") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "include-pk argument is null");
data->include_pk = true;
}
else if (!parse_bool(strVal(elem->arg), &data->include_pk))
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-origin") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "include-origin argument is null");
data->include_origin = true;
}
else if (!parse_bool(strVal(elem->arg), &data->include_origin))
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-domain-data-type") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "include-types argument is null");
data->include_domain_data_type = true;
}
else if (!parse_bool(strVal(elem->arg), &data->include_domain_data_type))
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-column-positions") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "include-column-positions argument is null");
data->include_column_positions = true;
}
else if (!parse_bool(strVal(elem->arg), &data->include_column_positions))
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, "include-default") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "include-default argument is null");
data->include_default = true;
}
else if (!parse_bool(strVal(elem->arg), &data->include_default))
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)
{
data->ht[0] = '\t';
data->nl[0] = '\n';
data->sp[0] = ' ';
}
}
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, "actions") == 0)
{
char *rawstr;
if (elem->arg == NULL)
{
elog(DEBUG1, "actions argument is null");
/* argument null means default; nothing to do here */
}
else
{
List *selected_actions = NIL;
ListCell *lc;
rawstr = pstrdup(strVal(elem->arg));
if (!split_string_to_list(rawstr, ',', &selected_actions))
{
pfree(rawstr);
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("could not parse value \"%s\" for parameter \"%s\"",
strVal(elem->arg), elem->defname)));
}
data->actions.insert = false;
data->actions.update = false;
data->actions.delete = false;
data->actions.truncate = false;
foreach(lc, selected_actions)
{
char *p = lfirst(lc);
if (strcmp(p, "insert") == 0)
data->actions.insert = true;
else if (strcmp(p, "update") == 0)
data->actions.update = true;
else if (strcmp(p, "delete") == 0)
data->actions.delete = true;
else if (strcmp(p, "truncate") == 0)
data->actions.truncate = true;
else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not parse value \"%s\" for parameter \"%s\"",
p, elem->defname)));
}
pfree(rawstr);
list_free(selected_actions);
}
}
else if (strcmp(elem->defname, "filter-origins") == 0)
{
char *rawstr;
if (elem->arg == NULL)
{
elog(DEBUG1, "filter-origins argument is null");
data->filter_origins = NIL;
}
else
{
rawstr = pstrdup(strVal(elem->arg));
if (!split_string_to_oid_list(rawstr, ',', &data->filter_origins))
{
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, "skip-empty-xacts") ==0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "skip-empty-xacts argument is null");
data->skip_empty_xacts = false;
}
else if (!parse_bool(strVal(elem->arg), &data->skip_empty_xacts))
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, "filter-tables") == 0)
{
char *rawstr;
if (elem->arg == NULL)
{
elog(DEBUG1, "filter-tables argument is null");
data->filter_tables = NIL;
}
else
{
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 if (strcmp(elem->defname, "filter-msg-prefixes") == 0)
{
char *rawstr;
if (elem->arg == NULL)
{
elog(DEBUG1, "filter-msg-prefixes argument is null");
data->filter_msg_prefixes = NIL;
}
else
{
rawstr = pstrdup(strVal(elem->arg));
if (!split_string_to_list(rawstr, ',', &data->filter_msg_prefixes))
{
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-msg-prefixes") == 0)
{
char *rawstr;
if (elem->arg == NULL)
{
elog(DEBUG1, "add-msg-prefixes argument is null");
data->add_msg_prefixes = NIL;
}
else
{
rawstr = pstrdup(strVal(elem->arg));
if (!split_string_to_list(rawstr, ',', &data->add_msg_prefixes))
{
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, "format-version") == 0)
{
if (elem->arg == NULL)
{
elog(DEBUG1, "format-version argument is null");
data->format_version = WAL2JSON_FORMAT_VERSION;
}
else if (!parse_int(strVal(elem->arg), &data->format_version, 0, NULL))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not parse value \"%s\" for parameter \"%s\"",
strVal(elem->arg), elem->defname)));
if (data->format_version > WAL2JSON_FORMAT_VERSION)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("client sent format_version=%d but we only support format %d or lower",
data->format_version, WAL2JSON_FORMAT_VERSION)));
if (data->format_version < WAL2JSON_FORMAT_MIN_VERSION)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("client sent format_version=%d but we only support format %d or higher",
data->format_version, WAL2JSON_FORMAT_MIN_VERSION)));
}
else
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("option \"%s\" = \"%s\" is unknown",
elem->defname,
elem->arg ? strVal(elem->arg) : "(null)")));
}
}
elog(DEBUG2, "format version: %d", data->format_version);
}
/* 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);
}
#if PG_VERSION_NUM >= 90500
static bool
pg_filter_by_origin(LogicalDecodingContext *ctx, RepOriginId origin_id)
{
JsonDecodingData *data = ctx->output_plugin_private;
elog(DEBUG3, "origin: %u", origin_id);
/* changes produced locally are never filtered */
if (origin_id == InvalidRepOriginId)
return false;
/* Filter origins, if available */
if (list_length(data->filter_origins) > 0 && list_member_oid(data->filter_origins, origin_id))
{
elog(DEBUG2, "origin \"%u\" was filtered out", origin_id);
return true;
}
/*
* There isn't a list of origins to filter or origin is not contained in
* the filter list hence forward to all subscribers.
*/
return false;
}
#endif
/* BEGIN callback */
static void
pg_decode_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
{
JsonDecodingData *data = ctx->output_plugin_private;
JsonTxnData *txndata = MemoryContextAllocZero(ctx->context, sizeof(JsonTxnData));
txndata->xact_wrote_changes = false;
txn->output_plugin_private = txndata;
/*
* If asked to skip empty transactions, we'll emit BEGIN at the point
* where the first operation is received for this transaction.
*/
if (data->skip_empty_xacts)
{
elog(DEBUG2, "skipping an empty transaction");
return;
}
pg_output_begin(ctx, txn, true);
}
static void
pg_output_begin(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, bool wrote_change)
{
JsonDecodingData *data = ctx->output_plugin_private;
if (data->format_version == 2)
pg_decode_begin_txn_v2(ctx, txn, wrote_change);
else if (data->format_version == 1)
pg_decode_begin_txn_v1(ctx, txn, wrote_change);
else
elog(ERROR, "format version %d is not supported", data->format_version);
}
static void
pg_decode_begin_txn_v1(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, bool wrote_change)
{
JsonDecodingData *data = ctx->output_plugin_private;
data->nr_changes = 0;
/* Transaction starts */
OutputPluginPrepareWrite(ctx, wrote_change);
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, UInt64GetDatum(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);
#if PG_VERSION_NUM >= 90500
if (data->include_origin)
appendStringInfo(ctx->out, "%s\"origin\":%s%u,%s", data->ht, data->sp, txn->origin_id, data->nl);
#endif
appendStringInfo(ctx->out, "%s\"change\":%s[", data->ht, data->sp);
if (data->write_in_chunks)
OutputPluginWrite(ctx, wrote_change);
}
static void
pg_decode_begin_txn_v2(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, bool wrote_change)
{
JsonDecodingData *data = ctx->output_plugin_private;
/* don't include BEGIN object */
if (!data->include_transaction)
return;
OutputPluginPrepareWrite(ctx, wrote_change);
appendStringInfoString(ctx->out, "{\"action\":\"B\"");
if (data->include_xids)
appendStringInfo(ctx->out, ",\"xid\":%u", txn->xid);
if (data->include_timestamp)
appendStringInfo(ctx->out, ",\"timestamp\":\"%s\"", timestamptz_to_str(txn->commit_time));
#if PG_VERSION_NUM >= 90500
if (data->include_origin)
appendStringInfo(ctx->out, ",\"origin\":%u", txn->origin_id);
#endif
if (data->include_lsn)
{
char *lsn_str = DatumGetCString(DirectFunctionCall1(pg_lsn_out, UInt64GetDatum(txn->final_lsn)));
appendStringInfo(ctx->out, ",\"lsn\":\"%s\"", lsn_str);
pfree(lsn_str);
lsn_str = DatumGetCString(DirectFunctionCall1(pg_lsn_out, UInt64GetDatum(txn->end_lsn)));
appendStringInfo(ctx->out, ",\"nextlsn\":\"%s\"", lsn_str);
pfree(lsn_str);
}
appendStringInfoChar(ctx->out, '}');
OutputPluginWrite(ctx, wrote_change);
}
/* COMMIT callback */
static void
pg_decode_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
XLogRecPtr commit_lsn)
{
JsonDecodingData *data = ctx->output_plugin_private;
JsonTxnData *txndata = txn->output_plugin_private;
bool xact_wrote_changes = txndata->xact_wrote_changes;
#if PG_VERSION_NUM >= 100000
OutputPluginUpdateProgress(ctx);
#endif
pfree(txndata);
txn->output_plugin_private = NULL;
if (data->skip_empty_xacts && !xact_wrote_changes)
return;
elog(DEBUG2, "my change counter: " UINT64_FORMAT " ; # of changes: " UINT64_FORMAT " ; # of changes in memory: " UINT64_FORMAT, data->nr_changes, txn->nentries, txn->nentries_mem);
elog(DEBUG2, "# of subxacts: %d", txn->nsubtxns);
if (data->format_version == 2)
pg_decode_commit_txn_v2(ctx, txn, commit_lsn);
else if (data->format_version == 1)
pg_decode_commit_txn_v1(ctx, txn, commit_lsn);
else
elog(ERROR, "format version %d is not supported", data->format_version);
}
static void
pg_decode_commit_txn_v1(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
XLogRecPtr commit_lsn)
{
JsonDecodingData *data = ctx->output_plugin_private;
/* 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);
}
static void
pg_decode_commit_txn_v2(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
XLogRecPtr commit_lsn)
{
JsonDecodingData *data = ctx->output_plugin_private;
/* don't include COMMIT object */
if (!data->include_transaction)
return;
OutputPluginPrepareWrite(ctx, true);
appendStringInfoString(ctx->out, "{\"action\":\"C\"");
if (data->include_xids)
appendStringInfo(ctx->out, ",\"xid\":%u", txn->xid);
if (data->include_timestamp)
appendStringInfo(ctx->out, ",\"timestamp\":\"%s\"", timestamptz_to_str(txn->commit_time));
#if PG_VERSION_NUM >= 90500
if (data->include_origin)
appendStringInfo(ctx->out, ",\"origin\":%u", txn->origin_id);
#endif
if (data->include_lsn)
{
char *lsn_str = DatumGetCString(DirectFunctionCall1(pg_lsn_out, UInt64GetDatum(commit_lsn)));
appendStringInfo(ctx->out, ",\"lsn\":\"%s\"", lsn_str);
pfree(lsn_str);
lsn_str = DatumGetCString(DirectFunctionCall1(pg_lsn_out, UInt64GetDatum(txn->end_lsn)));