PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
tupmacs.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * tupmacs.h
4  * Tuple macros used by both index tuples and heap tuples.
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/tupmacs.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef TUPMACS_H
15 #define TUPMACS_H
16 
17 
18 /*
19  * check to see if the ATT'th bit of an array of 8-bit bytes is set.
20  */
21 #define att_isnull(ATT, BITS) (!((BITS)[(ATT) >> 3] & (1 << ((ATT) & 0x07))))
22 
23 /*
24  * Given a Form_pg_attribute and a pointer into a tuple's data area,
25  * return the correct value or pointer.
26  *
27  * We return a Datum value in all cases. If the attribute has "byval" false,
28  * we return the same pointer into the tuple data area that we're passed.
29  * Otherwise, we return the correct number of bytes fetched from the data
30  * area and extended to Datum form.
31  *
32  * On machines where Datum is 8 bytes, we support fetching 8-byte byval
33  * attributes; otherwise, only 1, 2, and 4-byte values are supported.
34  *
35  * Note that T must already be properly aligned for this to work correctly.
36  */
37 #define fetchatt(A,T) fetch_att(T, (A)->attbyval, (A)->attlen)
38 
39 /*
40  * Same, but work from byval/len parameters rather than Form_pg_attribute.
41  */
42 #if SIZEOF_DATUM == 8
43 
44 #define fetch_att(T,attbyval,attlen) \
45 ( \
46  (attbyval) ? \
47  ( \
48  (attlen) == (int) sizeof(Datum) ? \
49  *((Datum *)(T)) \
50  : \
51  ( \
52  (attlen) == (int) sizeof(int32) ? \
53  Int32GetDatum(*((int32 *)(T))) \
54  : \
55  ( \
56  (attlen) == (int) sizeof(int16) ? \
57  Int16GetDatum(*((int16 *)(T))) \
58  : \
59  ( \
60  AssertMacro((attlen) == 1), \
61  CharGetDatum(*((char *)(T))) \
62  ) \
63  ) \
64  ) \
65  ) \
66  : \
67  PointerGetDatum((char *) (T)) \
68 )
69 #else /* SIZEOF_DATUM != 8 */
70 
71 #define fetch_att(T,attbyval,attlen) \
72 ( \
73  (attbyval) ? \
74  ( \
75  (attlen) == (int) sizeof(int32) ? \
76  Int32GetDatum(*((int32 *)(T))) \
77  : \
78  ( \
79  (attlen) == (int) sizeof(int16) ? \
80  Int16GetDatum(*((int16 *)(T))) \
81  : \
82  ( \
83  AssertMacro((attlen) == 1), \
84  CharGetDatum(*((char *)(T))) \
85  ) \
86  ) \
87  ) \
88  : \
89  PointerGetDatum((char *) (T)) \
90 )
91 #endif /* SIZEOF_DATUM == 8 */
92 
93 /*
94  * att_align_datum aligns the given offset as needed for a datum of alignment
95  * requirement attalign and typlen attlen. attdatum is the Datum variable
96  * we intend to pack into a tuple (it's only accessed if we are dealing with
97  * a varlena type). Note that this assumes the Datum will be stored as-is;
98  * callers that are intending to convert non-short varlena datums to short
99  * format have to account for that themselves.
100  */
101 #define att_align_datum(cur_offset, attalign, attlen, attdatum) \
102 ( \
103  ((attlen) == -1 && VARATT_IS_SHORT(DatumGetPointer(attdatum))) ? \
104  (uintptr_t) (cur_offset) : \
105  att_align_nominal(cur_offset, attalign) \
106 )
107 
108 /*
109  * att_align_pointer performs the same calculation as att_align_datum,
110  * but is used when walking a tuple. attptr is the current actual data
111  * pointer; when accessing a varlena field we have to "peek" to see if we
112  * are looking at a pad byte or the first byte of a 1-byte-header datum.
113  * (A zero byte must be either a pad byte, or the first byte of a correctly
114  * aligned 4-byte length word; in either case we can align safely. A non-zero
115  * byte must be either a 1-byte length word, or the first byte of a correctly
116  * aligned 4-byte length word; in either case we need not align.)
117  *
118  * Note: some callers pass a "char *" pointer for cur_offset. This is
119  * a bit of a hack but should work all right as long as uintptr_t is the
120  * correct width.
121  */
122 #define att_align_pointer(cur_offset, attalign, attlen, attptr) \
123 ( \
124  ((attlen) == -1 && VARATT_NOT_PAD_BYTE(attptr)) ? \
125  (uintptr_t) (cur_offset) : \
126  att_align_nominal(cur_offset, attalign) \
127 )
128 
129 /*
130  * att_align_nominal aligns the given offset as needed for a datum of alignment
131  * requirement attalign, ignoring any consideration of packed varlena datums.
132  * There are three main use cases for using this macro directly:
133  * * we know that the att in question is not varlena (attlen != -1);
134  * in this case it is cheaper than the above macros and just as good.
135  * * we need to estimate alignment padding cost abstractly, ie without
136  * reference to a real tuple. We must assume the worst case that
137  * all varlenas are aligned.
138  * * within arrays, we unconditionally align varlenas (XXX this should be
139  * revisited, probably).
140  *
141  * The attalign cases are tested in what is hopefully something like their
142  * frequency of occurrence.
143  */
144 #define att_align_nominal(cur_offset, attalign) \
145 ( \
146  ((attalign) == 'i') ? INTALIGN(cur_offset) : \
147  (((attalign) == 'c') ? (uintptr_t) (cur_offset) : \
148  (((attalign) == 'd') ? DOUBLEALIGN(cur_offset) : \
149  ( \
150  AssertMacro((attalign) == 's'), \
151  SHORTALIGN(cur_offset) \
152  ))) \
153 )
154 
155 /*
156  * att_addlength_datum increments the given offset by the space needed for
157  * the given Datum variable. attdatum is only accessed if we are dealing
158  * with a variable-length attribute.
159  */
160 #define att_addlength_datum(cur_offset, attlen, attdatum) \
161  att_addlength_pointer(cur_offset, attlen, DatumGetPointer(attdatum))
162 
163 /*
164  * att_addlength_pointer performs the same calculation as att_addlength_datum,
165  * but is used when walking a tuple --- attptr is the pointer to the field
166  * within the tuple.
167  *
168  * Note: some callers pass a "char *" pointer for cur_offset. This is
169  * actually perfectly OK, but probably should be cleaned up along with
170  * the same practice for att_align_pointer.
171  */
172 #define att_addlength_pointer(cur_offset, attlen, attptr) \
173 ( \
174  ((attlen) > 0) ? \
175  ( \
176  (cur_offset) + (attlen) \
177  ) \
178  : (((attlen) == -1) ? \
179  ( \
180  (cur_offset) + VARSIZE_ANY(attptr) \
181  ) \
182  : \
183  ( \
184  AssertMacro((attlen) == -2), \
185  (cur_offset) + (strlen((char *) (attptr)) + 1) \
186  )) \
187 )
188 
189 /*
190  * store_att_byval is a partial inverse of fetch_att: store a given Datum
191  * value into a tuple data area at the specified address. However, it only
192  * handles the byval case, because in typical usage the caller needs to
193  * distinguish by-val and by-ref cases anyway, and so a do-it-all macro
194  * wouldn't be convenient.
195  */
196 #if SIZEOF_DATUM == 8
197 
198 #define store_att_byval(T,newdatum,attlen) \
199  do { \
200  switch (attlen) \
201  { \
202  case sizeof(char): \
203  *(char *) (T) = DatumGetChar(newdatum); \
204  break; \
205  case sizeof(int16): \
206  *(int16 *) (T) = DatumGetInt16(newdatum); \
207  break; \
208  case sizeof(int32): \
209  *(int32 *) (T) = DatumGetInt32(newdatum); \
210  break; \
211  case sizeof(Datum): \
212  *(Datum *) (T) = (newdatum); \
213  break; \
214  default: \
215  elog(ERROR, "unsupported byval length: %d", \
216  (int) (attlen)); \
217  break; \
218  } \
219  } while (0)
220 #else /* SIZEOF_DATUM != 8 */
221 
222 #define store_att_byval(T,newdatum,attlen) \
223  do { \
224  switch (attlen) \
225  { \
226  case sizeof(char): \
227  *(char *) (T) = DatumGetChar(newdatum); \
228  break; \
229  case sizeof(int16): \
230  *(int16 *) (T) = DatumGetInt16(newdatum); \
231  break; \
232  case sizeof(int32): \
233  *(int32 *) (T) = DatumGetInt32(newdatum); \
234  break; \
235  default: \
236  elog(ERROR, "unsupported byval length: %d", \
237  (int) (attlen)); \
238  break; \
239  } \
240  } while (0)
241 #endif /* SIZEOF_DATUM == 8 */
242 
243 #endif