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

Commit 5e3c09a

Browse files
committed
Coerce unknown-literal-constant default values to the column type during
CREATE TABLE (or ALTER TABLE SET DEFAULT), rather than postponing it to the time that the default is inserted into an INSERT command by the rewriter. This reverses an old decision that was intended to make the world safe for writing f1 timestamp default 'now' but in fact merely made the failure modes subtle rather than obvious. Per recent trouble report and followup discussion. initdb forced since there is a chance that stored default expressions will change.
1 parent f353f8e commit 5e3c09a

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

src/backend/catalog/heap.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.248 2003/07/21 01:59:08 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.249 2003/07/29 17:21:20 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -1724,9 +1724,9 @@ SetRelationNumChecks(Relation rel, int numchecks)
17241724
* in the expression. (Even though we plan to reject vars, it's more
17251725
* user-friendly to give the correct error message than "unknown var".)
17261726
*
1727-
* If atttypid is not InvalidOid, check that the expression is coercible
1728-
* to the specified type. atttypmod is needed in this case, and attname
1729-
* is used in the error message if any.
1727+
* If atttypid is not InvalidOid, coerce the expression to the specified
1728+
* type (and typmod atttypmod). attname is only needed in this case:
1729+
* it is used in the error message, if any.
17301730
*/
17311731
Node *
17321732
cookDefault(ParseState *pstate,
@@ -1773,24 +1773,19 @@ cookDefault(ParseState *pstate,
17731773
errmsg("cannot use aggregate in DEFAULT clause")));
17741774

17751775
/*
1776-
* Check that it will be possible to coerce the expression to the
1777-
* column's type. We store the expression without coercion, however,
1778-
* to avoid premature coercion in cases like
1779-
*
1780-
* CREATE TABLE tbl (fld timestamp DEFAULT 'now');
1781-
*
1782-
* NB: this should match the code in rewrite/rewriteHandler.c that will
1783-
* actually do the coercion, to ensure we don't accept an unusable
1784-
* default expression.
1776+
* Coerce the expression to the correct type and typmod, if given. This
1777+
* should match the parser's processing of non-defaulted expressions ---
1778+
* see updateTargetListEntry().
17851779
*/
17861780
if (OidIsValid(atttypid))
17871781
{
17881782
Oid type_id = exprType(expr);
17891783

1790-
if (coerce_to_target_type(pstate, expr, type_id,
1791-
atttypid, atttypmod,
1792-
COERCION_ASSIGNMENT,
1793-
COERCE_IMPLICIT_CAST) == NULL)
1784+
expr = coerce_to_target_type(pstate, expr, type_id,
1785+
atttypid, atttypmod,
1786+
COERCION_ASSIGNMENT,
1787+
COERCE_IMPLICIT_CAST);
1788+
if (expr == NULL)
17941789
ereport(ERROR,
17951790
(errcode(ERRCODE_DATATYPE_MISMATCH),
17961791
errmsg("column \"%s\" is of type %s"
@@ -1801,7 +1796,7 @@ cookDefault(ParseState *pstate,
18011796
errhint("You will need to rewrite or cast the expression.")));
18021797
}
18031798

1804-
return (expr);
1799+
return expr;
18051800
}
18061801

18071802

src/backend/rewrite/rewriteHandler.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.124 2003/07/25 00:01:08 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.125 2003/07/29 17:21:24 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -538,10 +538,11 @@ build_column_default(Relation rel, int attrno)
538538
return NULL; /* No default anywhere */
539539

540540
/*
541-
* Make sure the value is coerced to the target column type (might not
542-
* be right type yet if it's not a constant!) This should match the
543-
* parser's processing of non-defaulted expressions --- see
544-
* updateTargetListEntry().
541+
* Make sure the value is coerced to the target column type; this will
542+
* generally be true already, but there seem to be some corner cases
543+
* involving domain defaults where it might not be true.
544+
* This should match the parser's processing of non-defaulted expressions
545+
* --- see updateTargetListEntry().
545546
*/
546547
exprtype = exprType(expr);
547548

@@ -550,10 +551,6 @@ build_column_default(Relation rel, int attrno)
550551
atttype, atttypmod,
551552
COERCION_ASSIGNMENT,
552553
COERCE_IMPLICIT_CAST);
553-
/*
554-
* This really shouldn't fail; should have checked the default's
555-
* type when it was created ...
556-
*/
557554
if (expr == NULL)
558555
ereport(ERROR,
559556
(errcode(ERRCODE_DATATYPE_MISMATCH),

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $Id: catversion.h,v 1.202 2003/06/29 00:33:44 tgl Exp $
40+
* $Id: catversion.h,v 1.203 2003/07/29 17:21:27 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200306281
56+
#define CATALOG_VERSION_NO 200307291
5757

5858
#endif

0 commit comments

Comments
 (0)