-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathspock_jsonb_utils.c
258 lines (209 loc) · 6.3 KB
/
spock_jsonb_utils.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
#include "postgres.h"
#include "miscadmin.h"
#include "libpq/libpq-be.h"
#include "access/commit_ts.h"
#include "access/xact.h"
#include "commands/dbcommands.h"
#include "common/hashfn.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/pg_type.h"
#include "executor/spi.h"
#include "nodes/makefuncs.h"
#include "postmaster/interrupt.h"
#include "storage/ipc.h"
#include "storage/proc.h"
#include "storage/procsignal.h"
#include "storage/procarray.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/timestamp.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/builtins.h"
#include "utils/json.h"
#include "utils/jsonb.h"
#include "utils/jsonfuncs.h"
#include "utils/typcache.h"
#include "utils/attoptcache.h"
#include "parser/parse_coerce.h"
#include "replication/origin.h"
#include "replication/slot.h"
#include "pgstat.h"
#include "spock_sync.h"
#include "spock_worker.h"
#include "spock_conflict.h"
#include "spock_relcache.h"
#include "spock_jsonb_utils.h"
/*
* Convert one Datum to CString containing valid Json
*
* This may be optimized to use a direct call of to_json(ANYELEMENT)
* via the fmgr. However, this is not performance critical enough to
* develop this now because due to ANYELEMENT this involves the
* executor and not just the fmgr.
*/
static char *
datum_to_json_cstring(Datum val, Oid type, bool isnull)
{
char *query = "SELECT pg_catalog.to_json($1)::text";
Oid argtypes[1];
Datum values[1];
Datum resval;
char *result;
bool resnull;
int rc;
/* Return a 'null' string if called with a NULL value */
if (isnull)
return "null";
/* Setup call arguments for SPI_execute_with_args() */
argtypes[0] = type;
values[0] = val;
/* Execute the SPI query */
rc = SPI_execute_with_args(query, 1, argtypes, values, NULL, true, 0);
if (rc != SPI_OK_SELECT)
elog(ERROR, "datum_to_json_cstring: SPI query '%s' returned %d",
query, rc);
if (SPI_processed != 1)
elog(ERROR, "datum_to_json_cstring: query returned %lu tupes - "
"expected 1", SPI_processed);
/* Get the binary text datum */
resval = SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc,
1, &resnull);
/* If for any reason to_json() returned NULL we convert to 'null' string */
if (resnull)
{
SPI_freetuptable(SPI_tuptable);
return "null";
}
/* Convert the result to CString, cleanup and return result */
result = DatumGetCString(DirectFunctionCall1(textout, resval));
SPI_freetuptable(SPI_tuptable);
return result;
}
static inline char *
name_to_json_cstring(Name name)
{
return datum_to_json_cstring(NameGetDatum(name), NAMEOID, false);
}
char *
spock_tuple_to_json_cstring(SpockTupleData *tuple, TupleDesc tupleDesc)
{
int natt;
Oid typid;
int rc;
StringInfoData s;
bool add_comma = false;
initStringInfo(&s);
appendStringInfoString(&s, "[");
/* Connect to SPI, every call to convert a datum to json needs it */
rc = SPI_connect();
if (rc != SPI_OK_CONNECT)
elog(ERROR, "datum_to_json_cstring: SPI_connect returned %d", rc);
/* print all columns individually */
for (natt = 0; natt < tupleDesc->natts; natt++)
{
Form_pg_attribute attr; /* the attribute itself */
HeapTuple type_tuple; /* information about a type */
Form_pg_type type_form;
attr = TupleDescAttr(tupleDesc, natt);
/*
* don't print dropped columns, we can't be sure everything is
* available for them
*/
if (attr->attisdropped)
continue;
/*
* Don't print system columns
*/
if (attr->attnum < 0)
continue;
typid = attr->atttypid;
/* gather type name */
type_tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
if (!HeapTupleIsValid(type_tuple))
elog(ERROR, "cache lookup failed for type %u", typid);
type_form = (Form_pg_type) GETSTRUCT(type_tuple);
ReleaseSysCache(type_tuple);
/* Assemble the attributes of the tuple */
if (add_comma)
appendStringInfoString(&s, ", ");
else
add_comma = true;
appendStringInfo(&s, "{\"attname\": %s, ",
name_to_json_cstring(&attr->attname));
appendStringInfo(&s, "\"atttype\": %s, ",
name_to_json_cstring(&type_form->typname));
appendStringInfo(&s, "\"value\": %s}",
datum_to_json_cstring(tuple->values[natt],
type_form->oid,
tuple->nulls[natt]));
}
/* Cleanup SPI */
SPI_finish();
/* Terminate the json string and return the result */
appendStringInfoString(&s, "]");
return s.data;
}
char *
heap_tuple_to_json_cstring(HeapTuple *tuple, TupleDesc tupleDesc)
{
int natt;
bool add_comma = false;
StringInfoData s;
int rc;
initStringInfo(&s);
appendStringInfo(&s, "[");
/* Connect to SPI, every call to convert a datum to json needs it */
rc = SPI_connect();
if (rc != SPI_OK_CONNECT)
elog(ERROR, "datum_to_json_cstring: SPI_connect returned %d", rc);
/* print all columns individually */
for (natt = 0; natt < tupleDesc->natts; natt++)
{
Form_pg_attribute attr; /* the attribute itself */
Oid typid; /* type of current attribute */
HeapTuple type_tuple; /* information about a type */
Form_pg_type type_form;
Datum origval = PointerGetDatum(NULL); /* possibly toasted
* Datum */
bool isnull = false; /* column is null? */
attr = TupleDescAttr(tupleDesc, natt);
/*
* don't print dropped columns, we can't be sure everything is
* available for them
*/
if (attr->attisdropped)
continue;
/*
* Don't print system columns
*/
if (attr->attnum < 0)
continue;
typid = attr->atttypid;
/* gather type name */
type_tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
if (!HeapTupleIsValid(type_tuple))
elog(ERROR, "cache lookup failed for type %u", typid);
type_form = (Form_pg_type) GETSTRUCT(type_tuple);
ReleaseSysCache(type_tuple);
/* Assemble the attributes of the tuple */
if (add_comma)
appendStringInfoString(&s, ", ");
else
add_comma = true;
appendStringInfo(&s, "{\"attname\": %s, ",
name_to_json_cstring(&attr->attname));
appendStringInfo(&s, "\"atttype\": %s, ",
name_to_json_cstring(&type_form->typname));
origval = heap_getattr(*tuple, natt + 1, tupleDesc, &isnull);
appendStringInfo(&s, "\"value\": %s}",
datum_to_json_cstring(origval,
type_form->oid,
isnull));
}
/* Cleanup SPI */
SPI_finish();
appendStringInfoString(&s, "]");
return s.data;
}