LCOV - differential code coverage report
Current view: top level - src/backend/utils/adt - rowtypes.c (source / functions) Coverage Total Hit UNC UBC GNC CBC EUB ECB DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 79.3 % 847 672 175 2 670 2
Current Date: 2025-09-06 07:49:51 +0900 Functions: 91.7 % 24 22 2 1 21
Baseline: lcov-20250906-005545-baseline Branches: 61.6 % 648 399 2 247 2 397 2 2
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 2 2 2
(360..) days: 79.3 % 845 670 175 670
Function coverage date bins:
(360..) days: 91.7 % 24 22 2 1 21
Branch coverage date bins:
(7,30] days: 50.0 % 4 2 2 2
(360..) days: 61.3 % 648 397 247 397 2 2

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * rowtypes.c
                                  4                 :                :  *    I/O and comparison functions for generic composite types.
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/utils/adt/rowtypes.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : #include "postgres.h"
                                 16                 :                : 
                                 17                 :                : #include <ctype.h>
                                 18                 :                : 
                                 19                 :                : #include "access/detoast.h"
                                 20                 :                : #include "access/htup_details.h"
                                 21                 :                : #include "catalog/pg_type.h"
                                 22                 :                : #include "funcapi.h"
                                 23                 :                : #include "libpq/pqformat.h"
                                 24                 :                : #include "miscadmin.h"
                                 25                 :                : #include "utils/builtins.h"
                                 26                 :                : #include "utils/datum.h"
                                 27                 :                : #include "utils/lsyscache.h"
                                 28                 :                : #include "utils/typcache.h"
                                 29                 :                : 
                                 30                 :                : 
                                 31                 :                : /*
                                 32                 :                :  * structure to cache metadata needed for record I/O
                                 33                 :                :  */
                                 34                 :                : typedef struct ColumnIOData
                                 35                 :                : {
                                 36                 :                :     Oid         column_type;
                                 37                 :                :     Oid         typiofunc;
                                 38                 :                :     Oid         typioparam;
                                 39                 :                :     bool        typisvarlena;
                                 40                 :                :     FmgrInfo    proc;
                                 41                 :                : } ColumnIOData;
                                 42                 :                : 
                                 43                 :                : typedef struct RecordIOData
                                 44                 :                : {
                                 45                 :                :     Oid         record_type;
                                 46                 :                :     int32       record_typmod;
                                 47                 :                :     int         ncolumns;
                                 48                 :                :     ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER];
                                 49                 :                : } RecordIOData;
                                 50                 :                : 
                                 51                 :                : /*
                                 52                 :                :  * structure to cache metadata needed for record comparison
                                 53                 :                :  */
                                 54                 :                : typedef struct ColumnCompareData
                                 55                 :                : {
                                 56                 :                :     TypeCacheEntry *typentry;   /* has everything we need, actually */
                                 57                 :                : } ColumnCompareData;
                                 58                 :                : 
                                 59                 :                : typedef struct RecordCompareData
                                 60                 :                : {
                                 61                 :                :     int         ncolumns;       /* allocated length of columns[] */
                                 62                 :                :     Oid         record1_type;
                                 63                 :                :     int32       record1_typmod;
                                 64                 :                :     Oid         record2_type;
                                 65                 :                :     int32       record2_typmod;
                                 66                 :                :     ColumnCompareData columns[FLEXIBLE_ARRAY_MEMBER];
                                 67                 :                : } RecordCompareData;
                                 68                 :                : 
                                 69                 :                : 
                                 70                 :                : /*
                                 71                 :                :  * record_in        - input routine for any composite type.
                                 72                 :                :  */
                                 73                 :                : Datum
 7828 tgl@sss.pgh.pa.us          74                 :CBC         956 : record_in(PG_FUNCTION_ARGS)
                                 75                 :                : {
 7762                            76                 :            956 :     char       *string = PG_GETARG_CSTRING(0);
                                 77                 :            956 :     Oid         tupType = PG_GETARG_OID(1);
 3669                            78                 :            956 :     int32       tupTypmod = PG_GETARG_INT32(2);
 1002                            79                 :            956 :     Node       *escontext = fcinfo->context;
                                 80                 :                :     HeapTupleHeader result;
                                 81                 :                :     TupleDesc   tupdesc;
                                 82                 :                :     HeapTuple   tuple;
                                 83                 :                :     RecordIOData *my_extra;
 7703                            84                 :            956 :     bool        needComma = false;
                                 85                 :                :     int         ncolumns;
                                 86                 :                :     int         i;
                                 87                 :                :     char       *ptr;
                                 88                 :                :     Datum      *values;
                                 89                 :                :     bool       *nulls;
                                 90                 :                :     StringInfoData buf;
                                 91                 :                : 
 3624 noah@leadboat.com          92                 :            956 :     check_stack_depth();        /* recurses for record-type columns */
                                 93                 :                : 
                                 94                 :                :     /*
                                 95                 :                :      * Give a friendly error message if we did not get enough info to identify
                                 96                 :                :      * the target record type.  (lookup_rowtype_tupdesc would fail anyway, but
                                 97                 :                :      * with a non-user-friendly message.)  In ordinary SQL usage, we'll get -1
                                 98                 :                :      * for typmod, since composite types and RECORD have no type modifiers at
                                 99                 :                :      * the SQL level, and thus must fail for RECORD.  However some callers can
                                100                 :                :      * supply a valid typmod, and then we can do something useful for RECORD.
                                101                 :                :      */
 3669 tgl@sss.pgh.pa.us         102   [ +  +  -  + ]:            956 :     if (tupType == RECORDOID && tupTypmod < 0)
 1002 tgl@sss.pgh.pa.us         103         [ #  # ]:UBC           0 :         ereturn(escontext, (Datum) 0,
                                104                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                105                 :                :                  errmsg("input of anonymous composite types is not implemented")));
                                106                 :                : 
                                107                 :                :     /*
                                108                 :                :      * This comes from the composite type's pg_type.oid and stores system oids
                                109                 :                :      * in user tables, specifically DatumTupleFields. This oid must be
                                110                 :                :      * preserved by binary upgrades.
                                111                 :                :      */
 7762 tgl@sss.pgh.pa.us         112                 :CBC         956 :     tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
                                113                 :            956 :     ncolumns = tupdesc->natts;
                                114                 :                : 
                                115                 :                :     /*
                                116                 :                :      * We arrange to look up the needed I/O info just once per series of
                                117                 :                :      * calls, assuming the record type doesn't change underneath us.
                                118                 :                :      */
                                119                 :            956 :     my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
                                120         [ +  + ]:            956 :     if (my_extra == NULL ||
                                121         [ -  + ]:            589 :         my_extra->ncolumns != ncolumns)
                                122                 :                :     {
                                123                 :            734 :         fcinfo->flinfo->fn_extra =
                                124                 :            367 :             MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
                                125                 :                :                                offsetof(RecordIOData, columns) +
 3851                           126                 :            367 :                                ncolumns * sizeof(ColumnIOData));
 7762                           127                 :            367 :         my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
                                128                 :            367 :         my_extra->record_type = InvalidOid;
                                129                 :            367 :         my_extra->record_typmod = 0;
                                130                 :                :     }
                                131                 :                : 
                                132         [ +  + ]:            956 :     if (my_extra->record_type != tupType ||
                                133         [ -  + ]:            589 :         my_extra->record_typmod != tupTypmod)
                                134                 :                :     {
                                135   [ +  -  +  -  :           8957 :         MemSet(my_extra, 0,
                                     +  -  +  -  +  
                                                 + ]
                                136                 :                :                offsetof(RecordIOData, columns) +
                                137                 :                :                ncolumns * sizeof(ColumnIOData));
                                138                 :            367 :         my_extra->record_type = tupType;
                                139                 :            367 :         my_extra->record_typmod = tupTypmod;
                                140                 :            367 :         my_extra->ncolumns = ncolumns;
                                141                 :                :     }
                                142                 :                : 
                                143                 :            956 :     values = (Datum *) palloc(ncolumns * sizeof(Datum));
 6152                           144                 :            956 :     nulls = (bool *) palloc(ncolumns * sizeof(bool));
                                145                 :                : 
                                146                 :                :     /*
                                147                 :                :      * Scan the string.  We use "buf" to accumulate the de-quoted data for
                                148                 :                :      * each column, which is then fed to the appropriate input converter.
                                149                 :                :      */
 7762                           150                 :            956 :     ptr = string;
                                151                 :                :     /* Allow leading whitespace */
                                152   [ +  -  +  + ]:            959 :     while (*ptr && isspace((unsigned char) *ptr))
                                153                 :              3 :         ptr++;
                                154         [ +  + ]:            956 :     if (*ptr++ != '(')
                                155                 :                :     {
 1002                           156         [ +  - ]:              5 :         errsave(escontext,
                                157                 :                :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                158                 :                :                  errmsg("malformed record literal: \"%s\"", string),
                                159                 :                :                  errdetail("Missing left parenthesis.")));
 1002 tgl@sss.pgh.pa.us         160                 :UBC           0 :         goto fail;
                                161                 :                :     }
                                162                 :                : 
 7762 tgl@sss.pgh.pa.us         163                 :CBC         951 :     initStringInfo(&buf);
                                164                 :                : 
                                165         [ +  + ]:           4623 :     for (i = 0; i < ncolumns; i++)
                                166                 :                :     {
 2939 andres@anarazel.de        167                 :           3691 :         Form_pg_attribute att = TupleDescAttr(tupdesc, i);
 7762 tgl@sss.pgh.pa.us         168                 :           3691 :         ColumnIOData *column_info = &my_extra->columns[i];
 2939 andres@anarazel.de        169                 :           3691 :         Oid         column_type = att->atttypid;
                                170                 :                :         char       *column_data;
                                171                 :                : 
                                172                 :                :         /* Ignore dropped columns in datatype, but fill with nulls */
                                173         [ +  + ]:           3691 :         if (att->attisdropped)
                                174                 :                :         {
 7703 tgl@sss.pgh.pa.us         175                 :            197 :             values[i] = (Datum) 0;
 6152                           176                 :            197 :             nulls[i] = true;
 7703                           177                 :            197 :             continue;
                                178                 :                :         }
                                179                 :                : 
                                180         [ +  + ]:           3494 :         if (needComma)
                                181                 :                :         {
                                182                 :                :             /* Skip comma that separates prior field from this one */
                                183         [ +  + ]:           2543 :             if (*ptr == ',')
                                184                 :           2540 :                 ptr++;
                                185                 :                :             else
                                186                 :                :                 /* *ptr must be ')' */
                                187                 :                :             {
 1002                           188         [ +  - ]:              3 :                 errsave(escontext,
                                189                 :                :                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                190                 :                :                          errmsg("malformed record literal: \"%s\"", string),
                                191                 :                :                          errdetail("Too few columns.")));
 1002 tgl@sss.pgh.pa.us         192                 :UBC           0 :                 goto fail;
                                193                 :                :             }
                                194                 :                :         }
                                195                 :                : 
                                196                 :                :         /* Check for null: completely empty input means null */
 7762 tgl@sss.pgh.pa.us         197   [ +  +  +  + ]:CBC        3491 :         if (*ptr == ',' || *ptr == ')')
                                198                 :                :         {
 7095                           199                 :            312 :             column_data = NULL;
 6152                           200                 :            312 :             nulls[i] = true;
                                201                 :                :         }
                                202                 :                :         else
                                203                 :                :         {
                                204                 :                :             /* Extract string for this column */
 7678 bruce@momjian.us          205                 :           3179 :             bool        inquote = false;
                                206                 :                : 
 6762 neilc@samurai.com         207                 :           3179 :             resetStringInfo(&buf);
 7762 tgl@sss.pgh.pa.us         208   [ +  +  +  +  :          18744 :             while (inquote || !(*ptr == ',' || *ptr == ')'))
                                              +  + ]
                                209                 :                :             {
 7678 bruce@momjian.us          210                 :          15568 :                 char        ch = *ptr++;
                                211                 :                : 
 7762 tgl@sss.pgh.pa.us         212         [ +  + ]:          15568 :                 if (ch == '\0')
                                213                 :                :                 {
 1002                           214         [ -  + ]:              3 :                     errsave(escontext,
                                215                 :                :                             (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                216                 :                :                              errmsg("malformed record literal: \"%s\"",
                                217                 :                :                                     string),
                                218                 :                :                              errdetail("Unexpected end of input.")));
                                219                 :              3 :                     goto fail;
                                220                 :                :                 }
 7762                           221         [ +  + ]:          15565 :                 if (ch == '\\')
                                222                 :                :                 {
                                223         [ -  + ]:              3 :                     if (*ptr == '\0')
                                224                 :                :                     {
 1002 tgl@sss.pgh.pa.us         225         [ #  # ]:UBC           0 :                         errsave(escontext,
                                226                 :                :                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                227                 :                :                                  errmsg("malformed record literal: \"%s\"",
                                228                 :                :                                         string),
                                229                 :                :                                  errdetail("Unexpected end of input.")));
                                230                 :              0 :                         goto fail;
                                231                 :                :                     }
 7762 tgl@sss.pgh.pa.us         232                 :CBC           3 :                     appendStringInfoChar(&buf, *ptr++);
                                233                 :                :                 }
 3546 peter_e@gmx.net           234         [ +  + ]:          15562 :                 else if (ch == '"')
                                235                 :                :                 {
 7762 tgl@sss.pgh.pa.us         236         [ +  + ]:           1041 :                     if (!inquote)
                                237                 :            496 :                         inquote = true;
 3546 peter_e@gmx.net           238         [ +  + ]:            545 :                     else if (*ptr == '"')
                                239                 :                :                     {
                                240                 :                :                         /* doubled quote within quote sequence */
 7762 tgl@sss.pgh.pa.us         241                 :             49 :                         appendStringInfoChar(&buf, *ptr++);
                                242                 :                :                     }
                                243                 :                :                     else
                                244                 :            496 :                         inquote = false;
                                245                 :                :                 }
                                246                 :                :                 else
                                247                 :          14521 :                     appendStringInfoChar(&buf, ch);
                                248                 :                :             }
                                249                 :                : 
 7095                           250                 :           3176 :             column_data = buf.data;
 6152                           251                 :           3176 :             nulls[i] = false;
                                252                 :                :         }
                                253                 :                : 
                                254                 :                :         /*
                                255                 :                :          * Convert the column value
                                256                 :                :          */
 7095                           257         [ +  + ]:           3488 :         if (column_info->column_type != column_type)
                                258                 :                :         {
                                259                 :            947 :             getTypeInputInfo(column_type,
                                260                 :                :                              &column_info->typiofunc,
                                261                 :                :                              &column_info->typioparam);
                                262                 :            947 :             fmgr_info_cxt(column_info->typiofunc, &column_info->proc,
                                263                 :            947 :                           fcinfo->flinfo->fn_mcxt);
                                264                 :            947 :             column_info->column_type = column_type;
                                265                 :                :         }
                                266                 :                : 
 1002                           267         [ +  + ]:           3484 :         if (!InputFunctionCallSafe(&column_info->proc,
                                268                 :                :                                    column_data,
                                269                 :                :                                    column_info->typioparam,
                                270                 :                :                                    att->atttypmod,
                                271                 :                :                                    escontext,
                                272                 :           3488 :                                    &values[i]))
                                273                 :              9 :             goto fail;
                                274                 :                : 
                                275                 :                :         /*
                                276                 :                :          * Prep for next column
                                277                 :                :          */
 7703                           278                 :           3475 :         needComma = true;
                                279                 :                :     }
                                280                 :                : 
 7762                           281         [ +  + ]:            932 :     if (*ptr++ != ')')
                                282                 :                :     {
 1002                           283         [ +  - ]:              3 :         errsave(escontext,
                                284                 :                :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                285                 :                :                  errmsg("malformed record literal: \"%s\"", string),
                                286                 :                :                  errdetail("Too many columns.")));
 1002 tgl@sss.pgh.pa.us         287                 :UBC           0 :         goto fail;
                                288                 :                :     }
                                289                 :                :     /* Allow trailing whitespace */
 7762 tgl@sss.pgh.pa.us         290   [ +  +  +  + ]:CBC         938 :     while (*ptr && isspace((unsigned char) *ptr))
                                291                 :              9 :         ptr++;
                                292         [ +  + ]:            929 :     if (*ptr)
                                293                 :                :     {
 1002                           294         [ +  - ]:              3 :         errsave(escontext,
                                295                 :                :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                296                 :                :                  errmsg("malformed record literal: \"%s\"", string),
                                297                 :                :                  errdetail("Junk after right parenthesis.")));
 1002 tgl@sss.pgh.pa.us         298                 :UBC           0 :         goto fail;
                                299                 :                :     }
                                300                 :                : 
 6152 tgl@sss.pgh.pa.us         301                 :CBC         926 :     tuple = heap_form_tuple(tupdesc, values, nulls);
                                302                 :                : 
                                303                 :                :     /*
                                304                 :                :      * We cannot return tuple->t_data because heap_form_tuple allocates it as
                                305                 :                :      * part of a larger chunk, and our caller may expect to be able to pfree
                                306                 :                :      * our result.  So must copy the info into a new palloc chunk.
                                307                 :                :      */
 7446                           308                 :            926 :     result = (HeapTupleHeader) palloc(tuple->t_len);
                                309                 :            926 :     memcpy(result, tuple->t_data, tuple->t_len);
                                310                 :                : 
                                311                 :            926 :     heap_freetuple(tuple);
 7762                           312                 :            926 :     pfree(buf.data);
                                313                 :            926 :     pfree(values);
                                314                 :            926 :     pfree(nulls);
 7022                           315         [ +  - ]:            926 :     ReleaseTupleDesc(tupdesc);
                                316                 :                : 
 7446                           317                 :            926 :     PG_RETURN_HEAPTUPLEHEADER(result);
                                318                 :                : 
                                319                 :                :     /* exit here once we've done lookup_rowtype_tupdesc */
 1002                           320                 :             12 : fail:
                                321         [ +  - ]:             12 :     ReleaseTupleDesc(tupdesc);
                                322                 :             12 :     PG_RETURN_NULL();
                                323                 :                : }
                                324                 :                : 
                                325                 :                : /*
                                326                 :                :  * record_out       - output routine for any composite type.
                                327                 :                :  */
                                328                 :                : Datum
 7828                           329                 :          18118 : record_out(PG_FUNCTION_ARGS)
                                330                 :                : {
 7762                           331                 :          18118 :     HeapTupleHeader rec = PG_GETARG_HEAPTUPLEHEADER(0);
                                332                 :                :     Oid         tupType;
                                333                 :                :     int32       tupTypmod;
                                334                 :                :     TupleDesc   tupdesc;
                                335                 :                :     HeapTupleData tuple;
                                336                 :                :     RecordIOData *my_extra;
 7703                           337                 :          18118 :     bool        needComma = false;
                                338                 :                :     int         ncolumns;
                                339                 :                :     int         i;
                                340                 :                :     Datum      *values;
                                341                 :                :     bool       *nulls;
                                342                 :                :     StringInfoData buf;
                                343                 :                : 
 3624 noah@leadboat.com         344                 :          18118 :     check_stack_depth();        /* recurses for record-type columns */
                                345                 :                : 
                                346                 :                :     /* Extract type info from the tuple itself */
 7434 tgl@sss.pgh.pa.us         347                 :          18118 :     tupType = HeapTupleHeaderGetTypeId(rec);
                                348                 :          18118 :     tupTypmod = HeapTupleHeaderGetTypMod(rec);
 7762                           349                 :          18118 :     tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
                                350                 :          18118 :     ncolumns = tupdesc->natts;
                                351                 :                : 
                                352                 :                :     /* Build a temporary HeapTuple control structure */
                                353                 :          18118 :     tuple.t_len = HeapTupleHeaderGetDatumLength(rec);
                                354                 :          18118 :     ItemPointerSetInvalid(&(tuple.t_self));
                                355                 :          18118 :     tuple.t_tableOid = InvalidOid;
                                356                 :          18118 :     tuple.t_data = rec;
                                357                 :                : 
                                358                 :                :     /*
                                359                 :                :      * We arrange to look up the needed I/O info just once per series of
                                360                 :                :      * calls, assuming the record type doesn't change underneath us.
                                361                 :                :      */
                                362                 :          18118 :     my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
                                363         [ +  + ]:          18118 :     if (my_extra == NULL ||
                                364         [ +  + ]:          15497 :         my_extra->ncolumns != ncolumns)
                                365                 :                :     {
                                366                 :           5266 :         fcinfo->flinfo->fn_extra =
                                367                 :           2633 :             MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
                                368                 :                :                                offsetof(RecordIOData, columns) +
 3851                           369                 :           2633 :                                ncolumns * sizeof(ColumnIOData));
 7762                           370                 :           2633 :         my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
                                371                 :           2633 :         my_extra->record_type = InvalidOid;
                                372                 :           2633 :         my_extra->record_typmod = 0;
                                373                 :                :     }
                                374                 :                : 
                                375         [ +  + ]:          18118 :     if (my_extra->record_type != tupType ||
                                376         [ +  + ]:          15485 :         my_extra->record_typmod != tupTypmod)
                                377                 :                :     {
                                378   [ +  -  +  -  :          60379 :         MemSet(my_extra, 0,
                                     +  -  +  +  +  
                                                 + ]
                                379                 :                :                offsetof(RecordIOData, columns) +
                                380                 :                :                ncolumns * sizeof(ColumnIOData));
                                381                 :           2653 :         my_extra->record_type = tupType;
                                382                 :           2653 :         my_extra->record_typmod = tupTypmod;
                                383                 :           2653 :         my_extra->ncolumns = ncolumns;
                                384                 :                :     }
                                385                 :                : 
                                386                 :          18118 :     values = (Datum *) palloc(ncolumns * sizeof(Datum));
 6152                           387                 :          18118 :     nulls = (bool *) palloc(ncolumns * sizeof(bool));
                                388                 :                : 
                                389                 :                :     /* Break down the tuple into fields */
                                390                 :          18118 :     heap_deform_tuple(&tuple, tupdesc, values, nulls);
                                391                 :                : 
                                392                 :                :     /* And build the result string */
 7762                           393                 :          18118 :     initStringInfo(&buf);
                                394                 :                : 
                                395                 :          18118 :     appendStringInfoChar(&buf, '(');
                                396                 :                : 
                                397         [ +  + ]:         109425 :     for (i = 0; i < ncolumns; i++)
                                398                 :                :     {
 2939 andres@anarazel.de        399                 :          91307 :         Form_pg_attribute att = TupleDescAttr(tupdesc, i);
 7762 tgl@sss.pgh.pa.us         400                 :          91307 :         ColumnIOData *column_info = &my_extra->columns[i];
 2939 andres@anarazel.de        401                 :          91307 :         Oid         column_type = att->atttypid;
                                402                 :                :         Datum       attr;
                                403                 :                :         char       *value;
                                404                 :                :         char       *tmp;
                                405                 :                :         bool        nq;
                                406                 :                : 
                                407                 :                :         /* Ignore dropped columns in datatype */
                                408         [ +  + ]:          91307 :         if (att->attisdropped)
 7703 tgl@sss.pgh.pa.us         409                 :            322 :             continue;
                                410                 :                : 
                                411         [ +  + ]:          90985 :         if (needComma)
 7762                           412                 :          72870 :             appendStringInfoChar(&buf, ',');
 7703                           413                 :          90985 :         needComma = true;
                                414                 :                : 
 6152                           415         [ +  + ]:          90985 :         if (nulls[i])
                                416                 :                :         {
                                417                 :                :             /* emit nothing... */
 7762                           418                 :           2510 :             continue;
                                419                 :                :         }
                                420                 :                : 
                                421                 :                :         /*
                                422                 :                :          * Convert the column value to text
                                423                 :                :          */
                                424         [ +  + ]:          88475 :         if (column_info->column_type != column_type)
                                425                 :                :         {
                                426                 :           6196 :             getTypeOutputInfo(column_type,
                                427                 :                :                               &column_info->typiofunc,
                                428                 :                :                               &column_info->typisvarlena);
                                429                 :           6196 :             fmgr_info_cxt(column_info->typiofunc, &column_info->proc,
                                430                 :           6196 :                           fcinfo->flinfo->fn_mcxt);
                                431                 :           6196 :             column_info->column_type = column_type;
                                432                 :                :         }
                                433                 :                : 
 4325                           434                 :          88475 :         attr = values[i];
 4680                           435                 :          88475 :         value = OutputFunctionCall(&column_info->proc, attr);
                                436                 :                : 
                                437                 :                :         /* Detect whether we need double quotes for this value */
 7762                           438                 :          88475 :         nq = (value[0] == '\0');    /* force quotes for empty string */
                                439         [ +  + ]:       54139413 :         for (tmp = value; *tmp; tmp++)
                                440                 :                :         {
                                441                 :       54077189 :             char        ch = *tmp;
                                442                 :                : 
                                443   [ +  +  +  +  :       54077189 :             if (ch == '"' || ch == '\\' ||
                                              +  + ]
                                444   [ +  -  +  + ]:       54076727 :                 ch == '(' || ch == ')' || ch == ',' ||
                                445         [ +  + ]:       54076221 :                 isspace((unsigned char) ch))
                                446                 :                :             {
                                447                 :          26251 :                 nq = true;
                                448                 :          26251 :                 break;
                                449                 :                :             }
                                450                 :                :         }
                                451                 :                : 
                                452                 :                :         /* And emit the string */
                                453         [ +  + ]:          88475 :         if (nq)
 4680                           454         [ -  + ]:          26253 :             appendStringInfoCharMacro(&buf, '"');
 7762                           455         [ +  + ]:       54407362 :         for (tmp = value; *tmp; tmp++)
                                456                 :                :         {
                                457                 :       54318887 :             char        ch = *tmp;
                                458                 :                : 
                                459   [ +  +  +  + ]:       54318887 :             if (ch == '"' || ch == '\\')
 4680                           460         [ -  + ]:            588 :                 appendStringInfoCharMacro(&buf, ch);
                                461         [ +  + ]:       54318887 :             appendStringInfoCharMacro(&buf, ch);
                                462                 :                :         }
 7762                           463         [ +  + ]:          88475 :         if (nq)
 4680                           464         [ -  + ]:          26253 :             appendStringInfoCharMacro(&buf, '"');
                                465                 :                :     }
                                466                 :                : 
 7762                           467                 :          18118 :     appendStringInfoChar(&buf, ')');
                                468                 :                : 
                                469                 :          18118 :     pfree(values);
                                470                 :          18118 :     pfree(nulls);
 7022                           471         [ +  + ]:          18118 :     ReleaseTupleDesc(tupdesc);
                                472                 :                : 
 7762                           473                 :          18118 :     PG_RETURN_CSTRING(buf.data);
                                474                 :                : }
                                475                 :                : 
                                476                 :                : /*
                                477                 :                :  * record_recv      - binary input routine for any composite type.
                                478                 :                :  */
                                479                 :                : Datum
 7828 tgl@sss.pgh.pa.us         480                 :UBC           0 : record_recv(PG_FUNCTION_ARGS)
                                481                 :                : {
 7762                           482                 :              0 :     StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
                                483                 :              0 :     Oid         tupType = PG_GETARG_OID(1);
 3669                           484                 :              0 :     int32       tupTypmod = PG_GETARG_INT32(2);
                                485                 :                :     HeapTupleHeader result;
                                486                 :                :     TupleDesc   tupdesc;
                                487                 :                :     HeapTuple   tuple;
                                488                 :                :     RecordIOData *my_extra;
                                489                 :                :     int         ncolumns;
                                490                 :                :     int         usercols;
                                491                 :                :     int         validcols;
                                492                 :                :     int         i;
                                493                 :                :     Datum      *values;
                                494                 :                :     bool       *nulls;
                                495                 :                : 
 3624 noah@leadboat.com         496                 :              0 :     check_stack_depth();        /* recurses for record-type columns */
                                497                 :                : 
                                498                 :                :     /*
                                499                 :                :      * Give a friendly error message if we did not get enough info to identify
                                500                 :                :      * the target record type.  (lookup_rowtype_tupdesc would fail anyway, but
                                501                 :                :      * with a non-user-friendly message.)  In ordinary SQL usage, we'll get -1
                                502                 :                :      * for typmod, since composite types and RECORD have no type modifiers at
                                503                 :                :      * the SQL level, and thus must fail for RECORD.  However some callers can
                                504                 :                :      * supply a valid typmod, and then we can do something useful for RECORD.
                                505                 :                :      */
 3669 tgl@sss.pgh.pa.us         506   [ #  #  #  # ]:              0 :     if (tupType == RECORDOID && tupTypmod < 0)
 7762                           507         [ #  # ]:              0 :         ereport(ERROR,
                                508                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                509                 :                :                  errmsg("input of anonymous composite types is not implemented")));
                                510                 :                : 
                                511                 :              0 :     tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
                                512                 :              0 :     ncolumns = tupdesc->natts;
                                513                 :                : 
                                514                 :                :     /*
                                515                 :                :      * We arrange to look up the needed I/O info just once per series of
                                516                 :                :      * calls, assuming the record type doesn't change underneath us.
                                517                 :                :      */
                                518                 :              0 :     my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
                                519         [ #  # ]:              0 :     if (my_extra == NULL ||
                                520         [ #  # ]:              0 :         my_extra->ncolumns != ncolumns)
                                521                 :                :     {
                                522                 :              0 :         fcinfo->flinfo->fn_extra =
                                523                 :              0 :             MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
                                524                 :                :                                offsetof(RecordIOData, columns) +
 3851                           525                 :              0 :                                ncolumns * sizeof(ColumnIOData));
 7762                           526                 :              0 :         my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
                                527                 :              0 :         my_extra->record_type = InvalidOid;
                                528                 :              0 :         my_extra->record_typmod = 0;
                                529                 :                :     }
                                530                 :                : 
                                531         [ #  # ]:              0 :     if (my_extra->record_type != tupType ||
                                532         [ #  # ]:              0 :         my_extra->record_typmod != tupTypmod)
                                533                 :                :     {
                                534   [ #  #  #  #  :              0 :         MemSet(my_extra, 0,
                                     #  #  #  #  #  
                                                 # ]
                                535                 :                :                offsetof(RecordIOData, columns) +
                                536                 :                :                ncolumns * sizeof(ColumnIOData));
                                537                 :              0 :         my_extra->record_type = tupType;
                                538                 :              0 :         my_extra->record_typmod = tupTypmod;
                                539                 :              0 :         my_extra->ncolumns = ncolumns;
                                540                 :                :     }
                                541                 :                : 
                                542                 :              0 :     values = (Datum *) palloc(ncolumns * sizeof(Datum));
 6152                           543                 :              0 :     nulls = (bool *) palloc(ncolumns * sizeof(bool));
                                544                 :                : 
                                545                 :                :     /* Fetch number of columns user thinks it has */
 7703                           546                 :              0 :     usercols = pq_getmsgint(buf, 4);
                                547                 :                : 
                                548                 :                :     /* Need to scan to count nondeleted columns */
                                549                 :              0 :     validcols = 0;
                                550         [ #  # ]:              0 :     for (i = 0; i < ncolumns; i++)
                                551                 :                :     {
 2939 andres@anarazel.de        552         [ #  # ]:              0 :         if (!TupleDescAttr(tupdesc, i)->attisdropped)
 7703 tgl@sss.pgh.pa.us         553                 :              0 :             validcols++;
                                554                 :                :     }
                                555         [ #  # ]:              0 :     if (usercols != validcols)
 7762                           556         [ #  # ]:              0 :         ereport(ERROR,
                                557                 :                :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
                                558                 :                :                  errmsg("wrong number of columns: %d, expected %d",
                                559                 :                :                         usercols, validcols)));
                                560                 :                : 
                                561                 :                :     /* Process each column */
                                562         [ #  # ]:              0 :     for (i = 0; i < ncolumns; i++)
                                563                 :                :     {
 2939 andres@anarazel.de        564                 :              0 :         Form_pg_attribute att = TupleDescAttr(tupdesc, i);
 7762 tgl@sss.pgh.pa.us         565                 :              0 :         ColumnIOData *column_info = &my_extra->columns[i];
 2939 andres@anarazel.de        566                 :              0 :         Oid         column_type = att->atttypid;
                                567                 :                :         Oid         coltypoid;
                                568                 :                :         int         itemlen;
                                569                 :                :         StringInfoData item_buf;
                                570                 :                :         StringInfo  bufptr;
                                571                 :                : 
                                572                 :                :         /* Ignore dropped columns in datatype, but fill with nulls */
                                573         [ #  # ]:              0 :         if (att->attisdropped)
                                574                 :                :         {
 7703 tgl@sss.pgh.pa.us         575                 :              0 :             values[i] = (Datum) 0;
 6152                           576                 :              0 :             nulls[i] = true;
 7703                           577                 :              0 :             continue;
                                578                 :                :         }
                                579                 :                : 
                                580                 :                :         /* Check column type recorded in the data */
 7762                           581                 :              0 :         coltypoid = pq_getmsgint(buf, sizeof(Oid));
                                582                 :                : 
                                583                 :                :         /*
                                584                 :                :          * From a security standpoint, it doesn't matter whether the input's
                                585                 :                :          * column type matches what we expect: the column type's receive
                                586                 :                :          * function has to be robust enough to cope with invalid data.
                                587                 :                :          * However, from a user-friendliness standpoint, it's nicer to
                                588                 :                :          * complain about type mismatches than to throw "improper binary
                                589                 :                :          * format" errors.  But there's a problem: only built-in types have
                                590                 :                :          * OIDs that are stable enough to believe that a mismatch is a real
                                591                 :                :          * issue.  So complain only if both OIDs are in the built-in range.
                                592                 :                :          * Otherwise, carry on with the column type we "should" be getting.
                                593                 :                :          */
 1873                           594   [ #  #  #  # ]:              0 :         if (coltypoid != column_type &&
                                595         [ #  # ]:              0 :             coltypoid < FirstGenbkiObjectId &&
                                596                 :                :             column_type < FirstGenbkiObjectId)
 7703                           597         [ #  # ]:              0 :             ereport(ERROR,
                                598                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                                599                 :                :                      errmsg("binary data has type %u (%s) instead of expected %u (%s) in record column %d",
                                600                 :                :                             coltypoid,
                                601                 :                :                             format_type_extended(coltypoid, -1,
                                602                 :                :                                                  FORMAT_TYPE_ALLOW_INVALID),
                                603                 :                :                             column_type,
                                604                 :                :                             format_type_extended(column_type, -1,
                                605                 :                :                                                  FORMAT_TYPE_ALLOW_INVALID),
                                606                 :                :                             i + 1)));
                                607                 :                : 
                                608                 :                :         /* Get and check the item length */
 7762                           609                 :              0 :         itemlen = pq_getmsgint(buf, 4);
                                610   [ #  #  #  # ]:              0 :         if (itemlen < -1 || itemlen > (buf->len - buf->cursor))
                                611         [ #  # ]:              0 :             ereport(ERROR,
                                612                 :                :                     (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
                                613                 :                :                      errmsg("insufficient data left in message")));
                                614                 :                : 
                                615         [ #  # ]:              0 :         if (itemlen == -1)
                                616                 :                :         {
                                617                 :                :             /* -1 length means NULL */
 7095                           618                 :              0 :             bufptr = NULL;
 6152                           619                 :              0 :             nulls[i] = true;
                                620                 :                :         }
                                621                 :                :         else
                                622                 :                :         {
                                623                 :                :             char       *strbuff;
                                624                 :                : 
                                625                 :                :             /*
                                626                 :                :              * Rather than copying data around, we just initialize a
                                627                 :                :              * StringInfo pointing to the correct portion of the message
                                628                 :                :              * buffer.
                                629                 :                :              */
  681 drowley@postgresql.o      630                 :              0 :             strbuff = &buf->data[buf->cursor];
 7762 tgl@sss.pgh.pa.us         631                 :              0 :             buf->cursor += itemlen;
  681 drowley@postgresql.o      632                 :              0 :             initReadOnlyStringInfo(&item_buf, strbuff, itemlen);
                                633                 :                : 
 7095 tgl@sss.pgh.pa.us         634                 :              0 :             bufptr = &item_buf;
 6152                           635                 :              0 :             nulls[i] = false;
                                636                 :                :         }
                                637                 :                : 
                                638                 :                :         /* Now call the column's receiveproc */
 7095                           639         [ #  # ]:              0 :         if (column_info->column_type != column_type)
                                640                 :                :         {
                                641                 :              0 :             getTypeBinaryInputInfo(column_type,
                                642                 :                :                                    &column_info->typiofunc,
                                643                 :                :                                    &column_info->typioparam);
                                644                 :              0 :             fmgr_info_cxt(column_info->typiofunc, &column_info->proc,
                                645                 :              0 :                           fcinfo->flinfo->fn_mcxt);
                                646                 :              0 :             column_info->column_type = column_type;
                                647                 :                :         }
                                648                 :                : 
                                649                 :              0 :         values[i] = ReceiveFunctionCall(&column_info->proc,
                                650                 :                :                                         bufptr,
                                651                 :                :                                         column_info->typioparam,
                                652                 :                :                                         att->atttypmod);
                                653                 :                : 
                                654         [ #  # ]:              0 :         if (bufptr)
                                655                 :                :         {
                                656                 :                :             /* Trouble if it didn't eat the whole buffer */
 7762                           657         [ #  # ]:              0 :             if (item_buf.cursor != itemlen)
                                658         [ #  # ]:              0 :                 ereport(ERROR,
                                659                 :                :                         (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
                                660                 :                :                          errmsg("improper binary format in record column %d",
                                661                 :                :                                 i + 1)));
                                662                 :                :         }
                                663                 :                :     }
                                664                 :                : 
 6152                           665                 :              0 :     tuple = heap_form_tuple(tupdesc, values, nulls);
                                666                 :                : 
                                667                 :                :     /*
                                668                 :                :      * We cannot return tuple->t_data because heap_form_tuple allocates it as
                                669                 :                :      * part of a larger chunk, and our caller may expect to be able to pfree
                                670                 :                :      * our result.  So must copy the info into a new palloc chunk.
                                671                 :                :      */
 7446                           672                 :              0 :     result = (HeapTupleHeader) palloc(tuple->t_len);
                                673                 :              0 :     memcpy(result, tuple->t_data, tuple->t_len);
                                674                 :                : 
                                675                 :              0 :     heap_freetuple(tuple);
 7762                           676                 :              0 :     pfree(values);
                                677                 :              0 :     pfree(nulls);
 7022                           678         [ #  # ]:              0 :     ReleaseTupleDesc(tupdesc);
                                679                 :                : 
 7446                           680                 :              0 :     PG_RETURN_HEAPTUPLEHEADER(result);
                                681                 :                : }
                                682                 :                : 
                                683                 :                : /*
                                684                 :                :  * record_send      - binary output routine for any composite type.
                                685                 :                :  */
                                686                 :                : Datum
 7828                           687                 :              0 : record_send(PG_FUNCTION_ARGS)
                                688                 :                : {
 7762                           689                 :              0 :     HeapTupleHeader rec = PG_GETARG_HEAPTUPLEHEADER(0);
                                690                 :                :     Oid         tupType;
                                691                 :                :     int32       tupTypmod;
                                692                 :                :     TupleDesc   tupdesc;
                                693                 :                :     HeapTupleData tuple;
                                694                 :                :     RecordIOData *my_extra;
                                695                 :                :     int         ncolumns;
                                696                 :                :     int         validcols;
                                697                 :                :     int         i;
                                698                 :                :     Datum      *values;
                                699                 :                :     bool       *nulls;
                                700                 :                :     StringInfoData buf;
                                701                 :                : 
 3624 noah@leadboat.com         702                 :              0 :     check_stack_depth();        /* recurses for record-type columns */
                                703                 :                : 
                                704                 :                :     /* Extract type info from the tuple itself */
 7434 tgl@sss.pgh.pa.us         705                 :              0 :     tupType = HeapTupleHeaderGetTypeId(rec);
                                706                 :              0 :     tupTypmod = HeapTupleHeaderGetTypMod(rec);
 7762                           707                 :              0 :     tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
                                708                 :              0 :     ncolumns = tupdesc->natts;
                                709                 :                : 
                                710                 :                :     /* Build a temporary HeapTuple control structure */
                                711                 :              0 :     tuple.t_len = HeapTupleHeaderGetDatumLength(rec);
                                712                 :              0 :     ItemPointerSetInvalid(&(tuple.t_self));
                                713                 :              0 :     tuple.t_tableOid = InvalidOid;
                                714                 :              0 :     tuple.t_data = rec;
                                715                 :                : 
                                716                 :                :     /*
                                717                 :                :      * We arrange to look up the needed I/O info just once per series of
                                718                 :                :      * calls, assuming the record type doesn't change underneath us.
                                719                 :                :      */
                                720                 :              0 :     my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
                                721         [ #  # ]:              0 :     if (my_extra == NULL ||
                                722         [ #  # ]:              0 :         my_extra->ncolumns != ncolumns)
                                723                 :                :     {
                                724                 :              0 :         fcinfo->flinfo->fn_extra =
                                725                 :              0 :             MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
                                726                 :                :                                offsetof(RecordIOData, columns) +
 3851                           727                 :              0 :                                ncolumns * sizeof(ColumnIOData));
 7762                           728                 :              0 :         my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
                                729                 :              0 :         my_extra->record_type = InvalidOid;
                                730                 :              0 :         my_extra->record_typmod = 0;
                                731                 :                :     }
                                732                 :                : 
                                733         [ #  # ]:              0 :     if (my_extra->record_type != tupType ||
                                734         [ #  # ]:              0 :         my_extra->record_typmod != tupTypmod)
                                735                 :                :     {
                                736   [ #  #  #  #  :              0 :         MemSet(my_extra, 0,
                                     #  #  #  #  #  
                                                 # ]
                                737                 :                :                offsetof(RecordIOData, columns) +
                                738                 :                :                ncolumns * sizeof(ColumnIOData));
                                739                 :              0 :         my_extra->record_type = tupType;
                                740                 :              0 :         my_extra->record_typmod = tupTypmod;
                                741                 :              0 :         my_extra->ncolumns = ncolumns;
                                742                 :                :     }
                                743                 :                : 
                                744                 :              0 :     values = (Datum *) palloc(ncolumns * sizeof(Datum));
 6152                           745                 :              0 :     nulls = (bool *) palloc(ncolumns * sizeof(bool));
                                746                 :                : 
                                747                 :                :     /* Break down the tuple into fields */
                                748                 :              0 :     heap_deform_tuple(&tuple, tupdesc, values, nulls);
                                749                 :                : 
                                750                 :                :     /* And build the result string */
 7762                           751                 :              0 :     pq_begintypsend(&buf);
                                752                 :                : 
                                753                 :                :     /* Need to scan to count nondeleted columns */
 7703                           754                 :              0 :     validcols = 0;
                                755         [ #  # ]:              0 :     for (i = 0; i < ncolumns; i++)
                                756                 :                :     {
 2939 andres@anarazel.de        757         [ #  # ]:              0 :         if (!TupleDescAttr(tupdesc, i)->attisdropped)
 7703 tgl@sss.pgh.pa.us         758                 :              0 :             validcols++;
                                759                 :                :     }
 2887 andres@anarazel.de        760                 :              0 :     pq_sendint32(&buf, validcols);
                                761                 :                : 
 7762 tgl@sss.pgh.pa.us         762         [ #  # ]:              0 :     for (i = 0; i < ncolumns; i++)
                                763                 :                :     {
 2939 andres@anarazel.de        764                 :              0 :         Form_pg_attribute att = TupleDescAttr(tupdesc, i);
 7762 tgl@sss.pgh.pa.us         765                 :              0 :         ColumnIOData *column_info = &my_extra->columns[i];
 2939 andres@anarazel.de        766                 :              0 :         Oid         column_type = att->atttypid;
                                767                 :                :         Datum       attr;
                                768                 :                :         bytea      *outputbytes;
                                769                 :                : 
                                770                 :                :         /* Ignore dropped columns in datatype */
                                771         [ #  # ]:              0 :         if (att->attisdropped)
 7703 tgl@sss.pgh.pa.us         772                 :              0 :             continue;
                                773                 :                : 
 2887 andres@anarazel.de        774                 :              0 :         pq_sendint32(&buf, column_type);
                                775                 :                : 
 6152 tgl@sss.pgh.pa.us         776         [ #  # ]:              0 :         if (nulls[i])
                                777                 :                :         {
                                778                 :                :             /* emit -1 data length to signify a NULL */
 2887 andres@anarazel.de        779                 :              0 :             pq_sendint32(&buf, -1);
 7762 tgl@sss.pgh.pa.us         780                 :              0 :             continue;
                                781                 :                :         }
                                782                 :                : 
                                783                 :                :         /*
                                784                 :                :          * Convert the column value to binary
                                785                 :                :          */
                                786         [ #  # ]:              0 :         if (column_info->column_type != column_type)
                                787                 :                :         {
                                788                 :              0 :             getTypeBinaryOutputInfo(column_type,
                                789                 :                :                                     &column_info->typiofunc,
                                790                 :                :                                     &column_info->typisvarlena);
                                791                 :              0 :             fmgr_info_cxt(column_info->typiofunc, &column_info->proc,
                                792                 :              0 :                           fcinfo->flinfo->fn_mcxt);
                                793                 :              0 :             column_info->column_type = column_type;
                                794                 :                :         }
                                795                 :                : 
 4325                           796                 :              0 :         attr = values[i];
 4680                           797                 :              0 :         outputbytes = SendFunctionCall(&column_info->proc, attr);
 2887 andres@anarazel.de        798                 :              0 :         pq_sendint32(&buf, VARSIZE(outputbytes) - VARHDRSZ);
 7762 tgl@sss.pgh.pa.us         799                 :              0 :         pq_sendbytes(&buf, VARDATA(outputbytes),
                                800                 :              0 :                      VARSIZE(outputbytes) - VARHDRSZ);
                                801                 :                :     }
                                802                 :                : 
                                803                 :              0 :     pfree(values);
                                804                 :              0 :     pfree(nulls);
 7022                           805         [ #  # ]:              0 :     ReleaseTupleDesc(tupdesc);
                                806                 :                : 
 7762                           807                 :              0 :     PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
                                808                 :                : }
                                809                 :                : 
                                810                 :                : 
                                811                 :                : /*
                                812                 :                :  * record_cmp()
                                813                 :                :  * Internal comparison function for records.
                                814                 :                :  *
                                815                 :                :  * Returns -1, 0 or 1
                                816                 :                :  *
                                817                 :                :  * Do not assume that the two inputs are exactly the same record type;
                                818                 :                :  * for instance we might be comparing an anonymous ROW() construct against a
                                819                 :                :  * named composite type.  We will compare as long as they have the same number
                                820                 :                :  * of non-dropped columns of the same types.
                                821                 :                :  */
                                822                 :                : static int
 6172 tgl@sss.pgh.pa.us         823                 :CBC        2332 : record_cmp(FunctionCallInfo fcinfo)
                                824                 :                : {
                                825                 :           2332 :     HeapTupleHeader record1 = PG_GETARG_HEAPTUPLEHEADER(0);
                                826                 :           2332 :     HeapTupleHeader record2 = PG_GETARG_HEAPTUPLEHEADER(1);
                                827                 :           2332 :     int         result = 0;
                                828                 :                :     Oid         tupType1;
                                829                 :                :     Oid         tupType2;
                                830                 :                :     int32       tupTypmod1;
                                831                 :                :     int32       tupTypmod2;
                                832                 :                :     TupleDesc   tupdesc1;
                                833                 :                :     TupleDesc   tupdesc2;
                                834                 :                :     HeapTupleData tuple1;
                                835                 :                :     HeapTupleData tuple2;
                                836                 :                :     int         ncolumns1;
                                837                 :                :     int         ncolumns2;
                                838                 :                :     RecordCompareData *my_extra;
                                839                 :                :     int         ncols;
                                840                 :                :     Datum      *values1;
                                841                 :                :     Datum      *values2;
                                842                 :                :     bool       *nulls1;
                                843                 :                :     bool       *nulls2;
                                844                 :                :     int         i1;
                                845                 :                :     int         i2;
                                846                 :                :     int         j;
                                847                 :                : 
 3624 noah@leadboat.com         848                 :           2332 :     check_stack_depth();        /* recurses for record-type columns */
                                849                 :                : 
                                850                 :                :     /* Extract type info from the tuples */
 6172 tgl@sss.pgh.pa.us         851                 :           2332 :     tupType1 = HeapTupleHeaderGetTypeId(record1);
                                852                 :           2332 :     tupTypmod1 = HeapTupleHeaderGetTypMod(record1);
                                853                 :           2332 :     tupdesc1 = lookup_rowtype_tupdesc(tupType1, tupTypmod1);
                                854                 :           2332 :     ncolumns1 = tupdesc1->natts;
                                855                 :           2332 :     tupType2 = HeapTupleHeaderGetTypeId(record2);
                                856                 :           2332 :     tupTypmod2 = HeapTupleHeaderGetTypMod(record2);
                                857                 :           2332 :     tupdesc2 = lookup_rowtype_tupdesc(tupType2, tupTypmod2);
                                858                 :           2332 :     ncolumns2 = tupdesc2->natts;
                                859                 :                : 
                                860                 :                :     /* Build temporary HeapTuple control structures */
                                861                 :           2332 :     tuple1.t_len = HeapTupleHeaderGetDatumLength(record1);
                                862                 :           2332 :     ItemPointerSetInvalid(&(tuple1.t_self));
                                863                 :           2332 :     tuple1.t_tableOid = InvalidOid;
                                864                 :           2332 :     tuple1.t_data = record1;
                                865                 :           2332 :     tuple2.t_len = HeapTupleHeaderGetDatumLength(record2);
                                866                 :           2332 :     ItemPointerSetInvalid(&(tuple2.t_self));
                                867                 :           2332 :     tuple2.t_tableOid = InvalidOid;
                                868                 :           2332 :     tuple2.t_data = record2;
                                869                 :                : 
                                870                 :                :     /*
                                871                 :                :      * We arrange to look up the needed comparison info just once per series
                                872                 :                :      * of calls, assuming the record types don't change underneath us.
                                873                 :                :      */
                                874                 :           2332 :     ncols = Max(ncolumns1, ncolumns2);
                                875                 :           2332 :     my_extra = (RecordCompareData *) fcinfo->flinfo->fn_extra;
                                876         [ +  + ]:           2332 :     if (my_extra == NULL ||
                                877         [ -  + ]:           2078 :         my_extra->ncolumns < ncols)
                                878                 :                :     {
                                879                 :            508 :         fcinfo->flinfo->fn_extra =
                                880                 :            254 :             MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
 3851                           881                 :            254 :                                offsetof(RecordCompareData, columns) +
                                882                 :                :                                ncols * sizeof(ColumnCompareData));
 6172                           883                 :            254 :         my_extra = (RecordCompareData *) fcinfo->flinfo->fn_extra;
                                884                 :            254 :         my_extra->ncolumns = ncols;
                                885                 :            254 :         my_extra->record1_type = InvalidOid;
                                886                 :            254 :         my_extra->record1_typmod = 0;
                                887                 :            254 :         my_extra->record2_type = InvalidOid;
                                888                 :            254 :         my_extra->record2_typmod = 0;
                                889                 :                :     }
                                890                 :                : 
                                891         [ +  + ]:           2332 :     if (my_extra->record1_type != tupType1 ||
                                892         [ +  + ]:           2078 :         my_extra->record1_typmod != tupTypmod1 ||
                                893         [ +  - ]:           2075 :         my_extra->record2_type != tupType2 ||
                                894         [ -  + ]:           2075 :         my_extra->record2_typmod != tupTypmod2)
                                895                 :                :     {
                                896   [ +  -  +  -  :            943 :         MemSet(my_extra->columns, 0, ncols * sizeof(ColumnCompareData));
                                     +  -  +  -  +  
                                                 + ]
                                897                 :            257 :         my_extra->record1_type = tupType1;
                                898                 :            257 :         my_extra->record1_typmod = tupTypmod1;
                                899                 :            257 :         my_extra->record2_type = tupType2;
                                900                 :            257 :         my_extra->record2_typmod = tupTypmod2;
                                901                 :                :     }
                                902                 :                : 
                                903                 :                :     /* Break down the tuples into fields */
                                904                 :           2332 :     values1 = (Datum *) palloc(ncolumns1 * sizeof(Datum));
                                905                 :           2332 :     nulls1 = (bool *) palloc(ncolumns1 * sizeof(bool));
                                906                 :           2332 :     heap_deform_tuple(&tuple1, tupdesc1, values1, nulls1);
                                907                 :           2332 :     values2 = (Datum *) palloc(ncolumns2 * sizeof(Datum));
                                908                 :           2332 :     nulls2 = (bool *) palloc(ncolumns2 * sizeof(bool));
                                909                 :           2332 :     heap_deform_tuple(&tuple2, tupdesc2, values2, nulls2);
                                910                 :                : 
                                911                 :                :     /*
                                912                 :                :      * Scan corresponding columns, allowing for dropped columns in different
                                913                 :                :      * places in the two rows.  i1 and i2 are physical column indexes, j is
                                914                 :                :      * the logical column index.
                                915                 :                :      */
                                916                 :           2332 :     i1 = i2 = j = 0;
                                917   [ +  +  -  + ]:           3548 :     while (i1 < ncolumns1 || i2 < ncolumns2)
                                918                 :                :     {
                                919                 :                :         Form_pg_attribute att1;
                                920                 :                :         Form_pg_attribute att2;
                                921                 :                :         TypeCacheEntry *typentry;
                                922                 :                :         Oid         collation;
                                923                 :                : 
                                924                 :                :         /*
                                925                 :                :          * Skip dropped columns
                                926                 :                :          */
 2939 andres@anarazel.de        927   [ +  -  -  + ]:           3151 :         if (i1 < ncolumns1 && TupleDescAttr(tupdesc1, i1)->attisdropped)
                                928                 :                :         {
 6172 tgl@sss.pgh.pa.us         929                 :UBC           0 :             i1++;
                                930                 :              0 :             continue;
                                931                 :                :         }
 2939 andres@anarazel.de        932   [ +  +  -  + ]:CBC        3151 :         if (i2 < ncolumns2 && TupleDescAttr(tupdesc2, i2)->attisdropped)
                                933                 :                :         {
 6172 tgl@sss.pgh.pa.us         934                 :UBC           0 :             i2++;
                                935                 :              0 :             continue;
                                936                 :                :         }
 6172 tgl@sss.pgh.pa.us         937   [ +  -  +  + ]:CBC        3151 :         if (i1 >= ncolumns1 || i2 >= ncolumns2)
                                938                 :                :             break;              /* we'll deal with mismatch below loop */
                                939                 :                : 
 2939 andres@anarazel.de        940                 :           3148 :         att1 = TupleDescAttr(tupdesc1, i1);
                                941                 :           3148 :         att2 = TupleDescAttr(tupdesc2, i2);
                                942                 :                : 
                                943                 :                :         /*
                                944                 :                :          * Have two matching columns, they must be same type
                                945                 :                :          */
                                946         [ +  + ]:           3148 :         if (att1->atttypid != att2->atttypid)
 6172 tgl@sss.pgh.pa.us         947         [ +  - ]:              3 :             ereport(ERROR,
                                948                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                                949                 :                :                      errmsg("cannot compare dissimilar column types %s and %s at record column %d",
                                950                 :                :                             format_type_be(att1->atttypid),
                                951                 :                :                             format_type_be(att2->atttypid),
                                952                 :                :                             j + 1)));
                                953                 :                : 
                                954                 :                :         /*
                                955                 :                :          * If they're not same collation, we don't complain here, but the
                                956                 :                :          * comparison function might.
                                957                 :                :          */
 2939 andres@anarazel.de        958                 :           3145 :         collation = att1->attcollation;
                                959         [ -  + ]:           3145 :         if (collation != att2->attcollation)
 5261 tgl@sss.pgh.pa.us         960                 :UBC           0 :             collation = InvalidOid;
                                961                 :                : 
                                962                 :                :         /*
                                963                 :                :          * Lookup the comparison function if not done already
                                964                 :                :          */
 6172 tgl@sss.pgh.pa.us         965                 :CBC        3145 :         typentry = my_extra->columns[j].typentry;
                                966         [ +  + ]:           3145 :         if (typentry == NULL ||
 2939 andres@anarazel.de        967         [ -  + ]:           2685 :             typentry->type_id != att1->atttypid)
                                968                 :                :         {
                                969                 :            460 :             typentry = lookup_type_cache(att1->atttypid,
                                970                 :                :                                          TYPECACHE_CMP_PROC_FINFO);
 6172 tgl@sss.pgh.pa.us         971         [ +  + ]:            460 :             if (!OidIsValid(typentry->cmp_proc_finfo.fn_oid))
                                972         [ +  - ]:              3 :                 ereport(ERROR,
                                973                 :                :                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
                                974                 :                :                          errmsg("could not identify a comparison function for type %s",
                                975                 :                :                                 format_type_be(typentry->type_id))));
                                976                 :            457 :             my_extra->columns[j].typentry = typentry;
                                977                 :                :         }
                                978                 :                : 
                                979                 :                :         /*
                                980                 :                :          * We consider two NULLs equal; NULL > not-NULL.
                                981                 :                :          */
                                982   [ +  +  +  + ]:           3142 :         if (!nulls1[i1] || !nulls2[i2])
                                983                 :                :         {
 2415 andres@anarazel.de        984                 :           3137 :             LOCAL_FCINFO(locfcinfo, 2);
                                985                 :                :             int32       cmpresult;
                                986                 :                : 
 6172 tgl@sss.pgh.pa.us         987         [ +  + ]:           3137 :             if (nulls1[i1])
                                988                 :                :             {
                                989                 :                :                 /* arg1 is greater than arg2 */
                                990                 :             12 :                 result = 1;
                                991                 :           1926 :                 break;
                                992                 :                :             }
                                993         [ -  + ]:           3125 :             if (nulls2[i2])
                                994                 :                :             {
                                995                 :                :                 /* arg1 is less than arg2 */
 6172 tgl@sss.pgh.pa.us         996                 :UBC           0 :                 result = -1;
                                997                 :              0 :                 break;
                                998                 :                :             }
                                999                 :                : 
                               1000                 :                :             /* Compare the pair of elements */
 2415 andres@anarazel.de       1001                 :CBC        3125 :             InitFunctionCallInfoData(*locfcinfo, &typentry->cmp_proc_finfo, 2,
                               1002                 :                :                                      collation, NULL, NULL);
                               1003                 :           3125 :             locfcinfo->args[0].value = values1[i1];
                               1004                 :           3125 :             locfcinfo->args[0].isnull = false;
                               1005                 :           3125 :             locfcinfo->args[1].value = values2[i2];
                               1006                 :           3125 :             locfcinfo->args[1].isnull = false;
                               1007                 :           3125 :             cmpresult = DatumGetInt32(FunctionCallInvoke(locfcinfo));
                               1008                 :                : 
                               1009                 :                :             /* We don't expect comparison support functions to return null */
 1964 tgl@sss.pgh.pa.us        1010         [ -  + ]:           3125 :             Assert(!locfcinfo->isnull);
                               1011                 :                : 
 6172                          1012         [ +  + ]:           3125 :             if (cmpresult < 0)
                               1013                 :                :             {
                               1014                 :                :                 /* arg1 is less than arg2 */
                               1015                 :            997 :                 result = -1;
                               1016                 :            997 :                 break;
                               1017                 :                :             }
                               1018         [ +  + ]:           2128 :             else if (cmpresult > 0)
                               1019                 :                :             {
                               1020                 :                :                 /* arg1 is greater than arg2 */
                               1021                 :            917 :                 result = 1;
                               1022                 :            917 :                 break;
                               1023                 :                :             }
                               1024                 :                :         }
                               1025                 :                : 
                               1026                 :                :         /* equal, so continue to next column */
                               1027                 :           1216 :         i1++, i2++, j++;
                               1028                 :                :     }
                               1029                 :                : 
                               1030                 :                :     /*
                               1031                 :                :      * If we didn't break out of the loop early, check for column count
                               1032                 :                :      * mismatch.  (We do not report such mismatch if we found unequal column
                               1033                 :                :      * values; is that a feature or a bug?)
                               1034                 :                :      */
                               1035         [ +  + ]:           2326 :     if (result == 0)
                               1036                 :                :     {
                               1037   [ +  +  -  + ]:            400 :         if (i1 != ncolumns1 || i2 != ncolumns2)
                               1038         [ +  - ]:              3 :             ereport(ERROR,
                               1039                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               1040                 :                :                      errmsg("cannot compare record types with different numbers of columns")));
                               1041                 :                :     }
                               1042                 :                : 
                               1043                 :           2323 :     pfree(values1);
                               1044                 :           2323 :     pfree(nulls1);
                               1045                 :           2323 :     pfree(values2);
                               1046                 :           2323 :     pfree(nulls2);
                               1047         [ +  - ]:           2323 :     ReleaseTupleDesc(tupdesc1);
                               1048         [ +  - ]:           2323 :     ReleaseTupleDesc(tupdesc2);
                               1049                 :                : 
                               1050                 :                :     /* Avoid leaking memory when handed toasted input. */
                               1051         [ +  + ]:           2323 :     PG_FREE_IF_COPY(record1, 0);
                               1052         [ +  + ]:           2323 :     PG_FREE_IF_COPY(record2, 1);
                               1053                 :                : 
                               1054                 :           2323 :     return result;
                               1055                 :                : }
                               1056                 :                : 
                               1057                 :                : /*
                               1058                 :                :  * record_eq :
                               1059                 :                :  *        compares two records for equality
                               1060                 :                :  * result :
                               1061                 :                :  *        returns true if the records are equal, false otherwise.
                               1062                 :                :  *
                               1063                 :                :  * Note: we do not use record_cmp here, since equality may be meaningful in
                               1064                 :                :  * datatypes that don't have a total ordering (and hence no btree support).
                               1065                 :                :  */
                               1066                 :                : Datum
                               1067                 :           1809 : record_eq(PG_FUNCTION_ARGS)
                               1068                 :                : {
                               1069                 :           1809 :     HeapTupleHeader record1 = PG_GETARG_HEAPTUPLEHEADER(0);
                               1070                 :           1809 :     HeapTupleHeader record2 = PG_GETARG_HEAPTUPLEHEADER(1);
                               1071                 :           1809 :     bool        result = true;
                               1072                 :                :     Oid         tupType1;
                               1073                 :                :     Oid         tupType2;
                               1074                 :                :     int32       tupTypmod1;
                               1075                 :                :     int32       tupTypmod2;
                               1076                 :                :     TupleDesc   tupdesc1;
                               1077                 :                :     TupleDesc   tupdesc2;
                               1078                 :                :     HeapTupleData tuple1;
                               1079                 :                :     HeapTupleData tuple2;
                               1080                 :                :     int         ncolumns1;
                               1081                 :                :     int         ncolumns2;
                               1082                 :                :     RecordCompareData *my_extra;
                               1083                 :                :     int         ncols;
                               1084                 :                :     Datum      *values1;
                               1085                 :                :     Datum      *values2;
                               1086                 :                :     bool       *nulls1;
                               1087                 :                :     bool       *nulls2;
                               1088                 :                :     int         i1;
                               1089                 :                :     int         i2;
                               1090                 :                :     int         j;
                               1091                 :                : 
 3624 noah@leadboat.com        1092                 :           1809 :     check_stack_depth();        /* recurses for record-type columns */
                               1093                 :                : 
                               1094                 :                :     /* Extract type info from the tuples */
 6172 tgl@sss.pgh.pa.us        1095                 :           1809 :     tupType1 = HeapTupleHeaderGetTypeId(record1);
                               1096                 :           1809 :     tupTypmod1 = HeapTupleHeaderGetTypMod(record1);
                               1097                 :           1809 :     tupdesc1 = lookup_rowtype_tupdesc(tupType1, tupTypmod1);
                               1098                 :           1809 :     ncolumns1 = tupdesc1->natts;
                               1099                 :           1809 :     tupType2 = HeapTupleHeaderGetTypeId(record2);
                               1100                 :           1809 :     tupTypmod2 = HeapTupleHeaderGetTypMod(record2);
                               1101                 :           1809 :     tupdesc2 = lookup_rowtype_tupdesc(tupType2, tupTypmod2);
                               1102                 :           1809 :     ncolumns2 = tupdesc2->natts;
                               1103                 :                : 
                               1104                 :                :     /* Build temporary HeapTuple control structures */
                               1105                 :           1809 :     tuple1.t_len = HeapTupleHeaderGetDatumLength(record1);
                               1106                 :           1809 :     ItemPointerSetInvalid(&(tuple1.t_self));
                               1107                 :           1809 :     tuple1.t_tableOid = InvalidOid;
                               1108                 :           1809 :     tuple1.t_data = record1;
                               1109                 :           1809 :     tuple2.t_len = HeapTupleHeaderGetDatumLength(record2);
                               1110                 :           1809 :     ItemPointerSetInvalid(&(tuple2.t_self));
                               1111                 :           1809 :     tuple2.t_tableOid = InvalidOid;
                               1112                 :           1809 :     tuple2.t_data = record2;
                               1113                 :                : 
                               1114                 :                :     /*
                               1115                 :                :      * We arrange to look up the needed comparison info just once per series
                               1116                 :                :      * of calls, assuming the record types don't change underneath us.
                               1117                 :                :      */
                               1118                 :           1809 :     ncols = Max(ncolumns1, ncolumns2);
                               1119                 :           1809 :     my_extra = (RecordCompareData *) fcinfo->flinfo->fn_extra;
                               1120         [ +  + ]:           1809 :     if (my_extra == NULL ||
                               1121         [ -  + ]:           1636 :         my_extra->ncolumns < ncols)
                               1122                 :                :     {
                               1123                 :            346 :         fcinfo->flinfo->fn_extra =
                               1124                 :            173 :             MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
 3851                          1125                 :            173 :                                offsetof(RecordCompareData, columns) +
                               1126                 :                :                                ncols * sizeof(ColumnCompareData));
 6172                          1127                 :            173 :         my_extra = (RecordCompareData *) fcinfo->flinfo->fn_extra;
                               1128                 :            173 :         my_extra->ncolumns = ncols;
                               1129                 :            173 :         my_extra->record1_type = InvalidOid;
                               1130                 :            173 :         my_extra->record1_typmod = 0;
                               1131                 :            173 :         my_extra->record2_type = InvalidOid;
                               1132                 :            173 :         my_extra->record2_typmod = 0;
                               1133                 :                :     }
                               1134                 :                : 
                               1135         [ +  + ]:           1809 :     if (my_extra->record1_type != tupType1 ||
                               1136         [ +  - ]:           1636 :         my_extra->record1_typmod != tupTypmod1 ||
                               1137         [ +  - ]:           1636 :         my_extra->record2_type != tupType2 ||
                               1138         [ -  + ]:           1636 :         my_extra->record2_typmod != tupTypmod2)
                               1139                 :                :     {
                               1140   [ +  -  +  -  :            538 :         MemSet(my_extra->columns, 0, ncols * sizeof(ColumnCompareData));
                                     +  -  +  -  +  
                                                 + ]
                               1141                 :            173 :         my_extra->record1_type = tupType1;
                               1142                 :            173 :         my_extra->record1_typmod = tupTypmod1;
                               1143                 :            173 :         my_extra->record2_type = tupType2;
                               1144                 :            173 :         my_extra->record2_typmod = tupTypmod2;
                               1145                 :                :     }
                               1146                 :                : 
                               1147                 :                :     /* Break down the tuples into fields */
                               1148                 :           1809 :     values1 = (Datum *) palloc(ncolumns1 * sizeof(Datum));
                               1149                 :           1809 :     nulls1 = (bool *) palloc(ncolumns1 * sizeof(bool));
                               1150                 :           1809 :     heap_deform_tuple(&tuple1, tupdesc1, values1, nulls1);
                               1151                 :           1809 :     values2 = (Datum *) palloc(ncolumns2 * sizeof(Datum));
                               1152                 :           1809 :     nulls2 = (bool *) palloc(ncolumns2 * sizeof(bool));
                               1153                 :           1809 :     heap_deform_tuple(&tuple2, tupdesc2, values2, nulls2);
                               1154                 :                : 
                               1155                 :                :     /*
                               1156                 :                :      * Scan corresponding columns, allowing for dropped columns in different
                               1157                 :                :      * places in the two rows.  i1 and i2 are physical column indexes, j is
                               1158                 :                :      * the logical column index.
                               1159                 :                :      */
                               1160                 :           1809 :     i1 = i2 = j = 0;
                               1161   [ +  +  -  + ]:           2950 :     while (i1 < ncolumns1 || i2 < ncolumns2)
                               1162                 :                :     {
 2415 andres@anarazel.de       1163                 :           2593 :         LOCAL_FCINFO(locfcinfo, 2);
                               1164                 :                :         Form_pg_attribute att1;
                               1165                 :                :         Form_pg_attribute att2;
                               1166                 :                :         TypeCacheEntry *typentry;
                               1167                 :                :         Oid         collation;
                               1168                 :                :         bool        oprresult;
                               1169                 :                : 
                               1170                 :                :         /*
                               1171                 :                :          * Skip dropped columns
                               1172                 :                :          */
 2939                          1173   [ +  -  -  + ]:           2593 :         if (i1 < ncolumns1 && TupleDescAttr(tupdesc1, i1)->attisdropped)
                               1174                 :                :         {
 6172 tgl@sss.pgh.pa.us        1175                 :UBC           0 :             i1++;
                               1176                 :              0 :             continue;
                               1177                 :                :         }
 2939 andres@anarazel.de       1178   [ +  +  -  + ]:CBC        2593 :         if (i2 < ncolumns2 && TupleDescAttr(tupdesc2, i2)->attisdropped)
                               1179                 :                :         {
 6172 tgl@sss.pgh.pa.us        1180                 :UBC           0 :             i2++;
                               1181                 :              0 :             continue;
                               1182                 :                :         }
 6172 tgl@sss.pgh.pa.us        1183   [ +  -  +  + ]:CBC        2593 :         if (i1 >= ncolumns1 || i2 >= ncolumns2)
                               1184                 :                :             break;              /* we'll deal with mismatch below loop */
                               1185                 :                : 
 2939 andres@anarazel.de       1186                 :           2590 :         att1 = TupleDescAttr(tupdesc1, i1);
                               1187                 :           2590 :         att2 = TupleDescAttr(tupdesc2, i2);
                               1188                 :                : 
                               1189                 :                :         /*
                               1190                 :                :          * Have two matching columns, they must be same type
                               1191                 :                :          */
                               1192         [ +  + ]:           2590 :         if (att1->atttypid != att2->atttypid)
 6172 tgl@sss.pgh.pa.us        1193         [ +  - ]:              6 :             ereport(ERROR,
                               1194                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               1195                 :                :                      errmsg("cannot compare dissimilar column types %s and %s at record column %d",
                               1196                 :                :                             format_type_be(att1->atttypid),
                               1197                 :                :                             format_type_be(att2->atttypid),
                               1198                 :                :                             j + 1)));
                               1199                 :                : 
                               1200                 :                :         /*
                               1201                 :                :          * If they're not same collation, we don't complain here, but the
                               1202                 :                :          * equality function might.
                               1203                 :                :          */
 2939 andres@anarazel.de       1204                 :           2584 :         collation = att1->attcollation;
                               1205         [ -  + ]:           2584 :         if (collation != att2->attcollation)
 5261 tgl@sss.pgh.pa.us        1206                 :UBC           0 :             collation = InvalidOid;
                               1207                 :                : 
                               1208                 :                :         /*
                               1209                 :                :          * Lookup the equality function if not done already
                               1210                 :                :          */
 6172 tgl@sss.pgh.pa.us        1211                 :CBC        2584 :         typentry = my_extra->columns[j].typentry;
                               1212         [ +  + ]:           2584 :         if (typentry == NULL ||
 2939 andres@anarazel.de       1213         [ -  + ]:           2240 :             typentry->type_id != att1->atttypid)
                               1214                 :                :         {
                               1215                 :            344 :             typentry = lookup_type_cache(att1->atttypid,
                               1216                 :                :                                          TYPECACHE_EQ_OPR_FINFO);
 6172 tgl@sss.pgh.pa.us        1217         [ +  + ]:            344 :             if (!OidIsValid(typentry->eq_opr_finfo.fn_oid))
                               1218         [ +  - ]:              3 :                 ereport(ERROR,
                               1219                 :                :                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
                               1220                 :                :                          errmsg("could not identify an equality operator for type %s",
                               1221                 :                :                                 format_type_be(typentry->type_id))));
                               1222                 :            341 :             my_extra->columns[j].typentry = typentry;
                               1223                 :                :         }
                               1224                 :                : 
                               1225                 :                :         /*
                               1226                 :                :          * We consider two NULLs equal; NULL > not-NULL.
                               1227                 :                :          */
                               1228   [ +  +  +  + ]:           2581 :         if (!nulls1[i1] || !nulls2[i2])
                               1229                 :                :         {
                               1230   [ +  +  -  + ]:           2456 :             if (nulls1[i1] || nulls2[i2])
                               1231                 :                :             {
                               1232                 :              3 :                 result = false;
                               1233                 :              3 :                 break;
                               1234                 :                :             }
                               1235                 :                : 
                               1236                 :                :             /* Compare the pair of elements */
 2415 andres@anarazel.de       1237                 :           2453 :             InitFunctionCallInfoData(*locfcinfo, &typentry->eq_opr_finfo, 2,
                               1238                 :                :                                      collation, NULL, NULL);
                               1239                 :           2453 :             locfcinfo->args[0].value = values1[i1];
                               1240                 :           2453 :             locfcinfo->args[0].isnull = false;
                               1241                 :           2453 :             locfcinfo->args[1].value = values2[i2];
                               1242                 :           2453 :             locfcinfo->args[1].isnull = false;
                               1243                 :           2453 :             oprresult = DatumGetBool(FunctionCallInvoke(locfcinfo));
 1964 tgl@sss.pgh.pa.us        1244   [ +  -  +  + ]:           2453 :             if (locfcinfo->isnull || !oprresult)
                               1245                 :                :             {
 6172                          1246                 :           1437 :                 result = false;
                               1247                 :           1437 :                 break;
                               1248                 :                :             }
                               1249                 :                :         }
                               1250                 :                : 
                               1251                 :                :         /* equal, so continue to next column */
                               1252                 :           1141 :         i1++, i2++, j++;
                               1253                 :                :     }
                               1254                 :                : 
                               1255                 :                :     /*
                               1256                 :                :      * If we didn't break out of the loop early, check for column count
                               1257                 :                :      * mismatch.  (We do not report such mismatch if we found unequal column
                               1258                 :                :      * values; is that a feature or a bug?)
                               1259                 :                :      */
                               1260         [ +  + ]:           1800 :     if (result)
                               1261                 :                :     {
                               1262   [ +  +  -  + ]:            360 :         if (i1 != ncolumns1 || i2 != ncolumns2)
                               1263         [ +  - ]:              3 :             ereport(ERROR,
                               1264                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               1265                 :                :                      errmsg("cannot compare record types with different numbers of columns")));
                               1266                 :                :     }
                               1267                 :                : 
                               1268                 :           1797 :     pfree(values1);
                               1269                 :           1797 :     pfree(nulls1);
                               1270                 :           1797 :     pfree(values2);
                               1271                 :           1797 :     pfree(nulls2);
                               1272         [ +  - ]:           1797 :     ReleaseTupleDesc(tupdesc1);
                               1273         [ +  - ]:           1797 :     ReleaseTupleDesc(tupdesc2);
                               1274                 :                : 
                               1275                 :                :     /* Avoid leaking memory when handed toasted input. */
                               1276         [ +  + ]:           1797 :     PG_FREE_IF_COPY(record1, 0);
                               1277         [ +  + ]:           1797 :     PG_FREE_IF_COPY(record2, 1);
                               1278                 :                : 
                               1279                 :           1797 :     PG_RETURN_BOOL(result);
                               1280                 :                : }
                               1281                 :                : 
                               1282                 :                : Datum
                               1283                 :             27 : record_ne(PG_FUNCTION_ARGS)
                               1284                 :                : {
                               1285                 :             27 :     PG_RETURN_BOOL(!DatumGetBool(record_eq(fcinfo)));
                               1286                 :                : }
                               1287                 :                : 
                               1288                 :                : Datum
                               1289                 :             18 : record_lt(PG_FUNCTION_ARGS)
                               1290                 :                : {
                               1291                 :             18 :     PG_RETURN_BOOL(record_cmp(fcinfo) < 0);
                               1292                 :                : }
                               1293                 :                : 
                               1294                 :                : Datum
                               1295                 :              6 : record_gt(PG_FUNCTION_ARGS)
                               1296                 :                : {
                               1297                 :              6 :     PG_RETURN_BOOL(record_cmp(fcinfo) > 0);
                               1298                 :                : }
                               1299                 :                : 
                               1300                 :                : Datum
                               1301                 :              6 : record_le(PG_FUNCTION_ARGS)
                               1302                 :                : {
                               1303                 :              6 :     PG_RETURN_BOOL(record_cmp(fcinfo) <= 0);
                               1304                 :                : }
                               1305                 :                : 
                               1306                 :                : Datum
                               1307                 :             21 : record_ge(PG_FUNCTION_ARGS)
                               1308                 :                : {
                               1309                 :             21 :     PG_RETURN_BOOL(record_cmp(fcinfo) >= 0);
                               1310                 :                : }
                               1311                 :                : 
                               1312                 :                : Datum
                               1313                 :           2245 : btrecordcmp(PG_FUNCTION_ARGS)
                               1314                 :                : {
                               1315                 :           2245 :     PG_RETURN_INT32(record_cmp(fcinfo));
                               1316                 :                : }
                               1317                 :                : 
                               1318                 :                : Datum
  422                          1319                 :             18 : record_larger(PG_FUNCTION_ARGS)
                               1320                 :                : {
                               1321         [ +  + ]:             18 :     if (record_cmp(fcinfo) > 0)
                               1322                 :              9 :         PG_RETURN_DATUM(PG_GETARG_DATUM(0));
                               1323                 :                :     else
                               1324                 :              9 :         PG_RETURN_DATUM(PG_GETARG_DATUM(1));
                               1325                 :                : }
                               1326                 :                : 
                               1327                 :                : Datum
                               1328                 :             18 : record_smaller(PG_FUNCTION_ARGS)
                               1329                 :                : {
                               1330         [ +  + ]:             18 :     if (record_cmp(fcinfo) < 0)
                               1331                 :             12 :         PG_RETURN_DATUM(PG_GETARG_DATUM(0));
                               1332                 :                :     else
                               1333                 :              6 :         PG_RETURN_DATUM(PG_GETARG_DATUM(1));
                               1334                 :                : }
                               1335                 :                : 
                               1336                 :                : 
                               1337                 :                : /*
                               1338                 :                :  * record_image_cmp :
                               1339                 :                :  * Internal byte-oriented comparison function for records.
                               1340                 :                :  *
                               1341                 :                :  * Returns -1, 0 or 1
                               1342                 :                :  *
                               1343                 :                :  * Note: The normal concepts of "equality" do not apply here; different
                               1344                 :                :  * representation of values considered to be equal are not considered to be
                               1345                 :                :  * identical.  As an example, for the citext type 'A' and 'a' are equal, but
                               1346                 :                :  * they are not identical.
                               1347                 :                :  */
                               1348                 :                : static int
 4348 kgrittn@postgresql.o     1349                 :            426 : record_image_cmp(FunctionCallInfo fcinfo)
                               1350                 :                : {
 4350                          1351                 :            426 :     HeapTupleHeader record1 = PG_GETARG_HEAPTUPLEHEADER(0);
                               1352                 :            426 :     HeapTupleHeader record2 = PG_GETARG_HEAPTUPLEHEADER(1);
 4348                          1353                 :            426 :     int         result = 0;
                               1354                 :                :     Oid         tupType1;
                               1355                 :                :     Oid         tupType2;
                               1356                 :                :     int32       tupTypmod1;
                               1357                 :                :     int32       tupTypmod2;
                               1358                 :                :     TupleDesc   tupdesc1;
                               1359                 :                :     TupleDesc   tupdesc2;
                               1360                 :                :     HeapTupleData tuple1;
                               1361                 :                :     HeapTupleData tuple2;
                               1362                 :                :     int         ncolumns1;
                               1363                 :                :     int         ncolumns2;
                               1364                 :                :     RecordCompareData *my_extra;
                               1365                 :                :     int         ncols;
                               1366                 :                :     Datum      *values1;
                               1367                 :                :     Datum      *values2;
                               1368                 :                :     bool       *nulls1;
                               1369                 :                :     bool       *nulls2;
                               1370                 :                :     int         i1;
                               1371                 :                :     int         i2;
                               1372                 :                :     int         j;
                               1373                 :                : 
                               1374                 :                :     /* Extract type info from the tuples */
 4350                          1375                 :            426 :     tupType1 = HeapTupleHeaderGetTypeId(record1);
                               1376                 :            426 :     tupTypmod1 = HeapTupleHeaderGetTypMod(record1);
                               1377                 :            426 :     tupdesc1 = lookup_rowtype_tupdesc(tupType1, tupTypmod1);
                               1378                 :            426 :     ncolumns1 = tupdesc1->natts;
                               1379                 :            426 :     tupType2 = HeapTupleHeaderGetTypeId(record2);
                               1380                 :            426 :     tupTypmod2 = HeapTupleHeaderGetTypMod(record2);
                               1381                 :            426 :     tupdesc2 = lookup_rowtype_tupdesc(tupType2, tupTypmod2);
                               1382                 :            426 :     ncolumns2 = tupdesc2->natts;
                               1383                 :                : 
                               1384                 :                :     /* Build temporary HeapTuple control structures */
                               1385                 :            426 :     tuple1.t_len = HeapTupleHeaderGetDatumLength(record1);
                               1386                 :            426 :     ItemPointerSetInvalid(&(tuple1.t_self));
                               1387                 :            426 :     tuple1.t_tableOid = InvalidOid;
                               1388                 :            426 :     tuple1.t_data = record1;
                               1389                 :            426 :     tuple2.t_len = HeapTupleHeaderGetDatumLength(record2);
                               1390                 :            426 :     ItemPointerSetInvalid(&(tuple2.t_self));
                               1391                 :            426 :     tuple2.t_tableOid = InvalidOid;
                               1392                 :            426 :     tuple2.t_data = record2;
                               1393                 :                : 
                               1394                 :                :     /*
                               1395                 :                :      * We arrange to look up the needed comparison info just once per series
                               1396                 :                :      * of calls, assuming the record types don't change underneath us.
                               1397                 :                :      */
                               1398                 :            426 :     ncols = Max(ncolumns1, ncolumns2);
                               1399                 :            426 :     my_extra = (RecordCompareData *) fcinfo->flinfo->fn_extra;
                               1400         [ +  + ]:            426 :     if (my_extra == NULL ||
                               1401         [ -  + ]:            303 :         my_extra->ncolumns < ncols)
                               1402                 :                :     {
                               1403                 :            246 :         fcinfo->flinfo->fn_extra =
                               1404                 :            123 :             MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
 3851 tgl@sss.pgh.pa.us        1405                 :            123 :                                offsetof(RecordCompareData, columns) +
                               1406                 :                :                                ncols * sizeof(ColumnCompareData));
 4350 kgrittn@postgresql.o     1407                 :            123 :         my_extra = (RecordCompareData *) fcinfo->flinfo->fn_extra;
                               1408                 :            123 :         my_extra->ncolumns = ncols;
                               1409                 :            123 :         my_extra->record1_type = InvalidOid;
                               1410                 :            123 :         my_extra->record1_typmod = 0;
                               1411                 :            123 :         my_extra->record2_type = InvalidOid;
                               1412                 :            123 :         my_extra->record2_typmod = 0;
                               1413                 :                :     }
                               1414                 :                : 
                               1415         [ +  + ]:            426 :     if (my_extra->record1_type != tupType1 ||
                               1416         [ +  - ]:            303 :         my_extra->record1_typmod != tupTypmod1 ||
                               1417         [ +  - ]:            303 :         my_extra->record2_type != tupType2 ||
                               1418         [ -  + ]:            303 :         my_extra->record2_typmod != tupTypmod2)
                               1419                 :                :     {
                               1420   [ +  -  +  -  :            438 :         MemSet(my_extra->columns, 0, ncols * sizeof(ColumnCompareData));
                                     +  -  +  -  +  
                                                 + ]
                               1421                 :            123 :         my_extra->record1_type = tupType1;
                               1422                 :            123 :         my_extra->record1_typmod = tupTypmod1;
                               1423                 :            123 :         my_extra->record2_type = tupType2;
                               1424                 :            123 :         my_extra->record2_typmod = tupTypmod2;
                               1425                 :                :     }
                               1426                 :                : 
                               1427                 :                :     /* Break down the tuples into fields */
                               1428                 :            426 :     values1 = (Datum *) palloc(ncolumns1 * sizeof(Datum));
                               1429                 :            426 :     nulls1 = (bool *) palloc(ncolumns1 * sizeof(bool));
                               1430                 :            426 :     heap_deform_tuple(&tuple1, tupdesc1, values1, nulls1);
                               1431                 :            426 :     values2 = (Datum *) palloc(ncolumns2 * sizeof(Datum));
                               1432                 :            426 :     nulls2 = (bool *) palloc(ncolumns2 * sizeof(bool));
                               1433                 :            426 :     heap_deform_tuple(&tuple2, tupdesc2, values2, nulls2);
                               1434                 :                : 
                               1435                 :                :     /*
                               1436                 :                :      * Scan corresponding columns, allowing for dropped columns in different
                               1437                 :                :      * places in the two rows.  i1 and i2 are physical column indexes, j is
                               1438                 :                :      * the logical column index.
                               1439                 :                :      */
                               1440                 :            426 :     i1 = i2 = j = 0;
                               1441   [ +  +  -  + ]:            833 :     while (i1 < ncolumns1 || i2 < ncolumns2)
                               1442                 :                :     {
                               1443                 :                :         Form_pg_attribute att1;
                               1444                 :                :         Form_pg_attribute att2;
                               1445                 :                : 
                               1446                 :                :         /*
                               1447                 :                :          * Skip dropped columns
                               1448                 :                :          */
 2939 andres@anarazel.de       1449   [ +  -  -  + ]:            749 :         if (i1 < ncolumns1 && TupleDescAttr(tupdesc1, i1)->attisdropped)
                               1450                 :                :         {
 4350 kgrittn@postgresql.o     1451                 :UBC           0 :             i1++;
                               1452                 :              0 :             continue;
                               1453                 :                :         }
 2939 andres@anarazel.de       1454   [ +  +  -  + ]:CBC         749 :         if (i2 < ncolumns2 && TupleDescAttr(tupdesc2, i2)->attisdropped)
                               1455                 :                :         {
 4350 kgrittn@postgresql.o     1456                 :UBC           0 :             i2++;
                               1457                 :              0 :             continue;
                               1458                 :                :         }
 4350 kgrittn@postgresql.o     1459   [ +  -  +  + ]:CBC         749 :         if (i1 >= ncolumns1 || i2 >= ncolumns2)
                               1460                 :                :             break;              /* we'll deal with mismatch below loop */
                               1461                 :                : 
 2939 andres@anarazel.de       1462                 :            746 :         att1 = TupleDescAttr(tupdesc1, i1);
                               1463                 :            746 :         att2 = TupleDescAttr(tupdesc2, i2);
                               1464                 :                : 
                               1465                 :                :         /*
                               1466                 :                :          * Have two matching columns, they must be same type
                               1467                 :                :          */
                               1468         [ +  + ]:            746 :         if (att1->atttypid != att2->atttypid)
 4350 kgrittn@postgresql.o     1469         [ +  - ]:              3 :             ereport(ERROR,
                               1470                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               1471                 :                :                      errmsg("cannot compare dissimilar column types %s and %s at record column %d",
                               1472                 :                :                             format_type_be(att1->atttypid),
                               1473                 :                :                             format_type_be(att2->atttypid),
                               1474                 :                :                             j + 1)));
                               1475                 :                : 
                               1476                 :                :         /*
                               1477                 :                :          * The same type should have the same length (or both should be
                               1478                 :                :          * variable).
                               1479                 :                :          */
 2939 andres@anarazel.de       1480         [ -  + ]:            743 :         Assert(att1->attlen == att2->attlen);
                               1481                 :                : 
                               1482                 :                :         /*
                               1483                 :                :          * We consider two NULLs equal; NULL > not-NULL.
                               1484                 :                :          */
 4350 kgrittn@postgresql.o     1485   [ -  +  -  - ]:            743 :         if (!nulls1[i1] || !nulls2[i2])
                               1486                 :                :         {
 4337 tgl@sss.pgh.pa.us        1487                 :            743 :             int         cmpresult = 0;
                               1488                 :                : 
 4350 kgrittn@postgresql.o     1489         [ -  + ]:            743 :             if (nulls1[i1])
                               1490                 :                :             {
                               1491                 :                :                 /* arg1 is greater than arg2 */
 4350 kgrittn@postgresql.o     1492                 :UBC           0 :                 result = 1;
                               1493                 :              0 :                 break;
                               1494                 :                :             }
 4350 kgrittn@postgresql.o     1495         [ -  + ]:CBC         743 :             if (nulls2[i2])
                               1496                 :                :             {
                               1497                 :                :                 /* arg1 is less than arg2 */
 4350 kgrittn@postgresql.o     1498                 :UBC           0 :                 result = -1;
                               1499                 :              0 :                 break;
                               1500                 :                :             }
                               1501                 :                : 
                               1502                 :                :             /* Compare the pair of elements */
 2133 peter@eisentraut.org     1503         [ +  + ]:CBC         743 :             if (att1->attbyval)
                               1504                 :                :             {
                               1505         [ +  + ]:            489 :                 if (values1[i1] != values2[i2])
                               1506         [ +  + ]:            256 :                     cmpresult = (values1[i1] < values2[i2]) ? -1 : 1;
                               1507                 :                :             }
                               1508         [ +  + ]:            254 :             else if (att1->attlen > 0)
                               1509                 :                :             {
                               1510                 :             18 :                 cmpresult = memcmp(DatumGetPointer(values1[i1]),
                               1511                 :             18 :                                    DatumGetPointer(values2[i2]),
                               1512                 :             18 :                                    att1->attlen);
                               1513                 :                :             }
                               1514         [ +  - ]:            236 :             else if (att1->attlen == -1)
                               1515                 :                :             {
                               1516                 :                :                 Size        len1,
                               1517                 :                :                             len2;
                               1518                 :                :                 struct varlena *arg1val;
                               1519                 :                :                 struct varlena *arg2val;
                               1520                 :                : 
 4350 kgrittn@postgresql.o     1521                 :            236 :                 len1 = toast_raw_datum_size(values1[i1]);
                               1522                 :            236 :                 len2 = toast_raw_datum_size(values2[i2]);
                               1523                 :            236 :                 arg1val = PG_DETOAST_DATUM_PACKED(values1[i1]);
                               1524                 :            236 :                 arg2val = PG_DETOAST_DATUM_PACKED(values2[i2]);
                               1525                 :                : 
                               1526         [ +  - ]:            236 :                 cmpresult = memcmp(VARDATA_ANY(arg1val),
                               1527                 :            236 :                                    VARDATA_ANY(arg2val),
 4348                          1528         [ +  - ]:            236 :                                    Min(len1, len2) - VARHDRSZ);
 4350                          1529   [ +  +  +  + ]:            236 :                 if ((cmpresult == 0) && (len1 != len2))
                               1530         [ +  - ]:              3 :                     cmpresult = (len1 < len2) ? -1 : 1;
                               1531                 :                : 
   29 peter@eisentraut.org     1532         [ -  + ]:GNC         236 :                 if ((Pointer) arg1val != DatumGetPointer(values1[i1]))
 4350 kgrittn@postgresql.o     1533                 :UBC           0 :                     pfree(arg1val);
   29 peter@eisentraut.org     1534         [ -  + ]:GNC         236 :                 if ((Pointer) arg2val != DatumGetPointer(values2[i2]))
 4350 kgrittn@postgresql.o     1535                 :UBC           0 :                     pfree(arg2val);
                               1536                 :                :             }
                               1537                 :                :             else
 2133 peter@eisentraut.org     1538         [ #  # ]:              0 :                 elog(ERROR, "unexpected attlen: %d", att1->attlen);
                               1539                 :                : 
 4350 kgrittn@postgresql.o     1540         [ +  + ]:CBC         743 :             if (cmpresult < 0)
                               1541                 :                :             {
                               1542                 :                :                 /* arg1 is less than arg2 */
                               1543                 :            184 :                 result = -1;
                               1544                 :            184 :                 break;
                               1545                 :                :             }
                               1546         [ +  + ]:            559 :             else if (cmpresult > 0)
                               1547                 :                :             {
                               1548                 :                :                 /* arg1 is greater than arg2 */
                               1549                 :            152 :                 result = 1;
                               1550                 :            152 :                 break;
                               1551                 :                :             }
                               1552                 :                :         }
                               1553                 :                : 
                               1554                 :                :         /* equal, so continue to next column */
                               1555                 :            407 :         i1++, i2++, j++;
                               1556                 :                :     }
                               1557                 :                : 
                               1558                 :                :     /*
                               1559                 :                :      * If we didn't break out of the loop early, check for column count
                               1560                 :                :      * mismatch.  (We do not report such mismatch if we found unequal column
                               1561                 :                :      * values; is that a feature or a bug?)
                               1562                 :                :      */
                               1563         [ +  + ]:            423 :     if (result == 0)
                               1564                 :                :     {
                               1565   [ +  +  -  + ]:             87 :         if (i1 != ncolumns1 || i2 != ncolumns2)
                               1566         [ +  - ]:              3 :             ereport(ERROR,
                               1567                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               1568                 :                :                      errmsg("cannot compare record types with different numbers of columns")));
                               1569                 :                :     }
                               1570                 :                : 
                               1571                 :            420 :     pfree(values1);
                               1572                 :            420 :     pfree(nulls1);
                               1573                 :            420 :     pfree(values2);
                               1574                 :            420 :     pfree(nulls2);
                               1575         [ +  - ]:            420 :     ReleaseTupleDesc(tupdesc1);
                               1576         [ +  - ]:            420 :     ReleaseTupleDesc(tupdesc2);
                               1577                 :                : 
                               1578                 :                :     /* Avoid leaking memory when handed toasted input. */
                               1579         [ +  + ]:            420 :     PG_FREE_IF_COPY(record1, 0);
                               1580         [ +  + ]:            420 :     PG_FREE_IF_COPY(record2, 1);
                               1581                 :                : 
                               1582                 :            420 :     return result;
                               1583                 :                : }
                               1584                 :                : 
                               1585                 :                : /*
                               1586                 :                :  * record_image_eq :
                               1587                 :                :  *        compares two records for identical contents, based on byte images
                               1588                 :                :  * result :
                               1589                 :                :  *        returns true if the records are identical, false otherwise.
                               1590                 :                :  *
                               1591                 :                :  * Note: we do not use record_image_cmp here, since we can avoid
                               1592                 :                :  * de-toasting for unequal lengths this way.
                               1593                 :                :  */
                               1594                 :                : Datum
                               1595                 :            130 : record_image_eq(PG_FUNCTION_ARGS)
                               1596                 :                : {
                               1597                 :            130 :     HeapTupleHeader record1 = PG_GETARG_HEAPTUPLEHEADER(0);
                               1598                 :            130 :     HeapTupleHeader record2 = PG_GETARG_HEAPTUPLEHEADER(1);
                               1599                 :            130 :     bool        result = true;
                               1600                 :                :     Oid         tupType1;
                               1601                 :                :     Oid         tupType2;
                               1602                 :                :     int32       tupTypmod1;
                               1603                 :                :     int32       tupTypmod2;
                               1604                 :                :     TupleDesc   tupdesc1;
                               1605                 :                :     TupleDesc   tupdesc2;
                               1606                 :                :     HeapTupleData tuple1;
                               1607                 :                :     HeapTupleData tuple2;
                               1608                 :                :     int         ncolumns1;
                               1609                 :                :     int         ncolumns2;
                               1610                 :                :     RecordCompareData *my_extra;
                               1611                 :                :     int         ncols;
                               1612                 :                :     Datum      *values1;
                               1613                 :                :     Datum      *values2;
                               1614                 :                :     bool       *nulls1;
                               1615                 :                :     bool       *nulls2;
                               1616                 :                :     int         i1;
                               1617                 :                :     int         i2;
                               1618                 :                :     int         j;
                               1619                 :                : 
                               1620                 :                :     /* Extract type info from the tuples */
                               1621                 :            130 :     tupType1 = HeapTupleHeaderGetTypeId(record1);
                               1622                 :            130 :     tupTypmod1 = HeapTupleHeaderGetTypMod(record1);
                               1623                 :            130 :     tupdesc1 = lookup_rowtype_tupdesc(tupType1, tupTypmod1);
                               1624                 :            130 :     ncolumns1 = tupdesc1->natts;
                               1625                 :            130 :     tupType2 = HeapTupleHeaderGetTypeId(record2);
                               1626                 :            130 :     tupTypmod2 = HeapTupleHeaderGetTypMod(record2);
                               1627                 :            130 :     tupdesc2 = lookup_rowtype_tupdesc(tupType2, tupTypmod2);
                               1628                 :            130 :     ncolumns2 = tupdesc2->natts;
                               1629                 :                : 
                               1630                 :                :     /* Build temporary HeapTuple control structures */
                               1631                 :            130 :     tuple1.t_len = HeapTupleHeaderGetDatumLength(record1);
                               1632                 :            130 :     ItemPointerSetInvalid(&(tuple1.t_self));
                               1633                 :            130 :     tuple1.t_tableOid = InvalidOid;
                               1634                 :            130 :     tuple1.t_data = record1;
                               1635                 :            130 :     tuple2.t_len = HeapTupleHeaderGetDatumLength(record2);
                               1636                 :            130 :     ItemPointerSetInvalid(&(tuple2.t_self));
                               1637                 :            130 :     tuple2.t_tableOid = InvalidOid;
                               1638                 :            130 :     tuple2.t_data = record2;
                               1639                 :                : 
                               1640                 :                :     /*
                               1641                 :                :      * We arrange to look up the needed comparison info just once per series
                               1642                 :                :      * of calls, assuming the record types don't change underneath us.
                               1643                 :                :      */
                               1644                 :            130 :     ncols = Max(ncolumns1, ncolumns2);
                               1645                 :            130 :     my_extra = (RecordCompareData *) fcinfo->flinfo->fn_extra;
                               1646         [ +  + ]:            130 :     if (my_extra == NULL ||
                               1647         [ -  + ]:             65 :         my_extra->ncolumns < ncols)
                               1648                 :                :     {
                               1649                 :            130 :         fcinfo->flinfo->fn_extra =
                               1650                 :             65 :             MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
 3851 tgl@sss.pgh.pa.us        1651                 :             65 :                                offsetof(RecordCompareData, columns) +
                               1652                 :                :                                ncols * sizeof(ColumnCompareData));
 4350 kgrittn@postgresql.o     1653                 :             65 :         my_extra = (RecordCompareData *) fcinfo->flinfo->fn_extra;
                               1654                 :             65 :         my_extra->ncolumns = ncols;
                               1655                 :             65 :         my_extra->record1_type = InvalidOid;
                               1656                 :             65 :         my_extra->record1_typmod = 0;
                               1657                 :             65 :         my_extra->record2_type = InvalidOid;
                               1658                 :             65 :         my_extra->record2_typmod = 0;
                               1659                 :                :     }
                               1660                 :                : 
                               1661         [ +  + ]:            130 :     if (my_extra->record1_type != tupType1 ||
                               1662         [ +  - ]:             65 :         my_extra->record1_typmod != tupTypmod1 ||
                               1663         [ +  - ]:             65 :         my_extra->record2_type != tupType2 ||
                               1664         [ -  + ]:             65 :         my_extra->record2_typmod != tupTypmod2)
                               1665                 :                :     {
                               1666   [ +  -  +  -  :            212 :         MemSet(my_extra->columns, 0, ncols * sizeof(ColumnCompareData));
                                     +  -  +  -  +  
                                                 + ]
                               1667                 :             65 :         my_extra->record1_type = tupType1;
                               1668                 :             65 :         my_extra->record1_typmod = tupTypmod1;
                               1669                 :             65 :         my_extra->record2_type = tupType2;
                               1670                 :             65 :         my_extra->record2_typmod = tupTypmod2;
                               1671                 :                :     }
                               1672                 :                : 
                               1673                 :                :     /* Break down the tuples into fields */
                               1674                 :            130 :     values1 = (Datum *) palloc(ncolumns1 * sizeof(Datum));
                               1675                 :            130 :     nulls1 = (bool *) palloc(ncolumns1 * sizeof(bool));
                               1676                 :            130 :     heap_deform_tuple(&tuple1, tupdesc1, values1, nulls1);
                               1677                 :            130 :     values2 = (Datum *) palloc(ncolumns2 * sizeof(Datum));
                               1678                 :            130 :     nulls2 = (bool *) palloc(ncolumns2 * sizeof(bool));
                               1679                 :            130 :     heap_deform_tuple(&tuple2, tupdesc2, values2, nulls2);
                               1680                 :                : 
                               1681                 :                :     /*
                               1682                 :                :      * Scan corresponding columns, allowing for dropped columns in different
                               1683                 :                :      * places in the two rows.  i1 and i2 are physical column indexes, j is
                               1684                 :                :      * the logical column index.
                               1685                 :                :      */
                               1686                 :            130 :     i1 = i2 = j = 0;
                               1687   [ +  +  -  + ]:            494 :     while (i1 < ncolumns1 || i2 < ncolumns2)
                               1688                 :                :     {
                               1689                 :                :         Form_pg_attribute att1;
                               1690                 :                :         Form_pg_attribute att2;
                               1691                 :                : 
                               1692                 :                :         /*
                               1693                 :                :          * Skip dropped columns
                               1694                 :                :          */
 2939 andres@anarazel.de       1695   [ +  -  -  + ]:            399 :         if (i1 < ncolumns1 && TupleDescAttr(tupdesc1, i1)->attisdropped)
                               1696                 :                :         {
 4350 kgrittn@postgresql.o     1697                 :UBC           0 :             i1++;
                               1698                 :              0 :             continue;
                               1699                 :                :         }
 2939 andres@anarazel.de       1700   [ +  +  -  + ]:CBC         399 :         if (i2 < ncolumns2 && TupleDescAttr(tupdesc2, i2)->attisdropped)
                               1701                 :                :         {
 4350 kgrittn@postgresql.o     1702                 :UBC           0 :             i2++;
                               1703                 :              0 :             continue;
                               1704                 :                :         }
 4350 kgrittn@postgresql.o     1705   [ +  -  +  + ]:CBC         399 :         if (i1 >= ncolumns1 || i2 >= ncolumns2)
                               1706                 :                :             break;              /* we'll deal with mismatch below loop */
                               1707                 :                : 
 2939 andres@anarazel.de       1708                 :            396 :         att1 = TupleDescAttr(tupdesc1, i1);
                               1709                 :            396 :         att2 = TupleDescAttr(tupdesc2, i2);
                               1710                 :                : 
                               1711                 :                :         /*
                               1712                 :                :          * Have two matching columns, they must be same type
                               1713                 :                :          */
                               1714         [ +  + ]:            396 :         if (att1->atttypid != att2->atttypid)
 4350 kgrittn@postgresql.o     1715         [ +  - ]:              3 :             ereport(ERROR,
                               1716                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               1717                 :                :                      errmsg("cannot compare dissimilar column types %s and %s at record column %d",
                               1718                 :                :                             format_type_be(att1->atttypid),
                               1719                 :                :                             format_type_be(att2->atttypid),
                               1720                 :                :                             j + 1)));
                               1721                 :                : 
                               1722                 :                :         /*
                               1723                 :                :          * We consider two NULLs equal; NULL > not-NULL.
                               1724                 :                :          */
                               1725   [ +  +  -  + ]:            393 :         if (!nulls1[i1] || !nulls2[i2])
                               1726                 :                :         {
                               1727   [ +  -  -  + ]:            387 :             if (nulls1[i1] || nulls2[i2])
                               1728                 :                :             {
 4350 kgrittn@postgresql.o     1729                 :UBC           0 :                 result = false;
                               1730                 :              0 :                 break;
                               1731                 :                :             }
                               1732                 :                : 
                               1733                 :                :             /* Compare the pair of elements */
 2364 peter@eisentraut.org     1734                 :CBC         387 :             result = datum_image_eq(values1[i1], values2[i2], att1->attbyval, att2->attlen);
 4350 kgrittn@postgresql.o     1735         [ +  + ]:            387 :             if (!result)
                               1736                 :             29 :                 break;
                               1737                 :                :         }
                               1738                 :                : 
                               1739                 :                :         /* equal, so continue to next column */
                               1740                 :            364 :         i1++, i2++, j++;
                               1741                 :                :     }
                               1742                 :                : 
                               1743                 :                :     /*
                               1744                 :                :      * If we didn't break out of the loop early, check for column count
                               1745                 :                :      * mismatch.  (We do not report such mismatch if we found unequal column
                               1746                 :                :      * values; is that a feature or a bug?)
                               1747                 :                :      */
                               1748         [ +  + ]:            127 :     if (result)
                               1749                 :                :     {
                               1750   [ +  +  -  + ]:             98 :         if (i1 != ncolumns1 || i2 != ncolumns2)
                               1751         [ +  - ]:              3 :             ereport(ERROR,
                               1752                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               1753                 :                :                      errmsg("cannot compare record types with different numbers of columns")));
                               1754                 :                :     }
                               1755                 :                : 
                               1756                 :            124 :     pfree(values1);
                               1757                 :            124 :     pfree(nulls1);
                               1758                 :            124 :     pfree(values2);
                               1759                 :            124 :     pfree(nulls2);
                               1760         [ +  - ]:            124 :     ReleaseTupleDesc(tupdesc1);
                               1761         [ +  - ]:            124 :     ReleaseTupleDesc(tupdesc2);
                               1762                 :                : 
                               1763                 :                :     /* Avoid leaking memory when handed toasted input. */
                               1764         [ +  + ]:            124 :     PG_FREE_IF_COPY(record1, 0);
                               1765         [ +  + ]:            124 :     PG_FREE_IF_COPY(record2, 1);
                               1766                 :                : 
                               1767                 :            124 :     PG_RETURN_BOOL(result);
                               1768                 :                : }
                               1769                 :                : 
                               1770                 :                : Datum
                               1771                 :             24 : record_image_ne(PG_FUNCTION_ARGS)
                               1772                 :                : {
                               1773                 :             24 :     PG_RETURN_BOOL(!DatumGetBool(record_image_eq(fcinfo)));
                               1774                 :                : }
                               1775                 :                : 
                               1776                 :                : Datum
                               1777                 :             36 : record_image_lt(PG_FUNCTION_ARGS)
                               1778                 :                : {
                               1779                 :             36 :     PG_RETURN_BOOL(record_image_cmp(fcinfo) < 0);
                               1780                 :                : }
                               1781                 :                : 
                               1782                 :                : Datum
                               1783                 :              9 : record_image_gt(PG_FUNCTION_ARGS)
                               1784                 :                : {
                               1785                 :              9 :     PG_RETURN_BOOL(record_image_cmp(fcinfo) > 0);
                               1786                 :                : }
                               1787                 :                : 
                               1788                 :                : Datum
                               1789                 :              6 : record_image_le(PG_FUNCTION_ARGS)
                               1790                 :                : {
                               1791                 :              6 :     PG_RETURN_BOOL(record_image_cmp(fcinfo) <= 0);
                               1792                 :                : }
                               1793                 :                : 
                               1794                 :                : Datum
                               1795                 :              9 : record_image_ge(PG_FUNCTION_ARGS)
                               1796                 :                : {
                               1797                 :              9 :     PG_RETURN_BOOL(record_image_cmp(fcinfo) >= 0);
                               1798                 :                : }
                               1799                 :                : 
                               1800                 :                : Datum
                               1801                 :            366 : btrecordimagecmp(PG_FUNCTION_ARGS)
                               1802                 :                : {
                               1803                 :            366 :     PG_RETURN_INT32(record_image_cmp(fcinfo));
                               1804                 :                : }
                               1805                 :                : 
                               1806                 :                : 
                               1807                 :                : /*
                               1808                 :                :  * Row type hash functions
                               1809                 :                :  */
                               1810                 :                : 
                               1811                 :                : Datum
 1752 peter@eisentraut.org     1812                 :            450 : hash_record(PG_FUNCTION_ARGS)
                               1813                 :                : {
                               1814                 :            450 :     HeapTupleHeader record = PG_GETARG_HEAPTUPLEHEADER(0);
                               1815                 :            450 :     uint32      result = 0;
                               1816                 :                :     Oid         tupType;
                               1817                 :                :     int32       tupTypmod;
                               1818                 :                :     TupleDesc   tupdesc;
                               1819                 :                :     HeapTupleData tuple;
                               1820                 :                :     int         ncolumns;
                               1821                 :                :     RecordCompareData *my_extra;
                               1822                 :                :     Datum      *values;
                               1823                 :                :     bool       *nulls;
                               1824                 :                : 
                               1825                 :            450 :     check_stack_depth();        /* recurses for record-type columns */
                               1826                 :                : 
                               1827                 :                :     /* Extract type info from tuple */
                               1828                 :            450 :     tupType = HeapTupleHeaderGetTypeId(record);
                               1829                 :            450 :     tupTypmod = HeapTupleHeaderGetTypMod(record);
                               1830                 :            450 :     tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
                               1831                 :            450 :     ncolumns = tupdesc->natts;
                               1832                 :                : 
                               1833                 :                :     /* Build temporary HeapTuple control structure */
                               1834                 :            450 :     tuple.t_len = HeapTupleHeaderGetDatumLength(record);
                               1835                 :            450 :     ItemPointerSetInvalid(&(tuple.t_self));
                               1836                 :            450 :     tuple.t_tableOid = InvalidOid;
                               1837                 :            450 :     tuple.t_data = record;
                               1838                 :                : 
                               1839                 :                :     /*
                               1840                 :                :      * We arrange to look up the needed hashing info just once per series of
                               1841                 :                :      * calls, assuming the record type doesn't change underneath us.
                               1842                 :                :      */
                               1843                 :            450 :     my_extra = (RecordCompareData *) fcinfo->flinfo->fn_extra;
                               1844         [ +  + ]:            450 :     if (my_extra == NULL ||
                               1845         [ -  + ]:            426 :         my_extra->ncolumns < ncolumns)
                               1846                 :                :     {
                               1847                 :             48 :         fcinfo->flinfo->fn_extra =
                               1848                 :             24 :             MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
                               1849                 :             24 :                                offsetof(RecordCompareData, columns) +
                               1850                 :                :                                ncolumns * sizeof(ColumnCompareData));
                               1851                 :             24 :         my_extra = (RecordCompareData *) fcinfo->flinfo->fn_extra;
                               1852                 :             24 :         my_extra->ncolumns = ncolumns;
                               1853                 :             24 :         my_extra->record1_type = InvalidOid;
                               1854                 :             24 :         my_extra->record1_typmod = 0;
                               1855                 :                :     }
                               1856                 :                : 
                               1857         [ +  + ]:            450 :     if (my_extra->record1_type != tupType ||
                               1858         [ -  + ]:            426 :         my_extra->record1_typmod != tupTypmod)
                               1859                 :                :     {
                               1860   [ +  -  +  -  :             75 :         MemSet(my_extra->columns, 0, ncolumns * sizeof(ColumnCompareData));
                                     +  -  +  -  +  
                                                 + ]
                               1861                 :             24 :         my_extra->record1_type = tupType;
                               1862                 :             24 :         my_extra->record1_typmod = tupTypmod;
                               1863                 :                :     }
                               1864                 :                : 
                               1865                 :                :     /* Break down the tuple into fields */
                               1866                 :            450 :     values = (Datum *) palloc(ncolumns * sizeof(Datum));
                               1867                 :            450 :     nulls = (bool *) palloc(ncolumns * sizeof(bool));
                               1868                 :            450 :     heap_deform_tuple(&tuple, tupdesc, values, nulls);
                               1869                 :                : 
                               1870         [ +  + ]:           1365 :     for (int i = 0; i < ncolumns; i++)
                               1871                 :                :     {
                               1872                 :                :         Form_pg_attribute att;
                               1873                 :                :         TypeCacheEntry *typentry;
                               1874                 :                :         uint32      element_hash;
                               1875                 :                : 
                               1876                 :            918 :         att = TupleDescAttr(tupdesc, i);
                               1877                 :                : 
                               1878         [ -  + ]:            918 :         if (att->attisdropped)
 1752 peter@eisentraut.org     1879                 :UBC           0 :             continue;
                               1880                 :                : 
                               1881                 :                :         /*
                               1882                 :                :          * Lookup the hash function if not done already
                               1883                 :                :          */
 1752 peter@eisentraut.org     1884                 :CBC         918 :         typentry = my_extra->columns[i].typentry;
                               1885         [ +  + ]:            918 :         if (typentry == NULL ||
                               1886         [ -  + ]:            870 :             typentry->type_id != att->atttypid)
                               1887                 :                :         {
                               1888                 :             48 :             typentry = lookup_type_cache(att->atttypid,
                               1889                 :                :                                          TYPECACHE_HASH_PROC_FINFO);
                               1890         [ +  + ]:             48 :             if (!OidIsValid(typentry->hash_proc_finfo.fn_oid))
                               1891         [ +  - ]:              3 :                 ereport(ERROR,
                               1892                 :                :                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
                               1893                 :                :                          errmsg("could not identify a hash function for type %s",
                               1894                 :                :                                 format_type_be(typentry->type_id))));
                               1895                 :             45 :             my_extra->columns[i].typentry = typentry;
                               1896                 :                :         }
                               1897                 :                : 
                               1898                 :                :         /* Compute hash of element */
                               1899         [ -  + ]:            915 :         if (nulls[i])
                               1900                 :                :         {
 1752 peter@eisentraut.org     1901                 :UBC           0 :             element_hash = 0;
                               1902                 :                :         }
                               1903                 :                :         else
                               1904                 :                :         {
 1752 peter@eisentraut.org     1905                 :CBC         915 :             LOCAL_FCINFO(locfcinfo, 1);
                               1906                 :                : 
                               1907                 :            915 :             InitFunctionCallInfoData(*locfcinfo, &typentry->hash_proc_finfo, 1,
                               1908                 :                :                                      att->attcollation, NULL, NULL);
                               1909                 :            915 :             locfcinfo->args[0].value = values[i];
                               1910                 :            915 :             locfcinfo->args[0].isnull = false;
                               1911                 :            915 :             element_hash = DatumGetUInt32(FunctionCallInvoke(locfcinfo));
                               1912                 :                : 
                               1913                 :                :             /* We don't expect hash support functions to return null */
                               1914         [ -  + ]:            915 :             Assert(!locfcinfo->isnull);
                               1915                 :                :         }
                               1916                 :                : 
                               1917                 :                :         /* see hash_array() */
                               1918                 :            915 :         result = (result << 5) - result + element_hash;
                               1919                 :                :     }
                               1920                 :                : 
                               1921                 :            447 :     pfree(values);
                               1922                 :            447 :     pfree(nulls);
                               1923         [ +  - ]:            447 :     ReleaseTupleDesc(tupdesc);
                               1924                 :                : 
                               1925                 :                :     /* Avoid leaking memory when handed toasted input. */
                               1926         [ -  + ]:            447 :     PG_FREE_IF_COPY(record, 0);
                               1927                 :                : 
                               1928                 :            447 :     PG_RETURN_UINT32(result);
                               1929                 :                : }
                               1930                 :                : 
                               1931                 :                : Datum
                               1932                 :             15 : hash_record_extended(PG_FUNCTION_ARGS)
                               1933                 :                : {
                               1934                 :             15 :     HeapTupleHeader record = PG_GETARG_HEAPTUPLEHEADER(0);
                               1935                 :             15 :     uint64      seed = PG_GETARG_INT64(1);
                               1936                 :             15 :     uint64      result = 0;
                               1937                 :                :     Oid         tupType;
                               1938                 :                :     int32       tupTypmod;
                               1939                 :                :     TupleDesc   tupdesc;
                               1940                 :                :     HeapTupleData tuple;
                               1941                 :                :     int         ncolumns;
                               1942                 :                :     RecordCompareData *my_extra;
                               1943                 :                :     Datum      *values;
                               1944                 :                :     bool       *nulls;
                               1945                 :                : 
                               1946                 :             15 :     check_stack_depth();        /* recurses for record-type columns */
                               1947                 :                : 
                               1948                 :                :     /* Extract type info from tuple */
                               1949                 :             15 :     tupType = HeapTupleHeaderGetTypeId(record);
                               1950                 :             15 :     tupTypmod = HeapTupleHeaderGetTypMod(record);
                               1951                 :             15 :     tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
                               1952                 :             15 :     ncolumns = tupdesc->natts;
                               1953                 :                : 
                               1954                 :                :     /* Build temporary HeapTuple control structure */
                               1955                 :             15 :     tuple.t_len = HeapTupleHeaderGetDatumLength(record);
                               1956                 :             15 :     ItemPointerSetInvalid(&(tuple.t_self));
                               1957                 :             15 :     tuple.t_tableOid = InvalidOid;
                               1958                 :             15 :     tuple.t_data = record;
                               1959                 :                : 
                               1960                 :                :     /*
                               1961                 :                :      * We arrange to look up the needed hashing info just once per series of
                               1962                 :                :      * calls, assuming the record type doesn't change underneath us.
                               1963                 :                :      */
                               1964                 :             15 :     my_extra = (RecordCompareData *) fcinfo->flinfo->fn_extra;
                               1965         [ -  + ]:             15 :     if (my_extra == NULL ||
 1752 peter@eisentraut.org     1966         [ #  # ]:UBC           0 :         my_extra->ncolumns < ncolumns)
                               1967                 :                :     {
 1752 peter@eisentraut.org     1968                 :CBC          30 :         fcinfo->flinfo->fn_extra =
                               1969                 :             15 :             MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
                               1970                 :             15 :                                offsetof(RecordCompareData, columns) +
                               1971                 :                :                                ncolumns * sizeof(ColumnCompareData));
                               1972                 :             15 :         my_extra = (RecordCompareData *) fcinfo->flinfo->fn_extra;
                               1973                 :             15 :         my_extra->ncolumns = ncolumns;
                               1974                 :             15 :         my_extra->record1_type = InvalidOid;
                               1975                 :             15 :         my_extra->record1_typmod = 0;
                               1976                 :                :     }
                               1977                 :                : 
                               1978         [ -  + ]:             15 :     if (my_extra->record1_type != tupType ||
 1752 peter@eisentraut.org     1979         [ #  # ]:UBC           0 :         my_extra->record1_typmod != tupTypmod)
                               1980                 :                :     {
 1752 peter@eisentraut.org     1981   [ +  -  +  -  :CBC          45 :         MemSet(my_extra->columns, 0, ncolumns * sizeof(ColumnCompareData));
                                     +  -  +  -  +  
                                                 + ]
                               1982                 :             15 :         my_extra->record1_type = tupType;
                               1983                 :             15 :         my_extra->record1_typmod = tupTypmod;
                               1984                 :                :     }
                               1985                 :                : 
                               1986                 :                :     /* Break down the tuple into fields */
                               1987                 :             15 :     values = (Datum *) palloc(ncolumns * sizeof(Datum));
                               1988                 :             15 :     nulls = (bool *) palloc(ncolumns * sizeof(bool));
                               1989                 :             15 :     heap_deform_tuple(&tuple, tupdesc, values, nulls);
                               1990                 :                : 
                               1991         [ +  + ]:             39 :     for (int i = 0; i < ncolumns; i++)
                               1992                 :                :     {
                               1993                 :                :         Form_pg_attribute att;
                               1994                 :                :         TypeCacheEntry *typentry;
                               1995                 :                :         uint64      element_hash;
                               1996                 :                : 
                               1997                 :             27 :         att = TupleDescAttr(tupdesc, i);
                               1998                 :                : 
                               1999         [ -  + ]:             27 :         if (att->attisdropped)
 1752 peter@eisentraut.org     2000                 :UBC           0 :             continue;
                               2001                 :                : 
                               2002                 :                :         /*
                               2003                 :                :          * Lookup the hash function if not done already
                               2004                 :                :          */
 1752 peter@eisentraut.org     2005                 :CBC          27 :         typentry = my_extra->columns[i].typentry;
                               2006         [ -  + ]:             27 :         if (typentry == NULL ||
 1752 peter@eisentraut.org     2007         [ #  # ]:UBC           0 :             typentry->type_id != att->atttypid)
                               2008                 :                :         {
 1752 peter@eisentraut.org     2009                 :CBC          27 :             typentry = lookup_type_cache(att->atttypid,
                               2010                 :                :                                          TYPECACHE_HASH_EXTENDED_PROC_FINFO);
                               2011         [ +  + ]:             27 :             if (!OidIsValid(typentry->hash_extended_proc_finfo.fn_oid))
                               2012         [ +  - ]:              3 :                 ereport(ERROR,
                               2013                 :                :                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
                               2014                 :                :                          errmsg("could not identify an extended hash function for type %s",
                               2015                 :                :                                 format_type_be(typentry->type_id))));
                               2016                 :             24 :             my_extra->columns[i].typentry = typentry;
                               2017                 :                :         }
                               2018                 :                : 
                               2019                 :                :         /* Compute hash of element */
                               2020         [ -  + ]:             24 :         if (nulls[i])
                               2021                 :                :         {
 1752 peter@eisentraut.org     2022                 :UBC           0 :             element_hash = 0;
                               2023                 :                :         }
                               2024                 :                :         else
                               2025                 :                :         {
 1752 peter@eisentraut.org     2026                 :CBC          24 :             LOCAL_FCINFO(locfcinfo, 2);
                               2027                 :                : 
                               2028                 :             24 :             InitFunctionCallInfoData(*locfcinfo, &typentry->hash_extended_proc_finfo, 2,
                               2029                 :                :                                      att->attcollation, NULL, NULL);
                               2030                 :             24 :             locfcinfo->args[0].value = values[i];
                               2031                 :             24 :             locfcinfo->args[0].isnull = false;
                               2032                 :             24 :             locfcinfo->args[1].value = Int64GetDatum(seed);
                               2033                 :             24 :             locfcinfo->args[0].isnull = false;
                               2034                 :             24 :             element_hash = DatumGetUInt64(FunctionCallInvoke(locfcinfo));
                               2035                 :                : 
                               2036                 :                :             /* We don't expect hash support functions to return null */
                               2037         [ -  + ]:             24 :             Assert(!locfcinfo->isnull);
                               2038                 :                :         }
                               2039                 :                : 
                               2040                 :                :         /* see hash_array_extended() */
                               2041                 :             24 :         result = (result << 5) - result + element_hash;
                               2042                 :                :     }
                               2043                 :                : 
                               2044                 :             12 :     pfree(values);
                               2045                 :             12 :     pfree(nulls);
                               2046         [ +  - ]:             12 :     ReleaseTupleDesc(tupdesc);
                               2047                 :                : 
                               2048                 :                :     /* Avoid leaking memory when handed toasted input. */
                               2049         [ -  + ]:             12 :     PG_FREE_IF_COPY(record, 0);
                               2050                 :                : 
                               2051                 :             12 :     PG_RETURN_UINT64(result);
                               2052                 :                : }
        

Generated by: LCOV version 2.4-beta