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 1lJgrk-0005ZH-2U for pgsql-general@arkaria.postgresql.org; Tue, 09 Mar 2021 18:13:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lJgri-0005fe-9N for pgsql-general@arkaria.postgresql.org; Tue, 09 Mar 2021 18:13:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lJgri-0005fV-1p for pgsql-general@lists.postgresql.org; Tue, 09 Mar 2021 18:13:54 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lJgrf-0004ov-VE for pgsql-general@postgresql.org; Tue, 09 Mar 2021 18:13:53 +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 129IDnxA2036986; Tue, 9 Mar 2021 13:13:49 -0500 From: Tom Lane To: Radoslav Nedyalkov cc: pgsql-general Subject: Re: how to limit statement memory allocation In-reply-to: References: <2022857.1615305820@sss.pgh.pa.us> <2033832.1615308833@sss.pgh.pa.us> Comments: In-reply-to Radoslav Nedyalkov message dated "Tue, 09 Mar 2021 19:14:57 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2036984.1615313629.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 09 Mar 2021 13:13:49 -0500 Message-ID: <2036985.1615313629@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Radoslav Nedyalkov writes: > On Tue, Mar 9, 2021 at 6:53 PM Tom Lane wrote: >> The only thing I'm aware of that could consume unbounded memory >> on the server side is hash aggregation. (v13 has improved that >> situation, but I'm guessing you are running some older version.) >> The planner won't choose hash aggregation if it estimates that >> the hash table would need to exceed work_mem, but its estimate >> could be wrong. > Query and plan attached. Yeah, so, this looks suspicious: -> HashAggregate (cost=3D181320662.52..181323190.52 rows=3D252800 w= idth=3D16) Group Key: gsm.merchant_id, cer.date -> Merge Left Join (cost=3D161250580.17..170174828.82 rows=3D= 891666696 width=3D71) How accurate is that estimate for the number of merchant_id/date groups? Is the estimate for the size of the input join reasonable? Assuming this is the problem, a brute-force fix could be to turn off enable_hashagg. But it'd be better to try to get the planner's estimates more in line with reality. regards, tom lane