Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita06e41d

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

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
boolnew_notnull;/* T if we added new NOT NULL constraints */
144144
boolnew_changeoids;/* T if we added/dropped the OID column */
145+
boolnew_changetypes;/* T if we changed column types */
145146
OidnewTableSpace;/* 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 weneed 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 wechange column data types or add/remove OIDs, the operation has to
3383+
*be propagated totables 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
elseif (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-
--atthemoment this will not work due toALTER TABLE inadequacy:
85+
-- thedefault doesn't need to propagate through tothe rowtypes, so this is OK
8686
alter table fullname add column suffix text default '';
87-
ERROR: cannotalter table"fullname" because column"people"."fn" uses its rowtype
88-
--butthisshould work:
87+
alter table fullname drop columnsuffix;
88+
-- thisone, 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-
--atthemoment this will not work due toALTER TABLE inadequacy:
48+
-- thedefault doesn't need to propagate through tothe rowtypes, so this is OK
4949
altertable fullname add column suffixtext default'';
50+
altertable fullname drop column suffix;
5051

51-
--butthisshould work:
52+
-- thisone, without a default, is OK too
5253
altertable fullname add column suffixtext defaultnull;
5354

55+
-- but this should fail, due to ALTER TABLE inadequacy
56+
altertable fullname alter column suffixset data typeinteger usingnull;
57+
5458
select*from people;
5559

5660
-- test insertion/updating of subfields

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp