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.
This commit is contained in:
Tom Lane 2003-07-29 17:21:27 +00:00
parent f353f8e83b
commit 5e3c09a114
3 changed files with 21 additions and 29 deletions

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.248 2003/07/21 01:59:08 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.249 2003/07/29 17:21:20 tgl Exp $
*
*
* INTERFACE ROUTINES
@ -1724,9 +1724,9 @@ SetRelationNumChecks(Relation rel, int numchecks)
* in the expression. (Even though we plan to reject vars, it's more
* user-friendly to give the correct error message than "unknown var".)
*
* If atttypid is not InvalidOid, check that the expression is coercible
* to the specified type. atttypmod is needed in this case, and attname
* is used in the error message if any.
* If atttypid is not InvalidOid, coerce the expression to the specified
* type (and typmod atttypmod). attname is only needed in this case:
* it is used in the error message, if any.
*/
Node *
cookDefault(ParseState *pstate,
@ -1773,24 +1773,19 @@ cookDefault(ParseState *pstate,
errmsg("cannot use aggregate in DEFAULT clause")));
/*
* Check that it will be possible to coerce the expression to the
* column's type. We store the expression without coercion, however,
* to avoid premature coercion in cases like
*
* CREATE TABLE tbl (fld timestamp DEFAULT 'now');
*
* NB: this should match the code in rewrite/rewriteHandler.c that will
* actually do the coercion, to ensure we don't accept an unusable
* default expression.
* Coerce the expression to the correct type and typmod, if given. This
* should match the parser's processing of non-defaulted expressions ---
* see updateTargetListEntry().
*/
if (OidIsValid(atttypid))
{
Oid type_id = exprType(expr);
if (coerce_to_target_type(pstate, expr, type_id,
atttypid, atttypmod,
COERCION_ASSIGNMENT,
COERCE_IMPLICIT_CAST) == NULL)
expr = coerce_to_target_type(pstate, expr, type_id,
atttypid, atttypmod,
COERCION_ASSIGNMENT,
COERCE_IMPLICIT_CAST);
if (expr == NULL)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("column \"%s\" is of type %s"
@ -1801,7 +1796,7 @@ cookDefault(ParseState *pstate,
errhint("You will need to rewrite or cast the expression.")));
}
return (expr);
return expr;
}

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.124 2003/07/25 00:01:08 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.125 2003/07/29 17:21:24 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -538,10 +538,11 @@ build_column_default(Relation rel, int attrno)
return NULL; /* No default anywhere */
/*
* Make sure the value is coerced to the target column type (might not
* be right type yet if it's not a constant!) This should match the
* parser's processing of non-defaulted expressions --- see
* updateTargetListEntry().
* Make sure the value is coerced to the target column type; this will
* generally be true already, but there seem to be some corner cases
* involving domain defaults where it might not be true.
* This should match the parser's processing of non-defaulted expressions
* --- see updateTargetListEntry().
*/
exprtype = exprType(expr);
@ -550,10 +551,6 @@ build_column_default(Relation rel, int attrno)
atttype, atttypmod,
COERCION_ASSIGNMENT,
COERCE_IMPLICIT_CAST);
/*
* This really shouldn't fail; should have checked the default's
* type when it was created ...
*/
if (expr == NULL)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),

View File

@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: catversion.h,v 1.202 2003/06/29 00:33:44 tgl Exp $
* $Id: catversion.h,v 1.203 2003/07/29 17:21:27 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -53,6 +53,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 200306281
#define CATALOG_VERSION_NO 200307291
#endif