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 1u4Qo1-001yZn-C3 for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Apr 2025 20:53:25 +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 1u4Qnz-004n5b-M3 for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Apr 2025 20:53:24 +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 1u4Qnz-004n5O-CE for pgsql-hackers@lists.postgresql.org; Mon, 14 Apr 2025 20:53:24 +0000 Received: from mail-lf1-x134.google.com ([2a00:1450:4864:20::134]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1u4Qnx-000532-2l for pgsql-hackers@lists.postgresql.org; Mon, 14 Apr 2025 20:53:23 +0000 Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-54acc04516fso4633149e87.0 for ; Mon, 14 Apr 2025 13:53:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744663999; x=1745268799; 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=8BlUxmGf9cCDRp7k1AGzR+kpsoesk78XbNLaDUOqjOg=; b=INRM9iVQ0kROtR517Mm3gIZhJEMGjbq0z1bNYlmAajabtyTeSIBteWOxPDaWLowVF8 +LPXIuR3OgJ7Im75FqqJ0QdH56ju3aQTgW7IX9sQMA1GGh7UGXfPS1RhREKxr0gkTwTi QU/M6KoyvTGaFa9zI2Gb6MLmiORjauXpuHk5pOTGtYjWr4K+rpG7ZPrUX4DrL7HoPL1U cv/tjUfdr/JTvVUxPkptSxh1SwrOmTmUsUgO+yKqAGnNGU8ciGYiGGqt7OWU4hX0vJwJ hp50wKFUC3LrMf+cmmDCas2pdyppSL9397xh024TYuLJ/AKEaIvlBnkZPBzdRidV5ddL Dxkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744663999; x=1745268799; 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=8BlUxmGf9cCDRp7k1AGzR+kpsoesk78XbNLaDUOqjOg=; b=L24CrC3IIW6btgJS22+7bzuUntmLjsVsa0CR9xCu6LM0ZwSlRM8ql04n/2rvCmlEQM 6C7yl+VMJVpnsuBeyNuIQs5y7u7VZ3XH3Wk2i9cCWHAKLNeLax+MwfH4HOg4aamrFxM8 +ufQWCqDpIoAUm5gnLJs57tzLLz/2rbQWbdTLQ5eLImIFjiuvY4Hjt9Y7th9VI+yVZW5 hnNg0Z5jIYkuwuCfY9W1wy/Dg9birojDO4qtBxEEhcymgvG6orZHjLNvFGhJiLtVOnj/ 9NNGBZoEL/R3J+gS82L2l//zmCR32gZ5fnuuUzw1byRlrsxir04BzX1vG3Dli5lHSNGK ywvg== X-Forwarded-Encrypted: i=1; AJvYcCVEZSslTRO1MGgSeb8/5dUIxOYdumVZD8lNTKTPLZHTUPVCmPM8r3rGrxq8u1oNoPfFddCn94AVg67ydv27@lists.postgresql.org X-Gm-Message-State: AOJu0YwdZuN36Q+7H3rdm92uzL35LDFjLAZhMuIa6pTj6Ez5NJK2N0mp apUY++MzTZT83728+eoN2qZmd6kcWFFGpApnfjmCUzJxKGdH2ztQdJQGt03WlUF5qHTHAeOtP4s 4Cvt4UIoK+tuPueWREkZ3nasS/aI= X-Gm-Gg: ASbGncvxWeVx20FPFOecAg4hYW/ApS/ZwhP+X6J3pu7dEvhcRDPJJxw56DE+YSv5wD0 V7umr+AXmPT8g+9XvGPezohiXo2wCu82+0Emt+4f7iu4v+AURp8IWm263/guNj74McJ3tRnadTD PE0QPXr1WqrOGhuqedEBjsCAUsK4M53aVsmoUqqA5AKeLgTLTjZzvOtNvvmVfBXsNZNOI= X-Google-Smtp-Source: AGHT+IEuolq6Fd9jkUfpJWh1tPWrYJt/4CNUKNpnc0T9mYBcrIffXlpv+0YHzxC89SedaCV6QmE4LawvioGC5bh4dyU= X-Received: by 2002:a05:6512:2255:b0:54b:ed9:2cf8 with SMTP id 2adb3069b0e04-54d4528b3famr3489965e87.10.1744663998768; Mon, 14 Apr 2025 13:53:18 -0700 (PDT) MIME-Version: 1.0 References: <44229379-3901-4cb0-8812-b354ef70d5e2@gmail.com> <8e12aa55-39eb-4e03-a8f7-077fe02309c3@gmail.com> <11c709e1-9b17-4c00-9ead-93609a09b9db@gmail.com> <1161645.1742854534@sss.pgh.pa.us> <1164845.1742856354@sss.pgh.pa.us> <3ba16aa7-3cb2-4289-b773-522ae2f03a77@tantorlabs.com> <81ef0f5a-b6a1-4417-ae3a-2bae9b8d419e@gmail.com> <67b5c84a-dedd-4103-af7e-0117046102fe@tantorlabs.com> <580fe72c-85be-45a8-9e00-6b5417690557@tantorlabs.com> In-Reply-To: From: David Rowley Date: Tue, 15 Apr 2025 08:53:05 +1200 X-Gm-Features: ATxdqUGjgHOYxKdtga_l50eUGABr5SV3YjrLyIDA-iFN5IfQeXkWWSaQGv2DTLQ Message-ID: Subject: Re: Add estimated hit ratio to Memoize in EXPLAIN to explain cost adjustment To: Robert Haas Cc: Ilia Evdokimov , Tom Lane , Lukas Fittl , Daniel Gustafsson , PostgreSQL Hackers , Nikolay Samokhvalov , Andrei Lepikhov Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 15 Apr 2025 at 04:31, Robert Haas wrote: > I don't think we should use ANALYZE for this because, IME, that should > just be about whether the query gets executed. Since this looks like > information that is available at plan time, I think it should be > displayed all the time or made contingent on VERBOSE. It would be sad > if you had to run the query to get information that was available > without needing to run the query. Personally, I think we can probably > just display it all the time. I mean, the typical plan is not going to > contain enough Memoize nodes that a little extra chatter for each one > impedes readability significantly. You might be right there. I just offered that as a possible solution to the concern of bloating the EXPLAIN output. > Having looked at v6, I think it would help, at least if the reader is > sufficiently knowledgeable. From the values displayed, it looks like > someone could reconstruct the evict_ratio value in > cost_memoize_rescan(), and if they know the loop count, also the > hit_ratio. But that seems hard: if you weren't reading the code, how > would you know how to do it? Even if you are reading the code, are you > sure you'd reconstruct it correctly? I wonder why we think it's better > to display this than a more "cooked" number like the estimated hit > ratio that was proposed in v1? I think the problem with only showing the estimated hit ratio is that it's difficult to then know what you might change to improve that in cases where you might have other join types disabled and are experimenting to figure out why Nested Loop / Memoize isn't being chosen. If you see the estimated capacity isn't big enough to store all the rows for all distinct keys then you know you can increase work_mem to assist. If the Estimated Unique Keys is too high, then you might be able to adjust some ndistinct estimates or increase stats targets somewhere to improve that. If we only showed the estimated hit ratio, then you wouldn't know where to start to get the planner to pick the Memoize plan. As I mentioned in [1], I wouldn't object to having the estimated hit ratio shown in addition to the components that are used to calculate it. Per [2], Andrei didn't seem to like the idea due to the duplication. FWIW, I do agree with you that the hit ratio isn't exactly easy to calculate on the fly when looking at the input numbers that are used to calculate it. I just wasn't 100% certain that it would be needed in its calculated form. Maybe it depends on the reason you're looking at EXPLAIN. Maybe the reason I mentioned above for looking at EXPLAIN is just one of many and I'm not thinking hard enough to consider the other ones. If we can't get consensus for everything people want to add at once then maybe the patch could be broken into two, with 0001 being pretty much the v4 patch and then have 0002 add the Estimated Hit Ratio. Having the physical patches and being able to try it out or view the regression test changes might lower the bar on people chipping in with their views. David [1] https://postgr.es/m/CAApHDvoE5S5FkvEq+N3-J9LfaVUpWLOnczOYAOEvBMCY20=pdg@mail.gmail.com [2] https://postgr.es/m/e76f1635-a933-47bf-a826-045e20d5cfff@gmail.com