PostgreSQL Source Code  git master
toast_helper.c File Reference
#include "postgres.h"
#include "access/detoast.h"
#include "access/table.h"
#include "access/toast_helper.h"
#include "access/toast_internals.h"
#include "catalog/pg_type_d.h"
Include dependency graph for toast_helper.c:

Go to the source code of this file.

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)
 

Function Documentation

◆ toast_delete_external()

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

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

317 {
318  TupleDesc tupleDesc = rel->rd_att;
319  int numAttrs = tupleDesc->natts;
320  int i;
321 
322  for (i = 0; i < numAttrs; i++)
323  {
324  if (TupleDescAttr(tupleDesc, i)->attlen == -1)
325  {
326  Datum value = values[i];
327 
328  if (isnull[i])
329  continue;
331  toast_delete_datum(rel, value, is_speculative);
332  }
333  }
334 }
#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
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:110
static struct @143 value
static Datum values[MAXATTR]
Definition: bootstrap.c:165
int i

◆ toast_tuple_cleanup()

void toast_tuple_cleanup ( ToastTupleContext ttc)

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

273 {
274  TupleDesc tupleDesc = ttc->ttc_rel->rd_att;
275  int numAttrs = tupleDesc->natts;
276 
277  /*
278  * Free allocated temp values
279  */
280  if ((ttc->ttc_flags & TOAST_NEEDS_FREE) != 0)
281  {
282  int i;
283 
284  for (i = 0; i < numAttrs; i++)
285  {
286  ToastAttrInfo *attr = &ttc->ttc_attr[i];
287 
288  if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
289  pfree(DatumGetPointer(ttc->ttc_values[i]));
290  }
291  }
292 
293  /*
294  * Delete external values from the old tuple
295  */
296  if ((ttc->ttc_flags & TOAST_NEEDS_DELETE_OLD) != 0)
297  {
298  int i;
299 
300  for (i = 0; i < numAttrs; i++)
301  {
302  ToastAttrInfo *attr = &ttc->ttc_attr[i];
303 
304  if ((attr->tai_colflags & TOASTCOL_NEEDS_DELETE_OLD) != 0)
305  toast_delete_datum(ttc->ttc_rel, ttc->ttc_oldvalues[i], false);
306  }
307  }
308 }
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:1057
#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:110
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 253 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().

254 {
255  Datum *value = &ttc->ttc_values[attribute];
256  Datum old_value = *value;
257  ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
258 
259  attr->tai_colflags |= TOASTCOL_IGNORE;
260  *value = toast_save_datum(ttc->ttc_rel, old_value, attr->tai_oldexternal,
261  options);
262  if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
263  pfree(DatumGetPointer(old_value));
266 }
uint8 tai_colflags
Definition: toast_helper.h:34
#define TOAST_NEEDS_CHANGE
Definition: toast_helper.h:80
void pfree(void *pointer)
Definition: mcxt.c:1057
#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
static struct @143 value
#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 180 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().

182 {
183  TupleDesc tupleDesc = ttc->ttc_rel->rd_att;
184  int numAttrs = tupleDesc->natts;
185  int biggest_attno = -1;
186  int32 biggest_size = MAXALIGN(TOAST_POINTER_SIZE);
187  int32 skip_colflags = TOASTCOL_IGNORE;
188  int i;
189 
190  if (for_compression)
191  skip_colflags |= TOASTCOL_INCOMPRESSIBLE;
192 
193  for (i = 0; i < numAttrs; i++)
194  {
195  Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
196 
197  if ((ttc->ttc_attr[i].tai_colflags & skip_colflags) != 0)
198  continue;
200  continue; /* can't happen, toast_action would be PLAIN */
201  if (for_compression &&
203  continue;
204  if (check_main && att->attstorage != TYPSTORAGE_MAIN)
205  continue;
206  if (!check_main && att->attstorage != TYPSTORAGE_EXTENDED &&
207  att->attstorage != TYPSTORAGE_EXTERNAL)
208  continue;
209 
210  if (ttc->ttc_attr[i].tai_size > biggest_size)
211  {
212  biggest_attno = i;
213  biggest_size = ttc->ttc_attr[i].tai_size;
214  }
215  }
216 
217  return biggest_attno;
218 }
#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:362
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:313
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
#define TOAST_POINTER_SIZE
Definition: detoast.h:41
TupleDesc rd_att
Definition: rel.h:110
#define MAXALIGN(LEN)
Definition: c.h:698
#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 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_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 
58  if (ttc->ttc_oldvalues != NULL)
59  {
60  /*
61  * For UPDATE get the old and new values of this attribute
62  */
63  old_value =
64  (struct varlena *) DatumGetPointer(ttc->ttc_oldvalues[i]);
65  new_value =
66  (struct varlena *) DatumGetPointer(ttc->ttc_values[i]);
67 
68  /*
69  * If the old value is stored on disk, check if it has changed so
70  * we have to delete it later.
71  */
72  if (att->attlen == -1 && !ttc->ttc_oldisnull[i] &&
73  VARATT_IS_EXTERNAL_ONDISK(old_value))
74  {
75  if (ttc->ttc_isnull[i] ||
76  !VARATT_IS_EXTERNAL_ONDISK(new_value) ||
77  memcmp((char *) old_value, (char *) new_value,
78  VARSIZE_EXTERNAL(old_value)) != 0)
79  {
80  /*
81  * The old external stored value isn't needed any more
82  * after the update
83  */
86  }
87  else
88  {
89  /*
90  * This attribute isn't changed by this update so we reuse
91  * the original reference to the old value in the new
92  * tuple.
93  */
95  continue;
96  }
97  }
98  }
99  else
100  {
101  /*
102  * For INSERT simply get the new value
103  */
104  new_value = (struct varlena *) DatumGetPointer(ttc->ttc_values[i]);
105  }
106 
107  /*
108  * Handle NULL attributes
109  */
110  if (ttc->ttc_isnull[i])
111  {
113  ttc->ttc_flags |= TOAST_HAS_NULLS;
114  continue;
115  }
116 
117  /*
118  * Now look at varlena attributes
119  */
120  if (att->attlen == -1)
121  {
122  /*
123  * If the table's attribute says PLAIN always, force it so.
124  */
125  if (att->attstorage == TYPSTORAGE_PLAIN)
127 
128  /*
129  * We took care of UPDATE above, so any external value we find
130  * still in the tuple must be someone else's that we cannot reuse
131  * (this includes the case of an out-of-line in-memory datum).
132  * Fetch it back (without decompression, unless we are forcing
133  * PLAIN storage). If necessary, we'll push it out as a new
134  * external value below.
135  */
136  if (VARATT_IS_EXTERNAL(new_value))
137  {
138  ttc->ttc_attr[i].tai_oldexternal = new_value;
139  if (att->attstorage == TYPSTORAGE_PLAIN)
140  new_value = detoast_attr(new_value);
141  else
142  new_value = detoast_external_attr(new_value);
143  ttc->ttc_values[i] = PointerGetDatum(new_value);
146  }
147 
148  /*
149  * Remember the size of this attribute
150  */
151  ttc->ttc_attr[i].tai_size = VARSIZE_ANY(new_value);
152  }
153  else
154  {
155  /*
156  * Not a varlena attribute, plain storage always
157  */
159  }
160  }
161 }
#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:44
#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:115
#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:193
#define TOAST_HAS_NULLS
Definition: toast_helper.h:79
TupleDesc rd_att
Definition: rel.h:110
#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:562
#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 226 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().

227 {
228  Datum *value = &ttc->ttc_values[attribute];
229  Datum new_value = toast_compress_datum(*value);
230  ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
231 
232  if (DatumGetPointer(new_value) != NULL)
233  {
234  /* successful compression */
235  if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
236  pfree(DatumGetPointer(*value));
237  *value = new_value;
239  attr->tai_size = VARSIZE(DatumGetPointer(*value));
241  }
242  else
243  {
244  /* incompressible, ignore on subsequent compression passes */
246  }
247 }
#define TOASTCOL_INCOMPRESSIBLE
Definition: toast_helper.h:101
uint8 tai_colflags
Definition: toast_helper.h:34
#define VARSIZE(PTR)
Definition: postgres.h:303
#define TOAST_NEEDS_CHANGE
Definition: toast_helper.h:80
void pfree(void *pointer)
Definition: mcxt.c:1057
#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
static struct @143 value
#define DatumGetPointer(X)
Definition: postgres.h:549
Datum toast_compress_datum(Datum value)
ToastAttrInfo * ttc_attr
Definition: toast_helper.h:61