Skip to content

Commit 54f7f62

Browse files
committed
Fix thinko: cost_mergejoin must pay attention to which side of the
mergeclause is which when extracting selectivity info.
1 parent ed6986d commit 54f7f62

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/backend/optimizer/path/costsize.c

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* Portions Copyright (c) 1994, Regents of the University of California
4343
*
4444
* IDENTIFICATION
45-
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/costsize.c,v 1.81 2002/03/01 06:01:19 tgl Exp $
45+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/costsize.c,v 1.82 2002/03/01 20:50:20 tgl Exp $
4646
*
4747
*-------------------------------------------------------------------------
4848
*/
@@ -467,10 +467,11 @@ cost_sort(Path *path, Query *root,
467467
}
468468

469469
/*
470-
* Note: should we bother to assign a nonzero run_cost to reflect the
471-
* overhead of extracting tuples from the sort result? Probably not
472-
* worth worrying about.
470+
* Also charge a small amount (arbitrarily set equal to operator cost)
471+
* per extracted tuple.
473472
*/
473+
run_cost += cpu_operator_cost * tuples;
474+
474475
path->startup_cost = startup_cost;
475476
path->total_cost = startup_cost + run_cost;
476477
}
@@ -567,11 +568,12 @@ cost_mergejoin(Path *path, Query *root,
567568
Cost run_cost = 0;
568569
Cost cpu_per_tuple;
569570
RestrictInfo *firstclause;
571+
Var *leftvar;
570572
double outer_rows,
571573
inner_rows;
572574
double ntuples;
573-
Selectivity leftscan,
574-
rightscan;
575+
Selectivity outerscansel,
576+
innerscansel;
575577
Path sort_path; /* dummy for result of cost_sort */
576578

577579
if (!enable_mergejoin)
@@ -592,11 +594,24 @@ cost_mergejoin(Path *path, Query *root,
592594
mergejoinscansel(root, (Node *) firstclause->clause,
593595
&firstclause->left_mergescansel,
594596
&firstclause->right_mergescansel);
595-
leftscan = firstclause->left_mergescansel;
596-
rightscan = firstclause->right_mergescansel;
597597

598-
outer_rows = outer_path->parent->rows * leftscan;
599-
inner_rows = inner_path->parent->rows * rightscan;
598+
leftvar = get_leftop(firstclause->clause);
599+
Assert(IsA(leftvar, Var));
600+
if (intMember(leftvar->varno, outer_path->parent->relids))
601+
{
602+
/* left side of clause is outer */
603+
outerscansel = firstclause->left_mergescansel;
604+
innerscansel = firstclause->right_mergescansel;
605+
}
606+
else
607+
{
608+
/* left side of clause is inner */
609+
outerscansel = firstclause->right_mergescansel;
610+
innerscansel = firstclause->left_mergescansel;
611+
}
612+
613+
outer_rows = outer_path->parent->rows * outerscansel;
614+
inner_rows = inner_path->parent->rows * innerscansel;
600615

601616
/* cost of source data */
602617

@@ -616,13 +631,13 @@ cost_mergejoin(Path *path, Query *root,
616631
outer_path->parent->width);
617632
startup_cost += sort_path.startup_cost;
618633
run_cost += (sort_path.total_cost - sort_path.startup_cost)
619-
* leftscan;
634+
* outerscansel;
620635
}
621636
else
622637
{
623638
startup_cost += outer_path->startup_cost;
624639
run_cost += (outer_path->total_cost - outer_path->startup_cost)
625-
* leftscan;
640+
* outerscansel;
626641
}
627642

628643
if (innersortkeys) /* do we need to sort inner? */
@@ -635,13 +650,13 @@ cost_mergejoin(Path *path, Query *root,
635650
inner_path->parent->width);
636651
startup_cost += sort_path.startup_cost;
637652
run_cost += (sort_path.total_cost - sort_path.startup_cost)
638-
* rightscan;
653+
* innerscansel;
639654
}
640655
else
641656
{
642657
startup_cost += inner_path->startup_cost;
643658
run_cost += (inner_path->total_cost - inner_path->startup_cost)
644-
* rightscan;
659+
* innerscansel;
645660
}
646661

647662
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy