PostgreSQL Source Code  git master
indextuple.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * indextuple.c
4  * This file contains index tuple accessor and mutator routines,
5  * as well as various tuple utilities.
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *
11  * IDENTIFICATION
12  * src/backend/access/common/indextuple.c
13  *
14  *-------------------------------------------------------------------------
15  */
16 
17 #include "postgres.h"
18 
19 #include "access/heapam.h"
20 #include "access/itup.h"
21 #include "access/tuptoaster.h"
22 
23 
24 /* ----------------------------------------------------------------
25  * index_ tuple interface routines
26  * ----------------------------------------------------------------
27  */
28 
29 /* ----------------
30  * index_form_tuple
31  *
32  * This shouldn't leak any memory; otherwise, callers such as
33  * tuplesort_putindextuplevalues() will be very unhappy.
34  * ----------------
35  */
37 index_form_tuple(TupleDesc tupleDescriptor,
38  Datum *values,
39  bool *isnull)
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 = TupleDescAttr(tupleDescriptor, 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  */
79  if (VARATT_IS_EXTERNAL(DatumGetPointer(values[i])))
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 }
187 
188 /* ----------------
189  * nocache_index_getattr
190  *
191  * This gets called from index_getattr() macro, and only in cases
192  * where we can't use cacheoffset and the value is not null.
193  *
194  * This caches attribute offsets in the attribute descriptor.
195  *
196  * An alternative way to speed things up would be to cache offsets
197  * with the tuple, but that seems more difficult unless you take
198  * the storage hit of actually putting those offsets into the
199  * tuple you send to disk. Yuck.
200  *
201  * This scheme will be slightly slower than that, but should
202  * perform well for queries which hit large #'s of tuples. After
203  * you cache the offsets once, examining all the other tuples using
204  * the same attribute descriptor will go much quicker. -cim 5/4/91
205  * ----------------
206  */
207 Datum
209  int attnum,
211 {
212  char *tp; /* ptr to data part of tuple */
213  bits8 *bp = NULL; /* ptr to null bitmap in tuple */
214  bool slow = false; /* do we have to walk attrs? */
215  int data_off; /* tuple data offset */
216  int off; /* current offset within data */
217 
218  /* ----------------
219  * Three cases:
220  *
221  * 1: No nulls and no variable-width attributes.
222  * 2: Has a null or a var-width AFTER att.
223  * 3: Has nulls or var-widths BEFORE att.
224  * ----------------
225  */
226 
227  data_off = IndexInfoFindDataOffset(tup->t_info);
228 
229  attnum--;
230 
231  if (IndexTupleHasNulls(tup))
232  {
233  /*
234  * there's a null somewhere in the tuple
235  *
236  * check to see if desired att is null
237  */
238 
239  /* XXX "knows" t_bits are just after fixed tuple header! */
240  bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
241 
242  /*
243  * Now check to see if any preceding bits are null...
244  */
245  {
246  int byte = attnum >> 3;
247  int finalbit = attnum & 0x07;
248 
249  /* check for nulls "before" final bit of last byte */
250  if ((~bp[byte]) & ((1 << finalbit) - 1))
251  slow = true;
252  else
253  {
254  /* check for nulls in any "earlier" bytes */
255  int i;
256 
257  for (i = 0; i < byte; i++)
258  {
259  if (bp[i] != 0xFF)
260  {
261  slow = true;
262  break;
263  }
264  }
265  }
266  }
267  }
268 
269  tp = (char *) tup + data_off;
270 
271  if (!slow)
272  {
273  Form_pg_attribute att;
274 
275  /*
276  * If we get here, there are no nulls up to and including the target
277  * attribute. If we have a cached offset, we can use it.
278  */
279  att = TupleDescAttr(tupleDesc, attnum);
280  if (att->attcacheoff >= 0)
281  return fetchatt(att, tp + att->attcacheoff);
282 
283  /*
284  * Otherwise, check for non-fixed-length attrs up to and including
285  * target. If there aren't any, it's safe to cheaply initialize the
286  * cached offsets for these attrs.
287  */
288  if (IndexTupleHasVarwidths(tup))
289  {
290  int j;
291 
292  for (j = 0; j <= attnum; j++)
293  {
294  if (TupleDescAttr(tupleDesc, j)->attlen <= 0)
295  {
296  slow = true;
297  break;
298  }
299  }
300  }
301  }
302 
303  if (!slow)
304  {
305  int natts = tupleDesc->natts;
306  int j = 1;
307 
308  /*
309  * If we get here, we have a tuple with no nulls or var-widths up to
310  * and including the target attribute, so we can use the cached offset
311  * ... only we don't have it yet, or we'd not have got here. Since
312  * it's cheap to compute offsets for fixed-width columns, we take the
313  * opportunity to initialize the cached offsets for *all* the leading
314  * fixed-width columns, in hope of avoiding future visits to this
315  * routine.
316  */
317  TupleDescAttr(tupleDesc, 0)->attcacheoff = 0;
318 
319  /* we might have set some offsets in the slow path previously */
320  while (j < natts && TupleDescAttr(tupleDesc, j)->attcacheoff > 0)
321  j++;
322 
323  off = TupleDescAttr(tupleDesc, j - 1)->attcacheoff +
324  TupleDescAttr(tupleDesc, j - 1)->attlen;
325 
326  for (; j < natts; j++)
327  {
328  Form_pg_attribute att = TupleDescAttr(tupleDesc, j);
329 
330  if (att->attlen <= 0)
331  break;
332 
333  off = att_align_nominal(off, att->attalign);
334 
335  att->attcacheoff = off;
336 
337  off += att->attlen;
338  }
339 
340  Assert(j > attnum);
341 
342  off = TupleDescAttr(tupleDesc, attnum)->attcacheoff;
343  }
344  else
345  {
346  bool usecache = true;
347  int i;
348 
349  /*
350  * Now we know that we have to walk the tuple CAREFULLY. But we still
351  * might be able to cache some offsets for next time.
352  *
353  * Note - This loop is a little tricky. For each non-null attribute,
354  * we have to first account for alignment padding before the attr,
355  * then advance over the attr based on its length. Nulls have no
356  * storage and no alignment padding either. We can use/set
357  * attcacheoff until we reach either a null or a var-width attribute.
358  */
359  off = 0;
360  for (i = 0;; i++) /* loop exit is at "break" */
361  {
362  Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
363 
364  if (IndexTupleHasNulls(tup) && att_isnull(i, bp))
365  {
366  usecache = false;
367  continue; /* this cannot be the target att */
368  }
369 
370  /* If we know the next offset, we can skip the rest */
371  if (usecache && att->attcacheoff >= 0)
372  off = att->attcacheoff;
373  else if (att->attlen == -1)
374  {
375  /*
376  * We can only cache the offset for a varlena attribute if the
377  * offset is already suitably aligned, so that there would be
378  * no pad bytes in any case: then the offset will be valid for
379  * either an aligned or unaligned value.
380  */
381  if (usecache &&
382  off == att_align_nominal(off, att->attalign))
383  att->attcacheoff = off;
384  else
385  {
386  off = att_align_pointer(off, att->attalign, -1,
387  tp + off);
388  usecache = false;
389  }
390  }
391  else
392  {
393  /* not varlena, so safe to use att_align_nominal */
394  off = att_align_nominal(off, att->attalign);
395 
396  if (usecache)
397  att->attcacheoff = off;
398  }
399 
400  if (i == attnum)
401  break;
402 
403  off = att_addlength_pointer(off, att->attlen, tp + off);
404 
405  if (usecache && att->attlen <= 0)
406  usecache = false;
407  }
408  }
409 
410  return fetchatt(TupleDescAttr(tupleDesc, attnum), tp + off);
411 }
412 
413 /*
414  * Convert an index tuple into Datum/isnull arrays.
415  *
416  * The caller must allocate sufficient storage for the output arrays.
417  * (INDEX_MAX_KEYS entries should be enough.)
418  */
419 void
421  Datum *values, bool *isnull)
422 {
423  int i;
424 
425  /* Assert to protect callers who allocate fixed-size arrays */
426  Assert(tupleDescriptor->natts <= INDEX_MAX_KEYS);
427 
428  for (i = 0; i < tupleDescriptor->natts; i++)
429  {
430  values[i] = index_getattr(tup, i + 1, tupleDescriptor, &isnull[i]);
431  }
432 }
433 
434 /*
435  * Create a palloc'd copy of an index tuple.
436  */
439 {
440  IndexTuple result;
441  Size size;
442 
443  size = IndexTupleSize(source);
444  result = (IndexTuple) palloc(size);
445  memcpy(result, source, size);
446  return result;
447 }
#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:145
#define IndexInfoFindDataOffset(t_info)
Definition: itup.h:80
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:144
#define VARSIZE(PTR)
Definition: postgres.h:304
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:21
#define PointerGetDatum(X)
Definition: postgres.h:562
struct varlena * heap_tuple_fetch_attr(struct varlena *attr)
Definition: tuptoaster.c:101
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
#define INDEX_SIZE_MASK
Definition: itup.h:65
int errcode(int sqlerrcode)
Definition: elog.c:575
#define IndexTupleHasNulls(itup)
Definition: itup.h:72
int natts
Definition: tupdesc.h:79
#define fetchatt(A, T)
Definition: tupmacs.h:37
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:314
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:37
unsigned short uint16
Definition: c.h:305
void pfree(void *pointer)
Definition: mcxt.c:936
#define ERROR
Definition: elog.h:43
Datum toast_compress_datum(Datum value)
Definition: tuptoaster.c:1371
#define INDEX_VAR_MASK
Definition: itup.h:67
IndexTuple CopyIndexTuple(IndexTuple source)
Definition: indextuple.c:438
IndexTupleData * IndexTuple
Definition: itup.h:53
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:420
#define INDEX_NULL_MASK
Definition: itup.h:68
#define HEAP_HASVARWIDTH
Definition: htup_details.h:181
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:172
#define ereport(elevel, rest)
Definition: elog.h:122
#define byte(x, n)
Definition: rijndael.c:68
Datum nocache_index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc)
Definition: indextuple.c:208
uint8 bits8
Definition: c.h:313
void * palloc0(Size size)
Definition: mcxt.c:864
uintptr_t Datum
Definition: postgres.h:372
struct IndexTupleData IndexTupleData
#define att_align_pointer(cur_offset, attalign, attlen, attptr)
Definition: tupmacs.h:122
#define TOAST_INDEX_TARGET
Definition: tuptoaster.h:75
#define Assert(condition)
Definition: c.h:680
#define INDEX_MAX_KEYS
size_t Size
Definition: c.h:414
#define MAXALIGN(LEN)
Definition: c.h:633
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
#define IndexTupleHasVarwidths(itup)
Definition: itup.h:73
Size heap_compute_data_size(TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:85
#define VARATT_IS_EXTENDED(PTR)
Definition: postgres.h:326
#define DatumGetPointer(X)
Definition: postgres.h:555
static Datum values[MAXATTR]
Definition: bootstrap.c:164
void * palloc(Size size)
Definition: mcxt.c:835
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define HEAP_HASEXTERNAL
Definition: htup_details.h:182
Definition: c.h:497
unsigned short t_info
Definition: itup.h:49
#define IndexTupleSize(itup)
Definition: itup.h:70