Skip to content

Commit dbab0c0

Browse files
committed
Remove forced toast recompression in VACUUM FULL/CLUSTER
The extra checks added by the recompression of toast data introduced in bbe0a81 is proving to have a performance impact on VACUUM or CLUSTER even if no recompression is done. This is more noticeable with more toastable columns that contain non-NULL values. Improvements could be done to make those extra checks less expensive, but that's not material for 14 at this stage, and we are not sure either if the code path of VACUUM FULL/CLUSTER is adapted for this job. Per discussion with several people, including Andres Freund, Robert Haas, Álvaro Herrera, Tom Lane and myself. Discussion: https://postgr.es/m/20210527003144.xxqppojoiwurc2iz@alap3.anarazel.de
1 parent f807e34 commit dbab0c0

File tree

5 files changed

+6
-66
lines changed

5 files changed

+6
-66
lines changed

doc/src/sgml/ref/alter_table.sgml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
394394
values inserted in future will be compressed (if the storage mode
395395
permits compression at all).
396396
This does not cause the table to be rewritten, so existing data may still
397-
be compressed with other compression methods. If the table is rewritten with
398-
<command>VACUUM FULL</command> or <command>CLUSTER</command>, or restored
397+
be compressed with other compression methods. If the table is restored
399398
with <application>pg_restore</application>, then all values are rewritten
400399
with the configured compression method.
401400
However, when data is inserted from another relation (for example,

src/backend/access/heap/heapam_handler.c

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919
*/
2020
#include "postgres.h"
2121

22-
#include "access/detoast.h"
2322
#include "access/genam.h"
2423
#include "access/heapam.h"
2524
#include "access/heaptoast.h"
2625
#include "access/multixact.h"
2726
#include "access/rewriteheap.h"
2827
#include "access/syncscan.h"
2928
#include "access/tableam.h"
30-
#include "access/toast_compression.h"
3129
#include "access/tsmapi.h"
3230
#include "access/xact.h"
3331
#include "catalog/catalog.h"
@@ -2463,78 +2461,21 @@ reform_and_rewrite_tuple(HeapTuple tuple,
24632461
TupleDesc newTupDesc = RelationGetDescr(NewHeap);
24642462
HeapTuple copiedTuple;
24652463
int i;
2466-
bool values_free[MaxTupleAttributeNumber];
2467-
2468-
memset(values_free, 0, newTupDesc->natts * sizeof(bool));
24692464

24702465
heap_deform_tuple(tuple, oldTupDesc, values, isnull);
24712466

2467+
/* Be sure to null out any dropped columns */
24722468
for (i = 0; i < newTupDesc->natts; i++)
24732469
{
2474-
/* Be sure to null out any dropped columns */
24752470
if (TupleDescAttr(newTupDesc, i)->attisdropped)
24762471
isnull[i] = true;
2477-
else if (!isnull[i] && TupleDescAttr(newTupDesc, i)->attlen == -1)
2478-
{
2479-
/*
2480-
* Use this opportunity to force recompression of any data that's
2481-
* compressed with some TOAST compression method other than the
2482-
* one configured for the column. We don't actually need to
2483-
* perform the compression here; we just need to decompress. That
2484-
* will trigger recompression later on.
2485-
*/
2486-
struct varlena *new_value;
2487-
ToastCompressionId cmid;
2488-
char cmethod;
2489-
char targetmethod;
2490-
2491-
new_value = (struct varlena *) DatumGetPointer(values[i]);
2492-
cmid = toast_get_compression_id(new_value);
2493-
2494-
/* nothing to be done for uncompressed data */
2495-
if (cmid == TOAST_INVALID_COMPRESSION_ID)
2496-
continue;
2497-
2498-
/* convert existing compression id to compression method */
2499-
switch (cmid)
2500-
{
2501-
case TOAST_PGLZ_COMPRESSION_ID:
2502-
cmethod = TOAST_PGLZ_COMPRESSION;
2503-
break;
2504-
case TOAST_LZ4_COMPRESSION_ID:
2505-
cmethod = TOAST_LZ4_COMPRESSION;
2506-
break;
2507-
default:
2508-
elog(ERROR, "invalid compression method id %d", cmid);
2509-
cmethod = '\0'; /* keep compiler quiet */
2510-
}
2511-
2512-
/* figure out what the target method is */
2513-
targetmethod = TupleDescAttr(newTupDesc, i)->attcompression;
2514-
if (!CompressionMethodIsValid(targetmethod))
2515-
targetmethod = default_toast_compression;
2516-
2517-
/* if compression method doesn't match then detoast the value */
2518-
if (targetmethod != cmethod)
2519-
{
2520-
values[i] = PointerGetDatum(detoast_attr(new_value));
2521-
values_free[i] = true;
2522-
}
2523-
}
25242472
}
25252473

25262474
copiedTuple = heap_form_tuple(newTupDesc, values, isnull);
25272475

25282476
/* The heap rewrite module does the rest */
25292477
rewrite_heap_tuple(rwstate, tuple, copiedTuple);
25302478

2531-
/* Free any value detoasted previously */
2532-
for (i = 0; i < newTupDesc->natts; i++)
2533-
{
2534-
if (values_free[i])
2535-
pfree(DatumGetPointer(values[i]));
2536-
}
2537-
25382479
heap_freetuple(copiedTuple);
25392480
}
25402481

src/test/regress/expected/compression.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ SELECT pg_column_compression(f1) FROM cmpart2;
297297
lz4
298298
(2 rows)
299299

300-
--vacuum full to recompress the data
300+
-- VACUUM FULL does not recompress
301301
SELECT pg_column_compression(f1) FROM cmdata;
302302
pg_column_compression
303303
-----------------------
@@ -309,7 +309,7 @@ VACUUM FULL cmdata;
309309
SELECT pg_column_compression(f1) FROM cmdata;
310310
pg_column_compression
311311
-----------------------
312-
lz4
312+
pglz
313313
lz4
314314
(2 rows)
315315

src/test/regress/expected/compression_1.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ SELECT pg_column_compression(f1) FROM cmpart2;
293293
-----------------------
294294
(0 rows)
295295

296-
--vacuum full to recompress the data
296+
-- VACUUM FULL does not recompress
297297
SELECT pg_column_compression(f1) FROM cmdata;
298298
pg_column_compression
299299
-----------------------

src/test/regress/sql/compression.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ INSERT INTO cmpart VALUES (repeat('123456789', 4004));
126126
SELECT pg_column_compression(f1) FROM cmpart1;
127127
SELECT pg_column_compression(f1) FROM cmpart2;
128128

129-
--vacuum full to recompress the data
129+
-- VACUUM FULL does not recompress
130130
SELECT pg_column_compression(f1) FROM cmdata;
131131
VACUUM FULL cmdata;
132132
SELECT pg_column_compression(f1) FROM cmdata;

0 commit comments

Comments
 (0)