1 /*-------------------------------------------------------------------------
2  *
3  * detoast.h
4  *	  Access to compressed and external varlena values.
5  *
6  * Copyright (c) 2000-2021, PostgreSQL Global Development Group
7  *
8  * src/include/access/detoast.h
9  *
10  *-------------------------------------------------------------------------
11  */
12 #ifndef DETOAST_H
13 #define DETOAST_H
14 
15 /*
16  * Macro to fetch the possibly-unaligned contents of an EXTERNAL datum
17  * into a local "struct varatt_external" toast pointer.  This should be
18  * just a memcpy, but some versions of gcc seem to produce broken code
19  * that assumes the datum contents are aligned.  Introducing an explicit
20  * intermediate "varattrib_1b_e *" variable seems to fix it.
21  */
22 #define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr) \
23 do { \
24 	varattrib_1b_e *attre = (varattrib_1b_e *) (attr); \
25 	Assert(VARATT_IS_EXTERNAL(attre)); \
26 	Assert(VARSIZE_EXTERNAL(attre) == sizeof(toast_pointer) + VARHDRSZ_EXTERNAL); \
27 	memcpy(&(toast_pointer), VARDATA_EXTERNAL(attre), sizeof(toast_pointer)); \
28 } while (0)
29 
30 /* Size of an EXTERNAL datum that contains a standard TOAST pointer */
31 #define TOAST_POINTER_SIZE (VARHDRSZ_EXTERNAL + sizeof(varatt_external))
32 
33 /* Size of an EXTERNAL datum that contains an indirection pointer */
34 #define INDIRECT_POINTER_SIZE (VARHDRSZ_EXTERNAL + sizeof(varatt_indirect))
35 
36 /* ----------
37  * detoast_external_attr() -
38  *
39  *		Fetches an external stored attribute from the toast
40  *		relation. Does NOT decompress it, if stored external
41  *		in compressed format.
42  * ----------
43  */
44 extern struct varlena *detoast_external_attr(struct varlena *attr);
45 
46 /* ----------
47  * detoast_attr() -
48  *
49  *		Fully detoasts one attribute, fetching and/or decompressing
50  *		it as needed.
51  * ----------
52  */
53 extern struct varlena *detoast_attr(struct varlena *attr);
54 
55 /* ----------
56  * detoast_attr_slice() -
57  *
58  *		Fetches only the specified portion of an attribute.
59  *		(Handles all cases for attribute storage)
60  * ----------
61  */
62 extern struct varlena *detoast_attr_slice(struct varlena *attr,
63 										  int32 sliceoffset,
64 										  int32 slicelength);
65 
66 /* ----------
67  * toast_raw_datum_size -
68  *
69  *	Return the raw (detoasted) size of a varlena datum
70  * ----------
71  */
72 extern Size toast_raw_datum_size(Datum value);
73 
74 /* ----------
75  * toast_datum_size -
76  *
77  *	Return the storage size of a varlena datum
78  * ----------
79  */
80 extern Size toast_datum_size(Datum value);
81 
82 #endif							/* DETOAST_H */
83