Re: Making jsonb_agg() faster
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: jian he <jian.universality@gmail.com>
Cc: pgsql-hackers@lists.postgresql.org
Date: 2025-08-15T22:40:29Z
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
jian he <jian.universality@gmail.com> writes: > about 0002: > jsonb_agg_finalfn > /* > * The final function can be called more than once, so we must not change > * the stored JsonbValue data structure. Fortunately, the WJB_END_ARRAY > * action will only change fields in the JsonbInState struct itself, so we > * can simply invoke pushJsonbValue on a local copy of that. > */ > I don't understand the above comments. > If I add another ``pushJsonbValue(&result, WJB_END_ARRAY, NULL);`` > then it will cause segmentation fault, that means we can not call > WJB_END_ARRAY action twice. No, but so what? We're not doing that twice here. > in finalize_aggregate: > there is no foreach loop within > ```if (OidIsValid(peragg->finalfn_oid))``` > Overall, I can not come up with a case where the final function is > called more than once. If memory serves, the issue arises when the aggregate is used as a window function. If the frame definition allows, the operation will proceed like "accumulate a row into the transition state, call the finalfn to get the aggregate's result at this row, accumulate another row into the transition state, call the finalfn to get the aggregate's result at that row, accumulate another row, yadda yadda". So in this example, the point is not about calling the finalfn more than once per row, but that it mustn't damage the transition state in case we choose to accumulate more data. There are also cases where multiple finalfns can be called on the same transition state, although that requires aggregates that share a transfn, and I don't think jsonb_agg shares its transfn with anything else. See the discussion of FINALFUNC_MODIFY in the CREATE AGGREGATE man page for more details. regards, tom lane