PostgreSQL Source Code  git master
expandedrecord.h File Reference
#include "access/htup.h"
#include "access/tupdesc.h"
#include "fmgr.h"
#include "utils/expandeddatum.h"
Include dependency graph for expandedrecord.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ExpandedRecordHeader
 
struct  ExpandedRecordFieldInfo
 

Macros

#define ER_MAGIC   1384727874 /* ID for debugging crosschecks */
 
#define ER_FLAG_FVALUE_VALID   0x0001 /* fvalue is up to date? */
 
#define ER_FLAG_FVALUE_ALLOCED   0x0002 /* fvalue is local storage? */
 
#define ER_FLAG_DVALUES_VALID   0x0004 /* dvalues/dnulls are up to date? */
 
#define ER_FLAG_DVALUES_ALLOCED   0x0008 /* any field values local storage? */
 
#define ER_FLAG_HAVE_EXTERNAL   0x0010 /* any field values are external? */
 
#define ER_FLAG_TUPDESC_ALLOCED   0x0020 /* tupdesc is local storage? */
 
#define ER_FLAG_IS_DOMAIN   0x0040 /* er_decltypeid is domain? */
 
#define ER_FLAG_IS_DUMMY   0x0080 /* this header is dummy (see below) */
 
#define ER_FLAGS_NON_DATA   (ER_FLAG_TUPDESC_ALLOCED | ER_FLAG_IS_DOMAIN | ER_FLAG_IS_DUMMY)
 
#define PG_GETARG_EXPANDED_RECORD(n)   DatumGetExpandedRecord(PG_GETARG_DATUM(n))
 
#define ExpandedRecordGetDatum(erh)   EOHPGetRWDatum(&(erh)->hdr)
 
#define ExpandedRecordGetRODatum(erh)   EOHPGetRODatum(&(erh)->hdr)
 
#define PG_RETURN_EXPANDED_RECORD(x)   PG_RETURN_DATUM(ExpandedRecordGetDatum(x))
 
#define ExpandedRecordIsEmpty(erh)   (((erh)->flags & (ER_FLAG_DVALUES_VALID | ER_FLAG_FVALUE_VALID)) == 0)
 
#define ExpandedRecordIsDomain(erh)   (((erh)->flags & ER_FLAG_IS_DOMAIN) != 0)
 
#define TransferExpandedRecord(erh, cxt)   MemoryContextSetParent((erh)->hdr.eoh_context, cxt)
 
#define expanded_record_set_field(erh, fnumber, newValue, isnull, expand_external)   expanded_record_set_field_internal(erh, fnumber, newValue, isnull, expand_external, true)
 

Typedefs

typedef struct ExpandedRecordHeader ExpandedRecordHeader
 
typedef struct ExpandedRecordFieldInfo ExpandedRecordFieldInfo
 

Functions

ExpandedRecordHeadermake_expanded_record_from_typeid (Oid type_id, int32 typmod, MemoryContext parentcontext)
 
ExpandedRecordHeadermake_expanded_record_from_tupdesc (TupleDesc tupdesc, MemoryContext parentcontext)
 
ExpandedRecordHeadermake_expanded_record_from_exprecord (ExpandedRecordHeader *olderh, MemoryContext parentcontext)
 
void expanded_record_set_tuple (ExpandedRecordHeader *erh, HeapTuple tuple, bool copy, bool expand_external)
 
Datum make_expanded_record_from_datum (Datum recorddatum, MemoryContext parentcontext)
 
TupleDesc expanded_record_fetch_tupdesc (ExpandedRecordHeader *erh)
 
HeapTuple expanded_record_get_tuple (ExpandedRecordHeader *erh)
 
ExpandedRecordHeaderDatumGetExpandedRecord (Datum d)
 
void deconstruct_expanded_record (ExpandedRecordHeader *erh)
 
bool expanded_record_lookup_field (ExpandedRecordHeader *erh, const char *fieldname, ExpandedRecordFieldInfo *finfo)
 
Datum expanded_record_fetch_field (ExpandedRecordHeader *erh, int fnumber, bool *isnull)
 
void expanded_record_set_field_internal (ExpandedRecordHeader *erh, int fnumber, Datum newValue, bool isnull, bool expand_external, bool check_constraints)
 
void expanded_record_set_fields (ExpandedRecordHeader *erh, const Datum *newValues, const bool *isnulls, bool expand_external)
 
static TupleDesc expanded_record_get_tupdesc (ExpandedRecordHeader *erh)
 
static Datum expanded_record_get_field (ExpandedRecordHeader *erh, int fnumber, bool *isnull)
 

Macro Definition Documentation

◆ ER_FLAG_DVALUES_ALLOCED

#define ER_FLAG_DVALUES_ALLOCED   0x0008 /* any field values local storage? */

◆ ER_FLAG_DVALUES_VALID

#define ER_FLAG_DVALUES_VALID   0x0004 /* dvalues/dnulls are up to date? */

◆ ER_FLAG_FVALUE_ALLOCED

#define ER_FLAG_FVALUE_ALLOCED   0x0002 /* fvalue is local storage? */

Definition at line 53 of file expandedrecord.h.

Referenced by expanded_record_set_tuple(), and make_expanded_record_from_datum().

◆ ER_FLAG_FVALUE_VALID

◆ ER_FLAG_HAVE_EXTERNAL

#define ER_FLAG_HAVE_EXTERNAL   0x0010 /* any field values are external? */

◆ ER_FLAG_IS_DOMAIN

#define ER_FLAG_IS_DOMAIN   0x0040 /* er_decltypeid is domain? */

◆ ER_FLAG_IS_DUMMY

#define ER_FLAG_IS_DUMMY   0x0080 /* this header is dummy (see below) */

◆ ER_FLAG_TUPDESC_ALLOCED

#define ER_FLAG_TUPDESC_ALLOCED   0x0020 /* tupdesc is local storage? */

◆ ER_FLAGS_NON_DATA

#define ER_FLAGS_NON_DATA   (ER_FLAG_TUPDESC_ALLOCED | ER_FLAG_IS_DOMAIN | ER_FLAG_IS_DUMMY)

Definition at line 61 of file expandedrecord.h.

Referenced by expanded_record_set_tuple().

◆ ER_MAGIC

◆ expanded_record_set_field

#define expanded_record_set_field (   erh,
  fnumber,
  newValue,
  isnull,
  expand_external 
)    expanded_record_set_field_internal(erh, fnumber, newValue, isnull, expand_external, true)

Definition at line 198 of file expandedrecord.h.

Referenced by exec_assign_value().

◆ ExpandedRecordGetDatum

#define ExpandedRecordGetDatum (   erh)    EOHPGetRWDatum(&(erh)->hdr)

◆ ExpandedRecordGetRODatum

#define ExpandedRecordGetRODatum (   erh)    EOHPGetRODatum(&(erh)->hdr)

◆ ExpandedRecordIsDomain

#define ExpandedRecordIsDomain (   erh)    (((erh)->flags & ER_FLAG_IS_DOMAIN) != 0)

◆ ExpandedRecordIsEmpty

#define ExpandedRecordIsEmpty (   erh)    (((erh)->flags & (ER_FLAG_DVALUES_VALID | ER_FLAG_FVALUE_VALID)) == 0)

◆ PG_GETARG_EXPANDED_RECORD

#define PG_GETARG_EXPANDED_RECORD (   n)    DatumGetExpandedRecord(PG_GETARG_DATUM(n))

Definition at line 142 of file expandedrecord.h.

◆ PG_RETURN_EXPANDED_RECORD

#define PG_RETURN_EXPANDED_RECORD (   x)    PG_RETURN_DATUM(ExpandedRecordGetDatum(x))

Definition at line 145 of file expandedrecord.h.

◆ TransferExpandedRecord

#define TransferExpandedRecord (   erh,
  cxt 
)    MemoryContextSetParent((erh)->hdr.eoh_context, cxt)

Definition at line 154 of file expandedrecord.h.

Referenced by assign_record_var().

Typedef Documentation

◆ ExpandedRecordFieldInfo

◆ ExpandedRecordHeader

Function Documentation

◆ DatumGetExpandedRecord()

ExpandedRecordHeader* DatumGetExpandedRecord ( Datum  d)

Definition at line 927 of file expandedrecord.c.

References Assert, CurrentMemoryContext, DatumGetEOHP(), DatumGetPointer, ER_MAGIC, ExpandedRecordHeader::er_magic, make_expanded_record_from_datum(), and VARATT_IS_EXTERNAL_EXPANDED_RW.

928 {
929  /* If it's a writable expanded record already, just return it */
931  {
933 
934  Assert(erh->er_magic == ER_MAGIC);
935  return erh;
936  }
937 
938  /* Else expand the hard way */
940  return (ExpandedRecordHeader *) DatumGetEOHP(d);
941 }
Datum make_expanded_record_from_datum(Datum recorddatum, MemoryContext parentcontext)
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
#define ER_MAGIC
#define Assert(condition)
Definition: c.h:732
#define DatumGetPointer(X)
Definition: postgres.h:549
#define VARATT_IS_EXTERNAL_EXPANDED_RW(PTR)
Definition: postgres.h:320

◆ deconstruct_expanded_record()

void deconstruct_expanded_record ( ExpandedRecordHeader erh)

Definition at line 952 of file expandedrecord.c.

References ExpandedRecordHeader::dnulls, ExpandedRecordHeader::dvalues, ExpandedObjectHeader::eoh_context, ER_FLAG_DVALUES_VALID, ER_FLAG_FVALUE_VALID, expanded_record_get_tupdesc(), ExpandedRecordHeader::flags, ExpandedRecordHeader::fvalue, ExpandedRecordHeader::hdr, heap_deform_tuple(), MemoryContextAlloc(), TupleDescData::natts, and ExpandedRecordHeader::nfields.

Referenced by check_domain_for_new_field(), ER_get_flat_size(), exec_move_row(), exec_move_row_from_datum(), exec_stmt_return_next(), expanded_record_fetch_field(), expanded_record_set_field_internal(), and expanded_record_set_fields().

953 {
954  TupleDesc tupdesc;
955  Datum *dvalues;
956  bool *dnulls;
957  int nfields;
958 
959  if (erh->flags & ER_FLAG_DVALUES_VALID)
960  return; /* already valid, nothing to do */
961 
962  /* We'll need the tuple descriptor */
963  tupdesc = expanded_record_get_tupdesc(erh);
964 
965  /*
966  * Allocate arrays in private context, if we don't have them already. We
967  * don't expect to see a change in nfields here, so while we cope if it
968  * happens, we don't bother avoiding a leak of the old arrays (which might
969  * not be separately palloc'd, anyway).
970  */
971  nfields = tupdesc->natts;
972  if (erh->dvalues == NULL || erh->nfields != nfields)
973  {
974  char *chunk;
975 
976  /*
977  * To save a palloc cycle, we allocate both the Datum and isnull
978  * arrays in one palloc chunk.
979  */
980  chunk = MemoryContextAlloc(erh->hdr.eoh_context,
981  nfields * (sizeof(Datum) + sizeof(bool)));
982  dvalues = (Datum *) chunk;
983  dnulls = (bool *) (chunk + nfields * sizeof(Datum));
984  erh->dvalues = dvalues;
985  erh->dnulls = dnulls;
986  erh->nfields = nfields;
987  }
988  else
989  {
990  dvalues = erh->dvalues;
991  dnulls = erh->dnulls;
992  }
993 
994  if (erh->flags & ER_FLAG_FVALUE_VALID)
995  {
996  /* Deconstruct tuple */
997  heap_deform_tuple(erh->fvalue, tupdesc, dvalues, dnulls);
998  }
999  else
1000  {
1001  /* If record was empty, instantiate it as a row of nulls */
1002  memset(dvalues, 0, nfields * sizeof(Datum));
1003  memset(dnulls, true, nfields * sizeof(bool));
1004  }
1005 
1006  /* Mark the dvalues as valid */
1007  erh->flags |= ER_FLAG_DVALUES_VALID;
1008 }
#define ER_FLAG_FVALUE_VALID
ExpandedObjectHeader hdr
#define ER_FLAG_DVALUES_VALID
static TupleDesc expanded_record_get_tupdesc(ExpandedRecordHeader *erh)
uintptr_t Datum
Definition: postgres.h:367
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1249
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
MemoryContext eoh_context

◆ expanded_record_fetch_field()

Datum expanded_record_fetch_field ( ExpandedRecordHeader erh,
int  fnumber,
bool isnull 
)

Definition at line 1063 of file expandedrecord.c.

References deconstruct_expanded_record(), ExpandedRecordHeader::dnulls, ExpandedRecordHeader::dvalues, ExpandedRecordIsEmpty, ExpandedRecordHeader::fvalue, heap_getsysattr(), ExpandedRecordHeader::nfields, and unlikely.

Referenced by expanded_record_get_field().

1065 {
1066  if (fnumber > 0)
1067  {
1068  /* Empty record has null fields */
1069  if (ExpandedRecordIsEmpty(erh))
1070  {
1071  *isnull = true;
1072  return (Datum) 0;
1073  }
1074  /* Make sure we have deconstructed form */
1076  /* Out-of-range field number reads as null */
1077  if (unlikely(fnumber > erh->nfields))
1078  {
1079  *isnull = true;
1080  return (Datum) 0;
1081  }
1082  *isnull = erh->dnulls[fnumber - 1];
1083  return erh->dvalues[fnumber - 1];
1084  }
1085  else
1086  {
1087  /* System columns read as null if we haven't got flat tuple */
1088  if (erh->fvalue == NULL)
1089  {
1090  *isnull = true;
1091  return (Datum) 0;
1092  }
1093  /* heap_getsysattr doesn't actually use tupdesc, so just pass null */
1094  return heap_getsysattr(erh->fvalue, fnumber, NULL, isnull);
1095  }
1096 }
#define ExpandedRecordIsEmpty(erh)
Datum heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: heaptuple.c:627
uintptr_t Datum
Definition: postgres.h:367
#define unlikely(x)
Definition: c.h:208
void deconstruct_expanded_record(ExpandedRecordHeader *erh)

◆ expanded_record_fetch_tupdesc()

TupleDesc expanded_record_fetch_tupdesc ( ExpandedRecordHeader erh)

Definition at line 824 of file expandedrecord.c.

References MemoryContextCallback::arg, assign_record_type_identifier(), DecrTupleDescRefCount(), ExpandedObjectHeader::eoh_context, ER_mc_callback(), ExpandedRecordHeader::er_mcb, ExpandedRecordHeader::er_tupdesc, ExpandedRecordHeader::er_tupdesc_id, ExpandedRecordHeader::er_typeid, ExpandedRecordHeader::er_typmod, MemoryContextCallback::func, ExpandedRecordHeader::hdr, lookup_rowtype_tupdesc(), MemoryContextRegisterResetCallback(), TupleDescData::tdrefcount, TupleDescData::tdtypeid, and TupleDescData::tdtypmod.

Referenced by expanded_record_get_tupdesc().

825 {
826  TupleDesc tupdesc;
827 
828  /* Easy if we already have it (but caller should have checked already) */
829  if (erh->er_tupdesc)
830  return erh->er_tupdesc;
831 
832  /* Lookup the composite type's tupdesc using the typcache */
833  tupdesc = lookup_rowtype_tupdesc(erh->er_typeid, erh->er_typmod);
834 
835  /*
836  * If it's a refcounted tupdesc rather than a statically allocated one, we
837  * want to manage the refcount with a memory context callback rather than
838  * assuming that the CurrentResourceOwner is longer-lived than this
839  * expanded object.
840  */
841  if (tupdesc->tdrefcount >= 0)
842  {
843  /* Register callback if we didn't already */
844  if (erh->er_mcb.arg == NULL)
845  {
846  erh->er_mcb.func = ER_mc_callback;
847  erh->er_mcb.arg = (void *) erh;
849  &erh->er_mcb);
850  }
851 
852  /* Remember our own pointer */
853  erh->er_tupdesc = tupdesc;
854  tupdesc->tdrefcount++;
855 
856  /* Release the pin lookup_rowtype_tupdesc acquired */
857  DecrTupleDescRefCount(tupdesc);
858  }
859  else
860  {
861  /* Just remember the pointer */
862  erh->er_tupdesc = tupdesc;
863  }
864 
865  /* In either case, fetch the process-global ID for this tupdesc */
867  tupdesc->tdtypmod);
868 
869  return tupdesc;
870 }
MemoryContextCallbackFunction func
Definition: palloc.h:49
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1652
ExpandedObjectHeader hdr
int32 tdtypmod
Definition: tupdesc.h:83
MemoryContextCallback er_mcb
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:393
Oid tdtypeid
Definition: tupdesc.h:82
int tdrefcount
Definition: tupdesc.h:84
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:285
uint64 assign_record_type_identifier(Oid type_id, int32 typmod)
Definition: typcache.c:1847
static void ER_mc_callback(void *arg)
MemoryContext eoh_context

◆ expanded_record_get_field()

static Datum expanded_record_get_field ( ExpandedRecordHeader erh,
int  fnumber,
bool isnull 
)
inlinestatic

Definition at line 218 of file expandedrecord.h.

References ExpandedRecordHeader::dnulls, ExpandedRecordHeader::dvalues, ER_FLAG_DVALUES_VALID, expanded_record_fetch_field(), ExpandedRecordHeader::flags, likely, and ExpandedRecordHeader::nfields.

Referenced by exec_eval_datum(), ExecEvalFieldSelect(), and plpgsql_param_eval_recfield().

220 {
221  if ((erh->flags & ER_FLAG_DVALUES_VALID) &&
222  likely(fnumber > 0 && fnumber <= erh->nfields))
223  {
224  *isnull = erh->dnulls[fnumber - 1];
225  return erh->dvalues[fnumber - 1];
226  }
227  else
228  return expanded_record_fetch_field(erh, fnumber, isnull);
229 }
#define likely(x)
Definition: c.h:207
#define ER_FLAG_DVALUES_VALID
Datum expanded_record_fetch_field(ExpandedRecordHeader *erh, int fnumber, bool *isnull)

◆ expanded_record_get_tupdesc()

◆ expanded_record_get_tuple()

HeapTuple expanded_record_get_tuple ( ExpandedRecordHeader erh)

Definition at line 884 of file expandedrecord.c.

References ExpandedRecordHeader::dnulls, ExpandedRecordHeader::dvalues, ER_FLAG_DVALUES_VALID, ER_FLAG_FVALUE_VALID, ExpandedRecordHeader::er_tupdesc, ExpandedRecordHeader::flags, ExpandedRecordHeader::fvalue, and heap_form_tuple().

Referenced by coerce_function_result_tuple(), exec_stmt_return_next(), and plpgsql_exec_trigger().

885 {
886  /* Easy case if we still have original tuple */
887  if (erh->flags & ER_FLAG_FVALUE_VALID)
888  return erh->fvalue;
889 
890  /* Else just build a tuple from datums */
891  if (erh->flags & ER_FLAG_DVALUES_VALID)
892  return heap_form_tuple(erh->er_tupdesc, erh->dvalues, erh->dnulls);
893 
894  /* Expanded record is empty */
895  return NULL;
896 }
#define ER_FLAG_FVALUE_VALID
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define ER_FLAG_DVALUES_VALID

◆ expanded_record_lookup_field()

bool expanded_record_lookup_field ( ExpandedRecordHeader erh,
const char *  fieldname,
ExpandedRecordFieldInfo finfo 
)

Definition at line 1017 of file expandedrecord.c.

References expanded_record_get_tupdesc(), ExpandedRecordFieldInfo::fcollation, ExpandedRecordFieldInfo::fnumber, FormData_pg_attribute, ExpandedRecordFieldInfo::ftypeid, ExpandedRecordFieldInfo::ftypmod, namestrcmp(), TupleDescData::natts, SystemAttributeByName(), and TupleDescAttr.

Referenced by exec_assign_value(), exec_eval_datum(), plpgsql_exec_get_datum_type(), plpgsql_exec_get_datum_type_info(), plpgsql_param_eval_recfield(), and plpgsql_param_fetch().

1019 {
1020  TupleDesc tupdesc;
1021  int fno;
1022  Form_pg_attribute attr;
1023  const FormData_pg_attribute *sysattr;
1024 
1025  tupdesc = expanded_record_get_tupdesc(erh);
1026 
1027  /* First, check user-defined attributes */
1028  for (fno = 0; fno < tupdesc->natts; fno++)
1029  {
1030  attr = TupleDescAttr(tupdesc, fno);
1031  if (namestrcmp(&attr->attname, fieldname) == 0 &&
1032  !attr->attisdropped)
1033  {
1034  finfo->fnumber = attr->attnum;
1035  finfo->ftypeid = attr->atttypid;
1036  finfo->ftypmod = attr->atttypmod;
1037  finfo->fcollation = attr->attcollation;
1038  return true;
1039  }
1040  }
1041 
1042  /* How about system attributes? */
1043  sysattr = SystemAttributeByName(fieldname);
1044  if (sysattr != NULL)
1045  {
1046  finfo->fnumber = sysattr->attnum;
1047  finfo->ftypeid = sysattr->atttypid;
1048  finfo->ftypmod = sysattr->atttypmod;
1049  finfo->fcollation = sysattr->attcollation;
1050  return true;
1051  }
1052 
1053  return false;
1054 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int namestrcmp(Name name, const char *str)
Definition: name.c:287
const FormData_pg_attribute * SystemAttributeByName(const char *attname)
Definition: heap.c:262
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
static TupleDesc expanded_record_get_tupdesc(ExpandedRecordHeader *erh)
FormData_pg_attribute
Definition: pg_attribute.h:184

◆ expanded_record_set_field_internal()

void expanded_record_set_field_internal ( ExpandedRecordHeader erh,
int  fnumber,
Datum  newValue,
bool  isnull,
bool  expand_external,
bool  check_constraints 
)

Definition at line 1112 of file expandedrecord.c.

References Assert, check_domain_for_new_field(), datumCopy(), DatumGetPointer, deconstruct_expanded_record(), detoast_external_attr(), ExpandedRecordHeader::dnulls, ExpandedRecordHeader::dvalues, elog, ExpandedObjectHeader::eoh_context, ER_FLAG_DVALUES_ALLOCED, ER_FLAG_DVALUES_VALID, ER_FLAG_FVALUE_VALID, ER_FLAG_HAVE_EXTERNAL, ER_FLAG_IS_DOMAIN, ER_FLAG_IS_DUMMY, ExpandedRecordHeader::er_short_term_cxt, ExpandedRecordHeader::er_tupdesc, ERROR, ExpandedRecordHeader::fendptr, ExpandedRecordHeader::flags, ExpandedRecordHeader::flat_size, get_short_term_cxt(), ExpandedRecordHeader::hdr, MemoryContextReset(), MemoryContextSwitchTo(), TupleDescData::natts, ExpandedRecordHeader::nfields, pfree(), PointerGetDatum, TupleDescAttr, unlikely, and VARATT_IS_EXTERNAL.

Referenced by ER_get_flat_size(), and plpgsql_exec_trigger().

1116 {
1117  TupleDesc tupdesc;
1118  Form_pg_attribute attr;
1119  Datum *dvalues;
1120  bool *dnulls;
1121  char *oldValue;
1122 
1123  /*
1124  * Shouldn't ever be trying to assign new data to a dummy header, except
1125  * in the case of an internal call for field inlining.
1126  */
1127  Assert(!(erh->flags & ER_FLAG_IS_DUMMY) || !check_constraints);
1128 
1129  /* Before performing the assignment, see if result will satisfy domain */
1130  if ((erh->flags & ER_FLAG_IS_DOMAIN) && check_constraints)
1131  check_domain_for_new_field(erh, fnumber, newValue, isnull);
1132 
1133  /* If we haven't yet deconstructed the tuple, do that */
1134  if (!(erh->flags & ER_FLAG_DVALUES_VALID))
1136 
1137  /* Tuple descriptor must be valid by now */
1138  tupdesc = erh->er_tupdesc;
1139  Assert(erh->nfields == tupdesc->natts);
1140 
1141  /* Caller error if fnumber is system column or nonexistent column */
1142  if (unlikely(fnumber <= 0 || fnumber > erh->nfields))
1143  elog(ERROR, "cannot assign to field %d of expanded record", fnumber);
1144 
1145  /*
1146  * Copy new field value into record's context, and deal with detoasting,
1147  * if needed.
1148  */
1149  attr = TupleDescAttr(tupdesc, fnumber - 1);
1150  if (!isnull && !attr->attbyval)
1151  {
1152  MemoryContext oldcxt;
1153 
1154  /* If requested, detoast any external value */
1155  if (expand_external)
1156  {
1157  if (attr->attlen == -1 &&
1159  {
1160  /* Detoasting should be done in short-lived context. */
1162  newValue = PointerGetDatum(detoast_external_attr((struct varlena *) DatumGetPointer(newValue)));
1163  MemoryContextSwitchTo(oldcxt);
1164  }
1165  else
1166  expand_external = false; /* need not clean up below */
1167  }
1168 
1169  /* Copy value into record's context */
1170  oldcxt = MemoryContextSwitchTo(erh->hdr.eoh_context);
1171  newValue = datumCopy(newValue, false, attr->attlen);
1172  MemoryContextSwitchTo(oldcxt);
1173 
1174  /* We can now flush anything that detoasting might have leaked */
1175  if (expand_external)
1177 
1178  /* Remember that we have field(s) that may need to be pfree'd */
1180 
1181  /*
1182  * While we're here, note whether it's an external toasted value,
1183  * because that could mean we need to inline it later. (Think not to
1184  * merge this into the previous expand_external logic: datumCopy could
1185  * by itself have made the value non-external.)
1186  */
1187  if (attr->attlen == -1 &&
1189  erh->flags |= ER_FLAG_HAVE_EXTERNAL;
1190  }
1191 
1192  /*
1193  * We're ready to make irreversible changes.
1194  */
1195  dvalues = erh->dvalues;
1196  dnulls = erh->dnulls;
1197 
1198  /* Flattened value will no longer represent record accurately */
1199  erh->flags &= ~ER_FLAG_FVALUE_VALID;
1200  /* And we don't know the flattened size either */
1201  erh->flat_size = 0;
1202 
1203  /* Grab old field value for pfree'ing, if needed. */
1204  if (!attr->attbyval && !dnulls[fnumber - 1])
1205  oldValue = (char *) DatumGetPointer(dvalues[fnumber - 1]);
1206  else
1207  oldValue = NULL;
1208 
1209  /* And finally we can insert the new field. */
1210  dvalues[fnumber - 1] = newValue;
1211  dnulls[fnumber - 1] = isnull;
1212 
1213  /*
1214  * Free old field if needed; this keeps repeated field replacements from
1215  * bloating the record's storage. If the pfree somehow fails, it won't
1216  * corrupt the record.
1217  *
1218  * If we're updating a dummy header, we can't risk pfree'ing the old
1219  * value, because most likely the expanded record's main header still has
1220  * a pointer to it. This won't result in any sustained memory leak, since
1221  * whatever we just allocated here is in the short-lived domain check
1222  * context.
1223  */
1224  if (oldValue && !(erh->flags & ER_FLAG_IS_DUMMY))
1225  {
1226  /* Don't try to pfree a part of the original flat record */
1227  if (oldValue < erh->fstartptr || oldValue >= erh->fendptr)
1228  pfree(oldValue);
1229  }
1230 }
#define ER_FLAG_FVALUE_VALID
#define ER_FLAG_HAVE_EXTERNAL
#define PointerGetDatum(X)
Definition: postgres.h:556
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
struct varlena * detoast_external_attr(struct varlena *attr)
Definition: detoast.c:45
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
#define ER_FLAG_IS_DOMAIN
ExpandedObjectHeader hdr
MemoryContext er_short_term_cxt
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:313
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
#define ER_FLAG_DVALUES_VALID
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:130
#define ER_FLAG_DVALUES_ALLOCED
uintptr_t Datum
Definition: postgres.h:367
#define Assert(condition)
Definition: c.h:732
static MemoryContext get_short_term_cxt(ExpandedRecordHeader *erh)
#define DatumGetPointer(X)
Definition: postgres.h:549
#define ER_FLAG_IS_DUMMY
#define elog(elevel,...)
Definition: elog.h:226
#define unlikely(x)
Definition: c.h:208
Definition: c.h:549
static pg_noinline void check_domain_for_new_field(ExpandedRecordHeader *erh, int fnumber, Datum newValue, bool isnull)
void deconstruct_expanded_record(ExpandedRecordHeader *erh)
MemoryContext eoh_context

◆ expanded_record_set_fields()

void expanded_record_set_fields ( ExpandedRecordHeader erh,
const Datum newValues,
const bool isnulls,
bool  expand_external 
)

Definition at line 1249 of file expandedrecord.c.

References Assert, datumCopy(), DatumGetPointer, deconstruct_expanded_record(), detoast_external_attr(), ExpandedRecordHeader::dnulls, domain_check(), ExpandedRecordHeader::dvalues, ExpandedObjectHeader::eoh_context, ExpandedRecordHeader::er_decltypeid, ExpandedRecordHeader::er_domaininfo, ER_FLAG_DVALUES_ALLOCED, ER_FLAG_DVALUES_VALID, ER_FLAG_FVALUE_VALID, ER_FLAG_HAVE_EXTERNAL, ER_FLAG_IS_DOMAIN, ER_FLAG_IS_DUMMY, ExpandedRecordHeader::er_tupdesc, ExpandedRecordGetRODatum, ExpandedRecordHeader::fendptr, ExpandedRecordHeader::flags, ExpandedRecordHeader::flat_size, get_short_term_cxt(), ExpandedRecordHeader::hdr, MemoryContextSwitchTo(), TupleDescData::natts, ExpandedRecordHeader::nfields, pfree(), PointerGetDatum, TupleDescAttr, unlikely, and VARATT_IS_EXTERNAL.

Referenced by exec_move_row_from_fields().

1252 {
1253  TupleDesc tupdesc;
1254  Datum *dvalues;
1255  bool *dnulls;
1256  int fnumber;
1257  MemoryContext oldcxt;
1258 
1259  /* Shouldn't ever be trying to assign new data to a dummy header */
1260  Assert(!(erh->flags & ER_FLAG_IS_DUMMY));
1261 
1262  /* If we haven't yet deconstructed the tuple, do that */
1263  if (!(erh->flags & ER_FLAG_DVALUES_VALID))
1265 
1266  /* Tuple descriptor must be valid by now */
1267  tupdesc = erh->er_tupdesc;
1268  Assert(erh->nfields == tupdesc->natts);
1269 
1270  /* Flattened value will no longer represent record accurately */
1271  erh->flags &= ~ER_FLAG_FVALUE_VALID;
1272  /* And we don't know the flattened size either */
1273  erh->flat_size = 0;
1274 
1275  oldcxt = MemoryContextSwitchTo(erh->hdr.eoh_context);
1276 
1277  dvalues = erh->dvalues;
1278  dnulls = erh->dnulls;
1279 
1280  for (fnumber = 0; fnumber < erh->nfields; fnumber++)
1281  {
1282  Form_pg_attribute attr = TupleDescAttr(tupdesc, fnumber);
1283  Datum newValue;
1284  bool isnull;
1285 
1286  /* Ignore dropped columns */
1287  if (attr->attisdropped)
1288  continue;
1289 
1290  newValue = newValues[fnumber];
1291  isnull = isnulls[fnumber];
1292 
1293  if (!attr->attbyval)
1294  {
1295  /*
1296  * Copy new field value into record's context, and deal with
1297  * detoasting, if needed.
1298  */
1299  if (!isnull)
1300  {
1301  /* Is it an external toasted value? */
1302  if (attr->attlen == -1 &&
1304  {
1305  if (expand_external)
1306  {
1307  /* Detoast as requested while copying the value */
1308  newValue = PointerGetDatum(detoast_external_attr((struct varlena *) DatumGetPointer(newValue)));
1309  }
1310  else
1311  {
1312  /* Just copy the value */
1313  newValue = datumCopy(newValue, false, -1);
1314  /* If it's still external, remember that */
1315  if (VARATT_IS_EXTERNAL(DatumGetPointer(newValue)))
1316  erh->flags |= ER_FLAG_HAVE_EXTERNAL;
1317  }
1318  }
1319  else
1320  {
1321  /* Not an external value, just copy it */
1322  newValue = datumCopy(newValue, false, attr->attlen);
1323  }
1324 
1325  /* Remember that we have field(s) that need to be pfree'd */
1327  }
1328 
1329  /*
1330  * Free old field value, if any (not likely, since really we ought
1331  * to be inserting into an empty record).
1332  */
1333  if (unlikely(!dnulls[fnumber]))
1334  {
1335  char *oldValue;
1336 
1337  oldValue = (char *) DatumGetPointer(dvalues[fnumber]);
1338  /* Don't try to pfree a part of the original flat record */
1339  if (oldValue < erh->fstartptr || oldValue >= erh->fendptr)
1340  pfree(oldValue);
1341  }
1342  }
1343 
1344  /* And finally we can insert the new field. */
1345  dvalues[fnumber] = newValue;
1346  dnulls[fnumber] = isnull;
1347  }
1348 
1349  /*
1350  * Because we don't guarantee atomicity of set_fields(), we can just leave
1351  * checking of domain constraints to occur as the final step; if it throws
1352  * an error, too bad.
1353  */
1354  if (erh->flags & ER_FLAG_IS_DOMAIN)
1355  {
1356  /* We run domain_check in a short-lived context to limit cruft */
1358 
1360  erh->er_decltypeid,
1361  &erh->er_domaininfo,
1362  erh->hdr.eoh_context);
1363  }
1364 
1365  MemoryContextSwitchTo(oldcxt);
1366 }
#define ER_FLAG_FVALUE_VALID
#define ER_FLAG_HAVE_EXTERNAL
#define PointerGetDatum(X)
Definition: postgres.h:556
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
struct varlena * detoast_external_attr(struct varlena *attr)
Definition: detoast.c:45
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define ER_FLAG_IS_DOMAIN
ExpandedObjectHeader hdr
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:313
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ER_FLAG_DVALUES_VALID
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:130
#define ER_FLAG_DVALUES_ALLOCED
uintptr_t Datum
Definition: postgres.h:367
void domain_check(Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt)
Definition: domains.c:327
#define Assert(condition)
Definition: c.h:732
#define ExpandedRecordGetRODatum(erh)
static MemoryContext get_short_term_cxt(ExpandedRecordHeader *erh)
#define DatumGetPointer(X)
Definition: postgres.h:549
#define ER_FLAG_IS_DUMMY
#define unlikely(x)
Definition: c.h:208
Definition: c.h:549
void deconstruct_expanded_record(ExpandedRecordHeader *erh)
MemoryContext eoh_context

◆ expanded_record_set_tuple()

void expanded_record_set_tuple ( ExpandedRecordHeader erh,
HeapTuple  tuple,
bool  copy,
bool  expand_external 
)

Definition at line 440 of file expandedrecord.c.

References Assert, attbyval, check_domain_for_new_tuple(), DatumGetPointer, ExpandedRecordHeader::dnulls, ExpandedRecordHeader::dvalues, ExpandedObjectHeader::eoh_context, ER_FLAG_DVALUES_ALLOCED, ER_FLAG_FVALUE_ALLOCED, ER_FLAG_FVALUE_VALID, ER_FLAG_HAVE_EXTERNAL, ER_FLAG_IS_DOMAIN, ER_FLAG_IS_DUMMY, ER_FLAGS_NON_DATA, ExpandedRecordHeader::er_short_term_cxt, ExpandedRecordHeader::er_tupdesc, ExpandedRecordHeader::fendptr, ExpandedRecordHeader::flags, ExpandedRecordHeader::flat_size, ExpandedRecordHeader::fstartptr, ExpandedRecordHeader::fvalue, get_short_term_cxt(), ExpandedRecordHeader::hdr, heap_copytuple(), heap_freetuple(), HeapTupleHasExternal, i, MemoryContextReset(), MemoryContextSwitchTo(), ExpandedRecordHeader::nfields, pfree(), HeapTupleData::t_data, HeapTupleData::t_len, toast_flatten_tuple(), and TupleDescAttr.

Referenced by exec_for_query(), exec_move_row(), exec_move_row_from_datum(), and plpgsql_exec_trigger().

444 {
445  int oldflags;
446  HeapTuple oldtuple;
447  char *oldfstartptr;
448  char *oldfendptr;
449  int newflags;
450  HeapTuple newtuple;
451  MemoryContext oldcxt;
452 
453  /* Shouldn't ever be trying to assign new data to a dummy header */
454  Assert(!(erh->flags & ER_FLAG_IS_DUMMY));
455 
456  /*
457  * Before performing the assignment, see if result will satisfy domain.
458  */
459  if (erh->flags & ER_FLAG_IS_DOMAIN)
460  check_domain_for_new_tuple(erh, tuple);
461 
462  /*
463  * If we need to get rid of out-of-line field values, do so, using the
464  * short-term context to avoid leaking whatever cruft the toast fetch
465  * might generate.
466  */
467  if (expand_external && tuple)
468  {
469  /* Assert caller didn't ask for unsupported case */
470  Assert(copy);
471  if (HeapTupleHasExternal(tuple))
472  {
474  tuple = toast_flatten_tuple(tuple, erh->er_tupdesc);
475  MemoryContextSwitchTo(oldcxt);
476  }
477  else
478  expand_external = false; /* need not clean up below */
479  }
480 
481  /*
482  * Initialize new flags, keeping only non-data status bits.
483  */
484  oldflags = erh->flags;
485  newflags = oldflags & ER_FLAGS_NON_DATA;
486 
487  /*
488  * Copy tuple into local storage if needed. We must be sure this succeeds
489  * before we start to modify the expanded record's state.
490  */
491  if (copy && tuple)
492  {
493  oldcxt = MemoryContextSwitchTo(erh->hdr.eoh_context);
494  newtuple = heap_copytuple(tuple);
495  newflags |= ER_FLAG_FVALUE_ALLOCED;
496  MemoryContextSwitchTo(oldcxt);
497 
498  /* We can now flush anything that detoasting might have leaked. */
499  if (expand_external)
501  }
502  else
503  newtuple = tuple;
504 
505  /* Make copies of fields we're about to overwrite */
506  oldtuple = erh->fvalue;
507  oldfstartptr = erh->fstartptr;
508  oldfendptr = erh->fendptr;
509 
510  /*
511  * It's now safe to update the expanded record's state.
512  */
513  if (newtuple)
514  {
515  /* Save flat representation */
516  erh->fvalue = newtuple;
517  erh->fstartptr = (char *) newtuple->t_data;
518  erh->fendptr = ((char *) newtuple->t_data) + newtuple->t_len;
519  newflags |= ER_FLAG_FVALUE_VALID;
520 
521  /* Remember if we have any out-of-line field values */
522  if (HeapTupleHasExternal(newtuple))
523  newflags |= ER_FLAG_HAVE_EXTERNAL;
524  }
525  else
526  {
527  erh->fvalue = NULL;
528  erh->fstartptr = erh->fendptr = NULL;
529  }
530 
531  erh->flags = newflags;
532 
533  /* Reset flat-size info; we don't bother to make it valid now */
534  erh->flat_size = 0;
535 
536  /*
537  * Now, release any storage belonging to old field values. It's safe to
538  * do this because ER_FLAG_DVALUES_VALID is no longer set in erh->flags;
539  * even if we fail partway through, the record is valid, and at worst
540  * we've failed to reclaim some space.
541  */
542  if (oldflags & ER_FLAG_DVALUES_ALLOCED)
543  {
544  TupleDesc tupdesc = erh->er_tupdesc;
545  int i;
546 
547  for (i = 0; i < erh->nfields; i++)
548  {
549  if (!erh->dnulls[i] &&
550  !(TupleDescAttr(tupdesc, i)->attbyval))
551  {
552  char *oldValue = (char *) DatumGetPointer(erh->dvalues[i]);
553 
554  if (oldValue < oldfstartptr || oldValue >= oldfendptr)
555  pfree(oldValue);
556  }
557  }
558  }
559 
560  /* Likewise free the old tuple, if it was locally allocated */
561  if (oldflags & ER_FLAG_FVALUE_ALLOCED)
562  heap_freetuple(oldtuple);
563 
564  /* We won't make a new deconstructed representation until/unless needed */
565 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
#define ER_FLAG_FVALUE_VALID
#define ER_FLAG_HAVE_EXTERNAL
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
#define ER_FLAG_IS_DOMAIN
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
ExpandedObjectHeader hdr
MemoryContext er_short_term_cxt
HeapTupleHeader t_data
Definition: htup.h:68
bool attbyval
Definition: pg_attribute.h:108
void pfree(void *pointer)
Definition: mcxt.c:1056
HeapTuple toast_flatten_tuple(HeapTuple tup, TupleDesc tupleDesc)
Definition: heaptoast.c:343
uint32 t_len
Definition: htup.h:64
#define ER_FLAGS_NON_DATA
#define ER_FLAG_DVALUES_ALLOCED
#define Assert(condition)
Definition: c.h:732
static pg_noinline void check_domain_for_new_tuple(ExpandedRecordHeader *erh, HeapTuple tuple)
#define ER_FLAG_FVALUE_ALLOCED
static MemoryContext get_short_term_cxt(ExpandedRecordHeader *erh)
#define DatumGetPointer(X)
Definition: postgres.h:549
#define ER_FLAG_IS_DUMMY
#define HeapTupleHasExternal(tuple)
Definition: htup_details.h:673
int i
MemoryContext eoh_context

◆ make_expanded_record_from_datum()

Datum make_expanded_record_from_datum ( Datum  recorddatum,
MemoryContext  parentcontext 
)

Definition at line 580 of file expandedrecord.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, DatumGetHeapTupleHeader, EOH_init_header(), EOHPGetRWDatum, ExpandedRecordHeader::er_decltypeid, ER_FLAG_FVALUE_ALLOCED, ER_FLAG_FVALUE_VALID, ER_MAGIC, ExpandedRecordHeader::er_magic, ExpandedRecordHeader::er_typeid, ExpandedRecordHeader::er_typmod, ExpandedRecordHeader::fendptr, ExpandedRecordHeader::flags, ExpandedRecordHeader::fstartptr, ExpandedRecordHeader::fvalue, ExpandedRecordHeader::hdr, heap_copytuple(), HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, HeapTupleHeaderHasExternal, InvalidOid, ItemPointerSetInvalid, MemoryContextAllocZero(), MemoryContextSwitchTo(), HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

Referenced by DatumGetExpandedRecord().

581 {
583  HeapTupleHeader tuphdr;
584  HeapTupleData tmptup;
585  HeapTuple newtuple;
586  MemoryContext objcxt;
587  MemoryContext oldcxt;
588 
589  /*
590  * Allocate private context for expanded object. We use a regular-size
591  * context, not a small one, to improve the odds that we can fit a tupdesc
592  * into it without needing an extra malloc block.
593  */
594  objcxt = AllocSetContextCreate(parentcontext,
595  "expanded record",
597 
598  /* Set up expanded record header, initializing fields to 0/null */
599  erh = (ExpandedRecordHeader *)
601 
602  EOH_init_header(&erh->hdr, &ER_methods, objcxt);
603  erh->er_magic = ER_MAGIC;
604 
605  /*
606  * Detoast and copy source record into private context, as a HeapTuple.
607  * (If we actually have to detoast the source, we'll leak some memory in
608  * the caller's context, but it doesn't seem worth worrying about.)
609  */
610  tuphdr = DatumGetHeapTupleHeader(recorddatum);
611 
612  tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr);
613  ItemPointerSetInvalid(&(tmptup.t_self));
614  tmptup.t_tableOid = InvalidOid;
615  tmptup.t_data = tuphdr;
616 
617  oldcxt = MemoryContextSwitchTo(objcxt);
618  newtuple = heap_copytuple(&tmptup);
620  MemoryContextSwitchTo(oldcxt);
621 
622  /* Fill in composite-type identification info */
623  erh->er_decltypeid = erh->er_typeid = HeapTupleHeaderGetTypeId(tuphdr);
624  erh->er_typmod = HeapTupleHeaderGetTypMod(tuphdr);
625 
626  /* remember we have a flat representation */
627  erh->fvalue = newtuple;
628  erh->fstartptr = (char *) newtuple->t_data;
629  erh->fendptr = ((char *) newtuple->t_data) + newtuple->t_len;
630  erh->flags |= ER_FLAG_FVALUE_VALID;
631 
632  /* Shouldn't need to set ER_FLAG_HAVE_EXTERNAL */
634 
635  /*
636  * We won't look up the tupdesc till we have to, nor make a deconstructed
637  * representation. We don't have enough info to fill flat_size and
638  * friends, either.
639  */
640 
641  /* return a R/W pointer to the expanded record */
642  return EOHPGetRWDatum(&erh->hdr);
643 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
#define AllocSetContextCreate
Definition: memutils.h:170
#define ER_FLAG_FVALUE_VALID
static const ExpandedObjectMethods ER_methods
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ExpandedObjectHeader hdr
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:289
HeapTupleHeader t_data
Definition: htup.h:68
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:468
ItemPointerData t_self
Definition: htup.h:65
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
uint32 t_len
Definition: htup.h:64
void EOH_init_header(ExpandedObjectHeader *eohptr, const ExpandedObjectMethods *methods, MemoryContext obj_context)
Definition: expandeddatum.c:48
Oid t_tableOid
Definition: htup.h:66
#define ER_MAGIC
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:458
#define HeapTupleHeaderHasExternal(tup)
Definition: htup_details.h:539
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:839
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:732
#define ER_FLAG_FVALUE_ALLOCED
#define EOHPGetRWDatum(eohptr)
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:172
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:452

◆ make_expanded_record_from_exprecord()

ExpandedRecordHeader* make_expanded_record_from_exprecord ( ExpandedRecordHeader olderh,
MemoryContext  parentcontext 
)

Definition at line 329 of file expandedrecord.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, MemoryContextCallback::arg, CreateTupleDescCopy(), ExpandedRecordHeader::dnulls, ExpandedRecordHeader::dvalues, ExpandedObjectHeader::eoh_context, EOH_init_header(), ExpandedRecordHeader::er_decltypeid, ER_FLAG_IS_DOMAIN, ER_FLAG_TUPDESC_ALLOCED, ER_MAGIC, ExpandedRecordHeader::er_magic, ER_mc_callback(), ExpandedRecordHeader::er_mcb, ExpandedRecordHeader::er_tupdesc, ExpandedRecordHeader::er_tupdesc_id, ExpandedRecordHeader::er_typeid, ExpandedRecordHeader::er_typmod, expanded_record_get_tupdesc(), ExpandedRecordHeader::flags, MemoryContextCallback::func, ExpandedRecordHeader::hdr, MAXALIGN, MemoryContextAlloc(), MemoryContextRegisterResetCallback(), MemoryContextSwitchTo(), TupleDescData::natts, ExpandedRecordHeader::nfields, and TupleDescData::tdrefcount.

Referenced by make_expanded_record_for_rec(), and plpgsql_exec_trigger().

331 {
333  TupleDesc tupdesc = expanded_record_get_tupdesc(olderh);
334  MemoryContext objcxt;
335  MemoryContext oldcxt;
336  char *chunk;
337 
338  /*
339  * Allocate private context for expanded object. We use a regular-size
340  * context, not a small one, to improve the odds that we can fit a tupdesc
341  * into it without needing an extra malloc block.
342  */
343  objcxt = AllocSetContextCreate(parentcontext,
344  "expanded record",
346 
347  /*
348  * Since we already know the number of fields in the tupdesc, we can
349  * allocate the dvalues/dnulls arrays along with the record header. This
350  * is useless if we never need those arrays, but it costs almost nothing,
351  * and it will save a palloc cycle if we do need them.
352  */
353  erh = (ExpandedRecordHeader *)
355  + tupdesc->natts * (sizeof(Datum) + sizeof(bool)));
356 
357  /* Ensure all header fields are initialized to 0/null */
358  memset(erh, 0, sizeof(ExpandedRecordHeader));
359 
360  EOH_init_header(&erh->hdr, &ER_methods, objcxt);
361  erh->er_magic = ER_MAGIC;
362 
363  /* Set up dvalues/dnulls, with no valid contents as yet */
364  chunk = (char *) erh + MAXALIGN(sizeof(ExpandedRecordHeader));
365  erh->dvalues = (Datum *) chunk;
366  erh->dnulls = (bool *) (chunk + tupdesc->natts * sizeof(Datum));
367  erh->nfields = tupdesc->natts;
368 
369  /* Fill in composite-type identification info */
370  erh->er_decltypeid = olderh->er_decltypeid;
371  erh->er_typeid = olderh->er_typeid;
372  erh->er_typmod = olderh->er_typmod;
373  erh->er_tupdesc_id = olderh->er_tupdesc_id;
374 
375  /* The only flag bit that transfers over is IS_DOMAIN */
376  erh->flags = olderh->flags & ER_FLAG_IS_DOMAIN;
377 
378  /*
379  * Copy tupdesc if needed, but we prefer to bump its refcount if possible.
380  * We manage the refcount with a memory context callback rather than
381  * assuming that the CurrentResourceOwner is longer-lived than this
382  * expanded object.
383  */
384  if (tupdesc->tdrefcount >= 0)
385  {
386  /* Register callback to release the refcount */
387  erh->er_mcb.func = ER_mc_callback;
388  erh->er_mcb.arg = (void *) erh;
390  &erh->er_mcb);
391 
392  /* And save the pointer */
393  erh->er_tupdesc = tupdesc;
394  tupdesc->tdrefcount++;
395  }
396  else if (olderh->flags & ER_FLAG_TUPDESC_ALLOCED)
397  {
398  /* We need to make our own copy of the tupdesc */
399  oldcxt = MemoryContextSwitchTo(objcxt);
400  erh->er_tupdesc = CreateTupleDescCopy(tupdesc);
402  MemoryContextSwitchTo(oldcxt);
403  }
404  else
405  {
406  /*
407  * Assume the tupdesc will outlive this expanded object, just like
408  * we're assuming it will outlive the source object.
409  */
410  erh->er_tupdesc = tupdesc;
411  }
412 
413  /*
414  * We don't set ER_FLAG_DVALUES_VALID or ER_FLAG_FVALUE_VALID, so the
415  * record remains logically empty.
416  */
417 
418  return erh;
419 }
MemoryContextCallbackFunction func
Definition: palloc.h:49
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
#define AllocSetContextCreate
Definition: memutils.h:170
static const ExpandedObjectMethods ER_methods
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define ER_FLAG_IS_DOMAIN
ExpandedObjectHeader hdr
#define ER_FLAG_TUPDESC_ALLOCED
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void EOH_init_header(ExpandedObjectHeader *eohptr, const ExpandedObjectMethods *methods, MemoryContext obj_context)
Definition: expandeddatum.c:48
static TupleDesc expanded_record_get_tupdesc(ExpandedRecordHeader *erh)
MemoryContextCallback er_mcb
uintptr_t Datum
Definition: postgres.h:367
#define ER_MAGIC
#define MAXALIGN(LEN)
Definition: c.h:685
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
int tdrefcount
Definition: tupdesc.h:84
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:285
static void ER_mc_callback(void *arg)
MemoryContext eoh_context

◆ make_expanded_record_from_tupdesc()

ExpandedRecordHeader* make_expanded_record_from_tupdesc ( TupleDesc  tupdesc,
MemoryContext  parentcontext 
)

Definition at line 205 of file expandedrecord.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, MemoryContextCallback::arg, assign_record_type_identifier(), CreateTupleDescCopy(), ExpandedRecordHeader::dnulls, ExpandedRecordHeader::dvalues, ExpandedObjectHeader::eoh_context, EOH_init_header(), ExpandedRecordHeader::er_decltypeid, ER_FLAG_TUPDESC_ALLOCED, ER_MAGIC, ExpandedRecordHeader::er_magic, ER_mc_callback(), ExpandedRecordHeader::er_mcb, ExpandedRecordHeader::er_tupdesc, ExpandedRecordHeader::er_tupdesc_id, ExpandedRecordHeader::er_typeid, ExpandedRecordHeader::er_typmod, ereport, errcode(), errmsg(), ERROR, ExpandedRecordHeader::flags, format_type_be(), MemoryContextCallback::func, ExpandedRecordHeader::hdr, lookup_type_cache(), MAXALIGN, MemoryContextAlloc(), MemoryContextRegisterResetCallback(), MemoryContextSwitchTo(), TupleDescData::natts, ExpandedRecordHeader::nfields, TupleDescData::tdrefcount, TupleDescData::tdtypeid, TupleDescData::tdtypmod, TypeCacheEntry::tupDesc, TypeCacheEntry::tupDesc_identifier, and TYPECACHE_TUPDESC.

Referenced by make_expanded_record_for_rec(), and plpgsql_exec_trigger().

207 {
209  uint64 tupdesc_id;
210  MemoryContext objcxt;
211  MemoryContext oldcxt;
212  char *chunk;
213 
214  if (tupdesc->tdtypeid != RECORDOID)
215  {
216  /*
217  * If it's a named composite type (not RECORD), we prefer to reference
218  * the typcache's copy of the tupdesc, which is guaranteed to be
219  * refcounted (the given tupdesc might not be). In any case, we need
220  * to consult the typcache to get the correct tupdesc identifier.
221  *
222  * Note that tdtypeid couldn't be a domain type, so we need not
223  * consider that case here.
224  */
225  TypeCacheEntry *typentry;
226 
227  typentry = lookup_type_cache(tupdesc->tdtypeid, TYPECACHE_TUPDESC);
228  if (typentry->tupDesc == NULL)
229  ereport(ERROR,
230  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
231  errmsg("type %s is not composite",
232  format_type_be(tupdesc->tdtypeid))));
233  tupdesc = typentry->tupDesc;
234  tupdesc_id = typentry->tupDesc_identifier;
235  }
236  else
237  {
238  /*
239  * For RECORD types, get the appropriate unique identifier (possibly
240  * freshly assigned).
241  */
242  tupdesc_id = assign_record_type_identifier(tupdesc->tdtypeid,
243  tupdesc->tdtypmod);
244  }
245 
246  /*
247  * Allocate private context for expanded object. We use a regular-size
248  * context, not a small one, to improve the odds that we can fit a tupdesc
249  * into it without needing an extra malloc block.
250  */
251  objcxt = AllocSetContextCreate(parentcontext,
252  "expanded record",
254 
255  /*
256  * Since we already know the number of fields in the tupdesc, we can
257  * allocate the dvalues/dnulls arrays along with the record header. This
258  * is useless if we never need those arrays, but it costs almost nothing,
259  * and it will save a palloc cycle if we do need them.
260  */
261  erh = (ExpandedRecordHeader *)
263  + tupdesc->natts * (sizeof(Datum) + sizeof(bool)));
264 
265  /* Ensure all header fields are initialized to 0/null */
266  memset(erh, 0, sizeof(ExpandedRecordHeader));
267 
268  EOH_init_header(&erh->hdr, &ER_methods, objcxt);
269  erh->er_magic = ER_MAGIC;
270 
271  /* Set up dvalues/dnulls, with no valid contents as yet */
272  chunk = (char *) erh + MAXALIGN(sizeof(ExpandedRecordHeader));
273  erh->dvalues = (Datum *) chunk;
274  erh->dnulls = (bool *) (chunk + tupdesc->natts * sizeof(Datum));
275  erh->nfields = tupdesc->natts;
276 
277  /* Fill in composite-type identification info */
278  erh->er_decltypeid = erh->er_typeid = tupdesc->tdtypeid;
279  erh->er_typmod = tupdesc->tdtypmod;
280  erh->er_tupdesc_id = tupdesc_id;
281 
282  /*
283  * Copy tupdesc if needed, but we prefer to bump its refcount if possible.
284  * We manage the refcount with a memory context callback rather than
285  * assuming that the CurrentResourceOwner is longer-lived than this
286  * expanded object.
287  */
288  if (tupdesc->tdrefcount >= 0)
289  {
290  /* Register callback to release the refcount */
291  erh->er_mcb.func = ER_mc_callback;
292  erh->er_mcb.arg = (void *) erh;
294  &erh->er_mcb);
295 
296  /* And save the pointer */
297  erh->er_tupdesc = tupdesc;
298  tupdesc->tdrefcount++;
299  }
300  else
301  {
302  /* Just copy it */
303  oldcxt = MemoryContextSwitchTo(objcxt);
304  erh->er_tupdesc = CreateTupleDescCopy(tupdesc);
306  MemoryContextSwitchTo(oldcxt);
307  }
308 
309  /*
310  * We don't set ER_FLAG_DVALUES_VALID or ER_FLAG_FVALUE_VALID, so the
311  * record remains logically empty.
312  */
313 
314  return erh;
315 }
MemoryContextCallbackFunction func
Definition: palloc.h:49
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
#define AllocSetContextCreate
Definition: memutils.h:170
static const ExpandedObjectMethods ER_methods
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:570
char * format_type_be(Oid type_oid)
Definition: format_type.c:326
ExpandedObjectHeader hdr
#define ER_FLAG_TUPDESC_ALLOCED
#define ERROR
Definition: elog.h:43
int32 tdtypmod
Definition: tupdesc.h:83
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void EOH_init_header(ExpandedObjectHeader *eohptr, const ExpandedObjectMethods *methods, MemoryContext obj_context)
Definition: expandeddatum.c:48
#define ereport(elevel, rest)
Definition: elog.h:141
MemoryContextCallback er_mcb
uintptr_t Datum
Definition: postgres.h:367
#define ER_MAGIC
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:322
#define MAXALIGN(LEN)
Definition: c.h:685
uint64 tupDesc_identifier
Definition: typcache.h:87
Oid tdtypeid
Definition: tupdesc.h:82
int errmsg(const char *fmt,...)
Definition: elog.c:784
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
int tdrefcount
Definition: tupdesc.h:84
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:285
TupleDesc tupDesc
Definition: typcache.h:86
uint64 assign_record_type_identifier(Oid type_id, int32 typmod)
Definition: typcache.c:1847
static void ER_mc_callback(void *arg)
MemoryContext eoh_context
#define TYPECACHE_TUPDESC
Definition: typcache.h:136

◆ make_expanded_record_from_typeid()

ExpandedRecordHeader* make_expanded_record_from_typeid ( Oid  type_id,
int32  typmod,
MemoryContext  parentcontext 
)

Definition at line 69 of file expandedrecord.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, MemoryContextCallback::arg, assign_record_type_identifier(), DecrTupleDescRefCount(), ExpandedRecordHeader::dnulls, TypeCacheEntry::domainBaseType, ExpandedRecordHeader::dvalues, ExpandedObjectHeader::eoh_context, EOH_init_header(), ExpandedRecordHeader::er_decltypeid, ER_FLAG_IS_DOMAIN, ER_MAGIC, ExpandedRecordHeader::er_magic, ER_mc_callback(), ExpandedRecordHeader::er_mcb, ExpandedRecordHeader::er_tupdesc, ExpandedRecordHeader::er_tupdesc_id, ExpandedRecordHeader::er_typeid, ExpandedRecordHeader::er_typmod, ereport, errcode(), errmsg(), ERROR, ExpandedRecordHeader::flags, format_type_be(), MemoryContextCallback::func, ExpandedRecordHeader::hdr, lookup_rowtype_tupdesc(), lookup_type_cache(), MAXALIGN, MemoryContextAlloc(), MemoryContextRegisterResetCallback(), TupleDescData::natts, ExpandedRecordHeader::nfields, TupleDescData::tdrefcount, TupleDescData::tdtypeid, TupleDescData::tdtypmod, TypeCacheEntry::tupDesc, TypeCacheEntry::tupDesc_identifier, TYPECACHE_DOMAIN_BASE_INFO, TYPECACHE_TUPDESC, and TypeCacheEntry::typtype.

Referenced by exec_move_row_from_datum(), instantiate_empty_record_variable(), and make_expanded_record_for_rec().

71 {
73  int flags = 0;
74  TupleDesc tupdesc;
75  uint64 tupdesc_id;
76  MemoryContext objcxt;
77  char *chunk;
78 
79  if (type_id != RECORDOID)
80  {
81  /*
82  * Consult the typcache to see if it's a domain over composite, and in
83  * any case to get the tupdesc and tupdesc identifier.
84  */
85  TypeCacheEntry *typentry;
86 
87  typentry = lookup_type_cache(type_id,
90  if (typentry->typtype == TYPTYPE_DOMAIN)
91  {
92  flags |= ER_FLAG_IS_DOMAIN;
93  typentry = lookup_type_cache(typentry->domainBaseType,
95  }
96  if (typentry->tupDesc == NULL)
97  ereport(ERROR,
98  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
99  errmsg("type %s is not composite",
100  format_type_be(type_id))));
101  tupdesc = typentry->tupDesc;
102  tupdesc_id = typentry->tupDesc_identifier;
103  }
104  else
105  {
106  /*
107  * For RECORD types, get the tupdesc and identifier from typcache.
108  */
109  tupdesc = lookup_rowtype_tupdesc(type_id, typmod);
110  tupdesc_id = assign_record_type_identifier(type_id, typmod);
111  }
112 
113  /*
114  * Allocate private context for expanded object. We use a regular-size
115  * context, not a small one, to improve the odds that we can fit a tupdesc
116  * into it without needing an extra malloc block. (This code path doesn't
117  * ever need to copy a tupdesc into the expanded record, but let's be
118  * consistent with the other ways of making an expanded record.)
119  */
120  objcxt = AllocSetContextCreate(parentcontext,
121  "expanded record",
123 
124  /*
125  * Since we already know the number of fields in the tupdesc, we can
126  * allocate the dvalues/dnulls arrays along with the record header. This
127  * is useless if we never need those arrays, but it costs almost nothing,
128  * and it will save a palloc cycle if we do need them.
129  */
130  erh = (ExpandedRecordHeader *)
132  + tupdesc->natts * (sizeof(Datum) + sizeof(bool)));
133 
134  /* Ensure all header fields are initialized to 0/null */
135  memset(erh, 0, sizeof(ExpandedRecordHeader));
136 
137  EOH_init_header(&erh->hdr, &ER_methods, objcxt);
138  erh->er_magic = ER_MAGIC;
139 
140  /* Set up dvalues/dnulls, with no valid contents as yet */
141  chunk = (char *) erh + MAXALIGN(sizeof(ExpandedRecordHeader));
142  erh->dvalues = (Datum *) chunk;
143  erh->dnulls = (bool *) (chunk + tupdesc->natts * sizeof(Datum));
144  erh->nfields = tupdesc->natts;
145 
146  /* Fill in composite-type identification info */
147  erh->er_decltypeid = type_id;
148  erh->er_typeid = tupdesc->tdtypeid;
149  erh->er_typmod = tupdesc->tdtypmod;
150  erh->er_tupdesc_id = tupdesc_id;
151 
152  erh->flags = flags;
153 
154  /*
155  * If what we got from the typcache is a refcounted tupdesc, we need to
156  * acquire our own refcount on it. We manage the refcount with a memory
157  * context callback rather than assuming that the CurrentResourceOwner is
158  * longer-lived than this expanded object.
159  */
160  if (tupdesc->tdrefcount >= 0)
161  {
162  /* Register callback to release the refcount */
163  erh->er_mcb.func = ER_mc_callback;
164  erh->er_mcb.arg = (void *) erh;
166  &erh->er_mcb);
167 
168  /* And save the pointer */
169  erh->er_tupdesc = tupdesc;
170  tupdesc->tdrefcount++;
171 
172  /* If we called lookup_rowtype_tupdesc, release the pin it took */
173  if (type_id == RECORDOID)
174  DecrTupleDescRefCount(tupdesc);
175  }
176  else
177  {
178  /*
179  * If it's not refcounted, just assume it will outlive the expanded
180  * object. (This can happen for shared record types, for instance.)
181  */
182  erh->er_tupdesc = tupdesc;
183  }
184 
185  /*
186  * We don't set ER_FLAG_DVALUES_VALID or ER_FLAG_FVALUE_VALID, so the
187  * record remains logically empty.
188  */
189 
190  return erh;
191 }
MemoryContextCallbackFunction func
Definition: palloc.h:49
#define AllocSetContextCreate
Definition: memutils.h:170
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1652
static const ExpandedObjectMethods ER_methods
int errcode(int sqlerrcode)
Definition: elog.c:570
char * format_type_be(Oid type_oid)
Definition: format_type.c:326
#define ER_FLAG_IS_DOMAIN
ExpandedObjectHeader hdr
Oid domainBaseType
Definition: typcache.h:105
#define ERROR
Definition: elog.h:43
int32 tdtypmod
Definition: tupdesc.h:83
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void EOH_init_header(ExpandedObjectHeader *eohptr, const ExpandedObjectMethods *methods, MemoryContext obj_context)
Definition: expandeddatum.c:48
#define ereport(elevel, rest)
Definition: elog.h:141
#define TYPECACHE_DOMAIN_BASE_INFO
Definition: typcache.h:140
MemoryContextCallback er_mcb
uintptr_t Datum
Definition: postgres.h:367
#define ER_MAGIC
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:322
char typtype
Definition: typcache.h:41
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:393
#define MAXALIGN(LEN)
Definition: c.h:685
uint64 tupDesc_identifier
Definition: typcache.h:87
Oid tdtypeid
Definition: tupdesc.h:82
int errmsg(const char *fmt,...)
Definition: elog.c:784
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
int tdrefcount
Definition: tupdesc.h:84
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:285
TupleDesc tupDesc
Definition: typcache.h:86
uint64 assign_record_type_identifier(Oid type_id, int32 typmod)
Definition: typcache.c:1847
static void ER_mc_callback(void *arg)
MemoryContext eoh_context
#define TYPECACHE_TUPDESC
Definition: typcache.h:136