Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e2gRz-000774-Un for pgsql-bugs@arkaria.postgresql.org; Thu, 12 Oct 2017 16:35:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e2gRz-0002tK-Eq for pgsql-bugs@arkaria.postgresql.org; Thu, 12 Oct 2017 16:35:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e2gRz-0002sw-0Q for pgsql-bugs@postgresql.org; Thu, 12 Oct 2017 16:35:11 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e2gRv-0000Ow-SO for pgsql-bugs@postgresql.org; Thu, 12 Oct 2017 16:35:10 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v9CGYq31029150; Thu, 12 Oct 2017 12:34:52 -0400 From: Tom Lane To: Heikki Linnakangas cc: David Rowley , Lukas Eder , PostgreSQL mailing lists Subject: Re: Combination of ordered-set aggregate function terminates JDBC connection on PostgreSQL 9.6.5 In-reply-to: <748.1507820150@sss.pgh.pa.us> References: <23270.1507751492@sss.pgh.pa.us> <3802.1507775225@sss.pgh.pa.us> <748.1507820150@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Thu, 12 Oct 2017 10:55:50 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <28808.1507826022.0@sss.pgh.pa.us> Date: Thu, 12 Oct 2017 12:34:52 -0400 Message-ID: <29149.1507826092@sss.pgh.pa.us> List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <28808.1507826022.1@sss.pgh.pa.us> I wrote: > Thinking about it more clearly, if a transition function is being run > on behalf of several different Aggrefs (with identical input states), > then no, the transition function should not care which Aggref it > looks at. As you say it mustn't do anything different on the basis of > the finalfn-related fields. The problem occurs when a finalfn calls > AggGetAggref --- then, I think that the finalfn is entirely entitled > to expect that it will see its own Aggref, not some other one that > happens to share input+transition. Concretely, I think we need to do the attached. This seems like a bug fix to me, so I'm inclined to back-patch it. In the back branches we could put the extra AggState field at the end, to minimize ABI-break hazards. BTW ... I was quite surprised to notice that the aggdirectargs are treated as a property that has to be matched in order to combine transition states. They aren't used during the transition phase, so this seems like a pointless constraint. We could move the aggdirectargs ExprState list to AggStatePerAggData and treat the aggdirectargs as part of the finalfn-related data, instead. As long as we're not merging states at all for OSAs, this is moot, but it seems like something to fix along with that. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="make-AggGetAggref-honest.patch"; charset="us-ascii" Content-ID: <28808.1507826022.2@sss.pgh.pa.us> Content-Description: make-AggGetAggref-honest.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c index 1a1aebe..c804a61 100644 *** a/src/backend/executor/nodeAgg.c --- b/src/backend/executor/nodeAgg.c *************** finalize_aggregate(AggState *aggstate, *** 1528,1535 **** { int numFinalArgs =3D peragg->numFinalArgs; =20=20 ! /* set up aggstate->curpertrans for AggGetAggref() */ ! aggstate->curpertrans =3D pertrans; =20=20 InitFunctionCallInfoData(fcinfo, &peragg->finalfn, numFinalArgs, --- 1528,1535 ---- { int numFinalArgs =3D peragg->numFinalArgs; =20=20 ! /* set up aggstate->curperagg for AggGetAggref() */ ! aggstate->curperagg =3D peragg; =20=20 InitFunctionCallInfoData(fcinfo, &peragg->finalfn, numFinalArgs, *************** finalize_aggregate(AggState *aggstate, *** 1562,1568 **** *resultVal =3D FunctionCallInvoke(&fcinfo); *resultIsNull =3D fcinfo.isnull; } ! aggstate->curpertrans =3D NULL; } else { --- 1562,1568 ---- *resultVal =3D FunctionCallInvoke(&fcinfo); *resultIsNull =3D fcinfo.isnull; } ! aggstate->curperagg =3D NULL; } else { *************** ExecInitAgg(Agg *node, EState *estate, i *** 2712,2717 **** --- 2712,2718 ---- aggstate->current_set =3D 0; aggstate->peragg =3D NULL; aggstate->pertrans =3D NULL; + aggstate->curperagg =3D NULL; aggstate->curpertrans =3D NULL; aggstate->input_done =3D false; aggstate->agg_done =3D false; *************** AggCheckCallContext(FunctionCallInfo fci *** 4070,4075 **** --- 4071,4083 ---- * If the function is being called as an aggregate support function, * return the Aggref node for the aggregate call. Otherwise, return NULL. * + * Aggregates sharing the same inputs and transition functions can get + * merged into a single transition calculation. If the transition functi= on + * calls AggGetAggref, it will get some one of the Aggrefs for which it is + * executing. It must therefore not pay attention to the Aggref fields t= hat + * relate to the final function, as those are indeterminate. But if a fi= nal + * function calls AggGetAggref, it will get a precise result. + * * Note that if an aggregate is being used as a window function, this will * return NULL. We could provide a similar function to return the releva= nt * WindowFunc node in such cases, but it's not needed yet. *************** AggGetAggref(FunctionCallInfo fcinfo) *** 4079,4086 **** --- 4087,4102 ---- { if (fcinfo->context && IsA(fcinfo->context, AggState)) { + AggStatePerAgg curperagg; AggStatePerTrans curpertrans; =20=20 + /* check curperagg (valid when in a final function) */ + curperagg =3D ((AggState *) fcinfo->context)->curperagg; +=20 + if (curperagg) + return curperagg->aggref; +=20 + /* check curpertrans (valid when in a transition function) */ curpertrans =3D ((AggState *) fcinfo->context)->curpertrans; =20=20 if (curpertrans) diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index c461134..d4ce8d8 100644 *** a/src/include/nodes/execnodes.h --- b/src/include/nodes/execnodes.h *************** typedef struct AggState *** 1808,1814 **** ExprContext **aggcontexts; /* econtexts for long-lived data (per GS) */ ExprContext *tmpcontext; /* econtext for input expressions */ ExprContext *curaggcontext; /* currently active aggcontext */ ! AggStatePerTrans curpertrans; /* currently active trans state */ bool input_done; /* indicates end of input */ bool agg_done; /* indicates completion of Agg scan */ int projected_set; /* The last projected grouping set */ --- 1808,1815 ---- ExprContext **aggcontexts; /* econtexts for long-lived data (per GS) */ ExprContext *tmpcontext; /* econtext for input expressions */ ExprContext *curaggcontext; /* currently active aggcontext */ ! AggStatePerAgg curperagg; /* currently active aggregate, if any */ ! AggStatePerTrans curpertrans; /* currently active trans state, if any */ bool input_done; /* indicates end of input */ bool agg_done; /* indicates completion of Agg scan */ int projected_set; /* The last projected grouping set */ ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--