2
0

htup_details.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*-------------------------------------------------------------------------
  2. *
  3. * htup_details.h
  4. * POSTGRES heap tuple header definitions.
  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/htup_details.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef HTUP_DETAILS_H
  15. #define HTUP_DETAILS_H
  16. #include "access/htup.h"
  17. #include "access/transam.h"
  18. #include "access/tupdesc.h"
  19. #include "access/tupmacs.h"
  20. #include "storage/bufpage.h"
  21. /*
  22. * MaxTupleAttributeNumber limits the number of (user) columns in a tuple.
  23. * The key limit on this value is that the size of the fixed overhead for
  24. * a tuple, plus the size of the null-values bitmap (at 1 bit per column),
  25. * plus MAXALIGN alignment, must fit into t_hoff which is uint8. On most
  26. * machines the upper limit without making t_hoff wider would be a little
  27. * over 1700. We use round numbers here and for MaxHeapAttributeNumber
  28. * so that alterations in HeapTupleHeaderData layout won't change the
  29. * supported max number of columns.
  30. */
  31. #define MaxTupleAttributeNumber 1664 /* 8 * 208 */
  32. /*
  33. * MaxHeapAttributeNumber limits the number of (user) columns in a table.
  34. * This should be somewhat less than MaxTupleAttributeNumber. It must be
  35. * at least one less, else we will fail to do UPDATEs on a maximal-width
  36. * table (because UPDATE has to form working tuples that include CTID).
  37. * In practice we want some additional daylight so that we can gracefully
  38. * support operations that add hidden "resjunk" columns, for example
  39. * SELECT * FROM wide_table ORDER BY foo, bar, baz.
  40. * In any case, depending on column data types you will likely be running
  41. * into the disk-block-based limit on overall tuple size if you have more
  42. * than a thousand or so columns. TOAST won't help.
  43. */
  44. #define MaxHeapAttributeNumber 1600 /* 8 * 200 */
  45. /*
  46. * Heap tuple header. To avoid wasting space, the fields should be
  47. * laid out in such a way as to avoid structure padding.
  48. *
  49. * Datums of composite types (row types) share the same general structure
  50. * as on-disk tuples, so that the same routines can be used to build and
  51. * examine them. However the requirements are slightly different: a Datum
  52. * does not need any transaction visibility information, and it does need
  53. * a length word and some embedded type information. We can achieve this
  54. * by overlaying the xmin/cmin/xmax/cmax/xvac fields of a heap tuple
  55. * with the fields needed in the Datum case. Typically, all tuples built
  56. * in-memory will be initialized with the Datum fields; but when a tuple is
  57. * about to be inserted in a table, the transaction fields will be filled,
  58. * overwriting the datum fields.
  59. *
  60. * The overall structure of a heap tuple looks like:
  61. * fixed fields (HeapTupleHeaderData struct)
  62. * nulls bitmap (if HEAP_HASNULL is set in t_infomask)
  63. * alignment padding (as needed to make user data MAXALIGN'd)
  64. * object ID (if HEAP_HASOID_OLD is set in t_infomask, not created
  65. * anymore)
  66. * user data fields
  67. *
  68. * We store five "virtual" fields Xmin, Cmin, Xmax, Cmax, and Xvac in three
  69. * physical fields. Xmin and Xmax are always really stored, but Cmin, Cmax
  70. * and Xvac share a field. This works because we know that Cmin and Cmax
  71. * are only interesting for the lifetime of the inserting and deleting
  72. * transaction respectively. If a tuple is inserted and deleted in the same
  73. * transaction, we store a "combo" command id that can be mapped to the real
  74. * cmin and cmax, but only by use of local state within the originating
  75. * backend. See combocid.c for more details. Meanwhile, Xvac is only set by
  76. * old-style VACUUM FULL, which does not have any command sub-structure and so
  77. * does not need either Cmin or Cmax. (This requires that old-style VACUUM
  78. * FULL never try to move a tuple whose Cmin or Cmax is still interesting,
  79. * ie, an insert-in-progress or delete-in-progress tuple.)
  80. *
  81. * A word about t_ctid: whenever a new tuple is stored on disk, its t_ctid
  82. * is initialized with its own TID (location). If the tuple is ever updated,
  83. * its t_ctid is changed to point to the replacement version of the tuple. Or
  84. * if the tuple is moved from one partition to another, due to an update of
  85. * the partition key, t_ctid is set to a special value to indicate that
  86. * (see ItemPointerSetMovedPartitions). Thus, a tuple is the latest version
  87. * of its row iff XMAX is invalid or
  88. * t_ctid points to itself (in which case, if XMAX is valid, the tuple is
  89. * either locked or deleted). One can follow the chain of t_ctid links
  90. * to find the newest version of the row, unless it was moved to a different
  91. * partition. Beware however that VACUUM might
  92. * erase the pointed-to (newer) tuple before erasing the pointing (older)
  93. * tuple. Hence, when following a t_ctid link, it is necessary to check
  94. * to see if the referenced slot is empty or contains an unrelated tuple.
  95. * Check that the referenced tuple has XMIN equal to the referencing tuple's
  96. * XMAX to verify that it is actually the descendant version and not an
  97. * unrelated tuple stored into a slot recently freed by VACUUM. If either
  98. * check fails, one may assume that there is no live descendant version.
  99. *
  100. * t_ctid is sometimes used to store a speculative insertion token, instead
  101. * of a real TID. A speculative token is set on a tuple that's being
  102. * inserted, until the inserter is sure that it wants to go ahead with the
  103. * insertion. Hence a token should only be seen on a tuple with an XMAX
  104. * that's still in-progress, or invalid/aborted. The token is replaced with
  105. * the tuple's real TID when the insertion is confirmed. One should never
  106. * see a speculative insertion token while following a chain of t_ctid links,
  107. * because they are not used on updates, only insertions.
  108. *
  109. * Following the fixed header fields, the nulls bitmap is stored (beginning
  110. * at t_bits). The bitmap is *not* stored if t_infomask shows that there
  111. * are no nulls in the tuple. If an OID field is present (as indicated by
  112. * t_infomask), then it is stored just before the user data, which begins at
  113. * the offset shown by t_hoff. Note that t_hoff must be a multiple of
  114. * MAXALIGN.
  115. */
  116. typedef struct HeapTupleFields
  117. {
  118. TransactionId t_xmin; /* inserting xact ID */
  119. TransactionId t_xmax; /* deleting or locking xact ID */
  120. union
  121. {
  122. CommandId t_cid; /* inserting or deleting command ID, or both */
  123. TransactionId t_xvac; /* old-style VACUUM FULL xact ID */
  124. } t_field3;
  125. } HeapTupleFields;
  126. typedef struct DatumTupleFields
  127. {
  128. int32 datum_len_; /* varlena header (do not touch directly!) */
  129. int32 datum_typmod; /* -1, or identifier of a record type */
  130. Oid datum_typeid; /* composite type OID, or RECORDOID */
  131. /*
  132. * datum_typeid cannot be a domain over composite, only plain composite,
  133. * even if the datum is meant as a value of a domain-over-composite type.
  134. * This is in line with the general principle that CoerceToDomain does not
  135. * change the physical representation of the base type value.
  136. *
  137. * Note: field ordering is chosen with thought that Oid might someday
  138. * widen to 64 bits.
  139. */
  140. } DatumTupleFields;
  141. struct HeapTupleHeaderData
  142. {
  143. union
  144. {
  145. HeapTupleFields t_heap;
  146. DatumTupleFields t_datum;
  147. } t_choice;
  148. ItemPointerData t_ctid; /* current TID of this or newer tuple (or a
  149. * speculative insertion token) */
  150. /* Fields below here must match MinimalTupleData! */
  151. #define FIELDNO_HEAPTUPLEHEADERDATA_INFOMASK2 2
  152. uint16 t_infomask2; /* number of attributes + various flags */
  153. #define FIELDNO_HEAPTUPLEHEADERDATA_INFOMASK 3
  154. uint16 t_infomask; /* various flag bits, see below */
  155. #define FIELDNO_HEAPTUPLEHEADERDATA_HOFF 4
  156. uint8 t_hoff; /* sizeof header incl. bitmap, padding */
  157. /* ^ - 23 bytes - ^ */
  158. #define FIELDNO_HEAPTUPLEHEADERDATA_BITS 5
  159. bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]; /* bitmap of NULLs */
  160. /* MORE DATA FOLLOWS AT END OF STRUCT */
  161. };
  162. /* typedef appears in htup.h */
  163. #define SizeofHeapTupleHeader offsetof(HeapTupleHeaderData, t_bits)
  164. /*
  165. * information stored in t_infomask:
  166. */
  167. #define HEAP_HASNULL 0x0001 /* has null attribute(s) */
  168. #define HEAP_HASVARWIDTH 0x0002 /* has variable-width attribute(s) */
  169. #define HEAP_HASEXTERNAL 0x0004 /* has external stored attribute(s) */
  170. #define HEAP_HASOID_OLD 0x0008 /* has an object-id field */
  171. #define HEAP_XMAX_KEYSHR_LOCK 0x0010 /* xmax is a key-shared locker */
  172. #define HEAP_COMBOCID 0x0020 /* t_cid is a combo CID */
  173. #define HEAP_XMAX_EXCL_LOCK 0x0040 /* xmax is exclusive locker */
  174. #define HEAP_XMAX_LOCK_ONLY 0x0080 /* xmax, if valid, is only a locker */
  175. /* xmax is a shared locker */
  176. #define HEAP_XMAX_SHR_LOCK (HEAP_XMAX_EXCL_LOCK | HEAP_XMAX_KEYSHR_LOCK)
  177. #define HEAP_LOCK_MASK (HEAP_XMAX_SHR_LOCK | HEAP_XMAX_EXCL_LOCK | \
  178. HEAP_XMAX_KEYSHR_LOCK)
  179. #define HEAP_XMIN_COMMITTED 0x0100 /* t_xmin committed */
  180. #define HEAP_XMIN_INVALID 0x0200 /* t_xmin invalid/aborted */
  181. #define HEAP_XMIN_FROZEN (HEAP_XMIN_COMMITTED|HEAP_XMIN_INVALID)
  182. #define HEAP_XMAX_COMMITTED 0x0400 /* t_xmax committed */
  183. #define HEAP_XMAX_INVALID 0x0800 /* t_xmax invalid/aborted */
  184. #define HEAP_XMAX_IS_MULTI 0x1000 /* t_xmax is a MultiXactId */
  185. #define HEAP_UPDATED 0x2000 /* this is UPDATEd version of row */
  186. #define HEAP_MOVED_OFF 0x4000 /* moved to another place by pre-9.0
  187. * VACUUM FULL; kept for binary
  188. * upgrade support */
  189. #define HEAP_MOVED_IN 0x8000 /* moved from another place by pre-9.0
  190. * VACUUM FULL; kept for binary
  191. * upgrade support */
  192. #define HEAP_MOVED (HEAP_MOVED_OFF | HEAP_MOVED_IN)
  193. #define HEAP_XACT_MASK 0xFFF0 /* visibility-related bits */
  194. /*
  195. * A tuple is only locked (i.e. not updated by its Xmax) if the
  196. * HEAP_XMAX_LOCK_ONLY bit is set; or, for pg_upgrade's sake, if the Xmax is
  197. * not a multi and the EXCL_LOCK bit is set.
  198. *
  199. * See also HeapTupleHeaderIsOnlyLocked, which also checks for a possible
  200. * aborted updater transaction.
  201. *
  202. * Beware of multiple evaluations of the argument.
  203. */
  204. #define HEAP_XMAX_IS_LOCKED_ONLY(infomask) \
  205. (((infomask) & HEAP_XMAX_LOCK_ONLY) || \
  206. (((infomask) & (HEAP_XMAX_IS_MULTI | HEAP_LOCK_MASK)) == HEAP_XMAX_EXCL_LOCK))
  207. /*
  208. * A tuple that has HEAP_XMAX_IS_MULTI and HEAP_XMAX_LOCK_ONLY but neither of
  209. * HEAP_XMAX_EXCL_LOCK and HEAP_XMAX_KEYSHR_LOCK must come from a tuple that was
  210. * share-locked in 9.2 or earlier and then pg_upgrade'd.
  211. *
  212. * In 9.2 and prior, HEAP_XMAX_IS_MULTI was only set when there were multiple
  213. * FOR SHARE lockers of that tuple. That set HEAP_XMAX_LOCK_ONLY (with a
  214. * different name back then) but neither of HEAP_XMAX_EXCL_LOCK and
  215. * HEAP_XMAX_KEYSHR_LOCK. That combination is no longer possible in 9.3 and
  216. * up, so if we see that combination we know for certain that the tuple was
  217. * locked in an earlier release; since all such lockers are gone (they cannot
  218. * survive through pg_upgrade), such tuples can safely be considered not
  219. * locked.
  220. *
  221. * We must not resolve such multixacts locally, because the result would be
  222. * bogus, regardless of where they stand with respect to the current valid
  223. * multixact range.
  224. */
  225. #define HEAP_LOCKED_UPGRADED(infomask) \
  226. ( \
  227. ((infomask) & HEAP_XMAX_IS_MULTI) != 0 && \
  228. ((infomask) & HEAP_XMAX_LOCK_ONLY) != 0 && \
  229. (((infomask) & (HEAP_XMAX_EXCL_LOCK | HEAP_XMAX_KEYSHR_LOCK)) == 0) \
  230. )
  231. /*
  232. * Use these to test whether a particular lock is applied to a tuple
  233. */
  234. #define HEAP_XMAX_IS_SHR_LOCKED(infomask) \
  235. (((infomask) & HEAP_LOCK_MASK) == HEAP_XMAX_SHR_LOCK)
  236. #define HEAP_XMAX_IS_EXCL_LOCKED(infomask) \
  237. (((infomask) & HEAP_LOCK_MASK) == HEAP_XMAX_EXCL_LOCK)
  238. #define HEAP_XMAX_IS_KEYSHR_LOCKED(infomask) \
  239. (((infomask) & HEAP_LOCK_MASK) == HEAP_XMAX_KEYSHR_LOCK)
  240. /* turn these all off when Xmax is to change */
  241. #define HEAP_XMAX_BITS (HEAP_XMAX_COMMITTED | HEAP_XMAX_INVALID | \
  242. HEAP_XMAX_IS_MULTI | HEAP_LOCK_MASK | HEAP_XMAX_LOCK_ONLY)
  243. /*
  244. * information stored in t_infomask2:
  245. */
  246. #define HEAP_NATTS_MASK 0x07FF /* 11 bits for number of attributes */
  247. /* bits 0x1800 are available */
  248. #define HEAP_KEYS_UPDATED 0x2000 /* tuple was updated and key cols
  249. * modified, or tuple deleted */
  250. #define HEAP_HOT_UPDATED 0x4000 /* tuple was HOT-updated */
  251. #define HEAP_ONLY_TUPLE 0x8000 /* this is heap-only tuple */
  252. #define HEAP2_XACT_MASK 0xE000 /* visibility-related bits */
  253. /*
  254. * HEAP_TUPLE_HAS_MATCH is a temporary flag used during hash joins. It is
  255. * only used in tuples that are in the hash table, and those don't need
  256. * any visibility information, so we can overlay it on a visibility flag
  257. * instead of using up a dedicated bit.
  258. */
  259. #define HEAP_TUPLE_HAS_MATCH HEAP_ONLY_TUPLE /* tuple has a join match */
  260. /*
  261. * HeapTupleHeader accessor macros
  262. *
  263. * Note: beware of multiple evaluations of "tup" argument. But the Set
  264. * macros evaluate their other argument only once.
  265. */
  266. /*
  267. * HeapTupleHeaderGetRawXmin returns the "raw" xmin field, which is the xid
  268. * originally used to insert the tuple. However, the tuple might actually
  269. * be frozen (via HeapTupleHeaderSetXminFrozen) in which case the tuple's xmin
  270. * is visible to every snapshot. Prior to PostgreSQL 9.4, we actually changed
  271. * the xmin to FrozenTransactionId, and that value may still be encountered
  272. * on disk.
  273. */
  274. #define HeapTupleHeaderGetRawXmin(tup) \
  275. ( \
  276. (tup)->t_choice.t_heap.t_xmin \
  277. )
  278. #define HeapTupleHeaderGetXmin(tup) \
  279. ( \
  280. HeapTupleHeaderXminFrozen(tup) ? \
  281. FrozenTransactionId : HeapTupleHeaderGetRawXmin(tup) \
  282. )
  283. #define HeapTupleHeaderSetXmin(tup, xid) \
  284. ( \
  285. (tup)->t_choice.t_heap.t_xmin = (xid) \
  286. )
  287. #define HeapTupleHeaderXminCommitted(tup) \
  288. ( \
  289. ((tup)->t_infomask & HEAP_XMIN_COMMITTED) != 0 \
  290. )
  291. #define HeapTupleHeaderXminInvalid(tup) \
  292. ( \
  293. ((tup)->t_infomask & (HEAP_XMIN_COMMITTED|HEAP_XMIN_INVALID)) == \
  294. HEAP_XMIN_INVALID \
  295. )
  296. #define HeapTupleHeaderXminFrozen(tup) \
  297. ( \
  298. ((tup)->t_infomask & (HEAP_XMIN_FROZEN)) == HEAP_XMIN_FROZEN \
  299. )
  300. #define HeapTupleHeaderSetXminCommitted(tup) \
  301. ( \
  302. AssertMacro(!HeapTupleHeaderXminInvalid(tup)), \
  303. ((tup)->t_infomask |= HEAP_XMIN_COMMITTED) \
  304. )
  305. #define HeapTupleHeaderSetXminInvalid(tup) \
  306. ( \
  307. AssertMacro(!HeapTupleHeaderXminCommitted(tup)), \
  308. ((tup)->t_infomask |= HEAP_XMIN_INVALID) \
  309. )
  310. #define HeapTupleHeaderSetXminFrozen(tup) \
  311. ( \
  312. AssertMacro(!HeapTupleHeaderXminInvalid(tup)), \
  313. ((tup)->t_infomask |= HEAP_XMIN_FROZEN) \
  314. )
  315. /*
  316. * HeapTupleHeaderGetRawXmax gets you the raw Xmax field. To find out the Xid
  317. * that updated a tuple, you might need to resolve the MultiXactId if certain
  318. * bits are set. HeapTupleHeaderGetUpdateXid checks those bits and takes care
  319. * to resolve the MultiXactId if necessary. This might involve multixact I/O,
  320. * so it should only be used if absolutely necessary.
  321. */
  322. #define HeapTupleHeaderGetUpdateXid(tup) \
  323. ( \
  324. (!((tup)->t_infomask & HEAP_XMAX_INVALID) && \
  325. ((tup)->t_infomask & HEAP_XMAX_IS_MULTI) && \
  326. !((tup)->t_infomask & HEAP_XMAX_LOCK_ONLY)) ? \
  327. HeapTupleGetUpdateXid(tup) \
  328. : \
  329. HeapTupleHeaderGetRawXmax(tup) \
  330. )
  331. #define HeapTupleHeaderGetRawXmax(tup) \
  332. ( \
  333. (tup)->t_choice.t_heap.t_xmax \
  334. )
  335. #define HeapTupleHeaderSetXmax(tup, xid) \
  336. ( \
  337. (tup)->t_choice.t_heap.t_xmax = (xid) \
  338. )
  339. /*
  340. * HeapTupleHeaderGetRawCommandId will give you what's in the header whether
  341. * it is useful or not. Most code should use HeapTupleHeaderGetCmin or
  342. * HeapTupleHeaderGetCmax instead, but note that those Assert that you can
  343. * get a legitimate result, ie you are in the originating transaction!
  344. */
  345. #define HeapTupleHeaderGetRawCommandId(tup) \
  346. ( \
  347. (tup)->t_choice.t_heap.t_field3.t_cid \
  348. )
  349. /* SetCmin is reasonably simple since we never need a combo CID */
  350. #define HeapTupleHeaderSetCmin(tup, cid) \
  351. do { \
  352. Assert(!((tup)->t_infomask & HEAP_MOVED)); \
  353. (tup)->t_choice.t_heap.t_field3.t_cid = (cid); \
  354. (tup)->t_infomask &= ~HEAP_COMBOCID; \
  355. } while (0)
  356. /* SetCmax must be used after HeapTupleHeaderAdjustCmax; see combocid.c */
  357. #define HeapTupleHeaderSetCmax(tup, cid, iscombo) \
  358. do { \
  359. Assert(!((tup)->t_infomask & HEAP_MOVED)); \
  360. (tup)->t_choice.t_heap.t_field3.t_cid = (cid); \
  361. if (iscombo) \
  362. (tup)->t_infomask |= HEAP_COMBOCID; \
  363. else \
  364. (tup)->t_infomask &= ~HEAP_COMBOCID; \
  365. } while (0)
  366. #define HeapTupleHeaderGetXvac(tup) \
  367. ( \
  368. ((tup)->t_infomask & HEAP_MOVED) ? \
  369. (tup)->t_choice.t_heap.t_field3.t_xvac \
  370. : \
  371. InvalidTransactionId \
  372. )
  373. #define HeapTupleHeaderSetXvac(tup, xid) \
  374. do { \
  375. Assert((tup)->t_infomask & HEAP_MOVED); \
  376. (tup)->t_choice.t_heap.t_field3.t_xvac = (xid); \
  377. } while (0)
  378. #define HeapTupleHeaderIsSpeculative(tup) \
  379. ( \
  380. (ItemPointerGetOffsetNumberNoCheck(&(tup)->t_ctid) == SpecTokenOffsetNumber) \
  381. )
  382. #define HeapTupleHeaderGetSpeculativeToken(tup) \
  383. ( \
  384. AssertMacro(HeapTupleHeaderIsSpeculative(tup)), \
  385. ItemPointerGetBlockNumber(&(tup)->t_ctid) \
  386. )
  387. #define HeapTupleHeaderSetSpeculativeToken(tup, token) \
  388. ( \
  389. ItemPointerSet(&(tup)->t_ctid, token, SpecTokenOffsetNumber) \
  390. )
  391. #define HeapTupleHeaderIndicatesMovedPartitions(tup) \
  392. ItemPointerIndicatesMovedPartitions(&(tup)->t_ctid)
  393. #define HeapTupleHeaderSetMovedPartitions(tup) \
  394. ItemPointerSetMovedPartitions(&(tup)->t_ctid)
  395. #define HeapTupleHeaderGetDatumLength(tup) \
  396. VARSIZE(tup)
  397. #define HeapTupleHeaderSetDatumLength(tup, len) \
  398. SET_VARSIZE(tup, len)
  399. #define HeapTupleHeaderGetTypeId(tup) \
  400. ( \
  401. (tup)->t_choice.t_datum.datum_typeid \
  402. )
  403. #define HeapTupleHeaderSetTypeId(tup, typeid) \
  404. ( \
  405. (tup)->t_choice.t_datum.datum_typeid = (typeid) \
  406. )
  407. #define HeapTupleHeaderGetTypMod(tup) \
  408. ( \
  409. (tup)->t_choice.t_datum.datum_typmod \
  410. )
  411. #define HeapTupleHeaderSetTypMod(tup, typmod) \
  412. ( \
  413. (tup)->t_choice.t_datum.datum_typmod = (typmod) \
  414. )
  415. /*
  416. * Note that we stop considering a tuple HOT-updated as soon as it is known
  417. * aborted or the would-be updating transaction is known aborted. For best
  418. * efficiency, check tuple visibility before using this macro, so that the
  419. * INVALID bits will be as up to date as possible.
  420. */
  421. #define HeapTupleHeaderIsHotUpdated(tup) \
  422. ( \
  423. ((tup)->t_infomask2 & HEAP_HOT_UPDATED) != 0 && \
  424. ((tup)->t_infomask & HEAP_XMAX_INVALID) == 0 && \
  425. !HeapTupleHeaderXminInvalid(tup) \
  426. )
  427. #define HeapTupleHeaderSetHotUpdated(tup) \
  428. ( \
  429. (tup)->t_infomask2 |= HEAP_HOT_UPDATED \
  430. )
  431. #define HeapTupleHeaderClearHotUpdated(tup) \
  432. ( \
  433. (tup)->t_infomask2 &= ~HEAP_HOT_UPDATED \
  434. )
  435. #define HeapTupleHeaderIsHeapOnly(tup) \
  436. ( \
  437. ((tup)->t_infomask2 & HEAP_ONLY_TUPLE) != 0 \
  438. )
  439. #define HeapTupleHeaderSetHeapOnly(tup) \
  440. ( \
  441. (tup)->t_infomask2 |= HEAP_ONLY_TUPLE \
  442. )
  443. #define HeapTupleHeaderClearHeapOnly(tup) \
  444. ( \
  445. (tup)->t_infomask2 &= ~HEAP_ONLY_TUPLE \
  446. )
  447. #define HeapTupleHeaderHasMatch(tup) \
  448. ( \
  449. ((tup)->t_infomask2 & HEAP_TUPLE_HAS_MATCH) != 0 \
  450. )
  451. #define HeapTupleHeaderSetMatch(tup) \
  452. ( \
  453. (tup)->t_infomask2 |= HEAP_TUPLE_HAS_MATCH \
  454. )
  455. #define HeapTupleHeaderClearMatch(tup) \
  456. ( \
  457. (tup)->t_infomask2 &= ~HEAP_TUPLE_HAS_MATCH \
  458. )
  459. #define HeapTupleHeaderGetNatts(tup) \
  460. ((tup)->t_infomask2 & HEAP_NATTS_MASK)
  461. #define HeapTupleHeaderSetNatts(tup, natts) \
  462. ( \
  463. (tup)->t_infomask2 = ((tup)->t_infomask2 & ~HEAP_NATTS_MASK) | (natts) \
  464. )
  465. #define HeapTupleHeaderHasExternal(tup) \
  466. (((tup)->t_infomask & HEAP_HASEXTERNAL) != 0)
  467. /*
  468. * BITMAPLEN(NATTS) -
  469. * Computes size of null bitmap given number of data columns.
  470. */
  471. #define BITMAPLEN(NATTS) (((int)(NATTS) + 7) / 8)
  472. /*
  473. * MaxHeapTupleSize is the maximum allowed size of a heap tuple, including
  474. * header and MAXALIGN alignment padding. Basically it's BLCKSZ minus the
  475. * other stuff that has to be on a disk page. Since heap pages use no
  476. * "special space", there's no deduction for that.
  477. *
  478. * NOTE: we allow for the ItemId that must point to the tuple, ensuring that
  479. * an otherwise-empty page can indeed hold a tuple of this size. Because
  480. * ItemIds and tuples have different alignment requirements, don't assume that
  481. * you can, say, fit 2 tuples of size MaxHeapTupleSize/2 on the same page.
  482. */
  483. #define MaxHeapTupleSize (BLCKSZ - MAXALIGN(SizeOfPageHeaderData + sizeof(ItemIdData)))
  484. #define MinHeapTupleSize MAXALIGN(SizeofHeapTupleHeader)
  485. /*
  486. * MaxHeapTuplesPerPage is an upper bound on the number of tuples that can
  487. * fit on one heap page. (Note that indexes could have more, because they
  488. * use a smaller tuple header.) We arrive at the divisor because each tuple
  489. * must be maxaligned, and it must have an associated line pointer.
  490. *
  491. * Note: with HOT, there could theoretically be more line pointers (not actual
  492. * tuples) than this on a heap page. However we constrain the number of line
  493. * pointers to this anyway, to avoid excessive line-pointer bloat and not
  494. * require increases in the size of work arrays.
  495. */
  496. #define MaxHeapTuplesPerPage \
  497. ((int) ((BLCKSZ - SizeOfPageHeaderData) / \
  498. (MAXALIGN(SizeofHeapTupleHeader) + sizeof(ItemIdData))))
  499. /*
  500. * MaxAttrSize is a somewhat arbitrary upper limit on the declared size of
  501. * data fields of char(n) and similar types. It need not have anything
  502. * directly to do with the *actual* upper limit of varlena values, which
  503. * is currently 1Gb (see TOAST structures in postgres.h). I've set it
  504. * at 10Mb which seems like a reasonable number --- tgl 8/6/00.
  505. */
  506. #define MaxAttrSize (10 * 1024 * 1024)
  507. /*
  508. * MinimalTuple is an alternative representation that is used for transient
  509. * tuples inside the executor, in places where transaction status information
  510. * is not required, the tuple rowtype is known, and shaving off a few bytes
  511. * is worthwhile because we need to store many tuples. The representation
  512. * is chosen so that tuple access routines can work with either full or
  513. * minimal tuples via a HeapTupleData pointer structure. The access routines
  514. * see no difference, except that they must not access the transaction status
  515. * or t_ctid fields because those aren't there.
  516. *
  517. * For the most part, MinimalTuples should be accessed via TupleTableSlot
  518. * routines. These routines will prevent access to the "system columns"
  519. * and thereby prevent accidental use of the nonexistent fields.
  520. *
  521. * MinimalTupleData contains a length word, some padding, and fields matching
  522. * HeapTupleHeaderData beginning with t_infomask2. The padding is chosen so
  523. * that offsetof(t_infomask2) is the same modulo MAXIMUM_ALIGNOF in both
  524. * structs. This makes data alignment rules equivalent in both cases.
  525. *
  526. * When a minimal tuple is accessed via a HeapTupleData pointer, t_data is
  527. * set to point MINIMAL_TUPLE_OFFSET bytes before the actual start of the
  528. * minimal tuple --- that is, where a full tuple matching the minimal tuple's
  529. * data would start. This trick is what makes the structs seem equivalent.
  530. *
  531. * Note that t_hoff is computed the same as in a full tuple, hence it includes
  532. * the MINIMAL_TUPLE_OFFSET distance. t_len does not include that, however.
  533. *
  534. * MINIMAL_TUPLE_DATA_OFFSET is the offset to the first useful (non-pad) data
  535. * other than the length word. tuplesort.c and tuplestore.c use this to avoid
  536. * writing the padding to disk.
  537. */
  538. #define MINIMAL_TUPLE_OFFSET \
  539. ((offsetof(HeapTupleHeaderData, t_infomask2) - sizeof(uint32)) / MAXIMUM_ALIGNOF * MAXIMUM_ALIGNOF)
  540. #define MINIMAL_TUPLE_PADDING \
  541. ((offsetof(HeapTupleHeaderData, t_infomask2) - sizeof(uint32)) % MAXIMUM_ALIGNOF)
  542. #define MINIMAL_TUPLE_DATA_OFFSET \
  543. offsetof(MinimalTupleData, t_infomask2)
  544. struct MinimalTupleData
  545. {
  546. uint32 t_len; /* actual length of minimal tuple */
  547. char mt_padding[MINIMAL_TUPLE_PADDING];
  548. /* Fields below here must match HeapTupleHeaderData! */
  549. uint16 t_infomask2; /* number of attributes + various flags */
  550. uint16 t_infomask; /* various flag bits, see below */
  551. uint8 t_hoff; /* sizeof header incl. bitmap, padding */
  552. /* ^ - 23 bytes - ^ */
  553. bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]; /* bitmap of NULLs */
  554. /* MORE DATA FOLLOWS AT END OF STRUCT */
  555. };
  556. /* typedef appears in htup.h */
  557. #define SizeofMinimalTupleHeader offsetof(MinimalTupleData, t_bits)
  558. /*
  559. * GETSTRUCT - given a HeapTuple pointer, return address of the user data
  560. */
  561. #define GETSTRUCT(TUP) ((char *) ((TUP)->t_data) + (TUP)->t_data->t_hoff)
  562. /*
  563. * Accessor macros to be used with HeapTuple pointers.
  564. */
  565. #define HeapTupleHasNulls(tuple) \
  566. (((tuple)->t_data->t_infomask & HEAP_HASNULL) != 0)
  567. #define HeapTupleNoNulls(tuple) \
  568. (!((tuple)->t_data->t_infomask & HEAP_HASNULL))
  569. #define HeapTupleHasVarWidth(tuple) \
  570. (((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH) != 0)
  571. #define HeapTupleAllFixed(tuple) \
  572. (!((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH))
  573. #define HeapTupleHasExternal(tuple) \
  574. (((tuple)->t_data->t_infomask & HEAP_HASEXTERNAL) != 0)
  575. #define HeapTupleIsHotUpdated(tuple) \
  576. HeapTupleHeaderIsHotUpdated((tuple)->t_data)
  577. #define HeapTupleSetHotUpdated(tuple) \
  578. HeapTupleHeaderSetHotUpdated((tuple)->t_data)
  579. #define HeapTupleClearHotUpdated(tuple) \
  580. HeapTupleHeaderClearHotUpdated((tuple)->t_data)
  581. #define HeapTupleIsHeapOnly(tuple) \
  582. HeapTupleHeaderIsHeapOnly((tuple)->t_data)
  583. #define HeapTupleSetHeapOnly(tuple) \
  584. HeapTupleHeaderSetHeapOnly((tuple)->t_data)
  585. #define HeapTupleClearHeapOnly(tuple) \
  586. HeapTupleHeaderClearHeapOnly((tuple)->t_data)
  587. /* prototypes for functions in common/heaptuple.c */
  588. extern Size heap_compute_data_size(TupleDesc tupleDesc,
  589. Datum *values, bool *isnull);
  590. extern void heap_fill_tuple(TupleDesc tupleDesc,
  591. Datum *values, bool *isnull,
  592. char *data, Size data_size,
  593. uint16 *infomask, bits8 *bit);
  594. extern bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc);
  595. extern Datum nocachegetattr(HeapTuple tup, int attnum,
  596. TupleDesc att);
  597. extern Datum heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
  598. bool *isnull);
  599. extern Datum getmissingattr(TupleDesc tupleDesc,
  600. int attnum, bool *isnull);
  601. extern HeapTuple heap_copytuple(HeapTuple tuple);
  602. extern void heap_copytuple_with_tuple(HeapTuple src, HeapTuple dest);
  603. extern Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc);
  604. extern HeapTuple heap_form_tuple(TupleDesc tupleDescriptor,
  605. Datum *values, bool *isnull);
  606. extern HeapTuple heap_modify_tuple(HeapTuple tuple,
  607. TupleDesc tupleDesc,
  608. Datum *replValues,
  609. bool *replIsnull,
  610. bool *doReplace);
  611. extern HeapTuple heap_modify_tuple_by_cols(HeapTuple tuple,
  612. TupleDesc tupleDesc,
  613. int nCols,
  614. int *replCols,
  615. Datum *replValues,
  616. bool *replIsnull);
  617. extern void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc,
  618. Datum *values, bool *isnull);
  619. extern void heap_freetuple(HeapTuple htup);
  620. extern MinimalTuple heap_form_minimal_tuple(TupleDesc tupleDescriptor,
  621. Datum *values, bool *isnull);
  622. extern void heap_free_minimal_tuple(MinimalTuple mtup);
  623. extern MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup);
  624. extern HeapTuple heap_tuple_from_minimal_tuple(MinimalTuple mtup);
  625. extern MinimalTuple minimal_tuple_from_heap_tuple(HeapTuple htup);
  626. extern size_t varsize_any(void *p);
  627. extern HeapTuple heap_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc);
  628. extern MinimalTuple minimal_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc);
  629. #ifndef FRONTEND
  630. /*
  631. * fastgetattr
  632. * Fetch a user attribute's value as a Datum (might be either a
  633. * value, or a pointer into the data area of the tuple).
  634. *
  635. * This must not be used when a system attribute might be requested.
  636. * Furthermore, the passed attnum MUST be valid. Use heap_getattr()
  637. * instead, if in doubt.
  638. *
  639. * This gets called many times, so we macro the cacheable and NULL
  640. * lookups, and call nocachegetattr() for the rest.
  641. */
  642. static inline Datum
  643. fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
  644. {
  645. Assert(attnum > 0);
  646. *isnull = false;
  647. if (HeapTupleNoNulls(tup))
  648. {
  649. Form_pg_attribute att;
  650. att = TupleDescAttr(tupleDesc, attnum - 1);
  651. if (att->attcacheoff >= 0)
  652. return fetchatt(att, (char *) tup->t_data + tup->t_data->t_hoff +
  653. att->attcacheoff);
  654. else
  655. return nocachegetattr(tup, attnum, tupleDesc);
  656. }
  657. else
  658. {
  659. if (att_isnull(attnum - 1, tup->t_data->t_bits))
  660. {
  661. *isnull = true;
  662. return (Datum) NULL;
  663. }
  664. else
  665. return nocachegetattr(tup, attnum, tupleDesc);
  666. }
  667. }
  668. /*
  669. * heap_getattr
  670. * Extract an attribute of a heap tuple and return it as a Datum.
  671. * This works for either system or user attributes. The given attnum
  672. * is properly range-checked.
  673. *
  674. * If the field in question has a NULL value, we return a zero Datum
  675. * and set *isnull == true. Otherwise, we set *isnull == false.
  676. *
  677. * <tup> is the pointer to the heap tuple. <attnum> is the attribute
  678. * number of the column (field) caller wants. <tupleDesc> is a
  679. * pointer to the structure describing the row and all its fields.
  680. *
  681. */
  682. static inline Datum
  683. heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
  684. {
  685. if (attnum > 0)
  686. {
  687. if (attnum > (int) HeapTupleHeaderGetNatts(tup->t_data))
  688. return getmissingattr(tupleDesc, attnum, isnull);
  689. else
  690. return fastgetattr(tup, attnum, tupleDesc, isnull);
  691. }
  692. else
  693. return heap_getsysattr(tup, attnum, tupleDesc, isnull);
  694. }
  695. #endif /* FRONTEND */
  696. #endif /* HTUP_DETAILS_H */