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

Commit 0da39aa

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 6cddecd commit 0da39aa

File tree

6 files changed

+92
-78
lines changed

6 files changed

+92
-78
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

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

42744274
PREPARE st7 AS INSERT INTO ft1 (c1,c2,c3) VALUES (1001,101,'foo');
42754275
EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st7;
4276-
QUERY PLAN
4277-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4276+
QUERY PLAN
4277+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
42784278
Insert on public.ft1
42794279
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)
42804280
Batch Size: 1
42814281
-> Result
4282-
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
4282+
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
42834283
(5 rows)
42844284

42854285
ALTER TABLE "S 1"."T 1" RENAME TO "T 0";
@@ -4307,13 +4307,13 @@ EXECUTE st6;
43074307
(9 rows)
43084308

43094309
EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st7;
4310-
QUERY PLAN
4311-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4310+
QUERY PLAN
4311+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
43124312
Insert on public.ft1
43134313
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)
43144314
Batch Size: 1
43154315
-> Result
4316-
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
4316+
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
43174317
(5 rows)
43184318

43194319
ALTER TABLE "S 1"."T 0" RENAME TO "T 1";
@@ -4961,13 +4961,13 @@ SELECT ft1.c1 FROM ft1 JOIN ft2 on ft1.c1 = ft2.c1 WHERE
49614961
-- ===================================================================
49624962
EXPLAIN (verbose, costs off)
49634963
INSERT INTO ft2 (c1,c2,c3) SELECT c1+1000,c2+100, c3 || c3 FROM ft2 LIMIT 20;
4964-
QUERY PLAN
4965-
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4964+
QUERY PLAN
4965+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
49664966
Insert on public.ft2
49674967
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)
49684968
Batch Size: 1
49694969
-> Subquery Scan on "*SELECT*"
4970-
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
4970+
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
49714971
-> Foreign Scan on public.ft2 ft2_1
49724972
Output: (ft2_1.c1 + 1000), (ft2_1.c2 + 100), (ft2_1.c3 || ft2_1.c3)
49734973
Remote SQL: SELECT "C 1", c2, c3 FROM "S 1"."T 1" LIMIT 20::bigint
@@ -6125,14 +6125,14 @@ SELECT c1,c2,c3,c4 FROM ft2 ORDER BY c1;
61256125

61266126
EXPLAIN (verbose, costs off)
61276127
INSERT INTO ft2 (c1,c2,c3) VALUES (1200,999,'foo') RETURNING tableoid::regclass;
6128-
QUERY PLAN
6129-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6128+
QUERY PLAN
6129+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
61306130
Insert on public.ft2
61316131
Output: (ft2.tableoid)::regclass
61326132
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)
61336133
Batch Size: 1
61346134
-> Result
6135-
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
6135+
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
61366136
(6 rows)
61376137

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

src/backend/optimizer/prep/preptlist.c

Lines changed: 14 additions & 20 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-
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

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(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

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,23 +1008,11 @@ rewriteTargetListIU(List *targetList,
10081008
if (commandType == CMD_INSERT)
10091009
new_tle = NULL;
10101010
else
1011-
{
1012-
new_expr = (Node *) makeConst(att_tup->atttypid,
1013-
-1,
1014-
att_tup->attcollation,
1015-
att_tup->attlen,
1016-
(Datum) 0,
1017-
true, /* isnull */
1018-
att_tup->attbyval);
1019-
/* this is to catch a NOT NULL domain constraint */
1020-
new_expr = coerce_to_domain(new_expr,
1021-
InvalidOid, -1,
1022-
att_tup->atttypid,
1023-
COERCION_IMPLICIT,
1024-
COERCE_IMPLICIT_CAST,
1025-
-1,
1026-
false);
1027-
}
1011+
new_expr = coerce_null_to_domain(att_tup->atttypid,
1012+
att_tup->atttypmod,
1013+
att_tup->attcollation,
1014+
att_tup->attlen,
1015+
att_tup->attbyval);
10281016
}
10291017

10301018
if (new_expr)
@@ -1572,21 +1560,11 @@ rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti,
15721560
continue;
15731561
}
15741562

1575-
new_expr = (Node *) makeConst(att_tup->atttypid,
1576-
-1,
1577-
att_tup->attcollation,
1578-
att_tup->attlen,
1579-
(Datum) 0,
1580-
true, /* isnull */
1581-
att_tup->attbyval);
1582-
/* this is to catch a NOT NULL domain constraint */
1583-
new_expr = coerce_to_domain(new_expr,
1584-
InvalidOid, -1,
1585-
att_tup->atttypid,
1586-
COERCION_IMPLICIT,
1587-
COERCE_IMPLICIT_CAST,
1588-
-1,
1589-
false);
1563+
new_expr = coerce_null_to_domain(att_tup->atttypid,
1564+
att_tup->atttypmod,
1565+
att_tup->attcollation,
1566+
att_tup->attlen,
1567+
att_tup->attbyval);
15901568
}
15911569
newList = lappend(newList, new_expr);
15921570
}

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
typedef struct
@@ -1802,20 +1803,21 @@ ReplaceVarsFromTargetList_callback(Var *var,
18021803
return (Node *) var;
18031804

18041805
case REPLACEVARS_SUBSTITUTE_NULL:
1805-
1806-
/*
1807-
* If Var is of domain type, we should add a CoerceToDomain
1808-
* node, in case there is a NOT NULL domain constraint.
1809-
*/
1810-
return coerce_to_domain((Node *) makeNullConst(var->vartype,
1811-
var->vartypmod,
1812-
var->varcollid),
1813-
InvalidOid, -1,
1814-
var->vartype,
1815-
COERCION_IMPLICIT,
1816-
COERCE_IMPLICIT_CAST,
1817-
-1,
1818-
false);
1806+
{
1807+
/*
1808+
* If Var is of domain type, we must add a CoerceToDomain
1809+
* node, in case there is a NOT NULL domain constraint.
1810+
*/
1811+
int16 vartyplen;
1812+
bool vartypbyval;
1813+
1814+
get_typlenbyval(var->vartype, &vartyplen, &vartypbyval);
1815+
return coerce_null_to_domain(var->vartype,
1816+
var->vartypmod,
1817+
var->varcollid,
1818+
vartyplen,
1819+
vartypbyval);
1820+
}
18191821
}
18201822
elog(ERROR, "could not find replacement targetlist entry for attno %d",
18211823
var->varattno);

src/include/parser/parse_coerce.h

Lines changed: 3 additions & 0 deletions
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)