Branch data Line data Source code
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-2026, 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/toast_helper.h"
19 : : #include "access/toast_internals.h"
20 : : #include "catalog/pg_type_d.h"
21 : : #include "varatt.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 a 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
41 : 24982 : toast_tuple_init(ToastTupleContext *ttc)
42 : : {
43 : 24982 : TupleDesc tupleDesc = ttc->ttc_rel->rd_att;
44 : 24982 : int numAttrs = tupleDesc->natts;
45 : : int i;
46 : :
47 : 24982 : ttc->ttc_flags = 0;
48 : :
49 [ + + ]: 252338 : for (i = 0; i < numAttrs; i++)
50 : : {
51 : 227356 : Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
52 : : varlena *old_value;
53 : : varlena *new_value;
54 : :
55 : 227356 : ttc->ttc_attr[i].tai_colflags = 0;
56 : 227356 : ttc->ttc_attr[i].tai_oldexternal = NULL;
57 : 227356 : ttc->ttc_attr[i].tai_compression = att->attcompression;
58 : :
59 [ + + ]: 227356 : if (ttc->ttc_oldvalues != NULL)
60 : : {
61 : : /*
62 : : * For UPDATE get the old and new values of this attribute
63 : : */
64 : : old_value =
65 : 53331 : (varlena *) DatumGetPointer(ttc->ttc_oldvalues[i]);
66 : : new_value =
67 : 53331 : (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 : : * Note that TOAST pointers could have different vartags, for oid
74 : : * or oid8, and these can have different sizes.
75 : : */
76 [ + + + + : 59619 : if (att->attlen == -1 && !ttc->ttc_oldisnull[i] &&
+ + ]
77 : 6288 : VARATT_IS_EXTERNAL_ONDISK(old_value))
78 : : {
79 [ + + ]: 692 : if (ttc->ttc_isnull[i] ||
80 [ + + + + ]: 744 : !VARATT_IS_EXTERNAL_ONDISK(new_value) ||
81 : 68 : VARTAG_EXTERNAL(old_value) != VARTAG_EXTERNAL(new_value) ||
82 [ + + ]: 60 : memcmp(old_value, new_value,
83 : : VARSIZE_EXTERNAL(old_value)) != 0)
84 : : {
85 : : /*
86 : : * The old external stored value isn't needed any more
87 : : * after the update
88 : : */
89 : 633 : ttc->ttc_attr[i].tai_colflags |= TOASTCOL_NEEDS_DELETE_OLD;
90 : 633 : ttc->ttc_flags |= TOAST_NEEDS_DELETE_OLD;
91 : : }
92 : : else
93 : : {
94 : : /*
95 : : * This attribute isn't changed by this update so we reuse
96 : : * the original reference to the old value in the new
97 : : * tuple.
98 : : */
99 : 59 : ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
100 : 59 : continue;
101 : : }
102 : : }
103 : : }
104 : : else
105 : : {
106 : : /*
107 : : * For INSERT simply get the new value
108 : : */
109 : 174025 : new_value = (varlena *) DatumGetPointer(ttc->ttc_values[i]);
110 : : }
111 : :
112 : : /*
113 : : * Handle NULL attributes
114 : : */
115 [ + + ]: 227297 : if (ttc->ttc_isnull[i])
116 : : {
117 : 24347 : ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
118 : 24347 : ttc->ttc_flags |= TOAST_HAS_NULLS;
119 : 24347 : continue;
120 : : }
121 : :
122 : : /*
123 : : * Now look at varlena attributes
124 : : */
125 [ + + ]: 202950 : if (att->attlen == -1)
126 : : {
127 : : /*
128 : : * If the table's attribute says PLAIN always, force it so.
129 : : */
130 [ + + ]: 46962 : if (att->attstorage == TYPSTORAGE_PLAIN)
131 : 1439 : ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
132 : :
133 : : /*
134 : : * We took care of UPDATE above, so any external value we find
135 : : * still in the tuple must be someone else's that we cannot reuse
136 : : * (this includes the case of an out-of-line in-memory datum).
137 : : * Fetch it back (without decompression, unless we are forcing
138 : : * PLAIN storage). If necessary, we'll push it out as a new
139 : : * external value below.
140 : : */
141 [ + + ]: 46962 : if (VARATT_IS_EXTERNAL(new_value))
142 : : {
143 : 727 : ttc->ttc_attr[i].tai_oldexternal = new_value;
144 [ - + ]: 727 : if (att->attstorage == TYPSTORAGE_PLAIN)
145 : 0 : new_value = detoast_attr(new_value);
146 : : else
147 : 727 : new_value = detoast_external_attr(new_value);
148 : 727 : ttc->ttc_values[i] = PointerGetDatum(new_value);
149 : 727 : ttc->ttc_attr[i].tai_colflags |= TOASTCOL_NEEDS_FREE;
150 : 727 : ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
151 : : }
152 : :
153 : : /*
154 : : * Remember the size of this attribute
155 : : */
156 : 46962 : ttc->ttc_attr[i].tai_size = VARSIZE_ANY(new_value);
157 : : }
158 : : else
159 : : {
160 : : /*
161 : : * Not a varlena attribute, plain storage always
162 : : */
163 : 155988 : ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
164 : : }
165 : : }
166 : 24982 : }
167 : :
168 : : /*
169 : : * Find the largest varlena attribute that satisfies certain criteria.
170 : : *
171 : : * The relevant column must not be marked TOASTCOL_IGNORE, and if the
172 : : * for_compression flag is passed as true, it must also not be marked
173 : : * TOASTCOL_INCOMPRESSIBLE.
174 : : *
175 : : * The column must have attstorage EXTERNAL or EXTENDED if check_main is
176 : : * false, and must have attstorage MAIN if check_main is true.
177 : : *
178 : : * The column must be larger than the TOAST pointer that would replace it;
179 : : * if not, no benefit is to be expected by compressing it. Note that this
180 : : * choice depends on the TOAST value type, oid or oid8.
181 : : *
182 : : * The return value is the index of the biggest suitable column, or
183 : : * -1 if there is none.
184 : : */
185 : : int
186 : 30037 : toast_tuple_find_biggest_attribute(ToastTupleContext *ttc,
187 : : bool for_compression, bool check_main)
188 : : {
189 : 30037 : TupleDesc tupleDesc = ttc->ttc_rel->rd_att;
190 : 30037 : int numAttrs = tupleDesc->natts;
191 : 30037 : int biggest_attno = -1;
192 : : int32 biggest_size;
193 : 30037 : int32 skip_colflags = TOASTCOL_IGNORE;
194 : : int i;
195 : :
196 : : /*
197 : : * Size of the TOAST pointer this relation would use. A relation without
198 : : * a TOAST table cannot have any of its attributes moved out-of-line, but
199 : : * it can still have some of them compressed. Fall back to the oid size
200 : : * in that case.
201 : : */
202 [ + + ]: 30037 : if (RelationGetToastChunkIdType(ttc->ttc_rel) == OID8OID)
203 : 93 : biggest_size = MAXALIGN(TOAST_OID8_POINTER_SIZE);
204 : : else
205 : 29944 : biggest_size = MAXALIGN(TOAST_OID_POINTER_SIZE);
206 : :
207 [ + + ]: 30037 : if (for_compression)
208 : 28946 : skip_colflags |= TOASTCOL_INCOMPRESSIBLE;
209 : :
210 [ + + ]: 401854 : for (i = 0; i < numAttrs; i++)
211 : : {
212 : 371817 : Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
213 : :
214 [ + + ]: 371817 : if ((ttc->ttc_attr[i].tai_colflags & skip_colflags) != 0)
215 : 305025 : continue;
216 [ - + ]: 66792 : if (VARATT_IS_EXTERNAL(DatumGetPointer(ttc->ttc_values[i])))
217 : 0 : continue; /* can't happen, toast_action would be PLAIN */
218 [ + + + + ]: 129595 : if (for_compression &&
219 : 62803 : VARATT_IS_COMPRESSED(DatumGetPointer(ttc->ttc_values[i])))
220 : 9048 : continue;
221 [ + + - + ]: 57744 : if (check_main && att->attstorage != TYPSTORAGE_MAIN)
222 : 0 : continue;
223 [ + + + + ]: 57744 : if (!check_main && att->attstorage != TYPSTORAGE_EXTENDED &&
224 [ + + ]: 3282 : att->attstorage != TYPSTORAGE_EXTERNAL)
225 : 91 : continue;
226 : :
227 [ + + ]: 57653 : if (ttc->ttc_attr[i].tai_size > biggest_size)
228 : : {
229 : 38054 : biggest_attno = i;
230 : 38054 : biggest_size = ttc->ttc_attr[i].tai_size;
231 : : }
232 : : }
233 : :
234 : 30037 : return biggest_attno;
235 : : }
236 : :
237 : : /*
238 : : * Try compression for an attribute.
239 : : *
240 : : * If we find that the attribute is not compressible, mark it so.
241 : : */
242 : : void
243 : 24653 : toast_tuple_try_compression(ToastTupleContext *ttc, int attribute)
244 : : {
245 : 24653 : Datum *value = &ttc->ttc_values[attribute];
246 : : Datum new_value;
247 : 24653 : ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
248 : :
249 : 24653 : new_value = toast_compress_datum(*value, attr->tai_compression);
250 : :
251 [ + + ]: 24653 : if (DatumGetPointer(new_value) != NULL)
252 : : {
253 : : /* successful compression */
254 [ + + ]: 23662 : if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
255 : 66 : pfree(DatumGetPointer(*value));
256 : 23662 : *value = new_value;
257 : 23662 : attr->tai_colflags |= TOASTCOL_NEEDS_FREE;
258 : 23662 : attr->tai_size = VARSIZE(DatumGetPointer(*value));
259 : 23662 : ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
260 : : }
261 : : else
262 : : {
263 : : /* incompressible, ignore on subsequent compression passes */
264 : 991 : attr->tai_colflags |= TOASTCOL_INCOMPRESSIBLE;
265 : : }
266 : 24653 : }
267 : :
268 : : /*
269 : : * Move an attribute to external storage.
270 : : */
271 : : void
272 : 11370 : toast_tuple_externalize(ToastTupleContext *ttc, int attribute, uint32 options)
273 : : {
274 : 11370 : Datum *value = &ttc->ttc_values[attribute];
275 : 11370 : Datum old_value = *value;
276 : 11370 : ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
277 : :
278 : 11370 : attr->tai_colflags |= TOASTCOL_IGNORE;
279 : 11370 : *value = toast_save_datum(ttc->ttc_rel, old_value, attr->tai_oldexternal,
280 : : options);
281 [ + + ]: 11370 : if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
282 : 8357 : pfree(DatumGetPointer(old_value));
283 : 11370 : attr->tai_colflags |= TOASTCOL_NEEDS_FREE;
284 : 11370 : ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
285 : 11370 : }
286 : :
287 : : /*
288 : : * Perform appropriate cleanup after one tuple has been subjected to TOAST.
289 : : */
290 : : void
291 : 24982 : toast_tuple_cleanup(ToastTupleContext *ttc)
292 : : {
293 : 24982 : TupleDesc tupleDesc = ttc->ttc_rel->rd_att;
294 : 24982 : int numAttrs = tupleDesc->natts;
295 : :
296 : : /*
297 : : * Free allocated temp values
298 : : */
299 [ + + ]: 24982 : if ((ttc->ttc_flags & TOAST_NEEDS_FREE) != 0)
300 : : {
301 : : int i;
302 : :
303 [ + + ]: 250897 : for (i = 0; i < numAttrs; i++)
304 : : {
305 : 226042 : ToastAttrInfo *attr = &ttc->ttc_attr[i];
306 : :
307 [ + + ]: 226042 : if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
308 : 27336 : pfree(DatumGetPointer(ttc->ttc_values[i]));
309 : : }
310 : : }
311 : :
312 : : /*
313 : : * Delete external values from the old tuple
314 : : */
315 [ + + ]: 24982 : if ((ttc->ttc_flags & TOAST_NEEDS_DELETE_OLD) != 0)
316 : : {
317 : : int i;
318 : :
319 [ + + ]: 13740 : for (i = 0; i < numAttrs; i++)
320 : : {
321 : 13209 : ToastAttrInfo *attr = &ttc->ttc_attr[i];
322 : :
323 [ + + ]: 13209 : if ((attr->tai_colflags & TOASTCOL_NEEDS_DELETE_OLD) != 0)
324 : 633 : toast_delete_datum(ttc->ttc_rel, ttc->ttc_oldvalues[i], false);
325 : : }
326 : : }
327 : 24982 : }
328 : :
329 : : /*
330 : : * Check for external stored attributes and delete them from the secondary
331 : : * relation.
332 : : */
333 : : void
334 : 530 : toast_delete_external(Relation rel, const Datum *values, const bool *isnull,
335 : : bool is_speculative)
336 : : {
337 : 530 : TupleDesc tupleDesc = rel->rd_att;
338 : 530 : int numAttrs = tupleDesc->natts;
339 : : int i;
340 : :
341 [ + + ]: 5128 : for (i = 0; i < numAttrs; i++)
342 : : {
343 [ + + ]: 4598 : if (TupleDescCompactAttr(tupleDesc, i)->attlen == -1)
344 : : {
345 : 1491 : Datum value = values[i];
346 : :
347 [ + + ]: 1491 : if (isnull[i])
348 : 423 : continue;
349 [ + + ]: 1068 : else if (VARATT_IS_EXTERNAL_ONDISK(DatumGetPointer(value)))
350 : 542 : toast_delete_datum(rel, value, is_speculative);
351 : : }
352 : : }
353 : 530 : }
|