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 1ujLeG-006QZS-O3 for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Aug 2025 17:40:29 +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 1ujLeF-00Ans8-KP for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Aug 2025 17:40:27 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ujLeF-00Anrw-6P for pgsql-hackers@lists.postgresql.org; Tue, 05 Aug 2025 17:40:27 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ujLeB-000tXm-1v for pgsql-hackers@lists.postgresql.org; Tue, 05 Aug 2025 17:40:26 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-2406fe901c4so38411215ad.1 for ; Tue, 05 Aug 2025 10:40:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20230601.gappssmtp.com; s=20230601; t=1754415623; x=1755020423; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=EVfOJpvqbny63CRxx8pshXqC1wv5tP+Mgk8unkMyjv8=; b=qFHd5iYvaApnKxtTj499j5FC59zgnDNFYo6lv7hyIWv5Do8XOTsBjBIkKV7dQyiKGL hEnEKD406yaLyRwt5tLJow+f1oE+1YihRKImpjHKXgP2heqfzm/IYCHnfCOCzxH5ADvf 6n988xc9T8FIQikdnrlzMkwm7IIkzy4z8wuJjUFLZGpfR3IULLy5yX0G+LSP25lul765 cTrEQgKOTlC5K/ZETcmizqNaqopez5+ILGrNW2qZgSrBdMudmGHWeDvfvvB9LCKBQEL6 NANszJC66CLEDj3Q1b2oMbqBsDeqrklAKCHe/wMPwDK5DJES0jdAJUjHXQY8ljR7Bajy +qDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754415623; x=1755020423; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=EVfOJpvqbny63CRxx8pshXqC1wv5tP+Mgk8unkMyjv8=; b=p7tp3GqKGY2Io1fri6mSuQbMBUeV7D9Wz0WsMn7v0dd4GiLfqtVuL6ZWPPyW8MSGyu B6+fZqlIwbKag8pAxHrInq3fpS5HID6vifXBaghmgroyTdd125Sc6qiA4jSnGtHDa7MM 7nVMid6pQe1cYVxnEEymV2FFhZhjSzApKwWVEiAmFF462S2F+CwPBDcNloNIe5CE4G1i T0NUSYQJoIedrKoVqLRHr+A1cl7cVoRB+AqvAyHm84ypywtoPgd61C9zt42WLocWsOst xQJGRyb8+A/O4tETjq/BZlQEj7eQGzX/wZiYp/37g41nfQVdVaGvqDOl60fBa7tBklEF whGg== X-Gm-Message-State: AOJu0Yyxc8jaTgIKdSxUySF3gJiq/zPrASADJxN7f/6iKIMIMr+3MFGC GIwJNmi1xvmhkXRrccGwPCHtsjqCf7AS2e8lfR+kUZbnRrRA1tsqkt9uRrsmmO1zYg== X-Gm-Gg: ASbGncstQVGrRhQ9scFqP9L+e7n6FG80Zipo6eW3fkVa16Eg7rSW/HWobuudX8Ix5Bq XwxDPsOA8/P+2b7B3WCwCuatFB4hlpXyfQPwZorrrvZeTjYhPK9+EQaoFODxITjNOvADbgDNAQ8 6LL7UqnMVZm5hXJ67suO/sQkyPctBm4Iq2wKQVGJSq0AdP7OmtHl04rFI7mwgFstqCAUSfCqw/v hGPynef14wwugwpRDQc+XDm9vpPUFl4nSuA0hGyFHGRgmzOPKwgTVwShu5X/VuSgAj6jaW8Q8vo FF4Kp7tVzDu70gIYhlj0LCO90Fn4h/vWIlPY2ri1HCSIZp/jC8iHwdaUeToM4z3nRIcjh7wap+F FGJnHg1AOWZ6c4YTGEv9nFawCaFpxO72sVJ7wmjMAwc0R5BEfwDbeXIoVYKP9jdgTJhiQ X-Google-Smtp-Source: AGHT+IESOce64KjZtTb/nTAtlCWcpyjjfSpo8Zs+2VbU281B3FwUIkjHJcSy0vQskBaPmQ8663rqxA== X-Received: by 2002:a17:903:2f8d:b0:23f:8d03:c4ac with SMTP id d9443c01a7336-24246f46063mr213434605ad.2.1754415623507; Tue, 05 Aug 2025 10:40:23 -0700 (PDT) Received: from jeff-ws-bridge.lan (c-76-102-242-158.hsd1.ca.comcast.net. [76.102.242.158]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-241d1ef59b2sm139759515ad.7.2025.08.05.10.40.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Aug 2025 10:40:23 -0700 (PDT) Message-ID: <9800a3044cf2f2d948def3e5b68f8eda61a82d22.camel@j-davis.com> Subject: Re: Proposal: "query_work_mem" GUC, to distribute working memory to the query's individual operators From: Jeff Davis To: =?ISO-8859-1?Q?=C1lvaro?= Herrera , James Hunter Cc: "pgsql-hackers@lists.postgresql.org" Date: Tue, 05 Aug 2025 10:40:22 -0700 In-Reply-To: <202508051215.x5niwfkbomdf@alvherre.pgsql> References: <202508051215.x5niwfkbomdf@alvherre.pgsql> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.3-0ubuntu1 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 2025-08-05 at 14:15 +0200, =C3=81lvaro Herrera wrote: > Here's a rebased version of this patch.=C2=A0 I didn't review it or touch > it > in any way, just fixed conflicts from current master. James, Patch 0001 is doing too much. For a first step, I think it would be useful to create a new field for each Plan node, and use that to enforce the execution-time memory limit. There's a related discussion here about prepared statements: https://www.postgresql.org/message-id/83fbc36b66077e6ed0ad3a1c18fff3a7d2b22= d36.camel@j-davis.com so we may need to force replans if work_mem changes. That first step wouldn't affect how memory usage is enforced (aside from the prepared statement issue), because that field would just be a copy of work_mem anyway. But once it's in place, extensions could experiment by tweaking the work memory of individual plan nodes with a planner_hook. There's still a lot of work to do, but that work could be broken into the following mostly-independent efforts: * You point out that it's hard for an extension to handle subplans without walking the expression tree. I haven't looked at this problem in detail, but we can look at that as a separate change. * Save the estimates, as well, which enables an extension to be smarter about setting limits. * We can consider starting from the paths first before copying to the plan nodes. That would enable extensions to use set_rel_pathlist_hook to affect the structure of the plan before it's generated rather than just the per-node enforced limits after planning is done. For this to be useful, we'd also need to have some additional infrastructure to keep higher-cost lower-memory paths around. We'd need to find some practical ways to prevent path explosion here, though. * We should be more consistent about tracking memory usage by using MemoryContextMemAllocated() where it makes sense. * Consider enforcing the limit across all significant data structures used within a node -- rather than the current behavior, where a single node can use work_mem several times over by using multiple data structures. * We should free the memory from a node when execution is complete, not wait until ExecutorEnd(). What really matters is the maximum *concurrent* memory usage. Regards, Jeff Davis