PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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_VAR_MASK   0x4000
 
#define INDEX_NULL_MASK   0x8000
 
#define IndexTupleSize(itup)   ((Size) (((IndexTuple) (itup))->t_info & INDEX_SIZE_MASK))
 
#define IndexTupleDSize(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 MinIndexTupleSize   MAXALIGN(sizeof(IndexTupleData) + 1)
 
#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)
 

Macro Definition Documentation

#define index_getattr (   tup,
  attnum,
  tupleDesc,
  isnull 
)
Value:
( \
AssertMacro(PointerIsValid(isnull) && (attnum) > 0), \
*(isnull) = false, \
( \
(tupleDesc)->attrs[(attnum)-1]->attcacheoff >= 0 ? \
( \
fetchatt((tupleDesc)->attrs[(attnum)-1], \
(char *) (tup) + IndexInfoFindDataOffset((tup)->t_info) \
+ (tupleDesc)->attrs[(attnum)-1]->attcacheoff) \
) \
: \
nocache_index_getattr((tup), (attnum), (tupleDesc)) \
) \
: \
( \
(att_isnull((attnum)-1, (char *)(tup) + sizeof(IndexTupleData))) ? \
( \
*(isnull) = true, \
) \
: \
( \
nocache_index_getattr((tup), (attnum), (tupleDesc)) \
) \
) \
)
#define IndexInfoFindDataOffset(t_info)
Definition: itup.h:80
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:21
#define IndexTupleHasNulls(itup)
Definition: itup.h:72
#define fetchatt(A, T)
Definition: tupmacs.h:37
#define AssertMacro(condition)
Definition: c.h:649
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:202
Datum nocache_index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc)
Definition: indextuple.c:208
#define PointerIsValid(pointer)
Definition: c.h:502

Definition at line 100 of file itup.h.

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

#define INDEX_NULL_MASK   0x8000

Definition at line 68 of file itup.h.

Referenced by index_form_tuple(), and spgFormNodeTuple().

#define INDEX_SIZE_MASK   0x1FFF

Definition at line 65 of file itup.h.

Referenced by GinFormInteriorTuple(), GinFormTuple(), index_form_tuple(), and spgFormNodeTuple().

#define INDEX_VAR_MASK   0x4000

Definition at line 67 of file itup.h.

Referenced by index_form_tuple().

#define IndexInfoFindDataOffset (   t_info)
Value:
( \
(!((t_info) & INDEX_NULL_MASK)) ? \
( \
) \
: \
( \
) \
)
#define INDEX_NULL_MASK
Definition: itup.h:68
size_t Size
Definition: c.h:333
#define MAXALIGN(LEN)
Definition: c.h:561

Definition at line 80 of file itup.h.

Referenced by _hash_get_indextuple_hashkey(), bt_page_items(), index_form_tuple(), and nocache_index_getattr().

#define IndexTupleDSize (   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))

Definition at line 73 of file itup.h.

Referenced by bt_page_items(), and nocache_index_getattr().

#define MaxIndexTuplesPerPage
Value:
((int) ((BLCKSZ - SizeOfPageHeaderData) / \
(MAXALIGN(sizeof(IndexTupleData) + 1) + sizeof(ItemIdData))))
#define SizeOfPageHeaderData
Definition: bufpage.h:213
struct ItemIdData ItemIdData
#define MAXALIGN(LEN)
Definition: c.h:561

Definition at line 137 of file itup.h.

Referenced by _bt_readpage(), _bt_restore_page(), btgettuple(), PageIndexMultiDelete(), spgPageIndexMultiDelete(), storeGettuple(), vacuumLeafPage(), vacuumLeafRoot(), and vacuumRedirectAndPlaceholder().

#define MinIndexTupleSize   MAXALIGN(sizeof(IndexTupleData) + 1)

Definition at line 136 of file itup.h.

Typedef Documentation

Definition at line 53 of file itup.h.

Function Documentation

IndexTuple CopyIndexTuple ( IndexTuple  source)

Definition at line 434 of file indextuple.c.

References IndexTupleSize, and palloc().

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

435 {
436  IndexTuple result;
437  Size size;
438 
439  size = IndexTupleSize(source);
440  result = (IndexTuple) palloc(size);
441  memcpy(result, source, size);
442  return result;
443 }
IndexTupleData * IndexTuple
Definition: itup.h:53
size_t Size
Definition: c.h:333
void * palloc(Size size)
Definition: mcxt.c:812
#define IndexTupleSize(itup)
Definition: itup.h:70
void index_deform_tuple ( IndexTuple  tup,
TupleDesc  tupleDescriptor,
Datum values,
bool isnull 
)

Definition at line 416 of file indextuple.c.

References Assert, i, index_getattr, INDEX_MAX_KEYS, and tupleDesc::natts.

Referenced by _bt_check_unique(), and comparetup_index_btree().

418 {
419  int i;
420 
421  /* Assert to protect callers who allocate fixed-size arrays */
422  Assert(tupleDescriptor->natts <= INDEX_MAX_KEYS);
423 
424  for (i = 0; i < tupleDescriptor->natts; i++)
425  {
426  values[i] = index_getattr(tup, i + 1, tupleDescriptor, &isnull[i]);
427  }
428 }
int natts
Definition: tupdesc.h:73
#define Assert(condition)
Definition: c.h:648
#define INDEX_MAX_KEYS
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
static Datum values[MAXATTR]
Definition: bootstrap.c:159
int i
IndexTuple index_form_tuple ( TupleDesc  tupleDescriptor,
Datum values,
bool isnull 
)

Definition at line 37 of file indextuple.c.

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

Referenced by _hash_form_tuple(), btinsert(), GinFormTuple(), gistFetchTuple(), gistFormTuple(), storeGettuple(), and tuplesort_putindextuplevalues().

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

Definition at line 208 of file indextuple.c.

References Assert, att_addlength_pointer, att_align_nominal, att_align_pointer, att_isnull, tupleDesc::attrs, byte, fetchatt, i, IndexInfoFindDataOffset, IndexTupleHasNulls, IndexTupleHasVarwidths, tupleDesc::natts, NULL, and IndexTupleData::t_info.

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