Age Owner Branch data TLA 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
2593 rhaas@postgresql.org 41 :CBC 25099 : toast_tuple_init(ToastTupleContext *ttc)
42 : : {
43 : 25099 : TupleDesc tupleDesc = ttc->ttc_rel->rd_att;
44 : 25099 : int numAttrs = tupleDesc->natts;
45 : : int i;
46 : :
47 : 25099 : ttc->ttc_flags = 0;
48 : :
49 [ + + ]: 256432 : for (i = 0; i < numAttrs; i++)
50 : : {
51 : 231333 : Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
52 : : varlena *old_value;
53 : : varlena *new_value;
54 : :
55 : 231333 : ttc->ttc_attr[i].tai_colflags = 0;
56 : 231333 : ttc->ttc_attr[i].tai_oldexternal = NULL;
2033 57 : 231333 : ttc->ttc_attr[i].tai_compression = att->attcompression;
58 : :
2593 59 [ + + ]: 231333 : if (ttc->ttc_oldvalues != NULL)
60 : : {
61 : : /*
62 : : * For UPDATE get the old and new values of this attribute
63 : : */
64 : : old_value =
243 michael@paquier.xyz 65 : 58614 : (varlena *) DatumGetPointer(ttc->ttc_oldvalues[i]);
66 : : new_value =
67 : 58614 : (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 : : */
2593 rhaas@postgresql.org 76 [ + + + + : 65606 : if (att->attlen == -1 && !ttc->ttc_oldisnull[i] &&
+ + ]
77 : 6992 : VARATT_IS_EXTERNAL_ONDISK(old_value))
78 : : {
79 [ + + ]: 800 : if (ttc->ttc_isnull[i] ||
80 [ + + + + ]: 857 : !VARATT_IS_EXTERNAL_ONDISK(new_value) ||
27 michael@paquier.xyz 81 :GNC 73 : VARTAG_EXTERNAL(old_value) != VARTAG_EXTERNAL(new_value) ||
607 peter@eisentraut.org 82 [ + + ]:CBC 65 : 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 : : */
2593 rhaas@postgresql.org 89 : 736 : ttc->ttc_attr[i].tai_colflags |= TOASTCOL_NEEDS_DELETE_OLD;
90 : 736 : 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 : 64 : ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
100 : 64 : continue;
101 : : }
102 : : }
103 : : }
104 : : else
105 : : {
106 : : /*
107 : : * For INSERT simply get the new value
108 : : */
243 michael@paquier.xyz 109 : 172719 : new_value = (varlena *) DatumGetPointer(ttc->ttc_values[i]);
110 : : }
111 : :
112 : : /*
113 : : * Handle NULL attributes
114 : : */
2593 rhaas@postgresql.org 115 [ + + ]: 231269 : if (ttc->ttc_isnull[i])
116 : : {
117 : 25361 : ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
118 : 25361 : ttc->ttc_flags |= TOAST_HAS_NULLS;
119 : 25361 : continue;
120 : : }
121 : :
122 : : /*
123 : : * Now look at varlena attributes
124 : : */
125 [ + + ]: 205908 : if (att->attlen == -1)
126 : : {
127 : : /*
128 : : * If the table's attribute says PLAIN always, force it so.
129 : : */
2413 tgl@sss.pgh.pa.us 130 [ + + ]: 47453 : if (att->attstorage == TYPSTORAGE_PLAIN)
2593 rhaas@postgresql.org 131 : 1420 : 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 [ + + ]: 47453 : if (VARATT_IS_EXTERNAL(new_value))
142 : : {
143 : 795 : ttc->ttc_attr[i].tai_oldexternal = new_value;
2413 tgl@sss.pgh.pa.us 144 [ - + ]: 795 : if (att->attstorage == TYPSTORAGE_PLAIN)
2565 rhaas@postgresql.org 145 :UBC 0 : new_value = detoast_attr(new_value);
146 : : else
2565 rhaas@postgresql.org 147 :CBC 795 : new_value = detoast_external_attr(new_value);
2593 148 : 795 : ttc->ttc_values[i] = PointerGetDatum(new_value);
149 : 795 : ttc->ttc_attr[i].tai_colflags |= TOASTCOL_NEEDS_FREE;
150 : 795 : ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
151 : : }
152 : :
153 : : /*
154 : : * Remember the size of this attribute
155 : : */
156 : 47453 : 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 : 158455 : ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
164 : : }
165 : : }
166 : 25099 : }
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 : 30546 : toast_tuple_find_biggest_attribute(ToastTupleContext *ttc,
187 : : bool for_compression, bool check_main)
188 : : {
189 : 30546 : TupleDesc tupleDesc = ttc->ttc_rel->rd_att;
190 : 30546 : int numAttrs = tupleDesc->natts;
191 : 30546 : int biggest_attno = -1;
192 : : int32 biggest_size;
193 : 30546 : 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 : : */
27 michael@paquier.xyz 202 [ + + ]:GNC 30546 : if (RelationGetToastChunkIdType(ttc->ttc_rel) == OID8OID)
203 : 94 : biggest_size = MAXALIGN(TOAST_OID8_POINTER_SIZE);
204 : : else
205 : 30452 : biggest_size = MAXALIGN(TOAST_OID_POINTER_SIZE);
206 : :
2593 rhaas@postgresql.org 207 [ + + ]:CBC 30546 : if (for_compression)
208 : 29381 : skip_colflags |= TOASTCOL_INCOMPRESSIBLE;
209 : :
210 [ + + ]: 417432 : for (i = 0; i < numAttrs; i++)
211 : : {
212 : 386886 : Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
213 : :
214 [ + + ]: 386886 : if ((ttc->ttc_attr[i].tai_colflags & skip_colflags) != 0)
215 : 318162 : continue;
216 [ - + ]: 68724 : if (VARATT_IS_EXTERNAL(DatumGetPointer(ttc->ttc_values[i])))
2413 tgl@sss.pgh.pa.us 217 :UBC 0 : continue; /* can't happen, toast_action would be PLAIN */
2593 rhaas@postgresql.org 218 [ + + + + ]:CBC 133185 : if (for_compression &&
219 : 64461 : VARATT_IS_COMPRESSED(DatumGetPointer(ttc->ttc_values[i])))
220 : 9700 : continue;
2413 tgl@sss.pgh.pa.us 221 [ + + - + ]: 59024 : if (check_main && att->attstorage != TYPSTORAGE_MAIN)
2593 rhaas@postgresql.org 222 :UBC 0 : continue;
2413 tgl@sss.pgh.pa.us 223 [ + + + + ]:CBC 59024 : if (!check_main && att->attstorage != TYPSTORAGE_EXTENDED &&
224 [ + + ]: 3290 : att->attstorage != TYPSTORAGE_EXTERNAL)
2593 rhaas@postgresql.org 225 : 91 : continue;
226 : :
227 [ + + ]: 58933 : if (ttc->ttc_attr[i].tai_size > biggest_size)
228 : : {
229 : 39053 : biggest_attno = i;
230 : 39053 : biggest_size = ttc->ttc_attr[i].tai_size;
231 : : }
232 : : }
233 : :
234 : 30546 : 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 : 25006 : toast_tuple_try_compression(ToastTupleContext *ttc, int attribute)
244 : : {
245 : 25006 : Datum *value = &ttc->ttc_values[attribute];
246 : : Datum new_value;
247 : 25006 : ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
248 : :
2033 249 : 25006 : new_value = toast_compress_datum(*value, attr->tai_compression);
250 : :
2593 251 [ + + ]: 25006 : if (DatumGetPointer(new_value) != NULL)
252 : : {
253 : : /* successful compression */
254 [ + + ]: 23859 : if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
255 : 66 : pfree(DatumGetPointer(*value));
256 : 23859 : *value = new_value;
257 : 23859 : attr->tai_colflags |= TOASTCOL_NEEDS_FREE;
258 : 23859 : attr->tai_size = VARSIZE(DatumGetPointer(*value));
259 : 23859 : ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
260 : : }
261 : : else
262 : : {
263 : : /* incompressible, ignore on subsequent compression passes */
264 : 1147 : attr->tai_colflags |= TOASTCOL_INCOMPRESSIBLE;
265 : : }
266 : 25006 : }
267 : :
268 : : /*
269 : : * Move an attribute to external storage.
270 : : */
271 : : void
194 alvherre@kurilemu.de 272 : 11456 : toast_tuple_externalize(ToastTupleContext *ttc, int attribute, uint32 options)
273 : : {
2593 rhaas@postgresql.org 274 : 11456 : Datum *value = &ttc->ttc_values[attribute];
275 : 11456 : Datum old_value = *value;
276 : 11456 : ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
277 : :
278 : 11456 : attr->tai_colflags |= TOASTCOL_IGNORE;
279 : 11456 : *value = toast_save_datum(ttc->ttc_rel, old_value, attr->tai_oldexternal,
280 : : options);
281 [ + + ]: 11456 : if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
282 : 8391 : pfree(DatumGetPointer(old_value));
283 : 11456 : attr->tai_colflags |= TOASTCOL_NEEDS_FREE;
284 : 11456 : ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
285 : 11456 : }
286 : :
287 : : /*
288 : : * Perform appropriate cleanup after one tuple has been subjected to TOAST.
289 : : */
290 : : void
291 : 25099 : toast_tuple_cleanup(ToastTupleContext *ttc)
292 : : {
293 : 25099 : TupleDesc tupleDesc = ttc->ttc_rel->rd_att;
294 : 25099 : int numAttrs = tupleDesc->natts;
295 : :
296 : : /*
297 : : * Free allocated temp values
298 : : */
299 [ + + ]: 25099 : if ((ttc->ttc_flags & TOAST_NEEDS_FREE) != 0)
300 : : {
301 : : int i;
302 : :
303 [ + + ]: 254971 : for (i = 0; i < numAttrs; i++)
304 : : {
305 : 230003 : ToastAttrInfo *attr = &ttc->ttc_attr[i];
306 : :
307 [ + + ]: 230003 : if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
308 : 27653 : pfree(DatumGetPointer(ttc->ttc_values[i]));
309 : : }
310 : : }
311 : :
312 : : /*
313 : : * Delete external values from the old tuple
314 : : */
315 [ + + ]: 25099 : if ((ttc->ttc_flags & TOAST_NEEDS_DELETE_OLD) != 0)
316 : : {
317 : : int i;
318 : :
319 [ + + ]: 16264 : for (i = 0; i < numAttrs; i++)
320 : : {
321 : 15637 : ToastAttrInfo *attr = &ttc->ttc_attr[i];
322 : :
323 [ + + ]: 15637 : if ((attr->tai_colflags & TOASTCOL_NEEDS_DELETE_OLD) != 0)
324 : 736 : toast_delete_datum(ttc->ttc_rel, ttc->ttc_oldvalues[i], false);
325 : : }
326 : : }
327 : 25099 : }
328 : :
329 : : /*
330 : : * Check for external stored attributes and delete them from the secondary
331 : : * relation.
332 : : */
333 : : void
1098 peter@eisentraut.org 334 : 531 : toast_delete_external(Relation rel, const Datum *values, const bool *isnull,
335 : : bool is_speculative)
336 : : {
2593 rhaas@postgresql.org 337 : 531 : TupleDesc tupleDesc = rel->rd_att;
338 : 531 : int numAttrs = tupleDesc->natts;
339 : : int i;
340 : :
341 [ + + ]: 5131 : for (i = 0; i < numAttrs; i++)
342 : : {
661 drowley@postgresql.o 343 [ + + ]: 4600 : if (TupleDescCompactAttr(tupleDesc, i)->attlen == -1)
344 : : {
2593 rhaas@postgresql.org 345 : 1493 : Datum value = values[i];
346 : :
347 [ + + ]: 1493 : if (isnull[i])
348 : 423 : continue;
433 peter@eisentraut.org 349 [ + + ]: 1070 : else if (VARATT_IS_EXTERNAL_ONDISK(DatumGetPointer(value)))
2593 rhaas@postgresql.org 350 : 543 : toast_delete_datum(rel, value, is_speculative);
351 : : }
352 : : }
353 : 531 : }
|