Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * toast_compression.c
4 : : * Functions for toast compression.
5 : : *
6 : : * Copyright (c) 2021-2026, PostgreSQL Global Development Group
7 : : *
8 : : *
9 : : * IDENTIFICATION
10 : : * src/backend/access/common/toast_compression.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : : #include "postgres.h"
15 : :
16 : : #ifdef USE_LZ4
17 : : #include <lz4.h>
18 : : #endif
19 : :
20 : : #include "access/detoast.h"
21 : : #include "access/toast_compression.h"
22 : : #include "common/pg_lzcompress.h"
23 : : #include "utils/memutils.h"
24 : : #include "varatt.h"
25 : :
26 : : /* GUC */
27 : : int default_toast_compression = DEFAULT_TOAST_COMPRESSION;
28 : :
29 : : #define NO_COMPRESSION_SUPPORT(method) \
30 : : ereport(ERROR, \
31 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
32 : : errmsg("compression method %s not supported", method), \
33 : : errdetail("This functionality requires the server to be built with %s support.", method)))
34 : :
35 : : /*
36 : : * Compress a varlena using PGLZ.
37 : : *
38 : : * Returns the compressed varlena, or NULL if compression fails.
39 : : */
40 : : varlena *
41 : 236 : pglz_compress_datum(const varlena *value)
42 : : {
43 : : int32 valsize,
44 : : len;
45 : 236 : varlena *tmp = NULL;
46 : :
47 : 236 : valsize = VARSIZE_ANY_EXHDR(value);
48 : :
49 : : /*
50 : : * No point in wasting a palloc cycle if value size is outside the allowed
51 : : * range for compression.
52 : : */
53 [ + - ]: 236 : if (valsize < PGLZ_strategy_default->min_input_size ||
54 [ - + ]: 236 : valsize > PGLZ_strategy_default->max_input_size)
55 : 0 : return NULL;
56 : :
57 : : /*
58 : : * Figure out the maximum possible size of the pglz output, add the bytes
59 : : * that will be needed for varlena overhead, and allocate that amount.
60 : : *
61 : : * VARSIZE_ANY_EXHDR's refusal to return values higher than 1GB are what
62 : : * protect this addition from wraparound; the PGLZ strategy doesn't
63 : : * provide a useful backstop.
64 : : */
65 : 236 : tmp = (varlena *) palloc(PGLZ_MAX_OUTPUT(valsize) +
66 : : VARHDRSZ_COMPRESSED);
67 : :
68 : 236 : len = pglz_compress(VARDATA_ANY(value),
69 : : valsize,
70 : : (char *) tmp + VARHDRSZ_COMPRESSED,
71 : : NULL);
72 [ + + ]: 236 : if (len < 0)
73 : : {
74 : 112 : pfree(tmp);
75 : 112 : return NULL;
76 : : }
77 : :
78 : 124 : SET_VARSIZE_COMPRESSED(tmp, len + VARHDRSZ_COMPRESSED);
79 : :
80 : 124 : return tmp;
81 : : }
82 : :
83 : : /*
84 : : * Decompress a varlena that was compressed using PGLZ.
85 : : */
86 : : varlena *
87 : 383 : pglz_decompress_datum(const varlena *value)
88 : : {
89 : : varlena *result;
90 : : int32 rawsize;
91 : :
92 : : /*
93 : : * Allocate memory for the uncompressed data. VARLENA_EXTSIZE_MASK
94 : : * protects this against overflow.
95 : : */
96 : 383 : result = (varlena *) palloc(VARDATA_COMPRESSED_GET_EXTSIZE(value) + VARHDRSZ);
97 : :
98 : : /* decompress the data */
99 : 383 : rawsize = pglz_decompress((const char *) value + VARHDRSZ_COMPRESSED,
100 : 383 : VARSIZE(value) - VARHDRSZ_COMPRESSED,
101 : : VARDATA(result),
102 : 383 : VARDATA_COMPRESSED_GET_EXTSIZE(value), true);
103 [ - + ]: 383 : if (rawsize < 0)
104 [ # # ]: 0 : ereport(ERROR,
105 : : (errcode(ERRCODE_DATA_CORRUPTED),
106 : : errmsg_internal("compressed pglz data is corrupt")));
107 : :
108 : 383 : SET_VARSIZE(result, rawsize + VARHDRSZ);
109 : :
110 : 383 : return result;
111 : : }
112 : :
113 : : /*
114 : : * Decompress part of a varlena that was compressed using PGLZ.
115 : : */
116 : : varlena *
117 : 24 : pglz_decompress_datum_slice(const varlena *value,
118 : : int32 slicelength)
119 : : {
120 : : varlena *result;
121 : : int32 rawsize;
122 : :
123 : : /*
124 : : * Allocate memory for the uncompressed data. Caller must have checked
125 : : * the requested size, but verify.
126 : : */
127 [ - + ]: 24 : if (!AllocSizeIsValid(slicelength))
128 [ # # ]: 0 : elog(ERROR, "value is too big");
129 : 24 : result = (varlena *) palloc(slicelength + VARHDRSZ);
130 : :
131 : : /* decompress the data */
132 : 24 : rawsize = pglz_decompress((const char *) value + VARHDRSZ_COMPRESSED,
133 : 24 : VARSIZE(value) - VARHDRSZ_COMPRESSED,
134 : : VARDATA(result),
135 : : slicelength, false);
136 [ - + ]: 24 : if (rawsize < 0)
137 [ # # ]: 0 : ereport(ERROR,
138 : : (errcode(ERRCODE_DATA_CORRUPTED),
139 : : errmsg_internal("compressed pglz data is corrupt")));
140 : :
141 : 24 : SET_VARSIZE(result, rawsize + VARHDRSZ);
142 : :
143 : 24 : return result;
144 : : }
145 : :
146 : : /*
147 : : * Compress a varlena using LZ4.
148 : : *
149 : : * Returns the compressed varlena, or NULL if compression fails.
150 : : */
151 : : varlena *
152 : 32019 : lz4_compress_datum(const varlena *value)
153 : : {
154 : : #ifndef USE_LZ4
155 : : NO_COMPRESSION_SUPPORT("lz4");
156 : : return NULL; /* keep compiler quiet */
157 : : #else
158 : : int32 valsize;
159 : : int32 len;
160 : : int32 max_size;
161 : 32019 : varlena *tmp = NULL;
162 : :
163 : 32019 : valsize = VARSIZE_ANY_EXHDR(value);
164 : :
165 : : /*
166 : : * Figure out the maximum possible size of the LZ4 output, add the bytes
167 : : * that will be needed for varlena overhead, and allocate that amount.
168 : : *
169 : : * The current (2026) definition of LZ4_compressBound cannot yield a value
170 : : * that can wrap-around the VARHDRSZ_COMPRESSED addition in 32-bit
171 : : * machines, but we have no way to prevent future LZ4 development (or a
172 : : * malicious LZ4 library) from changing that, so verify.
173 : : */
174 : 32019 : max_size = LZ4_compressBound(valsize);
175 [ - + ]: 32019 : if (!AllocSizeIsValid(max_size))
176 [ # # ]: 0 : elog(ERROR, "toast object size is too big");
177 : 32019 : tmp = (varlena *) palloc(max_size + VARHDRSZ_COMPRESSED);
178 : :
179 : 32019 : len = LZ4_compress_default(VARDATA_ANY(value),
180 : : (char *) tmp + VARHDRSZ_COMPRESSED,
181 : : valsize, max_size);
182 [ - + ]: 32019 : if (len <= 0)
183 [ # # ]: 0 : elog(ERROR, "lz4 compression failed");
184 : :
185 : : /* data is incompressible so just free the memory and return NULL */
186 [ + + ]: 32019 : if (len > valsize)
187 : : {
188 : 407 : pfree(tmp);
189 : 407 : return NULL;
190 : : }
191 : :
192 : 31612 : SET_VARSIZE_COMPRESSED(tmp, len + VARHDRSZ_COMPRESSED);
193 : :
194 : 31612 : return tmp;
195 : : #endif
196 : : }
197 : :
198 : : /*
199 : : * Decompress a varlena that was compressed using LZ4.
200 : : */
201 : : varlena *
202 : 104336 : lz4_decompress_datum(const varlena *value)
203 : : {
204 : : #ifndef USE_LZ4
205 : : NO_COMPRESSION_SUPPORT("lz4");
206 : : return NULL; /* keep compiler quiet */
207 : : #else
208 : : int32 rawsize;
209 : : varlena *result;
210 : :
211 : : /*
212 : : * Allocate memory for the uncompressed data. VARLENA_EXTSIZE_MASK
213 : : * protects this against overflow.
214 : : */
215 : : Assert(AllocSizeIsValid(VARDATA_COMPRESSED_GET_EXTSIZE(value)));
216 : 104336 : result = (varlena *) palloc(VARDATA_COMPRESSED_GET_EXTSIZE(value) + VARHDRSZ);
217 : :
218 : : /* decompress the data */
219 : 104336 : rawsize = LZ4_decompress_safe((const char *) value + VARHDRSZ_COMPRESSED,
220 : : VARDATA(result),
221 : 104336 : VARSIZE(value) - VARHDRSZ_COMPRESSED,
222 : 104336 : VARDATA_COMPRESSED_GET_EXTSIZE(value));
223 [ - + ]: 104336 : if (rawsize < 0)
224 [ # # ]: 0 : ereport(ERROR,
225 : : (errcode(ERRCODE_DATA_CORRUPTED),
226 : : errmsg_internal("compressed lz4 data is corrupt")));
227 : :
228 : :
229 : 104336 : SET_VARSIZE(result, rawsize + VARHDRSZ);
230 : :
231 : 104336 : return result;
232 : : #endif
233 : : }
234 : :
235 : : /*
236 : : * Decompress part of a varlena that was compressed using LZ4.
237 : : */
238 : : varlena *
239 : 164 : lz4_decompress_datum_slice(const varlena *value, int32 slicelength)
240 : : {
241 : : #ifndef USE_LZ4
242 : : NO_COMPRESSION_SUPPORT("lz4");
243 : : return NULL; /* keep compiler quiet */
244 : : #else
245 : : int32 rawsize;
246 : : varlena *result;
247 : :
248 : : /* slice decompression not supported prior to 1.8.3 */
249 [ - + ]: 164 : if (LZ4_versionNumber() < 10803)
250 : 0 : return lz4_decompress_datum(value);
251 : :
252 : : /*
253 : : * Allocate memory for the uncompressed data. Caller must have checked
254 : : * the requested size, but verify.
255 : : */
256 [ - + ]: 164 : if (!AllocSizeIsValid(slicelength))
257 [ # # ]: 0 : elog(ERROR, "toast object size is too big");
258 : 164 : result = (varlena *) palloc(slicelength + VARHDRSZ);
259 : :
260 : : /* decompress the data */
261 : 164 : rawsize = LZ4_decompress_safe_partial((const char *) value + VARHDRSZ_COMPRESSED,
262 : : VARDATA(result),
263 : 164 : VARSIZE(value) - VARHDRSZ_COMPRESSED,
264 : : slicelength,
265 : : slicelength);
266 [ - + ]: 164 : if (rawsize < 0)
267 [ # # ]: 0 : ereport(ERROR,
268 : : (errcode(ERRCODE_DATA_CORRUPTED),
269 : : errmsg_internal("compressed lz4 data is corrupt")));
270 : :
271 : 164 : SET_VARSIZE(result, rawsize + VARHDRSZ);
272 : :
273 : 164 : return result;
274 : : #endif
275 : : }
276 : :
277 : : /*
278 : : * Extract compression ID from a varlena.
279 : : *
280 : : * Returns TOAST_INVALID_COMPRESSION_ID if the varlena is not compressed.
281 : : */
282 : : ToastCompressionId
283 : 154 : toast_get_compression_id(varlena *attr)
284 : : {
285 : 154 : ToastCompressionId cmid = TOAST_INVALID_COMPRESSION_ID;
286 : :
287 : : /*
288 : : * If it is stored externally then fetch the compression method id from
289 : : * the external toast pointer. If compressed inline, fetch it from the
290 : : * toast compression header.
291 : : */
292 [ + + ]: 154 : if (VARATT_IS_EXTERNAL_ONDISK(attr))
293 : : {
294 : : toast_external_data toast_ext_data;
295 : :
296 : 54 : toast_external_info_get(attr, &toast_ext_data);
297 : :
298 [ + + ]: 54 : if (VARATT_EXTINFO_IS_COMPRESSED(toast_ext_data.extinfo, toast_ext_data.rawsize))
299 : 23 : cmid = VARATT_EXTINFO_GET_COMPRESS_METHOD(toast_ext_data.extinfo);
300 : : }
301 [ + + ]: 100 : else if (VARATT_IS_COMPRESSED(attr))
302 : 92 : cmid = VARDATA_COMPRESSED_GET_COMPRESS_METHOD(attr);
303 : :
304 : 154 : return cmid;
305 : : }
306 : :
307 : : /*
308 : : * CompressionNameToMethod - Get compression method from compression name
309 : : *
310 : : * Search in the available built-in methods. If the compression not found
311 : : * in the built-in methods then return InvalidCompressionMethod.
312 : : */
313 : : char
314 : 117 : CompressionNameToMethod(const char *compression)
315 : : {
316 [ + + ]: 117 : if (strcmp(compression, "pglz") == 0)
317 : 66 : return TOAST_PGLZ_COMPRESSION;
318 [ + + ]: 51 : else if (strcmp(compression, "lz4") == 0)
319 : : {
320 : : #ifndef USE_LZ4
321 : : NO_COMPRESSION_SUPPORT("lz4");
322 : : #endif
323 : 43 : return TOAST_LZ4_COMPRESSION;
324 : : }
325 : :
326 : 8 : return InvalidCompressionMethod;
327 : : }
328 : :
329 : : /*
330 : : * GetCompressionMethodName - Get compression method name
331 : : */
332 : : const char *
333 : 28 : GetCompressionMethodName(char method)
334 : : {
335 [ + + - ]: 28 : switch (method)
336 : : {
337 : 16 : case TOAST_PGLZ_COMPRESSION:
338 : 16 : return "pglz";
339 : 12 : case TOAST_LZ4_COMPRESSION:
340 : 12 : return "lz4";
341 : 0 : default:
342 [ # # ]: 0 : elog(ERROR, "invalid compression method %c", method);
343 : : return NULL; /* keep compiler quiet */
344 : : }
345 : : }
|