Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dbb9aed

Browse filesBrowse files
committed
Optimize get_jsonb_path_all avoiding an iterator
Instead of creating an iterator object at each step down the JSONB object/array, we can just just examine its object/array flags, which is faster. Also, use the recently introduced JsonbValueAsText instead of open-coding the same thing, for code simplicity. Author: Nikita Glukhov Discussion: https://postgr.es/m/7c417f90-f95f-247e-ba63-d95e39c0ad14@postgrespro.ru
1 parent abb014a commit dbb9aed
Copy full SHA for dbb9aed

File tree

Expand file treeCollapse file tree

1 file changed

+10
-23
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+10
-23
lines changed

‎src/backend/utils/adt/jsonfuncs.c

Copy file name to clipboardExpand all lines: src/backend/utils/adt/jsonfuncs.c
+10-23Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,15 +1329,13 @@ get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text)
13291329
{
13301330
Jsonb *jb = PG_GETARG_JSONB_P(0);
13311331
ArrayType *path = PG_GETARG_ARRAYTYPE_P(1);
1332-
Jsonb *res;
13331332
Datum *pathtext;
13341333
bool *pathnulls;
13351334
int npath;
13361335
int i;
13371336
bool have_object = false,
13381337
have_array = false;
13391338
JsonbValue *jbvp = NULL;
1340-
JsonbValue tv;
13411339
JsonbContainer *container;
13421340

13431341
/*
@@ -1449,41 +1447,30 @@ get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text)
14491447

14501448
if (jbvp->type == jbvBinary)
14511449
{
1452-
JsonbIterator *it = JsonbIteratorInit((JsonbContainer *) jbvp->val.binary.data);
1453-
JsonbIteratorToken r;
1454-
1455-
r = JsonbIteratorNext(&it, &tv, true);
1456-
container = (JsonbContainer *) jbvp->val.binary.data;
1457-
have_object = r == WJB_BEGIN_OBJECT;
1458-
have_array = r == WJB_BEGIN_ARRAY;
1450+
container = jbvp->val.binary.data;
1451+
have_object = JsonContainerIsObject(container);
1452+
have_array = JsonContainerIsArray(container);
1453+
Assert(!JsonContainerIsScalar(container));
14591454
}
14601455
else
14611456
{
1462-
have_object = jbvp->type == jbvObject;
1463-
have_array = jbvp->type == jbvArray;
1457+
Assert(IsAJsonbScalar(jbvp));
1458+
have_object = false;
1459+
have_array = false;
14641460
}
14651461
}
14661462

14671463
if (as_text)
14681464
{
1469-
/* special-case outputs for string and null values */
1470-
if (jbvp->type == jbvString)
1471-
PG_RETURN_TEXT_P(cstring_to_text_with_len(jbvp->val.string.val,
1472-
jbvp->val.string.len));
14731465
if (jbvp->type == jbvNull)
14741466
PG_RETURN_NULL();
1475-
}
1476-
1477-
res = JsonbValueToJsonb(jbvp);
14781467

1479-
if (as_text)
1480-
{
1481-
PG_RETURN_TEXT_P(cstring_to_text(JsonbToCString(NULL,
1482-
&res->root,
1483-
VARSIZE(res))));
1468+
PG_RETURN_TEXT_P(JsonbValueAsText(jbvp));
14841469
}
14851470
else
14861471
{
1472+
Jsonb *res = JsonbValueToJsonb(jbvp);
1473+
14871474
/* not text mode - just hand back the jsonb */
14881475
PG_RETURN_JSONB_P(res);
14891476
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.