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

Generated by: LCOV version 1.14