Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mxfnq-0000iJ-GZ for pgsql-general@arkaria.postgresql.org; Thu, 16 Dec 2021 01:43:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mxfnp-00016Q-Cg for pgsql-general@arkaria.postgresql.org; Thu, 16 Dec 2021 01:43:25 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mxfnp-00016H-3I for pgsql-general@lists.postgresql.org; Thu, 16 Dec 2021 01:43:25 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mxfnm-0007Gs-HF for pgsql-general@lists.postgresql.org; Thu, 16 Dec 2021 01:43:24 +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 1BG1hLHN846905; Wed, 15 Dec 2021 20:43:21 -0500 From: Tom Lane To: Matt Magoffin cc: pgsql-general@lists.postgresql.org Subject: Re: Properly handling aggregate in nested function call In-reply-to: References: <0005FD8A-0FBE-4D53-BBDE-7242A4DC525C@msqr.us> <559031.1639522278@sss.pgh.pa.us> Comments: In-reply-to Matt Magoffin message dated "Thu, 16 Dec 2021 11:54:33 +1300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <846903.1639619001.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 15 Dec 2021 20:43:21 -0500 Message-ID: <846904.1639619001@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Matt Magoffin writes: > Any other ideas I could look into? Per the old saw, when you can't see the problem, it usually means you're looking in the wrong place. I looked at the SQL declaration of the function [1], and saw: CREATE OR REPLACE FUNCTION vec_agg_mean_finalfn(internal) RETURNS bigint[] AS 'aggs_for_vecs', 'vec_agg_mean_finalfn' LANGUAGE c; Of course what this function is actually returning is numeric[]. There is some code such as array_out that will look at the element type OID embedded in the array value, and do the right thing. But other code will believe the function's declared result type, and that sort of code will go wrong. I see from your C code that you're hoping to make this stuff somewhat polymorphic. The simplest way to do that, and maybe the best, is to make multiple SQL aliases for the same C function, and then multiple aggregate declarations on top of that. Alternatively you can declare one set of polymorphic functions and aggregates, in which case you'll need to closely study the stuff about the finalfunc_extra option [2]. regards, tom lane [1] https://github.com/SolarNetwork/aggs_for_vecs/blob/9e742cdc32a113268fd= 3c1f928c8ac724acec9f5/aggs_for_vecs--1.3.0.sql#L1342 [2] https://www.postgresql.org/docs/current/xaggr.html#XAGGR-POLYMORPHIC-A= GGREGATES