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-2026, 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 "access/xact.h"
64 : #include "catalog/pg_type.h"
65 : #include "funcapi.h"
66 : #include "nodes/nodeFuncs.h"
67 : #include "storage/bufmgr.h"
68 : #include "utils/builtins.h"
69 : #include "utils/expandeddatum.h"
70 : #include "utils/lsyscache.h"
71 : #include "utils/typcache.h"
72 :
73 : static TupleDesc ExecTypeFromTLInternal(List *targetList,
74 : bool skipjunk);
75 : static pg_attribute_always_inline void slot_deform_heap_tuple(TupleTableSlot *slot, HeapTuple tuple, uint32 *offp,
76 : int reqnatts);
77 : static inline void tts_buffer_heap_store_tuple(TupleTableSlot *slot,
78 : HeapTuple tuple,
79 : Buffer buffer,
80 : bool transfer_pin);
81 : static void tts_heap_store_tuple(TupleTableSlot *slot, HeapTuple tuple, bool shouldFree);
82 :
83 :
84 : const TupleTableSlotOps TTSOpsVirtual;
85 : const TupleTableSlotOps TTSOpsHeapTuple;
86 : const TupleTableSlotOps TTSOpsMinimalTuple;
87 : const TupleTableSlotOps TTSOpsBufferHeapTuple;
88 :
89 :
90 : /*
91 : * TupleTableSlotOps implementations.
92 : */
93 :
94 : /*
95 : * TupleTableSlotOps implementation for VirtualTupleTableSlot.
96 : */
97 : static void
98 1657522 : tts_virtual_init(TupleTableSlot *slot)
99 : {
100 1657522 : }
101 :
102 : static void
103 1637799 : tts_virtual_release(TupleTableSlot *slot)
104 : {
105 1637799 : }
106 :
107 : static void
108 59252278 : tts_virtual_clear(TupleTableSlot *slot)
109 : {
110 59252278 : if (unlikely(TTS_SHOULDFREE(slot)))
111 : {
112 1206298 : VirtualTupleTableSlot *vslot = (VirtualTupleTableSlot *) slot;
113 :
114 1206298 : pfree(vslot->data);
115 1206298 : vslot->data = NULL;
116 :
117 1206298 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
118 : }
119 :
120 59252278 : slot->tts_nvalid = 0;
121 59252278 : slot->tts_flags |= TTS_FLAG_EMPTY;
122 59252278 : ItemPointerSetInvalid(&slot->tts_tid);
123 59252278 : }
124 :
125 : /*
126 : * VirtualTupleTableSlots always have fully populated tts_values and
127 : * tts_isnull arrays. So this function should never be called.
128 : */
129 : static void
130 0 : tts_virtual_getsomeattrs(TupleTableSlot *slot, int natts)
131 : {
132 0 : elog(ERROR, "getsomeattrs is not required to be called on a virtual tuple table slot");
133 : }
134 :
135 : /*
136 : * VirtualTupleTableSlots never provide system attributes (except those
137 : * handled generically, such as tableoid). We generally shouldn't get
138 : * here, but provide a user-friendly message if we do.
139 : */
140 : static Datum
141 8 : tts_virtual_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
142 : {
143 : Assert(!TTS_EMPTY(slot));
144 :
145 8 : ereport(ERROR,
146 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
147 : errmsg("cannot retrieve a system column in this context")));
148 :
149 : return 0; /* silence compiler warnings */
150 : }
151 :
152 : /*
153 : * VirtualTupleTableSlots never have storage tuples. We generally
154 : * shouldn't get here, but provide a user-friendly message if we do.
155 : */
156 : static bool
157 0 : tts_virtual_is_current_xact_tuple(TupleTableSlot *slot)
158 : {
159 : Assert(!TTS_EMPTY(slot));
160 :
161 0 : ereport(ERROR,
162 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
163 : errmsg("don't have transaction information for this type of tuple")));
164 :
165 : return false; /* silence compiler warnings */
166 : }
167 :
168 : /*
169 : * To materialize a virtual slot all the datums that aren't passed by value
170 : * have to be copied into the slot's memory context. To do so, compute the
171 : * required size, and allocate enough memory to store all attributes. That's
172 : * good for cache hit ratio, but more importantly requires only memory
173 : * allocation/deallocation.
174 : */
175 : static void
176 2841260 : tts_virtual_materialize(TupleTableSlot *slot)
177 : {
178 2841260 : VirtualTupleTableSlot *vslot = (VirtualTupleTableSlot *) slot;
179 2841260 : TupleDesc desc = slot->tts_tupleDescriptor;
180 2841260 : Size sz = 0;
181 : char *data;
182 :
183 : /* already materialized */
184 2841260 : if (TTS_SHOULDFREE(slot))
185 232135 : return;
186 :
187 : /* compute size of memory required */
188 8285676 : for (int natt = 0; natt < desc->natts; natt++)
189 : {
190 5676551 : CompactAttribute *att = TupleDescCompactAttr(desc, natt);
191 : Datum val;
192 :
193 5676551 : if (att->attbyval || slot->tts_isnull[natt])
194 4208753 : continue;
195 :
196 1467798 : val = slot->tts_values[natt];
197 :
198 2586098 : if (att->attlen == -1 &&
199 1118300 : VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(val)))
200 : {
201 : /*
202 : * We want to flatten the expanded value so that the materialized
203 : * slot doesn't depend on it.
204 : */
205 0 : sz = att_nominal_alignby(sz, att->attalignby);
206 0 : sz += EOH_get_flat_size(DatumGetEOHP(val));
207 : }
208 : else
209 : {
210 1467798 : sz = att_nominal_alignby(sz, att->attalignby);
211 1467798 : sz = att_addlength_datum(sz, att->attlen, val);
212 : }
213 : }
214 :
215 : /* all data is byval */
216 2609125 : if (sz == 0)
217 1402748 : return;
218 :
219 : /* allocate memory */
220 1206377 : vslot->data = data = MemoryContextAlloc(slot->tts_mcxt, sz);
221 1206377 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
222 :
223 : /* and copy all attributes into the pre-allocated space */
224 4687471 : for (int natt = 0; natt < desc->natts; natt++)
225 : {
226 3481094 : CompactAttribute *att = TupleDescCompactAttr(desc, natt);
227 : Datum val;
228 :
229 3481094 : if (att->attbyval || slot->tts_isnull[natt])
230 2013296 : continue;
231 :
232 1467798 : val = slot->tts_values[natt];
233 :
234 2586098 : if (att->attlen == -1 &&
235 1118300 : VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(val)))
236 0 : {
237 : Size data_length;
238 :
239 : /*
240 : * We want to flatten the expanded value so that the materialized
241 : * slot doesn't depend on it.
242 : */
243 0 : ExpandedObjectHeader *eoh = DatumGetEOHP(val);
244 :
245 0 : data = (char *) att_nominal_alignby(data,
246 : att->attalignby);
247 0 : data_length = EOH_get_flat_size(eoh);
248 0 : EOH_flatten_into(eoh, data, data_length);
249 :
250 0 : slot->tts_values[natt] = PointerGetDatum(data);
251 0 : data += data_length;
252 : }
253 : else
254 : {
255 1467798 : Size data_length = 0;
256 :
257 1467798 : data = (char *) att_nominal_alignby(data, att->attalignby);
258 1467798 : data_length = att_addlength_datum(data_length, att->attlen, val);
259 :
260 1467798 : memcpy(data, DatumGetPointer(val), data_length);
261 :
262 1467798 : slot->tts_values[natt] = PointerGetDatum(data);
263 1467798 : data += data_length;
264 : }
265 : }
266 : }
267 :
268 : static void
269 89793 : tts_virtual_copyslot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
270 : {
271 89793 : TupleDesc srcdesc = srcslot->tts_tupleDescriptor;
272 :
273 89793 : tts_virtual_clear(dstslot);
274 :
275 89793 : slot_getallattrs(srcslot);
276 :
277 184349 : for (int natt = 0; natt < srcdesc->natts; natt++)
278 : {
279 94556 : dstslot->tts_values[natt] = srcslot->tts_values[natt];
280 94556 : dstslot->tts_isnull[natt] = srcslot->tts_isnull[natt];
281 : }
282 :
283 89793 : dstslot->tts_nvalid = srcdesc->natts;
284 89793 : dstslot->tts_flags &= ~TTS_FLAG_EMPTY;
285 :
286 : /* make sure storage doesn't depend on external memory */
287 89793 : tts_virtual_materialize(dstslot);
288 89793 : }
289 :
290 : static HeapTuple
291 9340267 : tts_virtual_copy_heap_tuple(TupleTableSlot *slot)
292 : {
293 : Assert(!TTS_EMPTY(slot));
294 :
295 18680534 : return heap_form_tuple(slot->tts_tupleDescriptor,
296 9340267 : slot->tts_values,
297 9340267 : slot->tts_isnull);
298 : }
299 :
300 : static MinimalTuple
301 18952440 : tts_virtual_copy_minimal_tuple(TupleTableSlot *slot, Size extra)
302 : {
303 : Assert(!TTS_EMPTY(slot));
304 :
305 37904880 : return heap_form_minimal_tuple(slot->tts_tupleDescriptor,
306 18952440 : slot->tts_values,
307 18952440 : slot->tts_isnull,
308 : extra);
309 : }
310 :
311 :
312 : /*
313 : * TupleTableSlotOps implementation for HeapTupleTableSlot.
314 : */
315 :
316 : static void
317 2464894 : tts_heap_init(TupleTableSlot *slot)
318 : {
319 2464894 : }
320 :
321 : static void
322 2464255 : tts_heap_release(TupleTableSlot *slot)
323 : {
324 2464255 : }
325 :
326 : static void
327 6307966 : tts_heap_clear(TupleTableSlot *slot)
328 : {
329 6307966 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
330 :
331 : /* Free the memory for the heap tuple if it's allowed. */
332 6307966 : if (TTS_SHOULDFREE(slot))
333 : {
334 1058245 : heap_freetuple(hslot->tuple);
335 1058245 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
336 : }
337 :
338 6307966 : slot->tts_nvalid = 0;
339 6307966 : slot->tts_flags |= TTS_FLAG_EMPTY;
340 6307966 : ItemPointerSetInvalid(&slot->tts_tid);
341 6307966 : hslot->off = 0;
342 6307966 : hslot->tuple = NULL;
343 6307966 : }
344 :
345 : static void
346 6369464 : tts_heap_getsomeattrs(TupleTableSlot *slot, int natts)
347 : {
348 6369464 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
349 :
350 : Assert(!TTS_EMPTY(slot));
351 :
352 6369464 : slot_deform_heap_tuple(slot, hslot->tuple, &hslot->off, natts);
353 6369464 : }
354 :
355 : static Datum
356 0 : tts_heap_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
357 : {
358 0 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
359 :
360 : Assert(!TTS_EMPTY(slot));
361 :
362 : /*
363 : * In some code paths it's possible to get here with a non-materialized
364 : * slot, in which case we can't retrieve system columns.
365 : */
366 0 : if (!hslot->tuple)
367 0 : ereport(ERROR,
368 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
369 : errmsg("cannot retrieve a system column in this context")));
370 :
371 0 : return heap_getsysattr(hslot->tuple, attnum,
372 : slot->tts_tupleDescriptor, isnull);
373 : }
374 :
375 : static bool
376 0 : tts_heap_is_current_xact_tuple(TupleTableSlot *slot)
377 : {
378 0 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
379 : TransactionId xmin;
380 :
381 : Assert(!TTS_EMPTY(slot));
382 :
383 : /*
384 : * In some code paths it's possible to get here with a non-materialized
385 : * slot, in which case we can't check if tuple is created by the current
386 : * transaction.
387 : */
388 0 : if (!hslot->tuple)
389 0 : ereport(ERROR,
390 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
391 : errmsg("don't have a storage tuple in this context")));
392 :
393 0 : xmin = HeapTupleHeaderGetRawXmin(hslot->tuple->t_data);
394 :
395 0 : return TransactionIdIsCurrentTransactionId(xmin);
396 : }
397 :
398 : static void
399 2115235 : tts_heap_materialize(TupleTableSlot *slot)
400 : {
401 2115235 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
402 : MemoryContext oldContext;
403 :
404 : Assert(!TTS_EMPTY(slot));
405 :
406 : /* If slot has its tuple already materialized, nothing to do. */
407 2115235 : if (TTS_SHOULDFREE(slot))
408 1057979 : return;
409 :
410 1057256 : oldContext = MemoryContextSwitchTo(slot->tts_mcxt);
411 :
412 : /*
413 : * Have to deform from scratch, otherwise tts_values[] entries could point
414 : * into the non-materialized tuple (which might be gone when accessed).
415 : */
416 1057256 : slot->tts_nvalid = 0;
417 1057256 : hslot->off = 0;
418 :
419 1057256 : if (!hslot->tuple)
420 1057249 : hslot->tuple = heap_form_tuple(slot->tts_tupleDescriptor,
421 1057249 : slot->tts_values,
422 1057249 : slot->tts_isnull);
423 : else
424 : {
425 : /*
426 : * The tuple contained in this slot is not allocated in the memory
427 : * context of the given slot (else it would have TTS_FLAG_SHOULDFREE
428 : * set). Copy the tuple into the given slot's memory context.
429 : */
430 7 : hslot->tuple = heap_copytuple(hslot->tuple);
431 : }
432 :
433 1057256 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
434 :
435 1057256 : MemoryContextSwitchTo(oldContext);
436 : }
437 :
438 : static void
439 900 : tts_heap_copyslot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
440 : {
441 : HeapTuple tuple;
442 : MemoryContext oldcontext;
443 :
444 900 : oldcontext = MemoryContextSwitchTo(dstslot->tts_mcxt);
445 900 : tuple = ExecCopySlotHeapTuple(srcslot);
446 900 : MemoryContextSwitchTo(oldcontext);
447 :
448 900 : ExecStoreHeapTuple(tuple, dstslot, true);
449 900 : }
450 :
451 : static HeapTuple
452 2114229 : tts_heap_get_heap_tuple(TupleTableSlot *slot)
453 : {
454 2114229 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
455 :
456 : Assert(!TTS_EMPTY(slot));
457 2114229 : if (!hslot->tuple)
458 0 : tts_heap_materialize(slot);
459 :
460 2114229 : return hslot->tuple;
461 : }
462 :
463 : static HeapTuple
464 344 : tts_heap_copy_heap_tuple(TupleTableSlot *slot)
465 : {
466 344 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
467 :
468 : Assert(!TTS_EMPTY(slot));
469 344 : if (!hslot->tuple)
470 0 : tts_heap_materialize(slot);
471 :
472 344 : return heap_copytuple(hslot->tuple);
473 : }
474 :
475 : static MinimalTuple
476 2718 : tts_heap_copy_minimal_tuple(TupleTableSlot *slot, Size extra)
477 : {
478 2718 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
479 :
480 2718 : if (!hslot->tuple)
481 21 : tts_heap_materialize(slot);
482 :
483 2718 : return minimal_tuple_from_heap_tuple(hslot->tuple, extra);
484 : }
485 :
486 : static void
487 2784891 : tts_heap_store_tuple(TupleTableSlot *slot, HeapTuple tuple, bool shouldFree)
488 : {
489 2784891 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
490 :
491 2784891 : tts_heap_clear(slot);
492 :
493 2784891 : slot->tts_nvalid = 0;
494 2784891 : hslot->tuple = tuple;
495 2784891 : hslot->off = 0;
496 2784891 : slot->tts_flags &= ~(TTS_FLAG_EMPTY | TTS_FLAG_SHOULDFREE);
497 2784891 : slot->tts_tid = tuple->t_self;
498 :
499 2784891 : if (shouldFree)
500 1000 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
501 2784891 : }
502 :
503 :
504 : /*
505 : * TupleTableSlotOps implementation for MinimalTupleTableSlot.
506 : */
507 :
508 : static void
509 260088 : tts_minimal_init(TupleTableSlot *slot)
510 : {
511 260088 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
512 :
513 : /*
514 : * Initialize the heap tuple pointer to access attributes of the minimal
515 : * tuple contained in the slot as if its a heap tuple.
516 : */
517 260088 : mslot->tuple = &mslot->minhdr;
518 260088 : }
519 :
520 : static void
521 226983 : tts_minimal_release(TupleTableSlot *slot)
522 : {
523 226983 : }
524 :
525 : static void
526 51110383 : tts_minimal_clear(TupleTableSlot *slot)
527 : {
528 51110383 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
529 :
530 51110383 : if (TTS_SHOULDFREE(slot))
531 : {
532 8232501 : heap_free_minimal_tuple(mslot->mintuple);
533 8232501 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
534 : }
535 :
536 51110383 : slot->tts_nvalid = 0;
537 51110383 : slot->tts_flags |= TTS_FLAG_EMPTY;
538 51110383 : ItemPointerSetInvalid(&slot->tts_tid);
539 51110383 : mslot->off = 0;
540 51110383 : mslot->mintuple = NULL;
541 51110383 : }
542 :
543 : static void
544 37440451 : tts_minimal_getsomeattrs(TupleTableSlot *slot, int natts)
545 : {
546 37440451 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
547 :
548 : Assert(!TTS_EMPTY(slot));
549 :
550 37440451 : slot_deform_heap_tuple(slot, mslot->tuple, &mslot->off, natts);
551 37440451 : }
552 :
553 : /*
554 : * MinimalTupleTableSlots never provide system attributes. We generally
555 : * shouldn't get here, but provide a user-friendly message if we do.
556 : */
557 : static Datum
558 0 : tts_minimal_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
559 : {
560 : Assert(!TTS_EMPTY(slot));
561 :
562 0 : ereport(ERROR,
563 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
564 : errmsg("cannot retrieve a system column in this context")));
565 :
566 : return 0; /* silence compiler warnings */
567 : }
568 :
569 : /*
570 : * Within MinimalTuple abstraction transaction information is unavailable.
571 : * We generally shouldn't get here, but provide a user-friendly message if
572 : * we do.
573 : */
574 : static bool
575 0 : tts_minimal_is_current_xact_tuple(TupleTableSlot *slot)
576 : {
577 : Assert(!TTS_EMPTY(slot));
578 :
579 0 : ereport(ERROR,
580 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
581 : errmsg("don't have transaction information for this type of tuple")));
582 :
583 : return false; /* silence compiler warnings */
584 : }
585 :
586 : static void
587 1008626 : tts_minimal_materialize(TupleTableSlot *slot)
588 : {
589 1008626 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
590 : MemoryContext oldContext;
591 :
592 : Assert(!TTS_EMPTY(slot));
593 :
594 : /* If slot has its tuple already materialized, nothing to do. */
595 1008626 : if (TTS_SHOULDFREE(slot))
596 96017 : return;
597 :
598 912609 : oldContext = MemoryContextSwitchTo(slot->tts_mcxt);
599 :
600 : /*
601 : * Have to deform from scratch, otherwise tts_values[] entries could point
602 : * into the non-materialized tuple (which might be gone when accessed).
603 : */
604 912609 : slot->tts_nvalid = 0;
605 912609 : mslot->off = 0;
606 :
607 912609 : if (!mslot->mintuple)
608 : {
609 856951 : mslot->mintuple = heap_form_minimal_tuple(slot->tts_tupleDescriptor,
610 856951 : slot->tts_values,
611 856951 : slot->tts_isnull,
612 : 0);
613 : }
614 : else
615 : {
616 : /*
617 : * The minimal tuple contained in this slot is not allocated in the
618 : * memory context of the given slot (else it would have
619 : * TTS_FLAG_SHOULDFREE set). Copy the minimal tuple into the given
620 : * slot's memory context.
621 : */
622 55658 : mslot->mintuple = heap_copy_minimal_tuple(mslot->mintuple, 0);
623 : }
624 :
625 912609 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
626 :
627 : Assert(mslot->tuple == &mslot->minhdr);
628 :
629 912609 : mslot->minhdr.t_len = mslot->mintuple->t_len + MINIMAL_TUPLE_OFFSET;
630 912609 : mslot->minhdr.t_data = (HeapTupleHeader) ((char *) mslot->mintuple - MINIMAL_TUPLE_OFFSET);
631 :
632 912609 : MemoryContextSwitchTo(oldContext);
633 : }
634 :
635 : static void
636 769194 : tts_minimal_copyslot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
637 : {
638 : MemoryContext oldcontext;
639 : MinimalTuple mintuple;
640 :
641 769194 : oldcontext = MemoryContextSwitchTo(dstslot->tts_mcxt);
642 769194 : mintuple = ExecCopySlotMinimalTuple(srcslot);
643 769194 : MemoryContextSwitchTo(oldcontext);
644 :
645 769194 : ExecStoreMinimalTuple(mintuple, dstslot, true);
646 769194 : }
647 :
648 : static MinimalTuple
649 3356995 : tts_minimal_get_minimal_tuple(TupleTableSlot *slot)
650 : {
651 3356995 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
652 :
653 3356995 : if (!mslot->mintuple)
654 0 : tts_minimal_materialize(slot);
655 :
656 3356995 : return mslot->mintuple;
657 : }
658 :
659 : static HeapTuple
660 491346 : tts_minimal_copy_heap_tuple(TupleTableSlot *slot)
661 : {
662 491346 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
663 :
664 491346 : if (!mslot->mintuple)
665 998 : tts_minimal_materialize(slot);
666 :
667 491346 : return heap_tuple_from_minimal_tuple(mslot->mintuple);
668 : }
669 :
670 : static MinimalTuple
671 1850105 : tts_minimal_copy_minimal_tuple(TupleTableSlot *slot, Size extra)
672 : {
673 1850105 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
674 :
675 1850105 : if (!mslot->mintuple)
676 732339 : tts_minimal_materialize(slot);
677 :
678 1850105 : return heap_copy_minimal_tuple(mslot->mintuple, extra);
679 : }
680 :
681 : static void
682 42710090 : tts_minimal_store_tuple(TupleTableSlot *slot, MinimalTuple mtup, bool shouldFree)
683 : {
684 42710090 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
685 :
686 42710090 : tts_minimal_clear(slot);
687 :
688 : Assert(!TTS_SHOULDFREE(slot));
689 : Assert(TTS_EMPTY(slot));
690 :
691 42710090 : slot->tts_flags &= ~TTS_FLAG_EMPTY;
692 42710090 : slot->tts_nvalid = 0;
693 42710090 : mslot->off = 0;
694 :
695 42710090 : mslot->mintuple = mtup;
696 : Assert(mslot->tuple == &mslot->minhdr);
697 42710090 : mslot->minhdr.t_len = mtup->t_len + MINIMAL_TUPLE_OFFSET;
698 42710090 : mslot->minhdr.t_data = (HeapTupleHeader) ((char *) mtup - MINIMAL_TUPLE_OFFSET);
699 : /* no need to set t_self or t_tableOid since we won't allow access */
700 :
701 42710090 : if (shouldFree)
702 7320922 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
703 42710090 : }
704 :
705 :
706 : /*
707 : * TupleTableSlotOps implementation for BufferHeapTupleTableSlot.
708 : */
709 :
710 : static void
711 18691793 : tts_buffer_heap_init(TupleTableSlot *slot)
712 : {
713 18691793 : }
714 :
715 : static void
716 18682242 : tts_buffer_heap_release(TupleTableSlot *slot)
717 : {
718 18682242 : }
719 :
720 : static void
721 33826739 : tts_buffer_heap_clear(TupleTableSlot *slot)
722 : {
723 33826739 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
724 :
725 : /*
726 : * Free the memory for heap tuple if allowed. A tuple coming from buffer
727 : * can never be freed. But we may have materialized a tuple from buffer.
728 : * Such a tuple can be freed.
729 : */
730 33826739 : if (TTS_SHOULDFREE(slot))
731 : {
732 : /* We should have unpinned the buffer while materializing the tuple. */
733 : Assert(!BufferIsValid(bslot->buffer));
734 :
735 8425957 : heap_freetuple(bslot->base.tuple);
736 8425957 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
737 : }
738 :
739 33826739 : if (BufferIsValid(bslot->buffer))
740 10880376 : ReleaseBuffer(bslot->buffer);
741 :
742 33826739 : slot->tts_nvalid = 0;
743 33826739 : slot->tts_flags |= TTS_FLAG_EMPTY;
744 33826739 : ItemPointerSetInvalid(&slot->tts_tid);
745 33826739 : bslot->base.tuple = NULL;
746 33826739 : bslot->base.off = 0;
747 33826739 : bslot->buffer = InvalidBuffer;
748 33826739 : }
749 :
750 : static void
751 90622345 : tts_buffer_heap_getsomeattrs(TupleTableSlot *slot, int natts)
752 : {
753 90622345 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
754 :
755 : Assert(!TTS_EMPTY(slot));
756 :
757 90622345 : slot_deform_heap_tuple(slot, bslot->base.tuple, &bslot->base.off, natts);
758 90622345 : }
759 :
760 : static Datum
761 72787 : tts_buffer_heap_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
762 : {
763 72787 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
764 :
765 : Assert(!TTS_EMPTY(slot));
766 :
767 : /*
768 : * In some code paths it's possible to get here with a non-materialized
769 : * slot, in which case we can't retrieve system columns.
770 : */
771 72787 : if (!bslot->base.tuple)
772 0 : ereport(ERROR,
773 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
774 : errmsg("cannot retrieve a system column in this context")));
775 :
776 72787 : return heap_getsysattr(bslot->base.tuple, attnum,
777 : slot->tts_tupleDescriptor, isnull);
778 : }
779 :
780 : static bool
781 564 : tts_buffer_is_current_xact_tuple(TupleTableSlot *slot)
782 : {
783 564 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
784 : TransactionId xmin;
785 :
786 : Assert(!TTS_EMPTY(slot));
787 :
788 : /*
789 : * In some code paths it's possible to get here with a non-materialized
790 : * slot, in which case we can't check if tuple is created by the current
791 : * transaction.
792 : */
793 564 : if (!bslot->base.tuple)
794 0 : ereport(ERROR,
795 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
796 : errmsg("don't have a storage tuple in this context")));
797 :
798 564 : xmin = HeapTupleHeaderGetRawXmin(bslot->base.tuple->t_data);
799 :
800 564 : return TransactionIdIsCurrentTransactionId(xmin);
801 : }
802 :
803 : static void
804 16699409 : tts_buffer_heap_materialize(TupleTableSlot *slot)
805 : {
806 16699409 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
807 : MemoryContext oldContext;
808 :
809 : Assert(!TTS_EMPTY(slot));
810 :
811 : /* If slot has its tuple already materialized, nothing to do. */
812 16699409 : if (TTS_SHOULDFREE(slot))
813 15122717 : return;
814 :
815 1576692 : oldContext = MemoryContextSwitchTo(slot->tts_mcxt);
816 :
817 : /*
818 : * Have to deform from scratch, otherwise tts_values[] entries could point
819 : * into the non-materialized tuple (which might be gone when accessed).
820 : */
821 1576692 : bslot->base.off = 0;
822 1576692 : slot->tts_nvalid = 0;
823 :
824 1576692 : if (!bslot->base.tuple)
825 : {
826 : /*
827 : * Normally BufferHeapTupleTableSlot should have a tuple + buffer
828 : * associated with it, unless it's materialized (which would've
829 : * returned above). But when it's useful to allow storing virtual
830 : * tuples in a buffer slot, which then also needs to be
831 : * materializable.
832 : */
833 1338434 : bslot->base.tuple = heap_form_tuple(slot->tts_tupleDescriptor,
834 1338434 : slot->tts_values,
835 1338434 : slot->tts_isnull);
836 : }
837 : else
838 : {
839 238258 : bslot->base.tuple = heap_copytuple(bslot->base.tuple);
840 :
841 : /*
842 : * A heap tuple stored in a BufferHeapTupleTableSlot should have a
843 : * buffer associated with it, unless it's materialized or virtual.
844 : */
845 238258 : if (likely(BufferIsValid(bslot->buffer)))
846 238258 : ReleaseBuffer(bslot->buffer);
847 238258 : bslot->buffer = InvalidBuffer;
848 : }
849 :
850 : /*
851 : * We don't set TTS_FLAG_SHOULDFREE until after releasing the buffer, if
852 : * any. This avoids having a transient state that would fall foul of our
853 : * assertions that a slot with TTS_FLAG_SHOULDFREE doesn't own a buffer.
854 : * In the unlikely event that ReleaseBuffer() above errors out, we'd
855 : * effectively leak the copied tuple, but that seems fairly harmless.
856 : */
857 1576692 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
858 :
859 1576692 : MemoryContextSwitchTo(oldContext);
860 : }
861 :
862 : static void
863 7047218 : tts_buffer_heap_copyslot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
864 : {
865 7047218 : BufferHeapTupleTableSlot *bsrcslot = (BufferHeapTupleTableSlot *) srcslot;
866 7047218 : BufferHeapTupleTableSlot *bdstslot = (BufferHeapTupleTableSlot *) dstslot;
867 :
868 : /*
869 : * If the source slot is of a different kind, or is a buffer slot that has
870 : * been materialized / is virtual, make a new copy of the tuple. Otherwise
871 : * make a new reference to the in-buffer tuple.
872 : */
873 7047218 : if (dstslot->tts_ops != srcslot->tts_ops ||
874 4294 : TTS_SHOULDFREE(srcslot) ||
875 4292 : !bsrcslot->base.tuple)
876 7042926 : {
877 : MemoryContext oldContext;
878 :
879 7042926 : ExecClearTuple(dstslot);
880 7042926 : dstslot->tts_flags &= ~TTS_FLAG_EMPTY;
881 7042926 : oldContext = MemoryContextSwitchTo(dstslot->tts_mcxt);
882 7042926 : bdstslot->base.tuple = ExecCopySlotHeapTuple(srcslot);
883 7042926 : dstslot->tts_flags |= TTS_FLAG_SHOULDFREE;
884 7042926 : MemoryContextSwitchTo(oldContext);
885 : }
886 : else
887 : {
888 : Assert(BufferIsValid(bsrcslot->buffer));
889 :
890 4292 : tts_buffer_heap_store_tuple(dstslot, bsrcslot->base.tuple,
891 : bsrcslot->buffer, false);
892 :
893 : /*
894 : * The HeapTupleData portion of the source tuple might be shorter
895 : * lived than the destination slot. Therefore copy the HeapTuple into
896 : * our slot's tupdata, which is guaranteed to live long enough (but
897 : * will still point into the buffer).
898 : */
899 4292 : memcpy(&bdstslot->base.tupdata, bdstslot->base.tuple, sizeof(HeapTupleData));
900 4292 : bdstslot->base.tuple = &bdstslot->base.tupdata;
901 : }
902 7047218 : }
903 :
904 : static HeapTuple
905 24770280 : tts_buffer_heap_get_heap_tuple(TupleTableSlot *slot)
906 : {
907 24770280 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
908 :
909 : Assert(!TTS_EMPTY(slot));
910 :
911 24770280 : if (!bslot->base.tuple)
912 0 : tts_buffer_heap_materialize(slot);
913 :
914 24770280 : return bslot->base.tuple;
915 : }
916 :
917 : static HeapTuple
918 7046005 : tts_buffer_heap_copy_heap_tuple(TupleTableSlot *slot)
919 : {
920 7046005 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
921 :
922 : Assert(!TTS_EMPTY(slot));
923 :
924 7046005 : if (!bslot->base.tuple)
925 0 : tts_buffer_heap_materialize(slot);
926 :
927 7046005 : return heap_copytuple(bslot->base.tuple);
928 : }
929 :
930 : static MinimalTuple
931 1868797 : tts_buffer_heap_copy_minimal_tuple(TupleTableSlot *slot, Size extra)
932 : {
933 1868797 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
934 :
935 : Assert(!TTS_EMPTY(slot));
936 :
937 1868797 : if (!bslot->base.tuple)
938 0 : tts_buffer_heap_materialize(slot);
939 :
940 1868797 : return minimal_tuple_from_heap_tuple(bslot->base.tuple, extra);
941 : }
942 :
943 : static inline void
944 106699431 : tts_buffer_heap_store_tuple(TupleTableSlot *slot, HeapTuple tuple,
945 : Buffer buffer, bool transfer_pin)
946 : {
947 106699431 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
948 :
949 106699431 : if (TTS_SHOULDFREE(slot))
950 : {
951 : /* materialized slot shouldn't have a buffer to release */
952 : Assert(!BufferIsValid(bslot->buffer));
953 :
954 239403 : heap_freetuple(bslot->base.tuple);
955 239403 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
956 : }
957 :
958 106699431 : slot->tts_flags &= ~TTS_FLAG_EMPTY;
959 106699431 : slot->tts_nvalid = 0;
960 106699431 : bslot->base.tuple = tuple;
961 106699431 : bslot->base.off = 0;
962 106699431 : slot->tts_tid = tuple->t_self;
963 :
964 : /*
965 : * If tuple is on a disk page, keep the page pinned as long as we hold a
966 : * pointer into it. We assume the caller already has such a pin. If
967 : * transfer_pin is true, we'll transfer that pin to this slot, if not
968 : * we'll pin it again ourselves.
969 : *
970 : * This is coded to optimize the case where the slot previously held a
971 : * tuple on the same disk page: in that case releasing and re-acquiring
972 : * the pin is a waste of cycles. This is a common situation during
973 : * seqscans, so it's worth troubling over.
974 : */
975 106699431 : if (bslot->buffer != buffer)
976 : {
977 14562603 : if (BufferIsValid(bslot->buffer))
978 3438664 : ReleaseBuffer(bslot->buffer);
979 :
980 14562603 : bslot->buffer = buffer;
981 :
982 14562603 : if (!transfer_pin && BufferIsValid(buffer))
983 13577950 : IncrBufferRefCount(buffer);
984 : }
985 92136828 : else if (transfer_pin && BufferIsValid(buffer))
986 : {
987 : /*
988 : * In transfer_pin mode the caller won't know about the same-page
989 : * optimization, so we gotta release its pin.
990 : */
991 169326 : ReleaseBuffer(buffer);
992 : }
993 106699431 : }
994 :
995 : /*
996 : * slot_deform_heap_tuple
997 : * Given a TupleTableSlot, extract data from the slot's physical tuple
998 : * into its Datum/isnull arrays. Data is extracted up through the
999 : * reqnatts'th column. If there are insufficient attributes in the given
1000 : * tuple, then slot_getmissingattrs() is called to populate the
1001 : * remainder. If reqnatts is above the number of attributes in the
1002 : * slot's TupleDesc, an error is raised.
1003 : *
1004 : * This is essentially an incremental version of heap_deform_tuple:
1005 : * on each call we extract attributes up to the one needed, without
1006 : * re-computing information about previously extracted attributes.
1007 : * slot->tts_nvalid is the number of attributes already extracted.
1008 : *
1009 : * This is marked as always inline, so the different offp for different types
1010 : * of slots gets optimized away.
1011 : */
1012 : static pg_attribute_always_inline void
1013 134432260 : slot_deform_heap_tuple(TupleTableSlot *slot, HeapTuple tuple, uint32 *offp,
1014 : int reqnatts)
1015 : {
1016 : CompactAttribute *cattrs;
1017 : CompactAttribute *cattr;
1018 134432260 : TupleDesc tupleDesc = slot->tts_tupleDescriptor;
1019 134432260 : HeapTupleHeader tup = tuple->t_data;
1020 : size_t attnum;
1021 : int firstNonCacheOffsetAttr;
1022 : int firstNonGuaranteedAttr;
1023 : int firstNullAttr;
1024 : int natts;
1025 : Datum *values;
1026 : bool *isnull;
1027 : char *tp; /* ptr to tuple data */
1028 : uint32 off; /* offset in tuple data */
1029 :
1030 : /* Did someone forget to call TupleDescFinalize()? */
1031 : Assert(tupleDesc->firstNonCachedOffsetAttr >= 0);
1032 :
1033 134432260 : isnull = slot->tts_isnull;
1034 :
1035 : /*
1036 : * Some callers may form and deform tuples prior to NOT NULL constraints
1037 : * being checked. Here we'd like to optimize the case where we only need
1038 : * to fetch attributes before or up to the point where the attribute is
1039 : * guaranteed to exist in the tuple. We rely on the slot flag being set
1040 : * correctly to only enable this optimization when it's valid to do so.
1041 : * This optimization allows us to save fetching the number of attributes
1042 : * from the tuple and saves the additional cost of handling non-byval
1043 : * attrs.
1044 : */
1045 134432260 : firstNonGuaranteedAttr = Min(reqnatts, slot->tts_first_nonguaranteed);
1046 :
1047 134432260 : firstNonCacheOffsetAttr = tupleDesc->firstNonCachedOffsetAttr;
1048 :
1049 134432260 : if (HeapTupleHasNulls(tuple))
1050 : {
1051 33723573 : natts = HeapTupleHeaderGetNatts(tup);
1052 33723573 : tp = (char *) tup + MAXALIGN(offsetof(HeapTupleHeaderData, t_bits) +
1053 : BITMAPLEN(natts));
1054 :
1055 33723573 : natts = Min(natts, reqnatts);
1056 33723573 : if (natts > firstNonGuaranteedAttr)
1057 : {
1058 31785679 : bits8 *bp = tup->t_bits;
1059 :
1060 : /* Find the first NULL attr */
1061 31785679 : firstNullAttr = first_null_attr(bp, natts);
1062 :
1063 : /*
1064 : * And populate the isnull array for all attributes being fetched
1065 : * from the tuple.
1066 : */
1067 31785679 : populate_isnull_array(bp, natts, isnull);
1068 : }
1069 : else
1070 : {
1071 : /* Otherwise all required columns are guaranteed to exist */
1072 1937894 : firstNullAttr = natts;
1073 : }
1074 : }
1075 : else
1076 : {
1077 100708687 : tp = (char *) tup + MAXALIGN(offsetof(HeapTupleHeaderData, t_bits));
1078 :
1079 : /*
1080 : * We only need to look at the tuple's natts if we need more than the
1081 : * guaranteed number of columns
1082 : */
1083 100708687 : if (reqnatts > firstNonGuaranteedAttr)
1084 90870394 : natts = Min(HeapTupleHeaderGetNatts(tup), reqnatts);
1085 : else
1086 : {
1087 : /* No need to access the number of attributes in the tuple */
1088 9838293 : natts = reqnatts;
1089 : }
1090 :
1091 : /* All attrs can be fetched without checking for NULLs */
1092 100708687 : firstNullAttr = natts;
1093 : }
1094 :
1095 134432260 : attnum = slot->tts_nvalid;
1096 134432260 : values = slot->tts_values;
1097 134432260 : slot->tts_nvalid = reqnatts;
1098 :
1099 : /*
1100 : * We store the tupleDesc's CompactAttribute array in 'cattrs' as gcc
1101 : * seems to be unwilling to optimize accessing the CompactAttribute
1102 : * element efficiently when accessing it via TupleDescCompactAttr().
1103 : */
1104 134432260 : cattrs = tupleDesc->compact_attrs;
1105 :
1106 : /* Ensure we calculated tp correctly */
1107 : Assert(tp == (char *) tup + tup->t_hoff);
1108 :
1109 134432260 : if (attnum < firstNonGuaranteedAttr)
1110 : {
1111 : int attlen;
1112 :
1113 : do
1114 : {
1115 52627909 : isnull[attnum] = false;
1116 52627909 : cattr = &cattrs[attnum];
1117 52627909 : attlen = cattr->attlen;
1118 :
1119 : /* We don't expect any non-byval types */
1120 52627909 : pg_assume(attlen > 0);
1121 : Assert(cattr->attbyval == true);
1122 :
1123 52627909 : off = cattr->attcacheoff;
1124 52627909 : values[attnum] = fetch_att_noerr(tp + off, true, attlen);
1125 52627909 : attnum++;
1126 52627909 : } while (attnum < firstNonGuaranteedAttr);
1127 :
1128 33298395 : off += attlen;
1129 :
1130 33298395 : if (attnum == reqnatts)
1131 11776187 : goto done;
1132 : }
1133 : else
1134 : {
1135 : /*
1136 : * We may be incrementally deforming the tuple, so set 'off' to the
1137 : * previously cached value. This may be 0, if the slot has just
1138 : * received a new tuple.
1139 : */
1140 101133865 : off = *offp;
1141 :
1142 : /* We expect *offp to be set to 0 when attnum == 0 */
1143 : Assert(off == 0 || attnum > 0);
1144 : }
1145 :
1146 : /* We can use attcacheoff up until the first NULL */
1147 122656073 : firstNonCacheOffsetAttr = Min(firstNonCacheOffsetAttr, firstNullAttr);
1148 :
1149 : /*
1150 : * Handle the portion of the tuple that we have cached the offset for up
1151 : * to the first NULL attribute. The offset is effectively fixed for
1152 : * these, so we can use the CompactAttribute's attcacheoff.
1153 : */
1154 122656073 : if (attnum < firstNonCacheOffsetAttr)
1155 : {
1156 : int attlen;
1157 :
1158 : do
1159 : {
1160 358964710 : isnull[attnum] = false;
1161 358964710 : cattr = &cattrs[attnum];
1162 358964710 : attlen = cattr->attlen;
1163 358964710 : off = cattr->attcacheoff;
1164 717929420 : values[attnum] = fetch_att_noerr(tp + off,
1165 358964710 : cattr->attbyval,
1166 : attlen);
1167 358964710 : attnum++;
1168 358964710 : } while (attnum < firstNonCacheOffsetAttr);
1169 :
1170 : /*
1171 : * Point the offset after the end of the last attribute with a cached
1172 : * offset. We expect the final cached offset attribute to have a
1173 : * fixed width, so just add the attlen to the attcacheoff
1174 : */
1175 : Assert(attlen > 0);
1176 106126141 : off += attlen;
1177 : }
1178 :
1179 : /*
1180 : * Handle any portion of the tuple that doesn't have a fixed offset up
1181 : * until the first NULL attribute. This loop only differs from the one
1182 : * after it by the NULL checks.
1183 : */
1184 158057621 : for (; attnum < firstNullAttr; attnum++)
1185 : {
1186 : int attlen;
1187 :
1188 35401548 : isnull[attnum] = false;
1189 35401548 : cattr = &cattrs[attnum];
1190 35401548 : attlen = cattr->attlen;
1191 :
1192 : /*
1193 : * cstrings don't exist in heap tuples. Use pg_assume to instruct the
1194 : * compiler not to emit the cstring-related code in
1195 : * align_fetch_then_add().
1196 : */
1197 35401548 : pg_assume(attlen > 0 || attlen == -1);
1198 :
1199 : /* align 'off', fetch the datum, and increment off beyond the datum */
1200 35401548 : values[attnum] = align_fetch_then_add(tp,
1201 : &off,
1202 35401548 : cattr->attbyval,
1203 : attlen,
1204 35401548 : cattr->attalignby);
1205 : }
1206 :
1207 : /*
1208 : * Now handle any remaining attributes in the tuple up to the requested
1209 : * attnum. This time, include NULL checks as we're now at the first NULL
1210 : * attribute.
1211 : */
1212 170998568 : for (; attnum < natts; attnum++)
1213 : {
1214 : int attlen;
1215 :
1216 48342495 : if (isnull[attnum])
1217 : {
1218 33911363 : values[attnum] = (Datum) 0;
1219 33911363 : continue;
1220 : }
1221 :
1222 14431132 : cattr = &cattrs[attnum];
1223 14431132 : attlen = cattr->attlen;
1224 :
1225 : /* As above, we don't expect cstrings */
1226 14431132 : pg_assume(attlen > 0 || attlen == -1);
1227 :
1228 : /* align 'off', fetch the datum, and increment off beyond the datum */
1229 14431132 : values[attnum] = align_fetch_then_add(tp,
1230 : &off,
1231 14431132 : cattr->attbyval,
1232 : attlen,
1233 14431132 : cattr->attalignby);
1234 : }
1235 :
1236 : /* Fetch any missing attrs and raise an error if reqnatts is invalid */
1237 122656073 : if (unlikely(attnum < reqnatts))
1238 : {
1239 : /*
1240 : * Cache the offset before calling the function to allow the compiler
1241 : * to implement a tail-call optimization
1242 : */
1243 4831 : *offp = off;
1244 4831 : slot_getmissingattrs(slot, attnum, reqnatts);
1245 4831 : return;
1246 : }
1247 122651242 : done:
1248 :
1249 : /* Save current offset for next execution */
1250 134427429 : *offp = off;
1251 : }
1252 :
1253 : const TupleTableSlotOps TTSOpsVirtual = {
1254 : .base_slot_size = sizeof(VirtualTupleTableSlot),
1255 : .init = tts_virtual_init,
1256 : .release = tts_virtual_release,
1257 : .clear = tts_virtual_clear,
1258 : .getsomeattrs = tts_virtual_getsomeattrs,
1259 : .getsysattr = tts_virtual_getsysattr,
1260 : .materialize = tts_virtual_materialize,
1261 : .is_current_xact_tuple = tts_virtual_is_current_xact_tuple,
1262 : .copyslot = tts_virtual_copyslot,
1263 :
1264 : /*
1265 : * A virtual tuple table slot can not "own" a heap tuple or a minimal
1266 : * tuple.
1267 : */
1268 : .get_heap_tuple = NULL,
1269 : .get_minimal_tuple = NULL,
1270 : .copy_heap_tuple = tts_virtual_copy_heap_tuple,
1271 : .copy_minimal_tuple = tts_virtual_copy_minimal_tuple
1272 : };
1273 :
1274 : const TupleTableSlotOps TTSOpsHeapTuple = {
1275 : .base_slot_size = sizeof(HeapTupleTableSlot),
1276 : .init = tts_heap_init,
1277 : .release = tts_heap_release,
1278 : .clear = tts_heap_clear,
1279 : .getsomeattrs = tts_heap_getsomeattrs,
1280 : .getsysattr = tts_heap_getsysattr,
1281 : .is_current_xact_tuple = tts_heap_is_current_xact_tuple,
1282 : .materialize = tts_heap_materialize,
1283 : .copyslot = tts_heap_copyslot,
1284 : .get_heap_tuple = tts_heap_get_heap_tuple,
1285 :
1286 : /* A heap tuple table slot can not "own" a minimal tuple. */
1287 : .get_minimal_tuple = NULL,
1288 : .copy_heap_tuple = tts_heap_copy_heap_tuple,
1289 : .copy_minimal_tuple = tts_heap_copy_minimal_tuple
1290 : };
1291 :
1292 : const TupleTableSlotOps TTSOpsMinimalTuple = {
1293 : .base_slot_size = sizeof(MinimalTupleTableSlot),
1294 : .init = tts_minimal_init,
1295 : .release = tts_minimal_release,
1296 : .clear = tts_minimal_clear,
1297 : .getsomeattrs = tts_minimal_getsomeattrs,
1298 : .getsysattr = tts_minimal_getsysattr,
1299 : .is_current_xact_tuple = tts_minimal_is_current_xact_tuple,
1300 : .materialize = tts_minimal_materialize,
1301 : .copyslot = tts_minimal_copyslot,
1302 :
1303 : /* A minimal tuple table slot can not "own" a heap tuple. */
1304 : .get_heap_tuple = NULL,
1305 : .get_minimal_tuple = tts_minimal_get_minimal_tuple,
1306 : .copy_heap_tuple = tts_minimal_copy_heap_tuple,
1307 : .copy_minimal_tuple = tts_minimal_copy_minimal_tuple
1308 : };
1309 :
1310 : const TupleTableSlotOps TTSOpsBufferHeapTuple = {
1311 : .base_slot_size = sizeof(BufferHeapTupleTableSlot),
1312 : .init = tts_buffer_heap_init,
1313 : .release = tts_buffer_heap_release,
1314 : .clear = tts_buffer_heap_clear,
1315 : .getsomeattrs = tts_buffer_heap_getsomeattrs,
1316 : .getsysattr = tts_buffer_heap_getsysattr,
1317 : .is_current_xact_tuple = tts_buffer_is_current_xact_tuple,
1318 : .materialize = tts_buffer_heap_materialize,
1319 : .copyslot = tts_buffer_heap_copyslot,
1320 : .get_heap_tuple = tts_buffer_heap_get_heap_tuple,
1321 :
1322 : /* A buffer heap tuple table slot can not "own" a minimal tuple. */
1323 : .get_minimal_tuple = NULL,
1324 : .copy_heap_tuple = tts_buffer_heap_copy_heap_tuple,
1325 : .copy_minimal_tuple = tts_buffer_heap_copy_minimal_tuple
1326 : };
1327 :
1328 :
1329 : /* ----------------------------------------------------------------
1330 : * tuple table create/delete functions
1331 : * ----------------------------------------------------------------
1332 : */
1333 :
1334 : /* --------------------------------
1335 : * MakeTupleTableSlot
1336 : *
1337 : * Basic routine to make an empty TupleTableSlot of given
1338 : * TupleTableSlotType. If tupleDesc is specified the slot's descriptor is
1339 : * fixed for its lifetime, gaining some efficiency. If that's
1340 : * undesirable, pass NULL. 'flags' allows any of non-TTS_FLAGS_TRANSIENT
1341 : * flags to be set in tts_flags.
1342 : * --------------------------------
1343 : */
1344 : TupleTableSlot *
1345 23074297 : MakeTupleTableSlot(TupleDesc tupleDesc,
1346 : const TupleTableSlotOps *tts_ops, uint16 flags)
1347 : {
1348 : Size basesz,
1349 : allocsz;
1350 : TupleTableSlot *slot;
1351 :
1352 23074297 : basesz = tts_ops->base_slot_size;
1353 :
1354 : /* Ensure callers don't have any way to set transient flags permanently */
1355 23074297 : flags &= ~TTS_FLAGS_TRANSIENT;
1356 :
1357 : /*
1358 : * When a fixed descriptor is specified, we can reduce overhead by
1359 : * allocating the entire slot in one go.
1360 : *
1361 : * We round the size of tts_isnull up to the next highest multiple of 8.
1362 : * This is needed as populate_isnull_array() operates on 8 elements at a
1363 : * time when converting a tuple's NULL bitmap into a boolean array.
1364 : */
1365 23074297 : if (tupleDesc)
1366 22635641 : allocsz = MAXALIGN(basesz) +
1367 22635641 : MAXALIGN(tupleDesc->natts * sizeof(Datum)) +
1368 22635641 : TYPEALIGN(8, tupleDesc->natts * sizeof(bool));
1369 : else
1370 438656 : allocsz = basesz;
1371 :
1372 23074297 : slot = palloc0(allocsz);
1373 : /* const for optimization purposes, OK to modify at allocation time */
1374 23074297 : *((const TupleTableSlotOps **) &slot->tts_ops) = tts_ops;
1375 23074297 : slot->type = T_TupleTableSlot;
1376 23074297 : slot->tts_flags = TTS_FLAG_EMPTY | flags;
1377 23074297 : if (tupleDesc != NULL)
1378 22635641 : slot->tts_flags |= TTS_FLAG_FIXED;
1379 23074297 : slot->tts_tupleDescriptor = tupleDesc;
1380 23074297 : slot->tts_mcxt = CurrentMemoryContext;
1381 23074297 : slot->tts_nvalid = 0;
1382 :
1383 23074297 : if (tupleDesc != NULL)
1384 : {
1385 22635641 : slot->tts_values = (Datum *)
1386 : (((char *) slot)
1387 22635641 : + MAXALIGN(basesz));
1388 :
1389 22635641 : slot->tts_isnull = (bool *)
1390 : (((char *) slot)
1391 22635641 : + MAXALIGN(basesz)
1392 22635641 : + MAXALIGN(tupleDesc->natts * sizeof(Datum)));
1393 :
1394 22635641 : PinTupleDesc(tupleDesc);
1395 :
1396 : /*
1397 : * Precalculate the maximum guaranteed attribute that has to exist in
1398 : * every tuple which gets deformed into this slot. When the
1399 : * TTS_FLAG_OBEYS_NOT_NULL_CONSTRAINTS flag is enabled, we simply take
1400 : * the precalculated value from the tupleDesc, otherwise the
1401 : * optimization is disabled, and we set the value to 0.
1402 : */
1403 22635641 : if ((flags & TTS_FLAG_OBEYS_NOT_NULL_CONSTRAINTS) != 0)
1404 688193 : slot->tts_first_nonguaranteed = tupleDesc->firstNonGuaranteedAttr;
1405 : else
1406 21947448 : slot->tts_first_nonguaranteed = 0;
1407 : }
1408 :
1409 : /*
1410 : * And allow slot type specific initialization.
1411 : */
1412 23074297 : slot->tts_ops->init(slot);
1413 :
1414 23074297 : return slot;
1415 : }
1416 :
1417 : /* --------------------------------
1418 : * ExecAllocTableSlot
1419 : *
1420 : * Create a tuple table slot within a tuple table (which is just a List).
1421 : * --------------------------------
1422 : */
1423 : TupleTableSlot *
1424 2546412 : ExecAllocTableSlot(List **tupleTable, TupleDesc desc,
1425 : const TupleTableSlotOps *tts_ops, uint16 flags)
1426 : {
1427 2546412 : TupleTableSlot *slot = MakeTupleTableSlot(desc, tts_ops, flags);
1428 :
1429 2546412 : *tupleTable = lappend(*tupleTable, slot);
1430 :
1431 2546412 : return slot;
1432 : }
1433 :
1434 : /* --------------------------------
1435 : * ExecResetTupleTable
1436 : *
1437 : * This releases any resources (buffer pins, tupdesc refcounts)
1438 : * held by the tuple table, and optionally releases the memory
1439 : * occupied by the tuple table data structure.
1440 : * It is expected that this routine be called by ExecEndPlan().
1441 : * --------------------------------
1442 : */
1443 : void
1444 1301207 : ExecResetTupleTable(List *tupleTable, /* tuple table */
1445 : bool shouldFree) /* true if we should free memory */
1446 : {
1447 : ListCell *lc;
1448 :
1449 4371921 : foreach(lc, tupleTable)
1450 : {
1451 3070714 : TupleTableSlot *slot = lfirst_node(TupleTableSlot, lc);
1452 :
1453 : /* Always release resources and reset the slot to empty */
1454 3070714 : ExecClearTuple(slot);
1455 3070714 : slot->tts_ops->release(slot);
1456 3070714 : if (slot->tts_tupleDescriptor)
1457 : {
1458 3070678 : ReleaseTupleDesc(slot->tts_tupleDescriptor);
1459 3070678 : slot->tts_tupleDescriptor = NULL;
1460 : }
1461 :
1462 : /* If shouldFree, release memory occupied by the slot itself */
1463 3070714 : if (shouldFree)
1464 : {
1465 406470 : if (!TTS_FIXED(slot))
1466 : {
1467 0 : if (slot->tts_values)
1468 0 : pfree(slot->tts_values);
1469 0 : if (slot->tts_isnull)
1470 0 : pfree(slot->tts_isnull);
1471 : }
1472 406470 : pfree(slot);
1473 : }
1474 : }
1475 :
1476 : /* If shouldFree, release the list structure */
1477 1301207 : if (shouldFree)
1478 406384 : list_free(tupleTable);
1479 1301207 : }
1480 :
1481 : /* --------------------------------
1482 : * MakeSingleTupleTableSlot
1483 : *
1484 : * This is a convenience routine for operations that need a standalone
1485 : * TupleTableSlot not gotten from the main executor tuple table. It makes
1486 : * a single slot of given TupleTableSlotType and initializes it to use the
1487 : * given tuple descriptor.
1488 : * --------------------------------
1489 : */
1490 : TupleTableSlot *
1491 20527769 : MakeSingleTupleTableSlot(TupleDesc tupdesc,
1492 : const TupleTableSlotOps *tts_ops)
1493 : {
1494 20527769 : TupleTableSlot *slot = MakeTupleTableSlot(tupdesc, tts_ops, 0);
1495 :
1496 20527769 : return slot;
1497 : }
1498 :
1499 : /* --------------------------------
1500 : * ExecDropSingleTupleTableSlot
1501 : *
1502 : * Release a TupleTableSlot made with MakeSingleTupleTableSlot.
1503 : * DON'T use this on a slot that's part of a tuple table list!
1504 : * --------------------------------
1505 : */
1506 : void
1507 19940565 : ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
1508 : {
1509 : /* This should match ExecResetTupleTable's processing of one slot */
1510 : Assert(IsA(slot, TupleTableSlot));
1511 19940565 : ExecClearTuple(slot);
1512 19940565 : slot->tts_ops->release(slot);
1513 19940565 : if (slot->tts_tupleDescriptor)
1514 19940565 : ReleaseTupleDesc(slot->tts_tupleDescriptor);
1515 19940565 : if (!TTS_FIXED(slot))
1516 : {
1517 0 : if (slot->tts_values)
1518 0 : pfree(slot->tts_values);
1519 0 : if (slot->tts_isnull)
1520 0 : pfree(slot->tts_isnull);
1521 : }
1522 19940565 : pfree(slot);
1523 19940565 : }
1524 :
1525 :
1526 : /* ----------------------------------------------------------------
1527 : * tuple table slot accessor functions
1528 : * ----------------------------------------------------------------
1529 : */
1530 :
1531 : /* --------------------------------
1532 : * ExecSetSlotDescriptor
1533 : *
1534 : * This function is used to set the tuple descriptor associated
1535 : * with the slot's tuple. The passed descriptor must have lifespan
1536 : * at least equal to the slot's. If it is a reference-counted descriptor
1537 : * then the reference count is incremented for as long as the slot holds
1538 : * a reference.
1539 : * --------------------------------
1540 : */
1541 : void
1542 438620 : ExecSetSlotDescriptor(TupleTableSlot *slot, /* slot to change */
1543 : TupleDesc tupdesc) /* new tuple descriptor */
1544 : {
1545 : Assert(!TTS_FIXED(slot));
1546 :
1547 : /* For safety, make sure slot is empty before changing it */
1548 438620 : ExecClearTuple(slot);
1549 :
1550 : /*
1551 : * Release any old descriptor. Also release old Datum/isnull arrays if
1552 : * present (we don't bother to check if they could be re-used).
1553 : */
1554 438620 : if (slot->tts_tupleDescriptor)
1555 0 : ReleaseTupleDesc(slot->tts_tupleDescriptor);
1556 :
1557 438620 : if (slot->tts_values)
1558 0 : pfree(slot->tts_values);
1559 438620 : if (slot->tts_isnull)
1560 0 : pfree(slot->tts_isnull);
1561 :
1562 : /*
1563 : * Install the new descriptor; if it's refcounted, bump its refcount.
1564 : */
1565 438620 : slot->tts_tupleDescriptor = tupdesc;
1566 438620 : PinTupleDesc(tupdesc);
1567 :
1568 : /*
1569 : * Allocate Datum/isnull arrays of the appropriate size. These must have
1570 : * the same lifetime as the slot, so allocate in the slot's own context.
1571 : */
1572 438620 : slot->tts_values = (Datum *)
1573 438620 : MemoryContextAlloc(slot->tts_mcxt, tupdesc->natts * sizeof(Datum));
1574 :
1575 : /*
1576 : * We round the size of tts_isnull up to the next highest multiple of 8.
1577 : * This is needed as populate_isnull_array() operates on 8 elements at a
1578 : * time when converting a tuple's NULL bitmap into a boolean array.
1579 : */
1580 438620 : slot->tts_isnull = (bool *)
1581 438620 : MemoryContextAlloc(slot->tts_mcxt, TYPEALIGN(8, tupdesc->natts * sizeof(bool)));
1582 438620 : }
1583 :
1584 : /* --------------------------------
1585 : * ExecStoreHeapTuple
1586 : *
1587 : * This function is used to store an on-the-fly physical tuple into a specified
1588 : * slot in the tuple table.
1589 : *
1590 : * tuple: tuple to store
1591 : * slot: TTSOpsHeapTuple type slot to store it in
1592 : * shouldFree: true if ExecClearTuple should pfree() the tuple
1593 : * when done with it
1594 : *
1595 : * shouldFree is normally set 'true' for tuples constructed on-the-fly. But it
1596 : * can be 'false' when the referenced tuple is held in a tuple table slot
1597 : * belonging to a lower-level executor Proc node. In this case the lower-level
1598 : * slot retains ownership and responsibility for eventually releasing the
1599 : * tuple. When this method is used, we must be certain that the upper-level
1600 : * Proc node will lose interest in the tuple sooner than the lower-level one
1601 : * does! If you're not certain, copy the lower-level tuple with heap_copytuple
1602 : * and let the upper-level table slot assume ownership of the copy!
1603 : *
1604 : * Return value is just the passed-in slot pointer.
1605 : *
1606 : * If the target slot is not guaranteed to be TTSOpsHeapTuple type slot, use
1607 : * the, more expensive, ExecForceStoreHeapTuple().
1608 : * --------------------------------
1609 : */
1610 : TupleTableSlot *
1611 2784891 : ExecStoreHeapTuple(HeapTuple tuple,
1612 : TupleTableSlot *slot,
1613 : bool shouldFree)
1614 : {
1615 : /*
1616 : * sanity checks
1617 : */
1618 : Assert(tuple != NULL);
1619 : Assert(slot != NULL);
1620 : Assert(slot->tts_tupleDescriptor != NULL);
1621 :
1622 2784891 : if (unlikely(!TTS_IS_HEAPTUPLE(slot)))
1623 0 : elog(ERROR, "trying to store a heap tuple into wrong type of slot");
1624 2784891 : tts_heap_store_tuple(slot, tuple, shouldFree);
1625 :
1626 2784891 : slot->tts_tableOid = tuple->t_tableOid;
1627 :
1628 2784891 : return slot;
1629 : }
1630 :
1631 : /* --------------------------------
1632 : * ExecStoreBufferHeapTuple
1633 : *
1634 : * This function is used to store an on-disk physical tuple from a buffer
1635 : * into a specified slot in the tuple table.
1636 : *
1637 : * tuple: tuple to store
1638 : * slot: TTSOpsBufferHeapTuple type slot to store it in
1639 : * buffer: disk buffer if tuple is in a disk page, else InvalidBuffer
1640 : *
1641 : * The tuple table code acquires a pin on the buffer which is held until the
1642 : * slot is cleared, so that the tuple won't go away on us.
1643 : *
1644 : * Return value is just the passed-in slot pointer.
1645 : *
1646 : * If the target slot is not guaranteed to be TTSOpsBufferHeapTuple type slot,
1647 : * use the, more expensive, ExecForceStoreHeapTuple().
1648 : * --------------------------------
1649 : */
1650 : TupleTableSlot *
1651 105541160 : ExecStoreBufferHeapTuple(HeapTuple tuple,
1652 : TupleTableSlot *slot,
1653 : Buffer buffer)
1654 : {
1655 : /*
1656 : * sanity checks
1657 : */
1658 : Assert(tuple != NULL);
1659 : Assert(slot != NULL);
1660 : Assert(slot->tts_tupleDescriptor != NULL);
1661 : Assert(BufferIsValid(buffer));
1662 :
1663 105541160 : if (unlikely(!TTS_IS_BUFFERTUPLE(slot)))
1664 0 : elog(ERROR, "trying to store an on-disk heap tuple into wrong type of slot");
1665 105541160 : tts_buffer_heap_store_tuple(slot, tuple, buffer, false);
1666 :
1667 105541160 : slot->tts_tableOid = tuple->t_tableOid;
1668 :
1669 105541160 : return slot;
1670 : }
1671 :
1672 : /*
1673 : * Like ExecStoreBufferHeapTuple, but transfer an existing pin from the caller
1674 : * to the slot, i.e. the caller doesn't need to, and may not, release the pin.
1675 : */
1676 : TupleTableSlot *
1677 1153979 : ExecStorePinnedBufferHeapTuple(HeapTuple tuple,
1678 : TupleTableSlot *slot,
1679 : Buffer buffer)
1680 : {
1681 : /*
1682 : * sanity checks
1683 : */
1684 : Assert(tuple != NULL);
1685 : Assert(slot != NULL);
1686 : Assert(slot->tts_tupleDescriptor != NULL);
1687 : Assert(BufferIsValid(buffer));
1688 :
1689 1153979 : if (unlikely(!TTS_IS_BUFFERTUPLE(slot)))
1690 0 : elog(ERROR, "trying to store an on-disk heap tuple into wrong type of slot");
1691 1153979 : tts_buffer_heap_store_tuple(slot, tuple, buffer, true);
1692 :
1693 1153979 : slot->tts_tableOid = tuple->t_tableOid;
1694 :
1695 1153979 : return slot;
1696 : }
1697 :
1698 : /*
1699 : * Store a minimal tuple into TTSOpsMinimalTuple type slot.
1700 : *
1701 : * If the target slot is not guaranteed to be TTSOpsMinimalTuple type slot,
1702 : * use the, more expensive, ExecForceStoreMinimalTuple().
1703 : */
1704 : TupleTableSlot *
1705 39696121 : ExecStoreMinimalTuple(MinimalTuple mtup,
1706 : TupleTableSlot *slot,
1707 : bool shouldFree)
1708 : {
1709 : /*
1710 : * sanity checks
1711 : */
1712 : Assert(mtup != NULL);
1713 : Assert(slot != NULL);
1714 : Assert(slot->tts_tupleDescriptor != NULL);
1715 :
1716 39696121 : if (unlikely(!TTS_IS_MINIMALTUPLE(slot)))
1717 0 : elog(ERROR, "trying to store a minimal tuple into wrong type of slot");
1718 39696121 : tts_minimal_store_tuple(slot, mtup, shouldFree);
1719 :
1720 39696121 : return slot;
1721 : }
1722 :
1723 : /*
1724 : * Store a HeapTuple into any kind of slot, performing conversion if
1725 : * necessary.
1726 : */
1727 : void
1728 1137101 : ExecForceStoreHeapTuple(HeapTuple tuple,
1729 : TupleTableSlot *slot,
1730 : bool shouldFree)
1731 : {
1732 1137101 : if (TTS_IS_HEAPTUPLE(slot))
1733 : {
1734 263 : ExecStoreHeapTuple(tuple, slot, shouldFree);
1735 : }
1736 1136838 : else if (TTS_IS_BUFFERTUPLE(slot))
1737 : {
1738 : MemoryContext oldContext;
1739 48048 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
1740 :
1741 48048 : ExecClearTuple(slot);
1742 48048 : slot->tts_flags &= ~TTS_FLAG_EMPTY;
1743 48048 : oldContext = MemoryContextSwitchTo(slot->tts_mcxt);
1744 48048 : bslot->base.tuple = heap_copytuple(tuple);
1745 48048 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
1746 48048 : MemoryContextSwitchTo(oldContext);
1747 :
1748 48048 : if (shouldFree)
1749 46845 : pfree(tuple);
1750 : }
1751 : else
1752 : {
1753 1088790 : ExecClearTuple(slot);
1754 1088790 : heap_deform_tuple(tuple, slot->tts_tupleDescriptor,
1755 : slot->tts_values, slot->tts_isnull);
1756 1088790 : ExecStoreVirtualTuple(slot);
1757 :
1758 1088790 : if (shouldFree)
1759 : {
1760 136606 : ExecMaterializeSlot(slot);
1761 136606 : pfree(tuple);
1762 : }
1763 : }
1764 1137101 : }
1765 :
1766 : /*
1767 : * Store a MinimalTuple into any kind of slot, performing conversion if
1768 : * necessary.
1769 : */
1770 : void
1771 4772849 : ExecForceStoreMinimalTuple(MinimalTuple mtup,
1772 : TupleTableSlot *slot,
1773 : bool shouldFree)
1774 : {
1775 4772849 : if (TTS_IS_MINIMALTUPLE(slot))
1776 : {
1777 3013969 : tts_minimal_store_tuple(slot, mtup, shouldFree);
1778 : }
1779 : else
1780 : {
1781 : HeapTupleData htup;
1782 :
1783 1758880 : ExecClearTuple(slot);
1784 :
1785 1758880 : htup.t_len = mtup->t_len + MINIMAL_TUPLE_OFFSET;
1786 1758880 : htup.t_data = (HeapTupleHeader) ((char *) mtup - MINIMAL_TUPLE_OFFSET);
1787 1758880 : heap_deform_tuple(&htup, slot->tts_tupleDescriptor,
1788 : slot->tts_values, slot->tts_isnull);
1789 1758880 : ExecStoreVirtualTuple(slot);
1790 :
1791 1758880 : if (shouldFree)
1792 : {
1793 958856 : ExecMaterializeSlot(slot);
1794 958856 : pfree(mtup);
1795 : }
1796 : }
1797 4772849 : }
1798 :
1799 : /* --------------------------------
1800 : * ExecStoreVirtualTuple
1801 : * Mark a slot as containing a virtual tuple.
1802 : *
1803 : * The protocol for loading a slot with virtual tuple data is:
1804 : * * Call ExecClearTuple to mark the slot empty.
1805 : * * Store data into the Datum/isnull arrays.
1806 : * * Call ExecStoreVirtualTuple to mark the slot valid.
1807 : * This is a bit unclean but it avoids one round of data copying.
1808 : * --------------------------------
1809 : */
1810 : TupleTableSlot *
1811 17715562 : ExecStoreVirtualTuple(TupleTableSlot *slot)
1812 : {
1813 : /*
1814 : * sanity checks
1815 : */
1816 : Assert(slot != NULL);
1817 : Assert(slot->tts_tupleDescriptor != NULL);
1818 : Assert(TTS_EMPTY(slot));
1819 :
1820 17715562 : slot->tts_flags &= ~TTS_FLAG_EMPTY;
1821 17715562 : slot->tts_nvalid = slot->tts_tupleDescriptor->natts;
1822 :
1823 17715562 : return slot;
1824 : }
1825 :
1826 : /* --------------------------------
1827 : * ExecStoreAllNullTuple
1828 : * Set up the slot to contain a null in every column.
1829 : *
1830 : * At first glance this might sound just like ExecClearTuple, but it's
1831 : * entirely different: the slot ends up full, not empty.
1832 : * --------------------------------
1833 : */
1834 : TupleTableSlot *
1835 29464 : ExecStoreAllNullTuple(TupleTableSlot *slot)
1836 : {
1837 : /*
1838 : * sanity checks
1839 : */
1840 : Assert(slot != NULL);
1841 : Assert(slot->tts_tupleDescriptor != NULL);
1842 :
1843 : /* Clear any old contents */
1844 29464 : ExecClearTuple(slot);
1845 :
1846 : /*
1847 : * Fill all the columns of the virtual tuple with nulls
1848 : */
1849 215060 : MemSet(slot->tts_values, 0,
1850 : slot->tts_tupleDescriptor->natts * sizeof(Datum));
1851 29464 : memset(slot->tts_isnull, true,
1852 29464 : slot->tts_tupleDescriptor->natts * sizeof(bool));
1853 :
1854 29464 : return ExecStoreVirtualTuple(slot);
1855 : }
1856 :
1857 : /*
1858 : * Store a HeapTuple in datum form, into a slot. That always requires
1859 : * deforming it and storing it in virtual form.
1860 : *
1861 : * Until the slot is materialized, the contents of the slot depend on the
1862 : * datum.
1863 : */
1864 : void
1865 9 : ExecStoreHeapTupleDatum(Datum data, TupleTableSlot *slot)
1866 : {
1867 9 : HeapTupleData tuple = {0};
1868 : HeapTupleHeader td;
1869 :
1870 9 : td = DatumGetHeapTupleHeader(data);
1871 :
1872 9 : tuple.t_len = HeapTupleHeaderGetDatumLength(td);
1873 9 : tuple.t_self = td->t_ctid;
1874 9 : tuple.t_data = td;
1875 :
1876 9 : ExecClearTuple(slot);
1877 :
1878 9 : heap_deform_tuple(&tuple, slot->tts_tupleDescriptor,
1879 : slot->tts_values, slot->tts_isnull);
1880 9 : ExecStoreVirtualTuple(slot);
1881 9 : }
1882 :
1883 : /*
1884 : * ExecFetchSlotHeapTuple - fetch HeapTuple representing the slot's content
1885 : *
1886 : * The returned HeapTuple represents the slot's content as closely as
1887 : * possible.
1888 : *
1889 : * If materialize is true, the contents of the slots will be made independent
1890 : * from the underlying storage (i.e. all buffer pins are released, memory is
1891 : * allocated in the slot's context).
1892 : *
1893 : * If shouldFree is not-NULL it'll be set to true if the returned tuple has
1894 : * been allocated in the calling memory context, and must be freed by the
1895 : * caller (via explicit pfree() or a memory context reset).
1896 : *
1897 : * NB: If materialize is true, modifications of the returned tuple are
1898 : * allowed. But it depends on the type of the slot whether such modifications
1899 : * will also affect the slot's contents. While that is not the nicest
1900 : * behaviour, all such modifications are in the process of being removed.
1901 : */
1902 : HeapTuple
1903 28824244 : ExecFetchSlotHeapTuple(TupleTableSlot *slot, bool materialize, bool *shouldFree)
1904 : {
1905 : /*
1906 : * sanity checks
1907 : */
1908 : Assert(slot != NULL);
1909 : Assert(!TTS_EMPTY(slot));
1910 :
1911 : /* Materialize the tuple so that the slot "owns" it, if requested. */
1912 28824244 : if (materialize)
1913 12119522 : slot->tts_ops->materialize(slot);
1914 :
1915 28824244 : if (slot->tts_ops->get_heap_tuple == NULL)
1916 : {
1917 1939735 : if (shouldFree)
1918 1939735 : *shouldFree = true;
1919 1939735 : return slot->tts_ops->copy_heap_tuple(slot);
1920 : }
1921 : else
1922 : {
1923 26884509 : if (shouldFree)
1924 24535343 : *shouldFree = false;
1925 26884509 : return slot->tts_ops->get_heap_tuple(slot);
1926 : }
1927 : }
1928 :
1929 : /* --------------------------------
1930 : * ExecFetchSlotMinimalTuple
1931 : * Fetch the slot's minimal physical tuple.
1932 : *
1933 : * If the given tuple table slot can hold a minimal tuple, indicated by a
1934 : * non-NULL get_minimal_tuple callback, the function returns the minimal
1935 : * tuple returned by that callback. It assumes that the minimal tuple
1936 : * returned by the callback is "owned" by the slot i.e. the slot is
1937 : * responsible for freeing the memory consumed by the tuple. Hence it sets
1938 : * *shouldFree to false, indicating that the caller should not free the
1939 : * memory consumed by the minimal tuple. In this case the returned minimal
1940 : * tuple should be considered as read-only.
1941 : *
1942 : * If that callback is not supported, it calls copy_minimal_tuple callback
1943 : * which is expected to return a copy of minimal tuple representing the
1944 : * contents of the slot. In this case *shouldFree is set to true,
1945 : * indicating the caller that it should free the memory consumed by the
1946 : * minimal tuple. In this case the returned minimal tuple may be written
1947 : * up.
1948 : * --------------------------------
1949 : */
1950 : MinimalTuple
1951 14502287 : ExecFetchSlotMinimalTuple(TupleTableSlot *slot,
1952 : bool *shouldFree)
1953 : {
1954 : /*
1955 : * sanity checks
1956 : */
1957 : Assert(slot != NULL);
1958 : Assert(!TTS_EMPTY(slot));
1959 :
1960 14502287 : if (slot->tts_ops->get_minimal_tuple)
1961 : {
1962 3356995 : if (shouldFree)
1963 3356995 : *shouldFree = false;
1964 3356995 : return slot->tts_ops->get_minimal_tuple(slot);
1965 : }
1966 : else
1967 : {
1968 11145292 : if (shouldFree)
1969 11145292 : *shouldFree = true;
1970 11145292 : return slot->tts_ops->copy_minimal_tuple(slot, 0);
1971 : }
1972 : }
1973 :
1974 : /* --------------------------------
1975 : * ExecFetchSlotHeapTupleDatum
1976 : * Fetch the slot's tuple as a composite-type Datum.
1977 : *
1978 : * The result is always freshly palloc'd in the caller's memory context.
1979 : * --------------------------------
1980 : */
1981 : Datum
1982 40501 : ExecFetchSlotHeapTupleDatum(TupleTableSlot *slot)
1983 : {
1984 : HeapTuple tup;
1985 : TupleDesc tupdesc;
1986 : bool shouldFree;
1987 : Datum ret;
1988 :
1989 : /* Fetch slot's contents in regular-physical-tuple form */
1990 40501 : tup = ExecFetchSlotHeapTuple(slot, false, &shouldFree);
1991 40501 : tupdesc = slot->tts_tupleDescriptor;
1992 :
1993 : /* Convert to Datum form */
1994 40501 : ret = heap_copy_tuple_as_datum(tup, tupdesc);
1995 :
1996 40501 : if (shouldFree)
1997 40501 : pfree(tup);
1998 :
1999 40501 : return ret;
2000 : }
2001 :
2002 : /* ----------------------------------------------------------------
2003 : * convenience initialization routines
2004 : * ----------------------------------------------------------------
2005 : */
2006 :
2007 : /* ----------------
2008 : * ExecInitResultTypeTL
2009 : *
2010 : * Initialize result type, using the plan node's targetlist.
2011 : * ----------------
2012 : */
2013 : void
2014 1677216 : ExecInitResultTypeTL(PlanState *planstate)
2015 : {
2016 1677216 : TupleDesc tupDesc = ExecTypeFromTL(planstate->plan->targetlist);
2017 :
2018 1677216 : planstate->ps_ResultTupleDesc = tupDesc;
2019 1677216 : }
2020 :
2021 : /* --------------------------------
2022 : * ExecInit{Result,Scan,Extra}TupleSlot[TL]
2023 : *
2024 : * These are convenience routines to initialize the specified slot
2025 : * in nodes inheriting the appropriate state. ExecInitExtraTupleSlot
2026 : * is used for initializing special-purpose slots.
2027 : * --------------------------------
2028 : */
2029 :
2030 : /* ----------------
2031 : * ExecInitResultTupleSlotTL
2032 : *
2033 : * Initialize result tuple slot, using the tuple descriptor previously
2034 : * computed with ExecInitResultTypeTL().
2035 : * ----------------
2036 : */
2037 : void
2038 1002623 : ExecInitResultSlot(PlanState *planstate, const TupleTableSlotOps *tts_ops)
2039 : {
2040 : TupleTableSlot *slot;
2041 :
2042 1002623 : slot = ExecAllocTableSlot(&planstate->state->es_tupleTable,
2043 : planstate->ps_ResultTupleDesc, tts_ops, 0);
2044 1002623 : planstate->ps_ResultTupleSlot = slot;
2045 :
2046 1002623 : planstate->resultopsfixed = planstate->ps_ResultTupleDesc != NULL;
2047 1002623 : planstate->resultops = tts_ops;
2048 1002623 : planstate->resultopsset = true;
2049 1002623 : }
2050 :
2051 : /* ----------------
2052 : * ExecInitResultTupleSlotTL
2053 : *
2054 : * Initialize result tuple slot, using the plan node's targetlist.
2055 : * ----------------
2056 : */
2057 : void
2058 425243 : ExecInitResultTupleSlotTL(PlanState *planstate,
2059 : const TupleTableSlotOps *tts_ops)
2060 : {
2061 425243 : ExecInitResultTypeTL(planstate);
2062 425243 : ExecInitResultSlot(planstate, tts_ops);
2063 425243 : }
2064 :
2065 : /* ----------------
2066 : * ExecInitScanTupleSlot
2067 : * ----------------
2068 : */
2069 : void
2070 865668 : ExecInitScanTupleSlot(EState *estate, ScanState *scanstate,
2071 : TupleDesc tupledesc, const TupleTableSlotOps *tts_ops,
2072 : uint16 flags)
2073 : {
2074 865668 : scanstate->ss_ScanTupleSlot = ExecAllocTableSlot(&estate->es_tupleTable,
2075 : tupledesc, tts_ops, flags);
2076 865668 : scanstate->ps.scandesc = tupledesc;
2077 865668 : scanstate->ps.scanopsfixed = tupledesc != NULL;
2078 865668 : scanstate->ps.scanops = tts_ops;
2079 865668 : scanstate->ps.scanopsset = true;
2080 865668 : }
2081 :
2082 : /* ----------------
2083 : * ExecInitExtraTupleSlot
2084 : *
2085 : * Return a newly created slot. If tupledesc is non-NULL the slot will have
2086 : * that as its fixed tupledesc. Otherwise the caller needs to use
2087 : * ExecSetSlotDescriptor() to set the descriptor before use.
2088 : * ----------------
2089 : */
2090 : TupleTableSlot *
2091 661011 : ExecInitExtraTupleSlot(EState *estate,
2092 : TupleDesc tupledesc,
2093 : const TupleTableSlotOps *tts_ops)
2094 : {
2095 661011 : return ExecAllocTableSlot(&estate->es_tupleTable, tupledesc, tts_ops, 0);
2096 : }
2097 :
2098 : /* ----------------
2099 : * ExecInitNullTupleSlot
2100 : *
2101 : * Build a slot containing an all-nulls tuple of the given type.
2102 : * This is used as a substitute for an input tuple when performing an
2103 : * outer join.
2104 : * ----------------
2105 : */
2106 : TupleTableSlot *
2107 28509 : ExecInitNullTupleSlot(EState *estate, TupleDesc tupType,
2108 : const TupleTableSlotOps *tts_ops)
2109 : {
2110 28509 : TupleTableSlot *slot = ExecInitExtraTupleSlot(estate, tupType, tts_ops);
2111 :
2112 28509 : return ExecStoreAllNullTuple(slot);
2113 : }
2114 :
2115 : /* ---------------------------------------------------------------
2116 : * Routines for setting/accessing attributes in a slot.
2117 : * ---------------------------------------------------------------
2118 : */
2119 :
2120 : /*
2121 : * Fill in missing values for a TupleTableSlot.
2122 : *
2123 : * This is only exposed because it's needed for JIT compiled tuple
2124 : * deforming. That exception aside, there should be no callers outside of this
2125 : * file.
2126 : */
2127 : void
2128 4831 : slot_getmissingattrs(TupleTableSlot *slot, int startAttNum, int lastAttNum)
2129 : {
2130 4831 : AttrMissing *attrmiss = NULL;
2131 :
2132 : /* Check for invalid attnums */
2133 4831 : if (unlikely(lastAttNum > slot->tts_tupleDescriptor->natts))
2134 0 : elog(ERROR, "invalid attribute number %d", lastAttNum);
2135 :
2136 4831 : if (slot->tts_tupleDescriptor->constr)
2137 3130 : attrmiss = slot->tts_tupleDescriptor->constr->missing;
2138 :
2139 4831 : if (!attrmiss)
2140 : {
2141 : /* no missing values array at all, so just fill everything in as NULL */
2142 3831 : for (int attnum = startAttNum; attnum < lastAttNum; attnum++)
2143 : {
2144 2020 : slot->tts_values[attnum] = (Datum) 0;
2145 2020 : slot->tts_isnull[attnum] = true;
2146 : }
2147 : }
2148 : else
2149 : {
2150 : /* use attrmiss to set the missing values */
2151 7003 : for (int attnum = startAttNum; attnum < lastAttNum; attnum++)
2152 : {
2153 3983 : slot->tts_values[attnum] = attrmiss[attnum].am_value;
2154 3983 : slot->tts_isnull[attnum] = !attrmiss[attnum].am_present;
2155 : }
2156 : }
2157 4831 : }
2158 :
2159 : /*
2160 : * slot_getsomeattrs_int
2161 : * external function to call getsomeattrs() for use in JIT
2162 : */
2163 : void
2164 111884 : slot_getsomeattrs_int(TupleTableSlot *slot, int attnum)
2165 : {
2166 : /* Check for caller errors */
2167 : Assert(slot->tts_nvalid < attnum); /* checked in slot_getsomeattrs */
2168 : Assert(attnum > 0);
2169 :
2170 : /* Fetch as many attributes as possible from the underlying tuple. */
2171 111884 : slot->tts_ops->getsomeattrs(slot, attnum);
2172 :
2173 : /*
2174 : * Avoid putting new code here as that would prevent the compiler from
2175 : * using the sibling call optimization for the above function.
2176 : */
2177 111884 : }
2178 :
2179 : /* ----------------------------------------------------------------
2180 : * ExecTypeFromTL
2181 : *
2182 : * Generate a tuple descriptor for the result tuple of a targetlist.
2183 : * (A parse/plan tlist must be passed, not an ExprState tlist.)
2184 : * Note that resjunk columns, if any, are included in the result.
2185 : *
2186 : * Currently there are about 4 different places where we create
2187 : * TupleDescriptors. They should all be merged, or perhaps
2188 : * be rewritten to call BuildDesc().
2189 : * ----------------------------------------------------------------
2190 : */
2191 : TupleDesc
2192 1697127 : ExecTypeFromTL(List *targetList)
2193 : {
2194 1697127 : return ExecTypeFromTLInternal(targetList, false);
2195 : }
2196 :
2197 : /* ----------------------------------------------------------------
2198 : * ExecCleanTypeFromTL
2199 : *
2200 : * Same as above, but resjunk columns are omitted from the result.
2201 : * ----------------------------------------------------------------
2202 : */
2203 : TupleDesc
2204 474142 : ExecCleanTypeFromTL(List *targetList)
2205 : {
2206 474142 : return ExecTypeFromTLInternal(targetList, true);
2207 : }
2208 :
2209 : static TupleDesc
2210 2171269 : ExecTypeFromTLInternal(List *targetList, bool skipjunk)
2211 : {
2212 : TupleDesc typeInfo;
2213 : ListCell *l;
2214 : int len;
2215 2171269 : int cur_resno = 1;
2216 :
2217 2171269 : if (skipjunk)
2218 474142 : len = ExecCleanTargetListLength(targetList);
2219 : else
2220 1697127 : len = ExecTargetListLength(targetList);
2221 2171269 : typeInfo = CreateTemplateTupleDesc(len);
2222 :
2223 8587984 : foreach(l, targetList)
2224 : {
2225 6416715 : TargetEntry *tle = lfirst(l);
2226 :
2227 6416715 : if (skipjunk && tle->resjunk)
2228 421895 : continue;
2229 17984460 : TupleDescInitEntry(typeInfo,
2230 : cur_resno,
2231 5994820 : tle->resname,
2232 5994820 : exprType((Node *) tle->expr),
2233 5994820 : exprTypmod((Node *) tle->expr),
2234 : 0);
2235 5994820 : TupleDescInitEntryCollation(typeInfo,
2236 : cur_resno,
2237 5994820 : exprCollation((Node *) tle->expr));
2238 5994820 : cur_resno++;
2239 : }
2240 :
2241 2171269 : TupleDescFinalize(typeInfo);
2242 :
2243 2171269 : return typeInfo;
2244 : }
2245 :
2246 : /*
2247 : * ExecTypeFromExprList - build a tuple descriptor from a list of Exprs
2248 : *
2249 : * This is roughly like ExecTypeFromTL, but we work from bare expressions
2250 : * not TargetEntrys. No names are attached to the tupledesc's columns.
2251 : */
2252 : TupleDesc
2253 9548 : ExecTypeFromExprList(List *exprList)
2254 : {
2255 : TupleDesc typeInfo;
2256 : ListCell *lc;
2257 9548 : int cur_resno = 1;
2258 :
2259 9548 : typeInfo = CreateTemplateTupleDesc(list_length(exprList));
2260 :
2261 26501 : foreach(lc, exprList)
2262 : {
2263 16953 : Node *e = lfirst(lc);
2264 :
2265 16953 : TupleDescInitEntry(typeInfo,
2266 : cur_resno,
2267 : NULL,
2268 : exprType(e),
2269 : exprTypmod(e),
2270 : 0);
2271 16953 : TupleDescInitEntryCollation(typeInfo,
2272 : cur_resno,
2273 : exprCollation(e));
2274 16953 : cur_resno++;
2275 : }
2276 :
2277 9548 : TupleDescFinalize(typeInfo);
2278 :
2279 9548 : return typeInfo;
2280 : }
2281 :
2282 : /*
2283 : * ExecTypeSetColNames - set column names in a RECORD TupleDesc
2284 : *
2285 : * Column names must be provided as an alias list (list of String nodes).
2286 : */
2287 : void
2288 2796 : ExecTypeSetColNames(TupleDesc typeInfo, List *namesList)
2289 : {
2290 2796 : int colno = 0;
2291 : ListCell *lc;
2292 :
2293 : /* It's only OK to change col names in a not-yet-blessed RECORD type */
2294 : Assert(typeInfo->tdtypeid == RECORDOID);
2295 : Assert(typeInfo->tdtypmod < 0);
2296 :
2297 9626 : foreach(lc, namesList)
2298 : {
2299 6830 : char *cname = strVal(lfirst(lc));
2300 : Form_pg_attribute attr;
2301 :
2302 : /* Guard against too-long names list (probably can't happen) */
2303 6830 : if (colno >= typeInfo->natts)
2304 0 : break;
2305 6830 : attr = TupleDescAttr(typeInfo, colno);
2306 6830 : colno++;
2307 :
2308 : /*
2309 : * Do nothing for empty aliases or dropped columns (these cases
2310 : * probably can't arise in RECORD types, either)
2311 : */
2312 6830 : if (cname[0] == '\0' || attr->attisdropped)
2313 16 : continue;
2314 :
2315 : /* OK, assign the column name */
2316 6814 : namestrcpy(&(attr->attname), cname);
2317 : }
2318 2796 : }
2319 :
2320 : /*
2321 : * BlessTupleDesc - make a completed tuple descriptor useful for SRFs
2322 : *
2323 : * Rowtype Datums returned by a function must contain valid type information.
2324 : * This happens "for free" if the tupdesc came from a relcache entry, but
2325 : * not if we have manufactured a tupdesc for a transient RECORD datatype.
2326 : * In that case we have to notify typcache.c of the existence of the type.
2327 : *
2328 : * TupleDescFinalize() must be called on the TupleDesc before calling this
2329 : * function.
2330 : */
2331 : TupleDesc
2332 58085 : BlessTupleDesc(TupleDesc tupdesc)
2333 : {
2334 : /* Did someone forget to call TupleDescFinalize()? */
2335 : Assert(tupdesc->firstNonCachedOffsetAttr >= 0);
2336 :
2337 58085 : if (tupdesc->tdtypeid == RECORDOID &&
2338 55408 : tupdesc->tdtypmod < 0)
2339 23204 : assign_record_type_typmod(tupdesc);
2340 :
2341 58085 : return tupdesc; /* just for notational convenience */
2342 : }
2343 :
2344 : /*
2345 : * TupleDescGetAttInMetadata - Build an AttInMetadata structure based on the
2346 : * supplied TupleDesc. AttInMetadata can be used in conjunction with C strings
2347 : * to produce a properly formed tuple.
2348 : */
2349 : AttInMetadata *
2350 14226 : TupleDescGetAttInMetadata(TupleDesc tupdesc)
2351 : {
2352 14226 : int natts = tupdesc->natts;
2353 : int i;
2354 : Oid atttypeid;
2355 : Oid attinfuncid;
2356 : FmgrInfo *attinfuncinfo;
2357 : Oid *attioparams;
2358 : int32 *atttypmods;
2359 : AttInMetadata *attinmeta;
2360 :
2361 14226 : attinmeta = palloc_object(AttInMetadata);
2362 :
2363 : /* "Bless" the tupledesc so that we can make rowtype datums with it */
2364 14226 : attinmeta->tupdesc = BlessTupleDesc(tupdesc);
2365 :
2366 : /*
2367 : * Gather info needed later to call the "in" function for each attribute
2368 : */
2369 14226 : attinfuncinfo = (FmgrInfo *) palloc0(natts * sizeof(FmgrInfo));
2370 14226 : attioparams = (Oid *) palloc0(natts * sizeof(Oid));
2371 14226 : atttypmods = (int32 *) palloc0(natts * sizeof(int32));
2372 :
2373 72436 : for (i = 0; i < natts; i++)
2374 : {
2375 58210 : Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2376 :
2377 : /* Ignore dropped attributes */
2378 58210 : if (!att->attisdropped)
2379 : {
2380 58095 : atttypeid = att->atttypid;
2381 58095 : getTypeInputInfo(atttypeid, &attinfuncid, &attioparams[i]);
2382 58095 : fmgr_info(attinfuncid, &attinfuncinfo[i]);
2383 58095 : atttypmods[i] = att->atttypmod;
2384 : }
2385 : }
2386 14226 : attinmeta->attinfuncs = attinfuncinfo;
2387 14226 : attinmeta->attioparams = attioparams;
2388 14226 : attinmeta->atttypmods = atttypmods;
2389 :
2390 14226 : return attinmeta;
2391 : }
2392 :
2393 : /*
2394 : * BuildTupleFromCStrings - build a HeapTuple given user data in C string form.
2395 : * values is an array of C strings, one for each attribute of the return tuple.
2396 : * A NULL string pointer indicates we want to create a NULL field.
2397 : */
2398 : HeapTuple
2399 938788 : BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
2400 : {
2401 938788 : TupleDesc tupdesc = attinmeta->tupdesc;
2402 938788 : int natts = tupdesc->natts;
2403 : Datum *dvalues;
2404 : bool *nulls;
2405 : int i;
2406 : HeapTuple tuple;
2407 :
2408 938788 : dvalues = (Datum *) palloc(natts * sizeof(Datum));
2409 938788 : nulls = (bool *) palloc(natts * sizeof(bool));
2410 :
2411 : /*
2412 : * Call the "in" function for each non-dropped attribute, even for nulls,
2413 : * to support domains.
2414 : */
2415 13877394 : for (i = 0; i < natts; i++)
2416 : {
2417 12938607 : if (!TupleDescCompactAttr(tupdesc, i)->attisdropped)
2418 : {
2419 : /* Non-dropped attributes */
2420 25877213 : dvalues[i] = InputFunctionCall(&attinmeta->attinfuncs[i],
2421 12938607 : values[i],
2422 12938607 : attinmeta->attioparams[i],
2423 12938607 : attinmeta->atttypmods[i]);
2424 12938606 : if (values[i] != NULL)
2425 8935172 : nulls[i] = false;
2426 : else
2427 4003434 : nulls[i] = true;
2428 : }
2429 : else
2430 : {
2431 : /* Handle dropped attributes by setting to NULL */
2432 0 : dvalues[i] = (Datum) 0;
2433 0 : nulls[i] = true;
2434 : }
2435 : }
2436 :
2437 : /*
2438 : * Form a tuple
2439 : */
2440 938787 : tuple = heap_form_tuple(tupdesc, dvalues, nulls);
2441 :
2442 : /*
2443 : * Release locally palloc'd space. XXX would probably be good to pfree
2444 : * values of pass-by-reference datums, as well.
2445 : */
2446 938787 : pfree(dvalues);
2447 938787 : pfree(nulls);
2448 :
2449 938787 : return tuple;
2450 : }
2451 :
2452 : /*
2453 : * HeapTupleHeaderGetDatum - convert a HeapTupleHeader pointer to a Datum.
2454 : *
2455 : * This must *not* get applied to an on-disk tuple; the tuple should be
2456 : * freshly made by heap_form_tuple or some wrapper routine for it (such as
2457 : * BuildTupleFromCStrings). Be sure also that the tupledesc used to build
2458 : * the tuple has a properly "blessed" rowtype.
2459 : *
2460 : * Formerly this was a macro equivalent to PointerGetDatum, relying on the
2461 : * fact that heap_form_tuple fills in the appropriate tuple header fields
2462 : * for a composite Datum. However, we now require that composite Datums not
2463 : * contain any external TOAST pointers. We do not want heap_form_tuple itself
2464 : * to enforce that; more specifically, the rule applies only to actual Datums
2465 : * and not to HeapTuple structures. Therefore, HeapTupleHeaderGetDatum is
2466 : * now a function that detects whether there are externally-toasted fields
2467 : * and constructs a new tuple with inlined fields if so. We still need
2468 : * heap_form_tuple to insert the Datum header fields, because otherwise this
2469 : * code would have no way to obtain a tupledesc for the tuple.
2470 : *
2471 : * Note that if we do build a new tuple, it's palloc'd in the current
2472 : * memory context. Beware of code that changes context between the initial
2473 : * heap_form_tuple/etc call and calling HeapTuple(Header)GetDatum.
2474 : *
2475 : * For performance-critical callers, it could be worthwhile to take extra
2476 : * steps to ensure that there aren't TOAST pointers in the output of
2477 : * heap_form_tuple to begin with. It's likely however that the costs of the
2478 : * typcache lookup and tuple disassembly/reassembly are swamped by TOAST
2479 : * dereference costs, so that the benefits of such extra effort would be
2480 : * minimal.
2481 : *
2482 : * XXX it would likely be better to create wrapper functions that produce
2483 : * a composite Datum from the field values in one step. However, there's
2484 : * enough code using the existing APIs that we couldn't get rid of this
2485 : * hack anytime soon.
2486 : */
2487 : Datum
2488 1178731 : HeapTupleHeaderGetDatum(HeapTupleHeader tuple)
2489 : {
2490 : Datum result;
2491 : TupleDesc tupDesc;
2492 :
2493 : /* No work if there are no external TOAST pointers in the tuple */
2494 1178731 : if (!HeapTupleHeaderHasExternal(tuple))
2495 1178723 : return PointerGetDatum(tuple);
2496 :
2497 : /* Use the type data saved by heap_form_tuple to look up the rowtype */
2498 8 : tupDesc = lookup_rowtype_tupdesc(HeapTupleHeaderGetTypeId(tuple),
2499 : HeapTupleHeaderGetTypMod(tuple));
2500 :
2501 : /* And do the flattening */
2502 8 : result = toast_flatten_tuple_to_datum(tuple,
2503 : HeapTupleHeaderGetDatumLength(tuple),
2504 : tupDesc);
2505 :
2506 8 : ReleaseTupleDesc(tupDesc);
2507 :
2508 8 : return result;
2509 : }
2510 :
2511 :
2512 : /*
2513 : * Functions for sending tuples to the frontend (or other specified destination)
2514 : * as though it is a SELECT result. These are used by utility commands that
2515 : * need to project directly to the destination and don't need or want full
2516 : * table function capability. Currently used by EXPLAIN and SHOW ALL.
2517 : */
2518 : TupOutputState *
2519 19299 : begin_tup_output_tupdesc(DestReceiver *dest,
2520 : TupleDesc tupdesc,
2521 : const TupleTableSlotOps *tts_ops)
2522 : {
2523 : TupOutputState *tstate;
2524 :
2525 19299 : tstate = palloc_object(TupOutputState);
2526 :
2527 19299 : tstate->slot = MakeSingleTupleTableSlot(tupdesc, tts_ops);
2528 19299 : tstate->dest = dest;
2529 :
2530 19299 : tstate->dest->rStartup(tstate->dest, (int) CMD_SELECT, tupdesc);
2531 :
2532 19299 : return tstate;
2533 : }
2534 :
2535 : /*
2536 : * write a single tuple
2537 : */
2538 : void
2539 114420 : do_tup_output(TupOutputState *tstate, const Datum *values, const bool *isnull)
2540 : {
2541 114420 : TupleTableSlot *slot = tstate->slot;
2542 114420 : int natts = slot->tts_tupleDescriptor->natts;
2543 :
2544 : /* make sure the slot is clear */
2545 114420 : ExecClearTuple(slot);
2546 :
2547 : /* insert data */
2548 114420 : memcpy(slot->tts_values, values, natts * sizeof(Datum));
2549 114420 : memcpy(slot->tts_isnull, isnull, natts * sizeof(bool));
2550 :
2551 : /* mark slot as containing a virtual tuple */
2552 114420 : ExecStoreVirtualTuple(slot);
2553 :
2554 : /* send the tuple to the receiver */
2555 114420 : (void) tstate->dest->receiveSlot(slot, tstate->dest);
2556 :
2557 : /* clean up */
2558 114420 : ExecClearTuple(slot);
2559 114420 : }
2560 :
2561 : /*
2562 : * write a chunk of text, breaking at newline characters
2563 : *
2564 : * Should only be used with a single-TEXT-attribute tupdesc.
2565 : */
2566 : void
2567 15988 : do_text_output_multiline(TupOutputState *tstate, const char *txt)
2568 : {
2569 : Datum values[1];
2570 15988 : bool isnull[1] = {false};
2571 :
2572 126356 : while (*txt)
2573 : {
2574 : const char *eol;
2575 : int len;
2576 :
2577 110368 : eol = strchr(txt, '\n');
2578 110368 : if (eol)
2579 : {
2580 110368 : len = eol - txt;
2581 110368 : eol++;
2582 : }
2583 : else
2584 : {
2585 0 : len = strlen(txt);
2586 0 : eol = txt + len;
2587 : }
2588 :
2589 110368 : values[0] = PointerGetDatum(cstring_to_text_with_len(txt, len));
2590 110368 : do_tup_output(tstate, values, isnull);
2591 110368 : pfree(DatumGetPointer(values[0]));
2592 110368 : txt = eol;
2593 : }
2594 15988 : }
2595 :
2596 : void
2597 19299 : end_tup_output(TupOutputState *tstate)
2598 : {
2599 19299 : tstate->dest->rShutdown(tstate->dest);
2600 : /* note that destroying the dest is not ours to do */
2601 19299 : ExecDropSingleTupleTableSlot(tstate->slot);
2602 19299 : pfree(tstate);
2603 19299 : }
|