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 1lJfcN-0002ii-If for pgsql-general@arkaria.postgresql.org; Tue, 09 Mar 2021 16:53:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lJfcM-0001gc-CZ for pgsql-general@arkaria.postgresql.org; Tue, 09 Mar 2021 16:53:58 +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 1lJfcM-0001gS-4R for pgsql-general@lists.postgresql.org; Tue, 09 Mar 2021 16:53:58 +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 1lJfcJ-0001z2-Io for pgsql-general@postgresql.org; Tue, 09 Mar 2021 16:53:57 +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 129Grr472033833; Tue, 9 Mar 2021 11:53:53 -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> Comments: In-reply-to Radoslav Nedyalkov message dated "Tue, 09 Mar 2021 18:32:46 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2033831.1615308833.1@sss.pgh.pa.us> Date: Tue, 09 Mar 2021 11:53:53 -0500 Message-ID: <2033832.1615308833@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:03 PM Tom Lane wrote: >> Perhaps the accumulation is happening on the client side? libpq doesn't >> have any provision for spilling a result set to disk. > Ah, I named it result set wrongly perhaps. > These are queries , part of a larger ETL function or statement which at the > end just write to a table. > The huge join is an intermediate step. Hm. What's the query plan look like? 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. If that's the scenario, you could back off work_mem until hash aggregation isn't used, or you could try to improve the planner's estimates. If your ETL process involves intermediate tables, you might need to explicitly ANALYZE those after you fill them so that the planner has a better idea how to plan the later steps. regards, tom lane