1/*-------------------------------------------------------------------------
4 * variable-length datatypes (TOAST support)
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1995, Regents of the University of California
10 * src/include/varatt.h
12 *-------------------------------------------------------------------------
19 * struct varatt_external is a traditional "TOAST pointer", that is, the
20 * information needed to fetch a Datum stored out-of-line in a TOAST table.
21 * The data is compressed if and only if the external size stored in
22 * va_extinfo is less than va_rawsize - VARHDRSZ.
24 * This struct must not contain any padding, because we sometimes compare
25 * these pointers using memcmp.
27 * Note that this information is stored unaligned within actual tuples, so
28 * you need to memcpy from the tuple into a local struct variable before
29 * you can look at these fields! (The reason we use memcmp is to avoid
30 * having to do that just to detect equality of two TOAST pointers...)
36 * compression method */
42 * These macros define the "saved size" portion of va_extinfo. Its remaining
43 * two high-order bits identify the compression method.
45 #define VARLENA_EXTSIZE_BITS 30
46 #define VARLENA_EXTSIZE_MASK ((1U << VARLENA_EXTSIZE_BITS) - 1)
49 * struct varatt_indirect is a "TOAST pointer" representing an out-of-line
50 * Datum that's stored in memory, not in an external toast relation.
51 * The creator of such a Datum is entirely responsible that the referenced
52 * storage survives for as long as referencing pointer Datums can exist.
54 * Note that just as for struct varatt_external, this struct is stored
55 * unaligned within any containing tuple.
63 * struct varatt_expanded is a "TOAST pointer" representing an out-of-line
64 * Datum that is stored in memory, in some type-specific, not necessarily
65 * physically contiguous format that is convenient for computation not
66 * storage. APIs for this, in particular the definition of struct
67 * ExpandedObjectHeader, are in src/include/utils/expandeddatum.h.
69 * Note that just as for struct varatt_external, this struct is stored
70 * unaligned within any containing tuple.
80 * Type tag for the various sorts of "TOAST pointer" datums. The peculiar
81 * value for VARTAG_ONDISK comes from a requirement for on-disk compatibility
82 * with a previous notion that the tag field was the pointer datum's length.
92/* Is a TOAST pointer either type of expanded-object pointer? */
93/* this test relies on the specific tag values above */
100/* Size of the data part of a "TOAST pointer" datum */
118 * These structs describe the header of a varlena object that may have been
119 * TOASTed. Generally, don't reference these structs directly, but use the
120 * functions and macros below.
122 * We use separate structs for the aligned and unaligned cases because the
123 * compiler might otherwise think it could generate code that assumes
124 * alignment while touching fields of a 1-byte-header varlena.
128 struct /* Normal varlena (4-byte length) */
133 struct /* Compressed-in-line format */
137 * compression method; see va_extinfo */
148/* TOAST pointers are a subset of varattrib_1b with an identifying tag byte */
157 * Bit layouts for varlena headers on big-endian machines:
159 * 00xxxxxx 4-byte length word, aligned, uncompressed data (up to 1G)
160 * 01xxxxxx 4-byte length word, aligned, *compressed* data (up to 1G)
161 * 10000000 1-byte length word, unaligned, TOAST pointer
162 * 1xxxxxxx 1-byte length word, unaligned, uncompressed data (up to 126b)
164 * Bit layouts for varlena headers on little-endian machines:
166 * xxxxxx00 4-byte length word, aligned, uncompressed data (up to 1G)
167 * xxxxxx10 4-byte length word, aligned, *compressed* data (up to 1G)
168 * 00000001 1-byte length word, unaligned, TOAST pointer
169 * xxxxxxx1 1-byte length word, unaligned, uncompressed data (up to 126b)
171 * The "xxx" bits are the length field (which includes itself in all cases).
172 * In the big-endian case we mask to extract the length, in the little-endian
173 * case we shift. Note that in both cases the flag bits are in the physically
174 * first byte. Also, it is not possible for a 1-byte length word to be zero;
175 * this lets us disambiguate alignment padding bytes from the start of an
176 * unaligned datum. (We now *require* pad bytes to be filled with zero!)
178 * In TOAST pointers the va_tag field (see varattrib_1b_e) is used to discern
179 * the specific type and length of the pointer datum.
183 * Endian-dependent macros. These are considered internal --- use the
184 * external functions below instead of using these directly. All of these
185 * expect an argument that is a pointer, not a Datum. Some of them have
186 * multiple-evaluation hazards, too.
188 * Note: IS_1B is true for external toast records but VARSIZE_1B will return 0
189 * for such records. Hence you should usually check for IS_EXTERNAL before
190 * checking for IS_1B.
193#ifdef WORDS_BIGENDIAN
195#define VARATT_IS_4B(PTR) \
196 ((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x00)
197#define VARATT_IS_4B_U(PTR) \
198 ((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x00)
199#define VARATT_IS_4B_C(PTR) \
200 ((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x40)
201#define VARATT_IS_1B(PTR) \
202 ((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x80)
203#define VARATT_IS_1B_E(PTR) \
204 ((((varattrib_1b *) (PTR))->va_header) == 0x80)
205#define VARATT_NOT_PAD_BYTE(PTR) \
206 (*((uint8 *) (PTR)) != 0)
208/* VARSIZE_4B() should only be used on known-aligned data */
209#define VARSIZE_4B(PTR) \
210 (((varattrib_4b *) (PTR))->va_4byte.va_header & 0x3FFFFFFF)
211#define VARSIZE_1B(PTR) \
212 (((varattrib_1b *) (PTR))->va_header & 0x7F)
213#define VARTAG_1B_E(PTR) \
214 ((vartag_external) ((varattrib_1b_e *) (PTR))->va_tag)
216#define SET_VARSIZE_4B(PTR,len) \
217 (((varattrib_4b *) (PTR))->va_4byte.va_header = (len) & 0x3FFFFFFF)
218#define SET_VARSIZE_4B_C(PTR,len) \
219 (((varattrib_4b *) (PTR))->va_4byte.va_header = ((len) & 0x3FFFFFFF) | 0x40000000)
220#define SET_VARSIZE_1B(PTR,len) \
221 (((varattrib_1b *) (PTR))->va_header = (len) | 0x80)
222#define SET_VARTAG_1B_E(PTR,tag) \
223 (((varattrib_1b_e *) (PTR))->va_header = 0x80, \
224 ((varattrib_1b_e *) (PTR))->va_tag = (tag))
226#else /* !WORDS_BIGENDIAN */
228 #define VARATT_IS_4B(PTR) \
229 ((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x00)
230 #define VARATT_IS_4B_U(PTR) \
231 ((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x00)
232 #define VARATT_IS_4B_C(PTR) \
233 ((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x02)
234 #define VARATT_IS_1B(PTR) \
235 ((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x01)
236 #define VARATT_IS_1B_E(PTR) \
237 ((((varattrib_1b *) (PTR))->va_header) == 0x01)
238 #define VARATT_NOT_PAD_BYTE(PTR) \
239 (*((uint8 *) (PTR)) != 0)
241/* VARSIZE_4B() should only be used on known-aligned data */
242 #define VARSIZE_4B(PTR) \
243 ((((varattrib_4b *) (PTR))->va_4byte.va_header >> 2) & 0x3FFFFFFF)
244 #define VARSIZE_1B(PTR) \
245 ((((varattrib_1b *) (PTR))->va_header >> 1) & 0x7F)
246 #define VARTAG_1B_E(PTR) \
247 ((vartag_external) ((varattrib_1b_e *) (PTR))->va_tag)
249 #define SET_VARSIZE_4B(PTR,len) \
250 (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2))
251 #define SET_VARSIZE_4B_C(PTR,len) \
252 (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2) | 0x02)
253 #define SET_VARSIZE_1B(PTR,len) \
254 (((varattrib_1b *) (PTR))->va_header = (((uint8) (len)) << 1) | 0x01)
255 #define SET_VARTAG_1B_E(PTR,tag) \
256 (((varattrib_1b_e *) (PTR))->va_header = 0x01, \
257 ((varattrib_1b_e *) (PTR))->va_tag = (tag))
259#endif /* WORDS_BIGENDIAN */
261 #define VARDATA_4B(PTR) (((varattrib_4b *) (PTR))->va_4byte.va_data)
262 #define VARDATA_4B_C(PTR) (((varattrib_4b *) (PTR))->va_compressed.va_data)
263 #define VARDATA_1B(PTR) (((varattrib_1b *) (PTR))->va_data)
264 #define VARDATA_1B_E(PTR) (((varattrib_1b_e *) (PTR))->va_data)
267 * Externally visible TOAST functions and macros begin here. All of these
268 * were originally macros, accounting for the upper-case naming.
270 * Most of these functions accept a pointer to a value of a toastable data
271 * type. The caller's variable might be declared "text *" or the like,
272 * so we use "void *" here. Callers that are working with a Datum variable
273 * must apply DatumGetPointer before calling these functions.
276 #define VARHDRSZ_EXTERNAL offsetof(varattrib_1b_e, va_data)
277 #define VARHDRSZ_COMPRESSED offsetof(varattrib_4b, va_compressed.va_data)
278 #define VARHDRSZ_SHORT offsetof(varattrib_1b, va_data)
279 #define VARATT_SHORT_MAX 0x7F
282 * In consumers oblivious to data alignment, call PG_DETOAST_DATUM_PACKED(),
283 * VARDATA_ANY(), VARSIZE_ANY() and VARSIZE_ANY_EXHDR(). Elsewhere, call
284 * PG_DETOAST_DATUM(), VARDATA() and VARSIZE(). Directly fetching an int16,
285 * int32 or wider field in the struct representing the datum layout requires
286 * aligned data. memcpy() is alignment-oblivious, as are most operations on
287 * datatypes, such as text, whose layout struct contains only char fields.
289 * Code assembling a new datum should call VARDATA() and SET_VARSIZE().
290 * (Datums begin life untoasted.)
292 * Other functions here should usually be used only by tuple assembly/disassembly
293 * code and code that specifically wants to work with still-toasted Datums.
296/* Size of a known-not-toasted varlena datum, including header */
303/* Start of data area of a known-not-toasted varlena datum */
310/* Size of a known-short-header varlena datum, including header */
317/* Start of data area of a known-short-header varlena datum */
324/* Type tag of a "TOAST pointer" datum */
331/* Size of a "TOAST pointer" datum, including header */
338/* Start of data area of a "TOAST pointer" datum */
345/* Is varlena datum in inline-compressed format? */
352/* Is varlena datum a "TOAST pointer" datum? */
359/* Is varlena datum a pointer to on-disk toasted data? */
366/* Is varlena datum an indirect pointer? */
373/* Is varlena datum a read-only pointer to an expanded object? */
380/* Is varlena datum a read-write pointer to an expanded object? */
387/* Is varlena datum either type of pointer to an expanded object? */
394/* Is varlena datum a "TOAST pointer", but not for an expanded object? */
401/* Is varlena datum a short-header datum? */
408/* Is varlena datum not in traditional (4-byte-header, uncompressed) format? */
415/* Is varlena datum short enough to convert to short-header format? */
423/* Size that datum will have in short-header format, including header */
430/* Set the size (including header) of a 4-byte-header varlena datum */
437/* Set the size (including header) of a short-header varlena datum */
444/* Set the size (including header) of an inline-compressed varlena datum */
451/* Set the type tag of a "TOAST pointer" datum */
458/* Size of a varlena datum of any format, including header */
470/* Size of a varlena datum of any format, excluding header */
482/* Start of data area of a plain or short-header varlena datum */
483/* caution: this will not work on an external or compressed-in-line Datum */
484/* caution: this will return a possibly unaligned pointer */
491/* Decompressed size of a compressed-in-line varlena datum */
498/* Compression method of a compressed-in-line varlena datum */
505/* Same for external Datums; but note argument is a struct varatt_external */
518/* Set size and compress method of an externally-stored varlena datum */
519/* This has to remain a macro; beware multiple evaluations! */
520 #define VARATT_EXTERNAL_SET_SIZE_AND_COMPRESS_METHOD(toast_pointer, len, cm) \
522 Assert((cm) == TOAST_PGLZ_COMPRESSION_ID || \
523 (cm) == TOAST_LZ4_COMPRESSION_ID); \
524 ((toast_pointer).va_extinfo = \
525 (len) | ((uint32) (cm) << VARLENA_EXTSIZE_BITS)); \
529 * Testing whether an externally-stored value is compressed now requires
530 * comparing size stored in va_extinfo (the actual length of the external data)
531 * to rawsize (the original uncompressed datum's size). The latter includes
532 * VARHDRSZ overhead, the former doesn't. We never use compression unless it
533 * actually saves space, so we expect either equality or less-than.
#define FLEXIBLE_ARRAY_MEMBER
Assert(PointerIsAligned(start, uint64))
ExpandedObjectHeader * eohptr
#define VARLENA_EXTSIZE_BITS
static bool VARATT_IS_SHORT(const void *PTR)
#define VARATT_IS_4B_C(PTR)
#define SET_VARSIZE_4B(PTR, len)
static Size VARDATA_COMPRESSED_GET_EXTSIZE(const void *PTR)
static void SET_VARSIZE_COMPRESSED(void *PTR, Size len)
#define VARATT_IS_4B_U(PTR)
static bool VARATT_IS_EXTERNAL_ONDISK(const void *PTR)
static Size VARATT_EXTERNAL_GET_EXTSIZE(struct varatt_external toast_pointer)
static Size VARSIZE_ANY(const void *PTR)
static bool VARATT_CAN_MAKE_SHORT(const void *PTR)
static bool VARATT_IS_EXTENDED(const void *PTR)
static bool VARTAG_IS_EXPANDED(vartag_external tag)
static uint32 VARDATA_COMPRESSED_GET_COMPRESS_METHOD(const void *PTR)
#define VARATT_IS_1B_E(PTR)
static Size VARSIZE_ANY_EXHDR(const void *PTR)
static bool VARATT_IS_EXTERNAL_EXPANDED_RW(const void *PTR)
static bool VARATT_IS_EXTERNAL(const void *PTR)
static char * VARDATA_EXTERNAL(const void *PTR)
static bool VARATT_IS_EXTERNAL_INDIRECT(const void *PTR)
static Size VARSIZE(const void *PTR)
static char * VARDATA(const void *PTR)
static char * VARDATA_ANY(const void *PTR)
static Size VARATT_CONVERTED_SHORT_SIZE(const void *PTR)
static void SET_VARTAG_EXTERNAL(void *PTR, vartag_external tag)
#define SET_VARSIZE_1B(PTR, len)
struct varatt_external varatt_external
static bool VARATT_IS_EXTERNAL_NON_EXPANDED(const void *PTR)
#define VARDATA_1B_E(PTR)
static bool VARATT_IS_COMPRESSED(const void *PTR)
static vartag_external VARTAG_EXTERNAL(const void *PTR)
static Size VARSIZE_EXTERNAL(const void *PTR)
#define VARLENA_EXTSIZE_MASK
struct varatt_indirect varatt_indirect
static bool VARATT_IS_EXTERNAL_EXPANDED(const void *PTR)
#define SET_VARTAG_1B_E(PTR, tag)
static void SET_VARSIZE_SHORT(void *PTR, Size len)
struct varatt_expanded varatt_expanded
static Size VARTAG_SIZE(vartag_external tag)
static bool VARATT_IS_EXTERNAL_EXPANDED_RO(const void *PTR)
#define SET_VARSIZE_4B_C(PTR, len)
static char * VARDATA_SHORT(const void *PTR)
#define VARATT_IS_1B(PTR)
static uint32 VARATT_EXTERNAL_GET_COMPRESS_METHOD(struct varatt_external toast_pointer)
#define VARHDRSZ_EXTERNAL
static bool VARATT_EXTERNAL_IS_COMPRESSED(struct varatt_external toast_pointer)
static void SET_VARSIZE(void *PTR, Size len)
static Size VARSIZE_SHORT(const void *PTR)