PostgreSQL Source Code  git master
toast_helper.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * toast_helper.c
4  * Helper functions for table AMs implementing compressed or
5  * out-of-line storage of varlena attributes.
6  *
7  * Copyright (c) 2000-2019, PostgreSQL Global Development Group
8  *
9  * IDENTIFICATION
10  * src/backend/access/common/toast_helper.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 
15 #include "postgres.h"
16 
17 #include "access/detoast.h"
18 #include "access/table.h"
19 #include "access/toast_helper.h"
20 #include "access/toast_internals.h"
21 
22 /*
23  * Prepare to TOAST a tuple.
24  *
25  * tupleDesc, toast_values, and toast_isnull are required parameters; they
26  * provide the necessary details about the tuple to be toasted.
27  *
28  * toast_oldvalues and toast_oldisnull should be NULL for a newly-inserted
29  * tuple; for an update, they should describe the existing tuple.
30  *
31  * All of these arrays should have a length equal to tupleDesc->natts.
32  *
33  * On return, toast_flags and toast_attr will have been initialized.
34  * toast_flags is just a single uint8, but toast_attr is an caller-provided
35  * array with a length equal to tupleDesc->natts. The caller need not
36  * perform any initialization of the array before calling this function.
37  */
38 void
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 }
160 
161 /*
162  * Find the largest varlena attribute that satisfies certain criteria.
163  *
164  * The relevant column must not be marked TOASTCOL_IGNORE, and if the
165  * for_compression flag is passed as true, it must also not be marked
166  * TOASTCOL_INCOMPRESSIBLE.
167  *
168  * The column must have attstorage 'e' or 'x' if check_main is false, and
169  * must have attstorage 'm' if check_main is true.
170  *
171  * The column must have a minimum size of MAXALIGN(TOAST_POINTER_SIZE);
172  * if not, no benefit is to be expected by compressing it.
173  *
174  * The return value is the index of the biggest suitable column, or
175  * -1 if there is none.
176  */
177 int
179  bool for_compression, bool check_main)
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 }
216 
217 /*
218  * Try compression for an attribute.
219  *
220  * If we find that the attribute is not compressible, mark it so.
221  */
222 void
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 }
245 
246 /*
247  * Move an attribute to external storage.
248  */
249 void
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 }
264 
265 /*
266  * Perform appropriate cleanup after one tuple has been subjected to TOAST.
267  */
268 void
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 }
306 
307 /*
308  * Check for external stored attributes and delete them from the secondary
309  * relation.
310  */
311 void
313  bool is_speculative)
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 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 TOASTCOL_NEEDS_DELETE_OLD
Definition: toast_helper.h:98
void toast_tuple_try_compression(ToastTupleContext *ttc, int attribute)
Definition: toast_helper.c:223
#define VARSIZE(PTR)
Definition: postgres.h:303
#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
static struct @145 value
signed int int32
Definition: c.h:346
#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
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_tuple_cleanup(ToastTupleContext *ttc)
Definition: toast_helper.c:269
int toast_tuple_find_biggest_attribute(ToastTupleContext *ttc, bool for_compression, bool check_main)
Definition: toast_helper.c:178
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define TOAST_POINTER_SIZE
Definition: detoast.h:41
void toast_delete_datum(Relation rel, Datum value, bool is_speculative)
void toast_delete_external(Relation rel, Datum *values, bool *isnull, bool is_speculative)
Definition: toast_helper.c:312
#define TOAST_HAS_NULLS
Definition: toast_helper.h:79
int16 attlen
Definition: pg_attribute.h:64
uintptr_t Datum
Definition: postgres.h:367
TupleDesc rd_att
Definition: rel.h:84
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
Datum * ttc_oldvalues
Definition: toast_helper.h:51
#define MAXALIGN(LEN)
Definition: c.h:685
#define DatumGetPointer(X)
Definition: postgres.h:549
static Datum values[MAXATTR]
Definition: bootstrap.c:167
Datum toast_compress_datum(Datum value)
#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
Datum toast_save_datum(Relation rel, Datum value, struct varlena *oldexternal, int options)
ToastAttrInfo * ttc_attr
Definition: toast_helper.h:61
#define VARSIZE_EXTERNAL(PTR)
Definition: postgres.h:309
void toast_tuple_init(ToastTupleContext *ttc)
Definition: toast_helper.c:39
void toast_tuple_externalize(ToastTupleContext *ttc, int attribute, int options)
Definition: toast_helper.c:250