LCOV - differential code coverage report
Current view: top level - src/backend/executor - execTuples.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 92.0 % 823 757 1 5 60 100 657 1 1 76
Current Date: 2026-05-05 10:23:31 +0900 Functions: 91.9 % 86 79 1 6 15 64 2
Baseline: lcov-20260505-025707-baseline Branches: 66.2 % 464 307 12 6 2 137 1 2 50 254 12 16 8 56
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 99.0 % 102 101 1 98 3
(360..) days: 91.0 % 721 656 5 60 2 654 1
Function coverage date bins:
(360..) days: 91.9 % 86 79 1 6 15 64
Branch coverage date bins:
(30,360] days: 80.6 % 62 50 12 50
(360..) days: 59.8 % 430 257 6 2 137 1 2 254 12 16

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

Generated by: LCOV version 2.5.0-beta