88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/executor/nodeBitmapIndexscan.c,v 1.5 2005/04/2417:32:46 tgl Exp $
11+ * $PostgreSQL: pgsql/src/backend/executor/nodeBitmapIndexscan.c,v 1.6 2005/04/2418:16:38 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -41,6 +41,8 @@ MultiExecBitmapIndexScan(BitmapIndexScanState *node)
4141{
4242#define MAX_TIDS 1024
4343TIDBitmap * tbm ;
44+ Oid indxid ;
45+ Relation indexRelation ;
4446IndexScanDesc scandesc ;
4547ItemPointerData tids [MAX_TIDS ];
4648int32 ntids ;
@@ -58,9 +60,22 @@ MultiExecBitmapIndexScan(BitmapIndexScanState *node)
5860ExecReScan ((PlanState * )node ,NULL );
5961
6062/*
61- * extract necessary information from index scan node
63+ * We do not open or lock the base relation here. We assume that an
64+ * ancestor BitmapHeapScan node is holding AccessShareLock on the
65+ * heap relation throughout the execution of the plan tree.
6266 */
63- scandesc = node -> biss_ScanDesc ;
67+
68+ /*
69+ * open the index relation and initialize relation and scan
70+ * descriptors. Note we acquire no locks here; the index machinery
71+ * does its own locks and unlocks.
72+ */
73+ indxid = ((BitmapIndexScan * )node -> ss .ps .plan )-> indxid ;
74+ indexRelation = index_open (indxid );
75+ scandesc = index_beginscan_multi (indexRelation ,
76+ node -> ss .ps .state -> es_snapshot ,
77+ node -> biss_NumScanKeys ,
78+ node -> biss_ScanKeys );
6479
6580/*
6681 * Prepare the result bitmap. Normally we just create a new one to pass
@@ -98,6 +113,12 @@ MultiExecBitmapIndexScan(BitmapIndexScanState *node)
98113CHECK_FOR_INTERRUPTS ();
99114}
100115
116+ /*
117+ * close the index relation
118+ */
119+ index_endscan (scandesc );
120+ index_close (indexRelation );
121+
101122/* must provide our own instrumentation support */
102123if (node -> ss .ps .instrument )
103124InstrStopNodeMulti (node -> ss .ps .instrument ,nTuples );
@@ -109,24 +130,18 @@ MultiExecBitmapIndexScan(BitmapIndexScanState *node)
109130 *ExecBitmapIndexReScan(node)
110131 *
111132 *Recalculates the value of the scan keys whose value depends on
112- *information known at runtime and rescans the indexed relation.
113- *Updating the scan key was formerly done separately in
114- *ExecUpdateIndexScanKeys. Integrating it into ReScan makes
115- *rescans of indices and relations/general streams more uniform.
116- *
133+ *information known at runtime.
117134 * ----------------------------------------------------------------
118135 */
119136void
120137ExecBitmapIndexReScan (BitmapIndexScanState * node ,ExprContext * exprCtxt )
121138{
122139ExprContext * econtext ;
123140ExprState * * runtimeKeyInfo ;
124- Index scanrelid ;
125141
126142econtext = node -> biss_RuntimeContext ;/* context for runtime
127143 * keys */
128144runtimeKeyInfo = node -> biss_RuntimeKeyInfo ;
129- scanrelid = ((BitmapIndexScan * )node -> ss .ps .plan )-> scan .scanrelid ;
130145
131146if (econtext )
132147{
@@ -194,8 +209,6 @@ ExecBitmapIndexReScan(BitmapIndexScanState *node, ExprContext *exprCtxt)
194209
195210node -> biss_RuntimeKeysReady = true;
196211}
197-
198- index_rescan (node -> biss_ScanDesc ,node -> biss_ScanKeys );
199212}
200213
201214/* ----------------------------------------------------------------
@@ -205,58 +218,19 @@ ExecBitmapIndexReScan(BitmapIndexScanState *node, ExprContext *exprCtxt)
205218void
206219ExecEndBitmapIndexScan (BitmapIndexScanState * node )
207220{
208- Relation relation ;
209-
210- /*
211- * extract information from the node
212- */
213- relation = node -> ss .ss_currentRelation ;
214-
215221/*
216222 * Free the exprcontext ... now dead code, see ExecFreeExprContext
217223 */
218224#ifdef NOT_USED
219225if (node -> biss_RuntimeContext )
220226FreeExprContext (node -> biss_RuntimeContext );
221227#endif
222-
223- /*
224- * close the index relation
225- */
226- if (node -> biss_ScanDesc != NULL )
227- index_endscan (node -> biss_ScanDesc );
228-
229- if (node -> biss_RelationDesc != NULL )
230- index_close (node -> biss_RelationDesc );
231-
232- /*
233- * close the heap relation.
234- *
235- * Currently, we do not release the AccessShareLock acquired by
236- * ExecInitBitmapIndexScan. This lock should be held till end of
237- * transaction. (There is a faction that considers this too much
238- * locking, however.)
239- */
240- heap_close (relation ,NoLock );
241228}
242229
243230/* ----------------------------------------------------------------
244231 *ExecInitBitmapIndexScan
245232 *
246- *Initializes the index scan's state information, creates
247- *scan keys, and opens the base and index relations.
248- *
249- *Note: index scans have 2 sets of state information because
250- * we have to keep track of the base relation and the
251- * index relations.
252- *
253- * old comments
254- *Creates the run-time state information for the node and
255- *sets the relation id to contain relevant descriptors.
256- *
257- *Parameters:
258- * node: BitmapIndexNode node produced by the planner.
259- * estate: the execution state initialized in InitPlan.
233+ *Initializes the index scan's state information.
260234 * ----------------------------------------------------------------
261235 */
262236BitmapIndexScanState *
@@ -265,10 +239,6 @@ ExecInitBitmapIndexScan(BitmapIndexScan *node, EState *estate)
265239BitmapIndexScanState * indexstate ;
266240ExprState * * runtimeKeyInfo ;
267241bool have_runtime_keys ;
268- RangeTblEntry * rtentry ;
269- Index relid ;
270- Oid reloid ;
271- Relation currentRelation ;
272242
273243/*
274244 * create state structure
@@ -306,8 +276,6 @@ ExecInitBitmapIndexScan(BitmapIndexScan *node, EState *estate)
306276indexstate -> biss_RuntimeKeyInfo = NULL ;
307277indexstate -> biss_RuntimeContext = NULL ;
308278indexstate -> biss_RuntimeKeysReady = false;
309- indexstate -> biss_RelationDesc = NULL ;
310- indexstate -> biss_ScanDesc = NULL ;
311279
312280CXT1_printf ("ExecInitBitmapIndexScan: context is %d\n" ,CurrentMemoryContext );
313281
@@ -459,7 +427,6 @@ ExecInitBitmapIndexScan(BitmapIndexScan *node, EState *estate)
459427runtimeKeyInfo = run_keys ;
460428}
461429
462-
463430/*
464431 * If all of our keys have the form (var op const), then we have no
465432 * runtime keys so we store NULL in the runtime key info. Otherwise
@@ -489,30 +456,9 @@ ExecInitBitmapIndexScan(BitmapIndexScan *node, EState *estate)
489456pfree (runtimeKeyInfo );
490457}
491458
492- /*
493- * open the base relation and acquire AccessShareLock on it.
494- */
495- relid = node -> scan .scanrelid ;
496- rtentry = rt_fetch (relid ,estate -> es_range_table );
497- reloid = rtentry -> relid ;
498-
499- currentRelation = heap_open (reloid ,AccessShareLock );
500-
501- indexstate -> ss .ss_currentRelation = currentRelation ;
502- indexstate -> ss .ss_currentScanDesc = NULL ;/* no heap scan here */
503-
504- /*
505- * open the index relation and initialize relation and scan
506- * descriptors. Note we acquire no locks here; the index machinery
507- * does its own locks and unlocks.(We rely on having AccessShareLock
508- * on the parent table to ensure the index won't go away!)
509- */
510- indexstate -> biss_RelationDesc = index_open (node -> indxid );
511- indexstate -> biss_ScanDesc =
512- index_beginscan_multi (indexstate -> biss_RelationDesc ,
513- estate -> es_snapshot ,
514- indexstate -> biss_NumScanKeys ,
515- indexstate -> biss_ScanKeys );
459+ /* We don't keep the table or index open across calls */
460+ indexstate -> ss .ss_currentRelation = NULL ;
461+ indexstate -> ss .ss_currentScanDesc = NULL ;
516462
517463/*
518464 * all done.