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 * 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 * INTERFACE ROUTINES
@ -1724,9 +1724,9 @@ SetRelationNumChecks(Relation rel, int numchecks)
* in the expression. (Even though we plan to reject vars, it's more * in the expression. (Even though we plan to reject vars, it's more
* user-friendly to give the correct error message than "unknown var".) * user-friendly to give the correct error message than "unknown var".)
* *
* If atttypid is not InvalidOid, check that the expression is coercible * If atttypid is not InvalidOid, coerce the expression to the specified
* to the specified type. atttypmod is needed in this case, and attname * type (and typmod atttypmod). attname is only needed in this case:
* is used in the error message if any. * it is used in the error message, if any.
*/ */
Node * Node *
cookDefault(ParseState *pstate, cookDefault(ParseState *pstate,
@ -1773,24 +1773,19 @@ cookDefault(ParseState *pstate,
errmsg("cannot use aggregate in DEFAULT clause"))); errmsg("cannot use aggregate in DEFAULT clause")));
/* /*
* Check that it will be possible to coerce the expression to the * Coerce the expression to the correct type and typmod, if given. This
* column's type. We store the expression without coercion, however, * should match the parser's processing of non-defaulted expressions ---
* to avoid premature coercion in cases like * see updateTargetListEntry().
*
* 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.
*/ */
if (OidIsValid(atttypid)) if (OidIsValid(atttypid))
{ {
Oid type_id = exprType(expr); Oid type_id = exprType(expr);
if (coerce_to_target_type(pstate, expr, type_id, expr = coerce_to_target_type(pstate, expr, type_id,
atttypid, atttypmod, atttypid, atttypmod,
COERCION_ASSIGNMENT, COERCION_ASSIGNMENT,
COERCE_IMPLICIT_CAST) == NULL) COERCE_IMPLICIT_CAST);
if (expr == NULL)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH), (errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("column \"%s\" is of type %s" errmsg("column \"%s\" is of type %s"
@ -1801,7 +1796,7 @@ cookDefault(ParseState *pstate,
errhint("You will need to rewrite or cast the expression."))); 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 * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * 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 */ return NULL; /* No default anywhere */
/* /*
* Make sure the value is coerced to the target column type (might not * Make sure the value is coerced to the target column type; this will
* be right type yet if it's not a constant!) This should match the * generally be true already, but there seem to be some corner cases
* parser's processing of non-defaulted expressions --- see * involving domain defaults where it might not be true.
* updateTargetListEntry(). * This should match the parser's processing of non-defaulted expressions
* --- see updateTargetListEntry().
*/ */
exprtype = exprType(expr); exprtype = exprType(expr);
@ -550,10 +551,6 @@ build_column_default(Relation rel, int attrno)
atttype, atttypmod, atttype, atttypmod,
COERCION_ASSIGNMENT, COERCION_ASSIGNMENT,
COERCE_IMPLICIT_CAST); COERCE_IMPLICIT_CAST);
/*
* This really shouldn't fail; should have checked the default's
* type when it was created ...
*/
if (expr == NULL) if (expr == NULL)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH), (errcode(ERRCODE_DATATYPE_MISMATCH),

View File

@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * 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 */ /* yyyymmddN */
#define CATALOG_VERSION_NO 200306281 #define CATALOG_VERSION_NO 200307291
#endif #endif