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 1tvSSu-004rRK-Fh for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Mar 2025 02:50:32 +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 1tvSSr-00EGtm-Jd for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Mar 2025 02:50:29 +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 1tvSSr-00EGtd-8w for pgsql-hackers@lists.postgresql.org; Fri, 21 Mar 2025 02:50:29 +0000 Received: from mail-lf1-x131.google.com ([2a00:1450:4864:20::131]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tvSSp-000EyQ-1x for pgsql-hackers@lists.postgresql.org; Fri, 21 Mar 2025 02:50:28 +0000 Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-5499c5d9691so1655722e87.2 for ; Thu, 20 Mar 2025 19:50:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742525425; x=1743130225; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=0E1OBX9mbKiA4JHEN7bilxLb0YgU7kFwiUu7djCGxTw=; b=WSrgb8Iwl9TuWrT00QXHtT1RrV8eyeaMrK4rtusTBFh0am0GDAXGz3AB90Pk2iX7iP Ba7UgqvJ/h6M8szdQcFtHnm00KesyFYuVmP6GQ0/+Mob76g6HajXIVBNdhBDBlJa5nLC uG1SwX2cxMKLydcu5071Q2MxugyhIaRgxlaBbGntwj2waxbs5V6DgtVAa6PGUt6JYVpb 0vjVuIkZ1Ne23RDToChkv1ejtRDRO5ulB1hWK7/JAj+08wOzouW6Im2OYtLvua0jwUjH jCAfEgAwj5layxjmtdiqqIkFlssYTJt9sHMxI7cicuWFDA2GEeqIjDGwPmb3nDF0U6H2 PbWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742525425; x=1743130225; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0E1OBX9mbKiA4JHEN7bilxLb0YgU7kFwiUu7djCGxTw=; b=TCsa0hIV95Pc1Lqgv0t/JvVYX2ORwA9nYYdagpHs6rZF9qNmBrPgeW96i9EwpxTaGR wY5CIV63NpEmElm9sN3PT2fDtKOAAtoT9T5/WWY5uYtfmlxmU0eydiMAa7RFLIBKvKoB 9N9y5KL43nIIK3mKoAl2RzrclZaFcKFo54Kc6Z2LMjs/VOR5KMPTSyFrs6urgfYUpVNs DmP+mvQ4TDtd9QRJeSwoKeGq7jMH18Nc6KCX0OgCQyGs1hGN+2eM+A1Z2xBmaFyzYVqz FEdcF4A7IsksNW85SvXT7YJN2uhjXyd2I21+mmO0qPypbWZn9dRIdmhE3P6Gabs7XSYg ch3Q== X-Forwarded-Encrypted: i=1; AJvYcCVRFkmEgS/DlNnAdEGiD8y95Udmp9lMTXfGajLKp2Cng9lcAaQDMTwKupZsHE/fl7RiyIitj74oaTqa8mgl@lists.postgresql.org X-Gm-Message-State: AOJu0YzY5/1hwyhFxiuw0DlFMtCX09wp3ljY0uhAgdfKw8WuogETWLGV 8MQUcqpex46M4xPMtmvY56tTEPJ6xBVdcOe5qSN8UAP2V0Igr6WgSdlxYDQtO1zM8s9pPV3URmh OgbEozARhIS1Mr4K1PIhZqx7xHgA= X-Gm-Gg: ASbGnctd+ia6z6r9Ia1ALKmSEljwjA6iYzYCy/OXguPMdEIzAzDgGIglPRcZkuUdADd UXqvThjRPJDqcG/sIwlo02Yf67avZ0wIQUs42EPsKeaIHTupEW26U7TOdby4YhsWX2ti8ApO/ph S19mzEw/7Yzg1oIEw+fU670L1BT8mgFufZu5OoKY1vMJMbaglo1gilBjwAKDKu X-Google-Smtp-Source: AGHT+IHIXDrHzjspheimtcSGpyd8+BKzjf7iYo9mVs2E9gRHiOsEgczEEyAM67eyAY0tdfK93pr6qvbaD/cRjIhATM8= X-Received: by 2002:a05:6512:2349:b0:545:e2e:8425 with SMTP id 2adb3069b0e04-54ad64f68f9mr632350e87.39.1742525424333; Thu, 20 Mar 2025 19:50:24 -0700 (PDT) MIME-Version: 1.0 References: <51A15CD7-E31B-483D-B911-D0EB9F5FF952@yesql.se> <44229379-3901-4cb0-8812-b354ef70d5e2@gmail.com> <8e12aa55-39eb-4e03-a8f7-077fe02309c3@gmail.com> In-Reply-To: <8e12aa55-39eb-4e03-a8f7-077fe02309c3@gmail.com> From: David Rowley Date: Fri, 21 Mar 2025 15:50:11 +1300 X-Gm-Features: AQ5f1JqcxBVcdcXMjByrj6Fs08xKf3x55uY_PBPiAc3mwy0AfWSn5_zz-7rORDc Message-ID: Subject: Re: Add estimated hit ratio to Memoize in EXPLAIN to explain cost adjustment To: Andrei Lepikhov Cc: Ilia Evdokimov , Lukas Fittl , Daniel Gustafsson , PostgreSQL Hackers Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 21 Mar 2025 at 07:54, Andrei Lepikhov wrote: > I have some doubts here. > The number of distinct values says something only when it has taken > together with the number of calls. Couldn't the reader just look at the Nested Loop's outer side row estimate for that? > Frequently, one of the caching keys is from outer table A (10 tuples), > and another is from outer table B (100 tuples). Calculating the number > of successful cache fetches predicted by the planner may not be evident > in the case of a composite cache key. > > What I may propose here is: > 1. Use fraction of calls, for example - 50% duplicated key values. > 2. Show the calculated hit and eviction ratio. My concern with showing just the estimated hit and evict ratios it that it might lead to more questions, like how those were calculated. However, I can see the argument for having one or both of these in addition to the expected unique keys and expected cache capacity. I think the primary factors in how useful Memoize is are: 1) How many items do we expect to be able to store in the cache concurrently, and; 2) How many unique lookups keys do we expect to be looked up, and; 3) The total number of expected lookups. #1 is quite difficult to figure out (maybe by looking at row width and row estimates) and there's just no information about #2. #3 is already shown, in the Nested Loop's outer side. The reason that the hit and evict ratios might also be useful are that it you need a bit of inside knowledge on how the 3 input values are used to calculate these. For example you might come up with a higher estimated hit ratio if you assumed the keys arrived in order vs unordered. If they're unordered and you don't have room for all keys in the cache at once then that increases the chances that Memoize had to evict something that will be needed for a future lookup. Also, I was just looking back at the concern I had with increasing the size of struct Memoize. I suspect that might not be that much of a concern. The WindowAgg problem I mentioned was with the executor state, not the plan node. I see the only time we access the plan node for Memoize during execution is when we call build_hash_table(). David