1999-11-23 21:07:06 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* tidpath.c
|
|
|
|
* Routines to determine which tids are usable for scanning a
|
|
|
|
* given relation, and create TidPaths accordingly.
|
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1999-11-23 21:07:06 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2000-05-30 02:49:57 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/tidpath.c,v 1.7 2000/05/30 00:49:47 momjian Exp $
|
1999-11-23 21:07:06 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "catalog/pg_operator.h"
|
|
|
|
#include "optimizer/clauses.h"
|
|
|
|
#include "optimizer/cost.h"
|
|
|
|
#include "optimizer/pathnode.h"
|
|
|
|
#include "optimizer/paths.h"
|
|
|
|
#include "parser/parse_coerce.h"
|
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
static void create_tidscan_joinpaths(RelOptInfo *rel);
|
2000-04-12 19:17:23 +02:00
|
|
|
static List *TidqualFromRestrictinfo(List *relids, List *restrictinfo);
|
|
|
|
static bool isEvaluable(int varno, Node *node);
|
|
|
|
static Node *TidequalClause(int varno, Expr *node);
|
|
|
|
static List *TidqualFromExpr(int varno, Expr *expr);
|
1999-11-23 21:07:06 +01:00
|
|
|
|
|
|
|
static
|
2000-04-12 19:17:23 +02:00
|
|
|
bool
|
|
|
|
isEvaluable(int varno, Node *node)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
List *lst;
|
|
|
|
Expr *expr;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
if (IsA(node, Const))
|
|
|
|
return true;
|
|
|
|
if (IsA(node, Param))
|
|
|
|
return true;
|
1999-11-23 21:07:06 +01:00
|
|
|
if (IsA(node, Var))
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
Var *var = (Var *) node;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
|
|
|
if (var->varno == varno)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!is_funcclause(node))
|
|
|
|
return false;
|
|
|
|
expr = (Expr *) node;
|
|
|
|
foreach(lst, expr->args)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
|
|
|
if (!isEvaluable(varno, lfirst(lst)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The 2nd parameter should be an opclause
|
|
|
|
* Extract the right node if the opclause is CTID= ....
|
2000-04-12 19:17:23 +02:00
|
|
|
* or the left node if the opclause is ....=CTID
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
|
|
|
static
|
2000-04-12 19:17:23 +02:00
|
|
|
Node *
|
|
|
|
TidequalClause(int varno, Expr *node)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
Node *rnode = 0,
|
|
|
|
*arg1,
|
|
|
|
*arg2,
|
|
|
|
*arg;
|
|
|
|
Oper *oper;
|
|
|
|
Var *var;
|
|
|
|
Const *aconst;
|
|
|
|
Param *param;
|
|
|
|
Expr *expr;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!node->oper)
|
|
|
|
return rnode;
|
|
|
|
if (!node->args)
|
|
|
|
return rnode;
|
|
|
|
if (length(node->args) != 2)
|
|
|
|
return rnode;
|
|
|
|
oper = (Oper *) node->oper;
|
1999-11-23 21:07:06 +01:00
|
|
|
if (oper->opno != TIDEqualOperator)
|
|
|
|
return rnode;
|
|
|
|
arg1 = lfirst(node->args);
|
|
|
|
arg2 = lsecond(node->args);
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
arg = (Node *) 0;
|
1999-11-23 21:07:06 +01:00
|
|
|
if (IsA(arg1, Var))
|
|
|
|
{
|
|
|
|
var = (Var *) arg1;
|
|
|
|
if (var->varno == varno &&
|
2000-04-12 19:17:23 +02:00
|
|
|
var->varattno == SelfItemPointerAttributeNumber &&
|
|
|
|
var->vartype == TIDOID)
|
1999-11-23 21:07:06 +01:00
|
|
|
arg = arg2;
|
|
|
|
else if (var->varnoold == varno &&
|
2000-04-12 19:17:23 +02:00
|
|
|
var->varoattno == SelfItemPointerAttributeNumber &&
|
|
|
|
var->vartype == TIDOID)
|
1999-11-23 21:07:06 +01:00
|
|
|
arg = arg2;
|
|
|
|
}
|
|
|
|
if ((!arg) && IsA(arg2, Var))
|
|
|
|
{
|
|
|
|
var = (Var *) arg2;
|
|
|
|
if (var->varno == varno &&
|
2000-04-12 19:17:23 +02:00
|
|
|
var->varattno == SelfItemPointerAttributeNumber &&
|
|
|
|
var->vartype == TIDOID)
|
1999-11-23 21:07:06 +01:00
|
|
|
arg = arg1;
|
|
|
|
}
|
|
|
|
if (!arg)
|
|
|
|
return rnode;
|
|
|
|
switch (nodeTag(arg))
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
case T_Const:
|
1999-11-23 21:07:06 +01:00
|
|
|
aconst = (Const *) arg;
|
|
|
|
if (aconst->consttype != TIDOID)
|
|
|
|
return rnode;
|
|
|
|
if (aconst->constbyval)
|
|
|
|
return rnode;
|
|
|
|
rnode = arg;
|
|
|
|
break;
|
2000-04-12 19:17:23 +02:00
|
|
|
case T_Param:
|
1999-11-23 21:07:06 +01:00
|
|
|
param = (Param *) arg;
|
|
|
|
if (param->paramtype != TIDOID)
|
|
|
|
return rnode;
|
|
|
|
rnode = arg;
|
|
|
|
break;
|
2000-04-12 19:17:23 +02:00
|
|
|
case T_Var:
|
1999-11-23 21:07:06 +01:00
|
|
|
var = (Var *) arg;
|
|
|
|
if (var->varno == varno ||
|
2000-04-12 19:17:23 +02:00
|
|
|
var->vartype != TIDOID)
|
1999-11-23 21:07:06 +01:00
|
|
|
return rnode;
|
|
|
|
rnode = arg;
|
|
|
|
break;
|
2000-04-12 19:17:23 +02:00
|
|
|
case T_Expr:
|
1999-11-23 21:07:06 +01:00
|
|
|
expr = (Expr *) arg;
|
2000-04-12 19:17:23 +02:00
|
|
|
if (expr->typeOid != TIDOID)
|
|
|
|
return rnode;
|
|
|
|
if (expr->opType != FUNC_EXPR)
|
|
|
|
return rnode;
|
|
|
|
if (isEvaluable(varno, (Node *) expr))
|
1999-11-23 21:07:06 +01:00
|
|
|
rnode = arg;
|
|
|
|
break;
|
2000-04-12 19:17:23 +02:00
|
|
|
default:
|
1999-11-23 21:07:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract the list of CTID values from a specified expr node.
|
|
|
|
* When the expr node is an or_clause,we try to extract CTID
|
|
|
|
* values from all member nodes. However we would discard them
|
|
|
|
* all if we couldn't extract CTID values from a member node.
|
|
|
|
* When the expr node is an and_clause,we return the list of
|
|
|
|
* CTID values if we could extract the CTID values from a member
|
|
|
|
* node.
|
2000-04-12 19:17:23 +02:00
|
|
|
*/
|
1999-11-23 21:07:06 +01:00
|
|
|
static
|
2000-04-12 19:17:23 +02:00
|
|
|
List *
|
|
|
|
TidqualFromExpr(int varno, Expr *expr)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
List *rlst = NIL,
|
|
|
|
*lst,
|
|
|
|
*frtn;
|
|
|
|
Node *node = (Node *) expr,
|
|
|
|
*rnode;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
|
|
|
if (is_opclause(node))
|
|
|
|
{
|
|
|
|
rnode = TidequalClause(varno, expr);
|
|
|
|
if (rnode)
|
|
|
|
rlst = lcons(rnode, rlst);
|
|
|
|
}
|
|
|
|
else if (and_clause(node))
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
foreach(lst, expr->args)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
|
|
|
node = lfirst(lst);
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!IsA(node, Expr))
|
1999-11-23 21:07:06 +01:00
|
|
|
continue;
|
2000-04-12 19:17:23 +02:00
|
|
|
rlst = TidqualFromExpr(varno, (Expr *) node);
|
1999-11-23 21:07:06 +01:00
|
|
|
if (rlst)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (or_clause(node))
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
foreach(lst, expr->args)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
|
|
|
node = lfirst(lst);
|
|
|
|
if (IsA(node, Expr) &&
|
2000-04-12 19:17:23 +02:00
|
|
|
(frtn = TidqualFromExpr(varno, (Expr *) node)))
|
1999-11-23 21:07:06 +01:00
|
|
|
rlst = nconc(rlst, frtn);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (rlst)
|
|
|
|
freeList(rlst);
|
|
|
|
rlst = NIL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rlst;
|
2000-04-12 19:17:23 +02:00
|
|
|
}
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
static List *
|
|
|
|
TidqualFromRestrictinfo(List *relids, List *restrictinfo)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
List *lst,
|
|
|
|
*rlst = NIL;
|
|
|
|
int varno;
|
|
|
|
Node *node;
|
|
|
|
Expr *expr;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
if (length(relids) != 1)
|
|
|
|
return NIL;
|
|
|
|
varno = lfirsti(relids);
|
2000-04-12 19:17:23 +02:00
|
|
|
foreach(lst, restrictinfo)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
|
|
|
node = lfirst(lst);
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!IsA(node, RestrictInfo))
|
|
|
|
continue;
|
|
|
|
expr = ((RestrictInfo *) node)->clause;
|
1999-11-23 21:07:06 +01:00
|
|
|
rlst = TidqualFromExpr(varno, expr);
|
|
|
|
if (rlst)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rlst;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create_tidscan_joinpaths
|
2000-02-15 21:49:31 +01:00
|
|
|
* Create innerjoin paths if there are suitable joinclauses.
|
1999-11-23 21:07:06 +01:00
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* XXX does this actually work?
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
static void
|
1999-11-23 21:07:06 +01:00
|
|
|
create_tidscan_joinpaths(RelOptInfo *rel)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
List *rlst = NIL,
|
|
|
|
*lst;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
foreach(lst, rel->joininfo)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2000-02-15 21:49:31 +01:00
|
|
|
JoinInfo *joininfo = (JoinInfo *) lfirst(lst);
|
2000-04-12 19:17:23 +02:00
|
|
|
List *restinfo,
|
|
|
|
*tideval;
|
2000-01-09 01:26:47 +01:00
|
|
|
|
1999-11-23 21:07:06 +01:00
|
|
|
restinfo = joininfo->jinfo_restrictinfo;
|
|
|
|
tideval = TidqualFromRestrictinfo(rel->relids, restinfo);
|
2000-01-09 01:26:47 +01:00
|
|
|
if (length(tideval) == 1)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
TidPath *pathnode = makeNode(TidPath);
|
1999-11-23 21:07:06 +01:00
|
|
|
|
|
|
|
pathnode->path.pathtype = T_TidScan;
|
|
|
|
pathnode->path.parent = rel;
|
|
|
|
pathnode->path.pathkeys = NIL;
|
|
|
|
pathnode->tideval = tideval;
|
|
|
|
pathnode->unjoined_relids = joininfo->unjoined_relids;
|
2000-02-15 21:49:31 +01:00
|
|
|
|
|
|
|
cost_tidscan(&pathnode->path, rel, tideval);
|
|
|
|
|
1999-11-23 21:07:06 +01:00
|
|
|
rlst = lappend(rlst, pathnode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rel->innerjoin = nconc(rel->innerjoin, rlst);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create_tidscan_paths
|
2000-02-15 21:49:31 +01:00
|
|
|
* Creates paths corresponding to tid direct scans of the given rel.
|
|
|
|
* Candidate paths are added to the rel's pathlist (using add_path).
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
void
|
1999-11-23 21:07:06 +01:00
|
|
|
create_tidscan_paths(Query *root, RelOptInfo *rel)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
List *tideval = TidqualFromRestrictinfo(rel->relids,
|
|
|
|
rel->baserestrictinfo);
|
|
|
|
|
1999-11-23 21:07:06 +01:00
|
|
|
if (tideval)
|
2000-02-15 21:49:31 +01:00
|
|
|
add_path(rel, (Path *) create_tidscan_path(rel, tideval));
|
1999-11-23 21:07:06 +01:00
|
|
|
create_tidscan_joinpaths(rel);
|
|
|
|
}
|