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

Commit9221f9d

Browse files
committed
Make contrib/btree_gist's GiST penalty function a bit saner.
The previous coding supposed that the first differing bytes in two varlenadatums must have the same sign difference as their overall comparisonresult. This is obviously bogus for text strings in non-C locales, andprobably wrong for numeric, and even for bytea I think it was wrong onmachines where char is signed. When the assumption failed, the functioncould deliver a zero or negative penalty in situations where such a resultis quite ridiculous, leading the core GiST code to make very bad page-splitdecisions.To fix, take the absolute values of the byte-level differences. Also,switch the code to using unsigned char not just char, so that the behaviorwill be consistent whether char is signed or not.Per investigation of a trouble report from Tomas Vondra. Back-patch to allsupported branches.
1 parent94f565d commit9221f9d

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

‎contrib/btree_gist/btree_utils_var.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,12 @@ gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo)
108108
staticint32
109109
gbt_var_node_cp_len(constGBT_VARKEY*node,constgbtree_vinfo*tinfo)
110110
{
111-
112111
GBT_VARKEY_Rr=gbt_var_key_readable(node);
113112
int32i=0;
114113
int32l=0;
115114
int32t1len=VARSIZE(r.lower)-VARHDRSZ;
116115
int32t2len=VARSIZE(r.upper)-VARHDRSZ;
117116
int32ml=Min(t1len,t2len);
118-
119117
char*p1=VARDATA(r.lower);
120118
char*p2=VARDATA(r.upper);
121119

@@ -126,7 +124,6 @@ gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
126124
{
127125
if (tinfo->eml>1&&l==0)
128126
{
129-
130127
if ((l=pg_mblen(p1))!=pg_mblen(p2))
131128
{
132129
returni;
@@ -369,13 +366,14 @@ gbt_var_penalty(float *res, const GISTENTRY *o, const GISTENTRY *n,
369366
GBT_VARKEY*newe= (GBT_VARKEY*)DatumGetPointer(n->key);
370367
GBT_VARKEY_Rok,
371368
nk;
372-
GBT_VARKEY*tmp=NULL;
373369

374370
*res=0.0;
375371

376372
nk=gbt_var_key_readable(newe);
377373
if (nk.lower==nk.upper)/* leaf */
378374
{
375+
GBT_VARKEY*tmp;
376+
379377
tmp=gbt_var_leaf2node(newe,tinfo);
380378
if (tmp!=newe)
381379
nk=gbt_var_key_readable(tmp);
@@ -390,7 +388,7 @@ gbt_var_penalty(float *res, const GISTENTRY *o, const GISTENTRY *n,
390388
gbt_bytea_pf_match(ok.upper,nk.upper,tinfo))))
391389
{
392390
Datumd=PointerGetDatum(0);
393-
doubledres=0.0;
391+
doubledres;
394392
int32ol,
395393
ul;
396394

@@ -401,20 +399,18 @@ gbt_var_penalty(float *res, const GISTENTRY *o, const GISTENTRY *n,
401399

402400
if (ul<ol)
403401
{
404-
dres= (ol-ul);/*lost of common prefix len */
402+
dres= (ol-ul);/*reduction of common prefix len */
405403
}
406404
else
407405
{
408406
GBT_VARKEY_Ruk=gbt_var_key_readable((GBT_VARKEY*)DatumGetPointer(d));
407+
unsignedchartmp[4];
409408

410-
chartmp[4];
411-
412-
tmp[0]= ((VARSIZE(ok.lower)-VARHDRSZ)==ul) ? (CHAR_MIN) : (VARDATA(ok.lower)[ul]);
413-
tmp[1]= ((VARSIZE(uk.lower)-VARHDRSZ)==ul) ? (CHAR_MIN) : (VARDATA(uk.lower)[ul]);
414-
tmp[2]= ((VARSIZE(ok.upper)-VARHDRSZ)==ul) ? (CHAR_MIN) : (VARDATA(ok.upper)[ul]);
415-
tmp[3]= ((VARSIZE(uk.upper)-VARHDRSZ)==ul) ? (CHAR_MIN) : (VARDATA(uk.upper)[ul]);
416-
dres= (tmp[0]-tmp[1])+
417-
(tmp[3]-tmp[2]);
409+
tmp[0]= (unsignedchar) (((VARSIZE(ok.lower)-VARHDRSZ) <=ul) ?0 : (VARDATA(ok.lower)[ul]));
410+
tmp[1]= (unsignedchar) (((VARSIZE(uk.lower)-VARHDRSZ) <=ul) ?0 : (VARDATA(uk.lower)[ul]));
411+
tmp[2]= (unsignedchar) (((VARSIZE(ok.upper)-VARHDRSZ) <=ul) ?0 : (VARDATA(ok.upper)[ul]));
412+
tmp[3]= (unsignedchar) (((VARSIZE(uk.upper)-VARHDRSZ) <=ul) ?0 : (VARDATA(uk.upper)[ul]));
413+
dres=Abs(tmp[0]-tmp[1])+Abs(tmp[3]-tmp[2]);
418414
dres /=256.0;
419415
}
420416

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp