Skip to content

Commit d4ebd63

Browse files
author
Nikita Glukhov
committed
Use json cache in jsonb_contains(), jsonb_object_field(), jsonb_object_field_text()
1 parent 391b30a commit d4ebd63

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/backend/utils/adt/jsonb_op.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,19 @@ jsonb_contains(PG_FUNCTION_ARGS)
115115
{
116116
Jsonb *val = PG_GETARG_JSONB(0);
117117
Jsonb *tmpl = PG_GETARG_JSONB(1);
118+
JsonCacheContext oldcxt;
119+
bool res;
118120

119121
if (JB_ROOT_IS_OBJECT(val) != JB_ROOT_IS_OBJECT(tmpl))
120122
PG_RETURN_BOOL(false);
121123

122-
PG_RETURN_BOOL(JsonbDeepContains(JsonRoot(val), JsonRoot(tmpl)));
124+
oldcxt = JsonCacheSwitchToFunc(fcinfo);
125+
126+
res = JsonbDeepContains(JsonRoot(val), JsonRoot(tmpl));
127+
128+
JsonCacheSwitchTo(oldcxt);
129+
130+
PG_RETURN_BOOL(res);
123131
}
124132

125133
Datum

src/backend/utils/adt/jsonfuncs.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,14 +764,19 @@ jsonb_object_field(PG_FUNCTION_ARGS)
764764
Jsonb *jb = PG_GETARG_JSONB(0);
765765
text *key = PG_GETARG_TEXT_PP(1);
766766
JsonbValue *v;
767+
JsonCacheContext oldcxt;
767768

768769
if (!JB_ROOT_IS_OBJECT(jb))
769770
PG_RETURN_NULL();
770771

772+
oldcxt = JsonCacheSwitchToFunc(fcinfo);
773+
771774
v = findJsonbValueFromContainerLen(&jb->root, JB_FOBJECT,
772775
VARDATA_ANY(key),
773776
VARSIZE_ANY_EXHDR(key));
774777

778+
JsonCacheSwitchTo(oldcxt);
779+
775780
if (v != NULL)
776781
PG_RETURN_JSONB(JsonbValueToJsonb(v));
777782

@@ -803,14 +808,19 @@ jsonb_object_field_text(PG_FUNCTION_ARGS)
803808
text *key = PG_GETARG_TEXT_PP(1);
804809
JsonbValue *v;
805810
JsonbValue vbuf;
811+
JsonCacheContext oldcxt;
806812

807813
if (!JB_ROOT_IS_OBJECT(jb))
808814
PG_RETURN_NULL();
809815

816+
oldcxt = JsonCacheSwitchToFunc(fcinfo);
817+
810818
v = findJsonbValueFromContainerLen(&jb->root, JB_FOBJECT,
811819
VARDATA_ANY(key),
812820
VARSIZE_ANY_EXHDR(key));
813821

822+
JsonCacheSwitchTo(oldcxt);
823+
814824
if (v != NULL)
815825
{
816826
text *result = NULL;
@@ -1444,13 +1454,23 @@ get_scalar(void *state, char *token, JsonTokenType tokentype)
14441454
Datum
14451455
jsonb_extract_path(PG_FUNCTION_ARGS)
14461456
{
1447-
return get_jsonb_path_all(fcinfo, false);
1457+
JsonCacheContext oldcxt = JsonCacheSwitchToFunc(fcinfo);
1458+
Datum result = get_jsonb_path_all(fcinfo, false);
1459+
1460+
JsonCacheSwitchTo(oldcxt);
1461+
1462+
PG_RETURN_DATUM(result);
14481463
}
14491464

14501465
Datum
14511466
jsonb_extract_path_text(PG_FUNCTION_ARGS)
14521467
{
1453-
return get_jsonb_path_all(fcinfo, true);
1468+
JsonCacheContext oldcxt = JsonCacheSwitchToFunc(fcinfo);
1469+
Datum result = get_jsonb_path_all(fcinfo, true);
1470+
1471+
JsonCacheSwitchTo(oldcxt);
1472+
1473+
PG_RETURN_DATUM(result);
14541474
}
14551475

14561476
static Datum

0 commit comments

Comments
 (0)