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

Generated by: LCOV version 1.13