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 1taLlz-0015Se-Hz for pgsql-hackers@arkaria.postgresql.org; Tue, 21 Jan 2025 21:27:00 +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 1taLly-0064GQ-1Z for pgsql-hackers@arkaria.postgresql.org; Tue, 21 Jan 2025 21:26:58 +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 1taLlx-0064GG-JL for pgsql-hackers@lists.postgresql.org; Tue, 21 Jan 2025 21:26:57 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1taLlv-000nLa-0t for pgsql-hackers@lists.postgresql.org; Tue, 21 Jan 2025 21:26:56 +0000 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-21634338cfdso144579235ad.2 for ; Tue, 21 Jan 2025 13:26:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20230601.gappssmtp.com; s=20230601; t=1737494814; x=1738099614; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=KPT3gMCdISRoaYJuSJ+GVExyKAJ/VI1Ia7+XblpbTzQ=; b=Z04LM0Cb7YPHrD8bdnWXNCFV9pUBYvJ9PPqSe2dJ3WoinbEos/Gs5BcZ76qG2bfjTz dpQk333h3laF26Gyc9EXl5InDpvSnZCj1QNpCU5Mr/mn74nb2W0h/60fVOFc70VtNdFo OSGpqurrckw/nQvG2BnqPjZYfpxXvhi4alBxcz9u6GTKfhJivqFOKPgNcNPGCY2vQp2Z lgDiPmzOplVX0x6K7zRHyqUgfXNUS4jMHjOVOhBKfNBV1PIBVbpOvpBKNSfT+5LDEXwZ K2Nvx9Mu2W6sA6m2S0X8X/Yafet5yt8HmsXAy9cT9Fjxm6AOhV7Oj++fysAC1REBetHu 75sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737494814; x=1738099614; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=KPT3gMCdISRoaYJuSJ+GVExyKAJ/VI1Ia7+XblpbTzQ=; b=Tvv7DRIO9SaVHz7qN3Nj26R03DarX1tkXRCHWwrTeagIFMmrNFDPyBfF/Q0WIWs6Q8 d+m8Hs56gICktEIABUZldHsOX39E4XzQTwJL4Desj6URLZd0tJUo+lmVxviqS/GM1VNk VAESDBVeDJjbVp9rZaHEL6COOP7fVHOCeUNqw15+ADcpnsuliL2QPEr0WPl4lRzgetqB JobwlLlgXs39TrqoRNWWteNOa1pPhMppRDcH4H3mk4Lzp/sRsmetp9uovxEG6as2SfId b3XWChMtgYUqYTBCPvbZe5se5zKO/NDkyXQE1S8mK5jkEsL1wlvA1HTbvzBhK2Ijb6pu 54Gg== X-Forwarded-Encrypted: i=1; AJvYcCW2thuX77SY9gMJ3ktwP7tbxi2d3rjeQ1Em2gAqc8PJ17L/CU2bvT2wUujx3qxCaNhZ/KzWHzjnNhy73cKn@lists.postgresql.org X-Gm-Message-State: AOJu0YwLVr3coWOlcURJsUqUzV7Iw0BF6ZiIiAVF7Ym7XWNg8MFgbSP4 WtEBpSocj2KCuojE7quDX/QPIfU+NJJT+gNf9llPdlZtBkzuzoJacQZsZT5WAQ== X-Gm-Gg: ASbGncu4Fk2XJDCTa0Gr8aqk1fD4gEJOWKMLMsha/mWL9bvidVDJ0kddE7PVOEI6xxY yrhZYjsGEyfGRdwWHT7MFcX7CzQ/PYh8An6PXesb/T9iJ1PazEmday7tKuEe6PIaG1H0Md8Imeq c1zyz8CCRdh/VhH0vSM6HorFUfLkQZNtSJ1Tzbd5nrVv/iQyn3FAYRwfnGiT9Gzza80EOLzG3VN migzHlwuDkSmwH4+2+/0svSYxXYe16qGeIxCIEIwpYOHdyQJyYvZcskGFPopAYRAPnTaMWV+xTU uzl0BCctL2JtaOj1NKZOLfsr+t2GuJNQjcCK X-Google-Smtp-Source: AGHT+IFB1dnUClLmOfSZ9hlTIuhAgZ+jw7RnMfzHMavWLcB26PT9qqQnRXtaxZDe33VkqAiSWQ5F5w== X-Received: by 2002:a17:902:d503:b0:216:401f:acd with SMTP id d9443c01a7336-21c355c7a48mr308195935ad.21.1737494814345; Tue, 21 Jan 2025 13:26:54 -0800 (PST) Received: from jeff-laptop.lan (c-76-102-242-158.hsd1.ca.comcast.net. [76.102.242.158]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21c2d4215dfsm83213075ad.248.2025.01.21.13.26.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jan 2025 13:26:53 -0800 (PST) Message-ID: <0beedccb92ba5a1db386bd09b44e15b20c1ac1fc.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: James Hunter , "pgsql-hackers@lists.postgresql.org" Date: Tue, 21 Jan 2025 13:26:52 -0800 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.44.4-0ubuntu2 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2025-01-10 at 10:00 -0800, James Hunter wrote: > How should =E2=80=9Cquery_work_mem=E2=80=9D work? Let=E2=80=99s start wit= h an example: > suppose > we have an OLAP query that has 2 Hash Joins, and no other operators > that use work_mem. So we plan first, and then assign available memory afterward? If we do it that way, then the costing will be inaccurate, because the original costs are based on the original work_mem. It may be better than killing the query, but not ideal. > -- Second, we could say, instead: the small Hash Join is *highly > unlikely* to use > 1 MB, so let=E2=80=99s just give both Hash Joins 1023 = MB, > expecting that the small Hash Join won=E2=80=99t use more than 1 MB of it= s > 1023 MB allotment anyway, so we won=E2=80=99t run OOM. In effect, we=E2= =80=99re > oversubscribing, betting that the small Hash Join will just stay > within some smaller, =E2=80=9Cunenforced=E2=80=9D memory limit. >=20 > In this example, this bet is probably fine =E2=80=94 but it won=E2=80=99t= work in > general. I don=E2=80=99t want to be in the business of gambling with cust= omer > resources: if the small Hash Join is unlikely to use more than 1 MB, > then let=E2=80=99s just assign it 1 MB of work_mem. I like this idea. Operators that either know they don't need much memory, or estimate that they don't need much memory, can constrain themselves. That would protect against misestimations and advertise to the higher levels of the planner how much memory the operator actually wants. Right now, the planner doesn't know which operators need a lot of memory and which ones don't need any significant amount at all. The challenge, of course, is what the higher levels of the planner would do with that information, which goes to the rest of your proposal. But tracking the information seems very reasonable to me. > I propose that we add a =E2=80=9Cquery_work_mem=E2=80=9D GUC, which works= by > distributing (using some algorithm to be described in a follow-up > email) the entire =E2=80=9Cquery_work_mem=E2=80=9D to the query=E2=80=99s= operators. And then > each operator will spill when it exceeds its own work_mem limit. So > we=E2=80=99ll preserve the existing =E2=80=9Cspill=E2=80=9D logic as much= as possible. The description above sounds too "top-down" to me. That may work, but has the disadvantage that costing has already happened. We should also consider: * Reusing the path generation infrastructure so that both "high memory" and "low memory" paths can be considered, and if a path requires too much memory in aggregate, then it would be rejected in favor of a path that uses less memory. This feels like it fits within the planner architecture the best, but it also might lead to a path explosion, so we may need additional controls. * Some kind of negotiation where the top level of the planner finds that the plan uses too much memory, and replans some or all of it. (I think is similar to what you described as the "feedback loop" later in your email.) I agree that this is complex and may not have enough benefit to justify. Regards, Jeff Davis