Skip to content

Commit 09a8912

Browse files
committed
Ensure clause_selectivity() behaves sanely when examining an uplevel Var
or a Var that references a subquery output.
1 parent 4ad9fe4 commit 09a8912

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

src/backend/optimizer/path/clausesel.c

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/clausesel.c,v 1.39 2000/08/13 02:50:04 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/clausesel.c,v 1.40 2000/10/25 21:48:12 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -371,24 +371,40 @@ clause_selectivity(Query *root,
371371
return s1;
372372
if (IsA(clause, Var))
373373
{
374+
Var *var = (Var *) clause;
374375

375376
/*
376-
* we have a bool Var. This is exactly equivalent to the clause:
377-
* reln.attribute = 't' so we compute the selectivity as if that
378-
* is what we have. The magic #define constants are a hack. I
379-
* didn't want to have to do system cache look ups to find out all
380-
* of that info.
377+
* We probably shouldn't ever see an uplevel Var here, but if we
378+
* do, return the default selectivity...
381379
*/
382-
Index varno = ((Var *) clause)->varno;
383-
384-
if (varRelid == 0 || varRelid == (int) varno)
385-
s1 = restriction_selectivity(F_EQSEL,
386-
BooleanEqualOperator,
387-
getrelid(varno, root->rtable),
388-
((Var *) clause)->varattno,
389-
BoolGetDatum(true),
390-
SEL_CONSTANT | SEL_RIGHT);
391-
/* an outer-relation bool var is taken as always true... */
380+
if (var->varlevelsup == 0 &&
381+
(varRelid == 0 || varRelid == (int) var->varno))
382+
{
383+
RangeTblEntry *rte = rt_fetch(var->varno, root->rtable);
384+
385+
if (rte->subquery)
386+
{
387+
/*
388+
* XXX not smart about subquery references...
389+
* any way to do better?
390+
*/
391+
s1 = 0.5;
392+
}
393+
else
394+
{
395+
/*
396+
* A Var at the top of a clause must be a bool Var.
397+
* This is equivalent to the clause reln.attribute = 't',
398+
* so we compute the selectivity as if that is what we have.
399+
*/
400+
s1 = restriction_selectivity(F_EQSEL,
401+
BooleanEqualOperator,
402+
rte->relid,
403+
var->varattno,
404+
BoolGetDatum(true),
405+
SEL_CONSTANT | SEL_RIGHT);
406+
}
407+
}
392408
}
393409
else if (IsA(clause, Param))
394410
{

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