PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
itup.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * itup.h
4  * POSTGRES index tuple 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/itup.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef ITUP_H
15 #define ITUP_H
16 
17 #include "access/tupdesc.h"
18 #include "access/tupmacs.h"
19 #include "storage/bufpage.h"
20 #include "storage/itemptr.h"
21 
22 /*
23  * Index tuple header structure
24  *
25  * All index tuples start with IndexTupleData. If the HasNulls bit is set,
26  * this is followed by an IndexAttributeBitMapData. The index attribute
27  * values follow, beginning at a MAXALIGN boundary.
28  *
29  * Note that the space allocated for the bitmap does not vary with the number
30  * of attributes; that is because we don't have room to store the number of
31  * attributes in the header. Given the MAXALIGN constraint there's no space
32  * savings to be had anyway, for usual values of INDEX_MAX_KEYS.
33  */
34 
35 typedef struct IndexTupleData
36 {
37  ItemPointerData t_tid; /* reference TID to heap tuple */
38 
39  /* ---------------
40  * t_info is laid out in the following fashion:
41  *
42  * 15th (high) bit: has nulls
43  * 14th bit: has var-width attributes
44  * 13th bit: unused
45  * 12-0 bit: size of tuple
46  * ---------------
47  */
48 
49  unsigned short t_info; /* various info about tuple */
50 
51 } IndexTupleData; /* MORE DATA FOLLOWS AT END OF STRUCT */
52 
54 
56 {
57  bits8 bits[(INDEX_MAX_KEYS + 8 - 1) / 8];
59 
61 
62 /*
63  * t_info manipulation macros
64  */
65 #define INDEX_SIZE_MASK 0x1FFF
66 /* bit 0x2000 is reserved for index-AM specific usage */
67 #define INDEX_VAR_MASK 0x4000
68 #define INDEX_NULL_MASK 0x8000
69 
70 #define IndexTupleSize(itup) ((Size) (((IndexTuple) (itup))->t_info & INDEX_SIZE_MASK))
71 #define IndexTupleDSize(itup) ((Size) ((itup).t_info & INDEX_SIZE_MASK))
72 #define IndexTupleHasNulls(itup) ((((IndexTuple) (itup))->t_info & INDEX_NULL_MASK))
73 #define IndexTupleHasVarwidths(itup) ((((IndexTuple) (itup))->t_info & INDEX_VAR_MASK))
74 
75 
76 /*
77  * Takes an infomask as argument (primarily because this needs to be usable
78  * at index_form_tuple time so enough space is allocated).
79  */
80 #define IndexInfoFindDataOffset(t_info) \
81 ( \
82  (!((t_info) & INDEX_NULL_MASK)) ? \
83  ( \
84  (Size)MAXALIGN(sizeof(IndexTupleData)) \
85  ) \
86  : \
87  ( \
88  (Size)MAXALIGN(sizeof(IndexTupleData) + sizeof(IndexAttributeBitMapData)) \
89  ) \
90 )
91 
92 /* ----------------
93  * index_getattr
94  *
95  * This gets called many times, so we macro the cacheable and NULL
96  * lookups, and call nocache_index_getattr() for the rest.
97  *
98  * ----------------
99  */
100 #define index_getattr(tup, attnum, tupleDesc, isnull) \
101 ( \
102  AssertMacro(PointerIsValid(isnull) && (attnum) > 0), \
103  *(isnull) = false, \
104  !IndexTupleHasNulls(tup) ? \
105  ( \
106  (tupleDesc)->attrs[(attnum)-1]->attcacheoff >= 0 ? \
107  ( \
108  fetchatt((tupleDesc)->attrs[(attnum)-1], \
109  (char *) (tup) + IndexInfoFindDataOffset((tup)->t_info) \
110  + (tupleDesc)->attrs[(attnum)-1]->attcacheoff) \
111  ) \
112  : \
113  nocache_index_getattr((tup), (attnum), (tupleDesc)) \
114  ) \
115  : \
116  ( \
117  (att_isnull((attnum)-1, (char *)(tup) + sizeof(IndexTupleData))) ? \
118  ( \
119  *(isnull) = true, \
120  (Datum)NULL \
121  ) \
122  : \
123  ( \
124  nocache_index_getattr((tup), (attnum), (tupleDesc)) \
125  ) \
126  ) \
127 )
128 
129 /*
130  * MaxIndexTuplesPerPage is an upper bound on the number of tuples that can
131  * fit on one index page. An index tuple must have either data or a null
132  * bitmap, so we can safely assume it's at least 1 byte bigger than a bare
133  * IndexTupleData struct. We arrive at the divisor because each tuple
134  * must be maxaligned, and it must have an associated item pointer.
135  */
136 #define MinIndexTupleSize MAXALIGN(sizeof(IndexTupleData) + 1)
137 #define MaxIndexTuplesPerPage \
138  ((int) ((BLCKSZ - SizeOfPageHeaderData) / \
139  (MAXALIGN(sizeof(IndexTupleData) + 1) + sizeof(ItemIdData))))
140 
141 
142 /* routines in indextuple.c */
143 extern IndexTuple index_form_tuple(TupleDesc tupleDescriptor,
144  Datum *values, bool *isnull);
145 extern Datum nocache_index_getattr(IndexTuple tup, int attnum,
147 extern void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor,
148  Datum *values, bool *isnull);
149 extern IndexTuple CopyIndexTuple(IndexTuple source);
150 
151 #endif /* ITUP_H */
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:37
ItemPointerData t_tid
Definition: itup.h:37
struct IndexAttributeBitMapData IndexAttributeBitMapData
IndexTupleData * IndexTuple
Definition: itup.h:53
bits8 bits[(INDEX_MAX_KEYS+8-1)/8]
Definition: itup.h:57
uint8 bits8
Definition: c.h:275
uintptr_t Datum
Definition: postgres.h:372
struct IndexTupleData IndexTupleData
#define INDEX_MAX_KEYS
static Datum values[MAXATTR]
Definition: bootstrap.c:163
void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:416
Datum nocache_index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc)
Definition: indextuple.c:208
unsigned short t_info
Definition: itup.h:49
IndexTuple CopyIndexTuple(IndexTuple source)
Definition: indextuple.c:434
IndexAttributeBitMapData * IndexAttributeBitMap
Definition: itup.h:60