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 1tniqC-0012AD-77 for pgsql-hackers@arkaria.postgresql.org; Thu, 27 Feb 2025 18:42:37 +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 1tniqD-002l57-8N for pgsql-hackers@arkaria.postgresql.org; Thu, 27 Feb 2025 18:42:35 +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 1tniqC-002l4W-QK for pgsql-hackers@lists.postgresql.org; Thu, 27 Feb 2025 18:42:35 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tniq7-00023C-0x for pgsql-hackers@postgresql.org; Thu, 27 Feb 2025 18:42:34 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-2233622fdffso26418365ad.2 for ; Thu, 27 Feb 2025 10:42:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20230601.gappssmtp.com; s=20230601; t=1740681750; x=1741286550; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:date:to:from :subject:message-id:from:to:cc:subject:date:message-id:reply-to; bh=+UUJDIRvqWHK7F//2WMUjLBRLglN1fu0VN0Hf+EzSc8=; b=zYLj7J4IvxO37k3glo1uszxGiPBQzmg+G9dmHyOwRhkndYEgzSUxQ3TzHCVX4KKyyQ USXQ8+zlWWFyEC7BOs8Mg5ZUGWhhCq8EhKSord2UqNA6/cnxCqnx8PNLei0a7AgdLo1n mvIrjpt0QkZ6MK7wwCFG0Kj96a5f8hKub93ctt6TYp7XtfzIIWvosbUliVDlccXl021b ThhERcAy82TQQ5sr09PeL6lZieemk3214/5GDnUCJ/+7WCiy1JbJq5YRO6s5gnCpCBE8 UT6e1deFGQDpbfnWbaO18jDhl3olQAv9lenmSx7+Df/VeSt5NGQixjERsqnQQoNCfydk v6SQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740681750; x=1741286550; h=mime-version:user-agent:content-transfer-encoding:date:to:from :subject:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=+UUJDIRvqWHK7F//2WMUjLBRLglN1fu0VN0Hf+EzSc8=; b=OzzXJqjRxgisYulJyePLKlmnrQWnOsnnH+RXKK8Mgkl1O3CzEU75GASvYy53g9igSb H+pt8cCNUzMA7Q8IzVrFsOJM8HC8CcStj1j0pdKOhzG8zUiUNfrQRJ5A8oA5wImiJTdE KPo7vDq92b+Vxn63HBeOpWELzwxWgEk2hWhEBtHCbqJpmB3b37aTnoz70pT3qscnzbTW o3l7u15tkDdy0prnnmr24aRJE5S9GhyGJoFQzWembHY0pwniWJPlCTovnZTSKDCuWRQg 5rtHKq+OXb04QgKzi4RHVF7p8sTHGnMXwsm+oWwY4EAhEg2HHl6mf2Eu/CG3KbeHTov4 OVbw== X-Gm-Message-State: AOJu0YwKFyJaQ1VdLp4dwcP107wWwDfj6rdK1Cq19q2tL68BcFR4YkW8 2EEDJrneZYpNgy5JgFia+no5fBYBNioz+EwP4kgVVEBVpbbHwzItsAvIsLT0jTJcklIql0IABWI nCQ== X-Gm-Gg: ASbGncvkyIbaJbVRi0PWfCpbzwlF7Z31ENsoORgkUXJGOftx7ktjS1btbE5l+G+4Xbz mYoeu5ank91GCzW3mmBZKp/rdxGnqyRKHSeK8v35aypniMS7cGrbbMaxfWos8b/QwhbgPzxWteb 77qHTeTtTB+ZW113yPSdE9zlq1c4cc3+ss4HZ0EPr9FlDzJHscuWhWFgdCqfVL8SZj8Vgx7l+zW h/At2bP27cSDvMhGSWcDuXUZjustJ65oBHdQdMDgAChKt6LXKvvxJnYxdz5b6+qZ0QLAHXkbFk1 yMPRzgvaFt9AkOeEI/1OeZFn1l7G5j6CmxGJ0rx4sxaLxNTYlFyUdjxpyaoFKUTJYLfF X-Google-Smtp-Source: AGHT+IGcwa7lECGxO09sIv8D0JyInFp9OjD4MyUtN8j+Lb4mmxS4GdZRr85pqGLmbWxoFW2ylJLQrQ== X-Received: by 2002:a17:902:f603:b0:223:5a6e:b10 with SMTP id d9443c01a7336-223690e1acbmr2677695ad.27.1740681749616; Thu, 27 Feb 2025 10:42:29 -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-22350534004sm18246475ad.252.2025.02.27.10.42.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Feb 2025 10:42:29 -0800 (PST) Message-ID: <2a2b28e2ea677e3cec2850b2dd38b467bf1291fb.camel@j-davis.com> Subject: Should work_mem be stable for a prepared statement? From: Jeff Davis To: pgsql-hackers@postgresql.org Date: Thu, 27 Feb 2025 10:42:27 -0800 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 As a part of this discussion: https://www.postgresql.org/message-id/CAJVSvF6s1LgXF6KB2Cz68sHzk%2Bv%2BO_vm= wEkaon%3DH8O9VcOr-tQ%40mail.gmail.com James pointed out something interesting, which is that a prepared statement enforces the work_mem limit at execution time, which might be different from the work_mem at the time the statement was prepared. For instance: SET work_mem=3D'1GB'; PREPARE foo AS ...; -- plans using 1GB limit SET work_mem=3D'1MB'; EXECUTE foo; -- enforces 1MB limit My first reaction is that it's not right because the costing for the plan is completely bogus with a different work_mem. It would make more sense to me if we either (a) enforced work_mem as it was at the time of planning; or (b) replanned if executed with a different work_mem (similar to how we replan sometimes with different parameters). But I'm not sure whether someone might be relying on the existing behavior? If we were to implement (a) or (b), we couldn't use the work_mem global directly, we'd need to save it in the plan, and enforce using the plan's saved value. But that might be a good change anyway. In theory we might need to do something similar for hash_mem_multiplier, too. Regards, Jeff Davis