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