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 "varatt.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 319 of file toast_helper.c.

321 {
322  TupleDesc tupleDesc = rel->rd_att;
323  int numAttrs = tupleDesc->natts;
324  int i;
325 
326  for (i = 0; i < numAttrs; i++)
327  {
328  if (TupleDescAttr(tupleDesc, i)->attlen == -1)
329  {
330  Datum value = values[i];
331 
332  if (isnull[i])
333  continue;
335  toast_delete_datum(rel, value, is_speculative);
336  }
337  }
338 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
static struct @147 value
int i
Definition: isn.c:73
int16 attlen
Definition: pg_attribute.h:59
uintptr_t Datum
Definition: postgres.h:64
TupleDesc rd_att
Definition: rel.h:112
void toast_delete_datum(Relation rel, Datum value, bool is_speculative)
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: varatt.h:290

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

Referenced by heap_toast_delete().

◆ toast_tuple_cleanup()

void toast_tuple_cleanup ( ToastTupleContext ttc)

Definition at line 276 of file toast_helper.c.

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

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().

◆ toast_tuple_externalize()

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

Definition at line 257 of file toast_helper.c.

258 {
259  Datum *value = &ttc->ttc_values[attribute];
260  Datum old_value = *value;
261  ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
262 
263  attr->tai_colflags |= TOASTCOL_IGNORE;
264  *value = toast_save_datum(ttc->ttc_rel, old_value, attr->tai_oldexternal,
265  options);
266  if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
267  pfree(DatumGetPointer(old_value));
270 }
struct varlena * tai_oldexternal
Definition: toast_helper.h:32
#define TOAST_NEEDS_CHANGE
Definition: toast_helper.h:81
#define TOASTCOL_IGNORE
Definition: toast_helper.h:101
Datum toast_save_datum(Relation rel, Datum value, struct varlena *oldexternal, int options)

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().

◆ toast_tuple_find_biggest_attribute()

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

Definition at line 182 of file toast_helper.c.

184 {
185  TupleDesc tupleDesc = ttc->ttc_rel->rd_att;
186  int numAttrs = tupleDesc->natts;
187  int biggest_attno = -1;
188  int32 biggest_size = MAXALIGN(TOAST_POINTER_SIZE);
189  int32 skip_colflags = TOASTCOL_IGNORE;
190  int i;
191 
192  if (for_compression)
193  skip_colflags |= TOASTCOL_INCOMPRESSIBLE;
194 
195  for (i = 0; i < numAttrs; i++)
196  {
197  Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
198 
199  if ((ttc->ttc_attr[i].tai_colflags & skip_colflags) != 0)
200  continue;
202  continue; /* can't happen, toast_action would be PLAIN */
203  if (for_compression &&
205  continue;
206  if (check_main && att->attstorage != TYPSTORAGE_MAIN)
207  continue;
208  if (!check_main && att->attstorage != TYPSTORAGE_EXTENDED &&
209  att->attstorage != TYPSTORAGE_EXTERNAL)
210  continue;
211 
212  if (ttc->ttc_attr[i].tai_size > biggest_size)
213  {
214  biggest_attno = i;
215  biggest_size = ttc->ttc_attr[i].tai_size;
216  }
217  }
218 
219  return biggest_attno;
220 }
#define MAXALIGN(LEN)
Definition: c.h:795
signed int int32
Definition: c.h:478
#define TOAST_POINTER_SIZE
Definition: detoast.h:31
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
#define TOASTCOL_INCOMPRESSIBLE
Definition: toast_helper.h:102
#define VARATT_IS_COMPRESSED(PTR)
Definition: varatt.h:288
#define VARATT_IS_EXTERNAL(PTR)
Definition: varatt.h:289

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().

◆ toast_tuple_init()

void toast_tuple_init ( ToastTupleContext ttc)

Definition at line 42 of file toast_helper.c.

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

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().

◆ toast_tuple_try_compression()

void toast_tuple_try_compression ( ToastTupleContext ttc,
int  attribute 
)

Definition at line 228 of file toast_helper.c.

229 {
230  Datum *value = &ttc->ttc_values[attribute];
231  Datum new_value;
232  ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
233 
234  new_value = toast_compress_datum(*value, attr->tai_compression);
235 
236  if (DatumGetPointer(new_value) != NULL)
237  {
238  /* successful compression */
239  if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
241  *value = new_value;
245  }
246  else
247  {
248  /* incompressible, ignore on subsequent compression passes */
250  }
251 }
Datum toast_compress_datum(Datum value, char cmethod)
#define VARSIZE(PTR)
Definition: varatt.h:279

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().