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-2008, PostgreSQL Global Development Group
8 : * Portions Copyright (c) 1994, Regents of the University of California
9 : *
10 : *
11 : * IDENTIFICATION
12 : * $PostgreSQL: pgsql/src/backend/access/common/indextuple.c,v 1.84 2007/11/15 21:14:31 momjian Exp $
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 : */
33 : IndexTuple
34 : index_form_tuple(TupleDesc tupleDescriptor,
35 : Datum *values,
36 : bool *isnull)
37 298649 : {
38 : char *tp; /* tuple pointer */
39 : IndexTuple tuple; /* return tuple */
40 : Size size,
41 : data_size,
42 : hoff;
43 : int i;
44 298649 : unsigned short infomask = 0;
45 298649 : bool hasnull = false;
46 298649 : uint16 tupmask = 0;
47 298649 : int numberOfAttributes = tupleDescriptor->natts;
48 :
49 : #ifdef TOAST_INDEX_HACK
50 : Datum untoasted_values[INDEX_MAX_KEYS];
51 : bool untoasted_free[INDEX_MAX_KEYS];
52 : #endif
53 :
54 298649 : if (numberOfAttributes > INDEX_MAX_KEYS)
55 0 : ereport(ERROR,
56 : (errcode(ERRCODE_TOO_MANY_COLUMNS),
57 : errmsg("number of index columns (%d) exceeds limit (%d)",
58 : numberOfAttributes, INDEX_MAX_KEYS)));
59 :
60 : #ifdef TOAST_INDEX_HACK
61 708571 : for (i = 0; i < numberOfAttributes; i++)
62 : {
63 409922 : Form_pg_attribute att = tupleDescriptor->attrs[i];
64 :
65 409922 : untoasted_values[i] = values[i];
66 409922 : untoasted_free[i] = false;
67 :
68 : /* Do nothing if value is NULL or not of varlena type */
69 409922 : if (isnull[i] || att->attlen != -1)
70 : continue;
71 :
72 : /*
73 : * If value is stored EXTERNAL, must fetch it so we are not depending
74 : * on outside storage. This should be improved someday.
75 : */
76 32072 : if (VARATT_IS_EXTERNAL(values[i]))
77 : {
78 0 : untoasted_values[i] =
79 : PointerGetDatum(heap_tuple_fetch_attr((struct varlena *)
80 : DatumGetPointer(values[i])));
81 0 : untoasted_free[i] = true;
82 : }
83 :
84 : /*
85 : * If value is above size target, and is of a compressible datatype,
86 : * try to compress it in-line.
87 : */
88 32072 : if (!VARATT_IS_EXTENDED(untoasted_values[i]) &&
89 : VARSIZE(untoasted_values[i]) > TOAST_INDEX_TARGET &&
90 : (att->attstorage == 'x' || att->attstorage == 'm'))
91 : {
92 0 : Datum cvalue = toast_compress_datum(untoasted_values[i]);
93 :
94 0 : if (DatumGetPointer(cvalue) != NULL)
95 : {
96 : /* successful compression */
97 0 : if (untoasted_free[i])
98 0 : pfree(DatumGetPointer(untoasted_values[i]));
99 0 : untoasted_values[i] = cvalue;
100 0 : untoasted_free[i] = true;
101 : }
102 : }
103 : }
104 : #endif
105 :
106 707699 : for (i = 0; i < numberOfAttributes; i++)
107 : {
108 409917 : if (isnull[i])
109 : {
110 867 : hasnull = true;
111 867 : break;
112 : }
113 : }
114 :
115 298649 : if (hasnull)
116 867 : infomask |= INDEX_NULL_MASK;
117 :
118 298649 : hoff = IndexInfoFindDataOffset(infomask);
119 : #ifdef TOAST_INDEX_HACK
120 298649 : data_size = heap_compute_data_size(tupleDescriptor,
121 : untoasted_values, isnull);
122 : #else
123 : data_size = heap_compute_data_size(tupleDescriptor,
124 : values, isnull);
125 : #endif
126 298649 : size = hoff + data_size;
127 298649 : size = MAXALIGN(size); /* be conservative */
128 :
129 298649 : tp = (char *) palloc0(size);
130 298649 : tuple = (IndexTuple) tp;
131 :
132 298649 : heap_fill_tuple(tupleDescriptor,
133 : #ifdef TOAST_INDEX_HACK
134 : untoasted_values,
135 : #else
136 : values,
137 : #endif
138 : isnull,
139 : (char *) tp + hoff,
140 : data_size,
141 : &tupmask,
142 : (hasnull ? (bits8 *) tp + sizeof(IndexTupleData) : NULL));
143 :
144 : #ifdef TOAST_INDEX_HACK
145 708571 : for (i = 0; i < numberOfAttributes; i++)
146 : {
147 409922 : if (untoasted_free[i])
148 0 : pfree(DatumGetPointer(untoasted_values[i]));
149 : }
150 : #endif
151 :
152 : /*
153 : * We do this because heap_fill_tuple wants to initialize a "tupmask"
154 : * which is used for HeapTuples, but we want an indextuple infomask. The
155 : * only relevant info is the "has variable attributes" field. We have
156 : * already set the hasnull bit above.
157 : */
158 298649 : if (tupmask & HEAP_HASVARWIDTH)
159 32069 : infomask |= INDEX_VAR_MASK;
160 :
161 : /*
162 : * Here we make sure that the size will fit in the field reserved for it
163 : * in t_info.
164 : */
165 298649 : if ((size & INDEX_SIZE_MASK) != size)
166 0 : ereport(ERROR,
167 : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
168 : errmsg("index row requires %lu bytes, maximum size is %lu",
169 : (unsigned long) size,
170 : (unsigned long) INDEX_SIZE_MASK)));
171 :
172 298649 : infomask |= size;
173 :
174 : /*
175 : * initialize metadata
176 : */
177 298649 : tuple->t_info = infomask;
178 298649 : return tuple;
179 : }
180 :
181 : /* ----------------
182 : * nocache_index_getattr
183 : *
184 : * This gets called from index_getattr() macro, and only in cases
185 : * where we can't use cacheoffset and the value is not null.
186 : *
187 : * This caches attribute offsets in the attribute descriptor.
188 : *
189 : * An alternative way to speed things up would be to cache offsets
190 : * with the tuple, but that seems more difficult unless you take
191 : * the storage hit of actually putting those offsets into the
192 : * tuple you send to disk. Yuck.
193 : *
194 : * This scheme will be slightly slower than that, but should
195 : * perform well for queries which hit large #'s of tuples. After
196 : * you cache the offsets once, examining all the other tuples using
197 : * the same attribute descriptor will go much quicker. -cim 5/4/91
198 : * ----------------
199 : */
200 : Datum
201 : nocache_index_getattr(IndexTuple tup,
202 : int attnum,
203 : TupleDesc tupleDesc,
204 : bool *isnull)
205 2059 : {
206 2059 : Form_pg_attribute *att = tupleDesc->attrs;
207 : char *tp; /* ptr to data part of tuple */
208 2059 : bits8 *bp = NULL; /* ptr to null bitmap in tuple */
209 2059 : bool slow = false; /* do we have to walk attrs? */
210 : int data_off; /* tuple data offset */
211 : int off; /* current offset within data */
212 :
213 : (void) isnull; /* not used */
214 :
215 : /* ----------------
216 : * Three cases:
217 : *
218 : * 1: No nulls and no variable-width attributes.
219 : * 2: Has a null or a var-width AFTER att.
220 : * 3: Has nulls or var-widths BEFORE att.
221 : * ----------------
222 : */
223 :
224 : #ifdef IN_MACRO
225 : /* This is handled in the macro */
226 : Assert(PointerIsValid(isnull));
227 : Assert(attnum > 0);
228 :
229 : *isnull = false;
230 : #endif
231 :
232 2059 : data_off = IndexInfoFindDataOffset(tup->t_info);
233 :
234 2059 : attnum--;
235 :
236 2059 : if (!IndexTupleHasNulls(tup))
237 : {
238 : #ifdef IN_MACRO
239 : /* This is handled in the macro */
240 : if (att[attnum]->attcacheoff >= 0)
241 : {
242 : return fetchatt(att[attnum],
243 : (char *) tup + data_off +
244 : att[attnum]->attcacheoff);
245 : }
246 : #endif
247 : }
248 : else
249 : {
250 : /*
251 : * there's a null somewhere in the tuple
252 : *
253 : * check to see if desired att is null
254 : */
255 :
256 : /* XXX "knows" t_bits are just after fixed tuple header! */
257 172 : bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
258 :
259 : #ifdef IN_MACRO
260 : /* This is handled in the macro */
261 :
262 : if (att_isnull(attnum, bp))
263 : {
264 : *isnull = true;
265 : return (Datum) NULL;
266 : }
267 : #endif
268 :
269 : /*
270 : * Now check to see if any preceding bits are null...
271 : */
272 : {
273 172 : int byte = attnum >> 3;
274 172 : int finalbit = attnum & 0x07;
275 :
276 : /* check for nulls "before" final bit of last byte */
277 172 : if ((~bp[byte]) & ((1 << finalbit) - 1))
278 1 : slow = true;
279 : else
280 : {
281 : /* check for nulls in any "earlier" bytes */
282 : int i;
283 :
284 171 : for (i = 0; i < byte; i++)
285 : {
286 0 : if (bp[i] != 0xFF)
287 : {
288 0 : slow = true;
289 0 : break;
290 : }
291 : }
292 : }
293 : }
294 : }
295 :
296 2059 : tp = (char *) tup + data_off;
297 :
298 2059 : if (!slow)
299 : {
300 : /*
301 : * If we get here, there are no nulls up to and including the target
302 : * attribute. If we have a cached offset, we can use it.
303 : */
304 2058 : if (att[attnum]->attcacheoff >= 0)
305 : {
306 171 : return fetchatt(att[attnum],
307 : tp + att[attnum]->attcacheoff);
308 : }
309 :
310 : /*
311 : * Otherwise, check for non-fixed-length attrs up to and including
312 : * target. If there aren't any, it's safe to cheaply initialize the
313 : * cached offsets for these attrs.
314 : */
315 1887 : if (IndexTupleHasVarwidths(tup))
316 : {
317 : int j;
318 :
319 1009 : for (j = 0; j <= attnum; j++)
320 : {
321 1008 : if (att[j]->attlen <= 0)
322 : {
323 522 : slow = true;
324 522 : break;
325 : }
326 : }
327 : }
328 : }
329 :
330 1888 : if (!slow)
331 : {
332 1365 : int natts = tupleDesc->natts;
333 1365 : int j = 1;
334 :
335 : /*
336 : * If we get here, we have a tuple with no nulls or var-widths up to
337 : * and including the target attribute, so we can use the cached offset
338 : * ... only we don't have it yet, or we'd not have got here. Since
339 : * it's cheap to compute offsets for fixed-width columns, we take the
340 : * opportunity to initialize the cached offsets for *all* the leading
341 : * fixed-width columns, in hope of avoiding future visits to this
342 : * routine.
343 : */
344 1365 : att[0]->attcacheoff = 0;
345 :
346 : /* we might have set some offsets in the slow path previously */
347 2730 : while (j < natts && att[j]->attcacheoff > 0)
348 0 : j++;
349 :
350 1365 : off = att[j - 1]->attcacheoff + att[j - 1]->attlen;
351 :
352 3368 : for (; j < natts; j++)
353 : {
354 2004 : if (att[j]->attlen <= 0)
355 1 : break;
356 :
357 2003 : off = att_align_nominal(off, att[j]->attalign);
358 :
359 2003 : att[j]->attcacheoff = off;
360 :
361 2003 : off += att[j]->attlen;
362 : }
363 :
364 : Assert(j > attnum);
365 :
366 1365 : off = att[attnum]->attcacheoff;
367 : }
368 : else
369 : {
370 523 : bool usecache = true;
371 : int i;
372 :
373 : /*
374 : * Now we know that we have to walk the tuple CAREFULLY. But we still
375 : * might be able to cache some offsets for next time.
376 : *
377 : * Note - This loop is a little tricky. For each non-null attribute,
378 : * we have to first account for alignment padding before the attr,
379 : * then advance over the attr based on its length. Nulls have no
380 : * storage and no alignment padding either. We can use/set
381 : * attcacheoff until we reach either a null or a var-width attribute.
382 : */
383 523 : off = 0;
384 1489 : for (i = 0;; i++) /* loop exit is at "break" */
385 : {
386 1489 : if (IndexTupleHasNulls(tup) && att_isnull(i, bp))
387 : {
388 1 : usecache = false;
389 1 : continue; /* this cannot be the target att */
390 : }
391 :
392 : /* If we know the next offset, we can skip the rest */
393 2452 : if (usecache && att[i]->attcacheoff >= 0)
394 964 : off = att[i]->attcacheoff;
395 524 : else if (att[i]->attlen == -1)
396 : {
397 : /*
398 : * We can only cache the offset for a varlena attribute if the
399 : * offset is already suitably aligned, so that there would be
400 : * no pad bytes in any case: then the offset will be valid for
401 : * either an aligned or unaligned value.
402 : */
403 91 : if (usecache &&
404 : off == att_align_nominal(off, att[i]->attalign))
405 43 : att[i]->attcacheoff = off;
406 : else
407 : {
408 5 : off = att_align_pointer(off, att[i]->attalign, -1,
409 : tp + off);
410 5 : usecache = false;
411 : }
412 : }
413 : else
414 : {
415 : /* not varlena, so safe to use att_align_nominal */
416 476 : off = att_align_nominal(off, att[i]->attalign);
417 :
418 476 : if (usecache)
419 0 : att[i]->attcacheoff = off;
420 : }
421 :
422 1488 : if (i == attnum)
423 523 : break;
424 :
425 965 : off = att_addlength_pointer(off, att[i]->attlen, tp + off);
426 :
427 965 : if (usecache && att[i]->attlen <= 0)
428 480 : usecache = false;
429 966 : }
430 : }
431 :
432 1888 : return fetchatt(att[attnum], tp + off);
433 : }
434 :
435 : /*
436 : * Create a palloc'd copy of an index tuple.
437 : */
438 : IndexTuple
439 : CopyIndexTuple(IndexTuple source)
440 1087 : {
441 : IndexTuple result;
442 : Size size;
443 :
444 1087 : size = IndexTupleSize(source);
445 1087 : result = (IndexTuple) palloc(size);
446 1087 : memcpy(result, source, size);
447 1087 : return result;
448 : }
|