PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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/detoast.h"
20#include "access/heaptoast.h"
21#include "access/htup_details.h"
22#include "access/itup.h"
24
25/*
26 * This enables de-toasting of index entries. Needed until VACUUM is
27 * smart enough to rebuild indexes from scratch.
28 */
29#define TOAST_INDEX_HACK
30
31/* ----------------------------------------------------------------
32 * index_ tuple interface routines
33 * ----------------------------------------------------------------
34 */
35
36 /* ----------------
37 * index_form_tuple
38 *
39 * As index_form_tuple_context, but allocates the returned tuple in the
40 * CurrentMemoryContext.
41 * ----------------
42 */
51
52/* ----------------
53 * index_form_tuple_context
54 *
55 * This shouldn't leak any memory; otherwise, callers such as
56 * tuplesort_putindextuplevalues() will be very unhappy.
57 *
58 * This shouldn't perform external table access provided caller
59 * does not pass values that are stored EXTERNAL.
60 *
61 * Allocates returned tuple in provided 'context'.
62 * ----------------
63 */
66 const Datum *values,
67 const bool *isnull,
68 MemoryContext context)
69{
70 char *tp; /* tuple pointer */
71 IndexTuple tuple; /* return tuple */
72 Size size,
74 hoff;
75 int i;
76 unsigned short infomask = 0;
77 bool hasnull = false;
78 uint16 tupmask = 0;
80
81#ifdef TOAST_INDEX_HACK
84#endif
85
89 errmsg("number of index columns (%d) exceeds limit (%d)",
91
92#ifdef TOAST_INDEX_HACK
93 for (i = 0; i < numberOfAttributes; i++)
94 {
96
98 untoasted_free[i] = false;
99
100 /* Do nothing if value is NULL or not of varlena type */
101 if (isnull[i] || att->attlen != -1)
102 continue;
103
104 /*
105 * If value is stored EXTERNAL, must fetch it so we are not depending
106 * on outside storage. This should be improved someday.
107 */
109 {
113 untoasted_free[i] = true;
114 }
115
116 /*
117 * If value is above size target, and is of a compressible datatype,
118 * try to compress it in-line.
119 */
122 (att->attstorage == TYPSTORAGE_EXTENDED ||
123 att->attstorage == TYPSTORAGE_MAIN))
124 {
126
128 att->attcompression);
129
131 {
132 /* successful compression */
133 if (untoasted_free[i])
136 untoasted_free[i] = true;
137 }
138 }
139 }
140#endif
141
142 for (i = 0; i < numberOfAttributes; i++)
143 {
144 if (isnull[i])
145 {
146 hasnull = true;
147 break;
148 }
149 }
150
151 if (hasnull)
153
155#ifdef TOAST_INDEX_HACK
157 untoasted_values, isnull);
158#else
160 values, isnull);
161#endif
162 size = hoff + data_size;
163 size = MAXALIGN(size); /* be conservative */
164
165 tp = (char *) MemoryContextAllocZero(context, size);
166 tuple = (IndexTuple) tp;
167
171#else
172 values,
173#endif
174 isnull,
175 tp + hoff,
176 data_size,
177 &tupmask,
178 (hasnull ? (bits8 *) tp + sizeof(IndexTupleData) : NULL));
179
180#ifdef TOAST_INDEX_HACK
181 for (i = 0; i < numberOfAttributes; i++)
182 {
183 if (untoasted_free[i])
185 }
186#endif
187
188 /*
189 * We do this because heap_fill_tuple wants to initialize a "tupmask"
190 * which is used for HeapTuples, but we want an indextuple infomask. The
191 * only relevant info is the "has variable attributes" field. We have
192 * already set the hasnull bit above.
193 */
196
197 /* Also assert we got rid of external attributes */
198#ifdef TOAST_INDEX_HACK
200#endif
201
202 /*
203 * Here we make sure that the size will fit in the field reserved for it
204 * in t_info.
205 */
206 if ((size & INDEX_SIZE_MASK) != size)
209 errmsg("index row requires %zu bytes, maximum size is %zu",
210 size, (Size) INDEX_SIZE_MASK)));
211
212 infomask |= size;
213
214 /*
215 * initialize metadata
216 */
217 tuple->t_info = infomask;
218 return tuple;
219}
220
221/* ----------------
222 * nocache_index_getattr
223 *
224 * This gets called from index_getattr() macro, and only in cases
225 * where we can't use cacheoffset and the value is not null.
226 * ----------------
227 */
228Datum
230 int attnum,
232{
234 char *tp; /* ptr to data part of tuple */
235 bits8 *bp = NULL; /* ptr to null bitmap in tuple */
236 int data_off; /* tuple data offset */
237 int off; /* current offset within data */
238 int startAttr;
239 int firstNullAttr;
241 int i;
242
243 /* Did someone forget to call TupleDescFinalize()? */
244 Assert(tupleDesc->firstNonCachedOffsetAttr >= 0);
245
246 attnum--;
247
249 tp = (char *) tup + data_off;
250
251 /*
252 * To minimize the number of attributes we need to look at, start walking
253 * the tuple at the attribute with the highest attcacheoff prior to attnum
254 * or the first NULL attribute prior to attnum, whichever comes first.
255 */
256 if (hasnulls)
257 {
258 bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
260 }
261 else
263
264 if (tupleDesc->firstNonCachedOffsetAttr > 0 && firstNullAttr > 0)
265 {
266 /*
267 * Try to start with the highest attribute with an attcacheoff that's
268 * prior to the one we're looking for, or with the attribute prior to
269 * the first NULL attribute, if there is one.
270 */
271 startAttr = Min(tupleDesc->firstNonCachedOffsetAttr - 1, firstNullAttr - 1);
273 }
274 else
275 {
276 /* Otherwise, start at the beginning... */
277 startAttr = 0;
278 off = 0;
279 }
280
281 /*
282 * Calculate 'off' up to the first NULL attr. We use two cheaper loops
283 * when the tuple has no variable-width columns. When variable-width
284 * columns exists, we use att_addlength_pointer() to move the offset
285 * beyond the current attribute.
286 */
288 {
289 /* Calculate the offset up until the first NULL */
290 for (i = startAttr; i < firstNullAttr; i++)
291 {
293
294 off = att_pointer_alignby(off,
295 cattr->attalignby,
296 cattr->attlen,
297 tp + off);
298 off = att_addlength_pointer(off, cattr->attlen, tp + off);
299 }
300
301 /* Calculate the offset for any remaining columns. */
302 for (; i < attnum; i++)
303 {
305
306 if (att_isnull(i, bp))
307 continue;
308
310
311 off = att_pointer_alignby(off,
312 cattr->attalignby,
313 cattr->attlen,
314 tp + off);
315 off = att_addlength_pointer(off, cattr->attlen, tp + off);
316 }
317 }
318 else
319 {
320 /* Handle tuples with only fixed-width attributes */
321
322 /* Calculate the offset up until the first NULL */
323 for (i = startAttr; i < firstNullAttr; i++)
324 {
326
327 Assert(cattr->attlen > 0);
328 off = att_nominal_alignby(off, cattr->attalignby);
329 off += cattr->attlen;
330 }
331
332 /* Calculate the offset for any remaining columns. */
333 for (; i < attnum; i++)
334 {
336
337 if (att_isnull(i, bp))
338 continue;
339
341
342 Assert(cattr->attlen > 0);
343 off = att_nominal_alignby(off, cattr->attalignby);
344 off += cattr->attlen;
345 }
346 }
347
349 off = att_pointer_alignby(off, cattr->attalignby,
350 cattr->attlen, tp + off);
351 return fetchatt(cattr, tp + off);
352}
353
354/*
355 * Convert an index tuple into Datum/isnull arrays.
356 *
357 * The caller must allocate sufficient storage for the output arrays.
358 * (INDEX_MAX_KEYS entries should be enough.)
359 *
360 * This is nearly the same as heap_deform_tuple(), but for IndexTuples.
361 * One difference is that the tuple should never have any missing columns.
362 */
363void
365 Datum *values, bool *isnull)
366{
367 char *tp; /* ptr to tuple data */
368 bits8 *bp; /* ptr to null bitmap in tuple */
369
370 /* XXX "knows" t_bits are just after fixed tuple header! */
371 bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
372
373 tp = (char *) tup + IndexInfoFindDataOffset(tup->t_info);
374
377}
378
379/*
380 * Convert an index tuple into Datum/isnull arrays,
381 * without assuming any specific layout of the index tuple header.
382 *
383 * Caller must supply pointer to data area, pointer to nulls bitmap
384 * (which can be NULL if !hasnulls), and hasnulls flag.
385 */
386void
388 Datum *values, bool *isnull,
389 char *tp, bits8 *bp, int hasnulls)
390{
392 int natts = tupleDescriptor->natts; /* number of atts to extract */
393 int attnum = 0;
394 uint32 off = 0; /* offset in tuple data */
396 int firstNullAttr;
397
398 /* Assert to protect callers who allocate fixed-size arrays */
399 Assert(natts <= INDEX_MAX_KEYS);
400
401 /* Did someone forget to call TupleDescFinalize()? */
402 Assert(tupleDescriptor->firstNonCachedOffsetAttr >= 0);
403
404 firstNonCacheOffsetAttr = Min(tupleDescriptor->firstNonCachedOffsetAttr, natts);
405
406 if (hasnulls)
407 {
410 }
411 else
412 firstNullAttr = natts;
413
415 {
416#ifdef USE_ASSERT_CHECKING
417 /* In Assert enabled builds, verify attcacheoff is correct */
418 off = 0;
419#endif
420
421 do
422 {
423 isnull[attnum] = false;
425
426#ifdef USE_ASSERT_CHECKING
427 off = att_nominal_alignby(off, cattr->attalignby);
428 Assert(off == cattr->attcacheoff);
429 off += cattr->attlen;
430#endif
431
432 values[attnum] = fetch_att_noerr(tp + cattr->attcacheoff, cattr->attbyval,
433 cattr->attlen);
434 } while (++attnum < firstNonCacheOffsetAttr);
435
436 off = cattr->attcacheoff + cattr->attlen;
437 }
438
439 for (; attnum < firstNullAttr; attnum++)
440 {
441 isnull[attnum] = false;
443
444 /* align 'off', fetch the datum, and increment off beyond the datum */
446 &off,
447 cattr->attbyval,
448 cattr->attlen,
449 cattr->attalignby);
450 }
451
452 for (; attnum < natts; attnum++)
453 {
455
456 if (att_isnull(attnum, bp))
457 {
458 values[attnum] = (Datum) 0;
459 isnull[attnum] = true;
460 continue;
461 }
462
463 isnull[attnum] = false;
465
466 /* align 'off', fetch the attr's value, and increment off beyond it */
468 &off,
469 cattr->attbyval,
470 cattr->attlen,
471 cattr->attalignby);
472 }
473}
474
475/*
476 * Create a palloc'd copy of an index tuple.
477 */
480{
481 IndexTuple result;
482 Size size;
483
484 size = IndexTupleSize(source);
485 result = (IndexTuple) palloc(size);
486 memcpy(result, source, size);
487 return result;
488}
489
490/*
491 * Create a palloc'd copy of an index tuple, leaving only the first
492 * leavenatts attributes remaining.
493 *
494 * Truncation is guaranteed to result in an index tuple that is no
495 * larger than the original. It is safe to use the IndexTuple with
496 * the original tuple descriptor, but caller must avoid actually
497 * accessing truncated attributes from returned tuple! In practice
498 * this means that index_getattr() must be called with special care,
499 * and that the truncated tuple should only ever be accessed by code
500 * under caller's direct control.
501 *
502 * It's safe to call this function with a buffer lock held, since it
503 * never performs external table access. If it ever became possible
504 * for index tuples to contain EXTERNAL TOAST values, then this would
505 * have to be revisited.
506 */
509 int leavenatts)
510{
513 bool isnull[INDEX_MAX_KEYS];
514 IndexTuple truncated;
515
517
518 /* Easy case: no truncation actually required */
519 if (leavenatts == sourceDescriptor->natts)
520 return CopyIndexTuple(source);
521
522 /* Create temporary truncated tuple descriptor */
524
525 /* Deform, form copy of tuple with fewer attributes */
527 truncated = index_form_tuple(truncdesc, values, isnull);
528 truncated->t_tid = source->t_tid;
530
531 /*
532 * Cannot leak memory here, TupleDescCopy() doesn't allocate any inner
533 * structure, so, plain pfree() should clean all allocated memory
534 */
536
537 return truncated;
538}
static Datum values[MAXATTR]
Definition bootstrap.c:188
#define Min(x, y)
Definition c.h:1093
#define MAXALIGN(LEN)
Definition c.h:898
#define Assert(condition)
Definition c.h:945
uint8 bits8
Definition c.h:625
uint16_t uint16
Definition c.h:617
uint32_t uint32
Definition c.h:618
size_t Size
Definition c.h:691
varlena * detoast_external_attr(varlena *attr)
Definition detoast.c:45
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
#define TOAST_INDEX_TARGET
Definition heaptoast.h:68
Size heap_compute_data_size(TupleDesc tupleDesc, const Datum *values, const bool *isnull)
Definition heaptuple.c:219
void heap_fill_tuple(TupleDesc tupleDesc, const Datum *values, const bool *isnull, char *data, Size data_size, uint16 *infomask, bits8 *bit)
Definition heaptuple.c:401
#define HEAP_HASVARWIDTH
#define HEAP_HASEXTERNAL
void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition indextuple.c:364
IndexTuple index_truncate_tuple(TupleDesc sourceDescriptor, IndexTuple source, int leavenatts)
Definition indextuple.c:508
IndexTuple CopyIndexTuple(IndexTuple source)
Definition indextuple.c:479
#define TOAST_INDEX_HACK
Definition indextuple.c:29
void index_deform_tuple_internal(TupleDesc tupleDescriptor, Datum *values, bool *isnull, char *tp, bits8 *bp, int hasnulls)
Definition indextuple.c:387
IndexTuple index_form_tuple_context(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull, MemoryContext context)
Definition indextuple.c:65
Datum nocache_index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc)
Definition indextuple.c:229
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition indextuple.c:44
int i
Definition isn.c:77
#define INDEX_VAR_MASK
Definition itup.h:67
#define INDEX_NULL_MASK
Definition itup.h:68
static bool IndexTupleHasVarwidths(const IndexTupleData *itup)
Definition itup.h:83
IndexTupleData * IndexTuple
Definition itup.h:53
static bool IndexTupleHasNulls(const IndexTupleData *itup)
Definition itup.h:77
static Size IndexTupleSize(const IndexTupleData *itup)
Definition itup.h:71
static Size IndexInfoFindDataOffset(unsigned short t_info)
Definition itup.h:112
#define INDEX_SIZE_MASK
Definition itup.h:65
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1266
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc(Size size)
Definition mcxt.c:1387
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
static char * errmsg
int16 attnum
FormData_pg_attribute * Form_pg_attribute
#define INDEX_MAX_KEYS
static rewind_source * source
Definition pg_rewind.c:89
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
static int fb(int x)
int16 attcacheoff
Definition tupdesc.h:70
ItemPointerData t_tid
Definition itup.h:37
unsigned short t_info
Definition itup.h:49
Definition c.h:778
Datum toast_compress_datum(Datum value, char cmethod)
TupleDesc CreateTupleDescTruncatedCopy(TupleDesc tupdesc, int natts)
Definition tupdesc.c:288
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:193
#define att_nominal_alignby(cur_offset, attalignby)
Definition tupmacs.h:411
static int first_null_attr(const bits8 *bits, int natts)
Definition tupmacs.h:244
static bool att_isnull(int ATT, const bits8 *BITS)
Definition tupmacs.h:28
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition tupmacs.h:431
static Datum align_fetch_then_add(const char *tupptr, uint32 *off, bool attbyval, int attlen, uint8 attalignby)
Definition tupmacs.h:172
#define att_pointer_alignby(cur_offset, attalignby, attlen, attptr)
Definition tupmacs.h:383
#define fetchatt(A, T)
Definition tupmacs.h:102
static Datum fetch_att_noerr(const void *T, bool attbyval, int attlen)
Definition tupmacs.h:137
static bool VARATT_IS_EXTENDED(const void *PTR)
Definition varatt.h:410
static bool VARATT_IS_EXTERNAL(const void *PTR)
Definition varatt.h:354
static Size VARSIZE(const void *PTR)
Definition varatt.h:298