Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a06e41d

Browse files
committed
Remove arbitrary ALTER TABLE .. ADD COLUMN restriction.
The previous coding prevented ALTER TABLE .. ADD COLUMN from being used with a non-NULL default in situations where the table's rowtype was being used elsewhere. But this is a completely arbitrary restriction since you could do the same operation in multiple steps (add the column, add the default, update the table). Inspired by a patch from Noah Misch, though I didn't use his code.
1 parent 64bc872 commit a06e41d

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

src/backend/commands/tablecmds.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ typedef struct AlteredTableInfo
142142
List *newvals; /* List of NewColumnValue */
143143
bool new_notnull; /* T if we added new NOT NULL constraints */
144144
bool new_changeoids; /* T if we added/dropped the OID column */
145+
bool new_changetypes; /* T if we changed column types */
145146
Oid newTableSpace; /* new tablespace; 0 means no change */
146147
/* Objects to rebuild after completing ALTER TYPE operations */
147148
List *changedConstraintOids; /* OIDs of constraints to rebuild */
@@ -3378,14 +3379,14 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
33783379
}
33793380

33803381
/*
3381-
* If we need to rewrite the table, the operation has to be propagated to
3382-
* tables that use this table's rowtype as a column type.
3382+
* If we change column data types or add/remove OIDs, the operation has to
3383+
* be propagated to tables that use this table's rowtype as a column type.
33833384
*
33843385
* (Eventually this will probably become true for scans as well, but at
33853386
* the moment a composite type does not enforce any constraints, so it's
33863387
* not necessary/appropriate to enforce them just during ALTER.)
33873388
*/
3388-
if (newrel)
3389+
if (tab->new_changetypes || tab->new_changeoids)
33893390
find_composite_type_dependencies(oldrel->rd_rel->reltype,
33903391
RelationGetRelationName(oldrel),
33913392
NULL);
@@ -6429,6 +6430,7 @@ ATPrepAlterColumnType(List **wqueue,
64296430
newval->expr = (Expr *) transform;
64306431

64316432
tab->newvals = lappend(tab->newvals, newval);
6433+
tab->new_changetypes = true;
64326434
}
64336435
else if (tab->relkind == RELKIND_FOREIGN_TABLE)
64346436
{

src/test/regress/expected/rowtypes.out

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,14 @@ select * from people;
8282
(Joe,Blow) | 01-10-1984
8383
(1 row)
8484

85-
-- at the moment this will not work due to ALTER TABLE inadequacy:
85+
-- the default doesn't need to propagate through to the rowtypes, so this is OK
8686
alter table fullname add column suffix text default '';
87-
ERROR: cannot alter table "fullname" because column "people"."fn" uses its rowtype
88-
-- but this should work:
87+
alter table fullname drop column suffix;
88+
-- this one, without a default, is OK too
8989
alter table fullname add column suffix text default null;
90+
-- but this should fail, due to ALTER TABLE inadequacy
91+
alter table fullname alter column suffix set data type integer using null;
92+
ERROR: cannot alter table "fullname" because column "people"."fn" uses its rowtype
9093
select * from people;
9194
fn | bd
9295
-------------+------------

src/test/regress/sql/rowtypes.sql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@ insert into people values ('(Joe,Blow)', '1984-01-10');
4545

4646
select * from people;
4747

48-
-- at the moment this will not work due to ALTER TABLE inadequacy:
48+
-- the default doesn't need to propagate through to the rowtypes, so this is OK
4949
alter table fullname add column suffix text default '';
50+
alter table fullname drop column suffix;
5051

51-
-- but this should work:
52+
-- this one, without a default, is OK too
5253
alter table fullname add column suffix text default null;
5354

55+
-- but this should fail, due to ALTER TABLE inadequacy
56+
alter table fullname alter column suffix set data type integer using null;
57+
5458
select * from people;
5559

5660
-- test insertion/updating of subfields

0 commit comments

Comments
 (0)