LCOV - differential code coverage report
Current view: top level - src/backend/access/common - attmap.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 96.8 % 94 91 3 91
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 6 6 6
Baseline: lcov-20250906-005545-baseline Branches: 78.6 % 70 55 15 55
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 10 10 10
(360..) days: 96.4 % 84 81 3 81
Function coverage date bins:
(360..) days: 100.0 % 6 6 6
Branch coverage date bins:
(30,360] days: 64.3 % 14 9 5 9
(360..) days: 82.1 % 56 46 10 46

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * attmap.c
                                  4                 :                :  *    Attribute mapping support.
                                  5                 :                :  *
                                  6                 :                :  * This file provides utility routines to build and manage attribute
                                  7                 :                :  * mappings by comparing input and output TupleDescs.  Such mappings
                                  8                 :                :  * are typically used by DDL operating on inheritance and partition trees
                                  9                 :                :  * to do a conversion between rowtypes logically equivalent but with
                                 10                 :                :  * columns in a different order, taking into account dropped columns.
                                 11                 :                :  * They are also used by the tuple conversion routines in tupconvert.c.
                                 12                 :                :  *
                                 13                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                 14                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 15                 :                :  *
                                 16                 :                :  *
                                 17                 :                :  * IDENTIFICATION
                                 18                 :                :  *    src/backend/access/common/attmap.c
                                 19                 :                :  *
                                 20                 :                :  *-------------------------------------------------------------------------
                                 21                 :                :  */
                                 22                 :                : 
                                 23                 :                : #include "postgres.h"
                                 24                 :                : 
                                 25                 :                : #include "access/attmap.h"
                                 26                 :                : #include "utils/builtins.h"
                                 27                 :                : 
                                 28                 :                : 
                                 29                 :                : static bool check_attrmap_match(TupleDesc indesc,
                                 30                 :                :                                 TupleDesc outdesc,
                                 31                 :                :                                 AttrMap *attrMap);
                                 32                 :                : 
                                 33                 :                : /*
                                 34                 :                :  * make_attrmap
                                 35                 :                :  *
                                 36                 :                :  * Utility routine to allocate an attribute map in the current memory
                                 37                 :                :  * context.
                                 38                 :                :  */
                                 39                 :                : AttrMap *
 2089 michael@paquier.xyz        40                 :CBC       29535 : make_attrmap(int maplen)
                                 41                 :                : {
                                 42                 :                :     AttrMap    *res;
                                 43                 :                : 
                                 44                 :          29535 :     res = (AttrMap *) palloc0(sizeof(AttrMap));
                                 45                 :          29535 :     res->maplen = maplen;
                                 46                 :          29535 :     res->attnums = (AttrNumber *) palloc0(sizeof(AttrNumber) * maplen);
                                 47                 :          29535 :     return res;
                                 48                 :                : }
                                 49                 :                : 
                                 50                 :                : /*
                                 51                 :                :  * free_attrmap
                                 52                 :                :  *
                                 53                 :                :  * Utility routine to release an attribute map.
                                 54                 :                :  */
                                 55                 :                : void
                                 56                 :          16107 : free_attrmap(AttrMap *map)
                                 57                 :                : {
                                 58                 :          16107 :     pfree(map->attnums);
                                 59                 :          16107 :     pfree(map);
                                 60                 :          16107 : }
                                 61                 :                : 
                                 62                 :                : /*
                                 63                 :                :  * build_attrmap_by_position
                                 64                 :                :  *
                                 65                 :                :  * Return a palloc'd bare attribute map for tuple conversion, matching input
                                 66                 :                :  * and output columns by position.  Dropped columns are ignored in both input
                                 67                 :                :  * and output, marked as 0.  This is normally a subroutine for
                                 68                 :                :  * convert_tuples_by_position in tupconvert.c, but it can be used standalone.
                                 69                 :                :  *
                                 70                 :                :  * Note: the errdetail messages speak of indesc as the "returned" rowtype,
                                 71                 :                :  * outdesc as the "expected" rowtype.  This is okay for current uses but
                                 72                 :                :  * might need generalization in future.
                                 73                 :                :  */
                                 74                 :                : AttrMap *
                                 75                 :           4788 : build_attrmap_by_position(TupleDesc indesc,
                                 76                 :                :                           TupleDesc outdesc,
                                 77                 :                :                           const char *msg)
                                 78                 :                : {
                                 79                 :                :     AttrMap    *attrMap;
                                 80                 :                :     int         nincols;
                                 81                 :                :     int         noutcols;
                                 82                 :                :     int         n;
                                 83                 :                :     int         i;
                                 84                 :                :     int         j;
                                 85                 :                :     bool        same;
                                 86                 :                : 
                                 87                 :                :     /*
                                 88                 :                :      * The length is computed as the number of attributes of the expected
                                 89                 :                :      * rowtype as it includes dropped attributes in its count.
                                 90                 :                :      */
                                 91                 :           4788 :     n = outdesc->natts;
                                 92                 :           4788 :     attrMap = make_attrmap(n);
                                 93                 :                : 
                                 94                 :           4788 :     j = 0;                      /* j is next physical input attribute */
                                 95                 :           4788 :     nincols = noutcols = 0;     /* these count non-dropped attributes */
                                 96                 :           4788 :     same = true;
                                 97         [ +  + ]:          17351 :     for (i = 0; i < n; i++)
                                 98                 :                :     {
  183 tgl@sss.pgh.pa.us          99                 :          12573 :         Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
                                100                 :                : 
                                101         [ +  + ]:          12573 :         if (outatt->attisdropped)
 2089 michael@paquier.xyz       102                 :             76 :             continue;           /* attrMap->attnums[i] is already 0 */
                                103                 :          12497 :         noutcols++;
                                104         [ +  + ]:          12506 :         for (; j < indesc->natts; j++)
                                105                 :                :         {
  183 tgl@sss.pgh.pa.us         106                 :          12502 :             Form_pg_attribute inatt = TupleDescAttr(indesc, j);
                                107                 :                : 
                                108         [ +  + ]:          12502 :             if (inatt->attisdropped)
 2089 michael@paquier.xyz       109                 :              9 :                 continue;
                                110                 :          12493 :             nincols++;
                                111                 :                : 
                                112                 :                :             /* Found matching column, now check type */
  183 tgl@sss.pgh.pa.us         113         [ +  + ]:          12493 :             if (outatt->atttypid != inatt->atttypid ||
                                114   [ -  +  -  - ]:          12483 :                 (outatt->atttypmod != inatt->atttypmod && outatt->atttypmod >= 0))
 2089 michael@paquier.xyz       115         [ +  - ]:             10 :                 ereport(ERROR,
                                116                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                                117                 :                :                          errmsg_internal("%s", _(msg)),
                                118                 :                :                          errdetail("Returned type %s does not match expected type %s in column \"%s\" (position %d).",
                                119                 :                :                                    format_type_with_typemod(inatt->atttypid,
                                120                 :                :                                                             inatt->atttypmod),
                                121                 :                :                                    format_type_with_typemod(outatt->atttypid,
                                122                 :                :                                                             outatt->atttypmod),
                                123                 :                :                                    NameStr(outatt->attname),
                                124                 :                :                                    noutcols)));
                                125                 :          12483 :             attrMap->attnums[i] = (AttrNumber) (j + 1);
                                126                 :          12483 :             j++;
                                127                 :          12483 :             break;
                                128                 :                :         }
                                129         [ +  + ]:          12487 :         if (attrMap->attnums[i] == 0)
                                130                 :              4 :             same = false;       /* we'll complain below */
                                131                 :                :     }
                                132                 :                : 
                                133                 :                :     /* Check for unused input columns */
                                134         [ +  + ]:           4781 :     for (; j < indesc->natts; j++)
                                135                 :                :     {
  260 drowley@postgresql.o      136         [ -  + ]:              3 :         if (TupleDescCompactAttr(indesc, j)->attisdropped)
 2089 michael@paquier.xyz       137                 :UBC           0 :             continue;
 2089 michael@paquier.xyz       138                 :CBC           3 :         nincols++;
                                139                 :              3 :         same = false;           /* we'll complain below */
                                140                 :                :     }
                                141                 :                : 
                                142                 :                :     /* Report column count mismatch using the non-dropped-column counts */
                                143         [ +  + ]:           4778 :     if (!same)
                                144         [ +  - ]:              7 :         ereport(ERROR,
                                145                 :                :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
                                146                 :                :                  errmsg_internal("%s", _(msg)),
                                147                 :                :                  errdetail("Number of returned columns (%d) does not match "
                                148                 :                :                            "expected column count (%d).",
                                149                 :                :                            nincols, noutcols)));
                                150                 :                : 
                                151                 :                :     /* Check if the map has a one-to-one match */
                                152         [ +  + ]:           4771 :     if (check_attrmap_match(indesc, outdesc, attrMap))
                                153                 :                :     {
                                154                 :                :         /* Runtime conversion is not needed */
                                155                 :           4723 :         free_attrmap(attrMap);
                                156                 :           4723 :         return NULL;
                                157                 :                :     }
                                158                 :                : 
                                159                 :             48 :     return attrMap;
                                160                 :                : }
                                161                 :                : 
                                162                 :                : /*
                                163                 :                :  * build_attrmap_by_name
                                164                 :                :  *
                                165                 :                :  * Return a palloc'd bare attribute map for tuple conversion, matching input
                                166                 :                :  * and output columns by name.  (Dropped columns are ignored in both input and
                                167                 :                :  * output.)  This is normally a subroutine for convert_tuples_by_name in
                                168                 :                :  * tupconvert.c, but can be used standalone.
                                169                 :                :  *
                                170                 :                :  * If 'missing_ok' is true, a column from 'outdesc' not being present in
                                171                 :                :  * 'indesc' is not flagged as an error; AttrMap.attnums[] entry for such an
                                172                 :                :  * outdesc column will be 0 in that case.
                                173                 :                :  */
                                174                 :                : AttrMap *
                                175                 :          18958 : build_attrmap_by_name(TupleDesc indesc,
                                176                 :                :                       TupleDesc outdesc,
                                177                 :                :                       bool missing_ok)
                                178                 :                : {
                                179                 :                :     AttrMap    *attrMap;
                                180                 :                :     int         outnatts;
                                181                 :                :     int         innatts;
                                182                 :                :     int         i;
                                183                 :          18958 :     int         nextindesc = -1;
                                184                 :                : 
                                185                 :          18958 :     outnatts = outdesc->natts;
                                186                 :          18958 :     innatts = indesc->natts;
                                187                 :                : 
                                188                 :          18958 :     attrMap = make_attrmap(outnatts);
                                189         [ +  + ]:          64571 :     for (i = 0; i < outnatts; i++)
                                190                 :                :     {
                                191                 :          45613 :         Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
                                192                 :                :         char       *attname;
                                193                 :                :         Oid         atttypid;
                                194                 :                :         int32       atttypmod;
                                195                 :                :         int         j;
                                196                 :                : 
                                197         [ +  + ]:          45613 :         if (outatt->attisdropped)
                                198                 :           2145 :             continue;           /* attrMap->attnums[i] is already 0 */
                                199                 :          43468 :         attname = NameStr(outatt->attname);
                                200                 :          43468 :         atttypid = outatt->atttypid;
                                201                 :          43468 :         atttypmod = outatt->atttypmod;
                                202                 :                : 
                                203                 :                :         /*
                                204                 :                :          * Now search for an attribute with the same name in the indesc. It
                                205                 :                :          * seems likely that a partitioned table will have the attributes in
                                206                 :                :          * the same order as the partition, so the search below is optimized
                                207                 :                :          * for that case.  It is possible that columns are dropped in one of
                                208                 :                :          * the relations, but not the other, so we use the 'nextindesc'
                                209                 :                :          * counter to track the starting point of the search.  If the inner
                                210                 :                :          * loop encounters dropped columns then it will have to skip over
                                211                 :                :          * them, but it should leave 'nextindesc' at the correct position for
                                212                 :                :          * the next outer loop.
                                213                 :                :          */
                                214         [ +  + ]:          53920 :         for (j = 0; j < innatts; j++)
                                215                 :                :         {
                                216                 :                :             Form_pg_attribute inatt;
                                217                 :                : 
                                218                 :          53805 :             nextindesc++;
                                219         [ +  + ]:          53805 :             if (nextindesc >= innatts)
                                220                 :           3488 :                 nextindesc = 0;
                                221                 :                : 
                                222                 :          53805 :             inatt = TupleDescAttr(indesc, nextindesc);
                                223         [ +  + ]:          53805 :             if (inatt->attisdropped)
                                224                 :           1841 :                 continue;
                                225         [ +  + ]:          51964 :             if (strcmp(attname, NameStr(inatt->attname)) == 0)
                                226                 :                :             {
                                227                 :                :                 /* Found it, check type */
                                228   [ +  -  -  + ]:          43353 :                 if (atttypid != inatt->atttypid || atttypmod != inatt->atttypmod)
 2089 michael@paquier.xyz       229         [ #  # ]:UBC           0 :                     ereport(ERROR,
                                230                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                                231                 :                :                              errmsg("could not convert row type"),
                                232                 :                :                              errdetail("Attribute \"%s\" of type %s does not match corresponding attribute of type %s.",
                                233                 :                :                                        attname,
                                234                 :                :                                        format_type_be(outdesc->tdtypeid),
                                235                 :                :                                        format_type_be(indesc->tdtypeid))));
 2089 michael@paquier.xyz       236                 :CBC       43353 :                 attrMap->attnums[i] = inatt->attnum;
                                237                 :          43353 :                 break;
                                238                 :                :             }
                                239                 :                :         }
 1012 alvherre@alvh.no-ip.      240   [ +  +  -  + ]:          43468 :         if (attrMap->attnums[i] == 0 && !missing_ok)
 2089 michael@paquier.xyz       241         [ #  # ]:UBC           0 :             ereport(ERROR,
                                242                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                                243                 :                :                      errmsg("could not convert row type"),
                                244                 :                :                      errdetail("Attribute \"%s\" of type %s does not exist in type %s.",
                                245                 :                :                                attname,
                                246                 :                :                                format_type_be(outdesc->tdtypeid),
                                247                 :                :                                format_type_be(indesc->tdtypeid))));
                                248                 :                :     }
 2089 michael@paquier.xyz       249                 :CBC       18958 :     return attrMap;
                                250                 :                : }
                                251                 :                : 
                                252                 :                : /*
                                253                 :                :  * build_attrmap_by_name_if_req
                                254                 :                :  *
                                255                 :                :  * Returns mapping created by build_attrmap_by_name, or NULL if no
                                256                 :                :  * conversion is required.  This is a convenience routine for
                                257                 :                :  * convert_tuples_by_name() in tupconvert.c and other functions, but it
                                258                 :                :  * can be used standalone.
                                259                 :                :  */
                                260                 :                : AttrMap *
                                261                 :           6885 : build_attrmap_by_name_if_req(TupleDesc indesc,
                                262                 :                :                              TupleDesc outdesc,
                                263                 :                :                              bool missing_ok)
                                264                 :                : {
                                265                 :                :     AttrMap    *attrMap;
                                266                 :                : 
                                267                 :                :     /* Verify compatibility and prepare attribute-number map */
 1012 alvherre@alvh.no-ip.      268                 :           6885 :     attrMap = build_attrmap_by_name(indesc, outdesc, missing_ok);
                                269                 :                : 
                                270                 :                :     /* Check if the map has a one-to-one match */
 2089 michael@paquier.xyz       271         [ +  + ]:           6885 :     if (check_attrmap_match(indesc, outdesc, attrMap))
                                272                 :                :     {
                                273                 :                :         /* Runtime conversion is not needed */
                                274                 :           5300 :         free_attrmap(attrMap);
                                275                 :           5300 :         return NULL;
                                276                 :                :     }
                                277                 :                : 
                                278                 :           1585 :     return attrMap;
                                279                 :                : }
                                280                 :                : 
                                281                 :                : /*
                                282                 :                :  * check_attrmap_match
                                283                 :                :  *
                                284                 :                :  * Check to see if the map is a one-to-one match, in which case we need
                                285                 :                :  * not to do a tuple conversion, and the attribute map is not necessary.
                                286                 :                :  */
                                287                 :                : static bool
                                288                 :          11656 : check_attrmap_match(TupleDesc indesc,
                                289                 :                :                     TupleDesc outdesc,
                                290                 :                :                     AttrMap *attrMap)
                                291                 :                : {
                                292                 :                :     int         i;
                                293                 :                : 
                                294                 :                :     /* no match if attribute numbers are not the same */
                                295         [ +  + ]:          11656 :     if (indesc->natts != outdesc->natts)
                                296                 :            729 :         return false;
                                297                 :                : 
                                298         [ +  + ]:          36088 :     for (i = 0; i < attrMap->maplen; i++)
                                299                 :                :     {
  260 drowley@postgresql.o      300                 :          26065 :         CompactAttribute *inatt = TupleDescCompactAttr(indesc, i);
                                301                 :                :         CompactAttribute *outatt;
                                302                 :                : 
                                303                 :                :         /*
                                304                 :                :          * If the input column has a missing attribute, we need a conversion.
                                305                 :                :          */
 2040 rhodiumtoad@postgres      306         [ +  + ]:          26065 :         if (inatt->atthasmissing)
                                307                 :             25 :             return false;
                                308                 :                : 
 2089 michael@paquier.xyz       309         [ +  + ]:          26040 :         if (attrMap->attnums[i] == (i + 1))
                                310                 :          25131 :             continue;
                                311                 :                : 
  260 drowley@postgresql.o      312                 :            909 :         outatt = TupleDescCompactAttr(outdesc, i);
                                313                 :                : 
                                314                 :                :         /*
                                315                 :                :          * If it's a dropped column and the corresponding input column is also
                                316                 :                :          * dropped, we don't need a conversion.  However, attlen and
                                317                 :                :          * attalignby must agree.
                                318                 :                :          */
 2089 michael@paquier.xyz       319         [ +  + ]:            909 :         if (attrMap->attnums[i] == 0 &&
                                320         [ +  + ]:             46 :             inatt->attisdropped &&
                                321         [ +  - ]:             30 :             inatt->attlen == outatt->attlen &&
  259 drowley@postgresql.o      322         [ +  - ]:             30 :             inatt->attalignby == outatt->attalignby)
 2089 michael@paquier.xyz       323                 :             30 :             continue;
                                324                 :                : 
                                325                 :            879 :         return false;
                                326                 :                :     }
                                327                 :                : 
                                328                 :          10023 :     return true;
                                329                 :                : }
        

Generated by: LCOV version 2.4-beta