8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
* DESCRIPTION
14
14
* The "DefineFoo" routines take the parse tree and pick out the
@@ -765,20 +765,40 @@ DefineDomain(CreateDomainStmt *stmt)
765
765
domainName );
766
766
767
767
/*
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.
771
776
*/
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
+ }
778
798
}
779
799
else
780
800
{
781
- /* DEFAULT NULL is same as not having a default */
801
+ /* No default (can this still happen?) */
782
802
defaultValue = NULL ;
783
803
defaultValueBin = NULL ;
784
804
}
@@ -1443,7 +1463,7 @@ AlterDomainDefault(List *names, Node *defaultRaw)
1443
1463
MemSet (new_record_nulls , ' ' , sizeof (new_record_nulls ));
1444
1464
MemSet (new_record_repl , ' ' , sizeof (new_record_repl ));
1445
1465
1446
- /* Store the new default, if null then skip this step */
1466
+ /* Store the new default into the tuple */
1447
1467
if (defaultRaw )
1448
1468
{
1449
1469
/* Create a dummy ParseState for transformExpr */
@@ -1459,30 +1479,46 @@ AlterDomainDefault(List *names, Node *defaultRaw)
1459
1479
NameStr (typTup -> typname ));
1460
1480
1461
1481
/*
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.)
1465
1485
*/
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 ,
1467
1503
deparse_context_for (NameStr (typTup -> typname ),
1468
1504
InvalidOid ),
1469
1505
false, false);
1470
1506
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 )));
1477
1512
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 ,
1480
1515
CStringGetDatum (defaultValue ));
1481
- new_record_repl [Anum_pg_type_typdefault - 1 ] = 'r' ;
1516
+ new_record_repl [Anum_pg_type_typdefault - 1 ] = 'r' ;
1517
+ }
1482
1518
}
1483
1519
else
1484
- /* Default is NULL, drop it */
1485
1520
{
1521
+ /* ALTER ... DROP DEFAULT */
1486
1522
new_record_nulls [Anum_pg_type_typdefaultbin - 1 ] = 'n' ;
1487
1523
new_record_repl [Anum_pg_type_typdefaultbin - 1 ] = 'r' ;
1488
1524
new_record_nulls [Anum_pg_type_typdefault - 1 ] = 'n' ;
0 commit comments