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 1tvDHe-002AE4-8a for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Mar 2025 10:37:54 +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 1tvDHd-001wxT-2M for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Mar 2025 10:37:52 +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 1tvDHc-001wxI-Pc for pgsql-hackers@lists.postgresql.org; Thu, 20 Mar 2025 10:37:52 +0000 Received: from mail-lj1-x230.google.com ([2a00:1450:4864:20::230]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tvDHa-0007BT-0K for pgsql-hackers@lists.postgresql.org; Thu, 20 Mar 2025 10:37:52 +0000 Received: by mail-lj1-x230.google.com with SMTP id 38308e7fff4ca-30613802a59so7143201fa.0 for ; Thu, 20 Mar 2025 03:37:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742467069; x=1743071869; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=+2Wbx/6NjYB38reoU2kSDLW2rL3UY9Mj/M84IvroR4o=; b=GDqKsOsafESC0+I7BegO+XN0EoAvAJ7qPWbD25sFbRkTzDEajXLJ6wsJQBAZYg2NyH 9UTeCZ4qzyHbsxXD8c4+QbHpRPud8NUISKaBpyChcPqkOsNrQ83tal6A1rFOLNYgzRyQ 9uGqi1PCqThsNRc02Efp7B8sqP0Hdx82esCq9q7z5HiMGpZBEHemMZXwN26MraENcU6R fx7KPeP0SKhC1Sb9p/lJ6IufEdS8H5TVsjfG7rt/3TLs4TBX+xTVSyb71+9Gj/JnBSgt hrUA+Y6JoKJLZEerYok+fU1v07uxMuDaX7pGgxa2YYmSiZsOuQrJXnChLBoRh5+LlCx+ JwNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742467069; x=1743071869; h=content-transfer-encoding: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=+2Wbx/6NjYB38reoU2kSDLW2rL3UY9Mj/M84IvroR4o=; b=XjGhTS9vlbvDSDk+38rOhDM0OpWGGl+Wt1kVRmF1FWthTE1kpVdWarcZAmH7IriSM6 gYCItVFFQ2267BLWZBMFSo49AFD7rDlZ9TSNJwUZvnJZsr0wlqm9FzhKLAga5WQX0eaW wDPV3pOkTQrD6wYjoJRlVlg3cOMQDt9xZNr6x8CgK7tCRY+r/+BuZVQEpEJrDbQ/3b+l Eves2wHaqW9402J/p6tQVuMqZQUQMXNewXvgq6JfcTZtr86ZdRJaZ63Wb3Hz3dbqIwAU 5q4QO9xFxysKgnagnEuk+p5fCepghZqCfGpYkrhUA/x6gVzZL4gHuL4yj38gRIRnvVAX Pyqg== X-Forwarded-Encrypted: i=1; AJvYcCXVd5dal8ulr6lzINBOu+Xy6Kw8mAKn7L8DSmqGxCzOjc5z6ex3dbpoM/QV+HJr1NNXnCVrD2xm33l/GBcS@lists.postgresql.org X-Gm-Message-State: AOJu0YxEwPIrDbKMOdBeYo+UnclTCNg6rrX+q8rkmDEzBLIKvGxsvJpt 1bwuhEyDnhFlr03w9JB4dANRfJadIrCVMiF48cMtt1w0CsJmIA1ryGox552tlr3676Ch7ZbO8zA fzcFEr4e836+5YIjEOTKAz5RhxOQ= X-Gm-Gg: ASbGncuS048Hn6FjvqDMhlvzKICyUXbWWhlnY4tqHlW0G7F+IvGrjLeq6h6kh1wiEN0 vaedIbA6WflI385tTPfGkuKAm+acuXhEALJm2kWxn2lG/1HA/PA/Y+3qZYgROcZl4DWXsohG+OU //WwKbj28foR52gotkZ4fF7aBg2O/7Xs/BVivboFZRB8ccjWjOuiO9Ab/JV83b8ERJ7fdA0z4= X-Google-Smtp-Source: AGHT+IFmjCqNTVbychxA5GvRMBL1N/aC+1TX15k3fYORmtkc+ZG3jrn4ZS23ud5K3BXEsFzRvSCcL9pINgOk4Xx424c= X-Received: by 2002:a05:6512:1052:b0:540:2fd2:6c87 with SMTP id 2adb3069b0e04-54ad0629c87mr829733e87.16.1742467068927; Thu, 20 Mar 2025 03:37:48 -0700 (PDT) MIME-Version: 1.0 References: <51A15CD7-E31B-483D-B911-D0EB9F5FF952@yesql.se> In-Reply-To: From: David Rowley Date: Thu, 20 Mar 2025 23:37:35 +1300 X-Gm-Features: AQ5f1JoTFhjtk0UjigKtkSlESCuOXRUIks8shEkweWhJ8L2O7k4lR7OOHg6Wj44 Message-ID: Subject: Re: Add estimated hit ratio to Memoize in EXPLAIN to explain cost adjustment To: Ilia Evdokimov Cc: Lukas Fittl , Daniel Gustafsson , PostgreSQL Hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 20 Mar 2025 at 21:48, Ilia Evdokimov wrote: > -> Memoize (cost=3D0.30..0.41 rows=3D1 width=3D4) > Cache Key: t2.thousand > Cache Mode: logical > Cache Estimated Entries: 655 > Cache Estimated NDistinct: 721 > -> Index Only Scan using tenk1_unique1 on tenk1 t1 (cost= =3D0.29..0.40 rows=3D1 width=3D4) > Index Cond: (unique1 =3D t2.thousand) > (11 rows) > > Additionally, since this information would only be shown in EXPLAIN when = costs are enabled, it should not cause any performance regression in normal= execution. However, reviewers should be especially careful when verifying = test outputs, as this change could affect plan details in regression tests. > > Any thoughts? > + ExplainIndentText(es); > + appendStringInfo(es->str, "Cache Estimated Entries: %d\n", ((Memoize *)= plan)->est_entries); > + ExplainIndentText(es); > + appendStringInfo(es->str, "Cache Estimated NDistinct: %0.0f\n", ((Memoi= ze *) plan)->ndistinct); est_entries is a uint32, so %u is the correct format character for that typ= e. I don't think you need to prefix all these properties with "Cache" just because the other two properties have that prefix. I also don't think the names you've chosen really reflect the meaning. How about something like: "Estimated Distinct Lookup Keys: 721 Estimated Capacity: 655", in that order. I think maybe having that as one line for format=3Dtext is better than 2 lines. The EXPLAIN output is already often taking up more lines in v18 than in v17, would be good to not make that even worse unnecessarily. I see the existing code there could use ExplainPropertyText rather than have a special case for text and non-text formats. That's likely my fault. If we're touching this code, then we should probably tidy that up. Do you want to create a precursor fixup patch for that? + double ndistinct; /* Estimated number of distinct memoization keys, + * used for cache size evaluation. Kept for EXPLAIN */ Maybe this field in MemoizePath needs a better name. How about "est_unique_keys"? and also do the rename in struct Memoize. I'm also slightly concerned about making struct Memoize bigger. I had issues with a performance regression [1] for 908a96861 when increasing the WindowAgg struct size last year and the only way I found to make it go away was to shuffle the fields around so that the struct size didn't increase. I think we'll need to see a benchmark of a query that hits Memoize quite hard with a small cache size to see if the performance decreases as a result of adding the ndistinct field. It's unfortunate that we'll not have the luxury of squeezing this double into padding if we do see a slowdown. David [1] https://postgr.es/m/flat/CAHoyFK9n-QCXKTUWT_xxtXninSMEv%2BgbJN66-y6prM3= f4WkEHw%40mail.gmail.com