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.96) (envelope-from ) id 1x3aKe-006WcD-0N for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Sep 2026 14:28:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x3aKd-001zVe-0N for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Sep 2026 14:28:23 +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.96) (envelope-from ) id 1x3aKc-001zVW-2c for pgsql-hackers@lists.postgresql.org; Mon, 07 Sep 2026 14:28:22 +0000 Received: from mail-lf1-x133.google.com ([2a00:1450:4864:20::133]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x3aKa-00000003Q5M-2LLk for pgsql-hackers@lists.postgresql.org; Mon, 07 Sep 2026 14:28:22 +0000 Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-5b4a9994c20so3577412e87.0 for ; Mon, 07 Sep 2026 07:28:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788791299; x=1789396099; darn=lists.postgresql.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=fQmKNt+jhAR21bxyBzAm/ZhXecVcXPICOfYh16PG6i0=; b=JOQI9fLI3N5nM5SRBk4QmHbeA2alhxItmClGVdJ1nVRSNu2TTWjdD1FdUExR875fho 4nxnys4c3zCvW5rkNYzU3eA3fnBeH/n5OWr5RHqP+hY7OWrnAPPkKTvBsGmZe5EN1VUN 8IXMO8aBKqEctH8Hb2KOYCaZ2QgjKirZ+A7g9d7rDlRHX70phXbpNrA9GdOYKHCXj27Q n7VmsgwPn+S/qWSOMBJiXVQLY0i7E2dnoJlDkOZmITfmkAxVxnAiAxcL8leM+9Y0DPJO lp8MGE1HRpeMFdAjfvlUp2CzN2W0BKbcGHw2mAx5efw7QAxj2a9bD0HfN3nNqQlJhSE9 7bVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788791299; x=1789396099; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=fQmKNt+jhAR21bxyBzAm/ZhXecVcXPICOfYh16PG6i0=; b=MtC2IJ7UpY0I+aolzI77xYrsi62dDulYIqmgxd7jUlmXZRgZtZvYE4s38ccUWaUiCu wKvKB3pcG8FS9RPWqlv+IO/luCELDTFFq3Jw+qiABos4K3mTuRsPOICgB4UEQipzv9gx 94+g7zog/sFTsVJ3ixN6t4QUQy3ivldjsff21HXoTnSENTtbBI0TCQD1euPRlmtHeRe9 Xk0OXrZWgfRhgI6j1jMQqdgKKMAsVBY3YZJUSoBHqyQDgm1kc09B42StoDT8yiZwdTg8 h7Ig2qcMpn//nUjkdqzQ/GFjq6pfE5sjYRyAusbLLx/J8pXRwhWQR3iYFku/wKUbYAQY 1T9w== X-Gm-Message-State: AFuF++lHd6M4xyPuXV4F/X7hs0TKZSR+nSQ3RJ/Ngn18ac4IRpQ4BAH6 asaKL+3H00ZbCHWyAvRc62BciLRjMVnLMT08WLRTP2YPfsiRcd6f7QjZ X-Gm-Gg: AYBFou3tElVP78d69kahnSFpi2IHR8NJnc0W5G8RTmxik5KK7TioN/o2QYALW+hm9vC A5HJmdZrDQptSsfNv3Gb2scffUrumYmTuaHLLeUrYxnpiTVMblSvfjWnZ31lVFinvcB2J7yQbr5 V9HgQaVYWpin/PATs8D7jzUEcjkwnklIDocPnDEBV+fdZQZ3DLlKWMe0kVpPLFCHdU499UeWQHB an61po3/0MR5n0ZRv9FGlRkEzAzHlJsLx0LkCeOThzZucgCmZiNZUycWFE7HM6W3eq9BspgKS3q XPBqsX0NEmZr+HTbB3LCcC+1p7seBVgxVmexTUt1BKOUPT43mFeDiP+CV1zGhCCTQ9MBC7B0APP k6lkr9u0TXs3qPtQp3TmAOI4ZVKeFfvuhI8CN7OLjM7q0OmXadhgaOHgO8bFh4RTO5R1axrdnGD zc2yYzsBufH2rEQyv+Hh/a97IKgv5SRGHq3Fc0ZLc2AcR9OfFGbyEjo8aoeapqsTAvhBHBIdSjv OU= X-Received: by 2002:a05:6512:3d0b:b0:5b0:1b0a:2370 with SMTP id 2adb3069b0e04-5b616edeb86mr3546678e87.2.1788791298775; Mon, 07 Sep 2026 07:28:18 -0700 (PDT) Received: from [192.168.15.240] ([80.251.191.198]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5b6166f694bsm2369703e87.31.2026.09.07.07.28.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 07 Sep 2026 07:28:17 -0700 (PDT) Message-ID: <2c61c2c2-6bf3-4155-b569-713ddbb5d22c@gmail.com> Date: Mon, 7 Sep 2026 16:28:16 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Make the transition state of avg(int2)/avg(int4)/sum(int2)/sum(int4) internal To: Tom Lane , Andres Freund Cc: PostgreSQL Hackers , Chao Li , Michael Paquier , David Rowley References: <762e811a-6584-4b03-a6ca-52600adcd6e9@gmail.com> <1571559.1788537022@sss.pgh.pa.us> Content-Language: en-US From: Andrei Lepikhov In-Reply-To: <1571559.1788537022@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 04/09/2026 17:50, Tom Lane wrote: > Andres Freund writes: >> Seems like if we do this - and I think we should - we should go broader than >> just doing this for int8[]. So yea, let's do it for float8 too. >> I think it might make sense to have an opr_sanity.sql check that verifies that >> we don't add new builtin aggregates that have an array transition state. Or >> perhaps even more strictly, test that aggregates either have an internal >> transition state, or the argument type's (for stuff like min/max). > > On reflection, I'm not sure that such a policy is a win. If you go > with an internal-type transition state, then (if you want parallel > aggregation support) you need serialize/deserialize functions, and > I think also some other stuff that comes for free if the transition > state is a real SQL type. So this is not so much a clear win as > a tradeoff of which code you want to write. > As I see it, with an internal state, some aggregates gain, while others don't. Plain sum(int2/int4/float8) already has a trivial 8-byte transition state: minimal memory, passed by value, no detoast. The moving-aggregate mode of sum(int2/int4), on the other hand, reuses avg()'s int2/int4_avg_accum_inv functions, which pay for array overhead and a detoast check on every row. Internal state might reduce memory consumption there. float8 has no moving-aggregate mode. Also note that the regression tests currently call float8_regr_combine() directly, with literal array values. Not sure how much it matters, but introducing an internal state also means giving up initcond. Hence, sum(float8) with an internal state would lose its STRICT marking, pushing NULL-checking into the function body. Quick tests show sum() losing 1-3% in serial mode and a more significant ~7% in a parallel GROUP BY, due to serialize/deserialize across workers. This was run on my laptop, not a dedicated benchmarking box, but the overhead looks clearly detectable. Overall, in my opinion, only AVG() and other statistical aggregates, such as STDDEV(), benefit from moving to internal state. Basic aggregates like sum() should probably stay as they are, if we want to avoid a performance regression. -- regards, Andrei Lepikhov, pgEdge