PostgreSQL Source Code  git master
heaptuple.c File Reference
#include "postgres.h"
#include "access/heaptoast.h"
#include "access/sysattr.h"
#include "access/tupdesc_details.h"
#include "common/hashfn.h"
#include "utils/datum.h"
#include "utils/expandeddatum.h"
#include "utils/hsearch.h"
#include "utils/memutils.h"
Include dependency graph for heaptuple.c:

Go to the source code of this file.

Data Structures

struct  missing_cache_key
 

Macros

#define ATT_IS_PACKABLE(att)    ((att)->attlen == -1 && (att)->attstorage != TYPSTORAGE_PLAIN)
 
#define VARLENA_ATT_IS_PACKABLE(att)    ((att)->attstorage != TYPSTORAGE_PLAIN)
 

Functions

static uint32 missing_hash (const void *key, Size keysize)
 
static int missing_match (const void *key1, const void *key2, Size keysize)
 
static void init_missing_cache ()
 
Datum getmissingattr (TupleDesc tupleDesc, int attnum, bool *isnull)
 
Size heap_compute_data_size (TupleDesc tupleDesc, const Datum *values, const bool *isnull)
 
static void fill_val (Form_pg_attribute att, bits8 **bit, int *bitmask, char **dataP, uint16 *infomask, Datum datum, bool isnull)
 
void heap_fill_tuple (TupleDesc tupleDesc, const Datum *values, const bool *isnull, char *data, Size data_size, uint16 *infomask, bits8 *bit)
 
bool heap_attisnull (HeapTuple tup, int attnum, TupleDesc tupleDesc)
 
Datum nocachegetattr (HeapTuple tup, int attnum, TupleDesc tupleDesc)
 
Datum heap_getsysattr (HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
 
HeapTuple heap_copytuple (HeapTuple tuple)
 
void heap_copytuple_with_tuple (HeapTuple src, HeapTuple dest)
 
static void expand_tuple (HeapTuple *targetHeapTuple, MinimalTuple *targetMinimalTuple, HeapTuple sourceTuple, TupleDesc tupleDesc)
 
MinimalTuple minimal_expand_tuple (HeapTuple sourceTuple, TupleDesc tupleDesc)
 
HeapTuple heap_expand_tuple (HeapTuple sourceTuple, TupleDesc tupleDesc)
 
Datum heap_copy_tuple_as_datum (HeapTuple tuple, TupleDesc tupleDesc)
 
HeapTuple heap_form_tuple (TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
 
HeapTuple heap_modify_tuple (HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
 
HeapTuple heap_modify_tuple_by_cols (HeapTuple tuple, TupleDesc tupleDesc, int nCols, const int *replCols, const Datum *replValues, const bool *replIsnull)
 
void heap_deform_tuple (HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
 
void heap_freetuple (HeapTuple htup)
 
MinimalTuple heap_form_minimal_tuple (TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
 
void heap_free_minimal_tuple (MinimalTuple mtup)
 
MinimalTuple heap_copy_minimal_tuple (MinimalTuple mtup)
 
HeapTuple heap_tuple_from_minimal_tuple (MinimalTuple mtup)
 
MinimalTuple minimal_tuple_from_heap_tuple (HeapTuple htup)
 
size_t varsize_any (void *p)
 

Variables

static HTABmissing_cache = NULL
 

Macro Definition Documentation

◆ ATT_IS_PACKABLE

#define ATT_IS_PACKABLE (   att)     ((att)->attlen == -1 && (att)->attstorage != TYPSTORAGE_PLAIN)

Definition at line 80 of file heaptuple.c.

◆ VARLENA_ATT_IS_PACKABLE

#define VARLENA_ATT_IS_PACKABLE (   att)     ((att)->attstorage != TYPSTORAGE_PLAIN)

Definition at line 83 of file heaptuple.c.

Function Documentation

◆ expand_tuple()

static void expand_tuple ( HeapTuple targetHeapTuple,
MinimalTuple targetMinimalTuple,
HeapTuple  sourceTuple,
TupleDesc  tupleDesc 
)
static

Definition at line 828 of file heaptuple.c.

832 {
833  AttrMissing *attrmiss = NULL;
834  int attnum;
835  int firstmissingnum;
836  bool hasNulls = HeapTupleHasNulls(sourceTuple);
837  HeapTupleHeader targetTHeader;
838  HeapTupleHeader sourceTHeader = sourceTuple->t_data;
839  int sourceNatts = HeapTupleHeaderGetNatts(sourceTHeader);
840  int natts = tupleDesc->natts;
841  int sourceNullLen;
842  int targetNullLen;
843  Size sourceDataLen = sourceTuple->t_len - sourceTHeader->t_hoff;
844  Size targetDataLen;
845  Size len;
846  int hoff;
847  bits8 *nullBits = NULL;
848  int bitMask = 0;
849  char *targetData;
850  uint16 *infoMask;
851 
852  Assert((targetHeapTuple && !targetMinimalTuple)
853  || (!targetHeapTuple && targetMinimalTuple));
854 
855  Assert(sourceNatts < natts);
856 
857  sourceNullLen = (hasNulls ? BITMAPLEN(sourceNatts) : 0);
858 
859  targetDataLen = sourceDataLen;
860 
861  if (tupleDesc->constr &&
862  tupleDesc->constr->missing)
863  {
864  /*
865  * If there are missing values we want to put them into the tuple.
866  * Before that we have to compute the extra length for the values
867  * array and the variable length data.
868  */
869  attrmiss = tupleDesc->constr->missing;
870 
871  /*
872  * Find the first item in attrmiss for which we don't have a value in
873  * the source. We can ignore all the missing entries before that.
874  */
875  for (firstmissingnum = sourceNatts;
876  firstmissingnum < natts;
877  firstmissingnum++)
878  {
879  if (attrmiss[firstmissingnum].am_present)
880  break;
881  else
882  hasNulls = true;
883  }
884 
885  /*
886  * Now walk the missing attributes. If there is a missing value make
887  * space for it. Otherwise, it's going to be NULL.
888  */
889  for (attnum = firstmissingnum;
890  attnum < natts;
891  attnum++)
892  {
893  if (attrmiss[attnum].am_present)
894  {
895  Form_pg_attribute att = TupleDescAttr(tupleDesc, attnum);
896 
897  targetDataLen = att_align_datum(targetDataLen,
898  att->attalign,
899  att->attlen,
900  attrmiss[attnum].am_value);
901 
902  targetDataLen = att_addlength_pointer(targetDataLen,
903  att->attlen,
904  attrmiss[attnum].am_value);
905  }
906  else
907  {
908  /* no missing value, so it must be null */
909  hasNulls = true;
910  }
911  }
912  } /* end if have missing values */
913  else
914  {
915  /*
916  * If there are no missing values at all then NULLS must be allowed,
917  * since some of the attributes are known to be absent.
918  */
919  hasNulls = true;
920  }
921 
922  len = 0;
923 
924  if (hasNulls)
925  {
926  targetNullLen = BITMAPLEN(natts);
927  len += targetNullLen;
928  }
929  else
930  targetNullLen = 0;
931 
932  /*
933  * Allocate and zero the space needed. Note that the tuple body and
934  * HeapTupleData management structure are allocated in one chunk.
935  */
936  if (targetHeapTuple)
937  {
938  len += offsetof(HeapTupleHeaderData, t_bits);
939  hoff = len = MAXALIGN(len); /* align user data safely */
940  len += targetDataLen;
941 
942  *targetHeapTuple = (HeapTuple) palloc0(HEAPTUPLESIZE + len);
943  (*targetHeapTuple)->t_data
944  = targetTHeader
945  = (HeapTupleHeader) ((char *) *targetHeapTuple + HEAPTUPLESIZE);
946  (*targetHeapTuple)->t_len = len;
947  (*targetHeapTuple)->t_tableOid = sourceTuple->t_tableOid;
948  (*targetHeapTuple)->t_self = sourceTuple->t_self;
949 
950  targetTHeader->t_infomask = sourceTHeader->t_infomask;
951  targetTHeader->t_hoff = hoff;
952  HeapTupleHeaderSetNatts(targetTHeader, natts);
953  HeapTupleHeaderSetDatumLength(targetTHeader, len);
954  HeapTupleHeaderSetTypeId(targetTHeader, tupleDesc->tdtypeid);
955  HeapTupleHeaderSetTypMod(targetTHeader, tupleDesc->tdtypmod);
956  /* We also make sure that t_ctid is invalid unless explicitly set */
957  ItemPointerSetInvalid(&(targetTHeader->t_ctid));
958  if (targetNullLen > 0)
959  nullBits = (bits8 *) ((char *) (*targetHeapTuple)->t_data
960  + offsetof(HeapTupleHeaderData, t_bits));
961  targetData = (char *) (*targetHeapTuple)->t_data + hoff;
962  infoMask = &(targetTHeader->t_infomask);
963  }
964  else
965  {
967  hoff = len = MAXALIGN(len); /* align user data safely */
968  len += targetDataLen;
969 
970  *targetMinimalTuple = (MinimalTuple) palloc0(len);
971  (*targetMinimalTuple)->t_len = len;
972  (*targetMinimalTuple)->t_hoff = hoff + MINIMAL_TUPLE_OFFSET;
973  (*targetMinimalTuple)->t_infomask = sourceTHeader->t_infomask;
974  /* Same macro works for MinimalTuples */
975  HeapTupleHeaderSetNatts(*targetMinimalTuple, natts);
976  if (targetNullLen > 0)
977  nullBits = (bits8 *) ((char *) *targetMinimalTuple
978  + offsetof(MinimalTupleData, t_bits));
979  targetData = (char *) *targetMinimalTuple + hoff;
980  infoMask = &((*targetMinimalTuple)->t_infomask);
981  }
982 
983  if (targetNullLen > 0)
984  {
985  if (sourceNullLen > 0)
986  {
987  /* if bitmap pre-existed copy in - all is set */
988  memcpy(nullBits,
989  ((char *) sourceTHeader)
990  + offsetof(HeapTupleHeaderData, t_bits),
991  sourceNullLen);
992  nullBits += sourceNullLen - 1;
993  }
994  else
995  {
996  sourceNullLen = BITMAPLEN(sourceNatts);
997  /* Set NOT NULL for all existing attributes */
998  memset(nullBits, 0xff, sourceNullLen);
999 
1000  nullBits += sourceNullLen - 1;
1001 
1002  if (sourceNatts & 0x07)
1003  {
1004  /* build the mask (inverted!) */
1005  bitMask = 0xff << (sourceNatts & 0x07);
1006  /* Voila */
1007  *nullBits = ~bitMask;
1008  }
1009  }
1010 
1011  bitMask = (1 << ((sourceNatts - 1) & 0x07));
1012  } /* End if have null bitmap */
1013 
1014  memcpy(targetData,
1015  ((char *) sourceTuple->t_data) + sourceTHeader->t_hoff,
1016  sourceDataLen);
1017 
1018  targetData += sourceDataLen;
1019 
1020  /* Now fill in the missing values */
1021  for (attnum = sourceNatts; attnum < natts; attnum++)
1022  {
1023 
1024  Form_pg_attribute attr = TupleDescAttr(tupleDesc, attnum);
1025 
1026  if (attrmiss && attrmiss[attnum].am_present)
1027  {
1028  fill_val(attr,
1029  nullBits ? &nullBits : NULL,
1030  &bitMask,
1031  &targetData,
1032  infoMask,
1033  attrmiss[attnum].am_value,
1034  false);
1035  }
1036  else
1037  {
1038  fill_val(attr,
1039  &nullBits,
1040  &bitMask,
1041  &targetData,
1042  infoMask,
1043  (Datum) 0,
1044  true);
1045  }
1046  } /* end loop over missing attributes */
1047 }
#define MAXALIGN(LEN)
Definition: c.h:765
#define Assert(condition)
Definition: c.h:812
uint8 bits8
Definition: c.h:492
uint16_t uint16
Definition: c.h:484
size_t Size
Definition: c.h:559
static void fill_val(Form_pg_attribute att, bits8 **bit, int *bitmask, char **dataP, uint16 *infomask, Datum datum, bool isnull)
Definition: heaptuple.c:271
#define HEAPTUPLESIZE
Definition: htup.h:73
HeapTupleData * HeapTuple
Definition: htup.h:71
MinimalTupleData * MinimalTuple
Definition: htup.h:27
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define MINIMAL_TUPLE_OFFSET
Definition: htup_details.h:617
#define HeapTupleHeaderSetTypMod(tup, typmod)
Definition: htup_details.h:471
#define HeapTupleHeaderGetNatts(tup)
Definition: htup_details.h:529
#define SizeofMinimalTupleHeader
Definition: htup_details.h:647
#define BITMAPLEN(NATTS)
Definition: htup_details.h:545
#define HeapTupleHeaderSetNatts(tup, natts)
Definition: htup_details.h:532
#define HeapTupleHeaderSetDatumLength(tup, len)
Definition: htup_details.h:453
#define HeapTupleHasNulls(tuple)
Definition: htup_details.h:659
#define HeapTupleHeaderSetTypeId(tup, typeid)
Definition: htup_details.h:461
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition: itemptr.h:184
void * palloc0(Size size)
Definition: mcxt.c:1347
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
const void size_t len
uintptr_t Datum
Definition: postgres.h:64
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
HeapTupleHeader t_data
Definition: htup.h:68
Oid t_tableOid
Definition: htup.h:66
ItemPointerData t_ctid
Definition: htup_details.h:161
struct AttrMissing * missing
Definition: tupdesc.h:41
TupleConstr * constr
Definition: tupdesc.h:85
int32 tdtypmod
Definition: tupdesc.h:83
Oid tdtypeid
Definition: tupdesc.h:82
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define att_align_datum(cur_offset, attalign, attlen, attdatum)
Definition: tupmacs.h:86
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:157

References AttrMissing::am_value, Assert, att_addlength_pointer, att_align_datum, attnum, BITMAPLEN, TupleDescData::constr, fill_val(), HeapTupleHasNulls, HeapTupleHeaderGetNatts, HeapTupleHeaderSetDatumLength, HeapTupleHeaderSetNatts, HeapTupleHeaderSetTypeId, HeapTupleHeaderSetTypMod, HEAPTUPLESIZE, ItemPointerSetInvalid(), len, MAXALIGN, MINIMAL_TUPLE_OFFSET, TupleConstr::missing, TupleDescData::natts, palloc0(), SizeofMinimalTupleHeader, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, HeapTupleHeaderData::t_infomask, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TupleDescData::tdtypeid, TupleDescData::tdtypmod, and TupleDescAttr.

Referenced by heap_expand_tuple(), and minimal_expand_tuple().

◆ fill_val()

static void fill_val ( Form_pg_attribute  att,
bits8 **  bit,
int *  bitmask,
char **  dataP,
uint16 infomask,
Datum  datum,
bool  isnull 
)
inlinestatic

Definition at line 271 of file heaptuple.c.

278 {
279  Size data_length;
280  char *data = *dataP;
281 
282  /*
283  * If we're building a null bitmap, set the appropriate bit for the
284  * current column value here.
285  */
286  if (bit != NULL)
287  {
288  if (*bitmask != HIGHBIT)
289  *bitmask <<= 1;
290  else
291  {
292  *bit += 1;
293  **bit = 0x0;
294  *bitmask = 1;
295  }
296 
297  if (isnull)
298  {
299  *infomask |= HEAP_HASNULL;
300  return;
301  }
302 
303  **bit |= *bitmask;
304  }
305 
306  /*
307  * XXX we use the att_align macros on the pointer value itself, not on an
308  * offset. This is a bit of a hack.
309  */
310  if (att->attbyval)
311  {
312  /* pass-by-value */
313  data = (char *) att_align_nominal(data, att->attalign);
314  store_att_byval(data, datum, att->attlen);
315  data_length = att->attlen;
316  }
317  else if (att->attlen == -1)
318  {
319  /* varlena */
320  Pointer val = DatumGetPointer(datum);
321 
322  *infomask |= HEAP_HASVARWIDTH;
323  if (VARATT_IS_EXTERNAL(val))
324  {
326  {
327  /*
328  * we want to flatten the expanded value so that the
329  * constructed tuple doesn't depend on it
330  */
331  ExpandedObjectHeader *eoh = DatumGetEOHP(datum);
332 
333  data = (char *) att_align_nominal(data,
334  att->attalign);
335  data_length = EOH_get_flat_size(eoh);
336  EOH_flatten_into(eoh, data, data_length);
337  }
338  else
339  {
340  *infomask |= HEAP_HASEXTERNAL;
341  /* no alignment, since it's short by definition */
342  data_length = VARSIZE_EXTERNAL(val);
343  memcpy(data, val, data_length);
344  }
345  }
346  else if (VARATT_IS_SHORT(val))
347  {
348  /* no alignment for short varlenas */
349  data_length = VARSIZE_SHORT(val);
350  memcpy(data, val, data_length);
351  }
352  else if (VARLENA_ATT_IS_PACKABLE(att) &&
354  {
355  /* convert to short varlena -- no alignment */
356  data_length = VARATT_CONVERTED_SHORT_SIZE(val);
357  SET_VARSIZE_SHORT(data, data_length);
358  memcpy(data + 1, VARDATA(val), data_length - 1);
359  }
360  else
361  {
362  /* full 4-byte header varlena */
363  data = (char *) att_align_nominal(data,
364  att->attalign);
365  data_length = VARSIZE(val);
366  memcpy(data, val, data_length);
367  }
368  }
369  else if (att->attlen == -2)
370  {
371  /* cstring ... never needs alignment */
372  *infomask |= HEAP_HASVARWIDTH;
373  Assert(att->attalign == TYPALIGN_CHAR);
374  data_length = strlen(DatumGetCString(datum)) + 1;
375  memcpy(data, DatumGetPointer(datum), data_length);
376  }
377  else
378  {
379  /* fixed-length pass-by-reference */
380  data = (char *) att_align_nominal(data, att->attalign);
381  Assert(att->attlen > 0);
382  data_length = att->attlen;
383  memcpy(data, DatumGetPointer(datum), data_length);
384  }
385 
386  data += data_length;
387  *dataP = data;
388 }
char * Pointer
Definition: c.h:476
#define HIGHBIT
Definition: c.h:1108
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
void EOH_flatten_into(ExpandedObjectHeader *eohptr, void *result, Size allocated_size)
Definition: expandeddatum.c:81
Size EOH_get_flat_size(ExpandedObjectHeader *eohptr)
Definition: expandeddatum.c:75
#define VARLENA_ATT_IS_PACKABLE(att)
Definition: heaptuple.c:83
#define HEAP_HASVARWIDTH
Definition: htup_details.h:191
#define HEAP_HASNULL
Definition: htup_details.h:190
#define HEAP_HASEXTERNAL
Definition: htup_details.h:192
long val
Definition: informix.c:689
const void * data
static char * DatumGetCString(Datum X)
Definition: postgres.h:335
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:129
static void store_att_byval(void *T, Datum newdatum, int attlen)
Definition: tupmacs.h:183
#define VARSIZE_SHORT(PTR)
Definition: varatt.h:281
#define SET_VARSIZE_SHORT(PTR, len)
Definition: varatt.h:306
#define VARATT_CAN_MAKE_SHORT(PTR)
Definition: varatt.h:258
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: varatt.h:298
#define VARATT_IS_SHORT(PTR)
Definition: varatt.h:302
#define VARDATA(PTR)
Definition: varatt.h:278
#define VARSIZE_EXTERNAL(PTR)
Definition: varatt.h:285
#define VARSIZE(PTR)
Definition: varatt.h:279
#define VARATT_CONVERTED_SHORT_SIZE(PTR)
Definition: varatt.h:261
#define VARATT_IS_EXTERNAL(PTR)
Definition: varatt.h:289
Datum bit(PG_FUNCTION_ARGS)
Definition: varbit.c:391

References Assert, att_align_nominal, bit(), data, DatumGetCString(), DatumGetEOHP(), DatumGetPointer(), EOH_flatten_into(), EOH_get_flat_size(), HEAP_HASEXTERNAL, HEAP_HASNULL, HEAP_HASVARWIDTH, HIGHBIT, SET_VARSIZE_SHORT, store_att_byval(), val, VARATT_CAN_MAKE_SHORT, VARATT_CONVERTED_SHORT_SIZE, VARATT_IS_EXTERNAL, VARATT_IS_EXTERNAL_EXPANDED, VARATT_IS_SHORT, VARDATA, VARLENA_ATT_IS_PACKABLE, VARSIZE, VARSIZE_EXTERNAL, and VARSIZE_SHORT.

Referenced by expand_tuple(), and heap_fill_tuple().

◆ getmissingattr()

Datum getmissingattr ( TupleDesc  tupleDesc,
int  attnum,
bool *  isnull 
)

Definition at line 147 of file heaptuple.c.

149 {
150  Form_pg_attribute att;
151 
152  Assert(attnum <= tupleDesc->natts);
153  Assert(attnum > 0);
154 
155  att = TupleDescAttr(tupleDesc, attnum - 1);
156 
157  if (att->atthasmissing)
158  {
159  AttrMissing *attrmiss;
160 
161  Assert(tupleDesc->constr);
162  Assert(tupleDesc->constr->missing);
163 
164  attrmiss = tupleDesc->constr->missing + (attnum - 1);
165 
166  if (attrmiss->am_present)
167  {
169  missing_cache_key *entry;
170  bool found;
171  MemoryContext oldctx;
172 
173  *isnull = false;
174 
175  /* no need to cache by-value attributes */
176  if (att->attbyval)
177  return attrmiss->am_value;
178 
179  /* set up cache if required */
180  if (missing_cache == NULL)
182 
183  /* check if there's a cache entry */
184  Assert(att->attlen > 0 || att->attlen == -1);
185  if (att->attlen > 0)
186  key.len = att->attlen;
187  else
188  key.len = VARSIZE_ANY(attrmiss->am_value);
189  key.value = attrmiss->am_value;
190 
191  entry = hash_search(missing_cache, &key, HASH_ENTER, &found);
192 
193  if (!found)
194  {
195  /* cache miss, so we need a non-transient copy of the datum */
197  entry->value =
198  datumCopy(attrmiss->am_value, false, att->attlen);
199  MemoryContextSwitchTo(oldctx);
200  }
201 
202  return entry->value;
203  }
204  }
205 
206  *isnull = true;
207  return PointerGetDatum(NULL);
208 }
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
static void init_missing_cache()
Definition: heaptuple.c:122
static HTAB * missing_cache
Definition: heaptuple.c:97
@ HASH_ENTER
Definition: hsearch.h:114
MemoryContext TopMemoryContext
Definition: mcxt.c:149
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
MemoryContextSwitchTo(old_ctx)
#define VARSIZE_ANY(PTR)
Definition: varatt.h:311

References AttrMissing::am_present, AttrMissing::am_value, Assert, attnum, TupleDescData::constr, datumCopy(), HASH_ENTER, hash_search(), init_missing_cache(), sort-test::key, MemoryContextSwitchTo(), TupleConstr::missing, missing_cache, PointerGetDatum(), TopMemoryContext, TupleDescAttr, missing_cache_key::value, and VARSIZE_ANY.

Referenced by heap_getattr().

◆ heap_attisnull()

bool heap_attisnull ( HeapTuple  tup,
int  attnum,
TupleDesc  tupleDesc 
)

Definition at line 455 of file heaptuple.c.

456 {
457  /*
458  * We allow a NULL tupledesc for relations not expected to have missing
459  * values, such as catalog relations and indexes.
460  */
461  Assert(!tupleDesc || attnum <= tupleDesc->natts);
462  if (attnum > (int) HeapTupleHeaderGetNatts(tup->t_data))
463  {
464  if (tupleDesc && TupleDescAttr(tupleDesc, attnum - 1)->atthasmissing)
465  return false;
466  else
467  return true;
468  }
469 
470  if (attnum > 0)
471  {
472  if (HeapTupleNoNulls(tup))
473  return false;
474  return att_isnull(attnum - 1, tup->t_data->t_bits);
475  }
476 
477  switch (attnum)
478  {
485  /* these are never null */
486  break;
487 
488  default:
489  elog(ERROR, "invalid attnum: %d", attnum);
490  }
491 
492  return false;
493 }
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define HeapTupleNoNulls(tuple)
Definition: htup_details.h:662
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Definition: htup_details.h:178
#define MinTransactionIdAttributeNumber
Definition: sysattr.h:22
#define MaxCommandIdAttributeNumber
Definition: sysattr.h:25
#define MaxTransactionIdAttributeNumber
Definition: sysattr.h:24
#define TableOidAttributeNumber
Definition: sysattr.h:26
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
#define MinCommandIdAttributeNumber
Definition: sysattr.h:23
static bool att_isnull(int ATT, const bits8 *BITS)
Definition: tupmacs.h:26

References Assert, att_isnull(), attnum, elog, ERROR, HeapTupleHeaderGetNatts, HeapTupleNoNulls, MaxCommandIdAttributeNumber, MaxTransactionIdAttributeNumber, MinCommandIdAttributeNumber, MinTransactionIdAttributeNumber, SelfItemPointerAttributeNumber, HeapTupleHeaderData::t_bits, HeapTupleData::t_data, TableOidAttributeNumber, and TupleDescAttr.

Referenced by AlterPublicationOptions(), AlterPublicationSchemas(), build_function_result_tupdesc_t(), check_index_is_clusterable(), CheckIndexCompatible(), ExecEvalRowNullInt(), ExecuteCallStmt(), fmgr_info_cxt_security(), fmgr_symbol(), get_func_result_name(), index_drop(), inline_function(), inline_set_returning_function(), IsIndexUsableForReplicaIdentityFull(), pg_get_indexdef_worker(), pg_get_partkeydef_worker(), pg_get_statisticsobj_worker(), pg_get_statisticsobjdef_expressions(), RelationGetDummyIndexExpressions(), RelationGetIndexExpressions(), RelationGetIndexList(), RelationGetIndexPredicate(), statext_is_kind_built(), and transformFkeyCheckAttrs().

◆ heap_compute_data_size()

Size heap_compute_data_size ( TupleDesc  tupleDesc,
const Datum values,
const bool *  isnull 
)

Definition at line 215 of file heaptuple.c.

218 {
219  Size data_length = 0;
220  int i;
221  int numberOfAttributes = tupleDesc->natts;
222 
223  for (i = 0; i < numberOfAttributes; i++)
224  {
225  Datum val;
226  Form_pg_attribute atti;
227 
228  if (isnull[i])
229  continue;
230 
231  val = values[i];
232  atti = TupleDescAttr(tupleDesc, i);
233 
234  if (ATT_IS_PACKABLE(atti) &&
236  {
237  /*
238  * we're anticipating converting to a short varlena header, so
239  * adjust length and don't count any alignment
240  */
242  }
243  else if (atti->attlen == -1 &&
245  {
246  /*
247  * we want to flatten the expanded value so that the constructed
248  * tuple doesn't depend on it
249  */
250  data_length = att_align_nominal(data_length, atti->attalign);
251  data_length += EOH_get_flat_size(DatumGetEOHP(val));
252  }
253  else
254  {
255  data_length = att_align_datum(data_length, atti->attalign,
256  atti->attlen, val);
257  data_length = att_addlength_datum(data_length, atti->attlen,
258  val);
259  }
260  }
261 
262  return data_length;
263 }
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define ATT_IS_PACKABLE(att)
Definition: heaptuple.c:80
int i
Definition: isn.c:72
#define att_addlength_datum(cur_offset, attlen, attdatum)
Definition: tupmacs.h:145

References att_addlength_datum, att_align_datum, att_align_nominal, ATT_IS_PACKABLE, DatumGetEOHP(), DatumGetPointer(), EOH_get_flat_size(), i, TupleDescData::natts, TupleDescAttr, val, values, VARATT_CAN_MAKE_SHORT, VARATT_CONVERTED_SHORT_SIZE, and VARATT_IS_EXTERNAL_EXPANDED.

Referenced by brin_form_tuple(), ER_get_flat_size(), heap_form_minimal_tuple(), heap_form_tuple(), heap_toast_insert_or_update(), index_form_tuple_context(), spgFormLeafTuple(), SpGistGetLeafTupleSize(), and toast_flatten_tuple_to_datum().

◆ heap_copy_minimal_tuple()

MinimalTuple heap_copy_minimal_tuple ( MinimalTuple  mtup)

Definition at line 1535 of file heaptuple.c.

1536 {
1537  MinimalTuple result;
1538 
1539  result = (MinimalTuple) palloc(mtup->t_len);
1540  memcpy(result, mtup, mtup->t_len);
1541  return result;
1542 }
void * palloc(Size size)
Definition: mcxt.c:1317

References palloc(), and MinimalTupleData::t_len.

Referenced by gm_readnext_tuple(), tts_minimal_copy_minimal_tuple(), tts_minimal_materialize(), tuplesort_gettupleslot(), and tuplestore_gettupleslot().

◆ heap_copy_tuple_as_datum()

Datum heap_copy_tuple_as_datum ( HeapTuple  tuple,
TupleDesc  tupleDesc 
)

Definition at line 1080 of file heaptuple.c.

1081 {
1082  HeapTupleHeader td;
1083 
1084  /*
1085  * If the tuple contains any external TOAST pointers, we have to inline
1086  * those fields to meet the conventions for composite-type Datums.
1087  */
1088  if (HeapTupleHasExternal(tuple))
1089  return toast_flatten_tuple_to_datum(tuple->t_data,
1090  tuple->t_len,
1091  tupleDesc);
1092 
1093  /*
1094  * Fast path for easy case: just make a palloc'd copy and insert the
1095  * correct composite-Datum header fields (since those may not be set if
1096  * the given tuple came from disk, rather than from heap_form_tuple).
1097  */
1098  td = (HeapTupleHeader) palloc(tuple->t_len);
1099  memcpy((char *) td, (char *) tuple->t_data, tuple->t_len);
1100 
1102  HeapTupleHeaderSetTypeId(td, tupleDesc->tdtypeid);
1103  HeapTupleHeaderSetTypMod(td, tupleDesc->tdtypmod);
1104 
1105  return PointerGetDatum(td);
1106 }
Datum toast_flatten_tuple_to_datum(HeapTupleHeader tup, uint32 tup_len, TupleDesc tupleDesc)
Definition: heaptoast.c:449
#define HeapTupleHasExternal(tuple)
Definition: htup_details.h:671

References HeapTupleHasExternal, HeapTupleHeaderSetDatumLength, HeapTupleHeaderSetTypeId, HeapTupleHeaderSetTypMod, palloc(), PointerGetDatum(), HeapTupleData::t_data, HeapTupleData::t_len, TupleDescData::tdtypeid, TupleDescData::tdtypmod, and toast_flatten_tuple_to_datum().

Referenced by ExecEvalConvertRowtype(), ExecFetchSlotHeapTupleDatum(), PLyGenericObject_ToComposite(), PLyMapping_ToComposite(), PLySequence_ToComposite(), serialize_expr_stats(), and SPI_returntuple().

◆ heap_copytuple()

HeapTuple heap_copytuple ( HeapTuple  tuple)

Definition at line 776 of file heaptuple.c.

777 {
778  HeapTuple newTuple;
779 
780  if (!HeapTupleIsValid(tuple) || tuple->t_data == NULL)
781  return NULL;
782 
783  newTuple = (HeapTuple) palloc(HEAPTUPLESIZE + tuple->t_len);
784  newTuple->t_len = tuple->t_len;
785  newTuple->t_self = tuple->t_self;
786  newTuple->t_tableOid = tuple->t_tableOid;
787  newTuple->t_data = (HeapTupleHeader) ((char *) newTuple + HEAPTUPLESIZE);
788  memcpy((char *) newTuple->t_data, (char *) tuple->t_data, tuple->t_len);
789  return newTuple;
790 }
#define HeapTupleIsValid(tuple)
Definition: htup.h:78

References HeapTupleIsValid, HEAPTUPLESIZE, palloc(), HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

Referenced by AlterConstraintNamespaces(), AlterDomainValidateConstraint(), AlterExtensionNamespace(), AlterSequence(), AlterTypeOwner(), ATExecAlterConstrRecurse(), ATExecValidateConstraint(), CatalogCacheCreateEntry(), changeDependenciesOf(), changeDependenciesOn(), changeDependencyFor(), ConstraintSetParentConstraint(), CopyStatistics(), CreateTriggerFiringOn(), DefineIndex(), dropconstraint_internal(), EnableDisableTrigger(), ExecForceStoreHeapTuple(), expanded_record_set_tuple(), findDomainNotNullConstraint(), findNotNullConstraintAttnum(), get_catalog_object_by_oid(), GetDatabaseTuple(), GetDatabaseTupleByOid(), index_concurrently_swap(), make_expanded_record_from_datum(), MarkInheritDetached(), MergeConstraintsIntoExisting(), MergeWithExistingConstraint(), RelationInitIndexAccessInfo(), RemoveInheritance(), rename_policy(), RenameEnumLabel(), RenameTableSpace(), renametrig_internal(), RenumberEnumType(), ResetSequence(), rewrite_heap_tuple(), ScanPgRelation(), SearchSysCacheCopy(), SearchSysCacheCopyAttName(), SearchSysCacheCopyAttNum(), SearchSysCacheLockedCopy1(), shdepChangeDep(), SPI_copytuple(), statext_expressions_load(), systable_inplace_update_begin(), TriggerSetParentTrigger(), tts_buffer_heap_copy_heap_tuple(), tts_buffer_heap_materialize(), tts_heap_copy_heap_tuple(), tts_heap_materialize(), and tuplesort_putheaptuple().

◆ heap_copytuple_with_tuple()

void heap_copytuple_with_tuple ( HeapTuple  src,
HeapTuple  dest 
)

Definition at line 802 of file heaptuple.c.

803 {
804  if (!HeapTupleIsValid(src) || src->t_data == NULL)
805  {
806  dest->t_data = NULL;
807  return;
808  }
809 
810  dest->t_len = src->t_len;
811  dest->t_self = src->t_self;
812  dest->t_tableOid = src->t_tableOid;
813  dest->t_data = (HeapTupleHeader) palloc(src->t_len);
814  memcpy((char *) dest->t_data, (char *) src->t_data, src->t_len);
815 }

References generate_unaccent_rules::dest, HeapTupleIsValid, palloc(), HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

◆ heap_deform_tuple()

void heap_deform_tuple ( HeapTuple  tuple,
TupleDesc  tupleDesc,
Datum values,
bool *  isnull 
)

Definition at line 1345 of file heaptuple.c.

1347 {
1348  HeapTupleHeader tup = tuple->t_data;
1349  bool hasnulls = HeapTupleHasNulls(tuple);
1350  int tdesc_natts = tupleDesc->natts;
1351  int natts; /* number of atts to extract */
1352  int attnum;
1353  char *tp; /* ptr to tuple data */
1354  uint32 off; /* offset in tuple data */
1355  bits8 *bp = tup->t_bits; /* ptr to null bitmap in tuple */
1356  bool slow = false; /* can we use/set attcacheoff? */
1357 
1358  natts = HeapTupleHeaderGetNatts(tup);
1359 
1360  /*
1361  * In inheritance situations, it is possible that the given tuple actually
1362  * has more fields than the caller is expecting. Don't run off the end of
1363  * the caller's arrays.
1364  */
1365  natts = Min(natts, tdesc_natts);
1366 
1367  tp = (char *) tup + tup->t_hoff;
1368 
1369  off = 0;
1370 
1371  for (attnum = 0; attnum < natts; attnum++)
1372  {
1373  Form_pg_attribute thisatt = TupleDescAttr(tupleDesc, attnum);
1374 
1375  if (hasnulls && att_isnull(attnum, bp))
1376  {
1377  values[attnum] = (Datum) 0;
1378  isnull[attnum] = true;
1379  slow = true; /* can't use attcacheoff anymore */
1380  continue;
1381  }
1382 
1383  isnull[attnum] = false;
1384 
1385  if (!slow && thisatt->attcacheoff >= 0)
1386  off = thisatt->attcacheoff;
1387  else if (thisatt->attlen == -1)
1388  {
1389  /*
1390  * We can only cache the offset for a varlena attribute if the
1391  * offset is already suitably aligned, so that there would be no
1392  * pad bytes in any case: then the offset will be valid for either
1393  * an aligned or unaligned value.
1394  */
1395  if (!slow &&
1396  off == att_align_nominal(off, thisatt->attalign))
1397  thisatt->attcacheoff = off;
1398  else
1399  {
1400  off = att_align_pointer(off, thisatt->attalign, -1,
1401  tp + off);
1402  slow = true;
1403  }
1404  }
1405  else
1406  {
1407  /* not varlena, so safe to use att_align_nominal */
1408  off = att_align_nominal(off, thisatt->attalign);
1409 
1410  if (!slow)
1411  thisatt->attcacheoff = off;
1412  }
1413 
1414  values[attnum] = fetchatt(thisatt, tp + off);
1415 
1416  off = att_addlength_pointer(off, thisatt->attlen, tp + off);
1417 
1418  if (thisatt->attlen <= 0)
1419  slow = true; /* can't use attcacheoff anymore */
1420  }
1421 
1422  /*
1423  * If tuple doesn't have all the atts indicated by tupleDesc, read the
1424  * rest as nulls or missing values as appropriate.
1425  */
1426  for (; attnum < tdesc_natts; attnum++)
1427  values[attnum] = getmissingattr(tupleDesc, attnum + 1, &isnull[attnum]);
1428 }
#define Min(x, y)
Definition: c.h:958
uint32_t uint32
Definition: c.h:485
for(;;)
Datum getmissingattr(TupleDesc tupleDesc, int attnum, bool *isnull)
Definition: heaptuple.c:147
#define att_align_pointer(cur_offset, attalign, attlen, attptr)
Definition: tupmacs.h:107
#define fetchatt(A, T)
Definition: tupmacs.h:46

References att_addlength_pointer, att_align_nominal, att_align_pointer, att_isnull(), attnum, fetchatt, for(), HeapTupleHasNulls, HeapTupleHeaderGetNatts, Min, TupleDescData::natts, HeapTupleHeaderData::t_bits, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, TupleDescAttr, and values.

Referenced by attribute_statistics_update(), deconstruct_expanded_record(), exec_move_row(), ExecEvalFieldStoreDeForm(), ExecForceStoreHeapTuple(), ExecForceStoreMinimalTuple(), ExecStoreHeapTupleDatum(), execute_attr_map_tuple(), ExtractReplicaIdentity(), hash_record(), hash_record_extended(), heap_modify_tuple(), heap_modify_tuple_by_cols(), heap_toast_delete(), heap_toast_insert_or_update(), hstore_from_record(), hstore_populate_record(), make_tuple_indirect(), populate_record(), record_cmp(), record_eq(), record_image_cmp(), record_image_eq(), record_out(), record_send(), reform_and_rewrite_tuple(), ReorderBufferToastReplace(), RI_Initial_Check(), RI_PartitionRemove_Check(), SPI_modifytuple(), toast_flatten_tuple(), and toast_flatten_tuple_to_datum().

◆ heap_expand_tuple()

HeapTuple heap_expand_tuple ( HeapTuple  sourceTuple,
TupleDesc  tupleDesc 
)

Definition at line 1065 of file heaptuple.c.

1066 {
1067  HeapTuple heapTuple;
1068 
1069  expand_tuple(&heapTuple, NULL, sourceTuple, tupleDesc);
1070  return heapTuple;
1071 }
static void expand_tuple(HeapTuple *targetHeapTuple, MinimalTuple *targetMinimalTuple, HeapTuple sourceTuple, TupleDesc tupleDesc)
Definition: heaptuple.c:828

References expand_tuple().

◆ heap_fill_tuple()

void heap_fill_tuple ( TupleDesc  tupleDesc,
const Datum values,
const bool *  isnull,
char *  data,
Size  data_size,
uint16 infomask,
bits8 bit 
)

Definition at line 400 of file heaptuple.c.

404 {
405  bits8 *bitP;
406  int bitmask;
407  int i;
408  int numberOfAttributes = tupleDesc->natts;
409 
410 #ifdef USE_ASSERT_CHECKING
411  char *start = data;
412 #endif
413 
414  if (bit != NULL)
415  {
416  bitP = &bit[-1];
417  bitmask = HIGHBIT;
418  }
419  else
420  {
421  /* just to keep compiler quiet */
422  bitP = NULL;
423  bitmask = 0;
424  }
425 
426  *infomask &= ~(HEAP_HASNULL | HEAP_HASVARWIDTH | HEAP_HASEXTERNAL);
427 
428  for (i = 0; i < numberOfAttributes; i++)
429  {
430  Form_pg_attribute attr = TupleDescAttr(tupleDesc, i);
431 
432  fill_val(attr,
433  bitP ? &bitP : NULL,
434  &bitmask,
435  &data,
436  infomask,
437  values ? values[i] : PointerGetDatum(NULL),
438  isnull ? isnull[i] : true);
439  }
440 
441  Assert((data - start) == data_size);
442 }
return str start

References Assert, bit(), data, fill_val(), HEAP_HASEXTERNAL, HEAP_HASNULL, HEAP_HASVARWIDTH, HIGHBIT, i, TupleDescData::natts, PointerGetDatum(), start, TupleDescAttr, and values.

Referenced by brin_form_tuple(), ER_flatten_into(), heap_form_minimal_tuple(), heap_form_tuple(), heap_toast_insert_or_update(), index_form_tuple_context(), spgFormLeafTuple(), and toast_flatten_tuple_to_datum().

◆ heap_form_minimal_tuple()

MinimalTuple heap_form_minimal_tuple ( TupleDesc  tupleDescriptor,
const Datum values,
const bool *  isnull 
)

Definition at line 1452 of file heaptuple.c.

1455 {
1456  MinimalTuple tuple; /* return tuple */
1457  Size len,
1458  data_len;
1459  int hoff;
1460  bool hasnull = false;
1461  int numberOfAttributes = tupleDescriptor->natts;
1462  int i;
1463 
1464  if (numberOfAttributes > MaxTupleAttributeNumber)
1465  ereport(ERROR,
1466  (errcode(ERRCODE_TOO_MANY_COLUMNS),
1467  errmsg("number of columns (%d) exceeds limit (%d)",
1468  numberOfAttributes, MaxTupleAttributeNumber)));
1469 
1470  /*
1471  * Check for nulls
1472  */
1473  for (i = 0; i < numberOfAttributes; i++)
1474  {
1475  if (isnull[i])
1476  {
1477  hasnull = true;
1478  break;
1479  }
1480  }
1481 
1482  /*
1483  * Determine total space needed
1484  */
1486 
1487  if (hasnull)
1488  len += BITMAPLEN(numberOfAttributes);
1489 
1490  hoff = len = MAXALIGN(len); /* align user data safely */
1491 
1492  data_len = heap_compute_data_size(tupleDescriptor, values, isnull);
1493 
1494  len += data_len;
1495 
1496  /*
1497  * Allocate and zero the space needed.
1498  */
1499  tuple = (MinimalTuple) palloc0(len);
1500 
1501  /*
1502  * And fill in the information.
1503  */
1504  tuple->t_len = len;
1505  HeapTupleHeaderSetNatts(tuple, numberOfAttributes);
1506  tuple->t_hoff = hoff + MINIMAL_TUPLE_OFFSET;
1507 
1508  heap_fill_tuple(tupleDescriptor,
1509  values,
1510  isnull,
1511  (char *) tuple + hoff,
1512  data_len,
1513  &tuple->t_infomask,
1514  (hasnull ? tuple->t_bits : NULL));
1515 
1516  return tuple;
1517 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
Size heap_compute_data_size(TupleDesc tupleDesc, const Datum *values, const bool *isnull)
Definition: heaptuple.c:215
void heap_fill_tuple(TupleDesc tupleDesc, const Datum *values, const bool *isnull, char *data, Size data_size, uint16 *infomask, bits8 *bit)
Definition: heaptuple.c:400
#define MaxTupleAttributeNumber
Definition: htup_details.h:34
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Definition: htup_details.h:640

References BITMAPLEN, ereport, errcode(), errmsg(), ERROR, heap_compute_data_size(), heap_fill_tuple(), HeapTupleHeaderSetNatts, i, len, MAXALIGN, MaxTupleAttributeNumber, MINIMAL_TUPLE_OFFSET, TupleDescData::natts, palloc0(), SizeofMinimalTupleHeader, MinimalTupleData::t_bits, MinimalTupleData::t_hoff, MinimalTupleData::t_infomask, MinimalTupleData::t_len, and values.

Referenced by tts_minimal_materialize(), tts_virtual_copy_minimal_tuple(), and tuplestore_putvalues().

◆ heap_form_tuple()

HeapTuple heap_form_tuple ( TupleDesc  tupleDescriptor,
const Datum values,
const bool *  isnull 
)

Definition at line 1116 of file heaptuple.c.

1119 {
1120  HeapTuple tuple; /* return tuple */
1121  HeapTupleHeader td; /* tuple data */
1122  Size len,
1123  data_len;
1124  int hoff;
1125  bool hasnull = false;
1126  int numberOfAttributes = tupleDescriptor->natts;
1127  int i;
1128 
1129  if (numberOfAttributes > MaxTupleAttributeNumber)
1130  ereport(ERROR,
1131  (errcode(ERRCODE_TOO_MANY_COLUMNS),
1132  errmsg("number of columns (%d) exceeds limit (%d)",
1133  numberOfAttributes, MaxTupleAttributeNumber)));
1134 
1135  /*
1136  * Check for nulls
1137  */
1138  for (i = 0; i < numberOfAttributes; i++)
1139  {
1140  if (isnull[i])
1141  {
1142  hasnull = true;
1143  break;
1144  }
1145  }
1146 
1147  /*
1148  * Determine total space needed
1149  */
1150  len = offsetof(HeapTupleHeaderData, t_bits);
1151 
1152  if (hasnull)
1153  len += BITMAPLEN(numberOfAttributes);
1154 
1155  hoff = len = MAXALIGN(len); /* align user data safely */
1156 
1157  data_len = heap_compute_data_size(tupleDescriptor, values, isnull);
1158 
1159  len += data_len;
1160 
1161  /*
1162  * Allocate and zero the space needed. Note that the tuple body and
1163  * HeapTupleData management structure are allocated in one chunk.
1164  */
1165  tuple = (HeapTuple) palloc0(HEAPTUPLESIZE + len);
1166  tuple->t_data = td = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE);
1167 
1168  /*
1169  * And fill in the information. Note we fill the Datum fields even though
1170  * this tuple may never become a Datum. This lets HeapTupleHeaderGetDatum
1171  * identify the tuple type if needed.
1172  */
1173  tuple->t_len = len;
1174  ItemPointerSetInvalid(&(tuple->t_self));
1175  tuple->t_tableOid = InvalidOid;
1176 
1178  HeapTupleHeaderSetTypeId(td, tupleDescriptor->tdtypeid);
1179  HeapTupleHeaderSetTypMod(td, tupleDescriptor->tdtypmod);
1180  /* We also make sure that t_ctid is invalid unless explicitly set */
1181  ItemPointerSetInvalid(&(td->t_ctid));
1182 
1183  HeapTupleHeaderSetNatts(td, numberOfAttributes);
1184  td->t_hoff = hoff;
1185 
1186  heap_fill_tuple(tupleDescriptor,
1187  values,
1188  isnull,
1189  (char *) td + hoff,
1190  data_len,
1191  &td->t_infomask,
1192  (hasnull ? td->t_bits : NULL));
1193 
1194  return tuple;
1195 }
#define InvalidOid
Definition: postgres_ext.h:36

References BITMAPLEN, ereport, errcode(), errmsg(), ERROR, heap_compute_data_size(), heap_fill_tuple(), HeapTupleHeaderSetDatumLength, HeapTupleHeaderSetNatts, HeapTupleHeaderSetTypeId, HeapTupleHeaderSetTypMod, HEAPTUPLESIZE, i, InvalidOid, ItemPointerSetInvalid(), len, MAXALIGN, MaxTupleAttributeNumber, TupleDescData::natts, palloc0(), HeapTupleHeaderData::t_bits, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, HeapTupleHeaderData::t_infomask, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TupleDescData::tdtypeid, TupleDescData::tdtypmod, and values.

Referenced by aclexplode(), AddEnumLabel(), AddRoleMems(), AddSubscriptionRelState(), AggregateCreate(), AlterSetting(), brin_metapage_info(), bt_page_print_tuples(), BuildTupleFromCStrings(), CastCreate(), CollationCreate(), ConversionCreate(), copy_replication_slot(), CreateAccessMethod(), CreateComments(), CreateConstraintEntry(), createdb(), CreateForeignDataWrapper(), CreateForeignServer(), CreateForeignTable(), CreateOpFamily(), CreatePolicy(), CreateProceduralLanguage(), CreatePublication(), CreateRole(), CreateSharedComments(), CreateStatistics(), CreateSubscription(), CreateTableSpace(), CreateTransform(), CreateTriggerFiringOn(), CreateUserMapping(), DefineOpClass(), DefineSequence(), DefineTSConfiguration(), DefineTSDictionary(), DefineTSParser(), DefineTSTemplate(), each_object_field_end(), elements_array_element_end(), ExecEvalFieldStoreForm(), ExecEvalRow(), execute_attr_map_tuple(), expanded_record_get_tuple(), ExtractReplicaIdentity(), file_acquire_sample_rows(), fill_hba_line(), fill_ident_line(), gin_leafpage_items(), gin_metapage_info(), gin_page_opaque_info(), gist_page_opaque_info(), gistFetchTuple(), hash_bitmap_info(), hash_metapage_info(), hash_page_items(), hash_page_stats(), heap_modify_tuple(), heap_modify_tuple_by_cols(), heap_page_items(), heap_tuple_infomask_flags(), hstore_each(), hstore_populate_record(), injection_points_stats_fixed(), insert_event_trigger_tuple(), InsertExtensionTuple(), InsertOneTuple(), InsertPgClassTuple(), InsertRule(), inv_truncate(), inv_write(), LargeObjectCreate(), make_tuple_from_result_row(), make_tuple_from_row(), make_tuple_indirect(), NamespaceCreate(), OperatorCreate(), OperatorShellMake(), page_header(), ParameterAclCreate(), pg_available_wal_summaries(), pg_backup_stop(), pg_buffercache_pages(), pg_buffercache_summary(), pg_control_checkpoint(), pg_control_init(), pg_control_recovery(), pg_control_system(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), pg_get_catalog_foreign_keys(), pg_get_logical_snapshot_info(), pg_get_logical_snapshot_meta(), pg_get_object_address(), pg_get_publication_tables(), pg_get_sequence_data(), pg_get_wal_record_info(), pg_get_wal_summarizer_state(), pg_identify_object(), pg_identify_object_as_address(), pg_input_error_info(), pg_last_committed_xact(), pg_lock_status(), pg_partition_tree(), pg_prepared_xact(), pg_replication_slot_advance(), pg_sequence_parameters(), pg_split_walfile_name(), pg_stat_file(), pg_stat_get_archiver(), pg_stat_get_backend_subxact(), pg_stat_get_replication_slot(), pg_stat_get_subscription_stats(), pg_stat_get_wal(), pg_stat_get_wal_receiver(), pg_stat_statements_info(), pg_stats_ext_mcvlist_items(), pg_timezone_abbrevs(), pg_visibility(), pg_visibility_map(), pg_visibility_map_rel(), pg_visibility_map_summary(), pg_visibility_rel(), pg_wal_summary_contents(), pg_walfile_name_offset(), pg_xact_commit_timestamp_origin(), pgstatginindex_internal(), pgstathashindex(), pgstattuple_approx_internal(), plperl_build_tuple_result(), PLyGenericObject_ToComposite(), PLyMapping_ToComposite(), PLySequence_ToComposite(), populate_record(), ProcedureCreate(), publication_add_relation(), publication_add_schema(), RangeCreate(), record_in(), record_recv(), recordExtensionInitPrivWorker(), reform_and_rewrite_tuple(), ReorderBufferToastReplace(), replorigin_create(), report_corruption_internal(), serialize_expr_stats(), SetDefaultACL(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepAddDependency(), shdepChangeDep(), SPI_modifytuple(), ssl_extension_info(), statext_store(), StoreAttrDefault(), storeGettuple(), storeOperators(), StorePartitionKey(), storeProcedures(), StoreSingleInheritance(), test_enc_conversion(), test_predtest(), toast_build_flattened_tuple(), toast_flatten_tuple(), toast_save_datum(), tsvector_unnest(), tts_buffer_heap_materialize(), tts_heap_materialize(), tts_virtual_copy_heap_tuple(), TypeCreate(), TypeShellMake(), update_attstats(), UpdateIndexRelation(), and upsert_pg_statistic().

◆ heap_free_minimal_tuple()

void heap_free_minimal_tuple ( MinimalTuple  mtup)

◆ heap_freetuple()

void heap_freetuple ( HeapTuple  htup)

Definition at line 1434 of file heaptuple.c.

1435 {
1436  pfree(htup);
1437 }

References pfree().

Referenced by acquire_inherited_sample_rows(), acquire_sample_rows(), AddEnumLabel(), AddSubscriptionRelState(), AfterTriggerExecute(), AlterCollation(), AlterDatabaseOwner(), AlterDatabaseRefreshColl(), AlterDomainDefault(), AlterDomainNotNull(), AlterDomainValidateConstraint(), AlterEventTrigger(), AlterEventTriggerOwner(), AlterEventTriggerOwner_oid(), AlterForeignDataWrapper(), AlterForeignDataWrapperOwner(), AlterForeignDataWrapperOwner_oid(), AlterForeignServer(), AlterForeignServerOwner(), AlterForeignServerOwner_oid(), AlterFunction(), AlterObjectRename_internal(), AlterPolicy(), AlterPublication(), AlterPublicationOwner(), AlterPublicationOwner_oid(), AlterRelationNamespaceInternal(), AlterRole(), AlterSchemaOwner_internal(), AlterStatistics(), AlterSubscription(), AlterSubscriptionOwner(), AlterSubscriptionOwner_oid(), AlterTableSpaceOptions(), AlterTSDictionary(), AlterTypeNamespaceInternal(), AlterUserMapping(), analyze_row_processor(), ATExecAddColumn(), ATExecAddIdentity(), ATExecAddOf(), ATExecAlterColumnGenericOptions(), ATExecAlterColumnType(), ATExecAlterConstrRecurse(), ATExecChangeOwner(), ATExecDropColumn(), ATExecDropExpression(), ATExecDropIdentity(), ATExecDropNotNull(), ATExecDropOf(), ATExecForceNoForceRowSecurity(), ATExecGenericOptions(), ATExecSetAccessMethodNoStorage(), ATExecSetCompression(), ATExecSetIdentity(), ATExecSetOptions(), ATExecSetRelOptions(), ATExecSetRowSecurity(), ATExecSetStatistics(), ATExecSetStorage(), ATExecValidateConstraint(), build_tuplestore_recursively(), CastCreate(), CatalogCacheCreateEntry(), CatalogTuplesMultiInsertWithInfo(), change_owner_fix_column_acls(), changeDependenciesOf(), changeDependenciesOn(), changeDependencyFor(), clear_subscription_skip_lsn(), CollationCreate(), ConversionCreate(), copy_table_data(), CopyStatistics(), create_toast_table(), CreateAccessMethod(), CreateComments(), CreateForeignDataWrapper(), CreateForeignServer(), CreateForeignTable(), CreateOpFamily(), CreatePolicy(), CreatePublication(), CreateSharedComments(), CreateStatistics(), CreateSubscription(), CreateTableSpace(), CreateTransform(), CreateTriggerFiringOn(), CreateUserMapping(), crosstab(), DefineIndex(), DefineOpClass(), DefineSequence(), DefineTSConfiguration(), DefineTSDictionary(), DefineTSParser(), DefineTSTemplate(), DetachPartitionFinalize(), DisableSubscription(), dropconstraint_internal(), dropdb(), EnableDisableRule(), EnableDisableTrigger(), EventTriggerOnLogin(), examine_attribute(), examine_expression(), ExecBRDeleteTriggers(), ExecBRInsertTriggers(), ExecBRUpdateTriggers(), ExecIRDeleteTriggers(), ExecIRInsertTriggers(), ExecIRUpdateTriggers(), ExecReScanAgg(), ExecReScanIndexScan(), ExecReScanSetOp(), ExecScanSubPlan(), ExecSetParamPlan(), expanded_record_set_tuple(), ExtractReplicaIdentity(), file_acquire_sample_rows(), heap_delete(), heap_insert(), heap_update(), index_build(), index_concurrently_swap(), index_constraint_create(), index_update_stats(), insert_event_trigger_tuple(), InsertExtensionTuple(), InsertOneTuple(), InsertPgClassTuple(), InsertRule(), inv_truncate(), inv_write(), LargeObjectCreate(), mark_index_clustered(), MarkInheritDetached(), MergeAttributesIntoExisting(), MergeConstraintsIntoExisting(), OperatorShellMake(), ParameterAclCreate(), PLyGenericObject_ToComposite(), PLyMapping_ToComposite(), PLySequence_ToComposite(), ProcedureCreate(), publication_add_relation(), publication_add_schema(), RangeCreate(), raw_heap_insert(), record_in(), record_recv(), reform_and_rewrite_tuple(), relation_mark_replica_identity(), relation_statistics_update(), RelationBuildDesc(), RelationClearMissing(), RelationInitPhysicalAddr(), RelationReloadIndexInfo(), RelationReloadNailed(), RelationSetNewRelfilenumber(), RemoveConstraintById(), RemoveInheritance(), RemoveRoleFromObjectPolicy(), renameatt_internal(), RenameConstraintById(), RenameEnumLabel(), RenameRelationInternal(), RenameRewriteRule(), RenameSchema(), RenameTypeInternal(), RenumberEnumType(), replorigin_create(), ResetRelRewrite(), rewrite_heap_dead_tuple(), rewrite_heap_tuple(), set_attnotnull(), SetDatabaseHasLoginEventTriggers(), SetIndexStorageProperties(), SetMatViewPopulatedState(), SetRelationHasSubclass(), SetRelationNumChecks(), SetRelationRuleStatus(), SetRelationTableSpace(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepAddDependency(), shdepChangeDep(), SPI_freetuple(), statext_store(), StoreAttrDefault(), storeOperators(), StorePartitionBound(), storeProcedures(), StoreSingleInheritance(), swap_relation_files(), table_recheck_autovac(), toast_save_datum(), TriggerSetParentTrigger(), tts_buffer_heap_clear(), tts_buffer_heap_store_tuple(), tts_heap_clear(), TypeShellMake(), update_attstats(), update_default_partition_oid(), update_relispartition(), UpdateIndexRelation(), UpdateTwoPhaseState(), upsert_pg_statistic(), vac_update_datfrozenxid(), validatePartitionedIndex(), and xpath_table().

◆ heap_getsysattr()

Datum heap_getsysattr ( HeapTuple  tup,
int  attnum,
TupleDesc  tupleDesc,
bool *  isnull 
)

Definition at line 723 of file heaptuple.c.

724 {
725  Datum result;
726 
727  Assert(tup);
728 
729  /* Currently, no sys attribute ever reads as NULL. */
730  *isnull = false;
731 
732  switch (attnum)
733  {
735  /* pass-by-reference datatype */
736  result = PointerGetDatum(&(tup->t_self));
737  break;
740  break;
743  break;
746 
747  /*
748  * cmin and cmax are now both aliases for the same field, which
749  * can in fact also be a combo command id. XXX perhaps we should
750  * return the "real" cmin or cmax if possible, that is if we are
751  * inside the originating transaction?
752  */
754  break;
756  result = ObjectIdGetDatum(tup->t_tableOid);
757  break;
758  default:
759  elog(ERROR, "invalid attnum: %d", attnum);
760  result = 0; /* keep compiler quiet */
761  break;
762  }
763  return result;
764 }
#define HeapTupleHeaderGetRawXmin(tup)
Definition: htup_details.h:304
#define HeapTupleHeaderGetRawXmax(tup)
Definition: htup_details.h:371
#define HeapTupleHeaderGetRawCommandId(tup)
Definition: htup_details.h:387
static Datum TransactionIdGetDatum(TransactionId X)
Definition: postgres.h:272
static Datum CommandIdGetDatum(CommandId X)
Definition: postgres.h:302
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252

References Assert, attnum, CommandIdGetDatum(), elog, ERROR, HeapTupleHeaderGetRawCommandId, HeapTupleHeaderGetRawXmax, HeapTupleHeaderGetRawXmin, MaxCommandIdAttributeNumber, MaxTransactionIdAttributeNumber, MinCommandIdAttributeNumber, MinTransactionIdAttributeNumber, ObjectIdGetDatum(), PointerGetDatum(), SelfItemPointerAttributeNumber, HeapTupleData::t_data, HeapTupleData::t_self, HeapTupleData::t_tableOid, TableOidAttributeNumber, and TransactionIdGetDatum().

Referenced by expanded_record_fetch_field(), heap_getattr(), tts_buffer_heap_getsysattr(), and tts_heap_getsysattr().

◆ heap_modify_tuple()

HeapTuple heap_modify_tuple ( HeapTuple  tuple,
TupleDesc  tupleDesc,
const Datum replValues,
const bool *  replIsnull,
const bool *  doReplace 
)

Definition at line 1209 of file heaptuple.c.

1214 {
1215  int numberOfAttributes = tupleDesc->natts;
1216  int attoff;
1217  Datum *values;
1218  bool *isnull;
1219  HeapTuple newTuple;
1220 
1221  /*
1222  * allocate and fill values and isnull arrays from either the tuple or the
1223  * repl information, as appropriate.
1224  *
1225  * NOTE: it's debatable whether to use heap_deform_tuple() here or just
1226  * heap_getattr() only the non-replaced columns. The latter could win if
1227  * there are many replaced columns and few non-replaced ones. However,
1228  * heap_deform_tuple costs only O(N) while the heap_getattr way would cost
1229  * O(N^2) if there are many non-replaced columns, so it seems better to
1230  * err on the side of linear cost.
1231  */
1232  values = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
1233  isnull = (bool *) palloc(numberOfAttributes * sizeof(bool));
1234 
1235  heap_deform_tuple(tuple, tupleDesc, values, isnull);
1236 
1237  for (attoff = 0; attoff < numberOfAttributes; attoff++)
1238  {
1239  if (doReplace[attoff])
1240  {
1241  values[attoff] = replValues[attoff];
1242  isnull[attoff] = replIsnull[attoff];
1243  }
1244  }
1245 
1246  /*
1247  * create a new tuple from the values and isnull arrays
1248  */
1249  newTuple = heap_form_tuple(tupleDesc, values, isnull);
1250 
1251  pfree(values);
1252  pfree(isnull);
1253 
1254  /*
1255  * copy the identification info of the old tuple: t_ctid, t_self
1256  */
1257  newTuple->t_data->t_ctid = tuple->t_data->t_ctid;
1258  newTuple->t_self = tuple->t_self;
1259  newTuple->t_tableOid = tuple->t_tableOid;
1260 
1261  return newTuple;
1262 }
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1345

References heap_deform_tuple(), heap_form_tuple(), TupleDescData::natts, palloc(), pfree(), HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleData::t_self, HeapTupleData::t_tableOid, and values.

Referenced by AddRoleMems(), AggregateCreate(), AlterCollation(), AlterDatabase(), AlterDatabaseOwner(), AlterDatabaseRefreshColl(), AlterDomainDefault(), AlterForeignDataWrapper(), AlterForeignDataWrapperOwner_internal(), AlterForeignServer(), AlterForeignServerOwner_internal(), AlterFunction(), AlterObjectNamespace_internal(), AlterObjectOwner_internal(), AlterObjectRename_internal(), AlterOperator(), AlterPolicy(), AlterPublicationOptions(), AlterRole(), AlterSchemaOwner_internal(), AlterSetting(), AlterStatistics(), AlterSubscription(), AlterTableSpaceOptions(), AlterTSDictionary(), AlterTypeOwnerInternal(), AlterTypeRecurse(), AlterUserMapping(), ApplyExtensionUpdates(), ATExecAlterColumnGenericOptions(), ATExecAlterColumnType(), ATExecChangeOwner(), ATExecGenericOptions(), ATExecSetOptions(), ATExecSetRelOptions(), ATExecSetStatistics(), change_owner_fix_column_acls(), clear_subscription_skip_lsn(), CreateComments(), CreateProceduralLanguage(), CreateSharedComments(), CreateTransform(), DelRoleMems(), DetachPartitionFinalize(), DisableSubscription(), ExecGrant_Attribute(), ExecGrant_common(), ExecGrant_Largeobject(), ExecGrant_Parameter(), ExecGrant_Relation(), extension_config_remove(), index_concurrently_swap(), InsertRule(), inv_truncate(), inv_write(), MakeConfigurationMapping(), movedb(), OperatorCreate(), pg_extension_config_dump(), plperl_modify_tuple(), PLy_modify_tuple(), ProcedureCreate(), recordExtensionInitPrivWorker(), RelationClearMissing(), RemoveAttributeById(), RemoveRoleFromInitPriv(), RemoveRoleFromObjectPolicy(), RenameRole(), ReplaceRoleInInitPriv(), SetAttrMissing(), SetDefaultACL(), SetSecurityLabel(), SetSharedSecurityLabel(), StoreAttrDefault(), StorePartitionBound(), TypeCreate(), update_attstats(), UpdateSubscriptionRelState(), UpdateTwoPhaseState(), and upsert_pg_statistic().

◆ heap_modify_tuple_by_cols()

HeapTuple heap_modify_tuple_by_cols ( HeapTuple  tuple,
TupleDesc  tupleDesc,
int  nCols,
const int *  replCols,
const Datum replValues,
const bool *  replIsnull 
)

Definition at line 1277 of file heaptuple.c.

1283 {
1284  int numberOfAttributes = tupleDesc->natts;
1285  Datum *values;
1286  bool *isnull;
1287  HeapTuple newTuple;
1288  int i;
1289 
1290  /*
1291  * allocate and fill values and isnull arrays from the tuple, then replace
1292  * selected columns from the input arrays.
1293  */
1294  values = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
1295  isnull = (bool *) palloc(numberOfAttributes * sizeof(bool));
1296 
1297  heap_deform_tuple(tuple, tupleDesc, values, isnull);
1298 
1299  for (i = 0; i < nCols; i++)
1300  {
1301  int attnum = replCols[i];
1302 
1303  if (attnum <= 0 || attnum > numberOfAttributes)
1304  elog(ERROR, "invalid column number %d", attnum);
1305  values[attnum - 1] = replValues[i];
1306  isnull[attnum - 1] = replIsnull[i];
1307  }
1308 
1309  /*
1310  * create a new tuple from the values and isnull arrays
1311  */
1312  newTuple = heap_form_tuple(tupleDesc, values, isnull);
1313 
1314  pfree(values);
1315  pfree(isnull);
1316 
1317  /*
1318  * copy the identification info of the old tuple: t_ctid, t_self
1319  */
1320  newTuple->t_data->t_ctid = tuple->t_data->t_ctid;
1321  newTuple->t_self = tuple->t_self;
1322  newTuple->t_tableOid = tuple->t_tableOid;
1323 
1324  return newTuple;
1325 }

References attnum, elog, ERROR, heap_deform_tuple(), heap_form_tuple(), i, TupleDescData::natts, palloc(), pfree(), HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleData::t_self, HeapTupleData::t_tableOid, and values.

Referenced by autoinc(), insert_username(), moddatetime(), relation_statistics_update(), and tsvector_update_trigger().

◆ heap_tuple_from_minimal_tuple()

HeapTuple heap_tuple_from_minimal_tuple ( MinimalTuple  mtup)

Definition at line 1554 of file heaptuple.c.

1555 {
1556  HeapTuple result;
1558 
1559  result = (HeapTuple) palloc(HEAPTUPLESIZE + len);
1560  result->t_len = len;
1561  ItemPointerSetInvalid(&(result->t_self));
1562  result->t_tableOid = InvalidOid;
1563  result->t_data = (HeapTupleHeader) ((char *) result + HEAPTUPLESIZE);
1564  memcpy((char *) result->t_data + MINIMAL_TUPLE_OFFSET, mtup, mtup->t_len);
1565  memset(result->t_data, 0, offsetof(HeapTupleHeaderData, t_infomask2));
1566  return result;
1567 }

References HEAPTUPLESIZE, InvalidOid, ItemPointerSetInvalid(), len, MINIMAL_TUPLE_OFFSET, palloc(), HeapTupleData::t_data, HeapTupleData::t_len, MinimalTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

Referenced by tts_minimal_copy_heap_tuple().

◆ init_missing_cache()

static void init_missing_cache ( )
static

Definition at line 122 of file heaptuple.c.

123 {
124  HASHCTL hash_ctl;
125 
126  hash_ctl.keysize = sizeof(missing_cache_key);
127  hash_ctl.entrysize = sizeof(missing_cache_key);
128  hash_ctl.hcxt = TopMemoryContext;
129  hash_ctl.hash = missing_hash;
130  hash_ctl.match = missing_match;
131  missing_cache =
132  hash_create("Missing Values Cache",
133  32,
134  &hash_ctl,
136 }
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
static uint32 missing_hash(const void *key, Size keysize)
Definition: heaptuple.c:100
static int missing_match(const void *key1, const void *key2, Size keysize)
Definition: heaptuple.c:108
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_COMPARE
Definition: hsearch.h:99
#define HASH_FUNCTION
Definition: hsearch.h:98
Size keysize
Definition: hsearch.h:75
HashValueFunc hash
Definition: hsearch.h:78
Size entrysize
Definition: hsearch.h:76
HashCompareFunc match
Definition: hsearch.h:80
MemoryContext hcxt
Definition: hsearch.h:86

References HASHCTL::entrysize, HASHCTL::hash, HASH_COMPARE, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_FUNCTION, HASHCTL::hcxt, HASHCTL::keysize, HASHCTL::match, missing_cache, missing_hash(), missing_match(), and TopMemoryContext.

Referenced by getmissingattr().

◆ minimal_expand_tuple()

MinimalTuple minimal_expand_tuple ( HeapTuple  sourceTuple,
TupleDesc  tupleDesc 
)

Definition at line 1053 of file heaptuple.c.

1054 {
1055  MinimalTuple minimalTuple;
1056 
1057  expand_tuple(NULL, &minimalTuple, sourceTuple, tupleDesc);
1058  return minimalTuple;
1059 }

References expand_tuple().

◆ minimal_tuple_from_heap_tuple()

MinimalTuple minimal_tuple_from_heap_tuple ( HeapTuple  htup)

Definition at line 1576 of file heaptuple.c.

1577 {
1578  MinimalTuple result;
1579  uint32 len;
1580 
1582  len = htup->t_len - MINIMAL_TUPLE_OFFSET;
1583  result = (MinimalTuple) palloc(len);
1584  memcpy(result, (char *) htup->t_data + MINIMAL_TUPLE_OFFSET, len);
1585  result->t_len = len;
1586  return result;
1587 }

References Assert, len, MINIMAL_TUPLE_OFFSET, palloc(), HeapTupleData::t_data, HeapTupleData::t_len, and MinimalTupleData::t_len.

Referenced by copytup_heap(), tts_buffer_heap_copy_minimal_tuple(), and tts_heap_copy_minimal_tuple().

◆ missing_hash()

static uint32 missing_hash ( const void *  key,
Size  keysize 
)
static

Definition at line 100 of file heaptuple.c.

101 {
102  const missing_cache_key *entry = (missing_cache_key *) key;
103 
104  return hash_bytes((const unsigned char *) entry->value, entry->len);
105 }
uint32 hash_bytes(const unsigned char *k, int keylen)
Definition: hashfn.c:146

References hash_bytes(), sort-test::key, missing_cache_key::len, and missing_cache_key::value.

Referenced by init_missing_cache().

◆ missing_match()

static int missing_match ( const void *  key1,
const void *  key2,
Size  keysize 
)
static

Definition at line 108 of file heaptuple.c.

109 {
110  const missing_cache_key *entry1 = (missing_cache_key *) key1;
111  const missing_cache_key *entry2 = (missing_cache_key *) key2;
112 
113  if (entry1->len != entry2->len)
114  return entry1->len > entry2->len ? 1 : -1;
115 
116  return memcmp(DatumGetPointer(entry1->value),
117  DatumGetPointer(entry2->value),
118  entry1->len);
119 }

References DatumGetPointer(), missing_cache_key::len, and missing_cache_key::value.

Referenced by init_missing_cache().

◆ nocachegetattr()

Datum nocachegetattr ( HeapTuple  tup,
int  attnum,
TupleDesc  tupleDesc 
)

Definition at line 519 of file heaptuple.c.

522 {
523  HeapTupleHeader td = tup->t_data;
524  char *tp; /* ptr to data part of tuple */
525  bits8 *bp = td->t_bits; /* ptr to null bitmap in tuple */
526  bool slow = false; /* do we have to walk attrs? */
527  int off; /* current offset within data */
528 
529  /* ----------------
530  * Three cases:
531  *
532  * 1: No nulls and no variable-width attributes.
533  * 2: Has a null or a var-width AFTER att.
534  * 3: Has nulls or var-widths BEFORE att.
535  * ----------------
536  */
537 
538  attnum--;
539 
540  if (!HeapTupleNoNulls(tup))
541  {
542  /*
543  * there's a null somewhere in the tuple
544  *
545  * check to see if any preceding bits are null...
546  */
547  int byte = attnum >> 3;
548  int finalbit = attnum & 0x07;
549 
550  /* check for nulls "before" final bit of last byte */
551  if ((~bp[byte]) & ((1 << finalbit) - 1))
552  slow = true;
553  else
554  {
555  /* check for nulls in any "earlier" bytes */
556  int i;
557 
558  for (i = 0; i < byte; i++)
559  {
560  if (bp[i] != 0xFF)
561  {
562  slow = true;
563  break;
564  }
565  }
566  }
567  }
568 
569  tp = (char *) td + td->t_hoff;
570 
571  if (!slow)
572  {
573  Form_pg_attribute att;
574 
575  /*
576  * If we get here, there are no nulls up to and including the target
577  * attribute. If we have a cached offset, we can use it.
578  */
579  att = TupleDescAttr(tupleDesc, attnum);
580  if (att->attcacheoff >= 0)
581  return fetchatt(att, tp + att->attcacheoff);
582 
583  /*
584  * Otherwise, check for non-fixed-length attrs up to and including
585  * target. If there aren't any, it's safe to cheaply initialize the
586  * cached offsets for these attrs.
587  */
588  if (HeapTupleHasVarWidth(tup))
589  {
590  int j;
591 
592  for (j = 0; j <= attnum; j++)
593  {
594  if (TupleDescAttr(tupleDesc, j)->attlen <= 0)
595  {
596  slow = true;
597  break;
598  }
599  }
600  }
601  }
602 
603  if (!slow)
604  {
605  int natts = tupleDesc->natts;
606  int j = 1;
607 
608  /*
609  * If we get here, we have a tuple with no nulls or var-widths up to
610  * and including the target attribute, so we can use the cached offset
611  * ... only we don't have it yet, or we'd not have got here. Since
612  * it's cheap to compute offsets for fixed-width columns, we take the
613  * opportunity to initialize the cached offsets for *all* the leading
614  * fixed-width columns, in hope of avoiding future visits to this
615  * routine.
616  */
617  TupleDescAttr(tupleDesc, 0)->attcacheoff = 0;
618 
619  /* we might have set some offsets in the slow path previously */
620  while (j < natts && TupleDescAttr(tupleDesc, j)->attcacheoff > 0)
621  j++;
622 
623  off = TupleDescAttr(tupleDesc, j - 1)->attcacheoff +
624  TupleDescAttr(tupleDesc, j - 1)->attlen;
625 
626  for (; j < natts; j++)
627  {
628  Form_pg_attribute att = TupleDescAttr(tupleDesc, j);
629 
630  if (att->attlen <= 0)
631  break;
632 
633  off = att_align_nominal(off, att->attalign);
634 
635  att->attcacheoff = off;
636 
637  off += att->attlen;
638  }
639 
640  Assert(j > attnum);
641 
642  off = TupleDescAttr(tupleDesc, attnum)->attcacheoff;
643  }
644  else
645  {
646  bool usecache = true;
647  int i;
648 
649  /*
650  * Now we know that we have to walk the tuple CAREFULLY. But we still
651  * might be able to cache some offsets for next time.
652  *
653  * Note - This loop is a little tricky. For each non-null attribute,
654  * we have to first account for alignment padding before the attr,
655  * then advance over the attr based on its length. Nulls have no
656  * storage and no alignment padding either. We can use/set
657  * attcacheoff until we reach either a null or a var-width attribute.
658  */
659  off = 0;
660  for (i = 0;; i++) /* loop exit is at "break" */
661  {
662  Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
663 
664  if (HeapTupleHasNulls(tup) && att_isnull(i, bp))
665  {
666  usecache = false;
667  continue; /* this cannot be the target att */
668  }
669 
670  /* If we know the next offset, we can skip the rest */
671  if (usecache && att->attcacheoff >= 0)
672  off = att->attcacheoff;
673  else if (att->attlen == -1)
674  {
675  /*
676  * We can only cache the offset for a varlena attribute if the
677  * offset is already suitably aligned, so that there would be
678  * no pad bytes in any case: then the offset will be valid for
679  * either an aligned or unaligned value.
680  */
681  if (usecache &&
682  off == att_align_nominal(off, att->attalign))
683  att->attcacheoff = off;
684  else
685  {
686  off = att_align_pointer(off, att->attalign, -1,
687  tp + off);
688  usecache = false;
689  }
690  }
691  else
692  {
693  /* not varlena, so safe to use att_align_nominal */
694  off = att_align_nominal(off, att->attalign);
695 
696  if (usecache)
697  att->attcacheoff = off;
698  }
699 
700  if (i == attnum)
701  break;
702 
703  off = att_addlength_pointer(off, att->attlen, tp + off);
704 
705  if (usecache && att->attlen <= 0)
706  usecache = false;
707  }
708  }
709 
710  return fetchatt(TupleDescAttr(tupleDesc, attnum), tp + off);
711 }
#define HeapTupleHasVarWidth(tuple)
Definition: htup_details.h:665
int j
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
int16 attlen
Definition: pg_attribute.h:59

References Assert, att_addlength_pointer, att_align_nominal, att_align_pointer, att_isnull(), attlen, attnum, fetchatt, HeapTupleHasNulls, HeapTupleHasVarWidth, HeapTupleNoNulls, i, if(), j, TupleDescData::natts, HeapTupleHeaderData::t_bits, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, and TupleDescAttr.

Referenced by fastgetattr().

◆ varsize_any()

size_t varsize_any ( void *  p)

Definition at line 1594 of file heaptuple.c.

1595 {
1596  return VARSIZE_ANY(p);
1597 }

References VARSIZE_ANY.

Variable Documentation

◆ missing_cache

HTAB* missing_cache = NULL
static

Definition at line 97 of file heaptuple.c.

Referenced by getmissingattr(), and init_missing_cache().