Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e85fa2

Browse files
committed
patternsel() was improperly stripping RelabelType from the derived
expressions it constructed, causing scalarineqsel to become confused if the underlying variable was of a domain type. Per report from Kevin Grittner.
1 parent 7148de1 commit 1e85fa2

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/backend/utils/adt/selfuncs.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.178 2005/04/25 01:30:14 tgl Exp $
18+
* $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.179 2005/06/01 17:05:11 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -163,7 +163,7 @@ static void examine_variable(Query *root, Node *node, int varRelid,
163163
static double get_variable_numdistinct(VariableStatData *vardata);
164164
static bool get_variable_maximum(Query *root, VariableStatData *vardata,
165165
Oid sortop, Datum *max);
166-
static Selectivity prefix_selectivity(Query *root, VariableStatData *vardata,
166+
static Selectivity prefix_selectivity(Query *root, Node *variable,
167167
Oid opclass, Const *prefix);
168168
static Selectivity pattern_selectivity(Const *patt, Pattern_Type ptype);
169169
static Datum string_to_datum(const char *str, Oid datatype);
@@ -812,6 +812,7 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
812812
List *args = (List *) PG_GETARG_POINTER(2);
813813
int varRelid = PG_GETARG_INT32(3);
814814
VariableStatData vardata;
815+
Node *variable;
815816
Node *other;
816817
bool varonleft;
817818
Datum constval;
@@ -836,6 +837,7 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
836837
ReleaseVariableStats(vardata);
837838
return DEFAULT_MATCH_SEL;
838839
}
840+
variable = (Node *) linitial(args);
839841

840842
/*
841843
* If the constant is NULL, assume operator is strict and return zero,
@@ -939,7 +941,7 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
939941

940942
if (eqopr == InvalidOid)
941943
elog(ERROR, "no = operator for opclass %u", opclass);
942-
eqargs = list_make2(vardata.var, prefix);
944+
eqargs = list_make2(variable, prefix);
943945
result = DatumGetFloat8(DirectFunctionCall4(eqsel,
944946
PointerGetDatum(root),
945947
ObjectIdGetDatum(eqopr),
@@ -958,7 +960,7 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
958960
Selectivity selec;
959961

960962
if (pstatus == Pattern_Prefix_Partial)
961-
prefixsel = prefix_selectivity(root, &vardata, opclass, prefix);
963+
prefixsel = prefix_selectivity(root, variable, opclass, prefix);
962964
else
963965
prefixsel = 1.0;
964966
restsel = pattern_selectivity(rest, ptype);
@@ -3694,7 +3696,7 @@ pattern_fixed_prefix(Const *patt, Pattern_Type ptype,
36943696
* more useful to use the upper-bound code than not.
36953697
*/
36963698
static Selectivity
3697-
prefix_selectivity(Query *root, VariableStatData *vardata,
3699+
prefix_selectivity(Query *root, Node *variable,
36983700
Oid opclass, Const *prefixcon)
36993701
{
37003702
Selectivity prefixsel;
@@ -3706,7 +3708,7 @@ prefix_selectivity(Query *root, VariableStatData *vardata,
37063708
BTGreaterEqualStrategyNumber);
37073709
if (cmpopr == InvalidOid)
37083710
elog(ERROR, "no >= operator for opclass %u", opclass);
3709-
cmpargs = list_make2(vardata->var, prefixcon);
3711+
cmpargs = list_make2(variable, prefixcon);
37103712
/* Assume scalargtsel is appropriate for all supported types */
37113713
prefixsel = DatumGetFloat8(DirectFunctionCall4(scalargtsel,
37123714
PointerGetDatum(root),
@@ -3728,7 +3730,7 @@ prefix_selectivity(Query *root, VariableStatData *vardata,
37283730
BTLessStrategyNumber);
37293731
if (cmpopr == InvalidOid)
37303732
elog(ERROR, "no < operator for opclass %u", opclass);
3731-
cmpargs = list_make2(vardata->var, greaterstrcon);
3733+
cmpargs = list_make2(variable, greaterstrcon);
37323734
/* Assume scalarltsel is appropriate for all supported types */
37333735
topsel = DatumGetFloat8(DirectFunctionCall4(scalarltsel,
37343736
PointerGetDatum(root),
@@ -3743,7 +3745,7 @@ prefix_selectivity(Query *root, VariableStatData *vardata,
37433745
prefixsel = topsel + prefixsel - 1.0;
37443746

37453747
/* Adjust for double-exclusion of NULLs */
3746-
prefixsel += nulltestsel(root, IS_NULL, vardata->var, 0);
3748+
prefixsel += nulltestsel(root, IS_NULL, variable, 0);
37473749

37483750
/*
37493751
* A zero or slightly negative prefixsel should be converted into

0 commit comments

Comments
 (0)