88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.273 2008/12/13 19:13:44 tgl Exp $
11+ * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.274 2008/12/15 21:35:31 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -3459,9 +3459,7 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
34593459 attrdesc ;
34603460 HeapTuple reltup ;
34613461 FormData_pg_attribute attribute ;
3462- int i ;
3463- int minattnum ,
3464- maxatts ;
3462+ int newattnum ;
34653463 char relkind ;
34663464 HeapTuple typeTuple ;
34673465 Oid typeOid ;
@@ -3520,6 +3518,7 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
35203518 0 , 0 , 0 );
35213519 if (!HeapTupleIsValid (reltup ))
35223520 elog (ERROR , "cache lookup failed for relation %u" , myrelid );
3521+ relkind = ((Form_pg_class ) GETSTRUCT (reltup ))-> relkind ;
35233522
35243523 /*
35253524 * this test is deliberately not attisdropped-aware, since if one tries to
@@ -3534,15 +3533,12 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
35343533 errmsg ("column \"%s\" of relation \"%s\" already exists" ,
35353534 colDef -> colname , RelationGetRelationName (rel ))));
35363535
3537- minattnum = ((Form_pg_class ) GETSTRUCT (reltup ))-> relnatts ;
3538- relkind = ((Form_pg_class ) GETSTRUCT (reltup ))-> relkind ;
3539- maxatts = minattnum + 1 ;
3540- if (maxatts > MaxHeapAttributeNumber )
3536+ newattnum = ((Form_pg_class ) GETSTRUCT (reltup ))-> relnatts + 1 ;
3537+ if (newattnum > MaxHeapAttributeNumber )
35413538 ereport (ERROR ,
35423539 (errcode (ERRCODE_TOO_MANY_COLUMNS ),
35433540 errmsg ("tables can have at most %d columns" ,
35443541 MaxHeapAttributeNumber )));
3545- i = minattnum + 1 ;
35463542
35473543 typeTuple = typenameType (NULL , colDef -> typename , & typmod );
35483544 tform = (Form_pg_type ) GETSTRUCT (typeTuple );
@@ -3551,14 +3547,15 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
35513547 /* make sure datatype is legal for a column */
35523548 CheckAttributeType (colDef -> colname , typeOid );
35533549
3550+ /* construct new attribute's pg_attribute entry */
35543551 attribute .attrelid = myrelid ;
35553552 namestrcpy (& (attribute .attname ), colDef -> colname );
35563553 attribute .atttypid = typeOid ;
35573554 attribute .attstattarget = -1 ;
35583555 attribute .attlen = tform -> typlen ;
35593556 attribute .attcacheoff = -1 ;
35603557 attribute .atttypmod = typmod ;
3561- attribute .attnum = i ;
3558+ attribute .attnum = newattnum ;
35623559 attribute .attbyval = tform -> typbyval ;
35633560 attribute .attndims = list_length (colDef -> typename -> arrayBounds );
35643561 attribute .attstorage = tform -> typstorage ;
@@ -3578,7 +3575,7 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
35783575 /*
35793576 * Update number of attributes in pg_class tuple
35803577 */
3581- ((Form_pg_class ) GETSTRUCT (reltup ))-> relnatts = maxatts ;
3578+ ((Form_pg_class ) GETSTRUCT (reltup ))-> relnatts = newattnum ;
35823579
35833580 simple_heap_update (pgclass , & reltup -> t_self , reltup );
35843581
@@ -3635,9 +3632,13 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
36353632 * returned by AddRelationNewConstraints, so that the right thing happens
36363633 * when a datatype's default applies.
36373634 *
3638- * We skip this logic completely for views.
3635+ * We skip this step completely for views. For a view, we can only get
3636+ * here from CREATE OR REPLACE VIEW, which historically doesn't set up
3637+ * defaults, not even for domain-typed columns. And in any case we mustn't
3638+ * invoke Phase 3 on a view, since it has no storage.
36393639 */
3640- if (relkind != RELKIND_VIEW ) {
3640+ if (relkind != RELKIND_VIEW )
3641+ {
36413642 defval = (Expr * ) build_column_default (rel , attribute .attnum );
36423643
36433644 if (!defval && GetDomainConstraints (typeOid ) != NIL )
@@ -3680,7 +3681,7 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
36803681 /*
36813682 * Add needed dependency entries for the new column.
36823683 */
3683- add_column_datatype_dependency (myrelid , i , attribute .atttypid );
3684+ add_column_datatype_dependency (myrelid , newattnum , attribute .atttypid );
36843685}
36853686
36863687/*
0 commit comments