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 1xKGmu-00000000lZK-0gZo for pgsql-bugs@arkaria.postgresql.org; Fri, 23 Oct 2026 15:02:33 +0000 Received: from localhost ([127.0.0.1]:51140 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xKGms-0000000GH5R-1RUG for pgsql-bugs@arkaria.postgresql.org; Fri, 23 Oct 2026 15:02:30 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:57678) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xKGms-0000000GH5I-0JZv for pgsql-bugs@lists.postgresql.org; Fri, 23 Oct 2026 15:02:30 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xKGmp-00000000ZyK-3xxg for pgsql-bugs@lists.postgresql.org; Fri, 23 Oct 2026 15:02:29 +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 69NF2J482962070; Fri, 23 Oct 2026 11:02:19 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 sss.pgh.pa.us 69NF2J482962070 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sss.pgh.pa.us; s=default; t=1792767739; bh=Z6mDC78I62H7XCH1DaiUbPF3OvaodhS6wpLTI1iRFZ8=; h=From:To:cc:Subject:In-reply-to:References:Date:From; b=rzxfKlMCF8qpQgkIJ9GJYQxmE2lLy+2mtimmD0kzMWgOyfx5pmQ9hq93wKbylxstq 40tRHDf1PgRm5OQGykXNBEqVpXB+/7SVUCHWZCcgS9ZkwvGRJHykVrWKLMpGn2q4if jYRwJrw0BikURKOKtfmvTARkmo1LXQgK6k68eGcQ= From: Tom Lane To: shihao zhong cc: alpkeles99@gmail.com, pgsql-bugs@lists.postgresql.org, Richard Guo 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> Comments: In-reply-to shihao zhong message dated "Thu, 22 Oct 2026 22:32:47 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <2962047.1792767715.0@sss.pgh.pa.us> Date: Fri, 23 Oct 2026 11:02:19 -0400 Message-ID: <2962069.1792767739@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2962047.1792767715.1@sss.pgh.pa.us> shihao zhong writes: >> What is bothering me a bit here is that this is surely not the only >> estimator routine that calls examine_variable, so why don't we >> have other problems? > It does not fix this: > CREATE TABLE ei (a int4[]); > CREATE INDEX ON ei > ((((1 = ANY (a))::int + (2 = ANY (a))::int) <@ int4range(0, 5))); > SELECT * FROM ei; > ERROR: no relation entry for relid 1 Yeah, I thought there were probably other cases :-( > The attached patch on top of yours passes NULL as root to > cost_qual_eval_node() in find_simplified_clause(), like aba8f61c3 did > for cost_subplan(). It fixes all the attached cases. Surely this way madness lies. The change you make here seriously weakens our estimation abilities in normal cases, and it offers no particular guarantee that there aren't other code paths (now or in the future) that can trigger the same problem. I think the only reasonably bulletproof answer is to change examine_variable() to tolerate not finding a RelOptInfo. That allows us to take out the hack from 77d0e82e5, though I left its test case. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="allow-examine_variable-before-baserels-are-built.patch"; charset="us-ascii" Content-ID: <2962047.1792767715.2@sss.pgh.pa.us> Content-Description: allow-examine_variable-before-baserels-are-built.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/self= uncs.c index 5ee377595bd..0dfe39a88f5 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -2266,18 +2266,6 @@ estimate_array_length(PlannerInfo *root, Node *arra= yexpr) AttStatsSlot sslot; double nelem =3D 0; = - /* - * Skip calling examine_variable for Var with varno 0, which has no - * valid relation entry and would error in find_base_rel. Such a Var - * can appear when a nested set operation's output type doesn't match - * the parent's expected type, because recurse_set_operations builds a - * projection target list using generate_setop_tlist with varno 0, and - * if the required type coercion involves an ArrayCoerceExpr, we can - * be called on that Var. - */ - if (IsA(arrayexpr, Var) && ((Var *) arrayexpr)->varno =3D=3D 0) - return 10; /* default guess, should match scalararraysel */ - examine_variable(root, arrayexpr, 0, &vardata); if (HeapTupleIsValid(vardata.statsTuple)) { @@ -5645,6 +5633,10 @@ ReleaseDummy(HeapTuple tuple) * statistic_proc_security_check(). * * Caller is responsible for doing ReleaseVariableStats() before exiting. + * + * In some code paths this can be called before we've set up RelOptInfos + * for the query's relations. Therefore, we use find_base_rel_noerr() + * and don't complain if it returns NULL. */ void examine_variable(PlannerInfo *root, Node *node, int varRelid, @@ -5683,16 +5675,21 @@ examine_variable(PlannerInfo *root, Node *node, in= t varRelid, (varRelid =3D=3D 0 || varRelid =3D=3D ((Var *) basenode)->varno)) { Var *var =3D (Var *) basenode; + RelOptInfo *rel =3D find_base_rel_noerr(root, var->varno); = - /* Set up result fields other than the stats tuple */ - vardata->var =3D basenode; /* return Var without phvs or relabeling */ - vardata->rel =3D find_base_rel(root, var->varno); - vardata->atttype =3D var->vartype; - vardata->atttypmod =3D var->vartypmod; - vardata->isunique =3D has_unique_index(vardata->rel, var->varattno); - - /* Try to locate some stats */ - examine_simple_variable(root, var, vardata); + if (rel) + { + /* Set up result fields other than the stats tuple */ + vardata->var =3D basenode; /* Var without phvs or relabeling */ + vardata->rel =3D rel; + vardata->atttype =3D var->vartype; + vardata->atttypmod =3D var->vartypmod; + vardata->isunique =3D has_unique_index(rel, var->varattno); + + /* Try to locate some stats */ + examine_simple_variable(root, var, vardata); + } + /* If we didn't find a rel, not much we can do */ = return; } @@ -5720,7 +5717,7 @@ examine_variable(PlannerInfo *root, Node *node, int = varRelid, { if (varRelid =3D=3D 0 || varRelid =3D=3D relid) { - onerel =3D find_base_rel(root, relid); + onerel =3D find_base_rel_noerr(root, relid); vardata->rel =3D onerel; node =3D basenode; /* strip any phvs or relabeling */ } @@ -5732,13 +5729,14 @@ examine_variable(PlannerInfo *root, Node *node, in= t varRelid, if (varRelid =3D=3D 0) { /* treat it as a variable of a join relation */ + /* find_join_rel returns null if no such join has been made */ vardata->rel =3D find_join_rel(root, varnos); node =3D basenode; /* strip any phvs or relabeling */ } else if (bms_is_member(varRelid, varnos)) { /* ignore the vars belonging to other relations */ - vardata->rel =3D find_base_rel(root, varRelid); + vardata->rel =3D find_base_rel_noerr(root, varRelid); node =3D basenode; /* strip any phvs or relabeling */ /* note: no point in expressional-index search here */ } ------- =_aaaaaaaaaa0--