Re: Making jsonb_agg() faster
Chao Li <li.evan.chao@gmail.com>
From: Chao Li <li.evan.chao@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: jian he <jian.universality@gmail.com>,
pgsql-hackers@lists.postgresql.org
Date: 2025-08-27T01:22:17Z
Lists: pgsql-hackers
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Fix jsonb_object_agg crash after eliminating null-valued pairs.
- ef5f559b95e2 19 (unreleased) landed
-
Micro-optimize datatype conversions in datum_to_jsonb_internal.
- 005a2907dc30 19 (unreleased) landed
-
Remove fundamentally-redundant processing in jsonb_agg() et al.
- b61aa76e4585 19 (unreleased) landed
-
Revise APIs for pushJsonbValue() and associated routines.
- 0986e95161ce 19 (unreleased) landed
-
Add a macro for the declared typlen of type timetz.
- 3628af42107d 19 (unreleased) landed
>> On Aug 23, 2025, at 03:11, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>>
>>
>> v2-0001 takes care of that, and also adopts your suggestion in [1]
>> about not using two calls of pushJsonbValueScalar where one would do.
>> I also did a bit more micro-optimization in appendKey, appendValue,
>> appendElement to avoid redundant copying, because perf testing showed
>> that appendElement is still a hot-spot for jsonb_agg. Patches 0002
>> and 0003 are unchanged.
>>
>
>
A few more suggestions for pushJsonValue():
+ /* If an object or array is pushed, recursively push its contents */
+ if (jbval->type == jbvObject)
{
pushJsonbValue(pstate, WJB_BEGIN_OBJECT, NULL);
for (i = 0; i < jbval->val.object.nPairs; i++)
@@ -581,32 +607,29 @@ pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq,
pushJsonbValue(pstate, WJB_KEY, &jbval->val.object.pairs[i].key);
pushJsonbValue(pstate, WJB_VALUE, &jbval->val.object.pairs[i].value);
}
-
- return pushJsonbValue(pstate, WJB_END_OBJECT, NULL);
+ pushJsonbValue(pstate, WJB_END_OBJECT, NULL);
+ return;
}
To push WJB_BEGIN_OBJECT and WJB_END_OBJECT, we can directly call pushJsonValueScalar(), because once entering pushJsonbValue, they will meet the check of (seq != WJB_ELEM && seq != WJB_VALUE). Directly calling pushJsonValueScalar() will saves one level of recursion.
- if (jbval && (seq == WJB_ELEM || seq == WJB_VALUE) && jbval->type == jbvArray)
+ if (jbval->type == jbvArray)
{
pushJsonbValue(pstate, WJB_BEGIN_ARRAY, NULL);
for (i = 0; i < jbval->val.array.nElems; i++)
{
pushJsonbValue(pstate, WJB_ELEM, &jbval->val.array.elems[i]);
}
-
- return pushJsonbValue(pstate, WJB_END_ARRAY, NULL);
+ pushJsonbValue(pstate, WJB_END_ARRAY, NULL);
+ return;
}
Same thing for pushing WJB_BEGIN_ARRAY and WJB_END_ARRAY.
And for pushJsonbValueScalar():
- (*pstate)->size = 4;
+ ppstate->size = 4; /* initial guess at array size */
Can we do lazy allocation? Initially assume size = 0, only allocate memory when pushing the first element? This way, we won’t allocate memory for empty objects and arrays.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/