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-2016, 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
index_form_tuple(TupleDesc tupleDescriptor,Datum * values,bool * isnull)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 = 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 		 */
79 		if (VARATT_IS_EXTERNAL(DatumGetPointer(values[i])))
80 		{
81 			untoasted_values[i] =
82 				PointerGetDatum(heap_tuple_fetch_attr((struct varlena *)
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
nocache_index_getattr(IndexTuple tup,int attnum,TupleDesc tupleDesc)208 nocache_index_getattr(IndexTuple tup,
209 					  int attnum,
210 					  TupleDesc tupleDesc)
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 }
408 
409 /*
410  * Convert an index tuple into Datum/isnull arrays.
411  *
412  * The caller must allocate sufficient storage for the output arrays.
413  * (INDEX_MAX_KEYS entries should be enough.)
414  */
415 void
index_deform_tuple(IndexTuple tup,TupleDesc tupleDescriptor,Datum * values,bool * isnull)416 index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor,
417 				   Datum *values, bool *isnull)
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 }
429 
430 /*
431  * Create a palloc'd copy of an index tuple.
432  */
433 IndexTuple
CopyIndexTuple(IndexTuple source)434 CopyIndexTuple(IndexTuple source)
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 }
444