From c24beb3f1a74a9d3d485b85ea2a3026f71674aaa Mon Sep 17 00:00:00 2001 From: Amit Langote Date: Wed, 6 Sep 2023 17:54:11 +0900 Subject: [PATCH v47 6/8] Set inFromCl to false in child table RTEs This is to allow the executor be able to distinguish tables that are directly mentioned in the query from those that get added to the query during planning. A subsequent commit will teach the executor to lock only the tables of the latter kind when executing a cached plan. Discussion: https://postgr.es/m/CA+HiwqFGkMSge6TgC9KQzde0ohpAycLQuV7ooitEEpbKB0O_mg@mail.gmail.comk --- src/backend/optimizer/util/inherit.c | 6 ++++++ src/backend/parser/analyze.c | 7 +++---- src/include/nodes/parsenodes.h | 9 +++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/backend/optimizer/util/inherit.c b/src/backend/optimizer/util/inherit.c index 94de855a22..9bac07bf40 100644 --- a/src/backend/optimizer/util/inherit.c +++ b/src/backend/optimizer/util/inherit.c @@ -492,6 +492,12 @@ expand_single_inheritance_child(PlannerInfo *root, RangeTblEntry *parentrte, } else childrte->inh = false; + /* + * Mark child tables as not being directly mentioned in the query. This + * allows the executor's ExecGetRangeTableRelation() to conveniently + * identify it as an inheritance child table. + */ + childrte->inFromCl = false; childrte->securityQuals = NIL; /* diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 7a1dfb6364..cf269f8c53 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -3305,10 +3305,9 @@ transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc, /* * Lock all regular tables used in query and its subqueries. We * examine inFromCl to exclude auto-added RTEs, particularly NEW/OLD - * in rules. This is a bit of an abuse of a mostly-obsolete flag, but - * it's convenient. We can't rely on the namespace mechanism that has - * largely replaced inFromCl, since for example we need to lock - * base-relation RTEs even if they are masked by upper joins. + * in rules. We can't rely on the namespace mechanism since for + * example we need to lock base-relation RTEs even if they are masked + * by upper joins. */ i = 0; foreach(rt, qry->rtable) diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index fef4c714b8..d875e11192 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -994,11 +994,16 @@ typedef struct PartitionCmd * * inFromCl marks those range variables that are listed in the FROM clause. * It's false for RTEs that are added to a query behind the scenes, such - * as the NEW and OLD variables for a rule, or the subqueries of a UNION. + * as the NEW and OLD variables for a rule, or the subqueries of a UNION, + * or the RTEs of inheritance child tables that are added by the planner. * This flag is not used during parsing (except in transformLockingClause, * q.v.); the parser now uses a separate "namespace" data structure to * control visibility. But it is needed by ruleutils.c to determine - * whether RTEs should be shown in decompiled queries. + * whether RTEs should be shown in decompiled queries. It is used by the + * executor to determine that a given RTE_RELATION entry belongs to a table + * directly mentioned in the query or to a child table added by the planner. + * It needs to know that for the case where the child tables in a plan need + * to be locked. * * securityQuals is a list of security barrier quals (boolean expressions), * to be tested in the listed order before returning a row from the -- 2.35.3