PostgreSQL Source Code  git master
heaptuple.c File Reference
#include "postgres.h"
#include "access/sysattr.h"
#include "access/tuptoaster.h"
#include "executor/tuptable.h"
#include "utils/expandeddatum.h"
Include dependency graph for heaptuple.c:

Go to the source code of this file.

Macros

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

Functions

Size heap_compute_data_size (TupleDesc tupleDesc, Datum *values, bool *isnull)
 
void heap_fill_tuple (TupleDesc tupleDesc, Datum *values, bool *isnull, char *data, Size data_size, uint16 *infomask, bits8 *bit)
 
bool heap_attisnull (HeapTuple tup, int attnum)
 
Datum nocachegetattr (HeapTuple tuple, 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)
 
Datum heap_copy_tuple_as_datum (HeapTuple tuple, TupleDesc tupleDesc)
 
HeapTuple heap_form_tuple (TupleDesc tupleDescriptor, Datum *values, bool *isnull)
 
HeapTuple heap_modify_tuple (HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
 
HeapTuple heap_modify_tuple_by_cols (HeapTuple tuple, TupleDesc tupleDesc, int nCols, int *replCols, Datum *replValues, bool *replIsnull)
 
void heap_deform_tuple (HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
 
static void slot_deform_tuple (TupleTableSlot *slot, int natts)
 
Datum slot_getattr (TupleTableSlot *slot, int attnum, bool *isnull)
 
void slot_getallattrs (TupleTableSlot *slot)
 
void slot_getsomeattrs (TupleTableSlot *slot, int attnum)
 
bool slot_attisnull (TupleTableSlot *slot, int attnum)
 
void heap_freetuple (HeapTuple htup)
 
MinimalTuple heap_form_minimal_tuple (TupleDesc tupleDescriptor, Datum *values, 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)
 

Macro Definition Documentation

◆ ATT_IS_PACKABLE

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

Definition at line 67 of file heaptuple.c.

Referenced by heap_compute_data_size().

◆ VARLENA_ATT_IS_PACKABLE

#define VARLENA_ATT_IS_PACKABLE (   att)    ((att)->attstorage != 'p')

Definition at line 70 of file heaptuple.c.

Referenced by heap_fill_tuple().

Function Documentation

◆ heap_attisnull()

bool heap_attisnull ( HeapTuple  tup,
int  attnum 
)

Definition at line 296 of file heaptuple.c.

References att_isnull, elog, ERROR, HeapTupleHeaderGetNatts, HeapTupleNoNulls, MaxCommandIdAttributeNumber, MaxTransactionIdAttributeNumber, MinCommandIdAttributeNumber, MinTransactionIdAttributeNumber, ObjectIdAttributeNumber, SelfItemPointerAttributeNumber, HeapTupleHeaderData::t_bits, HeapTupleData::t_data, and TableOidAttributeNumber.

Referenced by AlterDomainNotNull(), ATRewriteTable(), build_function_result_tupdesc_t(), check_index_is_clusterable(), CheckIndexCompatible(), ExecEvalRowNullInt(), fmgr_info_cxt_security(), get_func_result_name(), index_drop(), inline_function(), inline_set_returning_function(), pg_attribute_aclcheck_all(), pg_get_indexdef_worker(), pg_get_partkeydef_worker(), RelationGetIndexExpressions(), RelationGetIndexList(), RelationGetIndexPredicate(), ri_NullCheck(), slot_attisnull(), statext_is_kind_built(), and transformFkeyCheckAttrs().

297 {
298  if (attnum > (int) HeapTupleHeaderGetNatts(tup->t_data))
299  return true;
300 
301  if (attnum > 0)
302  {
303  if (HeapTupleNoNulls(tup))
304  return false;
305  return att_isnull(attnum - 1, tup->t_data->t_bits);
306  }
307 
308  switch (attnum)
309  {
317  /* these are never null */
318  break;
319 
320  default:
321  elog(ERROR, "invalid attnum: %d", attnum);
322  }
323 
324  return false;
325 }
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Definition: htup_details.h:168
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:21
#define MinCommandIdAttributeNumber
Definition: sysattr.h:24
HeapTupleHeader t_data
Definition: htup.h:67
#define ERROR
Definition: elog.h:43
#define HeapTupleHeaderGetNatts(tup)
Definition: htup_details.h:537
#define TableOidAttributeNumber
Definition: sysattr.h:27
#define HeapTupleNoNulls(tuple)
Definition: htup_details.h:670
#define MaxCommandIdAttributeNumber
Definition: sysattr.h:26
#define MaxTransactionIdAttributeNumber
Definition: sysattr.h:25
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
#define elog
Definition: elog.h:219
#define MinTransactionIdAttributeNumber
Definition: sysattr.h:23

◆ heap_compute_data_size()

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

Definition at line 85 of file heaptuple.c.

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

Referenced by brin_form_tuple(), heap_form_minimal_tuple(), heap_form_tuple(), index_form_tuple(), toast_flatten_tuple_to_datum(), and toast_insert_or_update().

88 {
89  Size data_length = 0;
90  int i;
91  int numberOfAttributes = tupleDesc->natts;
92 
93  for (i = 0; i < numberOfAttributes; i++)
94  {
95  Datum val;
96  Form_pg_attribute atti;
97 
98  if (isnull[i])
99  continue;
100 
101  val = values[i];
102  atti = TupleDescAttr(tupleDesc, i);
103 
104  if (ATT_IS_PACKABLE(atti) &&
106  {
107  /*
108  * we're anticipating converting to a short varlena header, so
109  * adjust length and don't count any alignment
110  */
111  data_length += VARATT_CONVERTED_SHORT_SIZE(DatumGetPointer(val));
112  }
113  else if (atti->attlen == -1 &&
115  {
116  /*
117  * we want to flatten the expanded value so that the constructed
118  * tuple doesn't depend on it
119  */
120  data_length = att_align_nominal(data_length, atti->attalign);
121  data_length += EOH_get_flat_size(DatumGetEOHP(val));
122  }
123  else
124  {
125  data_length = att_align_datum(data_length, atti->attalign,
126  atti->attlen, val);
127  data_length = att_addlength_datum(data_length, atti->attlen,
128  val);
129  }
130  }
131 
132  return data_length;
133 }
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:144
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:323
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
int natts
Definition: tupdesc.h:79
#define att_align_datum(cur_offset, attalign, attlen, attdatum)
Definition: tupmacs.h:101
#define VARATT_CAN_MAKE_SHORT(PTR)
Definition: postgres.h:271
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
Size EOH_get_flat_size(ExpandedObjectHeader *eohptr)
Definition: expandeddatum.c:75
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
#define VARATT_CONVERTED_SHORT_SIZE(PTR)
Definition: postgres.h:274
uintptr_t Datum
Definition: postgres.h:372
size_t Size
Definition: c.h:404
#define DatumGetPointer(X)
Definition: postgres.h:555
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define att_addlength_datum(cur_offset, attlen, attdatum)
Definition: tupmacs.h:160
#define ATT_IS_PACKABLE(att)
Definition: heaptuple.c:67
int i
long val
Definition: informix.c:689

◆ heap_copy_minimal_tuple()

MinimalTuple heap_copy_minimal_tuple ( MinimalTuple  mtup)

Definition at line 1480 of file heaptuple.c.

References palloc(), and MinimalTupleData::t_len.

Referenced by ExecCopySlotMinimalTuple(), tuplesort_gettupleslot(), and tuplestore_gettupleslot().

1481 {
1482  MinimalTuple result;
1483 
1484  result = (MinimalTuple) palloc(mtup->t_len);
1485  memcpy(result, mtup, mtup->t_len);
1486  return result;
1487 }
MinimalTupleData * MinimalTuple
Definition: htup.h:27
void * palloc(Size size)
Definition: mcxt.c:848

◆ heap_copy_tuple_as_datum()

Datum heap_copy_tuple_as_datum ( HeapTuple  tuple,
TupleDesc  tupleDesc 
)

Definition at line 659 of file heaptuple.c.

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

Referenced by exec_eval_datum(), ExecEvalConvertRowtype(), ExecFetchSlotTupleDatum(), PLyGenericObject_ToComposite(), PLyMapping_ToComposite(), PLySequence_ToComposite(), and SPI_returntuple().

660 {
661  HeapTupleHeader td;
662 
663  /*
664  * If the tuple contains any external TOAST pointers, we have to inline
665  * those fields to meet the conventions for composite-type Datums.
666  */
667  if (HeapTupleHasExternal(tuple))
668  return toast_flatten_tuple_to_datum(tuple->t_data,
669  tuple->t_len,
670  tupleDesc);
671 
672  /*
673  * Fast path for easy case: just make a palloc'd copy and insert the
674  * correct composite-Datum header fields (since those may not be set if
675  * the given tuple came from disk, rather than from heap_form_tuple).
676  */
677  td = (HeapTupleHeader) palloc(tuple->t_len);
678  memcpy((char *) td, (char *) tuple->t_data, tuple->t_len);
679 
681  HeapTupleHeaderSetTypeId(td, tupleDesc->tdtypeid);
682  HeapTupleHeaderSetTypMod(td, tupleDesc->tdtypmod);
683 
684  return PointerGetDatum(td);
685 }
Oid tdtypeid
Definition: tupdesc.h:80
#define HeapTupleHeaderSetTypeId(tup, typeid)
Definition: htup_details.h:455
#define PointerGetDatum(X)
Definition: postgres.h:562
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
int32 tdtypmod
Definition: tupdesc.h:81
#define HeapTupleHeaderSetDatumLength(tup, len)
Definition: htup_details.h:447
HeapTupleHeader t_data
Definition: htup.h:67
uint32 t_len
Definition: htup.h:64
Datum toast_flatten_tuple_to_datum(HeapTupleHeader tup, uint32 tup_len, TupleDesc tupleDesc)
Definition: tuptoaster.c:1187
#define HeapTupleHeaderSetTypMod(tup, typmod)
Definition: htup_details.h:465
#define HeapTupleHasExternal(tuple)
Definition: htup_details.h:679
void * palloc(Size size)
Definition: mcxt.c:848

◆ heap_copytuple()

HeapTuple heap_copytuple ( HeapTuple  tuple)

Definition at line 611 of file heaptuple.c.

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

Referenced by acquire_sample_rows(), AlterConstraintNamespaces(), AlterDomainValidateConstraint(), AlterExtensionNamespace(), AlterSequence(), AlterTypeOwner(), ATExecAlterConstraint(), ATExecDropConstraint(), ATExecValidateConstraint(), changeDependencyFor(), copytup_cluster(), EnableDisableTrigger(), EvalPlanQualFetch(), EvalPlanQualFetchRowMarks(), exec_move_row(), ExecCopySlotTuple(), ExecLockRows(), gather_merge_readnext(), get_catalog_object_by_oid(), get_tuple_from_datum(), GetDatabaseTuple(), GetDatabaseTupleByOid(), GetTupleForTrigger(), index_update_stats(), load_tuple_array(), MergeConstraintsIntoExisting(), MergeWithExistingConstraint(), RelationInitIndexAccessInfo(), RemoveInheritance(), rename_policy(), RenameEnumLabel(), RenameTableSpace(), renametrig(), RenumberEnumType(), reorderqueue_push(), ResetSequence(), rewrite_heap_tuple(), ScanPgRelation(), SearchSysCacheCopy(), SearchSysCacheCopyAttName(), SearchSysCacheCopyAttNum(), shdepChangeDep(), SPI_copytuple(), and TupleQueueReaderNext().

612 {
613  HeapTuple newTuple;
614 
615  if (!HeapTupleIsValid(tuple) || tuple->t_data == NULL)
616  return NULL;
617 
618  newTuple = (HeapTuple) palloc(HEAPTUPLESIZE + tuple->t_len);
619  newTuple->t_len = tuple->t_len;
620  newTuple->t_self = tuple->t_self;
621  newTuple->t_tableOid = tuple->t_tableOid;
622  newTuple->t_data = (HeapTupleHeader) ((char *) newTuple + HEAPTUPLESIZE);
623  memcpy((char *) newTuple->t_data, (char *) tuple->t_data, tuple->t_len);
624  return newTuple;
625 }
HeapTupleData * HeapTuple
Definition: htup.h:70
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
HeapTupleHeader t_data
Definition: htup.h:67
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
Oid t_tableOid
Definition: htup.h:66
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void * palloc(Size size)
Definition: mcxt.c:848
#define HEAPTUPLESIZE
Definition: htup.h:72

◆ heap_copytuple_with_tuple()

void heap_copytuple_with_tuple ( HeapTuple  src,
HeapTuple  dest 
)

Definition at line 637 of file heaptuple.c.

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

638 {
639  if (!HeapTupleIsValid(src) || src->t_data == NULL)
640  {
641  dest->t_data = NULL;
642  return;
643  }
644 
645  dest->t_len = src->t_len;
646  dest->t_self = src->t_self;
647  dest->t_tableOid = src->t_tableOid;
648  dest->t_data = (HeapTupleHeader) palloc(src->t_len);
649  memcpy((char *) dest->t_data, (char *) src->t_data, src->t_len);
650 }
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
HeapTupleHeader t_data
Definition: htup.h:67
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
Oid t_tableOid
Definition: htup.h:66
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void * palloc(Size size)
Definition: mcxt.c:848

◆ heap_deform_tuple()

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

Definition at line 936 of file heaptuple.c.

References att_addlength_pointer, att_align_nominal, att_align_pointer, att_isnull, fetchatt, HeapTupleHasNulls, HeapTupleHeaderGetNatts, Min, tupleDesc::natts, HeapTupleHeaderData::t_bits, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, TupleDescAttr, and values.

Referenced by ATRewriteTable(), CopyTo(), do_convert_tuple(), ExecEvalFieldStoreDeForm(), ExtractReplicaIdentity(), heap_modify_tuple(), heap_modify_tuple_by_cols(), hstore_from_record(), hstore_populate_record(), logicalrep_write_tuple(), make_tuple_indirect(), populate_record(), record_cmp(), record_eq(), record_image_cmp(), record_image_eq(), record_out(), record_send(), reform_and_rewrite_tuple(), ReorderBufferToastReplace(), SPI_modifytuple(), toast_delete(), toast_flatten_tuple(), toast_flatten_tuple_to_datum(), toast_insert_or_update(), and tuple_equals_slot().

938 {
939  HeapTupleHeader tup = tuple->t_data;
940  bool hasnulls = HeapTupleHasNulls(tuple);
941  int tdesc_natts = tupleDesc->natts;
942  int natts; /* number of atts to extract */
943  int attnum;
944  char *tp; /* ptr to tuple data */
945  long off; /* offset in tuple data */
946  bits8 *bp = tup->t_bits; /* ptr to null bitmap in tuple */
947  bool slow = false; /* can we use/set attcacheoff? */
948 
949  natts = HeapTupleHeaderGetNatts(tup);
950 
951  /*
952  * In inheritance situations, it is possible that the given tuple actually
953  * has more fields than the caller is expecting. Don't run off the end of
954  * the caller's arrays.
955  */
956  natts = Min(natts, tdesc_natts);
957 
958  tp = (char *) tup + tup->t_hoff;
959 
960  off = 0;
961 
962  for (attnum = 0; attnum < natts; attnum++)
963  {
964  Form_pg_attribute thisatt = TupleDescAttr(tupleDesc, attnum);
965 
966  if (hasnulls && att_isnull(attnum, bp))
967  {
968  values[attnum] = (Datum) 0;
969  isnull[attnum] = true;
970  slow = true; /* can't use attcacheoff anymore */
971  continue;
972  }
973 
974  isnull[attnum] = false;
975 
976  if (!slow && thisatt->attcacheoff >= 0)
977  off = thisatt->attcacheoff;
978  else if (thisatt->attlen == -1)
979  {
980  /*
981  * We can only cache the offset for a varlena attribute if the
982  * offset is already suitably aligned, so that there would be no
983  * pad bytes in any case: then the offset will be valid for either
984  * an aligned or unaligned value.
985  */
986  if (!slow &&
987  off == att_align_nominal(off, thisatt->attalign))
988  thisatt->attcacheoff = off;
989  else
990  {
991  off = att_align_pointer(off, thisatt->attalign, -1,
992  tp + off);
993  slow = true;
994  }
995  }
996  else
997  {
998  /* not varlena, so safe to use att_align_nominal */
999  off = att_align_nominal(off, thisatt->attalign);
1000 
1001  if (!slow)
1002  thisatt->attcacheoff = off;
1003  }
1004 
1005  values[attnum] = fetchatt(thisatt, tp + off);
1006 
1007  off = att_addlength_pointer(off, thisatt->attlen, tp + off);
1008 
1009  if (thisatt->attlen <= 0)
1010  slow = true; /* can't use attcacheoff anymore */
1011  }
1012 
1013  /*
1014  * If tuple doesn't have all the atts indicated by tupleDesc, read the
1015  * rest as null
1016  */
1017  for (; attnum < tdesc_natts; attnum++)
1018  {
1019  values[attnum] = (Datum) 0;
1020  isnull[attnum] = true;
1021  }
1022 }
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:144
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Definition: htup_details.h:168
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:21
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
#define Min(x, y)
Definition: c.h:802
int natts
Definition: tupdesc.h:79
#define fetchatt(A, T)
Definition: tupmacs.h:37
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleHeaderGetNatts(tup)
Definition: htup_details.h:537
#define HeapTupleHasNulls(tuple)
Definition: htup_details.h:667
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:172
uint8 bits8
Definition: c.h:303
uintptr_t Datum
Definition: postgres.h:372
#define att_align_pointer(cur_offset, attalign, attlen, attptr)
Definition: tupmacs.h:122
static Datum values[MAXATTR]
Definition: bootstrap.c:164

◆ heap_fill_tuple()

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

Definition at line 145 of file heaptuple.c.

References Assert, att_align_nominal, DatumGetCString, DatumGetEOHP(), DatumGetPointer, EOH_flatten_into(), EOH_get_flat_size(), HEAP_HASEXTERNAL, HEAP_HASNULL, HEAP_HASVARWIDTH, HIGHBIT, i, tupleDesc::natts, SET_VARSIZE_SHORT, store_att_byval, TupleDescAttr, 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 brin_form_tuple(), heap_form_minimal_tuple(), heap_form_tuple(), index_form_tuple(), toast_flatten_tuple_to_datum(), and toast_insert_or_update().

149 {
150  bits8 *bitP;
151  int bitmask;
152  int i;
153  int numberOfAttributes = tupleDesc->natts;
154 
155 #ifdef USE_ASSERT_CHECKING
156  char *start = data;
157 #endif
158 
159  if (bit != NULL)
160  {
161  bitP = &bit[-1];
162  bitmask = HIGHBIT;
163  }
164  else
165  {
166  /* just to keep compiler quiet */
167  bitP = NULL;
168  bitmask = 0;
169  }
170 
171  *infomask &= ~(HEAP_HASNULL | HEAP_HASVARWIDTH | HEAP_HASEXTERNAL);
172 
173  for (i = 0; i < numberOfAttributes; i++)
174  {
175  Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
176  Size data_length;
177 
178  if (bit != NULL)
179  {
180  if (bitmask != HIGHBIT)
181  bitmask <<= 1;
182  else
183  {
184  bitP += 1;
185  *bitP = 0x0;
186  bitmask = 1;
187  }
188 
189  if (isnull[i])
190  {
191  *infomask |= HEAP_HASNULL;
192  continue;
193  }
194 
195  *bitP |= bitmask;
196  }
197 
198  /*
199  * XXX we use the att_align macros on the pointer value itself, not on
200  * an offset. This is a bit of a hack.
201  */
202 
203  if (att->attbyval)
204  {
205  /* pass-by-value */
206  data = (char *) att_align_nominal(data, att->attalign);
207  store_att_byval(data, values[i], att->attlen);
208  data_length = att->attlen;
209  }
210  else if (att->attlen == -1)
211  {
212  /* varlena */
214 
215  *infomask |= HEAP_HASVARWIDTH;
216  if (VARATT_IS_EXTERNAL(val))
217  {
219  {
220  /*
221  * we want to flatten the expanded value so that the
222  * constructed tuple doesn't depend on it
223  */
225 
226  data = (char *) att_align_nominal(data,
227  att->attalign);
228  data_length = EOH_get_flat_size(eoh);
229  EOH_flatten_into(eoh, data, data_length);
230  }
231  else
232  {
233  *infomask |= HEAP_HASEXTERNAL;
234  /* no alignment, since it's short by definition */
235  data_length = VARSIZE_EXTERNAL(val);
236  memcpy(data, val, data_length);
237  }
238  }
239  else if (VARATT_IS_SHORT(val))
240  {
241  /* no alignment for short varlenas */
242  data_length = VARSIZE_SHORT(val);
243  memcpy(data, val, data_length);
244  }
245  else if (VARLENA_ATT_IS_PACKABLE(att) &&
247  {
248  /* convert to short varlena -- no alignment */
249  data_length = VARATT_CONVERTED_SHORT_SIZE(val);
250  SET_VARSIZE_SHORT(data, data_length);
251  memcpy(data + 1, VARDATA(val), data_length - 1);
252  }
253  else
254  {
255  /* full 4-byte header varlena */
256  data = (char *) att_align_nominal(data,
257  att->attalign);
258  data_length = VARSIZE(val);
259  memcpy(data, val, data_length);
260  }
261  }
262  else if (att->attlen == -2)
263  {
264  /* cstring ... never needs alignment */
265  *infomask |= HEAP_HASVARWIDTH;
266  Assert(att->attalign == 'c');
267  data_length = strlen(DatumGetCString(values[i])) + 1;
268  memcpy(data, DatumGetPointer(values[i]), data_length);
269  }
270  else
271  {
272  /* fixed-length pass-by-reference */
273  data = (char *) att_align_nominal(data, att->attalign);
274  Assert(att->attlen > 0);
275  data_length = att->attlen;
276  memcpy(data, DatumGetPointer(values[i]), data_length);
277  }
278 
279  data += data_length;
280  }
281 
282  Assert((data - start) == data_size);
283 }
#define SET_VARSIZE_SHORT(PTR, len)
Definition: postgres.h:329
#define VARDATA(PTR)
Definition: postgres.h:303
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:144
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:323
#define VARSIZE(PTR)
Definition: postgres.h:304
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
int natts
Definition: tupdesc.h:79
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:314
char * Pointer
Definition: c.h:273
#define HEAP_HASNULL
Definition: htup_details.h:180
#define DatumGetCString(X)
Definition: postgres.h:572
#define VARATT_IS_SHORT(PTR)
Definition: postgres.h:325
#define VARATT_CAN_MAKE_SHORT(PTR)
Definition: postgres.h:271
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
Size EOH_get_flat_size(ExpandedObjectHeader *eohptr)
Definition: expandeddatum.c:75
#define HEAP_HASVARWIDTH
Definition: htup_details.h:181
#define HIGHBIT
Definition: c.h:938
#define VARSIZE_SHORT(PTR)
Definition: postgres.h:306
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
#define VARATT_CONVERTED_SHORT_SIZE(PTR)
Definition: postgres.h:274
#define store_att_byval(T, newdatum, attlen)
Definition: tupmacs.h:222
uint8 bits8
Definition: c.h:303
#define VARLENA_ATT_IS_PACKABLE(att)
Definition: heaptuple.c:70
void EOH_flatten_into(ExpandedObjectHeader *eohptr, void *result, Size allocated_size)
Definition: expandeddatum.c:81
Datum bit(PG_FUNCTION_ARGS)
Definition: varbit.c:361
#define Assert(condition)
Definition: c.h:670
size_t Size
Definition: c.h:404
#define DatumGetPointer(X)
Definition: postgres.h:555
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int i
#define HEAP_HASEXTERNAL
Definition: htup_details.h:182
long val
Definition: informix.c:689
#define VARSIZE_EXTERNAL(PTR)
Definition: postgres.h:310

◆ heap_form_minimal_tuple()

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

Definition at line 1391 of file heaptuple.c.

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

Referenced by ExecCopySlotMinimalTuple(), and tuplestore_putvalues().

1394 {
1395  MinimalTuple tuple; /* return tuple */
1396  Size len,
1397  data_len;
1398  int hoff;
1399  bool hasnull = false;
1400  int numberOfAttributes = tupleDescriptor->natts;
1401  int i;
1402 
1403  if (numberOfAttributes > MaxTupleAttributeNumber)
1404  ereport(ERROR,
1405  (errcode(ERRCODE_TOO_MANY_COLUMNS),
1406  errmsg("number of columns (%d) exceeds limit (%d)",
1407  numberOfAttributes, MaxTupleAttributeNumber)));
1408 
1409  /*
1410  * Check for nulls
1411  */
1412  for (i = 0; i < numberOfAttributes; i++)
1413  {
1414  if (isnull[i])
1415  {
1416  hasnull = true;
1417  break;
1418  }
1419  }
1420 
1421  /*
1422  * Determine total space needed
1423  */
1425 
1426  if (hasnull)
1427  len += BITMAPLEN(numberOfAttributes);
1428 
1429  if (tupleDescriptor->tdhasoid)
1430  len += sizeof(Oid);
1431 
1432  hoff = len = MAXALIGN(len); /* align user data safely */
1433 
1434  data_len = heap_compute_data_size(tupleDescriptor, values, isnull);
1435 
1436  len += data_len;
1437 
1438  /*
1439  * Allocate and zero the space needed.
1440  */
1441  tuple = (MinimalTuple) palloc0(len);
1442 
1443  /*
1444  * And fill in the information.
1445  */
1446  tuple->t_len = len;
1447  HeapTupleHeaderSetNatts(tuple, numberOfAttributes);
1448  tuple->t_hoff = hoff + MINIMAL_TUPLE_OFFSET;
1449 
1450  if (tupleDescriptor->tdhasoid) /* else leave infomask = 0 */
1451  tuple->t_infomask = HEAP_HASOID;
1452 
1453  heap_fill_tuple(tupleDescriptor,
1454  values,
1455  isnull,
1456  (char *) tuple + hoff,
1457  data_len,
1458  &tuple->t_infomask,
1459  (hasnull ? tuple->t_bits : NULL));
1460 
1461  return tuple;
1462 }
void heap_fill_tuple(TupleDesc tupleDesc, Datum *values, bool *isnull, char *data, Size data_size, uint16 *infomask, bits8 *bit)
Definition: heaptuple.c:145
bool tdhasoid
Definition: tupdesc.h:82
#define MaxTupleAttributeNumber
Definition: htup_details.h:33
int errcode(int sqlerrcode)
Definition: elog.c:575
#define BITMAPLEN(NATTS)
Definition: htup_details.h:553
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:79
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Definition: htup_details.h:648
#define ERROR
Definition: elog.h:43
MinimalTupleData * MinimalTuple
Definition: htup.h:27
#define ereport(elevel, rest)
Definition: elog.h:122
#define SizeofMinimalTupleHeader
Definition: htup_details.h:655
void * palloc0(Size size)
Definition: mcxt.c:877
#define HeapTupleHeaderSetNatts(tup, natts)
Definition: htup_details.h:540
size_t Size
Definition: c.h:404
#define MAXALIGN(LEN)
Definition: c.h:623
#define MINIMAL_TUPLE_OFFSET
Definition: htup_details.h:625
Size heap_compute_data_size(TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:85
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define HEAP_HASOID
Definition: htup_details.h:183

◆ heap_form_tuple()

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

Definition at line 695 of file heaptuple.c.

References BITMAPLEN, ereport, errcode(), errmsg(), ERROR, heap_compute_data_size(), heap_fill_tuple(), HEAP_HASOID, HeapTupleHeaderSetDatumLength, HeapTupleHeaderSetNatts, HeapTupleHeaderSetTypeId, HeapTupleHeaderSetTypMod, HEAPTUPLESIZE, i, InvalidOid, ItemPointerSetInvalid, MAXALIGN, MaxTupleAttributeNumber, tupleDesc::natts, offsetof, 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, tupleDesc::tdhasoid, tupleDesc::tdtypeid, and tupleDesc::tdtypmod.

Referenced by aclexplode(), AddEnumLabel(), AddRoleMems(), AggregateCreate(), AlterSetting(), ATRewriteTable(), brin_metapage_info(), BuildTupleFromCStrings(), CollationCreate(), ConversionCreate(), CopyFrom(), create_proc_lang(), CreateAccessMethod(), CreateCast(), CreateComments(), CreateConstraintEntry(), createdb(), CreateForeignDataWrapper(), CreateForeignServer(), CreateForeignTable(), CreateOpFamily(), CreatePolicy(), CreatePublication(), CreateRole(), CreateSharedComments(), CreateStatistics(), CreateSubscription(), CreateTableSpace(), CreateTransform(), CreateTrigger(), CreateUserMapping(), DefineOpClass(), DefineSequence(), DefineTSConfiguration(), DefineTSDictionary(), DefineTSParser(), DefineTSTemplate(), do_convert_tuple(), each_object_field_end(), each_worker_jsonb(), elements_array_element_end(), elements_worker_jsonb(), EnumValuesCreate(), exec_move_row(), ExecCopySlotTuple(), ExecEvalFieldStoreForm(), ExecEvalRow(), ExtractReplicaIdentity(), file_acquire_sample_rows(), fill_hba_line(), gin_leafpage_items(), gin_metapage_info(), gin_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(), hstore_each(), hstore_populate_record(), insert_event_trigger_tuple(), InsertExtensionTuple(), InsertOneTuple(), InsertPgAttributeTuple(), InsertPgClassTuple(), InsertRule(), inv_truncate(), inv_write(), LargeObjectCreate(), make_tuple_from_result_row(), make_tuple_from_row(), make_tuple_indirect(), MakeConfigurationMapping(), NamespaceCreate(), OperatorCreate(), OperatorShellMake(), page_header(), pg_buffercache_pages(), pg_control_checkpoint(), pg_control_init(), pg_control_recovery(), pg_control_system(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), pg_get_object_address(), pg_identify_object(), pg_identify_object_as_address(), pg_last_committed_xact(), pg_lock_status(), pg_ls_dir_files(), pg_prepared_xact(), pg_sequence_parameters(), pg_stat_file(), pg_stat_get_archiver(), pg_stat_get_wal_receiver(), pg_timezone_abbrevs(), pg_timezone_names(), pg_visibility(), pg_visibility_map(), pg_visibility_map_rel(), pg_visibility_map_summary(), pg_visibility_rel(), pg_walfile_name_offset(), pgstatginindex_internal(), pgstathashindex(), pgstattuple_approx_internal(), plperl_build_tuple_result(), PLyGenericObject_ToComposite(), PLyMapping_ToComposite(), PLySequence_ToComposite(), populate_record(), ProcedureCreate(), publication_add_relation(), RangeCreate(), record_in(), record_recv(), recordExtensionInitPrivWorker(), recordMultipleDependencies(), reform_and_rewrite_tuple(), ReorderBufferToastReplace(), replorigin_create(), SetDefaultACL(), SetSecurityLabel(), SetSharedSecurityLabel(), SetSubscriptionRelState(), shdepAddDependency(), shdepChangeDep(), SPI_modifytuple(), ssl_extension_info(), StoreAttrDefault(), StoreCatalogInheritance1(), storeGettuple(), storeOperators(), StorePartitionKey(), storeProcedures(), toast_build_flattened_tuple(), toast_flatten_tuple(), toast_save_datum(), tsvector_unnest(), TypeCreate(), TypeShellMake(), update_attstats(), and UpdateIndexRelation().

698 {
699  HeapTuple tuple; /* return tuple */
700  HeapTupleHeader td; /* tuple data */
701  Size len,
702  data_len;
703  int hoff;
704  bool hasnull = false;
705  int numberOfAttributes = tupleDescriptor->natts;
706  int i;
707 
708  if (numberOfAttributes > MaxTupleAttributeNumber)
709  ereport(ERROR,
710  (errcode(ERRCODE_TOO_MANY_COLUMNS),
711  errmsg("number of columns (%d) exceeds limit (%d)",
712  numberOfAttributes, MaxTupleAttributeNumber)));
713 
714  /*
715  * Check for nulls
716  */
717  for (i = 0; i < numberOfAttributes; i++)
718  {
719  if (isnull[i])
720  {
721  hasnull = true;
722  break;
723  }
724  }
725 
726  /*
727  * Determine total space needed
728  */
729  len = offsetof(HeapTupleHeaderData, t_bits);
730 
731  if (hasnull)
732  len += BITMAPLEN(numberOfAttributes);
733 
734  if (tupleDescriptor->tdhasoid)
735  len += sizeof(Oid);
736 
737  hoff = len = MAXALIGN(len); /* align user data safely */
738 
739  data_len = heap_compute_data_size(tupleDescriptor, values, isnull);
740 
741  len += data_len;
742 
743  /*
744  * Allocate and zero the space needed. Note that the tuple body and
745  * HeapTupleData management structure are allocated in one chunk.
746  */
747  tuple = (HeapTuple) palloc0(HEAPTUPLESIZE + len);
748  tuple->t_data = td = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE);
749 
750  /*
751  * And fill in the information. Note we fill the Datum fields even though
752  * this tuple may never become a Datum. This lets HeapTupleHeaderGetDatum
753  * identify the tuple type if needed.
754  */
755  tuple->t_len = len;
756  ItemPointerSetInvalid(&(tuple->t_self));
757  tuple->t_tableOid = InvalidOid;
758 
760  HeapTupleHeaderSetTypeId(td, tupleDescriptor->tdtypeid);
761  HeapTupleHeaderSetTypMod(td, tupleDescriptor->tdtypmod);
762  /* We also make sure that t_ctid is invalid unless explicitly set */
764 
765  HeapTupleHeaderSetNatts(td, numberOfAttributes);
766  td->t_hoff = hoff;
767 
768  if (tupleDescriptor->tdhasoid) /* else leave infomask = 0 */
769  td->t_infomask = HEAP_HASOID;
770 
771  heap_fill_tuple(tupleDescriptor,
772  values,
773  isnull,
774  (char *) td + hoff,
775  data_len,
776  &td->t_infomask,
777  (hasnull ? td->t_bits : NULL));
778 
779  return tuple;
780 }
void heap_fill_tuple(TupleDesc tupleDesc, Datum *values, bool *isnull, char *data, Size data_size, uint16 *infomask, bits8 *bit)
Definition: heaptuple.c:145
HeapTupleData * HeapTuple
Definition: htup.h:70
Oid tdtypeid
Definition: tupdesc.h:80
#define HeapTupleHeaderSetTypeId(tup, typeid)
Definition: htup_details.h:455
bool tdhasoid
Definition: tupdesc.h:82
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Definition: htup_details.h:168
#define MaxTupleAttributeNumber
Definition: htup_details.h:33
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
int errcode(int sqlerrcode)
Definition: elog.c:575
#define BITMAPLEN(NATTS)
Definition: htup_details.h:553
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:79
int32 tdtypmod
Definition: tupdesc.h:81
#define HeapTupleHeaderSetDatumLength(tup, len)
Definition: htup_details.h:447
HeapTupleHeader t_data
Definition: htup.h:67
#define ERROR
Definition: elog.h:43
ItemPointerData t_ctid
Definition: htup_details.h:155
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
Oid t_tableOid
Definition: htup.h:66
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:877
#define HeapTupleHeaderSetTypMod(tup, typmod)
Definition: htup_details.h:465
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleHeaderSetNatts(tup, natts)
Definition: htup_details.h:540
size_t Size
Definition: c.h:404
#define MAXALIGN(LEN)
Definition: c.h:623
Size heap_compute_data_size(TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:85
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:150
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HEAPTUPLESIZE
Definition: htup.h:72
int i
#define HEAP_HASOID
Definition: htup_details.h:183
#define offsetof(type, field)
Definition: c.h:593

◆ heap_free_minimal_tuple()

void heap_free_minimal_tuple ( MinimalTuple  mtup)

Definition at line 1468 of file heaptuple.c.

References pfree().

Referenced by ExecClearTuple(), ExecStoreMinimalTuple(), ExecStoreTuple(), and writetup_heap().

1469 {
1470  pfree(mtup);
1471 }
void pfree(void *pointer)
Definition: mcxt.c:949

◆ heap_freetuple()

void heap_freetuple ( HeapTuple  htup)

Definition at line 1373 of file heaptuple.c.

References pfree().

Referenced by acquire_inherited_sample_rows(), acquire_sample_rows(), AddEnumLabel(), AfterTriggerExecute(), AlterCollation(), AlterDatabaseOwner(), 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(), AlterSubscription(), AlterSubscriptionOwner(), AlterSubscriptionOwner_oid(), AlterTableSpaceOptions(), AlterTSDictionary(), AlterTypeNamespaceInternal(), AlterUserMapping(), analyze_row_processor(), ATExecAddColumn(), ATExecAddIdentity(), ATExecAddOf(), ATExecAlterColumnGenericOptions(), ATExecAlterColumnType(), ATExecAlterConstraint(), ATExecChangeOwner(), ATExecDetachPartition(), ATExecDisableRowSecurity(), ATExecDropColumn(), ATExecDropConstraint(), ATExecDropIdentity(), ATExecDropOf(), ATExecEnableRowSecurity(), ATExecForceNoForceRowSecurity(), ATExecGenericOptions(), ATExecSetIdentity(), ATExecSetOptions(), ATExecSetRelOptions(), ATExecSetStatistics(), ATExecSetStorage(), ATExecSetTableSpace(), ATExecValidateConstraint(), build_tuplestore_recursively(), CatalogCacheCreateEntry(), change_owner_fix_column_acls(), changeDependencyFor(), CollationCreate(), ConversionCreate(), copyTemplateDependencies(), create_toast_table(), CreateAccessMethod(), CreateCast(), CreateComments(), CreateForeignDataWrapper(), CreateForeignServer(), CreateForeignTable(), CreateOpFamily(), CreatePolicy(), CreatePublication(), CreateSharedComments(), CreateStatistics(), CreateSubscription(), CreateTableSpace(), CreateTransform(), CreateTrigger(), CreateUserMapping(), crosstab(), DefineOpClass(), DefineQueryRewrite(), DefineSequence(), DefineTSConfiguration(), DefineTSDictionary(), DefineTSParser(), DefineTSTemplate(), EnableDisableRule(), EnableDisableTrigger(), EnumValuesCreate(), EvalPlanQualSetTuple(), examine_attribute(), exec_assign_value(), exec_move_row(), exec_stmt_block(), exec_stmt_return_query(), ExecARDeleteTriggers(), ExecARUpdateTriggers(), ExecBRDeleteTriggers(), ExecBRInsertTriggers(), ExecBRUpdateTriggers(), ExecClearTuple(), ExecIRDeleteTriggers(), ExecIRInsertTriggers(), ExecIRUpdateTriggers(), ExecLockRows(), ExecReScanAgg(), ExecReScanSetOp(), ExecScanSubPlan(), ExecSetParamPlan(), ExecStoreMinimalTuple(), ExecStoreTuple(), ExtractReplicaIdentity(), file_acquire_sample_rows(), gather_merge_clear_tuples(), heap_delete(), heap_insert(), heap_update(), index_build(), index_constraint_create(), index_update_stats(), insert_event_trigger_tuple(), InsertExtensionTuple(), InsertOneTuple(), InsertPgAttributeTuple(), InsertPgClassTuple(), InsertRule(), inv_truncate(), inv_write(), LargeObjectCreate(), MakeConfigurationMapping(), mark_index_clustered(), MergeAttributesIntoExisting(), MergeConstraintsIntoExisting(), OperatorShellMake(), PLyGenericObject_ToComposite(), PLyMapping_ToComposite(), PLySequence_ToComposite(), ProcedureCreate(), publication_add_relation(), RangeCreate(), raw_heap_insert(), record_in(), record_recv(), recordMultipleDependencies(), reform_and_rewrite_tuple(), relation_mark_replica_identity(), RelationBuildDesc(), RelationInitPhysicalAddr(), RelationReloadIndexInfo(), RelationSetNewRelfilenode(), RemoveConstraintById(), RemoveInheritance(), RemoveRoleFromObjectPolicy(), renameatt_internal(), RenameConstraintById(), RenameEnumLabel(), RenameRelationInternal(), RenameRewriteRule(), RenameSchema(), RenameTypeInternal(), RenumberEnumType(), replorigin_create(), rewrite_heap_dead_tuple(), rewrite_heap_tuple(), SetMatViewPopulatedState(), SetRelationHasSubclass(), SetRelationNumChecks(), SetRelationRuleStatus(), SetSecurityLabel(), SetSharedSecurityLabel(), SetSubscriptionRelState(), shdepAddDependency(), shdepChangeDep(), SPI_freetuple(), statext_store(), StoreAttrDefault(), StoreCatalogInheritance1(), storeOperators(), StorePartitionBound(), storeProcedures(), swap_relation_files(), table_recheck_autovac(), toast_save_datum(), TypeShellMake(), update_attstats(), update_default_partition_oid(), UpdateIndexRelation(), vac_update_datfrozenxid(), writetup_cluster(), and xpath_table().

1374 {
1375  pfree(htup);
1376 }
void pfree(void *pointer)
Definition: mcxt.c:949

◆ heap_getsysattr()

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

Definition at line 555 of file heaptuple.c.

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

Referenced by ExecInterpExpr(), and slot_getattr().

556 {
557  Datum result;
558 
559  Assert(tup);
560 
561  /* Currently, no sys attribute ever reads as NULL. */
562  *isnull = false;
563 
564  switch (attnum)
565  {
567  /* pass-by-reference datatype */
568  result = PointerGetDatum(&(tup->t_self));
569  break;
571  result = ObjectIdGetDatum(HeapTupleGetOid(tup));
572  break;
575  break;
578  break;
581 
582  /*
583  * cmin and cmax are now both aliases for the same field, which
584  * can in fact also be a combo command id. XXX perhaps we should
585  * return the "real" cmin or cmax if possible, that is if we are
586  * inside the originating transaction?
587  */
589  break;
591  result = ObjectIdGetDatum(tup->t_tableOid);
592  break;
593  default:
594  elog(ERROR, "invalid attnum: %d", attnum);
595  result = 0; /* keep compiler quiet */
596  break;
597  }
598  return result;
599 }
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
#define PointerGetDatum(X)
Definition: postgres.h:562
#define MinCommandIdAttributeNumber
Definition: sysattr.h:24
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleHeaderGetRawXmax(tup)
Definition: htup_details.h:374
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define TableOidAttributeNumber
Definition: sysattr.h:27
Oid t_tableOid
Definition: htup.h:66
#define MaxCommandIdAttributeNumber
Definition: sysattr.h:26
#define MaxTransactionIdAttributeNumber
Definition: sysattr.h:25
#define TransactionIdGetDatum(X)
Definition: postgres.h:527
uintptr_t Datum
Definition: postgres.h:372
#define Assert(condition)
Definition: c.h:670
#define HeapTupleHeaderGetRawXmin(tup)
Definition: htup_details.h:307
#define HeapTupleHeaderGetRawCommandId(tup)
Definition: htup_details.h:390
#define CommandIdGetDatum(X)
Definition: postgres.h:548
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
#define elog
Definition: elog.h:219
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:700
#define MinTransactionIdAttributeNumber
Definition: sysattr.h:23

◆ heap_modify_tuple()

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

Definition at line 794 of file heaptuple.c.

References heap_deform_tuple(), heap_form_tuple(), HeapTupleGetOid, HeapTupleSetOid, tupleDesc::natts, palloc(), pfree(), HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleData::t_self, HeapTupleData::t_tableOid, tupleDesc::tdhasoid, and values.

Referenced by AddRoleMems(), AlterCollation(), AlterDatabase(), AlterDatabaseOwner(), AlterDomainDefault(), AlterForeignDataWrapper(), AlterForeignDataWrapperOwner_internal(), AlterForeignServer(), AlterForeignServerOwner_internal(), AlterFunction(), AlterObjectNamespace_internal(), AlterObjectOwner_internal(), AlterObjectRename_internal(), AlterOperator(), AlterPolicy(), AlterPublicationOptions(), AlterRole(), AlterSchemaOwner_internal(), AlterSetting(), AlterSubscription(), AlterTableSpaceOptions(), AlterTSDictionary(), AlterTypeOwnerInternal(), AlterUserMapping(), ApplyExtensionUpdates(), ATExecAlterColumnGenericOptions(), ATExecChangeOwner(), ATExecDetachPartition(), ATExecGenericOptions(), ATExecSetOptions(), ATExecSetRelOptions(), change_owner_fix_column_acls(), copyTemplateDependencies(), create_proc_lang(), CreateComments(), CreateSharedComments(), CreateTransform(), DelRoleMems(), ExecGrant_Attribute(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), extension_config_remove(), InsertRule(), inv_truncate(), inv_write(), MakeConfigurationMapping(), movedb(), OperatorCreate(), pg_extension_config_dump(), plperl_modify_tuple(), PLy_modify_tuple(), ProcedureCreate(), recordExtensionInitPrivWorker(), RemoveRoleFromObjectPolicy(), RenameRole(), SetDefaultACL(), SetSecurityLabel(), SetSharedSecurityLabel(), SetSubscriptionRelState(), statext_store(), StorePartitionBound(), TypeCreate(), and update_attstats().

799 {
800  int numberOfAttributes = tupleDesc->natts;
801  int attoff;
802  Datum *values;
803  bool *isnull;
804  HeapTuple newTuple;
805 
806  /*
807  * allocate and fill values and isnull arrays from either the tuple or the
808  * repl information, as appropriate.
809  *
810  * NOTE: it's debatable whether to use heap_deform_tuple() here or just
811  * heap_getattr() only the non-replaced columns. The latter could win if
812  * there are many replaced columns and few non-replaced ones. However,
813  * heap_deform_tuple costs only O(N) while the heap_getattr way would cost
814  * O(N^2) if there are many non-replaced columns, so it seems better to
815  * err on the side of linear cost.
816  */
817  values = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
818  isnull = (bool *) palloc(numberOfAttributes * sizeof(bool));
819 
820  heap_deform_tuple(tuple, tupleDesc, values, isnull);
821 
822  for (attoff = 0; attoff < numberOfAttributes; attoff++)
823  {
824  if (doReplace[attoff])
825  {
826  values[attoff] = replValues[attoff];
827  isnull[attoff] = replIsnull[attoff];
828  }
829  }
830 
831  /*
832  * create a new tuple from the values and isnull arrays
833  */
834  newTuple = heap_form_tuple(tupleDesc, values, isnull);
835 
836  pfree(values);
837  pfree(isnull);
838 
839  /*
840  * copy the identification info of the old tuple: t_ctid, t_self, and OID
841  * (if any)
842  */
843  newTuple->t_data->t_ctid = tuple->t_data->t_ctid;
844  newTuple->t_self = tuple->t_self;
845  newTuple->t_tableOid = tuple->t_tableOid;
846  if (tupleDesc->tdhasoid)
847  HeapTupleSetOid(newTuple, HeapTupleGetOid(tuple));
848 
849  return newTuple;
850 }
bool tdhasoid
Definition: tupdesc.h:82
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
int natts
Definition: tupdesc.h:79
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleSetOid(tuple, oid)
Definition: htup_details.h:703
void pfree(void *pointer)
Definition: mcxt.c:949
ItemPointerData t_ctid
Definition: htup_details.h:155
ItemPointerData t_self
Definition: htup.h:65
Oid t_tableOid
Definition: htup.h:66
uintptr_t Datum
Definition: postgres.h:372
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:936
static Datum values[MAXATTR]
Definition: bootstrap.c:164
void * palloc(Size size)
Definition: mcxt.c:848
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:700

◆ heap_modify_tuple_by_cols()

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

Definition at line 865 of file heaptuple.c.

References elog, ERROR, heap_deform_tuple(), heap_form_tuple(), HeapTupleGetOid, HeapTupleSetOid, i, tupleDesc::natts, palloc(), pfree(), HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleData::t_self, HeapTupleData::t_tableOid, tupleDesc::tdhasoid, and values.

Referenced by autoinc(), exec_assign_value(), insert_username(), moddatetime(), timetravel(), and tsvector_update_trigger().

871 {
872  int numberOfAttributes = tupleDesc->natts;
873  Datum *values;
874  bool *isnull;
875  HeapTuple newTuple;
876  int i;
877 
878  /*
879  * allocate and fill values and isnull arrays from the tuple, then replace
880  * selected columns from the input arrays.
881  */
882  values = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
883  isnull = (bool *) palloc(numberOfAttributes * sizeof(bool));
884 
885  heap_deform_tuple(tuple, tupleDesc, values, isnull);
886 
887  for (i = 0; i < nCols; i++)
888  {
889  int attnum = replCols[i];
890 
891  if (attnum <= 0 || attnum > numberOfAttributes)
892  elog(ERROR, "invalid column number %d", attnum);
893  values[attnum - 1] = replValues[i];
894  isnull[attnum - 1] = replIsnull[i];
895  }
896 
897  /*
898  * create a new tuple from the values and isnull arrays
899  */
900  newTuple = heap_form_tuple(tupleDesc, values, isnull);
901 
902  pfree(values);
903  pfree(isnull);
904 
905  /*
906  * copy the identification info of the old tuple: t_ctid, t_self, and OID
907  * (if any)
908  */
909  newTuple->t_data->t_ctid = tuple->t_data->t_ctid;
910  newTuple->t_self = tuple->t_self;
911  newTuple->t_tableOid = tuple->t_tableOid;
912  if (tupleDesc->tdhasoid)
913  HeapTupleSetOid(newTuple, HeapTupleGetOid(tuple));
914 
915  return newTuple;
916 }
bool tdhasoid
Definition: tupdesc.h:82
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
int natts
Definition: tupdesc.h:79
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleSetOid(tuple, oid)
Definition: htup_details.h:703
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
ItemPointerData t_ctid
Definition: htup_details.h:155
ItemPointerData t_self
Definition: htup.h:65
Oid t_tableOid
Definition: htup.h:66
uintptr_t Datum
Definition: postgres.h:372
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:936
static Datum values[MAXATTR]
Definition: bootstrap.c:164
void * palloc(Size size)
Definition: mcxt.c:848
int i
#define elog
Definition: elog.h:219
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:700

◆ heap_tuple_from_minimal_tuple()

HeapTuple heap_tuple_from_minimal_tuple ( MinimalTuple  mtup)

Definition at line 1499 of file heaptuple.c.

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

Referenced by ExecCopySlotTuple().

1500 {
1501  HeapTuple result;
1502  uint32 len = mtup->t_len + MINIMAL_TUPLE_OFFSET;
1503 
1504  result = (HeapTuple) palloc(HEAPTUPLESIZE + len);
1505  result->t_len = len;
1506  ItemPointerSetInvalid(&(result->t_self));
1507  result->t_tableOid = InvalidOid;
1508  result->t_data = (HeapTupleHeader) ((char *) result + HEAPTUPLESIZE);
1509  memcpy((char *) result->t_data + MINIMAL_TUPLE_OFFSET, mtup, mtup->t_len);
1510  memset(result->t_data, 0, offsetof(HeapTupleHeaderData, t_infomask2));
1511  return result;
1512 }
HeapTupleData * HeapTuple
Definition: htup.h:70
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
HeapTupleHeader t_data
Definition: htup.h:67
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
unsigned int uint32
Definition: c.h:296
Oid t_tableOid
Definition: htup.h:66
#define InvalidOid
Definition: postgres_ext.h:36
#define MINIMAL_TUPLE_OFFSET
Definition: htup_details.h:625
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:150
void * palloc(Size size)
Definition: mcxt.c:848
#define HEAPTUPLESIZE
Definition: htup.h:72
#define offsetof(type, field)
Definition: c.h:593

◆ minimal_tuple_from_heap_tuple()

MinimalTuple minimal_tuple_from_heap_tuple ( HeapTuple  htup)

Definition at line 1521 of file heaptuple.c.

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

Referenced by copytup_heap(), and ExecCopySlotMinimalTuple().

1522 {
1523  MinimalTuple result;
1524  uint32 len;
1525 
1527  len = htup->t_len - MINIMAL_TUPLE_OFFSET;
1528  result = (MinimalTuple) palloc(len);
1529  memcpy(result, (char *) htup->t_data + MINIMAL_TUPLE_OFFSET, len);
1530  result->t_len = len;
1531  return result;
1532 }
HeapTupleHeader t_data
Definition: htup.h:67
uint32 t_len
Definition: htup.h:64
MinimalTupleData * MinimalTuple
Definition: htup.h:27
unsigned int uint32
Definition: c.h:296
#define Assert(condition)
Definition: c.h:670
#define MINIMAL_TUPLE_OFFSET
Definition: htup_details.h:625
void * palloc(Size size)
Definition: mcxt.c:848

◆ nocachegetattr()

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

Definition at line 351 of file heaptuple.c.

References Assert, att_addlength_pointer, att_align_nominal, att_align_pointer, att_isnull, byte, fetchatt, HeapTupleHasNulls, HeapTupleHasVarWidth, HeapTupleNoNulls, i, tupleDesc::natts, HeapTupleHeaderData::t_bits, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, and TupleDescAttr.

Referenced by heapgettup_pagemode().

354 {
355  HeapTupleHeader tup = tuple->t_data;
356  char *tp; /* ptr to data part of tuple */
357  bits8 *bp = tup->t_bits; /* ptr to null bitmap in tuple */
358  bool slow = false; /* do we have to walk attrs? */
359  int off; /* current offset within data */
360 
361  /* ----------------
362  * Three cases:
363  *
364  * 1: No nulls and no variable-width attributes.
365  * 2: Has a null or a var-width AFTER att.
366  * 3: Has nulls or var-widths BEFORE att.
367  * ----------------
368  */
369 
370  attnum--;
371 
372  if (!HeapTupleNoNulls(tuple))
373  {
374  /*
375  * there's a null somewhere in the tuple
376  *
377  * check to see if any preceding bits are null...
378  */
379  int byte = attnum >> 3;
380  int finalbit = attnum & 0x07;
381 
382  /* check for nulls "before" final bit of last byte */
383  if ((~bp[byte]) & ((1 << finalbit) - 1))
384  slow = true;
385  else
386  {
387  /* check for nulls in any "earlier" bytes */
388  int i;
389 
390  for (i = 0; i < byte; i++)
391  {
392  if (bp[i] != 0xFF)
393  {
394  slow = true;
395  break;
396  }
397  }
398  }
399  }
400 
401  tp = (char *) tup + tup->t_hoff;
402 
403  if (!slow)
404  {
405  Form_pg_attribute att;
406 
407  /*
408  * If we get here, there are no nulls up to and including the target
409  * attribute. If we have a cached offset, we can use it.
410  */
411  att = TupleDescAttr(tupleDesc, attnum);
412  if (att->attcacheoff >= 0)
413  return fetchatt(att, tp + att->attcacheoff);
414 
415  /*
416  * Otherwise, check for non-fixed-length attrs up to and including
417  * target. If there aren't any, it's safe to cheaply initialize the
418  * cached offsets for these attrs.
419  */
420  if (HeapTupleHasVarWidth(tuple))
421  {
422  int j;
423 
424  for (j = 0; j <= attnum; j++)
425  {
426  if (TupleDescAttr(tupleDesc, j)->attlen <= 0)
427  {
428  slow = true;
429  break;
430  }
431  }
432  }
433  }
434 
435  if (!slow)
436  {
437  int natts = tupleDesc->natts;
438  int j = 1;
439 
440  /*
441  * If we get here, we have a tuple with no nulls or var-widths up to
442  * and including the target attribute, so we can use the cached offset
443  * ... only we don't have it yet, or we'd not have got here. Since
444  * it's cheap to compute offsets for fixed-width columns, we take the
445  * opportunity to initialize the cached offsets for *all* the leading
446  * fixed-width columns, in hope of avoiding future visits to this
447  * routine.
448  */
449  TupleDescAttr(tupleDesc, 0)->attcacheoff = 0;
450 
451  /* we might have set some offsets in the slow path previously */
452  while (j < natts && TupleDescAttr(tupleDesc, j)->attcacheoff > 0)
453  j++;
454 
455  off = TupleDescAttr(tupleDesc, j - 1)->attcacheoff +
456  TupleDescAttr(tupleDesc, j - 1)->attlen;
457 
458  for (; j < natts; j++)
459  {
460  Form_pg_attribute att = TupleDescAttr(tupleDesc, j);
461 
462  if (att->attlen <= 0)
463  break;
464 
465  off = att_align_nominal(off, att->attalign);
466 
467  att->attcacheoff = off;
468 
469  off += att->attlen;
470  }
471 
472  Assert(j > attnum);
473 
474  off = TupleDescAttr(tupleDesc, attnum)->attcacheoff;
475  }
476  else
477  {
478  bool usecache = true;
479  int i;
480 
481  /*
482  * Now we know that we have to walk the tuple CAREFULLY. But we still
483  * might be able to cache some offsets for next time.
484  *
485  * Note - This loop is a little tricky. For each non-null attribute,
486  * we have to first account for alignment padding before the attr,
487  * then advance over the attr based on its length. Nulls have no
488  * storage and no alignment padding either. We can use/set
489  * attcacheoff until we reach either a null or a var-width attribute.
490  */
491  off = 0;
492  for (i = 0;; i++) /* loop exit is at "break" */
493  {
494  Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
495 
496  if (HeapTupleHasNulls(tuple) && att_isnull(i, bp))
497  {
498  usecache = false;
499  continue; /* this cannot be the target att */
500  }
501 
502  /* If we know the next offset, we can skip the rest */
503  if (usecache && att->attcacheoff >= 0)
504  off = att->attcacheoff;
505  else if (att->attlen == -1)
506  {
507  /*
508  * We can only cache the offset for a varlena attribute if the
509  * offset is already suitably aligned, so that there would be
510  * no pad bytes in any case: then the offset will be valid for
511  * either an aligned or unaligned value.
512  */
513  if (usecache &&
514  off == att_align_nominal(off, att->attalign))
515  att->attcacheoff = off;
516  else
517  {
518  off = att_align_pointer(off, att->attalign, -1,
519  tp + off);
520  usecache = false;
521  }
522  }
523  else
524  {
525  /* not varlena, so safe to use att_align_nominal */
526  off = att_align_nominal(off, att->attalign);
527 
528  if (usecache)
529  att->attcacheoff = off;
530  }
531 
532  if (i == attnum)
533  break;
534 
535  off = att_addlength_pointer(off, att->attlen, tp + off);
536 
537  if (usecache && att->attlen <= 0)
538  usecache = false;
539  }
540  }
541 
542  return fetchatt(TupleDescAttr(tupleDesc, attnum), tp + off);
543 }
#define HeapTupleHasVarWidth(tuple)
Definition: htup_details.h:673
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:144
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Definition: htup_details.h:168
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:21
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
int natts
Definition: tupdesc.h:79
#define fetchatt(A, T)
Definition: tupmacs.h:37
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleHasNulls(tuple)
Definition: htup_details.h:667
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:172
#define HeapTupleNoNulls(tuple)
Definition: htup_details.h:670
#define byte(x, n)
Definition: rijndael.c:68
uint8 bits8
Definition: c.h:303
#define att_align_pointer(cur_offset, attalign, attlen, attptr)
Definition: tupmacs.h:122
#define Assert(condition)
Definition: c.h:670
int i

◆ slot_attisnull()

bool slot_attisnull ( TupleTableSlot slot,
int  attnum 
)

Definition at line 1329 of file heaptuple.c.

References elog, ERROR, heap_attisnull(), tupleDesc::natts, TupleTableSlot::tts_isnull, TupleTableSlot::tts_minhdr, TupleTableSlot::tts_tuple, and TupleTableSlot::tts_tupleDescriptor.

Referenced by ExecConstraints(), slotAllNulls(), and slotNoNulls().

1330 {
1331  HeapTuple tuple = slot->tts_tuple;
1333 
1334  /*
1335  * system attributes are handled by heap_attisnull
1336  */
1337  if (attnum <= 0)
1338  {
1339  if (tuple == NULL) /* internal error */
1340  elog(ERROR, "cannot extract system attribute from virtual tuple");
1341  if (tuple == &(slot->tts_minhdr)) /* internal error */
1342  elog(ERROR, "cannot extract system attribute from minimal tuple");
1343  return heap_attisnull(tuple, attnum);
1344  }
1345 
1346  /*
1347  * fast path if desired attribute already cached
1348  */
1349  if (attnum <= slot->tts_nvalid)
1350  return slot->tts_isnull[attnum - 1];
1351 
1352  /*
1353  * return NULL if attnum is out of range according to the tupdesc
1354  */
1355  if (attnum > tupleDesc->natts)
1356  return true;
1357 
1358  /*
1359  * otherwise we had better have a physical tuple (tts_nvalid should equal
1360  * natts in all virtual-tuple cases)
1361  */
1362  if (tuple == NULL) /* internal error */
1363  elog(ERROR, "cannot extract attribute from empty tuple slot");
1364 
1365  /* and let the tuple tell it */
1366  return heap_attisnull(tuple, attnum);
1367 }
int natts
Definition: tupdesc.h:79
#define ERROR
Definition: elog.h:43
HeapTupleData tts_minhdr
Definition: tuptable.h:128
bool * tts_isnull
Definition: tuptable.h:126
bool heap_attisnull(HeapTuple tup, int attnum)
Definition: heaptuple.c:296
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
HeapTuple tts_tuple
Definition: tuptable.h:120
#define elog
Definition: elog.h:219

◆ slot_deform_tuple()

static void slot_deform_tuple ( TupleTableSlot slot,
int  natts 
)
static

Definition at line 1036 of file heaptuple.c.

References att_addlength_pointer, att_align_nominal, att_align_pointer, att_isnull, fetchatt, HeapTupleHasNulls, HeapTupleHeaderData::t_bits, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, TupleTableSlot::tts_isnull, TupleTableSlot::tts_nvalid, TupleTableSlot::tts_off, TupleTableSlot::tts_slow, TupleTableSlot::tts_tuple, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, TupleDescAttr, and values.

Referenced by slot_getallattrs(), slot_getattr(), and slot_getsomeattrs().

1037 {
1038  HeapTuple tuple = slot->tts_tuple;
1040  Datum *values = slot->tts_values;
1041  bool *isnull = slot->tts_isnull;
1042  HeapTupleHeader tup = tuple->t_data;
1043  bool hasnulls = HeapTupleHasNulls(tuple);
1044  int attnum;
1045  char *tp; /* ptr to tuple data */
1046  long off; /* offset in tuple data */
1047  bits8 *bp = tup->t_bits; /* ptr to null bitmap in tuple */
1048  bool slow; /* can we use/set attcacheoff? */
1049 
1050  /*
1051  * Check whether the first call for this tuple, and initialize or restore
1052  * loop state.
1053  */
1054  attnum = slot->tts_nvalid;
1055  if (attnum == 0)
1056  {
1057  /* Start from the first attribute */
1058  off = 0;
1059  slow = false;
1060  }
1061  else
1062  {
1063  /* Restore state from previous execution */
1064  off = slot->tts_off;
1065  slow = slot->tts_slow;
1066  }
1067 
1068  tp = (char *) tup + tup->t_hoff;
1069 
1070  for (; attnum < natts; attnum++)
1071  {
1072  Form_pg_attribute thisatt = TupleDescAttr(tupleDesc, attnum);
1073 
1074  if (hasnulls && att_isnull(attnum, bp))
1075  {
1076  values[attnum] = (Datum) 0;
1077  isnull[attnum] = true;
1078  slow = true; /* can't use attcacheoff anymore */
1079  continue;
1080  }
1081 
1082  isnull[attnum] = false;
1083 
1084  if (!slow && thisatt->attcacheoff >= 0)
1085  off = thisatt->attcacheoff;
1086  else if (thisatt->attlen == -1)
1087  {
1088  /*
1089  * We can only cache the offset for a varlena attribute if the
1090  * offset is already suitably aligned, so that there would be no
1091  * pad bytes in any case: then the offset will be valid for either
1092  * an aligned or unaligned value.
1093  */
1094  if (!slow &&
1095  off == att_align_nominal(off, thisatt->attalign))
1096  thisatt->attcacheoff = off;
1097  else
1098  {
1099  off = att_align_pointer(off, thisatt->attalign, -1,
1100  tp + off);
1101  slow = true;
1102  }
1103  }
1104  else
1105  {
1106  /* not varlena, so safe to use att_align_nominal */
1107  off = att_align_nominal(off, thisatt->attalign);
1108 
1109  if (!slow)
1110  thisatt->attcacheoff = off;
1111  }
1112 
1113  values[attnum] = fetchatt(thisatt, tp + off);
1114 
1115  off = att_addlength_pointer(off, thisatt->attlen, tp + off);
1116 
1117  if (thisatt->attlen <= 0)
1118  slow = true; /* can't use attcacheoff anymore */
1119  }
1120 
1121  /*
1122  * Save state for next execution
1123  */
1124  slot->tts_nvalid = attnum;
1125  slot->tts_off = off;
1126  slot->tts_slow = slow;
1127 }
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:144
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Definition: htup_details.h:168
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:21
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
Datum * tts_values
Definition: tuptable.h:125
#define fetchatt(A, T)
Definition: tupmacs.h:37
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleHasNulls(tuple)
Definition: htup_details.h:667
bool * tts_isnull
Definition: tuptable.h:126
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:172
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
uint8 bits8
Definition: c.h:303
uintptr_t Datum
Definition: postgres.h:372
#define att_align_pointer(cur_offset, attalign, attlen, attptr)
Definition: tupmacs.h:122
static Datum values[MAXATTR]
Definition: bootstrap.c:164
HeapTuple tts_tuple
Definition: tuptable.h:120

◆ slot_getallattrs()

void slot_getallattrs ( TupleTableSlot slot)

Definition at line 1238 of file heaptuple.c.

References elog, ERROR, HeapTupleHeaderGetNatts, Min, tupleDesc::natts, slot_deform_tuple(), HeapTupleData::t_data, TupleTableSlot::tts_isnull, TupleTableSlot::tts_nvalid, TupleTableSlot::tts_tuple, TupleTableSlot::tts_tupleDescriptor, and TupleTableSlot::tts_values.

Referenced by agg_retrieve_hash_table(), copy_dest_receive(), ExecBuildSlotValueDescription(), ExecEvalWholeRowVar(), ExecFilterJunk(), FunctionNext(), printsimple(), printtup(), printtup_20(), printtup_internal_20(), slot_modify_cstrings(), and tstoreReceiveSlot_detoast().

1239 {
1240  int tdesc_natts = slot->tts_tupleDescriptor->natts;
1241  int attnum;
1242  HeapTuple tuple;
1243 
1244  /* Quick out if we have 'em all already */
1245  if (slot->tts_nvalid == tdesc_natts)
1246  return;
1247 
1248  /*
1249  * otherwise we had better have a physical tuple (tts_nvalid should equal
1250  * natts in all virtual-tuple cases)
1251  */
1252  tuple = slot->tts_tuple;
1253  if (tuple == NULL) /* internal error */
1254  elog(ERROR, "cannot extract attribute from empty tuple slot");
1255 
1256  /*
1257  * load up any slots available from physical tuple
1258  */
1259  attnum = HeapTupleHeaderGetNatts(tuple->t_data);
1260  attnum = Min(attnum, tdesc_natts);
1261 
1262  slot_deform_tuple(slot, attnum);
1263 
1264  /*
1265  * If tuple doesn't have all the atts indicated by tupleDesc, read the
1266  * rest as null
1267  */
1268  for (; attnum < tdesc_natts; attnum++)
1269  {
1270  slot->tts_values[attnum] = (Datum) 0;
1271  slot->tts_isnull[attnum] = true;
1272  }
1273  slot->tts_nvalid = tdesc_natts;
1274 }
#define Min(x, y)
Definition: c.h:802
Datum * tts_values
Definition: tuptable.h:125
int natts
Definition: tupdesc.h:79
HeapTupleHeader t_data
Definition: htup.h:67
#define ERROR
Definition: elog.h:43
#define HeapTupleHeaderGetNatts(tup)
Definition: htup_details.h:537
bool * tts_isnull
Definition: tuptable.h:126
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
uintptr_t Datum
Definition: postgres.h:372
HeapTuple tts_tuple
Definition: tuptable.h:120
#define elog
Definition: elog.h:219
static void slot_deform_tuple(TupleTableSlot *slot, int natts)
Definition: heaptuple.c:1036

◆ slot_getattr()

Datum slot_getattr ( TupleTableSlot slot,
int  attnum,
bool isnull 
)

Definition at line 1142 of file heaptuple.c.

References att_isnull, elog, ERROR, heap_getsysattr(), HeapTupleHasNulls, HeapTupleHeaderGetNatts, tupleDesc::natts, slot_deform_tuple(), HeapTupleHeaderData::t_bits, HeapTupleData::t_data, TupleTableSlot::tts_isnull, TupleTableSlot::tts_minhdr, TupleTableSlot::tts_tuple, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, and TupleDescAttr.

Referenced by buildSubPlanHash(), convert_prep_stmt_params(), debugtup(), execCurrentOf(), ExecGetJunkAttribute(), ExecJustAssignInnerVar(), ExecJustAssignOuterVar(), ExecJustAssignScanVar(), ExecJustInnerVar(), ExecJustInnerVarFirst(), ExecJustOuterVar(), ExecJustOuterVarFirst(), ExecJustScanVar(), ExecJustScanVarFirst(), ExecMakeFunctionResultSet(), ExecNestLoop(), ExecScanSubPlan(), ExecSetParamPlan(), execTuplesMatch(), execTuplesUnequal(), fetch_remote_table_info(), fetch_table_list(), fetch_tuple_flag(), FormIndexDatum(), FormPartitionKeyDatum(), heap_compare_slots(), hypothetical_dense_rank_final(), hypothetical_rank_common(), postquel_get_single_result(), and TupleHashTableHash().

1143 {
1144  HeapTuple tuple = slot->tts_tuple;
1146  HeapTupleHeader tup;
1147 
1148  /*
1149  * system attributes are handled by heap_getsysattr
1150  */
1151  if (attnum <= 0)
1152  {
1153  if (tuple == NULL) /* internal error */
1154  elog(ERROR, "cannot extract system attribute from virtual tuple");
1155  if (tuple == &(slot->tts_minhdr)) /* internal error */
1156  elog(ERROR, "cannot extract system attribute from minimal tuple");
1157  return heap_getsysattr(tuple, attnum, tupleDesc, isnull);
1158  }
1159 
1160  /*
1161  * fast path if desired attribute already cached
1162  */
1163  if (attnum <= slot->tts_nvalid)
1164  {
1165  *isnull = slot->tts_isnull[attnum - 1];
1166  return slot->tts_values[attnum - 1];
1167  }
1168 
1169  /*
1170  * return NULL if attnum is out of range according to the tupdesc
1171  */
1172  if (attnum > tupleDesc->natts)
1173  {
1174  *isnull = true;
1175  return (Datum) 0;
1176  }
1177 
1178  /*
1179  * otherwise we had better have a physical tuple (tts_nvalid should equal
1180  * natts in all virtual-tuple cases)
1181  */
1182  if (tuple == NULL) /* internal error */
1183  elog(ERROR, "cannot extract attribute from empty tuple slot");
1184 
1185  /*
1186  * return NULL if attnum is out of range according to the tuple
1187  *
1188  * (We have to check this separately because of various inheritance and
1189  * table-alteration scenarios: the tuple could be either longer or shorter
1190  * than the tupdesc.)
1191  */
1192  tup = tuple->t_data;
1193  if (attnum > HeapTupleHeaderGetNatts(tup))
1194  {
1195  *isnull = true;
1196  return (Datum) 0;
1197  }
1198 
1199  /*
1200  * check if target attribute is null: no point in groveling through tuple
1201  */
1202  if (HeapTupleHasNulls(tuple) && att_isnull(attnum - 1, tup->t_bits))
1203  {
1204  *isnull = true;
1205  return (Datum) 0;
1206  }
1207 
1208  /*
1209  * If the attribute's column has been dropped, we force a NULL result.
1210  * This case should not happen in normal use, but it could happen if we
1211  * are executing a plan cached before the column was dropped.
1212  */
1213  if (TupleDescAttr(tupleDesc, attnum - 1)->attisdropped)
1214  {
1215  *isnull = true;
1216  return (Datum) 0;
1217  }
1218 
1219  /*
1220  * Extract the attribute, along with any preceding attributes.
1221  */
1222  slot_deform_tuple(slot, attnum);
1223 
1224  /*
1225  * The result is acquired from tts_values array.
1226  */
1227  *isnull = slot->tts_isnull[attnum - 1];
1228  return slot->tts_values[attnum - 1];
1229 }
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Definition: htup_details.h:168
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:21
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
Datum * tts_values
Definition: tuptable.h:125
int natts
Definition: tupdesc.h:79
HeapTupleHeader t_data
Definition: htup.h:67
#define ERROR
Definition: elog.h:43
Datum heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: heaptuple.c:555
HeapTupleData tts_minhdr
Definition: tuptable.h:128
#define HeapTupleHeaderGetNatts(tup)
Definition: htup_details.h:537
#define HeapTupleHasNulls(tuple)
Definition: htup_details.h:667
bool * tts_isnull
Definition: tuptable.h:126
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
uintptr_t Datum
Definition: postgres.h:372
HeapTuple tts_tuple
Definition: tuptable.h:120
#define elog
Definition: elog.h:219
static void slot_deform_tuple(TupleTableSlot *slot, int natts)
Definition: heaptuple.c:1036

◆ slot_getsomeattrs()

void slot_getsomeattrs ( TupleTableSlot slot,
int  attnum 
)

Definition at line 1282 of file heaptuple.c.

References elog, ERROR, HeapTupleHeaderGetNatts, Min, tupleDesc::natts, slot_deform_tuple(), HeapTupleData::t_data, TupleTableSlot::tts_isnull, TupleTableSlot::tts_nvalid, TupleTableSlot::tts_tuple, TupleTableSlot::tts_tupleDescriptor, and TupleTableSlot::tts_values.

Referenced by ExecInterpExpr(), lookup_hash_entry(), prepare_projection_slot(), and process_ordered_aggregate_multi().

1283 {
1284  HeapTuple tuple;
1285  int attno;
1286 
1287  /* Quick out if we have 'em all already */
1288  if (slot->tts_nvalid >= attnum)
1289  return;
1290 
1291  /* Check for caller error */
1292  if (attnum <= 0 || attnum > slot->tts_tupleDescriptor->natts)
1293  elog(ERROR, "invalid attribute number %d", attnum);
1294 
1295  /*
1296  * otherwise we had better have a physical tuple (tts_nvalid should equal
1297  * natts in all virtual-tuple cases)
1298  */
1299  tuple = slot->tts_tuple;
1300  if (tuple == NULL) /* internal error */
1301  elog(ERROR, "cannot extract attribute from empty tuple slot");
1302 
1303  /*
1304  * load up any slots available from physical tuple
1305  */
1306  attno = HeapTupleHeaderGetNatts(tuple->t_data);
1307  attno = Min(attno, attnum);
1308 
1309  slot_deform_tuple(slot, attno);
1310 
1311  /*
1312  * If tuple doesn't have all the atts indicated by tupleDesc, read the
1313  * rest as null
1314  */
1315  for (; attno < attnum; attno++)
1316  {
1317  slot->tts_values[attno] = (Datum) 0;
1318  slot->tts_isnull[attno] = true;
1319  }
1320  slot->tts_nvalid = attnum;
1321 }
#define Min(x, y)
Definition: c.h:802
Datum * tts_values
Definition: tuptable.h:125
int natts
Definition: tupdesc.h:79
HeapTupleHeader t_data
Definition: htup.h:67
#define ERROR
Definition: elog.h:43
#define HeapTupleHeaderGetNatts(tup)
Definition: htup_details.h:537
bool * tts_isnull
Definition: tuptable.h:126
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
uintptr_t Datum
Definition: postgres.h:372
HeapTuple tts_tuple
Definition: tuptable.h:120
#define elog
Definition: elog.h:219
static void slot_deform_tuple(TupleTableSlot *slot, int natts)
Definition: heaptuple.c:1036