LCOV - differential code coverage report
Current view: top level - src/backend/access/common - tupdesc.c (source / functions) Coverage Total Hit UNC LBC UBC GIC GNC CBC ECB DUB DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 92.3 % 532 491 2 1 38 1 20 470 2 10 51
Current Date: 2026-11-28 16:48:44 +0900 Functions: 96.7 % 30 29 1 13 16 1 7
Baseline: lcov-20261128-baseline Branches: 74.6 % 346 258 5 7 76 9 249
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: 88.4 % 43 38 2 1 2 1 17 20 2
(30,360] days: 98.0 % 49 48 1 3 45
(360..) days: 92.0 % 440 405 35 405
Function coverage date bins:
(7,30] days: 100.0 % 5 5 5
(30,360] days: 100.0 % 1 1 1
(360..) days: 95.8 % 24 23 1 7 16
Branch coverage date bins:
(7,30] days: 72.2 % 36 26 5 1 4 9 17
(30,360] days: 85.7 % 28 24 4 24
(360..) days: 73.8 % 282 208 6 68 208

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * tupdesc.c
                                  4                 :                :  *    POSTGRES tuple descriptor support code
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/access/common/tupdesc.c
                                 12                 :                :  *
                                 13                 :                :  * NOTES
                                 14                 :                :  *    some of the executor utility code such as "ExecTypeFromTL" should be
                                 15                 :                :  *    moved here.
                                 16                 :                :  *
                                 17                 :                :  *-------------------------------------------------------------------------
                                 18                 :                :  */
                                 19                 :                : 
                                 20                 :                : #include "postgres.h"
                                 21                 :                : 
                                 22                 :                : #include "access/htup_details.h"
                                 23                 :                : #include "access/toast_compression.h"
                                 24                 :                : #include "access/tupdesc_details.h"
                                 25                 :                : #include "catalog/catalog.h"
                                 26                 :                : #include "catalog/pg_collation.h"
                                 27                 :                : #include "catalog/pg_type.h"
                                 28                 :                : #include "common/hashfn.h"
                                 29                 :                : #include "utils/builtins.h"
                                 30                 :                : #include "utils/datum.h"
                                 31                 :                : #include "utils/resowner.h"
                                 32                 :                : #include "utils/syscache.h"
                                 33                 :                : 
                                 34                 :                : /* ResourceOwner callbacks to hold tupledesc references  */
                                 35                 :                : static void ResOwnerReleaseTupleDesc(Datum res);
                                 36                 :                : static char *ResOwnerPrintTupleDesc(Datum res);
                                 37                 :                : 
                                 38                 :                : static const ResourceOwnerDesc tupdesc_resowner_desc =
                                 39                 :                : {
                                 40                 :                :     .name = "tupdesc reference",
                                 41                 :                :     .release_phase = RESOURCE_RELEASE_AFTER_LOCKS,
                                 42                 :                :     .release_priority = RELEASE_PRIO_TUPDESC_REFS,
                                 43                 :                :     .ReleaseResource = ResOwnerReleaseTupleDesc,
                                 44                 :                :     .DebugPrint = ResOwnerPrintTupleDesc
                                 45                 :                : };
                                 46                 :                : 
                                 47                 :                : /* Convenience wrappers over ResourceOwnerRemember/Forget */
                                 48                 :                : static inline void
 1116 heikki.linnakangas@i       49                 :CBC    13444546 : ResourceOwnerRememberTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
                                 50                 :                : {
                                 51                 :       13444546 :     ResourceOwnerRemember(owner, PointerGetDatum(tupdesc), &tupdesc_resowner_desc);
                                 52                 :       13444546 : }
                                 53                 :                : 
                                 54                 :                : static inline void
                                 55                 :       13432053 : ResourceOwnerForgetTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
                                 56                 :                : {
                                 57                 :       13432053 :     ResourceOwnerForget(owner, PointerGetDatum(tupdesc), &tupdesc_resowner_desc);
                                 58                 :       13432053 : }
                                 59                 :                : 
                                 60                 :                : /*
                                 61                 :                :  * populate_compact_attribute_internal
                                 62                 :                :  *      Helper function for populate_compact_attribute()
                                 63                 :                :  */
                                 64                 :                : static inline void
  704 drowley@postgresql.o       65                 :     1408261628 : populate_compact_attribute_internal(Form_pg_attribute src,
                                 66                 :                :                                     CompactAttribute *dst)
                                 67                 :                : {
  708                            68                 :     1408261628 :     memset(dst, 0, sizeof(CompactAttribute));
                                 69                 :                : 
                                 70                 :     1408261628 :     dst->attcacheoff = -1;
                                 71                 :     1408261628 :     dst->attlen = src->attlen;
                                 72                 :                : 
                                 73                 :     1408261628 :     dst->attbyval = src->attbyval;
                                 74                 :     1408261628 :     dst->attispackable = (src->attstorage != TYPSTORAGE_PLAIN);
                                 75                 :     1408261628 :     dst->atthasmissing = src->atthasmissing;
                                 76                 :     1408261628 :     dst->attisdropped = src->attisdropped;
                                 77                 :     1408261628 :     dst->attgenerated = (src->attgenerated != '\0');
                                 78                 :                : 
                                 79                 :                :     /*
                                 80                 :                :      * Assign nullability status for this column.  Assuming that a constraint
                                 81                 :                :      * exists, at this point we don't know if a not-null constraint is valid,
                                 82                 :                :      * so we assign UNKNOWN unless the table is a catalog, in which case we
                                 83                 :                :      * know it's valid.
                                 84                 :                :      */
  600 alvherre@alvh.no-ip.       85   [ +  +  +  + ]:     1768230426 :     dst->attnullability = !src->attnotnull ? ATTNULLABLE_UNRESTRICTED :
                                 86                 :      359968798 :         IsCatalogRelationOid(src->attrelid) ? ATTNULLABLE_VALID :
                                 87                 :                :         ATTNULLABLE_UNKNOWN;
                                 88                 :                : 
                                 89                 :                :     /* Compute numeric alignment requirement, too */
  299 tgl@sss.pgh.pa.us          90                 :     1408261628 :     dst->attalignby = typalign_to_alignby(src->attalign);
  708 drowley@postgresql.o       91                 :     1408261628 : }
                                 92                 :                : 
                                 93                 :                : /*
                                 94                 :                :  * populate_compact_attribute
                                 95                 :                :  *      Fill in the corresponding CompactAttribute element from the
                                 96                 :                :  *      Form_pg_attribute for the given attribute number.  This must be called
                                 97                 :                :  *      whenever a change is made to a Form_pg_attribute in the TupleDesc.
                                 98                 :                :  */
                                 99                 :                : void
  704                           100                 :       44463863 : populate_compact_attribute(TupleDesc tupdesc, int attnum)
                                101                 :                : {
                                102                 :       44463863 :     Form_pg_attribute src = TupleDescAttr(tupdesc, attnum);
                                103                 :                :     CompactAttribute *dst;
                                104                 :                : 
                                105                 :                :     /*
                                106                 :                :      * Don't use TupleDescCompactAttr to prevent infinite recursion in assert
                                107                 :                :      * builds.
                                108                 :                :      */
                                109                 :       44463863 :     dst = &tupdesc->compact_attrs[attnum];
                                110                 :                : 
                                111                 :       44463863 :     populate_compact_attribute_internal(src, dst);
                                112                 :       44463863 : }
                                113                 :                : 
                                114                 :                : /*
                                115                 :                :  * verify_compact_attribute
                                116                 :                :  *      In Assert enabled builds, we verify that the CompactAttribute is
                                117                 :                :  *      populated correctly.  This helps find bugs in places such as ALTER
                                118                 :                :  *      TABLE where code makes changes to the FormData_pg_attribute but
                                119                 :                :  *      forgets to call populate_compact_attribute().
                                120                 :                :  *
                                121                 :                :  * This is used in TupleDescCompactAttr(), but declared here to allow access
                                122                 :                :  * to populate_compact_attribute_internal().
                                123                 :                :  */
                                124                 :                : void
                                125                 :     1363797765 : verify_compact_attribute(TupleDesc tupdesc, int attnum)
                                126                 :                : {
                                127                 :                : #ifdef USE_ASSERT_CHECKING
                                128                 :                :     CompactAttribute cattr;
                                129                 :     1363797765 :     Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum);
                                130                 :                :     CompactAttribute tmp;
                                131                 :                : 
                                132                 :                :     /*
                                133                 :                :      * Make a temp copy of the TupleDesc's CompactAttribute.  This may be a
                                134                 :                :      * shared TupleDesc and the attcacheoff might get changed by another
                                135                 :                :      * backend.
                                136                 :                :      */
  529                           137                 :     1363797765 :     memcpy(&cattr, &tupdesc->compact_attrs[attnum], sizeof(CompactAttribute));
                                138                 :                : 
                                139                 :                :     /*
                                140                 :                :      * Populate the temporary CompactAttribute from the corresponding
                                141                 :                :      * Form_pg_attribute
                                142                 :                :      */
  704                           143                 :     1363797765 :     populate_compact_attribute_internal(attr, &tmp);
                                144                 :                : 
                                145                 :                :     /*
                                146                 :                :      * Make the attcacheoff match since it's been reset to -1 by
                                147                 :                :      * populate_compact_attribute_internal.  Same with attnullability.
                                148                 :                :      */
  529                           149                 :     1363797765 :     tmp.attcacheoff = cattr.attcacheoff;
                                150                 :     1363797765 :     tmp.attnullability = cattr.attnullability;
                                151                 :                : 
                                152                 :                :     /* Check the freshly populated CompactAttribute matches the TupleDesc's */
                                153         [ -  + ]:     1363797765 :     Assert(memcmp(&tmp, &cattr, sizeof(CompactAttribute)) == 0);
                                154                 :                : #endif
  686                           155                 :     1363797765 : }
                                156                 :                : 
                                157                 :                : /*
                                158                 :                :  * CreateTemplateTupleDesc
                                159                 :                :  *      This function allocates an empty tuple descriptor structure.
                                160                 :                :  *
                                161                 :                :  * Tuple type ID information is initially set for an anonymous record type;
                                162                 :                :  * caller can overwrite this if needed.
                                163                 :                :  */
                                164                 :                : TupleDesc
 2930 andres@anarazel.de        165                 :        6356110 : CreateTemplateTupleDesc(int natts)
                                166                 :                : {
                                167                 :                :     TupleDesc   desc;
                                168                 :                :     size_t      sz;
                                169                 :                : 
                                170                 :                :     /*
                                171                 :                :      * sanity checks
                                172                 :                :      */
 1492 peter@eisentraut.org      173         [ -  + ]:        6356110 :     Assert(natts >= 0);
                                174                 :                : 
                                175                 :                :     /*
                                176                 :                :      * Allocate enough memory for the tuple descriptor, the CompactAttribute
                                177                 :                :      * array and also an array of FormData_pg_attribute.
                                178                 :                :      *
                                179                 :                :      * Note: the FormData_pg_attribute array stride is
                                180                 :                :      * sizeof(FormData_pg_attribute), since we declare the array elements as
                                181                 :                :      * FormData_pg_attribute for notational convenience.  However, we only
                                182                 :                :      * guarantee that the first ATTRIBUTE_FIXED_PART_SIZE bytes of each entry
                                183                 :                :      * are valid; most code that copies tupdesc entries around copies just
                                184                 :                :      * that much.  In principle that could be less due to trailing padding,
                                185                 :                :      * although with the current definition of pg_attribute there probably
                                186                 :                :      * isn't any padding.
                                187                 :                :      */
   19 andres@anarazel.de        188                 :        6356110 :     sz = offsetof(struct TupleDescData, compact_attrs) +
   19 andres@anarazel.de        189                 :GIC     6356110 :         natts * sizeof(CompactAttribute) +
   19 andres@anarazel.de        190                 :ECB   (6103216) :         natts * sizeof(FormData_pg_attribute);
                                191                 :                : 
   19 andres@anarazel.de        192                 :CBC     6356110 :     desc = (TupleDesc) palloc(sz);
                                193                 :                : 
                                194                 :                :     /*
                                195                 :                :      * Initialize other fields of the tupdesc.
                                196                 :                :      */
 8276 tgl@sss.pgh.pa.us         197                 :        6356110 :     desc->natts = natts;
 8827                           198                 :        6356110 :     desc->constr = NULL;
 8276                           199                 :        6356110 :     desc->tdtypeid = RECORDOID;
                                200                 :        6356110 :     desc->tdtypmod = -1;
 7470                           201                 :        6356110 :     desc->tdrefcount = -1;       /* assume not reference-counted */
                                202                 :                : 
                                203                 :                :     /*
                                204                 :                :      * Zero-initialize the aggregate version. RECORD descriptors aren't
                                205                 :                :      * versioned, and we don't know if this is going to be used for something
                                206                 :                :      * else, so we have to zero it here.
                                207                 :                :      */
   19 andres@anarazel.de        208                 :GNC     6356110 :     TupleDescSetVersion(desc, InvalidXLogRecPtr);
                                209                 :                : 
                                210                 :                :     /* This will be set to the correct value by TupleDescFinalize() */
  257 drowley@postgresql.o      211                 :CBC     6356110 :     desc->firstNonCachedOffsetAttr = -1;
                                212                 :        6356110 :     desc->firstNonGuaranteedAttr = -1;
                                213                 :                : 
10315 bruce@momjian.us          214                 :        6356110 :     return desc;
                                215                 :                : }
                                216                 :                : 
                                217                 :                : /*
                                218                 :                :  * CreateTupleDesc
                                219                 :                :  *      This function allocates a new TupleDesc by copying a given
                                220                 :                :  *      Form_pg_attribute array.
                                221                 :                :  *
                                222                 :                :  * Tuple type ID information is initially set for an anonymous record type;
                                223                 :                :  * caller can overwrite this if needed.
                                224                 :                :  */
                                225                 :                : TupleDesc
 2930 andres@anarazel.de        226                 :         702220 : CreateTupleDesc(int natts, Form_pg_attribute *attrs)
                                227                 :                : {
                                228                 :                :     TupleDesc   desc;
                                229                 :                :     int         i;
                                230                 :                : 
                                231                 :         702220 :     desc = CreateTemplateTupleDesc(natts);
                                232                 :                : 
 3387                           233         [ +  + ]:       10738300 :     for (i = 0; i < natts; ++i)
                                234                 :                :     {
                                235                 :       10036080 :         memcpy(TupleDescAttr(desc, i), attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
  708 drowley@postgresql.o      236                 :       10036080 :         populate_compact_attribute(desc, i);
                                237                 :                :     }
                                238                 :                : 
  257                           239                 :         702220 :     TupleDescFinalize(desc);
                                240                 :                : 
10315 bruce@momjian.us          241                 :         702220 :     return desc;
                                242                 :                : }
                                243                 :                : 
                                244                 :                : /*
                                245                 :                :  * CreateTupleDescCopy
                                246                 :                :  *      This function creates a new TupleDesc by copying from an existing
                                247                 :                :  *      TupleDesc.
                                248                 :                :  *
                                249                 :                :  * !!! Constraints and defaults are not copied !!!
                                250                 :                :  */
                                251                 :                : TupleDesc
11099 scrappy@hub.org           252                 :         273195 : CreateTupleDescCopy(TupleDesc tupdesc)
                                253                 :                : {
                                254                 :                :     TupleDesc   desc;
                                255                 :                :     int         i;
                                256                 :                : 
 2930 andres@anarazel.de        257                 :         273195 :     desc = CreateTemplateTupleDesc(tupdesc->natts);
                                258                 :                : 
                                259                 :                :     /* Flat-copy the attribute array (unless there are no attributes) */
  249 rhaas@postgresql.org      260         [ +  + ]:         273195 :     if (desc->natts > 0)
                                261                 :         270920 :         memcpy(TupleDescAttr(desc, 0),
                                262                 :         270920 :                TupleDescAttr(tupdesc, 0),
                                263                 :         270920 :                desc->natts * sizeof(FormData_pg_attribute));
                                264                 :                : 
                                265                 :                :     /*
                                266                 :                :      * Since we're not copying constraints and defaults, clear fields
                                267                 :                :      * associated with them.
                                268                 :                :      */
 7936 tgl@sss.pgh.pa.us         269         [ +  + ]:        1393201 :     for (i = 0; i < desc->natts; i++)
                                270                 :                :     {
 3387 andres@anarazel.de        271                 :        1120006 :         Form_pg_attribute att = TupleDescAttr(desc, i);
                                272                 :                : 
                                273                 :        1120006 :         att->attnotnull = false;
                                274                 :        1120006 :         att->atthasdef = false;
 3167 andrew@dunslane.net       275                 :        1120006 :         att->atthasmissing = false;
 3387 andres@anarazel.de        276                 :        1120006 :         att->attidentity = '\0';
 2800 peter@eisentraut.org      277                 :        1120006 :         att->attgenerated = '\0';
                                278                 :                : 
  708 drowley@postgresql.o      279                 :        1120006 :         populate_compact_attribute(desc, i);
                                280                 :                :     }
                                281                 :                : 
                                282                 :                :     /* We can copy the tuple type identification, too */
                                283                 :         273195 :     desc->tdtypeid = tupdesc->tdtypeid;
                                284                 :         273195 :     desc->tdtypmod = tupdesc->tdtypmod;
                                285                 :                : 
  257                           286                 :         273195 :     TupleDescFinalize(desc);
                                287                 :                : 
  708                           288                 :         273195 :     return desc;
                                289                 :                : }
                                290                 :                : 
                                291                 :                : /*
                                292                 :                :  * CreateTupleDescTruncatedCopy
                                293                 :                :  *      This function creates a new TupleDesc with only the first 'natts'
                                294                 :                :  *      attributes from an existing TupleDesc
                                295                 :                :  *
                                296                 :                :  * !!! Constraints and defaults are not copied !!!
                                297                 :                :  */
                                298                 :                : TupleDesc
                                299                 :          22474 : CreateTupleDescTruncatedCopy(TupleDesc tupdesc, int natts)
                                300                 :                : {
                                301                 :                :     TupleDesc   desc;
                                302                 :                :     int         i;
                                303                 :                : 
                                304         [ -  + ]:          22474 :     Assert(natts <= tupdesc->natts);
                                305                 :                : 
                                306                 :          22474 :     desc = CreateTemplateTupleDesc(natts);
                                307                 :                : 
                                308                 :                :     /* Flat-copy the attribute array (unless there are no attributes) */
  249 rhaas@postgresql.org      309         [ +  - ]:          22474 :     if (desc->natts > 0)
                                310                 :          22474 :         memcpy(TupleDescAttr(desc, 0),
                                311                 :          22474 :                TupleDescAttr(tupdesc, 0),
                                312                 :          22474 :                desc->natts * sizeof(FormData_pg_attribute));
                                313                 :                : 
                                314                 :                :     /*
                                315                 :                :      * Since we're not copying constraints and defaults, clear fields
                                316                 :                :      * associated with them.
                                317                 :                :      */
  708 drowley@postgresql.o      318         [ +  + ]:          52546 :     for (i = 0; i < desc->natts; i++)
                                319                 :                :     {
                                320                 :          30072 :         Form_pg_attribute att = TupleDescAttr(desc, i);
                                321                 :                : 
                                322                 :          30072 :         att->attnotnull = false;
                                323                 :          30072 :         att->atthasdef = false;
                                324                 :          30072 :         att->atthasmissing = false;
                                325                 :          30072 :         att->attidentity = '\0';
                                326                 :          30072 :         att->attgenerated = '\0';
                                327                 :                : 
                                328                 :          30072 :         populate_compact_attribute(desc, i);
                                329                 :                :     }
                                330                 :                : 
                                331                 :                :     /* We can copy the tuple type identification, too */
 8276 tgl@sss.pgh.pa.us         332                 :          22474 :     desc->tdtypeid = tupdesc->tdtypeid;
                                333                 :          22474 :     desc->tdtypmod = tupdesc->tdtypmod;
                                334                 :                : 
  257 drowley@postgresql.o      335                 :          22474 :     TupleDescFinalize(desc);
                                336                 :                : 
10674 bruce@momjian.us          337                 :          22474 :     return desc;
                                338                 :                : }
                                339                 :                : 
                                340                 :                : /*
                                341                 :                :  * CreateTupleDescCopyConstr
                                342                 :                :  *      This function creates a new TupleDesc by copying from an existing
                                343                 :                :  *      TupleDesc (including its constraints and defaults).
                                344                 :                :  */
                                345                 :                : TupleDesc
10690 vadim4o@yahoo.com         346                 :         528767 : CreateTupleDescCopyConstr(TupleDesc tupdesc)
                                347                 :                : {
                                348                 :                :     TupleDesc   desc;
10673 bruce@momjian.us          349                 :         528767 :     TupleConstr *constr = tupdesc->constr;
                                350                 :                :     int         i;
                                351                 :                : 
 2930 andres@anarazel.de        352                 :         528767 :     desc = CreateTemplateTupleDesc(tupdesc->natts);
                                353                 :                : 
                                354                 :                :     /* Flat-copy the attribute array (unless there are no attributes) */
  249 rhaas@postgresql.org      355         [ +  + ]:         528767 :     if (desc->natts > 0)
                                356                 :         528696 :         memcpy(TupleDescAttr(desc, 0),
                                357                 :         528696 :                TupleDescAttr(tupdesc, 0),
                                358                 :         528696 :                desc->natts * sizeof(FormData_pg_attribute));
                                359                 :                : 
  708 drowley@postgresql.o      360         [ +  + ]:        7843639 :     for (i = 0; i < desc->natts; i++)
                                361                 :                :     {
                                362                 :        7314872 :         populate_compact_attribute(desc, i);
                                363                 :                : 
  600 alvherre@alvh.no-ip.      364                 :        7314872 :         TupleDescCompactAttr(desc, i)->attnullability =
                                365                 :       14629744 :             TupleDescCompactAttr(tupdesc, i)->attnullability;
                                366                 :                :     }
                                367                 :                : 
                                368                 :                :     /* Copy the TupleConstr data structure, if any */
10674 bruce@momjian.us          369         [ +  + ]:         528767 :     if (constr)
                                370                 :                :     {
  353 michael@paquier.xyz       371                 :         471013 :         TupleConstr *cpy = palloc0_object(TupleConstr);
                                372                 :                : 
10674 bruce@momjian.us          373                 :         471013 :         cpy->has_not_null = constr->has_not_null;
 2800 peter@eisentraut.org      374                 :         471013 :         cpy->has_generated_stored = constr->has_generated_stored;
  659                           375                 :         471013 :         cpy->has_generated_virtual = constr->has_generated_virtual;
                                376                 :                : 
10674 bruce@momjian.us          377         [ +  + ]:         471013 :         if ((cpy->num_defval = constr->num_defval) > 0)
                                378                 :                :         {
  103 michael@paquier.xyz       379                 :GNC        2786 :             cpy->defval = palloc_array(AttrDefault, cpy->num_defval);
10674 bruce@momjian.us          380                 :CBC        2786 :             memcpy(cpy->defval, constr->defval, cpy->num_defval * sizeof(AttrDefault));
                                381         [ +  + ]:           6675 :             for (i = cpy->num_defval - 1; i >= 0; i--)
 2062 tgl@sss.pgh.pa.us         382                 :           3889 :                 cpy->defval[i].adbin = pstrdup(constr->defval[i].adbin);
                                383                 :                :         }
                                384                 :                : 
 3167 andrew@dunslane.net       385         [ +  + ]:         471013 :         if (constr->missing)
                                386                 :                :         {
  103 michael@paquier.xyz       387                 :GNC         428 :             cpy->missing = palloc_array(AttrMissing, tupdesc->natts);
 3167 andrew@dunslane.net       388                 :CBC         428 :             memcpy(cpy->missing, constr->missing, tupdesc->natts * sizeof(AttrMissing));
                                389         [ +  + ]:           3271 :             for (i = tupdesc->natts - 1; i >= 0; i--)
                                390                 :                :             {
 3076 akapila@postgresql.o      391         [ +  + ]:           2843 :                 if (constr->missing[i].am_present)
                                392                 :                :                 {
  708 drowley@postgresql.o      393                 :            764 :                     CompactAttribute *attr = TupleDescCompactAttr(tupdesc, i);
                                394                 :                : 
 3076 akapila@postgresql.o      395                 :            764 :                     cpy->missing[i].am_value = datumCopy(constr->missing[i].am_value,
                                396                 :            764 :                                                          attr->attbyval,
                                397                 :            764 :                                                          attr->attlen);
                                398                 :                :                 }
                                399                 :                :             }
                                400                 :                :         }
                                401                 :                : 
10674 bruce@momjian.us          402         [ +  + ]:         471013 :         if ((cpy->num_check = constr->num_check) > 0)
                                403                 :                :         {
  103 michael@paquier.xyz       404                 :GNC        1771 :             cpy->check = palloc_array(ConstrCheck, cpy->num_check);
10674 bruce@momjian.us          405                 :CBC        1771 :             memcpy(cpy->check, constr->check, cpy->num_check * sizeof(ConstrCheck));
                                406         [ +  + ]:           5215 :             for (i = cpy->num_check - 1; i >= 0; i--)
                                407                 :                :             {
 2062 tgl@sss.pgh.pa.us         408                 :           3444 :                 cpy->check[i].ccname = pstrdup(constr->check[i].ccname);
                                409                 :           3444 :                 cpy->check[i].ccbin = pstrdup(constr->check[i].ccbin);
  686 peter@eisentraut.org      410                 :           3444 :                 cpy->check[i].ccenforced = constr->check[i].ccenforced;
 5659 alvherre@alvh.no-ip.      411                 :           3444 :                 cpy->check[i].ccvalid = constr->check[i].ccvalid;
 4633 noah@leadboat.com         412                 :           3444 :                 cpy->check[i].ccnoinherit = constr->check[i].ccnoinherit;
                                413                 :                :             }
                                414                 :                :         }
                                415                 :                : 
10674 bruce@momjian.us          416                 :         471013 :         desc->constr = cpy;
                                417                 :                :     }
                                418                 :                : 
                                419                 :                :     /* We can copy the tuple type identification, too */
 8276 tgl@sss.pgh.pa.us         420                 :         528767 :     desc->tdtypeid = tupdesc->tdtypeid;
                                421                 :         528767 :     desc->tdtypmod = tupdesc->tdtypmod;
                                422                 :                : 
  257 drowley@postgresql.o      423                 :         528767 :     TupleDescFinalize(desc);
                                424                 :                : 
10674 bruce@momjian.us          425                 :         528767 :     return desc;
                                426                 :                : }
                                427                 :                : 
                                428                 :                : /*
                                429                 :                :  * TupleDescCopySize
                                430                 :                :  *      Return the number of bytes TupleDescCopySized() needs to copy src.
                                431                 :                :  */
                                432                 :                : Size
   19 andres@anarazel.de        433                 :            570 : TupleDescCopySize(TupleDesc src)
                                434                 :                : {
                                435                 :                :     return offsetof(struct TupleDescData, compact_attrs) +
   19 andres@anarazel.de        436                 :GNC         570 :         src->natts * sizeof(CompactAttribute) +
                                437                 :                :         src->natts * sizeof(FormData_pg_attribute);
                                438                 :                : }
                                439                 :                : 
                                440                 :                : /*
                                441                 :                :  * TupleDescCopySized
                                442                 :                :  *      Copy a tuple descriptor into caller-supplied memory of dstsize bytes.
                                443                 :                :  *      The memory may be shared memory mapped at any address, and must
                                444                 :                :  *      be sufficient to hold TupleDescCopySize(src) bytes.
                                445                 :                :  *
                                446                 :                :  * !!! Constraints and defaults are not copied !!!
                                447                 :                :  */
                                448                 :                : void
                                449                 :            285 : TupleDescCopySized(TupleDesc dst, Size dstsize, TupleDesc src)
                                450                 :                : {
                                451                 :            285 :     Size        sz = TupleDescCopySize(src);
                                452                 :                : 
                                453         [ -  + ]:            285 :     if (dstsize < sz)
   19 andres@anarazel.de        454         [ #  # ]:UNC           0 :         elog(ERROR, "insufficient space to copy tuple descriptor: %zu bytes, need %zu",
                                455                 :                :              dstsize, sz);
                                456                 :                : 
                                457                 :                :     /* Flat-copy the header and attribute arrays */
   19 andres@anarazel.de        458                 :GNC         285 :     memcpy(dst, src, sz);
                                459                 :                : 
                                460                 :                :     /*
                                461                 :                :      * Since we're not copying constraints and defaults, clear fields
                                462                 :                :      * associated with them.
                                463                 :                :      */
   19 andres@anarazel.de        464         [ +  + ]:CBC        1048 :     for (int i = 0; i < dst->natts; i++)
                                465                 :                :     {
 3251 tgl@sss.pgh.pa.us         466                 :            763 :         Form_pg_attribute att = TupleDescAttr(dst, i);
                                467                 :                : 
                                468                 :            763 :         att->attnotnull = false;
                                469                 :            763 :         att->atthasdef = false;
 3167 andrew@dunslane.net       470                 :            763 :         att->atthasmissing = false;
 3251 tgl@sss.pgh.pa.us         471                 :            763 :         att->attidentity = '\0';
 2800 peter@eisentraut.org      472                 :            763 :         att->attgenerated = '\0';
                                473                 :                : 
  708 drowley@postgresql.o      474                 :            763 :         populate_compact_attribute(dst, i);
                                475                 :                :     }
 3362 andres@anarazel.de        476                 :            285 :     dst->constr = NULL;
                                477                 :                : 
                                478                 :                :     /*
                                479                 :                :      * Also, assume the destination is not to be ref-counted.  (Copying the
                                480                 :                :      * source's refcount would be wrong in any case.)
                                481                 :                :      */
                                482                 :            285 :     dst->tdrefcount = -1;
                                483                 :                : 
  257 drowley@postgresql.o      484                 :            285 :     TupleDescFinalize(dst);
 3362 andres@anarazel.de        485                 :            285 : }
                                486                 :                : 
                                487                 :                : /*
                                488                 :                :  * TupleDescCopyEntry
                                489                 :                :  *      This function copies a single attribute structure from one tuple
                                490                 :                :  *      descriptor to another.
                                491                 :                :  *
                                492                 :                :  * !!! Constraints and defaults are not copied !!!
                                493                 :                :  *
                                494                 :                :  * The caller must take care of calling TupleDescFinalize() on 'dst' once all
                                495                 :                :  * TupleDesc changes have been made.
                                496                 :                :  */
                                497                 :                : void
 4755 tgl@sss.pgh.pa.us         498                 :           2715 : TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
                                499                 :                :                    TupleDesc src, AttrNumber srcAttno)
                                500                 :                : {
 3387 andres@anarazel.de        501                 :           2715 :     Form_pg_attribute dstAtt = TupleDescAttr(dst, dstAttno - 1);
                                502                 :           2715 :     Form_pg_attribute srcAtt = TupleDescAttr(src, srcAttno - 1);
                                503                 :                : 
                                504                 :                :     /*
                                505                 :                :      * sanity checks
                                506                 :                :      */
  430 peter@eisentraut.org      507         [ -  + ]:           2715 :     Assert(src);
                                508         [ -  + ]:           2715 :     Assert(dst);
 1492                           509         [ -  + ]:           2715 :     Assert(srcAttno >= 1);
                                510         [ -  + ]:           2715 :     Assert(srcAttno <= src->natts);
                                511         [ -  + ]:           2715 :     Assert(dstAttno >= 1);
                                512         [ -  + ]:           2715 :     Assert(dstAttno <= dst->natts);
                                513                 :                : 
 3387 andres@anarazel.de        514                 :           2715 :     memcpy(dstAtt, srcAtt, ATTRIBUTE_FIXED_PART_SIZE);
                                515                 :                : 
                                516                 :           2715 :     dstAtt->attnum = dstAttno;
                                517                 :                : 
                                518                 :                :     /* since we're not copying constraints or defaults, clear these */
                                519                 :           2715 :     dstAtt->attnotnull = false;
                                520                 :           2715 :     dstAtt->atthasdef = false;
 3167 andrew@dunslane.net       521                 :           2715 :     dstAtt->atthasmissing = false;
 3387 andres@anarazel.de        522                 :           2715 :     dstAtt->attidentity = '\0';
 2800 peter@eisentraut.org      523                 :           2715 :     dstAtt->attgenerated = '\0';
                                524                 :                : 
  708 drowley@postgresql.o      525                 :           2715 :     populate_compact_attribute(dst, dstAttno - 1);
 4755 tgl@sss.pgh.pa.us         526                 :           2715 : }
                                527                 :                : 
                                528                 :                : /*
                                529                 :                :  * TupleDescFinalize
                                530                 :                :  *      Finalize the given TupleDesc.  This must be called after the
                                531                 :                :  *      attributes arrays have been populated or adjusted by any code.
                                532                 :                :  *
                                533                 :                :  * Must be called after populate_compact_attribute() and before
                                534                 :                :  * BlessTupleDesc().
                                535                 :                :  */
                                536                 :                : void
  257 drowley@postgresql.o      537                 :        6492012 : TupleDescFinalize(TupleDesc tupdesc)
                                538                 :                : {
                                539                 :        6492012 :     int         firstNonCachedOffsetAttr = 0;
                                540                 :        6492012 :     int         firstNonGuaranteedAttr = tupdesc->natts;
                                541                 :        6492012 :     int         off = 0;
                                542                 :                : 
                                543         [ +  + ]:       37165719 :     for (int i = 0; i < tupdesc->natts; i++)
                                544                 :                :     {
                                545                 :       34001503 :         CompactAttribute *cattr = TupleDescCompactAttr(tupdesc, i);
  175                           546                 :       34001503 :         Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
                                547                 :                : 
                                548                 :                :         /*
                                549                 :                :          * Find the highest attnum which is guaranteed to exist in all tuples
                                550                 :                :          * in the table.  We currently only pay attention to byval attributes
                                551                 :                :          * to allow additional optimizations during tuple deformation.
                                552                 :                :          */
  257                           553         [ +  + ]:       34001503 :         if (firstNonGuaranteedAttr == tupdesc->natts &&
                                554   [ +  +  +  +  :       13745563 :             (cattr->attnullability != ATTNULLABLE_VALID || !cattr->attbyval ||
                                              +  + ]
  175                           555         [ +  - ]:        7788491 :              cattr->atthasmissing || cattr->attisdropped ||
                                556         [ +  - ]:        7788491 :              cattr->attlen <= 0 ||
                                557         [ +  + ]:        7788491 :              attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL))
  257                           558                 :        5957455 :             firstNonGuaranteedAttr = i;
                                559                 :                : 
                                560                 :                :         /*
                                561                 :                :          * Don't cache offsets beyond fixed-width attributes.  Virtual
                                562                 :                :          * generated attributes are stored as NULLs in the tuple, so we don't
                                563                 :                :          * cache offsets beyond these.
                                564                 :                :          */
  175                           565         [ +  + ]:       34001503 :         if (cattr->attlen <= 0 ||
                                566         [ +  + ]:       30679475 :             attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
                                567                 :                :             break;
                                568                 :                : 
  257                           569                 :       30673707 :         off = att_nominal_alignby(off, cattr->attalignby);
                                570                 :                : 
                                571                 :                :         /*
                                572                 :                :          * attcacheoff is an int16, so don't try to cache any offsets larger
                                573                 :                :          * than will fit in that type.  Any attributes which are offset more
                                574                 :                :          * than 2^15 are likely due to variable-length attributes.  Since we
                                575                 :                :          * don't cache offsets for or beyond variable-length attributes, using
                                576                 :                :          * an int16 rather than an int32 here is unlikely to cost us anything.
                                577                 :                :          */
  256                           578         [ -  + ]:       30673707 :         if (off > PG_INT16_MAX)
  256 drowley@postgresql.o      579                 :UBC           0 :             break;
                                580                 :                : 
  256 drowley@postgresql.o      581                 :CBC    30673707 :         cattr->attcacheoff = (int16) off;
                                582                 :                : 
  257                           583                 :       30673707 :         off += cattr->attlen;
                                584                 :       30673707 :         firstNonCachedOffsetAttr = i + 1;
                                585                 :                :     }
                                586                 :                : 
                                587                 :        6492012 :     tupdesc->firstNonCachedOffsetAttr = firstNonCachedOffsetAttr;
                                588                 :        6492012 :     tupdesc->firstNonGuaranteedAttr = firstNonGuaranteedAttr;
                                589                 :                : 
                                590                 :                :     /* set the aggregate version of the tupledesc, as appropriate */
   19 andres@anarazel.de        591   [ +  +  +  + ]:GNC     6492012 :     if (tupdesc->natts == 0 || tupdesc->tdtypeid == RECORDOID)
                                592                 :        4630293 :         TupleDescSetVersion(tupdesc, InvalidXLogRecPtr);
                                593                 :                :     else
                                594                 :        1861719 :         TupleDescSetVersion(tupdesc,
                                595                 :        1861719 :                             TupleDescGetAttrVersion(tupdesc, tupdesc->natts));
  257 drowley@postgresql.o      596                 :CBC     6492012 : }
                                597                 :                : 
                                598                 :                : /*
                                599                 :                :  * Free a TupleDesc including all substructure
                                600                 :                :  */
                                601                 :                : void
10674 bruce@momjian.us          602                 :         941837 : FreeTupleDesc(TupleDesc tupdesc)
                                603                 :                : {
                                604                 :                :     int         i;
                                605                 :                : 
                                606                 :                :     /*
                                607                 :                :      * Possibly this should assert tdrefcount == 0, to disallow explicit
                                608                 :                :      * freeing of un-refcounted tupdescs?
                                609                 :                :      */
 7470 tgl@sss.pgh.pa.us         610         [ -  + ]:         941837 :     Assert(tupdesc->tdrefcount <= 0);
                                611                 :                : 
10674 bruce@momjian.us          612         [ +  + ]:         941837 :     if (tupdesc->constr)
                                613                 :                :     {
                                614         [ +  + ]:         268012 :         if (tupdesc->constr->num_defval > 0)
                                615                 :                :         {
10673                           616                 :          21960 :             AttrDefault *attrdef = tupdesc->constr->defval;
                                617                 :                : 
10674                           618         [ +  + ]:          53040 :             for (i = tupdesc->constr->num_defval - 1; i >= 0; i--)
 2062 tgl@sss.pgh.pa.us         619                 :          31080 :                 pfree(attrdef[i].adbin);
10674 bruce@momjian.us          620                 :          21960 :             pfree(attrdef);
                                621                 :                :         }
 3167 andrew@dunslane.net       622         [ +  + ]:         268012 :         if (tupdesc->constr->missing)
                                623                 :                :         {
                                624                 :           2618 :             AttrMissing *attrmiss = tupdesc->constr->missing;
                                625                 :                : 
                                626         [ +  + ]:          18298 :             for (i = tupdesc->natts - 1; i >= 0; i--)
                                627                 :                :             {
 3076 akapila@postgresql.o      628         [ +  + ]:          15680 :                 if (attrmiss[i].am_present
 3167 andrew@dunslane.net       629         [ +  + ]:           5419 :                     && !TupleDescAttr(tupdesc, i)->attbyval)
 3076 akapila@postgresql.o      630                 :           1984 :                     pfree(DatumGetPointer(attrmiss[i].am_value));
                                631                 :                :             }
 3167 andrew@dunslane.net       632                 :           2618 :             pfree(attrmiss);
                                633                 :                :         }
10674 bruce@momjian.us          634         [ +  + ]:         268012 :         if (tupdesc->constr->num_check > 0)
                                635                 :                :         {
10673                           636                 :           7820 :             ConstrCheck *check = tupdesc->constr->check;
                                637                 :                : 
10674                           638         [ +  + ]:          21448 :             for (i = tupdesc->constr->num_check - 1; i >= 0; i--)
                                639                 :                :             {
 2062 tgl@sss.pgh.pa.us         640                 :          13628 :                 pfree(check[i].ccname);
                                641                 :          13628 :                 pfree(check[i].ccbin);
                                642                 :                :             }
10674 bruce@momjian.us          643                 :           7820 :             pfree(check);
                                644                 :                :         }
                                645                 :         268012 :         pfree(tupdesc->constr);
                                646                 :                :     }
                                647                 :                : 
                                648                 :         941837 :     pfree(tupdesc);
10690 vadim4o@yahoo.com         649                 :         941837 : }
                                650                 :                : 
                                651                 :                : /*
                                652                 :                :  * Increment the reference count of a tupdesc, and log the reference in
                                653                 :                :  * CurrentResourceOwner.
                                654                 :                :  *
                                655                 :                :  * Do not apply this to tupdescs that are not being refcounted.  (Use the
                                656                 :                :  * macro PinTupleDesc for tupdescs of uncertain status.)
                                657                 :                :  */
                                658                 :                : void
 7470 tgl@sss.pgh.pa.us         659                 :       13444546 : IncrTupleDescRefCount(TupleDesc tupdesc)
                                660                 :                : {
                                661         [ -  + ]:       13444546 :     Assert(tupdesc->tdrefcount >= 0);
                                662                 :                : 
 1116 heikki.linnakangas@i      663                 :       13444546 :     ResourceOwnerEnlarge(CurrentResourceOwner);
 7470 tgl@sss.pgh.pa.us         664                 :       13444546 :     tupdesc->tdrefcount++;
                                665                 :       13444546 :     ResourceOwnerRememberTupleDesc(CurrentResourceOwner, tupdesc);
                                666                 :       13444546 : }
                                667                 :                : 
                                668                 :                : /*
                                669                 :                :  * Decrement the reference count of a tupdesc, remove the corresponding
                                670                 :                :  * reference from CurrentResourceOwner, and free the tupdesc if no more
                                671                 :                :  * references remain.
                                672                 :                :  *
                                673                 :                :  * Do not apply this to tupdescs that are not being refcounted.  (Use the
                                674                 :                :  * macro ReleaseTupleDesc for tupdescs of uncertain status.)
                                675                 :                :  */
                                676                 :                : void
                                677                 :       13432053 : DecrTupleDescRefCount(TupleDesc tupdesc)
                                678                 :                : {
                                679         [ -  + ]:       13432053 :     Assert(tupdesc->tdrefcount > 0);
                                680                 :                : 
                                681                 :       13432053 :     ResourceOwnerForgetTupleDesc(CurrentResourceOwner, tupdesc);
                                682         [ +  + ]:       13432053 :     if (--tupdesc->tdrefcount == 0)
                                683                 :              4 :         FreeTupleDesc(tupdesc);
                                684                 :       13432053 : }
                                685                 :                : 
                                686                 :                : XLogRecPtr
   19 andres@anarazel.de        687                 :        9321747 : TupleDescGetVersion(TupleDesc tupdesc)
                                688                 :                : {
                                689                 :                :     /*
                                690                 :                :      * RECORDOID descriptors are often created by copying the descriptor of a
                                691                 :                :      * named composite type and relabeling the copy. The versions of the named
                                692                 :                :      * type remain in the copy, but don't apply to it.
                                693                 :                :      */
                                694         [ +  + ]:        9321747 :     if (tupdesc->tdtypeid == RECORDOID)
                                695                 :        1596338 :         return InvalidXLogRecPtr;
   19 andres@anarazel.de        696                 :GNC     7725409 :     return tupdesc->tdversion;
                                697                 :                : }
                                698                 :                : 
                                699                 :                : void
   19 andres@anarazel.de        700                 :CBC    13145655 : TupleDescSetVersion(TupleDesc tupdesc, XLogRecPtr version)
                                701                 :                : {
   19 andres@anarazel.de        702   [ +  +  -  + ]:GNC    13145655 :     Assert(tupdesc->tdtypeid != RECORDOID ||
                                703                 :                :            version == InvalidXLogRecPtr);
                                704                 :                : 
                                705                 :       13145655 :     tupdesc->tdversion = version;
   19 andres@anarazel.de        706                 :CBC    13145655 : }
                                707                 :                : 
                                708                 :                : XLogRecPtr
                                709                 :        3554439 : TupleDescGetAttrVersion(TupleDesc tupdesc, AttrNumber attnum)
                                710                 :                : {
                                711   [ +  -  -  + ]:        3554439 :     Assert(attnum >= 1 && attnum <= tupdesc->natts);
                                712                 :                :     /* see TupleDescGetVersion() */
                                713         [ -  + ]:        3554439 :     if (tupdesc->tdtypeid == RECORDOID)
   19 andres@anarazel.de        714                 :LBC      (1035) :         return InvalidXLogRecPtr;
   19 andres@anarazel.de        715                 :GNC     3554439 :     return TupleDescAttr(tupdesc, attnum - 1)->attversion;
   19 andres@anarazel.de        716                 :ECB   (7971648) : }
                                717                 :                : 
                                718                 :                : /*
                                719                 :                :  * Compare two TupleDesc structures for logical equality
                                720                 :                :  */
                                721                 :                : bool
 9798 tgl@sss.pgh.pa.us         722                 :CBC      268595 : equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
                                723                 :                : {
                                724                 :                :     int         i,
                                725                 :                :                 n;
                                726                 :                : 
                                727         [ +  + ]:         268595 :     if (tupdesc1->natts != tupdesc2->natts)
                                728                 :           4074 :         return false;
 8276                           729         [ -  + ]:         264521 :     if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
 8276 tgl@sss.pgh.pa.us         730                 :UBC           0 :         return false;
                                731                 :                : 
                                732                 :                :     /*
                                733                 :                :      * Need to take the version into account, otherwise
                                734                 :                :      * RelationRebuildRelation() will consider two tupledescs that differ just
                                735                 :                :      * by version equal and not swap them.
                                736                 :                :      */
   19 andres@anarazel.de        737         [ +  + ]:CBC      264521 :     if (TupleDescGetVersion(tupdesc1) != TupleDescGetVersion(tupdesc2))
                                738                 :          41176 :         return false;
                                739                 :                : 
                                740                 :                :     /* tdtypmod and tdrefcount are not checked */
                                741                 :                : 
 9798 tgl@sss.pgh.pa.us         742         [ +  + ]:         998080 :     for (i = 0; i < tupdesc1->natts; i++)
                                743                 :                :     {
 3387 andres@anarazel.de        744                 :         783483 :         Form_pg_attribute attr1 = TupleDescAttr(tupdesc1, i);
                                745                 :         783483 :         Form_pg_attribute attr2 = TupleDescAttr(tupdesc2, i);
                                746                 :                : 
                                747                 :                :         /*
                                748                 :                :          * We do not need to check every single field here: we can disregard
                                749                 :                :          * attrelid and attnum (which were used to place the row in the attrs
                                750                 :                :          * array in the first place).  It might look like we could dispense
                                751                 :                :          * with checking attlen/attbyval/attalign, since these are derived
                                752                 :                :          * from atttypid; but in the case of dropped columns we must check
                                753                 :                :          * them (since atttypid will be zero for all dropped columns) and in
                                754                 :                :          * general it seems safer to check them always.
                                755                 :                :          *
                                756                 :                :          * We intentionally ignore atthasmissing, since that's not very
                                757                 :                :          * relevant in tupdescs, which lack the attmissingval field.
                                758                 :                :          */
 9798 tgl@sss.pgh.pa.us         759         [ +  + ]:         783483 :         if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
                                760                 :            942 :             return false;
                                761         [ +  + ]:         782541 :         if (attr1->atttypid != attr2->atttypid)
                                762                 :             94 :             return false;
 7898                           763         [ +  + ]:         782447 :         if (attr1->attlen != attr2->attlen)
                                764                 :              7 :             return false;
   19 andres@anarazel.de        765         [ -  + ]:GNC      782440 :         if (attr1->attversion != attr2->attversion)
   19 andres@anarazel.de        766                 :UNC           0 :             return false;
 8276 tgl@sss.pgh.pa.us         767         [ -  + ]:CBC      782440 :         if (attr1->attndims != attr2->attndims)
 8276 tgl@sss.pgh.pa.us         768                 :UBC           0 :             return false;
 9798 tgl@sss.pgh.pa.us         769         [ -  + ]:CBC      782440 :         if (attr1->atttypmod != attr2->atttypmod)
 9798 tgl@sss.pgh.pa.us         770                 :UBC           0 :             return false;
 7898 tgl@sss.pgh.pa.us         771         [ +  + ]:CBC      782440 :         if (attr1->attbyval != attr2->attbyval)
                                772                 :             39 :             return false;
 2015                           773         [ -  + ]:         782401 :         if (attr1->attalign != attr2->attalign)
 2015 tgl@sss.pgh.pa.us         774                 :UBC           0 :             return false;
 9798 tgl@sss.pgh.pa.us         775         [ +  + ]:CBC      782401 :         if (attr1->attstorage != attr2->attstorage)
                                776                 :            227 :             return false;
 2015                           777         [ +  + ]:         782174 :         if (attr1->attcompression != attr2->attcompression)
 7898                           778                 :             45 :             return false;
 9798                           779         [ +  + ]:         782129 :         if (attr1->attnotnull != attr2->attnotnull)
                                780                 :           1210 :             return false;
                                781                 :                : 
                                782                 :                :         /*
                                783                 :                :          * When the column has a not-null constraint, we also need to consider
                                784                 :                :          * its validity aspect, which only manifests in CompactAttribute->
                                785                 :                :          * attnullability, so verify that.
                                786                 :                :          */
  600 alvherre@alvh.no-ip.      787         [ +  + ]:         780919 :         if (attr1->attnotnull)
                                788                 :                :         {
                                789                 :         265211 :             CompactAttribute *cattr1 = TupleDescCompactAttr(tupdesc1, i);
                                790                 :         265211 :             CompactAttribute *cattr2 = TupleDescCompactAttr(tupdesc2, i);
                                791                 :                : 
                                792         [ -  + ]:         265211 :             Assert(cattr1->attnullability != ATTNULLABLE_UNKNOWN);
                                793         [ -  + ]:         265211 :             Assert((cattr1->attnullability == ATTNULLABLE_UNKNOWN) ==
                                794                 :                :                    (cattr2->attnullability == ATTNULLABLE_UNKNOWN));
                                795                 :                : 
                                796         [ +  + ]:         265211 :             if (cattr1->attnullability != cattr2->attnullability)
                                797                 :             54 :                 return false;
                                798                 :                :         }
 8856 tgl@sss.pgh.pa.us         799         [ +  + ]:         780865 :         if (attr1->atthasdef != attr2->atthasdef)
                                800                 :           3697 :             return false;
 3523 peter_e@gmx.net           801         [ +  + ]:         777168 :         if (attr1->attidentity != attr2->attidentity)
                                802                 :            125 :             return false;
 2800 peter@eisentraut.org      803         [ +  + ]:         777043 :         if (attr1->attgenerated != attr2->attgenerated)
                                804                 :             37 :             return false;
 8884 tgl@sss.pgh.pa.us         805         [ -  + ]:         777006 :         if (attr1->attisdropped != attr2->attisdropped)
 8884 tgl@sss.pgh.pa.us         806                 :UBC           0 :             return false;
 8833 tgl@sss.pgh.pa.us         807         [ +  + ]:CBC      777006 :         if (attr1->attislocal != attr2->attislocal)
                                808                 :           1770 :             return false;
                                809         [ +  + ]:         775236 :         if (attr1->attinhcount != attr2->attinhcount)
 8856                           810                 :            501 :             return false;
 5699                           811         [ -  + ]:         774735 :         if (attr1->attcollation != attr2->attcollation)
 5699 tgl@sss.pgh.pa.us         812                 :UBC           0 :             return false;
                                813                 :                :         /* variable-length fields are not even present... */
                                814                 :                :     }
                                815                 :                : 
 9798 tgl@sss.pgh.pa.us         816         [ +  + ]:CBC      214597 :     if (tupdesc1->constr != NULL)
                                817                 :                :     {
 9726 bruce@momjian.us          818                 :          77759 :         TupleConstr *constr1 = tupdesc1->constr;
                                819                 :          77759 :         TupleConstr *constr2 = tupdesc2->constr;
                                820                 :                : 
 9798 tgl@sss.pgh.pa.us         821         [ +  + ]:          77759 :         if (constr2 == NULL)
                                822                 :            151 :             return false;
 9516                           823         [ -  + ]:          77608 :         if (constr1->has_not_null != constr2->has_not_null)
 9516 tgl@sss.pgh.pa.us         824                 :UBC           0 :             return false;
 2800 peter@eisentraut.org      825         [ +  + ]:CBC       77608 :         if (constr1->has_generated_stored != constr2->has_generated_stored)
                                826                 :             29 :             return false;
  659                           827         [ +  + ]:          77579 :         if (constr1->has_generated_virtual != constr2->has_generated_virtual)
                                828                 :             23 :             return false;
 9516 tgl@sss.pgh.pa.us         829                 :          77556 :         n = constr1->num_defval;
                                830         [ -  + ]:          77556 :         if (n != (int) constr2->num_defval)
 9798 tgl@sss.pgh.pa.us         831                 :UBC           0 :             return false;
                                832                 :                :         /* We assume here that both AttrDefault arrays are in adnum order */
 9516 tgl@sss.pgh.pa.us         833         [ +  + ]:CBC       90168 :         for (i = 0; i < n; i++)
                                834                 :                :         {
 9726 bruce@momjian.us          835                 :          12612 :             AttrDefault *defval1 = constr1->defval + i;
 2062 tgl@sss.pgh.pa.us         836                 :          12612 :             AttrDefault *defval2 = constr2->defval + i;
                                837                 :                : 
                                838         [ -  + ]:          12612 :             if (defval1->adnum != defval2->adnum)
 9798 tgl@sss.pgh.pa.us         839                 :UBC           0 :                 return false;
 9798 tgl@sss.pgh.pa.us         840         [ -  + ]:CBC       12612 :             if (strcmp(defval1->adbin, defval2->adbin) != 0)
 9798 tgl@sss.pgh.pa.us         841                 :UBC           0 :                 return false;
                                842                 :                :         }
 3167 andrew@dunslane.net       843         [ +  + ]:CBC       77556 :         if (constr1->missing)
                                844                 :                :         {
                                845         [ +  + ]:            547 :             if (!constr2->missing)
                                846                 :             66 :                 return false;
                                847         [ +  + ]:           2776 :             for (i = 0; i < tupdesc1->natts; i++)
                                848                 :                :             {
                                849                 :           2400 :                 AttrMissing *missval1 = constr1->missing + i;
                                850                 :           2400 :                 AttrMissing *missval2 = constr2->missing + i;
                                851                 :                : 
 3076 akapila@postgresql.o      852         [ +  + ]:           2400 :                 if (missval1->am_present != missval2->am_present)
 3167 andrew@dunslane.net       853                 :            105 :                     return false;
 3076 akapila@postgresql.o      854         [ +  + ]:           2295 :                 if (missval1->am_present)
                                855                 :                :                 {
  708 drowley@postgresql.o      856                 :            787 :                     CompactAttribute *missatt1 = TupleDescCompactAttr(tupdesc1, i);
                                857                 :                : 
 3076 akapila@postgresql.o      858         [ -  + ]:            787 :                     if (!datumIsEqual(missval1->am_value, missval2->am_value,
 3167 andrew@dunslane.net       859                 :            787 :                                       missatt1->attbyval, missatt1->attlen))
 3167 andrew@dunslane.net       860                 :UBC           0 :                         return false;
                                861                 :                :                 }
                                862                 :                :             }
                                863                 :                :         }
 3167 andrew@dunslane.net       864         [ +  + ]:CBC       77009 :         else if (constr2->missing)
                                865                 :            275 :             return false;
 9516 tgl@sss.pgh.pa.us         866                 :          77110 :         n = constr1->num_check;
                                867         [ +  + ]:          77110 :         if (n != (int) constr2->num_check)
 9798                           868                 :           1107 :             return false;
                                869                 :                : 
                                870                 :                :         /*
                                871                 :                :          * Similarly, we rely here on the ConstrCheck entries being sorted by
                                872                 :                :          * name.  If there are duplicate names, the outcome of the comparison
                                873                 :                :          * is uncertain, but that should not happen.
                                874                 :                :          */
 9516                           875         [ +  + ]:          78368 :         for (i = 0; i < n; i++)
                                876                 :                :         {
 9726 bruce@momjian.us          877                 :           2529 :             ConstrCheck *check1 = constr1->check + i;
 2062 tgl@sss.pgh.pa.us         878                 :           2529 :             ConstrCheck *check2 = constr2->check + i;
                                879                 :                : 
                                880         [ +  - ]:           2529 :             if (!(strcmp(check1->ccname, check2->ccname) == 0 &&
                                881         [ +  - ]:           2529 :                   strcmp(check1->ccbin, check2->ccbin) == 0 &&
  686 peter@eisentraut.org      882         [ +  + ]:           2529 :                   check1->ccenforced == check2->ccenforced &&
 2062 tgl@sss.pgh.pa.us         883         [ +  + ]:           2425 :                   check1->ccvalid == check2->ccvalid &&
                                884         [ -  + ]:           2365 :                   check1->ccnoinherit == check2->ccnoinherit))
 9798                           885                 :            164 :                 return false;
                                886                 :                :         }
                                887                 :                :     }
                                888         [ +  + ]:         136838 :     else if (tupdesc2->constr != NULL)
                                889                 :            761 :         return false;
                                890                 :         211916 :     return true;
                                891                 :                : }
                                892                 :                : 
                                893                 :                : /*
                                894                 :                :  * equalRowTypes
                                895                 :                :  *
                                896                 :                :  * This determines whether two tuple descriptors have equal row types.  This
                                897                 :                :  * only checks those fields in pg_attribute that are applicable for row types,
                                898                 :                :  * while ignoring those fields that define the physical row storage or those
                                899                 :                :  * that define table column metadata.
                                900                 :                :  *
                                901                 :                :  * Specifically, this checks:
                                902                 :                :  *
                                903                 :                :  * - same number of attributes
                                904                 :                :  * - same composite type ID (but could both be zero)
                                905                 :                :  * - corresponding attributes (in order) have same the name, type, typmod,
                                906                 :                :  *   collation
                                907                 :                :  * - corresponding dropped attributes have the same width and alignment
                                908                 :                :  *   (needed because no other test reliably detects pairs of dropped
                                909                 :                :  *   attributes whose types would otherwise make tuple deformation unsafe)
                                910                 :                :  * - the versions of the descriptors
                                911                 :                :  *
                                912                 :                :  * This is used to check whether two record types are compatible, whether
                                913                 :                :  * function return row types are the same, and other similar situations.
                                914                 :                :  *
                                915                 :                :  * (XXX There was some discussion whether attndims should be checked here, but
                                916                 :                :  * for now it has been decided not to.)
                                917                 :                :  *
                                918                 :                :  * Note: We deliberately do not check the tdtypmod field.  This allows
                                919                 :                :  * typcache.c to use this routine to see if a cached record type matches a
                                920                 :                :  * requested type.
                                921                 :                :  */
                                922                 :                : bool
  986 peter@eisentraut.org      923                 :         331619 : equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2)
                                924                 :                : {
                                925         [ +  + ]:         331619 :     if (tupdesc1->natts != tupdesc2->natts)
                                926                 :            116 :         return false;
                                927         [ +  + ]:         331503 :     if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
                                928                 :           1332 :         return false;
                                929                 :                : 
                                930                 :                :     /*
                                931                 :                :      * For !RECORDOID style descriptors, check that the versions match. That's
                                932                 :                :      * important, as several callers assume that they can continue to use the
                                933                 :                :      * old descriptor if this function returns true - which in turn can
                                934                 :                :      * trigger errors about version mismatches. Note that this does not make
                                935                 :                :      * this function much more aggressive, even without this check we'd return
                                936                 :                :      * false for descriptors of two tables with equivalent schemas, due to the
                                937                 :                :      * tdtypeid check above.
                                938                 :                :      *
                                939                 :                :      * We explicitly do *not* perform this check in the RECORDOID case (see
                                940                 :                :      * also function comment): For one, the version is not normally set for
                                941                 :                :      * RECORDOID descriptors, for another, these are transient types, so it's
                                942                 :                :      * normal to transfer data between semantically equivalent, but separately
                                943                 :                :      * instantiated descriptors.
                                944                 :                :      */
   19 andres@anarazel.de        945   [ +  +  +  + ]:         330681 :     if (tupdesc1->tdtypeid != RECORDOID &&
                                946                 :            510 :         TupleDescGetVersion(tupdesc1) != TupleDescGetVersion(tupdesc2))
                                947                 :             12 :         return false;
                                948                 :                : 
  986 peter@eisentraut.org      949         [ +  + ]:        4963422 :     for (int i = 0; i < tupdesc1->natts; i++)
                                950                 :                :     {
                                951                 :        4639644 :         Form_pg_attribute attr1 = TupleDescAttr(tupdesc1, i);
                                952                 :        4639644 :         Form_pg_attribute attr2 = TupleDescAttr(tupdesc2, i);
                                953                 :                : 
                                954         [ +  + ]:        4639644 :         if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
                                955                 :           6363 :             return false;
                                956         [ +  + ]:        4633281 :         if (attr1->atttypid != attr2->atttypid)
                                957                 :             14 :             return false;
                                958         [ +  + ]:        4633267 :         if (attr1->atttypmod != attr2->atttypmod)
                                959                 :              4 :             return false;
                                960         [ -  + ]:        4633263 :         if (attr1->attcollation != attr2->attcollation)
  986 peter@eisentraut.org      961                 :UBC           0 :             return false;
                                962                 :                : 
                                963                 :                :         /* Record types derived from tables could have dropped fields. */
  986 peter@eisentraut.org      964         [ -  + ]:CBC     4633263 :         if (attr1->attisdropped != attr2->attisdropped)
  986 peter@eisentraut.org      965                 :UBC           0 :             return false;
                                966                 :                : 
                                967                 :                :         /*
                                968                 :                :          * Dropped fields could have incompatible lengths/alignments (can't
                                969                 :                :          * rely on atttypid for this)
                                970                 :                :          */
   19 pg@bowt.ie                971         [ +  + ]:CBC     4633263 :         if (attr1->attisdropped)
                                972                 :                :         {
                                973         [ -  + ]:             31 :             if (attr1->attlen != attr2->attlen)
   19 pg@bowt.ie                974                 :UBC           0 :                 return false;
   19 pg@bowt.ie                975         [ -  + ]:CBC          31 :             if (attr1->attalign != attr2->attalign)
   19 pg@bowt.ie                976                 :UBC           0 :                 return false;
                                977                 :                :         }
                                978                 :                :     }
                                979                 :                : 
  986 peter@eisentraut.org      980                 :CBC      323778 :     return true;
                                981                 :                : }
                                982                 :                : 
                                983                 :                : /*
                                984                 :                :  * hashRowType
                                985                 :                :  *
                                986                 :                :  * If two tuple descriptors would be considered equal by equalRowTypes()
                                987                 :                :  * then their hash value will be equal according to this function.
                                988                 :                :  */
                                989                 :                : uint32
                                990                 :         341967 : hashRowType(TupleDesc desc)
                                991                 :                : {
                                992                 :                :     uint32      s;
                                993                 :                :     int         i;
                                994                 :                : 
  480                           995                 :         341967 :     s = hash_combine(0, hash_bytes_uint32(desc->natts));
                                996                 :         341967 :     s = hash_combine(s, hash_bytes_uint32(desc->tdtypeid));
 3385 andres@anarazel.de        997         [ +  + ]:        5172218 :     for (i = 0; i < desc->natts; ++i)
  480 peter@eisentraut.org      998                 :        4830251 :         s = hash_combine(s, hash_bytes_uint32(TupleDescAttr(desc, i)->atttypid));
                                999                 :                : 
 3385 andres@anarazel.de       1000                 :         341967 :     return s;
                               1001                 :                : }
                               1002                 :                : 
                               1003                 :                : /*
                               1004                 :                :  * TupleDescInitEntry
                               1005                 :                :  *      This function initializes a single attribute structure in
                               1006                 :                :  *      a previously allocated tuple descriptor.
                               1007                 :                :  *
                               1008                 :                :  * If attributeName is NULL, the attname field is set to an empty string
                               1009                 :                :  * (this is for cases where we don't know or need a name for the field).
                               1010                 :                :  * Also, some callers use this function to change the datatype-related fields
                               1011                 :                :  * in an existing tupdesc; they pass attributeName = NameStr(att->attname)
                               1012                 :                :  * to indicate that the attname field shouldn't be modified.
                               1013                 :                :  *
                               1014                 :                :  * Note that attcollation is set to the default for the specified datatype.
                               1015                 :                :  * If a nondefault collation is needed, insert it afterwards using
                               1016                 :                :  * TupleDescInitEntryCollation.
                               1017                 :                :  */
                               1018                 :                : void
11099 scrappy@hub.org          1019                 :        9180835 : TupleDescInitEntry(TupleDesc desc,
                               1020                 :                :                    AttrNumber attributeNumber,
                               1021                 :                :                    const char *attributeName,
                               1022                 :                :                    Oid oidtypeid,
                               1023                 :                :                    int32 typmod,
                               1024                 :                :                    int attdim)
                               1025                 :                : {
                               1026                 :                :     HeapTuple   tuple;
                               1027                 :                :     Form_pg_type typeForm;
                               1028                 :                :     Form_pg_attribute att;
                               1029                 :                : 
                               1030                 :                :     /*
                               1031                 :                :      * sanity checks
                               1032                 :                :      */
  430 peter@eisentraut.org     1033         [ -  + ]:        9180835 :     Assert(desc);
 1492                          1034         [ -  + ]:        9180835 :     Assert(attributeNumber >= 1);
                               1035         [ -  + ]:        9180835 :     Assert(attributeNumber <= desc->natts);
 1341                          1036         [ -  + ]:        9180835 :     Assert(attdim >= 0);
                               1037         [ -  + ]:        9180835 :     Assert(attdim <= PG_INT16_MAX);
                               1038                 :                : 
                               1039                 :                :     /*
                               1040                 :                :      * initialize the attribute fields
                               1041                 :                :      */
 3387 andres@anarazel.de       1042                 :        9180835 :     att = TupleDescAttr(desc, attributeNumber - 1);
                               1043                 :                : 
10674 bruce@momjian.us         1044                 :        9180835 :     att->attrelid = 0;           /* dummy value */
                               1045                 :                : 
                               1046                 :                :     /*
                               1047                 :                :      * Note: attributeName can be NULL, because the planner doesn't always
                               1048                 :                :      * fill in valid resname values in targetlists, particularly for resjunk
                               1049                 :                :      * attributes. Also, do nothing if caller wants to re-use the old attname.
                               1050                 :                :      */
 4779 tgl@sss.pgh.pa.us        1051         [ +  + ]:        9180835 :     if (attributeName == NULL)
 9883 bruce@momjian.us         1052   [ -  +  -  -  :        2750533 :         MemSet(NameStr(att->attname), 0, NAMEDATALEN);
                                     -  -  -  -  -  
                                                 - ]
 4779 tgl@sss.pgh.pa.us        1053         [ +  + ]:        6430302 :     else if (attributeName != NameStr(att->attname))
                               1054                 :        6428123 :         namestrcpy(&(att->attname), attributeName);
                               1055                 :                : 
10518 bruce@momjian.us         1056                 :        9180835 :     att->atttypmod = typmod;
                               1057                 :                : 
10674                          1058                 :        9180835 :     att->attnum = attributeNumber;
 9336 tgl@sss.pgh.pa.us        1059                 :        9180835 :     att->attndims = attdim;
                               1060                 :                : 
10674 bruce@momjian.us         1061                 :        9180835 :     att->attnotnull = false;
                               1062                 :        9180835 :     att->atthasdef = false;
 3167 andrew@dunslane.net      1063                 :        9180835 :     att->atthasmissing = false;
 3523 peter_e@gmx.net          1064                 :        9180835 :     att->attidentity = '\0';
 2800 peter@eisentraut.org     1065                 :        9180835 :     att->attgenerated = '\0';
 8884 tgl@sss.pgh.pa.us        1066                 :        9180835 :     att->attisdropped = false;
 8833                          1067                 :        9180835 :     att->attislocal = true;
                               1068                 :        9180835 :     att->attinhcount = 0;
   19 andres@anarazel.de       1069                 :GNC     9180835 :     att->attversion = InvalidXLogRecPtr;
                               1070                 :                :     /* variable-length fields are not present in tupledescs */
                               1071                 :                : 
 6131 rhaas@postgresql.org     1072                 :CBC     9180835 :     tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(oidtypeid));
10674 bruce@momjian.us         1073         [ -  + ]:        9180835 :     if (!HeapTupleIsValid(tuple))
 8531 tgl@sss.pgh.pa.us        1074         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for type %u", oidtypeid);
10315 bruce@momjian.us         1075                 :CBC     9180835 :     typeForm = (Form_pg_type) GETSTRUCT(tuple);
                               1076                 :                : 
 8276 tgl@sss.pgh.pa.us        1077                 :        9180835 :     att->atttypid = oidtypeid;
                               1078                 :        9180835 :     att->attlen = typeForm->typlen;
                               1079                 :        9180835 :     att->attbyval = typeForm->typbyval;
                               1080                 :        9180835 :     att->attalign = typeForm->typalign;
                               1081                 :        9180835 :     att->attstorage = typeForm->typstorage;
 2011                          1082                 :        9180835 :     att->attcompression = InvalidCompressionMethod;
 2015                          1083                 :        9180835 :     att->attcollation = typeForm->typcollation;
                               1084                 :                : 
  708 drowley@postgresql.o     1085                 :        9180835 :     populate_compact_attribute(desc, attributeNumber - 1);
                               1086                 :                : 
 9508 tgl@sss.pgh.pa.us        1087                 :        9180835 :     ReleaseSysCache(tuple);
11099 scrappy@hub.org          1088                 :        9180835 : }
                               1089                 :                : 
                               1090                 :                : /*
                               1091                 :                :  * TupleDescInitBuiltinEntry
                               1092                 :                :  *      Initialize a tuple descriptor without catalog access.  Only
                               1093                 :                :  *      a limited range of builtin types are supported.
                               1094                 :                :  */
                               1095                 :                : void
 3595 rhaas@postgresql.org     1096                 :           9229 : TupleDescInitBuiltinEntry(TupleDesc desc,
                               1097                 :                :                           AttrNumber attributeNumber,
                               1098                 :                :                           const char *attributeName,
                               1099                 :                :                           Oid oidtypeid,
                               1100                 :                :                           int32 typmod,
                               1101                 :                :                           int attdim)
                               1102                 :                : {
                               1103                 :                :     Form_pg_attribute att;
                               1104                 :                : 
                               1105                 :                :     /* sanity checks */
  430 peter@eisentraut.org     1106         [ -  + ]:           9229 :     Assert(desc);
 1492                          1107         [ -  + ]:           9229 :     Assert(attributeNumber >= 1);
                               1108         [ -  + ]:           9229 :     Assert(attributeNumber <= desc->natts);
 1341                          1109         [ -  + ]:           9229 :     Assert(attdim >= 0);
                               1110         [ -  + ]:           9229 :     Assert(attdim <= PG_INT16_MAX);
                               1111                 :                : 
                               1112                 :                :     /* initialize the attribute fields */
 3387 andres@anarazel.de       1113                 :           9229 :     att = TupleDescAttr(desc, attributeNumber - 1);
 3595 rhaas@postgresql.org     1114                 :           9229 :     att->attrelid = 0;           /* dummy value */
                               1115                 :                : 
                               1116                 :                :     /* unlike TupleDescInitEntry, we require an attribute name */
                               1117         [ -  + ]:           9229 :     Assert(attributeName != NULL);
                               1118                 :           9229 :     namestrcpy(&(att->attname), attributeName);
                               1119                 :                : 
                               1120                 :           9229 :     att->atttypmod = typmod;
                               1121                 :                : 
                               1122                 :           9229 :     att->attnum = attributeNumber;
                               1123                 :           9229 :     att->attndims = attdim;
                               1124                 :                : 
                               1125                 :           9229 :     att->attnotnull = false;
                               1126                 :           9229 :     att->atthasdef = false;
 3167 andrew@dunslane.net      1127                 :           9229 :     att->atthasmissing = false;
 3523 peter_e@gmx.net          1128                 :           9229 :     att->attidentity = '\0';
 2800 peter@eisentraut.org     1129                 :           9229 :     att->attgenerated = '\0';
 3595 rhaas@postgresql.org     1130                 :           9229 :     att->attisdropped = false;
                               1131                 :           9229 :     att->attislocal = true;
                               1132                 :           9229 :     att->attinhcount = 0;
   19 andres@anarazel.de       1133                 :GNC        9229 :     att->attversion = InvalidXLogRecPtr;
                               1134                 :                :     /* variable-length fields are not present in tupledescs */
                               1135                 :                : 
 3595 rhaas@postgresql.org     1136                 :CBC        9229 :     att->atttypid = oidtypeid;
                               1137                 :                : 
                               1138                 :                :     /*
                               1139                 :                :      * Our goal here is to support just enough types to let basic builtin
                               1140                 :                :      * commands work without catalog access - e.g. so that we can do certain
                               1141                 :                :      * things even in processes that are not connected to a database.
                               1142                 :                :      */
                               1143   [ +  -  -  +  :           9229 :     switch (oidtypeid)
                                              +  - ]
                               1144                 :                :     {
                               1145                 :           7502 :         case TEXTOID:
                               1146                 :                :         case TEXTARRAYOID:
                               1147                 :           7502 :             att->attlen = -1;
                               1148                 :           7502 :             att->attbyval = false;
 2460 tgl@sss.pgh.pa.us        1149                 :           7502 :             att->attalign = TYPALIGN_INT;
                               1150                 :           7502 :             att->attstorage = TYPSTORAGE_EXTENDED;
 2011                          1151                 :           7502 :             att->attcompression = InvalidCompressionMethod;
 3595 rhaas@postgresql.org     1152                 :           7502 :             att->attcollation = DEFAULT_COLLATION_OID;
                               1153                 :           7502 :             break;
                               1154                 :                : 
 3595 rhaas@postgresql.org     1155                 :UBC           0 :         case BOOLOID:
                               1156                 :              0 :             att->attlen = 1;
                               1157                 :              0 :             att->attbyval = true;
 2460 tgl@sss.pgh.pa.us        1158                 :              0 :             att->attalign = TYPALIGN_CHAR;
                               1159                 :              0 :             att->attstorage = TYPSTORAGE_PLAIN;
 2015                          1160                 :              0 :             att->attcompression = InvalidCompressionMethod;
 3595 rhaas@postgresql.org     1161                 :              0 :             att->attcollation = InvalidOid;
                               1162                 :              0 :             break;
                               1163                 :                : 
                               1164                 :              0 :         case INT4OID:
                               1165                 :              0 :             att->attlen = 4;
                               1166                 :              0 :             att->attbyval = true;
 2460 tgl@sss.pgh.pa.us        1167                 :              0 :             att->attalign = TYPALIGN_INT;
                               1168                 :              0 :             att->attstorage = TYPSTORAGE_PLAIN;
 2015                          1169                 :              0 :             att->attcompression = InvalidCompressionMethod;
 3595 rhaas@postgresql.org     1170                 :              0 :             att->attcollation = InvalidOid;
 3587                          1171                 :              0 :             break;
                               1172                 :                : 
 3587 rhaas@postgresql.org     1173                 :CBC        1528 :         case INT8OID:
                               1174                 :           1528 :             att->attlen = 8;
  472 tgl@sss.pgh.pa.us        1175                 :           1528 :             att->attbyval = true;
 2460                          1176                 :           1528 :             att->attalign = TYPALIGN_DOUBLE;
                               1177                 :           1528 :             att->attstorage = TYPSTORAGE_PLAIN;
 2015                          1178                 :           1528 :             att->attcompression = InvalidCompressionMethod;
 3587 rhaas@postgresql.org     1179                 :           1528 :             att->attcollation = InvalidOid;
 3595                          1180                 :           1528 :             break;
                               1181                 :                : 
 1608 peter@eisentraut.org     1182                 :            199 :         case OIDOID:
                               1183                 :            199 :             att->attlen = 4;
                               1184                 :            199 :             att->attbyval = true;
                               1185                 :            199 :             att->attalign = TYPALIGN_INT;
                               1186                 :            199 :             att->attstorage = TYPSTORAGE_PLAIN;
                               1187                 :            199 :             att->attcompression = InvalidCompressionMethod;
                               1188                 :            199 :             att->attcollation = InvalidOid;
                               1189                 :            199 :             break;
                               1190                 :                : 
 2910 tgl@sss.pgh.pa.us        1191                 :UBC           0 :         default:
                               1192         [ #  # ]:              0 :             elog(ERROR, "unsupported type %u", oidtypeid);
                               1193                 :                :     }
                               1194                 :                : 
  708 drowley@postgresql.o     1195                 :CBC        9229 :     populate_compact_attribute(desc, attributeNumber - 1);
 3595 rhaas@postgresql.org     1196                 :           9229 : }
                               1197                 :                : 
                               1198                 :                : /*
                               1199                 :                :  * TupleDescInitEntryCollation
                               1200                 :                :  *
                               1201                 :                :  * Assign a nondefault collation to a previously initialized tuple descriptor
                               1202                 :                :  * entry.
                               1203                 :                :  */
                               1204                 :                : void
 5772 peter_e@gmx.net          1205                 :        4261788 : TupleDescInitEntryCollation(TupleDesc desc,
                               1206                 :                :                             AttrNumber attributeNumber,
                               1207                 :                :                             Oid collationid)
                               1208                 :                : {
                               1209                 :                :     /*
                               1210                 :                :      * sanity checks
                               1211                 :                :      */
  430 peter@eisentraut.org     1212         [ -  + ]:        4261788 :     Assert(desc);
 1492                          1213         [ -  + ]:        4261788 :     Assert(attributeNumber >= 1);
                               1214         [ -  + ]:        4261788 :     Assert(attributeNumber <= desc->natts);
                               1215                 :                : 
 3387 andres@anarazel.de       1216                 :        4261788 :     TupleDescAttr(desc, attributeNumber - 1)->attcollation = collationid;
 5772 peter_e@gmx.net          1217                 :        4261788 : }
                               1218                 :                : 
                               1219                 :                : /*
                               1220                 :                :  * BuildDescFromLists
                               1221                 :                :  *
                               1222                 :                :  * Build a TupleDesc given lists of column names (as String nodes),
                               1223                 :                :  * column type OIDs, typmods, and collation OIDs.
                               1224                 :                :  *
                               1225                 :                :  * No constraints are generated.
                               1226                 :                :  *
                               1227                 :                :  * This is for use with functions returning RECORD.
                               1228                 :                :  */
                               1229                 :                : TupleDesc
 1159 peter@eisentraut.org     1230                 :           1097 : BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)
                               1231                 :                : {
                               1232                 :                :     int         natts;
                               1233                 :                :     AttrNumber  attnum;
                               1234                 :                :     ListCell   *l1;
                               1235                 :                :     ListCell   *l2;
                               1236                 :                :     ListCell   *l3;
                               1237                 :                :     ListCell   *l4;
                               1238                 :                :     TupleDesc   desc;
                               1239                 :                : 
 7562 tgl@sss.pgh.pa.us        1240                 :           1097 :     natts = list_length(names);
                               1241         [ -  + ]:           1097 :     Assert(natts == list_length(types));
                               1242         [ -  + ]:           1097 :     Assert(natts == list_length(typmods));
 5772 peter_e@gmx.net          1243         [ -  + ]:           1097 :     Assert(natts == list_length(collations));
                               1244                 :                : 
                               1245                 :                :     /*
                               1246                 :                :      * allocate a new tuple descriptor
                               1247                 :                :      */
 2930 andres@anarazel.de       1248                 :           1097 :     desc = CreateTemplateTupleDesc(natts);
                               1249                 :                : 
 7562 tgl@sss.pgh.pa.us        1250                 :           1097 :     attnum = 0;
 2830                          1251   [ +  -  +  +  :           3874 :     forfour(l1, names, l2, types, l3, typmods, l4, collations)
                                     +  -  +  +  +  
                                     -  +  +  +  -  
                                     +  +  +  +  +  
                                     -  +  -  +  -  
                                              +  + ]
                               1252                 :                :     {
 7562                          1253                 :           2777 :         char       *attname = strVal(lfirst(l1));
 2830                          1254                 :           2777 :         Oid         atttypid = lfirst_oid(l2);
                               1255                 :           2777 :         int32       atttypmod = lfirst_int(l3);
                               1256                 :           2777 :         Oid         attcollation = lfirst_oid(l4);
                               1257                 :                : 
 7562                          1258                 :           2777 :         attnum++;
                               1259                 :                : 
                               1260                 :           2777 :         TupleDescInitEntry(desc, attnum, attname, atttypid, atttypmod, 0);
 5772 peter_e@gmx.net          1261                 :           2777 :         TupleDescInitEntryCollation(desc, attnum, attcollation);
                               1262                 :                :     }
                               1263                 :                : 
  257 drowley@postgresql.o     1264                 :           1097 :     TupleDescFinalize(desc);
                               1265                 :                : 
 7562 tgl@sss.pgh.pa.us        1266                 :           1097 :     return desc;
                               1267                 :                : }
                               1268                 :                : 
                               1269                 :                : /*
                               1270                 :                :  * Get default expression (or NULL if none) for the given attribute number.
                               1271                 :                :  */
                               1272                 :                : Node *
 1158 peter@eisentraut.org     1273                 :          77953 : TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
                               1274                 :                : {
                               1275                 :          77953 :     Node       *result = NULL;
                               1276                 :                : 
                               1277         [ +  - ]:          77953 :     if (tupdesc->constr)
                               1278                 :                :     {
                               1279                 :          77953 :         AttrDefault *attrdef = tupdesc->constr->defval;
                               1280                 :                : 
                               1281         [ +  - ]:         118097 :         for (int i = 0; i < tupdesc->constr->num_defval; i++)
                               1282                 :                :         {
                               1283         [ +  + ]:         118097 :             if (attrdef[i].adnum == attnum)
                               1284                 :                :             {
                               1285                 :          77953 :                 result = stringToNode(attrdef[i].adbin);
                               1286                 :          77953 :                 break;
                               1287                 :                :             }
                               1288                 :                :         }
                               1289                 :                :     }
                               1290                 :                : 
                               1291                 :          77953 :     return result;
                               1292                 :                : }
                               1293                 :                : 
                               1294                 :                : /* ResourceOwner callbacks */
                               1295                 :                : 
                               1296                 :                : static void
 1116 heikki.linnakangas@i     1297                 :          12493 : ResOwnerReleaseTupleDesc(Datum res)
                               1298                 :                : {
                               1299                 :          12493 :     TupleDesc   tupdesc = (TupleDesc) DatumGetPointer(res);
                               1300                 :                : 
                               1301                 :                :     /* Like DecrTupleDescRefCount, but don't call ResourceOwnerForget() */
                               1302         [ -  + ]:          12493 :     Assert(tupdesc->tdrefcount > 0);
                               1303         [ +  + ]:          12493 :     if (--tupdesc->tdrefcount == 0)
                               1304                 :            614 :         FreeTupleDesc(tupdesc);
                               1305                 :          12493 : }
                               1306                 :                : 
                               1307                 :                : static char *
 1116 heikki.linnakangas@i     1308                 :UBC           0 : ResOwnerPrintTupleDesc(Datum res)
                               1309                 :                : {
                               1310                 :              0 :     TupleDesc   tupdesc = (TupleDesc) DatumGetPointer(res);
                               1311                 :                : 
                               1312                 :              0 :     return psprintf("TupleDesc %p (%u,%d)",
                               1313                 :                :                     tupdesc, tupdesc->tdtypeid, tupdesc->tdtypmod);
                               1314                 :                : }
        

Generated by: LCOV version 2.0-1