LCOV - code coverage report
Current view: top level - src/backend/utils/adt - expandedrecord.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta1 Lines: 399 473 84.4 %
Date: 2019-06-16 14:06:46 Functions: 17 20 85.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * expandedrecord.c
       4             :  *    Functions for manipulating composite expanded objects.
       5             :  *
       6             :  * This module supports "expanded objects" (cf. expandeddatum.h) that can
       7             :  * store values of named composite types, domains over named composite types,
       8             :  * and record types (registered or anonymous).
       9             :  *
      10             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
      11             :  * Portions Copyright (c) 1994, Regents of the University of California
      12             :  *
      13             :  *
      14             :  * IDENTIFICATION
      15             :  *    src/backend/utils/adt/expandedrecord.c
      16             :  *
      17             :  *-------------------------------------------------------------------------
      18             :  */
      19             : #include "postgres.h"
      20             : 
      21             : #include "access/htup_details.h"
      22             : #include "access/tuptoaster.h"
      23             : #include "catalog/heap.h"
      24             : #include "catalog/pg_type.h"
      25             : #include "utils/builtins.h"
      26             : #include "utils/datum.h"
      27             : #include "utils/expandedrecord.h"
      28             : #include "utils/memutils.h"
      29             : #include "utils/typcache.h"
      30             : 
      31             : 
      32             : /* "Methods" required for an expanded object */
      33             : static Size ER_get_flat_size(ExpandedObjectHeader *eohptr);
      34             : static void ER_flatten_into(ExpandedObjectHeader *eohptr,
      35             :                             void *result, Size allocated_size);
      36             : 
      37             : static const ExpandedObjectMethods ER_methods =
      38             : {
      39             :     ER_get_flat_size,
      40             :     ER_flatten_into
      41             : };
      42             : 
      43             : /* Other local functions */
      44             : static void ER_mc_callback(void *arg);
      45             : static MemoryContext get_short_term_cxt(ExpandedRecordHeader *erh);
      46             : static void build_dummy_expanded_header(ExpandedRecordHeader *main_erh);
      47             : static pg_noinline void check_domain_for_new_field(ExpandedRecordHeader *erh,
      48             :                                                    int fnumber,
      49             :                                                    Datum newValue, bool isnull);
      50             : static pg_noinline void check_domain_for_new_tuple(ExpandedRecordHeader *erh,
      51             :                                                    HeapTuple tuple);
      52             : 
      53             : 
      54             : /*
      55             :  * Build an expanded record of the specified composite type
      56             :  *
      57             :  * type_id can be RECORDOID, but only if a positive typmod is given.
      58             :  *
      59             :  * The expanded record is initially "empty", having a state logically
      60             :  * equivalent to a NULL composite value (not ROW(NULL, NULL, ...)).
      61             :  * Note that this might not be a valid state for a domain type;
      62             :  * if the caller needs to check that, call
      63             :  * expanded_record_set_tuple(erh, NULL, false, false).
      64             :  *
      65             :  * The expanded object will be a child of parentcontext.
      66             :  */
      67             : ExpandedRecordHeader *
      68         472 : make_expanded_record_from_typeid(Oid type_id, int32 typmod,
      69             :                                  MemoryContext parentcontext)
      70             : {
      71             :     ExpandedRecordHeader *erh;
      72         472 :     int         flags = 0;
      73             :     TupleDesc   tupdesc;
      74             :     uint64      tupdesc_id;
      75             :     MemoryContext objcxt;
      76             :     char       *chunk;
      77             : 
      78         472 :     if (type_id != RECORDOID)
      79             :     {
      80             :         /*
      81             :          * Consult the typcache to see if it's a domain over composite, and in
      82             :          * any case to get the tupdesc and tupdesc identifier.
      83             :          */
      84             :         TypeCacheEntry *typentry;
      85             : 
      86         424 :         typentry = lookup_type_cache(type_id,
      87             :                                      TYPECACHE_TUPDESC |
      88             :                                      TYPECACHE_DOMAIN_BASE_INFO);
      89         424 :         if (typentry->typtype == TYPTYPE_DOMAIN)
      90             :         {
      91          70 :             flags |= ER_FLAG_IS_DOMAIN;
      92          70 :             typentry = lookup_type_cache(typentry->domainBaseType,
      93             :                                          TYPECACHE_TUPDESC);
      94             :         }
      95         424 :         if (typentry->tupDesc == NULL)
      96           0 :             ereport(ERROR,
      97             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
      98             :                      errmsg("type %s is not composite",
      99             :                             format_type_be(type_id))));
     100         424 :         tupdesc = typentry->tupDesc;
     101         424 :         tupdesc_id = typentry->tupDesc_identifier;
     102             :     }
     103             :     else
     104             :     {
     105             :         /*
     106             :          * For RECORD types, get the tupdesc and identifier from typcache.
     107             :          */
     108          48 :         tupdesc = lookup_rowtype_tupdesc(type_id, typmod);
     109          48 :         tupdesc_id = assign_record_type_identifier(type_id, typmod);
     110             :     }
     111             : 
     112             :     /*
     113             :      * Allocate private context for expanded object.  We use a regular-size
     114             :      * context, not a small one, to improve the odds that we can fit a tupdesc
     115             :      * into it without needing an extra malloc block.  (This code path doesn't
     116             :      * ever need to copy a tupdesc into the expanded record, but let's be
     117             :      * consistent with the other ways of making an expanded record.)
     118             :      */
     119         472 :     objcxt = AllocSetContextCreate(parentcontext,
     120             :                                    "expanded record",
     121             :                                    ALLOCSET_DEFAULT_SIZES);
     122             : 
     123             :     /*
     124             :      * Since we already know the number of fields in the tupdesc, we can
     125             :      * allocate the dvalues/dnulls arrays along with the record header.  This
     126             :      * is useless if we never need those arrays, but it costs almost nothing,
     127             :      * and it will save a palloc cycle if we do need them.
     128             :      */
     129         472 :     erh = (ExpandedRecordHeader *)
     130         472 :         MemoryContextAlloc(objcxt, MAXALIGN(sizeof(ExpandedRecordHeader))
     131         472 :                            + tupdesc->natts * (sizeof(Datum) + sizeof(bool)));
     132             : 
     133             :     /* Ensure all header fields are initialized to 0/null */
     134         472 :     memset(erh, 0, sizeof(ExpandedRecordHeader));
     135             : 
     136         472 :     EOH_init_header(&erh->hdr, &ER_methods, objcxt);
     137         472 :     erh->er_magic = ER_MAGIC;
     138             : 
     139             :     /* Set up dvalues/dnulls, with no valid contents as yet */
     140         472 :     chunk = (char *) erh + MAXALIGN(sizeof(ExpandedRecordHeader));
     141         472 :     erh->dvalues = (Datum *) chunk;
     142         472 :     erh->dnulls = (bool *) (chunk + tupdesc->natts * sizeof(Datum));
     143         472 :     erh->nfields = tupdesc->natts;
     144             : 
     145             :     /* Fill in composite-type identification info */
     146         472 :     erh->er_decltypeid = type_id;
     147         472 :     erh->er_typeid = tupdesc->tdtypeid;
     148         472 :     erh->er_typmod = tupdesc->tdtypmod;
     149         472 :     erh->er_tupdesc_id = tupdesc_id;
     150             : 
     151         472 :     erh->flags = flags;
     152             : 
     153             :     /*
     154             :      * If what we got from the typcache is a refcounted tupdesc, we need to
     155             :      * acquire our own refcount on it.  We manage the refcount with a memory
     156             :      * context callback rather than assuming that the CurrentResourceOwner is
     157             :      * longer-lived than this expanded object.
     158             :      */
     159         472 :     if (tupdesc->tdrefcount >= 0)
     160             :     {
     161             :         /* Register callback to release the refcount */
     162         472 :         erh->er_mcb.func = ER_mc_callback;
     163         472 :         erh->er_mcb.arg = (void *) erh;
     164         472 :         MemoryContextRegisterResetCallback(erh->hdr.eoh_context,
     165             :                                            &erh->er_mcb);
     166             : 
     167             :         /* And save the pointer */
     168         472 :         erh->er_tupdesc = tupdesc;
     169         472 :         tupdesc->tdrefcount++;
     170             : 
     171             :         /* If we called lookup_rowtype_tupdesc, release the pin it took */
     172         472 :         if (type_id == RECORDOID)
     173          48 :             DecrTupleDescRefCount(tupdesc);
     174             :     }
     175             :     else
     176             :     {
     177             :         /*
     178             :          * If it's not refcounted, just assume it will outlive the expanded
     179             :          * object.  (This can happen for shared record types, for instance.)
     180             :          */
     181           0 :         erh->er_tupdesc = tupdesc;
     182             :     }
     183             : 
     184             :     /*
     185             :      * We don't set ER_FLAG_DVALUES_VALID or ER_FLAG_FVALUE_VALID, so the
     186             :      * record remains logically empty.
     187             :      */
     188             : 
     189         472 :     return erh;
     190             : }
     191             : 
     192             : /*
     193             :  * Build an expanded record of the rowtype defined by the tupdesc
     194             :  *
     195             :  * The tupdesc is copied if necessary (i.e., if we can't just bump its
     196             :  * reference count instead).
     197             :  *
     198             :  * The expanded record is initially "empty", having a state logically
     199             :  * equivalent to a NULL composite value (not ROW(NULL, NULL, ...)).
     200             :  *
     201             :  * The expanded object will be a child of parentcontext.
     202             :  */
     203             : ExpandedRecordHeader *
     204       11398 : make_expanded_record_from_tupdesc(TupleDesc tupdesc,
     205             :                                   MemoryContext parentcontext)
     206             : {
     207             :     ExpandedRecordHeader *erh;
     208             :     uint64      tupdesc_id;
     209             :     MemoryContext objcxt;
     210             :     MemoryContext oldcxt;
     211             :     char       *chunk;
     212             : 
     213       11398 :     if (tupdesc->tdtypeid != RECORDOID)
     214             :     {
     215             :         /*
     216             :          * If it's a named composite type (not RECORD), we prefer to reference
     217             :          * the typcache's copy of the tupdesc, which is guaranteed to be
     218             :          * refcounted (the given tupdesc might not be).  In any case, we need
     219             :          * to consult the typcache to get the correct tupdesc identifier.
     220             :          *
     221             :          * Note that tdtypeid couldn't be a domain type, so we need not
     222             :          * consider that case here.
     223             :          */
     224             :         TypeCacheEntry *typentry;
     225             : 
     226        9110 :         typentry = lookup_type_cache(tupdesc->tdtypeid, TYPECACHE_TUPDESC);
     227        9110 :         if (typentry->tupDesc == NULL)
     228           0 :             ereport(ERROR,
     229             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     230             :                      errmsg("type %s is not composite",
     231             :                             format_type_be(tupdesc->tdtypeid))));
     232        9110 :         tupdesc = typentry->tupDesc;
     233        9110 :         tupdesc_id = typentry->tupDesc_identifier;
     234             :     }
     235             :     else
     236             :     {
     237             :         /*
     238             :          * For RECORD types, get the appropriate unique identifier (possibly
     239             :          * freshly assigned).
     240             :          */
     241        2288 :         tupdesc_id = assign_record_type_identifier(tupdesc->tdtypeid,
     242             :                                                    tupdesc->tdtypmod);
     243             :     }
     244             : 
     245             :     /*
     246             :      * Allocate private context for expanded object.  We use a regular-size
     247             :      * context, not a small one, to improve the odds that we can fit a tupdesc
     248             :      * into it without needing an extra malloc block.
     249             :      */
     250       11398 :     objcxt = AllocSetContextCreate(parentcontext,
     251             :                                    "expanded record",
     252             :                                    ALLOCSET_DEFAULT_SIZES);
     253             : 
     254             :     /*
     255             :      * Since we already know the number of fields in the tupdesc, we can
     256             :      * allocate the dvalues/dnulls arrays along with the record header.  This
     257             :      * is useless if we never need those arrays, but it costs almost nothing,
     258             :      * and it will save a palloc cycle if we do need them.
     259             :      */
     260       11398 :     erh = (ExpandedRecordHeader *)
     261       11398 :         MemoryContextAlloc(objcxt, MAXALIGN(sizeof(ExpandedRecordHeader))
     262       11398 :                            + tupdesc->natts * (sizeof(Datum) + sizeof(bool)));
     263             : 
     264             :     /* Ensure all header fields are initialized to 0/null */
     265       11398 :     memset(erh, 0, sizeof(ExpandedRecordHeader));
     266             : 
     267       11398 :     EOH_init_header(&erh->hdr, &ER_methods, objcxt);
     268       11398 :     erh->er_magic = ER_MAGIC;
     269             : 
     270             :     /* Set up dvalues/dnulls, with no valid contents as yet */
     271       11398 :     chunk = (char *) erh + MAXALIGN(sizeof(ExpandedRecordHeader));
     272       11398 :     erh->dvalues = (Datum *) chunk;
     273       11398 :     erh->dnulls = (bool *) (chunk + tupdesc->natts * sizeof(Datum));
     274       11398 :     erh->nfields = tupdesc->natts;
     275             : 
     276             :     /* Fill in composite-type identification info */
     277       11398 :     erh->er_decltypeid = erh->er_typeid = tupdesc->tdtypeid;
     278       11398 :     erh->er_typmod = tupdesc->tdtypmod;
     279       11398 :     erh->er_tupdesc_id = tupdesc_id;
     280             : 
     281             :     /*
     282             :      * Copy tupdesc if needed, but we prefer to bump its refcount if possible.
     283             :      * We manage the refcount with a memory context callback rather than
     284             :      * assuming that the CurrentResourceOwner is longer-lived than this
     285             :      * expanded object.
     286             :      */
     287       11398 :     if (tupdesc->tdrefcount >= 0)
     288             :     {
     289             :         /* Register callback to release the refcount */
     290        9110 :         erh->er_mcb.func = ER_mc_callback;
     291        9110 :         erh->er_mcb.arg = (void *) erh;
     292        9110 :         MemoryContextRegisterResetCallback(erh->hdr.eoh_context,
     293             :                                            &erh->er_mcb);
     294             : 
     295             :         /* And save the pointer */
     296        9110 :         erh->er_tupdesc = tupdesc;
     297        9110 :         tupdesc->tdrefcount++;
     298             :     }
     299             :     else
     300             :     {
     301             :         /* Just copy it */
     302        2288 :         oldcxt = MemoryContextSwitchTo(objcxt);
     303        2288 :         erh->er_tupdesc = CreateTupleDescCopy(tupdesc);
     304        2288 :         erh->flags |= ER_FLAG_TUPDESC_ALLOCED;
     305        2288 :         MemoryContextSwitchTo(oldcxt);
     306             :     }
     307             : 
     308             :     /*
     309             :      * We don't set ER_FLAG_DVALUES_VALID or ER_FLAG_FVALUE_VALID, so the
     310             :      * record remains logically empty.
     311             :      */
     312             : 
     313       11398 :     return erh;
     314             : }
     315             : 
     316             : /*
     317             :  * Build an expanded record of the same rowtype as the given expanded record
     318             :  *
     319             :  * This is faster than either of the above routines because we can bypass
     320             :  * typcache lookup(s).
     321             :  *
     322             :  * The expanded record is initially "empty" --- we do not copy whatever
     323             :  * tuple might be in the source expanded record.
     324             :  *
     325             :  * The expanded object will be a child of parentcontext.
     326             :  */
     327             : ExpandedRecordHeader *
     328        9144 : make_expanded_record_from_exprecord(ExpandedRecordHeader *olderh,
     329             :                                     MemoryContext parentcontext)
     330             : {
     331             :     ExpandedRecordHeader *erh;
     332        9144 :     TupleDesc   tupdesc = expanded_record_get_tupdesc(olderh);
     333             :     MemoryContext objcxt;
     334             :     MemoryContext oldcxt;
     335             :     char       *chunk;
     336             : 
     337             :     /*
     338             :      * Allocate private context for expanded object.  We use a regular-size
     339             :      * context, not a small one, to improve the odds that we can fit a tupdesc
     340             :      * into it without needing an extra malloc block.
     341             :      */
     342        9144 :     objcxt = AllocSetContextCreate(parentcontext,
     343             :                                    "expanded record",
     344             :                                    ALLOCSET_DEFAULT_SIZES);
     345             : 
     346             :     /*
     347             :      * Since we already know the number of fields in the tupdesc, we can
     348             :      * allocate the dvalues/dnulls arrays along with the record header.  This
     349             :      * is useless if we never need those arrays, but it costs almost nothing,
     350             :      * and it will save a palloc cycle if we do need them.
     351             :      */
     352        9144 :     erh = (ExpandedRecordHeader *)
     353        9144 :         MemoryContextAlloc(objcxt, MAXALIGN(sizeof(ExpandedRecordHeader))
     354        9144 :                            + tupdesc->natts * (sizeof(Datum) + sizeof(bool)));
     355             : 
     356             :     /* Ensure all header fields are initialized to 0/null */
     357        9144 :     memset(erh, 0, sizeof(ExpandedRecordHeader));
     358             : 
     359        9144 :     EOH_init_header(&erh->hdr, &ER_methods, objcxt);
     360        9144 :     erh->er_magic = ER_MAGIC;
     361             : 
     362             :     /* Set up dvalues/dnulls, with no valid contents as yet */
     363        9144 :     chunk = (char *) erh + MAXALIGN(sizeof(ExpandedRecordHeader));
     364        9144 :     erh->dvalues = (Datum *) chunk;
     365        9144 :     erh->dnulls = (bool *) (chunk + tupdesc->natts * sizeof(Datum));
     366        9144 :     erh->nfields = tupdesc->natts;
     367             : 
     368             :     /* Fill in composite-type identification info */
     369        9144 :     erh->er_decltypeid = olderh->er_decltypeid;
     370        9144 :     erh->er_typeid = olderh->er_typeid;
     371        9144 :     erh->er_typmod = olderh->er_typmod;
     372        9144 :     erh->er_tupdesc_id = olderh->er_tupdesc_id;
     373             : 
     374             :     /* The only flag bit that transfers over is IS_DOMAIN */
     375        9144 :     erh->flags = olderh->flags & ER_FLAG_IS_DOMAIN;
     376             : 
     377             :     /*
     378             :      * Copy tupdesc if needed, but we prefer to bump its refcount if possible.
     379             :      * We manage the refcount with a memory context callback rather than
     380             :      * assuming that the CurrentResourceOwner is longer-lived than this
     381             :      * expanded object.
     382             :      */
     383        9144 :     if (tupdesc->tdrefcount >= 0)
     384             :     {
     385             :         /* Register callback to release the refcount */
     386        9144 :         erh->er_mcb.func = ER_mc_callback;
     387        9144 :         erh->er_mcb.arg = (void *) erh;
     388        9144 :         MemoryContextRegisterResetCallback(erh->hdr.eoh_context,
     389             :                                            &erh->er_mcb);
     390             : 
     391             :         /* And save the pointer */
     392        9144 :         erh->er_tupdesc = tupdesc;
     393        9144 :         tupdesc->tdrefcount++;
     394             :     }
     395           0 :     else if (olderh->flags & ER_FLAG_TUPDESC_ALLOCED)
     396             :     {
     397             :         /* We need to make our own copy of the tupdesc */
     398           0 :         oldcxt = MemoryContextSwitchTo(objcxt);
     399           0 :         erh->er_tupdesc = CreateTupleDescCopy(tupdesc);
     400           0 :         erh->flags |= ER_FLAG_TUPDESC_ALLOCED;
     401           0 :         MemoryContextSwitchTo(oldcxt);
     402             :     }
     403             :     else
     404             :     {
     405             :         /*
     406             :          * Assume the tupdesc will outlive this expanded object, just like
     407             :          * we're assuming it will outlive the source object.
     408             :          */
     409           0 :         erh->er_tupdesc = tupdesc;
     410             :     }
     411             : 
     412             :     /*
     413             :      * We don't set ER_FLAG_DVALUES_VALID or ER_FLAG_FVALUE_VALID, so the
     414             :      * record remains logically empty.
     415             :      */
     416             : 
     417        9144 :     return erh;
     418             : }
     419             : 
     420             : /*
     421             :  * Insert given tuple as the value of the expanded record
     422             :  *
     423             :  * It is caller's responsibility that the tuple matches the record's
     424             :  * previously-assigned rowtype.  (However domain constraints, if any,
     425             :  * will be checked here.)
     426             :  *
     427             :  * The tuple is physically copied into the expanded record's local storage
     428             :  * if "copy" is true, otherwise it's caller's responsibility that the tuple
     429             :  * will live as long as the expanded record does.
     430             :  *
     431             :  * Out-of-line field values in the tuple are automatically inlined if
     432             :  * "expand_external" is true, otherwise not.  (The combination copy = false,
     433             :  * expand_external = true is not sensible and not supported.)
     434             :  *
     435             :  * Alternatively, tuple can be NULL, in which case we just set the expanded
     436             :  * record to be empty.
     437             :  */
     438             : void
     439       16934 : expanded_record_set_tuple(ExpandedRecordHeader *erh,
     440             :                           HeapTuple tuple,
     441             :                           bool copy,
     442             :                           bool expand_external)
     443             : {
     444             :     int         oldflags;
     445             :     HeapTuple   oldtuple;
     446             :     char       *oldfstartptr;
     447             :     char       *oldfendptr;
     448             :     int         newflags;
     449             :     HeapTuple   newtuple;
     450             :     MemoryContext oldcxt;
     451             : 
     452             :     /* Shouldn't ever be trying to assign new data to a dummy header */
     453             :     Assert(!(erh->flags & ER_FLAG_IS_DUMMY));
     454             : 
     455             :     /*
     456             :      * Before performing the assignment, see if result will satisfy domain.
     457             :      */
     458       16934 :     if (erh->flags & ER_FLAG_IS_DOMAIN)
     459          68 :         check_domain_for_new_tuple(erh, tuple);
     460             : 
     461             :     /*
     462             :      * If we need to get rid of out-of-line field values, do so, using the
     463             :      * short-term context to avoid leaking whatever cruft the toast fetch
     464             :      * might generate.
     465             :      */
     466       16914 :     if (expand_external && tuple)
     467             :     {
     468             :         /* Assert caller didn't ask for unsupported case */
     469             :         Assert(copy);
     470        1124 :         if (HeapTupleHasExternal(tuple))
     471             :         {
     472          14 :             oldcxt = MemoryContextSwitchTo(get_short_term_cxt(erh));
     473          14 :             tuple = toast_flatten_tuple(tuple, erh->er_tupdesc);
     474          14 :             MemoryContextSwitchTo(oldcxt);
     475             :         }
     476             :         else
     477        1110 :             expand_external = false;    /* need not clean up below */
     478             :     }
     479             : 
     480             :     /*
     481             :      * Initialize new flags, keeping only non-data status bits.
     482             :      */
     483       16914 :     oldflags = erh->flags;
     484       16914 :     newflags = oldflags & ER_FLAGS_NON_DATA;
     485             : 
     486             :     /*
     487             :      * Copy tuple into local storage if needed.  We must be sure this succeeds
     488             :      * before we start to modify the expanded record's state.
     489             :      */
     490       16914 :     if (copy && tuple)
     491             :     {
     492        4380 :         oldcxt = MemoryContextSwitchTo(erh->hdr.eoh_context);
     493        4380 :         newtuple = heap_copytuple(tuple);
     494        4380 :         newflags |= ER_FLAG_FVALUE_ALLOCED;
     495        4380 :         MemoryContextSwitchTo(oldcxt);
     496             : 
     497             :         /* We can now flush anything that detoasting might have leaked. */
     498        8760 :         if (expand_external)
     499          14 :             MemoryContextReset(erh->er_short_term_cxt);
     500             :     }
     501             :     else
     502       12534 :         newtuple = tuple;
     503             : 
     504             :     /* Make copies of fields we're about to overwrite */
     505       16914 :     oldtuple = erh->fvalue;
     506       16914 :     oldfstartptr = erh->fstartptr;
     507       16914 :     oldfendptr = erh->fendptr;
     508             : 
     509             :     /*
     510             :      * It's now safe to update the expanded record's state.
     511             :      */
     512       16914 :     if (newtuple)
     513             :     {
     514             :         /* Save flat representation */
     515       16890 :         erh->fvalue = newtuple;
     516       16890 :         erh->fstartptr = (char *) newtuple->t_data;
     517       16890 :         erh->fendptr = ((char *) newtuple->t_data) + newtuple->t_len;
     518       16890 :         newflags |= ER_FLAG_FVALUE_VALID;
     519             : 
     520             :         /* Remember if we have any out-of-line field values */
     521       16890 :         if (HeapTupleHasExternal(newtuple))
     522         132 :             newflags |= ER_FLAG_HAVE_EXTERNAL;
     523             :     }
     524             :     else
     525             :     {
     526          24 :         erh->fvalue = NULL;
     527          24 :         erh->fstartptr = erh->fendptr = NULL;
     528             :     }
     529             : 
     530       16914 :     erh->flags = newflags;
     531             : 
     532             :     /* Reset flat-size info; we don't bother to make it valid now */
     533       16914 :     erh->flat_size = 0;
     534             : 
     535             :     /*
     536             :      * Now, release any storage belonging to old field values.  It's safe to
     537             :      * do this because ER_FLAG_DVALUES_VALID is no longer set in erh->flags;
     538             :      * even if we fail partway through, the record is valid, and at worst
     539             :      * we've failed to reclaim some space.
     540             :      */
     541       16914 :     if (oldflags & ER_FLAG_DVALUES_ALLOCED)
     542             :     {
     543          32 :         TupleDesc   tupdesc = erh->er_tupdesc;
     544             :         int         i;
     545             : 
     546         160 :         for (i = 0; i < erh->nfields; i++)
     547             :         {
     548         240 :             if (!erh->dnulls[i] &&
     549         112 :                 !(TupleDescAttr(tupdesc, i)->attbyval))
     550             :             {
     551          80 :                 char       *oldValue = (char *) DatumGetPointer(erh->dvalues[i]);
     552             : 
     553          80 :                 if (oldValue < oldfstartptr || oldValue >= oldfendptr)
     554          40 :                     pfree(oldValue);
     555             :             }
     556             :         }
     557             :     }
     558             : 
     559             :     /* Likewise free the old tuple, if it was locally allocated */
     560       16914 :     if (oldflags & ER_FLAG_FVALUE_ALLOCED)
     561        1768 :         heap_freetuple(oldtuple);
     562             : 
     563             :     /* We won't make a new deconstructed representation until/unless needed */
     564       16914 : }
     565             : 
     566             : /*
     567             :  * make_expanded_record_from_datum: build expanded record from composite Datum
     568             :  *
     569             :  * This combines the functions of make_expanded_record_from_typeid and
     570             :  * expanded_record_set_tuple.  However, we do not force a lookup of the
     571             :  * tupdesc immediately, reasoning that it might never be needed.
     572             :  *
     573             :  * The expanded object will be a child of parentcontext.
     574             :  *
     575             :  * Note: a composite datum cannot self-identify as being of a domain type,
     576             :  * so we need not consider domain cases here.
     577             :  */
     578             : Datum
     579           0 : make_expanded_record_from_datum(Datum recorddatum, MemoryContext parentcontext)
     580             : {
     581             :     ExpandedRecordHeader *erh;
     582             :     HeapTupleHeader tuphdr;
     583             :     HeapTupleData tmptup;
     584             :     HeapTuple   newtuple;
     585             :     MemoryContext objcxt;
     586             :     MemoryContext oldcxt;
     587             : 
     588             :     /*
     589             :      * Allocate private context for expanded object.  We use a regular-size
     590             :      * context, not a small one, to improve the odds that we can fit a tupdesc
     591             :      * into it without needing an extra malloc block.
     592             :      */
     593           0 :     objcxt = AllocSetContextCreate(parentcontext,
     594             :                                    "expanded record",
     595             :                                    ALLOCSET_DEFAULT_SIZES);
     596             : 
     597             :     /* Set up expanded record header, initializing fields to 0/null */
     598           0 :     erh = (ExpandedRecordHeader *)
     599             :         MemoryContextAllocZero(objcxt, sizeof(ExpandedRecordHeader));
     600             : 
     601           0 :     EOH_init_header(&erh->hdr, &ER_methods, objcxt);
     602           0 :     erh->er_magic = ER_MAGIC;
     603             : 
     604             :     /*
     605             :      * Detoast and copy source record into private context, as a HeapTuple.
     606             :      * (If we actually have to detoast the source, we'll leak some memory in
     607             :      * the caller's context, but it doesn't seem worth worrying about.)
     608             :      */
     609           0 :     tuphdr = DatumGetHeapTupleHeader(recorddatum);
     610             : 
     611           0 :     tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr);
     612           0 :     ItemPointerSetInvalid(&(tmptup.t_self));
     613           0 :     tmptup.t_tableOid = InvalidOid;
     614           0 :     tmptup.t_data = tuphdr;
     615             : 
     616           0 :     oldcxt = MemoryContextSwitchTo(objcxt);
     617           0 :     newtuple = heap_copytuple(&tmptup);
     618           0 :     erh->flags |= ER_FLAG_FVALUE_ALLOCED;
     619           0 :     MemoryContextSwitchTo(oldcxt);
     620             : 
     621             :     /* Fill in composite-type identification info */
     622           0 :     erh->er_decltypeid = erh->er_typeid = HeapTupleHeaderGetTypeId(tuphdr);
     623           0 :     erh->er_typmod = HeapTupleHeaderGetTypMod(tuphdr);
     624             : 
     625             :     /* remember we have a flat representation */
     626           0 :     erh->fvalue = newtuple;
     627           0 :     erh->fstartptr = (char *) newtuple->t_data;
     628           0 :     erh->fendptr = ((char *) newtuple->t_data) + newtuple->t_len;
     629           0 :     erh->flags |= ER_FLAG_FVALUE_VALID;
     630             : 
     631             :     /* Shouldn't need to set ER_FLAG_HAVE_EXTERNAL */
     632             :     Assert(!HeapTupleHeaderHasExternal(tuphdr));
     633             : 
     634             :     /*
     635             :      * We won't look up the tupdesc till we have to, nor make a deconstructed
     636             :      * representation.  We don't have enough info to fill flat_size and
     637             :      * friends, either.
     638             :      */
     639             : 
     640             :     /* return a R/W pointer to the expanded record */
     641           0 :     return EOHPGetRWDatum(&erh->hdr);
     642             : }
     643             : 
     644             : /*
     645             :  * get_flat_size method for expanded records
     646             :  *
     647             :  * Note: call this in a reasonably short-lived memory context, in case of
     648             :  * memory leaks from activities such as detoasting.
     649             :  */
     650             : static Size
     651         922 : ER_get_flat_size(ExpandedObjectHeader *eohptr)
     652             : {
     653         922 :     ExpandedRecordHeader *erh = (ExpandedRecordHeader *) eohptr;
     654             :     TupleDesc   tupdesc;
     655             :     Size        len;
     656             :     Size        data_len;
     657             :     int         hoff;
     658             :     bool        hasnull;
     659             :     int         i;
     660             : 
     661             :     Assert(erh->er_magic == ER_MAGIC);
     662             : 
     663             :     /*
     664             :      * The flat representation has to be a valid composite datum.  Make sure
     665             :      * that we have a registered, not anonymous, RECORD type.
     666             :      */
     667         968 :     if (erh->er_typeid == RECORDOID &&
     668          46 :         erh->er_typmod < 0)
     669             :     {
     670          12 :         tupdesc = expanded_record_get_tupdesc(erh);
     671          12 :         assign_record_type_typmod(tupdesc);
     672          12 :         erh->er_typmod = tupdesc->tdtypmod;
     673             :     }
     674             : 
     675             :     /*
     676             :      * If we have a valid flattened value without out-of-line fields, we can
     677             :      * just use it as-is.
     678             :      */
     679        1650 :     if (erh->flags & ER_FLAG_FVALUE_VALID &&
     680         728 :         !(erh->flags & ER_FLAG_HAVE_EXTERNAL))
     681         696 :         return erh->fvalue->t_len;
     682             : 
     683             :     /* If we have a cached size value, believe that */
     684         226 :     if (erh->flat_size)
     685          10 :         return erh->flat_size;
     686             : 
     687             :     /* If we haven't yet deconstructed the tuple, do that */
     688         216 :     if (!(erh->flags & ER_FLAG_DVALUES_VALID))
     689          32 :         deconstruct_expanded_record(erh);
     690             : 
     691             :     /* Tuple descriptor must be valid by now */
     692         216 :     tupdesc = erh->er_tupdesc;
     693             : 
     694             :     /*
     695             :      * Composite datums mustn't contain any out-of-line values.
     696             :      */
     697         216 :     if (erh->flags & ER_FLAG_HAVE_EXTERNAL)
     698             :     {
     699         160 :         for (i = 0; i < erh->nfields; i++)
     700             :         {
     701         128 :             Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
     702             : 
     703         240 :             if (!erh->dnulls[i] &&
     704         272 :                 !attr->attbyval && attr->attlen == -1 &&
     705          80 :                 VARATT_IS_EXTERNAL(DatumGetPointer(erh->dvalues[i])))
     706             :             {
     707             :                 /*
     708             :                  * expanded_record_set_field_internal can do the actual work
     709             :                  * of detoasting.  It needn't recheck domain constraints.
     710             :                  */
     711          40 :                 expanded_record_set_field_internal(erh, i + 1,
     712          40 :                                                    erh->dvalues[i], false,
     713             :                                                    true,
     714             :                                                    false);
     715             :             }
     716             :         }
     717             : 
     718             :         /*
     719             :          * We have now removed all external field values, so we can clear the
     720             :          * flag about them.  This won't cause ER_flatten_into() to mistakenly
     721             :          * take the fast path, since expanded_record_set_field() will have
     722             :          * cleared ER_FLAG_FVALUE_VALID.
     723             :          */
     724          32 :         erh->flags &= ~ER_FLAG_HAVE_EXTERNAL;
     725             :     }
     726             : 
     727             :     /* Test if we currently have any null values */
     728         216 :     hasnull = false;
     729         660 :     for (i = 0; i < erh->nfields; i++)
     730             :     {
     731         476 :         if (erh->dnulls[i])
     732             :         {
     733          32 :             hasnull = true;
     734          32 :             break;
     735             :         }
     736             :     }
     737             : 
     738             :     /* Determine total space needed */
     739         216 :     len = offsetof(HeapTupleHeaderData, t_bits);
     740             : 
     741         216 :     if (hasnull)
     742          32 :         len += BITMAPLEN(tupdesc->natts);
     743             : 
     744         216 :     hoff = len = MAXALIGN(len); /* align user data safely */
     745             : 
     746         216 :     data_len = heap_compute_data_size(tupdesc, erh->dvalues, erh->dnulls);
     747             : 
     748         216 :     len += data_len;
     749             : 
     750             :     /* Cache for next time */
     751         216 :     erh->flat_size = len;
     752         216 :     erh->data_len = data_len;
     753         216 :     erh->hoff = hoff;
     754         216 :     erh->hasnull = hasnull;
     755             : 
     756         216 :     return len;
     757             : }
     758             : 
     759             : /*
     760             :  * flatten_into method for expanded records
     761             :  */
     762             : static void
     763         922 : ER_flatten_into(ExpandedObjectHeader *eohptr,
     764             :                 void *result, Size allocated_size)
     765             : {
     766         922 :     ExpandedRecordHeader *erh = (ExpandedRecordHeader *) eohptr;
     767         922 :     HeapTupleHeader tuphdr = (HeapTupleHeader) result;
     768             :     TupleDesc   tupdesc;
     769             : 
     770             :     Assert(erh->er_magic == ER_MAGIC);
     771             : 
     772             :     /* Easy if we have a valid flattened value without out-of-line fields */
     773        1618 :     if (erh->flags & ER_FLAG_FVALUE_VALID &&
     774         696 :         !(erh->flags & ER_FLAG_HAVE_EXTERNAL))
     775             :     {
     776             :         Assert(allocated_size == erh->fvalue->t_len);
     777         696 :         memcpy(tuphdr, erh->fvalue->t_data, allocated_size);
     778             :         /* The original flattened value might not have datum header fields */
     779         696 :         HeapTupleHeaderSetDatumLength(tuphdr, allocated_size);
     780         696 :         HeapTupleHeaderSetTypeId(tuphdr, erh->er_typeid);
     781         696 :         HeapTupleHeaderSetTypMod(tuphdr, erh->er_typmod);
     782         696 :         return;
     783             :     }
     784             : 
     785             :     /* Else allocation should match previous get_flat_size result */
     786             :     Assert(allocated_size == erh->flat_size);
     787             : 
     788             :     /* We'll need the tuple descriptor */
     789         226 :     tupdesc = expanded_record_get_tupdesc(erh);
     790             : 
     791             :     /* We must ensure that any pad space is zero-filled */
     792         226 :     memset(tuphdr, 0, allocated_size);
     793             : 
     794             :     /* Set up header fields of composite Datum */
     795         226 :     HeapTupleHeaderSetDatumLength(tuphdr, allocated_size);
     796         226 :     HeapTupleHeaderSetTypeId(tuphdr, erh->er_typeid);
     797         226 :     HeapTupleHeaderSetTypMod(tuphdr, erh->er_typmod);
     798             :     /* We also make sure that t_ctid is invalid unless explicitly set */
     799         226 :     ItemPointerSetInvalid(&(tuphdr->t_ctid));
     800             : 
     801         226 :     HeapTupleHeaderSetNatts(tuphdr, tupdesc->natts);
     802         226 :     tuphdr->t_hoff = erh->hoff;
     803             : 
     804             :     /* And fill the data area from dvalues/dnulls */
     805         452 :     heap_fill_tuple(tupdesc,
     806             :                     erh->dvalues,
     807             :                     erh->dnulls,
     808         226 :                     (char *) tuphdr + erh->hoff,
     809             :                     erh->data_len,
     810             :                     &tuphdr->t_infomask,
     811         226 :                     (erh->hasnull ? tuphdr->t_bits : NULL));
     812             : }
     813             : 
     814             : /*
     815             :  * Look up the tupdesc for the expanded record's actual type
     816             :  *
     817             :  * Note: code internal to this module is allowed to just fetch
     818             :  * erh->er_tupdesc if ER_FLAG_DVALUES_VALID is set; otherwise it should call
     819             :  * expanded_record_get_tupdesc.  This function is the out-of-line portion
     820             :  * of expanded_record_get_tupdesc.
     821             :  */
     822             : TupleDesc
     823           0 : expanded_record_fetch_tupdesc(ExpandedRecordHeader *erh)
     824             : {
     825             :     TupleDesc   tupdesc;
     826             : 
     827             :     /* Easy if we already have it (but caller should have checked already) */
     828           0 :     if (erh->er_tupdesc)
     829           0 :         return erh->er_tupdesc;
     830             : 
     831             :     /* Lookup the composite type's tupdesc using the typcache */
     832           0 :     tupdesc = lookup_rowtype_tupdesc(erh->er_typeid, erh->er_typmod);
     833             : 
     834             :     /*
     835             :      * If it's a refcounted tupdesc rather than a statically allocated one, we
     836             :      * want to manage the refcount with a memory context callback rather than
     837             :      * assuming that the CurrentResourceOwner is longer-lived than this
     838             :      * expanded object.
     839             :      */
     840           0 :     if (tupdesc->tdrefcount >= 0)
     841             :     {
     842             :         /* Register callback if we didn't already */
     843           0 :         if (erh->er_mcb.arg == NULL)
     844             :         {
     845           0 :             erh->er_mcb.func = ER_mc_callback;
     846           0 :             erh->er_mcb.arg = (void *) erh;
     847           0 :             MemoryContextRegisterResetCallback(erh->hdr.eoh_context,
     848             :                                                &erh->er_mcb);
     849             :         }
     850             : 
     851             :         /* Remember our own pointer */
     852           0 :         erh->er_tupdesc = tupdesc;
     853           0 :         tupdesc->tdrefcount++;
     854             : 
     855             :         /* Release the pin lookup_rowtype_tupdesc acquired */
     856           0 :         DecrTupleDescRefCount(tupdesc);
     857             :     }
     858             :     else
     859             :     {
     860             :         /* Just remember the pointer */
     861           0 :         erh->er_tupdesc = tupdesc;
     862             :     }
     863             : 
     864             :     /* In either case, fetch the process-global ID for this tupdesc */
     865           0 :     erh->er_tupdesc_id = assign_record_type_identifier(tupdesc->tdtypeid,
     866             :                                                        tupdesc->tdtypmod);
     867             : 
     868           0 :     return tupdesc;
     869             : }
     870             : 
     871             : /*
     872             :  * Get a HeapTuple representing the current value of the expanded record
     873             :  *
     874             :  * If valid, the originally stored tuple is returned, so caller must not
     875             :  * scribble on it.  Otherwise, we return a HeapTuple created in the current
     876             :  * memory context.  In either case, no attempt has been made to inline
     877             :  * out-of-line toasted values, so the tuple isn't usable as a composite
     878             :  * datum.
     879             :  *
     880             :  * Returns NULL if expanded record is empty.
     881             :  */
     882             : HeapTuple
     883        8024 : expanded_record_get_tuple(ExpandedRecordHeader *erh)
     884             : {
     885             :     /* Easy case if we still have original tuple */
     886        8024 :     if (erh->flags & ER_FLAG_FVALUE_VALID)
     887        6660 :         return erh->fvalue;
     888             : 
     889             :     /* Else just build a tuple from datums */
     890        1364 :     if (erh->flags & ER_FLAG_DVALUES_VALID)
     891        1364 :         return heap_form_tuple(erh->er_tupdesc, erh->dvalues, erh->dnulls);
     892             : 
     893             :     /* Expanded record is empty */
     894           0 :     return NULL;
     895             : }
     896             : 
     897             : /*
     898             :  * Memory context reset callback for cleaning up external resources
     899             :  */
     900             : static void
     901       18726 : ER_mc_callback(void *arg)
     902             : {
     903       18726 :     ExpandedRecordHeader *erh = (ExpandedRecordHeader *) arg;
     904       18726 :     TupleDesc   tupdesc = erh->er_tupdesc;
     905             : 
     906             :     /* Release our privately-managed tupdesc refcount, if any */
     907       18726 :     if (tupdesc)
     908             :     {
     909       18726 :         erh->er_tupdesc = NULL; /* just for luck */
     910       18726 :         if (tupdesc->tdrefcount > 0)
     911             :         {
     912       18726 :             if (--tupdesc->tdrefcount == 0)
     913           0 :                 FreeTupleDesc(tupdesc);
     914             :         }
     915             :     }
     916       18726 : }
     917             : 
     918             : /*
     919             :  * DatumGetExpandedRecord: get a writable expanded record from an input argument
     920             :  *
     921             :  * Caution: if the input is a read/write pointer, this returns the input
     922             :  * argument; so callers must be sure that their changes are "safe", that is
     923             :  * they cannot leave the record in a corrupt state.
     924             :  */
     925             : ExpandedRecordHeader *
     926           0 : DatumGetExpandedRecord(Datum d)
     927             : {
     928             :     /* If it's a writable expanded record already, just return it */
     929           0 :     if (VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(d)))
     930             :     {
     931           0 :         ExpandedRecordHeader *erh = (ExpandedRecordHeader *) DatumGetEOHP(d);
     932             : 
     933             :         Assert(erh->er_magic == ER_MAGIC);
     934           0 :         return erh;
     935             :     }
     936             : 
     937             :     /* Else expand the hard way */
     938           0 :     d = make_expanded_record_from_datum(d, CurrentMemoryContext);
     939           0 :     return (ExpandedRecordHeader *) DatumGetEOHP(d);
     940             : }
     941             : 
     942             : /*
     943             :  * Create the Datum/isnull representation of an expanded record object
     944             :  * if we didn't do so already.  After calling this, it's OK to read the
     945             :  * dvalues/dnulls arrays directly, rather than going through get_field.
     946             :  *
     947             :  * Note that if the object is currently empty ("null"), this will change
     948             :  * it to represent a row of nulls.
     949             :  */
     950             : void
     951       12878 : deconstruct_expanded_record(ExpandedRecordHeader *erh)
     952             : {
     953             :     TupleDesc   tupdesc;
     954             :     Datum      *dvalues;
     955             :     bool       *dnulls;
     956             :     int         nfields;
     957             : 
     958       12878 :     if (erh->flags & ER_FLAG_DVALUES_VALID)
     959          30 :         return;                 /* already valid, nothing to do */
     960             : 
     961             :     /* We'll need the tuple descriptor */
     962       12848 :     tupdesc = expanded_record_get_tupdesc(erh);
     963             : 
     964             :     /*
     965             :      * Allocate arrays in private context, if we don't have them already.  We
     966             :      * don't expect to see a change in nfields here, so while we cope if it
     967             :      * happens, we don't bother avoiding a leak of the old arrays (which might
     968             :      * not be separately palloc'd, anyway).
     969             :      */
     970       12848 :     nfields = tupdesc->natts;
     971       12848 :     if (erh->dvalues == NULL || erh->nfields != nfields)
     972           0 :     {
     973             :         char       *chunk;
     974             : 
     975             :         /*
     976             :          * To save a palloc cycle, we allocate both the Datum and isnull
     977             :          * arrays in one palloc chunk.
     978             :          */
     979           0 :         chunk = MemoryContextAlloc(erh->hdr.eoh_context,
     980             :                                    nfields * (sizeof(Datum) + sizeof(bool)));
     981           0 :         dvalues = (Datum *) chunk;
     982           0 :         dnulls = (bool *) (chunk + nfields * sizeof(Datum));
     983           0 :         erh->dvalues = dvalues;
     984           0 :         erh->dnulls = dnulls;
     985           0 :         erh->nfields = nfields;
     986             :     }
     987             :     else
     988             :     {
     989       12848 :         dvalues = erh->dvalues;
     990       12848 :         dnulls = erh->dnulls;
     991             :     }
     992             : 
     993       12848 :     if (erh->flags & ER_FLAG_FVALUE_VALID)
     994             :     {
     995             :         /* Deconstruct tuple */
     996       12652 :         heap_deform_tuple(erh->fvalue, tupdesc, dvalues, dnulls);
     997             :     }
     998             :     else
     999             :     {
    1000             :         /* If record was empty, instantiate it as a row of nulls */
    1001         196 :         memset(dvalues, 0, nfields * sizeof(Datum));
    1002         196 :         memset(dnulls, true, nfields * sizeof(bool));
    1003             :     }
    1004             : 
    1005             :     /* Mark the dvalues as valid */
    1006       12848 :     erh->flags |= ER_FLAG_DVALUES_VALID;
    1007             : }
    1008             : 
    1009             : /*
    1010             :  * Look up a record field by name
    1011             :  *
    1012             :  * If there is a field named "fieldname", fill in the contents of finfo
    1013             :  * and return "true".  Else return "false" without changing *finfo.
    1014             :  */
    1015             : bool
    1016        3360 : expanded_record_lookup_field(ExpandedRecordHeader *erh, const char *fieldname,
    1017             :                              ExpandedRecordFieldInfo *finfo)
    1018             : {
    1019             :     TupleDesc   tupdesc;
    1020             :     int         fno;
    1021             :     Form_pg_attribute attr;
    1022             :     const FormData_pg_attribute *sysattr;
    1023             : 
    1024        3360 :     tupdesc = expanded_record_get_tupdesc(erh);
    1025             : 
    1026             :     /* First, check user-defined attributes */
    1027       12168 :     for (fno = 0; fno < tupdesc->natts; fno++)
    1028             :     {
    1029       12152 :         attr = TupleDescAttr(tupdesc, fno);
    1030       15496 :         if (namestrcmp(&attr->attname, fieldname) == 0 &&
    1031        3344 :             !attr->attisdropped)
    1032             :         {
    1033        3344 :             finfo->fnumber = attr->attnum;
    1034        3344 :             finfo->ftypeid = attr->atttypid;
    1035        3344 :             finfo->ftypmod = attr->atttypmod;
    1036        3344 :             finfo->fcollation = attr->attcollation;
    1037        3344 :             return true;
    1038             :         }
    1039             :     }
    1040             : 
    1041             :     /* How about system attributes? */
    1042          16 :     sysattr = SystemAttributeByName(fieldname);
    1043          16 :     if (sysattr != NULL)
    1044             :     {
    1045           4 :         finfo->fnumber = sysattr->attnum;
    1046           4 :         finfo->ftypeid = sysattr->atttypid;
    1047           4 :         finfo->ftypmod = sysattr->atttypmod;
    1048           4 :         finfo->fcollation = sysattr->attcollation;
    1049           4 :         return true;
    1050             :     }
    1051             : 
    1052          12 :     return false;
    1053             : }
    1054             : 
    1055             : /*
    1056             :  * Fetch value of record field
    1057             :  *
    1058             :  * expanded_record_get_field is the frontend for this; it handles the
    1059             :  * easy inline-able cases.
    1060             :  */
    1061             : Datum
    1062       12464 : expanded_record_fetch_field(ExpandedRecordHeader *erh, int fnumber,
    1063             :                             bool *isnull)
    1064             : {
    1065       12464 :     if (fnumber > 0)
    1066             :     {
    1067             :         /* Empty record has null fields */
    1068       12456 :         if (ExpandedRecordIsEmpty(erh))
    1069             :         {
    1070          22 :             *isnull = true;
    1071          22 :             return (Datum) 0;
    1072             :         }
    1073             :         /* Make sure we have deconstructed form */
    1074       12434 :         deconstruct_expanded_record(erh);
    1075             :         /* Out-of-range field number reads as null */
    1076       12434 :         if (unlikely(fnumber > erh->nfields))
    1077             :         {
    1078           0 :             *isnull = true;
    1079           0 :             return (Datum) 0;
    1080             :         }
    1081       12434 :         *isnull = erh->dnulls[fnumber - 1];
    1082       12434 :         return erh->dvalues[fnumber - 1];
    1083             :     }
    1084             :     else
    1085             :     {
    1086             :         /* System columns read as null if we haven't got flat tuple */
    1087           8 :         if (erh->fvalue == NULL)
    1088             :         {
    1089           0 :             *isnull = true;
    1090           0 :             return (Datum) 0;
    1091             :         }
    1092             :         /* heap_getsysattr doesn't actually use tupdesc, so just pass null */
    1093           8 :         return heap_getsysattr(erh->fvalue, fnumber, NULL, isnull);
    1094             :     }
    1095             : }
    1096             : 
    1097             : /*
    1098             :  * Set value of record field
    1099             :  *
    1100             :  * If the expanded record is of domain type, the assignment will be rejected
    1101             :  * (without changing the record's state) if the domain's constraints would
    1102             :  * be violated.
    1103             :  *
    1104             :  * If expand_external is true and newValue is an out-of-line value, we'll
    1105             :  * forcibly detoast it so that the record does not depend on external storage.
    1106             :  *
    1107             :  * Internal callers can pass check_constraints = false to skip application
    1108             :  * of domain constraints.  External callers should never do that.
    1109             :  */
    1110             : void
    1111        1724 : expanded_record_set_field_internal(ExpandedRecordHeader *erh, int fnumber,
    1112             :                                    Datum newValue, bool isnull,
    1113             :                                    bool expand_external,
    1114             :                                    bool check_constraints)
    1115             : {
    1116             :     TupleDesc   tupdesc;
    1117             :     Form_pg_attribute attr;
    1118             :     Datum      *dvalues;
    1119             :     bool       *dnulls;
    1120             :     char       *oldValue;
    1121             : 
    1122             :     /*
    1123             :      * Shouldn't ever be trying to assign new data to a dummy header, except
    1124             :      * in the case of an internal call for field inlining.
    1125             :      */
    1126             :     Assert(!(erh->flags & ER_FLAG_IS_DUMMY) || !check_constraints);
    1127             : 
    1128             :     /* Before performing the assignment, see if result will satisfy domain */
    1129        1724 :     if ((erh->flags & ER_FLAG_IS_DOMAIN) && check_constraints)
    1130          24 :         check_domain_for_new_field(erh, fnumber, newValue, isnull);
    1131             : 
    1132             :     /* If we haven't yet deconstructed the tuple, do that */
    1133        1718 :     if (!(erh->flags & ER_FLAG_DVALUES_VALID))
    1134         236 :         deconstruct_expanded_record(erh);
    1135             : 
    1136             :     /* Tuple descriptor must be valid by now */
    1137        1718 :     tupdesc = erh->er_tupdesc;
    1138             :     Assert(erh->nfields == tupdesc->natts);
    1139             : 
    1140             :     /* Caller error if fnumber is system column or nonexistent column */
    1141        1718 :     if (unlikely(fnumber <= 0 || fnumber > erh->nfields))
    1142           0 :         elog(ERROR, "cannot assign to field %d of expanded record", fnumber);
    1143             : 
    1144             :     /*
    1145             :      * Copy new field value into record's context, and deal with detoasting,
    1146             :      * if needed.
    1147             :      */
    1148        1718 :     attr = TupleDescAttr(tupdesc, fnumber - 1);
    1149        1718 :     if (!isnull && !attr->attbyval)
    1150             :     {
    1151             :         MemoryContext oldcxt;
    1152             : 
    1153             :         /* If requested, detoast any external value */
    1154        1244 :         if (expand_external)
    1155             :         {
    1156         144 :             if (attr->attlen == -1 &&
    1157          72 :                 VARATT_IS_EXTERNAL(DatumGetPointer(newValue)))
    1158             :             {
    1159             :                 /* Detoasting should be done in short-lived context. */
    1160          42 :                 oldcxt = MemoryContextSwitchTo(get_short_term_cxt(erh));
    1161          42 :                 newValue = PointerGetDatum(heap_tuple_fetch_attr((struct varlena *) DatumGetPointer(newValue)));
    1162          42 :                 MemoryContextSwitchTo(oldcxt);
    1163             :             }
    1164             :             else
    1165          30 :                 expand_external = false;    /* need not clean up below */
    1166             :         }
    1167             : 
    1168             :         /* Copy value into record's context */
    1169        1244 :         oldcxt = MemoryContextSwitchTo(erh->hdr.eoh_context);
    1170        1244 :         newValue = datumCopy(newValue, false, attr->attlen);
    1171        1244 :         MemoryContextSwitchTo(oldcxt);
    1172             : 
    1173             :         /* We can now flush anything that detoasting might have leaked */
    1174        1244 :         if (expand_external)
    1175          42 :             MemoryContextReset(erh->er_short_term_cxt);
    1176             : 
    1177             :         /* Remember that we have field(s) that may need to be pfree'd */
    1178        1244 :         erh->flags |= ER_FLAG_DVALUES_ALLOCED;
    1179             : 
    1180             :         /*
    1181             :          * While we're here, note whether it's an external toasted value,
    1182             :          * because that could mean we need to inline it later.  (Think not to
    1183             :          * merge this into the previous expand_external logic: datumCopy could
    1184             :          * by itself have made the value non-external.)
    1185             :          */
    1186        2488 :         if (attr->attlen == -1 &&
    1187        1244 :             VARATT_IS_EXTERNAL(DatumGetPointer(newValue)))
    1188           0 :             erh->flags |= ER_FLAG_HAVE_EXTERNAL;
    1189             :     }
    1190             : 
    1191             :     /*
    1192             :      * We're ready to make irreversible changes.
    1193             :      */
    1194        1718 :     dvalues = erh->dvalues;
    1195        1718 :     dnulls = erh->dnulls;
    1196             : 
    1197             :     /* Flattened value will no longer represent record accurately */
    1198        1718 :     erh->flags &= ~ER_FLAG_FVALUE_VALID;
    1199             :     /* And we don't know the flattened size either */
    1200        1718 :     erh->flat_size = 0;
    1201             : 
    1202             :     /* Grab old field value for pfree'ing, if needed. */
    1203        1718 :     if (!attr->attbyval && !dnulls[fnumber - 1])
    1204        1178 :         oldValue = (char *) DatumGetPointer(dvalues[fnumber - 1]);
    1205             :     else
    1206         540 :         oldValue = NULL;
    1207             : 
    1208             :     /* And finally we can insert the new field. */
    1209        1718 :     dvalues[fnumber - 1] = newValue;
    1210        1718 :     dnulls[fnumber - 1] = isnull;
    1211             : 
    1212             :     /*
    1213             :      * Free old field if needed; this keeps repeated field replacements from
    1214             :      * bloating the record's storage.  If the pfree somehow fails, it won't
    1215             :      * corrupt the record.
    1216             :      *
    1217             :      * If we're updating a dummy header, we can't risk pfree'ing the old
    1218             :      * value, because most likely the expanded record's main header still has
    1219             :      * a pointer to it.  This won't result in any sustained memory leak, since
    1220             :      * whatever we just allocated here is in the short-lived domain check
    1221             :      * context.
    1222             :      */
    1223        1718 :     if (oldValue && !(erh->flags & ER_FLAG_IS_DUMMY))
    1224             :     {
    1225             :         /* Don't try to pfree a part of the original flat record */
    1226        1178 :         if (oldValue < erh->fstartptr || oldValue >= erh->fendptr)
    1227          20 :             pfree(oldValue);
    1228             :     }
    1229        1718 : }
    1230             : 
    1231             : /*
    1232             :  * Set all record field(s)
    1233             :  *
    1234             :  * Caller must ensure that the provided datums are of the right types
    1235             :  * to match the record's previously assigned rowtype.
    1236             :  *
    1237             :  * If expand_external is true, we'll forcibly detoast out-of-line field values
    1238             :  * so that the record does not depend on external storage.
    1239             :  *
    1240             :  * Unlike repeated application of expanded_record_set_field(), this does not
    1241             :  * guarantee to leave the expanded record in a non-corrupt state in event
    1242             :  * of an error.  Typically it would only be used for initializing a new
    1243             :  * expanded record.  Also, because we expect this to be applied at most once
    1244             :  * in the lifespan of an expanded record, we do not worry about any cruft
    1245             :  * that detoasting might leak.
    1246             :  */
    1247             : void
    1248          86 : expanded_record_set_fields(ExpandedRecordHeader *erh,
    1249             :                            const Datum *newValues, const bool *isnulls,
    1250             :                            bool expand_external)
    1251             : {
    1252             :     TupleDesc   tupdesc;
    1253             :     Datum      *dvalues;
    1254             :     bool       *dnulls;
    1255             :     int         fnumber;
    1256             :     MemoryContext oldcxt;
    1257             : 
    1258             :     /* Shouldn't ever be trying to assign new data to a dummy header */
    1259             :     Assert(!(erh->flags & ER_FLAG_IS_DUMMY));
    1260             : 
    1261             :     /* If we haven't yet deconstructed the tuple, do that */
    1262          86 :     if (!(erh->flags & ER_FLAG_DVALUES_VALID))
    1263          86 :         deconstruct_expanded_record(erh);
    1264             : 
    1265             :     /* Tuple descriptor must be valid by now */
    1266          86 :     tupdesc = erh->er_tupdesc;
    1267             :     Assert(erh->nfields == tupdesc->natts);
    1268             : 
    1269             :     /* Flattened value will no longer represent record accurately */
    1270          86 :     erh->flags &= ~ER_FLAG_FVALUE_VALID;
    1271             :     /* And we don't know the flattened size either */
    1272          86 :     erh->flat_size = 0;
    1273             : 
    1274          86 :     oldcxt = MemoryContextSwitchTo(erh->hdr.eoh_context);
    1275             : 
    1276          86 :     dvalues = erh->dvalues;
    1277          86 :     dnulls = erh->dnulls;
    1278             : 
    1279         264 :     for (fnumber = 0; fnumber < erh->nfields; fnumber++)
    1280             :     {
    1281         178 :         Form_pg_attribute attr = TupleDescAttr(tupdesc, fnumber);
    1282             :         Datum       newValue;
    1283             :         bool        isnull;
    1284             : 
    1285             :         /* Ignore dropped columns */
    1286         178 :         if (attr->attisdropped)
    1287           6 :             continue;
    1288             : 
    1289         172 :         newValue = newValues[fnumber];
    1290         172 :         isnull = isnulls[fnumber];
    1291             : 
    1292         172 :         if (!attr->attbyval)
    1293             :         {
    1294             :             /*
    1295             :              * Copy new field value into record's context, and deal with
    1296             :              * detoasting, if needed.
    1297             :              */
    1298          12 :             if (!isnull)
    1299             :             {
    1300             :                 /* Is it an external toasted value? */
    1301          24 :                 if (attr->attlen == -1 &&
    1302          12 :                     VARATT_IS_EXTERNAL(DatumGetPointer(newValue)))
    1303             :                 {
    1304           4 :                     if (expand_external)
    1305             :                     {
    1306             :                         /* Detoast as requested while copying the value */
    1307           2 :                         newValue = PointerGetDatum(heap_tuple_fetch_attr((struct varlena *) DatumGetPointer(newValue)));
    1308             :                     }
    1309             :                     else
    1310             :                     {
    1311             :                         /* Just copy the value */
    1312           0 :                         newValue = datumCopy(newValue, false, -1);
    1313             :                         /* If it's still external, remember that */
    1314           0 :                         if (VARATT_IS_EXTERNAL(DatumGetPointer(newValue)))
    1315           0 :                             erh->flags |= ER_FLAG_HAVE_EXTERNAL;
    1316             :                     }
    1317             :                 }
    1318             :                 else
    1319             :                 {
    1320             :                     /* Not an external value, just copy it */
    1321          10 :                     newValue = datumCopy(newValue, false, attr->attlen);
    1322             :                 }
    1323             : 
    1324             :                 /* Remember that we have field(s) that need to be pfree'd */
    1325          12 :                 erh->flags |= ER_FLAG_DVALUES_ALLOCED;
    1326             :             }
    1327             : 
    1328             :             /*
    1329             :              * Free old field value, if any (not likely, since really we ought
    1330             :              * to be inserting into an empty record).
    1331             :              */
    1332          12 :             if (unlikely(!dnulls[fnumber]))
    1333             :             {
    1334             :                 char       *oldValue;
    1335             : 
    1336           0 :                 oldValue = (char *) DatumGetPointer(dvalues[fnumber]);
    1337             :                 /* Don't try to pfree a part of the original flat record */
    1338           0 :                 if (oldValue < erh->fstartptr || oldValue >= erh->fendptr)
    1339           0 :                     pfree(oldValue);
    1340             :             }
    1341             :         }
    1342             : 
    1343             :         /* And finally we can insert the new field. */
    1344         172 :         dvalues[fnumber] = newValue;
    1345         172 :         dnulls[fnumber] = isnull;
    1346             :     }
    1347             : 
    1348             :     /*
    1349             :      * Because we don't guarantee atomicity of set_fields(), we can just leave
    1350             :      * checking of domain constraints to occur as the final step; if it throws
    1351             :      * an error, too bad.
    1352             :      */
    1353          86 :     if (erh->flags & ER_FLAG_IS_DOMAIN)
    1354             :     {
    1355             :         /* We run domain_check in a short-lived context to limit cruft */
    1356          18 :         MemoryContextSwitchTo(get_short_term_cxt(erh));
    1357             : 
    1358          18 :         domain_check(ExpandedRecordGetRODatum(erh), false,
    1359             :                      erh->er_decltypeid,
    1360             :                      &erh->er_domaininfo,
    1361             :                      erh->hdr.eoh_context);
    1362             :     }
    1363             : 
    1364          78 :     MemoryContextSwitchTo(oldcxt);
    1365          78 : }
    1366             : 
    1367             : /*
    1368             :  * Construct (or reset) working memory context for short-term operations.
    1369             :  *
    1370             :  * This context is used for domain check evaluation and for detoasting.
    1371             :  *
    1372             :  * If we don't have a short-lived memory context, make one; if we have one,
    1373             :  * reset it to get rid of any leftover cruft.  (It is a tad annoying to need a
    1374             :  * whole context for this, since it will often go unused --- but it's hard to
    1375             :  * avoid memory leaks otherwise.  We can make the context small, at least.)
    1376             :  */
    1377             : static MemoryContext
    1378         166 : get_short_term_cxt(ExpandedRecordHeader *erh)
    1379             : {
    1380         166 :     if (erh->er_short_term_cxt == NULL)
    1381         122 :         erh->er_short_term_cxt =
    1382         122 :             AllocSetContextCreate(erh->hdr.eoh_context,
    1383             :                                   "expanded record short-term context",
    1384             :                                   ALLOCSET_SMALL_SIZES);
    1385             :     else
    1386          44 :         MemoryContextReset(erh->er_short_term_cxt);
    1387         166 :     return erh->er_short_term_cxt;
    1388             : }
    1389             : 
    1390             : /*
    1391             :  * Construct "dummy header" for checking domain constraints.
    1392             :  *
    1393             :  * Since we don't want to modify the state of the expanded record until
    1394             :  * we've validated the constraints, our approach is to set up a dummy
    1395             :  * record header containing the new field value(s) and then pass that to
    1396             :  * domain_check.  We retain the dummy header as part of the expanded
    1397             :  * record's state to save palloc cycles, but reinitialize (most of)
    1398             :  * its contents on each use.
    1399             :  */
    1400             : static void
    1401          56 : build_dummy_expanded_header(ExpandedRecordHeader *main_erh)
    1402             : {
    1403             :     ExpandedRecordHeader *erh;
    1404          56 :     TupleDesc   tupdesc = expanded_record_get_tupdesc(main_erh);
    1405             : 
    1406             :     /* Ensure we have a short-lived context */
    1407          56 :     (void) get_short_term_cxt(main_erh);
    1408             : 
    1409             :     /*
    1410             :      * Allocate dummy header on first time through, or in the unlikely event
    1411             :      * that the number of fields changes (in which case we just leak the old
    1412             :      * one).  Include space for its field values in the request.
    1413             :      */
    1414          56 :     erh = main_erh->er_dummy_header;
    1415          56 :     if (erh == NULL || erh->nfields != tupdesc->natts)
    1416             :     {
    1417             :         char       *chunk;
    1418             : 
    1419          36 :         erh = (ExpandedRecordHeader *)
    1420          36 :             MemoryContextAlloc(main_erh->hdr.eoh_context,
    1421             :                                MAXALIGN(sizeof(ExpandedRecordHeader))
    1422          36 :                                + tupdesc->natts * (sizeof(Datum) + sizeof(bool)));
    1423             : 
    1424             :         /* Ensure all header fields are initialized to 0/null */
    1425          36 :         memset(erh, 0, sizeof(ExpandedRecordHeader));
    1426             : 
    1427             :         /*
    1428             :          * We set up the dummy header with an indication that its memory
    1429             :          * context is the short-lived context.  This is so that, if any
    1430             :          * detoasting of out-of-line values happens due to an attempt to
    1431             :          * extract a composite datum from the dummy header, the detoasted
    1432             :          * stuff will end up in the short-lived context and not cause a leak.
    1433             :          * This is cheating a bit on the expanded-object protocol; but since
    1434             :          * we never pass a R/W pointer to the dummy object to any other code,
    1435             :          * nothing else is authorized to delete or transfer ownership of the
    1436             :          * object's context, so it should be safe enough.
    1437             :          */
    1438          36 :         EOH_init_header(&erh->hdr, &ER_methods, main_erh->er_short_term_cxt);
    1439          36 :         erh->er_magic = ER_MAGIC;
    1440             : 
    1441             :         /* Set up dvalues/dnulls, with no valid contents as yet */
    1442          36 :         chunk = (char *) erh + MAXALIGN(sizeof(ExpandedRecordHeader));
    1443          36 :         erh->dvalues = (Datum *) chunk;
    1444          36 :         erh->dnulls = (bool *) (chunk + tupdesc->natts * sizeof(Datum));
    1445          36 :         erh->nfields = tupdesc->natts;
    1446             : 
    1447             :         /*
    1448             :          * The fields we just set are assumed to remain constant through
    1449             :          * multiple uses of the dummy header to check domain constraints.  All
    1450             :          * other dummy header fields should be explicitly reset below, to
    1451             :          * ensure there's not accidental effects of one check on the next one.
    1452             :          */
    1453             : 
    1454          36 :         main_erh->er_dummy_header = erh;
    1455             :     }
    1456             : 
    1457             :     /*
    1458             :      * If anything inquires about the dummy header's declared type, it should
    1459             :      * report the composite base type, not the domain type (since the VALUE in
    1460             :      * a domain check constraint is of the base type not the domain).  Hence
    1461             :      * we do not transfer over the IS_DOMAIN flag, nor indeed any of the main
    1462             :      * header's flags, since the dummy header is empty of data at this point.
    1463             :      * But don't forget to mark header as dummy.
    1464             :      */
    1465          56 :     erh->flags = ER_FLAG_IS_DUMMY;
    1466             : 
    1467             :     /* Copy composite-type identification info */
    1468          56 :     erh->er_decltypeid = erh->er_typeid = main_erh->er_typeid;
    1469          56 :     erh->er_typmod = main_erh->er_typmod;
    1470             : 
    1471             :     /* Dummy header does not need its own tupdesc refcount */
    1472          56 :     erh->er_tupdesc = tupdesc;
    1473          56 :     erh->er_tupdesc_id = main_erh->er_tupdesc_id;
    1474             : 
    1475             :     /*
    1476             :      * It's tempting to copy over whatever we know about the flat size, but
    1477             :      * there's no point since we're surely about to modify the dummy record's
    1478             :      * field(s).  Instead just clear anything left over from a previous usage
    1479             :      * cycle.
    1480             :      */
    1481          56 :     erh->flat_size = 0;
    1482             : 
    1483             :     /* Copy over fvalue if we have it, so that system columns are available */
    1484          56 :     erh->fvalue = main_erh->fvalue;
    1485          56 :     erh->fstartptr = main_erh->fstartptr;
    1486          56 :     erh->fendptr = main_erh->fendptr;
    1487          56 : }
    1488             : 
    1489             : /*
    1490             :  * Precheck domain constraints for a set_field operation
    1491             :  */
    1492             : static pg_noinline void
    1493          24 : check_domain_for_new_field(ExpandedRecordHeader *erh, int fnumber,
    1494             :                            Datum newValue, bool isnull)
    1495             : {
    1496             :     ExpandedRecordHeader *dummy_erh;
    1497             :     MemoryContext oldcxt;
    1498             : 
    1499             :     /* Construct dummy header to contain proposed new field set */
    1500          24 :     build_dummy_expanded_header(erh);
    1501          24 :     dummy_erh = erh->er_dummy_header;
    1502             : 
    1503             :     /*
    1504             :      * If record isn't empty, just deconstruct it (if needed) and copy over
    1505             :      * the existing field values.  If it is empty, just fill fields with nulls
    1506             :      * manually --- don't call deconstruct_expanded_record prematurely.
    1507             :      */
    1508          24 :     if (!ExpandedRecordIsEmpty(erh))
    1509             :     {
    1510          16 :         deconstruct_expanded_record(erh);
    1511          16 :         memcpy(dummy_erh->dvalues, erh->dvalues,
    1512          16 :                dummy_erh->nfields * sizeof(Datum));
    1513          16 :         memcpy(dummy_erh->dnulls, erh->dnulls,
    1514          16 :                dummy_erh->nfields * sizeof(bool));
    1515             :         /* There might be some external values in there... */
    1516          16 :         dummy_erh->flags |= erh->flags & ER_FLAG_HAVE_EXTERNAL;
    1517             :     }
    1518             :     else
    1519             :     {
    1520           8 :         memset(dummy_erh->dvalues, 0, dummy_erh->nfields * sizeof(Datum));
    1521           8 :         memset(dummy_erh->dnulls, true, dummy_erh->nfields * sizeof(bool));
    1522             :     }
    1523             : 
    1524             :     /* Either way, we now have valid dvalues */
    1525          24 :     dummy_erh->flags |= ER_FLAG_DVALUES_VALID;
    1526             : 
    1527             :     /* Caller error if fnumber is system column or nonexistent column */
    1528          24 :     if (unlikely(fnumber <= 0 || fnumber > dummy_erh->nfields))
    1529           0 :         elog(ERROR, "cannot assign to field %d of expanded record", fnumber);
    1530             : 
    1531             :     /* Insert proposed new value into dummy field array */
    1532          24 :     dummy_erh->dvalues[fnumber - 1] = newValue;
    1533          24 :     dummy_erh->dnulls[fnumber - 1] = isnull;
    1534             : 
    1535             :     /*
    1536             :      * The proposed new value might be external, in which case we'd better set
    1537             :      * the flag for that in dummy_erh.  (This matters in case something in the
    1538             :      * domain check expressions tries to extract a flat value from the dummy
    1539             :      * header.)
    1540             :      */
    1541          24 :     if (!isnull)
    1542             :     {
    1543          22 :         Form_pg_attribute attr = TupleDescAttr(erh->er_tupdesc, fnumber - 1);
    1544             : 
    1545          34 :         if (!attr->attbyval && attr->attlen == -1 &&
    1546          12 :             VARATT_IS_EXTERNAL(DatumGetPointer(newValue)))
    1547           0 :             dummy_erh->flags |= ER_FLAG_HAVE_EXTERNAL;
    1548             :     }
    1549             : 
    1550             :     /*
    1551             :      * We call domain_check in the short-lived context, so that any cruft
    1552             :      * leaked by expression evaluation can be reclaimed.
    1553             :      */
    1554          24 :     oldcxt = MemoryContextSwitchTo(erh->er_short_term_cxt);
    1555             : 
    1556             :     /*
    1557             :      * And now we can apply the check.  Note we use main header's domain cache
    1558             :      * space, so that caching carries across repeated uses.
    1559             :      */
    1560          24 :     domain_check(ExpandedRecordGetRODatum(dummy_erh), false,
    1561             :                  erh->er_decltypeid,
    1562             :                  &erh->er_domaininfo,
    1563             :                  erh->hdr.eoh_context);
    1564             : 
    1565          18 :     MemoryContextSwitchTo(oldcxt);
    1566             : 
    1567             :     /* We might as well clean up cruft immediately. */
    1568          18 :     MemoryContextReset(erh->er_short_term_cxt);
    1569          18 : }
    1570             : 
    1571             : /*
    1572             :  * Precheck domain constraints for a set_tuple operation
    1573             :  */
    1574             : static pg_noinline void
    1575          68 : check_domain_for_new_tuple(ExpandedRecordHeader *erh, HeapTuple tuple)
    1576             : {
    1577             :     ExpandedRecordHeader *dummy_erh;
    1578             :     MemoryContext oldcxt;
    1579             : 
    1580             :     /* If we're being told to set record to empty, just see if NULL is OK */
    1581          68 :     if (tuple == NULL)
    1582             :     {
    1583             :         /* We run domain_check in a short-lived context to limit cruft */
    1584          36 :         oldcxt = MemoryContextSwitchTo(get_short_term_cxt(erh));
    1585             : 
    1586          36 :         domain_check((Datum) 0, true,
    1587             :                      erh->er_decltypeid,
    1588             :                      &erh->er_domaininfo,
    1589             :                      erh->hdr.eoh_context);
    1590             : 
    1591          24 :         MemoryContextSwitchTo(oldcxt);
    1592             : 
    1593             :         /* We might as well clean up cruft immediately. */
    1594          24 :         MemoryContextReset(erh->er_short_term_cxt);
    1595             : 
    1596          24 :         return;
    1597             :     }
    1598             : 
    1599             :     /* Construct dummy header to contain replacement tuple */
    1600          32 :     build_dummy_expanded_header(erh);
    1601          32 :     dummy_erh = erh->er_dummy_header;
    1602             : 
    1603             :     /* Insert tuple, but don't bother to deconstruct its fields for now */
    1604          32 :     dummy_erh->fvalue = tuple;
    1605          32 :     dummy_erh->fstartptr = (char *) tuple->t_data;
    1606          32 :     dummy_erh->fendptr = ((char *) tuple->t_data) + tuple->t_len;
    1607          32 :     dummy_erh->flags |= ER_FLAG_FVALUE_VALID;
    1608             : 
    1609             :     /* Remember if we have any out-of-line field values */
    1610          32 :     if (HeapTupleHasExternal(tuple))
    1611           4 :         dummy_erh->flags |= ER_FLAG_HAVE_EXTERNAL;
    1612             : 
    1613             :     /*
    1614             :      * We call domain_check in the short-lived context, so that any cruft
    1615             :      * leaked by expression evaluation can be reclaimed.
    1616             :      */
    1617          32 :     oldcxt = MemoryContextSwitchTo(erh->er_short_term_cxt);
    1618             : 
    1619             :     /*
    1620             :      * And now we can apply the check.  Note we use main header's domain cache
    1621             :      * space, so that caching carries across repeated uses.
    1622             :      */
    1623          32 :     domain_check(ExpandedRecordGetRODatum(dummy_erh), false,
    1624             :                  erh->er_decltypeid,
    1625             :                  &erh->er_domaininfo,
    1626             :                  erh->hdr.eoh_context);
    1627             : 
    1628          24 :     MemoryContextSwitchTo(oldcxt);
    1629             : 
    1630             :     /* We might as well clean up cruft immediately. */
    1631          24 :     MemoryContextReset(erh->er_short_term_cxt);
    1632             : }

Generated by: LCOV version 1.13