PostgreSQL Source Code git master
Loading...
Searching...
No Matches
nodeSeqscan.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * nodeSeqscan.c
4 * Support routines for sequential scans of relations.
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/executor/nodeSeqscan.c
12 *
13 *-------------------------------------------------------------------------
14 */
15/*
16 * INTERFACE ROUTINES
17 * ExecSeqScan sequentially scans a relation.
18 * ExecSeqNext retrieve next tuple in sequential order.
19 * ExecInitSeqScan creates and initializes a seqscan node.
20 * ExecEndSeqScan releases any storage allocated.
21 * ExecReScanSeqScan rescans the relation
22 *
23 * ExecSeqScanEstimate estimates DSM space needed for parallel scan
24 * ExecSeqScanInitializeDSM initialize DSM for parallel scan
25 * ExecSeqScanReInitializeDSM reinitialize DSM for fresh parallel scan
26 * ExecSeqScanInitializeWorker attach to DSM info in parallel worker
27 */
28#include "postgres.h"
29
30#include "access/relscan.h"
31#include "access/tableam.h"
33#include "executor/execScan.h"
34#include "executor/executor.h"
36#include "utils/rel.h"
37
39
40/* ----------------------------------------------------------------
41 * Scan Support
42 * ----------------------------------------------------------------
43 */
44
45/* ----------------------------------------------------------------
46 * SeqNext
47 *
48 * This is a workhorse for ExecSeqScan
49 * ----------------------------------------------------------------
50 */
53{
54 TableScanDesc scandesc;
55 EState *estate;
56 ScanDirection direction;
57 TupleTableSlot *slot;
58
59 /*
60 * get information from the estate and scan state
61 */
62 scandesc = node->ss.ss_currentScanDesc;
63 estate = node->ss.ps.state;
64 direction = estate->es_direction;
65 slot = node->ss.ss_ScanTupleSlot;
66
67 if (scandesc == NULL)
68 {
69 uint32 flags = SO_NONE;
70
71 if (ScanRelIsReadOnly(&node->ss))
72 flags |= SO_HINT_REL_READ_ONLY;
73
74 if (estate->es_instrument & INSTRUMENT_IO)
75 flags |= SO_SCAN_INSTRUMENT;
76
77 /*
78 * We reach here if the scan is not parallel, or if we're serially
79 * executing a scan that was planned to be parallel.
80 */
81 scandesc = table_beginscan(node->ss.ss_currentRelation,
82 estate->es_snapshot,
83 0, NULL, flags);
84 node->ss.ss_currentScanDesc = scandesc;
85 }
86
87 /*
88 * get the next tuple from the table
89 */
90 if (table_scan_getnextslot(scandesc, direction, slot))
91 return slot;
92 return NULL;
93}
94
95/*
96 * SeqRecheck -- access method routine to recheck a tuple in EvalPlanQual
97 */
100{
101 /*
102 * Note that unlike IndexScan, SeqScan never use keys in heap_beginscan
103 * (and this is very bad) - so, here we do not check are keys ok or not.
104 */
105 return true;
106}
107
108/* ----------------------------------------------------------------
109 * ExecSeqScan(node)
110 *
111 * Scans the relation sequentially and returns the next qualifying
112 * tuple. This variant is used when there is no es_epq_active, no qual
113 * and no projection. Passing const-NULLs for these to ExecScanExtended
114 * allows the compiler to eliminate the additional code that would
115 * ordinarily be required for the evaluation of these.
116 * ----------------------------------------------------------------
117 */
118static TupleTableSlot *
120{
121 SeqScanState *node = castNode(SeqScanState, pstate);
122
123 Assert(pstate->state->es_epq_active == NULL);
124 Assert(pstate->qual == NULL);
125 Assert(pstate->ps_ProjInfo == NULL);
126
127 return ExecScanExtended(&node->ss,
130 NULL,
131 NULL,
132 NULL);
133}
134
135/*
136 * Variant of ExecSeqScan() but when qual evaluation is required.
137 */
138static TupleTableSlot *
140{
141 SeqScanState *node = castNode(SeqScanState, pstate);
142
143 /*
144 * Use pg_assume() for != NULL tests to make the compiler realize no
145 * runtime check for the field is needed in ExecScanExtended().
146 */
147 Assert(pstate->state->es_epq_active == NULL);
148 pg_assume(pstate->qual != NULL);
149 Assert(pstate->ps_ProjInfo == NULL);
150
151 return ExecScanExtended(&node->ss,
154 NULL,
155 pstate->qual,
156 NULL);
157}
158
159/*
160 * Variant of ExecSeqScan() but when projection is required.
161 */
162static TupleTableSlot *
164{
165 SeqScanState *node = castNode(SeqScanState, pstate);
166
167 Assert(pstate->state->es_epq_active == NULL);
168 Assert(pstate->qual == NULL);
169 pg_assume(pstate->ps_ProjInfo != NULL);
170
171 return ExecScanExtended(&node->ss,
174 NULL,
175 NULL,
176 pstate->ps_ProjInfo);
177}
178
179/*
180 * Variant of ExecSeqScan() but when qual evaluation and projection are
181 * required.
182 */
183static TupleTableSlot *
185{
186 SeqScanState *node = castNode(SeqScanState, pstate);
187
188 Assert(pstate->state->es_epq_active == NULL);
189 pg_assume(pstate->qual != NULL);
190 pg_assume(pstate->ps_ProjInfo != NULL);
191
192 return ExecScanExtended(&node->ss,
195 NULL,
196 pstate->qual,
197 pstate->ps_ProjInfo);
198}
199
200/*
201 * Variant of ExecSeqScan for when EPQ evaluation is required. We don't
202 * bother adding variants of this for with/without qual and projection as
203 * EPQ doesn't seem as exciting a case to optimize for.
204 */
205static TupleTableSlot *
207{
208 SeqScanState *node = castNode(SeqScanState, pstate);
209
210 return ExecScan(&node->ss,
213}
214
215/* ----------------------------------------------------------------
216 * ExecInitSeqScan
217 * ----------------------------------------------------------------
218 */
220ExecInitSeqScan(SeqScan *node, EState *estate, int eflags)
221{
223
224 /*
225 * Once upon a time it was possible to have an outerPlan of a SeqScan, but
226 * not any more.
227 */
228 Assert(outerPlan(node) == NULL);
229 Assert(innerPlan(node) == NULL);
230
231 /*
232 * create state structure
233 */
235 scanstate->ss.ps.plan = (Plan *) node;
236 scanstate->ss.ps.state = estate;
237
238 /*
239 * Miscellaneous initialization
240 *
241 * create expression context for node
242 */
243 ExecAssignExprContext(estate, &scanstate->ss.ps);
244
245 /*
246 * open the scan relation
247 */
248 scanstate->ss.ss_currentRelation =
250 node->scan.scanrelid,
251 eflags);
252
253 /* and create slot with the appropriate rowtype */
254 ExecInitScanTupleSlot(estate, &scanstate->ss,
255 RelationGetDescr(scanstate->ss.ss_currentRelation),
256 table_slot_callbacks(scanstate->ss.ss_currentRelation),
258
259 /*
260 * Initialize result type and projection.
261 */
264
265 /*
266 * initialize child expressions
267 */
268 scanstate->ss.ps.qual =
269 ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
270
271 /*
272 * When EvalPlanQual() is not in use, assign ExecProcNode for this node
273 * based on the presence of qual and projection. Each ExecSeqScan*()
274 * variant is optimized for the specific combination of these conditions.
275 */
276 if (scanstate->ss.ps.state->es_epq_active != NULL)
277 scanstate->ss.ps.ExecProcNode = ExecSeqScanEPQ;
278 else if (scanstate->ss.ps.qual == NULL)
279 {
280 if (scanstate->ss.ps.ps_ProjInfo == NULL)
281 scanstate->ss.ps.ExecProcNode = ExecSeqScan;
282 else
283 scanstate->ss.ps.ExecProcNode = ExecSeqScanWithProject;
284 }
285 else
286 {
287 if (scanstate->ss.ps.ps_ProjInfo == NULL)
288 scanstate->ss.ps.ExecProcNode = ExecSeqScanWithQual;
289 else
290 scanstate->ss.ps.ExecProcNode = ExecSeqScanWithQualProject;
291 }
292
293 return scanstate;
294}
295
296/* ----------------------------------------------------------------
297 * ExecEndSeqScan
298 *
299 * frees any storage allocated through C routines.
300 * ----------------------------------------------------------------
301 */
302void
304{
306
307 /*
308 * get information from node
309 */
311
312 /*
313 * Collect I/O stats for this process into shared instrumentation.
314 */
315 if (node->sinstrument != NULL && IsParallelWorker())
316 {
318
319 Assert(ParallelWorkerNumber < node->sinstrument->num_workers);
321
322 if (scanDesc && scanDesc->rs_instrument)
323 {
324 AccumulateIOStats(&si->stats.io, &scanDesc->rs_instrument->io);
325 }
326 }
327
328 /*
329 * close heap scan
330 */
331 if (scanDesc != NULL)
333}
334
335/* ----------------------------------------------------------------
336 * Join Support
337 * ----------------------------------------------------------------
338 */
339
340/* ----------------------------------------------------------------
341 * ExecReScanSeqScan
342 *
343 * Rescans the relation.
344 * ----------------------------------------------------------------
345 */
346void
348{
349 TableScanDesc scan;
350
351 scan = node->ss.ss_currentScanDesc;
352
353 if (scan != NULL)
354 table_rescan(scan, /* scan desc */
355 NULL); /* new scan keys */
356
357 ExecScanReScan((ScanState *) node);
358}
359
360/* ----------------------------------------------------------------
361 * Parallel Scan Support
362 * ----------------------------------------------------------------
363 */
364
365/* ----------------------------------------------------------------
366 * ExecSeqScanEstimate
367 *
368 * Compute the amount of space we'll need in the parallel
369 * query DSM, and inform pcxt->estimator about our needs.
370 * ----------------------------------------------------------------
371 */
372void
374 ParallelContext *pcxt)
375{
376 EState *estate = node->ss.ps.state;
377
379 estate->es_snapshot);
382}
383
384/* ----------------------------------------------------------------
385 * ExecSeqScanInitializeDSM
386 *
387 * Set up a parallel heap scan descriptor.
388 * ----------------------------------------------------------------
389 */
390void
392 ParallelContext *pcxt)
393{
394 EState *estate = node->ss.ps.state;
396 uint32 flags = SO_NONE;
397
398 if (ScanRelIsReadOnly(&node->ss))
399 flags |= SO_HINT_REL_READ_ONLY;
400
401 if (estate->es_instrument & INSTRUMENT_IO)
402 flags |= SO_SCAN_INSTRUMENT;
403
404 pscan = shm_toc_allocate(pcxt->toc, node->pscan_len);
406 pscan,
407 estate->es_snapshot);
408 shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id, pscan);
409
410 node->ss.ss_currentScanDesc =
412}
413
414/* ----------------------------------------------------------------
415 * ExecSeqScanReInitializeDSM
416 *
417 * Reset shared state before beginning a fresh scan.
418 * ----------------------------------------------------------------
419 */
420void
429
430/* ----------------------------------------------------------------
431 * ExecSeqScanInitializeWorker
432 *
433 * Copy relevant information from TOC into planstate.
434 * ----------------------------------------------------------------
435 */
436void
453
454/*
455 * Compute the amount of space we'll need for the shared instrumentation and
456 * inform pcxt->estimator.
457 */
458void
460{
461 EState *estate = node->ss.ps.state;
462 Size size;
463
464 if ((estate->es_instrument & INSTRUMENT_IO) == 0 || pcxt->nworkers == 0)
465 return;
466
469
470 shm_toc_estimate_chunk(&pcxt->estimator, size);
472}
473
474/*
475 * Set up parallel sequential scan instrumentation.
476 */
477void
479{
480 EState *estate = node->ss.ps.state;
481 SharedSeqScanInstrumentation *sinstrument;
482 Size size;
483
484 if ((estate->es_instrument & INSTRUMENT_IO) == 0 || pcxt->nworkers == 0)
485 return;
486
489 sinstrument = shm_toc_allocate(pcxt->toc, size);
490 memset(sinstrument, 0, size);
491 sinstrument->num_workers = pcxt->nworkers;
492 shm_toc_insert(pcxt->toc,
493 node->ss.ps.plan->plan_node_id +
495 sinstrument);
496 node->sinstrument = sinstrument;
497}
498
499/*
500 * Look up and save the location of the shared instrumentation.
501 */
502void
505{
506 EState *estate = node->ss.ps.state;
507
508 if ((estate->es_instrument & INSTRUMENT_IO) == 0)
509 return;
510
511 node->sinstrument = shm_toc_lookup(pwcxt->toc,
512 node->ss.ps.plan->plan_node_id +
514 false);
515}
516
517/*
518 * Transfer sequential scan instrumentation from DSM to private memory.
519 */
520void
522{
523 SharedSeqScanInstrumentation *sinstrument = node->sinstrument;
524 Size size;
525
526 if (sinstrument == NULL)
527 return;
528
529 size = offsetof(SharedSeqScanInstrumentation, sinstrument)
530 + sinstrument->num_workers * sizeof(SeqScanInstrumentation);
531
532 node->sinstrument = palloc(size);
533 memcpy(node->sinstrument, sinstrument, size);
534}
int ParallelWorkerNumber
Definition parallel.c:117
#define Assert(condition)
Definition c.h:943
#define pg_attribute_always_inline
Definition c.h:305
#define pg_assume(expr)
Definition c.h:423
uint32_t uint32
Definition c.h:624
size_t Size
Definition c.h:689
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition execExpr.c:250
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition execScan.c:47
void ExecAssignScanProjectionInfo(ScanState *node)
Definition execScan.c:81
void ExecScanReScan(ScanState *node)
Definition execScan.c:108
static pg_attribute_always_inline TupleTableSlot * ExecScanExtended(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd, EPQState *epqstate, ExprState *qual, ProjectionInfo *projInfo)
Definition execScan.h:161
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops, uint16 flags)
void ExecInitResultTypeTL(PlanState *planstate)
bool ScanRelIsReadOnly(ScanState *ss)
Definition execUtils.c:751
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition execUtils.c:490
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition execUtils.c:768
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition executor.h:590
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition executor.h:589
#define IsParallelWorker()
Definition parallel.h:62
@ INSTRUMENT_IO
Definition instrument.h:67
#define PARALLEL_KEY_SCAN_INSTRUMENT_OFFSET
static void AccumulateIOStats(IOStats *dst, IOStats *src)
void * palloc(Size size)
Definition mcxt.c:1387
void ExecSeqScanRetrieveInstrumentation(SeqScanState *node)
static TupleTableSlot * ExecSeqScanWithQual(PlanState *pstate)
void ExecSeqScanReInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
void ExecSeqScanInstrumentInitDSM(SeqScanState *node, ParallelContext *pcxt)
void ExecSeqScanInstrumentEstimate(SeqScanState *node, ParallelContext *pcxt)
void ExecSeqScanInitializeWorker(SeqScanState *node, ParallelWorkerContext *pwcxt)
static pg_attribute_always_inline bool SeqRecheck(SeqScanState *node, TupleTableSlot *slot)
Definition nodeSeqscan.c:99
void ExecEndSeqScan(SeqScanState *node)
static TupleTableSlot * ExecSeqScanWithQualProject(PlanState *pstate)
static TupleTableSlot * ExecSeqScan(PlanState *pstate)
SeqScanState * ExecInitSeqScan(SeqScan *node, EState *estate, int eflags)
void ExecSeqScanInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
static TupleTableSlot * ExecSeqScanEPQ(PlanState *pstate)
static TupleTableSlot * ExecSeqScanWithProject(PlanState *pstate)
void ExecSeqScanEstimate(SeqScanState *node, ParallelContext *pcxt)
void ExecSeqScanInstrumentInitWorker(SeqScanState *node, ParallelWorkerContext *pwcxt)
static TupleTableSlot * SeqNext(SeqScanState *node)
Definition nodeSeqscan.c:52
void ExecReScanSeqScan(SeqScanState *node)
#define makeNode(_type_)
Definition nodes.h:161
#define castNode(_type_, nodeptr)
Definition nodes.h:182
#define innerPlan(node)
Definition plannodes.h:266
#define outerPlan(node)
Definition plannodes.h:267
static int fb(int x)
#define RelationGetDescr(relation)
Definition rel.h:542
ScanDirection
Definition sdir.h:25
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition shm_toc.c:88
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition shm_toc.c:171
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition shm_toc.c:239
#define shm_toc_estimate_chunk(e, sz)
Definition shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition shm_toc.h:53
Size add_size(Size s1, Size s2)
Definition shmem.c:1048
Size mul_size(Size s1, Size s2)
Definition shmem.c:1063
int es_instrument
Definition execnodes.h:756
ScanDirection es_direction
Definition execnodes.h:695
struct EPQState * es_epq_active
Definition execnodes.h:778
Snapshot es_snapshot
Definition execnodes.h:696
shm_toc_estimator estimator
Definition parallel.h:43
shm_toc * toc
Definition parallel.h:46
ExprState * qual
Definition execnodes.h:1224
Plan * plan
Definition execnodes.h:1201
EState * state
Definition execnodes.h:1203
ProjectionInfo * ps_ProjInfo
Definition execnodes.h:1243
int plan_node_id
Definition plannodes.h:233
Relation ss_currentRelation
Definition execnodes.h:1660
TupleTableSlot * ss_ScanTupleSlot
Definition execnodes.h:1662
PlanState ps
Definition execnodes.h:1659
struct TableScanDescData * ss_currentScanDesc
Definition execnodes.h:1661
Index scanrelid
Definition plannodes.h:544
struct SharedSeqScanInstrumentation * sinstrument
Definition execnodes.h:1673
ScanState ss
Definition execnodes.h:1671
Scan scan
Definition plannodes.h:553
SeqScanInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER]
struct ParallelTableScanDescData * rs_parallel
Definition relscan.h:66
TableScanDesc table_beginscan_parallel(Relation relation, ParallelTableScanDesc pscan, uint32 flags)
Definition tableam.c:166
Size table_parallelscan_estimate(Relation rel, Snapshot snapshot)
Definition tableam.c:131
void table_parallelscan_initialize(Relation rel, ParallelTableScanDesc pscan, Snapshot snapshot)
Definition tableam.c:146
const TupleTableSlotOps * table_slot_callbacks(Relation relation)
Definition tableam.c:59
@ SO_HINT_REL_READ_ONLY
Definition tableam.h:71
@ SO_NONE
Definition tableam.h:49
@ SO_SCAN_INSTRUMENT
Definition tableam.h:74
static void table_endscan(TableScanDesc scan)
Definition tableam.h:1061
static void table_rescan(TableScanDesc scan, ScanKeyData *key)
Definition tableam.h:1070
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, ScanKeyData *key, uint32 flags)
Definition tableam.h:943
static bool table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition tableam.h:1096
static void table_parallelscan_reinitialize(Relation rel, ParallelTableScanDesc pscan)
Definition tableam.h:1226
#define TTS_FLAG_OBEYS_NOT_NULL_CONSTRAINTS
Definition tuptable.h:102