Skip to content

Commit 37168b8

Browse files
committed
Clean up handling of variable-free qual clauses. System now does the
right thing with variable-free clauses that contain noncachable functions, such as 'WHERE random() < 0.5' --- these are evaluated once per potential output tuple. Expressions that contain only Params are now candidates to be indexscan quals --- for example, 'var = ($1 + 1)' can now be indexed. Cope with RelabelType nodes atop potential indexscan variables --- this oversight prevents 7.0.* from recognizing some potentially indexscanable situations.
1 parent 766fb7f commit 37168b8

File tree

15 files changed

+396
-163
lines changed

15 files changed

+396
-163
lines changed

src/backend/executor/execProcnode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
*
1414
* IDENTIFICATION
15-
* $Header: /cvsroot/pgsql/src/backend/executor/execProcnode.c,v 1.18 2000/01/26 05:56:21 momjian Exp $
15+
* $Header: /cvsroot/pgsql/src/backend/executor/execProcnode.c,v 1.19 2000/08/13 02:50:03 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -277,7 +277,7 @@ ExecProcNode(Plan *node, Plan *parent)
277277
* ----------------
278278
*/
279279
case T_NestLoop:
280-
result = ExecNestLoop((NestLoop *) node, parent);
280+
result = ExecNestLoop((NestLoop *) node);
281281
break;
282282

283283
case T_MergeJoin:

src/backend/executor/nodeIndexscan.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/executor/nodeIndexscan.c,v 1.52 2000/07/12 02:37:03 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/executor/nodeIndexscan.c,v 1.53 2000/08/13 02:50:03 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -281,6 +281,16 @@ IndexNext(IndexScan *node)
281281
TupleTableSlot *
282282
ExecIndexScan(IndexScan *node)
283283
{
284+
IndexScanState *indexstate = node->indxstate;
285+
286+
/* ----------------
287+
* If we have runtime keys and they've not already been set up,
288+
* do it now.
289+
* ----------------
290+
*/
291+
if (indexstate->iss_RuntimeKeyInfo && !indexstate->iss_RuntimeKeysReady)
292+
ExecReScan((Plan *) node, NULL, NULL);
293+
284294
/* ----------------
285295
* use IndexNext as access method
286296
* ----------------
@@ -335,9 +345,10 @@ ExecIndexReScan(IndexScan *node, ExprContext *exprCtxt, Plan *parent)
335345
scanKeys = indexstate->iss_ScanKeys;
336346
runtimeKeyInfo = indexstate->iss_RuntimeKeyInfo;
337347
numScanKeys = indexstate->iss_NumScanKeys;
338-
indexstate->iss_IndexPtr = -1;
339348
if (ScanDirectionIsBackward(node->indxorderdir))
340349
indexstate->iss_IndexPtr = numIndices;
350+
else
351+
indexstate->iss_IndexPtr = -1;
341352

342353
if (econtext)
343354
{
@@ -420,6 +431,9 @@ ExecIndexReScan(IndexScan *node, ExprContext *exprCtxt, Plan *parent)
420431
skey = scanKeys[i];
421432
index_rescan(scan, direction, skey);
422433
}
434+
435+
if (runtimeKeyInfo)
436+
indexstate->iss_RuntimeKeysReady = true;
423437
}
424438

425439
/* ----------------------------------------------------------------
@@ -603,7 +617,6 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
603617
Relation currentRelation;
604618
HeapScanDesc currentScanDesc;
605619
ScanDirection direction;
606-
List *execParam = NIL;
607620

608621
/* ----------------
609622
* assign execution state to node
@@ -656,6 +669,7 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
656669
indexstate->iss_NumScanKeys = NULL;
657670
indexstate->iss_RuntimeKeyInfo = NULL;
658671
indexstate->iss_RuntimeContext = NULL;
672+
indexstate->iss_RuntimeKeysReady = false;
659673
indexstate->iss_RelationDescs = NULL;
660674
indexstate->iss_ScanDescs = NULL;
661675

@@ -787,6 +801,9 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
787801
*/
788802
leftop = (Node *) get_leftop(clause);
789803

804+
if (leftop && IsA(leftop, RelabelType))
805+
leftop = ((RelabelType *) leftop)->arg;
806+
790807
Assert(leftop != NULL);
791808

792809
if (IsA(leftop, Var) && var_is_rel((Var *) leftop))
@@ -827,7 +844,6 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
827844
/* treat Param as runtime key */
828845
have_runtime_keys = true;
829846
run_keys[j] = LEFT_OP;
830-
execParam = lappendi(execParam, ((Param *) leftop)->paramid);
831847
}
832848
else
833849
{
@@ -857,6 +873,9 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
857873
*/
858874
rightop = (Node *) get_rightop(clause);
859875

876+
if (rightop && IsA(rightop, RelabelType))
877+
rightop = ((RelabelType *) rightop)->arg;
878+
860879
Assert(rightop != NULL);
861880

862881
if (IsA(rightop, Var) && var_is_rel((Var *) rightop))
@@ -906,7 +925,6 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
906925
/* treat Param as runtime key */
907926
have_runtime_keys = true;
908927
run_keys[j] = RIGHT_OP;
909-
execParam = lappendi(execParam, ((Param *) rightop)->paramid);
910928
}
911929
else
912930
{
@@ -1068,12 +1086,6 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
10681086
indexstate->iss_RelationDescs = relationDescs;
10691087
indexstate->iss_ScanDescs = scanDescs;
10701088

1071-
/*
1072-
* if there are some PARAM_EXEC in scankeys then force index rescan on
1073-
* first scan.
1074-
*/
1075-
((Plan *) node)->chgParam = execParam;
1076-
10771089
/* ----------------
10781090
* all done.
10791091
* ----------------

src/backend/executor/nodeNestloop.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/executor/nodeNestloop.c,v 1.18 2000/07/17 03:04:53 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/executor/nodeNestloop.c,v 1.19 2000/08/13 02:50:03 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -57,7 +57,7 @@
5757
* ----------------------------------------------------------------
5858
*/
5959
TupleTableSlot *
60-
ExecNestLoop(NestLoop *node, Plan *parent)
60+
ExecNestLoop(NestLoop *node)
6161
{
6262
NestLoopState *nlstate;
6363
Plan *innerPlan;
@@ -187,7 +187,7 @@ ExecNestLoop(NestLoop *node, Plan *parent)
187187
* outer tuple (e.g. in index scans), that's why we pass our
188188
* expr context.
189189
*/
190-
ExecReScan(innerPlan, econtext, parent);
190+
ExecReScan(innerPlan, econtext, (Plan *) node);
191191

192192
ENL1_printf("getting new inner tuple");
193193

src/backend/optimizer/path/clausesel.c

Lines changed: 10 additions & 3 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.38 2000/06/08 22:37:09 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/clausesel.c,v 1.39 2000/08/13 02:50:04 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -123,7 +123,7 @@ clauselist_selectivity(Query *root,
123123
Selectivity s2;
124124

125125
/*
126-
* See if it looks like a restriction clause with a Const or Param
126+
* See if it looks like a restriction clause with a pseudoconstant
127127
* on one side. (Anything more complicated than that might not
128128
* behave in the simple way we are expecting.)
129129
*
@@ -146,7 +146,7 @@ clauselist_selectivity(Query *root,
146146

147147
other = (flag & SEL_RIGHT) ? get_rightop((Expr *) clause) :
148148
get_leftop((Expr *) clause);
149-
if (IsA(other, Const) || IsA(other, Param))
149+
if (is_pseudo_constant_clause((Node *) other))
150150
{
151151
Oid opno = ((Oper *) ((Expr *) clause)->oper)->opno;
152152
RegProcedure oprrest = get_oprrest(opno);
@@ -533,6 +533,13 @@ clause_selectivity(Query *root,
533533
*/
534534
s1 = 1.0;
535535
}
536+
else if (IsA(clause, RelabelType))
537+
{
538+
/* Not sure this case is needed, but it can't hurt */
539+
s1 = clause_selectivity(root,
540+
((RelabelType *) clause)->arg,
541+
varRelid);
542+
}
536543

537544
return s1;
538545
}

0 commit comments

Comments
 (0)