1 /*-------------------------------------------------------------------------
2  *
3  * relscan.h
4  *	  POSTGRES relation scan descriptor definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/access/relscan.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef RELSCAN_H
15 #define RELSCAN_H
16 
17 #include "access/genam.h"
18 #include "access/heapam.h"
19 #include "access/htup_details.h"
20 #include "access/itup.h"
21 #include "access/tupdesc.h"
22 #include "storage/spin.h"
23 
24 /*
25  * Shared state for parallel heap scan.
26  *
27  * Each backend participating in a parallel heap scan has its own
28  * HeapScanDesc in backend-private memory, and those objects all contain
29  * a pointer to this structure.  The information here must be sufficient
30  * to properly initialize each new HeapScanDesc as workers join the scan,
31  * and it must act as a font of block numbers for those workers.
32  */
33 typedef struct ParallelHeapScanDescData
34 {
35 	Oid			phs_relid;		/* OID of relation to scan */
36 	bool		phs_syncscan;	/* report location to syncscan logic? */
37 	BlockNumber phs_nblocks;	/* # blocks in relation at start of scan */
38 	slock_t		phs_mutex;		/* mutual exclusion for block number fields */
39 	BlockNumber phs_startblock; /* starting block number */
40 	BlockNumber phs_cblock;		/* current block number */
41 	char		phs_snapshot_data[FLEXIBLE_ARRAY_MEMBER];
42 }			ParallelHeapScanDescData;
43 
44 typedef struct HeapScanDescData
45 {
46 	/* scan parameters */
47 	Relation	rs_rd;			/* heap relation descriptor */
48 	Snapshot	rs_snapshot;	/* snapshot to see */
49 	int			rs_nkeys;		/* number of scan keys */
50 	ScanKey		rs_key;			/* array of scan key descriptors */
51 	bool		rs_bitmapscan;	/* true if this is really a bitmap scan */
52 	bool		rs_samplescan;	/* true if this is really a sample scan */
53 	bool		rs_pageatatime; /* verify visibility page-at-a-time? */
54 	bool		rs_allow_strat; /* allow or disallow use of access strategy */
55 	bool		rs_allow_sync;	/* allow or disallow use of syncscan */
56 	bool		rs_temp_snap;	/* unregister snapshot at scan end? */
57 
58 	/* state set up at initscan time */
59 	BlockNumber rs_nblocks;		/* total number of blocks in rel */
60 	BlockNumber rs_startblock;	/* block # to start at */
61 	BlockNumber rs_numblocks;	/* max number of blocks to scan */
62 	/* rs_numblocks is usually InvalidBlockNumber, meaning "scan whole rel" */
63 	BufferAccessStrategy rs_strategy;	/* access strategy for reads */
64 	bool		rs_syncscan;	/* report location to syncscan logic? */
65 
66 	/* scan current state */
67 	bool		rs_inited;		/* false = scan not init'd yet */
68 	HeapTupleData rs_ctup;		/* current tuple in scan, if any */
69 	BlockNumber rs_cblock;		/* current block # in scan, if any */
70 	Buffer		rs_cbuf;		/* current buffer in scan, if any */
71 	/* NB: if rs_cbuf is not InvalidBuffer, we hold a pin on that buffer */
72 	ParallelHeapScanDesc rs_parallel;	/* parallel scan information */
73 
74 	/* these fields only used in page-at-a-time mode and for bitmap scans */
75 	int			rs_cindex;		/* current tuple's index in vistuples */
76 	int			rs_ntuples;		/* number of visible tuples on page */
77 	OffsetNumber rs_vistuples[MaxHeapTuplesPerPage];	/* their offsets */
78 }			HeapScanDescData;
79 
80 /*
81  * We use the same IndexScanDescData structure for both amgettuple-based
82  * and amgetbitmap-based index scans.  Some fields are only relevant in
83  * amgettuple-based scans.
84  */
85 typedef struct IndexScanDescData
86 {
87 	/* scan parameters */
88 	Relation	heapRelation;	/* heap relation descriptor, or NULL */
89 	Relation	indexRelation;	/* index relation descriptor */
90 	Snapshot	xs_snapshot;	/* snapshot to see */
91 	int			numberOfKeys;	/* number of index qualifier conditions */
92 	int			numberOfOrderBys;	/* number of ordering operators */
93 	ScanKey		keyData;		/* array of index qualifier descriptors */
94 	ScanKey		orderByData;	/* array of ordering op descriptors */
95 	bool		xs_want_itup;	/* caller requests index tuples */
96 	bool		xs_temp_snap;	/* unregister snapshot at scan end? */
97 
98 	/* signaling to index AM about killing index tuples */
99 	bool		kill_prior_tuple;	/* last-returned tuple is dead */
100 	bool		ignore_killed_tuples;	/* do not return killed entries */
101 	bool		xactStartedInRecovery;	/* prevents killing/seeing killed
102 										 * tuples */
103 
104 	/* index access method's private state */
105 	void	   *opaque;			/* access-method-specific info */
106 
107 	/*
108 	 * In an index-only scan, a successful amgettuple call must fill either
109 	 * xs_itup (and xs_itupdesc) or xs_hitup (and xs_hitupdesc) to provide the
110 	 * data returned by the scan.  It can fill both, in which case the heap
111 	 * format will be used.
112 	 */
113 	IndexTuple	xs_itup;		/* index tuple returned by AM */
114 	TupleDesc	xs_itupdesc;	/* rowtype descriptor of xs_itup */
115 	HeapTuple	xs_hitup;		/* index data returned by AM, as HeapTuple */
116 	TupleDesc	xs_hitupdesc;	/* rowtype descriptor of xs_hitup */
117 
118 	/* xs_ctup/xs_cbuf/xs_recheck are valid after a successful index_getnext */
119 	HeapTupleData xs_ctup;		/* current heap tuple, if any */
120 	Buffer		xs_cbuf;		/* current heap buffer in scan, if any */
121 	/* NB: if xs_cbuf is not InvalidBuffer, we hold a pin on that buffer */
122 	bool		xs_recheck;		/* T means scan keys must be rechecked */
123 
124 	/*
125 	 * When fetching with an ordering operator, the values of the ORDER BY
126 	 * expressions of the last returned tuple, according to the index.  If
127 	 * xs_recheckorderby is true, these need to be rechecked just like the
128 	 * scan keys, and the values returned here are a lower-bound on the actual
129 	 * values.
130 	 */
131 	Datum	   *xs_orderbyvals;
132 	bool	   *xs_orderbynulls;
133 	bool		xs_recheckorderby;
134 
135 	/* state data for traversing HOT chains in index_getnext */
136 	bool		xs_continue_hot;	/* T if must keep walking HOT chain */
137 
138 	/* parallel index scan information, in shared memory */
139 	ParallelIndexScanDesc parallel_scan;
140 }			IndexScanDescData;
141 
142 /* Generic structure for parallel scans */
143 typedef struct ParallelIndexScanDescData
144 {
145 	Oid			ps_relid;
146 	Oid			ps_indexid;
147 	Size		ps_offset;		/* Offset in bytes of am specific structure */
148 	char		ps_snapshot_data[FLEXIBLE_ARRAY_MEMBER];
149 }			ParallelIndexScanDescData;
150 
151 /* Struct for heap-or-index scans of system tables */
152 typedef struct SysScanDescData
153 {
154 	Relation	heap_rel;		/* catalog being scanned */
155 	Relation	irel;			/* NULL if doing heap scan */
156 	HeapScanDesc scan;			/* only valid in heap-scan case */
157 	IndexScanDesc iscan;		/* only valid in index-scan case */
158 	Snapshot	snapshot;		/* snapshot to unregister at end of scan */
159 }			SysScanDescData;
160 
161 #endif							/* RELSCAN_H */
162