|
8 | 8 | *
|
9 | 9 | *
|
10 | 10 | * IDENTIFICATION
|
11 |
| - * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.271 2008/11/19 10:34:51 heikki Exp $ |
| 11 | + * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.272 2008/12/06 23:22:46 momjian Exp $ |
12 | 12 | *
|
13 | 13 | *-------------------------------------------------------------------------
|
14 | 14 | */
|
@@ -2334,6 +2334,12 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
|
2334 | 2334 | ATPrepAddColumn(wqueue,rel,recurse,cmd);
|
2335 | 2335 | pass=AT_PASS_ADD_COL;
|
2336 | 2336 | break;
|
| 2337 | +caseAT_AddColumnToView:/* add column via CREATE OR REPLACE VIEW */ |
| 2338 | +ATSimplePermissions(rel, true); |
| 2339 | +/* Performs own recursion */ |
| 2340 | +ATPrepAddColumn(wqueue,rel,recurse,cmd); |
| 2341 | +pass=AT_PASS_ADD_COL; |
| 2342 | +break; |
2337 | 2343 | caseAT_ColumnDefault:/* ALTER COLUMN DEFAULT */
|
2338 | 2344 |
|
2339 | 2345 | /*
|
@@ -2555,6 +2561,7 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
|
2555 | 2561 | switch (cmd->subtype)
|
2556 | 2562 | {
|
2557 | 2563 | caseAT_AddColumn:/* ADD COLUMN */
|
| 2564 | +caseAT_AddColumnToView:/* add column via CREATE OR REPLACE VIEW */ |
2558 | 2565 | ATExecAddColumn(tab,rel, (ColumnDef*)cmd->def);
|
2559 | 2566 | break;
|
2560 | 2567 | caseAT_ColumnDefault:/* ALTER COLUMN DEFAULT */
|
@@ -3455,6 +3462,7 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
|
3455 | 3462 | inti;
|
3456 | 3463 | intminattnum,
|
3457 | 3464 | maxatts;
|
| 3465 | +charrelkind; |
3458 | 3466 | HeapTupletypeTuple;
|
3459 | 3467 | OidtypeOid;
|
3460 | 3468 | int32typmod;
|
@@ -3527,6 +3535,7 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
|
3527 | 3535 | colDef->colname,RelationGetRelationName(rel))));
|
3528 | 3536 |
|
3529 | 3537 | minattnum= ((Form_pg_class)GETSTRUCT(reltup))->relnatts;
|
| 3538 | +relkind= ((Form_pg_class)GETSTRUCT(reltup))->relkind; |
3530 | 3539 | maxatts=minattnum+1;
|
3531 | 3540 | if (maxatts>MaxHeapAttributeNumber)
|
3532 | 3541 | ereport(ERROR,
|
@@ -3625,44 +3634,48 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
|
3625 | 3634 | * Note: we use build_column_default, and not just the cooked default
|
3626 | 3635 | * returned by AddRelationNewConstraints, so that the right thing happens
|
3627 | 3636 | * when a datatype's default applies.
|
| 3637 | + * |
| 3638 | + * We skip this logic completely for views. |
3628 | 3639 | */
|
3629 |
| -defval= (Expr*)build_column_default(rel,attribute.attnum); |
| 3640 | +if (relkind!=RELKIND_VIEW) { |
| 3641 | +defval= (Expr*)build_column_default(rel,attribute.attnum); |
3630 | 3642 |
|
3631 |
| -if (!defval&&GetDomainConstraints(typeOid)!=NIL) |
3632 |
| -{ |
3633 |
| -OidbaseTypeId; |
3634 |
| -int32baseTypeMod; |
3635 |
| - |
3636 |
| -baseTypeMod=typmod; |
3637 |
| -baseTypeId=getBaseTypeAndTypmod(typeOid,&baseTypeMod); |
3638 |
| -defval= (Expr*)makeNullConst(baseTypeId,baseTypeMod); |
3639 |
| -defval= (Expr*)coerce_to_target_type(NULL, |
3640 |
| -(Node*)defval, |
3641 |
| -baseTypeId, |
3642 |
| -typeOid, |
3643 |
| -typmod, |
3644 |
| -COERCION_ASSIGNMENT, |
3645 |
| -COERCE_IMPLICIT_CAST, |
3646 |
| --1); |
3647 |
| -if (defval==NULL)/* should not happen */ |
3648 |
| -elog(ERROR,"failed to coerce base type to domain"); |
3649 |
| -} |
| 3643 | +if (!defval&&GetDomainConstraints(typeOid)!=NIL) |
| 3644 | +{ |
| 3645 | +OidbaseTypeId; |
| 3646 | +int32baseTypeMod; |
| 3647 | + |
| 3648 | +baseTypeMod=typmod; |
| 3649 | +baseTypeId=getBaseTypeAndTypmod(typeOid,&baseTypeMod); |
| 3650 | +defval= (Expr*)makeNullConst(baseTypeId,baseTypeMod); |
| 3651 | +defval= (Expr*)coerce_to_target_type(NULL, |
| 3652 | +(Node*)defval, |
| 3653 | +baseTypeId, |
| 3654 | +typeOid, |
| 3655 | +typmod, |
| 3656 | +COERCION_ASSIGNMENT, |
| 3657 | +COERCE_IMPLICIT_CAST, |
| 3658 | +-1); |
| 3659 | +if (defval==NULL)/* should not happen */ |
| 3660 | +elog(ERROR,"failed to coerce base type to domain"); |
| 3661 | +} |
3650 | 3662 |
|
3651 |
| -if (defval) |
3652 |
| -{ |
3653 |
| -NewColumnValue*newval; |
| 3663 | +if (defval) |
| 3664 | +{ |
| 3665 | +NewColumnValue*newval; |
3654 | 3666 |
|
3655 |
| -newval= (NewColumnValue*)palloc0(sizeof(NewColumnValue)); |
3656 |
| -newval->attnum=attribute.attnum; |
3657 |
| -newval->expr=defval; |
| 3667 | +newval= (NewColumnValue*)palloc0(sizeof(NewColumnValue)); |
| 3668 | +newval->attnum=attribute.attnum; |
| 3669 | +newval->expr=defval; |
3658 | 3670 |
|
3659 |
| -tab->newvals=lappend(tab->newvals,newval); |
3660 |
| -} |
| 3671 | +tab->newvals=lappend(tab->newvals,newval); |
| 3672 | +} |
3661 | 3673 |
|
3662 |
| -/* |
3663 |
| - * If the new column is NOT NULL, tell Phase 3 it needs to test that. |
3664 |
| - */ |
3665 |
| -tab->new_notnull |=colDef->is_not_null; |
| 3674 | +/* |
| 3675 | + * If the new column is NOT NULL, tell Phase 3 it needs to test that. |
| 3676 | + */ |
| 3677 | +tab->new_notnull |=colDef->is_not_null; |
| 3678 | +} |
3666 | 3679 |
|
3667 | 3680 | /*
|
3668 | 3681 | * Add needed dependency entries for the new column.
|
|