1 /*-------------------------------------------------------------------------
4 * This code provides support for generalized relation scans. ExecScan
5 * is passed a node and a pointer to a function to "do the right thing"
6 * and return a tuple from the relation. ExecScan then does the tedious
7 * stuff - checking the qualification and projecting the tuple
10 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
15 * $Header: /cvsroot/pgsql/src/backend/executor/execScan.c,v 1.16 2001/03/22 03:59:26 momjian Exp $
17 *-------------------------------------------------------------------------
20 #include <sys/types.h>
25 #include "executor/executor.h"
26 #include "utils/memutils.h"
29 /* ----------------------------------------------------------------
32 * Scans the relation using the 'access method' indicated and
33 * returns the next qualifying tuple in the direction specified
34 * in the global variable ExecDirection.
35 * The access method returns the next tuple and execScan() is
36 * responsible for checking the tuple returned against the qual-clause.
39 * -- the "cursor" maintained by the AMI is positioned at the tuple
40 * returned previously.
43 * -- the relation indicated is opened for scanning so that the
44 * "cursor" is positioned before the first qualifying tuple.
45 * ----------------------------------------------------------------
49 ExecScanAccessMtd accessMtd) /* function returning a tuple */
51 CommonScanState *scanstate;
53 ExprContext *econtext;
56 TupleTableSlot *resultSlot;
59 * Fetch data from node
62 estate = node->plan.state;
63 scanstate = node->scanstate;
64 econtext = scanstate->cstate.cs_ExprContext;
65 qual = node->plan.qual;
68 * Check to see if we're still projecting out tuples from a previous
69 * scan tuple (because there is a function-returning-set in the
70 * projection expressions). If so, try to project another one.
73 if (scanstate->cstate.cs_TupFromTlist)
75 resultSlot = ExecProject(scanstate->cstate.cs_ProjInfo, &isDone);
76 if (isDone == ExprMultipleResult)
78 /* Done with that source tuple... */
79 scanstate->cstate.cs_TupFromTlist = false;
83 * Reset per-tuple memory context to free any expression evaluation
84 * storage allocated in the previous tuple cycle. Note this can't
85 * happen until we're done projecting out tuples from a scan tuple.
88 ResetExprContext(econtext);
91 * get a tuple from the access method loop until we obtain a tuple
92 * which passes the qualification.
98 slot = (*accessMtd) (node);
101 * if the slot returned by the accessMtd contains
102 * NULL, then it means there is nothing more to scan
103 * so we just return an empty slot, being careful to use
104 * the projection result slot so it has correct tupleDesc.
109 return ExecStoreTuple(NULL,
110 scanstate->cstate.cs_ProjInfo->pi_slot,
116 * place the current tuple into the expr context
119 econtext->ecxt_scantuple = slot;
122 * check that the current tuple satisfies the qual-clause
124 * check for non-nil qual here to avoid a function call to
125 * ExecQual() when the qual is nil ... saves only a few cycles,
126 * but they add up ...
129 if (!qual || ExecQual(qual, econtext, false))
132 * Found a satisfactory scan tuple.
134 * Form a projection tuple, store it in the result tuple
135 * slot and return it --- unless we find we can project no
136 * tuples from this scan tuple, in which case continue scan.
139 resultSlot = ExecProject(scanstate->cstate.cs_ProjInfo, &isDone);
140 if (isDone != ExprEndResult)
142 scanstate->cstate.cs_TupFromTlist = (isDone == ExprMultipleResult);
148 * Tuple fails qual, so free per-tuple memory and try again.
151 ResetExprContext(econtext);