LCOV - code coverage report
Current view: top level - src/backend/access/common - tupdesc.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 91.3 % 493 450
Test Date: 2026-11-15 12:16:13 Functions: 93.3 % 30 28
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 80.7 % 274 221

             Branch data     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
      49                 :    14133858 : ResourceOwnerRememberTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
      50                 :             : {
      51                 :    14133858 :     ResourceOwnerRemember(owner, PointerGetDatum(tupdesc), &tupdesc_resowner_desc);
      52                 :    14133858 : }
      53                 :             : 
      54                 :             : static inline void
      55                 :    14121357 : ResourceOwnerForgetTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
      56                 :             : {
      57                 :    14121357 :     ResourceOwnerForget(owner, PointerGetDatum(tupdesc), &tupdesc_resowner_desc);
      58                 :    14121357 : }
      59                 :             : 
      60                 :             : /*
      61                 :             :  * populate_compact_attribute_internal
      62                 :             :  *      Helper function for populate_compact_attribute()
      63                 :             :  */
      64                 :             : static inline void
      65                 :    45765673 : populate_compact_attribute_internal(Form_pg_attribute src,
      66                 :             :                                     CompactAttribute *dst)
      67                 :             : {
      68                 :    45765673 :     memset(dst, 0, sizeof(CompactAttribute));
      69                 :             : 
      70                 :    45765673 :     dst->attcacheoff = -1;
      71                 :    45765673 :     dst->attlen = src->attlen;
      72                 :             : 
      73                 :    45765673 :     dst->attbyval = src->attbyval;
      74                 :    45765673 :     dst->attispackable = (src->attstorage != TYPSTORAGE_PLAIN);
      75                 :    45765673 :     dst->atthasmissing = src->atthasmissing;
      76                 :    45765673 :     dst->attisdropped = src->attisdropped;
      77                 :    45765673 :     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                 :             :      */
      85   [ +  +  +  + ]:    71252817 :     dst->attnullability = !src->attnotnull ? ATTNULLABLE_UNRESTRICTED :
      86                 :    25487144 :         IsCatalogRelationOid(src->attrelid) ? ATTNULLABLE_VALID :
      87                 :             :         ATTNULLABLE_UNKNOWN;
      88                 :             : 
      89                 :             :     /* Compute numeric alignment requirement, too */
      90                 :    45765673 :     dst->attalignby = typalign_to_alignby(src->attalign);
      91                 :    45765673 : }
      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
     100                 :    45765673 : populate_compact_attribute(TupleDesc tupdesc, int attnum)
     101                 :             : {
     102                 :    45765673 :     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                 :    45765673 :     dst = &tupdesc->compact_attrs[attnum];
     110                 :             : 
     111                 :    45765673 :     populate_compact_attribute_internal(src, dst);
     112                 :    45765673 : }
     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                 :           0 : verify_compact_attribute(TupleDesc tupdesc, int attnum)
     126                 :             : {
     127                 :             : #ifdef USE_ASSERT_CHECKING
     128                 :             :     CompactAttribute cattr;
     129                 :             :     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                 :             :      */
     137                 :             :     memcpy(&cattr, &tupdesc->compact_attrs[attnum], sizeof(CompactAttribute));
     138                 :             : 
     139                 :             :     /*
     140                 :             :      * Populate the temporary CompactAttribute from the corresponding
     141                 :             :      * Form_pg_attribute
     142                 :             :      */
     143                 :             :     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                 :             :      */
     149                 :             :     tmp.attcacheoff = cattr.attcacheoff;
     150                 :             :     tmp.attnullability = cattr.attnullability;
     151                 :             : 
     152                 :             :     /* Check the freshly populated CompactAttribute matches the TupleDesc's */
     153                 :             :     Assert(memcmp(&tmp, &cattr, sizeof(CompactAttribute)) == 0);
     154                 :             : #endif
     155                 :           0 : }
     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
     165                 :     6690100 : CreateTemplateTupleDesc(int natts)
     166                 :             : {
     167                 :             :     TupleDesc   desc;
     168                 :             :     size_t      sz;
     169                 :             : 
     170                 :             :     /*
     171                 :             :      * sanity checks
     172                 :             :      */
     173                 :             :     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                 :             :      */
     188                 :     6690100 :     sz = offsetof(struct TupleDescData, compact_attrs) +
     189                 :     6690100 :         natts * sizeof(CompactAttribute) +
     190                 :             :         natts * sizeof(FormData_pg_attribute);
     191                 :             : 
     192                 :     6690100 :     desc = (TupleDesc) palloc(sz);
     193                 :             : 
     194                 :             :     /*
     195                 :             :      * Initialize other fields of the tupdesc.
     196                 :             :      */
     197                 :     6690100 :     desc->natts = natts;
     198                 :     6690100 :     desc->constr = NULL;
     199                 :     6690100 :     desc->tdtypeid = RECORDOID;
     200                 :     6690100 :     desc->tdtypmod = -1;
     201                 :     6690100 :     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                 :             :      */
     208                 :     6690100 :     TupleDescSetVersion(desc, InvalidXLogRecPtr);
     209                 :             : 
     210                 :             :     /* This will be set to the correct value by TupleDescFinalize() */
     211                 :     6690100 :     desc->firstNonCachedOffsetAttr = -1;
     212                 :     6690100 :     desc->firstNonGuaranteedAttr = -1;
     213                 :             : 
     214                 :     6690100 :     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
     226                 :      702106 : CreateTupleDesc(int natts, Form_pg_attribute *attrs)
     227                 :             : {
     228                 :             :     TupleDesc   desc;
     229                 :             :     int         i;
     230                 :             : 
     231                 :      702106 :     desc = CreateTemplateTupleDesc(natts);
     232                 :             : 
     233         [ +  + ]:    10737502 :     for (i = 0; i < natts; ++i)
     234                 :             :     {
     235                 :    10035396 :         memcpy(TupleDescAttr(desc, i), attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
     236                 :    10035396 :         populate_compact_attribute(desc, i);
     237                 :             :     }
     238                 :             : 
     239                 :      702106 :     TupleDescFinalize(desc);
     240                 :             : 
     241                 :      702106 :     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
     252                 :      265232 : CreateTupleDescCopy(TupleDesc tupdesc)
     253                 :             : {
     254                 :             :     TupleDesc   desc;
     255                 :             :     int         i;
     256                 :             : 
     257                 :      265232 :     desc = CreateTemplateTupleDesc(tupdesc->natts);
     258                 :             : 
     259                 :             :     /* Flat-copy the attribute array (unless there are no attributes) */
     260         [ +  + ]:      265232 :     if (desc->natts > 0)
     261                 :      262957 :         memcpy(TupleDescAttr(desc, 0),
     262                 :      262957 :                TupleDescAttr(tupdesc, 0),
     263                 :      262957 :                desc->natts * sizeof(FormData_pg_attribute));
     264                 :             : 
     265                 :             :     /*
     266                 :             :      * Since we're not copying constraints and defaults, clear fields
     267                 :             :      * associated with them.
     268                 :             :      */
     269         [ +  + ]:     1338438 :     for (i = 0; i < desc->natts; i++)
     270                 :             :     {
     271                 :     1073206 :         Form_pg_attribute att = TupleDescAttr(desc, i);
     272                 :             : 
     273                 :     1073206 :         att->attnotnull = false;
     274                 :     1073206 :         att->atthasdef = false;
     275                 :     1073206 :         att->atthasmissing = false;
     276                 :     1073206 :         att->attidentity = '\0';
     277                 :     1073206 :         att->attgenerated = '\0';
     278                 :             : 
     279                 :     1073206 :         populate_compact_attribute(desc, i);
     280                 :             :     }
     281                 :             : 
     282                 :             :     /* We can copy the tuple type identification, too */
     283                 :      265232 :     desc->tdtypeid = tupdesc->tdtypeid;
     284                 :      265232 :     desc->tdtypmod = tupdesc->tdtypmod;
     285                 :             : 
     286                 :      265232 :     TupleDescFinalize(desc);
     287                 :             : 
     288                 :      265232 :     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                 :       22561 : CreateTupleDescTruncatedCopy(TupleDesc tupdesc, int natts)
     300                 :             : {
     301                 :             :     TupleDesc   desc;
     302                 :             :     int         i;
     303                 :             : 
     304                 :             :     Assert(natts <= tupdesc->natts);
     305                 :             : 
     306                 :       22561 :     desc = CreateTemplateTupleDesc(natts);
     307                 :             : 
     308                 :             :     /* Flat-copy the attribute array (unless there are no attributes) */
     309         [ +  - ]:       22561 :     if (desc->natts > 0)
     310                 :       22561 :         memcpy(TupleDescAttr(desc, 0),
     311                 :       22561 :                TupleDescAttr(tupdesc, 0),
     312                 :       22561 :                desc->natts * sizeof(FormData_pg_attribute));
     313                 :             : 
     314                 :             :     /*
     315                 :             :      * Since we're not copying constraints and defaults, clear fields
     316                 :             :      * associated with them.
     317                 :             :      */
     318         [ +  + ]:       52789 :     for (i = 0; i < desc->natts; i++)
     319                 :             :     {
     320                 :       30228 :         Form_pg_attribute att = TupleDescAttr(desc, i);
     321                 :             : 
     322                 :       30228 :         att->attnotnull = false;
     323                 :       30228 :         att->atthasdef = false;
     324                 :       30228 :         att->atthasmissing = false;
     325                 :       30228 :         att->attidentity = '\0';
     326                 :       30228 :         att->attgenerated = '\0';
     327                 :             : 
     328                 :       30228 :         populate_compact_attribute(desc, i);
     329                 :             :     }
     330                 :             : 
     331                 :             :     /* We can copy the tuple type identification, too */
     332                 :       22561 :     desc->tdtypeid = tupdesc->tdtypeid;
     333                 :       22561 :     desc->tdtypmod = tupdesc->tdtypmod;
     334                 :             : 
     335                 :       22561 :     TupleDescFinalize(desc);
     336                 :             : 
     337                 :       22561 :     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
     346                 :      559365 : CreateTupleDescCopyConstr(TupleDesc tupdesc)
     347                 :             : {
     348                 :             :     TupleDesc   desc;
     349                 :      559365 :     TupleConstr *constr = tupdesc->constr;
     350                 :             :     int         i;
     351                 :             : 
     352                 :      559365 :     desc = CreateTemplateTupleDesc(tupdesc->natts);
     353                 :             : 
     354                 :             :     /* Flat-copy the attribute array (unless there are no attributes) */
     355         [ +  + ]:      559365 :     if (desc->natts > 0)
     356                 :      559294 :         memcpy(TupleDescAttr(desc, 0),
     357                 :      559294 :                TupleDescAttr(tupdesc, 0),
     358                 :      559294 :                desc->natts * sizeof(FormData_pg_attribute));
     359                 :             : 
     360         [ +  + ]:     8273492 :     for (i = 0; i < desc->natts; i++)
     361                 :             :     {
     362                 :     7714127 :         populate_compact_attribute(desc, i);
     363                 :             : 
     364                 :     7714127 :         TupleDescCompactAttr(desc, i)->attnullability =
     365                 :    15428254 :             TupleDescCompactAttr(tupdesc, i)->attnullability;
     366                 :             :     }
     367                 :             : 
     368                 :             :     /* Copy the TupleConstr data structure, if any */
     369         [ +  + ]:      559365 :     if (constr)
     370                 :             :     {
     371                 :      501650 :         TupleConstr *cpy = palloc0_object(TupleConstr);
     372                 :             : 
     373                 :      501650 :         cpy->has_not_null = constr->has_not_null;
     374                 :      501650 :         cpy->has_generated_stored = constr->has_generated_stored;
     375                 :      501650 :         cpy->has_generated_virtual = constr->has_generated_virtual;
     376                 :             : 
     377         [ +  + ]:      501650 :         if ((cpy->num_defval = constr->num_defval) > 0)
     378                 :             :         {
     379                 :        2786 :             cpy->defval = palloc_array(AttrDefault, cpy->num_defval);
     380                 :        2786 :             memcpy(cpy->defval, constr->defval, cpy->num_defval * sizeof(AttrDefault));
     381         [ +  + ]:        6675 :             for (i = cpy->num_defval - 1; i >= 0; i--)
     382                 :        3889 :                 cpy->defval[i].adbin = pstrdup(constr->defval[i].adbin);
     383                 :             :         }
     384                 :             : 
     385         [ +  + ]:      501650 :         if (constr->missing)
     386                 :             :         {
     387                 :         428 :             cpy->missing = palloc_array(AttrMissing, tupdesc->natts);
     388                 :         428 :             memcpy(cpy->missing, constr->missing, tupdesc->natts * sizeof(AttrMissing));
     389         [ +  + ]:        3271 :             for (i = tupdesc->natts - 1; i >= 0; i--)
     390                 :             :             {
     391         [ +  + ]:        2843 :                 if (constr->missing[i].am_present)
     392                 :             :                 {
     393                 :         764 :                     CompactAttribute *attr = TupleDescCompactAttr(tupdesc, i);
     394                 :             : 
     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                 :             : 
     402         [ +  + ]:      501650 :         if ((cpy->num_check = constr->num_check) > 0)
     403                 :             :         {
     404                 :        1771 :             cpy->check = palloc_array(ConstrCheck, cpy->num_check);
     405                 :        1771 :             memcpy(cpy->check, constr->check, cpy->num_check * sizeof(ConstrCheck));
     406         [ +  + ]:        5215 :             for (i = cpy->num_check - 1; i >= 0; i--)
     407                 :             :             {
     408                 :        3444 :                 cpy->check[i].ccname = pstrdup(constr->check[i].ccname);
     409                 :        3444 :                 cpy->check[i].ccbin = pstrdup(constr->check[i].ccbin);
     410                 :        3444 :                 cpy->check[i].ccenforced = constr->check[i].ccenforced;
     411                 :        3444 :                 cpy->check[i].ccvalid = constr->check[i].ccvalid;
     412                 :        3444 :                 cpy->check[i].ccnoinherit = constr->check[i].ccnoinherit;
     413                 :             :             }
     414                 :             :         }
     415                 :             : 
     416                 :      501650 :         desc->constr = cpy;
     417                 :             :     }
     418                 :             : 
     419                 :             :     /* We can copy the tuple type identification, too */
     420                 :      559365 :     desc->tdtypeid = tupdesc->tdtypeid;
     421                 :      559365 :     desc->tdtypmod = tupdesc->tdtypmod;
     422                 :             : 
     423                 :      559365 :     TupleDescFinalize(desc);
     424                 :             : 
     425                 :      559365 :     return desc;
     426                 :             : }
     427                 :             : 
     428                 :             : /*
     429                 :             :  * TupleDescCopySize
     430                 :             :  *      Return the number of bytes TupleDescCopySized() needs to copy src.
     431                 :             :  */
     432                 :             : Size
     433                 :         570 : TupleDescCopySize(TupleDesc src)
     434                 :             : {
     435                 :             :     return offsetof(struct TupleDescData, compact_attrs) +
     436                 :         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)
     454         [ #  # ]:           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 */
     458                 :         285 :     memcpy(dst, src, sz);
     459                 :             : 
     460                 :             :     /*
     461                 :             :      * Since we're not copying constraints and defaults, clear fields
     462                 :             :      * associated with them.
     463                 :             :      */
     464         [ +  + ]:        1048 :     for (int i = 0; i < dst->natts; i++)
     465                 :             :     {
     466                 :         763 :         Form_pg_attribute att = TupleDescAttr(dst, i);
     467                 :             : 
     468                 :         763 :         att->attnotnull = false;
     469                 :         763 :         att->atthasdef = false;
     470                 :         763 :         att->atthasmissing = false;
     471                 :         763 :         att->attidentity = '\0';
     472                 :         763 :         att->attgenerated = '\0';
     473                 :             : 
     474                 :         763 :         populate_compact_attribute(dst, i);
     475                 :             :     }
     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                 :             : 
     484                 :         285 :     TupleDescFinalize(dst);
     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
     498                 :        2717 : TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
     499                 :             :                    TupleDesc src, AttrNumber srcAttno)
     500                 :             : {
     501                 :        2717 :     Form_pg_attribute dstAtt = TupleDescAttr(dst, dstAttno - 1);
     502                 :        2717 :     Form_pg_attribute srcAtt = TupleDescAttr(src, srcAttno - 1);
     503                 :             : 
     504                 :             :     /*
     505                 :             :      * sanity checks
     506                 :             :      */
     507                 :             :     Assert(src);
     508                 :             :     Assert(dst);
     509                 :             :     Assert(srcAttno >= 1);
     510                 :             :     Assert(srcAttno <= src->natts);
     511                 :             :     Assert(dstAttno >= 1);
     512                 :             :     Assert(dstAttno <= dst->natts);
     513                 :             : 
     514                 :        2717 :     memcpy(dstAtt, srcAtt, ATTRIBUTE_FIXED_PART_SIZE);
     515                 :             : 
     516                 :        2717 :     dstAtt->attnum = dstAttno;
     517                 :             : 
     518                 :             :     /* since we're not copying constraints or defaults, clear these */
     519                 :        2717 :     dstAtt->attnotnull = false;
     520                 :        2717 :     dstAtt->atthasdef = false;
     521                 :        2717 :     dstAtt->atthasmissing = false;
     522                 :        2717 :     dstAtt->attidentity = '\0';
     523                 :        2717 :     dstAtt->attgenerated = '\0';
     524                 :             : 
     525                 :        2717 :     populate_compact_attribute(dst, dstAttno - 1);
     526                 :        2717 : }
     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
     537                 :     6825739 : TupleDescFinalize(TupleDesc tupdesc)
     538                 :             : {
     539                 :     6825739 :     int         firstNonCachedOffsetAttr = 0;
     540                 :     6825739 :     int         firstNonGuaranteedAttr = tupdesc->natts;
     541                 :     6825739 :     int         off = 0;
     542                 :             : 
     543         [ +  + ]:    38938832 :     for (int i = 0; i < tupdesc->natts; i++)
     544                 :             :     {
     545                 :    35591732 :         CompactAttribute *cattr = TupleDescCompactAttr(tupdesc, i);
     546                 :    35591732 :         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                 :             :          */
     553         [ +  + ]:    35591732 :         if (firstNonGuaranteedAttr == tupdesc->natts &&
     554   [ +  +  +  +  :    14486539 :             (cattr->attnullability != ATTNULLABLE_VALID || !cattr->attbyval ||
                   +  + ]
     555         [ +  - ]:     8216342 :              cattr->atthasmissing || cattr->attisdropped ||
     556         [ +  - ]:     8216342 :              cattr->attlen <= 0 ||
     557         [ +  + ]:     8216342 :              attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL))
     558                 :     6270579 :             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                 :             :          */
     565         [ +  + ]:    35591732 :         if (cattr->attlen <= 0 ||
     566         [ +  + ]:    32118847 :             attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
     567                 :             :             break;
     568                 :             : 
     569                 :    32113093 :         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                 :             :          */
     578         [ -  + ]:    32113093 :         if (off > PG_INT16_MAX)
     579                 :           0 :             break;
     580                 :             : 
     581                 :    32113093 :         cattr->attcacheoff = (int16) off;
     582                 :             : 
     583                 :    32113093 :         off += cattr->attlen;
     584                 :    32113093 :         firstNonCachedOffsetAttr = i + 1;
     585                 :             :     }
     586                 :             : 
     587                 :     6825739 :     tupdesc->firstNonCachedOffsetAttr = firstNonCachedOffsetAttr;
     588                 :     6825739 :     tupdesc->firstNonGuaranteedAttr = firstNonGuaranteedAttr;
     589                 :             : 
     590                 :             :     /* set the aggregate version of the tupledesc, as appropriate */
     591   [ +  +  +  + ]:     6825739 :     if (tupdesc->natts == 0 || tupdesc->tdtypeid == RECORDOID)
     592                 :     4834280 :         TupleDescSetVersion(tupdesc, InvalidXLogRecPtr);
     593                 :             :     else
     594                 :     1991459 :         TupleDescSetVersion(tupdesc,
     595                 :     1991459 :                             TupleDescGetAttrVersion(tupdesc, tupdesc->natts));
     596                 :     6825739 : }
     597                 :             : 
     598                 :             : /*
     599                 :             :  * Free a TupleDesc including all substructure
     600                 :             :  */
     601                 :             : void
     602                 :      962631 : 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                 :             :      */
     610                 :             :     Assert(tupdesc->tdrefcount <= 0);
     611                 :             : 
     612         [ +  + ]:      962631 :     if (tupdesc->constr)
     613                 :             :     {
     614         [ +  + ]:      284836 :         if (tupdesc->constr->num_defval > 0)
     615                 :             :         {
     616                 :       22064 :             AttrDefault *attrdef = tupdesc->constr->defval;
     617                 :             : 
     618         [ +  + ]:       53298 :             for (i = tupdesc->constr->num_defval - 1; i >= 0; i--)
     619                 :       31234 :                 pfree(attrdef[i].adbin);
     620                 :       22064 :             pfree(attrdef);
     621                 :             :         }
     622         [ +  + ]:      284836 :         if (tupdesc->constr->missing)
     623                 :             :         {
     624                 :        2617 :             AttrMissing *attrmiss = tupdesc->constr->missing;
     625                 :             : 
     626         [ +  + ]:       18295 :             for (i = tupdesc->natts - 1; i >= 0; i--)
     627                 :             :             {
     628         [ +  + ]:       15678 :                 if (attrmiss[i].am_present
     629         [ +  + ]:        5418 :                     && !TupleDescAttr(tupdesc, i)->attbyval)
     630                 :        1984 :                     pfree(DatumGetPointer(attrmiss[i].am_value));
     631                 :             :             }
     632                 :        2617 :             pfree(attrmiss);
     633                 :             :         }
     634         [ +  + ]:      284836 :         if (tupdesc->constr->num_check > 0)
     635                 :             :         {
     636                 :        7814 :             ConstrCheck *check = tupdesc->constr->check;
     637                 :             : 
     638         [ +  + ]:       21434 :             for (i = tupdesc->constr->num_check - 1; i >= 0; i--)
     639                 :             :             {
     640                 :       13620 :                 pfree(check[i].ccname);
     641                 :       13620 :                 pfree(check[i].ccbin);
     642                 :             :             }
     643                 :        7814 :             pfree(check);
     644                 :             :         }
     645                 :      284836 :         pfree(tupdesc->constr);
     646                 :             :     }
     647                 :             : 
     648                 :      962631 :     pfree(tupdesc);
     649                 :      962631 : }
     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
     659                 :    14133858 : IncrTupleDescRefCount(TupleDesc tupdesc)
     660                 :             : {
     661                 :             :     Assert(tupdesc->tdrefcount >= 0);
     662                 :             : 
     663                 :    14133858 :     ResourceOwnerEnlarge(CurrentResourceOwner);
     664                 :    14133858 :     tupdesc->tdrefcount++;
     665                 :    14133858 :     ResourceOwnerRememberTupleDesc(CurrentResourceOwner, tupdesc);
     666                 :    14133858 : }
     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                 :    14121357 : DecrTupleDescRefCount(TupleDesc tupdesc)
     678                 :             : {
     679                 :             :     Assert(tupdesc->tdrefcount > 0);
     680                 :             : 
     681                 :    14121357 :     ResourceOwnerForgetTupleDesc(CurrentResourceOwner, tupdesc);
     682         [ +  + ]:    14121357 :     if (--tupdesc->tdrefcount == 0)
     683                 :           4 :         FreeTupleDesc(tupdesc);
     684                 :    14121357 : }
     685                 :             : 
     686                 :             : XLogRecPtr
     687                 :     6735704 : 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         [ +  + ]:     6735704 :     if (tupdesc->tdtypeid == RECORDOID)
     695                 :      170740 :         return InvalidXLogRecPtr;
     696                 :     6564964 :     return tupdesc->tdversion;
     697                 :             : }
     698                 :             : 
     699                 :             : void
     700                 :    13817277 : TupleDescSetVersion(TupleDesc tupdesc, XLogRecPtr version)
     701                 :             : {
     702                 :             :     Assert(tupdesc->tdtypeid != RECORDOID ||
     703                 :             :            version == InvalidXLogRecPtr);
     704                 :             : 
     705                 :    13817277 :     tupdesc->tdversion = version;
     706                 :    13817277 : }
     707                 :             : 
     708                 :             : XLogRecPtr
     709                 :     3416245 : TupleDescGetAttrVersion(TupleDesc tupdesc, AttrNumber attnum)
     710                 :             : {
     711                 :             :     Assert(attnum >= 1 && attnum <= tupdesc->natts);
     712                 :             :     /* see TupleDescGetVersion() */
     713         [ -  + ]:     3416245 :     if (tupdesc->tdtypeid == RECORDOID)
     714                 :           0 :         return InvalidXLogRecPtr;
     715                 :     3416245 :     return TupleDescAttr(tupdesc, attnum - 1)->attversion;
     716                 :             : }
     717                 :             : 
     718                 :             : /*
     719                 :             :  * Compare two TupleDesc structures for logical equality
     720                 :             :  */
     721                 :             : bool
     722                 :      283365 : equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
     723                 :             : {
     724                 :             :     int         i,
     725                 :             :                 n;
     726                 :             : 
     727         [ +  + ]:      283365 :     if (tupdesc1->natts != tupdesc2->natts)
     728                 :        4075 :         return false;
     729         [ -  + ]:      279290 :     if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
     730                 :           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                 :             :      */
     737         [ +  + ]:      279290 :     if (TupleDescGetVersion(tupdesc1) != TupleDescGetVersion(tupdesc2))
     738                 :       41074 :         return false;
     739                 :             : 
     740                 :             :     /* tdtypmod and tdrefcount are not checked */
     741                 :             : 
     742         [ +  + ]:     1104494 :     for (i = 0; i < tupdesc1->natts; i++)
     743                 :             :     {
     744                 :      875031 :         Form_pg_attribute attr1 = TupleDescAttr(tupdesc1, i);
     745                 :      875031 :         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                 :             :          */
     759         [ +  + ]:      875031 :         if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
     760                 :         934 :             return false;
     761         [ +  + ]:      874097 :         if (attr1->atttypid != attr2->atttypid)
     762                 :          94 :             return false;
     763         [ +  + ]:      874003 :         if (attr1->attlen != attr2->attlen)
     764                 :           7 :             return false;
     765         [ -  + ]:      873996 :         if (attr1->attversion != attr2->attversion)
     766                 :           0 :             return false;
     767         [ -  + ]:      873996 :         if (attr1->attndims != attr2->attndims)
     768                 :           0 :             return false;
     769         [ -  + ]:      873996 :         if (attr1->atttypmod != attr2->atttypmod)
     770                 :           0 :             return false;
     771         [ +  + ]:      873996 :         if (attr1->attbyval != attr2->attbyval)
     772                 :          39 :             return false;
     773         [ -  + ]:      873957 :         if (attr1->attalign != attr2->attalign)
     774                 :           0 :             return false;
     775         [ +  + ]:      873957 :         if (attr1->attstorage != attr2->attstorage)
     776                 :         227 :             return false;
     777         [ +  + ]:      873730 :         if (attr1->attcompression != attr2->attcompression)
     778                 :          45 :             return false;
     779         [ +  + ]:      873685 :         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                 :             :          */
     787         [ +  + ]:      872475 :         if (attr1->attnotnull)
     788                 :             :         {
     789                 :      324969 :             CompactAttribute *cattr1 = TupleDescCompactAttr(tupdesc1, i);
     790                 :      324969 :             CompactAttribute *cattr2 = TupleDescCompactAttr(tupdesc2, i);
     791                 :             : 
     792                 :             :             Assert(cattr1->attnullability != ATTNULLABLE_UNKNOWN);
     793                 :             :             Assert((cattr1->attnullability == ATTNULLABLE_UNKNOWN) ==
     794                 :             :                    (cattr2->attnullability == ATTNULLABLE_UNKNOWN));
     795                 :             : 
     796         [ +  + ]:      324969 :             if (cattr1->attnullability != cattr2->attnullability)
     797                 :          54 :                 return false;
     798                 :             :         }
     799         [ +  + ]:      872421 :         if (attr1->atthasdef != attr2->atthasdef)
     800                 :        3710 :             return false;
     801         [ +  + ]:      868711 :         if (attr1->attidentity != attr2->attidentity)
     802                 :         125 :             return false;
     803         [ +  + ]:      868586 :         if (attr1->attgenerated != attr2->attgenerated)
     804                 :          37 :             return false;
     805         [ -  + ]:      868549 :         if (attr1->attisdropped != attr2->attisdropped)
     806                 :           0 :             return false;
     807         [ +  + ]:      868549 :         if (attr1->attislocal != attr2->attislocal)
     808                 :        1770 :             return false;
     809         [ +  + ]:      866779 :         if (attr1->attinhcount != attr2->attinhcount)
     810                 :         501 :             return false;
     811         [ -  + ]:      866278 :         if (attr1->attcollation != attr2->attcollation)
     812                 :           0 :             return false;
     813                 :             :         /* variable-length fields are not even present... */
     814                 :             :     }
     815                 :             : 
     816         [ +  + ]:      229463 :     if (tupdesc1->constr != NULL)
     817                 :             :     {
     818                 :       86714 :         TupleConstr *constr1 = tupdesc1->constr;
     819                 :       86714 :         TupleConstr *constr2 = tupdesc2->constr;
     820                 :             : 
     821         [ +  + ]:       86714 :         if (constr2 == NULL)
     822                 :         151 :             return false;
     823         [ -  + ]:       86563 :         if (constr1->has_not_null != constr2->has_not_null)
     824                 :           0 :             return false;
     825         [ +  + ]:       86563 :         if (constr1->has_generated_stored != constr2->has_generated_stored)
     826                 :          29 :             return false;
     827         [ +  + ]:       86534 :         if (constr1->has_generated_virtual != constr2->has_generated_virtual)
     828                 :          23 :             return false;
     829                 :       86511 :         n = constr1->num_defval;
     830         [ -  + ]:       86511 :         if (n != (int) constr2->num_defval)
     831                 :           0 :             return false;
     832                 :             :         /* We assume here that both AttrDefault arrays are in adnum order */
     833         [ +  + ]:       99233 :         for (i = 0; i < n; i++)
     834                 :             :         {
     835                 :       12722 :             AttrDefault *defval1 = constr1->defval + i;
     836                 :       12722 :             AttrDefault *defval2 = constr2->defval + i;
     837                 :             : 
     838         [ -  + ]:       12722 :             if (defval1->adnum != defval2->adnum)
     839                 :           0 :                 return false;
     840         [ -  + ]:       12722 :             if (strcmp(defval1->adbin, defval2->adbin) != 0)
     841                 :           0 :                 return false;
     842                 :             :         }
     843         [ +  + ]:       86511 :         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                 :             : 
     852         [ +  + ]:        2400 :                 if (missval1->am_present != missval2->am_present)
     853                 :         105 :                     return false;
     854         [ +  + ]:        2295 :                 if (missval1->am_present)
     855                 :             :                 {
     856                 :         787 :                     CompactAttribute *missatt1 = TupleDescCompactAttr(tupdesc1, i);
     857                 :             : 
     858         [ -  + ]:         787 :                     if (!datumIsEqual(missval1->am_value, missval2->am_value,
     859                 :         787 :                                       missatt1->attbyval, missatt1->attlen))
     860                 :           0 :                         return false;
     861                 :             :                 }
     862                 :             :             }
     863                 :             :         }
     864         [ +  + ]:       85964 :         else if (constr2->missing)
     865                 :         275 :             return false;
     866                 :       86065 :         n = constr1->num_check;
     867         [ +  + ]:       86065 :         if (n != (int) constr2->num_check)
     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                 :             :          */
     875         [ +  + ]:       87321 :         for (i = 0; i < n; i++)
     876                 :             :         {
     877                 :        2527 :             ConstrCheck *check1 = constr1->check + i;
     878                 :        2527 :             ConstrCheck *check2 = constr2->check + i;
     879                 :             : 
     880         [ +  - ]:        2527 :             if (!(strcmp(check1->ccname, check2->ccname) == 0 &&
     881         [ +  - ]:        2527 :                   strcmp(check1->ccbin, check2->ccbin) == 0 &&
     882         [ +  + ]:        2527 :                   check1->ccenforced == check2->ccenforced &&
     883         [ +  + ]:        2423 :                   check1->ccvalid == check2->ccvalid &&
     884         [ -  + ]:        2363 :                   check1->ccnoinherit == check2->ccnoinherit))
     885                 :         164 :                 return false;
     886                 :             :         }
     887                 :             :     }
     888         [ +  + ]:      142749 :     else if (tupdesc2->constr != NULL)
     889                 :         761 :         return false;
     890                 :      226782 :     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
     923                 :      311836 : equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2)
     924                 :             : {
     925         [ +  + ]:      311836 :     if (tupdesc1->natts != tupdesc2->natts)
     926                 :         114 :         return false;
     927         [ +  + ]:      311722 :     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                 :             :      */
     945   [ +  +  +  + ]:      310900 :     if (tupdesc1->tdtypeid != RECORDOID &&
     946                 :         510 :         TupleDescGetVersion(tupdesc1) != TupleDescGetVersion(tupdesc2))
     947                 :          12 :         return false;
     948                 :             : 
     949         [ +  + ]:     4465470 :     for (int i = 0; i < tupdesc1->natts; i++)
     950                 :             :     {
     951                 :     4161435 :         Form_pg_attribute attr1 = TupleDescAttr(tupdesc1, i);
     952                 :     4161435 :         Form_pg_attribute attr2 = TupleDescAttr(tupdesc2, i);
     953                 :             : 
     954         [ +  + ]:     4161435 :         if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
     955                 :        6325 :             return false;
     956         [ +  + ]:     4155110 :         if (attr1->atttypid != attr2->atttypid)
     957                 :          14 :             return false;
     958         [ +  + ]:     4155096 :         if (attr1->atttypmod != attr2->atttypmod)
     959                 :           4 :             return false;
     960         [ -  + ]:     4155092 :         if (attr1->attcollation != attr2->attcollation)
     961                 :           0 :             return false;
     962                 :             : 
     963                 :             :         /* Record types derived from tables could have dropped fields. */
     964         [ -  + ]:     4155092 :         if (attr1->attisdropped != attr2->attisdropped)
     965                 :           0 :             return false;
     966                 :             : 
     967                 :             :         /*
     968                 :             :          * Dropped fields could have incompatible lengths/alignments (can't
     969                 :             :          * rely on atttypid for this)
     970                 :             :          */
     971         [ +  + ]:     4155092 :         if (attr1->attisdropped)
     972                 :             :         {
     973         [ -  + ]:          31 :             if (attr1->attlen != attr2->attlen)
     974                 :           0 :                 return false;
     975         [ -  + ]:          31 :             if (attr1->attalign != attr2->attalign)
     976                 :           0 :                 return false;
     977                 :             :         }
     978                 :             :     }
     979                 :             : 
     980                 :      304035 :     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                 :      321694 : hashRowType(TupleDesc desc)
     991                 :             : {
     992                 :             :     uint32      s;
     993                 :             :     int         i;
     994                 :             : 
     995                 :      321694 :     s = hash_combine(0, hash_bytes_uint32(desc->natts));
     996                 :      321694 :     s = hash_combine(s, hash_bytes_uint32(desc->tdtypeid));
     997         [ +  + ]:     4659182 :     for (i = 0; i < desc->natts; ++i)
     998                 :     4337488 :         s = hash_combine(s, hash_bytes_uint32(TupleDescAttr(desc, i)->atttypid));
     999                 :             : 
    1000                 :      321694 :     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
    1019                 :     8673275 : 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                 :             :      */
    1033                 :             :     Assert(desc);
    1034                 :             :     Assert(attributeNumber >= 1);
    1035                 :             :     Assert(attributeNumber <= desc->natts);
    1036                 :             :     Assert(attdim >= 0);
    1037                 :             :     Assert(attdim <= PG_INT16_MAX);
    1038                 :             : 
    1039                 :             :     /*
    1040                 :             :      * initialize the attribute fields
    1041                 :             :      */
    1042                 :     8673275 :     att = TupleDescAttr(desc, attributeNumber - 1);
    1043                 :             : 
    1044                 :     8673275 :     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                 :             :      */
    1051         [ +  + ]:     8673275 :     if (attributeName == NULL)
    1052   [ -  +  -  -  :     2732661 :         MemSet(NameStr(att->attname), 0, NAMEDATALEN);
          -  -  -  -  -  
                      - ]
    1053         [ +  + ]:     5940614 :     else if (attributeName != NameStr(att->attname))
    1054                 :     5938435 :         namestrcpy(&(att->attname), attributeName);
    1055                 :             : 
    1056                 :     8673275 :     att->atttypmod = typmod;
    1057                 :             : 
    1058                 :     8673275 :     att->attnum = attributeNumber;
    1059                 :     8673275 :     att->attndims = attdim;
    1060                 :             : 
    1061                 :     8673275 :     att->attnotnull = false;
    1062                 :     8673275 :     att->atthasdef = false;
    1063                 :     8673275 :     att->atthasmissing = false;
    1064                 :     8673275 :     att->attidentity = '\0';
    1065                 :     8673275 :     att->attgenerated = '\0';
    1066                 :     8673275 :     att->attisdropped = false;
    1067                 :     8673275 :     att->attislocal = true;
    1068                 :     8673275 :     att->attinhcount = 0;
    1069                 :     8673275 :     att->attversion = InvalidXLogRecPtr;
    1070                 :             :     /* variable-length fields are not present in tupledescs */
    1071                 :             : 
    1072                 :     8673275 :     tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(oidtypeid));
    1073         [ -  + ]:     8673275 :     if (!HeapTupleIsValid(tuple))
    1074         [ #  # ]:           0 :         elog(ERROR, "cache lookup failed for type %u", oidtypeid);
    1075                 :     8673275 :     typeForm = (Form_pg_type) GETSTRUCT(tuple);
    1076                 :             : 
    1077                 :     8673275 :     att->atttypid = oidtypeid;
    1078                 :     8673275 :     att->attlen = typeForm->typlen;
    1079                 :     8673275 :     att->attbyval = typeForm->typbyval;
    1080                 :     8673275 :     att->attalign = typeForm->typalign;
    1081                 :     8673275 :     att->attstorage = typeForm->typstorage;
    1082                 :     8673275 :     att->attcompression = InvalidCompressionMethod;
    1083                 :     8673275 :     att->attcollation = typeForm->typcollation;
    1084                 :             : 
    1085                 :     8673275 :     populate_compact_attribute(desc, attributeNumber - 1);
    1086                 :             : 
    1087                 :     8673275 :     ReleaseSysCache(tuple);
    1088                 :     8673275 : }
    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
    1096                 :        9224 : 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 */
    1106                 :             :     Assert(desc);
    1107                 :             :     Assert(attributeNumber >= 1);
    1108                 :             :     Assert(attributeNumber <= desc->natts);
    1109                 :             :     Assert(attdim >= 0);
    1110                 :             :     Assert(attdim <= PG_INT16_MAX);
    1111                 :             : 
    1112                 :             :     /* initialize the attribute fields */
    1113                 :        9224 :     att = TupleDescAttr(desc, attributeNumber - 1);
    1114                 :        9224 :     att->attrelid = 0;           /* dummy value */
    1115                 :             : 
    1116                 :             :     /* unlike TupleDescInitEntry, we require an attribute name */
    1117                 :             :     Assert(attributeName != NULL);
    1118                 :        9224 :     namestrcpy(&(att->attname), attributeName);
    1119                 :             : 
    1120                 :        9224 :     att->atttypmod = typmod;
    1121                 :             : 
    1122                 :        9224 :     att->attnum = attributeNumber;
    1123                 :        9224 :     att->attndims = attdim;
    1124                 :             : 
    1125                 :        9224 :     att->attnotnull = false;
    1126                 :        9224 :     att->atthasdef = false;
    1127                 :        9224 :     att->atthasmissing = false;
    1128                 :        9224 :     att->attidentity = '\0';
    1129                 :        9224 :     att->attgenerated = '\0';
    1130                 :        9224 :     att->attisdropped = false;
    1131                 :        9224 :     att->attislocal = true;
    1132                 :        9224 :     att->attinhcount = 0;
    1133                 :        9224 :     att->attversion = InvalidXLogRecPtr;
    1134                 :             :     /* variable-length fields are not present in tupledescs */
    1135                 :             : 
    1136                 :        9224 :     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   [ +  -  -  +  :        9224 :     switch (oidtypeid)
                   +  - ]
    1144                 :             :     {
    1145                 :        7495 :         case TEXTOID:
    1146                 :             :         case TEXTARRAYOID:
    1147                 :        7495 :             att->attlen = -1;
    1148                 :        7495 :             att->attbyval = false;
    1149                 :        7495 :             att->attalign = TYPALIGN_INT;
    1150                 :        7495 :             att->attstorage = TYPSTORAGE_EXTENDED;
    1151                 :        7495 :             att->attcompression = InvalidCompressionMethod;
    1152                 :        7495 :             att->attcollation = DEFAULT_COLLATION_OID;
    1153                 :        7495 :             break;
    1154                 :             : 
    1155                 :           0 :         case BOOLOID:
    1156                 :           0 :             att->attlen = 1;
    1157                 :           0 :             att->attbyval = true;
    1158                 :           0 :             att->attalign = TYPALIGN_CHAR;
    1159                 :           0 :             att->attstorage = TYPSTORAGE_PLAIN;
    1160                 :           0 :             att->attcompression = InvalidCompressionMethod;
    1161                 :           0 :             att->attcollation = InvalidOid;
    1162                 :           0 :             break;
    1163                 :             : 
    1164                 :           0 :         case INT4OID:
    1165                 :           0 :             att->attlen = 4;
    1166                 :           0 :             att->attbyval = true;
    1167                 :           0 :             att->attalign = TYPALIGN_INT;
    1168                 :           0 :             att->attstorage = TYPSTORAGE_PLAIN;
    1169                 :           0 :             att->attcompression = InvalidCompressionMethod;
    1170                 :           0 :             att->attcollation = InvalidOid;
    1171                 :           0 :             break;
    1172                 :             : 
    1173                 :        1532 :         case INT8OID:
    1174                 :        1532 :             att->attlen = 8;
    1175                 :        1532 :             att->attbyval = true;
    1176                 :        1532 :             att->attalign = TYPALIGN_DOUBLE;
    1177                 :        1532 :             att->attstorage = TYPSTORAGE_PLAIN;
    1178                 :        1532 :             att->attcompression = InvalidCompressionMethod;
    1179                 :        1532 :             att->attcollation = InvalidOid;
    1180                 :        1532 :             break;
    1181                 :             : 
    1182                 :         197 :         case OIDOID:
    1183                 :         197 :             att->attlen = 4;
    1184                 :         197 :             att->attbyval = true;
    1185                 :         197 :             att->attalign = TYPALIGN_INT;
    1186                 :         197 :             att->attstorage = TYPSTORAGE_PLAIN;
    1187                 :         197 :             att->attcompression = InvalidCompressionMethod;
    1188                 :         197 :             att->attcollation = InvalidOid;
    1189                 :         197 :             break;
    1190                 :             : 
    1191                 :           0 :         default:
    1192         [ #  # ]:           0 :             elog(ERROR, "unsupported type %u", oidtypeid);
    1193                 :             :     }
    1194                 :             : 
    1195                 :        9224 :     populate_compact_attribute(desc, attributeNumber - 1);
    1196                 :        9224 : }
    1197                 :             : 
    1198                 :             : /*
    1199                 :             :  * TupleDescInitEntryCollation
    1200                 :             :  *
    1201                 :             :  * Assign a nondefault collation to a previously initialized tuple descriptor
    1202                 :             :  * entry.
    1203                 :             :  */
    1204                 :             : void
    1205                 :     4241727 : TupleDescInitEntryCollation(TupleDesc desc,
    1206                 :             :                             AttrNumber attributeNumber,
    1207                 :             :                             Oid collationid)
    1208                 :             : {
    1209                 :             :     /*
    1210                 :             :      * sanity checks
    1211                 :             :      */
    1212                 :             :     Assert(desc);
    1213                 :             :     Assert(attributeNumber >= 1);
    1214                 :             :     Assert(attributeNumber <= desc->natts);
    1215                 :             : 
    1216                 :     4241727 :     TupleDescAttr(desc, attributeNumber - 1)->attcollation = collationid;
    1217                 :     4241727 : }
    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
    1230                 :        1099 : 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                 :             : 
    1240                 :        1099 :     natts = list_length(names);
    1241                 :             :     Assert(natts == list_length(types));
    1242                 :             :     Assert(natts == list_length(typmods));
    1243                 :             :     Assert(natts == list_length(collations));
    1244                 :             : 
    1245                 :             :     /*
    1246                 :             :      * allocate a new tuple descriptor
    1247                 :             :      */
    1248                 :        1099 :     desc = CreateTemplateTupleDesc(natts);
    1249                 :             : 
    1250                 :        1099 :     attnum = 0;
    1251   [ +  -  +  +  :        3878 :     forfour(l1, names, l2, types, l3, typmods, l4, collations)
          +  -  +  +  +  
          -  +  +  +  -  
          +  +  +  +  +  
          -  +  -  +  -  
                   +  + ]
    1252                 :             :     {
    1253                 :        2779 :         char       *attname = strVal(lfirst(l1));
    1254                 :        2779 :         Oid         atttypid = lfirst_oid(l2);
    1255                 :        2779 :         int32       atttypmod = lfirst_int(l3);
    1256                 :        2779 :         Oid         attcollation = lfirst_oid(l4);
    1257                 :             : 
    1258                 :        2779 :         attnum++;
    1259                 :             : 
    1260                 :        2779 :         TupleDescInitEntry(desc, attnum, attname, atttypid, atttypmod, 0);
    1261                 :        2779 :         TupleDescInitEntryCollation(desc, attnum, attcollation);
    1262                 :             :     }
    1263                 :             : 
    1264                 :        1099 :     TupleDescFinalize(desc);
    1265                 :             : 
    1266                 :        1099 :     return desc;
    1267                 :             : }
    1268                 :             : 
    1269                 :             : /*
    1270                 :             :  * Get default expression (or NULL if none) for the given attribute number.
    1271                 :             :  */
    1272                 :             : Node *
    1273                 :       78379 : TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
    1274                 :             : {
    1275                 :       78379 :     Node       *result = NULL;
    1276                 :             : 
    1277         [ +  - ]:       78379 :     if (tupdesc->constr)
    1278                 :             :     {
    1279                 :       78379 :         AttrDefault *attrdef = tupdesc->constr->defval;
    1280                 :             : 
    1281         [ +  - ]:      118528 :         for (int i = 0; i < tupdesc->constr->num_defval; i++)
    1282                 :             :         {
    1283         [ +  + ]:      118528 :             if (attrdef[i].adnum == attnum)
    1284                 :             :             {
    1285                 :       78379 :                 result = stringToNode(attrdef[i].adbin);
    1286                 :       78379 :                 break;
    1287                 :             :             }
    1288                 :             :         }
    1289                 :             :     }
    1290                 :             : 
    1291                 :       78379 :     return result;
    1292                 :             : }
    1293                 :             : 
    1294                 :             : /* ResourceOwner callbacks */
    1295                 :             : 
    1296                 :             : static void
    1297                 :       12501 : ResOwnerReleaseTupleDesc(Datum res)
    1298                 :             : {
    1299                 :       12501 :     TupleDesc   tupdesc = (TupleDesc) DatumGetPointer(res);
    1300                 :             : 
    1301                 :             :     /* Like DecrTupleDescRefCount, but don't call ResourceOwnerForget() */
    1302                 :             :     Assert(tupdesc->tdrefcount > 0);
    1303         [ +  + ]:       12501 :     if (--tupdesc->tdrefcount == 0)
    1304                 :         615 :         FreeTupleDesc(tupdesc);
    1305                 :       12501 : }
    1306                 :             : 
    1307                 :             : static char *
    1308                 :           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