Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit54e6184

Browse files
committed
amcheck: Support for different header sizes of short varlena datum
In the heap, tuples may contain short varlena datum with both 1B header and 4Bheaders. But the corresponding index tuple should always have such varlena'swith 1B headers. So, for fingerprinting, we need to convert.Backpatch to all supported versions.Discussion:https://postgr.es/m/flat/7bdbe559-d61a-4ae4-a6e1-48abdf3024cc%40postgrespro.ruAuthor: Michael ZhilinReviewed-by: Alexander Lakhin, Andrey Borodin, Jian He, Alexander KorotkovBackpatch-through: 12
1 parent12128be commit54e6184

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

‎contrib/amcheck/expected/check_btree.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ SELECT bt_index_check('bttest_a_expr_idx', true);
199199

200200
(1 row)
201201

202+
-- Check support of both 1B and 4B header sizes of short varlena datum
203+
CREATE TABLE varlena_bug (v text);
204+
ALTER TABLE varlena_bug ALTER column v SET storage plain;
205+
INSERT INTO varlena_bug VALUES ('x');
206+
COPY varlena_bug from stdin;
207+
CREATE INDEX varlena_bug_idx on varlena_bug(v);
208+
SELECT bt_index_check('varlena_bug_idx', true);
209+
bt_index_check
210+
----------------
211+
212+
(1 row)
213+
202214
-- cleanup
203215
DROP TABLE bttest_a;
204216
DROP TABLE bttest_b;
@@ -208,3 +220,4 @@ DROP TABLE toast_bug;
208220
DROP FUNCTION ifun(int8);
209221
DROP OWNED BY regress_bttest_role; -- permissions
210222
DROP ROLE regress_bttest_role;
223+
DROP TABLE varlena_bug;

‎contrib/amcheck/sql/check_btree.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ CREATE INDEX bttest_a_expr_idx ON bttest_a ((ifun(id) + ifun(0)))
135135

136136
SELECT bt_index_check('bttest_a_expr_idx', true);
137137

138+
-- Check support of both 1B and 4B header sizes of short varlena datum
139+
CREATETABLEvarlena_bug (vtext);
140+
ALTERTABLE varlena_bug ALTER column vSET storage plain;
141+
INSERT INTO varlena_bugVALUES ('x');
142+
COPY varlena_bugfrom stdin;
143+
x
144+
\.
145+
CREATEINDEXvarlena_bug_idxon varlena_bug(v);
146+
SELECT bt_index_check('varlena_bug_idx', true);
147+
138148
-- cleanup
139149
DROPTABLE bttest_a;
140150
DROPTABLE bttest_b;
@@ -144,3 +154,4 @@ DROP TABLE toast_bug;
144154
DROPFUNCTION ifun(int8);
145155
DROP OWNED BY regress_bttest_role;-- permissions
146156
DROP ROLE regress_bttest_role;
157+
DROPTABLE varlena_bug;

‎contrib/amcheck/verify_nbtree.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,7 +2641,7 @@ bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
26412641
TupleDesctupleDescriptor=RelationGetDescr(state->rel);
26422642
Datumnormalized[INDEX_MAX_KEYS];
26432643
boolisnull[INDEX_MAX_KEYS];
2644-
booltoast_free[INDEX_MAX_KEYS];
2644+
boolneed_free[INDEX_MAX_KEYS];
26452645
boolformnewtup= false;
26462646
IndexTuplereformed;
26472647
inti;
@@ -2660,7 +2660,7 @@ bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
26602660
att=TupleDescAttr(tupleDescriptor,i);
26612661

26622662
/* Assume untoasted/already normalized datum initially */
2663-
toast_free[i]= false;
2663+
need_free[i]= false;
26642664
normalized[i]=index_getattr(itup,att->attnum,
26652665
tupleDescriptor,
26662666
&isnull[i]);
@@ -2683,11 +2683,32 @@ bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
26832683
{
26842684
formnewtup= true;
26852685
normalized[i]=PointerGetDatum(PG_DETOAST_DATUM(normalized[i]));
2686-
toast_free[i]= true;
2686+
need_free[i]= true;
2687+
}
2688+
2689+
/*
2690+
* Short tuples may have 1B or 4B header. Convert 4B header of short
2691+
* tuples to 1B
2692+
*/
2693+
elseif (VARATT_CAN_MAKE_SHORT(DatumGetPointer(normalized[i])))
2694+
{
2695+
/* convert to short varlena */
2696+
Sizelen=VARATT_CONVERTED_SHORT_SIZE(DatumGetPointer(normalized[i]));
2697+
char*data=palloc(len);
2698+
2699+
SET_VARSIZE_SHORT(data,len);
2700+
memcpy(data+1,VARDATA(DatumGetPointer(normalized[i])),len-1);
2701+
2702+
formnewtup= true;
2703+
normalized[i]=PointerGetDatum(data);
2704+
need_free[i]= true;
26872705
}
26882706
}
26892707

2690-
/* Easier case: Tuple has varlena datums, none of which are compressed */
2708+
/*
2709+
* Easier case: Tuple has varlena datums, none of which are compressed or
2710+
* short with 4B header
2711+
*/
26912712
if (!formnewtup)
26922713
returnitup;
26932714

@@ -2697,6 +2718,11 @@ bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
26972718
* (normalized input datums). This is rather naive, but shouldn't be
26982719
* necessary too often.
26992720
*
2721+
* In the heap, tuples may contain short varlena datums with both 1B
2722+
* header and 4B headers. But the corresponding index tuple should always
2723+
* have such varlena's with 1B headers. So, if there is a short varlena
2724+
* with 4B header, we need to convert it for for fingerprinting.
2725+
*
27002726
* Note that we rely on deterministic index_form_tuple() TOAST compression
27012727
* of normalized input.
27022728
*/
@@ -2705,7 +2731,7 @@ bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
27052731

27062732
/* Cannot leak memory here */
27072733
for (i=0;i<tupleDescriptor->natts;i++)
2708-
if (toast_free[i])
2734+
if (need_free[i])
27092735
pfree(DatumGetPointer(normalized[i]));
27102736

27112737
returnreformed;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp