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-2020, PostgreSQL Global Development Group
8  *
9  * IDENTIFICATION
10  * src/backend/access/table/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 #include "catalog/pg_type_d.h"
22 
23 
24 /*
25  * Prepare to TOAST a tuple.
26  *
27  * tupleDesc, toast_values, and toast_isnull are required parameters; they
28  * provide the necessary details about the tuple to be toasted.
29  *
30  * toast_oldvalues and toast_oldisnull should be NULL for a newly-inserted
31  * tuple; for an update, they should describe the existing tuple.
32  *
33  * All of these arrays should have a length equal to tupleDesc->natts.
34  *
35  * On return, toast_flags and toast_attr will have been initialized.
36  * toast_flags is just a single uint8, but toast_attr is an caller-provided
37  * array with a length equal to tupleDesc->natts. The caller need not
38  * perform any initialization of the array before calling this function.
39  */
40 void
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 }
162 
163 /*
164  * Find the largest varlena attribute that satisfies certain criteria.
165  *
166  * The relevant column must not be marked TOASTCOL_IGNORE, and if the
167  * for_compression flag is passed as true, it must also not be marked
168  * TOASTCOL_INCOMPRESSIBLE.
169  *
170  * The column must have attstorage EXTERNAL or EXTENDED if check_main is
171  * false, and must have attstorage MAIN if check_main is true.
172  *
173  * The column must have a minimum size of MAXALIGN(TOAST_POINTER_SIZE);
174  * if not, no benefit is to be expected by compressing it.
175  *
176  * The return value is the index of the biggest suitable column, or
177  * -1 if there is none.
178  */
179 int
181  bool for_compression, bool check_main)
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 }
219 
220 /*
221  * Try compression for an attribute.
222  *
223  * If we find that the attribute is not compressible, mark it so.
224  */
225 void
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 }
248 
249 /*
250  * Move an attribute to external storage.
251  */
252 void
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 }
267 
268 /*
269  * Perform appropriate cleanup after one tuple has been subjected to TOAST.
270  */
271 void
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 }
309 
310 /*
311  * Check for external stored attributes and delete them from the secondary
312  * relation.
313  */
314 void
316  bool is_speculative)
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 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:226
#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:44
signed int int32
Definition: c.h:362
#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
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_tuple_cleanup(ToastTupleContext *ttc)
Definition: toast_helper.c:272
int toast_tuple_find_biggest_attribute(ToastTupleContext *ttc, bool for_compression, bool check_main)
Definition: toast_helper.c:180
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
#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:315
#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:110
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
Datum * ttc_oldvalues
Definition: toast_helper.h:51
static struct @143 value
#define MAXALIGN(LEN)
Definition: c.h:698
#define DatumGetPointer(X)
Definition: postgres.h:549
static Datum values[MAXATTR]
Definition: bootstrap.c:165
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:562
#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:41
void toast_tuple_externalize(ToastTupleContext *ttc, int attribute, int options)
Definition: toast_helper.c:253