tupmacs.h 7.4 KB

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