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

Commitbfade0e

Browse files
committed
Fix assorted issues in convert_to_scalar().
If convert_to_scalar is passed a pair of datatypes it can't cope with,its former behavior was just to elog(ERROR). While this is OK so far asthe core code is concerned, there's extension code that would like to usescalarltsel/scalargtsel/etc as selectivity estimators for operators thatwork on non-core datatypes, and this behavior is a show-stopper for thatuse-case. If we simply allow convert_to_scalar to return FALSE instead ofoutright failing, then the main logic of scalarltsel/scalargtsel will workfine for any operator that behaves like a scalar inequality comparison.The lack of conversion capability will mean that we can't estimate tobetter than histogram-bin-width precision, since the code will effectivelyassume that the comparison constant falls at the middle of its bin. Butthat's still a lot better than nothing. (Someday we should provide a wayfor extension code to supply a custom version of convert_to_scalar, buttoday is not that day.)While poking at this issue, we noted that the existing code for handlingtype bytea in convert_to_scalar is several bricks shy of a load.It assumes without checking that if the comparison value is type bytea,the bounds values are too; in the worst case this could lead to a crash.It also fails to detoast the input values, so that the comparison result iscomplete garbage if any input is toasted out-of-line, compressed, or evenjust short-header. I'm not sure how often such cases actually occur ---the bounds values, at least, are probably safe since they are elements ofan array and hence can't be toasted. But that doesn't make this code OK.Back-patch to all supported branches, partly because author requested that,but mostly because of the bytea bugs. The change in API for the exposedroutine convert_network_to_scalar() is theoretically a back-patch hazard,but it seems pretty unlikely that any third-party code is calling thatfunction directly.Tomas Vondra, with some adjustments by meDiscussion:https://postgr.es/m/b68441b6-d18f-13ab-b43b-9a72188a4e02@2ndquadrant.com
1 parent4346794 commitbfade0e

File tree

4 files changed

+90
-57
lines changed

4 files changed

+90
-57
lines changed

‎contrib/btree_gist/btree_inet.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ gbt_inet_compress(PG_FUNCTION_ARGS)
9999
if (entry->leafkey)
100100
{
101101
inetKEY*r= (inetKEY*)palloc(sizeof(inetKEY));
102+
boolfailure= false;
102103

103104
retval=palloc(sizeof(GISTENTRY));
104-
r->lower=convert_network_to_scalar(entry->key,INETOID);
105+
r->lower=convert_network_to_scalar(entry->key,INETOID,&failure);
106+
Assert(!failure);
105107
r->upper=r->lower;
106108
gistentryinit(*retval,PointerGetDatum(r),
107109
entry->rel,entry->page,
@@ -118,13 +120,18 @@ Datum
118120
gbt_inet_consistent(PG_FUNCTION_ARGS)
119121
{
120122
GISTENTRY*entry= (GISTENTRY*)PG_GETARG_POINTER(0);
121-
doublequery=convert_network_to_scalar(PG_GETARG_DATUM(1),INETOID);
123+
Datumdquery=PG_GETARG_DATUM(1);
122124
StrategyNumberstrategy= (StrategyNumber)PG_GETARG_UINT16(2);
123125

124126
/* Oidsubtype = PG_GETARG_OID(3); */
125127
bool*recheck= (bool*)PG_GETARG_POINTER(4);
126128
inetKEY*kkk= (inetKEY*)DatumGetPointer(entry->key);
127129
GBT_NUMKEY_Rkey;
130+
doublequery;
131+
boolfailure= false;
132+
133+
query=convert_network_to_scalar(dquery,INETOID,&failure);
134+
Assert(!failure);
128135

129136
/* All cases served by this function are inexact */
130137
*recheck= true;

‎src/backend/utils/adt/network.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -892,9 +892,12 @@ inet_merge(PG_FUNCTION_ARGS)
892892
* Convert a value of a network datatype to an approximate scalar value.
893893
* This is used for estimating selectivities of inequality operators
894894
* involving network types.
895+
*
896+
* On failure (e.g., unsupported typid), set *failure to true;
897+
* otherwise, that variable is not changed.
895898
*/
896899
double
897-
convert_network_to_scalar(Datumvalue,Oidtypid)
900+
convert_network_to_scalar(Datumvalue,Oidtypid,bool*failure)
898901
{
899902
switch (typid)
900903
{
@@ -921,8 +924,6 @@ convert_network_to_scalar(Datum value, Oid typid)
921924
res+=ip_addr(ip)[i];
922925
}
923926
returnres;
924-
925-
break;
926927
}
927928
caseMACADDROID:
928929
{
@@ -946,11 +947,7 @@ convert_network_to_scalar(Datum value, Oid typid)
946947
}
947948
}
948949

949-
/*
950-
* Can't get here unless someone tries to use scalarltsel/scalargtsel on
951-
* an operator with one network and one non-network operand.
952-
*/
953-
elog(ERROR,"unsupported type: %u",typid);
950+
*failure= true;
954951
return0;
955952
}
956953

‎src/backend/utils/adt/selfuncs.c

Lines changed: 75 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static bool estimate_multivariate_ndistinct(PlannerInfo *root,
175175
staticboolconvert_to_scalar(Datumvalue,Oidvaluetypid,double*scaledvalue,
176176
Datumlobound,Datumhibound,Oidboundstypid,
177177
double*scaledlobound,double*scaledhibound);
178-
staticdoubleconvert_numeric_to_scalar(Datumvalue,Oidtypid);
178+
staticdoubleconvert_numeric_to_scalar(Datumvalue,Oidtypid,bool*failure);
179179
staticvoidconvert_string_to_scalar(char*value,
180180
double*scaledvalue,
181181
char*lobound,
@@ -192,8 +192,9 @@ static double convert_one_string_to_scalar(char *value,
192192
intrangelo,intrangehi);
193193
staticdoubleconvert_one_bytea_to_scalar(unsignedchar*value,intvaluelen,
194194
intrangelo,intrangehi);
195-
staticchar*convert_string_datum(Datumvalue,Oidtypid);
196-
staticdoubleconvert_timevalue_to_scalar(Datumvalue,Oidtypid);
195+
staticchar*convert_string_datum(Datumvalue,Oidtypid,bool*failure);
196+
staticdoubleconvert_timevalue_to_scalar(Datumvalue,Oidtypid,
197+
bool*failure);
197198
staticvoidexamine_simple_variable(PlannerInfo*root,Var*var,
198199
VariableStatData*vardata);
199200
staticboolget_variable_range(PlannerInfo*root,VariableStatData*vardata,
@@ -552,7 +553,8 @@ neqsel(PG_FUNCTION_ARGS)
552553
*
553554
* This routine works for any datatype (or pair of datatypes) known to
554555
* convert_to_scalar(). If it is applied to some other datatype,
555-
* it will return a default estimate.
556+
* it will return an approximate estimate based on assuming that the constant
557+
* value falls in the middle of the bin identified by binary search.
556558
*/
557559
staticdouble
558560
scalarineqsel(PlannerInfo*root,Oidoperator,boolisgt,
@@ -3888,10 +3890,15 @@ convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
38883890
Datumlobound,Datumhibound,Oidboundstypid,
38893891
double*scaledlobound,double*scaledhibound)
38903892
{
3893+
boolfailure= false;
3894+
38913895
/*
38923896
* Both the valuetypid and the boundstypid should exactly match the
3893-
* declared input type(s) of the operator we are invoked for, so we just
3894-
* error out if either is not recognized.
3897+
* declared input type(s) of the operator we are invoked for. However,
3898+
* extensions might try to use scalarineqsel as estimator for operators
3899+
* with input type(s) we don't handle here; in such cases, we want to
3900+
* return false, not fail. In any case, we mustn't assume that valuetypid
3901+
* and boundstypid are identical.
38953902
*
38963903
* XXX The histogram we are interpolating between points of could belong
38973904
* to a column that's only binary-compatible with the declared type. In
@@ -3926,10 +3933,13 @@ convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
39263933
caseREGDICTIONARYOID:
39273934
caseREGROLEOID:
39283935
caseREGNAMESPACEOID:
3929-
*scaledvalue=convert_numeric_to_scalar(value,valuetypid);
3930-
*scaledlobound=convert_numeric_to_scalar(lobound,boundstypid);
3931-
*scaledhibound=convert_numeric_to_scalar(hibound,boundstypid);
3932-
return true;
3936+
*scaledvalue=convert_numeric_to_scalar(value,valuetypid,
3937+
&failure);
3938+
*scaledlobound=convert_numeric_to_scalar(lobound,boundstypid,
3939+
&failure);
3940+
*scaledhibound=convert_numeric_to_scalar(hibound,boundstypid,
3941+
&failure);
3942+
return !failure;
39333943

39343944
/*
39353945
* Built-in string types
@@ -3940,9 +3950,20 @@ convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
39403950
caseTEXTOID:
39413951
caseNAMEOID:
39423952
{
3943-
char*valstr=convert_string_datum(value,valuetypid);
3944-
char*lostr=convert_string_datum(lobound,boundstypid);
3945-
char*histr=convert_string_datum(hibound,boundstypid);
3953+
char*valstr=convert_string_datum(value,valuetypid,
3954+
&failure);
3955+
char*lostr=convert_string_datum(lobound,boundstypid,
3956+
&failure);
3957+
char*histr=convert_string_datum(hibound,boundstypid,
3958+
&failure);
3959+
3960+
/*
3961+
* Bail out if any of the values is not of string type. We
3962+
* might leak converted strings for the other value(s), but
3963+
* that's not worth troubling over.
3964+
*/
3965+
if (failure)
3966+
return false;
39463967

39473968
convert_string_to_scalar(valstr,scaledvalue,
39483969
lostr,scaledlobound,
@@ -3958,6 +3979,9 @@ convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
39583979
*/
39593980
caseBYTEAOID:
39603981
{
3982+
/* We only support bytea vs bytea comparison */
3983+
if (boundstypid!=BYTEAOID)
3984+
return false;
39613985
convert_bytea_to_scalar(value,scaledvalue,
39623986
lobound,scaledlobound,
39633987
hibound,scaledhibound);
@@ -3976,10 +4000,13 @@ convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
39764000
caseTINTERVALOID:
39774001
caseTIMEOID:
39784002
caseTIMETZOID:
3979-
*scaledvalue=convert_timevalue_to_scalar(value,valuetypid);
3980-
*scaledlobound=convert_timevalue_to_scalar(lobound,boundstypid);
3981-
*scaledhibound=convert_timevalue_to_scalar(hibound,boundstypid);
3982-
return true;
4003+
*scaledvalue=convert_timevalue_to_scalar(value,valuetypid,
4004+
&failure);
4005+
*scaledlobound=convert_timevalue_to_scalar(lobound,boundstypid,
4006+
&failure);
4007+
*scaledhibound=convert_timevalue_to_scalar(hibound,boundstypid,
4008+
&failure);
4009+
return !failure;
39834010

39844011
/*
39854012
* Built-in network types
@@ -3988,10 +4015,13 @@ convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
39884015
caseCIDROID:
39894016
caseMACADDROID:
39904017
caseMACADDR8OID:
3991-
*scaledvalue=convert_network_to_scalar(value,valuetypid);
3992-
*scaledlobound=convert_network_to_scalar(lobound,boundstypid);
3993-
*scaledhibound=convert_network_to_scalar(hibound,boundstypid);
3994-
return true;
4018+
*scaledvalue=convert_network_to_scalar(value,valuetypid,
4019+
&failure);
4020+
*scaledlobound=convert_network_to_scalar(lobound,boundstypid,
4021+
&failure);
4022+
*scaledhibound=convert_network_to_scalar(hibound,boundstypid,
4023+
&failure);
4024+
return !failure;
39954025
}
39964026
/* Don't know how to convert */
39974027
*scaledvalue=*scaledlobound=*scaledhibound=0;
@@ -4000,9 +4030,12 @@ convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
40004030

40014031
/*
40024032
* Do convert_to_scalar()'s work for any numeric data type.
4033+
*
4034+
* On failure (e.g., unsupported typid), set *failure to true;
4035+
* otherwise, that variable is not changed.
40034036
*/
40044037
staticdouble
4005-
convert_numeric_to_scalar(Datumvalue,Oidtypid)
4038+
convert_numeric_to_scalar(Datumvalue,Oidtypid,bool*failure)
40064039
{
40074040
switch (typid)
40084041
{
@@ -4038,11 +4071,7 @@ convert_numeric_to_scalar(Datum value, Oid typid)
40384071
return (double)DatumGetObjectId(value);
40394072
}
40404073

4041-
/*
4042-
* Can't get here unless someone tries to use scalarltsel/scalargtsel on
4043-
* an operator with one numeric and one non-numeric operand.
4044-
*/
4045-
elog(ERROR,"unsupported type: %u",typid);
4074+
*failure= true;
40464075
return0;
40474076
}
40484077

@@ -4191,11 +4220,14 @@ convert_one_string_to_scalar(char *value, int rangelo, int rangehi)
41914220
/*
41924221
* Convert a string-type Datum into a palloc'd, null-terminated string.
41934222
*
4223+
* On failure (e.g., unsupported typid), set *failure to true;
4224+
* otherwise, that variable is not changed. (We'll return NULL on failure.)
4225+
*
41944226
* When using a non-C locale, we must pass the string through strxfrm()
41954227
* before continuing, so as to generate correct locale-specific results.
41964228
*/
41974229
staticchar*
4198-
convert_string_datum(Datumvalue,Oidtypid)
4230+
convert_string_datum(Datumvalue,Oidtypid,bool*failure)
41994231
{
42004232
char*val;
42014233

@@ -4219,12 +4251,7 @@ convert_string_datum(Datum value, Oid typid)
42194251
break;
42204252
}
42214253
default:
4222-
4223-
/*
4224-
* Can't get here unless someone tries to use scalarltsel on an
4225-
* operator with one string and one non-string operand.
4226-
*/
4227-
elog(ERROR,"unsupported type: %u",typid);
4254+
*failure= true;
42284255
returnNULL;
42294256
}
42304257

@@ -4301,16 +4328,19 @@ convert_bytea_to_scalar(Datum value,
43014328
Datumhibound,
43024329
double*scaledhibound)
43034330
{
4331+
bytea*valuep=DatumGetByteaPP(value);
4332+
bytea*loboundp=DatumGetByteaPP(lobound);
4333+
bytea*hiboundp=DatumGetByteaPP(hibound);
43044334
intrangelo,
43054335
rangehi,
4306-
valuelen=VARSIZE(DatumGetPointer(value))-VARHDRSZ,
4307-
loboundlen=VARSIZE(DatumGetPointer(lobound))-VARHDRSZ,
4308-
hiboundlen=VARSIZE(DatumGetPointer(hibound))-VARHDRSZ,
4336+
valuelen=VARSIZE_ANY_EXHDR(valuep),
4337+
loboundlen=VARSIZE_ANY_EXHDR(loboundp),
4338+
hiboundlen=VARSIZE_ANY_EXHDR(hiboundp),
43094339
i,
43104340
minlen;
4311-
unsignedchar*valstr= (unsignedchar*)VARDATA(DatumGetPointer(value)),
4312-
*lostr= (unsignedchar*)VARDATA(DatumGetPointer(lobound)),
4313-
*histr= (unsignedchar*)VARDATA(DatumGetPointer(hibound));
4341+
unsignedchar*valstr= (unsignedchar*)VARDATA_ANY(valuep);
4342+
unsignedchar*lostr= (unsignedchar*)VARDATA_ANY(loboundp);
4343+
unsignedchar*histr= (unsignedchar*)VARDATA_ANY(hiboundp);
43144344

43154345
/*
43164346
* Assume bytea data is uniformly distributed across all byte values.
@@ -4377,9 +4407,12 @@ convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
43774407

43784408
/*
43794409
* Do convert_to_scalar()'s work for any timevalue data type.
4410+
*
4411+
* On failure (e.g., unsupported typid), set *failure to true;
4412+
* otherwise, that variable is not changed.
43804413
*/
43814414
staticdouble
4382-
convert_timevalue_to_scalar(Datumvalue,Oidtypid)
4415+
convert_timevalue_to_scalar(Datumvalue,Oidtypid,bool*failure)
43834416
{
43844417
switch (typid)
43854418
{
@@ -4425,11 +4458,7 @@ convert_timevalue_to_scalar(Datum value, Oid typid)
44254458
}
44264459
}
44274460

4428-
/*
4429-
* Can't get here unless someone tries to use scalarltsel/scalargtsel on
4430-
* an operator with one timevalue and one non-timevalue operand.
4431-
*/
4432-
elog(ERROR,"unsupported type: %u",typid);
4461+
*failure= true;
44334462
return0;
44344463
}
44354464

‎src/include/utils/builtins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ extern int inet_net_pton(int af, const char *src,
103103
void*dst,size_tsize);
104104

105105
/* network.c */
106-
externdoubleconvert_network_to_scalar(Datumvalue,Oidtypid);
106+
externdoubleconvert_network_to_scalar(Datumvalue,Oidtypid,bool*failure);
107107
externDatumnetwork_scan_first(Datumin);
108108
externDatumnetwork_scan_last(Datumin);
109109
externvoidclean_ipv6_addr(intaddr_family,char*addr);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp