PostgreSQL Source Code  git master
toast_helper.h File Reference
#include "utils/rel.h"
Include dependency graph for toast_helper.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ToastAttrInfo
 
struct  ToastTupleContext
 

Macros

#define TOAST_NEEDS_DELETE_OLD   0x0001
 
#define TOAST_NEEDS_FREE   0x0002
 
#define TOAST_HAS_NULLS   0x0004
 
#define TOAST_NEEDS_CHANGE   0x0008
 
#define TOASTCOL_NEEDS_DELETE_OLD   TOAST_NEEDS_DELETE_OLD
 
#define TOASTCOL_NEEDS_FREE   TOAST_NEEDS_FREE
 
#define TOASTCOL_IGNORE   0x0010
 
#define TOASTCOL_INCOMPRESSIBLE   0x0020
 

Functions

void toast_tuple_init (ToastTupleContext *ttc)
 
int toast_tuple_find_biggest_attribute (ToastTupleContext *ttc, bool for_compression, bool check_main)
 
void toast_tuple_try_compression (ToastTupleContext *ttc, int attribute)
 
void toast_tuple_externalize (ToastTupleContext *ttc, int attribute, int options)
 
void toast_tuple_cleanup (ToastTupleContext *ttc)
 
void toast_delete_external (Relation rel, Datum *values, bool *isnull, bool is_speculative)
 

Macro Definition Documentation

◆ TOAST_HAS_NULLS

#define TOAST_HAS_NULLS   0x0004

Definition at line 79 of file toast_helper.h.

Referenced by heap_toast_insert_or_update(), and toast_tuple_init().

◆ TOAST_NEEDS_CHANGE

#define TOAST_NEEDS_CHANGE   0x0008

◆ TOAST_NEEDS_DELETE_OLD

#define TOAST_NEEDS_DELETE_OLD   0x0001

Definition at line 77 of file toast_helper.h.

Referenced by toast_tuple_cleanup(), and toast_tuple_init().

◆ TOAST_NEEDS_FREE

#define TOAST_NEEDS_FREE   0x0002

◆ TOASTCOL_IGNORE

#define TOASTCOL_IGNORE   0x0010

◆ TOASTCOL_INCOMPRESSIBLE

#define TOASTCOL_INCOMPRESSIBLE   0x0020

◆ TOASTCOL_NEEDS_DELETE_OLD

#define TOASTCOL_NEEDS_DELETE_OLD   TOAST_NEEDS_DELETE_OLD

Definition at line 98 of file toast_helper.h.

Referenced by toast_tuple_cleanup(), and toast_tuple_init().

◆ TOASTCOL_NEEDS_FREE

#define TOASTCOL_NEEDS_FREE   TOAST_NEEDS_FREE

Function Documentation

◆ toast_delete_external()

void toast_delete_external ( Relation  rel,
Datum values,
bool isnull,
bool  is_speculative 
)

Definition at line 312 of file toast_helper.c.

References attlen, i, TupleDescData::natts, PointerGetDatum, RelationData::rd_att, toast_delete_datum(), TupleDescAttr, value, and VARATT_IS_EXTERNAL_ONDISK.

Referenced by heap_toast_delete().

314 {
315  TupleDesc tupleDesc = rel->rd_att;
316  int numAttrs = tupleDesc->natts;
317  int i;
318 
319  for (i = 0; i < numAttrs; i++)
320  {
321  if (TupleDescAttr(tupleDesc, i)->attlen == -1)
322  {
323  Datum value = values[i];
324 
325  if (isnull[i])
326  continue;
328  toast_delete_datum(rel, value, is_speculative);
329  }
330  }
331 }
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:314
#define PointerGetDatum(X)
Definition: postgres.h:556
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static struct @145 value
void toast_delete_datum(Relation rel, Datum value, bool is_speculative)
int16 attlen
Definition: pg_attribute.h:64
uintptr_t Datum
Definition: postgres.h:367
TupleDesc rd_att
Definition: rel.h:84
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int i

◆ toast_tuple_cleanup()

void toast_tuple_cleanup ( ToastTupleContext ttc)

Definition at line 269 of file toast_helper.c.

References DatumGetPointer, i, TupleDescData::natts, pfree(), RelationData::rd_att, ToastAttrInfo::tai_colflags, toast_delete_datum(), TOAST_NEEDS_DELETE_OLD, TOAST_NEEDS_FREE, TOASTCOL_NEEDS_DELETE_OLD, TOASTCOL_NEEDS_FREE, ToastTupleContext::ttc_attr, ToastTupleContext::ttc_flags, ToastTupleContext::ttc_oldvalues, ToastTupleContext::ttc_rel, and ToastTupleContext::ttc_values.

Referenced by heap_toast_insert_or_update().

270 {
271  TupleDesc tupleDesc = ttc->ttc_rel->rd_att;
272  int numAttrs = tupleDesc->natts;
273 
274  /*
275  * Free allocated temp values
276  */
277  if ((ttc->ttc_flags & TOAST_NEEDS_FREE) != 0)
278  {
279  int i;
280 
281  for (i = 0; i < numAttrs; i++)
282  {
283  ToastAttrInfo *attr = &ttc->ttc_attr[i];
284 
285  if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
286  pfree(DatumGetPointer(ttc->ttc_values[i]));
287  }
288  }
289 
290  /*
291  * Delete external values from the old tuple
292  */
293  if ((ttc->ttc_flags & TOAST_NEEDS_DELETE_OLD) != 0)
294  {
295  int i;
296 
297  for (i = 0; i < numAttrs; i++)
298  {
299  ToastAttrInfo *attr = &ttc->ttc_attr[i];
300 
301  if ((attr->tai_colflags & TOASTCOL_NEEDS_DELETE_OLD) != 0)
302  toast_delete_datum(ttc->ttc_rel, ttc->ttc_oldvalues[i], false);
303  }
304  }
305 }
uint8 tai_colflags
Definition: toast_helper.h:34
#define TOASTCOL_NEEDS_DELETE_OLD
Definition: toast_helper.h:98
void pfree(void *pointer)
Definition: mcxt.c:1056
#define TOAST_NEEDS_FREE
Definition: toast_helper.h:78
#define TOASTCOL_NEEDS_FREE
Definition: toast_helper.h:99
void toast_delete_datum(Relation rel, Datum value, bool is_speculative)
TupleDesc rd_att
Definition: rel.h:84
Datum * ttc_oldvalues
Definition: toast_helper.h:51
#define DatumGetPointer(X)
Definition: postgres.h:549
#define TOAST_NEEDS_DELETE_OLD
Definition: toast_helper.h:77
int i
ToastAttrInfo * ttc_attr
Definition: toast_helper.h:61

◆ toast_tuple_externalize()

void toast_tuple_externalize ( ToastTupleContext ttc,
int  attribute,
int  options 
)

Definition at line 250 of file toast_helper.c.

References DatumGetPointer, pfree(), ToastAttrInfo::tai_colflags, ToastAttrInfo::tai_oldexternal, TOAST_NEEDS_CHANGE, TOAST_NEEDS_FREE, toast_save_datum(), TOASTCOL_IGNORE, TOASTCOL_NEEDS_FREE, ToastTupleContext::ttc_attr, ToastTupleContext::ttc_flags, ToastTupleContext::ttc_rel, ToastTupleContext::ttc_values, and value.

Referenced by heap_toast_insert_or_update().

251 {
252  Datum *value = &ttc->ttc_values[attribute];
253  Datum old_value = *value;
254  ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
255 
256  attr->tai_colflags |= TOASTCOL_IGNORE;
257  *value = toast_save_datum(ttc->ttc_rel, old_value, attr->tai_oldexternal,
258  options);
259  if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
260  pfree(DatumGetPointer(old_value));
263 }
uint8 tai_colflags
Definition: toast_helper.h:34
static struct @145 value
#define TOAST_NEEDS_CHANGE
Definition: toast_helper.h:80
void pfree(void *pointer)
Definition: mcxt.c:1056
#define TOAST_NEEDS_FREE
Definition: toast_helper.h:78
#define TOASTCOL_NEEDS_FREE
Definition: toast_helper.h:99
uintptr_t Datum
Definition: postgres.h:367
#define DatumGetPointer(X)
Definition: postgres.h:549
struct varlena * tai_oldexternal
Definition: toast_helper.h:32
#define TOASTCOL_IGNORE
Definition: toast_helper.h:100
Datum toast_save_datum(Relation rel, Datum value, struct varlena *oldexternal, int options)
ToastAttrInfo * ttc_attr
Definition: toast_helper.h:61

◆ toast_tuple_find_biggest_attribute()

int toast_tuple_find_biggest_attribute ( ToastTupleContext ttc,
bool  for_compression,
bool  check_main 
)

Definition at line 178 of file toast_helper.c.

References DatumGetPointer, i, MAXALIGN, TupleDescData::natts, RelationData::rd_att, ToastAttrInfo::tai_colflags, ToastAttrInfo::tai_size, TOAST_POINTER_SIZE, TOASTCOL_IGNORE, TOASTCOL_INCOMPRESSIBLE, ToastTupleContext::ttc_attr, ToastTupleContext::ttc_rel, ToastTupleContext::ttc_values, TupleDescAttr, VARATT_IS_COMPRESSED, and VARATT_IS_EXTERNAL.

Referenced by heap_toast_insert_or_update().

180 {
181  TupleDesc tupleDesc = ttc->ttc_rel->rd_att;
182  int numAttrs = tupleDesc->natts;
183  int biggest_attno = -1;
184  int32 biggest_size = MAXALIGN(TOAST_POINTER_SIZE);
185  int32 skip_colflags = TOASTCOL_IGNORE;
186  int i;
187 
188  if (for_compression)
189  skip_colflags |= TOASTCOL_INCOMPRESSIBLE;
190 
191  for (i = 0; i < numAttrs; i++)
192  {
193  Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
194 
195  if ((ttc->ttc_attr[i].tai_colflags & skip_colflags) != 0)
196  continue;
198  continue; /* can't happen, toast_action would be 'p' */
199  if (for_compression &&
201  continue;
202  if (check_main && att->attstorage != 'm')
203  continue;
204  if (!check_main && att->attstorage != 'x' && att->attstorage != 'e')
205  continue;
206 
207  if (ttc->ttc_attr[i].tai_size > biggest_size)
208  {
209  biggest_attno = i;
210  biggest_size = ttc->ttc_attr[i].tai_size;
211  }
212  }
213 
214  return biggest_attno;
215 }
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:312
#define TOASTCOL_INCOMPRESSIBLE
Definition: toast_helper.h:101
uint8 tai_colflags
Definition: toast_helper.h:34
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
signed int int32
Definition: c.h:346
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:313
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define TOAST_POINTER_SIZE
Definition: detoast.h:41
TupleDesc rd_att
Definition: rel.h:84
#define MAXALIGN(LEN)
Definition: c.h:685
#define DatumGetPointer(X)
Definition: postgres.h:549
int i
#define TOASTCOL_IGNORE
Definition: toast_helper.h:100
ToastAttrInfo * ttc_attr
Definition: toast_helper.h:61

◆ toast_tuple_init()

void toast_tuple_init ( ToastTupleContext ttc)

Definition at line 39 of file toast_helper.c.

References DatumGetPointer, detoast_attr(), detoast_external_attr(), i, TupleDescData::natts, PointerGetDatum, RelationData::rd_att, ToastAttrInfo::tai_colflags, ToastAttrInfo::tai_oldexternal, ToastAttrInfo::tai_size, TOAST_HAS_NULLS, TOAST_NEEDS_CHANGE, TOAST_NEEDS_DELETE_OLD, TOAST_NEEDS_FREE, TOASTCOL_IGNORE, TOASTCOL_NEEDS_DELETE_OLD, TOASTCOL_NEEDS_FREE, ToastTupleContext::ttc_attr, ToastTupleContext::ttc_flags, ToastTupleContext::ttc_isnull, ToastTupleContext::ttc_oldisnull, ToastTupleContext::ttc_oldvalues, ToastTupleContext::ttc_rel, ToastTupleContext::ttc_values, TupleDescAttr, VARATT_IS_EXTERNAL, VARATT_IS_EXTERNAL_ONDISK, VARSIZE_ANY, and VARSIZE_EXTERNAL.

Referenced by heap_toast_insert_or_update().

40 {
41  TupleDesc tupleDesc = ttc->ttc_rel->rd_att;
42  int numAttrs = tupleDesc->natts;
43  int i;
44 
45  ttc->ttc_flags = 0;
46 
47  for (i = 0; i < numAttrs; i++)
48  {
49  Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
50  struct varlena *old_value;
51  struct varlena *new_value;
52 
53  ttc->ttc_attr[i].tai_colflags = 0;
54  ttc->ttc_attr[i].tai_oldexternal = NULL;
55 
56  if (ttc->ttc_oldvalues != NULL)
57  {
58  /*
59  * For UPDATE get the old and new values of this attribute
60  */
61  old_value =
62  (struct varlena *) DatumGetPointer(ttc->ttc_oldvalues[i]);
63  new_value =
64  (struct varlena *) DatumGetPointer(ttc->ttc_values[i]);
65 
66  /*
67  * If the old value is stored on disk, check if it has changed so
68  * we have to delete it later.
69  */
70  if (att->attlen == -1 && !ttc->ttc_oldisnull[i] &&
71  VARATT_IS_EXTERNAL_ONDISK(old_value))
72  {
73  if (ttc->ttc_isnull[i] ||
74  !VARATT_IS_EXTERNAL_ONDISK(new_value) ||
75  memcmp((char *) old_value, (char *) new_value,
76  VARSIZE_EXTERNAL(old_value)) != 0)
77  {
78  /*
79  * The old external stored value isn't needed any more
80  * after the update
81  */
84  }
85  else
86  {
87  /*
88  * This attribute isn't changed by this update so we reuse
89  * the original reference to the old value in the new
90  * tuple.
91  */
93  continue;
94  }
95  }
96  }
97  else
98  {
99  /*
100  * For INSERT simply get the new value
101  */
102  new_value = (struct varlena *) DatumGetPointer(ttc->ttc_values[i]);
103  }
104 
105  /*
106  * Handle NULL attributes
107  */
108  if (ttc->ttc_isnull[i])
109  {
111  ttc->ttc_flags |= TOAST_HAS_NULLS;
112  continue;
113  }
114 
115  /*
116  * Now look at varlena attributes
117  */
118  if (att->attlen == -1)
119  {
120  /*
121  * If the table's attribute says PLAIN always, force it so.
122  */
123  if (att->attstorage == 'p')
125 
126  /*
127  * We took care of UPDATE above, so any external value we find
128  * still in the tuple must be someone else's that we cannot reuse
129  * (this includes the case of an out-of-line in-memory datum).
130  * Fetch it back (without decompression, unless we are forcing
131  * PLAIN storage). If necessary, we'll push it out as a new
132  * external value below.
133  */
134  if (VARATT_IS_EXTERNAL(new_value))
135  {
136  ttc->ttc_attr[i].tai_oldexternal = new_value;
137  if (att->attstorage == 'p')
138  new_value = detoast_attr(new_value);
139  else
140  new_value = detoast_external_attr(new_value);
141  ttc->ttc_values[i] = PointerGetDatum(new_value);
144  }
145 
146  /*
147  * Remember the size of this attribute
148  */
149  ttc->ttc_attr[i].tai_size = VARSIZE_ANY(new_value);
150  }
151  else
152  {
153  /*
154  * Not a varlena attribute, plain storage always
155  */
157  }
158  }
159 }
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:314
uint8 tai_colflags
Definition: toast_helper.h:34
#define TOASTCOL_NEEDS_DELETE_OLD
Definition: toast_helper.h:98
#define PointerGetDatum(X)
Definition: postgres.h:556
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
struct varlena * detoast_external_attr(struct varlena *attr)
Definition: detoast.c:45
#define TOAST_NEEDS_CHANGE
Definition: toast_helper.h:80
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:313
struct varlena * detoast_attr(struct varlena *attr)
Definition: detoast.c:116
#define TOAST_NEEDS_FREE
Definition: toast_helper.h:78
#define TOASTCOL_NEEDS_FREE
Definition: toast_helper.h:99
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define TOAST_HAS_NULLS
Definition: toast_helper.h:79
TupleDesc rd_att
Definition: rel.h:84
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
Datum * ttc_oldvalues
Definition: toast_helper.h:51
#define DatumGetPointer(X)
Definition: postgres.h:549
#define TOAST_NEEDS_DELETE_OLD
Definition: toast_helper.h:77
int i
struct varlena * tai_oldexternal
Definition: toast_helper.h:32
Definition: c.h:549
#define TOASTCOL_IGNORE
Definition: toast_helper.h:100
ToastAttrInfo * ttc_attr
Definition: toast_helper.h:61
#define VARSIZE_EXTERNAL(PTR)
Definition: postgres.h:309

◆ toast_tuple_try_compression()

void toast_tuple_try_compression ( ToastTupleContext ttc,
int  attribute 
)

Definition at line 223 of file toast_helper.c.

References DatumGetPointer, pfree(), ToastAttrInfo::tai_colflags, ToastAttrInfo::tai_size, toast_compress_datum(), TOAST_NEEDS_CHANGE, TOAST_NEEDS_FREE, TOASTCOL_INCOMPRESSIBLE, TOASTCOL_NEEDS_FREE, ToastTupleContext::ttc_attr, ToastTupleContext::ttc_flags, ToastTupleContext::ttc_values, value, and VARSIZE.

Referenced by heap_toast_insert_or_update().

224 {
225  Datum *value = &ttc->ttc_values[attribute];
226  Datum new_value = toast_compress_datum(*value);
227  ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
228 
229  if (DatumGetPointer(new_value) != NULL)
230  {
231  /* successful compression */
232  if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
233  pfree(DatumGetPointer(*value));
234  *value = new_value;
236  attr->tai_size = VARSIZE(DatumGetPointer(*value));
238  }
239  else
240  {
241  /* incompressible, ignore on subsequent compression passes */
243  }
244 }
#define TOASTCOL_INCOMPRESSIBLE
Definition: toast_helper.h:101
uint8 tai_colflags
Definition: toast_helper.h:34
#define VARSIZE(PTR)
Definition: postgres.h:303
static struct @145 value
#define TOAST_NEEDS_CHANGE
Definition: toast_helper.h:80
void pfree(void *pointer)
Definition: mcxt.c:1056
#define TOAST_NEEDS_FREE
Definition: toast_helper.h:78
#define TOASTCOL_NEEDS_FREE
Definition: toast_helper.h:99
uintptr_t Datum
Definition: postgres.h:367
#define DatumGetPointer(X)
Definition: postgres.h:549
Datum toast_compress_datum(Datum value)
ToastAttrInfo * ttc_attr
Definition: toast_helper.h:61