-
Notifications
You must be signed in to change notification settings - Fork 15
/
spock_proto_json.c
706 lines (622 loc) · 17.6 KB
/
spock_proto_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
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
/*-------------------------------------------------------------------------
*
* spock_proto_json.c
* spock protocol functions for json support
*
* Copyright (c) 2022-2023, pgEdge, Inc.
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, The Regents of the University of California
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/sysattr.h"
#include "catalog/index.h"
#include "catalog/namespace.h"
#include "catalog/pg_class.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "executor/spi.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "replication/reorderbuffer.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/json.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/timestamp.h"
#include "utils/typcache.h"
#include "spock_output_plugin.h"
#include "spock_proto_json.h"
#ifdef HAVE_REPLICATION_ORIGINS
#include "replication/origin.h"
#endif
static void
json_write_tuple(StringInfo out, Relation rel, HeapTuple tuple,
Bitmapset *att_list);
/*
* Write BEGIN to the output stream.
*/
void
spock_json_write_begin(StringInfo out, SpockOutputData *data, ReorderBufferTXN *txn)
{
appendStringInfoChar(out, '{');
appendStringInfoString(out, "\"action\":\"B\"");
appendStringInfo(out, ", \"has_catalog_changes\":\"%c\"",
rbtxn_has_catalog_changes(txn) ? 't' : 'f');
#ifdef HAVE_REPLICATION_ORIGINS
if (txn->origin_id != InvalidRepOriginId)
appendStringInfo(out, ", \"origin_id\":\"%u\"", txn->origin_id);
#endif
if (!data->client_no_txinfo)
{
appendStringInfo(out, ", \"xid\":\"%u\"", txn->xid);
appendStringInfo(out, ", \"first_lsn\":\"%X/%X\"",
(uint32)(txn->first_lsn >> 32), (uint32)(txn->first_lsn));
#ifdef HAVE_REPLICATION_ORIGINS
appendStringInfo(out, ", \"origin_lsn\":\"%X/%X\"",
(uint32)(txn->origin_lsn >> 32), (uint32)(txn->origin_lsn));
#endif
#if PG_VERSION_NUM >= 150000
if (txn->xact_time.commit_time != 0)
#else
if (txn->commit_time != 0)
#endif
appendStringInfo(out, ", \"commit_time\":\"%s\"",
#if PG_VERSION_NUM >= 150000
timestamptz_to_str(txn->xact_time.commit_time));
#else
timestamptz_to_str(txn->commit_time));
#endif
}
appendStringInfoChar(out, '}');
}
/*
* Write COMMIT to the output stream.
*/
void
spock_json_write_commit(StringInfo out, SpockOutputData *data, ReorderBufferTXN *txn,
XLogRecPtr commit_lsn)
{
appendStringInfoChar(out, '{');
appendStringInfoString(out, "\"action\":\"C\"");
if (!data->client_no_txinfo)
{
appendStringInfo(out, ", \"final_lsn\":\"%X/%X\"",
(uint32)(txn->final_lsn >> 32), (uint32)(txn->final_lsn));
appendStringInfo(out, ", \"end_lsn\":\"%X/%X\"",
(uint32)(txn->end_lsn >> 32), (uint32)(txn->end_lsn));
}
appendStringInfoChar(out, '}');
}
/*
* Write change.
*
* Generic function handling DML changes.
*/
static void
spock_json_write_change(StringInfo out, const char *change, Relation rel,
HeapTuple oldtuple, HeapTuple newtuple,
Bitmapset *att_list)
{
appendStringInfoChar(out, '{');
appendStringInfo(out, "\"action\":\"%s\",\"relation\":[\"%s\",\"%s\"]",
change,
get_namespace_name(RelationGetNamespace(rel)),
RelationGetRelationName(rel));
if (oldtuple)
{
appendStringInfoString(out, ",\"oldtuple\":");
json_write_tuple(out, rel, oldtuple, att_list);
}
if (newtuple)
{
appendStringInfoString(out, ",\"newtuple\":");
json_write_tuple(out, rel, newtuple, att_list);
}
appendStringInfoChar(out, '}');
}
/*
* Write INSERT to the output stream.
*/
void
spock_json_write_insert(StringInfo out, SpockOutputData *data,
Relation rel, HeapTuple newtuple,
Bitmapset *att_list)
{
spock_json_write_change(out, "I", rel, NULL, newtuple, att_list);
}
/*
* Write UPDATE to the output stream.
*/
void
spock_json_write_update(StringInfo out, SpockOutputData *data,
Relation rel, HeapTuple oldtuple,
HeapTuple newtuple, Bitmapset *att_list)
{
spock_json_write_change(out, "U", rel, oldtuple, newtuple, att_list);
}
/*
* Write DELETE to the output stream.
*/
void
spock_json_write_delete(StringInfo out, SpockOutputData *data,
Relation rel, HeapTuple oldtuple,
Bitmapset *att_list)
{
spock_json_write_change(out, "D", rel, oldtuple, NULL, att_list);
}
/*
* The startup message should be constructed as a json object, one
* key/value per DefElem list member.
*/
void
json_write_startup_message(StringInfo out, List *msg)
{
ListCell *lc;
bool first = true;
appendStringInfoString(out, "{\"action\":\"S\", \"params\": {");
foreach (lc, msg)
{
DefElem *param = (DefElem*)lfirst(lc);
Assert(IsA(param->arg, String) && strVal(param->arg) != NULL);
if (first)
first = false;
else
appendStringInfoChar(out, ',');
escape_json(out, param->defname);
appendStringInfoChar(out, ':');
escape_json(out, strVal(param->arg));
}
appendStringInfoString(out, "}}");
}
/*
* Functions taken from json.c
*
* Current as of commit 272adf4f9cd67df323ae57ff3dee238b649d3b73
*/
#include "parser/parse_coerce.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "common/jsonapi.h"
/*
* Determine how we want to print values of a given type in datum_to_json.
*
* Given the datatype OID, return its JsonTypeCategory, as well as the type's
* output function OID. If the returned category is JSONTYPE_CAST, we
* return the OID of the type->JSON cast function instead.
*/
typedef enum /* type categories for datum_to_json */
{
JSONTYPE_NULL, /* null, so we didn't bother to identify */
JSONTYPE_BOOL, /* boolean (built-in types only) */
JSONTYPE_NUMERIC, /* numeric (ditto) */
JSONTYPE_DATE, /* we use special formatting for datetimes */
JSONTYPE_TIMESTAMP,
JSONTYPE_TIMESTAMPTZ,
JSONTYPE_JSON, /* JSON itself (and JSONB) */
JSONTYPE_ARRAY, /* array */
JSONTYPE_COMPOSITE, /* composite */
JSONTYPE_CAST, /* something with an explicit cast to JSON */
JSONTYPE_OTHER /* all else */
} JsonTypeCategory;
static void composite_to_json(Datum composite, StringInfo result,
bool use_line_feeds);
static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims,
Datum *vals, bool *nulls, int *valcount,
JsonTypeCategory tcategory, Oid outfuncoid,
bool use_line_feeds);
static void array_to_json_internal(Datum array, StringInfo result,
bool use_line_feeds);
static void json_categorize_type(Oid typoid,
JsonTypeCategory *tcategory,
Oid *outfuncoid);
static void
json_categorize_type(Oid typoid,
JsonTypeCategory *tcategory,
Oid *outfuncoid)
{
bool typisvarlena;
/* Look through any domain */
typoid = getBaseType(typoid);
*outfuncoid = InvalidOid;
/*
* We need to get the output function for everything except date and
* timestamp types, array and composite types, booleans, and non-builtin
* types where there's a cast to json.
*/
switch (typoid)
{
case BOOLOID:
*tcategory = JSONTYPE_BOOL;
break;
case INT2OID:
case INT4OID:
case INT8OID:
case FLOAT4OID:
case FLOAT8OID:
case NUMERICOID:
getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
*tcategory = JSONTYPE_NUMERIC;
break;
case DATEOID:
*tcategory = JSONTYPE_DATE;
break;
case TIMESTAMPOID:
*tcategory = JSONTYPE_TIMESTAMP;
break;
case TIMESTAMPTZOID:
*tcategory = JSONTYPE_TIMESTAMPTZ;
break;
case JSONOID:
case JSONBOID:
getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
*tcategory = JSONTYPE_JSON;
break;
default:
/* Check for arrays and composites */
if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
|| typoid == RECORDARRAYOID)
*tcategory = JSONTYPE_ARRAY;
else if (type_is_rowtype(typoid)) /* includes RECORDOID */
*tcategory = JSONTYPE_COMPOSITE;
else
{
/* It's probably the general case ... */
*tcategory = JSONTYPE_OTHER;
/* but let's look for a cast to json, if it's not built-in */
if (typoid >= FirstNormalObjectId)
{
Oid castfunc;
CoercionPathType ctype;
ctype = find_coercion_pathway(JSONOID, typoid,
COERCION_EXPLICIT,
&castfunc);
if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc))
{
*tcategory = JSONTYPE_CAST;
*outfuncoid = castfunc;
}
else
{
/* non builtin type with no cast */
getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
}
}
else
{
/* any other builtin type */
getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
}
}
break;
}
}
/*
* Turn a Datum into JSON text, appending the string to "result".
*
* tcategory and outfuncoid are from a previous call to json_categorize_type,
* except that if is_null is true then they can be invalid.
*
* If key_scalar is true, the value is being printed as a key, so insist
* it's of an acceptable type, and force it to be quoted.
*/
static void
datum_to_json(Datum val, bool is_null, StringInfo result,
JsonTypeCategory tcategory, Oid outfuncoid,
bool key_scalar)
{
char *outputstr;
text *jsontext;
check_stack_depth();
/* callers are expected to ensure that null keys are not passed in */
Assert(!(key_scalar && is_null));
if (is_null)
{
appendStringInfoString(result, "null");
return;
}
if (key_scalar &&
(tcategory == JSONTYPE_ARRAY ||
tcategory == JSONTYPE_COMPOSITE ||
tcategory == JSONTYPE_JSON ||
tcategory == JSONTYPE_CAST))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("key value must be scalar, not array, composite, or json")));
switch (tcategory)
{
case JSONTYPE_ARRAY:
array_to_json_internal(val, result, false);
break;
case JSONTYPE_COMPOSITE:
composite_to_json(val, result, false);
break;
case JSONTYPE_BOOL:
outputstr = DatumGetBool(val) ? "true" : "false";
if (key_scalar)
escape_json(result, outputstr);
else
appendStringInfoString(result, outputstr);
break;
case JSONTYPE_NUMERIC:
outputstr = OidOutputFunctionCall(outfuncoid, val);
/*
* Don't call escape_json for a non-key if it's a valid JSON
* number.
*/
if (!key_scalar && IsValidJsonNumber(outputstr, strlen(outputstr)))
appendStringInfoString(result, outputstr);
else
escape_json(result, outputstr);
pfree(outputstr);
break;
case JSONTYPE_DATE:
{
DateADT date;
struct pg_tm tm;
char buf[MAXDATELEN + 1];
date = DatumGetDateADT(val);
/* Same as date_out(), but forcing DateStyle */
if (DATE_NOT_FINITE(date))
EncodeSpecialDate(date, buf);
else
{
j2date(date + POSTGRES_EPOCH_JDATE,
&(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
EncodeDateOnly(&tm, USE_XSD_DATES, buf);
}
appendStringInfo(result, "\"%s\"", buf);
}
break;
case JSONTYPE_TIMESTAMP:
{
Timestamp timestamp;
struct pg_tm tm;
fsec_t fsec;
char buf[MAXDATELEN + 1];
timestamp = DatumGetTimestamp(val);
/* Same as timestamp_out(), but forcing DateStyle */
if (TIMESTAMP_NOT_FINITE(timestamp))
EncodeSpecialTimestamp(timestamp, buf);
else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
else
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
appendStringInfo(result, "\"%s\"", buf);
}
break;
case JSONTYPE_TIMESTAMPTZ:
{
TimestampTz timestamp;
struct pg_tm tm;
int tz;
fsec_t fsec;
const char *tzn = NULL;
char buf[MAXDATELEN + 1];
timestamp = DatumGetTimestampTz(val);
/* Same as timestamptz_out(), but forcing DateStyle */
if (TIMESTAMP_NOT_FINITE(timestamp))
EncodeSpecialTimestamp(timestamp, buf);
else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
else
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
appendStringInfo(result, "\"%s\"", buf);
}
break;
case JSONTYPE_JSON:
/* JSON and JSONB output will already be escaped */
outputstr = OidOutputFunctionCall(outfuncoid, val);
appendStringInfoString(result, outputstr);
pfree(outputstr);
break;
case JSONTYPE_CAST:
/* outfuncoid refers to a cast function, not an output function */
jsontext = DatumGetTextP(OidFunctionCall1(outfuncoid, val));
outputstr = text_to_cstring(jsontext);
appendStringInfoString(result, outputstr);
pfree(outputstr);
pfree(jsontext);
break;
default:
outputstr = OidOutputFunctionCall(outfuncoid, val);
escape_json(result, outputstr);
pfree(outputstr);
break;
}
}
/*
* Process a single dimension of an array.
* If it's the innermost dimension, output the values, otherwise call
* ourselves recursively to process the next dimension.
*/
static void
array_dim_to_json(StringInfo result, int dim, int ndims, int *dims, Datum *vals,
bool *nulls, int *valcount, JsonTypeCategory tcategory,
Oid outfuncoid, bool use_line_feeds)
{
int i;
const char *sep;
Assert(dim < ndims);
sep = use_line_feeds ? ",\n " : ",";
appendStringInfoChar(result, '[');
for (i = 1; i <= dims[dim]; i++)
{
if (i > 1)
appendStringInfoString(result, sep);
if (dim + 1 == ndims)
{
datum_to_json(vals[*valcount], nulls[*valcount], result, tcategory,
outfuncoid, false);
(*valcount)++;
}
else
{
/*
* Do we want line feeds on inner dimensions of arrays? For now
* we'll say no.
*/
array_dim_to_json(result, dim + 1, ndims, dims, vals, nulls,
valcount, tcategory, outfuncoid, false);
}
}
appendStringInfoChar(result, ']');
}
/*
* Turn an array into JSON.
*/
static void
array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
{
ArrayType *v = DatumGetArrayTypeP(array);
Oid element_type = ARR_ELEMTYPE(v);
int *dim;
int ndim;
int nitems;
int count = 0;
Datum *elements;
bool *nulls;
int16 typlen;
bool typbyval;
char typalign;
JsonTypeCategory tcategory;
Oid outfuncoid;
ndim = ARR_NDIM(v);
dim = ARR_DIMS(v);
nitems = ArrayGetNItems(ndim, dim);
if (nitems <= 0)
{
appendStringInfoString(result, "[]");
return;
}
get_typlenbyvalalign(element_type,
&typlen, &typbyval, &typalign);
json_categorize_type(element_type,
&tcategory, &outfuncoid);
deconstruct_array(v, element_type, typlen, typbyval,
typalign, &elements, &nulls,
&nitems);
array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory,
outfuncoid, use_line_feeds);
pfree(elements);
pfree(nulls);
}
/*
* Turn a composite / record into JSON.
*/
static void
composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
{
HeapTupleHeader td;
Oid tupType;
int32 tupTypmod;
TupleDesc tupdesc;
HeapTupleData tmptup,
*tuple;
int i;
bool needsep = false;
const char *sep;
sep = use_line_feeds ? ",\n " : ",";
td = DatumGetHeapTupleHeader(composite);
/* Extract rowtype info and find a tupdesc */
tupType = HeapTupleHeaderGetTypeId(td);
tupTypmod = HeapTupleHeaderGetTypMod(td);
tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
/* Build a temporary HeapTuple control structure */
tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
tmptup.t_data = td;
tuple = &tmptup;
appendStringInfoChar(result, '{');
for (i = 0; i < tupdesc->natts; i++)
{
Datum val;
bool isnull;
char *attname;
JsonTypeCategory tcategory;
Oid outfuncoid;
if (TupleDescAttr(tupdesc,i)->attisdropped)
continue;
if (needsep)
appendStringInfoString(result, sep);
needsep = true;
attname = NameStr(TupleDescAttr(tupdesc,i)->attname);
escape_json(result, attname);
appendStringInfoChar(result, ':');
val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
if (isnull)
{
tcategory = JSONTYPE_NULL;
outfuncoid = InvalidOid;
}
else
json_categorize_type(TupleDescAttr(tupdesc,i)->atttypid,
&tcategory, &outfuncoid);
datum_to_json(val, isnull, result, tcategory, outfuncoid, false);
}
appendStringInfoChar(result, '}');
ReleaseTupleDesc(tupdesc);
}
/*
* And finally the function that uses the above json functions.
*/
/*
* Write a tuple to the outputstream, in the most efficient format possible.
*/
static void
json_write_tuple(StringInfo out, Relation rel, HeapTuple tuple,
Bitmapset *att_list)
{
TupleDesc tupdesc;
int i;
bool needsep = false;
Datum values[MaxTupleAttributeNumber];
bool isnull[MaxTupleAttributeNumber];
tupdesc = RelationGetDescr(rel);
appendStringInfoChar(out, '{');
heap_deform_tuple(tuple, tupdesc, values, isnull);
for (i = 0; i < tupdesc->natts; i++)
{
Form_pg_attribute att = TupleDescAttr(tupdesc,i);
JsonTypeCategory tcategory;
Oid outfuncoid;
/* skip dropped columns */
if (att->attisdropped)
continue;
if (att_list &&
!bms_is_member(att->attnum - FirstLowInvalidHeapAttributeNumber,
att_list))
continue;
/*
* Don't send unchanged toast column as we may not be able to fetch
* them.
*/
if (!isnull[i] && att->attlen == -1 &&
VARATT_IS_EXTERNAL_ONDISK(values[i]))
continue;
if (needsep)
appendStringInfoChar(out, ',');
needsep = true;
escape_json(out, NameStr(att->attname));
appendStringInfoChar(out, ':');
if (isnull[i])
{
tcategory = JSONTYPE_NULL;
outfuncoid = InvalidOid;
}
else
json_categorize_type(att->atttypid, &tcategory, &outfuncoid);
datum_to_json(values[i], isnull[i], out, tcategory, outfuncoid, false);
}
appendStringInfoChar(out, '}');
}