-- Try (and fail) to add constraint due to invalide destination columns explicitly given
ALTER TABLE tmp3 add constraint tmpconstr foreign key(a) references tmp2(b) match full;
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
-ERROR: UNIQUE constraint matching given keys for referenced table "tmp2" not found
+ERROR: ALTER TABLE: column "b" referenced in foreign key constraint does not exist
-- Try (and fail) to add constraint due to invalid data
ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
alter table atacc1 add constraint atacc_test1 check (test>3);
-- should fail
insert into atacc1 (test) values (2);
-ERROR: ExecInsert: rejected due to CHECK constraint atacc_test1
+ERROR: ExecInsert: rejected due to CHECK constraint "atacc_test1" on "atacc1"
-- should succeed
insert into atacc1 (test) values (4);
drop table atacc1;
alter table atacc1 add constraint atacc_test1 check (test+test2<test3*4);
-- should fail
insert into atacc1 (test,test2,test3) values (4,4,2);
-ERROR: ExecInsert: rejected due to CHECK constraint atacc_test1
+ERROR: ExecInsert: rejected due to CHECK constraint "atacc_test1" on "atacc1"
-- should succeed
insert into atacc1 (test,test2,test3) values (4,4,5);
drop table atacc1;
alter table atacc1 add check (test2>test);
-- should fail for $2
insert into atacc1 (test2, test) values (3, 4);
-ERROR: ExecInsert: rejected due to CHECK constraint $1
+ERROR: ExecInsert: rejected due to CHECK constraint "$1" on "atacc1"
drop table atacc1;
-- inheritance related tests
create table atacc1 (test int);
alter table atacc2 add constraint foo check (test2>0);
-- fail and then succeed on atacc2
insert into atacc2 (test2) values (-3);
-ERROR: ExecInsert: rejected due to CHECK constraint foo
+ERROR: ExecInsert: rejected due to CHECK constraint "foo" on "atacc2"
insert into atacc2 (test2) values (3);
-- fail and then succeed on atacc3
insert into atacc3 (test2) values (-3);
-ERROR: ExecInsert: rejected due to CHECK constraint foo
+ERROR: ExecInsert: rejected due to CHECK constraint "foo" on "atacc3"
insert into atacc3 (test2) values (3);
drop table atacc3;
drop table atacc2;
alter table only atacc2 add constraint foo check (test2>0);
-- fail and then succeed on atacc2
insert into atacc2 (test2) values (-3);
-ERROR: ExecInsert: rejected due to CHECK constraint foo
+ERROR: ExecInsert: rejected due to CHECK constraint "foo" on "atacc2"
insert into atacc2 (test2) values (3);
-- both succeed on atacc3
insert into atacc3 (test2) values (-3);
create table atacc1 ( test int );
-- add a primary key constraint
alter table atacc1 add constraint atacc_test1 primary key (test);
-ERROR: Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
+NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_test1' for table 'atacc1'
-- insert first value
insert into atacc1 (test) values (2);
-- should fail
insert into atacc1 (test) values (2);
+ERROR: Cannot insert a duplicate key into unique index atacc_test1
-- should succeed
insert into atacc1 (test) values (4);
-- inserting NULL should fail
insert into atacc1 (test) values(NULL);
--- try adding a primary key oid constraint
+ERROR: ExecInsert: Fail to add null value in not null attribute test
+-- try adding a second primary key (should fail)
+alter table atacc1 add constraint atacc_oid1 primary key(oid);
+ERROR: ALTER TABLE / PRIMARY KEY multiple primary keys for table 'atacc1' are not allowed
+-- drop first primary key constraint
+alter table atacc1 drop constraint atacc_test1 restrict;
+-- try adding a primary key on oid (should succeed)
alter table atacc1 add constraint atacc_oid1 primary key(oid);
NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_oid1' for table 'atacc1'
drop table atacc1;
insert into atacc1 (test) values (2);
-- add a primary key (fails)
alter table atacc1 add constraint atacc_test1 primary key (test);
-ERROR: Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
+NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_test1' for table 'atacc1'
+ERROR: Cannot create unique index. Table contains non-unique values
insert into atacc1 (test) values (3);
drop table atacc1;
-- let's do another one where the primary key constraint fails when added
insert into atacc1 (test) values (NULL);
-- add a primary key (fails)
alter table atacc1 add constraint atacc_test1 primary key (test);
-ERROR: Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
+NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_test1' for table 'atacc1'
+ERROR: ALTER TABLE: Attribute "test" contains NULL values
insert into atacc1 (test) values (3);
drop table atacc1;
-- let's do one where the primary key constraint fails
create table atacc1 ( test int, test2 int);
-- add a primary key constraint
alter table atacc1 add constraint atacc_test1 primary key (test, test2);
-ERROR: Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
+NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_test1' for table 'atacc1'
-- try adding a second primary key - should fail
alter table atacc1 add constraint atacc_test2 primary key (test);
-ERROR: Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
+ERROR: ALTER TABLE / PRIMARY KEY multiple primary keys for table 'atacc1' are not allowed
-- insert initial value
insert into atacc1 (test,test2) values (4,4);
-- should fail
insert into atacc1 (test,test2) values (4,4);
+ERROR: Cannot insert a duplicate key into unique index atacc_test1
insert into atacc1 (test,test2) values (NULL,3);
+ERROR: ExecInsert: Fail to add null value in not null attribute test
insert into atacc1 (test,test2) values (3, NULL);
+ERROR: ExecInsert: Fail to add null value in not null attribute test2
insert into atacc1 (test,test2) values (NULL,NULL);
+ERROR: ExecInsert: Fail to add null value in not null attribute test
-- should all succeed
insert into atacc1 (test,test2) values (4,5);
insert into atacc1 (test,test2) values (5,4);
alter table pg_class alter relname set not null;
ERROR: ALTER TABLE: relation "pg_class" is a system catalog
-- try altering non-existent table, should fail
-alter table foo alter column bar set not null;
-ERROR: Relation "foo" does not exist
-alter table foo alter column bar drop not null;
-ERROR: Relation "foo" does not exist
+alter table non_existent alter column bar set not null;
+ERROR: Relation "non_existent" does not exist
+alter table non_existent alter column bar drop not null;
+ERROR: Relation "non_existent" does not exist
-- test setting columns to null and not null and vice versa
-- test checking for null values and primary key
create table atacc1 (test int not null);
alter table atacc1 alter test set not null;
-- try altering a non-existent column, should fail
alter table atacc1 alter bar set not null;
-ERROR: ALTER TABLE: relation "atacc1" has no column "bar"
+ERROR: Relation "atacc1" has no column "bar"
alter table atacc1 alter bar drop not null;
-ERROR: ALTER TABLE: relation "atacc1" has no column "bar"
+ERROR: Relation "atacc1" has no column "bar"
-- try altering the oid column, should fail
alter table atacc1 alter oid set not null;
ERROR: ALTER TABLE: Cannot alter system attribute "oid"
alter table def_test alter column c2 set default 20;
-- set defaults on a non-existent column: this should fail
alter table def_test alter column c3 set default 30;
-ERROR: ALTER TABLE: relation "def_test" has no column "c3"
+ERROR: Relation "def_test" has no column "c3"
-- set defaults on views: we need to create a view, add a rule
-- to allow insertions into it, and then alter the view to add
-- a default
drop rule def_view_test_ins on def_view_test;
drop view def_view_test;
drop table def_test;
+-- alter table / drop column tests
+-- try altering system catalogs, should fail
+alter table pg_class drop column relname;
+ERROR: ALTER TABLE: relation "pg_class" is a system catalog
+-- try altering non-existent table, should fail
+alter table foo drop column bar;
+ERROR: Relation "foo" does not exist
+-- test dropping columns
+create table atacc1 (a int4 not null, b int4, c int4 not null, d int4);
+insert into atacc1 values (1, 2, 3, 4);
+alter table atacc1 drop a;
+alter table atacc1 drop a;
+ERROR: Relation "atacc1" has no column "a"
+-- SELECTs
+select * from atacc1;
+ b | c | d
+---+---+---
+ 2 | 3 | 4
+(1 row)
+
+select * from atacc1 order by a;
+ERROR: Attribute "a" not found
+select * from atacc1 order by "........pg.dropped.1........";
+ERROR: Attribute "........pg.dropped.1........" not found
+select * from atacc1 group by a;
+ERROR: Attribute "a" not found
+select * from atacc1 group by "........pg.dropped.1........";
+ERROR: Attribute "........pg.dropped.1........" not found
+select atacc1.* from atacc1;
+ b | c | d
+---+---+---
+ 2 | 3 | 4
+(1 row)
+
+select a from atacc1;
+ERROR: Attribute "a" not found
+select atacc1.a from atacc1;
+ERROR: No such attribute atacc1.a
+select b,c,d from atacc1;
+ b | c | d
+---+---+---
+ 2 | 3 | 4
+(1 row)
+
+select a,b,c,d from atacc1;
+ERROR: Attribute "a" not found
+select * from atacc1 where a = 1;
+ERROR: Attribute "a" not found
+select "........pg.dropped.1........" from atacc1;
+ERROR: Attribute "........pg.dropped.1........" not found
+select atacc1."........pg.dropped.1........" from atacc1;
+ERROR: No such attribute atacc1.........pg.dropped.1........
+select "........pg.dropped.1........",b,c,d from atacc1;
+ERROR: Attribute "........pg.dropped.1........" not found
+select * from atacc1 where "........pg.dropped.1........" = 1;
+ERROR: Attribute "........pg.dropped.1........" not found
+-- UPDATEs
+update atacc1 set a = 3;
+ERROR: Relation "atacc1" has no column "a"
+update atacc1 set b = 2 where a = 3;
+ERROR: Attribute "a" not found
+update atacc1 set "........pg.dropped.1........" = 3;
+ERROR: Relation "atacc1" has no column "........pg.dropped.1........"
+update atacc1 set b = 2 where "........pg.dropped.1........" = 3;
+ERROR: Attribute "........pg.dropped.1........" not found
+-- INSERTs
+insert into atacc1 values (10, 11, 12, 13);
+ERROR: INSERT has more expressions than target columns
+insert into atacc1 values (default, 11, 12, 13);
+ERROR: INSERT has more expressions than target columns
+insert into atacc1 values (11, 12, 13);
+insert into atacc1 (a) values (10);
+ERROR: Relation "atacc1" has no column "a"
+insert into atacc1 (a) values (default);
+ERROR: Relation "atacc1" has no column "a"
+insert into atacc1 (a,b,c,d) values (10,11,12,13);
+ERROR: Relation "atacc1" has no column "a"
+insert into atacc1 (a,b,c,d) values (default,11,12,13);
+ERROR: Relation "atacc1" has no column "a"
+insert into atacc1 (b,c,d) values (11,12,13);
+insert into atacc1 ("........pg.dropped.1........") values (10);
+ERROR: Relation "atacc1" has no column "........pg.dropped.1........"
+insert into atacc1 ("........pg.dropped.1........") values (default);
+ERROR: Relation "atacc1" has no column "........pg.dropped.1........"
+insert into atacc1 ("........pg.dropped.1........",b,c,d) values (10,11,12,13);
+ERROR: Relation "atacc1" has no column "........pg.dropped.1........"
+insert into atacc1 ("........pg.dropped.1........",b,c,d) values (default,11,12,13);
+ERROR: Relation "atacc1" has no column "........pg.dropped.1........"
+-- DELETEs
+delete from atacc1 where a = 3;
+ERROR: Attribute "a" not found
+delete from atacc1 where "........pg.dropped.1........" = 3;
+ERROR: Attribute "........pg.dropped.1........" not found
+delete from atacc1;
+-- try dropping a non-existent column, should fail
+alter table atacc1 drop bar;
+ERROR: Relation "atacc1" has no column "bar"
+-- try dropping the oid column, should fail
+alter table atacc1 drop oid;
+ERROR: ALTER TABLE: Cannot drop system attribute "oid"
+-- try creating a view and altering that, should fail
+create view myview as select * from atacc1;
+select * from myview;
+ b | c | d
+---+---+---
+(0 rows)
+
+alter table myview drop d;
+ERROR: ALTER TABLE: relation "myview" is not a table
+drop view myview;
+-- test some commands to make sure they fail on the dropped column
+analyze atacc1(a);
+ERROR: Relation "atacc1" has no column "a"
+analyze atacc1("........pg.dropped.1........");
+ERROR: Relation "atacc1" has no column "........pg.dropped.1........"
+vacuum analyze atacc1(a);
+ERROR: Relation "atacc1" has no column "a"
+vacuum analyze atacc1("........pg.dropped.1........");
+ERROR: Relation "atacc1" has no column "........pg.dropped.1........"
+comment on column atacc1.a is 'testing';
+ERROR: Relation "atacc1" has no column "a"
+comment on column atacc1."........pg.dropped.1........" is 'testing';
+ERROR: Relation "atacc1" has no column "........pg.dropped.1........"
+alter table atacc1 alter a set storage plain;
+ERROR: ALTER TABLE: relation "atacc1" has no column "a"
+alter table atacc1 alter "........pg.dropped.1........" set storage plain;
+ERROR: ALTER TABLE: relation "atacc1" has no column "........pg.dropped.1........"
+alter table atacc1 alter a set statistics 0;
+ERROR: ALTER TABLE: relation "atacc1" has no column "a"
+alter table atacc1 alter "........pg.dropped.1........" set statistics 0;
+ERROR: ALTER TABLE: relation "atacc1" has no column "........pg.dropped.1........"
+alter table atacc1 alter a set default 3;
+ERROR: Relation "atacc1" has no column "a"
+alter table atacc1 alter "........pg.dropped.1........" set default 3;
+ERROR: Relation "atacc1" has no column "........pg.dropped.1........"
+alter table atacc1 alter a drop default;
+ERROR: Relation "atacc1" has no column "a"
+alter table atacc1 alter "........pg.dropped.1........" drop default;
+ERROR: Relation "atacc1" has no column "........pg.dropped.1........"
+alter table atacc1 alter a set not null;
+ERROR: Relation "atacc1" has no column "a"
+alter table atacc1 alter "........pg.dropped.1........" set not null;
+ERROR: Relation "atacc1" has no column "........pg.dropped.1........"
+alter table atacc1 alter a drop not null;
+ERROR: Relation "atacc1" has no column "a"
+alter table atacc1 alter "........pg.dropped.1........" drop not null;
+ERROR: Relation "atacc1" has no column "........pg.dropped.1........"
+alter table atacc1 rename a to x;
+ERROR: renameatt: attribute "a" does not exist
+alter table atacc1 rename "........pg.dropped.1........" to x;
+ERROR: renameatt: attribute "........pg.dropped.1........" does not exist
+alter table atacc1 add primary key(a);
+ERROR: ALTER TABLE: column "a" named in key does not exist
+alter table atacc1 add primary key("........pg.dropped.1........");
+ERROR: ALTER TABLE: column "........pg.dropped.1........" named in key does not exist
+alter table atacc1 add unique(a);
+ERROR: ALTER TABLE: column "a" named in key does not exist
+alter table atacc1 add unique("........pg.dropped.1........");
+ERROR: ALTER TABLE: column "........pg.dropped.1........" named in key does not exist
+alter table atacc1 add check (a > 3);
+ERROR: Attribute "a" not found
+alter table atacc1 add check ("........pg.dropped.1........" > 3);
+ERROR: Attribute "........pg.dropped.1........" not found
+create table atacc2 (id int4 unique);
+NOTICE: CREATE TABLE / UNIQUE will create implicit index 'atacc2_id_key' for table 'atacc2'
+alter table atacc1 add foreign key (a) references atacc2(id);
+NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
+ERROR: ALTER TABLE: column "a" referenced in foreign key constraint does not exist
+alter table atacc1 add foreign key ("........pg.dropped.1........") references atacc2(id);
+NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
+ERROR: ALTER TABLE: column "........pg.dropped.1........" referenced in foreign key constraint does not exist
+alter table atacc2 add foreign key (id) references atacc1(a);
+NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
+ERROR: ALTER TABLE: column "a" referenced in foreign key constraint does not exist
+alter table atacc2 add foreign key (id) references atacc1("........pg.dropped.1........");
+NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
+ERROR: ALTER TABLE: column "........pg.dropped.1........" referenced in foreign key constraint does not exist
+drop table atacc2;
+create index "testing_idx" on atacc1(a);
+ERROR: DefineIndex: attribute "a" not found
+create index "testing_idx" on atacc1("........pg.dropped.1........");
+ERROR: DefineIndex: attribute "........pg.dropped.1........" not found
+-- test create as and select into
+insert into atacc1 values (21, 22, 23);
+create table test1 as select * from atacc1;
+select * from test1;
+ b | c | d
+----+----+----
+ 21 | 22 | 23
+(1 row)
+
+drop table test1;
+select * into test2 from atacc1;
+select * from test2;
+ b | c | d
+----+----+----
+ 21 | 22 | 23
+(1 row)
+
+drop table test2;
+-- try dropping all columns
+alter table atacc1 drop c;
+alter table atacc1 drop d;
+alter table atacc1 drop b;
+select * from atacc1;
+
+--
+(1 row)
+
+drop table atacc1;
+-- test inheritance
+create table parent (a int, b int, c int);
+insert into parent values (1, 2, 3);
+alter table parent drop a;
+create table child (d varchar(255)) inherits (parent);
+insert into child values (12, 13, 'testing');
+select * from parent;
+ b | c
+----+----
+ 2 | 3
+ 12 | 13
+(2 rows)
+
+select * from child;
+ b | c | d
+----+----+---------
+ 12 | 13 | testing
+(1 row)
+
+alter table parent drop c;
+select * from parent;
+ b
+----
+ 2
+ 12
+(2 rows)
+
+select * from child;
+ b | d
+----+---------
+ 12 | testing
+(1 row)
+
+drop table child;
+drop table parent;
+-- test copy in/out
+create table test (a int4, b int4, c int4);
+insert into test values (1,2,3);
+alter table test drop a;
+copy test to stdout;
+2 3
+copy test(a) to stdout;
+ERROR: Relation "test" has no column "a"
+copy test("........pg.dropped.1........") to stdout;
+ERROR: Relation "test" has no column "........pg.dropped.1........"
+copy test from stdin;
+ERROR: copy: line 1, Extra data after last expected column
+lost synchronization with server, resetting connection
+SET autocommit TO 'on';
+select * from test;
+ b | c
+---+---
+ 2 | 3
+(1 row)
+
+copy test from stdin;
+select * from test;
+ b | c
+----+----
+ 2 | 3
+ 21 | 22
+(2 rows)
+
+copy test(a) from stdin;
+ERROR: Relation "test" has no column "a"
+copy test("........pg.dropped.1........") from stdin;
+ERROR: Relation "test" has no column "........pg.dropped.1........"
+copy test(b,c) from stdin;
+select * from test;
+ b | c
+----+----
+ 2 | 3
+ 21 | 22
+ 31 | 32
+(3 rows)
+
+drop table test;
+-- test inheritance
+create table dropColumn (a int, b int, e int);
+create table dropColumnChild (c int) inherits (dropColumn);
+create table dropColumnAnother (d int) inherits (dropColumnChild);
+-- these two should fail
+alter table dropColumnchild drop column a;
+ERROR: ALTER TABLE: Cannot drop inherited column "a"
+alter table only dropColumnChild drop column b;
+ERROR: ALTER TABLE: Cannot drop inherited column "b"
+-- these three should work
+alter table only dropColumn drop column e;
+alter table dropColumnChild drop column c;
+alter table dropColumn drop column a;
+create table renameColumn (a int);
+create table renameColumnChild (b int) inherits (renameColumn);
+create table renameColumnAnother (c int) inherits (renameColumnChild);
+-- these three should fail
+alter table renameColumnChild rename column a to d;
+ERROR: renameatt: inherited attribute "a" may not be renamed
+alter table only renameColumnChild rename column a to d;
+ERROR: Inherited attribute "a" must be renamed in child tables too
+alter table only renameColumn rename column a to d;
+ERROR: Inherited attribute "a" must be renamed in child tables too
+-- these should work
+alter table renameColumn rename column a to d;
+alter table renameColumnChild rename column b to a;
+-- this should work
+alter table renameColumn add column w int;
+-- this should fail
+alter table only renameColumn add column x int;
+ERROR: Attribute must be added to child tables too
+-- Test corner cases in dropping of inherited columns
+create table p1 (f1 int, f2 int);
+create table c1 (f1 int not null) inherits(p1);
+NOTICE: CREATE TABLE: merging attribute "f1" with inherited definition
+-- should be rejected since c1.f1 is inherited
+alter table c1 drop column f1;
+ERROR: ALTER TABLE: Cannot drop inherited column "f1"
+-- should work
+alter table p1 drop column f1;
+-- c1.f1 is still there, but no longer inherited
+select f1 from c1;
+ f1
+----
+(0 rows)
+
+alter table c1 drop column f1;
+select f1 from c1;
+ERROR: Attribute "f1" not found
+drop table p1 cascade;
+NOTICE: Drop cascades to table c1
+create table p1 (f1 int, f2 int);
+create table c1 () inherits(p1);
+-- should be rejected since c1.f1 is inherited
+alter table c1 drop column f1;
+ERROR: ALTER TABLE: Cannot drop inherited column "f1"
+alter table p1 drop column f1;
+-- c1.f1 is dropped now, since there is no local definition for it
+select f1 from c1;
+ERROR: Attribute "f1" not found
+drop table p1 cascade;
+NOTICE: Drop cascades to table c1
+create table p1 (f1 int, f2 int);
+create table c1 () inherits(p1);
+-- should be rejected since c1.f1 is inherited
+alter table c1 drop column f1;
+ERROR: ALTER TABLE: Cannot drop inherited column "f1"
+alter table only p1 drop column f1;
+-- c1.f1 is NOT dropped, but must now be considered non-inherited
+alter table c1 drop column f1;
+drop table p1 cascade;
+NOTICE: Drop cascades to table c1
+create table p1 (f1 int, f2 int);
+create table c1 (f1 int not null) inherits(p1);
+NOTICE: CREATE TABLE: merging attribute "f1" with inherited definition
+-- should be rejected since c1.f1 is inherited
+alter table c1 drop column f1;
+ERROR: ALTER TABLE: Cannot drop inherited column "f1"
+alter table only p1 drop column f1;
+-- c1.f1 is still there, but no longer inherited
+alter table c1 drop column f1;
+drop table p1 cascade;
+NOTICE: Drop cascades to table c1
+create table p1(id int, name text);
+create table p2(id2 int, name text, height int);
+create table c1(age int) inherits(p1,p2);
+NOTICE: CREATE TABLE: merging multiple inherited definitions of attribute "name"
+create table gc1() inherits (c1);
+select relname, attname, attinhcount, attislocal
+from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
+where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
+order by relname, attnum;
+ relname | attname | attinhcount | attislocal
+---------+---------+-------------+------------
+ c1 | id | 1 | f
+ c1 | name | 2 | f
+ c1 | id2 | 1 | f
+ c1 | height | 1 | f
+ c1 | age | 0 | t
+ gc1 | id | 1 | f
+ gc1 | name | 1 | f
+ gc1 | id2 | 1 | f
+ gc1 | height | 1 | f
+ gc1 | age | 1 | f
+ p1 | id | 0 | t
+ p1 | name | 0 | t
+ p2 | id2 | 0 | t
+ p2 | name | 0 | t
+ p2 | height | 0 | t
+(15 rows)
+
+-- should work
+alter table only p1 drop column name;
+-- should work. Now c1.name is local and inhcount is 0.
+alter table p2 drop column name;
+-- should be rejected since its inherited
+alter table gc1 drop column name;
+ERROR: ALTER TABLE: Cannot drop inherited column "name"
+-- should work, and drop gc1.name along
+alter table c1 drop column name;
+-- should fail: column does not exist
+alter table gc1 drop column name;
+ERROR: Relation "gc1" has no column "name"
+-- should work and drop the attribute in all tables
+alter table p2 drop column height;
+select relname, attname, attinhcount, attislocal
+from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
+where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
+order by relname, attnum;
+ relname | attname | attinhcount | attislocal
+---------+---------+-------------+------------
+ c1 | id | 1 | f
+ c1 | id2 | 1 | f
+ c1 | age | 0 | t
+ gc1 | id | 1 | f
+ gc1 | id2 | 1 | f
+ gc1 | age | 1 | f
+ p1 | id | 0 | t
+ p2 | id2 | 0 | t
+(8 rows)
+
+drop table p1, p2 cascade;
+NOTICE: Drop cascades to table c1
+NOTICE: Drop cascades to table gc1
+-- test renumbering of child-table columns in inherited operations
+create table p1 (f1 int);
+create table c1 (f2 text, f3 int) inherits (p1);
+alter table p1 add column a1 int check (a1 > 0);
+alter table p1 add column f2 text;
+NOTICE: ALTER TABLE: merging definition of column "f2" for child c1
+insert into p1 values (1,2,'abc');
+insert into c1 values(11,'xyz',33,0); -- should fail
+ERROR: ExecInsert: rejected due to CHECK constraint "p1_a1" on "c1"
+insert into c1 values(11,'xyz',33,22);
+select * from p1;
+ f1 | a1 | f2
+----+----+-----
+ 1 | 2 | abc
+ 11 | 22 | xyz
+(2 rows)
+
+update p1 set a1 = a1 + 1, f2 = upper(f2);
+select * from p1;
+ f1 | a1 | f2
+----+----+-----
+ 1 | 3 | ABC
+ 11 | 23 | XYZ
+(2 rows)
+
+drop table p1 cascade;
+NOTICE: Drop cascades to table c1
+NOTICE: Drop cascades to constraint p1_a1 on table c1