Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * tupmacs.h
4 : * Tuple macros used by both index tuples and heap tuples.
5 : *
6 : *
7 : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : * Portions Copyright (c) 1994, Regents of the University of California
9 : *
10 : * src/include/access/tupmacs.h
11 : *
12 : *-------------------------------------------------------------------------
13 : */
14 : #ifndef TUPMACS_H
15 : #define TUPMACS_H
16 :
17 : #include "catalog/pg_type_d.h" /* for TYPALIGN macros */
18 :
19 :
20 : /*
21 : * Check a tuple's null bitmap to determine whether the attribute is null.
22 : * Note that a 0 in the null bitmap indicates a null, while 1 indicates
23 : * non-null.
24 : */
25 : static inline bool
26 783389376 : att_isnull(int ATT, const bits8 *BITS)
27 : {
28 783389376 : return !(BITS[ATT >> 3] & (1 << (ATT & 0x07)));
29 : }
30 :
31 : #ifndef FRONTEND
32 : /*
33 : * Given an attbyval and an attlen from either a Form_pg_attribute or
34 : * CompactAttribute and a pointer into a tuple's data area, return the
35 : * correct value or pointer.
36 : *
37 : * We return a Datum value in all cases. If attbyval is false, we return the
38 : * same pointer into the tuple data area that we're passed. Otherwise, we
39 : * return the correct number of bytes fetched from the data area and extended
40 : * to Datum form.
41 : *
42 : * Note that T must already be properly aligned for this to work correctly.
43 : */
44 : #define fetchatt(A,T) fetch_att(T, (A)->attbyval, (A)->attlen)
45 :
46 : /*
47 : * Same, but work from byval/len parameters rather than Form_pg_attribute.
48 : */
49 : static inline Datum
50 1687643342 : fetch_att(const void *T, bool attbyval, int attlen)
51 : {
52 1687643342 : if (attbyval)
53 : {
54 1403232270 : switch (attlen)
55 : {
56 154798750 : case sizeof(char):
57 154798750 : return CharGetDatum(*((const char *) T));
58 81161782 : case sizeof(int16):
59 81161782 : return Int16GetDatum(*((const int16 *) T));
60 1125176172 : case sizeof(int32):
61 1125176172 : return Int32GetDatum(*((const int32 *) T));
62 42095566 : case sizeof(int64):
63 42095566 : return Int64GetDatum(*((const int64 *) T));
64 0 : default:
65 0 : elog(ERROR, "unsupported byval length: %d", attlen);
66 : return 0;
67 : }
68 : }
69 : else
70 284411072 : return PointerGetDatum(T);
71 : }
72 : #endif /* FRONTEND */
73 :
74 : /*
75 : * typalign_to_alignby: map a TYPALIGN_xxx value to the numeric alignment
76 : * value it represents. (We store TYPALIGN_xxx codes not the real alignment
77 : * values mainly so that initial catalog contents can be machine-independent.)
78 : */
79 : static inline uint8
80 137570334 : typalign_to_alignby(char typalign)
81 : {
82 : uint8 alignby;
83 :
84 137570334 : switch (typalign)
85 : {
86 21380256 : case TYPALIGN_CHAR:
87 21380256 : alignby = sizeof(char);
88 21380256 : break;
89 5819876 : case TYPALIGN_SHORT:
90 5819876 : alignby = ALIGNOF_SHORT;
91 5819876 : break;
92 103890160 : case TYPALIGN_INT:
93 103890160 : alignby = ALIGNOF_INT;
94 103890160 : break;
95 6480042 : case TYPALIGN_DOUBLE:
96 6480042 : alignby = ALIGNOF_DOUBLE;
97 6480042 : break;
98 0 : default:
99 : #ifndef FRONTEND
100 0 : elog(ERROR, "invalid typalign value: %c", typalign);
101 : #else
102 : fprintf(stderr, "invalid typalign value: %c\n", typalign);
103 : exit(1);
104 : #endif
105 : alignby = 0;
106 : break;
107 : }
108 137570334 : return alignby;
109 : }
110 :
111 : /*
112 : * att_align_datum aligns the given offset as needed for a datum of alignment
113 : * requirement attalign and typlen attlen. attdatum is the Datum variable
114 : * we intend to pack into a tuple (it's only accessed if we are dealing with
115 : * a varlena type). Note that this assumes the Datum will be stored as-is;
116 : * callers that are intending to convert non-short varlena datums to short
117 : * format have to account for that themselves.
118 : */
119 : #define att_align_datum(cur_offset, attalign, attlen, attdatum) \
120 : ( \
121 : ((attlen) == -1 && VARATT_IS_SHORT(DatumGetPointer(attdatum))) ? \
122 : (uintptr_t) (cur_offset) : \
123 : att_align_nominal(cur_offset, attalign) \
124 : )
125 :
126 : /*
127 : * Similar to att_align_datum, but accepts a number of bytes, typically from
128 : * CompactAttribute.attalignby to align the Datum by.
129 : */
130 : #define att_datum_alignby(cur_offset, attalignby, attlen, attdatum) \
131 : ( \
132 : ((attlen) == -1 && VARATT_IS_SHORT(DatumGetPointer(attdatum))) ? \
133 : (uintptr_t) (cur_offset) : \
134 : TYPEALIGN(attalignby, cur_offset))
135 :
136 : /*
137 : * att_align_pointer performs the same calculation as att_align_datum,
138 : * but is used when walking a tuple. attptr is the current actual data
139 : * pointer; when accessing a varlena field we have to "peek" to see if we
140 : * are looking at a pad byte or the first byte of a 1-byte-header datum.
141 : * (A zero byte must be either a pad byte, or the first byte of a correctly
142 : * aligned 4-byte length word; in either case we can align safely. A non-zero
143 : * byte must be either a 1-byte length word, or the first byte of a correctly
144 : * aligned 4-byte length word; in either case we need not align.)
145 : *
146 : * Note: some callers pass a "char *" pointer for cur_offset. This is
147 : * a bit of a hack but should work all right as long as uintptr_t is the
148 : * correct width.
149 : */
150 : #define att_align_pointer(cur_offset, attalign, attlen, attptr) \
151 : ( \
152 : ((attlen) == -1 && VARATT_NOT_PAD_BYTE(attptr)) ? \
153 : (uintptr_t) (cur_offset) : \
154 : att_align_nominal(cur_offset, attalign) \
155 : )
156 :
157 : /*
158 : * Similar to att_align_pointer, but accepts a number of bytes, typically from
159 : * CompactAttribute.attalignby to align the pointer by.
160 : */
161 : #define att_pointer_alignby(cur_offset, attalignby, attlen, attptr) \
162 : ( \
163 : ((attlen) == -1 && VARATT_NOT_PAD_BYTE(attptr)) ? \
164 : (uintptr_t) (cur_offset) : \
165 : TYPEALIGN(attalignby, cur_offset))
166 :
167 : /*
168 : * att_align_nominal aligns the given offset as needed for a datum of alignment
169 : * requirement attalign, ignoring any consideration of packed varlena datums.
170 : * There are three main use cases for using this macro directly:
171 : * * we know that the att in question is not varlena (attlen != -1);
172 : * in this case it is cheaper than the above macros and just as good.
173 : * * we need to estimate alignment padding cost abstractly, ie without
174 : * reference to a real tuple. We must assume the worst case that
175 : * all varlenas are aligned.
176 : * * within arrays and multiranges, we unconditionally align varlenas (XXX this
177 : * should be revisited, probably).
178 : *
179 : * In performance-critical loops, avoid using this macro; instead use
180 : * att_nominal_alignby with a pre-computed alignby value.
181 : */
182 : #define att_align_nominal(cur_offset, attalign) \
183 : att_nominal_alignby(cur_offset, typalign_to_alignby(attalign))
184 :
185 : /*
186 : * Similar to att_align_nominal, but accepts a number of bytes, typically from
187 : * CompactAttribute.attalignby to align the offset by.
188 : */
189 : #define att_nominal_alignby(cur_offset, attalignby) \
190 : TYPEALIGN(attalignby, cur_offset)
191 :
192 : /*
193 : * att_addlength_datum increments the given offset by the space needed for
194 : * the given Datum variable. attdatum is only accessed if we are dealing
195 : * with a variable-length attribute.
196 : */
197 : #define att_addlength_datum(cur_offset, attlen, attdatum) \
198 : att_addlength_pointer(cur_offset, attlen, DatumGetPointer(attdatum))
199 :
200 : /*
201 : * att_addlength_pointer performs the same calculation as att_addlength_datum,
202 : * but is used when walking a tuple --- attptr is the pointer to the field
203 : * within the tuple.
204 : *
205 : * Note: some callers pass a "char *" pointer for cur_offset. This is
206 : * actually perfectly OK, but probably should be cleaned up along with
207 : * the same practice for att_align_pointer.
208 : */
209 : #define att_addlength_pointer(cur_offset, attlen, attptr) \
210 : ( \
211 : ((attlen) > 0) ? \
212 : ( \
213 : (cur_offset) + (attlen) \
214 : ) \
215 : : (((attlen) == -1) ? \
216 : ( \
217 : (cur_offset) + VARSIZE_ANY(attptr) \
218 : ) \
219 : : \
220 : ( \
221 : AssertMacro((attlen) == -2), \
222 : (cur_offset) + (strlen((const char *) (attptr)) + 1) \
223 : )) \
224 : )
225 :
226 : #ifndef FRONTEND
227 : /*
228 : * store_att_byval is a partial inverse of fetch_att: store a given Datum
229 : * value into a tuple data area at the specified address. However, it only
230 : * handles the byval case, because in typical usage the caller needs to
231 : * distinguish by-val and by-ref cases anyway, and so a do-it-all function
232 : * wouldn't be convenient.
233 : */
234 : static inline void
235 198895026 : store_att_byval(void *T, Datum newdatum, int attlen)
236 : {
237 198895026 : switch (attlen)
238 : {
239 27192860 : case sizeof(char):
240 27192860 : *(char *) T = DatumGetChar(newdatum);
241 27192860 : break;
242 9824102 : case sizeof(int16):
243 9824102 : *(int16 *) T = DatumGetInt16(newdatum);
244 9824102 : break;
245 142246556 : case sizeof(int32):
246 142246556 : *(int32 *) T = DatumGetInt32(newdatum);
247 142246556 : break;
248 19631508 : case sizeof(int64):
249 19631508 : *(int64 *) T = DatumGetInt64(newdatum);
250 19631508 : break;
251 0 : default:
252 0 : elog(ERROR, "unsupported byval length: %d", attlen);
253 : }
254 198895026 : }
255 : #endif /* FRONTEND */
256 :
257 : #endif /* TUPMACS_H */
|