Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * execTuples.c
4 : * Routines dealing with TupleTableSlots. These are used for resource
5 : * management associated with tuples (eg, releasing buffer pins for
6 : * tuples in disk buffers, or freeing the memory occupied by transient
7 : * tuples). Slots also provide access abstraction that lets us implement
8 : * "virtual" tuples to reduce data-copying overhead.
9 : *
10 : * Routines dealing with the type information for tuples. Currently,
11 : * the type information for a tuple is an array of FormData_pg_attribute.
12 : * This information is needed by routines manipulating tuples
13 : * (getattribute, formtuple, etc.).
14 : *
15 : *
16 : * EXAMPLE OF HOW TABLE ROUTINES WORK
17 : * Suppose we have a query such as SELECT emp.name FROM emp and we have
18 : * a single SeqScan node in the query plan.
19 : *
20 : * At ExecutorStart()
21 : * ----------------
22 : *
23 : * - ExecInitSeqScan() calls ExecInitScanTupleSlot() to construct a
24 : * TupleTableSlots for the tuples returned by the access method, and
25 : * ExecInitResultTypeTL() to define the node's return
26 : * type. ExecAssignScanProjectionInfo() will, if necessary, create
27 : * another TupleTableSlot for the tuples resulting from performing
28 : * target list projections.
29 : *
30 : * During ExecutorRun()
31 : * ----------------
32 : * - SeqNext() calls ExecStoreBufferHeapTuple() to place the tuple
33 : * returned by the access method into the scan tuple slot.
34 : *
35 : * - ExecSeqScan() (via ExecScan), if necessary, calls ExecProject(),
36 : * putting the result of the projection in the result tuple slot. If
37 : * not necessary, it directly returns the slot returned by SeqNext().
38 : *
39 : * - ExecutePlan() calls the output function.
40 : *
41 : * The important thing to watch in the executor code is how pointers
42 : * to the slots containing tuples are passed instead of the tuples
43 : * themselves. This facilitates the communication of related information
44 : * (such as whether or not a tuple should be pfreed, what buffer contains
45 : * this tuple, the tuple's tuple descriptor, etc). It also allows us
46 : * to avoid physically constructing projection tuples in many cases.
47 : *
48 : *
49 : * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
50 : * Portions Copyright (c) 1994, Regents of the University of California
51 : *
52 : *
53 : * IDENTIFICATION
54 : * src/backend/executor/execTuples.c
55 : *
56 : *-------------------------------------------------------------------------
57 : */
58 : #include "postgres.h"
59 :
60 : #include "access/heaptoast.h"
61 : #include "access/htup_details.h"
62 : #include "access/tupdesc_details.h"
63 : #include "catalog/pg_type.h"
64 : #include "funcapi.h"
65 : #include "nodes/nodeFuncs.h"
66 : #include "storage/bufmgr.h"
67 : #include "utils/builtins.h"
68 : #include "utils/expandeddatum.h"
69 : #include "utils/lsyscache.h"
70 : #include "utils/typcache.h"
71 :
72 : static TupleDesc ExecTypeFromTLInternal(List *targetList,
73 : bool skipjunk);
74 : static pg_attribute_always_inline void slot_deform_heap_tuple(TupleTableSlot *slot, HeapTuple tuple, uint32 *offp,
75 : int natts);
76 : static inline void tts_buffer_heap_store_tuple(TupleTableSlot *slot,
77 : HeapTuple tuple,
78 : Buffer buffer,
79 : bool transfer_pin);
80 : static void tts_heap_store_tuple(TupleTableSlot *slot, HeapTuple tuple, bool shouldFree);
81 :
82 :
83 : const TupleTableSlotOps TTSOpsVirtual;
84 : const TupleTableSlotOps TTSOpsHeapTuple;
85 : const TupleTableSlotOps TTSOpsMinimalTuple;
86 : const TupleTableSlotOps TTSOpsBufferHeapTuple;
87 :
88 :
89 : /*
90 : * TupleTableSlotOps implementations.
91 : */
92 :
93 : /*
94 : * TupleTableSlotOps implementation for VirtualTupleTableSlot.
95 : */
96 : static void
97 1178826 : tts_virtual_init(TupleTableSlot *slot)
98 : {
99 1178826 : }
100 :
101 : static void
102 1155992 : tts_virtual_release(TupleTableSlot *slot)
103 : {
104 1155992 : }
105 :
106 : static void
107 77944732 : tts_virtual_clear(TupleTableSlot *slot)
108 : {
109 77944732 : if (unlikely(TTS_SHOULDFREE(slot)))
110 : {
111 1745370 : VirtualTupleTableSlot *vslot = (VirtualTupleTableSlot *) slot;
112 :
113 1745370 : pfree(vslot->data);
114 1745370 : vslot->data = NULL;
115 :
116 1745370 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
117 : }
118 :
119 77944732 : slot->tts_nvalid = 0;
120 77944732 : slot->tts_flags |= TTS_FLAG_EMPTY;
121 77944732 : ItemPointerSetInvalid(&slot->tts_tid);
122 77944732 : }
123 :
124 : /*
125 : * VirtualTupleTableSlots always have fully populated tts_values and
126 : * tts_isnull arrays. So this function should never be called.
127 : */
128 : static void
129 0 : tts_virtual_getsomeattrs(TupleTableSlot *slot, int natts)
130 : {
131 0 : elog(ERROR, "getsomeattrs is not required to be called on a virtual tuple table slot");
132 : }
133 :
134 : /*
135 : * VirtualTupleTableSlots never provide system attributes (except those
136 : * handled generically, such as tableoid). We generally shouldn't get
137 : * here, but provide a user-friendly message if we do.
138 : */
139 : static Datum
140 12 : tts_virtual_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
141 : {
142 : Assert(!TTS_EMPTY(slot));
143 :
144 12 : ereport(ERROR,
145 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
146 : errmsg("cannot retrieve a system column in this context")));
147 :
148 : return 0; /* silence compiler warnings */
149 : }
150 :
151 : /*
152 : * To materialize a virtual slot all the datums that aren't passed by value
153 : * have to be copied into the slot's memory context. To do so, compute the
154 : * required size, and allocate enough memory to store all attributes. That's
155 : * good for cache hit ratio, but more importantly requires only memory
156 : * allocation/deallocation.
157 : */
158 : static void
159 4277742 : tts_virtual_materialize(TupleTableSlot *slot)
160 : {
161 4277742 : VirtualTupleTableSlot *vslot = (VirtualTupleTableSlot *) slot;
162 4277742 : TupleDesc desc = slot->tts_tupleDescriptor;
163 4277742 : Size sz = 0;
164 : char *data;
165 :
166 : /* already materialized */
167 4277742 : if (TTS_SHOULDFREE(slot))
168 383806 : return;
169 :
170 : /* compute size of memory required */
171 12091062 : for (int natt = 0; natt < desc->natts; natt++)
172 : {
173 8197126 : Form_pg_attribute att = TupleDescAttr(desc, natt);
174 : Datum val;
175 :
176 8197126 : if (att->attbyval || slot->tts_isnull[natt])
177 6370712 : continue;
178 :
179 1826414 : val = slot->tts_values[natt];
180 :
181 1826414 : if (att->attlen == -1 &&
182 1235970 : VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(val)))
183 : {
184 : /*
185 : * We want to flatten the expanded value so that the materialized
186 : * slot doesn't depend on it.
187 : */
188 0 : sz = att_align_nominal(sz, att->attalign);
189 0 : sz += EOH_get_flat_size(DatumGetEOHP(val));
190 : }
191 : else
192 : {
193 1826414 : sz = att_align_nominal(sz, att->attalign);
194 1826414 : sz = att_addlength_datum(sz, att->attlen, val);
195 : }
196 : }
197 :
198 : /* all data is byval */
199 3893936 : if (sz == 0)
200 2148488 : return;
201 :
202 : /* allocate memory */
203 1745448 : vslot->data = data = MemoryContextAlloc(slot->tts_mcxt, sz);
204 1745448 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
205 :
206 : /* and copy all attributes into the pre-allocated space */
207 6695598 : for (int natt = 0; natt < desc->natts; natt++)
208 : {
209 4950150 : Form_pg_attribute att = TupleDescAttr(desc, natt);
210 : Datum val;
211 :
212 4950150 : if (att->attbyval || slot->tts_isnull[natt])
213 3123736 : continue;
214 :
215 1826414 : val = slot->tts_values[natt];
216 :
217 1826414 : if (att->attlen == -1 &&
218 1235970 : VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(val)))
219 0 : {
220 : Size data_length;
221 :
222 : /*
223 : * We want to flatten the expanded value so that the materialized
224 : * slot doesn't depend on it.
225 : */
226 0 : ExpandedObjectHeader *eoh = DatumGetEOHP(val);
227 :
228 0 : data = (char *) att_align_nominal(data,
229 : att->attalign);
230 0 : data_length = EOH_get_flat_size(eoh);
231 0 : EOH_flatten_into(eoh, data, data_length);
232 :
233 0 : slot->tts_values[natt] = PointerGetDatum(data);
234 0 : data += data_length;
235 : }
236 : else
237 : {
238 1826414 : Size data_length = 0;
239 :
240 1826414 : data = (char *) att_align_nominal(data, att->attalign);
241 1826414 : data_length = att_addlength_datum(data_length, att->attlen, val);
242 :
243 1826414 : memcpy(data, DatumGetPointer(val), data_length);
244 :
245 1826414 : slot->tts_values[natt] = PointerGetDatum(data);
246 1826414 : data += data_length;
247 : }
248 : }
249 : }
250 :
251 : static void
252 141092 : tts_virtual_copyslot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
253 : {
254 141092 : TupleDesc srcdesc = srcslot->tts_tupleDescriptor;
255 :
256 : Assert(srcdesc->natts <= dstslot->tts_tupleDescriptor->natts);
257 :
258 141092 : tts_virtual_clear(dstslot);
259 :
260 141092 : slot_getallattrs(srcslot);
261 :
262 290788 : for (int natt = 0; natt < srcdesc->natts; natt++)
263 : {
264 149696 : dstslot->tts_values[natt] = srcslot->tts_values[natt];
265 149696 : dstslot->tts_isnull[natt] = srcslot->tts_isnull[natt];
266 : }
267 :
268 141092 : dstslot->tts_nvalid = srcdesc->natts;
269 141092 : dstslot->tts_flags &= ~TTS_FLAG_EMPTY;
270 :
271 : /* make sure storage doesn't depend on external memory */
272 141092 : tts_virtual_materialize(dstslot);
273 141092 : }
274 :
275 : static HeapTuple
276 14110700 : tts_virtual_copy_heap_tuple(TupleTableSlot *slot)
277 : {
278 : Assert(!TTS_EMPTY(slot));
279 :
280 14110700 : return heap_form_tuple(slot->tts_tupleDescriptor,
281 : slot->tts_values,
282 : slot->tts_isnull);
283 : }
284 :
285 : static MinimalTuple
286 25054784 : tts_virtual_copy_minimal_tuple(TupleTableSlot *slot)
287 : {
288 : Assert(!TTS_EMPTY(slot));
289 :
290 25054784 : return heap_form_minimal_tuple(slot->tts_tupleDescriptor,
291 : slot->tts_values,
292 : slot->tts_isnull);
293 : }
294 :
295 :
296 : /*
297 : * TupleTableSlotOps implementation for HeapTupleTableSlot.
298 : */
299 :
300 : static void
301 9701340 : tts_heap_init(TupleTableSlot *slot)
302 : {
303 9701340 : }
304 :
305 : static void
306 9700534 : tts_heap_release(TupleTableSlot *slot)
307 : {
308 9700534 : }
309 :
310 : static void
311 21729192 : tts_heap_clear(TupleTableSlot *slot)
312 : {
313 21729192 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
314 :
315 : /* Free the memory for the heap tuple if it's allowed. */
316 21729192 : if (TTS_SHOULDFREE(slot))
317 : {
318 4591610 : heap_freetuple(hslot->tuple);
319 4591610 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
320 : }
321 :
322 21729192 : slot->tts_nvalid = 0;
323 21729192 : slot->tts_flags |= TTS_FLAG_EMPTY;
324 21729192 : ItemPointerSetInvalid(&slot->tts_tid);
325 21729192 : hslot->off = 0;
326 21729192 : hslot->tuple = NULL;
327 21729192 : }
328 :
329 : static void
330 21946494 : tts_heap_getsomeattrs(TupleTableSlot *slot, int natts)
331 : {
332 21946494 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
333 :
334 : Assert(!TTS_EMPTY(slot));
335 :
336 21946494 : slot_deform_heap_tuple(slot, hslot->tuple, &hslot->off, natts);
337 21946494 : }
338 :
339 : static Datum
340 0 : tts_heap_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
341 : {
342 0 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
343 :
344 : Assert(!TTS_EMPTY(slot));
345 :
346 : /*
347 : * In some code paths it's possible to get here with a non-materialized
348 : * slot, in which case we can't retrieve system columns.
349 : */
350 0 : if (!hslot->tuple)
351 0 : ereport(ERROR,
352 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
353 : errmsg("cannot retrieve a system column in this context")));
354 :
355 0 : return heap_getsysattr(hslot->tuple, attnum,
356 : slot->tts_tupleDescriptor, isnull);
357 : }
358 :
359 : static void
360 8520552 : tts_heap_materialize(TupleTableSlot *slot)
361 : {
362 8520552 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
363 : MemoryContext oldContext;
364 :
365 : Assert(!TTS_EMPTY(slot));
366 :
367 : /* If slot has its tuple already materialized, nothing to do. */
368 8520552 : if (TTS_SHOULDFREE(slot))
369 4331024 : return;
370 :
371 4189528 : oldContext = MemoryContextSwitchTo(slot->tts_mcxt);
372 :
373 : /*
374 : * Have to deform from scratch, otherwise tts_values[] entries could point
375 : * into the non-materialized tuple (which might be gone when accessed).
376 : */
377 4189528 : slot->tts_nvalid = 0;
378 4189528 : hslot->off = 0;
379 :
380 4189528 : if (!hslot->tuple)
381 4189514 : hslot->tuple = heap_form_tuple(slot->tts_tupleDescriptor,
382 : slot->tts_values,
383 : slot->tts_isnull);
384 : else
385 : {
386 : /*
387 : * The tuple contained in this slot is not allocated in the memory
388 : * context of the given slot (else it would have TTS_FLAG_SHOULDFREE
389 : * set). Copy the tuple into the given slot's memory context.
390 : */
391 14 : hslot->tuple = heap_copytuple(hslot->tuple);
392 : }
393 :
394 4189528 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
395 :
396 4189528 : MemoryContextSwitchTo(oldContext);
397 : }
398 :
399 : static void
400 281796 : tts_heap_copyslot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
401 : {
402 : HeapTuple tuple;
403 : MemoryContext oldcontext;
404 :
405 281796 : oldcontext = MemoryContextSwitchTo(dstslot->tts_mcxt);
406 281796 : tuple = ExecCopySlotHeapTuple(srcslot);
407 281796 : MemoryContextSwitchTo(oldcontext);
408 :
409 281796 : ExecStoreHeapTuple(tuple, dstslot, true);
410 281796 : }
411 :
412 : static HeapTuple
413 8378594 : tts_heap_get_heap_tuple(TupleTableSlot *slot)
414 : {
415 8378594 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
416 :
417 : Assert(!TTS_EMPTY(slot));
418 8378594 : if (!hslot->tuple)
419 0 : tts_heap_materialize(slot);
420 :
421 8378594 : return hslot->tuple;
422 : }
423 :
424 : static HeapTuple
425 140686 : tts_heap_copy_heap_tuple(TupleTableSlot *slot)
426 : {
427 140686 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
428 :
429 : Assert(!TTS_EMPTY(slot));
430 140686 : if (!hslot->tuple)
431 0 : tts_heap_materialize(slot);
432 :
433 140686 : return heap_copytuple(hslot->tuple);
434 : }
435 :
436 : static MinimalTuple
437 5422 : tts_heap_copy_minimal_tuple(TupleTableSlot *slot)
438 : {
439 5422 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
440 :
441 5422 : if (!hslot->tuple)
442 38 : tts_heap_materialize(slot);
443 :
444 5422 : return minimal_tuple_from_heap_tuple(hslot->tuple);
445 : }
446 :
447 : static void
448 7694066 : tts_heap_store_tuple(TupleTableSlot *slot, HeapTuple tuple, bool shouldFree)
449 : {
450 7694066 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
451 :
452 7694066 : tts_heap_clear(slot);
453 :
454 7694066 : slot->tts_nvalid = 0;
455 7694066 : hslot->tuple = tuple;
456 7694066 : hslot->off = 0;
457 7694066 : slot->tts_flags &= ~(TTS_FLAG_EMPTY | TTS_FLAG_SHOULDFREE);
458 7694066 : slot->tts_tid = tuple->t_self;
459 :
460 7694066 : if (shouldFree)
461 402100 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
462 7694066 : }
463 :
464 :
465 : /*
466 : * TupleTableSlotOps implementation for MinimalTupleTableSlot.
467 : */
468 :
469 : static void
470 430026 : tts_minimal_init(TupleTableSlot *slot)
471 : {
472 430026 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
473 :
474 : /*
475 : * Initialize the heap tuple pointer to access attributes of the minimal
476 : * tuple contained in the slot as if its a heap tuple.
477 : */
478 430026 : mslot->tuple = &mslot->minhdr;
479 430026 : }
480 :
481 : static void
482 395524 : tts_minimal_release(TupleTableSlot *slot)
483 : {
484 395524 : }
485 :
486 : static void
487 65153324 : tts_minimal_clear(TupleTableSlot *slot)
488 : {
489 65153324 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
490 :
491 65153324 : if (TTS_SHOULDFREE(slot))
492 : {
493 13503072 : heap_free_minimal_tuple(mslot->mintuple);
494 13503072 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
495 : }
496 :
497 65153324 : slot->tts_nvalid = 0;
498 65153324 : slot->tts_flags |= TTS_FLAG_EMPTY;
499 65153324 : ItemPointerSetInvalid(&slot->tts_tid);
500 65153324 : mslot->off = 0;
501 65153324 : mslot->mintuple = NULL;
502 65153324 : }
503 :
504 : static void
505 44414044 : tts_minimal_getsomeattrs(TupleTableSlot *slot, int natts)
506 : {
507 44414044 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
508 :
509 : Assert(!TTS_EMPTY(slot));
510 :
511 44414044 : slot_deform_heap_tuple(slot, mslot->tuple, &mslot->off, natts);
512 44414044 : }
513 :
514 : static Datum
515 0 : tts_minimal_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
516 : {
517 : Assert(!TTS_EMPTY(slot));
518 :
519 0 : ereport(ERROR,
520 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
521 : errmsg("cannot retrieve a system column in this context")));
522 :
523 : return 0; /* silence compiler warnings */
524 : }
525 :
526 : static void
527 1567948 : tts_minimal_materialize(TupleTableSlot *slot)
528 : {
529 1567948 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
530 : MemoryContext oldContext;
531 :
532 : Assert(!TTS_EMPTY(slot));
533 :
534 : /* If slot has its tuple already materialized, nothing to do. */
535 1567948 : if (TTS_SHOULDFREE(slot))
536 144012 : return;
537 :
538 1423936 : oldContext = MemoryContextSwitchTo(slot->tts_mcxt);
539 :
540 : /*
541 : * Have to deform from scratch, otherwise tts_values[] entries could point
542 : * into the non-materialized tuple (which might be gone when accessed).
543 : */
544 1423936 : slot->tts_nvalid = 0;
545 1423936 : mslot->off = 0;
546 :
547 1423936 : if (!mslot->mintuple)
548 : {
549 1312880 : mslot->mintuple = heap_form_minimal_tuple(slot->tts_tupleDescriptor,
550 : slot->tts_values,
551 : slot->tts_isnull);
552 : }
553 : else
554 : {
555 : /*
556 : * The minimal tuple contained in this slot is not allocated in the
557 : * memory context of the given slot (else it would have
558 : * TTS_FLAG_SHOULDFREE set). Copy the minimal tuple into the given
559 : * slot's memory context.
560 : */
561 111056 : mslot->mintuple = heap_copy_minimal_tuple(mslot->mintuple);
562 : }
563 :
564 1423936 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
565 :
566 : Assert(mslot->tuple == &mslot->minhdr);
567 :
568 1423936 : mslot->minhdr.t_len = mslot->mintuple->t_len + MINIMAL_TUPLE_OFFSET;
569 1423936 : mslot->minhdr.t_data = (HeapTupleHeader) ((char *) mslot->mintuple - MINIMAL_TUPLE_OFFSET);
570 :
571 1423936 : MemoryContextSwitchTo(oldContext);
572 : }
573 :
574 : static void
575 1063354 : tts_minimal_copyslot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
576 : {
577 : MemoryContext oldcontext;
578 : MinimalTuple mintuple;
579 :
580 1063354 : oldcontext = MemoryContextSwitchTo(dstslot->tts_mcxt);
581 1063354 : mintuple = ExecCopySlotMinimalTuple(srcslot);
582 1063354 : MemoryContextSwitchTo(oldcontext);
583 :
584 1063354 : ExecStoreMinimalTuple(mintuple, dstslot, true);
585 1063354 : }
586 :
587 : static MinimalTuple
588 3856224 : tts_minimal_get_minimal_tuple(TupleTableSlot *slot)
589 : {
590 3856224 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
591 :
592 3856224 : if (!mslot->mintuple)
593 0 : tts_minimal_materialize(slot);
594 :
595 3856224 : return mslot->mintuple;
596 : }
597 :
598 : static HeapTuple
599 990528 : tts_minimal_copy_heap_tuple(TupleTableSlot *slot)
600 : {
601 990528 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
602 :
603 990528 : if (!mslot->mintuple)
604 1192 : tts_minimal_materialize(slot);
605 :
606 990528 : return heap_tuple_from_minimal_tuple(mslot->mintuple);
607 : }
608 :
609 : static MinimalTuple
610 2642456 : tts_minimal_copy_minimal_tuple(TupleTableSlot *slot)
611 : {
612 2642456 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
613 :
614 2642456 : if (!mslot->mintuple)
615 1195270 : tts_minimal_materialize(slot);
616 :
617 2642456 : return heap_copy_minimal_tuple(mslot->mintuple);
618 : }
619 :
620 : static void
621 53547232 : tts_minimal_store_tuple(TupleTableSlot *slot, MinimalTuple mtup, bool shouldFree)
622 : {
623 53547232 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
624 :
625 53547232 : tts_minimal_clear(slot);
626 :
627 : Assert(!TTS_SHOULDFREE(slot));
628 : Assert(TTS_EMPTY(slot));
629 :
630 53547232 : slot->tts_flags &= ~TTS_FLAG_EMPTY;
631 53547232 : slot->tts_nvalid = 0;
632 53547232 : mslot->off = 0;
633 :
634 53547232 : mslot->mintuple = mtup;
635 : Assert(mslot->tuple == &mslot->minhdr);
636 53547232 : mslot->minhdr.t_len = mtup->t_len + MINIMAL_TUPLE_OFFSET;
637 53547232 : mslot->minhdr.t_data = (HeapTupleHeader) ((char *) mtup - MINIMAL_TUPLE_OFFSET);
638 : /* no need to set t_self or t_tableOid since we won't allow access */
639 :
640 53547232 : if (shouldFree)
641 12080050 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
642 53547232 : }
643 :
644 :
645 : /*
646 : * TupleTableSlotOps implementation for BufferHeapTupleTableSlot.
647 : */
648 :
649 : static void
650 30777404 : tts_buffer_heap_init(TupleTableSlot *slot)
651 : {
652 30777404 : }
653 :
654 : static void
655 30767624 : tts_buffer_heap_release(TupleTableSlot *slot)
656 : {
657 30767624 : }
658 :
659 : static void
660 53440844 : tts_buffer_heap_clear(TupleTableSlot *slot)
661 : {
662 53440844 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
663 :
664 : /*
665 : * Free the memory for heap tuple if allowed. A tuple coming from buffer
666 : * can never be freed. But we may have materialized a tuple from buffer.
667 : * Such a tuple can be freed.
668 : */
669 53440844 : if (TTS_SHOULDFREE(slot))
670 : {
671 : /* We should have unpinned the buffer while materializing the tuple. */
672 : Assert(!BufferIsValid(bslot->buffer));
673 :
674 13988372 : heap_freetuple(bslot->base.tuple);
675 13988372 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
676 : }
677 :
678 53440844 : if (BufferIsValid(bslot->buffer))
679 14064276 : ReleaseBuffer(bslot->buffer);
680 :
681 53440844 : slot->tts_nvalid = 0;
682 53440844 : slot->tts_flags |= TTS_FLAG_EMPTY;
683 53440844 : ItemPointerSetInvalid(&slot->tts_tid);
684 53440844 : bslot->base.tuple = NULL;
685 53440844 : bslot->base.off = 0;
686 53440844 : bslot->buffer = InvalidBuffer;
687 53440844 : }
688 :
689 : static void
690 140549632 : tts_buffer_heap_getsomeattrs(TupleTableSlot *slot, int natts)
691 : {
692 140549632 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
693 :
694 : Assert(!TTS_EMPTY(slot));
695 :
696 140549632 : slot_deform_heap_tuple(slot, bslot->base.tuple, &bslot->base.off, natts);
697 140549632 : }
698 :
699 : static Datum
700 1262 : tts_buffer_heap_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
701 : {
702 1262 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
703 :
704 : Assert(!TTS_EMPTY(slot));
705 :
706 : /*
707 : * In some code paths it's possible to get here with a non-materialized
708 : * slot, in which case we can't retrieve system columns.
709 : */
710 1262 : if (!bslot->base.tuple)
711 0 : ereport(ERROR,
712 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
713 : errmsg("cannot retrieve a system column in this context")));
714 :
715 1262 : return heap_getsysattr(bslot->base.tuple, attnum,
716 : slot->tts_tupleDescriptor, isnull);
717 : }
718 :
719 : static void
720 27896172 : tts_buffer_heap_materialize(TupleTableSlot *slot)
721 : {
722 27896172 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
723 : MemoryContext oldContext;
724 :
725 : Assert(!TTS_EMPTY(slot));
726 :
727 : /* If slot has its tuple already materialized, nothing to do. */
728 27896172 : if (TTS_SHOULDFREE(slot))
729 24918042 : return;
730 :
731 2978130 : oldContext = MemoryContextSwitchTo(slot->tts_mcxt);
732 :
733 : /*
734 : * Have to deform from scratch, otherwise tts_values[] entries could point
735 : * into the non-materialized tuple (which might be gone when accessed).
736 : */
737 2978130 : bslot->base.off = 0;
738 2978130 : slot->tts_nvalid = 0;
739 :
740 2978130 : if (!bslot->base.tuple)
741 : {
742 : /*
743 : * Normally BufferHeapTupleTableSlot should have a tuple + buffer
744 : * associated with it, unless it's materialized (which would've
745 : * returned above). But when it's useful to allow storing virtual
746 : * tuples in a buffer slot, which then also needs to be
747 : * materializable.
748 : */
749 2586356 : bslot->base.tuple = heap_form_tuple(slot->tts_tupleDescriptor,
750 : slot->tts_values,
751 : slot->tts_isnull);
752 : }
753 : else
754 : {
755 391774 : bslot->base.tuple = heap_copytuple(bslot->base.tuple);
756 :
757 : /*
758 : * A heap tuple stored in a BufferHeapTupleTableSlot should have a
759 : * buffer associated with it, unless it's materialized or virtual.
760 : */
761 391774 : if (likely(BufferIsValid(bslot->buffer)))
762 391774 : ReleaseBuffer(bslot->buffer);
763 391774 : bslot->buffer = InvalidBuffer;
764 : }
765 :
766 : /*
767 : * We don't set TTS_FLAG_SHOULDFREE until after releasing the buffer, if
768 : * any. This avoids having a transient state that would fall foul of our
769 : * assertions that a slot with TTS_FLAG_SHOULDFREE doesn't own a buffer.
770 : * In the unlikely event that ReleaseBuffer() above errors out, we'd
771 : * effectively leak the copied tuple, but that seems fairly harmless.
772 : */
773 2978130 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
774 :
775 2978130 : MemoryContextSwitchTo(oldContext);
776 : }
777 :
778 : static void
779 11343554 : tts_buffer_heap_copyslot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
780 : {
781 11343554 : BufferHeapTupleTableSlot *bsrcslot = (BufferHeapTupleTableSlot *) srcslot;
782 11343554 : BufferHeapTupleTableSlot *bdstslot = (BufferHeapTupleTableSlot *) dstslot;
783 :
784 : /*
785 : * If the source slot is of a different kind, or is a buffer slot that has
786 : * been materialized / is virtual, make a new copy of the tuple. Otherwise
787 : * make a new reference to the in-buffer tuple.
788 : */
789 11343554 : if (dstslot->tts_ops != srcslot->tts_ops ||
790 8802 : TTS_SHOULDFREE(srcslot) ||
791 8798 : !bsrcslot->base.tuple)
792 11334756 : {
793 : MemoryContext oldContext;
794 :
795 11334756 : ExecClearTuple(dstslot);
796 11334756 : dstslot->tts_flags &= ~TTS_FLAG_EMPTY;
797 11334756 : oldContext = MemoryContextSwitchTo(dstslot->tts_mcxt);
798 11334756 : bdstslot->base.tuple = ExecCopySlotHeapTuple(srcslot);
799 11334756 : dstslot->tts_flags |= TTS_FLAG_SHOULDFREE;
800 11334756 : MemoryContextSwitchTo(oldContext);
801 : }
802 : else
803 : {
804 : Assert(BufferIsValid(bsrcslot->buffer));
805 :
806 8798 : tts_buffer_heap_store_tuple(dstslot, bsrcslot->base.tuple,
807 : bsrcslot->buffer, false);
808 :
809 : /*
810 : * The HeapTupleData portion of the source tuple might be shorter
811 : * lived than the destination slot. Therefore copy the HeapTuple into
812 : * our slot's tupdata, which is guaranteed to live long enough (but
813 : * will still point into the buffer).
814 : */
815 8798 : memcpy(&bdstslot->base.tupdata, bdstslot->base.tuple, sizeof(HeapTupleData));
816 8798 : bdstslot->base.tuple = &bdstslot->base.tupdata;
817 : }
818 11343554 : }
819 :
820 : static HeapTuple
821 43950114 : tts_buffer_heap_get_heap_tuple(TupleTableSlot *slot)
822 : {
823 43950114 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
824 :
825 : Assert(!TTS_EMPTY(slot));
826 :
827 43950114 : if (!bslot->base.tuple)
828 0 : tts_buffer_heap_materialize(slot);
829 :
830 43950114 : return bslot->base.tuple;
831 : }
832 :
833 : static HeapTuple
834 19981334 : tts_buffer_heap_copy_heap_tuple(TupleTableSlot *slot)
835 : {
836 19981334 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
837 :
838 : Assert(!TTS_EMPTY(slot));
839 :
840 19981334 : if (!bslot->base.tuple)
841 0 : tts_buffer_heap_materialize(slot);
842 :
843 19981334 : return heap_copytuple(bslot->base.tuple);
844 : }
845 :
846 : static MinimalTuple
847 2529870 : tts_buffer_heap_copy_minimal_tuple(TupleTableSlot *slot)
848 : {
849 2529870 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
850 :
851 : Assert(!TTS_EMPTY(slot));
852 :
853 2529870 : if (!bslot->base.tuple)
854 0 : tts_buffer_heap_materialize(slot);
855 :
856 2529870 : return minimal_tuple_from_heap_tuple(bslot->base.tuple);
857 : }
858 :
859 : static inline void
860 166203108 : tts_buffer_heap_store_tuple(TupleTableSlot *slot, HeapTuple tuple,
861 : Buffer buffer, bool transfer_pin)
862 : {
863 166203108 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
864 :
865 166203108 : if (TTS_SHOULDFREE(slot))
866 : {
867 : /* materialized slot shouldn't have a buffer to release */
868 : Assert(!BufferIsValid(bslot->buffer));
869 :
870 393452 : heap_freetuple(bslot->base.tuple);
871 393452 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
872 : }
873 :
874 166203108 : slot->tts_flags &= ~TTS_FLAG_EMPTY;
875 166203108 : slot->tts_nvalid = 0;
876 166203108 : bslot->base.tuple = tuple;
877 166203108 : bslot->base.off = 0;
878 166203108 : slot->tts_tid = tuple->t_self;
879 :
880 : /*
881 : * If tuple is on a disk page, keep the page pinned as long as we hold a
882 : * pointer into it. We assume the caller already has such a pin. If
883 : * transfer_pin is true, we'll transfer that pin to this slot, if not
884 : * we'll pin it again ourselves.
885 : *
886 : * This is coded to optimize the case where the slot previously held a
887 : * tuple on the same disk page: in that case releasing and re-acquiring
888 : * the pin is a waste of cycles. This is a common situation during
889 : * seqscans, so it's worth troubling over.
890 : */
891 166203108 : if (bslot->buffer != buffer)
892 : {
893 20571292 : if (BufferIsValid(bslot->buffer))
894 6110790 : ReleaseBuffer(bslot->buffer);
895 :
896 20571292 : bslot->buffer = buffer;
897 :
898 20571292 : if (!transfer_pin && BufferIsValid(buffer))
899 20366310 : IncrBufferRefCount(buffer);
900 : }
901 145631816 : else if (transfer_pin && BufferIsValid(buffer))
902 : {
903 : /*
904 : * In transfer_pin mode the caller won't know about the same-page
905 : * optimization, so we gotta release its pin.
906 : */
907 351852 : ReleaseBuffer(buffer);
908 : }
909 166203108 : }
910 :
911 : /*
912 : * slot_deform_heap_tuple
913 : * Given a TupleTableSlot, extract data from the slot's physical tuple
914 : * into its Datum/isnull arrays. Data is extracted up through the
915 : * natts'th column (caller must ensure this is a legal column number).
916 : *
917 : * This is essentially an incremental version of heap_deform_tuple:
918 : * on each call we extract attributes up to the one needed, without
919 : * re-computing information about previously extracted attributes.
920 : * slot->tts_nvalid is the number of attributes already extracted.
921 : *
922 : * This is marked as always inline, so the different offp for different types
923 : * of slots gets optimized away.
924 : */
925 : static pg_attribute_always_inline void
926 206910170 : slot_deform_heap_tuple(TupleTableSlot *slot, HeapTuple tuple, uint32 *offp,
927 : int natts)
928 : {
929 206910170 : TupleDesc tupleDesc = slot->tts_tupleDescriptor;
930 206910170 : Datum *values = slot->tts_values;
931 206910170 : bool *isnull = slot->tts_isnull;
932 206910170 : HeapTupleHeader tup = tuple->t_data;
933 206910170 : bool hasnulls = HeapTupleHasNulls(tuple);
934 : int attnum;
935 : char *tp; /* ptr to tuple data */
936 : uint32 off; /* offset in tuple data */
937 206910170 : bits8 *bp = tup->t_bits; /* ptr to null bitmap in tuple */
938 : bool slow; /* can we use/set attcacheoff? */
939 :
940 : /* We can only fetch as many attributes as the tuple has. */
941 206910170 : natts = Min(HeapTupleHeaderGetNatts(tuple->t_data), natts);
942 :
943 : /*
944 : * Check whether the first call for this tuple, and initialize or restore
945 : * loop state.
946 : */
947 206910170 : attnum = slot->tts_nvalid;
948 206910170 : if (attnum == 0)
949 : {
950 : /* Start from the first attribute */
951 159551786 : off = 0;
952 159551786 : slow = false;
953 : }
954 : else
955 : {
956 : /* Restore state from previous execution */
957 47358384 : off = *offp;
958 47358384 : slow = TTS_SLOW(slot);
959 : }
960 :
961 206910170 : tp = (char *) tup + tup->t_hoff;
962 :
963 908964720 : for (; attnum < natts; attnum++)
964 : {
965 702054550 : Form_pg_attribute thisatt = TupleDescAttr(tupleDesc, attnum);
966 :
967 702054550 : if (hasnulls && att_isnull(attnum, bp))
968 : {
969 45610634 : values[attnum] = (Datum) 0;
970 45610634 : isnull[attnum] = true;
971 45610634 : slow = true; /* can't use attcacheoff anymore */
972 45610634 : continue;
973 : }
974 :
975 656443916 : isnull[attnum] = false;
976 :
977 656443916 : if (!slow && thisatt->attcacheoff >= 0)
978 621140528 : off = thisatt->attcacheoff;
979 35303388 : else if (thisatt->attlen == -1)
980 : {
981 : /*
982 : * We can only cache the offset for a varlena attribute if the
983 : * offset is already suitably aligned, so that there would be no
984 : * pad bytes in any case: then the offset will be valid for either
985 : * an aligned or unaligned value.
986 : */
987 14523698 : if (!slow &&
988 846354 : off == att_align_nominal(off, thisatt->attalign))
989 68288 : thisatt->attcacheoff = off;
990 : else
991 : {
992 13609056 : off = att_align_pointer(off, thisatt->attalign, -1,
993 : tp + off);
994 13609056 : slow = true;
995 : }
996 : }
997 : else
998 : {
999 : /* not varlena, so safe to use att_align_nominal */
1000 21626044 : off = att_align_nominal(off, thisatt->attalign);
1001 :
1002 21626044 : if (!slow)
1003 528072 : thisatt->attcacheoff = off;
1004 : }
1005 :
1006 656443916 : values[attnum] = fetchatt(thisatt, tp + off);
1007 :
1008 656443916 : off = att_addlength_pointer(off, thisatt->attlen, tp + off);
1009 :
1010 656443916 : if (thisatt->attlen <= 0)
1011 48741904 : slow = true; /* can't use attcacheoff anymore */
1012 : }
1013 :
1014 : /*
1015 : * Save state for next execution
1016 : */
1017 206910170 : slot->tts_nvalid = attnum;
1018 206910170 : *offp = off;
1019 206910170 : if (slow)
1020 42227418 : slot->tts_flags |= TTS_FLAG_SLOW;
1021 : else
1022 164682752 : slot->tts_flags &= ~TTS_FLAG_SLOW;
1023 206910170 : }
1024 :
1025 :
1026 : const TupleTableSlotOps TTSOpsVirtual = {
1027 : .base_slot_size = sizeof(VirtualTupleTableSlot),
1028 : .init = tts_virtual_init,
1029 : .release = tts_virtual_release,
1030 : .clear = tts_virtual_clear,
1031 : .getsomeattrs = tts_virtual_getsomeattrs,
1032 : .getsysattr = tts_virtual_getsysattr,
1033 : .materialize = tts_virtual_materialize,
1034 : .copyslot = tts_virtual_copyslot,
1035 :
1036 : /*
1037 : * A virtual tuple table slot can not "own" a heap tuple or a minimal
1038 : * tuple.
1039 : */
1040 : .get_heap_tuple = NULL,
1041 : .get_minimal_tuple = NULL,
1042 : .copy_heap_tuple = tts_virtual_copy_heap_tuple,
1043 : .copy_minimal_tuple = tts_virtual_copy_minimal_tuple
1044 : };
1045 :
1046 : const TupleTableSlotOps TTSOpsHeapTuple = {
1047 : .base_slot_size = sizeof(HeapTupleTableSlot),
1048 : .init = tts_heap_init,
1049 : .release = tts_heap_release,
1050 : .clear = tts_heap_clear,
1051 : .getsomeattrs = tts_heap_getsomeattrs,
1052 : .getsysattr = tts_heap_getsysattr,
1053 : .materialize = tts_heap_materialize,
1054 : .copyslot = tts_heap_copyslot,
1055 : .get_heap_tuple = tts_heap_get_heap_tuple,
1056 :
1057 : /* A heap tuple table slot can not "own" a minimal tuple. */
1058 : .get_minimal_tuple = NULL,
1059 : .copy_heap_tuple = tts_heap_copy_heap_tuple,
1060 : .copy_minimal_tuple = tts_heap_copy_minimal_tuple
1061 : };
1062 :
1063 : const TupleTableSlotOps TTSOpsMinimalTuple = {
1064 : .base_slot_size = sizeof(MinimalTupleTableSlot),
1065 : .init = tts_minimal_init,
1066 : .release = tts_minimal_release,
1067 : .clear = tts_minimal_clear,
1068 : .getsomeattrs = tts_minimal_getsomeattrs,
1069 : .getsysattr = tts_minimal_getsysattr,
1070 : .materialize = tts_minimal_materialize,
1071 : .copyslot = tts_minimal_copyslot,
1072 :
1073 : /* A minimal tuple table slot can not "own" a heap tuple. */
1074 : .get_heap_tuple = NULL,
1075 : .get_minimal_tuple = tts_minimal_get_minimal_tuple,
1076 : .copy_heap_tuple = tts_minimal_copy_heap_tuple,
1077 : .copy_minimal_tuple = tts_minimal_copy_minimal_tuple
1078 : };
1079 :
1080 : const TupleTableSlotOps TTSOpsBufferHeapTuple = {
1081 : .base_slot_size = sizeof(BufferHeapTupleTableSlot),
1082 : .init = tts_buffer_heap_init,
1083 : .release = tts_buffer_heap_release,
1084 : .clear = tts_buffer_heap_clear,
1085 : .getsomeattrs = tts_buffer_heap_getsomeattrs,
1086 : .getsysattr = tts_buffer_heap_getsysattr,
1087 : .materialize = tts_buffer_heap_materialize,
1088 : .copyslot = tts_buffer_heap_copyslot,
1089 : .get_heap_tuple = tts_buffer_heap_get_heap_tuple,
1090 :
1091 : /* A buffer heap tuple table slot can not "own" a minimal tuple. */
1092 : .get_minimal_tuple = NULL,
1093 : .copy_heap_tuple = tts_buffer_heap_copy_heap_tuple,
1094 : .copy_minimal_tuple = tts_buffer_heap_copy_minimal_tuple
1095 : };
1096 :
1097 :
1098 : /* ----------------------------------------------------------------
1099 : * tuple table create/delete functions
1100 : * ----------------------------------------------------------------
1101 : */
1102 :
1103 : /* --------------------------------
1104 : * MakeTupleTableSlot
1105 : *
1106 : * Basic routine to make an empty TupleTableSlot of given
1107 : * TupleTableSlotType. If tupleDesc is specified the slot's descriptor is
1108 : * fixed for its lifetime, gaining some efficiency. If that's
1109 : * undesirable, pass NULL.
1110 : * --------------------------------
1111 : */
1112 : TupleTableSlot *
1113 42087596 : MakeTupleTableSlot(TupleDesc tupleDesc,
1114 : const TupleTableSlotOps *tts_ops)
1115 : {
1116 : Size basesz,
1117 : allocsz;
1118 : TupleTableSlot *slot;
1119 :
1120 42087596 : basesz = tts_ops->base_slot_size;
1121 :
1122 : /*
1123 : * When a fixed descriptor is specified, we can reduce overhead by
1124 : * allocating the entire slot in one go.
1125 : */
1126 42087596 : if (tupleDesc)
1127 42049912 : allocsz = MAXALIGN(basesz) +
1128 42049912 : MAXALIGN(tupleDesc->natts * sizeof(Datum)) +
1129 42049912 : MAXALIGN(tupleDesc->natts * sizeof(bool));
1130 : else
1131 37684 : allocsz = basesz;
1132 :
1133 42087596 : slot = palloc0(allocsz);
1134 : /* const for optimization purposes, OK to modify at allocation time */
1135 42087596 : *((const TupleTableSlotOps **) &slot->tts_ops) = tts_ops;
1136 42087596 : slot->type = T_TupleTableSlot;
1137 42087596 : slot->tts_flags |= TTS_FLAG_EMPTY;
1138 42087596 : if (tupleDesc != NULL)
1139 42049912 : slot->tts_flags |= TTS_FLAG_FIXED;
1140 42087596 : slot->tts_tupleDescriptor = tupleDesc;
1141 42087596 : slot->tts_mcxt = CurrentMemoryContext;
1142 42087596 : slot->tts_nvalid = 0;
1143 :
1144 42087596 : if (tupleDesc != NULL)
1145 : {
1146 42049912 : slot->tts_values = (Datum *)
1147 : (((char *) slot)
1148 42049912 : + MAXALIGN(basesz));
1149 42049912 : slot->tts_isnull = (bool *)
1150 : (((char *) slot)
1151 42049912 : + MAXALIGN(basesz)
1152 42049912 : + MAXALIGN(tupleDesc->natts * sizeof(Datum)));
1153 :
1154 42049912 : PinTupleDesc(tupleDesc);
1155 : }
1156 :
1157 : /*
1158 : * And allow slot type specific initialization.
1159 : */
1160 42087596 : slot->tts_ops->init(slot);
1161 :
1162 42087596 : return slot;
1163 : }
1164 :
1165 : /* --------------------------------
1166 : * ExecAllocTableSlot
1167 : *
1168 : * Create a tuple table slot within a tuple table (which is just a List).
1169 : * --------------------------------
1170 : */
1171 : TupleTableSlot *
1172 1727734 : ExecAllocTableSlot(List **tupleTable, TupleDesc desc,
1173 : const TupleTableSlotOps *tts_ops)
1174 : {
1175 1727734 : TupleTableSlot *slot = MakeTupleTableSlot(desc, tts_ops);
1176 :
1177 1727734 : *tupleTable = lappend(*tupleTable, slot);
1178 :
1179 1727734 : return slot;
1180 : }
1181 :
1182 : /* --------------------------------
1183 : * ExecResetTupleTable
1184 : *
1185 : * This releases any resources (buffer pins, tupdesc refcounts)
1186 : * held by the tuple table, and optionally releases the memory
1187 : * occupied by the tuple table data structure.
1188 : * It is expected that this routine be called by ExecEndPlan().
1189 : * --------------------------------
1190 : */
1191 : void
1192 816168 : ExecResetTupleTable(List *tupleTable, /* tuple table */
1193 : bool shouldFree) /* true if we should free memory */
1194 : {
1195 : ListCell *lc;
1196 :
1197 2795786 : foreach(lc, tupleTable)
1198 : {
1199 1979618 : TupleTableSlot *slot = lfirst_node(TupleTableSlot, lc);
1200 :
1201 : /* Always release resources and reset the slot to empty */
1202 1979618 : ExecClearTuple(slot);
1203 1979618 : slot->tts_ops->release(slot);
1204 1979618 : if (slot->tts_tupleDescriptor)
1205 : {
1206 1979564 : ReleaseTupleDesc(slot->tts_tupleDescriptor);
1207 1979564 : slot->tts_tupleDescriptor = NULL;
1208 : }
1209 :
1210 : /* If shouldFree, release memory occupied by the slot itself */
1211 1979618 : if (shouldFree)
1212 : {
1213 5184 : if (!TTS_FIXED(slot))
1214 : {
1215 0 : if (slot->tts_values)
1216 0 : pfree(slot->tts_values);
1217 0 : if (slot->tts_isnull)
1218 0 : pfree(slot->tts_isnull);
1219 : }
1220 5184 : pfree(slot);
1221 : }
1222 : }
1223 :
1224 : /* If shouldFree, release the list structure */
1225 816168 : if (shouldFree)
1226 5108 : list_free(tupleTable);
1227 816168 : }
1228 :
1229 : /* --------------------------------
1230 : * MakeSingleTupleTableSlot
1231 : *
1232 : * This is a convenience routine for operations that need a standalone
1233 : * TupleTableSlot not gotten from the main executor tuple table. It makes
1234 : * a single slot of given TupleTableSlotType and initializes it to use the
1235 : * given tuple descriptor.
1236 : * --------------------------------
1237 : */
1238 : TupleTableSlot *
1239 40359718 : MakeSingleTupleTableSlot(TupleDesc tupdesc,
1240 : const TupleTableSlotOps *tts_ops)
1241 : {
1242 40359718 : TupleTableSlot *slot = MakeTupleTableSlot(tupdesc, tts_ops);
1243 :
1244 40359718 : return slot;
1245 : }
1246 :
1247 : /* --------------------------------
1248 : * ExecDropSingleTupleTableSlot
1249 : *
1250 : * Release a TupleTableSlot made with MakeSingleTupleTableSlot.
1251 : * DON'T use this on a slot that's part of a tuple table list!
1252 : * --------------------------------
1253 : */
1254 : void
1255 40040056 : ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
1256 : {
1257 : /* This should match ExecResetTupleTable's processing of one slot */
1258 : Assert(IsA(slot, TupleTableSlot));
1259 40040056 : ExecClearTuple(slot);
1260 40040056 : slot->tts_ops->release(slot);
1261 40040056 : if (slot->tts_tupleDescriptor)
1262 40040056 : ReleaseTupleDesc(slot->tts_tupleDescriptor);
1263 40040056 : if (!TTS_FIXED(slot))
1264 : {
1265 0 : if (slot->tts_values)
1266 0 : pfree(slot->tts_values);
1267 0 : if (slot->tts_isnull)
1268 0 : pfree(slot->tts_isnull);
1269 : }
1270 40040056 : pfree(slot);
1271 40040056 : }
1272 :
1273 :
1274 : /* ----------------------------------------------------------------
1275 : * tuple table slot accessor functions
1276 : * ----------------------------------------------------------------
1277 : */
1278 :
1279 : /* --------------------------------
1280 : * ExecSetSlotDescriptor
1281 : *
1282 : * This function is used to set the tuple descriptor associated
1283 : * with the slot's tuple. The passed descriptor must have lifespan
1284 : * at least equal to the slot's. If it is a reference-counted descriptor
1285 : * then the reference count is incremented for as long as the slot holds
1286 : * a reference.
1287 : * --------------------------------
1288 : */
1289 : void
1290 37630 : ExecSetSlotDescriptor(TupleTableSlot *slot, /* slot to change */
1291 : TupleDesc tupdesc) /* new tuple descriptor */
1292 : {
1293 : Assert(!TTS_FIXED(slot));
1294 :
1295 : /* For safety, make sure slot is empty before changing it */
1296 37630 : ExecClearTuple(slot);
1297 :
1298 : /*
1299 : * Release any old descriptor. Also release old Datum/isnull arrays if
1300 : * present (we don't bother to check if they could be re-used).
1301 : */
1302 37630 : if (slot->tts_tupleDescriptor)
1303 0 : ReleaseTupleDesc(slot->tts_tupleDescriptor);
1304 :
1305 37630 : if (slot->tts_values)
1306 0 : pfree(slot->tts_values);
1307 37630 : if (slot->tts_isnull)
1308 0 : pfree(slot->tts_isnull);
1309 :
1310 : /*
1311 : * Install the new descriptor; if it's refcounted, bump its refcount.
1312 : */
1313 37630 : slot->tts_tupleDescriptor = tupdesc;
1314 37630 : PinTupleDesc(tupdesc);
1315 :
1316 : /*
1317 : * Allocate Datum/isnull arrays of the appropriate size. These must have
1318 : * the same lifetime as the slot, so allocate in the slot's own context.
1319 : */
1320 37630 : slot->tts_values = (Datum *)
1321 37630 : MemoryContextAlloc(slot->tts_mcxt, tupdesc->natts * sizeof(Datum));
1322 37630 : slot->tts_isnull = (bool *)
1323 37630 : MemoryContextAlloc(slot->tts_mcxt, tupdesc->natts * sizeof(bool));
1324 37630 : }
1325 :
1326 : /* --------------------------------
1327 : * ExecStoreHeapTuple
1328 : *
1329 : * This function is used to store an on-the-fly physical tuple into a specified
1330 : * slot in the tuple table.
1331 : *
1332 : * tuple: tuple to store
1333 : * slot: TTSOpsHeapTuple type slot to store it in
1334 : * shouldFree: true if ExecClearTuple should pfree() the tuple
1335 : * when done with it
1336 : *
1337 : * shouldFree is normally set 'true' for tuples constructed on-the-fly. But it
1338 : * can be 'false' when the referenced tuple is held in a tuple table slot
1339 : * belonging to a lower-level executor Proc node. In this case the lower-level
1340 : * slot retains ownership and responsibility for eventually releasing the
1341 : * tuple. When this method is used, we must be certain that the upper-level
1342 : * Proc node will lose interest in the tuple sooner than the lower-level one
1343 : * does! If you're not certain, copy the lower-level tuple with heap_copytuple
1344 : * and let the upper-level table slot assume ownership of the copy!
1345 : *
1346 : * Return value is just the passed-in slot pointer.
1347 : *
1348 : * If the target slot is not guaranteed to be TTSOpsHeapTuple type slot, use
1349 : * the, more expensive, ExecForceStoreHeapTuple().
1350 : * --------------------------------
1351 : */
1352 : TupleTableSlot *
1353 7694066 : ExecStoreHeapTuple(HeapTuple tuple,
1354 : TupleTableSlot *slot,
1355 : bool shouldFree)
1356 : {
1357 : /*
1358 : * sanity checks
1359 : */
1360 : Assert(tuple != NULL);
1361 : Assert(slot != NULL);
1362 : Assert(slot->tts_tupleDescriptor != NULL);
1363 :
1364 7694066 : if (unlikely(!TTS_IS_HEAPTUPLE(slot)))
1365 0 : elog(ERROR, "trying to store a heap tuple into wrong type of slot");
1366 7694066 : tts_heap_store_tuple(slot, tuple, shouldFree);
1367 :
1368 7694066 : slot->tts_tableOid = tuple->t_tableOid;
1369 :
1370 7694066 : return slot;
1371 : }
1372 :
1373 : /* --------------------------------
1374 : * ExecStoreBufferHeapTuple
1375 : *
1376 : * This function is used to store an on-disk physical tuple from a buffer
1377 : * into a specified slot in the tuple table.
1378 : *
1379 : * tuple: tuple to store
1380 : * slot: TTSOpsBufferHeapTuple type slot to store it in
1381 : * buffer: disk buffer if tuple is in a disk page, else InvalidBuffer
1382 : *
1383 : * The tuple table code acquires a pin on the buffer which is held until the
1384 : * slot is cleared, so that the tuple won't go away on us.
1385 : *
1386 : * Return value is just the passed-in slot pointer.
1387 : *
1388 : * If the target slot is not guaranteed to be TTSOpsBufferHeapTuple type slot,
1389 : * use the, more expensive, ExecForceStoreHeapTuple().
1390 : * --------------------------------
1391 : */
1392 : TupleTableSlot *
1393 165637476 : ExecStoreBufferHeapTuple(HeapTuple tuple,
1394 : TupleTableSlot *slot,
1395 : Buffer buffer)
1396 : {
1397 : /*
1398 : * sanity checks
1399 : */
1400 : Assert(tuple != NULL);
1401 : Assert(slot != NULL);
1402 : Assert(slot->tts_tupleDescriptor != NULL);
1403 : Assert(BufferIsValid(buffer));
1404 :
1405 165637476 : if (unlikely(!TTS_IS_BUFFERTUPLE(slot)))
1406 0 : elog(ERROR, "trying to store an on-disk heap tuple into wrong type of slot");
1407 165637476 : tts_buffer_heap_store_tuple(slot, tuple, buffer, false);
1408 :
1409 165637476 : slot->tts_tableOid = tuple->t_tableOid;
1410 :
1411 165637476 : return slot;
1412 : }
1413 :
1414 : /*
1415 : * Like ExecStoreBufferHeapTuple, but transfer an existing pin from the caller
1416 : * to the slot, i.e. the caller doesn't need to, and may not, release the pin.
1417 : */
1418 : TupleTableSlot *
1419 556834 : ExecStorePinnedBufferHeapTuple(HeapTuple tuple,
1420 : TupleTableSlot *slot,
1421 : Buffer buffer)
1422 : {
1423 : /*
1424 : * sanity checks
1425 : */
1426 : Assert(tuple != NULL);
1427 : Assert(slot != NULL);
1428 : Assert(slot->tts_tupleDescriptor != NULL);
1429 : Assert(BufferIsValid(buffer));
1430 :
1431 556834 : if (unlikely(!TTS_IS_BUFFERTUPLE(slot)))
1432 0 : elog(ERROR, "trying to store an on-disk heap tuple into wrong type of slot");
1433 556834 : tts_buffer_heap_store_tuple(slot, tuple, buffer, true);
1434 :
1435 556834 : slot->tts_tableOid = tuple->t_tableOid;
1436 :
1437 556834 : return slot;
1438 : }
1439 :
1440 : /*
1441 : * Store a minimal tuple into TTSOpsMinimalTuple type slot.
1442 : *
1443 : * If the target slot is not guaranteed to be TTSOpsMinimalTuple type slot,
1444 : * use the, more expensive, ExecForceStoreMinimalTuple().
1445 : */
1446 : TupleTableSlot *
1447 50155052 : ExecStoreMinimalTuple(MinimalTuple mtup,
1448 : TupleTableSlot *slot,
1449 : bool shouldFree)
1450 : {
1451 : /*
1452 : * sanity checks
1453 : */
1454 : Assert(mtup != NULL);
1455 : Assert(slot != NULL);
1456 : Assert(slot->tts_tupleDescriptor != NULL);
1457 :
1458 50155052 : if (unlikely(!TTS_IS_MINIMALTUPLE(slot)))
1459 0 : elog(ERROR, "trying to store a minimal tuple into wrong type of slot");
1460 50155052 : tts_minimal_store_tuple(slot, mtup, shouldFree);
1461 :
1462 50155052 : return slot;
1463 : }
1464 :
1465 : /*
1466 : * Store a HeapTuple into any kind of slot, performing conversion if
1467 : * necessary.
1468 : */
1469 : void
1470 1777232 : ExecForceStoreHeapTuple(HeapTuple tuple,
1471 : TupleTableSlot *slot,
1472 : bool shouldFree)
1473 : {
1474 1777232 : if (TTS_IS_HEAPTUPLE(slot))
1475 : {
1476 426 : ExecStoreHeapTuple(tuple, slot, shouldFree);
1477 : }
1478 1776806 : else if (TTS_IS_BUFFERTUPLE(slot))
1479 : {
1480 : MemoryContext oldContext;
1481 71968 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
1482 :
1483 71968 : ExecClearTuple(slot);
1484 71968 : slot->tts_flags &= ~TTS_FLAG_EMPTY;
1485 71968 : oldContext = MemoryContextSwitchTo(slot->tts_mcxt);
1486 71968 : bslot->base.tuple = heap_copytuple(tuple);
1487 71968 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
1488 71968 : MemoryContextSwitchTo(oldContext);
1489 :
1490 71968 : if (shouldFree)
1491 70040 : pfree(tuple);
1492 : }
1493 : else
1494 : {
1495 1704838 : ExecClearTuple(slot);
1496 1704838 : heap_deform_tuple(tuple, slot->tts_tupleDescriptor,
1497 : slot->tts_values, slot->tts_isnull);
1498 1704838 : ExecStoreVirtualTuple(slot);
1499 :
1500 1704838 : if (shouldFree)
1501 : {
1502 266692 : ExecMaterializeSlot(slot);
1503 266692 : pfree(tuple);
1504 : }
1505 : }
1506 1777232 : }
1507 :
1508 : /*
1509 : * Store a MinimalTuple into any kind of slot, performing conversion if
1510 : * necessary.
1511 : */
1512 : void
1513 6062396 : ExecForceStoreMinimalTuple(MinimalTuple mtup,
1514 : TupleTableSlot *slot,
1515 : bool shouldFree)
1516 : {
1517 6062396 : if (TTS_IS_MINIMALTUPLE(slot))
1518 : {
1519 3392180 : tts_minimal_store_tuple(slot, mtup, shouldFree);
1520 : }
1521 : else
1522 : {
1523 : HeapTupleData htup;
1524 :
1525 2670216 : ExecClearTuple(slot);
1526 :
1527 2670216 : htup.t_len = mtup->t_len + MINIMAL_TUPLE_OFFSET;
1528 2670216 : htup.t_data = (HeapTupleHeader) ((char *) mtup - MINIMAL_TUPLE_OFFSET);
1529 2670216 : heap_deform_tuple(&htup, slot->tts_tupleDescriptor,
1530 : slot->tts_values, slot->tts_isnull);
1531 2670216 : ExecStoreVirtualTuple(slot);
1532 :
1533 2670216 : if (shouldFree)
1534 : {
1535 1470192 : ExecMaterializeSlot(slot);
1536 1470192 : pfree(mtup);
1537 : }
1538 : }
1539 6062396 : }
1540 :
1541 : /* --------------------------------
1542 : * ExecStoreVirtualTuple
1543 : * Mark a slot as containing a virtual tuple.
1544 : *
1545 : * The protocol for loading a slot with virtual tuple data is:
1546 : * * Call ExecClearTuple to mark the slot empty.
1547 : * * Store data into the Datum/isnull arrays.
1548 : * * Call ExecStoreVirtualTuple to mark the slot valid.
1549 : * This is a bit unclean but it avoids one round of data copying.
1550 : * --------------------------------
1551 : */
1552 : TupleTableSlot *
1553 26205550 : ExecStoreVirtualTuple(TupleTableSlot *slot)
1554 : {
1555 : /*
1556 : * sanity checks
1557 : */
1558 : Assert(slot != NULL);
1559 : Assert(slot->tts_tupleDescriptor != NULL);
1560 : Assert(TTS_EMPTY(slot));
1561 :
1562 26205550 : slot->tts_flags &= ~TTS_FLAG_EMPTY;
1563 26205550 : slot->tts_nvalid = slot->tts_tupleDescriptor->natts;
1564 :
1565 26205550 : return slot;
1566 : }
1567 :
1568 : /* --------------------------------
1569 : * ExecStoreAllNullTuple
1570 : * Set up the slot to contain a null in every column.
1571 : *
1572 : * At first glance this might sound just like ExecClearTuple, but it's
1573 : * entirely different: the slot ends up full, not empty.
1574 : * --------------------------------
1575 : */
1576 : TupleTableSlot *
1577 621870 : ExecStoreAllNullTuple(TupleTableSlot *slot)
1578 : {
1579 : /*
1580 : * sanity checks
1581 : */
1582 : Assert(slot != NULL);
1583 : Assert(slot->tts_tupleDescriptor != NULL);
1584 :
1585 : /* Clear any old contents */
1586 621870 : ExecClearTuple(slot);
1587 :
1588 : /*
1589 : * Fill all the columns of the virtual tuple with nulls
1590 : */
1591 10377650 : MemSet(slot->tts_values, 0,
1592 : slot->tts_tupleDescriptor->natts * sizeof(Datum));
1593 621870 : memset(slot->tts_isnull, true,
1594 621870 : slot->tts_tupleDescriptor->natts * sizeof(bool));
1595 :
1596 621870 : return ExecStoreVirtualTuple(slot);
1597 : }
1598 :
1599 : /*
1600 : * Store a HeapTuple in datum form, into a slot. That always requires
1601 : * deforming it and storing it in virtual form.
1602 : *
1603 : * Until the slot is materialized, the contents of the slot depend on the
1604 : * datum.
1605 : */
1606 : void
1607 6 : ExecStoreHeapTupleDatum(Datum data, TupleTableSlot *slot)
1608 : {
1609 6 : HeapTupleData tuple = {0};
1610 : HeapTupleHeader td;
1611 :
1612 6 : td = DatumGetHeapTupleHeader(data);
1613 :
1614 6 : tuple.t_len = HeapTupleHeaderGetDatumLength(td);
1615 6 : tuple.t_self = td->t_ctid;
1616 6 : tuple.t_data = td;
1617 :
1618 6 : ExecClearTuple(slot);
1619 :
1620 6 : heap_deform_tuple(&tuple, slot->tts_tupleDescriptor,
1621 : slot->tts_values, slot->tts_isnull);
1622 6 : ExecStoreVirtualTuple(slot);
1623 6 : }
1624 :
1625 : /*
1626 : * ExecFetchSlotHeapTuple - fetch HeapTuple representing the slot's content
1627 : *
1628 : * The returned HeapTuple represents the slot's content as closely as
1629 : * possible.
1630 : *
1631 : * If materialize is true, the contents of the slots will be made independent
1632 : * from the underlying storage (i.e. all buffer pins are released, memory is
1633 : * allocated in the slot's context).
1634 : *
1635 : * If shouldFree is not-NULL it'll be set to true if the returned tuple has
1636 : * been allocated in the calling memory context, and must be freed by the
1637 : * caller (via explicit pfree() or a memory context reset).
1638 : *
1639 : * NB: If materialize is true, modifications of the returned tuple are
1640 : * allowed. But it depends on the type of the slot whether such modifications
1641 : * will also affect the slot's contents. While that is not the nicest
1642 : * behaviour, all such modifications are in the process of being removed.
1643 : */
1644 : HeapTuple
1645 55168684 : ExecFetchSlotHeapTuple(TupleTableSlot *slot, bool materialize, bool *shouldFree)
1646 : {
1647 : /*
1648 : * sanity checks
1649 : */
1650 : Assert(slot != NULL);
1651 : Assert(!TTS_EMPTY(slot));
1652 :
1653 : /* Materialize the tuple so that the slot "owns" it, if requested. */
1654 55168684 : if (materialize)
1655 24665276 : slot->tts_ops->materialize(slot);
1656 :
1657 55168684 : if (slot->tts_ops->get_heap_tuple == NULL)
1658 : {
1659 2839976 : if (shouldFree)
1660 2839976 : *shouldFree = true;
1661 2839976 : return slot->tts_ops->copy_heap_tuple(slot);
1662 : }
1663 : else
1664 : {
1665 52328708 : if (shouldFree)
1666 45402652 : *shouldFree = false;
1667 52328708 : return slot->tts_ops->get_heap_tuple(slot);
1668 : }
1669 : }
1670 :
1671 : /* --------------------------------
1672 : * ExecFetchSlotMinimalTuple
1673 : * Fetch the slot's minimal physical tuple.
1674 : *
1675 : * If the given tuple table slot can hold a minimal tuple, indicated by a
1676 : * non-NULL get_minimal_tuple callback, the function returns the minimal
1677 : * tuple returned by that callback. It assumes that the minimal tuple
1678 : * returned by the callback is "owned" by the slot i.e. the slot is
1679 : * responsible for freeing the memory consumed by the tuple. Hence it sets
1680 : * *shouldFree to false, indicating that the caller should not free the
1681 : * memory consumed by the minimal tuple. In this case the returned minimal
1682 : * tuple should be considered as read-only.
1683 : *
1684 : * If that callback is not supported, it calls copy_minimal_tuple callback
1685 : * which is expected to return a copy of minimal tuple representing the
1686 : * contents of the slot. In this case *shouldFree is set to true,
1687 : * indicating the caller that it should free the memory consumed by the
1688 : * minimal tuple. In this case the returned minimal tuple may be written
1689 : * up.
1690 : * --------------------------------
1691 : */
1692 : MinimalTuple
1693 19553046 : ExecFetchSlotMinimalTuple(TupleTableSlot *slot,
1694 : bool *shouldFree)
1695 : {
1696 : /*
1697 : * sanity checks
1698 : */
1699 : Assert(slot != NULL);
1700 : Assert(!TTS_EMPTY(slot));
1701 :
1702 19553046 : if (slot->tts_ops->get_minimal_tuple)
1703 : {
1704 3856224 : if (shouldFree)
1705 3856224 : *shouldFree = false;
1706 3856224 : return slot->tts_ops->get_minimal_tuple(slot);
1707 : }
1708 : else
1709 : {
1710 15696822 : if (shouldFree)
1711 15696822 : *shouldFree = true;
1712 15696822 : return slot->tts_ops->copy_minimal_tuple(slot);
1713 : }
1714 : }
1715 :
1716 : /* --------------------------------
1717 : * ExecFetchSlotHeapTupleDatum
1718 : * Fetch the slot's tuple as a composite-type Datum.
1719 : *
1720 : * The result is always freshly palloc'd in the caller's memory context.
1721 : * --------------------------------
1722 : */
1723 : Datum
1724 60338 : ExecFetchSlotHeapTupleDatum(TupleTableSlot *slot)
1725 : {
1726 : HeapTuple tup;
1727 : TupleDesc tupdesc;
1728 : bool shouldFree;
1729 : Datum ret;
1730 :
1731 : /* Fetch slot's contents in regular-physical-tuple form */
1732 60338 : tup = ExecFetchSlotHeapTuple(slot, false, &shouldFree);
1733 60338 : tupdesc = slot->tts_tupleDescriptor;
1734 :
1735 : /* Convert to Datum form */
1736 60338 : ret = heap_copy_tuple_as_datum(tup, tupdesc);
1737 :
1738 60338 : if (shouldFree)
1739 60338 : pfree(tup);
1740 :
1741 60338 : return ret;
1742 : }
1743 :
1744 : /* ----------------------------------------------------------------
1745 : * convenience initialization routines
1746 : * ----------------------------------------------------------------
1747 : */
1748 :
1749 : /* ----------------
1750 : * ExecInitResultTypeTL
1751 : *
1752 : * Initialize result type, using the plan node's targetlist.
1753 : * ----------------
1754 : */
1755 : void
1756 1120860 : ExecInitResultTypeTL(PlanState *planstate)
1757 : {
1758 1120860 : TupleDesc tupDesc = ExecTypeFromTL(planstate->plan->targetlist);
1759 :
1760 1120860 : planstate->ps_ResultTupleDesc = tupDesc;
1761 1120860 : }
1762 :
1763 : /* --------------------------------
1764 : * ExecInit{Result,Scan,Extra}TupleSlot[TL]
1765 : *
1766 : * These are convenience routines to initialize the specified slot
1767 : * in nodes inheriting the appropriate state. ExecInitExtraTupleSlot
1768 : * is used for initializing special-purpose slots.
1769 : * --------------------------------
1770 : */
1771 :
1772 : /* ----------------
1773 : * ExecInitResultTupleSlotTL
1774 : *
1775 : * Initialize result tuple slot, using the tuple descriptor previously
1776 : * computed with ExecInitResultTypeTL().
1777 : * ----------------
1778 : */
1779 : void
1780 759084 : ExecInitResultSlot(PlanState *planstate, const TupleTableSlotOps *tts_ops)
1781 : {
1782 : TupleTableSlot *slot;
1783 :
1784 759084 : slot = ExecAllocTableSlot(&planstate->state->es_tupleTable,
1785 : planstate->ps_ResultTupleDesc, tts_ops);
1786 759084 : planstate->ps_ResultTupleSlot = slot;
1787 :
1788 759084 : planstate->resultopsfixed = planstate->ps_ResultTupleDesc != NULL;
1789 759084 : planstate->resultops = tts_ops;
1790 759084 : planstate->resultopsset = true;
1791 759084 : }
1792 :
1793 : /* ----------------
1794 : * ExecInitResultTupleSlotTL
1795 : *
1796 : * Initialize result tuple slot, using the plan node's targetlist.
1797 : * ----------------
1798 : */
1799 : void
1800 555992 : ExecInitResultTupleSlotTL(PlanState *planstate,
1801 : const TupleTableSlotOps *tts_ops)
1802 : {
1803 555992 : ExecInitResultTypeTL(planstate);
1804 555992 : ExecInitResultSlot(planstate, tts_ops);
1805 555992 : }
1806 :
1807 : /* ----------------
1808 : * ExecInitScanTupleSlot
1809 : * ----------------
1810 : */
1811 : void
1812 520900 : ExecInitScanTupleSlot(EState *estate, ScanState *scanstate,
1813 : TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
1814 : {
1815 520900 : scanstate->ss_ScanTupleSlot = ExecAllocTableSlot(&estate->es_tupleTable,
1816 : tupledesc, tts_ops);
1817 520900 : scanstate->ps.scandesc = tupledesc;
1818 520900 : scanstate->ps.scanopsfixed = tupledesc != NULL;
1819 520900 : scanstate->ps.scanops = tts_ops;
1820 520900 : scanstate->ps.scanopsset = true;
1821 520900 : }
1822 :
1823 : /* ----------------
1824 : * ExecInitExtraTupleSlot
1825 : *
1826 : * Return a newly created slot. If tupledesc is non-NULL the slot will have
1827 : * that as its fixed tupledesc. Otherwise the caller needs to use
1828 : * ExecSetSlotDescriptor() to set the descriptor before use.
1829 : * ----------------
1830 : */
1831 : TupleTableSlot *
1832 425270 : ExecInitExtraTupleSlot(EState *estate,
1833 : TupleDesc tupledesc,
1834 : const TupleTableSlotOps *tts_ops)
1835 : {
1836 425270 : return ExecAllocTableSlot(&estate->es_tupleTable, tupledesc, tts_ops);
1837 : }
1838 :
1839 : /* ----------------
1840 : * ExecInitNullTupleSlot
1841 : *
1842 : * Build a slot containing an all-nulls tuple of the given type.
1843 : * This is used as a substitute for an input tuple when performing an
1844 : * outer join.
1845 : * ----------------
1846 : */
1847 : TupleTableSlot *
1848 32956 : ExecInitNullTupleSlot(EState *estate, TupleDesc tupType,
1849 : const TupleTableSlotOps *tts_ops)
1850 : {
1851 32956 : TupleTableSlot *slot = ExecInitExtraTupleSlot(estate, tupType, tts_ops);
1852 :
1853 32956 : return ExecStoreAllNullTuple(slot);
1854 : }
1855 :
1856 : /* ---------------------------------------------------------------
1857 : * Routines for setting/accessing attributes in a slot.
1858 : * ---------------------------------------------------------------
1859 : */
1860 :
1861 : /*
1862 : * Fill in missing values for a TupleTableSlot.
1863 : *
1864 : * This is only exposed because it's needed for JIT compiled tuple
1865 : * deforming. That exception aside, there should be no callers outside of this
1866 : * file.
1867 : */
1868 : void
1869 7658 : slot_getmissingattrs(TupleTableSlot *slot, int startAttNum, int lastAttNum)
1870 : {
1871 7658 : AttrMissing *attrmiss = NULL;
1872 :
1873 7658 : if (slot->tts_tupleDescriptor->constr)
1874 4506 : attrmiss = slot->tts_tupleDescriptor->constr->missing;
1875 :
1876 7658 : if (!attrmiss)
1877 : {
1878 : /* no missing values array at all, so just fill everything in as NULL */
1879 3230 : memset(slot->tts_values + startAttNum, 0,
1880 3230 : (lastAttNum - startAttNum) * sizeof(Datum));
1881 3230 : memset(slot->tts_isnull + startAttNum, 1,
1882 3230 : (lastAttNum - startAttNum) * sizeof(bool));
1883 : }
1884 : else
1885 : {
1886 : int missattnum;
1887 :
1888 : /* if there is a missing values array we must process them one by one */
1889 10248 : for (missattnum = startAttNum;
1890 : missattnum < lastAttNum;
1891 5820 : missattnum++)
1892 : {
1893 5820 : slot->tts_values[missattnum] = attrmiss[missattnum].am_value;
1894 5820 : slot->tts_isnull[missattnum] = !attrmiss[missattnum].am_present;
1895 : }
1896 : }
1897 7658 : }
1898 :
1899 : /*
1900 : * slot_getsomeattrs_int - workhorse for slot_getsomeattrs()
1901 : */
1902 : void
1903 205395816 : slot_getsomeattrs_int(TupleTableSlot *slot, int attnum)
1904 : {
1905 : /* Check for caller errors */
1906 : Assert(slot->tts_nvalid < attnum); /* checked in slot_getsomeattrs */
1907 : Assert(attnum > 0);
1908 :
1909 205395816 : if (unlikely(attnum > slot->tts_tupleDescriptor->natts))
1910 0 : elog(ERROR, "invalid attribute number %d", attnum);
1911 :
1912 : /* Fetch as many attributes as possible from the underlying tuple. */
1913 205395816 : slot->tts_ops->getsomeattrs(slot, attnum);
1914 :
1915 : /*
1916 : * If the underlying tuple doesn't have enough attributes, tuple
1917 : * descriptor must have the missing attributes.
1918 : */
1919 205395816 : if (unlikely(slot->tts_nvalid < attnum))
1920 : {
1921 7658 : slot_getmissingattrs(slot, slot->tts_nvalid, attnum);
1922 7658 : slot->tts_nvalid = attnum;
1923 : }
1924 205395816 : }
1925 :
1926 : /* ----------------------------------------------------------------
1927 : * ExecTypeFromTL
1928 : *
1929 : * Generate a tuple descriptor for the result tuple of a targetlist.
1930 : * (A parse/plan tlist must be passed, not an ExprState tlist.)
1931 : * Note that resjunk columns, if any, are included in the result.
1932 : *
1933 : * Currently there are about 4 different places where we create
1934 : * TupleDescriptors. They should all be merged, or perhaps
1935 : * be rewritten to call BuildDesc().
1936 : * ----------------------------------------------------------------
1937 : */
1938 : TupleDesc
1939 1147064 : ExecTypeFromTL(List *targetList)
1940 : {
1941 1147064 : return ExecTypeFromTLInternal(targetList, false);
1942 : }
1943 :
1944 : /* ----------------------------------------------------------------
1945 : * ExecCleanTypeFromTL
1946 : *
1947 : * Same as above, but resjunk columns are omitted from the result.
1948 : * ----------------------------------------------------------------
1949 : */
1950 : TupleDesc
1951 85264 : ExecCleanTypeFromTL(List *targetList)
1952 : {
1953 85264 : return ExecTypeFromTLInternal(targetList, true);
1954 : }
1955 :
1956 : static TupleDesc
1957 1232328 : ExecTypeFromTLInternal(List *targetList, bool skipjunk)
1958 : {
1959 : TupleDesc typeInfo;
1960 : ListCell *l;
1961 : int len;
1962 1232328 : int cur_resno = 1;
1963 :
1964 1232328 : if (skipjunk)
1965 85264 : len = ExecCleanTargetListLength(targetList);
1966 : else
1967 1147064 : len = ExecTargetListLength(targetList);
1968 1232328 : typeInfo = CreateTemplateTupleDesc(len);
1969 :
1970 5797052 : foreach(l, targetList)
1971 : {
1972 4564724 : TargetEntry *tle = lfirst(l);
1973 :
1974 4564724 : if (skipjunk && tle->resjunk)
1975 24766 : continue;
1976 13619874 : TupleDescInitEntry(typeInfo,
1977 : cur_resno,
1978 4539958 : tle->resname,
1979 4539958 : exprType((Node *) tle->expr),
1980 4539958 : exprTypmod((Node *) tle->expr),
1981 : 0);
1982 4539958 : TupleDescInitEntryCollation(typeInfo,
1983 : cur_resno,
1984 4539958 : exprCollation((Node *) tle->expr));
1985 4539958 : cur_resno++;
1986 : }
1987 :
1988 1232328 : return typeInfo;
1989 : }
1990 :
1991 : /*
1992 : * ExecTypeFromExprList - build a tuple descriptor from a list of Exprs
1993 : *
1994 : * This is roughly like ExecTypeFromTL, but we work from bare expressions
1995 : * not TargetEntrys. No names are attached to the tupledesc's columns.
1996 : */
1997 : TupleDesc
1998 11652 : ExecTypeFromExprList(List *exprList)
1999 : {
2000 : TupleDesc typeInfo;
2001 : ListCell *lc;
2002 11652 : int cur_resno = 1;
2003 :
2004 11652 : typeInfo = CreateTemplateTupleDesc(list_length(exprList));
2005 :
2006 32298 : foreach(lc, exprList)
2007 : {
2008 20646 : Node *e = lfirst(lc);
2009 :
2010 20646 : TupleDescInitEntry(typeInfo,
2011 : cur_resno,
2012 : NULL,
2013 : exprType(e),
2014 : exprTypmod(e),
2015 : 0);
2016 20646 : TupleDescInitEntryCollation(typeInfo,
2017 : cur_resno,
2018 : exprCollation(e));
2019 20646 : cur_resno++;
2020 : }
2021 :
2022 11652 : return typeInfo;
2023 : }
2024 :
2025 : /*
2026 : * ExecTypeSetColNames - set column names in a RECORD TupleDesc
2027 : *
2028 : * Column names must be provided as an alias list (list of String nodes).
2029 : */
2030 : void
2031 3494 : ExecTypeSetColNames(TupleDesc typeInfo, List *namesList)
2032 : {
2033 3494 : int colno = 0;
2034 : ListCell *lc;
2035 :
2036 : /* It's only OK to change col names in a not-yet-blessed RECORD type */
2037 : Assert(typeInfo->tdtypeid == RECORDOID);
2038 : Assert(typeInfo->tdtypmod < 0);
2039 :
2040 12240 : foreach(lc, namesList)
2041 : {
2042 8746 : char *cname = strVal(lfirst(lc));
2043 : Form_pg_attribute attr;
2044 :
2045 : /* Guard against too-long names list (probably can't happen) */
2046 8746 : if (colno >= typeInfo->natts)
2047 0 : break;
2048 8746 : attr = TupleDescAttr(typeInfo, colno);
2049 8746 : colno++;
2050 :
2051 : /*
2052 : * Do nothing for empty aliases or dropped columns (these cases
2053 : * probably can't arise in RECORD types, either)
2054 : */
2055 8746 : if (cname[0] == '\0' || attr->attisdropped)
2056 26 : continue;
2057 :
2058 : /* OK, assign the column name */
2059 8720 : namestrcpy(&(attr->attname), cname);
2060 : }
2061 3494 : }
2062 :
2063 : /*
2064 : * BlessTupleDesc - make a completed tuple descriptor useful for SRFs
2065 : *
2066 : * Rowtype Datums returned by a function must contain valid type information.
2067 : * This happens "for free" if the tupdesc came from a relcache entry, but
2068 : * not if we have manufactured a tupdesc for a transient RECORD datatype.
2069 : * In that case we have to notify typcache.c of the existence of the type.
2070 : */
2071 : TupleDesc
2072 62074 : BlessTupleDesc(TupleDesc tupdesc)
2073 : {
2074 62074 : if (tupdesc->tdtypeid == RECORDOID &&
2075 59134 : tupdesc->tdtypmod < 0)
2076 25614 : assign_record_type_typmod(tupdesc);
2077 :
2078 62074 : return tupdesc; /* just for notational convenience */
2079 : }
2080 :
2081 : /*
2082 : * TupleDescGetAttInMetadata - Build an AttInMetadata structure based on the
2083 : * supplied TupleDesc. AttInMetadata can be used in conjunction with C strings
2084 : * to produce a properly formed tuple.
2085 : */
2086 : AttInMetadata *
2087 5754 : TupleDescGetAttInMetadata(TupleDesc tupdesc)
2088 : {
2089 5754 : int natts = tupdesc->natts;
2090 : int i;
2091 : Oid atttypeid;
2092 : Oid attinfuncid;
2093 : FmgrInfo *attinfuncinfo;
2094 : Oid *attioparams;
2095 : int32 *atttypmods;
2096 : AttInMetadata *attinmeta;
2097 :
2098 5754 : attinmeta = (AttInMetadata *) palloc(sizeof(AttInMetadata));
2099 :
2100 : /* "Bless" the tupledesc so that we can make rowtype datums with it */
2101 5754 : attinmeta->tupdesc = BlessTupleDesc(tupdesc);
2102 :
2103 : /*
2104 : * Gather info needed later to call the "in" function for each attribute
2105 : */
2106 5754 : attinfuncinfo = (FmgrInfo *) palloc0(natts * sizeof(FmgrInfo));
2107 5754 : attioparams = (Oid *) palloc0(natts * sizeof(Oid));
2108 5754 : atttypmods = (int32 *) palloc0(natts * sizeof(int32));
2109 :
2110 53928 : for (i = 0; i < natts; i++)
2111 : {
2112 48174 : Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2113 :
2114 : /* Ignore dropped attributes */
2115 48174 : if (!att->attisdropped)
2116 : {
2117 47948 : atttypeid = att->atttypid;
2118 47948 : getTypeInputInfo(atttypeid, &attinfuncid, &attioparams[i]);
2119 47948 : fmgr_info(attinfuncid, &attinfuncinfo[i]);
2120 47948 : atttypmods[i] = att->atttypmod;
2121 : }
2122 : }
2123 5754 : attinmeta->attinfuncs = attinfuncinfo;
2124 5754 : attinmeta->attioparams = attioparams;
2125 5754 : attinmeta->atttypmods = atttypmods;
2126 :
2127 5754 : return attinmeta;
2128 : }
2129 :
2130 : /*
2131 : * BuildTupleFromCStrings - build a HeapTuple given user data in C string form.
2132 : * values is an array of C strings, one for each attribute of the return tuple.
2133 : * A NULL string pointer indicates we want to create a NULL field.
2134 : */
2135 : HeapTuple
2136 829424 : BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
2137 : {
2138 829424 : TupleDesc tupdesc = attinmeta->tupdesc;
2139 829424 : int natts = tupdesc->natts;
2140 : Datum *dvalues;
2141 : bool *nulls;
2142 : int i;
2143 : HeapTuple tuple;
2144 :
2145 829424 : dvalues = (Datum *) palloc(natts * sizeof(Datum));
2146 829424 : nulls = (bool *) palloc(natts * sizeof(bool));
2147 :
2148 : /*
2149 : * Call the "in" function for each non-dropped attribute, even for nulls,
2150 : * to support domains.
2151 : */
2152 14669382 : for (i = 0; i < natts; i++)
2153 : {
2154 13839960 : if (!TupleDescAttr(tupdesc, i)->attisdropped)
2155 : {
2156 : /* Non-dropped attributes */
2157 27679918 : dvalues[i] = InputFunctionCall(&attinmeta->attinfuncs[i],
2158 13839960 : values[i],
2159 13839960 : attinmeta->attioparams[i],
2160 13839960 : attinmeta->atttypmods[i]);
2161 13839958 : if (values[i] != NULL)
2162 9359270 : nulls[i] = false;
2163 : else
2164 4480688 : nulls[i] = true;
2165 : }
2166 : else
2167 : {
2168 : /* Handle dropped attributes by setting to NULL */
2169 0 : dvalues[i] = (Datum) 0;
2170 0 : nulls[i] = true;
2171 : }
2172 : }
2173 :
2174 : /*
2175 : * Form a tuple
2176 : */
2177 829422 : tuple = heap_form_tuple(tupdesc, dvalues, nulls);
2178 :
2179 : /*
2180 : * Release locally palloc'd space. XXX would probably be good to pfree
2181 : * values of pass-by-reference datums, as well.
2182 : */
2183 829422 : pfree(dvalues);
2184 829422 : pfree(nulls);
2185 :
2186 829422 : return tuple;
2187 : }
2188 :
2189 : /*
2190 : * HeapTupleHeaderGetDatum - convert a HeapTupleHeader pointer to a Datum.
2191 : *
2192 : * This must *not* get applied to an on-disk tuple; the tuple should be
2193 : * freshly made by heap_form_tuple or some wrapper routine for it (such as
2194 : * BuildTupleFromCStrings). Be sure also that the tupledesc used to build
2195 : * the tuple has a properly "blessed" rowtype.
2196 : *
2197 : * Formerly this was a macro equivalent to PointerGetDatum, relying on the
2198 : * fact that heap_form_tuple fills in the appropriate tuple header fields
2199 : * for a composite Datum. However, we now require that composite Datums not
2200 : * contain any external TOAST pointers. We do not want heap_form_tuple itself
2201 : * to enforce that; more specifically, the rule applies only to actual Datums
2202 : * and not to HeapTuple structures. Therefore, HeapTupleHeaderGetDatum is
2203 : * now a function that detects whether there are externally-toasted fields
2204 : * and constructs a new tuple with inlined fields if so. We still need
2205 : * heap_form_tuple to insert the Datum header fields, because otherwise this
2206 : * code would have no way to obtain a tupledesc for the tuple.
2207 : *
2208 : * Note that if we do build a new tuple, it's palloc'd in the current
2209 : * memory context. Beware of code that changes context between the initial
2210 : * heap_form_tuple/etc call and calling HeapTuple(Header)GetDatum.
2211 : *
2212 : * For performance-critical callers, it could be worthwhile to take extra
2213 : * steps to ensure that there aren't TOAST pointers in the output of
2214 : * heap_form_tuple to begin with. It's likely however that the costs of the
2215 : * typcache lookup and tuple disassembly/reassembly are swamped by TOAST
2216 : * dereference costs, so that the benefits of such extra effort would be
2217 : * minimal.
2218 : *
2219 : * XXX it would likely be better to create wrapper functions that produce
2220 : * a composite Datum from the field values in one step. However, there's
2221 : * enough code using the existing APIs that we couldn't get rid of this
2222 : * hack anytime soon.
2223 : */
2224 : Datum
2225 994906 : HeapTupleHeaderGetDatum(HeapTupleHeader tuple)
2226 : {
2227 : Datum result;
2228 : TupleDesc tupDesc;
2229 :
2230 : /* No work if there are no external TOAST pointers in the tuple */
2231 994906 : if (!HeapTupleHeaderHasExternal(tuple))
2232 994894 : return PointerGetDatum(tuple);
2233 :
2234 : /* Use the type data saved by heap_form_tuple to look up the rowtype */
2235 12 : tupDesc = lookup_rowtype_tupdesc(HeapTupleHeaderGetTypeId(tuple),
2236 : HeapTupleHeaderGetTypMod(tuple));
2237 :
2238 : /* And do the flattening */
2239 12 : result = toast_flatten_tuple_to_datum(tuple,
2240 12 : HeapTupleHeaderGetDatumLength(tuple),
2241 : tupDesc);
2242 :
2243 12 : ReleaseTupleDesc(tupDesc);
2244 :
2245 12 : return result;
2246 : }
2247 :
2248 :
2249 : /*
2250 : * Functions for sending tuples to the frontend (or other specified destination)
2251 : * as though it is a SELECT result. These are used by utility commands that
2252 : * need to project directly to the destination and don't need or want full
2253 : * table function capability. Currently used by EXPLAIN and SHOW ALL.
2254 : */
2255 : TupOutputState *
2256 24096 : begin_tup_output_tupdesc(DestReceiver *dest,
2257 : TupleDesc tupdesc,
2258 : const TupleTableSlotOps *tts_ops)
2259 : {
2260 : TupOutputState *tstate;
2261 :
2262 24096 : tstate = (TupOutputState *) palloc(sizeof(TupOutputState));
2263 :
2264 24096 : tstate->slot = MakeSingleTupleTableSlot(tupdesc, tts_ops);
2265 24096 : tstate->dest = dest;
2266 :
2267 24096 : tstate->dest->rStartup(tstate->dest, (int) CMD_SELECT, tupdesc);
2268 :
2269 24096 : return tstate;
2270 : }
2271 :
2272 : /*
2273 : * write a single tuple
2274 : */
2275 : void
2276 129070 : do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull)
2277 : {
2278 129070 : TupleTableSlot *slot = tstate->slot;
2279 129070 : int natts = slot->tts_tupleDescriptor->natts;
2280 :
2281 : /* make sure the slot is clear */
2282 129070 : ExecClearTuple(slot);
2283 :
2284 : /* insert data */
2285 129070 : memcpy(slot->tts_values, values, natts * sizeof(Datum));
2286 129070 : memcpy(slot->tts_isnull, isnull, natts * sizeof(bool));
2287 :
2288 : /* mark slot as containing a virtual tuple */
2289 129070 : ExecStoreVirtualTuple(slot);
2290 :
2291 : /* send the tuple to the receiver */
2292 129070 : (void) tstate->dest->receiveSlot(slot, tstate->dest);
2293 :
2294 : /* clean up */
2295 129070 : ExecClearTuple(slot);
2296 129070 : }
2297 :
2298 : /*
2299 : * write a chunk of text, breaking at newline characters
2300 : *
2301 : * Should only be used with a single-TEXT-attribute tupdesc.
2302 : */
2303 : void
2304 19598 : do_text_output_multiline(TupOutputState *tstate, const char *txt)
2305 : {
2306 : Datum values[1];
2307 19598 : bool isnull[1] = {false};
2308 :
2309 142824 : while (*txt)
2310 : {
2311 : const char *eol;
2312 : int len;
2313 :
2314 123226 : eol = strchr(txt, '\n');
2315 123226 : if (eol)
2316 : {
2317 123226 : len = eol - txt;
2318 123226 : eol++;
2319 : }
2320 : else
2321 : {
2322 0 : len = strlen(txt);
2323 0 : eol = txt + len;
2324 : }
2325 :
2326 123226 : values[0] = PointerGetDatum(cstring_to_text_with_len(txt, len));
2327 123226 : do_tup_output(tstate, values, isnull);
2328 123226 : pfree(DatumGetPointer(values[0]));
2329 123226 : txt = eol;
2330 : }
2331 19598 : }
2332 :
2333 : void
2334 24096 : end_tup_output(TupOutputState *tstate)
2335 : {
2336 24096 : tstate->dest->rShutdown(tstate->dest);
2337 : /* note that destroying the dest is not ours to do */
2338 24096 : ExecDropSingleTupleTableSlot(tstate->slot);
2339 24096 : pfree(tstate);
2340 24096 : }
|