88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.108 2007/09 /29 17:18:58 tgl Exp $
11+ * $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.109 2007/10 /29 19:40:39 tgl Exp $
1212 *
1313 * DESCRIPTION
1414 * The "DefineFoo" routines take the parse tree and pick out the
@@ -765,20 +765,40 @@ DefineDomain(CreateDomainStmt *stmt)
765765 domainName );
766766
767767 /*
768- * Expression must be stored as a nodeToString result, but
769- * we also require a valid textual representation (mainly
770- * to make life easier for pg_dump).
768+ * If the expression is just a NULL constant, we treat
769+ * it like not having a default.
770+ *
771+ * Note that if the basetype is another domain, we'll see
772+ * a CoerceToDomain expr here and not discard the default.
773+ * This is critical because the domain default needs to be
774+ * retained to override any default that the base domain
775+ * might have.
771776 */
772- defaultValue =
773- deparse_expression (defaultExpr ,
774- deparse_context_for (domainName ,
775- InvalidOid ),
776- false, false);
777- defaultValueBin = nodeToString (defaultExpr );
777+ if (defaultExpr == NULL ||
778+ (IsA (defaultExpr , Const ) &&
779+ ((Const * ) defaultExpr )-> constisnull ))
780+ {
781+ defaultValue = NULL ;
782+ defaultValueBin = NULL ;
783+ }
784+ else
785+ {
786+ /*
787+ * Expression must be stored as a nodeToString result,
788+ * but we also require a valid textual representation
789+ * (mainly to make life easier for pg_dump).
790+ */
791+ defaultValue =
792+ deparse_expression (defaultExpr ,
793+ deparse_context_for (domainName ,
794+ InvalidOid ),
795+ false, false);
796+ defaultValueBin = nodeToString (defaultExpr );
797+ }
778798 }
779799 else
780800 {
781- /* DEFAULT NULL is same as not having a default */
801+ /* No default (can this still happen?) */
782802 defaultValue = NULL ;
783803 defaultValueBin = NULL ;
784804 }
@@ -1443,7 +1463,7 @@ AlterDomainDefault(List *names, Node *defaultRaw)
14431463 MemSet (new_record_nulls , ' ' , sizeof (new_record_nulls ));
14441464 MemSet (new_record_repl , ' ' , sizeof (new_record_repl ));
14451465
1446- /* Store the new default, if null then skip this step */
1466+ /* Store the new default into the tuple */
14471467 if (defaultRaw )
14481468 {
14491469 /* Create a dummy ParseState for transformExpr */
@@ -1459,30 +1479,46 @@ AlterDomainDefault(List *names, Node *defaultRaw)
14591479 NameStr (typTup -> typname ));
14601480
14611481 /*
1462- * Expression must be stored as a nodeToString result, but we also
1463- * require a valid textual representation (mainly to make life easier
1464- * for pg_dump).
1482+ * If the expression is just a NULL constant, we treat the command
1483+ * like ALTER ... DROP DEFAULT. (But see note for same test in
1484+ * DefineDomain.)
14651485 */
1466- defaultValue = deparse_expression (defaultExpr ,
1486+ if (defaultExpr == NULL ||
1487+ (IsA (defaultExpr , Const ) && ((Const * ) defaultExpr )-> constisnull ))
1488+ {
1489+ /* Default is NULL, drop it */
1490+ new_record_nulls [Anum_pg_type_typdefaultbin - 1 ] = 'n' ;
1491+ new_record_repl [Anum_pg_type_typdefaultbin - 1 ] = 'r' ;
1492+ new_record_nulls [Anum_pg_type_typdefault - 1 ] = 'n' ;
1493+ new_record_repl [Anum_pg_type_typdefault - 1 ] = 'r' ;
1494+ }
1495+ else
1496+ {
1497+ /*
1498+ * Expression must be stored as a nodeToString result, but we also
1499+ * require a valid textual representation (mainly to make life
1500+ * easier for pg_dump).
1501+ */
1502+ defaultValue = deparse_expression (defaultExpr ,
14671503 deparse_context_for (NameStr (typTup -> typname ),
14681504 InvalidOid ),
14691505 false, false);
14701506
1471- /*
1472- * Form an updated tuple with the new default and write it back.
1473- */
1474- new_record [Anum_pg_type_typdefaultbin - 1 ] = DirectFunctionCall1 (textin ,
1475- CStringGetDatum (
1476- nodeToString (defaultExpr )));
1507+ /*
1508+ * Form an updated tuple with the new default and write it back.
1509+ */
1510+ new_record [Anum_pg_type_typdefaultbin - 1 ] = DirectFunctionCall1 (textin ,
1511+ CStringGetDatum (nodeToString (defaultExpr )));
14771512
1478- new_record_repl [Anum_pg_type_typdefaultbin - 1 ] = 'r' ;
1479- new_record [Anum_pg_type_typdefault - 1 ] = DirectFunctionCall1 (textin ,
1513+ new_record_repl [Anum_pg_type_typdefaultbin - 1 ] = 'r' ;
1514+ new_record [Anum_pg_type_typdefault - 1 ] = DirectFunctionCall1 (textin ,
14801515 CStringGetDatum (defaultValue ));
1481- new_record_repl [Anum_pg_type_typdefault - 1 ] = 'r' ;
1516+ new_record_repl [Anum_pg_type_typdefault - 1 ] = 'r' ;
1517+ }
14821518 }
14831519 else
1484- /* Default is NULL, drop it */
14851520 {
1521+ /* ALTER ... DROP DEFAULT */
14861522 new_record_nulls [Anum_pg_type_typdefaultbin - 1 ] = 'n' ;
14871523 new_record_repl [Anum_pg_type_typdefaultbin - 1 ] = 'r' ;
14881524 new_record_nulls [Anum_pg_type_typdefault - 1 ] = 'n' ;
0 commit comments