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)
 
void index_deform_tuple_internal (TupleDesc tupleDescriptor, Datum *values, bool *isnull, char *tp, bits8 *bp, int hasnulls)
 
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:25
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define IndexTupleHasNulls(itup)
Definition: itup.h:72
uintptr_t Datum
Definition: postgres.h:411
int16 attnum
Definition: pg_attribute.h:83
Datum nocache_index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc)
Definition: indextuple.c:231
#define PointerIsValid(pointer)
Definition: c.h:698

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(), 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

◆ 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:540
#define MAXALIGN(LEN)
Definition: c.h:757

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

#define IndexTupleSize (   itup)    ((Size) ((itup)->t_info & INDEX_SIZE_MASK))

Definition at line 71 of file itup.h.

Referenced by _bt_buildadd(), _bt_check_third_page(), _bt_dedup_finish_pending(), _bt_dedup_save_htid(), _bt_dedup_start_pending(), _bt_delitems_delete(), _bt_delitems_vacuum(), _bt_doinsert(), _bt_form_posting(), _bt_insert_parent(), _bt_insertonpg(), _bt_mark_page_halfdead(), _bt_recsplitloc(), _bt_restore_page(), _bt_saveitem(), _bt_sort_dedup_finish_pending(), _bt_split(), _bt_truncate(), _hash_doinsert(), _hash_pgaddmultitup(), _hash_splitbucket(), _hash_squeezebucket(), bt_check_level_from_leftmost(), bt_page_print_tuples(), bt_pivot_tuple_identical(), bt_rootdescend(), bt_target_page_check(), bt_tuple_present_callback(), btree_xlog_insert(), btree_xlog_split(), btree_xlog_updates(), BTreeTupleGetHeapTID(), CopyIndexTuple(), decodePageSplitRecord(), entryExecPlaceToPage(), entryIsEnoughSpace(), entrySplitPage(), GetBTPageStatistics(), ginEntryFillRoot(), GinFormInteriorTuple(), GinFormTuple(), ginHeapTupleFastCollect(), ginHeapTupleFastInsert(), ginRedoInsertEntry(), ginRedoInsertListPage(), ginRedoUpdateMetapage(), ginVacuumEntryPage(), gist_indexsortbuild_pagestate_add(), gist_page_items(), gist_page_items_bytea(), gistBuildCallback(), gistfillbuffer(), gistfillitupvec(), gistfitpage(), gistGetItupFromPage(), gistnospace(), gistPlaceItupToPage(), gistplacetopage(), gistRedoPageUpdateRecord(), gistSplit(), gistXLogUpdate(), hash_xlog_move_page_contents(), hash_xlog_squeeze_page(), index_truncate_tuple(), makeSublist(), spgFormInnerTuple(), writeListPage(), and writetup_index().

◆ 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 537 of file indextuple.c.

References IndexTupleSize, and palloc().

Referenced by _bt_buildadd(), _bt_insert_parent(), _bt_insertonpg(), _bt_load(), _bt_newroot(), _bt_pagedel(), _bt_swap_posting(), _hash_splitbucket(), _hash_squeezebucket(), btree_xlog_insert(), btree_xlog_split(), gistformdownlink(), and index_truncate_tuple().

538 {
539  IndexTuple result;
540  Size size;
541 
542  size = IndexTupleSize(source);
543  result = (IndexTuple) palloc(size);
544  memcpy(result, source, size);
545  return result;
546 }
IndexTupleData * IndexTuple
Definition: itup.h:53
size_t Size
Definition: c.h:540
void * palloc(Size size)
Definition: mcxt.c:1062
#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 446 of file indextuple.c.

References index_deform_tuple_internal(), IndexInfoFindDataOffset, IndexTupleHasNulls, and IndexTupleData::t_info.

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

448 {
449  char *tp; /* ptr to tuple data */
450  bits8 *bp; /* ptr to null bitmap in tuple */
451 
452  /* XXX "knows" t_bits are just after fixed tuple header! */
453  bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
454 
455  tp = (char *) tup + IndexInfoFindDataOffset(tup->t_info);
456 
457  index_deform_tuple_internal(tupleDescriptor, values, isnull,
458  tp, bp, IndexTupleHasNulls(tup));
459 }
#define IndexInfoFindDataOffset(t_info)
Definition: itup.h:80
#define IndexTupleHasNulls(itup)
Definition: itup.h:72
void index_deform_tuple_internal(TupleDesc tupleDescriptor, Datum *values, bool *isnull, char *tp, bits8 *bp, int hasnulls)
Definition: indextuple.c:469
uint8 bits8
Definition: c.h:448
struct IndexTupleData IndexTupleData
static Datum values[MAXATTR]
Definition: bootstrap.c:166
unsigned short t_info
Definition: itup.h:49

◆ index_deform_tuple_internal()

void index_deform_tuple_internal ( TupleDesc  tupleDescriptor,
Datum values,
bool isnull,
char *  tp,
bits8 bp,
int  hasnulls 
)

Definition at line 469 of file indextuple.c.

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

Referenced by index_deform_tuple(), and spgDeformLeafTuple().

472 {
473  int natts = tupleDescriptor->natts; /* number of atts to extract */
474  int attnum;
475  int off = 0; /* offset in tuple data */
476  bool slow = false; /* can we use/set attcacheoff? */
477 
478  /* Assert to protect callers who allocate fixed-size arrays */
479  Assert(natts <= INDEX_MAX_KEYS);
480 
481  for (attnum = 0; attnum < natts; attnum++)
482  {
483  Form_pg_attribute thisatt = TupleDescAttr(tupleDescriptor, attnum);
484 
485  if (hasnulls && att_isnull(attnum, bp))
486  {
487  values[attnum] = (Datum) 0;
488  isnull[attnum] = true;
489  slow = true; /* can't use attcacheoff anymore */
490  continue;
491  }
492 
493  isnull[attnum] = false;
494 
495  if (!slow && thisatt->attcacheoff >= 0)
496  off = thisatt->attcacheoff;
497  else if (thisatt->attlen == -1)
498  {
499  /*
500  * We can only cache the offset for a varlena attribute if the
501  * offset is already suitably aligned, so that there would be no
502  * pad bytes in any case: then the offset will be valid for either
503  * an aligned or unaligned value.
504  */
505  if (!slow &&
506  off == att_align_nominal(off, thisatt->attalign))
507  thisatt->attcacheoff = off;
508  else
509  {
510  off = att_align_pointer(off, thisatt->attalign, -1,
511  tp + off);
512  slow = true;
513  }
514  }
515  else
516  {
517  /* not varlena, so safe to use att_align_nominal */
518  off = att_align_nominal(off, thisatt->attalign);
519 
520  if (!slow)
521  thisatt->attcacheoff = off;
522  }
523 
524  values[attnum] = fetchatt(thisatt, tp + off);
525 
526  off = att_addlength_pointer(off, thisatt->attlen, tp + off);
527 
528  if (thisatt->attlen <= 0)
529  slow = true; /* can't use attcacheoff anymore */
530  }
531 }
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:148
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:25
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define fetchatt(A, T)
Definition: tupmacs.h:41
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:176
uintptr_t Datum
Definition: postgres.h:411
#define att_align_pointer(cur_offset, attalign, attlen, attptr)
Definition: tupmacs.h:126
int16 attnum
Definition: pg_attribute.h:83
#define Assert(condition)
Definition: c.h:804
#define INDEX_MAX_KEYS
static Datum values[MAXATTR]
Definition: bootstrap.c:166

◆ index_form_tuple()

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

Definition at line 47 of file indextuple.c.

References Assert, CompressionMethodIsValid, DatumGetPointer, detoast_external_attr(), ereport, errcode(), errmsg(), ERROR, GetDefaultToastCompression(), 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 == TYPSTORAGE_EXTENDED ||
104  att->attstorage == TYPSTORAGE_MAIN))
105  {
106  Datum cvalue;
107  char compression = att->attcompression;
108 
109  /*
110  * If the compression method is not valid, use the default. We
111  * don't expect this to happen for regular index columns, which
112  * inherit the setting from the corresponding table column, but
113  * we do expect it to happen whenever an expression is indexed.
114  */
115  if (!CompressionMethodIsValid(compression))
116  compression = GetDefaultToastCompression();
117 
118  cvalue = toast_compress_datum(untoasted_values[i], compression);
119 
120  if (DatumGetPointer(cvalue) != NULL)
121  {
122  /* successful compression */
123  if (untoasted_free[i])
124  pfree(DatumGetPointer(untoasted_values[i]));
125  untoasted_values[i] = cvalue;
126  untoasted_free[i] = true;
127  }
128  }
129  }
130 #endif
131 
132  for (i = 0; i < numberOfAttributes; i++)
133  {
134  if (isnull[i])
135  {
136  hasnull = true;
137  break;
138  }
139  }
140 
141  if (hasnull)
142  infomask |= INDEX_NULL_MASK;
143 
144  hoff = IndexInfoFindDataOffset(infomask);
145 #ifdef TOAST_INDEX_HACK
146  data_size = heap_compute_data_size(tupleDescriptor,
147  untoasted_values, isnull);
148 #else
149  data_size = heap_compute_data_size(tupleDescriptor,
150  values, isnull);
151 #endif
152  size = hoff + data_size;
153  size = MAXALIGN(size); /* be conservative */
154 
155  tp = (char *) palloc0(size);
156  tuple = (IndexTuple) tp;
157 
158  heap_fill_tuple(tupleDescriptor,
159 #ifdef TOAST_INDEX_HACK
160  untoasted_values,
161 #else
162  values,
163 #endif
164  isnull,
165  (char *) tp + hoff,
166  data_size,
167  &tupmask,
168  (hasnull ? (bits8 *) tp + sizeof(IndexTupleData) : NULL));
169 
170 #ifdef TOAST_INDEX_HACK
171  for (i = 0; i < numberOfAttributes; i++)
172  {
173  if (untoasted_free[i])
174  pfree(DatumGetPointer(untoasted_values[i]));
175  }
176 #endif
177 
178  /*
179  * We do this because heap_fill_tuple wants to initialize a "tupmask"
180  * which is used for HeapTuples, but we want an indextuple infomask. The
181  * only relevant info is the "has variable attributes" field. We have
182  * already set the hasnull bit above.
183  */
184  if (tupmask & HEAP_HASVARWIDTH)
185  infomask |= INDEX_VAR_MASK;
186 
187  /* Also assert we got rid of external attributes */
188 #ifdef TOAST_INDEX_HACK
189  Assert((tupmask & HEAP_HASEXTERNAL) == 0);
190 #endif
191 
192  /*
193  * Here we make sure that the size will fit in the field reserved for it
194  * in t_info.
195  */
196  if ((size & INDEX_SIZE_MASK) != size)
197  ereport(ERROR,
198  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
199  errmsg("index row requires %zu bytes, maximum size is %zu",
200  size, (Size) INDEX_SIZE_MASK)));
201 
202  infomask |= size;
203 
204  /*
205  * initialize metadata
206  */
207  tuple->t_info = infomask;
208  return tuple;
209 }
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
Datum toast_compress_datum(Datum value, char cmethod)
#define CompressionMethodIsValid(cm)
#define VARSIZE(PTR)
Definition: postgres.h:316
#define PointerGetDatum(X)
Definition: postgres.h:600
#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:698
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:326
static char GetDefaultToastCompression(void)
unsigned short uint16
Definition: c.h:440
void pfree(void *pointer)
Definition: mcxt.c:1169
#define TOAST_INDEX_TARGET
Definition: heaptoast.h:68
#define ERROR
Definition: elog.h:46
#define INDEX_VAR_MASK
Definition: itup.h:68
IndexTupleData * IndexTuple
Definition: itup.h:53
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
#define INDEX_NULL_MASK
Definition: itup.h:69
#define HEAP_HASVARWIDTH
Definition: htup_details.h:190
uint8 bits8
Definition: c.h:448
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
#define TOAST_INDEX_HACK
Definition: indextuple.c:29
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
#define INDEX_MAX_KEYS
size_t Size
Definition: c.h:540
#define MAXALIGN(LEN)
Definition: c.h:757
Size heap_compute_data_size(TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:119
#define VARATT_IS_EXTENDED(PTR)
Definition: postgres.h:340
#define DatumGetPointer(X)
Definition: postgres.h:593
static Datum values[MAXATTR]
Definition: bootstrap.c:166
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
#define HEAP_HASEXTERNAL
Definition: htup_details.h:191
Definition: c.h:621
unsigned short t_info
Definition: itup.h:49

◆ index_truncate_tuple()

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

Definition at line 566 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().

568 {
569  TupleDesc truncdesc;
571  bool isnull[INDEX_MAX_KEYS];
572  IndexTuple truncated;
573 
574  Assert(leavenatts <= sourceDescriptor->natts);
575 
576  /* Easy case: no truncation actually required */
577  if (leavenatts == sourceDescriptor->natts)
578  return CopyIndexTuple(source);
579 
580  /* Create temporary descriptor to scribble on */
581  truncdesc = palloc(TupleDescSize(sourceDescriptor));
582  TupleDescCopy(truncdesc, sourceDescriptor);
583  truncdesc->natts = leavenatts;
584 
585  /* Deform, form copy of tuple with fewer attributes */
586  index_deform_tuple(source, truncdesc, values, isnull);
587  truncated = index_form_tuple(truncdesc, values, isnull);
588  truncated->t_tid = source->t_tid;
589  Assert(IndexTupleSize(truncated) <= IndexTupleSize(source));
590 
591  /*
592  * Cannot leak memory here, TupleDescCopy() doesn't allocate any inner
593  * structure, so, plain pfree() should clean all allocated memory
594  */
595  pfree(truncdesc);
596 
597  return truncated;
598 }
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:1169
IndexTuple CopyIndexTuple(IndexTuple source)
Definition: indextuple.c:537
void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:446
uintptr_t Datum
Definition: postgres.h:411
#define Assert(condition)
Definition: c.h:804
#define INDEX_MAX_KEYS
static Datum values[MAXATTR]
Definition: bootstrap.c:166
void TupleDescCopy(TupleDesc dst, TupleDesc src)
Definition: tupdesc.c:229
void * palloc(Size size)
Definition: mcxt.c:1062
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ nocache_index_getattr()

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

Definition at line 231 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.

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