]> granicus.if.org Git - postgresql/commitdiff
Fix ALTER TABLE ADD COLUMN ... PRIMARY KEY so that the new column is correctly
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 24 Apr 2008 20:18:07 +0000 (20:18 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 24 Apr 2008 20:18:07 +0000 (20:18 +0000)
checked to see if it's been initialized to all non-nulls.  The implicit NOT
NULL constraint was not being checked during the ALTER (in fact, not even if
there was an explicit NOT NULL too), because ATExecAddColumn neglected to
set the flag needed to make the test happen.  This has been broken since
the capability was first added, in 8.0.

Brendan Jurd, per a report from Kaloyan Iliev.

src/backend/commands/tablecmds.c
src/test/regress/expected/alter_table.out
src/test/regress/sql/alter_table.sql

index b506b617b4ecc313d895036e558aeb389e647d1b..97b7d692a31b75feef669dccd9fe63bda4554942 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.206.2.3 2007/05/11 20:17:31 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.206.2.4 2008/04/24 20:18:07 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -3344,6 +3344,11 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
                tab->newvals = lappend(tab->newvals, newval);
        }
 
+       /*
+        * If the new column is NOT NULL, tell Phase 3 it needs to test that.
+        */
+       tab->new_notnull |= colDef->is_not_null;
+
        /*
         * Add needed dependency entries for the new column.
         */
index 8776cb90737ac3fd8e35158bcc8a260ed2c52d19..bd91947d426786bec981a511e2f5b5a6d126c528 100644 (file)
@@ -485,6 +485,18 @@ create table atacc1 ( test int );
 alter table atacc1 add constraint atacc_test1 primary key (test1);
 ERROR:  column "test1" named in key does not exist
 drop table atacc1;
+-- adding a new column as primary key to a non-empty table.
+-- should fail unless the column has a non-null default value.
+create table atacc1 ( test int );
+insert into atacc1 (test) values (0);
+-- add a primary key column without a default (fails).
+alter table atacc1 add column test2 int primary key;
+NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "atacc1_pkey" for table "atacc1"
+ERROR:  column "test2" contains null values
+-- now add a primary key column with a default (succeeds).
+alter table atacc1 add column test2 int default 0 primary key;
+NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "atacc1_pkey" for table "atacc1"
+drop table atacc1;
 -- something a little more complicated
 create table atacc1 ( test int, test2 int);
 -- add a primary key constraint
index 2ed67b3a1c6eab92118ffefad4f26f7fb115bfdb..a864c4a38d0fe8de7e81a9d5ae0e3a2b436f27cb 100644 (file)
@@ -487,6 +487,16 @@ create table atacc1 ( test int );
 alter table atacc1 add constraint atacc_test1 primary key (test1);
 drop table atacc1;
 
+-- adding a new column as primary key to a non-empty table.
+-- should fail unless the column has a non-null default value.
+create table atacc1 ( test int );
+insert into atacc1 (test) values (0);
+-- add a primary key column without a default (fails).
+alter table atacc1 add column test2 int primary key;
+-- now add a primary key column with a default (succeeds).
+alter table atacc1 add column test2 int default 0 primary key;
+drop table atacc1;
+
 -- something a little more complicated
 create table atacc1 ( test int, test2 int);
 -- add a primary key constraint