Line data Source code
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 : */
36 : IndexTuple
37 1134218 : 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 1134218 : unsigned short infomask = 0;
48 1134218 : bool hasnull = false;
49 1134218 : uint16 tupmask = 0;
50 1134218 : 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 1134218 : if (numberOfAttributes > INDEX_MAX_KEYS)
58 0 : 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 2660318 : for (i = 0; i < numberOfAttributes; i++)
65 : {
66 1526100 : Form_pg_attribute att = TupleDescAttr(tupleDescriptor, i);
67 :
68 1526100 : untoasted_values[i] = values[i];
69 1526100 : untoasted_free[i] = false;
70 :
71 : /* Do nothing if value is NULL or not of varlena type */
72 1526100 : if (isnull[i] || att->attlen != -1)
73 1425742 : 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 100358 : if (VARATT_IS_EXTERNAL(DatumGetPointer(values[i])))
80 : {
81 0 : untoasted_values[i] =
82 0 : PointerGetDatum(heap_tuple_fetch_attr((struct varlena *)
83 : DatumGetPointer(values[i])));
84 0 : 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 113509 : if (!VARATT_IS_EXTENDED(DatumGetPointer(untoasted_values[i])) &&
92 13251 : VARSIZE(DatumGetPointer(untoasted_values[i])) > TOAST_INDEX_TARGET &&
93 100 : (att->attstorage == 'x' || att->attstorage == 'm'))
94 : {
95 100 : Datum cvalue = toast_compress_datum(untoasted_values[i]);
96 :
97 100 : if (DatumGetPointer(cvalue) != NULL)
98 : {
99 : /* successful compression */
100 0 : if (untoasted_free[i])
101 0 : pfree(DatumGetPointer(untoasted_values[i]));
102 0 : untoasted_values[i] = cvalue;
103 0 : untoasted_free[i] = true;
104 : }
105 : }
106 : }
107 : #endif
108 :
109 2658138 : for (i = 0; i < numberOfAttributes; i++)
110 : {
111 1526095 : if (isnull[i])
112 : {
113 2175 : hasnull = true;
114 2175 : break;
115 : }
116 : }
117 :
118 1134218 : if (hasnull)
119 2175 : infomask |= INDEX_NULL_MASK;
120 :
121 1134218 : hoff = IndexInfoFindDataOffset(infomask);
122 : #ifdef TOAST_INDEX_HACK
123 1134218 : 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 1134218 : size = hoff + data_size;
130 1134218 : size = MAXALIGN(size); /* be conservative */
131 :
132 1134218 : tp = (char *) palloc0(size);
133 1134218 : tuple = (IndexTuple) tp;
134 :
135 1134218 : 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 2660318 : for (i = 0; i < numberOfAttributes; i++)
149 : {
150 1526100 : if (untoasted_free[i])
151 0 : 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 1134218 : if (tupmask & HEAP_HASVARWIDTH)
162 141298 : infomask |= INDEX_VAR_MASK;
163 :
164 : /* Also assert we got rid of external attributes */
165 : #ifdef TOAST_INDEX_HACK
166 1134218 : 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 1134218 : if ((size & INDEX_SIZE_MASK) != size)
174 0 : 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 1134218 : infomask |= size;
180 :
181 : /*
182 : * initialize metadata
183 : */
184 1134218 : tuple->t_info = infomask;
185 1134218 : 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
208 198584 : nocache_index_getattr(IndexTuple tup,
209 : int attnum,
210 : TupleDesc tupleDesc)
211 : {
212 : char *tp; /* ptr to data part of tuple */
213 198584 : bits8 *bp = NULL; /* ptr to null bitmap in tuple */
214 198584 : 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 198584 : data_off = IndexInfoFindDataOffset(tup->t_info);
228 :
229 198584 : attnum--;
230 :
231 198584 : 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 2239 : bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
241 :
242 : /*
243 : * Now check to see if any preceding bits are null...
244 : */
245 : {
246 2239 : int byte = attnum >> 3;
247 2239 : int finalbit = attnum & 0x07;
248 :
249 : /* check for nulls "before" final bit of last byte */
250 2239 : if ((~bp[byte]) & ((1 << finalbit) - 1))
251 1 : slow = true;
252 : else
253 : {
254 : /* check for nulls in any "earlier" bytes */
255 : int i;
256 :
257 2238 : for (i = 0; i < byte; i++)
258 : {
259 0 : if (bp[i] != 0xFF)
260 : {
261 0 : slow = true;
262 0 : break;
263 : }
264 : }
265 : }
266 : }
267 : }
268 :
269 198584 : tp = (char *) tup + data_off;
270 :
271 198584 : 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 198583 : att = TupleDescAttr(tupleDesc, attnum);
280 198583 : if (att->attcacheoff >= 0)
281 2238 : 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 196345 : if (IndexTupleHasVarwidths(tup))
289 : {
290 : int j;
291 :
292 290233 : for (j = 0; j <= attnum; j++)
293 : {
294 290223 : if (TupleDescAttr(tupleDesc, j)->attlen <= 0)
295 : {
296 194150 : slow = true;
297 194150 : break;
298 : }
299 : }
300 : }
301 : }
302 :
303 196346 : if (!slow)
304 : {
305 2195 : int natts = tupleDesc->natts;
306 2195 : 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 2195 : TupleDescAttr(tupleDesc, 0)->attcacheoff = 0;
318 :
319 : /* we might have set some offsets in the slow path previously */
320 4390 : while (j < natts && TupleDescAttr(tupleDesc, j)->attcacheoff > 0)
321 0 : j++;
322 :
323 4390 : off = TupleDescAttr(tupleDesc, j - 1)->attcacheoff +
324 2195 : TupleDescAttr(tupleDesc, j - 1)->attlen;
325 :
326 5756 : for (; j < natts; j++)
327 : {
328 3564 : Form_pg_attribute att = TupleDescAttr(tupleDesc, j);
329 :
330 3564 : if (att->attlen <= 0)
331 3 : break;
332 :
333 3561 : off = att_align_nominal(off, att->attalign);
334 :
335 3561 : att->attcacheoff = off;
336 :
337 3561 : off += att->attlen;
338 : }
339 :
340 2195 : Assert(j > attnum);
341 :
342 2195 : off = TupleDescAttr(tupleDesc, attnum)->attcacheoff;
343 : }
344 : else
345 : {
346 194151 : 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 194151 : off = 0;
360 492026 : for (i = 0;; i++) /* loop exit is at "break" */
361 : {
362 492026 : Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
363 :
364 492026 : if (IndexTupleHasNulls(tup) && att_isnull(i, bp))
365 : {
366 1 : usecache = false;
367 1 : continue; /* this cannot be the target att */
368 : }
369 :
370 : /* If we know the next offset, we can skip the rest */
371 492025 : if (usecache && att->attcacheoff >= 0)
372 288630 : off = att->attcacheoff;
373 203395 : 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 65154 : if (usecache &&
382 1279 : off == att_align_nominal(off, att->attalign))
383 20 : att->attcacheoff = off;
384 : else
385 : {
386 63855 : off = att_align_pointer(off, att->attalign, -1,
387 : tp + off);
388 63855 : usecache = false;
389 : }
390 : }
391 : else
392 : {
393 : /* not varlena, so safe to use att_align_nominal */
394 139520 : off = att_align_nominal(off, att->attalign);
395 :
396 139520 : if (usecache)
397 304 : att->attcacheoff = off;
398 : }
399 :
400 492025 : if (i == attnum)
401 194151 : break;
402 :
403 297874 : off = att_addlength_pointer(off, att->attlen, tp + off);
404 :
405 297874 : if (usecache && att->attlen <= 0)
406 192575 : usecache = false;
407 297875 : }
408 : }
409 :
410 196346 : 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
420 38 : index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor,
421 : Datum *values, bool *isnull)
422 : {
423 : int i;
424 :
425 : /* Assert to protect callers who allocate fixed-size arrays */
426 38 : Assert(tupleDescriptor->natts <= INDEX_MAX_KEYS);
427 :
428 81 : for (i = 0; i < tupleDescriptor->natts; i++)
429 : {
430 43 : values[i] = index_getattr(tup, i + 1, tupleDescriptor, &isnull[i]);
431 : }
432 38 : }
433 :
434 : /*
435 : * Create a palloc'd copy of an index tuple.
436 : */
437 : IndexTuple
438 19873 : CopyIndexTuple(IndexTuple source)
439 : {
440 : IndexTuple result;
441 : Size size;
442 :
443 19873 : size = IndexTupleSize(source);
444 19873 : result = (IndexTuple) palloc(size);
445 19873 : memcpy(result, source, size);
446 19873 : return result;
447 : }
|