Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6fdc44b

Browse files
committed
Tweak querytree-dependency-extraction code so that columns of tables
that are explicitly JOINed are not considered dependencies unless theyare actually used in the query: mere presence in the joinaliasvarslist of a JOIN RTE doesn't count as being used. The patch touchesa number of files because I needed to generalize the API ofquery_tree_walker to support an additional flag bit, but the changesare otherwise quite small.
1 parentd634a59 commit6fdc44b

File tree

8 files changed

+89
-55
lines changed

8 files changed

+89
-55
lines changed

‎src/backend/catalog/dependency.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/dependency.c,v 1.9 2002/09/04 20:31:13 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/dependency.c,v 1.10 2002/09/11 14:48:54 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -684,6 +684,12 @@ recordDependencyOnExpr(const ObjectAddress *depender,
684684

685685
/*
686686
* Recursively search an expression tree for object references.
687+
*
688+
* Note: we avoid creating references to columns of tables that participate
689+
* in an SQL JOIN construct, but are not actually used anywhere in the query.
690+
* To do so, we do not scan the joinaliasvars list of a join RTE while
691+
* scanning the query rangetable, but instead scan each individual entry
692+
* of the alias list when we find a reference to it.
687693
*/
688694
staticbool
689695
find_expr_references_walker(Node*node,
@@ -716,10 +722,24 @@ find_expr_references_walker(Node *node,
716722
elog(ERROR,"find_expr_references_walker: bogus varno %d",
717723
var->varno);
718724
rte=rt_fetch(var->varno,rtable);
719-
/* If it's a plain relation, reference this column */
720725
if (rte->rtekind==RTE_RELATION)
726+
{
727+
/* If it's a plain relation, reference this column */
728+
/* NB: this code works for whole-row Var with attno 0, too */
721729
add_object_address(OCLASS_CLASS,rte->relid,var->varattno,
722730
&context->addrs);
731+
}
732+
elseif (rte->rtekind==RTE_JOIN)
733+
{
734+
/* Scan join output column to add references to join inputs */
735+
if (var->varattno <=0||
736+
var->varattno>length(rte->joinaliasvars))
737+
elog(ERROR,"find_expr_references_walker: bogus varattno %d",
738+
var->varattno);
739+
find_expr_references_walker((Node*)nth(var->varattno-1,
740+
rte->joinaliasvars),
741+
context);
742+
}
723743
return false;
724744
}
725745
if (IsA(node,Expr))
@@ -766,7 +786,7 @@ find_expr_references_walker(Node *node,
766786
* Add whole-relation refs for each plain relation mentioned in
767787
* the subquery's rtable. (Note: query_tree_walker takes care of
768788
* recursing into RTE_FUNCTION and RTE_SUBQUERY RTEs, so no need
769-
* to do that here.)
789+
* to do that here. But keep it from looking at join alias lists.)
770790
*/
771791
foreach(rtable,query->rtable)
772792
{
@@ -781,7 +801,8 @@ find_expr_references_walker(Node *node,
781801
context->rtables=lcons(query->rtable,context->rtables);
782802
result=query_tree_walker(query,
783803
find_expr_references_walker,
784-
(void*)context, true);
804+
(void*)context,
805+
QTW_IGNORE_JOINALIASES);
785806
context->rtables=lnext(context->rtables);
786807
returnresult;
787808
}

‎src/backend/optimizer/prep/prepunion.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.78 2002/09/04 20:31:22 momjian Exp $
17+
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.79 2002/09/11 14:48:55 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -774,7 +774,7 @@ adjust_inherited_attrs(Node *node,
774774
if (newnode->resultRelation==old_rt_index)
775775
newnode->resultRelation=new_rt_index;
776776
query_tree_mutator(newnode,adjust_inherited_attrs_mutator,
777-
(void*)&context,false);
777+
(void*)&context,QTW_IGNORE_SUBQUERIES);
778778
return (Node*)newnode;
779779
}
780780
else

‎src/backend/optimizer/util/clauses.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.108 2002/09/04 20:31:22 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.109 2002/09/11 14:48:54 tgl Exp $
1212
*
1313
* HISTORY
1414
* AUTHORDATEMAJOR EVENT
@@ -1788,7 +1788,7 @@ simplify_op_or_func(Expr *expr, List *args)
17881788
*{
17891789
*adjust context for subquery;
17901790
*result = query_tree_walker((Query *) node, my_walker, context,
1791-
*true); // to visitsubquery RTEs too
1791+
*0); // to visitrtable items too
17921792
*restore context if needed;
17931793
*return result;
17941794
*}
@@ -1994,16 +1994,17 @@ expression_tree_walker(Node *node,
19941994
* walker intends to descend into subqueries. It is also useful for
19951995
* descending into subqueries within a walker.
19961996
*
1997-
* If visitQueryRTEs is true, the walker will also be called on sub-Query
1998-
* nodes present in subquery rangetable entries of the given Query. This
1999-
* is optional since some callers handle those sub-queries separately,
2000-
* or don't really want to see subqueries anyway.
1997+
* Some callers want to suppress visitation of certain items in the sub-Query,
1998+
* typically because they need to process them specially, or don't actually
1999+
* want to recurse into subqueries. This is supported by the flags argument,
2000+
* which is the bitwise OR of flag values to suppress visitation of
2001+
* indicated items. (More flag bits may be added as needed.)
20012002
*/
20022003
bool
20032004
query_tree_walker(Query*query,
20042005
bool (*walker) (),
20052006
void*context,
2006-
boolvisitQueryRTEs)
2007+
intflags)
20072008
{
20082009
List*rt;
20092010

@@ -2028,13 +2029,14 @@ query_tree_walker(Query *query,
20282029
/* nothing to do */
20292030
break;
20302031
caseRTE_SUBQUERY:
2031-
if (visitQueryRTEs)
2032+
if (! (flags&QTW_IGNORE_SUBQUERIES))
20322033
if (walker(rte->subquery,context))
20332034
return true;
20342035
break;
20352036
caseRTE_JOIN:
2036-
if (walker(rte->joinaliasvars,context))
2037-
return true;
2037+
if (! (flags&QTW_IGNORE_JOINALIASES))
2038+
if (walker(rte->joinaliasvars,context))
2039+
return true;
20382040
break;
20392041
caseRTE_FUNCTION:
20402042
if (walker(rte->funcexpr,context))
@@ -2388,16 +2390,17 @@ expression_tree_mutator(Node *node,
23882390
* if you don't want to change the original. All substructure is safely
23892391
* copied, however.
23902392
*
2391-
* If visitQueryRTEs is true, the mutator will also be called on sub-Query
2392-
* nodes present in subquery rangetable entries of the given Query. This
2393-
* is optional since some callers handle those sub-queries separately,
2394-
* or don't really want to see subqueries anyway.
2393+
* Some callers want to suppress mutating of certain items in the sub-Query,
2394+
* typically because they need to process them specially, or don't actually
2395+
* want to recurse into subqueries. This is supported by the flags argument,
2396+
* which is the bitwise OR of flag values to suppress mutating of
2397+
* indicated items. (More flag bits may be added as needed.)
23952398
*/
23962399
void
23972400
query_tree_mutator(Query*query,
23982401
Node*(*mutator) (),
23992402
void*context,
2400-
boolvisitQueryRTEs)
2403+
intflags)
24012404
{
24022405
List*newrt=NIL;
24032406
List*rt;
@@ -2420,7 +2423,7 @@ query_tree_mutator(Query *query,
24202423
/* nothing to do, don't bother to make a copy */
24212424
break;
24222425
caseRTE_SUBQUERY:
2423-
if (visitQueryRTEs)
2426+
if (! (flags&QTW_IGNORE_SUBQUERIES))
24242427
{
24252428
FLATCOPY(newrte,rte,RangeTblEntry);
24262429
CHECKFLATCOPY(newrte->subquery,rte->subquery,Query);
@@ -2429,9 +2432,12 @@ query_tree_mutator(Query *query,
24292432
}
24302433
break;
24312434
caseRTE_JOIN:
2432-
FLATCOPY(newrte,rte,RangeTblEntry);
2433-
MUTATE(newrte->joinaliasvars,rte->joinaliasvars,List*);
2434-
rte=newrte;
2435+
if (! (flags&QTW_IGNORE_JOINALIASES))
2436+
{
2437+
FLATCOPY(newrte,rte,RangeTblEntry);
2438+
MUTATE(newrte->joinaliasvars,rte->joinaliasvars,List*);
2439+
rte=newrte;
2440+
}
24352441
break;
24362442
caseRTE_FUNCTION:
24372443
FLATCOPY(newrte,rte,RangeTblEntry);

‎src/backend/optimizer/util/var.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/var.c,v 1.39 2002/09/04 20:31:22 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/var.c,v 1.40 2002/09/11 14:48:54 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -82,7 +82,7 @@ pull_varnos(Node *node)
8282
*/
8383
if (node&&IsA(node,Query))
8484
query_tree_walker((Query*)node,pull_varnos_walker,
85-
(void*)&context,true);
85+
(void*)&context,0);
8686
else
8787
pull_varnos_walker(node,&context);
8888

@@ -128,7 +128,7 @@ pull_varnos_walker(Node *node, pull_varnos_context *context)
128128

129129
context->sublevels_up++;
130130
result=query_tree_walker((Query*)node,pull_varnos_walker,
131-
(void*)context,true);
131+
(void*)context,0);
132132
context->sublevels_up--;
133133
returnresult;
134134
}
@@ -165,7 +165,7 @@ contain_var_reference(Node *node, int varno, int varattno, int levelsup)
165165
if (node&&IsA(node,Query))
166166
returnquery_tree_walker((Query*)node,
167167
contain_var_reference_walker,
168-
(void*)&context,true);
168+
(void*)&context,0);
169169
else
170170
returncontain_var_reference_walker(node,&context);
171171
}
@@ -212,7 +212,7 @@ contain_var_reference_walker(Node *node,
212212
context->sublevels_up++;
213213
result=query_tree_walker((Query*)node,
214214
contain_var_reference_walker,
215-
(void*)context,true);
215+
(void*)context,0);
216216
context->sublevels_up--;
217217
returnresult;
218218
}

‎src/backend/rewrite/rewriteDefine.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.79 2002/09/04 20:31:25 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.80 2002/09/11 14:48:54 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -464,9 +464,10 @@ setRuleCheckAsUser(Query *qry, Oid userid)
464464
}
465465

466466
/* If there are sublinks, search for them and process their RTEs */
467+
/* ignore subqueries in rtable because we already processed them */
467468
if (qry->hasSubLinks)
468469
query_tree_walker(qry,setRuleCheckAsUser_walker, (void*)&userid,
469-
false/* already did the ones in rtable */);
470+
QTW_IGNORE_SUBQUERIES);
470471
}
471472

472473
/*

‎src/backend/rewrite/rewriteHandler.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.108 2002/09/04 20:31:25 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.109 2002/09/11 14:48:54 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -822,11 +822,12 @@ fireRIRrules(Query *parsetree)
822822
}
823823

824824
/*
825-
* Recurse into sublink subqueries, too.
825+
* Recurse into sublink subqueries, too. But we already did the ones
826+
* in the rtable.
826827
*/
827828
if (parsetree->hasSubLinks)
828829
query_tree_walker(parsetree,fireRIRonSubLink,NULL,
829-
false/* already handled the ones in rtable */);
830+
QTW_IGNORE_SUBQUERIES);
830831

831832
/*
832833
* If the query was marked having aggregates, check if this is still

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp