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

Commit5c06803

Browse files
committed
Fix ALTER TABLE ADD COLUMN ... PRIMARY KEY so that the new column is correctly
checked to see if it's been initialized to all non-nulls. The implicit NOTNULL constraint was not being checked during the ALTER (in fact, not even ifthere was an explicit NOT NULL too), because ATExecAddColumn neglected toset the flag needed to make the test happen. This has been broken sincethe capability was first added, in 8.0.Brendan Jurd, per a report from Kaloyan Iliev.
1 parent830f276 commit5c06803

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.250 2008/03/31 03:34:27 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.251 2008/04/24 20:17:50 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -3332,6 +3332,11 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
33323332
tab->newvals=lappend(tab->newvals,newval);
33333333
}
33343334

3335+
/*
3336+
* If the new column is NOT NULL, tell Phase 3 it needs to test that.
3337+
*/
3338+
tab->new_notnull |=colDef->is_not_null;
3339+
33353340
/*
33363341
* Add needed dependency entries for the new column.
33373342
*/

‎src/test/regress/expected/alter_table.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,18 @@ create table atacc1 ( test int );
505505
alter table atacc1 add constraint atacc_test1 primary key (test1);
506506
ERROR: column "test1" named in key does not exist
507507
drop table atacc1;
508+
-- adding a new column as primary key to a non-empty table.
509+
-- should fail unless the column has a non-null default value.
510+
create table atacc1 ( test int );
511+
insert into atacc1 (test) values (0);
512+
-- add a primary key column without a default (fails).
513+
alter table atacc1 add column test2 int primary key;
514+
NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "atacc1_pkey" for table "atacc1"
515+
ERROR: column "test2" contains null values
516+
-- now add a primary key column with a default (succeeds).
517+
alter table atacc1 add column test2 int default 0 primary key;
518+
NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "atacc1_pkey" for table "atacc1"
519+
drop table atacc1;
508520
-- something a little more complicated
509521
create table atacc1 ( test int, test2 int);
510522
-- add a primary key constraint

‎src/test/regress/sql/alter_table.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,16 @@ create table atacc1 ( test int );
506506
altertable atacc1 addconstraint atacc_test1primary key (test1);
507507
droptable atacc1;
508508

509+
-- adding a new column as primary key to a non-empty table.
510+
-- should fail unless the column has a non-null default value.
511+
createtableatacc1 ( testint );
512+
insert into atacc1 (test)values (0);
513+
-- add a primary key column without a default (fails).
514+
altertable atacc1 add column test2intprimary key;
515+
-- now add a primary key column with a default (succeeds).
516+
altertable atacc1 add column test2int default0primary key;
517+
droptable atacc1;
518+
509519
-- something a little more complicated
510520
createtableatacc1 ( testint, test2int);
511521
-- add a primary key constraint

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp