@@ -102,6 +102,30 @@ IndexNext(IndexScanState *node)
102
102
econtext = node -> ss .ps .ps_ExprContext ;
103
103
slot = node -> ss .ss_ScanTupleSlot ;
104
104
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
+
105
129
/*
106
130
* ok, now that we have what we need, fetch the next tuple.
107
131
*/
@@ -154,6 +178,7 @@ IndexNext(IndexScanState *node)
154
178
static TupleTableSlot *
155
179
IndexNextWithReorder (IndexScanState * node )
156
180
{
181
+ EState * estate ;
157
182
ExprContext * econtext ;
158
183
IndexScanDesc scandesc ;
159
184
HeapTuple tuple ;
@@ -164,6 +189,8 @@ IndexNextWithReorder(IndexScanState *node)
164
189
bool * lastfetched_nulls ;
165
190
int cmp ;
166
191
192
+ estate = node -> ss .ps .state ;
193
+
167
194
/*
168
195
* Only forward scan is supported with reordering. Note: we can get away
169
196
* with just Asserting here because the system will not try to run the
@@ -174,12 +201,36 @@ IndexNextWithReorder(IndexScanState *node)
174
201
* explicitly.
175
202
*/
176
203
Assert (!ScanDirectionIsBackward (((IndexScan * ) node -> ss .ps .plan )-> indexorderdir ));
177
- Assert (ScanDirectionIsForward (node -> ss . ps . state -> es_direction ));
204
+ Assert (ScanDirectionIsForward (estate -> es_direction ));
178
205
179
206
scandesc = node -> iss_ScanDesc ;
180
207
econtext = node -> ss .ps .ps_ExprContext ;
181
208
slot = node -> ss .ss_ScanTupleSlot ;
182
209
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
+
183
234
for (;;)
184
235
{
185
236
/*
@@ -1038,31 +1089,6 @@ ExecInitIndexScan(IndexScan *node, EState *estate, int eflags)
1038
1089
indexstate -> iss_RuntimeContext = NULL ;
1039
1090
}
1040
1091
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
-
1066
1092
/*
1067
1093
* all done.
1068
1094
*/
@@ -1674,10 +1700,10 @@ ExecIndexScanInitializeDSM(IndexScanState *node,
1674
1700
piscan );
1675
1701
1676
1702
/*
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.
1679
1705
*/
1680
- if (node -> iss_NumRuntimeKeys == 0 )
1706
+ if (node -> iss_NumRuntimeKeys == 0 || node -> iss_RuntimeKeysReady )
1681
1707
index_rescan (node -> iss_ScanDesc ,
1682
1708
node -> iss_ScanKeys , node -> iss_NumScanKeys ,
1683
1709
node -> iss_OrderByKeys , node -> iss_NumOrderByKeys );
@@ -1703,10 +1729,10 @@ ExecIndexScanInitializeWorker(IndexScanState *node, shm_toc *toc)
1703
1729
piscan );
1704
1730
1705
1731
/*
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.
1708
1734
*/
1709
- if (node -> iss_NumRuntimeKeys == 0 )
1735
+ if (node -> iss_NumRuntimeKeys == 0 || node -> iss_RuntimeKeysReady )
1710
1736
index_rescan (node -> iss_ScanDesc ,
1711
1737
node -> iss_ScanKeys , node -> iss_NumScanKeys ,
1712
1738
node -> iss_OrderByKeys , node -> iss_NumOrderByKeys );
0 commit comments