PostgreSQL Source Code  git master
itup.h File Reference
#include "access/tupdesc.h"
#include "access/tupmacs.h"
#include "storage/bufpage.h"
#include "storage/itemptr.h"
Include dependency graph for itup.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  IndexTupleData
 
struct  IndexAttributeBitMapData
 

Macros

#define INDEX_SIZE_MASK   0x1FFF
 
#define INDEX_AM_RESERVED_BIT
 
#define INDEX_VAR_MASK   0x4000
 
#define INDEX_NULL_MASK   0x8000
 
#define IndexTupleSize(itup)   ((Size) ((itup)->t_info & INDEX_SIZE_MASK))
 
#define IndexTupleHasNulls(itup)   ((((IndexTuple) (itup))->t_info & INDEX_NULL_MASK))
 
#define IndexTupleHasVarwidths(itup)   ((((IndexTuple) (itup))->t_info & INDEX_VAR_MASK))
 
#define IndexInfoFindDataOffset(t_info)
 
#define index_getattr(tup, attnum, tupleDesc, isnull)
 
#define MaxIndexTuplesPerPage
 

Typedefs

typedef struct IndexTupleData IndexTupleData
 
typedef IndexTupleDataIndexTuple
 
typedef struct IndexAttributeBitMapData IndexAttributeBitMapData
 
typedef IndexAttributeBitMapDataIndexAttributeBitMap
 

Functions

IndexTuple index_form_tuple (TupleDesc tupleDescriptor, Datum *values, bool *isnull)
 
Datum nocache_index_getattr (IndexTuple tup, int attnum, TupleDesc tupleDesc)
 
void index_deform_tuple (IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
 
IndexTuple CopyIndexTuple (IndexTuple source)
 
IndexTuple index_truncate_tuple (TupleDesc sourceDescriptor, IndexTuple source, int leavenatts)
 

Macro Definition Documentation

◆ INDEX_AM_RESERVED_BIT

#define INDEX_AM_RESERVED_BIT
Value:
0x2000 /* reserved for index-AM specific
* usage */

Definition at line 66 of file itup.h.

◆ index_getattr

#define index_getattr (   tup,
  attnum,
  tupleDesc,
  isnull 
)
Value:
( \
AssertMacro(PointerIsValid(isnull) && (attnum) > 0), \
*(isnull) = false, \
( \
TupleDescAttr((tupleDesc), (attnum)-1)->attcacheoff >= 0 ? \
( \
fetchatt(TupleDescAttr((tupleDesc), (attnum)-1), \
(char *) (tup) + IndexInfoFindDataOffset((tup)->t_info) \
+ TupleDescAttr((tupleDesc), (attnum)-1)->attcacheoff) \
) \
: \
nocache_index_getattr((tup), (attnum), (tupleDesc)) \
) \
: \
( \
(att_isnull((attnum)-1, (char *)(tup) + sizeof(IndexTupleData))) ? \
( \
*(isnull) = true, \
(Datum)NULL \
) \
: \
( \
nocache_index_getattr((tup), (attnum), (tupleDesc)) \
) \
) \
)
#define IndexInfoFindDataOffset(t_info)
Definition: itup.h:80
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:23
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define IndexTupleHasNulls(itup)
Definition: itup.h:72
uintptr_t Datum
Definition: postgres.h:367
int16 attnum
Definition: pg_attribute.h:79
Datum nocache_index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc)
Definition: indextuple.c:218
#define PointerIsValid(pointer)
Definition: c.h:627

Definition at line 100 of file itup.h.

Referenced by _bt_check_rowcompare(), _bt_checkkeys(), _bt_compare(), _bt_keep_natts(), _bt_keep_natts_fast(), _bt_load(), _bt_mkscankey(), _hash_checkqual(), bt_normalize_tuple(), comparetup_index_btree(), copytup_index(), gintuple_get_attrnum(), gintuple_get_key(), gistchoose(), gistDeCompressAtt(), gistFetchTuple(), gistindex_keytest(), gistMakeUnionItVec(), gistSplitByKey(), readtup_index(), and tuplesort_putindextuplevalues().

◆ INDEX_NULL_MASK

#define INDEX_NULL_MASK   0x8000

Definition at line 69 of file itup.h.

Referenced by index_form_tuple(), and spgFormNodeTuple().

◆ INDEX_SIZE_MASK

#define INDEX_SIZE_MASK   0x1FFF

◆ INDEX_VAR_MASK

#define INDEX_VAR_MASK   0x4000

Definition at line 68 of file itup.h.

Referenced by index_form_tuple().

◆ IndexInfoFindDataOffset

#define IndexInfoFindDataOffset (   t_info)
Value:
( \
(!((t_info) & INDEX_NULL_MASK)) ? \
( \
) \
: \
( \
) \
)
#define INDEX_NULL_MASK
Definition: itup.h:69
size_t Size
Definition: c.h:467
#define MAXALIGN(LEN)
Definition: c.h:686

Definition at line 80 of file itup.h.

Referenced by _hash_get_indextuple_hashkey(), bt_page_print_tuples(), index_deform_tuple(), index_form_tuple(), and nocache_index_getattr().

◆ IndexTupleHasNulls

#define IndexTupleHasNulls (   itup)    ((((IndexTuple) (itup))->t_info & INDEX_NULL_MASK))

◆ IndexTupleHasVarwidths

#define IndexTupleHasVarwidths (   itup)    ((((IndexTuple) (itup))->t_info & INDEX_VAR_MASK))

Definition at line 73 of file itup.h.

Referenced by bt_normalize_tuple(), bt_page_print_tuples(), and nocache_index_getattr().

◆ IndexTupleSize

◆ MaxIndexTuplesPerPage

Typedef Documentation

◆ IndexAttributeBitMap

Definition at line 60 of file itup.h.

◆ IndexAttributeBitMapData

◆ IndexTuple

Definition at line 53 of file itup.h.

◆ IndexTupleData

Function Documentation

◆ CopyIndexTuple()

IndexTuple CopyIndexTuple ( IndexTuple  source)

Definition at line 509 of file indextuple.c.

References IndexTupleSize, and palloc().

Referenced by _bt_buildadd(), _bt_insert_parent(), _bt_newroot(), _bt_pagedel(), _hash_splitbucket(), _hash_squeezebucket(), gistformdownlink(), and index_truncate_tuple().

510 {
511  IndexTuple result;
512  Size size;
513 
514  size = IndexTupleSize(source);
515  result = (IndexTuple) palloc(size);
516  memcpy(result, source, size);
517  return result;
518 }
IndexTupleData * IndexTuple
Definition: itup.h:53
size_t Size
Definition: c.h:467
void * palloc(Size size)
Definition: mcxt.c:949
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ index_deform_tuple()

void index_deform_tuple ( IndexTuple  tup,
TupleDesc  tupleDescriptor,
Datum values,
bool isnull 
)

Definition at line 433 of file indextuple.c.

References Assert, att_addlength_pointer, att_align_nominal, att_align_pointer, att_isnull, attnum, fetchatt, INDEX_MAX_KEYS, IndexInfoFindDataOffset, IndexTupleHasNulls, TupleDescData::natts, IndexTupleData::t_info, and TupleDescAttr.

Referenced by _bt_check_unique(), comparetup_index_btree(), get_actual_variable_endpoint(), index_truncate_tuple(), and StoreIndexTuple().

435 {
436  int hasnulls = IndexTupleHasNulls(tup);
437  int natts = tupleDescriptor->natts; /* number of atts to extract */
438  int attnum;
439  char *tp; /* ptr to tuple data */
440  int off; /* offset in tuple data */
441  bits8 *bp; /* ptr to null bitmap in tuple */
442  bool slow = false; /* can we use/set attcacheoff? */
443 
444  /* Assert to protect callers who allocate fixed-size arrays */
445  Assert(natts <= INDEX_MAX_KEYS);
446 
447  /* XXX "knows" t_bits are just after fixed tuple header! */
448  bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
449 
450  tp = (char *) tup + IndexInfoFindDataOffset(tup->t_info);
451  off = 0;
452 
453  for (attnum = 0; attnum < natts; attnum++)
454  {
455  Form_pg_attribute thisatt = TupleDescAttr(tupleDescriptor, attnum);
456 
457  if (hasnulls && att_isnull(attnum, bp))
458  {
459  values[attnum] = (Datum) 0;
460  isnull[attnum] = true;
461  slow = true; /* can't use attcacheoff anymore */
462  continue;
463  }
464 
465  isnull[attnum] = false;
466 
467  if (!slow && thisatt->attcacheoff >= 0)
468  off = thisatt->attcacheoff;
469  else if (thisatt->attlen == -1)
470  {
471  /*
472  * We can only cache the offset for a varlena attribute if the
473  * offset is already suitably aligned, so that there would be no
474  * pad bytes in any case: then the offset will be valid for either
475  * an aligned or unaligned value.
476  */
477  if (!slow &&
478  off == att_align_nominal(off, thisatt->attalign))
479  thisatt->attcacheoff = off;
480  else
481  {
482  off = att_align_pointer(off, thisatt->attalign, -1,
483  tp + off);
484  slow = true;
485  }
486  }
487  else
488  {
489  /* not varlena, so safe to use att_align_nominal */
490  off = att_align_nominal(off, thisatt->attalign);
491 
492  if (!slow)
493  thisatt->attcacheoff = off;
494  }
495 
496  values[attnum] = fetchatt(thisatt, tp + off);
497 
498  off = att_addlength_pointer(off, thisatt->attlen, tp + off);
499 
500  if (thisatt->attlen <= 0)
501  slow = true; /* can't use attcacheoff anymore */
502  }
503 }
#define IndexInfoFindDataOffset(t_info)
Definition: itup.h:80
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:146
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:23
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define IndexTupleHasNulls(itup)
Definition: itup.h:72
#define fetchatt(A, T)
Definition: tupmacs.h:39
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:174
uint8 bits8
Definition: c.h:366
uintptr_t Datum
Definition: postgres.h:367
struct IndexTupleData IndexTupleData
#define att_align_pointer(cur_offset, attalign, attlen, attptr)
Definition: tupmacs.h:124
int16 attnum
Definition: pg_attribute.h:79
#define Assert(condition)
Definition: c.h:733
#define INDEX_MAX_KEYS
static Datum values[MAXATTR]
Definition: bootstrap.c:167
unsigned short t_info
Definition: itup.h:49

◆ index_form_tuple()

IndexTuple index_form_tuple ( TupleDesc  tupleDescriptor,
Datum values,
bool isnull 
)

Definition at line 47 of file indextuple.c.

References Assert, DatumGetPointer, detoast_external_attr(), ereport, errcode(), errmsg(), ERROR, heap_compute_data_size(), heap_fill_tuple(), HEAP_HASEXTERNAL, HEAP_HASVARWIDTH, i, INDEX_MAX_KEYS, INDEX_NULL_MASK, INDEX_SIZE_MASK, INDEX_VAR_MASK, IndexInfoFindDataOffset, MAXALIGN, TupleDescData::natts, palloc0(), pfree(), PointerGetDatum, IndexTupleData::t_info, toast_compress_datum(), TOAST_INDEX_HACK, TOAST_INDEX_TARGET, TupleDescAttr, VARATT_IS_EXTENDED, VARATT_IS_EXTERNAL, and VARSIZE.

Referenced by bt_normalize_tuple(), bt_tuple_present_callback(), btinsert(), GinFormTuple(), gistFormTuple(), hashbuildCallback(), hashinsert(), index_truncate_tuple(), and tuplesort_putindextuplevalues().

50 {
51  char *tp; /* tuple pointer */
52  IndexTuple tuple; /* return tuple */
53  Size size,
54  data_size,
55  hoff;
56  int i;
57  unsigned short infomask = 0;
58  bool hasnull = false;
59  uint16 tupmask = 0;
60  int numberOfAttributes = tupleDescriptor->natts;
61 
62 #ifdef TOAST_INDEX_HACK
63  Datum untoasted_values[INDEX_MAX_KEYS];
64  bool untoasted_free[INDEX_MAX_KEYS];
65 #endif
66 
67  if (numberOfAttributes > INDEX_MAX_KEYS)
68  ereport(ERROR,
69  (errcode(ERRCODE_TOO_MANY_COLUMNS),
70  errmsg("number of index columns (%d) exceeds limit (%d)",
71  numberOfAttributes, INDEX_MAX_KEYS)));
72 
73 #ifdef TOAST_INDEX_HACK
74  for (i = 0; i < numberOfAttributes; i++)
75  {
76  Form_pg_attribute att = TupleDescAttr(tupleDescriptor, i);
77 
78  untoasted_values[i] = values[i];
79  untoasted_free[i] = false;
80 
81  /* Do nothing if value is NULL or not of varlena type */
82  if (isnull[i] || att->attlen != -1)
83  continue;
84 
85  /*
86  * If value is stored EXTERNAL, must fetch it so we are not depending
87  * on outside storage. This should be improved someday.
88  */
90  {
91  untoasted_values[i] =
93  DatumGetPointer(values[i])));
94  untoasted_free[i] = true;
95  }
96 
97  /*
98  * If value is above size target, and is of a compressible datatype,
99  * try to compress it in-line.
100  */
101  if (!VARATT_IS_EXTENDED(DatumGetPointer(untoasted_values[i])) &&
102  VARSIZE(DatumGetPointer(untoasted_values[i])) > TOAST_INDEX_TARGET &&
103  (att->attstorage == 'x' || att->attstorage == 'm'))
104  {
105  Datum cvalue = toast_compress_datum(untoasted_values[i]);
106 
107  if (DatumGetPointer(cvalue) != NULL)
108  {
109  /* successful compression */
110  if (untoasted_free[i])
111  pfree(DatumGetPointer(untoasted_values[i]));
112  untoasted_values[i] = cvalue;
113  untoasted_free[i] = true;
114  }
115  }
116  }
117 #endif
118 
119  for (i = 0; i < numberOfAttributes; i++)
120  {
121  if (isnull[i])
122  {
123  hasnull = true;
124  break;
125  }
126  }
127 
128  if (hasnull)
129  infomask |= INDEX_NULL_MASK;
130 
131  hoff = IndexInfoFindDataOffset(infomask);
132 #ifdef TOAST_INDEX_HACK
133  data_size = heap_compute_data_size(tupleDescriptor,
134  untoasted_values, isnull);
135 #else
136  data_size = heap_compute_data_size(tupleDescriptor,
137  values, isnull);
138 #endif
139  size = hoff + data_size;
140  size = MAXALIGN(size); /* be conservative */
141 
142  tp = (char *) palloc0(size);
143  tuple = (IndexTuple) tp;
144 
145  heap_fill_tuple(tupleDescriptor,
146 #ifdef TOAST_INDEX_HACK
147  untoasted_values,
148 #else
149  values,
150 #endif
151  isnull,
152  (char *) tp + hoff,
153  data_size,
154  &tupmask,
155  (hasnull ? (bits8 *) tp + sizeof(IndexTupleData) : NULL));
156 
157 #ifdef TOAST_INDEX_HACK
158  for (i = 0; i < numberOfAttributes; i++)
159  {
160  if (untoasted_free[i])
161  pfree(DatumGetPointer(untoasted_values[i]));
162  }
163 #endif
164 
165  /*
166  * We do this because heap_fill_tuple wants to initialize a "tupmask"
167  * which is used for HeapTuples, but we want an indextuple infomask. The
168  * only relevant info is the "has variable attributes" field. We have
169  * already set the hasnull bit above.
170  */
171  if (tupmask & HEAP_HASVARWIDTH)
172  infomask |= INDEX_VAR_MASK;
173 
174  /* Also assert we got rid of external attributes */
175 #ifdef TOAST_INDEX_HACK
176  Assert((tupmask & HEAP_HASEXTERNAL) == 0);
177 #endif
178 
179  /*
180  * Here we make sure that the size will fit in the field reserved for it
181  * in t_info.
182  */
183  if ((size & INDEX_SIZE_MASK) != size)
184  ereport(ERROR,
185  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
186  errmsg("index row requires %zu bytes, maximum size is %zu",
187  size, (Size) INDEX_SIZE_MASK)));
188 
189  infomask |= size;
190 
191  /*
192  * initialize metadata
193  */
194  tuple->t_info = infomask;
195  return tuple;
196 }
void heap_fill_tuple(TupleDesc tupleDesc, Datum *values, bool *isnull, char *data, Size data_size, uint16 *infomask, bits8 *bit)
Definition: heaptuple.c:304
#define IndexInfoFindDataOffset(t_info)
Definition: itup.h:80
#define VARSIZE(PTR)
Definition: postgres.h:303
#define PointerGetDatum(X)
Definition: postgres.h:556
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
struct varlena * detoast_external_attr(struct varlena *attr)
Definition: detoast.c:45
#define INDEX_SIZE_MASK
Definition: itup.h:65
int errcode(int sqlerrcode)
Definition: elog.c:608
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:313
unsigned short uint16
Definition: c.h:358
void pfree(void *pointer)
Definition: mcxt.c:1056
#define TOAST_INDEX_TARGET
Definition: heaptoast.h:68
#define ERROR
Definition: elog.h:43
#define INDEX_VAR_MASK
Definition: itup.h:68
IndexTupleData * IndexTuple
Definition: itup.h:53
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define INDEX_NULL_MASK
Definition: itup.h:69
#define HEAP_HASVARWIDTH
Definition: htup_details.h:190
#define ereport(elevel, rest)
Definition: elog.h:141
uint8 bits8
Definition: c.h:366
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
#define TOAST_INDEX_HACK
Definition: indextuple.c:29
#define Assert(condition)
Definition: c.h:733
#define INDEX_MAX_KEYS
size_t Size
Definition: c.h:467
#define MAXALIGN(LEN)
Definition: c.h:686
Size heap_compute_data_size(TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:119
#define VARATT_IS_EXTENDED(PTR)
Definition: postgres.h:327
#define DatumGetPointer(X)
Definition: postgres.h:549
static Datum values[MAXATTR]
Definition: bootstrap.c:167
Datum toast_compress_datum(Datum value)
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
#define HEAP_HASEXTERNAL
Definition: htup_details.h:191
Definition: c.h:550
unsigned short t_info
Definition: itup.h:49

◆ index_truncate_tuple()

IndexTuple index_truncate_tuple ( TupleDesc  sourceDescriptor,
IndexTuple  source,
int  leavenatts 
)

Definition at line 538 of file indextuple.c.

References Assert, CopyIndexTuple(), index_deform_tuple(), index_form_tuple(), INDEX_MAX_KEYS, IndexTupleSize, TupleDescData::natts, palloc(), pfree(), IndexTupleData::t_tid, TupleDescCopy(), TupleDescSize, and values.

Referenced by _bt_truncate().

540 {
541  TupleDesc truncdesc;
543  bool isnull[INDEX_MAX_KEYS];
544  IndexTuple truncated;
545 
546  Assert(leavenatts <= sourceDescriptor->natts);
547 
548  /* Easy case: no truncation actually required */
549  if (leavenatts == sourceDescriptor->natts)
550  return CopyIndexTuple(source);
551 
552  /* Create temporary descriptor to scribble on */
553  truncdesc = palloc(TupleDescSize(sourceDescriptor));
554  TupleDescCopy(truncdesc, sourceDescriptor);
555  truncdesc->natts = leavenatts;
556 
557  /* Deform, form copy of tuple with fewer attributes */
558  index_deform_tuple(source, truncdesc, values, isnull);
559  truncated = index_form_tuple(truncdesc, values, isnull);
560  truncated->t_tid = source->t_tid;
561  Assert(IndexTupleSize(truncated) <= IndexTupleSize(source));
562 
563  /*
564  * Cannot leak memory here, TupleDescCopy() doesn't allocate any inner
565  * structure, so, plain pfree() should clean all allocated memory
566  */
567  pfree(truncdesc);
568 
569  return truncated;
570 }
ItemPointerData t_tid
Definition: itup.h:37
#define TupleDescSize(src)
Definition: tupdesc.h:102
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:47
void pfree(void *pointer)
Definition: mcxt.c:1056
IndexTuple CopyIndexTuple(IndexTuple source)
Definition: indextuple.c:509
void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:433
uintptr_t Datum
Definition: postgres.h:367
#define Assert(condition)
Definition: c.h:733
#define INDEX_MAX_KEYS
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void TupleDescCopy(TupleDesc dst, TupleDesc src)
Definition: tupdesc.c:233
void * palloc(Size size)
Definition: mcxt.c:949
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ nocache_index_getattr()

Datum nocache_index_getattr ( IndexTuple  tup,
int  attnum,
TupleDesc  tupleDesc 
)

Definition at line 218 of file indextuple.c.

References Assert, att_addlength_pointer, att_align_nominal, att_align_pointer, att_isnull, attlen, attnum, byte, fetchatt, i, IndexInfoFindDataOffset, IndexTupleHasNulls, IndexTupleHasVarwidths, TupleDescData::natts, IndexTupleData::t_info, and TupleDescAttr.

221 {
222  char *tp; /* ptr to data part of tuple */
223  bits8 *bp = NULL; /* ptr to null bitmap in tuple */
224  bool slow = false; /* do we have to walk attrs? */
225  int data_off; /* tuple data offset */
226  int off; /* current offset within data */
227 
228  /* ----------------
229  * Three cases:
230  *
231  * 1: No nulls and no variable-width attributes.
232  * 2: Has a null or a var-width AFTER att.
233  * 3: Has nulls or var-widths BEFORE att.
234  * ----------------
235  */
236 
237  data_off = IndexInfoFindDataOffset(tup->t_info);
238 
239  attnum--;
240 
241  if (IndexTupleHasNulls(tup))
242  {
243  /*
244  * there's a null somewhere in the tuple
245  *
246  * check to see if desired att is null
247  */
248 
249  /* XXX "knows" t_bits are just after fixed tuple header! */
250  bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
251 
252  /*
253  * Now check to see if any preceding bits are null...
254  */
255  {
256  int byte = attnum >> 3;
257  int finalbit = attnum & 0x07;
258 
259  /* check for nulls "before" final bit of last byte */
260  if ((~bp[byte]) & ((1 << finalbit) - 1))
261  slow = true;
262  else
263  {
264  /* check for nulls in any "earlier" bytes */
265  int i;
266 
267  for (i = 0; i < byte; i++)
268  {
269  if (bp[i] != 0xFF)
270  {
271  slow = true;
272  break;
273  }
274  }
275  }
276  }
277  }
278 
279  tp = (char *) tup + data_off;
280 
281  if (!slow)
282  {
283  Form_pg_attribute att;
284 
285  /*
286  * If we get here, there are no nulls up to and including the target
287  * attribute. If we have a cached offset, we can use it.
288  */
289  att = TupleDescAttr(tupleDesc, attnum);
290  if (att->attcacheoff >= 0)
291  return fetchatt(att, tp + att->attcacheoff);
292 
293  /*
294  * Otherwise, check for non-fixed-length attrs up to and including
295  * target. If there aren't any, it's safe to cheaply initialize the
296  * cached offsets for these attrs.
297  */
298  if (IndexTupleHasVarwidths(tup))
299  {
300  int j;
301 
302  for (j = 0; j <= attnum; j++)
303  {
304  if (TupleDescAttr(tupleDesc, j)->attlen <= 0)
305  {
306  slow = true;
307  break;
308  }
309  }
310  }
311  }
312 
313  if (!slow)
314  {
315  int natts = tupleDesc->natts;
316  int j = 1;
317 
318  /*
319  * If we get here, we have a tuple with no nulls or var-widths up to
320  * and including the target attribute, so we can use the cached offset
321  * ... only we don't have it yet, or we'd not have got here. Since
322  * it's cheap to compute offsets for fixed-width columns, we take the
323  * opportunity to initialize the cached offsets for *all* the leading
324  * fixed-width columns, in hope of avoiding future visits to this
325  * routine.
326  */
327  TupleDescAttr(tupleDesc, 0)->attcacheoff = 0;
328 
329  /* we might have set some offsets in the slow path previously */
330  while (j < natts && TupleDescAttr(tupleDesc, j)->attcacheoff > 0)
331  j++;
332 
333  off = TupleDescAttr(tupleDesc, j - 1)->attcacheoff +
334  TupleDescAttr(tupleDesc, j - 1)->attlen;
335 
336  for (; j < natts; j++)
337  {
338  Form_pg_attribute att = TupleDescAttr(tupleDesc, j);
339 
340  if (att->attlen <= 0)
341  break;
342 
343  off = att_align_nominal(off, att->attalign);
344 
345  att->attcacheoff = off;
346 
347  off += att->attlen;
348  }
349 
350  Assert(j > attnum);
351 
352  off = TupleDescAttr(tupleDesc, attnum)->attcacheoff;
353  }
354  else
355  {
356  bool usecache = true;
357  int i;
358 
359  /*
360  * Now we know that we have to walk the tuple CAREFULLY. But we still
361  * might be able to cache some offsets for next time.
362  *
363  * Note - This loop is a little tricky. For each non-null attribute,
364  * we have to first account for alignment padding before the attr,
365  * then advance over the attr based on its length. Nulls have no
366  * storage and no alignment padding either. We can use/set
367  * attcacheoff until we reach either a null or a var-width attribute.
368  */
369  off = 0;
370  for (i = 0;; i++) /* loop exit is at "break" */
371  {
372  Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
373 
374  if (IndexTupleHasNulls(tup) && att_isnull(i, bp))
375  {
376  usecache = false;
377  continue; /* this cannot be the target att */
378  }
379 
380  /* If we know the next offset, we can skip the rest */
381  if (usecache && att->attcacheoff >= 0)
382  off = att->attcacheoff;
383  else if (att->attlen == -1)
384  {
385  /*
386  * We can only cache the offset for a varlena attribute if the
387  * offset is already suitably aligned, so that there would be
388  * no pad bytes in any case: then the offset will be valid for
389  * either an aligned or unaligned value.
390  */
391  if (usecache &&
392  off == att_align_nominal(off, att->attalign))
393  att->attcacheoff = off;
394  else
395  {
396  off = att_align_pointer(off, att->attalign, -1,
397  tp + off);
398  usecache = false;
399  }
400  }
401  else
402  {
403  /* not varlena, so safe to use att_align_nominal */
404  off = att_align_nominal(off, att->attalign);
405 
406  if (usecache)
407  att->attcacheoff = off;
408  }
409 
410  if (i == attnum)
411  break;
412 
413  off = att_addlength_pointer(off, att->attlen, tp + off);
414 
415  if (usecache && att->attlen <= 0)
416  usecache = false;
417  }
418  }
419 
420  return fetchatt(TupleDescAttr(tupleDesc, attnum), tp + off);
421 }
#define IndexInfoFindDataOffset(t_info)
Definition: itup.h:80
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:146
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:23
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define IndexTupleHasNulls(itup)
Definition: itup.h:72
#define fetchatt(A, T)
Definition: tupmacs.h:39
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:174
#define byte(x, n)
Definition: rijndael.c:68
int16 attlen
Definition: pg_attribute.h:64
uint8 bits8
Definition: c.h:366
struct IndexTupleData IndexTupleData
#define att_align_pointer(cur_offset, attalign, attlen, attptr)
Definition: tupmacs.h:124
int16 attnum
Definition: pg_attribute.h:79
#define Assert(condition)
Definition: c.h:733
#define IndexTupleHasVarwidths(itup)
Definition: itup.h:73
int i
unsigned short t_info
Definition: itup.h:49