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