Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * nodeIndexscan.c
4 : : * Routines to support indexed scans of relations
5 : : *
6 : : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/executor/nodeIndexscan.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : /*
16 : : * INTERFACE ROUTINES
17 : : * ExecIndexScan scans a relation using an index
18 : : * IndexNext retrieve next tuple using index
19 : : * IndexNextWithReorder same, but recheck ORDER BY expressions
20 : : * ExecInitIndexScan creates and initializes state info.
21 : : * ExecReScanIndexScan rescans the indexed relation.
22 : : * ExecEndIndexScan releases all storage.
23 : : * ExecIndexMarkPos marks scan position.
24 : : * ExecIndexRestrPos restores scan position.
25 : : * ExecIndexScanEstimate estimates DSM space needed for parallel index scan
26 : : * ExecIndexScanInitializeDSM initialize DSM for parallel indexscan
27 : : * ExecIndexScanReInitializeDSM reinitialize DSM for fresh scan
28 : : * ExecIndexScanInitializeWorker attach to DSM info in parallel worker
29 : : */
30 : : #include "postgres.h"
31 : :
32 : : #include "access/nbtree.h"
33 : : #include "access/relscan.h"
34 : : #include "access/tableam.h"
35 : : #include "catalog/pg_am.h"
36 : : #include "executor/executor.h"
37 : : #include "executor/nodeIndexscan.h"
38 : : #include "lib/pairingheap.h"
39 : : #include "miscadmin.h"
40 : : #include "nodes/nodeFuncs.h"
41 : : #include "utils/array.h"
42 : : #include "utils/datum.h"
43 : : #include "utils/lsyscache.h"
44 : : #include "utils/rel.h"
45 : :
46 : : /*
47 : : * When an ordering operator is used, tuples fetched from the index that
48 : : * need to be reordered are queued in a pairing heap, as ReorderTuples.
49 : : */
50 : : typedef struct
51 : : {
52 : : pairingheap_node ph_node;
53 : : HeapTuple htup;
54 : : Datum *orderbyvals;
55 : : bool *orderbynulls;
56 : : } ReorderTuple;
57 : :
58 : : static TupleTableSlot *IndexNext(IndexScanState *node);
59 : : static TupleTableSlot *IndexNextWithReorder(IndexScanState *node);
60 : : static void EvalOrderByExpressions(IndexScanState *node, ExprContext *econtext);
61 : : static bool IndexRecheck(IndexScanState *node, TupleTableSlot *slot);
62 : : static int cmp_orderbyvals(const Datum *adist, const bool *anulls,
63 : : const Datum *bdist, const bool *bnulls,
64 : : IndexScanState *node);
65 : : static int reorderqueue_cmp(const pairingheap_node *a,
66 : : const pairingheap_node *b, void *arg);
67 : : static void reorderqueue_push(IndexScanState *node, TupleTableSlot *slot,
68 : : Datum *orderbyvals, bool *orderbynulls);
69 : : static HeapTuple reorderqueue_pop(IndexScanState *node);
70 : :
71 : :
72 : : /* ----------------------------------------------------------------
73 : : * IndexNext
74 : : *
75 : : * Retrieve a tuple from the IndexScan node's currentRelation
76 : : * using the index specified in the IndexScanState information.
77 : : * ----------------------------------------------------------------
78 : : */
79 : : static TupleTableSlot *
8311 tgl@sss.pgh.pa.us 80 :CBC 1067368 : IndexNext(IndexScanState *node)
81 : : {
82 : : EState *estate;
83 : : ExprContext *econtext;
84 : : ScanDirection direction;
85 : : IndexScanDesc scandesc;
86 : : TupleTableSlot *slot;
87 : :
88 : : /*
89 : : * extract necessary information from index scan node
90 : : */
91 : 1067368 : estate = node->ss.ps.state;
92 : :
93 : : /*
94 : : * Determine which direction to scan the index in based on the plan's scan
95 : : * direction and the current direction of execution.
96 : : */
948 drowley@postgresql.o 97 : 1067368 : direction = ScanDirectionCombine(estate->es_direction,
98 : : ((IndexScan *) node->ss.ps.plan)->indexorderdir);
7439 tgl@sss.pgh.pa.us 99 : 1067368 : scandesc = node->iss_ScanDesc;
8311 100 : 1067368 : econtext = node->ss.ps.ps_ExprContext;
101 : 1067368 : slot = node->ss.ss_ScanTupleSlot;
102 : :
3104 rhaas@postgresql.org 103 [ + + ]: 1067368 : if (scandesc == NULL)
104 : : {
105 : : /*
106 : : * We reach here if the index scan is not parallel, or if we're
107 : : * serially executing an index scan that was planned to be parallel.
108 : : */
109 : 66277 : scandesc = index_beginscan(node->ss.ss_currentRelation,
110 : : node->iss_RelationDesc,
111 : : estate->es_snapshot,
112 : : &node->iss_Instrument,
113 : : node->iss_NumScanKeys,
114 : : node->iss_NumOrderByKeys);
115 : :
116 : 66277 : node->iss_ScanDesc = scandesc;
117 : :
118 : : /*
119 : : * If no run-time keys to calculate or they are ready, go ahead and
120 : : * pass the scankeys to the index AM.
121 : : */
122 [ + + + - ]: 66277 : if (node->iss_NumRuntimeKeys == 0 || node->iss_RuntimeKeysReady)
123 : 66277 : index_rescan(scandesc,
124 : 66277 : node->iss_ScanKeys, node->iss_NumScanKeys,
125 : 66277 : node->iss_OrderByKeys, node->iss_NumOrderByKeys);
126 : : }
127 : :
128 : : /*
129 : : * ok, now that we have what we need, fetch the next tuple.
130 : : */
2371 andres@anarazel.de 131 [ + + ]: 1068759 : while (index_getnext_slot(scandesc, direction, slot))
132 : : {
2965 133 [ + + ]: 837485 : CHECK_FOR_INTERRUPTS();
134 : :
135 : : /*
136 : : * If the index was lossy, we have to recheck the index quals using
137 : : * the fetched tuple.
138 : : */
6355 tgl@sss.pgh.pa.us 139 [ + + ]: 837485 : if (scandesc->xs_recheck)
140 : : {
141 : 162979 : econtext->ecxt_scantuple = slot;
2777 andres@anarazel.de 142 [ + + ]: 162979 : if (!ExecQualAndReset(node->indexqualorig, econtext))
143 : : {
144 : : /* Fails recheck, so drop it and loop back for another */
5098 tgl@sss.pgh.pa.us 145 [ - + ]: 1391 : InstrCountFiltered2(node, 1);
146 : 1391 : continue;
147 : : }
148 : : }
149 : :
7439 150 : 836094 : return slot;
151 : : }
152 : :
153 : : /*
154 : : * if we get here it means the index scan failed so we are at the end of
155 : : * the scan..
156 : : */
3767 heikki.linnakangas@i 157 : 231272 : node->iss_ReachedEnd = true;
158 : 231272 : return ExecClearTuple(slot);
159 : : }
160 : :
161 : : /* ----------------------------------------------------------------
162 : : * IndexNextWithReorder
163 : : *
164 : : * Like IndexNext, but this version can also re-check ORDER BY
165 : : * expressions, and reorder the tuples as necessary.
166 : : * ----------------------------------------------------------------
167 : : */
168 : : static TupleTableSlot *
169 : 41331 : IndexNextWithReorder(IndexScanState *node)
170 : : {
171 : : EState *estate;
172 : : ExprContext *econtext;
173 : : IndexScanDesc scandesc;
174 : : TupleTableSlot *slot;
175 : 41331 : ReorderTuple *topmost = NULL;
176 : : bool was_exact;
177 : : Datum *lastfetched_vals;
178 : : bool *lastfetched_nulls;
179 : : int cmp;
180 : :
3104 rhaas@postgresql.org 181 : 41331 : estate = node->ss.ps.state;
182 : :
183 : : /*
184 : : * Only forward scan is supported with reordering. Note: we can get away
185 : : * with just Asserting here because the system will not try to run the
186 : : * plan backwards if ExecSupportsBackwardScan() says it won't work.
187 : : * Currently, that is guaranteed because no index AMs support both
188 : : * amcanorderbyop and amcanbackward; if any ever do,
189 : : * ExecSupportsBackwardScan() will need to consider indexorderbys
190 : : * explicitly.
191 : : */
3767 heikki.linnakangas@i 192 [ - + ]: 41331 : Assert(!ScanDirectionIsBackward(((IndexScan *) node->ss.ps.plan)->indexorderdir));
3104 rhaas@postgresql.org 193 [ - + ]: 41331 : Assert(ScanDirectionIsForward(estate->es_direction));
194 : :
3767 heikki.linnakangas@i 195 : 41331 : scandesc = node->iss_ScanDesc;
196 : 41331 : econtext = node->ss.ps.ps_ExprContext;
2332 andres@anarazel.de 197 : 41331 : slot = node->ss.ss_ScanTupleSlot;
198 : :
3104 rhaas@postgresql.org 199 [ + + ]: 41331 : if (scandesc == NULL)
200 : : {
201 : : /*
202 : : * We reach here if the index scan is not parallel, or if we're
203 : : * serially executing an index scan that was planned to be parallel.
204 : : */
205 : 23 : scandesc = index_beginscan(node->ss.ss_currentRelation,
206 : : node->iss_RelationDesc,
207 : : estate->es_snapshot,
208 : : &node->iss_Instrument,
209 : : node->iss_NumScanKeys,
210 : : node->iss_NumOrderByKeys);
211 : :
212 : 23 : node->iss_ScanDesc = scandesc;
213 : :
214 : : /*
215 : : * If no run-time keys to calculate or they are ready, go ahead and
216 : : * pass the scankeys to the index AM.
217 : : */
218 [ + + + - ]: 23 : if (node->iss_NumRuntimeKeys == 0 || node->iss_RuntimeKeysReady)
219 : 23 : index_rescan(scandesc,
220 : 23 : node->iss_ScanKeys, node->iss_NumScanKeys,
221 : 23 : node->iss_OrderByKeys, node->iss_NumOrderByKeys);
222 : : }
223 : :
224 : : for (;;)
225 : : {
2965 andres@anarazel.de 226 [ - + ]: 43928 : CHECK_FOR_INTERRUPTS();
227 : :
228 : : /*
229 : : * Check the reorder queue first. If the topmost tuple in the queue
230 : : * has an ORDER BY value smaller than (or equal to) the value last
231 : : * returned by the index, we can return it now.
232 : : */
3767 heikki.linnakangas@i 233 [ + + ]: 43928 : if (!pairingheap_is_empty(node->iss_ReorderQueue))
234 : : {
235 : 5126 : topmost = (ReorderTuple *) pairingheap_first(node->iss_ReorderQueue);
236 : :
237 [ + + + + ]: 10249 : if (node->iss_ReachedEnd ||
238 : 5123 : cmp_orderbyvals(topmost->orderbyvals,
239 : 5123 : topmost->orderbynulls,
240 : 5123 : scandesc->xs_orderbyvals,
241 : 5123 : scandesc->xs_orderbynulls,
242 : : node) <= 0)
243 : : {
244 : : HeapTuple tuple;
245 : :
246 : 2538 : tuple = reorderqueue_pop(node);
247 : :
248 : : /* Pass 'true', as the tuple in the queue is a palloc'd copy */
2332 andres@anarazel.de 249 : 2538 : ExecForceStoreHeapTuple(tuple, slot, true);
3767 heikki.linnakangas@i 250 : 2538 : return slot;
251 : : }
252 : : }
253 [ + + ]: 38802 : else if (node->iss_ReachedEnd)
254 : : {
255 : : /* Queue is empty, and no more tuples from index. We're done. */
2332 andres@anarazel.de 256 : 9 : return ExecClearTuple(slot);
257 : : }
258 : :
259 : : /*
260 : : * Fetch next tuple from the index.
261 : : */
3767 heikki.linnakangas@i 262 : 41381 : next_indextuple:
2371 andres@anarazel.de 263 [ + + ]: 43451 : if (!index_getnext_slot(scandesc, ForwardScanDirection, slot))
264 : : {
265 : : /*
266 : : * No more tuples from the index. But we still need to drain any
267 : : * remaining tuples from the queue before we're done.
268 : : */
3767 heikki.linnakangas@i 269 : 9 : node->iss_ReachedEnd = true;
270 : 9 : continue;
271 : : }
272 : :
273 : : /*
274 : : * If the index was lossy, we have to recheck the index quals and
275 : : * ORDER BY expressions using the fetched tuple.
276 : : */
277 [ + + ]: 43442 : if (scandesc->xs_recheck)
278 : : {
279 : 4563 : econtext->ecxt_scantuple = slot;
2777 andres@anarazel.de 280 [ + + ]: 4563 : if (!ExecQualAndReset(node->indexqualorig, econtext))
281 : : {
282 : : /* Fails recheck, so drop it and loop back for another */
3767 heikki.linnakangas@i 283 [ - + ]: 2070 : InstrCountFiltered2(node, 1);
284 : : /* allow this loop to be cancellable */
2965 andres@anarazel.de 285 [ - + ]: 2070 : CHECK_FOR_INTERRUPTS();
3767 heikki.linnakangas@i 286 : 2070 : goto next_indextuple;
287 : : }
288 : : }
289 : :
290 [ + + ]: 41372 : if (scandesc->xs_recheckorderby)
291 : : {
292 : 2650 : econtext->ecxt_scantuple = slot;
293 : 2650 : ResetExprContext(econtext);
294 : 2650 : EvalOrderByExpressions(node, econtext);
295 : :
296 : : /*
297 : : * Was the ORDER BY value returned by the index accurate? The
298 : : * recheck flag means that the index can return inaccurate values,
299 : : * but then again, the value returned for any particular tuple
300 : : * could also be exactly correct. Compare the value returned by
301 : : * the index with the recalculated value. (If the value returned
302 : : * by the index happened to be exact right, we can often avoid
303 : : * pushing the tuple to the queue, just to pop it back out again.)
304 : : */
305 : 2650 : cmp = cmp_orderbyvals(node->iss_OrderByValues,
306 : 2650 : node->iss_OrderByNulls,
307 : 2650 : scandesc->xs_orderbyvals,
308 : 2650 : scandesc->xs_orderbynulls,
309 : : node);
310 [ - + ]: 2650 : if (cmp < 0)
3767 heikki.linnakangas@i 311 [ # # ]:UBC 0 : elog(ERROR, "index returned tuples in wrong order");
3767 heikki.linnakangas@i 312 [ + + ]:CBC 2650 : else if (cmp == 0)
313 : 66 : was_exact = true;
314 : : else
315 : 2584 : was_exact = false;
316 : 2650 : lastfetched_vals = node->iss_OrderByValues;
317 : 2650 : lastfetched_nulls = node->iss_OrderByNulls;
318 : : }
319 : : else
320 : : {
321 : 38722 : was_exact = true;
322 : 38722 : lastfetched_vals = scandesc->xs_orderbyvals;
323 : 38722 : lastfetched_nulls = scandesc->xs_orderbynulls;
324 : : }
325 : :
326 : : /*
327 : : * Can we return this tuple immediately, or does it need to be pushed
328 : : * to the reorder queue? If the ORDER BY expression values returned
329 : : * by the index were inaccurate, we can't return it yet, because the
330 : : * next tuple from the index might need to come before this one. Also,
331 : : * we can't return it yet if there are any smaller tuples in the queue
332 : : * already.
333 : : */
334 [ + + + + : 41417 : if (!was_exact || (topmost && cmp_orderbyvals(lastfetched_vals,
+ + ]
335 : : lastfetched_nulls,
336 : 45 : topmost->orderbyvals,
337 : 45 : topmost->orderbynulls,
338 : : node) > 0))
339 : : {
340 : : /* Put this tuple to the queue */
2371 andres@anarazel.de 341 : 2588 : reorderqueue_push(node, slot, lastfetched_vals, lastfetched_nulls);
3767 heikki.linnakangas@i 342 : 2588 : continue;
343 : : }
344 : : else
345 : : {
346 : : /* Can return this tuple immediately. */
347 : 38784 : return slot;
348 : : }
349 : : }
350 : :
351 : : /*
352 : : * if we get here it means the index scan failed so we are at the end of
353 : : * the scan..
354 : : */
355 : : return ExecClearTuple(slot);
356 : : }
357 : :
358 : : /*
359 : : * Calculate the expressions in the ORDER BY clause, based on the heap tuple.
360 : : */
361 : : static void
362 : 2650 : EvalOrderByExpressions(IndexScanState *node, ExprContext *econtext)
363 : : {
364 : : int i;
365 : : ListCell *l;
366 : : MemoryContext oldContext;
367 : :
368 : 2650 : oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
369 : :
370 : 2650 : i = 0;
371 [ + - + + : 5300 : foreach(l, node->indexorderbyorig)
+ + ]
372 : : {
373 : 2650 : ExprState *orderby = (ExprState *) lfirst(l);
374 : :
375 : 5300 : node->iss_OrderByValues[i] = ExecEvalExpr(orderby,
376 : : econtext,
3152 andres@anarazel.de 377 : 2650 : &node->iss_OrderByNulls[i]);
3767 heikki.linnakangas@i 378 : 2650 : i++;
379 : : }
380 : :
381 : 2650 : MemoryContextSwitchTo(oldContext);
382 : 2650 : }
383 : :
384 : : /*
385 : : * IndexRecheck -- access method routine to recheck a tuple in EvalPlanQual
386 : : */
387 : : static bool
5794 tgl@sss.pgh.pa.us 388 : 54 : IndexRecheck(IndexScanState *node, TupleTableSlot *slot)
389 : : {
390 : : ExprContext *econtext;
391 : :
392 : : /*
393 : : * extract necessary information from index scan node
394 : : */
395 : 54 : econtext = node->ss.ps.ps_ExprContext;
396 : :
397 : : /* Does the tuple meet the indexqual condition? */
398 : 54 : econtext->ecxt_scantuple = slot;
2777 andres@anarazel.de 399 : 54 : return ExecQualAndReset(node->indexqualorig, econtext);
400 : : }
401 : :
402 : :
403 : : /*
404 : : * Compare ORDER BY expression values.
405 : : */
406 : : static int
3767 heikki.linnakangas@i 407 : 14616 : cmp_orderbyvals(const Datum *adist, const bool *anulls,
408 : : const Datum *bdist, const bool *bnulls,
409 : : IndexScanState *node)
410 : : {
411 : : int i;
412 : : int result;
413 : :
414 [ + + ]: 14711 : for (i = 0; i < node->iss_NumOrderByKeys; i++)
415 : : {
416 : 14616 : SortSupport ssup = &node->iss_SortSupport[i];
417 : :
418 : : /*
419 : : * Handle nulls. We only need to support NULLS LAST ordering, because
420 : : * match_pathkeys_to_index() doesn't consider indexorderby
421 : : * implementation otherwise.
422 : : */
423 [ - + - - ]: 14616 : if (anulls[i] && !bnulls[i])
3767 heikki.linnakangas@i 424 :UBC 0 : return 1;
3767 heikki.linnakangas@i 425 [ + - - + ]:CBC 14616 : else if (!anulls[i] && bnulls[i])
3767 heikki.linnakangas@i 426 :UBC 0 : return -1;
3767 heikki.linnakangas@i 427 [ - + - - ]:CBC 14616 : else if (anulls[i] && bnulls[i])
3767 heikki.linnakangas@i 428 :UBC 0 : return 0;
429 : :
3767 heikki.linnakangas@i 430 :CBC 14616 : result = ssup->comparator(adist[i], bdist[i], ssup);
431 [ + + ]: 14616 : if (result != 0)
432 : 14521 : return result;
433 : : }
434 : :
435 : 95 : return 0;
436 : : }
437 : :
438 : : /*
439 : : * Pairing heap provides getting topmost (greatest) element while KNN provides
440 : : * ascending sort. That's why we invert the sort order.
441 : : */
442 : : static int
443 : 6798 : reorderqueue_cmp(const pairingheap_node *a, const pairingheap_node *b,
444 : : void *arg)
445 : : {
446 : 6798 : ReorderTuple *rta = (ReorderTuple *) a;
447 : 6798 : ReorderTuple *rtb = (ReorderTuple *) b;
448 : 6798 : IndexScanState *node = (IndexScanState *) arg;
449 : :
450 : : /* exchange argument order to invert the sort order */
2528 tgl@sss.pgh.pa.us 451 : 13596 : return cmp_orderbyvals(rtb->orderbyvals, rtb->orderbynulls,
452 : 6798 : rta->orderbyvals, rta->orderbynulls,
453 : : node);
454 : : }
455 : :
456 : : /*
457 : : * Helper function to push a tuple to the reorder queue.
458 : : */
459 : : static void
2371 andres@anarazel.de 460 : 2588 : reorderqueue_push(IndexScanState *node, TupleTableSlot *slot,
461 : : Datum *orderbyvals, bool *orderbynulls)
462 : : {
3767 heikki.linnakangas@i 463 : 2588 : IndexScanDesc scandesc = node->iss_ScanDesc;
464 : 2588 : EState *estate = node->ss.ps.state;
465 : 2588 : MemoryContext oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
466 : : ReorderTuple *rt;
467 : : int i;
468 : :
469 : 2588 : rt = (ReorderTuple *) palloc(sizeof(ReorderTuple));
2371 andres@anarazel.de 470 : 2588 : rt->htup = ExecCopySlotHeapTuple(slot);
3767 heikki.linnakangas@i 471 : 2588 : rt->orderbyvals =
472 : 2588 : (Datum *) palloc(sizeof(Datum) * scandesc->numberOfOrderBys);
473 : 2588 : rt->orderbynulls =
474 : 2588 : (bool *) palloc(sizeof(bool) * scandesc->numberOfOrderBys);
475 [ + + ]: 5176 : for (i = 0; i < node->iss_NumOrderByKeys; i++)
476 : : {
477 [ + - ]: 2588 : if (!orderbynulls[i])
478 : 2588 : rt->orderbyvals[i] = datumCopy(orderbyvals[i],
479 : 2588 : node->iss_OrderByTypByVals[i],
480 : 2588 : node->iss_OrderByTypLens[i]);
481 : : else
3767 heikki.linnakangas@i 482 :UBC 0 : rt->orderbyvals[i] = (Datum) 0;
3767 heikki.linnakangas@i 483 :CBC 2588 : rt->orderbynulls[i] = orderbynulls[i];
484 : : }
485 : 2588 : pairingheap_add(node->iss_ReorderQueue, &rt->ph_node);
486 : :
487 : 2588 : MemoryContextSwitchTo(oldContext);
488 : 2588 : }
489 : :
490 : : /*
491 : : * Helper function to pop the next tuple from the reorder queue.
492 : : */
493 : : static HeapTuple
494 : 2568 : reorderqueue_pop(IndexScanState *node)
495 : : {
496 : : HeapTuple result;
497 : : ReorderTuple *topmost;
498 : : int i;
499 : :
500 : 2568 : topmost = (ReorderTuple *) pairingheap_remove_first(node->iss_ReorderQueue);
501 : :
502 : 2568 : result = topmost->htup;
3759 tgl@sss.pgh.pa.us 503 [ + + ]: 5136 : for (i = 0; i < node->iss_NumOrderByKeys; i++)
504 : : {
505 [ - + - - ]: 2568 : if (!node->iss_OrderByTypByVals[i] && !topmost->orderbynulls[i])
3759 tgl@sss.pgh.pa.us 506 :UBC 0 : pfree(DatumGetPointer(topmost->orderbyvals[i]));
507 : : }
3767 heikki.linnakangas@i 508 :CBC 2568 : pfree(topmost->orderbyvals);
509 : 2568 : pfree(topmost->orderbynulls);
510 : 2568 : pfree(topmost);
511 : :
512 : 2568 : return result;
513 : : }
514 : :
515 : :
516 : : /* ----------------------------------------------------------------
517 : : * ExecIndexScan(node)
518 : : * ----------------------------------------------------------------
519 : : */
520 : : static TupleTableSlot *
2973 andres@anarazel.de 521 : 978546 : ExecIndexScan(PlanState *pstate)
522 : : {
523 : 978546 : IndexScanState *node = castNode(IndexScanState, pstate);
524 : :
525 : : /*
526 : : * If we have runtime keys and they've not already been set up, do it now.
527 : : */
7225 tgl@sss.pgh.pa.us 528 [ + + + + ]: 978546 : if (node->iss_NumRuntimeKeys != 0 && !node->iss_RuntimeKeysReady)
5535 529 : 11053 : ExecReScan((PlanState *) node);
530 : :
3767 heikki.linnakangas@i 531 [ + + ]: 978546 : if (node->iss_NumOrderByKeys > 0)
532 : 41331 : return ExecScan(&node->ss,
533 : : (ExecScanAccessMtd) IndexNextWithReorder,
534 : : (ExecScanRecheckMtd) IndexRecheck);
535 : : else
536 : 937215 : return ExecScan(&node->ss,
537 : : (ExecScanAccessMtd) IndexNext,
538 : : (ExecScanRecheckMtd) IndexRecheck);
539 : : }
540 : :
541 : : /* ----------------------------------------------------------------
542 : : * ExecReScanIndexScan(node)
543 : : *
544 : : * Recalculates the values of any scan keys whose value depends on
545 : : * information known at runtime, then rescans the indexed relation.
546 : : *
547 : : * Updating the scan key was formerly done separately in
548 : : * ExecUpdateIndexScanKeys. Integrating it into ReScan makes
549 : : * rescans of indices and relations/general streams more uniform.
550 : : * ----------------------------------------------------------------
551 : : */
552 : : void
5535 tgl@sss.pgh.pa.us 553 : 230142 : ExecReScanIndexScan(IndexScanState *node)
554 : : {
555 : : /*
556 : : * If we are doing runtime key calculations (ie, any of the index key
557 : : * values weren't simple Consts), compute the new key values. But first,
558 : : * reset the context so we don't leak memory as each outer tuple is
559 : : * scanned. Note this assumes that we will recalculate *all* runtime keys
560 : : * on each call.
561 : : */
7225 562 [ + + ]: 230142 : if (node->iss_NumRuntimeKeys != 0)
563 : : {
5535 564 : 224162 : ExprContext *econtext = node->iss_RuntimeContext;
565 : :
566 : 224162 : ResetExprContext(econtext);
7439 567 : 224162 : ExecIndexEvalRuntimeKeys(econtext,
568 : : node->iss_RuntimeKeys,
569 : : node->iss_NumRuntimeKeys);
570 : : }
7225 571 : 230142 : node->iss_RuntimeKeysReady = true;
572 : :
573 : : /* flush the reorder queue */
3757 heikki.linnakangas@i 574 [ + + ]: 230142 : if (node->iss_ReorderQueue)
575 : : {
576 : : HeapTuple tuple;
577 : :
578 [ + + ]: 63 : while (!pairingheap_is_empty(node->iss_ReorderQueue))
579 : : {
1300 akorotkov@postgresql 580 : 30 : tuple = reorderqueue_pop(node);
581 : 30 : heap_freetuple(tuple);
582 : : }
583 : : }
584 : :
585 : : /* reset index scan */
3125 rhaas@postgresql.org 586 [ + + ]: 230142 : if (node->iss_ScanDesc)
587 : 202943 : index_rescan(node->iss_ScanDesc,
588 : 202943 : node->iss_ScanKeys, node->iss_NumScanKeys,
589 : 202943 : node->iss_OrderByKeys, node->iss_NumOrderByKeys);
3757 heikki.linnakangas@i 590 : 230142 : node->iss_ReachedEnd = false;
591 : :
5794 tgl@sss.pgh.pa.us 592 : 230142 : ExecScanReScan(&node->ss);
7439 593 : 230142 : }
594 : :
595 : :
596 : : /*
597 : : * ExecIndexEvalRuntimeKeys
598 : : * Evaluate any runtime key values, and update the scankeys.
599 : : */
600 : : void
601 : 337370 : ExecIndexEvalRuntimeKeys(ExprContext *econtext,
602 : : IndexRuntimeKeyInfo *runtimeKeys, int numRuntimeKeys)
603 : : {
604 : : int j;
605 : : MemoryContext oldContext;
606 : :
607 : : /* We want to keep the key values in per-tuple memory */
5858 608 : 337370 : oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
609 : :
7225 610 [ + + ]: 688426 : for (j = 0; j < numRuntimeKeys; j++)
611 : : {
612 : 351056 : ScanKey scan_key = runtimeKeys[j].scan_key;
613 : 351056 : ExprState *key_expr = runtimeKeys[j].key_expr;
614 : : Datum scanvalue;
615 : : bool isNull;
616 : :
617 : : /*
618 : : * For each run-time key, extract the run-time expression and evaluate
619 : : * it with respect to the current context. We then stick the result
620 : : * into the proper scan key.
621 : : *
622 : : * Note: the result of the eval could be a pass-by-ref value that's
623 : : * stored in some outer scan's tuple, not in
624 : : * econtext->ecxt_per_tuple_memory. We assume that the outer tuple
625 : : * will stay put throughout our scan. If this is wrong, we could copy
626 : : * the result into our context explicitly, but I think that's not
627 : : * necessary.
628 : : *
629 : : * It's also entirely possible that the result of the eval is a
630 : : * toasted value. In this case we should forcibly detoast it, to
631 : : * avoid repeat detoastings each time the value is examined by an
632 : : * index support function.
633 : : */
5858 634 : 351056 : scanvalue = ExecEvalExpr(key_expr,
635 : : econtext,
636 : : &isNull);
7225 637 [ + + ]: 351056 : if (isNull)
638 : : {
5858 639 : 1483 : scan_key->sk_argument = scanvalue;
7225 640 : 1483 : scan_key->sk_flags |= SK_ISNULL;
641 : : }
642 : : else
643 : : {
5858 644 [ + + ]: 349573 : if (runtimeKeys[j].key_toastable)
645 : 34916 : scanvalue = PointerGetDatum(PG_DETOAST_DATUM(scanvalue));
646 : 349573 : scan_key->sk_argument = scanvalue;
7225 647 : 349573 : scan_key->sk_flags &= ~SK_ISNULL;
648 : : }
649 : : }
650 : :
5858 651 : 337370 : MemoryContextSwitchTo(oldContext);
7225 652 : 337370 : }
653 : :
654 : : /*
655 : : * ExecIndexEvalArrayKeys
656 : : * Evaluate any array key values, and set up to iterate through arrays.
657 : : *
658 : : * Returns true if there are array elements to consider; false means there
659 : : * is at least one null or empty array, so no match is possible. On true
660 : : * result, the scankeys are initialized with the first elements of the arrays.
661 : : */
662 : : bool
663 : 13 : ExecIndexEvalArrayKeys(ExprContext *econtext,
664 : : IndexArrayKeyInfo *arrayKeys, int numArrayKeys)
665 : : {
666 : 13 : bool result = true;
667 : : int j;
668 : : MemoryContext oldContext;
669 : :
670 : : /* We want to keep the arrays in per-tuple memory */
671 : 13 : oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
672 : :
673 [ + + ]: 26 : for (j = 0; j < numArrayKeys; j++)
674 : : {
675 : 13 : ScanKey scan_key = arrayKeys[j].scan_key;
676 : 13 : ExprState *array_expr = arrayKeys[j].array_expr;
677 : : Datum arraydatum;
678 : : bool isNull;
679 : : ArrayType *arrayval;
680 : : int16 elmlen;
681 : : bool elmbyval;
682 : : char elmalign;
683 : : int num_elems;
684 : : Datum *elem_values;
685 : : bool *elem_nulls;
686 : :
687 : : /*
688 : : * Compute and deconstruct the array expression. (Notes in
689 : : * ExecIndexEvalRuntimeKeys() apply here too.)
690 : : */
691 : 13 : arraydatum = ExecEvalExpr(array_expr,
692 : : econtext,
693 : : &isNull);
694 [ - + ]: 13 : if (isNull)
695 : : {
7225 tgl@sss.pgh.pa.us 696 :UBC 0 : result = false;
697 : 0 : break; /* no point in evaluating more */
698 : : }
7225 tgl@sss.pgh.pa.us 699 :CBC 13 : arrayval = DatumGetArrayTypeP(arraydatum);
700 : : /* We could cache this data, but not clear it's worth it */
701 : 13 : get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
702 : : &elmlen, &elmbyval, &elmalign);
703 : 13 : deconstruct_array(arrayval,
704 : : ARR_ELEMTYPE(arrayval),
705 : : elmlen, elmbyval, elmalign,
706 : : &elem_values, &elem_nulls, &num_elems);
707 [ - + ]: 13 : if (num_elems <= 0)
708 : : {
7225 tgl@sss.pgh.pa.us 709 :UBC 0 : result = false;
710 : 0 : break; /* no point in evaluating more */
711 : : }
712 : :
713 : : /*
714 : : * Note: we expect the previous array data, if any, to be
715 : : * automatically freed by resetting the per-tuple context; hence no
716 : : * pfree's here.
717 : : */
7225 tgl@sss.pgh.pa.us 718 :CBC 13 : arrayKeys[j].elem_values = elem_values;
719 : 13 : arrayKeys[j].elem_nulls = elem_nulls;
720 : 13 : arrayKeys[j].num_elems = num_elems;
721 : 13 : scan_key->sk_argument = elem_values[0];
722 [ - + ]: 13 : if (elem_nulls[0])
7225 tgl@sss.pgh.pa.us 723 :UBC 0 : scan_key->sk_flags |= SK_ISNULL;
724 : : else
7225 tgl@sss.pgh.pa.us 725 :CBC 13 : scan_key->sk_flags &= ~SK_ISNULL;
726 : 13 : arrayKeys[j].next_elem = 1;
727 : : }
728 : :
729 : 13 : MemoryContextSwitchTo(oldContext);
730 : :
731 : 13 : return result;
732 : : }
733 : :
734 : : /*
735 : : * ExecIndexAdvanceArrayKeys
736 : : * Advance to the next set of array key values, if any.
737 : : *
738 : : * Returns true if there is another set of values to consider, false if not.
739 : : * On true result, the scankeys are initialized with the next set of values.
740 : : */
741 : : bool
742 : 10164 : ExecIndexAdvanceArrayKeys(IndexArrayKeyInfo *arrayKeys, int numArrayKeys)
743 : : {
744 : 10164 : bool found = false;
745 : : int j;
746 : :
747 : : /*
748 : : * Note we advance the rightmost array key most quickly, since it will
749 : : * correspond to the lowest-order index column among the available
750 : : * qualifications. This is hypothesized to result in better locality of
751 : : * access in the index.
752 : : */
6381 753 [ + + ]: 10177 : for (j = numArrayKeys - 1; j >= 0; j--)
754 : : {
7225 755 : 26 : ScanKey scan_key = arrayKeys[j].scan_key;
756 : 26 : int next_elem = arrayKeys[j].next_elem;
757 : 26 : int num_elems = arrayKeys[j].num_elems;
758 : 26 : Datum *elem_values = arrayKeys[j].elem_values;
759 : 26 : bool *elem_nulls = arrayKeys[j].elem_nulls;
760 : :
761 [ + + ]: 26 : if (next_elem >= num_elems)
762 : : {
763 : 13 : next_elem = 0;
764 : 13 : found = false; /* need to advance next array key */
765 : : }
766 : : else
767 : 13 : found = true;
768 : 26 : scan_key->sk_argument = elem_values[next_elem];
769 [ - + ]: 26 : if (elem_nulls[next_elem])
7225 tgl@sss.pgh.pa.us 770 :UBC 0 : scan_key->sk_flags |= SK_ISNULL;
771 : : else
7225 tgl@sss.pgh.pa.us 772 :CBC 26 : scan_key->sk_flags &= ~SK_ISNULL;
773 : 26 : arrayKeys[j].next_elem = next_elem + 1;
774 [ + + ]: 26 : if (found)
775 : 13 : break;
776 : : }
777 : :
778 : 10164 : return found;
779 : : }
780 : :
781 : :
782 : : /* ----------------------------------------------------------------
783 : : * ExecEndIndexScan
784 : : * ----------------------------------------------------------------
785 : : */
786 : : void
8311 787 : 81984 : ExecEndIndexScan(IndexScanState *node)
788 : : {
789 : : Relation indexRelationDesc;
790 : : IndexScanDesc indexScanDesc;
791 : :
792 : : /*
793 : : * extract information from the node
794 : : */
7439 795 : 81984 : indexRelationDesc = node->iss_RelationDesc;
796 : 81984 : indexScanDesc = node->iss_ScanDesc;
797 : :
798 : : /*
799 : : * When ending a parallel worker, copy the statistics gathered by the
800 : : * worker back into shared memory so that it can be picked up by the main
801 : : * process to report in EXPLAIN ANALYZE
802 : : */
179 pg@bowt.ie 803 [ + + + + ]: 81984 : if (node->iss_SharedInfo != NULL && IsParallelWorker())
804 : : {
805 : : IndexScanInstrumentation *winstrument;
806 : :
807 [ - + ]: 135 : Assert(ParallelWorkerNumber <= node->iss_SharedInfo->num_workers);
808 : 135 : winstrument = &node->iss_SharedInfo->winstrument[ParallelWorkerNumber];
809 : :
810 : : /*
811 : : * We have to accumulate the stats rather than performing a memcpy.
812 : : * When a Gather/GatherMerge node finishes it will perform planner
813 : : * shutdown on the workers. On rescan it will spin up new workers
814 : : * which will have a new IndexOnlyScanState and zeroed stats.
815 : : */
816 : 135 : winstrument->nsearches += node->iss_Instrument.nsearches;
817 : : }
818 : :
819 : : /*
820 : : * close the index relation (no-op if we didn't open it)
821 : : */
6679 tgl@sss.pgh.pa.us 822 [ + + ]: 81984 : if (indexScanDesc)
823 : 65983 : index_endscan(indexScanDesc);
824 [ + + ]: 81984 : if (indexRelationDesc)
825 : 80290 : index_close(indexRelationDesc, NoLock);
10651 scrappy@hub.org 826 : 81984 : }
827 : :
828 : : /* ----------------------------------------------------------------
829 : : * ExecIndexMarkPos
830 : : *
831 : : * Note: we assume that no caller attempts to set a mark before having read
832 : : * at least one tuple. Otherwise, iss_ScanDesc might still be NULL.
833 : : * ----------------------------------------------------------------
834 : : */
835 : : void
8311 tgl@sss.pgh.pa.us 836 : 3030 : ExecIndexMarkPos(IndexScanState *node)
837 : : {
2779 838 : 3030 : EState *estate = node->ss.ps.state;
2193 andres@anarazel.de 839 : 3030 : EPQState *epqstate = estate->es_epq_active;
840 : :
841 [ + + ]: 3030 : if (epqstate != NULL)
842 : : {
843 : : /*
844 : : * We are inside an EvalPlanQual recheck. If a test tuple exists for
845 : : * this relation, then we shouldn't access the index at all. We would
846 : : * instead need to save, and later restore, the state of the
847 : : * relsubs_done flag, so that re-fetching the test tuple is possible.
848 : : * However, given the assumption that no caller sets a mark at the
849 : : * start of the scan, we can only get here with relsubs_done[i]
850 : : * already set, and so no state need be saved.
851 : : */
2779 tgl@sss.pgh.pa.us 852 : 1 : Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
853 : :
854 [ - + ]: 1 : Assert(scanrelid > 0);
2193 andres@anarazel.de 855 [ - + ]: 1 : if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
2193 andres@anarazel.de 856 [ # # ]:UBC 0 : epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
857 : : {
858 : : /* Verify the claim above */
2193 andres@anarazel.de 859 [ - + ]:CBC 1 : if (!epqstate->relsubs_done[scanrelid - 1])
2779 tgl@sss.pgh.pa.us 860 [ # # ]:UBC 0 : elog(ERROR, "unexpected ExecIndexMarkPos call in EPQ recheck");
2779 tgl@sss.pgh.pa.us 861 :CBC 1 : return;
862 : : }
863 : : }
864 : :
7439 865 : 3029 : index_markpos(node->iss_ScanDesc);
866 : : }
867 : :
868 : : /* ----------------------------------------------------------------
869 : : * ExecIndexRestrPos
870 : : * ----------------------------------------------------------------
871 : : */
872 : : void
8311 873 : 27009 : ExecIndexRestrPos(IndexScanState *node)
874 : : {
2779 875 : 27009 : EState *estate = node->ss.ps.state;
2193 andres@anarazel.de 876 : 27009 : EPQState *epqstate = estate->es_epq_active;
877 : :
878 [ - + ]: 27009 : if (estate->es_epq_active != NULL)
879 : : {
880 : : /* See comments in ExecIndexMarkPos */
2779 tgl@sss.pgh.pa.us 881 :UBC 0 : Index scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
882 : :
883 [ # # ]: 0 : Assert(scanrelid > 0);
2193 andres@anarazel.de 884 [ # # ]: 0 : if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
885 [ # # ]: 0 : epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
886 : : {
887 : : /* Verify the claim above */
888 [ # # ]: 0 : if (!epqstate->relsubs_done[scanrelid - 1])
2779 tgl@sss.pgh.pa.us 889 [ # # ]: 0 : elog(ERROR, "unexpected ExecIndexRestrPos call in EPQ recheck");
890 : 0 : return;
891 : : }
892 : : }
893 : :
7439 tgl@sss.pgh.pa.us 894 :CBC 27009 : index_restrpos(node->iss_ScanDesc);
895 : : }
896 : :
897 : : /* ----------------------------------------------------------------
898 : : * ExecInitIndexScan
899 : : *
900 : : * Initializes the index scan's state information, creates
901 : : * scan keys, and opens the base and index relations.
902 : : *
903 : : * Note: index scans have 2 sets of state information because
904 : : * we have to keep track of the base relation and the
905 : : * index relation.
906 : : * ----------------------------------------------------------------
907 : : */
908 : : IndexScanState *
7130 909 : 82387 : ExecInitIndexScan(IndexScan *node, EState *estate, int eflags)
910 : : {
911 : : IndexScanState *indexstate;
912 : : Relation currentRelation;
913 : : LOCKMODE lockmode;
914 : :
915 : : /*
916 : : * create state structure
917 : : */
8311 918 : 82387 : indexstate = makeNode(IndexScanState);
919 : 82387 : indexstate->ss.ps.plan = (Plan *) node;
920 : 82387 : indexstate->ss.ps.state = estate;
2973 andres@anarazel.de 921 : 82387 : indexstate->ss.ps.ExecProcNode = ExecIndexScan;
922 : :
923 : : /*
924 : : * Miscellaneous initialization
925 : : *
926 : : * create expression context for node
927 : : */
8311 tgl@sss.pgh.pa.us 928 : 82387 : ExecAssignExprContext(estate, &indexstate->ss.ps);
929 : :
930 : : /*
931 : : * open the scan relation
932 : : */
2759 andres@anarazel.de 933 : 82387 : currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
934 : :
935 : 82387 : indexstate->ss.ss_currentRelation = currentRelation;
936 : 82387 : indexstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
937 : :
938 : : /*
939 : : * get the scan type from the relation descriptor.
940 : : */
941 : 82387 : ExecInitScanTupleSlot(estate, &indexstate->ss,
942 : : RelationGetDescr(currentRelation),
943 : : table_slot_callbacks(currentRelation));
944 : :
945 : : /*
946 : : * Initialize result type and projection.
947 : : */
2493 948 : 82387 : ExecInitResultTypeTL(&indexstate->ss.ps);
2759 949 : 82387 : ExecAssignScanProjectionInfo(&indexstate->ss);
950 : :
951 : : /*
952 : : * initialize child expressions
953 : : *
954 : : * Note: we don't initialize all of the indexqual expression, only the
955 : : * sub-parts corresponding to runtime keys (see below). Likewise for
956 : : * indexorderby, if any. But the indexqualorig expression is always
957 : : * initialized even though it will only be used in some uncommon cases ---
958 : : * would be nice to improve that. (Problem is that any SubPlans present
959 : : * in the expression must be found now...)
960 : : */
3098 961 : 82387 : indexstate->ss.ps.qual =
962 : 82387 : ExecInitQual(node->scan.plan.qual, (PlanState *) indexstate);
963 : 82387 : indexstate->indexqualorig =
964 : 82387 : ExecInitQual(node->indexqualorig, (PlanState *) indexstate);
965 : 82387 : indexstate->indexorderbyorig =
966 : 82387 : ExecInitExprList(node->indexorderbyorig, (PlanState *) indexstate);
967 : :
968 : : /*
969 : : * If we are just doing EXPLAIN (ie, aren't going to run the plan), stop
970 : : * here. This allows an index-advisor plugin to EXPLAIN a plan containing
971 : : * references to nonexistent indexes.
972 : : */
6679 tgl@sss.pgh.pa.us 973 [ + + ]: 82387 : if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
974 : 1694 : return indexstate;
975 : :
976 : : /* Open the index relation. */
2347 977 : 80693 : lockmode = exec_rt_fetch(node->scan.scanrelid, estate)->rellockmode;
978 : 80693 : indexstate->iss_RelationDesc = index_open(node->indexid, lockmode);
979 : :
980 : : /*
981 : : * Initialize index-specific scan state
982 : : */
9155 983 : 80693 : indexstate->iss_RuntimeKeysReady = false;
5392 984 : 80693 : indexstate->iss_RuntimeKeys = NULL;
985 : 80693 : indexstate->iss_NumRuntimeKeys = 0;
986 : :
987 : : /*
988 : : * build the index scan keys from the index qualification
989 : : */
7225 990 : 80693 : ExecIndexBuildScanKeys((PlanState *) indexstate,
991 : : indexstate->iss_RelationDesc,
992 : : node->indexqual,
993 : : false,
994 : 80693 : &indexstate->iss_ScanKeys,
995 : : &indexstate->iss_NumScanKeys,
996 : : &indexstate->iss_RuntimeKeys,
997 : : &indexstate->iss_NumRuntimeKeys,
998 : : NULL, /* no ArrayKeys */
999 : : NULL);
1000 : :
1001 : : /*
1002 : : * any ORDER BY exprs have to be turned into scankeys in the same way
1003 : : */
5392 1004 : 80693 : ExecIndexBuildScanKeys((PlanState *) indexstate,
1005 : : indexstate->iss_RelationDesc,
1006 : : node->indexorderby,
1007 : : true,
1008 : 80693 : &indexstate->iss_OrderByKeys,
1009 : : &indexstate->iss_NumOrderByKeys,
1010 : : &indexstate->iss_RuntimeKeys,
1011 : : &indexstate->iss_NumRuntimeKeys,
1012 : : NULL, /* no ArrayKeys */
1013 : : NULL);
1014 : :
1015 : : /* Initialize sort support, if we need to re-check ORDER BY exprs */
3767 heikki.linnakangas@i 1016 [ + + ]: 80693 : if (indexstate->iss_NumOrderByKeys > 0)
1017 : : {
1018 : 23 : int numOrderByKeys = indexstate->iss_NumOrderByKeys;
1019 : : int i;
1020 : : ListCell *lco;
1021 : : ListCell *lcx;
1022 : :
1023 : : /*
1024 : : * Prepare sort support, and look up the data type for each ORDER BY
1025 : : * expression.
1026 : : */
3765 tgl@sss.pgh.pa.us 1027 [ - + ]: 23 : Assert(numOrderByKeys == list_length(node->indexorderbyops));
3761 1028 [ - + ]: 23 : Assert(numOrderByKeys == list_length(node->indexorderbyorig));
1029 : 23 : indexstate->iss_SortSupport = (SortSupportData *)
3767 heikki.linnakangas@i 1030 : 23 : palloc0(numOrderByKeys * sizeof(SortSupportData));
3761 tgl@sss.pgh.pa.us 1031 : 23 : indexstate->iss_OrderByTypByVals = (bool *)
3767 heikki.linnakangas@i 1032 : 23 : palloc(numOrderByKeys * sizeof(bool));
3761 tgl@sss.pgh.pa.us 1033 : 23 : indexstate->iss_OrderByTypLens = (int16 *)
3767 heikki.linnakangas@i 1034 : 23 : palloc(numOrderByKeys * sizeof(int16));
3765 tgl@sss.pgh.pa.us 1035 : 23 : i = 0;
3761 1036 [ + - + + : 46 : forboth(lco, node->indexorderbyops, lcx, node->indexorderbyorig)
+ - + + +
+ + - +
+ ]
1037 : : {
1038 : 23 : Oid orderbyop = lfirst_oid(lco);
1039 : 23 : Node *orderbyexpr = (Node *) lfirst(lcx);
1040 : 23 : Oid orderbyType = exprType(orderbyexpr);
3380 1041 : 23 : Oid orderbyColl = exprCollation(orderbyexpr);
1042 : 23 : SortSupport orderbysort = &indexstate->iss_SortSupport[i];
1043 : :
1044 : : /* Initialize sort support */
1045 : 23 : orderbysort->ssup_cxt = CurrentMemoryContext;
1046 : 23 : orderbysort->ssup_collation = orderbyColl;
1047 : : /* See cmp_orderbyvals() comments on NULLS LAST */
1048 : 23 : orderbysort->ssup_nulls_first = false;
1049 : : /* ssup_attno is unused here and elsewhere */
1050 : 23 : orderbysort->ssup_attno = 0;
1051 : : /* No abbreviation */
1052 : 23 : orderbysort->abbreviate = false;
1053 : 23 : PrepareSortSupportFromOrderingOp(orderbyop, orderbysort);
1054 : :
3767 heikki.linnakangas@i 1055 : 23 : get_typlenbyval(orderbyType,
1056 : 23 : &indexstate->iss_OrderByTypLens[i],
1057 : 23 : &indexstate->iss_OrderByTypByVals[i]);
3765 tgl@sss.pgh.pa.us 1058 : 23 : i++;
1059 : : }
1060 : :
1061 : : /* allocate arrays to hold the re-calculated distances */
3761 1062 : 23 : indexstate->iss_OrderByValues = (Datum *)
1063 : 23 : palloc(numOrderByKeys * sizeof(Datum));
1064 : 23 : indexstate->iss_OrderByNulls = (bool *)
1065 : 23 : palloc(numOrderByKeys * sizeof(bool));
1066 : :
1067 : : /* and initialize the reorder queue */
3767 heikki.linnakangas@i 1068 : 23 : indexstate->iss_ReorderQueue = pairingheap_allocate(reorderqueue_cmp,
1069 : : indexstate);
1070 : : }
1071 : :
1072 : : /*
1073 : : * If we have runtime keys, we need an ExprContext to evaluate them. The
1074 : : * node's standard context won't do because we want to reset that context
1075 : : * for every tuple. So, build another context just like the other one...
1076 : : * -tgl 7/11/00
1077 : : */
7225 tgl@sss.pgh.pa.us 1078 [ + + ]: 80693 : if (indexstate->iss_NumRuntimeKeys != 0)
1079 : : {
8311 1080 : 35576 : ExprContext *stdecontext = indexstate->ss.ps.ps_ExprContext;
1081 : :
1082 : 35576 : ExecAssignExprContext(estate, &indexstate->ss.ps);
1083 : 35576 : indexstate->iss_RuntimeContext = indexstate->ss.ps.ps_ExprContext;
1084 : 35576 : indexstate->ss.ps.ps_ExprContext = stdecontext;
1085 : : }
1086 : : else
1087 : : {
9187 1088 : 45117 : indexstate->iss_RuntimeContext = NULL;
1089 : : }
1090 : :
1091 : : /*
1092 : : * all done.
1093 : : */
8311 1094 : 80693 : return indexstate;
1095 : : }
1096 : :
1097 : :
1098 : : /*
1099 : : * ExecIndexBuildScanKeys
1100 : : * Build the index scan keys from the index qualification expressions
1101 : : *
1102 : : * The index quals are passed to the index AM in the form of a ScanKey array.
1103 : : * This routine sets up the ScanKeys, fills in all constant fields of the
1104 : : * ScanKeys, and prepares information about the keys that have non-constant
1105 : : * comparison values. We divide index qual expressions into five types:
1106 : : *
1107 : : * 1. Simple operator with constant comparison value ("indexkey op constant").
1108 : : * For these, we just fill in a ScanKey containing the constant value.
1109 : : *
1110 : : * 2. Simple operator with non-constant value ("indexkey op expression").
1111 : : * For these, we create a ScanKey with everything filled in except the
1112 : : * expression value, and set up an IndexRuntimeKeyInfo struct to drive
1113 : : * evaluation of the expression at the right times.
1114 : : *
1115 : : * 3. RowCompareExpr ("(indexkey, indexkey, ...) op (expr, expr, ...)").
1116 : : * For these, we create a header ScanKey plus a subsidiary ScanKey array,
1117 : : * as specified in access/skey.h. The elements of the row comparison
1118 : : * can have either constant or non-constant comparison values.
1119 : : *
1120 : : * 4. ScalarArrayOpExpr ("indexkey op ANY (array-expression)"). If the index
1121 : : * supports amsearcharray, we handle these the same as simple operators,
1122 : : * setting the SK_SEARCHARRAY flag to tell the AM to handle them. Otherwise,
1123 : : * we create a ScanKey with everything filled in except the comparison value,
1124 : : * and set up an IndexArrayKeyInfo struct to drive processing of the qual.
1125 : : * (Note that if we use an IndexArrayKeyInfo struct, the array expression is
1126 : : * always treated as requiring runtime evaluation, even if it's a constant.)
1127 : : *
1128 : : * 5. NullTest ("indexkey IS NULL/IS NOT NULL"). We just fill in the
1129 : : * ScanKey properly.
1130 : : *
1131 : : * This code is also used to prepare ORDER BY expressions for amcanorderbyop
1132 : : * indexes. The behavior is exactly the same, except that we have to look up
1133 : : * the operator differently. Note that only cases 1 and 2 are currently
1134 : : * possible for ORDER BY.
1135 : : *
1136 : : * Input params are:
1137 : : *
1138 : : * planstate: executor state node we are working for
1139 : : * index: the index we are building scan keys for
1140 : : * quals: indexquals (or indexorderbys) expressions
1141 : : * isorderby: true if processing ORDER BY exprs, false if processing quals
1142 : : * *runtimeKeys: ptr to pre-existing IndexRuntimeKeyInfos, or NULL if none
1143 : : * *numRuntimeKeys: number of pre-existing runtime keys
1144 : : *
1145 : : * Output params are:
1146 : : *
1147 : : * *scanKeys: receives ptr to array of ScanKeys
1148 : : * *numScanKeys: receives number of scankeys
1149 : : * *runtimeKeys: receives ptr to array of IndexRuntimeKeyInfos, or NULL if none
1150 : : * *numRuntimeKeys: receives number of runtime keys
1151 : : * *arrayKeys: receives ptr to array of IndexArrayKeyInfos, or NULL if none
1152 : : * *numArrayKeys: receives number of array keys
1153 : : *
1154 : : * Caller may pass NULL for arrayKeys and numArrayKeys to indicate that
1155 : : * IndexArrayKeyInfos are not supported.
1156 : : */
1157 : : void
5079 1158 : 183050 : ExecIndexBuildScanKeys(PlanState *planstate, Relation index,
1159 : : List *quals, bool isorderby,
1160 : : ScanKey *scanKeys, int *numScanKeys,
1161 : : IndexRuntimeKeyInfo **runtimeKeys, int *numRuntimeKeys,
1162 : : IndexArrayKeyInfo **arrayKeys, int *numArrayKeys)
1163 : : {
1164 : : ListCell *qual_cell;
1165 : : ScanKey scan_keys;
1166 : : IndexRuntimeKeyInfo *runtime_keys;
1167 : : IndexArrayKeyInfo *array_keys;
1168 : : int n_scan_keys;
1169 : : int n_runtime_keys;
1170 : : int max_runtime_keys;
1171 : : int n_array_keys;
1172 : : int j;
1173 : :
1174 : : /* Allocate array for ScanKey structs: one per qual */
5392 1175 : 183050 : n_scan_keys = list_length(quals);
1176 : 183050 : scan_keys = (ScanKey) palloc(n_scan_keys * sizeof(ScanKeyData));
1177 : :
1178 : : /*
1179 : : * runtime_keys array is dynamically resized as needed. We handle it this
1180 : : * way so that the same runtime keys array can be shared between
1181 : : * indexquals and indexorderbys, which will be processed in separate calls
1182 : : * of this function. Caller must be sure to pass in NULL/0 for first
1183 : : * call.
1184 : : */
1185 : 183050 : runtime_keys = *runtimeKeys;
1186 : 183050 : n_runtime_keys = max_runtime_keys = *numRuntimeKeys;
1187 : :
1188 : : /* Allocate array_keys as large as it could possibly need to be */
1189 : : array_keys = (IndexArrayKeyInfo *)
7225 1190 : 183050 : palloc0(n_scan_keys * sizeof(IndexArrayKeyInfo));
1191 : 183050 : n_array_keys = 0;
1192 : :
1193 : : /*
1194 : : * for each opclause in the given qual, convert the opclause into a single
1195 : : * scan key
1196 : : */
6355 1197 : 183050 : j = 0;
1198 [ + + + + : 292785 : foreach(qual_cell, quals)
+ + ]
1199 : : {
1200 : 109735 : Expr *clause = (Expr *) lfirst(qual_cell);
1201 : 109735 : ScanKey this_scan_key = &scan_keys[j++];
1202 : : Oid opno; /* operator's OID */
1203 : : RegProcedure opfuncid; /* operator proc id used in scan */
1204 : : Oid opfamily; /* opfamily of index column */
1205 : : int op_strategy; /* operator's strategy number */
1206 : : Oid op_lefttype; /* operator's declared input types */
1207 : : Oid op_righttype;
1208 : : Expr *leftop; /* expr on lhs of operator */
1209 : : Expr *rightop; /* expr on rhs ... */
1210 : : AttrNumber varattno; /* att number used in scan */
1211 : : int indnkeyatts;
1212 : :
2709 teodor@sigaev.ru 1213 : 109735 : indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index);
7225 tgl@sss.pgh.pa.us 1214 [ + + ]: 109735 : if (IsA(clause, OpExpr))
1215 : : {
1216 : : /* indexkey op const or indexkey op expression */
1217 : 108566 : int flags = 0;
1218 : : Datum scanvalue;
1219 : :
6355 1220 : 108566 : opno = ((OpExpr *) clause)->opno;
7225 1221 : 108566 : opfuncid = ((OpExpr *) clause)->opfuncid;
1222 : :
1223 : : /*
1224 : : * leftop should be the index key Var, possibly relabeled
1225 : : */
1226 : 108566 : leftop = (Expr *) get_leftop(clause);
1227 : :
1228 [ + - - + ]: 108566 : if (leftop && IsA(leftop, RelabelType))
7225 tgl@sss.pgh.pa.us 1229 :UBC 0 : leftop = ((RelabelType *) leftop)->arg;
1230 : :
7225 tgl@sss.pgh.pa.us 1231 [ - + ]:CBC 108566 : Assert(leftop != NULL);
1232 : :
1233 [ + - ]: 108566 : if (!(IsA(leftop, Var) &&
5079 1234 [ - + ]: 108566 : ((Var *) leftop)->varno == INDEX_VAR))
7225 tgl@sss.pgh.pa.us 1235 [ # # ]:UBC 0 : elog(ERROR, "indexqual doesn't have key on left side");
1236 : :
7225 tgl@sss.pgh.pa.us 1237 :CBC 108566 : varattno = ((Var *) leftop)->varattno;
2709 teodor@sigaev.ru 1238 [ + - - + ]: 108566 : if (varattno < 1 || varattno > indnkeyatts)
6355 tgl@sss.pgh.pa.us 1239 [ # # ]:UBC 0 : elog(ERROR, "bogus index qualification");
1240 : :
1241 : : /*
1242 : : * We have to look up the operator's strategy number. This
1243 : : * provides a cross-check that the operator does match the index.
1244 : : */
6355 tgl@sss.pgh.pa.us 1245 :CBC 108566 : opfamily = index->rd_opfamily[varattno - 1];
1246 : :
5392 1247 : 108566 : get_op_opfamily_properties(opno, opfamily, isorderby,
1248 : : &op_strategy,
1249 : : &op_lefttype,
1250 : : &op_righttype);
1251 : :
1252 [ + + ]: 108566 : if (isorderby)
1253 : 99 : flags |= SK_ORDER_BY;
1254 : :
1255 : : /*
1256 : : * rightop is the constant or variable comparison value
1257 : : */
7225 1258 : 108566 : rightop = (Expr *) get_rightop(clause);
1259 : :
1260 [ + - + + ]: 108566 : if (rightop && IsA(rightop, RelabelType))
1261 : 424 : rightop = ((RelabelType *) rightop)->arg;
1262 : :
1263 [ - + ]: 108566 : Assert(rightop != NULL);
1264 : :
1265 [ + + ]: 108566 : if (IsA(rightop, Const))
1266 : : {
1267 : : /* OK, simple constant comparison value */
1268 : 66014 : scanvalue = ((Const *) rightop)->constvalue;
1269 [ - + ]: 66014 : if (((Const *) rightop)->constisnull)
7225 tgl@sss.pgh.pa.us 1270 :UBC 0 : flags |= SK_ISNULL;
1271 : : }
1272 : : else
1273 : : {
1274 : : /* Need to treat this one as a runtime key */
5392 tgl@sss.pgh.pa.us 1275 [ + + ]:CBC 42552 : if (n_runtime_keys >= max_runtime_keys)
1276 : : {
1277 [ + + ]: 37619 : if (max_runtime_keys == 0)
1278 : : {
1279 : 37616 : max_runtime_keys = 8;
1280 : : runtime_keys = (IndexRuntimeKeyInfo *)
1281 : 37616 : palloc(max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
1282 : : }
1283 : : else
1284 : : {
1285 : 3 : max_runtime_keys *= 2;
1286 : : runtime_keys = (IndexRuntimeKeyInfo *)
1287 : 3 : repalloc(runtime_keys, max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
1288 : : }
1289 : : }
7225 1290 : 42552 : runtime_keys[n_runtime_keys].scan_key = this_scan_key;
1291 : 85104 : runtime_keys[n_runtime_keys].key_expr =
1292 : 42552 : ExecInitExpr(rightop, planstate);
5858 1293 : 42552 : runtime_keys[n_runtime_keys].key_toastable =
1294 : 42552 : TypeIsToastable(op_righttype);
7225 1295 : 42552 : n_runtime_keys++;
1296 : 42552 : scanvalue = (Datum) 0;
1297 : : }
1298 : :
1299 : : /*
1300 : : * initialize the scan key's fields appropriately
1301 : : */
1302 : 108566 : ScanKeyEntryInitialize(this_scan_key,
1303 : : flags,
1304 : : varattno, /* attribute number to scan */
1305 : : op_strategy, /* op's strategy */
1306 : : op_righttype, /* strategy subtype */
1307 : : ((OpExpr *) clause)->inputcollid, /* collation */
1308 : : opfuncid, /* reg proc to use */
1309 : : scanvalue); /* constant */
1310 : : }
7164 1311 [ + + ]: 1169 : else if (IsA(clause, RowCompareExpr))
1312 : : {
1313 : : /* (indexkey, indexkey, ...) op (expression, expression, ...) */
1314 : 42 : RowCompareExpr *rc = (RowCompareExpr *) clause;
1315 : : ScanKey first_sub_key;
1316 : : int n_sub_key;
1317 : : ListCell *largs_cell;
1318 : : ListCell *rargs_cell;
1319 : : ListCell *opnos_cell;
1320 : : ListCell *collids_cell;
1321 : :
5392 1322 [ - + ]: 42 : Assert(!isorderby);
1323 : :
1324 : : first_sub_key = (ScanKey)
1325 : 42 : palloc(list_length(rc->opnos) * sizeof(ScanKeyData));
1326 : 42 : n_sub_key = 0;
1327 : :
1328 : : /* Scan RowCompare columns and generate subsidiary ScanKey items */
2382 1329 [ + - + + : 126 : forfour(largs_cell, rc->largs, rargs_cell, rc->rargs,
+ - + + +
- + + + -
+ + + + +
- + - + -
+ + ]
1330 : : opnos_cell, rc->opnos, collids_cell, rc->inputcollids)
1331 : : {
5392 1332 : 84 : ScanKey this_sub_key = &first_sub_key[n_sub_key];
7164 1333 : 84 : int flags = SK_ROW_MEMBER;
1334 : : Datum scanvalue;
1335 : : Oid inputcollation;
1336 : :
2382 1337 : 84 : leftop = (Expr *) lfirst(largs_cell);
1338 : 84 : rightop = (Expr *) lfirst(rargs_cell);
1339 : 84 : opno = lfirst_oid(opnos_cell);
1340 : 84 : inputcollation = lfirst_oid(collids_cell);
1341 : :
1342 : : /*
1343 : : * leftop should be the index key Var, possibly relabeled
1344 : : */
7164 1345 [ + - - + ]: 84 : if (leftop && IsA(leftop, RelabelType))
7164 tgl@sss.pgh.pa.us 1346 :UBC 0 : leftop = ((RelabelType *) leftop)->arg;
1347 : :
7164 tgl@sss.pgh.pa.us 1348 [ - + ]:CBC 84 : Assert(leftop != NULL);
1349 : :
1350 [ + - ]: 84 : if (!(IsA(leftop, Var) &&
5079 1351 [ - + ]: 84 : ((Var *) leftop)->varno == INDEX_VAR))
7164 tgl@sss.pgh.pa.us 1352 [ # # ]:UBC 0 : elog(ERROR, "indexqual doesn't have key on left side");
1353 : :
7164 tgl@sss.pgh.pa.us 1354 :CBC 84 : varattno = ((Var *) leftop)->varattno;
1355 : :
1356 : : /*
1357 : : * We have to look up the operator's associated support
1358 : : * function
1359 : : */
191 peter@eisentraut.org 1360 [ + - + - ]: 84 : if (!index->rd_indam->amcanorder ||
2709 teodor@sigaev.ru 1361 [ - + ]: 84 : varattno < 1 || varattno > indnkeyatts)
5858 tgl@sss.pgh.pa.us 1362 [ # # ]:UBC 0 : elog(ERROR, "bogus RowCompare index qualification");
5858 tgl@sss.pgh.pa.us 1363 :CBC 84 : opfamily = index->rd_opfamily[varattno - 1];
1364 : :
5392 1365 : 84 : get_op_opfamily_properties(opno, opfamily, isorderby,
1366 : : &op_strategy,
1367 : : &op_lefttype,
1368 : : &op_righttype);
1369 : :
234 peter@eisentraut.org 1370 [ - + ]: 84 : if (op_strategy != rc->cmptype)
5858 tgl@sss.pgh.pa.us 1371 [ # # ]:UBC 0 : elog(ERROR, "RowCompare index qualification contains wrong operator");
1372 : :
5858 tgl@sss.pgh.pa.us 1373 :CBC 84 : opfuncid = get_opfamily_proc(opfamily,
1374 : : op_lefttype,
1375 : : op_righttype,
1376 : : BTORDER_PROC);
2966 1377 [ - + ]: 84 : if (!RegProcedureIsValid(opfuncid))
2966 tgl@sss.pgh.pa.us 1378 [ # # ]:UBC 0 : elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
1379 : : BTORDER_PROC, op_lefttype, op_righttype, opfamily);
1380 : :
1381 : : /*
1382 : : * rightop is the constant or variable comparison value
1383 : : */
7164 tgl@sss.pgh.pa.us 1384 [ + - - + ]:CBC 84 : if (rightop && IsA(rightop, RelabelType))
7164 tgl@sss.pgh.pa.us 1385 :UBC 0 : rightop = ((RelabelType *) rightop)->arg;
1386 : :
7164 tgl@sss.pgh.pa.us 1387 [ - + ]:CBC 84 : Assert(rightop != NULL);
1388 : :
1389 [ + - ]: 84 : if (IsA(rightop, Const))
1390 : : {
1391 : : /* OK, simple constant comparison value */
1392 : 84 : scanvalue = ((Const *) rightop)->constvalue;
1393 [ + + ]: 84 : if (((Const *) rightop)->constisnull)
1394 : 18 : flags |= SK_ISNULL;
1395 : : }
1396 : : else
1397 : : {
1398 : : /* Need to treat this one as a runtime key */
5392 tgl@sss.pgh.pa.us 1399 [ # # ]:UBC 0 : if (n_runtime_keys >= max_runtime_keys)
1400 : : {
1401 [ # # ]: 0 : if (max_runtime_keys == 0)
1402 : : {
1403 : 0 : max_runtime_keys = 8;
1404 : : runtime_keys = (IndexRuntimeKeyInfo *)
1405 : 0 : palloc(max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
1406 : : }
1407 : : else
1408 : : {
1409 : 0 : max_runtime_keys *= 2;
1410 : : runtime_keys = (IndexRuntimeKeyInfo *)
1411 : 0 : repalloc(runtime_keys, max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
1412 : : }
1413 : : }
7164 1414 : 0 : runtime_keys[n_runtime_keys].scan_key = this_sub_key;
1415 : 0 : runtime_keys[n_runtime_keys].key_expr =
1416 : 0 : ExecInitExpr(rightop, planstate);
5858 1417 : 0 : runtime_keys[n_runtime_keys].key_toastable =
1418 : 0 : TypeIsToastable(op_righttype);
7164 1419 : 0 : n_runtime_keys++;
1420 : 0 : scanvalue = (Datum) 0;
1421 : : }
1422 : :
1423 : : /*
1424 : : * initialize the subsidiary scan key's fields appropriately
1425 : : */
7164 tgl@sss.pgh.pa.us 1426 :CBC 84 : ScanKeyEntryInitialize(this_sub_key,
1427 : : flags,
1428 : : varattno, /* attribute number */
1429 : : op_strategy, /* op's strategy */
1430 : : op_righttype, /* strategy subtype */
1431 : : inputcollation, /* collation */
1432 : : opfuncid, /* reg proc to use */
1433 : : scanvalue); /* constant */
5392 1434 : 84 : n_sub_key++;
1435 : : }
1436 : :
1437 : : /* Mark the last subsidiary scankey correctly */
1438 : 42 : first_sub_key[n_sub_key - 1].sk_flags |= SK_ROW_END;
1439 : :
1440 : : /*
1441 : : * We don't use ScanKeyEntryInitialize for the header because it
1442 : : * isn't going to contain a valid sk_func pointer.
1443 : : */
7164 1444 [ + - + - : 420 : MemSet(this_scan_key, 0, sizeof(ScanKeyData));
+ - + - +
+ ]
1445 : 42 : this_scan_key->sk_flags = SK_ROW_HEADER;
1446 : 42 : this_scan_key->sk_attno = first_sub_key->sk_attno;
234 peter@eisentraut.org 1447 : 42 : this_scan_key->sk_strategy = rc->cmptype;
1448 : : /* sk_subtype, sk_collation, sk_func not used in a header */
7164 tgl@sss.pgh.pa.us 1449 : 42 : this_scan_key->sk_argument = PointerGetDatum(first_sub_key);
1450 : : }
7225 1451 [ + + ]: 1127 : else if (IsA(clause, ScalarArrayOpExpr))
1452 : : {
1453 : : /* indexkey op ANY (array-expression) */
1454 : 854 : ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
5074 1455 : 854 : int flags = 0;
1456 : : Datum scanvalue;
1457 : :
5392 1458 [ - + ]: 854 : Assert(!isorderby);
1459 : :
7225 1460 [ - + ]: 854 : Assert(saop->useOr);
6355 1461 : 854 : opno = saop->opno;
7225 1462 : 854 : opfuncid = saop->opfuncid;
1463 : :
1464 : : /*
1465 : : * leftop should be the index key Var, possibly relabeled
1466 : : */
1467 : 854 : leftop = (Expr *) linitial(saop->args);
1468 : :
1469 [ + - - + ]: 854 : if (leftop && IsA(leftop, RelabelType))
7225 tgl@sss.pgh.pa.us 1470 :UBC 0 : leftop = ((RelabelType *) leftop)->arg;
1471 : :
7225 tgl@sss.pgh.pa.us 1472 [ - + ]:CBC 854 : Assert(leftop != NULL);
1473 : :
1474 [ + - ]: 854 : if (!(IsA(leftop, Var) &&
5079 1475 [ - + ]: 854 : ((Var *) leftop)->varno == INDEX_VAR))
7225 tgl@sss.pgh.pa.us 1476 [ # # ]:UBC 0 : elog(ERROR, "indexqual doesn't have key on left side");
1477 : :
7225 tgl@sss.pgh.pa.us 1478 :CBC 854 : varattno = ((Var *) leftop)->varattno;
2709 teodor@sigaev.ru 1479 [ + - - + ]: 854 : if (varattno < 1 || varattno > indnkeyatts)
6355 tgl@sss.pgh.pa.us 1480 [ # # ]:UBC 0 : elog(ERROR, "bogus index qualification");
1481 : :
1482 : : /*
1483 : : * We have to look up the operator's strategy number. This
1484 : : * provides a cross-check that the operator does match the index.
1485 : : */
6355 tgl@sss.pgh.pa.us 1486 :CBC 854 : opfamily = index->rd_opfamily[varattno - 1];
1487 : :
5392 1488 : 854 : get_op_opfamily_properties(opno, opfamily, isorderby,
1489 : : &op_strategy,
1490 : : &op_lefttype,
1491 : : &op_righttype);
1492 : :
1493 : : /*
1494 : : * rightop is the constant or variable array value
1495 : : */
7225 1496 : 854 : rightop = (Expr *) lsecond(saop->args);
1497 : :
1498 [ + - - + ]: 854 : if (rightop && IsA(rightop, RelabelType))
7225 tgl@sss.pgh.pa.us 1499 :UBC 0 : rightop = ((RelabelType *) rightop)->arg;
1500 : :
7225 tgl@sss.pgh.pa.us 1501 [ - + ]:CBC 854 : Assert(rightop != NULL);
1502 : :
2420 andres@anarazel.de 1503 [ + + ]: 854 : if (index->rd_indam->amsearcharray)
1504 : : {
1505 : : /* Index AM will handle this like a simple operator */
5074 tgl@sss.pgh.pa.us 1506 : 841 : flags |= SK_SEARCHARRAY;
1507 [ + + ]: 841 : if (IsA(rightop, Const))
1508 : : {
1509 : : /* OK, simple constant comparison value */
1510 : 800 : scanvalue = ((Const *) rightop)->constvalue;
1511 [ + + ]: 800 : if (((Const *) rightop)->constisnull)
1512 : 3 : flags |= SK_ISNULL;
1513 : : }
1514 : : else
1515 : : {
1516 : : /* Need to treat this one as a runtime key */
1517 [ + - ]: 41 : if (n_runtime_keys >= max_runtime_keys)
1518 : : {
1519 [ + - ]: 41 : if (max_runtime_keys == 0)
1520 : : {
1521 : 41 : max_runtime_keys = 8;
1522 : : runtime_keys = (IndexRuntimeKeyInfo *)
1523 : 41 : palloc(max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
1524 : : }
1525 : : else
1526 : : {
5074 tgl@sss.pgh.pa.us 1527 :UBC 0 : max_runtime_keys *= 2;
1528 : : runtime_keys = (IndexRuntimeKeyInfo *)
1529 : 0 : repalloc(runtime_keys, max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
1530 : : }
1531 : : }
5074 tgl@sss.pgh.pa.us 1532 :CBC 41 : runtime_keys[n_runtime_keys].scan_key = this_scan_key;
1533 : 82 : runtime_keys[n_runtime_keys].key_expr =
1534 : 41 : ExecInitExpr(rightop, planstate);
1535 : :
1536 : : /*
1537 : : * Careful here: the runtime expression is not of
1538 : : * op_righttype, but rather is an array of same; so
1539 : : * TypeIsToastable() isn't helpful. However, we can
1540 : : * assume that all array types are toastable.
1541 : : */
1542 : 41 : runtime_keys[n_runtime_keys].key_toastable = true;
1543 : 41 : n_runtime_keys++;
1544 : 41 : scanvalue = (Datum) 0;
1545 : : }
1546 : : }
1547 : : else
1548 : : {
1549 : : /* Executor has to expand the array value */
1550 : 13 : array_keys[n_array_keys].scan_key = this_scan_key;
1551 : 26 : array_keys[n_array_keys].array_expr =
1552 : 13 : ExecInitExpr(rightop, planstate);
1553 : : /* the remaining fields were zeroed by palloc0 */
1554 : 13 : n_array_keys++;
1555 : 13 : scanvalue = (Datum) 0;
1556 : : }
1557 : :
1558 : : /*
1559 : : * initialize the scan key's fields appropriately
1560 : : */
7225 1561 : 854 : ScanKeyEntryInitialize(this_scan_key,
1562 : : flags,
1563 : : varattno, /* attribute number to scan */
1564 : : op_strategy, /* op's strategy */
1565 : : op_righttype, /* strategy subtype */
1566 : : saop->inputcollid, /* collation */
1567 : : opfuncid, /* reg proc to use */
1568 : : scanvalue); /* constant */
1569 : : }
6728 1570 [ + - ]: 273 : else if (IsA(clause, NullTest))
1571 : : {
1572 : : /* indexkey IS NULL or indexkey IS NOT NULL */
5727 1573 : 273 : NullTest *ntest = (NullTest *) clause;
1574 : : int flags;
1575 : :
5392 1576 [ - + ]: 273 : Assert(!isorderby);
1577 : :
1578 : : /*
1579 : : * argument should be the index key Var, possibly relabeled
1580 : : */
5727 1581 : 273 : leftop = ntest->arg;
1582 : :
6728 1583 [ + - - + ]: 273 : if (leftop && IsA(leftop, RelabelType))
6728 tgl@sss.pgh.pa.us 1584 :UBC 0 : leftop = ((RelabelType *) leftop)->arg;
1585 : :
6505 bruce@momjian.us 1586 [ - + ]:CBC 273 : Assert(leftop != NULL);
1587 : :
6728 tgl@sss.pgh.pa.us 1588 [ + - ]: 273 : if (!(IsA(leftop, Var) &&
5079 1589 [ - + ]: 273 : ((Var *) leftop)->varno == INDEX_VAR))
6728 tgl@sss.pgh.pa.us 1590 [ # # ]:UBC 0 : elog(ERROR, "NullTest indexqual has wrong key");
1591 : :
6728 tgl@sss.pgh.pa.us 1592 :CBC 273 : varattno = ((Var *) leftop)->varattno;
1593 : :
1594 : : /*
1595 : : * initialize the scan key's fields appropriately
1596 : : */
5727 1597 [ + + - ]: 273 : switch (ntest->nulltesttype)
1598 : : {
1599 : 97 : case IS_NULL:
1600 : 97 : flags = SK_ISNULL | SK_SEARCHNULL;
1601 : 97 : break;
1602 : 176 : case IS_NOT_NULL:
1603 : 176 : flags = SK_ISNULL | SK_SEARCHNOTNULL;
1604 : 176 : break;
5727 tgl@sss.pgh.pa.us 1605 :UBC 0 : default:
1606 [ # # ]: 0 : elog(ERROR, "unrecognized nulltesttype: %d",
1607 : : (int) ntest->nulltesttype);
1608 : : flags = 0; /* keep compiler quiet */
1609 : : break;
1610 : : }
1611 : :
6728 tgl@sss.pgh.pa.us 1612 :CBC 273 : ScanKeyEntryInitialize(this_scan_key,
1613 : : flags,
1614 : : varattno, /* attribute number to scan */
1615 : : InvalidStrategy, /* no strategy */
1616 : : InvalidOid, /* no strategy subtype */
1617 : : InvalidOid, /* no collation */
1618 : : InvalidOid, /* no reg proc for this */
1619 : : (Datum) 0); /* constant */
1620 : : }
1621 : : else
7225 tgl@sss.pgh.pa.us 1622 [ # # ]:UBC 0 : elog(ERROR, "unsupported indexqual type: %d",
1623 : : (int) nodeTag(clause));
1624 : : }
1625 : :
5392 tgl@sss.pgh.pa.us 1626 [ - + ]:CBC 183050 : Assert(n_runtime_keys <= max_runtime_keys);
1627 : :
1628 : : /* Get rid of any unused arrays */
7225 1629 [ + + ]: 183050 : if (n_array_keys == 0)
1630 : : {
1631 : 183037 : pfree(array_keys);
1632 : 183037 : array_keys = NULL;
1633 : : }
1634 : :
1635 : : /*
1636 : : * Return info to our caller.
1637 : : */
7439 1638 : 183050 : *scanKeys = scan_keys;
7225 1639 : 183050 : *numScanKeys = n_scan_keys;
1640 : 183050 : *runtimeKeys = runtime_keys;
1641 : 183050 : *numRuntimeKeys = n_runtime_keys;
1642 [ + + ]: 183050 : if (arrayKeys)
1643 : : {
1644 : 8732 : *arrayKeys = array_keys;
1645 : 8732 : *numArrayKeys = n_array_keys;
1646 : : }
1647 [ - + ]: 174318 : else if (n_array_keys != 0)
7225 tgl@sss.pgh.pa.us 1648 [ # # ]:UBC 0 : elog(ERROR, "ScalarArrayOpExpr index qual found where not allowed");
8051 tgl@sss.pgh.pa.us 1649 :CBC 183050 : }
1650 : :
1651 : : /* ----------------------------------------------------------------
1652 : : * Parallel Scan Support
1653 : : * ----------------------------------------------------------------
1654 : : */
1655 : :
1656 : : /* ----------------------------------------------------------------
1657 : : * ExecIndexScanEstimate
1658 : : *
1659 : : * Compute the amount of space we'll need in the parallel
1660 : : * query DSM, and inform pcxt->estimator about our needs.
1661 : : * ----------------------------------------------------------------
1662 : : */
1663 : : void
3125 rhaas@postgresql.org 1664 : 147 : ExecIndexScanEstimate(IndexScanState *node,
1665 : : ParallelContext *pcxt)
1666 : : {
1667 : 147 : EState *estate = node->ss.ps.state;
179 pg@bowt.ie 1668 : 147 : bool instrument = node->ss.ps.instrument != NULL;
1669 : 147 : bool parallel_aware = node->ss.ps.plan->parallel_aware;
1670 : :
1671 [ + + + + ]: 147 : if (!instrument && !parallel_aware)
1672 : : {
1673 : : /* No DSM required by the scan */
1674 : 3 : return;
1675 : : }
1676 : :
3125 rhaas@postgresql.org 1677 : 144 : node->iss_PscanLen = index_parallelscan_estimate(node->iss_RelationDesc,
1678 : : node->iss_NumScanKeys,
1679 : : node->iss_NumOrderByKeys,
1680 : : estate->es_snapshot,
1681 : : instrument, parallel_aware,
1682 : : pcxt->nworkers);
1683 : 144 : shm_toc_estimate_chunk(&pcxt->estimator, node->iss_PscanLen);
1684 : 144 : shm_toc_estimate_keys(&pcxt->estimator, 1);
1685 : : }
1686 : :
1687 : : /* ----------------------------------------------------------------
1688 : : * ExecIndexScanInitializeDSM
1689 : : *
1690 : : * Set up a parallel index scan descriptor.
1691 : : * ----------------------------------------------------------------
1692 : : */
1693 : : void
1694 : 147 : ExecIndexScanInitializeDSM(IndexScanState *node,
1695 : : ParallelContext *pcxt)
1696 : : {
1697 : 147 : EState *estate = node->ss.ps.state;
1698 : : ParallelIndexScanDesc piscan;
179 pg@bowt.ie 1699 : 147 : bool instrument = node->ss.ps.instrument != NULL;
1700 : 147 : bool parallel_aware = node->ss.ps.plan->parallel_aware;
1701 : :
1702 [ + + + + ]: 147 : if (!instrument && !parallel_aware)
1703 : : {
1704 : : /* No DSM required by the scan */
1705 : 3 : return;
1706 : : }
1707 : :
3125 rhaas@postgresql.org 1708 : 144 : piscan = shm_toc_allocate(pcxt->toc, node->iss_PscanLen);
1709 : 144 : index_parallelscan_initialize(node->ss.ss_currentRelation,
1710 : : node->iss_RelationDesc,
1711 : : estate->es_snapshot,
1712 : : instrument, parallel_aware, pcxt->nworkers,
1713 : : &node->iss_SharedInfo, piscan);
1714 : 144 : shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id, piscan);
1715 : :
179 pg@bowt.ie 1716 [ + + ]: 144 : if (!parallel_aware)
1717 : : {
1718 : : /* Only here to initialize SharedInfo in DSM */
1719 : 135 : return;
1720 : : }
1721 : :
3125 rhaas@postgresql.org 1722 : 9 : node->iss_ScanDesc =
1723 : 9 : index_beginscan_parallel(node->ss.ss_currentRelation,
1724 : : node->iss_RelationDesc,
1725 : : &node->iss_Instrument,
1726 : : node->iss_NumScanKeys,
1727 : : node->iss_NumOrderByKeys,
1728 : : piscan);
1729 : :
1730 : : /*
1731 : : * If no run-time keys to calculate or they are ready, go ahead and pass
1732 : : * the scankeys to the index AM.
1733 : : */
3104 1734 [ + + - + ]: 9 : if (node->iss_NumRuntimeKeys == 0 || node->iss_RuntimeKeysReady)
3125 1735 : 6 : index_rescan(node->iss_ScanDesc,
1736 : 6 : node->iss_ScanKeys, node->iss_NumScanKeys,
1737 : 6 : node->iss_OrderByKeys, node->iss_NumOrderByKeys);
1738 : : }
1739 : :
1740 : : /* ----------------------------------------------------------------
1741 : : * ExecIndexScanReInitializeDSM
1742 : : *
1743 : : * Reset shared state before beginning a fresh scan.
1744 : : * ----------------------------------------------------------------
1745 : : */
1746 : : void
2929 tgl@sss.pgh.pa.us 1747 : 6 : ExecIndexScanReInitializeDSM(IndexScanState *node,
1748 : : ParallelContext *pcxt)
1749 : : {
179 pg@bowt.ie 1750 [ - + ]: 6 : Assert(node->ss.ps.plan->parallel_aware);
2929 tgl@sss.pgh.pa.us 1751 : 6 : index_parallelrescan(node->iss_ScanDesc);
1752 : 6 : }
1753 : :
1754 : : /* ----------------------------------------------------------------
1755 : : * ExecIndexScanInitializeWorker
1756 : : *
1757 : : * Copy relevant information from TOC into planstate.
1758 : : * ----------------------------------------------------------------
1759 : : */
1760 : : void
2851 andres@anarazel.de 1761 : 198 : ExecIndexScanInitializeWorker(IndexScanState *node,
1762 : : ParallelWorkerContext *pwcxt)
1763 : : {
1764 : : ParallelIndexScanDesc piscan;
179 pg@bowt.ie 1765 : 198 : bool instrument = node->ss.ps.instrument != NULL;
1766 : 198 : bool parallel_aware = node->ss.ps.plan->parallel_aware;
1767 : :
1768 [ + + + + ]: 198 : if (!instrument && !parallel_aware)
1769 : : {
1770 : : /* No DSM required by the scan */
1771 : 3 : return;
1772 : : }
1773 : :
2851 andres@anarazel.de 1774 : 195 : piscan = shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, false);
1775 : :
179 pg@bowt.ie 1776 [ + + ]: 195 : if (instrument)
1777 : 135 : node->iss_SharedInfo = (SharedIndexScanInstrumentation *)
1778 : 135 : OffsetToPointer(piscan, piscan->ps_offset_ins);
1779 : :
1780 [ + + ]: 195 : if (!parallel_aware)
1781 : : {
1782 : : /* Only here to set up worker node's SharedInfo */
1783 : 135 : return;
1784 : : }
1785 : :
3125 rhaas@postgresql.org 1786 : 60 : node->iss_ScanDesc =
1787 : 60 : index_beginscan_parallel(node->ss.ss_currentRelation,
1788 : : node->iss_RelationDesc,
1789 : : &node->iss_Instrument,
1790 : : node->iss_NumScanKeys,
1791 : : node->iss_NumOrderByKeys,
1792 : : piscan);
1793 : :
1794 : : /*
1795 : : * If no run-time keys to calculate or they are ready, go ahead and pass
1796 : : * the scankeys to the index AM.
1797 : : */
3104 1798 [ + + - + ]: 60 : if (node->iss_NumRuntimeKeys == 0 || node->iss_RuntimeKeysReady)
3125 1799 : 48 : index_rescan(node->iss_ScanDesc,
1800 : 48 : node->iss_ScanKeys, node->iss_NumScanKeys,
1801 : 48 : node->iss_OrderByKeys, node->iss_NumOrderByKeys);
1802 : : }
1803 : :
1804 : : /* ----------------------------------------------------------------
1805 : : * ExecIndexScanRetrieveInstrumentation
1806 : : *
1807 : : * Transfer index scan statistics from DSM to private memory.
1808 : : * ----------------------------------------------------------------
1809 : : */
1810 : : void
179 pg@bowt.ie 1811 : 135 : ExecIndexScanRetrieveInstrumentation(IndexScanState *node)
1812 : : {
1813 : 135 : SharedIndexScanInstrumentation *SharedInfo = node->iss_SharedInfo;
1814 : : size_t size;
1815 : :
1816 [ - + ]: 135 : if (SharedInfo == NULL)
179 pg@bowt.ie 1817 :UBC 0 : return;
1818 : :
1819 : : /* Create a copy of SharedInfo in backend-local memory */
179 pg@bowt.ie 1820 :CBC 135 : size = offsetof(SharedIndexScanInstrumentation, winstrument) +
1821 : 135 : SharedInfo->num_workers * sizeof(IndexScanInstrumentation);
1822 : 135 : node->iss_SharedInfo = palloc(size);
1823 : 135 : memcpy(node->iss_SharedInfo, SharedInfo, size);
1824 : : }
|