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

Commit 6e41e9e

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 Const in CoerceToDomain, so as to throw a run-time error if the domain has a NOT NULL constraint. That's fine as far as it goes, but there are two problems: 1. We're being sloppy about the type/typmod that the Const is labeled with. It really should have the domain's base type/typmod, since it's the input to CoerceToDomain not the output. This can result in coerce_to_domain inserting a useless length-coercion function (useless because it's being applied to a null). The coercion would typically get const-folded away later, but it'd be 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 miss preprocessing altogether. This is at the least inefficient, since it means the length coercion and CoerceToDomain will actually be executed for each inserted row, though they could be const-folded away in most cases. Worse, it seems possible that missing preprocessing for the length coercion could result in an invalid plan (for example, due to failing to perform default-function-argument insertion). I'm not aware of any live bug of that sort with core datatypes, and it might be unreachable for extension types as well because of restrictions of CREATE CAST, but I'm not entirely convinced that it's unreachable. Hence, it seems worth back-patching the fix (although I only went back to v14, as the patch doesn't apply cleanly at all in v13). There are several places in the rewriter that are building null domain constants the same way as preptlist.c. While those are before the planner and hence don't have any reachable bug, they're still applying a length coercion that will be const-folded away later, uselessly wasting cycles. Hence, make a utility routine that all of these places can call to do it right. Making this code more careful about the typmod assigned to the generated NULL constant has visible but cosmetic effects on some of the plans shown in contrib/postgres_fdw's regression tests. Discussion: https://postgr.es/m/1865579.1738113656@sss.pgh.pa.us Backpatch-through: 14
1 parent 0b713b9 commit 6e41e9e

File tree

6 files changed

+92
-78
lines changed

6 files changed

+92
-78
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

+12-12
Original file line numberDiff line numberDiff line change
@@ -4266,13 +4266,13 @@ EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st6;
42664266

42674267
PREPARE st7 AS INSERT INTO ft1 (c1,c2,c3) VALUES (1001,101,'foo');
42684268
EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st7;
4269-
QUERY PLAN
4270-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4269+
QUERY PLAN
4270+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
42714271
Insert on public.ft1
42724272
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)
42734273
Batch Size: 1
42744274
-> Result
4275-
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
4275+
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
42764276
(5 rows)
42774277

42784278
ALTER TABLE "S 1"."T 1" RENAME TO "T 0";
@@ -4300,13 +4300,13 @@ EXECUTE st6;
43004300
(9 rows)
43014301

43024302
EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st7;
4303-
QUERY PLAN
4304-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4303+
QUERY PLAN
4304+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
43054305
Insert on public.ft1
43064306
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)
43074307
Batch Size: 1
43084308
-> Result
4309-
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
4309+
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
43104310
(5 rows)
43114311

43124312
ALTER TABLE "S 1"."T 0" RENAME TO "T 1";
@@ -4954,13 +4954,13 @@ SELECT ft1.c1 FROM ft1 JOIN ft2 on ft1.c1 = ft2.c1 WHERE
49544954
-- ===================================================================
49554955
EXPLAIN (verbose, costs off)
49564956
INSERT INTO ft2 (c1,c2,c3) SELECT c1+1000,c2+100, c3 || c3 FROM ft2 LIMIT 20;
4957-
QUERY PLAN
4958-
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4957+
QUERY PLAN
4958+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
49594959
Insert on public.ft2
49604960
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)
49614961
Batch Size: 1
49624962
-> Subquery Scan on "*SELECT*"
4963-
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
4963+
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
49644964
-> Foreign Scan on public.ft2 ft2_1
49654965
Output: (ft2_1.c1 + 1000), (ft2_1.c2 + 100), (ft2_1.c3 || ft2_1.c3)
49664966
Remote SQL: SELECT "C 1", c2, c3 FROM "S 1"."T 1" LIMIT 20::bigint
@@ -6070,14 +6070,14 @@ SELECT c1,c2,c3,c4 FROM ft2 ORDER BY c1;
60706070

60716071
EXPLAIN (verbose, costs off)
60726072
INSERT INTO ft2 (c1,c2,c3) VALUES (1200,999,'foo') RETURNING tableoid::regclass;
6073-
QUERY PLAN
6074-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6073+
QUERY PLAN
6074+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
60756075
Insert on public.ft2
60766076
Output: (ft2.tableoid)::regclass
60776077
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)
60786078
Batch Size: 1
60796079
-> Result
6080-
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
6080+
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
60816081
(6 rows)
60826082

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

src/backend/optimizer/prep/preptlist.c

+14-20
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
#include "parser/parsetree.h"
4747
#include "utils/rel.h"
4848

49-
static List *expand_insert_targetlist(List *tlist, Relation rel);
49+
static List *expand_insert_targetlist(PlannerInfo *root, List *tlist,
50+
Relation rel);
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
else if (command_type == CMD_UPDATE)
107108
root->update_colnos = extract_update_targetlist_colnos(tlist);
108109

@@ -148,7 +149,8 @@ preprocess_targetlist(PlannerInfo *root)
148149
ListCell *l2;
149150

150151
if (action->commandType == CMD_INSERT)
151-
action->targetList = expand_insert_targetlist(action->targetList,
152+
action->targetList = expand_insert_targetlist(root,
153+
action->targetList,
152154
target_relation);
153155
else if (action->commandType == CMD_UPDATE)
154156
action->updateColnos =
@@ -376,7 +378,7 @@ extract_update_targetlist_colnos(List *tlist)
376378
* but now this code is only applied to INSERT targetlists.
377379
*/
378380
static List *
379-
expand_insert_targetlist(List *tlist, Relation rel)
381+
expand_insert_targetlist(PlannerInfo *root, List *tlist, Relation rel)
380382
{
381383
List *new_tlist = NIL;
382384
ListCell *tlist_item;
@@ -430,26 +432,18 @@ expand_insert_targetlist(List *tlist, Relation rel)
430432
* confuse code comparing the finished plan to the target
431433
* relation, however.
432434
*/
433-
Oid atttype = att_tup->atttypid;
434-
Oid attcollation = att_tup->attcollation;
435435
Node *new_expr;
436436

437437
if (!att_tup->attisdropped)
438438
{
439-
new_expr = (Node *) makeConst(atttype,
440-
-1,
441-
attcollation,
442-
att_tup->attlen,
443-
(Datum) 0,
444-
true, /* isnull */
445-
att_tup->attbyval);
446-
new_expr = coerce_to_domain(new_expr,
447-
InvalidOid, -1,
448-
atttype,
449-
COERCION_IMPLICIT,
450-
COERCE_IMPLICIT_CAST,
451-
-1,
452-
false);
439+
new_expr = coerce_null_to_domain(att_tup->atttypid,
440+
att_tup->atttypmod,
441+
att_tup->attcollation,
442+
att_tup->attlen,
443+
att_tup->attbyval);
444+
/* Must run expression preprocessing on any non-const nodes */
445+
if (!IsA(new_expr, Const))
446+
new_expr = eval_const_expressions(root, new_expr);
453447
}
454448
else
455449
{

src/backend/parser/parse_coerce.c

+37
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(Oid typid, int32 typmod, Oid collation,
1274+
int typlen, bool typbyval)
1275+
{
1276+
Node *result;
1277+
Oid baseTypeId;
1278+
int32 baseTypeMod = 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+
return result;
1301+
}
1302+
12661303
/*
12671304
* parser_coercion_errposition - report coercion error location, if possible
12681305
*

src/backend/rewrite/rewriteHandler.c

+10-32
Original file line numberDiff line numberDiff line change
@@ -1002,23 +1002,11 @@ rewriteTargetListIU(List *targetList,
10021002
if (commandType == CMD_INSERT)
10031003
new_tle = NULL;
10041004
else
1005-
{
1006-
new_expr = (Node *) makeConst(att_tup->atttypid,
1007-
-1,
1008-
att_tup->attcollation,
1009-
att_tup->attlen,
1010-
(Datum) 0,
1011-
true, /* isnull */
1012-
att_tup->attbyval);
1013-
/* this is to catch a NOT NULL domain constraint */
1014-
new_expr = coerce_to_domain(new_expr,
1015-
InvalidOid, -1,
1016-
att_tup->atttypid,
1017-
COERCION_IMPLICIT,
1018-
COERCE_IMPLICIT_CAST,
1019-
-1,
1020-
false);
1021-
}
1005+
new_expr = coerce_null_to_domain(att_tup->atttypid,
1006+
att_tup->atttypmod,
1007+
att_tup->attcollation,
1008+
att_tup->attlen,
1009+
att_tup->attbyval);
10221010
}
10231011

10241012
if (new_expr)
@@ -1566,21 +1554,11 @@ rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti,
15661554
continue;
15671555
}
15681556

1569-
new_expr = (Node *) makeConst(att_tup->atttypid,
1570-
-1,
1571-
att_tup->attcollation,
1572-
att_tup->attlen,
1573-
(Datum) 0,
1574-
true, /* isnull */
1575-
att_tup->attbyval);
1576-
/* this is to catch a NOT NULL domain constraint */
1577-
new_expr = coerce_to_domain(new_expr,
1578-
InvalidOid, -1,
1579-
att_tup->atttypid,
1580-
COERCION_IMPLICIT,
1581-
COERCE_IMPLICIT_CAST,
1582-
-1,
1583-
false);
1557+
new_expr = coerce_null_to_domain(att_tup->atttypid,
1558+
att_tup->atttypmod,
1559+
att_tup->attcollation,
1560+
att_tup->attlen,
1561+
att_tup->attbyval);
15841562
}
15851563
newList = lappend(newList, new_expr);
15861564
}

src/backend/rewrite/rewriteManip.c

+16-14
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
typedef struct
@@ -1721,20 +1722,21 @@ ReplaceVarsFromTargetList_callback(Var *var,
17211722
return (Node *) var;
17221723

17231724
case REPLACEVARS_SUBSTITUTE_NULL:
1724-
1725-
/*
1726-
* If Var is of domain type, we should add a CoerceToDomain
1727-
* node, in case there is a NOT NULL domain constraint.
1728-
*/
1729-
return coerce_to_domain((Node *) makeNullConst(var->vartype,
1730-
var->vartypmod,
1731-
var->varcollid),
1732-
InvalidOid, -1,
1733-
var->vartype,
1734-
COERCION_IMPLICIT,
1735-
COERCE_IMPLICIT_CAST,
1736-
-1,
1737-
false);
1725+
{
1726+
/*
1727+
* If Var is of domain type, we must add a CoerceToDomain
1728+
* node, in case there is a NOT NULL domain constraint.
1729+
*/
1730+
int16 vartyplen;
1731+
bool vartypbyval;
1732+
1733+
get_typlenbyval(var->vartype, &vartyplen, &vartypbyval);
1734+
return coerce_null_to_domain(var->vartype,
1735+
var->vartypmod,
1736+
var->varcollid,
1737+
vartyplen,
1738+
vartypbyval);
1739+
}
17381740
}
17391741
elog(ERROR, "could not find replacement targetlist entry for attno %d",
17401742
var->varattno);

src/include/parser/parse_coerce.h

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ extern Node *coerce_to_specific_type_typmod(ParseState *pstate, Node *node,
6363
Oid targetTypeId, int32 targetTypmod,
6464
const char *constructName);
6565

66+
extern Node *coerce_null_to_domain(Oid typid, int32 typmod, Oid collation,
67+
int typlen, bool typbyval);
68+
6669
extern int parser_coercion_errposition(ParseState *pstate,
6770
int coerce_location,
6871
Node *input_expr);

0 commit comments

Comments
 (0)