LCOV - code coverage report
Current view: top level - src/backend/executor - execTuples.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 659 706 93.3 %
Date: 2019-09-19 02:07:14 Functions: 78 82 95.1 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13