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 1sfLT5-0058Tq-6L for pgsql-general@arkaria.postgresql.org; Sat, 17 Aug 2024 15:35:51 +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 1sfLT2-004KB3-BV for pgsql-general@arkaria.postgresql.org; Sat, 17 Aug 2024 15:35:49 +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 1sfLT1-004KAv-W9 for pgsql-general@lists.postgresql.org; Sat, 17 Aug 2024 15:35:48 +0000 Received: from mout.gmx.net ([212.227.15.18]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sfLSv-0009NV-5V for pgsql-general@lists.postgresql.org; Sat, 17 Aug 2024 15:35:47 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net; s=s31663417; t=1723908940; x=1724513740; i=jimis@gmx.net; bh=Mz7U3IoxuLFdFw9v7WxdtdXMVNorF6fXjezItYji6Rg=; h=X-UI-Sender-Class:Date:From:To:cc:Subject:In-Reply-To:Message-ID: References:MIME-Version:Content-Type:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=J/XGxQeix5AYyJQ96vqpy3xfG/DuuBMUOnUvQCQuWeNrBDmOxN6vFnJ1XN8yCb5X axd+blnT+t7WN5IZclwRnkcok4t7YpBDctgowfzquO/aLEdk3RSNYfa0NgLg0Onkg 9rMz2NsVowcCooN0Ax52hBiU7VLVfOoI1pwN8Be/kcrNLVXjmDndfWRTIe+u3UKVT Q5CwqyXO93DfUeM2b5ZCeSTc+v1ar4HBRyF+imnZp4/BqEk93AlRw/paqlAWBEmEw x1MK3/ToCpw9mq3mixp2Md2nte3E9XmRqrBkRfCMOfE8gCJfyWO1nU8C4jw4yTmF5 qRxdM/CBL5vNVeqSFA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [192.168.0.96] ([178.232.0.21]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MEFzr-1spYc51zLZ-00BX0F; Sat, 17 Aug 2024 17:35:40 +0200 Date: Sat, 17 Aug 2024 17:35:39 +0200 (CEST) From: Dimitrios Apostolou To: Tom Lane cc: pgsql-general@lists.postgresql.org Subject: Re: array_agg() does not stop aggregating according to HAVING clause In-Reply-To: <954894.1723907711@sss.pgh.pa.us> Message-ID: <3f255b39-e0a7-e04e-0747-c98b13a4e0f6@gmx.net> References: <215d6efa-bb4b-a76e-6066-7a83ccdb55e3@gmx.net> <954894.1723907711@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed X-Provags-ID: V03:K1:Go5PYGw7uxEPKuN9fZHmYujZI4SRBSc3Wbd/vF1AX+YfK9cB66H RpW+1UbGOtLWfYqTxFLn2aepNOE1rdOQKSuDohqNyFgQLCh3y92onaSQ9BS/MDerEOaUQNE Nyu/tH6SKLQ/lF2wFrO/zBdu0qgmQNbRimAcFsroYs8BymgVIGjUVJZ26neJc2pGrberfIF BMaUbYEoY6gPN5YvUpkVg== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:ndy6K+PVOu0=;CB0a8YxpTsYanIpcTBd8lwswjQP DUHMCES21if6TQ7vg7Pb1V5r6fBvT6kpogs4Vg9GaT5Rw3zMGWeiNj78vPImz9wT7Ynemadbw axGuYlw36DMMn0aKUqIbsB1RiT7eI7qxHH5Ne2nnBZmLsBVW8P5qTtzSJ/qhezSSwgYUzkvCR kPQWOlL8SWwgqI0UrlakjRhOD0cuudpgZz12SI7J2fjFVw6hgChE8G/CE+yKeNUc+3d7KUELj DSfJ12uaQz1cmM2+el8XKplmo5MW55KJyXKsDNKkLJJD5ZhCG7YnkdVgxwveNYmEdqIIrP10g fksx0ENQLUcy+ILTgNJKknhdk29j9wzhwzsyp2OaXVjHkGUlfwXKhp9lCZNl9+3q5MIOn7MFd eZFnCyPxREWzuTIAC1ZZlrMTSi4ZzZf9ER25KYzTMUWBMKGvu6PIzVCE2cwExlTL5HyBsImjb R9w2KHiuHAUaEgSv1PlOq21jUHWQ+lmdKDdnS0AyMFhlCGupWrY6C/RT4d9DZxSeW9Kh1bz3n +LsL02H0WFddNPhM7l80A9OK6u3xfnKz9FI14oooPkvfQSRCLJmkkEZHXwOA05U+4szTvLOSB ZXadEGyfdsJXX2d8zYdqAksMy9ALn8SvbZcWmtH7rbJgxYfMHwL7hAWFkCO4frJxWCH0TmMdD uR3Yfv9mkUuzOO7tMU3ABtnpq0Lqs5Mgvd4B6xtJlBT6hPsIG3/XNAfIYK1ClD17oS6ir9PzA hiaDi0EUFE36/m6922J0VQIAYd7cVpzgP84iEn5Up1gPJMwvFwc6uB2prDwOe7IBCPYTqMinV ayx457h4FK8LibiHhhIzM5LA== Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sat, 17 Aug 2024, Tom Lane wrote: > Well, yes: the two aggregates (array_agg and count) are computed > concurrently in a single Aggregate plan node scanning the output > of the JOIN. There's no way to apply the HAVING filter until > after the aggregation is finished. > > I think this approach is basically forced by the SQL standard's > semantics for grouping/aggregation. FWIW I also tried: HAVING array_length(array_agg(run_n), 1) < 10; but I saw the same amount of temp files, at least in the short duration of my test run. Thank you, I will split this into two passes like you suggested. It's just that I'm doing another 3 passes over this table for different things I calculate (different GROUP BY, different WHERE clauses) and I was hoping to minimize the time spent. But avoiding the array_agg() over everything is my top priority ATM so I'll definitely try. Regards, Dimitris