88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.75 2001/03/30 20:50:36 tgl Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.76 2001/04/02 18:30:49 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -276,6 +276,20 @@ TruncateRelation(char *name)
276276 *
277277 * If the same attribute name appears multiple times, then it appears
278278 * in the result table in the proper location for its first appearance.
279+ *
280+ * Constraints (including NOT NULL constraints) for the child table
281+ * are the union of all relevant constraints, from both the child schema
282+ * and parent tables.
283+ *
284+ * The default value for a child column is defined as:
285+ *(1) If the child schema specifies a default, that value is used.
286+ *(2) If neither the child nor any parent specifies a default, then
287+ *the column will not have a default.
288+ *(3) If conflicting defaults are inherited from different parents
289+ *(and not overridden by the child), an error is raised.
290+ *(4) Otherwise the inherited default is used.
291+ *Rule (3) is new in Postgres 7.1; in earlier releases you got a
292+ *rather arbitrary choice of which parent default to use.
279293 *----------
280294 */
281295static List *
@@ -286,6 +300,8 @@ MergeAttributes(List *schema, List *supers, bool istemp,
286300List * inhSchema = NIL ;
287301List * parentOids = NIL ;
288302List * constraints = NIL ;
303+ bool have_bogus_defaults = false;
304+ char * bogus_marker = "Bogus!" ;/* marks conflicting defaults */
289305int child_attno ;
290306
291307/*
@@ -305,10 +321,8 @@ MergeAttributes(List *schema, List *supers, bool istemp,
305321ColumnDef * restdef = lfirst (rest );
306322
307323if (strcmp (coldef -> colname ,restdef -> colname )== 0 )
308- {
309324elog (ERROR ,"CREATE TABLE: attribute \"%s\" duplicated" ,
310325coldef -> colname );
311- }
312326}
313327}
314328/*
@@ -321,10 +335,8 @@ MergeAttributes(List *schema, List *supers, bool istemp,
321335foreach (rest ,lnext (entry ))
322336{
323337if (strcmp (strVal (lfirst (entry )),strVal (lfirst (rest )))== 0 )
324- {
325338elog (ERROR ,"CREATE TABLE: inherited relation \"%s\" duplicated" ,
326339strVal (lfirst (entry )));
327- }
328340}
329341}
330342
@@ -436,31 +448,44 @@ MergeAttributes(List *schema, List *supers, bool istemp,
436448newattno [parent_attno - 1 ]= ++ child_attno ;
437449}
438450/*
439- * Copy default if any, overriding any default from earlier parent
451+ * Copy default if any
440452 */
441453if (attribute -> atthasdef )
442454{
455+ char * this_default = NULL ;
443456AttrDefault * attrdef ;
444457int i ;
445458
446- def -> raw_default = NULL ;
447- def -> cooked_default = NULL ;
448-
459+ /* Find default in constraint structure */
449460Assert (constr != NULL );
450461attrdef = constr -> defval ;
451462for (i = 0 ;i < constr -> num_defval ;i ++ )
452463{
453464if (attrdef [i ].adnum == parent_attno )
454465{
455- /*
456- * if default expr could contain any vars, we'd
457- * need to fix 'em, but it can't ...
458- */
459- def -> cooked_default = pstrdup (attrdef [i ].adbin );
466+ this_default = attrdef [i ].adbin ;
460467break ;
461468}
462469}
463- Assert (def -> cooked_default != NULL );
470+ Assert (this_default != NULL );
471+ /*
472+ * If default expr could contain any vars, we'd need to fix
473+ * 'em, but it can't; so default is ready to apply to child.
474+ *
475+ * If we already had a default from some prior parent,
476+ * check to see if they are the same. If so, no problem;
477+ * if not, mark the column as having a bogus default.
478+ * Below, we will complain if the bogus default isn't
479+ * overridden by the child schema.
480+ */
481+ Assert (def -> raw_default == NULL );
482+ if (def -> cooked_default == NULL )
483+ def -> cooked_default = pstrdup (this_default );
484+ else if (strcmp (def -> cooked_default ,this_default )!= 0 )
485+ {
486+ def -> cooked_default = bogus_marker ;
487+ have_bogus_defaults = true;
488+ }
464489}
465490}
466491/*
@@ -555,6 +580,23 @@ MergeAttributes(List *schema, List *supers, bool istemp,
555580schema = inhSchema ;
556581}
557582
583+ /*
584+ * If we found any conflicting parent default values, check to make
585+ * sure they were overridden by the child.
586+ */
587+ if (have_bogus_defaults )
588+ {
589+ foreach (entry ,schema )
590+ {
591+ ColumnDef * def = lfirst (entry );
592+
593+ if (def -> cooked_default == bogus_marker )
594+ elog (ERROR ,"CREATE TABLE: attribute \"%s\" inherits conflicting default values"
595+ "\n\tTo resolve the conflict, specify a default explicitly" ,
596+ def -> colname );
597+ }
598+ }
599+
558600* supOids = parentOids ;
559601* supconstr = constraints ;
560602return schema ;