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

Commitc05268e

Browse files
committed
Handle default NULL insertion a little better.
If a column is omitted in an INSERT, and there's no column default,the code in preptlist.c generates a NULL Const to be inserted.Furthermore, if the column is of a domain type, we wrap the Constin CoerceToDomain, so as to throw a run-time error if the domainhas a NOT NULL constraint. That's fine as far as it goes, butthere are two problems:1. We're being sloppy about the type/typmod that the Const islabeled with. It really should have the domain's base type/typmod,since it's the input to CoerceToDomain not the output. This canresult in coerce_to_domain inserting a useless length-coercionfunction (useless because it's being applied to a null). Thecoercion would typically get const-folded away later, but it'dbe better not to create it in the first place.2. We're not applying expression preprocessing (specifically,eval_const_expressions) to the resulting expression tree.The planner's primary expression-preprocessing pass already happened,so that means the length coercion step and CoerceToDomain node misspreprocessing altogether.This is at the least inefficient, since it means the length coercionand CoerceToDomain will actually be executed for each inserted row,though they could be const-folded away in most cases. Worse, itseems possible that missing preprocessing for the length coercioncould result in an invalid plan (for example, due to failing toperform default-function-argument insertion). I'm not aware ofany live bug of that sort with core datatypes, and it might beunreachable for extension types as well because of restrictions ofCREATE CAST, but I'm not entirely convinced that it's unreachable.Hence, it seems worth back-patching the fix (although I only wentback to v14, as the patch doesn't apply cleanly at all in v13).There are several places in the rewriter that are building nulldomain constants the same way as preptlist.c. While those arebefore the planner and hence don't have any reachable bug, they'restill applying a length coercion that will be const-folded awaylater, uselessly wasting cycles. Hence, make a utility routinethat all of these places can call to do it right.Making this code more careful about the typmod assigned to thegenerated NULL constant has visible but cosmetic effects on someof the plans shown in contrib/postgres_fdw's regression tests.Discussion:https://postgr.es/m/1865579.1738113656@sss.pgh.pa.usBackpatch-through: 14
1 parent54f9afe commitc05268e

File tree

6 files changed

+90
-77
lines changed

6 files changed

+90
-77
lines changed

‎contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4127,13 +4127,13 @@ EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st6;
41274127

41284128
PREPARE st7 AS INSERT INTO ft1 (c1,c2,c3) VALUES (1001,101,'foo');
41294129
EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st7;
4130-
QUERY PLAN
4131-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4130+
QUERY PLAN
4131+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
41324132
Insert on public.ft1
41334133
Remote SQL: INSERT INTO "S 1"."T 1"("C 1", c2, c3, c4, c5, c6, c7, c8) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
41344134
Batch Size: 1
41354135
-> Result
4136-
Output: NULL::integer, 1001, 101, 'foo'::text, NULL::timestamp with time zone, NULL::timestamp without time zone, NULL::character varying, 'ft1 '::character(10), NULL::user_enum
4136+
Output: NULL::integer, 1001, 101, 'foo'::text, NULL::timestamp with time zone, NULL::timestamp without time zone, NULL::character varying(10), 'ft1 '::character(10), NULL::user_enum
41374137
(5 rows)
41384138

41394139
ALTER TABLE "S 1"."T 1" RENAME TO "T 0";
@@ -4161,13 +4161,13 @@ EXECUTE st6;
41614161
(9 rows)
41624162

41634163
EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st7;
4164-
QUERY PLAN
4165-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4164+
QUERY PLAN
4165+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
41664166
Insert on public.ft1
41674167
Remote SQL: INSERT INTO "S 1"."T 0"("C 1", c2, c3, c4, c5, c6, c7, c8) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
41684168
Batch Size: 1
41694169
-> Result
4170-
Output: NULL::integer, 1001, 101, 'foo'::text, NULL::timestamp with time zone, NULL::timestamp without time zone, NULL::character varying, 'ft1 '::character(10), NULL::user_enum
4170+
Output: NULL::integer, 1001, 101, 'foo'::text, NULL::timestamp with time zone, NULL::timestamp without time zone, NULL::character varying(10), 'ft1 '::character(10), NULL::user_enum
41714171
(5 rows)
41724172

41734173
ALTER TABLE "S 1"."T 0" RENAME TO "T 1";
@@ -4491,13 +4491,13 @@ explain (verbose, costs off) select * from ft3 f, loct3 l
44914491
-- ===================================================================
44924492
EXPLAIN (verbose, costs off)
44934493
INSERT INTO ft2 (c1,c2,c3) SELECT c1+1000,c2+100, c3 || c3 FROM ft2 LIMIT 20;
4494-
QUERY PLAN
4495-
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4494+
QUERY PLAN
4495+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
44964496
Insert on public.ft2
44974497
Remote SQL: INSERT INTO "S 1"."T 1"("C 1", c2, c3, c4, c5, c6, c7, c8) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
44984498
Batch Size: 1
44994499
-> Subquery Scan on "*SELECT*"
4500-
Output: "*SELECT*"."?column?", "*SELECT*"."?column?_1", NULL::integer, "*SELECT*"."?column?_2", NULL::timestamp with time zone, NULL::timestamp without time zone, NULL::character varying, 'ft2 '::character(10), NULL::user_enum
4500+
Output: "*SELECT*"."?column?", "*SELECT*"."?column?_1", NULL::integer, "*SELECT*"."?column?_2", NULL::timestamp with time zone, NULL::timestamp without time zone, NULL::character varying(10), 'ft2 '::character(10), NULL::user_enum
45014501
-> Foreign Scan on public.ft2 ft2_1
45024502
Output: (ft2_1.c1 + 1000), (ft2_1.c2 + 100), (ft2_1.c3 || ft2_1.c3)
45034503
Remote SQL: SELECT "C 1", c2, c3 FROM "S 1"."T 1" LIMIT 20::bigint
@@ -5607,14 +5607,14 @@ SELECT c1,c2,c3,c4 FROM ft2 ORDER BY c1;
56075607

56085608
EXPLAIN (verbose, costs off)
56095609
INSERT INTO ft2 (c1,c2,c3) VALUES (1200,999,'foo') RETURNING tableoid::regclass;
5610-
QUERY PLAN
5611-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5610+
QUERY PLAN
5611+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
56125612
Insert on public.ft2
56135613
Output: (ft2.tableoid)::regclass
56145614
Remote SQL: INSERT INTO "S 1"."T 1"("C 1", c2, c3, c4, c5, c6, c7, c8) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
56155615
Batch Size: 1
56165616
-> Result
5617-
Output: 1200, 999, NULL::integer, 'foo'::text, NULL::timestamp with time zone, NULL::timestamp without time zone, NULL::character varying, 'ft2 '::character(10), NULL::user_enum
5617+
Output: 1200, 999, NULL::integer, 'foo'::text, NULL::timestamp with time zone, NULL::timestamp without time zone, NULL::character varying(10), 'ft2 '::character(10), NULL::user_enum
56185618
(6 rows)
56195619

56205620
INSERT INTO ft2 (c1,c2,c3) VALUES (1200,999,'foo') RETURNING tableoid::regclass;

‎src/backend/optimizer/prep/preptlist.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
#include"parser/parsetree.h"
4747
#include"utils/rel.h"
4848

49-
staticList*expand_insert_targetlist(List*tlist,Relationrel);
49+
staticList*expand_insert_targetlist(PlannerInfo*root,List*tlist,
50+
Relationrel);
5051

5152

5253
/*
@@ -102,7 +103,7 @@ preprocess_targetlist(PlannerInfo *root)
102103
*/
103104
tlist=parse->targetList;
104105
if (command_type==CMD_INSERT)
105-
tlist=expand_insert_targetlist(tlist,target_relation);
106+
tlist=expand_insert_targetlist(root,tlist,target_relation);
106107
elseif (command_type==CMD_UPDATE)
107108
root->update_colnos=extract_update_targetlist_colnos(tlist);
108109

@@ -292,7 +293,7 @@ extract_update_targetlist_colnos(List *tlist)
292293
* but now this code is only applied to INSERT targetlists.
293294
*/
294295
staticList*
295-
expand_insert_targetlist(List*tlist,Relationrel)
296+
expand_insert_targetlist(PlannerInfo*root,List*tlist,Relationrel)
296297
{
297298
List*new_tlist=NIL;
298299
ListCell*tlist_item;
@@ -346,26 +347,18 @@ expand_insert_targetlist(List *tlist, Relation rel)
346347
* confuse code comparing the finished plan to the target
347348
* relation, however.
348349
*/
349-
Oidatttype=att_tup->atttypid;
350-
Oidattcollation=att_tup->attcollation;
351350
Node*new_expr;
352351

353352
if (!att_tup->attisdropped)
354353
{
355-
new_expr= (Node*)makeConst(atttype,
356-
-1,
357-
attcollation,
358-
att_tup->attlen,
359-
(Datum)0,
360-
true,/* isnull */
361-
att_tup->attbyval);
362-
new_expr=coerce_to_domain(new_expr,
363-
InvalidOid,-1,
364-
atttype,
365-
COERCION_IMPLICIT,
366-
COERCE_IMPLICIT_CAST,
367-
-1,
368-
false);
354+
new_expr=coerce_null_to_domain(att_tup->atttypid,
355+
att_tup->atttypmod,
356+
att_tup->attcollation,
357+
att_tup->attlen,
358+
att_tup->attbyval);
359+
/* Must run expression preprocessing on any non-const nodes */
360+
if (!IsA(new_expr,Const))
361+
new_expr=eval_const_expressions(root,new_expr);
369362
}
370363
else
371364
{

‎src/backend/parser/parse_coerce.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,43 @@ coerce_to_specific_type(ParseState *pstate, Node *node,
12631263
constructName);
12641264
}
12651265

1266+
/*
1267+
* coerce_null_to_domain()
1268+
*Build a NULL constant, then wrap it in CoerceToDomain
1269+
*if the desired type is a domain type. This allows any
1270+
*NOT NULL domain constraint to be enforced at runtime.
1271+
*/
1272+
Node*
1273+
coerce_null_to_domain(Oidtypid,int32typmod,Oidcollation,
1274+
inttyplen,booltypbyval)
1275+
{
1276+
Node*result;
1277+
OidbaseTypeId;
1278+
int32baseTypeMod=typmod;
1279+
1280+
/*
1281+
* The constant must appear to have the domain's base type/typmod, else
1282+
* coerce_to_domain() will apply a length coercion which is useless.
1283+
*/
1284+
baseTypeId=getBaseTypeAndTypmod(typid,&baseTypeMod);
1285+
result= (Node*)makeConst(baseTypeId,
1286+
baseTypeMod,
1287+
collation,
1288+
typlen,
1289+
(Datum)0,
1290+
true,/* isnull */
1291+
typbyval);
1292+
if (typid!=baseTypeId)
1293+
result=coerce_to_domain(result,
1294+
baseTypeId,baseTypeMod,
1295+
typid,
1296+
COERCION_IMPLICIT,
1297+
COERCE_IMPLICIT_CAST,
1298+
-1,
1299+
false);
1300+
returnresult;
1301+
}
1302+
12661303
/*
12671304
* parser_coercion_errposition - report coercion error location, if possible
12681305
*

‎src/backend/rewrite/rewriteHandler.c

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -997,23 +997,11 @@ rewriteTargetListIU(List *targetList,
997997
if (commandType==CMD_INSERT)
998998
new_tle=NULL;
999999
else
1000-
{
1001-
new_expr= (Node*)makeConst(att_tup->atttypid,
1002-
-1,
1003-
att_tup->attcollation,
1004-
att_tup->attlen,
1005-
(Datum)0,
1006-
true,/* isnull */
1007-
att_tup->attbyval);
1008-
/* this is to catch a NOT NULL domain constraint */
1009-
new_expr=coerce_to_domain(new_expr,
1010-
InvalidOid,-1,
1011-
att_tup->atttypid,
1012-
COERCION_IMPLICIT,
1013-
COERCE_IMPLICIT_CAST,
1014-
-1,
1015-
false);
1016-
}
1000+
new_expr=coerce_null_to_domain(att_tup->atttypid,
1001+
att_tup->atttypmod,
1002+
att_tup->attcollation,
1003+
att_tup->attlen,
1004+
att_tup->attbyval);
10171005
}
10181006

10191007
if (new_expr)
@@ -1575,21 +1563,11 @@ rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti,
15751563
continue;
15761564
}
15771565

1578-
new_expr= (Node*)makeConst(att_tup->atttypid,
1579-
-1,
1580-
att_tup->attcollation,
1581-
att_tup->attlen,
1582-
(Datum)0,
1583-
true,/* isnull */
1584-
att_tup->attbyval);
1585-
/* this is to catch a NOT NULL domain constraint */
1586-
new_expr=coerce_to_domain(new_expr,
1587-
InvalidOid,-1,
1588-
att_tup->atttypid,
1589-
COERCION_IMPLICIT,
1590-
COERCE_IMPLICIT_CAST,
1591-
-1,
1592-
false);
1566+
new_expr=coerce_null_to_domain(att_tup->atttypid,
1567+
att_tup->atttypmod,
1568+
att_tup->attcollation,
1569+
att_tup->attlen,
1570+
att_tup->attbyval);
15931571
}
15941572
newList=lappend(newList,new_expr);
15951573
}

‎src/backend/rewrite/rewriteManip.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include"parser/parse_relation.h"
2323
#include"parser/parsetree.h"
2424
#include"rewrite/rewriteManip.h"
25+
#include"utils/lsyscache.h"
2526

2627

2728
typedefstruct
@@ -1466,20 +1467,21 @@ ReplaceVarsFromTargetList_callback(Var *var,
14661467
return (Node*)var;
14671468

14681469
caseREPLACEVARS_SUBSTITUTE_NULL:
1469-
1470-
/*
1471-
* If Var is of domain type, we should add a CoerceToDomain
1472-
* node, in case there is a NOT NULL domain constraint.
1473-
*/
1474-
returncoerce_to_domain((Node*)makeNullConst(var->vartype,
1475-
var->vartypmod,
1476-
var->varcollid),
1477-
InvalidOid,-1,
1478-
var->vartype,
1479-
COERCION_IMPLICIT,
1480-
COERCE_IMPLICIT_CAST,
1481-
-1,
1482-
false);
1470+
{
1471+
/*
1472+
* If Var is of domain type, we must add a CoerceToDomain
1473+
* node, in case there is a NOT NULL domain constraint.
1474+
*/
1475+
int16vartyplen;
1476+
boolvartypbyval;
1477+
1478+
get_typlenbyval(var->vartype,&vartyplen,&vartypbyval);
1479+
returncoerce_null_to_domain(var->vartype,
1480+
var->vartypmod,
1481+
var->varcollid,
1482+
vartyplen,
1483+
vartypbyval);
1484+
}
14831485
}
14841486
elog(ERROR,"could not find replacement targetlist entry for attno %d",
14851487
var->varattno);

‎src/include/parser/parse_coerce.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ extern Node *coerce_to_specific_type_typmod(ParseState *pstate, Node *node,
6161
OidtargetTypeId,int32targetTypmod,
6262
constchar*constructName);
6363

64+
externNode*coerce_null_to_domain(Oidtypid,int32typmod,Oidcollation,
65+
inttyplen,booltypbyval);
66+
6467
externintparser_coercion_errposition(ParseState*pstate,
6568
intcoerce_location,
6669
Node*input_expr);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp