Skip to content

Commit 3874abf

Browse files
author
Nikita Glukhov
committed
Optimize jsonb returning using PG_RETURN_JSONB_VALUE_P()
1 parent 5cd7b7e commit 3874abf

File tree

7 files changed

+79
-63
lines changed

7 files changed

+79
-63
lines changed

contrib/hstore/hstore_io.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ hstore_to_jsonb(PG_FUNCTION_ARGS)
14131413

14141414
res = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
14151415

1416-
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
1416+
PG_RETURN_JSONB_VALUE_P(res);
14171417
}
14181418

14191419
PG_FUNCTION_INFO_V1(hstore_to_jsonb_loose);
@@ -1489,5 +1489,5 @@ hstore_to_jsonb_loose(PG_FUNCTION_ARGS)
14891489

14901490
res = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
14911491

1492-
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
1492+
PG_RETURN_JSONB_VALUE_P(res);
14931493
}

contrib/jsonb_plperl/jsonb_plperl.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ plperl_to_jsonb(PG_FUNCTION_ARGS)
290290
JsonbParseState *jsonb_state = NULL;
291291
SV *in = (SV *) PG_GETARG_POINTER(0);
292292
JsonbValue *out = SV_to_JsonbValue(in, &jsonb_state, true);
293-
Jsonb *result = JsonbValueToJsonb(out);
294293

295-
PG_RETURN_JSONB_P(result);
294+
PG_RETURN_JSONB_VALUE_P(out);
296295
}

contrib/jsonb_plpython/jsonb_plpython.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ plpython_to_jsonb(PG_FUNCTION_ARGS)
461461

462462
obj = (PyObject *) PG_GETARG_POINTER(0);
463463
out = PLyObject_ToJsonbValue(obj, &jsonb_state, true);
464-
PG_RETURN_JSONB_P(JsonbValueToJsonb(out));
464+
PG_RETURN_JSONB_VALUE_P(out);
465465
}
466466

467467
/*

src/backend/utils/adt/jsonb.c

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ JsonValueFromCString(char *json, int len, bool unique_keys)
268268
static inline Datum
269269
jsonb_from_cstring(char *json, int len, bool unique_keys)
270270
{
271-
PG_RETURN_JSONB_P(JsonbValueToJsonb(JsonValueFromCString(json, len, unique_keys)));
271+
PG_RETURN_JSONB_VALUE_P(JsonValueFromCString(json, len, unique_keys));
272272
}
273273

274274
static void
@@ -1074,16 +1074,22 @@ add_jsonb(Datum val, bool is_null, JsonbInState *result,
10741074
datum_to_jsonb(val, is_null, result, tcategory, outfuncoid, key_scalar);
10751075
}
10761076

1077-
Datum
1078-
to_jsonb_worker(Datum val, JsonbTypeCategory tcategory, Oid outfuncoid)
1077+
static JsonbValue *
1078+
to_jsonb_worker_internal(Datum val, JsonbTypeCategory tcategory, Oid outfuncoid)
10791079
{
10801080
JsonbInState result;
10811081

10821082
memset(&result, 0, sizeof(JsonbInState));
10831083

10841084
datum_to_jsonb(val, false, &result, tcategory, outfuncoid, false);
10851085

1086-
return JsonbPGetDatum(JsonbValueToJsonb(result.res));
1086+
return result.res;
1087+
}
1088+
1089+
Datum
1090+
to_jsonb_worker(Datum val, JsonbTypeCategory tcategory, Oid outfuncoid)
1091+
{
1092+
return JsonValueToJsonbDatum(to_jsonb_worker_internal(val, tcategory, outfuncoid));
10871093
}
10881094

10891095
bool
@@ -1138,12 +1144,13 @@ to_jsonb(PG_FUNCTION_ARGS)
11381144
jsonb_categorize_type(val_type,
11391145
&tcategory, &outfuncoid);
11401146

1141-
PG_RETURN_DATUM(to_jsonb_worker(val, tcategory, outfuncoid));
1147+
PG_RETURN_JSONB_VALUE_P(to_jsonb_worker_internal(val, tcategory, outfuncoid));
11421148
}
11431149

1144-
Datum
1145-
jsonb_build_object_worker(int nargs, Datum *args, bool *nulls, Oid *types,
1146-
bool absent_on_null, bool unique_keys)
1150+
static JsonbValue *
1151+
jsonb_build_object_worker_internal(int nargs, Datum *args,
1152+
bool *nulls, Oid *types,
1153+
bool absent_on_null, bool unique_keys)
11471154
{
11481155
int i;
11491156
JsonbInState result;
@@ -1186,9 +1193,19 @@ jsonb_build_object_worker(int nargs, Datum *args, bool *nulls, Oid *types,
11861193
add_jsonb(args[i + 1], nulls[i + 1], &result, types[i + 1], false);
11871194
}
11881195

1189-
result.res = pushJsonbValue(&result.parseState, WJB_END_OBJECT, NULL);
1196+
return pushJsonbValue(&result.parseState, WJB_END_OBJECT, NULL);
1197+
}
1198+
1199+
Datum
1200+
jsonb_build_object_worker(int nargs, Datum *args, bool *nulls, Oid *types,
1201+
bool absent_on_null, bool unique_keys)
1202+
{
1203+
JsonbValue *res = jsonb_build_object_worker_internal(nargs, args,
1204+
nulls, types,
1205+
absent_on_null,
1206+
unique_keys);
11901207

1191-
return JsonbPGetDatum(JsonbValueToJsonb(result.res));
1208+
return JsonValueToJsonbDatum(res);
11921209
}
11931210

11941211
/*
@@ -1208,7 +1225,8 @@ jsonb_build_object(PG_FUNCTION_ARGS)
12081225
if (nargs < 0)
12091226
PG_RETURN_NULL();
12101227

1211-
PG_RETURN_DATUM(jsonb_build_object_worker(nargs, args, nulls, types, false, false));
1228+
PG_RETURN_JSONB_VALUE_P(
1229+
jsonb_build_object_worker_internal(nargs, args, nulls, types, false, false));
12121230
}
12131231

12141232
/*
@@ -1224,12 +1242,13 @@ jsonb_build_object_noargs(PG_FUNCTION_ARGS)
12241242
(void) pushJsonbValue(&result.parseState, WJB_BEGIN_OBJECT, NULL);
12251243
result.res = pushJsonbValue(&result.parseState, WJB_END_OBJECT, NULL);
12261244

1227-
PG_RETURN_JSONB_P(JsonbValueToJsonb(result.res));
1245+
PG_RETURN_JSONB_VALUE_P(result.res);
12281246
}
12291247

1230-
Datum
1231-
jsonb_build_array_worker(int nargs, Datum *args, bool *nulls, Oid *types,
1232-
bool absent_on_null)
1248+
static JsonbValue *
1249+
jsonb_build_array_worker_internal(int nargs, Datum *args,
1250+
bool *nulls, Oid *types,
1251+
bool absent_on_null)
12331252
{
12341253
int i;
12351254
JsonbInState result;
@@ -1246,9 +1265,18 @@ jsonb_build_array_worker(int nargs, Datum *args, bool *nulls, Oid *types,
12461265
add_jsonb(args[i], nulls[i], &result, types[i], false);
12471266
}
12481267

1249-
result.res = pushJsonbValue(&result.parseState, WJB_END_ARRAY, NULL);
1268+
return pushJsonbValue(&result.parseState, WJB_END_ARRAY, NULL);
1269+
}
12501270

1251-
return JsonbPGetDatum(JsonbValueToJsonb(result.res));
1271+
Datum
1272+
jsonb_build_array_worker(int nargs, Datum *args, bool *nulls, Oid *types,
1273+
bool absent_on_null)
1274+
{
1275+
JsonbValue *res = jsonb_build_array_worker_internal(nargs, args,
1276+
nulls, types,
1277+
absent_on_null);
1278+
1279+
return JsonValueToJsonbDatum(res);
12521280
}
12531281

12541282
/*
@@ -1268,10 +1296,10 @@ jsonb_build_array(PG_FUNCTION_ARGS)
12681296
if (nargs < 0)
12691297
PG_RETURN_NULL();
12701298

1271-
PG_RETURN_DATUM(jsonb_build_array_worker(nargs, args, nulls, types, false));
1299+
PG_RETURN_JSONB_VALUE_P(
1300+
jsonb_build_array_worker_internal(nargs, args, nulls, types, false));
12721301
}
12731302

1274-
12751303
/*
12761304
* degenerate case of jsonb_build_array where it gets 0 arguments.
12771305
*/
@@ -1285,7 +1313,7 @@ jsonb_build_array_noargs(PG_FUNCTION_ARGS)
12851313
(void) pushJsonbValue(&result.parseState, WJB_BEGIN_ARRAY, NULL);
12861314
result.res = pushJsonbValue(&result.parseState, WJB_END_ARRAY, NULL);
12871315

1288-
PG_RETURN_JSONB_P(JsonbValueToJsonb(result.res));
1316+
PG_RETURN_JSONB_VALUE_P(result.res);
12891317
}
12901318

12911319

@@ -1387,7 +1415,7 @@ jsonb_object(PG_FUNCTION_ARGS)
13871415
close_object:
13881416
result.res = pushJsonbValue(&result.parseState, WJB_END_OBJECT, NULL);
13891417

1390-
PG_RETURN_JSONB_P(JsonbValueToJsonb(result.res));
1418+
PG_RETURN_JSONB_VALUE_P(result.res);
13911419
}
13921420

13931421
/*
@@ -1479,7 +1507,7 @@ jsonb_object_two_arg(PG_FUNCTION_ARGS)
14791507
close_object:
14801508
result.res = pushJsonbValue(&result.parseState, WJB_END_OBJECT, NULL);
14811509

1482-
PG_RETURN_JSONB_P(JsonbValueToJsonb(result.res));
1510+
PG_RETURN_JSONB_VALUE_P(result.res);
14831511
}
14841512

14851513
static Datum
@@ -1626,7 +1654,6 @@ jsonb_agg_finalfn(PG_FUNCTION_ARGS)
16261654
{
16271655
JsonbAggState *arg;
16281656
JsonbInState result;
1629-
Jsonb *out;
16301657

16311658
/* cannot be called directly because of internal-type argument */
16321659
Assert(AggCheckCallContext(fcinfo, NULL));
@@ -1648,9 +1675,7 @@ jsonb_agg_finalfn(PG_FUNCTION_ARGS)
16481675
result.res = pushJsonbValue(&result.parseState,
16491676
WJB_END_ARRAY, NULL);
16501677

1651-
out = JsonbValueToJsonb(result.res);
1652-
1653-
PG_RETURN_JSONB_P(out);
1678+
PG_RETURN_JSONB_VALUE_P(result.res);
16541679
}
16551680

16561681
static Datum
@@ -1916,7 +1941,6 @@ jsonb_object_agg_finalfn(PG_FUNCTION_ARGS)
19161941
{
19171942
JsonbAggState *arg;
19181943
JsonbInState result;
1919-
Jsonb *out;
19201944

19211945
/* cannot be called directly because of internal-type argument */
19221946
Assert(AggCheckCallContext(fcinfo, NULL));
@@ -1939,9 +1963,7 @@ jsonb_object_agg_finalfn(PG_FUNCTION_ARGS)
19391963
result.res = pushJsonbValue(&result.parseState,
19401964
WJB_END_OBJECT, NULL);
19411965

1942-
out = JsonbValueToJsonb(result.res);
1943-
1944-
PG_RETURN_JSONB_P(out);
1966+
PG_RETURN_JSONB_VALUE_P(result.res);
19451967
}
19461968

19471969

src/backend/utils/adt/jsonfuncs.c

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ jsonb_object_field(PG_FUNCTION_ARGS)
832832
&vbuf);
833833

834834
if (v != NULL)
835-
PG_RETURN_JSONB_P(JsonbValueToJsonb(v));
835+
PG_RETURN_JSONB_VALUE_P(v);
836836

837837
PG_RETURN_NULL();
838838
}
@@ -913,7 +913,7 @@ jsonb_array_element(PG_FUNCTION_ARGS)
913913

914914
v = getIthJsonbValueFromContainer(JsonbRoot(jb), element);
915915
if (v != NULL)
916-
PG_RETURN_JSONB_P(JsonbValueToJsonb(v));
916+
PG_RETURN_JSONB_VALUE_P(v);
917917

918918
PG_RETURN_NULL();
919919
}
@@ -1607,10 +1607,8 @@ jsonb_get_element(Jsonb *jb, Datum *path, int npath, bool *isnull, bool as_text)
16071607
}
16081608
else
16091609
{
1610-
Jsonb *res = JsonbValueToJsonb(jbvp);
1611-
16121610
/* not text mode - just hand back the jsonb */
1613-
PG_RETURN_JSONB_P(res);
1611+
PG_RETURN_JSONB_VALUE_P(jbvp);
16141612
}
16151613
}
16161614

@@ -1634,7 +1632,7 @@ jsonb_set_element(Jsonb *jb, Datum *path, int path_len,
16341632

16351633
pfree(path_nulls);
16361634

1637-
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
1635+
PG_RETURN_JSONB_VALUE_P(res);
16381636
}
16391637

16401638
static void
@@ -1970,9 +1968,7 @@ each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
19701968
else
19711969
{
19721970
/* Not in text mode, just return the Jsonb */
1973-
Jsonb *val = JsonbValueToJsonb(&v);
1974-
1975-
values[1] = JsonbPGetDatum(val);
1971+
values[1] = JsonValueToJsonbDatum(&v);
19761972
}
19771973

19781974
tuplestore_putvalues(rsi->setResult, rsi->setDesc, values, nulls);
@@ -2202,9 +2198,7 @@ elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
22022198
else
22032199
{
22042200
/* Not in text mode, just return the Jsonb */
2205-
Jsonb *val = JsonbValueToJsonb(&v);
2206-
2207-
values[0] = JsonbPGetDatum(val);
2201+
values[0] = JsonValueToJsonbDatum(&v);
22082202
}
22092203

22102204
tuplestore_putvalues(rsi->setResult, rsi->setDesc, values, nulls);
@@ -2917,11 +2911,7 @@ populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv)
29172911
JsonbValue *jbv = jsv->val.jsonb;
29182912

29192913
if (typid == JSONBOID)
2920-
{
2921-
Jsonb *jsonb = JsonbValueToJsonb(jbv); /* directly use jsonb */
2922-
2923-
return JsonbPGetDatum(jsonb);
2924-
}
2914+
return JsonValueToJsonbDatum(jbv); /* directly use jsonb */
29252915
/* convert jsonb to string for typio call */
29262916
else if (typid == JSONOID && jbv->type != jbvBinary)
29272917
{
@@ -4223,7 +4213,7 @@ jsonb_strip_nulls(PG_FUNCTION_ARGS)
42234213

42244214
Assert(res != NULL);
42254215

4226-
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
4216+
PG_RETURN_JSONB_VALUE_P(res);
42274217
}
42284218

42294219
/*
@@ -4278,7 +4268,7 @@ jsonb_concat(PG_FUNCTION_ARGS)
42784268

42794269
Assert(res != NULL);
42804270

4281-
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
4271+
PG_RETURN_JSONB_VALUE_P(res);
42824272
}
42834273

42844274

@@ -4332,7 +4322,7 @@ jsonb_delete(PG_FUNCTION_ARGS)
43324322

43334323
Assert(res != NULL);
43344324

4335-
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
4325+
PG_RETURN_JSONB_VALUE_P(res);
43364326
}
43374327

43384328
/*
@@ -4417,7 +4407,7 @@ jsonb_delete_array(PG_FUNCTION_ARGS)
44174407

44184408
Assert(res != NULL);
44194409

4420-
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
4410+
PG_RETURN_JSONB_VALUE_P(res);
44214411
}
44224412

44234413
/*
@@ -4485,7 +4475,7 @@ jsonb_delete_idx(PG_FUNCTION_ARGS)
44854475

44864476
Assert(res != NULL);
44874477

4488-
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
4478+
PG_RETURN_JSONB_VALUE_P(res);
44894479
}
44904480

44914481
/*
@@ -4533,7 +4523,7 @@ jsonb_set(PG_FUNCTION_ARGS)
45334523

45344524
Assert(res != NULL);
45354525

4536-
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
4526+
PG_RETURN_JSONB_VALUE_P(res);
45374527
}
45384528

45394529

@@ -4644,7 +4634,7 @@ jsonb_delete_path(PG_FUNCTION_ARGS)
46444634

46454635
Assert(res != NULL);
46464636

4647-
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
4637+
PG_RETURN_JSONB_VALUE_P(res);
46484638
}
46494639

46504640
/*
@@ -4689,7 +4679,7 @@ jsonb_insert(PG_FUNCTION_ARGS)
46894679

46904680
Assert(res != NULL);
46914681

4692-
PG_RETURN_JSONB_P(JsonbValueToJsonb(res));
4682+
PG_RETURN_JSONB_VALUE_P(res);
46934683
}
46944684

46954685
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy