Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1un36E-0023i6-P0 for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Aug 2025 22:40:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1un36D-002oGM-9I for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Aug 2025 22:40:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1un36D-002oGD-08 for pgsql-hackers@lists.postgresql.org; Fri, 15 Aug 2025 22:40:37 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1un368-0004Xl-0n for pgsql-hackers@lists.postgresql.org; Fri, 15 Aug 2025 22:40:37 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 57FMeT4J654629; Fri, 15 Aug 2025 18:40:30 -0400 From: Tom Lane To: jian he cc: pgsql-hackers@lists.postgresql.org Subject: Re: Making jsonb_agg() faster In-reply-to: References: <1060917.1753202222@sss.pgh.pa.us> <617666.1755277022@sss.pgh.pa.us> Comments: In-reply-to jian he message dated "Sat, 16 Aug 2025 01:21:01 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <654627.1755297629.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 15 Aug 2025 18:40:29 -0400 Message-ID: <654628.1755297629@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk jian he writes: > about 0002: > jsonb_agg_finalfn > /* > * The final function can be called more than once, so we must not c= hange > * the stored JsonbValue data structure. Fortunately, the WJB_END_A= RRAY > * 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