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

Commit5df5d9c

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 parenta1a51dc commit5df5d9c

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
@@ -2640,7 +2640,7 @@ bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
26402640
TupleDesctupleDescriptor=RelationGetDescr(state->rel);
26412641
Datumnormalized[INDEX_MAX_KEYS];
26422642
boolisnull[INDEX_MAX_KEYS];
2643-
booltoast_free[INDEX_MAX_KEYS];
2643+
boolneed_free[INDEX_MAX_KEYS];
26442644
boolformnewtup= false;
26452645
IndexTuplereformed;
26462646
inti;
@@ -2659,7 +2659,7 @@ bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
26592659
att=TupleDescAttr(tupleDescriptor,i);
26602660

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

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

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

27052731
/* Cannot leak memory here */
27062732
for (i=0;i<tupleDescriptor->natts;i++)
2707-
if (toast_free[i])
2733+
if (need_free[i])
27082734
pfree(DatumGetPointer(normalized[i]));
27092735

27102736
returnreformed;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp