Wiki / Plan Nodes / Scan
Index Scan
IndexScan
Uses an index to identify table tuples and fetches them from the relation.
Reading PostgreSQL 18.6.
Description
Uses an index to identify table tuples and fetches them from the relation.
- Core node tag
- T_IndexScan
- Structured EXPLAIN Node Type
- Index Scan
- Inputs
- An index and its relation
- Output
- Qualified table tuples
- Executor initializer
- ExecInitIndexScan
- Memory mechanism
- unclassified
EXPLAIN names and attributes
Structured formats use the Node Type above. Text-format spellings can also include operation, strategy, join type, scan direction or aggregation-stage attributes.
Text names recorded by this source: Index Scan.
Parallel-aware and parallel-safe are different plan properties. A node running inside a parallel worker is not necessarily a parallel-aware node.
Memory and temporary storage
This extraction does not assign a universal memory limit or spill policy to this node. Inspect the same-build implementation and its expressions or provider.
If the index was lossy, we have to recheck the index quals using the fetched tuple.
If the index was lossy, we have to recheck the index quals and ORDER BY expressions using the fetched tuple.
Parallel execution and instrumentation
The source callbacks below can coordinate execution or collect worker instrumentation. Their presence is not a blanket claim that this node supports a shared parallel scan or shared state.
Callbacks in this build: ExecIndexScanEstimate, ExecIndexScanInitializeDSM, ExecIndexScanInitializeWorker, ExecIndexScanReInitializeDSM, ExecIndexScanRetrieveInstrumentation.
Same-version manual discussion
This is the same query as above, but we added a LIMIT so that not all the rows need be retrieved, and the planner changed its mind about what to do. Notice that the total cost and row count of the Index Scan node are shown as if it were run to completion. However, the Limit node is expected to stop after retrieving only a fifth of those rows, so its total cost is only a fifth as much, and that's the actual estimated cost of the query. This plan is preferred over adding a Limit node to the previous plan because the Limit could not avoid paying the startup cost of the bitmap scan, so the total cost would be something over 25 units with that approach.
Notice that here the planner has chosen to “ materialize ” the inner relation of the join, by putting a Materialize plan node atop it. This means that the t2 index scan will be done just once, even though the nested-loop join node needs to read that data ten times, once for each row from the outer relation. The Materialize node saves the data in memory as it's read, and then returns the data from memory on each subsequent pass.
Merge join requires its input data to be sorted on the join keys. In this example each input is sorted by using an index scan to visit the rows in the correct order; but a sequential scan and sort could also be used. (Sequential-scan-and-sort frequently beats an index scan for sorting many rows, because of the nonsequential disk access required by the index scan.)
In some query plans, it is possible for a subplan node to be executed more than once. For example, the inner index scan will be executed once per outer row in the above nested-loop plan. In such cases, the loops value reports the total number of executions of the node, and the actual time and rows values shown are averages per-execution. This is done to make the numbers comparable with the way that the cost estimates are shown. Multiply by the loops value to get the total time actually spent in the node. In the above example, we spent a total of 0.030 milliseconds executing the index scans on tenk2 .
Index Scan nodes (as well as Bitmap Index Scan and Index-Only Scan nodes) show an “ Index Searches ” line that reports the total number of searches across all node executions/ loops :
Here we see a Bitmap Index Scan node that needed 4 separate index searches. The scan had to search the index from the tenk1_thous_tenthous index root page once per integer value from the predicate's IN construct. However, the number of index searches often won't have such a simple correspondence to the query predicate:
Examples from this manual build
Example copied from the PostgreSQL 18.6 manual; it was not executed for this collection.
Now, let's make the condition more restrictive:
EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 100;
QUERY PLAN
------------------------------------------------------------------------------
Bitmap Heap Scan on tenk1 (cost=5.06..224.98 rows=100 width=244)
Recheck Cond: (unique1 < 100)
-> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=100 width=0)
Index Cond: (unique1 < 100)Example copied from the PostgreSQL 18.6 manual; it was not executed for this collection.
Now let's add another condition to the WHERE clause:
EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 100 AND stringu1 = 'xxx';
QUERY PLAN
------------------------------------------------------------------------------
Bitmap Heap Scan on tenk1 (cost=5.04..225.20 rows=1 width=244)
Recheck Cond: (unique1 < 100)
Filter: (stringu1 = 'xxx'::name)
-> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=100 width=0)
Index Cond: (unique1 < 100)Executor implementation notes
nodeIndexscan.c Routines to support indexed scans of relations
When an ordering operator is used, tuples fetched from the index that need to be reordered are queued in a pairing heap, as ReorderTuples.
Retrieve a tuple from the IndexScan node's currentRelation using the index specified in the IndexScanState information.
Determine which direction to scan the index in based on the plan's scan direction and the current direction of execution.
We reach here if the index scan is not parallel, or if we're serially executing an index scan that was planned to be parallel.
EXPLAIN identity in core source
case T_IndexScan:
pname = sname = "Index Scan";
break;EXPLAIN labels in this source build
| Text-format label | Structured node identity |
|---|---|
| Index Scan | Index Scan |
Related entries
Documentation and source
- src/backend/commands/explain.c:1444
- src/backend/executor/execProcnode.c:219
- src/backend/executor/nodeIndexscan.c
- src/include/nodes/plannodes.h
- PostgreSQL 18.6 · using-explain
- PostgreSQL 18.6 · using-explain
Source build
- Version
- 18.6
- Build
- PostgreSQL 18.6 source archive
- Source fingerprint
555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f
Compare versions
PostgreSQL 17 → 18: changed.
--- PostgreSQL 17
+++ PostgreSQL 18
@@ -6,7 +6,8 @@
"ExecIndexScanEstimate",
"ExecIndexScanInitializeDSM",
"ExecIndexScanInitializeWorker",
- "ExecIndexScanReInitializeDSM"
+ "ExecIndexScanReInitializeDSM",
+ "ExecIndexScanRetrieveInstrumentation"
],
"partial_modes": [],
"strategies": [],
Compares recorded interfaces and attributes. Source fingerprints and build metadata are excluded; an absent sample is not proof of the introduction or removal release.
Related entries
Bitmap Heap ScanBitmapHeapScanCTE ScanCteScanFunction ScanFunctionScanIndex Only ScanIndexOnlyScanNamed Tuplestore ScanNamedTuplestoreScanSample ScanSampleScanSeq ScanSeqScanSubquery ScanSubqueryScanTable Function ScanTableFuncScanTid Range ScanTidRangeScanTid ScanTidScanValues ScanValuesScan
Export JSON · Back to Plan Nodes · Recorded in PostgreSQL 10 through 20; the first sample is not necessarily its introduction.