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

Commite2c2414

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 parentbf038eb commite2c2414

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
@@ -2523,7 +2523,7 @@ bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
25232523
TupleDesctupleDescriptor=RelationGetDescr(state->rel);
25242524
Datumnormalized[INDEX_MAX_KEYS];
25252525
boolisnull[INDEX_MAX_KEYS];
2526-
booltoast_free[INDEX_MAX_KEYS];
2526+
boolneed_free[INDEX_MAX_KEYS];
25272527
boolformnewtup= false;
25282528
IndexTuplereformed;
25292529
inti;
@@ -2542,7 +2542,7 @@ bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
25422542
att=TupleDescAttr(tupleDescriptor,i);
25432543

25442544
/* Assume untoasted/already normalized datum initially */
2545-
toast_free[i]= false;
2545+
need_free[i]= false;
25462546
normalized[i]=index_getattr(itup,att->attnum,
25472547
tupleDescriptor,
25482548
&isnull[i]);
@@ -2565,11 +2565,32 @@ bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
25652565
{
25662566
formnewtup= true;
25672567
normalized[i]=PointerGetDatum(PG_DETOAST_DATUM(normalized[i]));
2568-
toast_free[i]= true;
2568+
need_free[i]= true;
2569+
}
2570+
2571+
/*
2572+
* Short tuples may have 1B or 4B header. Convert 4B header of short
2573+
* tuples to 1B
2574+
*/
2575+
elseif (VARATT_CAN_MAKE_SHORT(DatumGetPointer(normalized[i])))
2576+
{
2577+
/* convert to short varlena */
2578+
Sizelen=VARATT_CONVERTED_SHORT_SIZE(DatumGetPointer(normalized[i]));
2579+
char*data=palloc(len);
2580+
2581+
SET_VARSIZE_SHORT(data,len);
2582+
memcpy(data+1,VARDATA(DatumGetPointer(normalized[i])),len-1);
2583+
2584+
formnewtup= true;
2585+
normalized[i]=PointerGetDatum(data);
2586+
need_free[i]= true;
25692587
}
25702588
}
25712589

2572-
/* Easier case: Tuple has varlena datums, none of which are compressed */
2590+
/*
2591+
* Easier case: Tuple has varlena datums, none of which are compressed or
2592+
* short with 4B header
2593+
*/
25732594
if (!formnewtup)
25742595
returnitup;
25752596

@@ -2579,6 +2600,11 @@ bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
25792600
* (normalized input datums). This is rather naive, but shouldn't be
25802601
* necessary too often.
25812602
*
2603+
* In the heap, tuples may contain short varlena datums with both 1B
2604+
* header and 4B headers. But the corresponding index tuple should always
2605+
* have such varlena's with 1B headers. So, if there is a short varlena
2606+
* with 4B header, we need to convert it for for fingerprinting.
2607+
*
25822608
* Note that we rely on deterministic index_form_tuple() TOAST compression
25832609
* of normalized input.
25842610
*/
@@ -2587,7 +2613,7 @@ bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
25872613

25882614
/* Cannot leak memory here */
25892615
for (i=0;i<tupleDescriptor->natts;i++)
2590-
if (toast_free[i])
2616+
if (need_free[i])
25912617
pfree(DatumGetPointer(normalized[i]));
25922618

25932619
returnreformed;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp