Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mzLUF-0006eP-4S for pgsql-hackers@arkaria.postgresql.org; Mon, 20 Dec 2021 16:26:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mzLUC-0004tj-QM for pgsql-hackers@arkaria.postgresql.org; Mon, 20 Dec 2021 16:26:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mzLUC-0004ta-D9 for pgsql-hackers@lists.postgresql.org; Mon, 20 Dec 2021 16:26:04 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mzLU5-0004A9-80 for pgsql-hackers@lists.postgresql.org; Mon, 20 Dec 2021 16:26:03 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 1BKGPtoZ2445884; Mon, 20 Dec 2021 11:25:55 -0500 From: Tom Lane To: Robert Haas cc: Justin Pryzby , PostgreSQL Hackers Subject: Re: sqlsmith: ERROR: XX000: bogus varno: 2 In-reply-to: References: <20211219205422.GT17618@telsasoft.com> <2209221.1639948636@sss.pgh.pa.us> Comments: In-reply-to Robert Haas message dated "Mon, 20 Dec 2021 09:49:09 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <2445354.1640017418.0@sss.pgh.pa.us> Date: Mon, 20 Dec 2021 11:25:55 -0500 Message-ID: <2445883.1640017555@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: <2445354.1640017418.1@sss.pgh.pa.us> Robert Haas writes: > On Sun, Dec 19, 2021 at 4:17 PM Tom Lane wrote: >> I don't see anything particularly surprising here. pg_get_expr is only >> able to cope with expression trees over a single relation, but ON UPDATE >> rules can refer to both OLD and NEW relations. Maybe we could make the >> error message more friendly, but there's not much else to be done, >> I think. > +1 for making the error message more friendly. The problem is that the spot where it's thrown doesn't have a lot of context. We can fix that by having pg_get_expr itself check for out-of-spec Vars before starting the recursion, which adds a bit of overhead but I don't think we're terribly concerned about that. I figured this would be just a quick hack in ruleutils.c, but was dismayed to find the regression tests falling over, because some bozo neglected to teach nodeFuncs.c about partition expressions. It might be a good idea to back-patch that part, before we find some other place that fails. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="check-for-bogus-vars.patch"; charset="us-ascii" Content-ID: <2445354.1640017418.2@sss.pgh.pa.us> Content-Description: check-for-bogus-vars.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c index e276264882..5d4700430c 100644 --- a/src/backend/nodes/nodeFuncs.c +++ b/src/backend/nodes/nodeFuncs.c @@ -2201,6 +2201,26 @@ expression_tree_walker(Node *node, return true; } break; + case T_PartitionBoundSpec: + { + PartitionBoundSpec *pbs =3D (PartitionBoundSpec *) node; + + if (walker(pbs->listdatums, context)) + return true; + if (walker(pbs->lowerdatums, context)) + return true; + if (walker(pbs->upperdatums, context)) + return true; + } + break; + case T_PartitionRangeDatum: + { + PartitionRangeDatum *prd =3D (PartitionRangeDatum *) node; + + if (walker(prd->value, context)) + return true; + } + break; case T_List: foreach(temp, (List *) node) { @@ -3092,6 +3112,28 @@ expression_tree_mutator(Node *node, return (Node *) newnode; } break; + case T_PartitionBoundSpec: + { + PartitionBoundSpec *pbs =3D (PartitionBoundSpec *) node; + PartitionBoundSpec *newnode; + + FLATCOPY(newnode, pbs, PartitionBoundSpec); + MUTATE(newnode->listdatums, pbs->listdatums, List *); + MUTATE(newnode->lowerdatums, pbs->lowerdatums, List *); + MUTATE(newnode->upperdatums, pbs->upperdatums, List *); + return (Node *) newnode; + } + break; + case T_PartitionRangeDatum: + { + PartitionRangeDatum *prd =3D (PartitionRangeDatum *) node; + PartitionRangeDatum *newnode; + + FLATCOPY(newnode, prd, PartitionRangeDatum); + MUTATE(newnode->value, prd->value, Node *); + return (Node *) newnode; + } + break; case T_List: { /* diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/rul= eutils.c index 8da525c715..5d9596d445 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -2622,6 +2625,7 @@ static text * pg_get_expr_worker(text *expr, Oid relid, const char *relname, int pretty= Flags) { Node *node; + Relids relids; List *context; char *exprstr; char *str; @@ -2634,6 +2638,27 @@ pg_get_expr_worker(text *expr, Oid relid, const cha= r *relname, int prettyFlags) = pfree(exprstr); = + /* + * Throw a user-friendly error if the expression contains Vars we won't = be + * able to deparse. pull_varnos requires a PlannerInfo, but fortunately + * that can be dummy -- it need only contain an empty placeholder_list. + */ + relids =3D pull_varnos(makeNode(PlannerInfo), node); + if (OidIsValid(relid)) + { + if (!bms_is_subset(relids, bms_make_singleton(1))) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("expression contains variables of more than one relation")))= ; + } + else + { + if (!bms_is_empty(relids)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("expression contains variables"))); + } + /* Prepare deparse context if needed */ if (OidIsValid(relid)) context =3D deparse_context_for(relname, relid); ------- =_aaaaaaaaaa0--