PostgreSQL Source Code  git master
detoast.c File Reference
#include "postgres.h"
#include "access/detoast.h"
#include "access/table.h"
#include "access/tableam.h"
#include "access/toast_internals.h"
#include "common/int.h"
#include "common/pg_lzcompress.h"
#include "utils/expandeddatum.h"
#include "utils/rel.h"
Include dependency graph for detoast.c:

Go to the source code of this file.

Functions

static struct varlenatoast_fetch_datum (struct varlena *attr)
 
static struct varlenatoast_fetch_datum_slice (struct varlena *attr, int32 sliceoffset, int32 slicelength)
 
static struct varlenatoast_decompress_datum (struct varlena *attr)
 
static struct varlenatoast_decompress_datum_slice (struct varlena *attr, int32 slicelength)
 
struct varlenadetoast_external_attr (struct varlena *attr)
 
struct varlenadetoast_attr (struct varlena *attr)
 
struct varlenadetoast_attr_slice (struct varlena *attr, int32 sliceoffset, int32 slicelength)
 
Size toast_raw_datum_size (Datum value)
 
Size toast_datum_size (Datum value)
 

Function Documentation

◆ detoast_attr()

struct varlena* detoast_attr ( struct varlena attr)

Definition at line 116 of file detoast.c.

References Assert, detoast_attr(), detoast_external_attr(), palloc(), pfree(), varatt_indirect::pointer, SET_VARSIZE, toast_decompress_datum(), toast_fetch_datum(), VARATT_EXTERNAL_GET_POINTER, VARATT_IS_COMPRESSED, VARATT_IS_EXTENDED, VARATT_IS_EXTERNAL_EXPANDED, VARATT_IS_EXTERNAL_INDIRECT, VARATT_IS_EXTERNAL_ONDISK, VARATT_IS_SHORT, VARDATA, VARDATA_SHORT, VARHDRSZ, VARHDRSZ_SHORT, VARSIZE_ANY, and VARSIZE_SHORT.

Referenced by detoast_attr(), getdatafield(), pg_detoast_datum(), pg_detoast_datum_copy(), pg_detoast_datum_packed(), toast_flatten_tuple_to_datum(), and toast_tuple_init().

117 {
118  if (VARATT_IS_EXTERNAL_ONDISK(attr))
119  {
120  /*
121  * This is an externally stored datum --- fetch it back from there
122  */
123  attr = toast_fetch_datum(attr);
124  /* If it's compressed, decompress it */
125  if (VARATT_IS_COMPRESSED(attr))
126  {
127  struct varlena *tmp = attr;
128 
129  attr = toast_decompress_datum(tmp);
130  pfree(tmp);
131  }
132  }
133  else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
134  {
135  /*
136  * This is an indirect pointer --- dereference it
137  */
138  struct varatt_indirect redirect;
139 
140  VARATT_EXTERNAL_GET_POINTER(redirect, attr);
141  attr = (struct varlena *) redirect.pointer;
142 
143  /* nested indirect Datums aren't allowed */
145 
146  /* recurse in case value is still extended in some other way */
147  attr = detoast_attr(attr);
148 
149  /* if it isn't, we'd better copy it */
150  if (attr == (struct varlena *) redirect.pointer)
151  {
152  struct varlena *result;
153 
154  result = (struct varlena *) palloc(VARSIZE_ANY(attr));
155  memcpy(result, attr, VARSIZE_ANY(attr));
156  attr = result;
157  }
158  }
159  else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
160  {
161  /*
162  * This is an expanded-object pointer --- get flat format
163  */
164  attr = detoast_external_attr(attr);
165  /* flatteners are not allowed to produce compressed/short output */
166  Assert(!VARATT_IS_EXTENDED(attr));
167  }
168  else if (VARATT_IS_COMPRESSED(attr))
169  {
170  /*
171  * This is a compressed value inside of the main tuple
172  */
173  attr = toast_decompress_datum(attr);
174  }
175  else if (VARATT_IS_SHORT(attr))
176  {
177  /*
178  * This is a short-header varlena --- convert to 4-byte header format
179  */
180  Size data_size = VARSIZE_SHORT(attr) - VARHDRSZ_SHORT;
181  Size new_size = data_size + VARHDRSZ;
182  struct varlena *new_attr;
183 
184  new_attr = (struct varlena *) palloc(new_size);
185  SET_VARSIZE(new_attr, new_size);
186  memcpy(VARDATA(new_attr), VARDATA_SHORT(attr), data_size);
187  attr = new_attr;
188  }
189 
190  return attr;
191 }
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:314
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:312
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:322
#define VARHDRSZ_SHORT
Definition: postgres.h:268
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr)
Definition: detoast.h:32
#define VARHDRSZ
Definition: c.h:627
struct varlena * detoast_external_attr(struct varlena *attr)
Definition: detoast.c:45
struct varlena * detoast_attr(struct varlena *attr)
Definition: detoast.c:116
void pfree(void *pointer)
Definition: mcxt.c:1057
#define VARATT_IS_EXTERNAL_INDIRECT(PTR)
Definition: postgres.h:316
#define VARATT_IS_SHORT(PTR)
Definition: postgres.h:326
#define VARSIZE_SHORT(PTR)
Definition: postgres.h:305
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
static struct varlena * toast_fetch_datum(struct varlena *attr)
Definition: detoast.c:337
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
#define VARATT_IS_EXTENDED(PTR)
Definition: postgres.h:327
#define VARDATA_SHORT(PTR)
Definition: postgres.h:306
void * palloc(Size size)
Definition: mcxt.c:950
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
static struct varlena * toast_decompress_datum(struct varlena *attr)
Definition: detoast.c:465

◆ detoast_attr_slice()

struct varlena* detoast_attr_slice ( struct varlena attr,
int32  sliceoffset,
int32  slicelength 
)

Definition at line 205 of file detoast.c.

References Assert, detoast_attr_slice(), detoast_external_attr(), elog, ERROR, palloc(), pfree(), pg_add_s32_overflow(), pglz_maximum_compressed_size(), varatt_indirect::pointer, SET_VARSIZE, toast_decompress_datum(), toast_decompress_datum_slice(), toast_fetch_datum(), toast_fetch_datum_slice(), varatt_external::va_extsize, VARATT_EXTERNAL_GET_POINTER, VARATT_EXTERNAL_IS_COMPRESSED, VARATT_IS_COMPRESSED, VARATT_IS_EXTERNAL, VARATT_IS_EXTERNAL_EXPANDED, VARATT_IS_EXTERNAL_INDIRECT, VARATT_IS_EXTERNAL_ONDISK, VARATT_IS_SHORT, VARDATA, VARDATA_SHORT, VARHDRSZ, VARHDRSZ_SHORT, VARSIZE, and VARSIZE_SHORT.

Referenced by detoast_attr_slice(), and pg_detoast_datum_slice().

207 {
208  struct varlena *preslice;
209  struct varlena *result;
210  char *attrdata;
211  int32 slicelimit;
212  int32 attrsize;
213 
214  if (sliceoffset < 0)
215  elog(ERROR, "invalid sliceoffset: %d", sliceoffset);
216 
217  /*
218  * Compute slicelimit = offset + length, or -1 if we must fetch all of the
219  * value. In case of integer overflow, we must fetch all.
220  */
221  if (slicelength < 0)
222  slicelimit = -1;
223  else if (pg_add_s32_overflow(sliceoffset, slicelength, &slicelimit))
224  slicelength = slicelimit = -1;
225 
226  if (VARATT_IS_EXTERNAL_ONDISK(attr))
227  {
228  struct varatt_external toast_pointer;
229 
230  VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
231 
232  /* fast path for non-compressed external datums */
233  if (!VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer))
234  return toast_fetch_datum_slice(attr, sliceoffset, slicelength);
235 
236  /*
237  * For compressed values, we need to fetch enough slices to decompress
238  * at least the requested part (when a prefix is requested).
239  * Otherwise, just fetch all slices.
240  */
241  if (slicelimit >= 0)
242  {
243  int32 max_size;
244 
245  /*
246  * Determine maximum amount of compressed data needed for a prefix
247  * of a given length (after decompression).
248  */
249  max_size = pglz_maximum_compressed_size(slicelimit,
250  toast_pointer.va_extsize);
251 
252  /*
253  * Fetch enough compressed slices (compressed marker will get set
254  * automatically).
255  */
256  preslice = toast_fetch_datum_slice(attr, 0, max_size);
257  }
258  else
259  preslice = toast_fetch_datum(attr);
260  }
261  else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
262  {
263  struct varatt_indirect redirect;
264 
265  VARATT_EXTERNAL_GET_POINTER(redirect, attr);
266 
267  /* nested indirect Datums aren't allowed */
268  Assert(!VARATT_IS_EXTERNAL_INDIRECT(redirect.pointer));
269 
270  return detoast_attr_slice(redirect.pointer,
271  sliceoffset, slicelength);
272  }
273  else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
274  {
275  /* pass it off to detoast_external_attr to flatten */
276  preslice = detoast_external_attr(attr);
277  }
278  else
279  preslice = attr;
280 
281  Assert(!VARATT_IS_EXTERNAL(preslice));
282 
283  if (VARATT_IS_COMPRESSED(preslice))
284  {
285  struct varlena *tmp = preslice;
286 
287  /* Decompress enough to encompass the slice and the offset */
288  if (slicelimit >= 0)
289  preslice = toast_decompress_datum_slice(tmp, slicelimit);
290  else
291  preslice = toast_decompress_datum(tmp);
292 
293  if (tmp != attr)
294  pfree(tmp);
295  }
296 
297  if (VARATT_IS_SHORT(preslice))
298  {
299  attrdata = VARDATA_SHORT(preslice);
300  attrsize = VARSIZE_SHORT(preslice) - VARHDRSZ_SHORT;
301  }
302  else
303  {
304  attrdata = VARDATA(preslice);
305  attrsize = VARSIZE(preslice) - VARHDRSZ;
306  }
307 
308  /* slicing of datum for compressed cases and plain value */
309 
310  if (sliceoffset >= attrsize)
311  {
312  sliceoffset = 0;
313  slicelength = 0;
314  }
315  else if (slicelength < 0 || slicelimit > attrsize)
316  slicelength = attrsize - sliceoffset;
317 
318  result = (struct varlena *) palloc(slicelength + VARHDRSZ);
319  SET_VARSIZE(result, slicelength + VARHDRSZ);
320 
321  memcpy(VARDATA(result), attrdata + sliceoffset, slicelength);
322 
323  if (preslice != attr)
324  pfree(preslice);
325 
326  return result;
327 }
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:314
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:312
int32 pglz_maximum_compressed_size(int32 rawsize, int32 total_compressed_size)
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:322
#define VARHDRSZ_SHORT
Definition: postgres.h:268
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr)
Definition: detoast.h:32
#define VARHDRSZ
Definition: c.h:627
struct varlena * detoast_external_attr(struct varlena *attr)
Definition: detoast.c:45
struct varlena * detoast_attr_slice(struct varlena *attr, int32 sliceoffset, int32 slicelength)
Definition: detoast.c:205
signed int int32
Definition: c.h:429
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:313
void pfree(void *pointer)
Definition: mcxt.c:1057
#define VARATT_IS_EXTERNAL_INDIRECT(PTR)
Definition: postgres.h:316
#define ERROR
Definition: elog.h:45
#define VARATT_IS_SHORT(PTR)
Definition: postgres.h:326
#define VARSIZE_SHORT(PTR)
Definition: postgres.h:305
static struct varlena * toast_fetch_datum(struct varlena *attr)
Definition: detoast.c:337
#define Assert(condition)
Definition: c.h:804
static struct varlena * toast_fetch_datum_slice(struct varlena *attr, int32 sliceoffset, int32 slicelength)
Definition: detoast.c:390
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
#define VARDATA_SHORT(PTR)
Definition: postgres.h:306
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:227
#define VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer)
Definition: detoast.h:22
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
static struct varlena * toast_decompress_datum(struct varlena *attr)
Definition: detoast.c:465
static struct varlena * toast_decompress_datum_slice(struct varlena *attr, int32 slicelength)
Definition: detoast.c:493

◆ detoast_external_attr()

struct varlena* detoast_external_attr ( struct varlena attr)

Definition at line 45 of file detoast.c.

References Assert, DatumGetEOHP(), detoast_external_attr(), EOH_flatten_into(), EOH_get_flat_size(), palloc(), varatt_indirect::pointer, PointerGetDatum, toast_fetch_datum(), VARATT_EXTERNAL_GET_POINTER, VARATT_IS_EXTERNAL, VARATT_IS_EXTERNAL_EXPANDED, VARATT_IS_EXTERNAL_INDIRECT, VARATT_IS_EXTERNAL_ONDISK, and VARSIZE_ANY.

Referenced by assign_simple_var(), brin_form_tuple(), detoast_attr(), detoast_attr_slice(), detoast_external_attr(), expanded_record_set_field_internal(), expanded_record_set_fields(), index_form_tuple(), make_tuple_indirect(), toast_build_flattened_tuple(), toast_flatten_tuple(), toast_tuple_init(), and tstoreReceiveSlot_detoast().

46 {
47  struct varlena *result;
48 
49  if (VARATT_IS_EXTERNAL_ONDISK(attr))
50  {
51  /*
52  * This is an external stored plain value
53  */
54  result = toast_fetch_datum(attr);
55  }
56  else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
57  {
58  /*
59  * This is an indirect pointer --- dereference it
60  */
61  struct varatt_indirect redirect;
62 
63  VARATT_EXTERNAL_GET_POINTER(redirect, attr);
64  attr = (struct varlena *) redirect.pointer;
65 
66  /* nested indirect Datums aren't allowed */
68 
69  /* recurse if value is still external in some other way */
70  if (VARATT_IS_EXTERNAL(attr))
71  return detoast_external_attr(attr);
72 
73  /*
74  * Copy into the caller's memory context, in case caller tries to
75  * pfree the result.
76  */
77  result = (struct varlena *) palloc(VARSIZE_ANY(attr));
78  memcpy(result, attr, VARSIZE_ANY(attr));
79  }
80  else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
81  {
82  /*
83  * This is an expanded-object pointer --- get flat format
84  */
86  Size resultsize;
87 
88  eoh = DatumGetEOHP(PointerGetDatum(attr));
89  resultsize = EOH_get_flat_size(eoh);
90  result = (struct varlena *) palloc(resultsize);
91  EOH_flatten_into(eoh, (void *) result, resultsize);
92  }
93  else
94  {
95  /*
96  * This is a plain value inside of the main tuple - why am I called?
97  */
98  result = attr;
99  }
100 
101  return result;
102 }
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:314
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:322
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr)
Definition: detoast.h:32
#define PointerGetDatum(X)
Definition: postgres.h:556
struct varlena * detoast_external_attr(struct varlena *attr)
Definition: detoast.c:45
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:313
#define VARATT_IS_EXTERNAL_INDIRECT(PTR)
Definition: postgres.h:316
Size EOH_get_flat_size(ExpandedObjectHeader *eohptr)
Definition: expandeddatum.c:75
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
void EOH_flatten_into(ExpandedObjectHeader *eohptr, void *result, Size allocated_size)
Definition: expandeddatum.c:81
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
static struct varlena * toast_fetch_datum(struct varlena *attr)
Definition: detoast.c:337
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
void * palloc(Size size)
Definition: mcxt.c:950
Definition: c.h:621

◆ toast_datum_size()

Size toast_datum_size ( Datum  value)

Definition at line 577 of file detoast.c.

References Assert, DatumGetEOHP(), DatumGetPointer, EOH_get_flat_size(), varatt_indirect::pointer, PointerGetDatum, toast_datum_size(), varatt_external::va_extsize, VARATT_EXTERNAL_GET_POINTER, VARATT_IS_EXTERNAL_EXPANDED, VARATT_IS_EXTERNAL_INDIRECT, VARATT_IS_EXTERNAL_ONDISK, VARATT_IS_SHORT, VARSIZE, and VARSIZE_SHORT.

Referenced by pg_column_size(), and toast_datum_size().

578 {
579  struct varlena *attr = (struct varlena *) DatumGetPointer(value);
580  Size result;
581 
582  if (VARATT_IS_EXTERNAL_ONDISK(attr))
583  {
584  /*
585  * Attribute is stored externally - return the extsize whether
586  * compressed or not. We do not count the size of the toast pointer
587  * ... should we?
588  */
589  struct varatt_external toast_pointer;
590 
591  VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
592  result = toast_pointer.va_extsize;
593  }
594  else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
595  {
596  struct varatt_indirect toast_pointer;
597 
598  VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
599 
600  /* nested indirect Datums aren't allowed */
602 
603  return toast_datum_size(PointerGetDatum(toast_pointer.pointer));
604  }
605  else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
606  {
608  }
609  else if (VARATT_IS_SHORT(attr))
610  {
611  result = VARSIZE_SHORT(attr);
612  }
613  else
614  {
615  /*
616  * Attribute is stored inline either compressed or not, just calculate
617  * the size of the datum in either case.
618  */
619  result = VARSIZE(attr);
620  }
621  return result;
622 }
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:314
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:322
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr)
Definition: detoast.h:32
#define PointerGetDatum(X)
Definition: postgres.h:556
static struct @141 value
Size toast_datum_size(Datum value)
Definition: detoast.c:577
#define VARATT_IS_EXTERNAL_INDIRECT(PTR)
Definition: postgres.h:316
#define VARATT_IS_SHORT(PTR)
Definition: postgres.h:326
Size EOH_get_flat_size(ExpandedObjectHeader *eohptr)
Definition: expandeddatum.c:75
#define VARSIZE_SHORT(PTR)
Definition: postgres.h:305
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
#define DatumGetPointer(X)
Definition: postgres.h:549
Definition: c.h:621

◆ toast_decompress_datum()

static struct varlena * toast_decompress_datum ( struct varlena attr)
static

Definition at line 465 of file detoast.c.

References Assert, elog, ERROR, palloc(), pglz_decompress(), SET_VARSIZE, TOAST_COMPRESS_RAWDATA, TOAST_COMPRESS_RAWSIZE, TOAST_COMPRESS_SIZE, VARATT_IS_COMPRESSED, VARDATA, and VARHDRSZ.

Referenced by detoast_attr(), and detoast_attr_slice().

466 {
467  struct varlena *result;
468 
470 
471  result = (struct varlena *)
474 
476  TOAST_COMPRESS_SIZE(attr),
477  VARDATA(result),
478  TOAST_COMPRESS_RAWSIZE(attr), true) < 0)
479  elog(ERROR, "compressed data is corrupted");
480 
481  return result;
482 }
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:312
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:627
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
#define ERROR
Definition: elog.h:45
#define TOAST_COMPRESS_RAWDATA(ptr)
#define TOAST_COMPRESS_SIZE(ptr)
#define Assert(condition)
Definition: c.h:804
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:227
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define TOAST_COMPRESS_RAWSIZE(ptr)

◆ toast_decompress_datum_slice()

static struct varlena * toast_decompress_datum_slice ( struct varlena attr,
int32  slicelength 
)
static

Definition at line 493 of file detoast.c.

References Assert, elog, ERROR, palloc(), pglz_decompress(), SET_VARSIZE, TOAST_COMPRESS_HDRSZ, TOAST_COMPRESS_RAWDATA, VARATT_IS_COMPRESSED, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by detoast_attr_slice().

494 {
495  struct varlena *result;
496  int32 rawsize;
497 
499 
500  result = (struct varlena *) palloc(slicelength + VARHDRSZ);
501 
502  rawsize = pglz_decompress(TOAST_COMPRESS_RAWDATA(attr),
504  VARDATA(result),
505  slicelength, false);
506  if (rawsize < 0)
507  elog(ERROR, "compressed data is corrupted");
508 
509  SET_VARSIZE(result, rawsize + VARHDRSZ);
510  return result;
511 }
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:312
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:627
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:45
#define TOAST_COMPRESS_RAWDATA(ptr)
#define Assert(condition)
Definition: c.h:804
#define TOAST_COMPRESS_HDRSZ
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:227
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ toast_fetch_datum()

static struct varlena * toast_fetch_datum ( struct varlena attr)
static

Definition at line 337 of file detoast.c.

References AccessShareLock, elog, ERROR, palloc(), SET_VARSIZE, SET_VARSIZE_COMPRESSED, table_close(), table_open(), table_relation_fetch_toast_slice(), varatt_external::va_extsize, varatt_external::va_toastrelid, varatt_external::va_valueid, VARATT_EXTERNAL_GET_POINTER, VARATT_EXTERNAL_IS_COMPRESSED, VARATT_IS_EXTERNAL_ONDISK, and VARHDRSZ.

Referenced by detoast_attr(), detoast_attr_slice(), and detoast_external_attr().

338 {
339  Relation toastrel;
340  struct varlena *result;
341  struct varatt_external toast_pointer;
342  int32 attrsize;
343 
344  if (!VARATT_IS_EXTERNAL_ONDISK(attr))
345  elog(ERROR, "toast_fetch_datum shouldn't be called for non-ondisk datums");
346 
347  /* Must copy to access aligned fields */
348  VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
349 
350  attrsize = toast_pointer.va_extsize;
351 
352  result = (struct varlena *) palloc(attrsize + VARHDRSZ);
353 
354  if (VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer))
355  SET_VARSIZE_COMPRESSED(result, attrsize + VARHDRSZ);
356  else
357  SET_VARSIZE(result, attrsize + VARHDRSZ);
358 
359  if (attrsize == 0)
360  return result; /* Probably shouldn't happen, but just in
361  * case. */
362 
363  /*
364  * Open the toast relation and its indexes
365  */
366  toastrel = table_open(toast_pointer.va_toastrelid, AccessShareLock);
367 
368  /* Fetch all chunks */
369  table_relation_fetch_toast_slice(toastrel, toast_pointer.va_valueid,
370  attrsize, 0, attrsize, result);
371 
372  /* Close toast table */
373  table_close(toastrel, AccessShareLock);
374 
375  return result;
376 }
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:314
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr)
Definition: detoast.h:32
#define VARHDRSZ
Definition: c.h:627
static void table_relation_fetch_toast_slice(Relation toastrel, Oid valueid, int32 attrsize, int32 sliceoffset, int32 slicelength, struct varlena *result)
Definition: tableam.h:1886
#define AccessShareLock
Definition: lockdefs.h:36
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:45
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:227
#define SET_VARSIZE_COMPRESSED(PTR, len)
Definition: postgres.h:331
#define VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer)
Definition: detoast.h:22
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ toast_fetch_datum_slice()

static struct varlena * toast_fetch_datum_slice ( struct varlena attr,
int32  sliceoffset,
int32  slicelength 
)
static

Definition at line 390 of file detoast.c.

References AccessShareLock, Assert, elog, ERROR, palloc(), SET_VARSIZE, SET_VARSIZE_COMPRESSED, table_close(), table_open(), table_relation_fetch_toast_slice(), varatt_external::va_extsize, varatt_external::va_toastrelid, varatt_external::va_valueid, VARATT_EXTERNAL_GET_POINTER, VARATT_EXTERNAL_IS_COMPRESSED, VARATT_IS_EXTERNAL_ONDISK, and VARHDRSZ.

Referenced by detoast_attr_slice().

392 {
393  Relation toastrel;
394  struct varlena *result;
395  struct varatt_external toast_pointer;
396  int32 attrsize;
397 
398  if (!VARATT_IS_EXTERNAL_ONDISK(attr))
399  elog(ERROR, "toast_fetch_datum_slice shouldn't be called for non-ondisk datums");
400 
401  /* Must copy to access aligned fields */
402  VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
403 
404  /*
405  * It's nonsense to fetch slices of a compressed datum unless when it's a
406  * prefix -- this isn't lo_* we can't return a compressed datum which is
407  * meaningful to toast later.
408  */
409  Assert(!VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer) || 0 == sliceoffset);
410 
411  attrsize = toast_pointer.va_extsize;
412 
413  if (sliceoffset >= attrsize)
414  {
415  sliceoffset = 0;
416  slicelength = 0;
417  }
418 
419  /*
420  * When fetching a prefix of a compressed external datum, account for the
421  * rawsize tracking amount of raw data, which is stored at the beginning
422  * as an int32 value).
423  */
424  if (VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer) && slicelength > 0)
425  slicelength = slicelength + sizeof(int32);
426 
427  /*
428  * Adjust length request if needed. (Note: our sole caller,
429  * detoast_attr_slice, protects us against sliceoffset + slicelength
430  * overflowing.)
431  */
432  if (((sliceoffset + slicelength) > attrsize) || slicelength < 0)
433  slicelength = attrsize - sliceoffset;
434 
435  result = (struct varlena *) palloc(slicelength + VARHDRSZ);
436 
437  if (VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer))
438  SET_VARSIZE_COMPRESSED(result, slicelength + VARHDRSZ);
439  else
440  SET_VARSIZE(result, slicelength + VARHDRSZ);
441 
442  if (slicelength == 0)
443  return result; /* Can save a lot of work at this point! */
444 
445  /* Open the toast relation */
446  toastrel = table_open(toast_pointer.va_toastrelid, AccessShareLock);
447 
448  /* Fetch all chunks */
449  table_relation_fetch_toast_slice(toastrel, toast_pointer.va_valueid,
450  attrsize, sliceoffset, slicelength,
451  result);
452 
453  /* Close toast table */
454  table_close(toastrel, AccessShareLock);
455 
456  return result;
457 }
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:314
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr)
Definition: detoast.h:32
#define VARHDRSZ
Definition: c.h:627
static void table_relation_fetch_toast_slice(Relation toastrel, Oid valueid, int32 attrsize, int32 sliceoffset, int32 slicelength, struct varlena *result)
Definition: tableam.h:1886
#define AccessShareLock
Definition: lockdefs.h:36
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:45
#define Assert(condition)
Definition: c.h:804
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:227
#define SET_VARSIZE_COMPRESSED(PTR, len)
Definition: postgres.h:331
#define VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer)
Definition: detoast.h:22
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ toast_raw_datum_size()

Size toast_raw_datum_size ( Datum  value)

Definition at line 521 of file detoast.c.

References Assert, DatumGetEOHP(), DatumGetPointer, EOH_get_flat_size(), varatt_indirect::pointer, PointerGetDatum, toast_raw_datum_size(), varatt_external::va_rawsize, VARATT_EXTERNAL_GET_POINTER, VARATT_IS_COMPRESSED, VARATT_IS_EXTERNAL_EXPANDED, VARATT_IS_EXTERNAL_INDIRECT, VARATT_IS_EXTERNAL_ONDISK, VARATT_IS_SHORT, VARHDRSZ, VARHDRSZ_SHORT, VARRAWSIZE_4B_C, VARSIZE, and VARSIZE_SHORT.

Referenced by bpcharoctetlen(), build_sorted_items(), byteaeq(), byteane(), byteaoctetlen(), compute_array_stats(), compute_distinct_stats(), compute_scalar_stats(), datum_image_eq(), record_image_cmp(), text_length(), text_starts_with(), texteq(), textne(), textoctetlen(), and toast_raw_datum_size().

522 {
523  struct varlena *attr = (struct varlena *) DatumGetPointer(value);
524  Size result;
525 
526  if (VARATT_IS_EXTERNAL_ONDISK(attr))
527  {
528  /* va_rawsize is the size of the original datum -- including header */
529  struct varatt_external toast_pointer;
530 
531  VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
532  result = toast_pointer.va_rawsize;
533  }
534  else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
535  {
536  struct varatt_indirect toast_pointer;
537 
538  VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
539 
540  /* nested indirect Datums aren't allowed */
541  Assert(!VARATT_IS_EXTERNAL_INDIRECT(toast_pointer.pointer));
542 
543  return toast_raw_datum_size(PointerGetDatum(toast_pointer.pointer));
544  }
545  else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
546  {
548  }
549  else if (VARATT_IS_COMPRESSED(attr))
550  {
551  /* here, va_rawsize is just the payload size */
552  result = VARRAWSIZE_4B_C(attr) + VARHDRSZ;
553  }
554  else if (VARATT_IS_SHORT(attr))
555  {
556  /*
557  * we have to normalize the header length to VARHDRSZ or else the
558  * callers of this function will be confused.
559  */
560  result = VARSIZE_SHORT(attr) - VARHDRSZ_SHORT + VARHDRSZ;
561  }
562  else
563  {
564  /* plain untoasted datum */
565  result = VARSIZE(attr);
566  }
567  return result;
568 }
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:314
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:312
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:322
#define VARHDRSZ_SHORT
Definition: postgres.h:268
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr)
Definition: detoast.h:32
#define PointerGetDatum(X)
Definition: postgres.h:556
#define VARHDRSZ
Definition: c.h:627
static struct @141 value
#define VARATT_IS_EXTERNAL_INDIRECT(PTR)
Definition: postgres.h:316
#define VARATT_IS_SHORT(PTR)
Definition: postgres.h:326
Size EOH_get_flat_size(ExpandedObjectHeader *eohptr)
Definition: expandeddatum.c:75
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:521
#define VARSIZE_SHORT(PTR)
Definition: postgres.h:305
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
#define VARRAWSIZE_4B_C(PTR)
Definition: postgres.h:283
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
#define DatumGetPointer(X)
Definition: postgres.h:549
Definition: c.h:621