88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.50 2002/10/21 22:06:19 tgl Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.51 2002/11/02 22:02:08 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -1649,6 +1649,7 @@ AlterTableAddColumn(Oid myrelid,
16491649* children ;
16501650ColumnDef * colDefChild = copyObject (colDef );
16511651
1652+ /* Child should see column as singly inherited */
16521653colDefChild -> inhcount = 1 ;
16531654colDefChild -> is_local = false;
16541655
@@ -1665,37 +1666,53 @@ AlterTableAddColumn(Oid myrelid,
16651666if (childrelid == myrelid )
16661667continue ;
16671668
1669+ childrel = heap_open (childrelid ,AccessExclusiveLock );
1670+
1671+ /* Does child already have a column by this name? */
16681672attrdesc = heap_openr (AttributeRelationName ,RowExclusiveLock );
16691673tuple = SearchSysCacheCopyAttName (childrelid ,colDef -> colname );
16701674if (!HeapTupleIsValid (tuple ))
16711675{
1676+ /* No, recurse to add it normally */
16721677heap_close (attrdesc ,RowExclusiveLock );
1678+ heap_close (childrel ,NoLock );
16731679AlterTableAddColumn (childrelid , true,colDefChild );
16741680continue ;
16751681}
16761682childatt = (Form_pg_attribute )GETSTRUCT (tuple );
16771683
1678- typeTuple = typenameType (colDef -> typename );
1684+ /* Okay if child matches by type */
1685+ if (typenameTypeId (colDef -> typename )!= childatt -> atttypid ||
1686+ colDef -> typename -> typmod != childatt -> atttypmod )
1687+ elog (ERROR ,"ALTER TABLE: child table \"%s\" has different type for column \"%s\"" ,
1688+ get_rel_name (childrelid ),colDef -> colname );
16791689
1680- if (HeapTupleGetOid (typeTuple )!= childatt -> atttypid ||
1681- colDef -> typename -> typmod != childatt -> atttypmod )
1682- elog (ERROR ,"ALTER TABLE: child table %u has different "
1683- "type for column \"%s\"" ,
1684- childrelid ,colDef -> colname );
1690+ /*
1691+ * XXX if we supported NOT NULL or defaults, would need to do
1692+ * more work here to verify child matches
1693+ */
16851694
1695+ elog (NOTICE ,"ALTER TABLE: merging definition of column \"%s\" for child %s" ,
1696+ colDef -> colname ,get_rel_name (childrelid ));
1697+
1698+ /* Bump the existing child att's inhcount */
16861699childatt -> attinhcount ++ ;
16871700simple_heap_update (attrdesc ,& tuple -> t_self ,tuple );
16881701CatalogUpdateIndexes (attrdesc ,tuple );
1689-
1690- childrel = RelationIdGetRelation (childrelid );
1691- elog (NOTICE ,"ALTER TABLE: merging definition of column "
1692- "\"%s\" for child %s" ,colDef -> colname ,
1693- RelationGetRelationName (childrel ));
1694- RelationClose (childrel );
16951702
1696- heap_close (attrdesc ,RowExclusiveLock );
1703+ /*
1704+ * Propagate any new CHECK constraints into the child table
1705+ * and its descendants
1706+ */
1707+ if (colDef -> constraints != NIL )
1708+ {
1709+ CommandCounterIncrement ();
1710+ AlterTableAddConstraint (childrelid , true,colDef -> constraints );
1711+ }
1712+
16971713heap_freetuple (tuple );
1698- ReleaseSysCache (typeTuple );
1714+ heap_close (attrdesc ,RowExclusiveLock );
1715+ heap_close (childrel ,NoLock );
16991716}
17001717}
17011718else