LCOV - differential code coverage report
Current view: top level - src/backend/executor - execTuples.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 92.0 % 834 767 67 6 761 5
Current Date: 2026-11-28 16:48:44 +0900 Functions: 91.9 % 86 79 7 3 76
Baseline: lcov-20261128-baseline Branches: 66.0 % 488 322 2 164 4 318
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 90.9 % 11 10 1 1 9
(30,360] days: 99.1 % 109 108 1 5 103
(360..) days: 90.9 % 714 649 65 649
Function coverage date bins:
(360..) days: 91.9 % 86 79 7 3 76
Branch coverage date bins:
(7,30] days: 65.0 % 20 13 2 5 4 9
(30,360] days: 78.8 % 66 52 14 52
(360..) days: 63.9 % 402 257 145 257

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

Generated by: LCOV version 2.0-1