Branch data 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_always_inline void slot_deform_heap_tuple(TupleTableSlot *slot, HeapTuple tuple, uint32 *offp,
76 : : int reqnatts, bool support_cstring);
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 : 877709 : tts_virtual_init(TupleTableSlot *slot)
99 : : {
100 : 877709 : }
101 : :
102 : : static void
103 : 857214 : tts_virtual_release(TupleTableSlot *slot)
104 : : {
105 : 857214 : }
106 : :
107 : : static void
108 : 60553301 : tts_virtual_clear(TupleTableSlot *slot)
109 : : {
110 [ + + ]: 60553301 : if (unlikely(TTS_SHOULDFREE(slot)))
111 : : {
112 : 1208875 : VirtualTupleTableSlot *vslot = (VirtualTupleTableSlot *) slot;
113 : :
114 : 1208875 : pfree(vslot->data);
115 : 1208875 : vslot->data = NULL;
116 : :
117 : 1208875 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
118 : : }
119 : :
120 : 60553301 : slot->tts_nvalid = 0;
121 : 60553301 : slot->tts_flags |= TTS_FLAG_EMPTY;
122 : 60553301 : ItemPointerSetInvalid(&slot->tts_tid);
123 : 60553301 : }
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 : 3938876 : tts_virtual_materialize(TupleTableSlot *slot)
177 : : {
178 : 3938876 : VirtualTupleTableSlot *vslot = (VirtualTupleTableSlot *) slot;
179 : 3938876 : TupleDesc desc = slot->tts_tupleDescriptor;
180 : 3938876 : Size sz = 0;
181 : : char *data;
182 : :
183 : : /* already materialized */
184 [ + + ]: 3938876 : if (TTS_SHOULDFREE(slot))
185 : 234451 : return;
186 : :
187 : : /* compute size of memory required */
188 [ + + ]: 10479956 : for (int natt = 0; natt < desc->natts; natt++)
189 : : {
190 : 6775531 : CompactAttribute *att = TupleDescCompactAttr(desc, natt);
191 : : Datum val;
192 : :
193 [ + + + + ]: 6775531 : if (att->attbyval || slot->tts_isnull[natt])
194 : 5302661 : continue;
195 : :
196 : 1472870 : val = slot->tts_values[natt];
197 : :
198 [ + + - + ]: 2595162 : if (att->attlen == -1 &&
199 : 1122292 : 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 : 1472870 : sz = att_nominal_alignby(sz, att->attalignby);
211 [ + + + - ]: 1472870 : sz = att_addlength_datum(sz, att->attlen, val);
212 : : }
213 : : }
214 : :
215 : : /* all data is byval */
216 [ + + ]: 3704425 : if (sz == 0)
217 : 2495427 : return;
218 : :
219 : : /* allocate memory */
220 : 1208998 : vslot->data = data = MemoryContextAlloc(slot->tts_mcxt, sz);
221 : 1208998 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
222 : :
223 : : /* and copy all attributes into the pre-allocated space */
224 [ + + ]: 4696040 : for (int natt = 0; natt < desc->natts; natt++)
225 : : {
226 : 3487042 : CompactAttribute *att = TupleDescCompactAttr(desc, natt);
227 : : Datum val;
228 : :
229 [ + + + + ]: 3487042 : if (att->attbyval || slot->tts_isnull[natt])
230 : 2014172 : continue;
231 : :
232 : 1472870 : val = slot->tts_values[natt];
233 : :
234 [ + + - + ]: 2595162 : if (att->attlen == -1 &&
235 : 1122292 : 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 : 1472870 : Size data_length = 0;
256 : :
257 : 1472870 : data = (char *) att_nominal_alignby(data, att->attalignby);
258 [ + + + - ]: 1472870 : data_length = att_addlength_datum(data_length, att->attlen, val);
259 : :
260 : 1472870 : memcpy(data, DatumGetPointer(val), data_length);
261 : :
262 : 1472870 : slot->tts_values[natt] = PointerGetDatum(data);
263 : 1472870 : data += data_length;
264 : : }
265 : : }
266 : : }
267 : :
268 : : static void
269 : 91206 : tts_virtual_copyslot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
270 : : {
271 : 91206 : TupleDesc srcdesc = srcslot->tts_tupleDescriptor;
272 : :
273 : 91206 : tts_virtual_clear(dstslot);
274 : :
275 : 91206 : slot_getallattrs(srcslot);
276 : :
277 [ + + ]: 187346 : for (int natt = 0; natt < srcdesc->natts; natt++)
278 : : {
279 : 96140 : dstslot->tts_values[natt] = srcslot->tts_values[natt];
280 : 96140 : dstslot->tts_isnull[natt] = srcslot->tts_isnull[natt];
281 : : }
282 : :
283 : 91206 : dstslot->tts_nvalid = srcdesc->natts;
284 : 91206 : dstslot->tts_flags &= ~TTS_FLAG_EMPTY;
285 : :
286 : : /* make sure storage doesn't depend on external memory */
287 : 91206 : tts_virtual_materialize(dstslot);
288 : 91206 : }
289 : :
290 : : static HeapTuple
291 : 10450705 : tts_virtual_copy_heap_tuple(TupleTableSlot *slot)
292 : : {
293 : : Assert(!TTS_EMPTY(slot));
294 : :
295 : 20901410 : return heap_form_tuple(slot->tts_tupleDescriptor,
296 : 10450705 : slot->tts_values,
297 : 10450705 : slot->tts_isnull);
298 : : }
299 : :
300 : : static MinimalTuple
301 : 18596262 : tts_virtual_copy_minimal_tuple(TupleTableSlot *slot, Size extra)
302 : : {
303 : : Assert(!TTS_EMPTY(slot));
304 : :
305 : 37192524 : return heap_form_minimal_tuple(slot->tts_tupleDescriptor,
306 : 18596262 : slot->tts_values,
307 : 18596262 : slot->tts_isnull,
308 : : extra);
309 : : }
310 : :
311 : :
312 : : /*
313 : : * TupleTableSlotOps implementation for HeapTupleTableSlot.
314 : : */
315 : :
316 : : static void
317 : 2645091 : tts_heap_init(TupleTableSlot *slot)
318 : : {
319 : 2645091 : }
320 : :
321 : : static void
322 : 2644098 : tts_heap_release(TupleTableSlot *slot)
323 : : {
324 : 2644098 : }
325 : :
326 : : static void
327 : 7357046 : tts_heap_clear(TupleTableSlot *slot)
328 : : {
329 : 7357046 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
330 : :
331 : : /* Free the memory for the heap tuple if it's allowed. */
332 [ + + ]: 7357046 : if (TTS_SHOULDFREE(slot))
333 : : {
334 : 1134022 : heap_freetuple(hslot->tuple);
335 : 1134022 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
336 : : }
337 : :
338 : 7357046 : slot->tts_nvalid = 0;
339 : 7357046 : slot->tts_flags |= TTS_FLAG_EMPTY;
340 : 7357046 : ItemPointerSetInvalid(&slot->tts_tid);
341 : 7357046 : hslot->off = 0;
342 : 7357046 : hslot->tuple = NULL;
343 : 7357046 : }
344 : :
345 : : static void
346 : 7458270 : tts_heap_getsomeattrs(TupleTableSlot *slot, int natts)
347 : : {
348 : 7458270 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
349 : :
350 : : Assert(!TTS_EMPTY(slot));
351 : :
352 : 7458270 : slot_deform_heap_tuple(slot, hslot->tuple, &hslot->off, natts, false);
353 : 7458270 : }
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 : 2266786 : tts_heap_materialize(TupleTableSlot *slot)
400 : : {
401 : 2266786 : 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 [ + + ]: 2266786 : if (TTS_SHOULDFREE(slot))
408 : 1133754 : return;
409 : :
410 : 1133032 : 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 : 1133032 : slot->tts_nvalid = 0;
417 : 1133032 : hslot->off = 0;
418 : :
419 [ + + ]: 1133032 : if (!hslot->tuple)
420 : 1133025 : hslot->tuple = heap_form_tuple(slot->tts_tupleDescriptor,
421 : 1133025 : slot->tts_values,
422 : 1133025 : 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 : 1133032 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
434 : :
435 : 1133032 : 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 : 2265779 : tts_heap_get_heap_tuple(TupleTableSlot *slot)
453 : : {
454 : 2265779 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
455 : :
456 : : Assert(!TTS_EMPTY(slot));
457 [ - + ]: 2265779 : if (!hslot->tuple)
458 : 0 : tts_heap_materialize(slot);
459 : :
460 : 2265779 : 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 : 2722 : tts_heap_copy_minimal_tuple(TupleTableSlot *slot, Size extra)
477 : : {
478 : 2722 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
479 : :
480 [ + + ]: 2722 : if (!hslot->tuple)
481 : 21 : tts_heap_materialize(slot);
482 : :
483 : 2722 : return minimal_tuple_from_heap_tuple(hslot->tuple, extra);
484 : : }
485 : :
486 : : static void
487 : 3578323 : tts_heap_store_tuple(TupleTableSlot *slot, HeapTuple tuple, bool shouldFree)
488 : : {
489 : 3578323 : HeapTupleTableSlot *hslot = (HeapTupleTableSlot *) slot;
490 : :
491 : 3578323 : tts_heap_clear(slot);
492 : :
493 : 3578323 : slot->tts_nvalid = 0;
494 : 3578323 : hslot->tuple = tuple;
495 : 3578323 : hslot->off = 0;
496 : 3578323 : slot->tts_flags &= ~(TTS_FLAG_EMPTY | TTS_FLAG_SHOULDFREE);
497 : 3578323 : slot->tts_tid = tuple->t_self;
498 : :
499 [ + + ]: 3578323 : if (shouldFree)
500 : 1001 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
501 : 3578323 : }
502 : :
503 : :
504 : : /*
505 : : * TupleTableSlotOps implementation for MinimalTupleTableSlot.
506 : : */
507 : :
508 : : static void
509 : 266725 : tts_minimal_init(TupleTableSlot *slot)
510 : : {
511 : 266725 : 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 it's a heap tuple.
516 : : */
517 : 266725 : mslot->tuple = &mslot->minhdr;
518 : 266725 : }
519 : :
520 : : static void
521 : 233794 : tts_minimal_release(TupleTableSlot *slot)
522 : : {
523 : 233794 : }
524 : :
525 : : static void
526 : 52251572 : tts_minimal_clear(TupleTableSlot *slot)
527 : : {
528 : 52251572 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
529 : :
530 [ + + ]: 52251572 : if (TTS_SHOULDFREE(slot))
531 : : {
532 : 8576541 : heap_free_minimal_tuple(mslot->mintuple);
533 : 8576541 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
534 : : }
535 : :
536 : 52251572 : slot->tts_nvalid = 0;
537 : 52251572 : slot->tts_flags |= TTS_FLAG_EMPTY;
538 : 52251572 : ItemPointerSetInvalid(&slot->tts_tid);
539 : 52251572 : mslot->off = 0;
540 : 52251572 : mslot->mintuple = NULL;
541 : 52251572 : }
542 : :
543 : : static void
544 : 39134888 : tts_minimal_getsomeattrs(TupleTableSlot *slot, int natts)
545 : : {
546 : 39134888 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
547 : :
548 : : Assert(!TTS_EMPTY(slot));
549 : :
550 : 39134888 : slot_deform_heap_tuple(slot, mslot->tuple, &mslot->off, natts, true);
551 : 39134888 : }
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 : 1063732 : tts_minimal_materialize(TupleTableSlot *slot)
588 : : {
589 : 1063732 : 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 [ + + ]: 1063732 : if (TTS_SHOULDFREE(slot))
596 : 96006 : return;
597 : :
598 : 967726 : 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 : 967726 : slot->tts_nvalid = 0;
605 : 967726 : mslot->off = 0;
606 : :
607 [ + + ]: 967726 : if (!mslot->mintuple)
608 : : {
609 : 912054 : mslot->mintuple = heap_form_minimal_tuple(slot->tts_tupleDescriptor,
610 : 912054 : slot->tts_values,
611 : 912054 : 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 : 55672 : mslot->mintuple = heap_copy_minimal_tuple(mslot->mintuple, 0);
623 : : }
624 : :
625 : 967726 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
626 : :
627 : : Assert(mslot->tuple == &mslot->minhdr);
628 : :
629 : 967726 : mslot->minhdr.t_len = mslot->mintuple->t_len + MINIMAL_TUPLE_OFFSET;
630 : 967726 : mslot->minhdr.t_data = (HeapTupleHeader) ((char *) mslot->mintuple - MINIMAL_TUPLE_OFFSET);
631 : :
632 : 967726 : MemoryContextSwitchTo(oldContext);
633 : : }
634 : :
635 : : static void
636 : 785159 : tts_minimal_copyslot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
637 : : {
638 : : MemoryContext oldcontext;
639 : : MinimalTuple mintuple;
640 : :
641 : 785159 : oldcontext = MemoryContextSwitchTo(dstslot->tts_mcxt);
642 : 785159 : mintuple = ExecCopySlotMinimalTuple(srcslot);
643 : 785159 : MemoryContextSwitchTo(oldcontext);
644 : :
645 : 785159 : ExecStoreMinimalTuple(mintuple, dstslot, true);
646 : 785159 : }
647 : :
648 : : static MinimalTuple
649 : 3424914 : tts_minimal_get_minimal_tuple(TupleTableSlot *slot)
650 : : {
651 : 3424914 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
652 : :
653 [ + + ]: 3424914 : if (!mslot->mintuple)
654 : 9215 : tts_minimal_materialize(slot);
655 : :
656 : 3424914 : return mslot->mintuple;
657 : : }
658 : :
659 : : static HeapTuple
660 : 647700 : tts_minimal_copy_heap_tuple(TupleTableSlot *slot)
661 : : {
662 : 647700 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
663 : :
664 [ + + ]: 647700 : if (!mslot->mintuple)
665 : 1154 : tts_minimal_materialize(slot);
666 : :
667 : 647700 : return heap_tuple_from_minimal_tuple(mslot->mintuple);
668 : : }
669 : :
670 : : static MinimalTuple
671 : 1905732 : tts_minimal_copy_minimal_tuple(TupleTableSlot *slot, Size extra)
672 : : {
673 : 1905732 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
674 : :
675 [ + + ]: 1905732 : if (!mslot->mintuple)
676 : 777526 : tts_minimal_materialize(slot);
677 : :
678 : 1905732 : return heap_copy_minimal_tuple(mslot->mintuple, extra);
679 : : }
680 : :
681 : : static void
682 : 43727224 : tts_minimal_store_tuple(TupleTableSlot *slot, MinimalTuple mtup, bool shouldFree)
683 : : {
684 : 43727224 : MinimalTupleTableSlot *mslot = (MinimalTupleTableSlot *) slot;
685 : :
686 : 43727224 : tts_minimal_clear(slot);
687 : :
688 : : Assert(!TTS_SHOULDFREE(slot));
689 : : Assert(TTS_EMPTY(slot));
690 : :
691 : 43727224 : slot->tts_flags &= ~TTS_FLAG_EMPTY;
692 : 43727224 : slot->tts_nvalid = 0;
693 : 43727224 : mslot->off = 0;
694 : :
695 : 43727224 : mslot->mintuple = mtup;
696 : : Assert(mslot->tuple == &mslot->minhdr);
697 : 43727224 : mslot->minhdr.t_len = mtup->t_len + MINIMAL_TUPLE_OFFSET;
698 : 43727224 : 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 [ + + ]: 43727224 : if (shouldFree)
702 : 7609899 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
703 : 43727224 : }
704 : :
705 : :
706 : : /*
707 : : * TupleTableSlotOps implementation for BufferHeapTupleTableSlot.
708 : : */
709 : :
710 : : static void
711 : 18540855 : tts_buffer_heap_init(TupleTableSlot *slot)
712 : : {
713 : 18540855 : }
714 : :
715 : : static void
716 : 18530367 : tts_buffer_heap_release(TupleTableSlot *slot)
717 : : {
718 : 18530367 : }
719 : :
720 : : static void
721 : 34831425 : tts_buffer_heap_clear(TupleTableSlot *slot)
722 : : {
723 : 34831425 : 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 [ + + ]: 34831425 : if (TTS_SHOULDFREE(slot))
731 : : {
732 : : /* We should have unpinned the buffer while materializing the tuple. */
733 : : Assert(!BufferIsValid(bslot->buffer));
734 : :
735 : 11057053 : heap_freetuple(bslot->base.tuple);
736 : 11057053 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
737 : : }
738 : :
739 [ + + ]: 34831425 : if (BufferIsValid(bslot->buffer))
740 : 9976526 : ReleaseBuffer(bslot->buffer);
741 : :
742 : 34831425 : slot->tts_nvalid = 0;
743 : 34831425 : slot->tts_flags |= TTS_FLAG_EMPTY;
744 : 34831425 : ItemPointerSetInvalid(&slot->tts_tid);
745 : 34831425 : bslot->base.tuple = NULL;
746 : 34831425 : bslot->base.off = 0;
747 : 34831425 : bslot->buffer = InvalidBuffer;
748 : 34831425 : }
749 : :
750 : : static void
751 : 85623396 : tts_buffer_heap_getsomeattrs(TupleTableSlot *slot, int natts)
752 : : {
753 : 85623396 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
754 : :
755 : : Assert(!TTS_EMPTY(slot));
756 : :
757 : 85623396 : slot_deform_heap_tuple(slot, bslot->base.tuple, &bslot->base.off, natts, false);
758 : 85623396 : }
759 : :
760 : : static Datum
761 : 72790 : tts_buffer_heap_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
762 : : {
763 : 72790 : 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 [ - + ]: 72790 : 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 : 72790 : 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 : 24088770 : tts_buffer_heap_materialize(TupleTableSlot *slot)
805 : : {
806 : 24088770 : 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 [ + + ]: 24088770 : if (TTS_SHOULDFREE(slot))
813 : 20451544 : return;
814 : :
815 : 3637226 : 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 : 3637226 : bslot->base.off = 0;
822 : 3637226 : slot->tts_nvalid = 0;
823 : :
824 [ + + ]: 3637226 : 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 : 3398926 : bslot->base.tuple = heap_form_tuple(slot->tts_tupleDescriptor,
834 : 3398926 : slot->tts_values,
835 : 3398926 : slot->tts_isnull);
836 : : }
837 : : else
838 : : {
839 : 238300 : 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 [ + - ]: 238300 : if (likely(BufferIsValid(bslot->buffer)))
846 : 238300 : ReleaseBuffer(bslot->buffer);
847 : 238300 : 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 : 3637226 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
858 : :
859 : 3637226 : MemoryContextSwitchTo(oldContext);
860 : : }
861 : :
862 : : static void
863 : 7657874 : tts_buffer_heap_copyslot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
864 : : {
865 : 7657874 : BufferHeapTupleTableSlot *bsrcslot = (BufferHeapTupleTableSlot *) srcslot;
866 : 7657874 : 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 [ + + ]: 7657874 : if (dstslot->tts_ops != srcslot->tts_ops ||
874 [ + + ]: 4348 : TTS_SHOULDFREE(srcslot) ||
875 [ - + ]: 4346 : !bsrcslot->base.tuple)
876 : 7653528 : {
877 : : MemoryContext oldContext;
878 : :
879 : 7653528 : ExecClearTuple(dstslot);
880 : 7653528 : dstslot->tts_flags &= ~TTS_FLAG_EMPTY;
881 : 7653528 : oldContext = MemoryContextSwitchTo(dstslot->tts_mcxt);
882 : 7653528 : bdstslot->base.tuple = ExecCopySlotHeapTuple(srcslot);
883 : 7653528 : dstslot->tts_flags |= TTS_FLAG_SHOULDFREE;
884 : 7653528 : MemoryContextSwitchTo(oldContext);
885 : : }
886 : : else
887 : : {
888 : : Assert(BufferIsValid(bsrcslot->buffer));
889 : :
890 : 4346 : 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 : 4346 : memcpy(&bdstslot->base.tupdata, bdstslot->base.tuple, sizeof(HeapTupleData));
900 : 4346 : bdstslot->base.tuple = &bdstslot->base.tupdata;
901 : : }
902 : 7657874 : }
903 : :
904 : : static HeapTuple
905 : 28559861 : tts_buffer_heap_get_heap_tuple(TupleTableSlot *slot)
906 : : {
907 : 28559861 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
908 : :
909 : : Assert(!TTS_EMPTY(slot));
910 : :
911 [ - + ]: 28559861 : if (!bslot->base.tuple)
912 : 0 : tts_buffer_heap_materialize(slot);
913 : :
914 : 28559861 : return bslot->base.tuple;
915 : : }
916 : :
917 : : static HeapTuple
918 : 8406583 : tts_buffer_heap_copy_heap_tuple(TupleTableSlot *slot)
919 : : {
920 : 8406583 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
921 : :
922 : : Assert(!TTS_EMPTY(slot));
923 : :
924 [ - + ]: 8406583 : if (!bslot->base.tuple)
925 : 0 : tts_buffer_heap_materialize(slot);
926 : :
927 : 8406583 : return heap_copytuple(bslot->base.tuple);
928 : : }
929 : :
930 : : static MinimalTuple
931 : 1872794 : tts_buffer_heap_copy_minimal_tuple(TupleTableSlot *slot, Size extra)
932 : : {
933 : 1872794 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
934 : :
935 : : Assert(!TTS_EMPTY(slot));
936 : :
937 [ - + ]: 1872794 : if (!bslot->base.tuple)
938 : 0 : tts_buffer_heap_materialize(slot);
939 : :
940 : 1872794 : return minimal_tuple_from_heap_tuple(bslot->base.tuple, extra);
941 : : }
942 : :
943 : : static inline void
944 : 104186244 : tts_buffer_heap_store_tuple(TupleTableSlot *slot, HeapTuple tuple,
945 : : Buffer buffer, bool transfer_pin)
946 : : {
947 : 104186244 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
948 : :
949 [ + + ]: 104186244 : if (TTS_SHOULDFREE(slot))
950 : : {
951 : : /* materialized slot shouldn't have a buffer to release */
952 : : Assert(!BufferIsValid(bslot->buffer));
953 : :
954 : 239429 : heap_freetuple(bslot->base.tuple);
955 : 239429 : slot->tts_flags &= ~TTS_FLAG_SHOULDFREE;
956 : : }
957 : :
958 : 104186244 : slot->tts_flags &= ~TTS_FLAG_EMPTY;
959 : 104186244 : slot->tts_nvalid = 0;
960 : 104186244 : bslot->base.tuple = tuple;
961 : 104186244 : bslot->base.off = 0;
962 : 104186244 : 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 [ + + ]: 104186244 : if (bslot->buffer != buffer)
976 : : {
977 [ + + ]: 13406670 : if (BufferIsValid(bslot->buffer))
978 : 3186403 : ReleaseBuffer(bslot->buffer);
979 : :
980 : 13406670 : bslot->buffer = buffer;
981 : :
982 [ + + + - ]: 13406670 : if (!transfer_pin && BufferIsValid(buffer))
983 : 12609275 : IncrBufferRefCount(buffer);
984 : : }
985 [ + + + - ]: 90779574 : 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 : 2624241 : ReleaseBuffer(buffer);
992 : : }
993 : 104186244 : }
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 : : * support_cstring should be passed as a const to allow the compiler only
1013 : : * emit code during inlining for cstring deforming when it's required.
1014 : : * cstrings can exist in MinimalTuples, but not in HeapTuples.
1015 : : */
1016 : : static pg_always_inline void
1017 : 132216554 : slot_deform_heap_tuple(TupleTableSlot *slot, HeapTuple tuple, uint32 *offp,
1018 : : int reqnatts, bool support_cstring)
1019 : : {
1020 : : CompactAttribute *cattrs;
1021 : : CompactAttribute *cattr;
1022 : 132216554 : TupleDesc tupleDesc = slot->tts_tupleDescriptor;
1023 : 132216554 : HeapTupleHeader tup = tuple->t_data;
1024 : : size_t attnum;
1025 : : int firstNonCacheOffsetAttr;
1026 : : int firstNonGuaranteedAttr;
1027 : : int firstNullAttr;
1028 : : int natts;
1029 : : Datum *values;
1030 : : bool *isnull;
1031 : : char *tp; /* ptr to tuple data */
1032 : : uint32 off; /* offset in tuple data */
1033 : :
1034 : : /* Did someone forget to call TupleDescFinalize()? */
1035 : : Assert(tupleDesc->firstNonCachedOffsetAttr >= 0);
1036 : :
1037 : 132216554 : isnull = slot->tts_isnull;
1038 : :
1039 : : /*
1040 : : * Some callers may form and deform tuples prior to NOT NULL constraints
1041 : : * being checked. Here we'd like to optimize the case where we only need
1042 : : * to fetch attributes before or up to the point where the attribute is
1043 : : * guaranteed to exist in the tuple. We rely on the slot flag being set
1044 : : * correctly to only enable this optimization when it's valid to do so.
1045 : : * This optimization allows us to save fetching the number of attributes
1046 : : * from the tuple and saves the additional cost of handling non-byval
1047 : : * attrs.
1048 : : */
1049 : 132216554 : firstNonGuaranteedAttr = Min(reqnatts, slot->tts_first_nonguaranteed);
1050 : :
1051 : 132216554 : firstNonCacheOffsetAttr = tupleDesc->firstNonCachedOffsetAttr;
1052 : :
1053 [ + + ]: 132216554 : if (HeapTupleHasNulls(tuple))
1054 : : {
1055 : 29867498 : natts = HeapTupleHeaderGetNatts(tup);
1056 : 29867498 : tp = (char *) tup + MAXALIGN(offsetof(HeapTupleHeaderData, t_bits) +
1057 : : BITMAPLEN(natts));
1058 : :
1059 : 29867498 : natts = Min(natts, reqnatts);
1060 [ + + ]: 29867498 : if (natts > firstNonGuaranteedAttr)
1061 : : {
1062 : 27794444 : uint8 *bp = tup->t_bits;
1063 : :
1064 : : /* Find the first NULL attr */
1065 : 27794444 : firstNullAttr = first_null_attr(bp, natts);
1066 : :
1067 : : /*
1068 : : * And populate the isnull array for all attributes being fetched
1069 : : * from the tuple.
1070 : : */
1071 : 27794444 : populate_isnull_array(bp, natts, isnull);
1072 : : }
1073 : : else
1074 : : {
1075 : : /* Otherwise all required columns are guaranteed to exist */
1076 : 2073054 : firstNullAttr = natts;
1077 : :
1078 : : /*
1079 : : * Check TupleDescFinalize() didn't get confused when setting
1080 : : * firstNonGuaranteedAttr. There should never be a NULL in a
1081 : : * guaranteed column.
1082 : : */
1083 : : Assert(first_null_attr(tup->t_bits, natts) >= firstNullAttr);
1084 : : }
1085 : : }
1086 : : else
1087 : : {
1088 : 102349056 : tp = (char *) tup + MAXALIGN(offsetof(HeapTupleHeaderData, t_bits));
1089 : :
1090 : : /*
1091 : : * We only need to look at the tuple's natts if we need more than the
1092 : : * guaranteed number of columns
1093 : : */
1094 [ + + ]: 102349056 : if (reqnatts > firstNonGuaranteedAttr)
1095 : 98229760 : natts = Min(HeapTupleHeaderGetNatts(tup), reqnatts);
1096 : : else
1097 : : {
1098 : : /* No need to access the number of attributes in the tuple */
1099 : 4119296 : natts = reqnatts;
1100 : : }
1101 : :
1102 : : /* All attrs can be fetched without checking for NULLs */
1103 : 102349056 : firstNullAttr = natts;
1104 : : }
1105 : :
1106 : 132216554 : attnum = slot->tts_nvalid;
1107 : 132216554 : values = slot->tts_values;
1108 : 132216554 : slot->tts_nvalid = reqnatts;
1109 : :
1110 : : /*
1111 : : * We store the tupleDesc's CompactAttribute array in 'cattrs' as gcc
1112 : : * seems to be unwilling to optimize accessing the CompactAttribute
1113 : : * element efficiently when accessing it via TupleDescCompactAttr().
1114 : : */
1115 : 132216554 : cattrs = tupleDesc->compact_attrs;
1116 : :
1117 : : /* Ensure we calculated tp correctly */
1118 : : Assert(tp == (char *) tup + tup->t_hoff);
1119 : :
1120 [ + + ]: 132216554 : if (attnum < firstNonGuaranteedAttr)
1121 : : {
1122 : : int attlen;
1123 : :
1124 : : do
1125 : : {
1126 : 40016648 : isnull[attnum] = false;
1127 : 40016648 : cattr = &cattrs[attnum];
1128 : 40016648 : attlen = cattr->attlen;
1129 : :
1130 : : /* We don't expect any non-byval types */
1131 [ - + ]: 40016648 : pg_assume(attlen > 0);
1132 : : Assert(cattr->attbyval == true);
1133 : :
1134 : 40016648 : off = cattr->attcacheoff;
1135 : 40016648 : values[attnum] = fetch_att_noerr(tp + off, true, attlen);
1136 : 40016648 : attnum++;
1137 [ + + ]: 40016648 : } while (attnum < firstNonGuaranteedAttr);
1138 : :
1139 : 22406408 : off += attlen;
1140 : :
1141 [ + + ]: 22406408 : if (attnum == reqnatts)
1142 : 6192350 : goto done;
1143 : : }
1144 : : else
1145 : : {
1146 : : /*
1147 : : * We may be incrementally deforming the tuple, so set 'off' to the
1148 : : * previously cached value. This may be 0, if the slot has just
1149 : : * received a new tuple.
1150 : : */
1151 : 109810146 : off = *offp;
1152 : :
1153 : : /* We expect *offp to be set to 0 when attnum == 0 */
1154 : : Assert(off == 0 || attnum > 0);
1155 : : }
1156 : :
1157 : : /* We can use attcacheoff up until the first NULL */
1158 : 126024204 : firstNonCacheOffsetAttr = Min(firstNonCacheOffsetAttr, firstNullAttr);
1159 : :
1160 : : /*
1161 : : * Handle the portion of the tuple that we have cached the offset for up
1162 : : * to the first NULL attribute. The offset is effectively fixed for
1163 : : * these, so we can use the CompactAttribute's attcacheoff.
1164 : : */
1165 [ + + ]: 126024204 : if (attnum < firstNonCacheOffsetAttr)
1166 : : {
1167 : : int attlen;
1168 : :
1169 : : do
1170 : : {
1171 : 357873622 : isnull[attnum] = false;
1172 : 357873622 : cattr = &cattrs[attnum];
1173 : 357873622 : attlen = cattr->attlen;
1174 : 357873622 : off = cattr->attcacheoff;
1175 : 715747244 : values[attnum] = fetch_att_noerr(tp + off,
1176 : 357873622 : cattr->attbyval,
1177 : : attlen);
1178 : 357873622 : attnum++;
1179 [ + + ]: 357873622 : } while (attnum < firstNonCacheOffsetAttr);
1180 : :
1181 : : /*
1182 : : * Point the offset after the end of the last attribute with a cached
1183 : : * offset. We expect the final cached offset attribute to have a
1184 : : * fixed width, so just add the attlen to the attcacheoff
1185 : : */
1186 : : Assert(attlen > 0);
1187 : 110001638 : off += attlen;
1188 : : }
1189 : :
1190 : : /*
1191 : : * Handle any portion of the tuple that doesn't have a fixed offset up
1192 : : * until the first NULL attribute. This loop only differs from the one
1193 : : * after it by the NULL checks.
1194 : : */
1195 [ + + ]: 160343598 : for (; attnum < firstNullAttr; attnum++)
1196 : : {
1197 : : int attlen;
1198 : :
1199 : 34319394 : isnull[attnum] = false;
1200 : 34319394 : cattr = &cattrs[attnum];
1201 : 34319394 : attlen = cattr->attlen;
1202 : :
1203 : : /*
1204 : : * Only emit the cstring-related code in align_fetch_then_add() when
1205 : : * cstring support is needed. We assume support_cstring will be
1206 : : * passed as a const to allow the compiler to eliminate this branch.
1207 : : */
1208 [ + + ]: 34319394 : if (!support_cstring)
1209 [ + + - + ]: 20350541 : pg_assume(attlen > 0 || attlen == -1);
1210 : :
1211 : : /* align 'off', fetch the datum, and increment off beyond the datum */
1212 : 34319394 : values[attnum] = align_fetch_then_add(tp,
1213 : : &off,
1214 : 34319394 : cattr->attbyval,
1215 : : attlen,
1216 : 34319394 : cattr->attalignby);
1217 : : }
1218 : :
1219 : : /*
1220 : : * Now handle any remaining attributes in the tuple up to the requested
1221 : : * attnum. This time, include NULL checks as we're now at the first NULL
1222 : : * attribute.
1223 : : */
1224 [ + + ]: 174456896 : for (; attnum < natts; attnum++)
1225 : : {
1226 : : int attlen;
1227 : :
1228 [ + + ]: 48432692 : if (isnull[attnum])
1229 : : {
1230 : 33493877 : values[attnum] = (Datum) 0;
1231 : 33493877 : continue;
1232 : : }
1233 : :
1234 : 14938815 : cattr = &cattrs[attnum];
1235 : 14938815 : attlen = cattr->attlen;
1236 : :
1237 : : /* As above, only emit cstring code when needed. */
1238 [ + + ]: 14938815 : if (!support_cstring)
1239 [ + + - + ]: 10476913 : pg_assume(attlen > 0 || attlen == -1);
1240 : :
1241 : : /* align 'off', fetch the datum, and increment off beyond the datum */
1242 : 14938815 : values[attnum] = align_fetch_then_add(tp,
1243 : : &off,
1244 : 14938815 : cattr->attbyval,
1245 : : attlen,
1246 : 14938815 : cattr->attalignby);
1247 : : }
1248 : :
1249 : : /* Fetch any missing attrs and raise an error if reqnatts is invalid */
1250 [ + + ]: 126024204 : if (unlikely(attnum < reqnatts))
1251 : : {
1252 : : /*
1253 : : * Cache the offset before calling the function to allow the compiler
1254 : : * to implement a tail-call optimization
1255 : : */
1256 : 4870 : *offp = off;
1257 : :
1258 : : Assert(HeapTupleHeaderGetNatts(tup) <= attnum);
1259 : :
1260 : : /*
1261 : : * Fetch all missing attributes. We pass natts rather than attnum as
1262 : : * if we're deforming attributes after having already deformed some
1263 : : * missing attributes, then the call to populate_isnull_array() may
1264 : : * have overwritten the previous tts_isnull values from what was
1265 : : * stored in the previous call to slot_getmissingattrs().
1266 : : */
1267 : 4870 : slot_getmissingattrs(slot, HeapTupleHeaderGetNatts(tup), reqnatts);
1268 : 4870 : return;
1269 : : }
1270 : 126019334 : done:
1271 : :
1272 : : /* Save current offset for next execution */
1273 : 132211684 : *offp = off;
1274 : : }
1275 : :
1276 : : const TupleTableSlotOps TTSOpsVirtual = {
1277 : : .base_slot_size = sizeof(VirtualTupleTableSlot),
1278 : : .init = tts_virtual_init,
1279 : : .release = tts_virtual_release,
1280 : : .clear = tts_virtual_clear,
1281 : : .getsomeattrs = tts_virtual_getsomeattrs,
1282 : : .getsysattr = tts_virtual_getsysattr,
1283 : : .materialize = tts_virtual_materialize,
1284 : : .is_current_xact_tuple = tts_virtual_is_current_xact_tuple,
1285 : : .copyslot = tts_virtual_copyslot,
1286 : :
1287 : : /*
1288 : : * A virtual tuple table slot can not "own" a heap tuple or a minimal
1289 : : * tuple.
1290 : : */
1291 : : .get_heap_tuple = NULL,
1292 : : .get_minimal_tuple = NULL,
1293 : : .copy_heap_tuple = tts_virtual_copy_heap_tuple,
1294 : : .copy_minimal_tuple = tts_virtual_copy_minimal_tuple
1295 : : };
1296 : :
1297 : : const TupleTableSlotOps TTSOpsHeapTuple = {
1298 : : .base_slot_size = sizeof(HeapTupleTableSlot),
1299 : : .init = tts_heap_init,
1300 : : .release = tts_heap_release,
1301 : : .clear = tts_heap_clear,
1302 : : .getsomeattrs = tts_heap_getsomeattrs,
1303 : : .getsysattr = tts_heap_getsysattr,
1304 : : .is_current_xact_tuple = tts_heap_is_current_xact_tuple,
1305 : : .materialize = tts_heap_materialize,
1306 : : .copyslot = tts_heap_copyslot,
1307 : : .get_heap_tuple = tts_heap_get_heap_tuple,
1308 : :
1309 : : /* A heap tuple table slot can not "own" a minimal tuple. */
1310 : : .get_minimal_tuple = NULL,
1311 : : .copy_heap_tuple = tts_heap_copy_heap_tuple,
1312 : : .copy_minimal_tuple = tts_heap_copy_minimal_tuple
1313 : : };
1314 : :
1315 : : const TupleTableSlotOps TTSOpsMinimalTuple = {
1316 : : .base_slot_size = sizeof(MinimalTupleTableSlot),
1317 : : .init = tts_minimal_init,
1318 : : .release = tts_minimal_release,
1319 : : .clear = tts_minimal_clear,
1320 : : .getsomeattrs = tts_minimal_getsomeattrs,
1321 : : .getsysattr = tts_minimal_getsysattr,
1322 : : .is_current_xact_tuple = tts_minimal_is_current_xact_tuple,
1323 : : .materialize = tts_minimal_materialize,
1324 : : .copyslot = tts_minimal_copyslot,
1325 : :
1326 : : /* A minimal tuple table slot can not "own" a heap tuple. */
1327 : : .get_heap_tuple = NULL,
1328 : : .get_minimal_tuple = tts_minimal_get_minimal_tuple,
1329 : : .copy_heap_tuple = tts_minimal_copy_heap_tuple,
1330 : : .copy_minimal_tuple = tts_minimal_copy_minimal_tuple
1331 : : };
1332 : :
1333 : : const TupleTableSlotOps TTSOpsBufferHeapTuple = {
1334 : : .base_slot_size = sizeof(BufferHeapTupleTableSlot),
1335 : : .init = tts_buffer_heap_init,
1336 : : .release = tts_buffer_heap_release,
1337 : : .clear = tts_buffer_heap_clear,
1338 : : .getsomeattrs = tts_buffer_heap_getsomeattrs,
1339 : : .getsysattr = tts_buffer_heap_getsysattr,
1340 : : .is_current_xact_tuple = tts_buffer_is_current_xact_tuple,
1341 : : .materialize = tts_buffer_heap_materialize,
1342 : : .copyslot = tts_buffer_heap_copyslot,
1343 : : .get_heap_tuple = tts_buffer_heap_get_heap_tuple,
1344 : :
1345 : : /* A buffer heap tuple table slot can not "own" a minimal tuple. */
1346 : : .get_minimal_tuple = NULL,
1347 : : .copy_heap_tuple = tts_buffer_heap_copy_heap_tuple,
1348 : : .copy_minimal_tuple = tts_buffer_heap_copy_minimal_tuple
1349 : : };
1350 : :
1351 : :
1352 : : /* ----------------------------------------------------------------
1353 : : * tuple table create/delete functions
1354 : : * ----------------------------------------------------------------
1355 : : */
1356 : :
1357 : : /* --------------------------------
1358 : : * MakeTupleTableSlot
1359 : : *
1360 : : * Basic routine to make an empty TupleTableSlot of given
1361 : : * TupleTableSlotType. If tupleDesc is specified the slot's descriptor is
1362 : : * fixed for its lifetime, gaining some efficiency. If that's
1363 : : * undesirable, pass NULL. 'flags' allows any of non-TTS_FLAGS_TRANSIENT
1364 : : * flags to be set in tts_flags.
1365 : : * --------------------------------
1366 : : */
1367 : : TupleTableSlot *
1368 : 22330380 : MakeTupleTableSlot(TupleDesc tupleDesc,
1369 : : const TupleTableSlotOps *tts_ops, uint16 flags)
1370 : : {
1371 : : Size basesz,
1372 : : allocsz;
1373 : : TupleTableSlot *slot;
1374 : :
1375 : 22330380 : basesz = tts_ops->base_slot_size;
1376 : :
1377 : : /* Ensure callers don't have any way to set transient flags permanently */
1378 : 22330380 : flags &= ~TTS_FLAGS_TRANSIENT;
1379 : :
1380 : : /*
1381 : : * When a fixed descriptor is specified, we can reduce overhead by
1382 : : * allocating the entire slot in one go.
1383 : : *
1384 : : * We round the size of tts_isnull up to the next highest multiple of 8.
1385 : : * This is needed as populate_isnull_array() operates on 8 elements at a
1386 : : * time when converting a tuple's NULL bitmap into a boolean array.
1387 : : */
1388 [ + + ]: 22330380 : if (tupleDesc)
1389 : 22293414 : allocsz = MAXALIGN(basesz) +
1390 : 22293414 : MAXALIGN(tupleDesc->natts * sizeof(Datum)) +
1391 : 22293414 : TYPEALIGN(8, tupleDesc->natts * sizeof(bool));
1392 : : else
1393 : 36966 : allocsz = basesz;
1394 : :
1395 : 22330380 : slot = palloc0(allocsz);
1396 : : /* const for optimization purposes, OK to modify at allocation time */
1397 : 22330380 : *((const TupleTableSlotOps **) &slot->tts_ops) = tts_ops;
1398 : 22330380 : slot->type = T_TupleTableSlot;
1399 : 22330380 : slot->tts_flags = TTS_FLAG_EMPTY | flags;
1400 [ + + ]: 22330380 : if (tupleDesc != NULL)
1401 : 22293414 : slot->tts_flags |= TTS_FLAG_FIXED;
1402 : 22330380 : slot->tts_tupleDescriptor = tupleDesc;
1403 : 22330380 : slot->tts_mcxt = CurrentMemoryContext;
1404 : 22330380 : slot->tts_nvalid = 0;
1405 : :
1406 [ + + ]: 22330380 : if (tupleDesc != NULL)
1407 : : {
1408 : 22293414 : slot->tts_values = (Datum *)
1409 : : (((char *) slot)
1410 : 22293414 : + MAXALIGN(basesz));
1411 : :
1412 : 22293414 : slot->tts_isnull = (bool *)
1413 : : (((char *) slot)
1414 : 22293414 : + MAXALIGN(basesz)
1415 : 22293414 : + MAXALIGN(tupleDesc->natts * sizeof(Datum)));
1416 : :
1417 [ + + ]: 22293414 : PinTupleDesc(tupleDesc);
1418 : :
1419 : : /*
1420 : : * Precalculate the maximum guaranteed attribute that has to exist in
1421 : : * every tuple which gets deformed into this slot. When the
1422 : : * TTS_FLAG_OBEYS_NOT_NULL_CONSTRAINTS flag is enabled, we simply take
1423 : : * the pre-calculated value from the tupleDesc, otherwise the
1424 : : * optimization is disabled, and we set the value to 0.
1425 : : */
1426 [ + + ]: 22293414 : if ((flags & TTS_FLAG_OBEYS_NOT_NULL_CONSTRAINTS) != 0)
1427 : 286983 : slot->tts_first_nonguaranteed = tupleDesc->firstNonGuaranteedAttr;
1428 : : else
1429 : 22006431 : slot->tts_first_nonguaranteed = 0;
1430 : : }
1431 : :
1432 : : /*
1433 : : * And allow slot type specific initialization.
1434 : : */
1435 : 22330380 : slot->tts_ops->init(slot);
1436 : :
1437 : 22330380 : return slot;
1438 : : }
1439 : :
1440 : : /* --------------------------------
1441 : : * ExecAllocTableSlot
1442 : : *
1443 : : * Create a tuple table slot within a tuple table (which is just a List).
1444 : : * --------------------------------
1445 : : */
1446 : : TupleTableSlot *
1447 : 1372561 : ExecAllocTableSlot(List **tupleTable, TupleDesc desc,
1448 : : const TupleTableSlotOps *tts_ops, uint16 flags)
1449 : : {
1450 : 1372561 : TupleTableSlot *slot = MakeTupleTableSlot(desc, tts_ops, flags);
1451 : :
1452 : 1372561 : *tupleTable = lappend(*tupleTable, slot);
1453 : :
1454 : 1372561 : return slot;
1455 : : }
1456 : :
1457 : : /* --------------------------------
1458 : : * ExecResetTupleTable
1459 : : *
1460 : : * This releases any resources (buffer pins, tupdesc refcounts)
1461 : : * held by the tuple table, and optionally releases the memory
1462 : : * occupied by the tuple table data structure.
1463 : : * It is expected that this routine be called by ExecEndPlan().
1464 : : * --------------------------------
1465 : : */
1466 : : void
1467 : 518734 : ExecResetTupleTable(List *tupleTable, /* tuple table */
1468 : : bool shouldFree) /* true if we should free memory */
1469 : : {
1470 : : ListCell *lc;
1471 : :
1472 [ + + + + : 2018942 : foreach(lc, tupleTable)
+ + ]
1473 : : {
1474 : 1500208 : TupleTableSlot *slot = lfirst_node(TupleTableSlot, lc);
1475 : :
1476 : : /* Always release resources and reset the slot to empty */
1477 : 1500208 : ExecClearTuple(slot);
1478 : 1500208 : slot->tts_ops->release(slot);
1479 [ + + ]: 1500208 : if (slot->tts_tupleDescriptor)
1480 : : {
1481 [ + + ]: 1500172 : ReleaseTupleDesc(slot->tts_tupleDescriptor);
1482 : 1500172 : slot->tts_tupleDescriptor = NULL;
1483 : : }
1484 : :
1485 : : /* If shouldFree, release memory occupied by the slot itself */
1486 [ + + ]: 1500208 : if (shouldFree)
1487 : : {
1488 [ - + ]: 4900 : if (!TTS_FIXED(slot))
1489 : : {
1490 [ # # ]: 0 : if (slot->tts_values)
1491 : 0 : pfree(slot->tts_values);
1492 [ # # ]: 0 : if (slot->tts_isnull)
1493 : 0 : pfree(slot->tts_isnull);
1494 : : }
1495 : 4900 : pfree(slot);
1496 : : }
1497 : : }
1498 : :
1499 : : /* If shouldFree, release the list structure */
1500 [ + + ]: 518734 : if (shouldFree)
1501 : 4814 : list_free(tupleTable);
1502 : 518734 : }
1503 : :
1504 : : /* --------------------------------
1505 : : * MakeSingleTupleTableSlot
1506 : : *
1507 : : * This is a convenience routine for operations that need a standalone
1508 : : * TupleTableSlot not gotten from the main executor tuple table. It makes
1509 : : * a single slot of given TupleTableSlotType and initializes it to use the
1510 : : * given tuple descriptor.
1511 : : * --------------------------------
1512 : : */
1513 : : TupleTableSlot *
1514 : 20957701 : MakeSingleTupleTableSlot(TupleDesc tupdesc,
1515 : : const TupleTableSlotOps *tts_ops)
1516 : : {
1517 : 20957701 : TupleTableSlot *slot = MakeTupleTableSlot(tupdesc, tts_ops, 0);
1518 : :
1519 : 20957701 : return slot;
1520 : : }
1521 : :
1522 : : /* --------------------------------
1523 : : * ExecDropSingleTupleTableSlot
1524 : : *
1525 : : * Release a TupleTableSlot made with MakeSingleTupleTableSlot.
1526 : : * DON'T use this on a slot that's part of a tuple table list!
1527 : : * --------------------------------
1528 : : */
1529 : : void
1530 : 20765265 : ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
1531 : : {
1532 : : /* This should match ExecResetTupleTable's processing of one slot */
1533 : : Assert(IsA(slot, TupleTableSlot));
1534 : 20765265 : ExecClearTuple(slot);
1535 : 20765265 : slot->tts_ops->release(slot);
1536 [ + - ]: 20765265 : if (slot->tts_tupleDescriptor)
1537 [ + + ]: 20765265 : ReleaseTupleDesc(slot->tts_tupleDescriptor);
1538 [ - + ]: 20765265 : if (!TTS_FIXED(slot))
1539 : : {
1540 [ # # ]: 0 : if (slot->tts_values)
1541 : 0 : pfree(slot->tts_values);
1542 [ # # ]: 0 : if (slot->tts_isnull)
1543 : 0 : pfree(slot->tts_isnull);
1544 : : }
1545 : 20765265 : pfree(slot);
1546 : 20765265 : }
1547 : :
1548 : :
1549 : : /* ----------------------------------------------------------------
1550 : : * tuple table slot accessor functions
1551 : : * ----------------------------------------------------------------
1552 : : */
1553 : :
1554 : : /* --------------------------------
1555 : : * ExecSetSlotDescriptor
1556 : : *
1557 : : * This function is used to set the tuple descriptor associated
1558 : : * with the slot's tuple. The passed descriptor must have lifespan
1559 : : * at least equal to the slot's. If it is a reference-counted descriptor
1560 : : * then the reference count is incremented for as long as the slot holds
1561 : : * a reference.
1562 : : * --------------------------------
1563 : : */
1564 : : void
1565 : 36930 : ExecSetSlotDescriptor(TupleTableSlot *slot, /* slot to change */
1566 : : TupleDesc tupdesc) /* new tuple descriptor */
1567 : : {
1568 : : Assert(!TTS_FIXED(slot));
1569 : :
1570 : : /* For safety, make sure slot is empty before changing it */
1571 : 36930 : ExecClearTuple(slot);
1572 : :
1573 : : /*
1574 : : * Release any old descriptor. Also release old Datum/isnull arrays if
1575 : : * present (we don't bother to check if they could be re-used).
1576 : : */
1577 [ - + ]: 36930 : if (slot->tts_tupleDescriptor)
1578 [ # # ]: 0 : ReleaseTupleDesc(slot->tts_tupleDescriptor);
1579 : :
1580 [ - + ]: 36930 : if (slot->tts_values)
1581 : 0 : pfree(slot->tts_values);
1582 [ - + ]: 36930 : if (slot->tts_isnull)
1583 : 0 : pfree(slot->tts_isnull);
1584 : :
1585 : : /*
1586 : : * Install the new descriptor; if it's refcounted, bump its refcount.
1587 : : */
1588 : 36930 : slot->tts_tupleDescriptor = tupdesc;
1589 [ - + ]: 36930 : PinTupleDesc(tupdesc);
1590 : :
1591 : : /*
1592 : : * Allocate Datum/isnull arrays of the appropriate size. These must have
1593 : : * the same lifetime as the slot, so allocate in the slot's own context.
1594 : : */
1595 : 36930 : slot->tts_values = (Datum *)
1596 : 36930 : MemoryContextAlloc(slot->tts_mcxt, tupdesc->natts * sizeof(Datum));
1597 : :
1598 : : /*
1599 : : * We round the size of tts_isnull up to the next highest multiple of 8.
1600 : : * This is needed as populate_isnull_array() operates on 8 elements at a
1601 : : * time when converting a tuple's NULL bitmap into a boolean array.
1602 : : */
1603 : 36930 : slot->tts_isnull = (bool *)
1604 : 36930 : MemoryContextAlloc(slot->tts_mcxt, TYPEALIGN(8, tupdesc->natts * sizeof(bool)));
1605 : 36930 : }
1606 : :
1607 : : /* --------------------------------
1608 : : * ExecStoreHeapTuple
1609 : : *
1610 : : * This function is used to store an on-the-fly physical tuple into a specified
1611 : : * slot in the tuple table.
1612 : : *
1613 : : * tuple: tuple to store
1614 : : * slot: TTSOpsHeapTuple type slot to store it in
1615 : : * shouldFree: true if ExecClearTuple should pfree() the tuple
1616 : : * when done with it
1617 : : *
1618 : : * shouldFree is normally set 'true' for tuples constructed on-the-fly. But it
1619 : : * can be 'false' when the referenced tuple is held in a tuple table slot
1620 : : * belonging to a lower-level executor Proc node. In this case the lower-level
1621 : : * slot retains ownership and responsibility for eventually releasing the
1622 : : * tuple. When this method is used, we must be certain that the upper-level
1623 : : * Proc node will lose interest in the tuple sooner than the lower-level one
1624 : : * does! If you're not certain, copy the lower-level tuple with heap_copytuple
1625 : : * and let the upper-level table slot assume ownership of the copy!
1626 : : *
1627 : : * Return value is just the passed-in slot pointer.
1628 : : *
1629 : : * If the target slot is not guaranteed to be TTSOpsHeapTuple type slot, use
1630 : : * the, more expensive, ExecForceStoreHeapTuple().
1631 : : * --------------------------------
1632 : : */
1633 : : TupleTableSlot *
1634 : 3578323 : ExecStoreHeapTuple(HeapTuple tuple,
1635 : : TupleTableSlot *slot,
1636 : : bool shouldFree)
1637 : : {
1638 : : /*
1639 : : * sanity checks
1640 : : */
1641 : : Assert(tuple != NULL);
1642 : : Assert(slot != NULL);
1643 : : Assert(slot->tts_tupleDescriptor != NULL);
1644 : :
1645 [ - + ]: 3578323 : if (unlikely(!TTS_IS_HEAPTUPLE(slot)))
1646 [ # # ]: 0 : elog(ERROR, "trying to store a heap tuple into wrong type of slot");
1647 : 3578323 : tts_heap_store_tuple(slot, tuple, shouldFree);
1648 : :
1649 : 3578323 : slot->tts_tableOid = tuple->t_tableOid;
1650 : :
1651 : 3578323 : return slot;
1652 : : }
1653 : :
1654 : : /* --------------------------------
1655 : : * ExecStoreBufferHeapTuple
1656 : : *
1657 : : * This function is used to store an on-disk physical tuple from a buffer
1658 : : * into a specified slot in the tuple table.
1659 : : *
1660 : : * tuple: tuple to store
1661 : : * slot: TTSOpsBufferHeapTuple type slot to store it in
1662 : : * buffer: disk buffer if tuple is in a disk page, else InvalidBuffer
1663 : : *
1664 : : * The tuple table code acquires a pin on the buffer which is held until the
1665 : : * slot is cleared, so that the tuple won't go away on us.
1666 : : *
1667 : : * Return value is just the passed-in slot pointer.
1668 : : *
1669 : : * If the target slot is not guaranteed to be TTSOpsBufferHeapTuple type slot,
1670 : : * use the, more expensive, ExecForceStoreHeapTuple().
1671 : : * --------------------------------
1672 : : */
1673 : : TupleTableSlot *
1674 : 100760262 : ExecStoreBufferHeapTuple(HeapTuple tuple,
1675 : : TupleTableSlot *slot,
1676 : : Buffer buffer)
1677 : : {
1678 : : /*
1679 : : * sanity checks
1680 : : */
1681 : : Assert(tuple != NULL);
1682 : : Assert(slot != NULL);
1683 : : Assert(slot->tts_tupleDescriptor != NULL);
1684 : : Assert(BufferIsValid(buffer));
1685 : :
1686 [ - + ]: 100760262 : if (unlikely(!TTS_IS_BUFFERTUPLE(slot)))
1687 [ # # ]: 0 : elog(ERROR, "trying to store an on-disk heap tuple into wrong type of slot");
1688 : 100760262 : tts_buffer_heap_store_tuple(slot, tuple, buffer, false);
1689 : :
1690 : 100760262 : slot->tts_tableOid = tuple->t_tableOid;
1691 : :
1692 : 100760262 : return slot;
1693 : : }
1694 : :
1695 : : /*
1696 : : * Like ExecStoreBufferHeapTuple, but transfer an existing pin from the caller
1697 : : * to the slot, i.e. the caller doesn't need to, and may not, release the pin.
1698 : : */
1699 : : TupleTableSlot *
1700 : 3421636 : ExecStorePinnedBufferHeapTuple(HeapTuple tuple,
1701 : : TupleTableSlot *slot,
1702 : : Buffer buffer)
1703 : : {
1704 : : /*
1705 : : * sanity checks
1706 : : */
1707 : : Assert(tuple != NULL);
1708 : : Assert(slot != NULL);
1709 : : Assert(slot->tts_tupleDescriptor != NULL);
1710 : : Assert(BufferIsValid(buffer));
1711 : :
1712 [ - + ]: 3421636 : if (unlikely(!TTS_IS_BUFFERTUPLE(slot)))
1713 [ # # ]: 0 : elog(ERROR, "trying to store an on-disk heap tuple into wrong type of slot");
1714 : 3421636 : tts_buffer_heap_store_tuple(slot, tuple, buffer, true);
1715 : :
1716 : 3421636 : slot->tts_tableOid = tuple->t_tableOid;
1717 : :
1718 : 3421636 : return slot;
1719 : : }
1720 : :
1721 : : /*
1722 : : * Store a minimal tuple into TTSOpsMinimalTuple type slot.
1723 : : *
1724 : : * If the target slot is not guaranteed to be TTSOpsMinimalTuple type slot,
1725 : : * use the, more expensive, ExecForceStoreMinimalTuple().
1726 : : */
1727 : : TupleTableSlot *
1728 : 40662232 : ExecStoreMinimalTuple(MinimalTuple mtup,
1729 : : TupleTableSlot *slot,
1730 : : bool shouldFree)
1731 : : {
1732 : : /*
1733 : : * sanity checks
1734 : : */
1735 : : Assert(mtup != NULL);
1736 : : Assert(slot != NULL);
1737 : : Assert(slot->tts_tupleDescriptor != NULL);
1738 : :
1739 [ - + ]: 40662232 : if (unlikely(!TTS_IS_MINIMALTUPLE(slot)))
1740 [ # # ]: 0 : elog(ERROR, "trying to store a minimal tuple into wrong type of slot");
1741 : 40662232 : tts_minimal_store_tuple(slot, mtup, shouldFree);
1742 : :
1743 : 40662232 : return slot;
1744 : : }
1745 : :
1746 : : /*
1747 : : * Store a HeapTuple into any kind of slot, performing conversion if
1748 : : * necessary.
1749 : : */
1750 : : void
1751 : 148456 : ExecForceStoreHeapTuple(HeapTuple tuple,
1752 : : TupleTableSlot *slot,
1753 : : bool shouldFree)
1754 : : {
1755 [ + + ]: 148456 : if (TTS_IS_HEAPTUPLE(slot))
1756 : : {
1757 : 265 : ExecStoreHeapTuple(tuple, slot, shouldFree);
1758 : : }
1759 [ + + ]: 148191 : else if (TTS_IS_BUFFERTUPLE(slot))
1760 : : {
1761 : : MemoryContext oldContext;
1762 : 8167 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
1763 : :
1764 : 8167 : ExecClearTuple(slot);
1765 : 8167 : slot->tts_flags &= ~TTS_FLAG_EMPTY;
1766 : 8167 : oldContext = MemoryContextSwitchTo(slot->tts_mcxt);
1767 : 8167 : bslot->base.tuple = heap_copytuple(tuple);
1768 : 8167 : slot->tts_flags |= TTS_FLAG_SHOULDFREE;
1769 : 8167 : MemoryContextSwitchTo(oldContext);
1770 : :
1771 [ + + ]: 8167 : if (shouldFree)
1772 : 6952 : pfree(tuple);
1773 : : }
1774 : : else
1775 : : {
1776 : 140024 : ExecClearTuple(slot);
1777 : 140024 : heap_deform_tuple(tuple, slot->tts_tupleDescriptor,
1778 : : slot->tts_values, slot->tts_isnull);
1779 : 140024 : ExecStoreVirtualTuple(slot);
1780 : :
1781 [ + + ]: 140024 : if (shouldFree)
1782 : : {
1783 : 138317 : ExecMaterializeSlot(slot);
1784 : 138317 : pfree(tuple);
1785 : : }
1786 : : }
1787 : 148456 : }
1788 : :
1789 : : /*
1790 : : * Store a MinimalTuple into any kind of slot, performing conversion if
1791 : : * necessary.
1792 : : */
1793 : : void
1794 : 4824005 : ExecForceStoreMinimalTuple(MinimalTuple mtup,
1795 : : TupleTableSlot *slot,
1796 : : bool shouldFree)
1797 : : {
1798 [ + + ]: 4824005 : if (TTS_IS_MINIMALTUPLE(slot))
1799 : : {
1800 : 3064992 : tts_minimal_store_tuple(slot, mtup, shouldFree);
1801 : : }
1802 : : else
1803 : : {
1804 : : HeapTupleData htup;
1805 : :
1806 : 1759013 : ExecClearTuple(slot);
1807 : :
1808 : 1759013 : htup.t_len = mtup->t_len + MINIMAL_TUPLE_OFFSET;
1809 : 1759013 : htup.t_data = (HeapTupleHeader) ((char *) mtup - MINIMAL_TUPLE_OFFSET);
1810 : 1759013 : heap_deform_tuple(&htup, slot->tts_tupleDescriptor,
1811 : : slot->tts_values, slot->tts_isnull);
1812 : 1759013 : ExecStoreVirtualTuple(slot);
1813 : :
1814 [ + + ]: 1759013 : if (shouldFree)
1815 : : {
1816 : 958860 : ExecMaterializeSlot(slot);
1817 : 958860 : pfree(mtup);
1818 : : }
1819 : : }
1820 : 4824005 : }
1821 : :
1822 : : /* --------------------------------
1823 : : * ExecStoreVirtualTuple
1824 : : * Mark a slot as containing a virtual tuple.
1825 : : *
1826 : : * The protocol for loading a slot with virtual tuple data is:
1827 : : * * Call ExecClearTuple to mark the slot empty.
1828 : : * * Store data into the Datum/isnull arrays.
1829 : : * * Call ExecStoreVirtualTuple to mark the slot valid.
1830 : : * This is a bit unclean but it avoids one round of data copying.
1831 : : * --------------------------------
1832 : : */
1833 : : TupleTableSlot *
1834 : 19411853 : ExecStoreVirtualTuple(TupleTableSlot *slot)
1835 : : {
1836 : : /*
1837 : : * sanity checks
1838 : : */
1839 : : Assert(slot != NULL);
1840 : : Assert(slot->tts_tupleDescriptor != NULL);
1841 : : Assert(TTS_EMPTY(slot));
1842 : :
1843 : 19411853 : slot->tts_flags &= ~TTS_FLAG_EMPTY;
1844 : 19411853 : slot->tts_nvalid = slot->tts_tupleDescriptor->natts;
1845 : :
1846 : 19411853 : return slot;
1847 : : }
1848 : :
1849 : : /* --------------------------------
1850 : : * ExecStoreAllNullTuple
1851 : : * Set up the slot to contain a null in every column.
1852 : : *
1853 : : * At first glance this might sound just like ExecClearTuple, but it's
1854 : : * entirely different: the slot ends up full, not empty.
1855 : : * --------------------------------
1856 : : */
1857 : : TupleTableSlot *
1858 : 31209 : ExecStoreAllNullTuple(TupleTableSlot *slot)
1859 : : {
1860 : : /*
1861 : : * sanity checks
1862 : : */
1863 : : Assert(slot != NULL);
1864 : : Assert(slot->tts_tupleDescriptor != NULL);
1865 : :
1866 : : /* Clear any old contents */
1867 : 31209 : ExecClearTuple(slot);
1868 : :
1869 : : /*
1870 : : * Fill all the columns of the virtual tuple with nulls
1871 : : */
1872 [ + - + - : 213533 : MemSet(slot->tts_values, 0,
+ - + - +
+ ]
1873 : : slot->tts_tupleDescriptor->natts * sizeof(Datum));
1874 : 31209 : memset(slot->tts_isnull, true,
1875 : 31209 : slot->tts_tupleDescriptor->natts * sizeof(bool));
1876 : :
1877 : 31209 : return ExecStoreVirtualTuple(slot);
1878 : : }
1879 : :
1880 : : /*
1881 : : * Store a HeapTuple in datum form, into a slot. That always requires
1882 : : * deforming it and storing it in virtual form.
1883 : : *
1884 : : * Until the slot is materialized, the contents of the slot depend on the
1885 : : * datum.
1886 : : */
1887 : : void
1888 : 9 : ExecStoreHeapTupleDatum(Datum data, TupleTableSlot *slot)
1889 : : {
1890 : 9 : HeapTupleData tuple = {0};
1891 : : HeapTupleHeader td;
1892 : :
1893 : 9 : td = DatumGetHeapTupleHeader(data);
1894 : :
1895 : 9 : tuple.t_len = HeapTupleHeaderGetDatumLength(td);
1896 : 9 : tuple.t_self = td->t_ctid;
1897 : 9 : tuple.t_data = td;
1898 : :
1899 : 9 : ExecClearTuple(slot);
1900 : :
1901 : 9 : heap_deform_tuple(&tuple, slot->tts_tupleDescriptor,
1902 : : slot->tts_values, slot->tts_isnull);
1903 : 9 : ExecStoreVirtualTuple(slot);
1904 : 9 : }
1905 : :
1906 : : /*
1907 : : * ExecFetchSlotHeapTuple - fetch HeapTuple representing the slot's content
1908 : : *
1909 : : * The returned HeapTuple represents the slot's content as closely as
1910 : : * possible.
1911 : : *
1912 : : * If materialize is true, the contents of the slots will be made independent
1913 : : * from the underlying storage (i.e. all buffer pins are released, memory is
1914 : : * allocated in the slot's context).
1915 : : *
1916 : : * If shouldFree is not-NULL it'll be set to true if the returned tuple has
1917 : : * been allocated in the calling memory context, and must be freed by the
1918 : : * caller (via explicit pfree() or a memory context reset).
1919 : : *
1920 : : * NB: If materialize is true, modifications of the returned tuple are
1921 : : * allowed. But it depends on the type of the slot whether such modifications
1922 : : * will also affect the slot's contents. While that is not the nicest
1923 : : * behaviour, all such modifications are in the process of being removed.
1924 : : */
1925 : : HeapTuple
1926 : 33857525 : ExecFetchSlotHeapTuple(TupleTableSlot *slot, bool materialize, bool *shouldFree)
1927 : : {
1928 : : /*
1929 : : * sanity checks
1930 : : */
1931 : : Assert(slot != NULL);
1932 : : Assert(!TTS_EMPTY(slot));
1933 : :
1934 : : /* Materialize the tuple so that the slot "owns" it, if requested. */
1935 [ + + ]: 33857525 : if (materialize)
1936 : 16031949 : slot->tts_ops->materialize(slot);
1937 : :
1938 [ + + ]: 33857525 : if (slot->tts_ops->get_heap_tuple == NULL)
1939 : : {
1940 [ + - ]: 3031885 : if (shouldFree)
1941 : 3031885 : *shouldFree = true;
1942 : 3031885 : return slot->tts_ops->copy_heap_tuple(slot);
1943 : : }
1944 : : else
1945 : : {
1946 [ + + ]: 30825640 : if (shouldFree)
1947 : 28387516 : *shouldFree = false;
1948 : 30825640 : return slot->tts_ops->get_heap_tuple(slot);
1949 : : }
1950 : : }
1951 : :
1952 : : /* --------------------------------
1953 : : * ExecFetchSlotMinimalTuple
1954 : : * Fetch the slot's minimal physical tuple.
1955 : : *
1956 : : * If the given tuple table slot can hold a minimal tuple, indicated by a
1957 : : * non-NULL get_minimal_tuple callback, the function returns the minimal
1958 : : * tuple returned by that callback. It assumes that the minimal tuple
1959 : : * returned by the callback is "owned" by the slot i.e. the slot is
1960 : : * responsible for freeing the memory consumed by the tuple. Hence it sets
1961 : : * *shouldFree to false, indicating that the caller should not free the
1962 : : * memory consumed by the minimal tuple. In this case the returned minimal
1963 : : * tuple should be considered as read-only.
1964 : : *
1965 : : * If that callback is not supported, it calls copy_minimal_tuple callback
1966 : : * which is expected to return a copy of minimal tuple representing the
1967 : : * contents of the slot. In this case *shouldFree is set to true,
1968 : : * indicating the caller that it should free the memory consumed by the
1969 : : * minimal tuple. In this case the returned minimal tuple may be written
1970 : : * up.
1971 : : * --------------------------------
1972 : : */
1973 : : MinimalTuple
1974 : 14763765 : ExecFetchSlotMinimalTuple(TupleTableSlot *slot,
1975 : : bool *shouldFree)
1976 : : {
1977 : : /*
1978 : : * sanity checks
1979 : : */
1980 : : Assert(slot != NULL);
1981 : : Assert(!TTS_EMPTY(slot));
1982 : :
1983 [ + + ]: 14763765 : if (slot->tts_ops->get_minimal_tuple)
1984 : : {
1985 [ + - ]: 3424914 : if (shouldFree)
1986 : 3424914 : *shouldFree = false;
1987 : 3424914 : return slot->tts_ops->get_minimal_tuple(slot);
1988 : : }
1989 : : else
1990 : : {
1991 [ + - ]: 11338851 : if (shouldFree)
1992 : 11338851 : *shouldFree = true;
1993 : 11338851 : return slot->tts_ops->copy_minimal_tuple(slot, 0);
1994 : : }
1995 : : }
1996 : :
1997 : : /* --------------------------------
1998 : : * ExecFetchSlotHeapTupleDatum
1999 : : * Fetch the slot's tuple as a composite-type Datum.
2000 : : *
2001 : : * The result is always freshly palloc'd in the caller's memory context.
2002 : : * --------------------------------
2003 : : */
2004 : : Datum
2005 : 40648 : ExecFetchSlotHeapTupleDatum(TupleTableSlot *slot)
2006 : : {
2007 : : HeapTuple tup;
2008 : : TupleDesc tupdesc;
2009 : : bool shouldFree;
2010 : : Datum ret;
2011 : :
2012 : : /* Fetch slot's contents in regular-physical-tuple form */
2013 : 40648 : tup = ExecFetchSlotHeapTuple(slot, false, &shouldFree);
2014 : 40648 : tupdesc = slot->tts_tupleDescriptor;
2015 : :
2016 : : /* Convert to Datum form */
2017 : 40648 : ret = heap_copy_tuple_as_datum(tup, tupdesc);
2018 : :
2019 [ + + ]: 40648 : if (shouldFree)
2020 : 40504 : pfree(tup);
2021 : :
2022 : 40648 : return ret;
2023 : : }
2024 : :
2025 : : /* ----------------------------------------------------------------
2026 : : * convenience initialization routines
2027 : : * ----------------------------------------------------------------
2028 : : */
2029 : :
2030 : : /* ----------------
2031 : : * ExecInitResultTypeTL
2032 : : *
2033 : : * Initialize result type, using the plan node's targetlist.
2034 : : * ----------------
2035 : : */
2036 : : void
2037 : 884441 : ExecInitResultTypeTL(PlanState *planstate)
2038 : : {
2039 : 884441 : TupleDesc tupDesc = ExecTypeFromTL(planstate->plan->targetlist);
2040 : :
2041 : 884441 : planstate->ps_ResultTupleDesc = tupDesc;
2042 : 884441 : }
2043 : :
2044 : : /* --------------------------------
2045 : : * ExecInit{Result,Scan,Extra}TupleSlot[TL]
2046 : : *
2047 : : * These are convenience routines to initialize the specified slot
2048 : : * in nodes inheriting the appropriate state. ExecInitExtraTupleSlot
2049 : : * is used for initializing special-purpose slots.
2050 : : * --------------------------------
2051 : : */
2052 : :
2053 : : /* ----------------
2054 : : * ExecInitResultTupleSlotTL
2055 : : *
2056 : : * Initialize result tuple slot, using the tuple descriptor previously
2057 : : * computed with ExecInitResultTypeTL().
2058 : : * ----------------
2059 : : */
2060 : : void
2061 : 606664 : ExecInitResultSlot(PlanState *planstate, const TupleTableSlotOps *tts_ops)
2062 : : {
2063 : : TupleTableSlot *slot;
2064 : :
2065 : 606664 : slot = ExecAllocTableSlot(&planstate->state->es_tupleTable,
2066 : : planstate->ps_ResultTupleDesc, tts_ops, 0);
2067 : 606664 : planstate->ps_ResultTupleSlot = slot;
2068 : :
2069 : 606664 : planstate->resultopsfixed = planstate->ps_ResultTupleDesc != NULL;
2070 : 606664 : planstate->resultops = tts_ops;
2071 : 606664 : planstate->resultopsset = true;
2072 : 606664 : }
2073 : :
2074 : : /* ----------------
2075 : : * ExecInitResultTupleSlotTL
2076 : : *
2077 : : * Initialize result tuple slot, using the plan node's targetlist.
2078 : : * ----------------
2079 : : */
2080 : : void
2081 : 431299 : ExecInitResultTupleSlotTL(PlanState *planstate,
2082 : : const TupleTableSlotOps *tts_ops)
2083 : : {
2084 : 431299 : ExecInitResultTypeTL(planstate);
2085 : 431299 : ExecInitResultSlot(planstate, tts_ops);
2086 : 431299 : }
2087 : :
2088 : : /* ----------------
2089 : : * ExecInitScanTupleSlot
2090 : : * ----------------
2091 : : */
2092 : : void
2093 : 464979 : ExecInitScanTupleSlot(EState *estate, ScanState *scanstate,
2094 : : TupleDesc tupledesc, const TupleTableSlotOps *tts_ops,
2095 : : uint16 flags)
2096 : : {
2097 : 464979 : scanstate->ss_ScanTupleSlot = ExecAllocTableSlot(&estate->es_tupleTable,
2098 : : tupledesc, tts_ops, flags);
2099 : 464979 : scanstate->ps.scandesc = tupledesc;
2100 : 464979 : scanstate->ps.scanopsfixed = tupledesc != NULL;
2101 : 464979 : scanstate->ps.scanops = tts_ops;
2102 : 464979 : scanstate->ps.scanopsset = true;
2103 : 464979 : }
2104 : :
2105 : : /* ----------------
2106 : : * ExecInitExtraTupleSlot
2107 : : *
2108 : : * Return a newly created slot. If tupledesc is non-NULL the slot will have
2109 : : * that as its fixed tupledesc. Otherwise the caller needs to use
2110 : : * ExecSetSlotDescriptor() to set the descriptor before use.
2111 : : * ----------------
2112 : : */
2113 : : TupleTableSlot *
2114 : 283909 : ExecInitExtraTupleSlot(EState *estate,
2115 : : TupleDesc tupledesc,
2116 : : const TupleTableSlotOps *tts_ops)
2117 : : {
2118 : 283909 : return ExecAllocTableSlot(&estate->es_tupleTable, tupledesc, tts_ops, 0);
2119 : : }
2120 : :
2121 : : /* ----------------
2122 : : * ExecInitNullTupleSlot
2123 : : *
2124 : : * Build a slot containing an all-nulls tuple of the given type.
2125 : : * This is used as a substitute for an input tuple when performing an
2126 : : * outer join.
2127 : : * ----------------
2128 : : */
2129 : : TupleTableSlot *
2130 : 30201 : ExecInitNullTupleSlot(EState *estate, TupleDesc tupType,
2131 : : const TupleTableSlotOps *tts_ops)
2132 : : {
2133 : 30201 : TupleTableSlot *slot = ExecInitExtraTupleSlot(estate, tupType, tts_ops);
2134 : :
2135 : 30201 : return ExecStoreAllNullTuple(slot);
2136 : : }
2137 : :
2138 : : /* ---------------------------------------------------------------
2139 : : * Routines for setting/accessing attributes in a slot.
2140 : : * ---------------------------------------------------------------
2141 : : */
2142 : :
2143 : : /*
2144 : : * Fill in missing values for a TupleTableSlot.
2145 : : *
2146 : : * This is only exposed because it's needed for JIT compiled tuple
2147 : : * deforming. That exception aside, there should be no callers outside of this
2148 : : * file.
2149 : : */
2150 : : void
2151 : 4870 : slot_getmissingattrs(TupleTableSlot *slot, int startAttNum, int lastAttNum)
2152 : : {
2153 : 4870 : AttrMissing *attrmiss = NULL;
2154 : :
2155 : : /* Check for invalid attnums */
2156 [ - + ]: 4870 : if (unlikely(lastAttNum > slot->tts_tupleDescriptor->natts))
2157 [ # # ]: 0 : elog(ERROR, "invalid attribute number %d", lastAttNum);
2158 : :
2159 [ + + ]: 4870 : if (slot->tts_tupleDescriptor->constr)
2160 : 3169 : attrmiss = slot->tts_tupleDescriptor->constr->missing;
2161 : :
2162 [ + + ]: 4870 : if (!attrmiss)
2163 : : {
2164 : : /* no missing values array at all, so just fill everything in as NULL */
2165 [ + + ]: 3831 : for (int attnum = startAttNum; attnum < lastAttNum; attnum++)
2166 : : {
2167 : 2020 : slot->tts_values[attnum] = (Datum) 0;
2168 : 2020 : slot->tts_isnull[attnum] = true;
2169 : : }
2170 : : }
2171 : : else
2172 : : {
2173 : : /* use attrmiss to set the missing values */
2174 [ + + ]: 7121 : for (int attnum = startAttNum; attnum < lastAttNum; attnum++)
2175 : : {
2176 : 4062 : slot->tts_values[attnum] = attrmiss[attnum].am_value;
2177 : 4062 : slot->tts_isnull[attnum] = !attrmiss[attnum].am_present;
2178 : : }
2179 : : }
2180 : 4870 : }
2181 : :
2182 : : /*
2183 : : * slot_getsomeattrs_int
2184 : : * external function to call getsomeattrs() for use in JIT
2185 : : */
2186 : : void
2187 : 0 : slot_getsomeattrs_int(TupleTableSlot *slot, int attnum)
2188 : : {
2189 : : /* Check for caller errors */
2190 : : Assert(slot->tts_nvalid < attnum); /* checked in slot_getsomeattrs */
2191 : : Assert(attnum > 0);
2192 : :
2193 : : /* Fetch as many attributes as possible from the underlying tuple. */
2194 : 0 : slot->tts_ops->getsomeattrs(slot, attnum);
2195 : :
2196 : : /*
2197 : : * Avoid putting new code here as that would prevent the compiler from
2198 : : * using the sibling call optimization for the above function.
2199 : : */
2200 : 0 : }
2201 : :
2202 : : /* ----------------------------------------------------------------
2203 : : * ExecTypeFromTL
2204 : : *
2205 : : * Generate a tuple descriptor for the result tuple of a targetlist.
2206 : : * (A parse/plan tlist must be passed, not an ExprState tlist.)
2207 : : * Note that resjunk columns, if any, are included in the result.
2208 : : *
2209 : : * Currently there are about 4 different places where we create
2210 : : * TupleDescriptors. They should all be merged, or perhaps
2211 : : * be rewritten to call BuildDesc().
2212 : : * ----------------------------------------------------------------
2213 : : */
2214 : : TupleDesc
2215 : 904148 : ExecTypeFromTL(List *targetList)
2216 : : {
2217 : 904148 : return ExecTypeFromTLInternal(targetList, false);
2218 : : }
2219 : :
2220 : : /* ----------------------------------------------------------------
2221 : : * ExecCleanTypeFromTL
2222 : : *
2223 : : * Same as above, but resjunk columns are omitted from the result.
2224 : : * ----------------------------------------------------------------
2225 : : */
2226 : : TupleDesc
2227 : 73050 : ExecCleanTypeFromTL(List *targetList)
2228 : : {
2229 : 73050 : return ExecTypeFromTLInternal(targetList, true);
2230 : : }
2231 : :
2232 : : static TupleDesc
2233 : 977198 : ExecTypeFromTLInternal(List *targetList, bool skipjunk)
2234 : : {
2235 : : TupleDesc typeInfo;
2236 : : ListCell *l;
2237 : : int len;
2238 : 977198 : int cur_resno = 1;
2239 : :
2240 [ + + ]: 977198 : if (skipjunk)
2241 : 73050 : len = ExecCleanTargetListLength(targetList);
2242 : : else
2243 : 904148 : len = ExecTargetListLength(targetList);
2244 : 977198 : typeInfo = CreateTemplateTupleDesc(len);
2245 : :
2246 [ + + + + : 4971625 : foreach(l, targetList)
+ + ]
2247 : : {
2248 : 3994427 : TargetEntry *tle = lfirst(l);
2249 : :
2250 [ + + + + ]: 3994427 : if (skipjunk && tle->resjunk)
2251 : 20114 : continue;
2252 : 11922939 : TupleDescInitEntry(typeInfo,
2253 : : cur_resno,
2254 : 3974313 : tle->resname,
2255 : 3974313 : exprType((Node *) tle->expr),
2256 : 3974313 : exprTypmod((Node *) tle->expr),
2257 : : 0);
2258 : 3974313 : TupleDescInitEntryCollation(typeInfo,
2259 : : cur_resno,
2260 : 3974313 : exprCollation((Node *) tle->expr));
2261 : 3974313 : cur_resno++;
2262 : : }
2263 : :
2264 : 977198 : TupleDescFinalize(typeInfo);
2265 : :
2266 : 977198 : return typeInfo;
2267 : : }
2268 : :
2269 : : /*
2270 : : * ExecTypeFromExprList - build a tuple descriptor from a list of Exprs
2271 : : *
2272 : : * This is roughly like ExecTypeFromTL, but we work from bare expressions
2273 : : * not TargetEntrys. No names are attached to the tupledesc's columns.
2274 : : */
2275 : : TupleDesc
2276 : 9959 : ExecTypeFromExprList(List *exprList)
2277 : : {
2278 : : TupleDesc typeInfo;
2279 : : ListCell *lc;
2280 : 9959 : int cur_resno = 1;
2281 : :
2282 : 9959 : typeInfo = CreateTemplateTupleDesc(list_length(exprList));
2283 : :
2284 [ + + + + : 27742 : foreach(lc, exprList)
+ + ]
2285 : : {
2286 : 17783 : Node *e = lfirst(lc);
2287 : :
2288 : 17783 : TupleDescInitEntry(typeInfo,
2289 : : cur_resno,
2290 : : NULL,
2291 : : exprType(e),
2292 : : exprTypmod(e),
2293 : : 0);
2294 : 17783 : TupleDescInitEntryCollation(typeInfo,
2295 : : cur_resno,
2296 : : exprCollation(e));
2297 : 17783 : cur_resno++;
2298 : : }
2299 : :
2300 : 9959 : TupleDescFinalize(typeInfo);
2301 : :
2302 : 9959 : return typeInfo;
2303 : : }
2304 : :
2305 : : /*
2306 : : * ExecTypeSetColNames - set column names in a RECORD TupleDesc
2307 : : *
2308 : : * Column names must be provided as an alias list (list of String nodes).
2309 : : */
2310 : : void
2311 : 3012 : ExecTypeSetColNames(TupleDesc typeInfo, List *namesList)
2312 : : {
2313 : 3012 : int colno = 0;
2314 : : ListCell *lc;
2315 : :
2316 : : /* It's only OK to change col names in a not-yet-blessed RECORD type */
2317 : : Assert(typeInfo->tdtypeid == RECORDOID);
2318 : : Assert(typeInfo->tdtypmod < 0);
2319 : :
2320 [ + + + + : 10418 : foreach(lc, namesList)
+ + ]
2321 : : {
2322 : 7406 : char *cname = strVal(lfirst(lc));
2323 : : Form_pg_attribute attr;
2324 : :
2325 : : /* Guard against too-long names list (probably can't happen) */
2326 [ - + ]: 7406 : if (colno >= typeInfo->natts)
2327 : 0 : break;
2328 : 7406 : attr = TupleDescAttr(typeInfo, colno);
2329 : 7406 : colno++;
2330 : :
2331 : : /*
2332 : : * Do nothing for empty aliases or dropped columns (these cases
2333 : : * probably can't arise in RECORD types, either)
2334 : : */
2335 [ + + - + ]: 7406 : if (cname[0] == '\0' || attr->attisdropped)
2336 : 16 : continue;
2337 : :
2338 : : /* OK, assign the column name */
2339 : 7390 : namestrcpy(&(attr->attname), cname);
2340 : : }
2341 : 3012 : }
2342 : :
2343 : : /*
2344 : : * BlessTupleDesc - make a completed tuple descriptor useful for SRFs
2345 : : *
2346 : : * Rowtype Datums returned by a function must contain valid type information.
2347 : : * This happens "for free" if the tupdesc came from a relcache entry, but
2348 : : * not if we have manufactured a tupdesc for a transient RECORD datatype.
2349 : : * In that case we have to notify typcache.c of the existence of the type.
2350 : : *
2351 : : * TupleDescFinalize() must be called on the TupleDesc before calling this
2352 : : * function.
2353 : : */
2354 : : TupleDesc
2355 : 89548 : BlessTupleDesc(TupleDesc tupdesc)
2356 : : {
2357 : : /* Did someone forget to call TupleDescFinalize()? */
2358 : : Assert(tupdesc->firstNonCachedOffsetAttr >= 0);
2359 : :
2360 [ + + ]: 89548 : if (tupdesc->tdtypeid == RECORDOID &&
2361 [ + + ]: 86707 : tupdesc->tdtypmod < 0)
2362 : 56660 : assign_record_type_typmod(tupdesc);
2363 : :
2364 : 89548 : return tupdesc; /* just for notational convenience */
2365 : : }
2366 : :
2367 : : /*
2368 : : * TupleDescGetAttInMetadata - Build an AttInMetadata structure based on the
2369 : : * supplied TupleDesc. AttInMetadata can be used in conjunction with C strings
2370 : : * to produce a properly formed tuple.
2371 : : */
2372 : : AttInMetadata *
2373 : 14464 : TupleDescGetAttInMetadata(TupleDesc tupdesc)
2374 : : {
2375 : 14464 : int natts = tupdesc->natts;
2376 : : int i;
2377 : : Oid atttypeid;
2378 : : Oid attinfuncid;
2379 : : FmgrInfo *attinfuncinfo;
2380 : : Oid *attioparams;
2381 : : int32 *atttypmods;
2382 : : AttInMetadata *attinmeta;
2383 : :
2384 : 14464 : attinmeta = palloc_object(AttInMetadata);
2385 : :
2386 : : /* "Bless" the tupledesc so that we can make rowtype datums with it */
2387 : 14464 : attinmeta->tupdesc = BlessTupleDesc(tupdesc);
2388 : :
2389 : : /*
2390 : : * Gather info needed later to call the "in" function for each attribute
2391 : : */
2392 : 14464 : attinfuncinfo = (FmgrInfo *) palloc0(natts * sizeof(FmgrInfo));
2393 : 14464 : attioparams = (Oid *) palloc0(natts * sizeof(Oid));
2394 : 14464 : atttypmods = (int32 *) palloc0(natts * sizeof(int32));
2395 : :
2396 [ + + ]: 75662 : for (i = 0; i < natts; i++)
2397 : : {
2398 : 61198 : Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2399 : :
2400 : : /* Ignore dropped attributes */
2401 [ + + ]: 61198 : if (!att->attisdropped)
2402 : : {
2403 : 61080 : atttypeid = att->atttypid;
2404 : 61080 : getTypeInputInfo(atttypeid, &attinfuncid, &attioparams[i]);
2405 : 61080 : fmgr_info(attinfuncid, &attinfuncinfo[i]);
2406 : 61080 : atttypmods[i] = att->atttypmod;
2407 : : }
2408 : : }
2409 : 14464 : attinmeta->attinfuncs = attinfuncinfo;
2410 : 14464 : attinmeta->attioparams = attioparams;
2411 : 14464 : attinmeta->atttypmods = atttypmods;
2412 : :
2413 : 14464 : return attinmeta;
2414 : : }
2415 : :
2416 : : /*
2417 : : * BuildTupleFromCStrings - build a HeapTuple given user data in C string form.
2418 : : * values is an array of C strings, one for each attribute of the return tuple.
2419 : : * A NULL string pointer indicates we want to create a NULL field.
2420 : : */
2421 : : HeapTuple
2422 : 1031234 : BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
2423 : : {
2424 : 1031234 : TupleDesc tupdesc = attinmeta->tupdesc;
2425 : 1031234 : int natts = tupdesc->natts;
2426 : : Datum *dvalues;
2427 : : bool *nulls;
2428 : : int i;
2429 : : HeapTuple tuple;
2430 : :
2431 : 1031234 : dvalues = (Datum *) palloc(natts * sizeof(Datum));
2432 : 1031234 : nulls = (bool *) palloc(natts * sizeof(bool));
2433 : :
2434 : : /*
2435 : : * Call the "in" function for each non-dropped attribute, even for nulls,
2436 : : * to support domains.
2437 : : */
2438 [ + + ]: 15540041 : for (i = 0; i < natts; i++)
2439 : : {
2440 [ + - ]: 14508808 : if (!TupleDescCompactAttr(tupdesc, i)->attisdropped)
2441 : : {
2442 : : /* Non-dropped attributes */
2443 : 29017615 : dvalues[i] = InputFunctionCall(&attinmeta->attinfuncs[i],
2444 : 14508808 : values[i],
2445 : 14508808 : attinmeta->attioparams[i],
2446 : 14508808 : attinmeta->atttypmods[i]);
2447 [ + + ]: 14508807 : if (values[i] != NULL)
2448 : 10025146 : nulls[i] = false;
2449 : : else
2450 : 4483661 : nulls[i] = true;
2451 : : }
2452 : : else
2453 : : {
2454 : : /* Handle dropped attributes by setting to NULL */
2455 : 0 : dvalues[i] = (Datum) 0;
2456 : 0 : nulls[i] = true;
2457 : : }
2458 : : }
2459 : :
2460 : : /*
2461 : : * Form a tuple
2462 : : */
2463 : 1031233 : tuple = heap_form_tuple(tupdesc, dvalues, nulls);
2464 : :
2465 : : /*
2466 : : * Release locally palloc'd space. XXX would probably be good to pfree
2467 : : * values of pass-by-reference datums, as well.
2468 : : */
2469 : 1031233 : pfree(dvalues);
2470 : 1031233 : pfree(nulls);
2471 : :
2472 : 1031233 : return tuple;
2473 : : }
2474 : :
2475 : : /*
2476 : : * HeapTupleHeaderGetDatum - convert a HeapTupleHeader pointer to a Datum.
2477 : : *
2478 : : * This must *not* get applied to an on-disk tuple; the tuple should be
2479 : : * freshly made by heap_form_tuple or some wrapper routine for it (such as
2480 : : * BuildTupleFromCStrings). Be sure also that the tupledesc used to build
2481 : : * the tuple has a properly "blessed" rowtype.
2482 : : *
2483 : : * Formerly this was a macro equivalent to PointerGetDatum, relying on the
2484 : : * fact that heap_form_tuple fills in the appropriate tuple header fields
2485 : : * for a composite Datum. However, we now require that composite Datums not
2486 : : * contain any external TOAST pointers. We do not want heap_form_tuple itself
2487 : : * to enforce that; more specifically, the rule applies only to actual Datums
2488 : : * and not to HeapTuple structures. Therefore, HeapTupleHeaderGetDatum is
2489 : : * now a function that detects whether there are externally-toasted fields
2490 : : * and constructs a new tuple with inlined fields if so. We still need
2491 : : * heap_form_tuple to insert the Datum header fields, because otherwise this
2492 : : * code would have no way to obtain a tupledesc for the tuple.
2493 : : *
2494 : : * Note that if we do build a new tuple, it's palloc'd in the current
2495 : : * memory context. Beware of code that changes context between the initial
2496 : : * heap_form_tuple/etc call and calling HeapTuple(Header)GetDatum.
2497 : : *
2498 : : * For performance-critical callers, it could be worthwhile to take extra
2499 : : * steps to ensure that there aren't TOAST pointers in the output of
2500 : : * heap_form_tuple to begin with. It's likely however that the costs of the
2501 : : * typcache lookup and tuple disassembly/reassembly are swamped by TOAST
2502 : : * dereference costs, so that the benefits of such extra effort would be
2503 : : * minimal.
2504 : : *
2505 : : * XXX it would likely be better to create wrapper functions that produce
2506 : : * a composite Datum from the field values in one step. However, there's
2507 : : * enough code using the existing APIs that we couldn't get rid of this
2508 : : * hack anytime soon.
2509 : : */
2510 : : Datum
2511 : 1514190 : HeapTupleHeaderGetDatum(HeapTupleHeader tuple)
2512 : : {
2513 : : Datum result;
2514 : : TupleDesc tupDesc;
2515 : :
2516 : : /* No work if there are no external TOAST pointers in the tuple */
2517 [ + + ]: 1514190 : if (!HeapTupleHeaderHasExternal(tuple))
2518 : 1514182 : return PointerGetDatum(tuple);
2519 : :
2520 : : /* Use the type data saved by heap_form_tuple to look up the rowtype */
2521 : 8 : tupDesc = lookup_rowtype_tupdesc(HeapTupleHeaderGetTypeId(tuple),
2522 : : HeapTupleHeaderGetTypMod(tuple));
2523 : :
2524 : : /* And do the flattening */
2525 : 8 : result = toast_flatten_tuple_to_datum(tuple,
2526 : : HeapTupleHeaderGetDatumLength(tuple),
2527 : : tupDesc);
2528 : :
2529 [ + - ]: 8 : ReleaseTupleDesc(tupDesc);
2530 : :
2531 : 8 : return result;
2532 : : }
2533 : :
2534 : :
2535 : : /*
2536 : : * Functions for sending tuples to the frontend (or other specified destination)
2537 : : * as though it is a SELECT result. These are used by utility commands that
2538 : : * need to project directly to the destination and don't need or want full
2539 : : * table function capability. Currently used by EXPLAIN and SHOW ALL.
2540 : : */
2541 : : TupOutputState *
2542 : 20174 : begin_tup_output_tupdesc(DestReceiver *dest,
2543 : : TupleDesc tupdesc,
2544 : : const TupleTableSlotOps *tts_ops)
2545 : : {
2546 : : TupOutputState *tstate;
2547 : :
2548 : 20174 : tstate = palloc_object(TupOutputState);
2549 : :
2550 : 20174 : tstate->slot = MakeSingleTupleTableSlot(tupdesc, tts_ops);
2551 : 20174 : tstate->dest = dest;
2552 : :
2553 : 20174 : tstate->dest->rStartup(tstate->dest, (int) CMD_SELECT, tupdesc);
2554 : :
2555 : 20174 : return tstate;
2556 : : }
2557 : :
2558 : : /*
2559 : : * write a single tuple
2560 : : */
2561 : : void
2562 : 117961 : do_tup_output(TupOutputState *tstate, const Datum *values, const bool *isnull)
2563 : : {
2564 : 117961 : TupleTableSlot *slot = tstate->slot;
2565 : 117961 : int natts = slot->tts_tupleDescriptor->natts;
2566 : :
2567 : : /* make sure the slot is clear */
2568 : 117961 : ExecClearTuple(slot);
2569 : :
2570 : : /* insert data */
2571 : 117961 : memcpy(slot->tts_values, values, natts * sizeof(Datum));
2572 : 117961 : memcpy(slot->tts_isnull, isnull, natts * sizeof(bool));
2573 : :
2574 : : /* mark slot as containing a virtual tuple */
2575 : 117961 : ExecStoreVirtualTuple(slot);
2576 : :
2577 : : /* send the tuple to the receiver */
2578 : 117961 : (void) tstate->dest->receiveSlot(slot, tstate->dest);
2579 : :
2580 : : /* clean up */
2581 : 117961 : ExecClearTuple(slot);
2582 : 117961 : }
2583 : :
2584 : : /*
2585 : : * write a chunk of text, breaking at newline characters
2586 : : *
2587 : : * Should only be used with a single-TEXT-attribute tupdesc.
2588 : : */
2589 : : void
2590 : 16460 : do_text_output_multiline(TupOutputState *tstate, const char *txt)
2591 : : {
2592 : : Datum values[1];
2593 : 16460 : bool isnull[1] = {false};
2594 : :
2595 [ + + ]: 129940 : while (*txt)
2596 : : {
2597 : : const char *eol;
2598 : : int len;
2599 : :
2600 : 113480 : eol = strchr(txt, '\n');
2601 [ + - ]: 113480 : if (eol)
2602 : : {
2603 : 113480 : len = eol - txt;
2604 : 113480 : eol++;
2605 : : }
2606 : : else
2607 : : {
2608 : 0 : len = strlen(txt);
2609 : 0 : eol = txt + len;
2610 : : }
2611 : :
2612 : 113480 : values[0] = PointerGetDatum(cstring_to_text_with_len(txt, len));
2613 : 113480 : do_tup_output(tstate, values, isnull);
2614 : 113480 : pfree(DatumGetPointer(values[0]));
2615 : 113480 : txt = eol;
2616 : : }
2617 : 16460 : }
2618 : :
2619 : : void
2620 : 20174 : end_tup_output(TupOutputState *tstate)
2621 : : {
2622 : 20174 : tstate->dest->rShutdown(tstate->dest);
2623 : : /* note that destroying the dest is not ours to do */
2624 : 20174 : ExecDropSingleTupleTableSlot(tstate->slot);
2625 : 20174 : pfree(tstate);
2626 : 20174 : }
|