PostgreSQL Source Code git master
Loading...
Searching...
No Matches
indextuple.c File Reference
#include "postgres.h"
#include "access/detoast.h"
#include "access/heaptoast.h"
#include "access/htup_details.h"
#include "access/itup.h"
#include "access/toast_internals.h"
Include dependency graph for indextuple.c:

Go to the source code of this file.

Macros

#define TOAST_INDEX_HACK
 

Functions

IndexTuple index_form_tuple (TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
 
IndexTuple index_form_tuple_context (TupleDesc tupleDescriptor, const Datum *values, const bool *isnull, MemoryContext context)
 
Datum nocache_index_getattr (IndexTuple tup, int attnum, TupleDesc tupleDesc)
 
void index_deform_tuple (IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
 
void index_deform_tuple_internal (TupleDesc tupleDescriptor, Datum *values, bool *isnull, char *tp, bits8 *bp, int hasnulls)
 
IndexTuple CopyIndexTuple (IndexTuple source)
 
IndexTuple index_truncate_tuple (TupleDesc sourceDescriptor, IndexTuple source, int leavenatts)
 

Macro Definition Documentation

◆ TOAST_INDEX_HACK

#define TOAST_INDEX_HACK

Definition at line 29 of file indextuple.c.

Function Documentation

◆ CopyIndexTuple()

IndexTuple CopyIndexTuple ( IndexTuple  source)

Definition at line 479 of file indextuple.c.

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}
size_t Size
Definition c.h:691
IndexTupleData * IndexTuple
Definition itup.h:53
static Size IndexTupleSize(const IndexTupleData *itup)
Definition itup.h:71
void * palloc(Size size)
Definition mcxt.c:1387
static rewind_source * source
Definition pg_rewind.c:89
static int fb(int x)

References fb(), IndexTupleSize(), palloc(), and source.

Referenced by _bt_buildadd(), _bt_insert_parent(), _bt_insertonpg(), _bt_load(), _bt_newlevel(), _bt_pagedel(), _bt_swap_posting(), _hash_splitbucket(), _hash_squeezebucket(), btree_xlog_insert(), btree_xlog_split(), gin_check_parent_keys_consistency(), gin_refind_parent(), gistformdownlink(), and index_truncate_tuple().

◆ index_deform_tuple()

void index_deform_tuple ( IndexTuple  tup,
TupleDesc  tupleDescriptor,
Datum values,
bool isnull 
)

Definition at line 364 of file indextuple.c.

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}
static Datum values[MAXATTR]
Definition bootstrap.c:188
uint8 bits8
Definition c.h:625
void index_deform_tuple_internal(TupleDesc tupleDescriptor, Datum *values, bool *isnull, char *tp, bits8 *bp, int hasnulls)
Definition indextuple.c:387
static bool IndexTupleHasNulls(const IndexTupleData *itup)
Definition itup.h:77
static Size IndexInfoFindDataOffset(unsigned short t_info)
Definition itup.h:112

References fb(), index_deform_tuple_internal(), IndexInfoFindDataOffset(), IndexTupleHasNulls(), and values.

Referenced by _bt_check_unique(), comparetup_index_btree_tiebreak(), get_actual_variable_endpoint(), gist_page_items(), index_truncate_tuple(), and StoreIndexTuple().

◆ index_deform_tuple_internal()

void index_deform_tuple_internal ( TupleDesc  tupleDescriptor,
Datum values,
bool isnull,
char tp,
bits8 bp,
int  hasnulls 
)

Definition at line 387 of file indextuple.c.

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}
#define Min(x, y)
Definition c.h:1093
#define Assert(condition)
Definition c.h:945
uint32_t uint32
Definition c.h:618
int16 attnum
#define INDEX_MAX_KEYS
uint64_t Datum
Definition postgres.h:70
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
static Datum align_fetch_then_add(const char *tupptr, uint32 *off, bool attbyval, int attlen, uint8 attalignby)
Definition tupmacs.h:172
static Datum fetch_att_noerr(const void *T, bool attbyval, int attlen)
Definition tupmacs.h:137

References align_fetch_then_add(), Assert, att_isnull(), att_nominal_alignby, attnum, fb(), fetch_att_noerr(), first_null_attr(), INDEX_MAX_KEYS, Min, TupleDescCompactAttr(), and values.

Referenced by index_deform_tuple(), and spgDeformLeafTuple().

◆ index_form_tuple()

IndexTuple index_form_tuple ( TupleDesc  tupleDescriptor,
const Datum values,
const bool isnull 
)

Definition at line 44 of file indextuple.c.

47{
50}
IndexTuple index_form_tuple_context(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull, MemoryContext context)
Definition indextuple.c:65
MemoryContext CurrentMemoryContext
Definition mcxt.c:160

References CurrentMemoryContext, fb(), index_form_tuple_context(), and values.

Referenced by bt_normalize_tuple(), bt_tuple_present_callback(), btinsert(), GinFormTuple(), gistFormTuple(), hashbuildCallback(), hashinsert(), and index_truncate_tuple().

◆ index_form_tuple_context()

IndexTuple index_form_tuple_context ( TupleDesc  tupleDescriptor,
const Datum values,
const bool isnull,
MemoryContext  context 
)

Definition at line 65 of file indextuple.c.

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}
#define MAXALIGN(LEN)
Definition c.h:898
uint16_t uint16
Definition c.h:617
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
#define TOAST_INDEX_HACK
Definition indextuple.c:29
int i
Definition isn.c:77
#define INDEX_VAR_MASK
Definition itup.h:67
#define INDEX_NULL_MASK
Definition itup.h:68
#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
static char * errmsg
FormData_pg_attribute * Form_pg_attribute
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
unsigned short t_info
Definition itup.h:49
Definition c.h:778
Datum toast_compress_datum(Datum value, char cmethod)
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178
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

References Assert, DatumGetPointer(), detoast_external_attr(), ereport, errcode(), errmsg, ERROR, fb(), heap_compute_data_size(), heap_fill_tuple(), HEAP_HASEXTERNAL, HEAP_HASVARWIDTH, i, INDEX_MAX_KEYS, INDEX_NULL_MASK, INDEX_SIZE_MASK, INDEX_VAR_MASK, IndexInfoFindDataOffset(), MAXALIGN, MemoryContextAllocZero(), pfree(), PointerGetDatum(), IndexTupleData::t_info, toast_compress_datum(), TOAST_INDEX_HACK, TOAST_INDEX_TARGET, TupleDescAttr(), values, VARATT_IS_EXTENDED(), VARATT_IS_EXTERNAL(), and VARSIZE().

Referenced by index_form_tuple(), and tuplesort_putindextuplevalues().

◆ index_truncate_tuple()

IndexTuple index_truncate_tuple ( TupleDesc  sourceDescriptor,
IndexTuple  source,
int  leavenatts 
)

Definition at line 508 of file indextuple.c.

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}
void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition indextuple.c:364
IndexTuple CopyIndexTuple(IndexTuple source)
Definition indextuple.c:479
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition indextuple.c:44
ItemPointerData t_tid
Definition itup.h:37
TupleDesc CreateTupleDescTruncatedCopy(TupleDesc tupdesc, int natts)
Definition tupdesc.c:288

References Assert, CopyIndexTuple(), CreateTupleDescTruncatedCopy(), fb(), index_deform_tuple(), index_form_tuple(), INDEX_MAX_KEYS, IndexTupleSize(), pfree(), source, IndexTupleData::t_tid, and values.

Referenced by _bt_truncate().

◆ nocache_index_getattr()

Datum nocache_index_getattr ( IndexTuple  tup,
int  attnum,
TupleDesc  tupleDesc 
)

Definition at line 229 of file indextuple.c.

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}
static bool IndexTupleHasVarwidths(const IndexTupleData *itup)
Definition itup.h:83
int16 attcacheoff
Definition tupdesc.h:70
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition tupmacs.h:431
#define att_pointer_alignby(cur_offset, attalignby, attlen, attptr)
Definition tupmacs.h:383
#define fetchatt(A, T)
Definition tupmacs.h:102

References Assert, att_addlength_pointer, att_isnull(), att_nominal_alignby, att_pointer_alignby, CompactAttribute::attcacheoff, attnum, fb(), fetchatt, first_null_attr(), i, IndexInfoFindDataOffset(), IndexTupleHasNulls(), IndexTupleHasVarwidths(), Min, and TupleDescCompactAttr().

Referenced by index_getattr().