Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bfa4440

Browse files
committed
Pass collation to makeConst() instead of looking it up internally.
In nearly all cases, the caller already knows the correct collation, and in a number of places, the value the caller has handy is more correct than the default for the type would be. (In particular, this patch makes it significantly less likely that eval_const_expressions will result in changing the exposed collation of an expression.) So an internal lookup is both expensive and wrong.
1 parent c8e9935 commit bfa4440

File tree

18 files changed

+165
-56
lines changed

18 files changed

+165
-56
lines changed

src/backend/commands/tablecmds.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -4337,10 +4337,12 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
43374337
{
43384338
Oid baseTypeId;
43394339
int32 baseTypeMod;
4340+
Oid baseTypeColl;
43404341

43414342
baseTypeMod = typmod;
43424343
baseTypeId = getBaseTypeAndTypmod(typeOid, &baseTypeMod);
4343-
defval = (Expr *) makeNullConst(baseTypeId, baseTypeMod);
4344+
baseTypeColl = get_typcollation(baseTypeId);
4345+
defval = (Expr *) makeNullConst(baseTypeId, baseTypeMod, baseTypeColl);
43444346
defval = (Expr *) coerce_to_target_type(NULL,
43454347
(Node *) defval,
43464348
baseTypeId,

src/backend/executor/execQual.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4653,7 +4653,7 @@ ExecInitExpr(Expr *node, PlanState *parent)
46534653
* don't really care what type of NULL it is, so
46544654
* always make an int4 NULL.
46554655
*/
4656-
e = (Expr *) makeNullConst(INT4OID, -1);
4656+
e = (Expr *) makeNullConst(INT4OID, -1, InvalidOid);
46574657
}
46584658
estate = ExecInitExpr(e, parent);
46594659
outlist = lappend(outlist, estate);

src/backend/executor/functions.c

+2
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,7 @@ check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
15011501
/* The type of the null we insert isn't important */
15021502
null_expr = (Expr *) makeConst(INT4OID,
15031503
-1,
1504+
InvalidOid,
15041505
sizeof(int32),
15051506
(Datum) 0,
15061507
true, /* isnull */
@@ -1562,6 +1563,7 @@ check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
15621563
/* The type of the null we insert isn't important */
15631564
null_expr = (Expr *) makeConst(INT4OID,
15641565
-1,
1566+
InvalidOid,
15651567
sizeof(int32),
15661568
(Datum) 0,
15671569
true, /* isnull */

src/backend/nodes/makefuncs.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ makeFromExpr(List *fromlist, Node *quals)
271271
Const *
272272
makeConst(Oid consttype,
273273
int32 consttypmod,
274+
Oid constcollid,
274275
int constlen,
275276
Datum constvalue,
276277
bool constisnull,
@@ -280,7 +281,7 @@ makeConst(Oid consttype,
280281

281282
cnst->consttype = consttype;
282283
cnst->consttypmod = consttypmod;
283-
cnst->constcollid = get_typcollation(consttype);
284+
cnst->constcollid = constcollid;
284285
cnst->constlen = constlen;
285286
cnst->constvalue = constvalue;
286287
cnst->constisnull = constisnull;
@@ -298,14 +299,15 @@ makeConst(Oid consttype,
298299
* storage properties.
299300
*/
300301
Const *
301-
makeNullConst(Oid consttype, int32 consttypmod)
302+
makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
302303
{
303304
int16 typLen;
304305
bool typByVal;
305306

306307
get_typlenbyval(consttype, &typLen, &typByVal);
307308
return makeConst(consttype,
308309
consttypmod,
310+
constcollid,
309311
(int) typLen,
310312
(Datum) 0,
311313
true,
@@ -320,7 +322,7 @@ Node *
320322
makeBoolConst(bool value, bool isnull)
321323
{
322324
/* note that pg_type.h hardwires size of bool as 1 ... duplicate it */
323-
return (Node *) makeConst(BOOLOID, -1, 1,
325+
return (Node *) makeConst(BOOLOID, -1, InvalidOid, 1,
324326
BoolGetDatum(value), isnull, true);
325327
}
326328

src/backend/optimizer/path/indxpath.c

+39-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "access/skey.h"
2121
#include "catalog/pg_am.h"
22+
#include "catalog/pg_collation.h"
2223
#include "catalog/pg_operator.h"
2324
#include "catalog/pg_opfamily.h"
2425
#include "catalog/pg_type.h"
@@ -3233,7 +3234,9 @@ network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily, Datum rightop)
32333234

32343235
expr = make_opclause(opr1oid, BOOLOID, false,
32353236
(Expr *) leftop,
3236-
(Expr *) makeConst(datatype, -1, -1, opr1right,
3237+
(Expr *) makeConst(datatype, -1,
3238+
InvalidOid, /* not collatable */
3239+
-1, opr1right,
32373240
false, false),
32383241
InvalidOid, InvalidOid);
32393242
result = list_make1(make_simple_restrictinfo(expr));
@@ -3249,7 +3252,9 @@ network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily, Datum rightop)
32493252

32503253
expr = make_opclause(opr2oid, BOOLOID, false,
32513254
(Expr *) leftop,
3252-
(Expr *) makeConst(datatype, -1, -1, opr2right,
3255+
(Expr *) makeConst(datatype, -1,
3256+
InvalidOid, /* not collatable */
3257+
-1, opr2right,
32533258
false, false),
32543259
InvalidOid, InvalidOid);
32553260
result = lappend(result, make_simple_restrictinfo(expr));
@@ -3288,8 +3293,38 @@ static Const *
32883293
string_to_const(const char *str, Oid datatype)
32893294
{
32903295
Datum conval = string_to_datum(str, datatype);
3296+
Oid collation;
3297+
int constlen;
32913298

3292-
return makeConst(datatype, -1,
3293-
((datatype == NAMEOID) ? NAMEDATALEN : -1),
3299+
/*
3300+
* We only need to support a few datatypes here, so hard-wire properties
3301+
* instead of incurring the expense of catalog lookups.
3302+
*/
3303+
switch (datatype)
3304+
{
3305+
case TEXTOID:
3306+
case VARCHAROID:
3307+
case BPCHAROID:
3308+
collation = DEFAULT_COLLATION_OID;
3309+
constlen = -1;
3310+
break;
3311+
3312+
case NAMEOID:
3313+
collation = InvalidOid;
3314+
constlen = NAMEDATALEN;
3315+
break;
3316+
3317+
case BYTEAOID:
3318+
collation = InvalidOid;
3319+
constlen = -1;
3320+
break;
3321+
3322+
default:
3323+
elog(ERROR, "unexpected datatype in string_to_const: %u",
3324+
datatype);
3325+
return NULL;
3326+
}
3327+
3328+
return makeConst(datatype, -1, collation, constlen,
32943329
conval, false, false);
32953330
}

src/backend/optimizer/plan/planagg.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ build_minmax_path(PlannerInfo *root, MinMaxAggInfo *mminfo,
429429

430430
/* set up expressions for LIMIT 1 */
431431
parse->limitOffset = NULL;
432-
parse->limitCount = (Node *) makeConst(INT8OID, -1, sizeof(int64),
432+
parse->limitCount = (Node *) makeConst(INT8OID, -1, InvalidOid,
433+
sizeof(int64),
433434
Int64GetDatum(1), false,
434435
FLOAT8PASSBYVAL);
435436

src/backend/optimizer/prep/preptlist.c

+3
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ expand_targetlist(List *tlist, int command_type,
269269
{
270270
new_expr = (Node *) makeConst(atttype,
271271
-1,
272+
attcollation,
272273
att_tup->attlen,
273274
(Datum) 0,
274275
true, /* isnull */
@@ -286,6 +287,7 @@ expand_targetlist(List *tlist, int command_type,
286287
/* Insert NULL for dropped column */
287288
new_expr = (Node *) makeConst(INT4OID,
288289
-1,
290+
InvalidOid,
289291
sizeof(int32),
290292
(Datum) 0,
291293
true, /* isnull */
@@ -307,6 +309,7 @@ expand_targetlist(List *tlist, int command_type,
307309
/* Insert NULL for dropped column */
308310
new_expr = (Node *) makeConst(INT4OID,
309311
-1,
312+
InvalidOid,
310313
sizeof(int32),
311314
(Datum) 0,
312315
true, /* isnull */

src/backend/optimizer/prep/prepunion.c

+1
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,7 @@ generate_setop_tlist(List *colTypes, int flag,
910910
/* flag value is the given constant */
911911
expr = (Node *) makeConst(INT4OID,
912912
-1,
913+
InvalidOid,
913914
sizeof(int4),
914915
Int32GetDatum(flag),
915916
false,

0 commit comments

Comments
 (0)