Skip to content

Commit a87ee00

Browse files
committed
Quick hack to allow the outer query's tuple_fraction to be passed down
to a subquery if the outer query is simple enough that the LIMIT can be reflected directly to the subquery. This didn't use to be very interesting, because a subquery that couldn't have been flattened into the upper query was usually not going to be very responsive to tuple_fraction anyway. But with new code that allows UNION ALL subqueries to pay attention to tuple_fraction, this is useful to do. In particular this lets the optimization occur when the UNION ALL is directly inside a view.
1 parent 453d74b commit a87ee00

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

src/backend/optimizer/path/allpaths.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.133 2005/06/09 04:18:59 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.134 2005/06/10 03:32:21 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -353,6 +353,28 @@ set_inherited_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
353353
set_cheapest(rel);
354354
}
355355

356+
/* quick-and-dirty test to see if any joining is needed */
357+
static bool
358+
has_multiple_baserels(PlannerInfo *root)
359+
{
360+
int num_base_rels = 0;
361+
Index rti;
362+
363+
for (rti = 1; rti < root->base_rel_array_size; rti++)
364+
{
365+
RelOptInfo *brel = root->base_rel_array[rti];
366+
367+
if (brel == NULL)
368+
continue;
369+
370+
/* ignore RTEs that are "other rels" */
371+
if (brel->reloptkind == RELOPT_BASEREL)
372+
if (++num_base_rels > 1)
373+
return true;
374+
}
375+
return false;
376+
}
377+
356378
/*
357379
* set_subquery_pathlist
358380
* Build the (single) access path for a subquery RTE
@@ -361,8 +383,10 @@ static void
361383
set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
362384
Index rti, RangeTblEntry *rte)
363385
{
386+
Query *parse = root->parse;
364387
Query *subquery = rte->subquery;
365388
bool *differentTypes;
389+
double tuple_fraction;
366390
List *pathkeys;
367391
List *subquery_pathkeys;
368392

@@ -416,8 +440,24 @@ set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
416440

417441
pfree(differentTypes);
418442

443+
/*
444+
* We can safely pass the outer tuple_fraction down to the subquery
445+
* if the outer level has no joining, aggregation, or sorting to do.
446+
* Otherwise we'd better tell the subquery to plan for full retrieval.
447+
* (XXX This could probably be made more intelligent ...)
448+
*/
449+
if (parse->hasAggs ||
450+
parse->groupClause ||
451+
parse->havingQual ||
452+
parse->distinctClause ||
453+
parse->sortClause ||
454+
has_multiple_baserels(root))
455+
tuple_fraction = 0.0; /* default case */
456+
else
457+
tuple_fraction = root->tuple_fraction;
458+
419459
/* Generate the plan for the subquery */
420-
rel->subplan = subquery_planner(subquery, 0.0 /* default case */,
460+
rel->subplan = subquery_planner(subquery, tuple_fraction,
421461
&subquery_pathkeys);
422462

423463
/* Copy number of output rows from subplan */

src/backend/optimizer/plan/planmain.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planmain.c,v 1.84 2005/06/08 23:02:04 tgl Exp $
17+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planmain.c,v 1.85 2005/06/10 03:32:23 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -79,6 +79,9 @@ query_planner(PlannerInfo *root, List *tlist, double tuple_fraction,
7979
Path *cheapestpath;
8080
Path *sortedpath;
8181

82+
/* Make tuple_fraction accessible to lower-level routines */
83+
root->tuple_fraction = tuple_fraction;
84+
8285
/*
8386
* If the query has an empty join tree, then it's something easy like
8487
* "SELECT 2+2;" or "INSERT ... VALUES()". Fall through quickly.

src/include/nodes/relation.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/nodes/relation.h,v 1.113 2005/06/09 04:19:00 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/nodes/relation.h,v 1.114 2005/06/10 03:32:25 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -92,6 +92,8 @@ typedef struct PlannerInfo
9292
List *query_pathkeys; /* desired pathkeys for query_planner(),
9393
* and actual pathkeys afterwards */
9494

95+
double tuple_fraction; /* tuple_fraction passed to query_planner */
96+
9597
bool hasJoinRTEs; /* true if any RTEs are RTE_JOIN kind */
9698
bool hasHavingQual; /* true if havingQual was non-null */
9799
} PlannerInfo;

0 commit comments

Comments
 (0)