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.98.2) (envelope-from ) id 1xL5aK-00000001Ife-2g44 for pgsql-bugs@arkaria.postgresql.org; Sun, 25 Oct 2026 21:16:57 +0000 Received: from localhost ([127.0.0.1]:40398 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xL5aJ-000000018Cm-1WHF for pgsql-bugs@arkaria.postgresql.org; Sun, 25 Oct 2026 21:16:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]:46492) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xL5aJ-000000018Cc-0PWB for pgsql-bugs@lists.postgresql.org; Sun, 25 Oct 2026 21:16:55 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xL5aB-00000000x4F-3LfW for pgsql-bugs@lists.postgresql.org; Sun, 25 Oct 2026 21:16:49 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 69PLGjBu412691; Sun, 25 Oct 2026 17:16:45 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 sss.pgh.pa.us 69PLGjBu412691 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sss.pgh.pa.us; s=default; t=1792963005; bh=GUKJEw699YugjnBGEdHjDpVAXecSwTOAHdkD1FXArV0=; h=From:To:cc:Subject:In-reply-to:References:Date:From; b=jEHoksT/4Qn85uniI87fU4xr/ZOaDpuMhMeptsH6zF5XzfuPaRS3vFYxI4xcAYEYz c5bm25HE6jsGPMVOvaELDB2imJzDiOqv1tHkZ4ysjXxQ75cDQVyxzw/527T2jeLTbU JGXcQYhZxr+oJDGJytxI/dln3heM7WA/ID5AtIDU= From: Tom Lane To: shihao zhong cc: Richard Guo , alpkeles99@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19784: "no relation entry for relid 1" when costing = ANY () in a MinMaxAggPath In-reply-to: References: <19784-93f76236decd0656@postgresql.org> <2828180.1792700071@sss.pgh.pa.us> <2962069.1792767739@sss.pgh.pa.us> <3087487.1792771798@sss.pgh.pa.us> Comments: In-reply-to shihao zhong message dated "Sat, 24 Oct 2026 00:04:05 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <412689.1792963005.1@sss.pgh.pa.us> Date: Sun, 25 Oct 2026 17:16:45 -0400 Message-ID: <412690.1792963005@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk shihao zhong writes: >> So even with a fix that prevents examine_variable from failing, it >> seems likely that we are not managing to reduce index expressions the >> same as we will reduce query expressions later. > I think they are ok. The NullTest reduction in c925ad30b only needs the > RTE and array, not the RelOptInfo. And query quals are also folded before > any RelOptInfo exists. I'm not so sure :-(. Looking at the last example you showed, the failure occurs because when we do eval_const_expressions on the index expression, we call elem_contained_by_range_support to see if it can replace the <@ operator with something else. That calls cost_qual_eval_node, and that's what ultimately hits the examine_variable failure. However, even if we band-aid that to not fail, we are far from being okay. Because what elem_contained_by_range_support is going to do with the cost estimate is decide whether or not to transform the <@ operator to something else. Presumably, whoever wrote that code verified that it did something useful, so there must be code paths where it doesn't fail because the underlying RelOptInfo is available, and so more than one transformation behavior is possible. Now, it's a fundamental requirement of the optimizer that eval_const_expressions will give the same result on multiple copies of the same expression. Years ago that was safe because it didn't try to look at any statistical data, or indeed much of anything except the presented expression tree. But we've now injected variable statistics into its behavior, and that is a problem, not least because it's not obvious that elem_contained_by_range_support is doing something it shouldn't. (There are probably other functions doing the same, and even if we got rid of every case, people would keep trying to do that.) If we band-aid examine_variable to not fail depending on context, we will mask places that are not producing the same results, and I don't think we can expect to get away with that for long. So I think the only really acceptable answer here is going to be to fix things so we don't try to do eval_const_expressions until we've loaded up all the RelOptInfo statistical data. At the least, that'll require postponing simplification of index expressions until their baserels' data is available, as well as figuring out where we can postpone preprocess_minmax_aggregates to, plus whatever else turns out to be jumping the gun. That does not sound like it'll be a back-patchable fix. So maybe in the back branches we have to do what I posted upthread, but I'm not happy with it as a permanent fix. regards, tom lane