If we expect a hash join to be performed in multiple batches, suppress
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 26 Mar 2009 17:15:35 +0000 (17:15 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 26 Mar 2009 17:15:35 +0000 (17:15 +0000)
"physical tlist" optimization on the outer relation (ie, force a projection
step to occur in its scan).  This avoids storing useless column values when
the outer relation's tuples are written to temporary batch files.

Modified version of a patch by Michael Henderson and Ramon Lawrence.

src/backend/nodes/outfuncs.c
src/backend/optimizer/path/costsize.c
src/backend/optimizer/plan/createplan.c
src/backend/optimizer/util/pathnode.c
src/include/nodes/relation.h

index e715a2128d9431f2f28bffe11e1088be9cf19b0b..68a44339da5d62b46bb9f356827d83eea48a06fd 100644 (file)
@@ -1448,6 +1448,7 @@ _outHashPath(StringInfo str, HashPath *node)
        _outJoinPathInfo(str, (JoinPath *) node);
 
        WRITE_NODE_FIELD(path_hashclauses);
+       WRITE_INT_FIELD(num_batches);
 }
 
 static void
index 58beefe4a79b2872206624ef3a9543794d7d1690..0ef6acd59f66a1925710159f60905bd9288a5b56 100644 (file)
@@ -1880,6 +1880,8 @@ cost_hashjoin(HashPath *path, PlannerInfo *root, SpecialJoinInfo *sjinfo)
                                                        &numbatches,
                                                        &num_skew_mcvs);
        virtualbuckets = (double) numbuckets *(double) numbatches;
+       /* mark the path with estimated # of batches */
+       path->num_batches = numbatches;
 
        /*
         * Determine bucketsize fraction for inner relation.  We use the smallest
index 90fe45e8593b3ae079f45cf56592aa3a4336c00a..64b0209856df91d0224fb0eb974871ca0fea2d2e 100644 (file)
@@ -1910,6 +1910,10 @@ create_hashjoin_plan(PlannerInfo *root,
        /* We don't want any excess columns in the hashed tuples */
        disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
 
+       /* If we expect batching, suppress excess columns in outer tuples too */
+       if (best_path->num_batches > 1)
+               disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
+
        /*
         * If there is a single join clause and we can identify the outer
         * variable as a simple column reference, supply its identity for
index 7ecdfda24f91d29a1c49043133ffea684662692b..e3f3f2c2f073dd4b62cb10b004257c2efd9d1fe9 100644 (file)
@@ -1480,9 +1480,20 @@ create_hashjoin_path(PlannerInfo *root,
        pathnode->jpath.outerjoinpath = outer_path;
        pathnode->jpath.innerjoinpath = inner_path;
        pathnode->jpath.joinrestrictinfo = restrict_clauses;
-       /* A hashjoin never has pathkeys, since its ordering is unpredictable */
+       /*
+        * A hashjoin never has pathkeys, since its output ordering is
+        * unpredictable due to possible batching.  XXX If the inner relation is
+        * small enough, we could instruct the executor that it must not batch,
+        * and then we could assume that the output inherits the outer relation's
+        * ordering, which might save a sort step.  However there is considerable
+        * downside if our estimate of the inner relation size is badly off.
+        * For the moment we don't risk it.  (Note also that if we wanted to take
+        * this seriously, joinpath.c would have to consider many more paths for
+        * the outer rel than it does now.)
+        */
        pathnode->jpath.path.pathkeys = NIL;
        pathnode->path_hashclauses = hashclauses;
+       /* cost_hashjoin will fill in pathnode->num_batches */
 
        cost_hashjoin(pathnode, root, sjinfo);
 
index d4b4a76e5d6185e4e7e86874c439e7d96fe92daf..fa0e67617b40f9f7c082f56c11a07a36407cc0c8 100644 (file)
@@ -845,6 +845,7 @@ typedef struct HashPath
 {
        JoinPath        jpath;
        List       *path_hashclauses;           /* join clauses used for hashing */
+       int                     num_batches;                    /* number of batches expected */
 } HashPath;
 
 /*