1 /*-------------------------------------------------------------------------
4 * POSTGRES index tuple definitions.
7 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/access/itup.h
12 *-------------------------------------------------------------------------
17 #include "access/tupdesc.h"
18 #include "access/tupmacs.h"
19 #include "storage/bufpage.h"
20 #include "storage/itemptr.h"
23 * Index tuple header structure
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.
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.
35 typedef struct IndexTupleData
37 ItemPointerData t_tid
; /* reference TID to heap tuple */
40 * t_info is laid out in the following fashion:
42 * 15th (high) bit: has nulls
43 * 14th bit: has var-width attributes
44 * 13th bit: AM-defined meaning
45 * 12-0 bit: size of tuple
49 unsigned short t_info
; /* various info about tuple */
51 } IndexTupleData
; /* MORE DATA FOLLOWS AT END OF STRUCT */
53 typedef IndexTupleData
*IndexTuple
;
55 typedef struct IndexAttributeBitMapData
57 bits8 bits
[(INDEX_MAX_KEYS
+ 8 - 1) / 8];
58 } IndexAttributeBitMapData
;
60 typedef IndexAttributeBitMapData
* IndexAttributeBitMap
;
63 * t_info manipulation macros
65 #define INDEX_SIZE_MASK 0x1FFF
66 #define INDEX_AM_RESERVED_BIT 0x2000 /* reserved for index-AM specific
68 #define INDEX_VAR_MASK 0x4000
69 #define INDEX_NULL_MASK 0x8000
71 #define IndexTupleSize(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))
77 * Takes an infomask as argument (primarily because this needs to be usable
78 * at index_form_tuple time so enough space is allocated).
80 #define IndexInfoFindDataOffset(t_info) \
82 (!((t_info) & INDEX_NULL_MASK)) ? \
84 (Size)MAXALIGN(sizeof(IndexTupleData)) \
88 (Size)MAXALIGN(sizeof(IndexTupleData) + sizeof(IndexAttributeBitMapData)) \
95 * This gets called many times, so we macro the cacheable and NULL
96 * lookups, and call nocache_index_getattr() for the rest.
100 #define index_getattr(tup, attnum, tupleDesc, isnull) \
102 AssertMacro(PointerIsValid(isnull) && (attnum) > 0), \
104 !IndexTupleHasNulls(tup) ? \
106 TupleDescAttr((tupleDesc), (attnum)-1)->attcacheoff >= 0 ? \
108 fetchatt(TupleDescAttr((tupleDesc), (attnum)-1), \
109 (char *) (tup) + IndexInfoFindDataOffset((tup)->t_info) \
110 + TupleDescAttr((tupleDesc), (attnum)-1)->attcacheoff) \
113 nocache_index_getattr((tup), (attnum), (tupleDesc)) \
117 (att_isnull((attnum)-1, (char *)(tup) + sizeof(IndexTupleData))) ? \
124 nocache_index_getattr((tup), (attnum), (tupleDesc)) \
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 line pointer.
136 * To be index-type-independent, this does not account for any special space
137 * on the page, and is thus conservative.
139 * Note: in btree non-leaf pages, the first tuple has no key (it's implicitly
140 * minus infinity), thus breaking the "at least 1 byte bigger" assumption.
141 * On such a page, N tuples could take one MAXALIGN quantum less space than
142 * estimated here, seemingly allowing one more tuple than estimated here.
143 * But such a page always has at least MAXALIGN special space, so we're safe.
145 #define MaxIndexTuplesPerPage \
146 ((int) ((BLCKSZ - SizeOfPageHeaderData) / \
147 (MAXALIGN(sizeof(IndexTupleData) + 1) + sizeof(ItemIdData))))
150 /* routines in indextuple.c */
151 extern IndexTuple
index_form_tuple(TupleDesc tupleDescriptor
,
152 Datum
*values
, bool *isnull
);
153 extern Datum
nocache_index_getattr(IndexTuple tup
, int attnum
,
154 TupleDesc tupleDesc
);
155 extern void index_deform_tuple(IndexTuple tup
, TupleDesc tupleDescriptor
,
156 Datum
*values
, bool *isnull
);
157 extern void index_deform_tuple_internal(TupleDesc tupleDescriptor
,
158 Datum
*values
, bool *isnull
,
159 char *tp
, bits8
*bp
, int hasnulls
);
160 extern IndexTuple
CopyIndexTuple(IndexTuple source
);
161 extern IndexTuple
index_truncate_tuple(TupleDesc sourceDescriptor
,
162 IndexTuple source
, int leavenatts
);