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 1vEG75-008W4x-5A for pgsql-general@arkaria.postgresql.org; Thu, 30 Oct 2025 00:01:58 +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 1vEG73-004YfI-Tv for pgsql-general@arkaria.postgresql.org; Thu, 30 Oct 2025 00:01:56 +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 1vEG73-004Yf9-EF for pgsql-general@lists.postgresql.org; Thu, 30 Oct 2025 00:01:56 +0000 Received: from mail-lj1-x22d.google.com ([2a00:1450:4864:20::22d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vEG70-004THK-0y for pgsql-general@lists.postgresql.org; Thu, 30 Oct 2025 00:01:55 +0000 Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-378d54f657fso3946441fa.2 for ; Wed, 29 Oct 2025 17:01:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761782511; x=1762387311; 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=SPKLH6+m9UUa539uUbM5IeRfpgYLwjuS3zHVTYp7/cI=; b=E96BEBtGTXgMLWKE6xE/zJ8Y4Ho1Wv+1mZSxMqrJxoP6foZLI2zFZV7p+Kla2eklFm z1sgnE8UOb/0RafFqZ0zQFsEglPPFNuvabZh1DWB05DyI3oIf64gIXHB4YpvASAl42mT bPTyJGztvwLHItuBGEYiaEKfYTsVQ4Cf9/oEQt9Zhwkji9NcwaLK2P3O2ftv5dVMXR/M XoGqoBiD6tf7/gj41Y9hE3+YCyxq5+HH45m/XTVTh9L+23nt3uKrG0MXF9eAVzSI3TEK JVn0g1M6Pyqx0cEC9oFyMcs9k9wB6jewKNnNyPCRHDjB1Yp7F8PBM5uvNI9tHi6121f+ zUkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761782511; x=1762387311; 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=SPKLH6+m9UUa539uUbM5IeRfpgYLwjuS3zHVTYp7/cI=; b=JXJn6bGAciO7c68Oyg1Ph0IFJldQT6RlKWcJnFoLffTZUVq7phgWLdM7Rx56ZQxZhE 7plqTgnGFP4+6js4SuSmw12B+3G+0lc+ign4DS8tz415cV+MKiwcP2UUPw2u3AI3xRkO hDQ0O2yCUNsgbmfKYpaeJix9lY/j+yCBpaoWtJ0jxyhoB66+4uXCMNohblgI3SxwDz6H qM5K1ffd6PX245pGVQf1xt6W8UybOETJXus1rQt/QVjdm85KbmiKDGYu//4bezPZ1Q1v yizQf4FWvQmGChOxjUFtiAdD/fdcDEYk9rWxLIf/fW9rTAAh4CgHF4DnhEEf6wofW1WQ uC9Q== X-Gm-Message-State: AOJu0YybX5fxdf8HrU5qVZ0yWX5EfTPK5+nA6JJGBHn1lqOEhmSunyUX D10ZbCMX8vKfAtE2TgMzYzms/j+00Fu83zwF5QJKozRiycBmhN5Y0DTJYzf7rdWulD5bqaHZ12X o1QDOefRRBzyuLT+19jchLabFAOyFjXc= X-Gm-Gg: ASbGncuoYqwUqrh77XyfPC07r/5RSw4n5nNWm+lxQ+RKa9U5RSZHpZ8PnIGj9l9/O8+ dACvGUZUUFUsy90fhGENvNa3d2JnUjig/hAB5PFtBVYqGgTWph5myPqlzH3SqjGqSm/yyFW7xHb ygshc1M/GhNLmbhdxCdKQ9bq32IWXU/wUTU00aRgv6L2AcpJ1yzY4TTol4aN1JQdfGTNAoxnUHm ESKLUXqfqa7fVXRwCwomemtDDZJ0HPwE+kvFgAK6/jmJ+4EVjEMlPZPK7gcdL2xZLP889rOM2Xv ky5pIwdvJeJZHUec/3jiz45o6lhgGNaKc+nF6c+A8EQy0oeNvg== X-Google-Smtp-Source: AGHT+IHryCblkoRtUHZFTBksOpCAILMPZ/XZKETzr7brs1uUJrqDeDitwFkAzX6Sqy3x71mNkvCCpBV/C8M8X4Ucku0= X-Received: by 2002:a2e:9e56:0:b0:376:30c5:66ef with SMTP id 38308e7fff4ca-37a1094a526mr2940101fa.16.1761782511202; Wed, 29 Oct 2025 17:01:51 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Thu, 30 Oct 2025 13:01:39 +1300 X-Gm-Features: AWmQ_bnDqqQjhsTWnxNJ-6R3hq7HnT1uUZQS8VFesNd7Vjq0qiFNODnCpUh_Quo Message-ID: Subject: Re: Postgres memoizing inner loop of join when outer loop join key is guaranteed unique? To: Jacob Jackson Cc: pgsql-general@lists.postgresql.org 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, 30 Oct 2025 at 07:29, Jacob Jackson wrot= e: > Hello. I was looking at some query plans recently and noticed something t= hat didn't make sense. I have a query that joins a table of questions with = results for each question (using a table with a composite primary key of qu= estion id and a user id), filtered by user id. The question IDs and the com= bined question-userIds are guaranteed unique due to being primary keys, and= yet Postgres still memoizes the inner loop results. Any ideas why? I agree that when the outer side of the join has unique values that Memoize does not make any sense. The planner currenrly puts quite a bit of faith in the row estimates for this and if you're getting this plan, then the estimates came back indicating there'd be fewer unique values of "QuestionUserStatus".question in the input than there are input rows to the Memoize node. If you delve into cost_memoize_rescan(), you'll see the code for this (look for where "hit_ratio" is calculated). There are also a few prechecks in get_memoize_path() to try to avoid this sort of thing, but unfortunately, the information to avoid Memoize when the outer side of the join is unique isn't available. We do have an "inner_unique" in JoinPathExtraData, but what we'd need for this and don't have is "outer_unique". If we had that, we could just exit early in get_memoize_path() if that's set to true. Whether or not going to the trouble of calculating "outer_unique" is worth the trouble, I'm not sure. There was some work on UniqueKeys a few years ago, which could have helped in this scenario as we could have more easily identified uniqueness at different join levels. That's no longer being worked on, as I understand it. On the other hand, it may be better to somehow enhance estimate_num_groups() so it can be given more details about the context of the request, i.e the set of Relids that are joined already for the input_rows. That way the code could do more analysis into the RelOptInfo base quals for the relevant relations. Extended statistics for n_distinct could also be applied in some cases too by looking for baserestrictinfo with equality quals or EquivalenceClasses with ec_has_const =3D true and a member for other Vars/Exprs in the extended statistics. Unfortunately, neither of these is a trivial fix. David