Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * detoast.c
4 : : * Retrieve compressed or external variable size attributes.
5 : : *
6 : : * Copyright (c) 2000-2026, PostgreSQL Global Development Group
7 : : *
8 : : * IDENTIFICATION
9 : : * src/backend/access/common/detoast.c
10 : : *
11 : : *-------------------------------------------------------------------------
12 : : */
13 : :
14 : : #include "postgres.h"
15 : :
16 : : #include "access/detoast.h"
17 : : #include "access/table.h"
18 : : #include "access/tableam.h"
19 : : #include "access/toast_compression.h"
20 : : #include "access/toast_internals.h"
21 : : #include "common/int.h"
22 : : #include "common/pg_lzcompress.h"
23 : : #include "utils/expandeddatum.h"
24 : : #include "utils/memutils.h"
25 : : #include "utils/rel.h"
26 : :
27 : : static varlena *toast_fetch_datum(varlena *attr);
28 : : static varlena *toast_fetch_datum_slice(varlena *attr,
29 : : int32 sliceoffset,
30 : : int32 slicelength);
31 : : static varlena *toast_decompress_datum(varlena *attr);
32 : : static varlena *toast_decompress_datum_slice(varlena *attr, int32 slicelength);
33 : :
34 : : /* ----------
35 : : * detoast_external_attr -
36 : : *
37 : : * Public entry point to get back a toasted value from
38 : : * external source (possibly still in compressed format).
39 : : *
40 : : * This will return a datum that contains all the data internally, ie, not
41 : : * relying on external storage or memory, but it can still be compressed or
42 : : * have a short header. Note some callers assume that if the input is an
43 : : * EXTERNAL datum, the result will be a pfree'able chunk.
44 : : * ----------
45 : : */
46 : : varlena *
47 : 11317 : detoast_external_attr(varlena *attr)
48 : : {
49 : : varlena *result;
50 : :
51 : : /*
52 : : * It's critical that the stored varlena mask does not allow for toast
53 : : * objects bigger than what can be allocated with palloc; otherwise,
54 : : * buffer overflows can be caused by adversarial toast objects.
55 : : */
56 : : StaticAssertExpr(VARLENA_EXTSIZE_MASK <= MaxAllocSize,
57 : : "VARLENA_EXTSIZE_MASK is too big");
58 : :
59 [ + + ]: 11317 : if (VARATT_IS_EXTERNAL_ONDISK(attr))
60 : : {
61 : : /*
62 : : * This is an external stored plain value
63 : : */
64 : 5005 : result = toast_fetch_datum(attr);
65 : : }
66 [ + + ]: 6312 : else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
67 : : {
68 : : /*
69 : : * This is an indirect pointer --- dereference it
70 : : */
71 : : varatt_indirect redirect;
72 : :
73 : 359 : VARATT_EXTERNAL_GET_POINTER(redirect, attr);
74 : 359 : attr = (varlena *) redirect.pointer;
75 : :
76 : : /* nested indirect Datums aren't allowed */
77 : : Assert(!VARATT_IS_EXTERNAL_INDIRECT(attr));
78 : :
79 : : /* recurse if value is still external in some other way */
80 [ - + ]: 359 : if (VARATT_IS_EXTERNAL(attr))
81 : 0 : return detoast_external_attr(attr);
82 : :
83 : : /*
84 : : * Copy into the caller's memory context, in case caller tries to
85 : : * pfree the result.
86 : : */
87 : 359 : result = (varlena *) palloc(VARSIZE_ANY(attr));
88 : 359 : memcpy(result, attr, VARSIZE_ANY(attr));
89 : : }
90 [ + - ]: 5953 : else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
91 : : {
92 : : /*
93 : : * This is an expanded-object pointer --- get flat format
94 : : */
95 : : ExpandedObjectHeader *eoh;
96 : : Size resultsize;
97 : :
98 : 5953 : eoh = DatumGetEOHP(PointerGetDatum(attr));
99 : 5953 : resultsize = EOH_get_flat_size(eoh);
100 : 5953 : result = (varlena *) palloc(resultsize);
101 : 5953 : EOH_flatten_into(eoh, result, resultsize);
102 : : }
103 : : else
104 : : {
105 : : /*
106 : : * This is a plain value inside of the main tuple - why am I called?
107 : : */
108 : 0 : result = attr;
109 : : }
110 : :
111 : 11317 : return result;
112 : : }
113 : :
114 : :
115 : : /* ----------
116 : : * detoast_attr -
117 : : *
118 : : * Public entry point to get back a toasted value from compression
119 : : * or external storage. The result is always non-extended varlena form.
120 : : *
121 : : * Note some callers assume that if the input is an EXTERNAL or COMPRESSED
122 : : * datum, the result will be a pfree'able chunk.
123 : : * ----------
124 : : */
125 : : varlena *
126 : 34156037 : detoast_attr(varlena *attr)
127 : : {
128 [ + + ]: 34156037 : if (VARATT_IS_EXTERNAL_ONDISK(attr))
129 : : {
130 : : /*
131 : : * This is an externally stored datum --- fetch it back from there
132 : : */
133 : 12662 : attr = toast_fetch_datum(attr);
134 : : /* If it's compressed, decompress it */
135 [ + + ]: 12662 : if (VARATT_IS_COMPRESSED(attr))
136 : : {
137 : 12547 : varlena *tmp = attr;
138 : :
139 : 12547 : attr = toast_decompress_datum(tmp);
140 : 12547 : pfree(tmp);
141 : : }
142 : : }
143 [ + + ]: 34143375 : else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
144 : : {
145 : : /*
146 : : * This is an indirect pointer --- dereference it
147 : : */
148 : : varatt_indirect redirect;
149 : :
150 : 69 : VARATT_EXTERNAL_GET_POINTER(redirect, attr);
151 : 69 : attr = (varlena *) redirect.pointer;
152 : :
153 : : /* nested indirect Datums aren't allowed */
154 : : Assert(!VARATT_IS_EXTERNAL_INDIRECT(attr));
155 : :
156 : : /* recurse in case value is still extended in some other way */
157 : 69 : attr = detoast_attr(attr);
158 : :
159 : : /* if it isn't, we'd better copy it */
160 [ + + ]: 69 : if (attr == (varlena *) redirect.pointer)
161 : : {
162 : : varlena *result;
163 : :
164 : 17 : result = (varlena *) palloc(VARSIZE_ANY(attr));
165 : 17 : memcpy(result, attr, VARSIZE_ANY(attr));
166 : 17 : attr = result;
167 : : }
168 : : }
169 [ + + ]: 34143306 : else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
170 : : {
171 : : /*
172 : : * This is an expanded-object pointer --- get flat format
173 : : */
174 : 5953 : attr = detoast_external_attr(attr);
175 : : /* flatteners are not allowed to produce compressed/short output */
176 : : Assert(!VARATT_IS_EXTENDED(attr));
177 : : }
178 [ + + ]: 34137353 : else if (VARATT_IS_COMPRESSED(attr))
179 : : {
180 : : /*
181 : : * This is a compressed value inside of the main tuple
182 : : */
183 : 92705 : attr = toast_decompress_datum(attr);
184 : : }
185 [ + + ]: 34044648 : else if (VARATT_IS_SHORT(attr))
186 : : {
187 : : /*
188 : : * This is a short-header varlena --- convert to 4-byte header format
189 : : */
190 : 34044631 : Size data_size = VARSIZE_SHORT(attr) - VARHDRSZ_SHORT;
191 : 34044631 : Size new_size = data_size + VARHDRSZ;
192 : : varlena *new_attr;
193 : :
194 : 34044631 : new_attr = (varlena *) palloc(new_size);
195 : 34044631 : SET_VARSIZE(new_attr, new_size);
196 : 34044631 : memcpy(VARDATA(new_attr), VARDATA_SHORT(attr), data_size);
197 : 34044631 : attr = new_attr;
198 : : }
199 : :
200 : 34156037 : return attr;
201 : : }
202 : :
203 : :
204 : : /* ----------
205 : : * detoast_attr_slice -
206 : : *
207 : : * Public entry point to get back part of a toasted value
208 : : * from compression or external storage.
209 : : *
210 : : * sliceoffset is where to start (zero or more)
211 : : * If slicelength < 0, return everything beyond sliceoffset
212 : : * ----------
213 : : */
214 : : varlena *
215 : 2618 : detoast_attr_slice(varlena *attr,
216 : : int32 sliceoffset, int32 slicelength)
217 : : {
218 : : varlena *preslice;
219 : : varlena *result;
220 : : char *attrdata;
221 : : int32 slicelimit;
222 : : int32 attrsize;
223 : :
224 [ - + ]: 2618 : if (sliceoffset < 0)
225 [ # # ]: 0 : elog(ERROR, "invalid sliceoffset: %d", sliceoffset);
226 : :
227 : : /*
228 : : * Compute slicelimit = offset + length, or -1 if we must fetch all of the
229 : : * value. In case of integer overflow, we must fetch all.
230 : : */
231 [ + + ]: 2618 : if (slicelength < 0)
232 : 2129 : slicelimit = -1;
233 [ - + ]: 489 : else if (pg_add_s32_overflow(sliceoffset, slicelength, &slicelimit))
234 : 0 : slicelength = slicelimit = -1;
235 : :
236 [ + + ]: 2618 : if (VARATT_IS_EXTERNAL_ONDISK(attr))
237 : : {
238 : : toast_external_data toast_ext_data;
239 : : int32 extsize;
240 : : uint32 compress_method;
241 : : bool is_compressed;
242 : :
243 : 283 : toast_external_info_get(attr, &toast_ext_data);
244 : 283 : extsize = VARATT_EXTINFO_GET_EXTSIZE(toast_ext_data.extinfo);
245 : 283 : compress_method = VARATT_EXTINFO_GET_COMPRESS_METHOD(toast_ext_data.extinfo);
246 : 283 : is_compressed = VARATT_EXTINFO_IS_COMPRESSED(toast_ext_data.extinfo, toast_ext_data.rawsize);
247 : :
248 : : /* fast path for non-compressed external datums */
249 [ + + ]: 283 : if (!is_compressed)
250 : 175 : return toast_fetch_datum_slice(attr, sliceoffset, slicelength);
251 : :
252 : : /*
253 : : * For compressed values, we need to fetch enough slices to decompress
254 : : * at least the requested part (when a prefix is requested).
255 : : * Otherwise, just fetch all slices.
256 : : */
257 [ + - ]: 108 : if (slicelimit >= 0)
258 : : {
259 : 108 : int32 max_size = extsize;
260 : :
261 : : /*
262 : : * Determine maximum amount of compressed data needed for a prefix
263 : : * of a given length (after decompression).
264 : : *
265 : : * At least for now, if it's LZ4 data, we'll have to fetch the
266 : : * whole thing, because there doesn't seem to be an API call to
267 : : * determine how much compressed data we need to be sure of being
268 : : * able to decompress the required slice.
269 : : */
270 [ + + ]: 108 : if (compress_method == TOAST_PGLZ_COMPRESSION_ID)
271 : 8 : max_size = pglz_maximum_compressed_size(slicelimit, max_size);
272 : :
273 : : /*
274 : : * Fetch enough compressed slices (compressed marker will get set
275 : : * automatically).
276 : : */
277 : 108 : preslice = toast_fetch_datum_slice(attr, 0, max_size);
278 : : }
279 : : else
280 : 0 : preslice = toast_fetch_datum(attr);
281 : : }
282 [ - + ]: 2335 : else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
283 : : {
284 : : varatt_indirect redirect;
285 : :
286 : 0 : VARATT_EXTERNAL_GET_POINTER(redirect, attr);
287 : :
288 : : /* nested indirect Datums aren't allowed */
289 : : Assert(!VARATT_IS_EXTERNAL_INDIRECT(redirect.pointer));
290 : :
291 : 0 : return detoast_attr_slice(redirect.pointer,
292 : : sliceoffset, slicelength);
293 : : }
294 [ - + ]: 2335 : else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
295 : : {
296 : : /* pass it off to detoast_external_attr to flatten */
297 : 0 : preslice = detoast_external_attr(attr);
298 : : }
299 : : else
300 : 2335 : preslice = attr;
301 : :
302 : : Assert(!VARATT_IS_EXTERNAL(preslice));
303 : :
304 [ + + ]: 2443 : if (VARATT_IS_COMPRESSED(preslice))
305 : : {
306 : 300 : varlena *tmp = preslice;
307 : :
308 : : /* Decompress enough to encompass the slice and the offset */
309 [ + + ]: 300 : if (slicelimit >= 0)
310 : 252 : preslice = toast_decompress_datum_slice(tmp, slicelimit);
311 : : else
312 : 48 : preslice = toast_decompress_datum(tmp);
313 : :
314 [ + + ]: 300 : if (tmp != attr)
315 : 108 : pfree(tmp);
316 : : }
317 : :
318 [ + + ]: 2443 : if (VARATT_IS_SHORT(preslice))
319 : : {
320 : 1542 : attrdata = VARDATA_SHORT(preslice);
321 : 1542 : attrsize = VARSIZE_SHORT(preslice) - VARHDRSZ_SHORT;
322 : : }
323 : : else
324 : : {
325 : 901 : attrdata = VARDATA(preslice);
326 : 901 : attrsize = VARSIZE(preslice) - VARHDRSZ;
327 : : }
328 : :
329 : : /* slicing of datum for compressed cases and plain value */
330 : :
331 [ + + ]: 2443 : if (sliceoffset >= attrsize)
332 : : {
333 : 15 : sliceoffset = 0;
334 : 15 : slicelength = 0;
335 : : }
336 [ + + + + ]: 2428 : else if (slicelength < 0 || slicelimit > attrsize)
337 : 2115 : slicelength = attrsize - sliceoffset;
338 : :
339 : 2443 : result = (varlena *) palloc(slicelength + VARHDRSZ);
340 : 2443 : SET_VARSIZE(result, slicelength + VARHDRSZ);
341 : :
342 : 2443 : memcpy(VARDATA(result), attrdata + sliceoffset, slicelength);
343 : :
344 [ + + ]: 2443 : if (preslice != attr)
345 : 300 : pfree(preslice);
346 : :
347 : 2443 : return result;
348 : : }
349 : :
350 : : /* ----------
351 : : * toast_fetch_datum -
352 : : *
353 : : * Reconstruct an in memory Datum from the chunks saved
354 : : * in the toast relation
355 : : * ----------
356 : : */
357 : : static varlena *
358 : 17667 : toast_fetch_datum(varlena *attr)
359 : : {
360 : : Relation toastrel;
361 : : varlena *result;
362 : : toast_external_data toast_ext_data;
363 : : int32 attrsize;
364 : : Oid toastrelid;
365 : : Oid8 valueid;
366 : :
367 [ - + ]: 17667 : if (!VARATT_IS_EXTERNAL_ONDISK(attr))
368 [ # # ]: 0 : elog(ERROR, "toast_fetch_datum shouldn't be called for non-ondisk datums");
369 : :
370 : 17667 : toast_external_info_get(attr, &toast_ext_data);
371 : 17667 : attrsize = VARATT_EXTINFO_GET_EXTSIZE(toast_ext_data.extinfo);
372 : 17667 : toastrelid = toast_ext_data.toastrelid;
373 : 17667 : valueid = toast_ext_data.valueid;
374 : :
375 : 17667 : result = (varlena *) palloc(attrsize + VARHDRSZ);
376 : :
377 [ + + ]: 17667 : if (VARATT_EXTINFO_IS_COMPRESSED(toast_ext_data.extinfo, toast_ext_data.rawsize))
378 : 16761 : SET_VARSIZE_COMPRESSED(result, attrsize + VARHDRSZ);
379 : : else
380 : 906 : SET_VARSIZE(result, attrsize + VARHDRSZ);
381 : :
382 [ - + ]: 17667 : if (attrsize == 0)
383 : 0 : return result; /* Probably shouldn't happen, but just in
384 : : * case. */
385 : :
386 : : /*
387 : : * Open the toast relation and its indexes
388 : : */
389 : 17667 : toastrel = table_open(toastrelid, AccessShareLock);
390 : :
391 : : /* Fetch all chunks */
392 : 17667 : table_relation_fetch_toast_slice(toastrel, valueid,
393 : : attrsize, 0, attrsize, result);
394 : :
395 : : /* Close toast table */
396 : 17667 : table_close(toastrel, AccessShareLock);
397 : :
398 : 17667 : return result;
399 : : }
400 : :
401 : : /* ----------
402 : : * toast_fetch_datum_slice -
403 : : *
404 : : * Reconstruct a segment of a Datum from the chunks saved
405 : : * in the toast relation
406 : : *
407 : : * Note that this function supports non-compressed external datums
408 : : * and compressed external datums (in which case the requested slice
409 : : * has to be a prefix, i.e. sliceoffset has to be 0).
410 : : * ----------
411 : : */
412 : : static varlena *
413 : 283 : toast_fetch_datum_slice(varlena *attr, int32 sliceoffset,
414 : : int32 slicelength)
415 : : {
416 : : Relation toastrel;
417 : : varlena *result;
418 : : toast_external_data toast_ext_data;
419 : : int32 attrsize;
420 : : Oid toastrelid;
421 : : Oid8 valueid;
422 : : bool is_compressed;
423 : :
424 [ - + ]: 283 : if (!VARATT_IS_EXTERNAL_ONDISK(attr))
425 [ # # ]: 0 : elog(ERROR, "toast_fetch_datum_slice shouldn't be called for non-ondisk datums");
426 : :
427 : 283 : toast_external_info_get(attr, &toast_ext_data);
428 : 283 : attrsize = VARATT_EXTINFO_GET_EXTSIZE(toast_ext_data.extinfo);
429 : 283 : toastrelid = toast_ext_data.toastrelid;
430 : 283 : valueid = toast_ext_data.valueid;
431 : 283 : is_compressed = VARATT_EXTINFO_IS_COMPRESSED(toast_ext_data.extinfo, toast_ext_data.rawsize);
432 : :
433 : : /*
434 : : * It's nonsense to fetch slices of a compressed datum unless when it's a
435 : : * prefix -- this isn't lo_* we can't return a compressed datum which is
436 : : * meaningful to toast later.
437 : : */
438 : : Assert(!is_compressed || 0 == sliceoffset);
439 : :
440 [ - + ]: 283 : if (sliceoffset >= attrsize)
441 : : {
442 : 0 : sliceoffset = 0;
443 : 0 : slicelength = 0;
444 : : }
445 : :
446 : : /*
447 : : * When fetching a prefix of a compressed external datum, account for the
448 : : * space required by va_tcinfo, which is stored at the beginning as an
449 : : * int32 value.
450 : : */
451 [ + + + - ]: 283 : if (is_compressed && slicelength > 0)
452 : 108 : slicelength = slicelength + sizeof(int32);
453 : :
454 : : /*
455 : : * Adjust length request if needed. (Note: our sole caller,
456 : : * detoast_attr_slice, protects us against sliceoffset + slicelength
457 : : * overflowing.)
458 : : */
459 [ + + + + ]: 283 : if (((sliceoffset + slicelength) > attrsize) || slicelength < 0)
460 : 212 : slicelength = attrsize - sliceoffset;
461 : :
462 : 283 : result = (varlena *) palloc(slicelength + VARHDRSZ);
463 : :
464 [ + + ]: 283 : if (is_compressed)
465 : 108 : SET_VARSIZE_COMPRESSED(result, slicelength + VARHDRSZ);
466 : : else
467 : 175 : SET_VARSIZE(result, slicelength + VARHDRSZ);
468 : :
469 [ - + ]: 283 : if (slicelength == 0)
470 : 0 : return result; /* Can save a lot of work at this point! */
471 : :
472 : : /* Open the toast relation */
473 : 283 : toastrel = table_open(toastrelid, AccessShareLock);
474 : :
475 : : /* Fetch all chunks */
476 : 283 : table_relation_fetch_toast_slice(toastrel, valueid,
477 : : attrsize, sliceoffset, slicelength,
478 : : result);
479 : :
480 : : /* Close toast table */
481 : 283 : table_close(toastrel, AccessShareLock);
482 : :
483 : 283 : return result;
484 : : }
485 : :
486 : : /* ----------
487 : : * toast_decompress_datum -
488 : : *
489 : : * Decompress a compressed version of a varlena datum
490 : : */
491 : : static varlena *
492 : 105364 : toast_decompress_datum(varlena *attr)
493 : : {
494 : : ToastCompressionId cmid;
495 : :
496 : : Assert(VARATT_IS_COMPRESSED(attr));
497 : :
498 : : /*
499 : : * Fetch the compression method id stored in the compression header and
500 : : * decompress the data using the appropriate decompression routine.
501 : : */
502 : 105364 : cmid = VARDATA_COMPRESSED_GET_COMPRESS_METHOD(attr);
503 [ + + - ]: 105364 : switch (cmid)
504 : : {
505 : 383 : case TOAST_PGLZ_COMPRESSION_ID:
506 : 383 : return pglz_decompress_datum(attr);
507 : 104981 : case TOAST_LZ4_COMPRESSION_ID:
508 : 104981 : return lz4_decompress_datum(attr);
509 : 0 : default:
510 [ # # ]: 0 : elog(ERROR, "invalid compression method id %d", cmid);
511 : : return NULL; /* keep compiler quiet */
512 : : }
513 : : }
514 : :
515 : :
516 : : /* ----------
517 : : * toast_decompress_datum_slice -
518 : : *
519 : : * Decompress the front of a compressed version of a varlena datum.
520 : : * offset handling happens in detoast_attr_slice.
521 : : * Here we just decompress a slice from the front.
522 : : */
523 : : static varlena *
524 : 252 : toast_decompress_datum_slice(varlena *attr, int32 slicelength)
525 : : {
526 : : ToastCompressionId cmid;
527 : :
528 : : Assert(VARATT_IS_COMPRESSED(attr));
529 : :
530 : : /*
531 : : * Some callers may pass a slicelength that's more than the actual
532 : : * decompressed size. If so, just decompress normally. This avoids
533 : : * possibly allocating a larger-than-necessary result object, and may be
534 : : * faster and/or more robust as well. Notably, some versions of liblz4
535 : : * have been seen to give wrong results if passed an output size that is
536 : : * more than the data's true decompressed size.
537 : : */
538 [ + + ]: 252 : if ((uint32) slicelength >= VARDATA_COMPRESSED_GET_EXTSIZE(attr))
539 : 64 : return toast_decompress_datum(attr);
540 : :
541 : : /*
542 : : * Fetch the compression method id stored in the compression header and
543 : : * decompress the data slice using the appropriate decompression routine.
544 : : */
545 : 188 : cmid = VARDATA_COMPRESSED_GET_COMPRESS_METHOD(attr);
546 [ + + - ]: 188 : switch (cmid)
547 : : {
548 : 24 : case TOAST_PGLZ_COMPRESSION_ID:
549 : 24 : return pglz_decompress_datum_slice(attr, slicelength);
550 : 164 : case TOAST_LZ4_COMPRESSION_ID:
551 : 164 : return lz4_decompress_datum_slice(attr, slicelength);
552 : 0 : default:
553 [ # # ]: 0 : elog(ERROR, "invalid compression method id %d", cmid);
554 : : return NULL; /* keep compiler quiet */
555 : : }
556 : : }
557 : :
558 : : /* ----------
559 : : * toast_raw_datum_size -
560 : : *
561 : : * Return the raw (detoasted) size of a varlena datum
562 : : * (including the VARHDRSZ header)
563 : : * ----------
564 : : */
565 : : Size
566 : 19818324 : toast_raw_datum_size(Datum value)
567 : : {
568 : 19818324 : varlena *attr = (varlena *) DatumGetPointer(value);
569 : : Size result;
570 : :
571 [ + + ]: 19818324 : if (VARATT_IS_EXTERNAL_ONDISK(attr))
572 : : {
573 : : /* va_rawsize is the size of the original datum -- including header */
574 : : toast_external_data toast_ext_data;
575 : :
576 : 12076 : toast_external_info_get(attr, &toast_ext_data);
577 : 12076 : result = toast_ext_data.rawsize;
578 : : }
579 [ - + ]: 19806248 : else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
580 : : {
581 : : varatt_indirect toast_pointer;
582 : :
583 : 0 : VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
584 : :
585 : : /* nested indirect Datums aren't allowed */
586 : : Assert(!VARATT_IS_EXTERNAL_INDIRECT(toast_pointer.pointer));
587 : :
588 : 0 : return toast_raw_datum_size(PointerGetDatum(toast_pointer.pointer));
589 : : }
590 [ - + ]: 19806248 : else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
591 : : {
592 : 0 : result = EOH_get_flat_size(DatumGetEOHP(value));
593 : : }
594 [ + + ]: 19806248 : else if (VARATT_IS_COMPRESSED(attr))
595 : : {
596 : : /* here, va_rawsize is just the payload size */
597 : 10541 : result = VARDATA_COMPRESSED_GET_EXTSIZE(attr) + VARHDRSZ;
598 : : }
599 [ + + ]: 19795707 : else if (VARATT_IS_SHORT(attr))
600 : : {
601 : : /*
602 : : * we have to normalize the header length to VARHDRSZ or else the
603 : : * callers of this function will be confused.
604 : : */
605 : 12241311 : result = VARSIZE_SHORT(attr) - VARHDRSZ_SHORT + VARHDRSZ;
606 : : }
607 : : else
608 : : {
609 : : /* plain untoasted datum */
610 : 7554396 : result = VARSIZE(attr);
611 : : }
612 : 19818324 : return result;
613 : : }
614 : :
615 : : /* ----------
616 : : * toast_datum_size
617 : : *
618 : : * Return the physical storage size (possibly compressed) of a varlena datum
619 : : * ----------
620 : : */
621 : : Size
622 : 76 : toast_datum_size(Datum value)
623 : : {
624 : 76 : varlena *attr = (varlena *) DatumGetPointer(value);
625 : : Size result;
626 : :
627 [ + + ]: 76 : if (VARATT_IS_EXTERNAL_ONDISK(attr))
628 : : {
629 : : /*
630 : : * Attribute is stored externally - return the extsize whether
631 : : * compressed or not. We do not count the size of the toast pointer
632 : : * ... should we?
633 : : */
634 : : toast_external_data toast_ext_data;
635 : :
636 : 1 : toast_external_info_get(attr, &toast_ext_data);
637 : 1 : result = VARATT_EXTINFO_GET_EXTSIZE(toast_ext_data.extinfo);
638 : : }
639 [ - + ]: 75 : else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
640 : : {
641 : : varatt_indirect toast_pointer;
642 : :
643 : 0 : VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
644 : :
645 : : /* nested indirect Datums aren't allowed */
646 : : Assert(!VARATT_IS_EXTERNAL_INDIRECT(attr));
647 : :
648 : 0 : return toast_datum_size(PointerGetDatum(toast_pointer.pointer));
649 : : }
650 [ - + ]: 75 : else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
651 : : {
652 : 0 : result = EOH_get_flat_size(DatumGetEOHP(value));
653 : : }
654 [ - + ]: 75 : else if (VARATT_IS_SHORT(attr))
655 : : {
656 : 0 : result = VARSIZE_SHORT(attr);
657 : : }
658 : : else
659 : : {
660 : : /*
661 : : * Attribute is stored inline either compressed or not, just calculate
662 : : * the size of the datum in either case.
663 : : */
664 : 75 : result = VARSIZE(attr);
665 : : }
666 : 76 : return result;
667 : : }
|