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