1999-11-23 21:07:06 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* tidpath.c
|
2005-08-23 22:49:47 +02:00
|
|
|
* Routines to determine which TID conditions are usable for scanning
|
|
|
|
* a given relation, and create TidPaths accordingly.
|
|
|
|
*
|
|
|
|
* What we are looking for here is WHERE conditions of the form
|
|
|
|
* "CTID = pseudoconstant", which can be implemented by just fetching
|
|
|
|
* the tuple directly via heap_fetch(). We can also handle OR conditions
|
|
|
|
* if each OR arm contains such a condition; in particular this allows
|
|
|
|
* WHERE ctid IN (tid1, tid2, ...)
|
|
|
|
*
|
|
|
|
* There is currently no special support for joins involving CTID; in
|
2005-10-15 04:49:52 +02:00
|
|
|
* particular nothing corresponding to best_inner_indexscan(). Since it's
|
2005-08-23 22:49:47 +02:00
|
|
|
* not very useful to store TIDs of one table in another table, there
|
|
|
|
* doesn't seem to be enough use-case to justify adding a lot of code
|
|
|
|
* for that.
|
|
|
|
*
|
1999-11-23 21:07:06 +01:00
|
|
|
*
|
2004-12-31 23:04:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1999-11-23 21:07:06 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2005-10-15 04:49:52 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/optimizer/path/tidpath.c,v 1.25 2005/10/15 02:49:20 momjian Exp $
|
1999-11-23 21:07:06 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2005-08-23 22:49:47 +02:00
|
|
|
#include "access/htup.h"
|
1999-11-23 21:07:06 +01:00
|
|
|
#include "catalog/pg_operator.h"
|
2005-08-23 22:49:47 +02:00
|
|
|
#include "catalog/pg_type.h"
|
1999-11-23 21:07:06 +01:00
|
|
|
#include "optimizer/clauses.h"
|
|
|
|
#include "optimizer/pathnode.h"
|
|
|
|
#include "optimizer/paths.h"
|
2005-08-23 22:49:47 +02:00
|
|
|
#include "parser/parse_expr.h"
|
2005-06-06 00:32:58 +02:00
|
|
|
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2005-08-23 22:49:47 +02:00
|
|
|
static Node *IsTidEqualClause(int varno, OpExpr *node);
|
|
|
|
static List *TidQualFromExpr(int varno, Node *expr);
|
|
|
|
static List *TidQualFromRestrictinfo(int varno, List *restrictinfo);
|
2005-06-06 00:32:58 +02:00
|
|
|
|
1999-11-23 21:07:06 +01:00
|
|
|
|
|
|
|
/*
|
2005-08-23 22:49:47 +02:00
|
|
|
* Check to see if an opclause is of the form
|
|
|
|
* CTID = pseudoconstant
|
|
|
|
* or
|
|
|
|
* pseudoconstant = CTID
|
|
|
|
*
|
|
|
|
* If it is, return the pseudoconstant subnode; if not, return NULL.
|
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We check that the CTID Var belongs to relation "varno". That is probably
|
2005-08-23 22:49:47 +02:00
|
|
|
* redundant considering this is only applied to restriction clauses, but
|
|
|
|
* let's be safe.
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2002-12-12 16:49:42 +01:00
|
|
|
static Node *
|
2005-08-23 22:49:47 +02:00
|
|
|
IsTidEqualClause(int varno, OpExpr *node)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2005-08-23 22:49:47 +02:00
|
|
|
Node *arg1,
|
2000-04-12 19:17:23 +02:00
|
|
|
*arg2,
|
2005-08-23 22:49:47 +02:00
|
|
|
*other;
|
2000-04-12 19:17:23 +02:00
|
|
|
Var *var;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2005-08-23 22:49:47 +02:00
|
|
|
/* Operator must be tideq */
|
2002-12-12 16:49:42 +01:00
|
|
|
if (node->opno != TIDEqualOperator)
|
2005-08-23 22:49:47 +02:00
|
|
|
return NULL;
|
2004-05-31 01:40:41 +02:00
|
|
|
if (list_length(node->args) != 2)
|
2005-08-23 22:49:47 +02:00
|
|
|
return NULL;
|
2004-05-26 06:41:50 +02:00
|
|
|
arg1 = linitial(node->args);
|
1999-11-23 21:07:06 +01:00
|
|
|
arg2 = lsecond(node->args);
|
|
|
|
|
2005-08-23 22:49:47 +02:00
|
|
|
/* Look for CTID as either argument */
|
|
|
|
other = NULL;
|
|
|
|
if (arg1 && IsA(arg1, Var))
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
|
|
|
var = (Var *) arg1;
|
2005-08-23 22:49:47 +02:00
|
|
|
if (var->varattno == SelfItemPointerAttributeNumber &&
|
|
|
|
var->vartype == TIDOID &&
|
|
|
|
var->varno == varno &&
|
|
|
|
var->varlevelsup == 0)
|
|
|
|
other = arg2;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
2005-08-23 22:49:47 +02:00
|
|
|
if (!other && arg2 && IsA(arg2, Var))
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
|
|
|
var = (Var *) arg2;
|
2005-08-23 22:49:47 +02:00
|
|
|
if (var->varattno == SelfItemPointerAttributeNumber &&
|
|
|
|
var->vartype == TIDOID &&
|
|
|
|
var->varno == varno &&
|
|
|
|
var->varlevelsup == 0)
|
|
|
|
other = arg1;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
2005-08-23 22:49:47 +02:00
|
|
|
if (!other)
|
|
|
|
return NULL;
|
|
|
|
if (exprType(other) != TIDOID)
|
|
|
|
return NULL; /* probably can't happen */
|
|
|
|
|
|
|
|
/* The other argument must be a pseudoconstant */
|
|
|
|
if (!is_pseudo_constant_clause(other))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return other; /* success */
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-08-23 22:49:47 +02:00
|
|
|
* Extract a set of CTID conditions from the given qual expression
|
|
|
|
*
|
|
|
|
* If the expression is an AND clause, we can use a CTID condition
|
|
|
|
* from any sub-clause. If it is an OR clause, we must be able to
|
|
|
|
* extract a CTID condition from every sub-clause, or we can't use it.
|
|
|
|
*
|
|
|
|
* In theory, in the AND case we could get CTID conditions from different
|
|
|
|
* sub-clauses, in which case we could try to pick the most efficient one.
|
|
|
|
* In practice, such usage seems very unlikely, so we don't bother; we
|
|
|
|
* just exit as soon as we find the first candidate.
|
|
|
|
*
|
|
|
|
* Returns a List of pseudoconstant TID expressions, or NIL if no match.
|
|
|
|
* (Has to be a list for the OR case.)
|
2000-04-12 19:17:23 +02:00
|
|
|
*/
|
2002-12-12 16:49:42 +01:00
|
|
|
static List *
|
2005-08-23 22:49:47 +02:00
|
|
|
TidQualFromExpr(int varno, Node *expr)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
List *rlst = NIL,
|
|
|
|
*frtn;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *l;
|
2005-08-23 22:49:47 +02:00
|
|
|
Node *rnode;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2005-08-23 22:49:47 +02:00
|
|
|
if (is_opclause(expr))
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2005-08-23 22:49:47 +02:00
|
|
|
/* base case: check for tideq opclause */
|
|
|
|
rnode = IsTidEqualClause(varno, (OpExpr *) expr);
|
1999-11-23 21:07:06 +01:00
|
|
|
if (rnode)
|
2005-08-23 22:49:47 +02:00
|
|
|
rlst = list_make1(rnode);
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
2005-08-23 22:49:47 +02:00
|
|
|
else if (and_clause(expr))
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
foreach(l, ((BoolExpr *) expr)->args)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2005-08-23 22:49:47 +02:00
|
|
|
rlst = TidQualFromExpr(varno, (Node *) lfirst(l));
|
1999-11-23 21:07:06 +01:00
|
|
|
if (rlst)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-08-23 22:49:47 +02:00
|
|
|
else if (or_clause(expr))
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
foreach(l, ((BoolExpr *) expr)->args)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2005-08-23 22:49:47 +02:00
|
|
|
frtn = TidQualFromExpr(varno, (Node *) lfirst(l));
|
2002-12-12 16:49:42 +01:00
|
|
|
if (frtn)
|
2004-05-31 01:40:41 +02:00
|
|
|
rlst = list_concat(rlst, frtn);
|
1999-11-23 21:07:06 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (rlst)
|
2004-05-31 01:40:41 +02:00
|
|
|
list_free(rlst);
|
1999-11-23 21:07:06 +01:00
|
|
|
rlst = NIL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rlst;
|
2000-04-12 19:17:23 +02:00
|
|
|
}
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2005-08-23 22:49:47 +02:00
|
|
|
/*
|
|
|
|
* Extract a set of CTID conditions from the given restrictinfo list
|
|
|
|
*
|
|
|
|
* This is essentially identical to the AND case of TidQualFromExpr,
|
|
|
|
* except for the format of the input.
|
|
|
|
*/
|
2000-02-07 05:41:04 +01:00
|
|
|
static List *
|
2005-08-23 22:49:47 +02:00
|
|
|
TidQualFromRestrictinfo(int varno, List *restrictinfo)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
List *rlst = NIL;
|
2005-08-23 22:49:47 +02:00
|
|
|
ListCell *l;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2004-05-26 06:41:50 +02:00
|
|
|
foreach(l, restrictinfo)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2005-08-23 22:49:47 +02:00
|
|
|
RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
|
|
|
|
|
|
|
|
if (!IsA(rinfo, RestrictInfo))
|
|
|
|
continue; /* probably should never happen */
|
|
|
|
rlst = TidQualFromExpr(varno, (Node *) rinfo->clause);
|
1999-11-23 21:07:06 +01:00
|
|
|
if (rlst)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rlst;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create_tidscan_paths
|
2005-08-23 22:49:47 +02:00
|
|
|
* Create paths corresponding to direct TID scans of the given rel.
|
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* 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
|
2005-06-06 00:32:58 +02:00
|
|
|
create_tidscan_paths(PlannerInfo *root, RelOptInfo *rel)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2005-08-23 22:49:47 +02:00
|
|
|
List *tideval;
|
|
|
|
|
|
|
|
tideval = TidQualFromRestrictinfo(rel->relid, rel->baserestrictinfo);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-11-23 21:07:06 +01:00
|
|
|
if (tideval)
|
2001-06-05 07:26:05 +02:00
|
|
|
add_path(rel, (Path *) create_tidscan_path(root, rel, tideval));
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|