]> granicus.if.org Git - postgresql/commitdiff
Tweak hash join code to use an additional heuristic for deciding whether
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 28 Nov 2005 23:46:25 +0000 (23:46 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 28 Nov 2005 23:46:25 +0000 (23:46 +0000)
it's worth probing the outer relation for emptiness before building the
hash table.  To wit, if we're rescanning a join previously performed,
remember whether we found it nonempty the previous time, and don't bother
with the probe if it was nonempty.  This buys back the performance lost
in examples like Mario Weilguni's.

src/backend/executor/nodeHashjoin.c
src/include/nodes/execnodes.h

index 0d84d458740faee7508ae1fd68674135cd983b56..4b82c7fcea80add776aa6daf8a554eb3ccd09f21 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/executor/nodeHashjoin.c,v 1.75.2.2 2005/11/28 17:14:47 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/executor/nodeHashjoin.c,v 1.75.2.3 2005/11/28 23:46:24 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -120,16 +120,28 @@ ExecHashJoin(HashJoinState *node)
                 * since we aren't going to be able to skip the join on the strength
                 * of an empty inner relation anyway.)
                 *
+                * If we are rescanning the join, we make use of information gained
+                * on the previous scan: don't bother to try the prefetch if the
+                * previous scan found the outer relation nonempty.  This is not
+                * 100% reliable since with new parameters the outer relation might
+                * yield different results, but it's a good heuristic.
+                *
                 * The only way to make the check is to try to fetch a tuple from the
                 * outer plan node.  If we succeed, we have to stash it away for later
                 * consumption by ExecHashJoinOuterGetTuple.
                 */
-               if (outerNode->plan->startup_cost < hashNode->ps.plan->total_cost ||
-                       node->js.jointype == JOIN_LEFT)
+               if (node->js.jointype == JOIN_LEFT ||
+                       (outerNode->plan->startup_cost < hashNode->ps.plan->total_cost &&
+                        !node->hj_OuterNotEmpty))
                {
                        node->hj_FirstOuterTupleSlot = ExecProcNode(outerNode);
                        if (TupIsNull(node->hj_FirstOuterTupleSlot))
+                       {
+                               node->hj_OuterNotEmpty = false;
                                return NULL;
+                       }
+                       else
+                               node->hj_OuterNotEmpty = true;
                }
                else
                        node->hj_FirstOuterTupleSlot = NULL;
@@ -159,6 +171,13 @@ ExecHashJoin(HashJoinState *node)
                 * scanning the outer relation
                 */
                hashtable->nbatch_outstart = hashtable->nbatch;
+
+               /*
+                * Reset OuterNotEmpty for scan.  (It's OK if we fetched a tuple
+                * above, because ExecHashJoinOuterGetTuple will immediately
+                * set it again.)
+                */
+               node->hj_OuterNotEmpty = false;
        }
 
        /*
@@ -454,6 +473,7 @@ ExecInitHashJoin(HashJoin *node, EState *estate)
        hjstate->js.ps.ps_TupFromTlist = false;
        hjstate->hj_NeedNewOuter = true;
        hjstate->hj_MatchedOuter = false;
+       hjstate->hj_OuterNotEmpty = false;
 
        return hjstate;
 }
@@ -546,6 +566,9 @@ ExecHashJoinOuterGetTuple(PlanState *outerNode,
                        *hashvalue = ExecHashGetHashValue(hashtable, econtext,
                                                                                          hjstate->hj_OuterHashKeys);
 
+                       /* remember outer relation is not empty for possible rescan */
+                       hjstate->hj_OuterNotEmpty = true;
+
                        return slot;
                }
 
@@ -810,7 +833,19 @@ ExecReScanHashJoin(HashJoinState *node, ExprContext *exprCtxt)
                if (node->hj_HashTable->nbatch == 1 &&
                        ((PlanState *) node)->righttree->chgParam == NULL)
                {
-                       /* okay to reuse the hash table; needn't rescan inner, either */
+                       /*
+                        * okay to reuse the hash table; needn't rescan inner, either.
+                        *
+                        * What we do need to do is reset our state about the emptiness
+                        * of the outer relation, so that the new scan of the outer will
+                        * update it correctly if it turns out to be empty this time.
+                        * (There's no harm in clearing it now because ExecHashJoin won't
+                        * need the info.  In the other cases, where the hash table
+                        * doesn't exist or we are destroying it, we leave this state
+                        * alone because ExecHashJoin will need it the first time
+                        * through.)
+                        */
+                       node->hj_OuterNotEmpty = false;
                }
                else
                {
index 0aead5234c0fec36bb716727e90a21449c24cfea..2c53f12ce5a0681a4c9fd4252060c335995096c6 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/nodes/execnodes.h,v 1.139.2.2 2005/11/22 18:23:28 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/nodes/execnodes.h,v 1.139.2.3 2005/11/28 23:46:25 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1101,6 +1101,7 @@ typedef struct MergeJoinState
  *             hj_FirstOuterTupleSlot  first tuple retrieved from outer plan
  *             hj_NeedNewOuter                 true if need new outer tuple on next call
  *             hj_MatchedOuter                 true if found a join match for current outer
+ *             hj_OuterNotEmpty                true if outer relation known not empty
  * ----------------
  */
 
@@ -1125,6 +1126,7 @@ typedef struct HashJoinState
        TupleTableSlot *hj_FirstOuterTupleSlot;
        bool            hj_NeedNewOuter;
        bool            hj_MatchedOuter;
+       bool            hj_OuterNotEmpty;
 } HashJoinState;