tuptable.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*-------------------------------------------------------------------------
  2. *
  3. * tuptable.h
  4. * tuple table support stuff
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/executor/tuptable.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef TUPTABLE_H
  15. #define TUPTABLE_H
  16. #include "access/htup.h"
  17. #include "access/tupdesc.h"
  18. #include "storage/buf.h"
  19. /*----------
  20. * The executor stores tuples in a "tuple table" which is a List of
  21. * independent TupleTableSlots. There are several cases we need to handle:
  22. * 1. physical tuple in a disk buffer page
  23. * 2. physical tuple constructed in palloc'ed memory
  24. * 3. "minimal" physical tuple constructed in palloc'ed memory
  25. * 4. "virtual" tuple consisting of Datum/isnull arrays
  26. *
  27. * The first two cases are similar in that they both deal with "materialized"
  28. * tuples, but resource management is different. For a tuple in a disk page
  29. * we need to hold a pin on the buffer until the TupleTableSlot's reference
  30. * to the tuple is dropped; while for a palloc'd tuple we usually want the
  31. * tuple pfree'd when the TupleTableSlot's reference is dropped.
  32. *
  33. * A "minimal" tuple is handled similarly to a palloc'd regular tuple.
  34. * At present, minimal tuples never are stored in buffers, so there is no
  35. * parallel to case 1. Note that a minimal tuple has no "system columns".
  36. * (Actually, it could have an OID, but we have no need to access the OID.)
  37. *
  38. * A "virtual" tuple is an optimization used to minimize physical data
  39. * copying in a nest of plan nodes. Any pass-by-reference Datums in the
  40. * tuple point to storage that is not directly associated with the
  41. * TupleTableSlot; generally they will point to part of a tuple stored in
  42. * a lower plan node's output TupleTableSlot, or to a function result
  43. * constructed in a plan node's per-tuple econtext. It is the responsibility
  44. * of the generating plan node to be sure these resources are not released
  45. * for as long as the virtual tuple needs to be valid. We only use virtual
  46. * tuples in the result slots of plan nodes --- tuples to be copied anywhere
  47. * else need to be "materialized" into physical tuples. Note also that a
  48. * virtual tuple does not have any "system columns".
  49. *
  50. * It is also possible for a TupleTableSlot to hold both physical and minimal
  51. * copies of a tuple. This is done when the slot is requested to provide
  52. * the format other than the one it currently holds. (Originally we attempted
  53. * to handle such requests by replacing one format with the other, but that
  54. * had the fatal defect of invalidating any pass-by-reference Datums pointing
  55. * into the existing slot contents.) Both copies must contain identical data
  56. * payloads when this is the case.
  57. *
  58. * The Datum/isnull arrays of a TupleTableSlot serve double duty. When the
  59. * slot contains a virtual tuple, they are the authoritative data. When the
  60. * slot contains a physical tuple, the arrays contain data extracted from
  61. * the tuple. (In this state, any pass-by-reference Datums point into
  62. * the physical tuple.) The extracted information is built "lazily",
  63. * ie, only as needed. This serves to avoid repeated extraction of data
  64. * from the physical tuple.
  65. *
  66. * A TupleTableSlot can also be "empty", holding no valid data. This is
  67. * the only valid state for a freshly-created slot that has not yet had a
  68. * tuple descriptor assigned to it. In this state, tts_isempty must be
  69. * TRUE, tts_shouldFree FALSE, tts_tuple NULL, tts_buffer InvalidBuffer,
  70. * and tts_nvalid zero.
  71. *
  72. * The tupleDescriptor is simply referenced, not copied, by the TupleTableSlot
  73. * code. The caller of ExecSetSlotDescriptor() is responsible for providing
  74. * a descriptor that will live as long as the slot does. (Typically, both
  75. * slots and descriptors are in per-query memory and are freed by memory
  76. * context deallocation at query end; so it's not worth providing any extra
  77. * mechanism to do more. However, the slot will increment the tupdesc
  78. * reference count if a reference-counted tupdesc is supplied.)
  79. *
  80. * When tts_shouldFree is true, the physical tuple is "owned" by the slot
  81. * and should be freed when the slot's reference to the tuple is dropped.
  82. *
  83. * If tts_buffer is not InvalidBuffer, then the slot is holding a pin
  84. * on the indicated buffer page; drop the pin when we release the
  85. * slot's reference to that buffer. (tts_shouldFree should always be
  86. * false in such a case, since presumably tts_tuple is pointing at the
  87. * buffer page.)
  88. *
  89. * tts_nvalid indicates the number of valid columns in the tts_values/isnull
  90. * arrays. When the slot is holding a "virtual" tuple this must be equal
  91. * to the descriptor's natts. When the slot is holding a physical tuple
  92. * this is equal to the number of columns we have extracted (we always
  93. * extract columns from left to right, so there are no holes).
  94. *
  95. * tts_values/tts_isnull are allocated when a descriptor is assigned to the
  96. * slot; they are of length equal to the descriptor's natts.
  97. *
  98. * tts_mintuple must always be NULL if the slot does not hold a "minimal"
  99. * tuple. When it does, tts_mintuple points to the actual MinimalTupleData
  100. * object (the thing to be pfree'd if tts_shouldFreeMin is true). If the slot
  101. * has only a minimal and not also a regular physical tuple, then tts_tuple
  102. * points at tts_minhdr and the fields of that struct are set correctly
  103. * for access to the minimal tuple; in particular, tts_minhdr.t_data points
  104. * MINIMAL_TUPLE_OFFSET bytes before tts_mintuple. This allows column
  105. * extraction to treat the case identically to regular physical tuples.
  106. *
  107. * tts_slow/tts_off are saved state for slot_deform_tuple, and should not
  108. * be touched by any other code.
  109. *----------
  110. */
  111. typedef struct TupleTableSlot
  112. {
  113. NodeTag type;
  114. bool tts_isempty; /* true = slot is empty */
  115. bool tts_shouldFree; /* should pfree tts_tuple? */
  116. bool tts_shouldFreeMin; /* should pfree tts_mintuple? */
  117. bool tts_slow; /* saved state for slot_deform_tuple */
  118. HeapTuple tts_tuple; /* physical tuple, or NULL if virtual */
  119. TupleDesc tts_tupleDescriptor; /* slot's tuple descriptor */
  120. MemoryContext tts_mcxt; /* slot itself is in this context */
  121. Buffer tts_buffer; /* tuple's buffer, or InvalidBuffer */
  122. int tts_nvalid; /* # of valid values in tts_values */
  123. Datum *tts_values; /* current per-attribute values */
  124. bool *tts_isnull; /* current per-attribute isnull flags */
  125. MinimalTuple tts_mintuple; /* minimal tuple, or NULL if none */
  126. HeapTupleData tts_minhdr; /* workspace for minimal-tuple-only case */
  127. long tts_off; /* saved state for slot_deform_tuple */
  128. } TupleTableSlot;
  129. #define TTS_HAS_PHYSICAL_TUPLE(slot) \
  130. ((slot)->tts_tuple != NULL && (slot)->tts_tuple != &((slot)->tts_minhdr))
  131. /*
  132. * TupIsNull -- is a TupleTableSlot empty?
  133. */
  134. #define TupIsNull(slot) \
  135. ((slot) == NULL || (slot)->tts_isempty)
  136. /* in executor/execTuples.c */
  137. extern TupleTableSlot *MakeTupleTableSlot(void);
  138. extern TupleTableSlot *ExecAllocTableSlot(List **tupleTable);
  139. extern void ExecResetTupleTable(List *tupleTable, bool shouldFree);
  140. extern TupleTableSlot *MakeSingleTupleTableSlot(TupleDesc tupdesc);
  141. extern void ExecDropSingleTupleTableSlot(TupleTableSlot *slot);
  142. extern void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc);
  143. extern TupleTableSlot *ExecStoreTuple(HeapTuple tuple,
  144. TupleTableSlot *slot,
  145. Buffer buffer,
  146. bool shouldFree);
  147. extern TupleTableSlot *ExecStoreMinimalTuple(MinimalTuple mtup,
  148. TupleTableSlot *slot,
  149. bool shouldFree);
  150. extern TupleTableSlot *ExecClearTuple(TupleTableSlot *slot);
  151. extern TupleTableSlot *ExecStoreVirtualTuple(TupleTableSlot *slot);
  152. extern TupleTableSlot *ExecStoreAllNullTuple(TupleTableSlot *slot);
  153. extern HeapTuple ExecCopySlotTuple(TupleTableSlot *slot);
  154. extern MinimalTuple ExecCopySlotMinimalTuple(TupleTableSlot *slot);
  155. extern HeapTuple ExecFetchSlotTuple(TupleTableSlot *slot);
  156. extern MinimalTuple ExecFetchSlotMinimalTuple(TupleTableSlot *slot);
  157. extern Datum ExecFetchSlotTupleDatum(TupleTableSlot *slot);
  158. extern HeapTuple ExecMaterializeSlot(TupleTableSlot *slot);
  159. extern TupleTableSlot *ExecCopySlot(TupleTableSlot *dstslot,
  160. TupleTableSlot *srcslot);
  161. /* in access/common/heaptuple.c */
  162. extern Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull);
  163. extern void slot_getallattrs(TupleTableSlot *slot);
  164. extern void slot_getsomeattrs(TupleTableSlot *slot, int attnum);
  165. extern bool slot_attisnull(TupleTableSlot *slot, int attnum);
  166. #endif /* TUPTABLE_H */