Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 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
/*-------------------------------------------------------------------------
 *
 * toast_compression.c
 *    Functions for toast compression.
 *
 * Copyright (c) 2021-2026, PostgreSQL Global Development Group
 *
 *
 * IDENTIFICATION
 *    src/backend/access/common/toast_compression.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"
 
#ifdef USE_LZ4
#include <lz4.h>
#endif
 
#include "access/detoast.h"
#include "access/toast_compression.h"
#include "common/pg_lzcompress.h"
#include "utils/memutils.h"
#include "varatt.h"
 
/* GUC */
int         default_toast_compression = DEFAULT_TOAST_COMPRESSION;
 
#define NO_COMPRESSION_SUPPORT(method) \
    ereport(ERROR, \
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
             errmsg("compression method %s not supported", method), \
             errdetail("This functionality requires the server to be built with %s support.", method)))
 
/*
 * Compress a varlena using PGLZ.
 *
 * Returns the compressed varlena, or NULL if compression fails.
 */
varlena *
pglz_compress_datum(const varlena *value)
{
    int32       valsize,
                len;
    varlena    *tmp = NULL;
 
    valsize = VARSIZE_ANY_EXHDR(value);
 
    /*
     * No point in wasting a palloc cycle if value size is outside the allowed
     * range for compression.
     */
    if (valsize < PGLZ_strategy_default->min_input_size ||
        valsize > PGLZ_strategy_default->max_input_size)
        return NULL;
 
    /*
     * Figure out the maximum possible size of the pglz output, add the bytes
     * that will be needed for varlena overhead, and allocate that amount.
     *
     * VARSIZE_ANY_EXHDR's refusal to return values higher than 1GB are what
     * protect this addition from wraparound; the PGLZ strategy doesn't
     * provide a useful backstop.
     */
    tmp = (varlena *) palloc(PGLZ_MAX_OUTPUT(valsize) +
                             VARHDRSZ_COMPRESSED);
 
    len = pglz_compress(VARDATA_ANY(value),
                        valsize,
                        (char *) tmp + VARHDRSZ_COMPRESSED,
                        NULL);
    if (len < 0)
    {
        pfree(tmp);
        return NULL;
    }
 
    SET_VARSIZE_COMPRESSED(tmp, len + VARHDRSZ_COMPRESSED);
 
    return tmp;
}
 
/*
 * Decompress a varlena that was compressed using PGLZ.
 */
varlena *
pglz_decompress_datum(const varlena *value)
{
    varlena    *result;
    int32       rawsize;
 
    /*
     * Allocate memory for the uncompressed data.  VARLENA_EXTSIZE_MASK
     * protects this against overflow.
     */
    result = (varlena *) palloc(VARDATA_COMPRESSED_GET_EXTSIZE(value) + VARHDRSZ);
 
    /* decompress the data */
    rawsize = pglz_decompress((const char *) value + VARHDRSZ_COMPRESSED,
                              VARSIZE(value) - VARHDRSZ_COMPRESSED,
                              VARDATA(result),
                              VARDATA_COMPRESSED_GET_EXTSIZE(value), true);
    if (rawsize < 0)
        ereport(ERROR,
                (errcode(ERRCODE_DATA_CORRUPTED),
                 errmsg_internal("compressed pglz data is corrupt")));
 
    SET_VARSIZE(result, rawsize + VARHDRSZ);
 
    return result;
}
 
/*
 * Decompress part of a varlena that was compressed using PGLZ.
 */
varlena *
pglz_decompress_datum_slice(const varlena *value,
                            int32 slicelength)
{
    varlena    *result;
    int32       rawsize;
 
    /*
     * Allocate memory for the uncompressed data.  Caller must have checked
     * the requested size, but verify.
     */
    if (!AllocSizeIsValid(slicelength))
        elog(ERROR, "value is too big");
    result = (varlena *) palloc(slicelength + VARHDRSZ);
 
    /* decompress the data */
    rawsize = pglz_decompress((const char *) value + VARHDRSZ_COMPRESSED,
                              VARSIZE(value) - VARHDRSZ_COMPRESSED,
                              VARDATA(result),
                              slicelength, false);
    if (rawsize < 0)
        ereport(ERROR,
                (errcode(ERRCODE_DATA_CORRUPTED),
                 errmsg_internal("compressed pglz data is corrupt")));
 
    SET_VARSIZE(result, rawsize + VARHDRSZ);
 
    return result;
}
 
/*
 * Compress a varlena using LZ4.
 *
 * Returns the compressed varlena, or NULL if compression fails.
 */
varlena *
lz4_compress_datum(const varlena *value)
{
#ifndef USE_LZ4
    NO_COMPRESSION_SUPPORT("lz4");
    return NULL;                /* keep compiler quiet */
#else
    int32       valsize;
    int32       len;
    int32       max_size;
    varlena    *tmp = NULL;
 
    valsize = VARSIZE_ANY_EXHDR(value);
 
    /*
     * Figure out the maximum possible size of the LZ4 output, add the bytes
     * that will be needed for varlena overhead, and allocate that amount.
     *
     * The current (2026) definition of LZ4_compressBound cannot yield a value
     * that can wrap-around the VARHDRSZ_COMPRESSED addition in 32-bit
     * machines, but we have no way to prevent future LZ4 development (or a
     * malicious LZ4 library) from changing that, so verify.
     */
    max_size = LZ4_compressBound(valsize);
    if (!AllocSizeIsValid(max_size))
        elog(ERROR, "toast object size is too big");
    tmp = (varlena *) palloc(max_size + VARHDRSZ_COMPRESSED);
 
    len = LZ4_compress_default(VARDATA_ANY(value),
                               (char *) tmp + VARHDRSZ_COMPRESSED,
                               valsize, max_size);
    if (len <= 0)
        elog(ERROR, "lz4 compression failed");
 
    /* data is incompressible so just free the memory and return NULL */
    if (len > valsize)
    {
        pfree(tmp);
        return NULL;
    }
 
    SET_VARSIZE_COMPRESSED(tmp, len + VARHDRSZ_COMPRESSED);
 
    return tmp;
#endif
}
 
/*
 * Decompress a varlena that was compressed using LZ4.
 */
varlena *
lz4_decompress_datum(const varlena *value)
{
#ifndef USE_LZ4
    NO_COMPRESSION_SUPPORT("lz4");
    return NULL;                /* keep compiler quiet */
#else
    int32       rawsize;
    varlena    *result;
 
    /*
     * Allocate memory for the uncompressed data.  VARLENA_EXTSIZE_MASK
     * protects this against overflow.
     */
    Assert(AllocSizeIsValid(VARDATA_COMPRESSED_GET_EXTSIZE(value)));
    result = (varlena *) palloc(VARDATA_COMPRESSED_GET_EXTSIZE(value) + VARHDRSZ);
 
    /* decompress the data */
    rawsize = LZ4_decompress_safe((const char *) value + VARHDRSZ_COMPRESSED,
                                  VARDATA(result),
                                  VARSIZE(value) - VARHDRSZ_COMPRESSED,
                                  VARDATA_COMPRESSED_GET_EXTSIZE(value));
    if (rawsize < 0)
        ereport(ERROR,
                (errcode(ERRCODE_DATA_CORRUPTED),
                 errmsg_internal("compressed lz4 data is corrupt")));
 
 
    SET_VARSIZE(result, rawsize + VARHDRSZ);
 
    return result;
#endif
}
 
/*
 * Decompress part of a varlena that was compressed using LZ4.
 */
varlena *
lz4_decompress_datum_slice(const varlena *value, int32 slicelength)
{
#ifndef USE_LZ4
    NO_COMPRESSION_SUPPORT("lz4");
    return NULL;                /* keep compiler quiet */
#else
    int32       rawsize;
    varlena    *result;
 
    /* slice decompression not supported prior to 1.8.3 */
    if (LZ4_versionNumber() < 10803)
        return lz4_decompress_datum(value);
 
    /*
     * Allocate memory for the uncompressed data.  Caller must have checked
     * the requested size, but verify.
     */
    if (!AllocSizeIsValid(slicelength))
        elog(ERROR, "toast object size is too big");
    result = (varlena *) palloc(slicelength + VARHDRSZ);
 
    /* decompress the data */
    rawsize = LZ4_decompress_safe_partial((const char *) value + VARHDRSZ_COMPRESSED,
                                          VARDATA(result),
                                          VARSIZE(value) - VARHDRSZ_COMPRESSED,
                                          slicelength,
                                          slicelength);
    if (rawsize < 0)
        ereport(ERROR,
                (errcode(ERRCODE_DATA_CORRUPTED),
                 errmsg_internal("compressed lz4 data is corrupt")));
 
    SET_VARSIZE(result, rawsize + VARHDRSZ);
 
    return result;
#endif
}
 
/*
 * Extract compression ID from a varlena.
 *
 * Returns TOAST_INVALID_COMPRESSION_ID if the varlena is not compressed.
 */
ToastCompressionId
toast_get_compression_id(varlena *attr)
{
    ToastCompressionId cmid = TOAST_INVALID_COMPRESSION_ID;
 
    /*
     * If it is stored externally then fetch the compression method id from
     * the external toast pointer.  If compressed inline, fetch it from the
     * toast compression header.
     */
    if (VARATT_IS_EXTERNAL_ONDISK(attr))
    {
        varatt_external toast_pointer;
 
        VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
 
        if (VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer))
            cmid = VARATT_EXTERNAL_GET_COMPRESS_METHOD(toast_pointer);
    }
    else if (VARATT_IS_COMPRESSED(attr))
        cmid = VARDATA_COMPRESSED_GET_COMPRESS_METHOD(attr);
 
    return cmid;
}
 
/*
 * CompressionNameToMethod - Get compression method from compression name
 *
 * Search in the available built-in methods.  If the compression not found
 * in the built-in methods then return InvalidCompressionMethod.
 */
char
CompressionNameToMethod(const char *compression)
{
    if (strcmp(compression, "pglz") == 0)
        return TOAST_PGLZ_COMPRESSION;
    else if (strcmp(compression, "lz4") == 0)
    {
#ifndef USE_LZ4
        NO_COMPRESSION_SUPPORT("lz4");
#endif
        return TOAST_LZ4_COMPRESSION;
    }
 
    return InvalidCompressionMethod;
}
 
/*
 * GetCompressionMethodName - Get compression method name
 */
const char *
GetCompressionMethodName(char method)
{
    switch (method)
    {
        case TOAST_PGLZ_COMPRESSION:
            return "pglz";
        case TOAST_LZ4_COMPRESSION:
            return "lz4";
        default:
            elog(ERROR, "invalid compression method %c", method);
            return NULL;        /* keep compiler quiet */
    }
}