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 80 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 78 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 99 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 318 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().

320 {
321  TupleDesc tupleDesc = rel->rd_att;
322  int numAttrs = tupleDesc->natts;
323  int i;
324 
325  for (i = 0; i < numAttrs; i++)
326  {
327  if (TupleDescAttr(tupleDesc, i)->attlen == -1)
328  {
329  Datum value = values[i];
330 
331  if (isnull[i])
332  continue;
334  toast_delete_datum(rel, value, is_speculative);
335  }
336  }
337 }
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:327
static struct @142 value
#define PointerGetDatum(X)
Definition: postgres.h:600
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
void toast_delete_datum(Relation rel, Datum value, bool is_speculative)
int16 attlen
Definition: pg_attribute.h:68
uintptr_t Datum
Definition: postgres.h:411
TupleDesc rd_att
Definition: rel.h:110
static Datum values[MAXATTR]
Definition: bootstrap.c:166
int i

◆ toast_tuple_cleanup()

void toast_tuple_cleanup ( ToastTupleContext ttc)

Definition at line 275 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().

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

◆ toast_tuple_externalize()

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

Definition at line 256 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().

257 {
258  Datum *value = &ttc->ttc_values[attribute];
259  Datum old_value = *value;
260  ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
261 
262  attr->tai_colflags |= TOASTCOL_IGNORE;
263  *value = toast_save_datum(ttc->ttc_rel, old_value, attr->tai_oldexternal,
264  options);
265  if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
266  pfree(DatumGetPointer(old_value));
269 }
static struct @142 value
uint8 tai_colflags
Definition: toast_helper.h:34
#define TOAST_NEEDS_CHANGE
Definition: toast_helper.h:81
void pfree(void *pointer)
Definition: mcxt.c:1169
#define TOAST_NEEDS_FREE
Definition: toast_helper.h:79
#define TOASTCOL_NEEDS_FREE
Definition: toast_helper.h:100
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetPointer(X)
Definition: postgres.h:593
struct varlena * tai_oldexternal
Definition: toast_helper.h:32
#define TOASTCOL_IGNORE
Definition: toast_helper.h:101
Datum toast_save_datum(Relation rel, Datum value, struct varlena *oldexternal, int options)
ToastAttrInfo * ttc_attr
Definition: toast_helper.h:62

◆ toast_tuple_find_biggest_attribute()

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

Definition at line 181 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().

183 {
184  TupleDesc tupleDesc = ttc->ttc_rel->rd_att;
185  int numAttrs = tupleDesc->natts;
186  int biggest_attno = -1;
187  int32 biggest_size = MAXALIGN(TOAST_POINTER_SIZE);
188  int32 skip_colflags = TOASTCOL_IGNORE;
189  int i;
190 
191  if (for_compression)
192  skip_colflags |= TOASTCOL_INCOMPRESSIBLE;
193 
194  for (i = 0; i < numAttrs; i++)
195  {
196  Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
197 
198  if ((ttc->ttc_attr[i].tai_colflags & skip_colflags) != 0)
199  continue;
201  continue; /* can't happen, toast_action would be PLAIN */
202  if (for_compression &&
204  continue;
205  if (check_main && att->attstorage != TYPSTORAGE_MAIN)
206  continue;
207  if (!check_main && att->attstorage != TYPSTORAGE_EXTENDED &&
208  att->attstorage != TYPSTORAGE_EXTERNAL)
209  continue;
210 
211  if (ttc->ttc_attr[i].tai_size > biggest_size)
212  {
213  biggest_attno = i;
214  biggest_size = ttc->ttc_attr[i].tai_size;
215  }
216  }
217 
218  return biggest_attno;
219 }
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:325
#define TOASTCOL_INCOMPRESSIBLE
Definition: toast_helper.h:102
uint8 tai_colflags
Definition: toast_helper.h:34
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
signed int int32
Definition: c.h:429
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:326
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
#define TOAST_POINTER_SIZE
Definition: detoast.h:31
TupleDesc rd_att
Definition: rel.h:110
#define MAXALIGN(LEN)
Definition: c.h:757
#define DatumGetPointer(X)
Definition: postgres.h:593
int i
#define TOASTCOL_IGNORE
Definition: toast_helper.h:101
ToastAttrInfo * ttc_attr
Definition: toast_helper.h:62

◆ toast_tuple_init()

void toast_tuple_init ( ToastTupleContext ttc)

Definition at line 41 of file toast_helper.c.

References DatumGetPointer, detoast_attr(), detoast_external_attr(), i, TupleDescData::natts, PointerGetDatum, RelationData::rd_att, ToastAttrInfo::tai_colflags, ToastAttrInfo::tai_compression, 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().

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

◆ toast_tuple_try_compression()

void toast_tuple_try_compression ( ToastTupleContext ttc,
int  attribute 
)

Definition at line 227 of file toast_helper.c.

References DatumGetPointer, pfree(), ToastAttrInfo::tai_colflags, ToastAttrInfo::tai_compression, 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().

228 {
229  Datum *value = &ttc->ttc_values[attribute];
230  Datum new_value;
231  ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
232 
233  new_value = toast_compress_datum(*value, attr->tai_compression);
234 
235  if (DatumGetPointer(new_value) != NULL)
236  {
237  /* successful compression */
238  if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
239  pfree(DatumGetPointer(*value));
240  *value = new_value;
242  attr->tai_size = VARSIZE(DatumGetPointer(*value));
244  }
245  else
246  {
247  /* incompressible, ignore on subsequent compression passes */
249  }
250 }
Datum toast_compress_datum(Datum value, char cmethod)
static struct @142 value
#define TOASTCOL_INCOMPRESSIBLE
Definition: toast_helper.h:102
uint8 tai_colflags
Definition: toast_helper.h:34
#define VARSIZE(PTR)
Definition: postgres.h:316
#define TOAST_NEEDS_CHANGE
Definition: toast_helper.h:81
void pfree(void *pointer)
Definition: mcxt.c:1169
#define TOAST_NEEDS_FREE
Definition: toast_helper.h:79
char tai_compression
Definition: toast_helper.h:35
#define TOASTCOL_NEEDS_FREE
Definition: toast_helper.h:100
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetPointer(X)
Definition: postgres.h:593
ToastAttrInfo * ttc_attr
Definition: toast_helper.h:62