]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
amcheck: Normalize index tuples containing uncompressed varlena
authorAlexander Korotkov <akorotkov@postgresql.org>
Sat, 23 Mar 2024 21:00:06 +0000 (23:00 +0200)
committerAlexander Korotkov <akorotkov@postgresql.org>
Sat, 23 Mar 2024 22:08:13 +0000 (00:08 +0200)
It might happen that the varlena value wasn't compressed by index_form_tuple()
due to current storage parameters.  If compression is currently enabled, we
need to compress such values to match index tuple coming from the heap.

Backpatch to all supported versions.

Discussion: https://postgr.es/m/flat/7bdbe559-d61a-4ae4-a6e1-48abdf3024cc%40postgrespro.ru
Author: Andrey Borodin
Reviewed-by: Alexander Lakhin, Michael Zhilin, Jian He, Alexander Korotkov
Backpatch-through: 12

contrib/amcheck/expected/check_btree.out
contrib/amcheck/sql/check_btree.sql
contrib/amcheck/verify_nbtree.c

index fecbeb05beb7a793ec69e2887e5630fec5097755..5212af072255bef80f78e673b902aff87c161834 100644 (file)
@@ -202,6 +202,16 @@ SELECT bt_index_check('varlena_bug_idx', true);
  
 (1 row)
 
+-- Also check that we compress varlena values, which were previously stored
+-- uncompressed in index.
+INSERT INTO varlena_bug VALUES (repeat('Test', 250));
+ALTER TABLE varlena_bug ALTER COLUMN v SET STORAGE extended;
+SELECT bt_index_check('varlena_bug_idx', true);
+ bt_index_check 
+----------------
+(1 row)
+
 -- cleanup
 DROP TABLE bttest_a;
 DROP TABLE bttest_b;
index b65df1219304d542bc29694909c78e7215243f25..9871fc840239a4c2c570f75ce21dfb1b923cc44b 100644 (file)
@@ -140,6 +140,12 @@ x
 CREATE INDEX varlena_bug_idx on varlena_bug(v);
 SELECT bt_index_check('varlena_bug_idx', true);
 
+-- Also check that we compress varlena values, which were previously stored
+-- uncompressed in index.
+INSERT INTO varlena_bug VALUES (repeat('Test', 250));
+ALTER TABLE varlena_bug ALTER COLUMN v SET STORAGE extended;
+SELECT bt_index_check('varlena_bug_idx', true);
+
 -- cleanup
 DROP TABLE bttest_a;
 DROP TABLE bttest_b;
index fa94c6c9b4140114374aef07671e1830565d0a79..a8a6eb2bfda1c985cedb5349973215b32a9aada9 100644 (file)
@@ -28,6 +28,7 @@
 #include "access/table.h"
 #include "access/tableam.h"
 #include "access/transam.h"
+#include "access/tuptoaster.h"
 #include "access/xact.h"
 #include "catalog/index.h"
 #include "catalog/pg_am.h"
@@ -2093,6 +2094,18 @@ bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
                                                        ItemPointerGetBlockNumber(&(itup->t_tid)),
                                                        ItemPointerGetOffsetNumber(&(itup->t_tid)),
                                                        RelationGetRelationName(state->rel))));
+               else if (!VARATT_IS_COMPRESSED(DatumGetPointer(normalized[i])) &&
+                                VARSIZE(DatumGetPointer(normalized[i])) > TOAST_INDEX_TARGET &&
+                                (att->attstorage == 'x' ||
+                                 att->attstorage == 'm'))
+               {
+                       /*
+                        * This value will be compressed by index_form_tuple() with the
+                        * current storage settings.  We may be here because this tuple
+                        * was formed with different storage settings.  So, force forming.
+                        */
+                       formnewtup = true;
+               }
                else if (VARATT_IS_COMPRESSED(DatumGetPointer(normalized[i])))
                {
                        formnewtup = true;