@@ -102,6 +102,30 @@ IndexNext(IndexScanState *node)
102102 econtext = node -> ss .ps .ps_ExprContext ;
103103 slot = node -> ss .ss_ScanTupleSlot ;
104104
105+ if (scandesc == NULL )
106+ {
107+ /*
108+ * We reach here if the index scan is not parallel, or if we're
109+ * executing a index scan that was intended to be parallel serially.
110+ */
111+ scandesc = index_beginscan (node -> ss .ss_currentRelation ,
112+ node -> iss_RelationDesc ,
113+ estate -> es_snapshot ,
114+ node -> iss_NumScanKeys ,
115+ node -> iss_NumOrderByKeys );
116+
117+ node -> iss_ScanDesc = scandesc ;
118+
119+ /*
120+ * If no run-time keys to calculate or they are ready, go ahead and
121+ * pass the scankeys to the index AM.
122+ */
123+ if (node -> iss_NumRuntimeKeys == 0 || node -> iss_RuntimeKeysReady )
124+ index_rescan (scandesc ,
125+ node -> iss_ScanKeys , node -> iss_NumScanKeys ,
126+ node -> iss_OrderByKeys , node -> iss_NumOrderByKeys );
127+ }
128+
105129 /*
106130 * ok, now that we have what we need, fetch the next tuple.
107131 */
@@ -154,6 +178,7 @@ IndexNext(IndexScanState *node)
154178static TupleTableSlot *
155179IndexNextWithReorder (IndexScanState * node )
156180{
181+ EState * estate ;
157182 ExprContext * econtext ;
158183 IndexScanDesc scandesc ;
159184 HeapTuple tuple ;
@@ -164,6 +189,8 @@ IndexNextWithReorder(IndexScanState *node)
164189 bool * lastfetched_nulls ;
165190 int cmp ;
166191
192+ estate = node -> ss .ps .state ;
193+
167194 /*
168195 * Only forward scan is supported with reordering. Note: we can get away
169196 * with just Asserting here because the system will not try to run the
@@ -174,12 +201,36 @@ IndexNextWithReorder(IndexScanState *node)
174201 * explicitly.
175202 */
176203 Assert (!ScanDirectionIsBackward (((IndexScan * ) node -> ss .ps .plan )-> indexorderdir ));
177- Assert (ScanDirectionIsForward (node -> ss . ps . state -> es_direction ));
204+ Assert (ScanDirectionIsForward (estate -> es_direction ));
178205
179206 scandesc = node -> iss_ScanDesc ;
180207 econtext = node -> ss .ps .ps_ExprContext ;
181208 slot = node -> ss .ss_ScanTupleSlot ;
182209
210+ if (scandesc == NULL )
211+ {
212+ /*
213+ * We reach here if the index scan is not parallel, or if we're
214+ * executing a index scan that was intended to be parallel serially.
215+ */
216+ scandesc = index_beginscan (node -> ss .ss_currentRelation ,
217+ node -> iss_RelationDesc ,
218+ estate -> es_snapshot ,
219+ node -> iss_NumScanKeys ,
220+ node -> iss_NumOrderByKeys );
221+
222+ node -> iss_ScanDesc = scandesc ;
223+
224+ /*
225+ * If no run-time keys to calculate or they are ready, go ahead and
226+ * pass the scankeys to the index AM.
227+ */
228+ if (node -> iss_NumRuntimeKeys == 0 || node -> iss_RuntimeKeysReady )
229+ index_rescan (scandesc ,
230+ node -> iss_ScanKeys , node -> iss_NumScanKeys ,
231+ node -> iss_OrderByKeys , node -> iss_NumOrderByKeys );
232+ }
233+
183234 for (;;)
184235 {
185236 /*
@@ -1038,31 +1089,6 @@ ExecInitIndexScan(IndexScan *node, EState *estate, int eflags)
10381089 indexstate -> iss_RuntimeContext = NULL ;
10391090 }
10401091
1041- /*
1042- * for parallel-aware node, we initialize the scan descriptor after
1043- * initializing the shared memory for parallel execution.
1044- */
1045- if (!node -> scan .plan .parallel_aware )
1046- {
1047- /*
1048- * Initialize scan descriptor.
1049- */
1050- indexstate -> iss_ScanDesc = index_beginscan (currentRelation ,
1051- indexstate -> iss_RelationDesc ,
1052- estate -> es_snapshot ,
1053- indexstate -> iss_NumScanKeys ,
1054- indexstate -> iss_NumOrderByKeys );
1055-
1056- /*
1057- * If no run-time keys to calculate, go ahead and pass the scankeys to
1058- * the index AM.
1059- */
1060- if (indexstate -> iss_NumRuntimeKeys == 0 )
1061- index_rescan (indexstate -> iss_ScanDesc ,
1062- indexstate -> iss_ScanKeys , indexstate -> iss_NumScanKeys ,
1063- indexstate -> iss_OrderByKeys , indexstate -> iss_NumOrderByKeys );
1064- }
1065-
10661092 /*
10671093 * all done.
10681094 */
@@ -1674,10 +1700,10 @@ ExecIndexScanInitializeDSM(IndexScanState *node,
16741700 piscan );
16751701
16761702 /*
1677- * If no run-time keys to calculate, go ahead and pass the scankeys to the
1678- * index AM.
1703+ * If no run-time keys to calculate or they are ready , go ahead and pass
1704+ * the scankeys to the index AM.
16791705 */
1680- if (node -> iss_NumRuntimeKeys == 0 )
1706+ if (node -> iss_NumRuntimeKeys == 0 || node -> iss_RuntimeKeysReady )
16811707 index_rescan (node -> iss_ScanDesc ,
16821708 node -> iss_ScanKeys , node -> iss_NumScanKeys ,
16831709 node -> iss_OrderByKeys , node -> iss_NumOrderByKeys );
@@ -1703,10 +1729,10 @@ ExecIndexScanInitializeWorker(IndexScanState *node, shm_toc *toc)
17031729 piscan );
17041730
17051731 /*
1706- * If no run-time keys to calculate, go ahead and pass the scankeys to the
1707- * index AM.
1732+ * If no run-time keys to calculate or they are ready , go ahead and pass
1733+ * the scankeys to the index AM.
17081734 */
1709- if (node -> iss_NumRuntimeKeys == 0 )
1735+ if (node -> iss_NumRuntimeKeys == 0 || node -> iss_RuntimeKeysReady )
17101736 index_rescan (node -> iss_ScanDesc ,
17111737 node -> iss_ScanKeys , node -> iss_NumScanKeys ,
17121738 node -> iss_OrderByKeys , node -> iss_NumOrderByKeys );
0 commit comments