]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/alter_table.out
7015753d173fd457c28f3b31f0d75da5519caa3f
[postgresql] / src / test / regress / expected / alter_table.out
1 --
2 -- ALTER_TABLE
3 -- add attribute
4 --
5 CREATE TABLE tmp (initial int4);
6 ALTER TABLE tmp ADD COLUMN a int4;
7 ALTER TABLE tmp ADD COLUMN b name;
8 ALTER TABLE tmp ADD COLUMN c text;
9 ALTER TABLE tmp ADD COLUMN d float8;
10 ALTER TABLE tmp ADD COLUMN e float4;
11 ALTER TABLE tmp ADD COLUMN f int2;
12 ALTER TABLE tmp ADD COLUMN g polygon;
13 ALTER TABLE tmp ADD COLUMN h abstime;
14 ALTER TABLE tmp ADD COLUMN i char;
15 ALTER TABLE tmp ADD COLUMN j abstime[];
16 ALTER TABLE tmp ADD COLUMN k int4;
17 ALTER TABLE tmp ADD COLUMN l tid;
18 ALTER TABLE tmp ADD COLUMN m xid;
19 ALTER TABLE tmp ADD COLUMN n oidvector;
20 --ALTER TABLE tmp ADD COLUMN o lock;
21 ALTER TABLE tmp ADD COLUMN p smgr;
22 ALTER TABLE tmp ADD COLUMN q point;
23 ALTER TABLE tmp ADD COLUMN r lseg;
24 ALTER TABLE tmp ADD COLUMN s path;
25 ALTER TABLE tmp ADD COLUMN t box;
26 ALTER TABLE tmp ADD COLUMN u tinterval;
27 ALTER TABLE tmp ADD COLUMN v timestamp;
28 ALTER TABLE tmp ADD COLUMN w interval;
29 ALTER TABLE tmp ADD COLUMN x float8[];
30 ALTER TABLE tmp ADD COLUMN y float4[];
31 ALTER TABLE tmp ADD COLUMN z int2[];
32 INSERT INTO tmp (a, b, c, d, e, f, g, h, i, j, k, l, m, n, p, q, r, s, t, u,
33         v, w, x, y, z)
34    VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)', 
35         'Mon May  1 00:30:30 1995', 'c', '{Mon May  1 00:30:30 1995, Monday Aug 24 14:43:07 1992, epoch}', 
36         314159, '(1,1)', '512',
37         '1 2 3 4 5 6 7 8', 'magnetic disk', '(1.1,1.1)', '(4.1,4.1,3.1,3.1)',
38         '(0,2,4.1,4.1,3.1,3.1)', '(4.1,4.1,3.1,3.1)', '["epoch" "infinity"]',
39         'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
40 SELECT * FROM tmp;
41  initial | a |  b   |  c   |  d  |  e  | f |           g           |              h               | i |                                               j                                                |   k    |   l   |  m  |        n        |       p       |     q     |           r           |              s              |          t          |                      u                      |            v             |        w         |     x     |     y     |     z     
42 ---------+---+------+------+-----+-----+---+-----------------------+------------------------------+---+------------------------------------------------------------------------------------------------+--------+-------+-----+-----------------+---------------+-----------+-----------------------+-----------------------------+---------------------+---------------------------------------------+--------------------------+------------------+-----------+-----------+-----------
43          | 4 | name | text | 4.1 | 4.1 | 2 | ((4.1,4.1),(3.1,3.1)) | Mon May 01 00:30:30 1995 PDT | c | {"Mon May 01 00:30:30 1995 PDT","Mon Aug 24 14:43:07 1992 PDT","Wed Dec 31 16:00:00 1969 PST"} | 314159 | (1,1) | 512 | 1 2 3 4 5 6 7 8 | magnetic disk | (1.1,1.1) | [(4.1,4.1),(3.1,3.1)] | ((0,2),(4.1,4.1),(3.1,3.1)) | (4.1,4.1),(3.1,3.1) | ["Wed Dec 31 16:00:00 1969 PST" "infinity"] | Thu Jan 01 00:00:00 1970 | @ 1 hour 10 secs | {1,2,3,4} | {1,2,3,4} | {1,2,3,4}
44 (1 row)
45
46 DROP TABLE tmp;
47 -- the wolf bug - schema mods caused inconsistent row descriptors 
48 CREATE TABLE tmp (
49         initial         int4
50 );
51 ALTER TABLE tmp ADD COLUMN a int4;
52 ALTER TABLE tmp ADD COLUMN b name;
53 ALTER TABLE tmp ADD COLUMN c text;
54 ALTER TABLE tmp ADD COLUMN d float8;
55 ALTER TABLE tmp ADD COLUMN e float4;
56 ALTER TABLE tmp ADD COLUMN f int2;
57 ALTER TABLE tmp ADD COLUMN g polygon;
58 ALTER TABLE tmp ADD COLUMN h abstime;
59 ALTER TABLE tmp ADD COLUMN i char;
60 ALTER TABLE tmp ADD COLUMN j abstime[];
61 ALTER TABLE tmp ADD COLUMN k int4;
62 ALTER TABLE tmp ADD COLUMN l tid;
63 ALTER TABLE tmp ADD COLUMN m xid;
64 ALTER TABLE tmp ADD COLUMN n oidvector;
65 --ALTER TABLE tmp ADD COLUMN o lock;
66 ALTER TABLE tmp ADD COLUMN p smgr;
67 ALTER TABLE tmp ADD COLUMN q point;
68 ALTER TABLE tmp ADD COLUMN r lseg;
69 ALTER TABLE tmp ADD COLUMN s path;
70 ALTER TABLE tmp ADD COLUMN t box;
71 ALTER TABLE tmp ADD COLUMN u tinterval;
72 ALTER TABLE tmp ADD COLUMN v timestamp;
73 ALTER TABLE tmp ADD COLUMN w interval;
74 ALTER TABLE tmp ADD COLUMN x float8[];
75 ALTER TABLE tmp ADD COLUMN y float4[];
76 ALTER TABLE tmp ADD COLUMN z int2[];
77 INSERT INTO tmp (a, b, c, d, e, f, g, h, i, j, k, l, m, n, p, q, r, s, t, u,
78         v, w, x, y, z)
79    VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)', 
80         'Mon May  1 00:30:30 1995', 'c', '{Mon May  1 00:30:30 1995, Monday Aug 24 14:43:07 1992, epoch}', 
81         314159, '(1,1)', '512',
82         '1 2 3 4 5 6 7 8', 'magnetic disk', '(1.1,1.1)', '(4.1,4.1,3.1,3.1)',
83         '(0,2,4.1,4.1,3.1,3.1)', '(4.1,4.1,3.1,3.1)', '["epoch" "infinity"]',
84         'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
85 SELECT * FROM tmp;
86  initial | a |  b   |  c   |  d  |  e  | f |           g           |              h               | i |                                               j                                                |   k    |   l   |  m  |        n        |       p       |     q     |           r           |              s              |          t          |                      u                      |            v             |        w         |     x     |     y     |     z     
87 ---------+---+------+------+-----+-----+---+-----------------------+------------------------------+---+------------------------------------------------------------------------------------------------+--------+-------+-----+-----------------+---------------+-----------+-----------------------+-----------------------------+---------------------+---------------------------------------------+--------------------------+------------------+-----------+-----------+-----------
88          | 4 | name | text | 4.1 | 4.1 | 2 | ((4.1,4.1),(3.1,3.1)) | Mon May 01 00:30:30 1995 PDT | c | {"Mon May 01 00:30:30 1995 PDT","Mon Aug 24 14:43:07 1992 PDT","Wed Dec 31 16:00:00 1969 PST"} | 314159 | (1,1) | 512 | 1 2 3 4 5 6 7 8 | magnetic disk | (1.1,1.1) | [(4.1,4.1),(3.1,3.1)] | ((0,2),(4.1,4.1),(3.1,3.1)) | (4.1,4.1),(3.1,3.1) | ["Wed Dec 31 16:00:00 1969 PST" "infinity"] | Thu Jan 01 00:00:00 1970 | @ 1 hour 10 secs | {1,2,3,4} | {1,2,3,4} | {1,2,3,4}
89 (1 row)
90
91 DROP TABLE tmp;
92 --
93 -- rename -
94 --   should preserve indices, which we can check by seeing if a SELECT
95 --   chooses an indexscan; however, in the absence of vacuum statistics
96 --   it might not.  Therefore, vacuum first.
97 --
98 VACUUM ANALYZE tenk1;
99 ALTER TABLE tenk1 RENAME TO ten_k;
100 -- 20 values, sorted 
101 SELECT unique1 FROM ten_k WHERE unique1 < 20;
102  unique1 
103 ---------
104        0
105        1
106        2
107        3
108        4
109        5
110        6
111        7
112        8
113        9
114       10
115       11
116       12
117       13
118       14
119       15
120       16
121       17
122       18
123       19
124 (20 rows)
125
126 -- 20 values, sorted 
127 SELECT unique2 FROM ten_k WHERE unique2 < 20;
128  unique2 
129 ---------
130        0
131        1
132        2
133        3
134        4
135        5
136        6
137        7
138        8
139        9
140       10
141       11
142       12
143       13
144       14
145       15
146       16
147       17
148       18
149       19
150 (20 rows)
151
152 -- 100 values, sorted 
153 SELECT hundred FROM ten_k WHERE hundred = 50;
154  hundred 
155 ---------
156       50
157       50
158       50
159       50
160       50
161       50
162       50
163       50
164       50
165       50
166       50
167       50
168       50
169       50
170       50
171       50
172       50
173       50
174       50
175       50
176       50
177       50
178       50
179       50
180       50
181       50
182       50
183       50
184       50
185       50
186       50
187       50
188       50
189       50
190       50
191       50
192       50
193       50
194       50
195       50
196       50
197       50
198       50
199       50
200       50
201       50
202       50
203       50
204       50
205       50
206       50
207       50
208       50
209       50
210       50
211       50
212       50
213       50
214       50
215       50
216       50
217       50
218       50
219       50
220       50
221       50
222       50
223       50
224       50
225       50
226       50
227       50
228       50
229       50
230       50
231       50
232       50
233       50
234       50
235       50
236       50
237       50
238       50
239       50
240       50
241       50
242       50
243       50
244       50
245       50
246       50
247       50
248       50
249       50
250       50
251       50
252       50
253       50
254       50
255       50
256 (100 rows)
257
258 ALTER TABLE ten_k RENAME TO tenk1;
259 -- 5 values, sorted 
260 SELECT unique1 FROM tenk1 WHERE unique1 < 5;
261  unique1 
262 ---------
263        0
264        1
265        2
266        3
267        4
268 (5 rows)
269
270 -- ALTER TABLE ... RENAME on non-table relations
271 -- renaming indexes (FIXME: this should probably test the index's functionality)
272 ALTER TABLE onek_unique1 RENAME TO tmp_onek_unique1;
273 ALTER TABLE tmp_onek_unique1 RENAME TO onek_unique1;
274 -- renaming views
275 CREATE VIEW tmp_view (unique1) AS SELECT unique1 FROM tenk1;
276 ALTER TABLE tmp_view RENAME TO tmp_view_new;
277 -- 5 values, sorted 
278 SELECT unique1 FROM tenk1 WHERE unique1 < 5;
279  unique1 
280 ---------
281        0
282        1
283        2
284        3
285        4
286 (5 rows)
287
288 DROP VIEW tmp_view_new;
289 -- renaming sequences
290 CREATE SEQUENCE foo_seq;
291 ALTER TABLE foo_seq RENAME TO foo_seq_new;
292 SELECT * FROM foo_seq_new;
293  sequence_name | last_value | increment_by |      max_value      | min_value | cache_value | log_cnt | is_cycled | is_called 
294 ---------------+------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
295  foo_seq       |          1 |            1 | 9223372036854775807 |         1 |           1 |       1 | f         | f
296 (1 row)
297
298 DROP SEQUENCE foo_seq_new;
299 -- toast-like relation name
300 alter table stud_emp rename to pg_toast_stud_emp;
301 alter table pg_toast_stud_emp rename to stud_emp;
302 -- FOREIGN KEY CONSTRAINT adding TEST
303 CREATE TABLE tmp2 (a int primary key);
304 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'tmp2_pkey' for table 'tmp2'
305 CREATE TABLE tmp3 (a int, b int);
306 CREATE TABLE tmp4 (a int, b int, unique(a,b));
307 NOTICE:  CREATE TABLE / UNIQUE will create implicit index 'tmp4_a_key' for table 'tmp4'
308 CREATE TABLE tmp5 (a int, b int);
309 -- Insert rows into tmp2 (pktable)
310 INSERT INTO tmp2 values (1);
311 INSERT INTO tmp2 values (2);
312 INSERT INTO tmp2 values (3);
313 INSERT INTO tmp2 values (4);
314 -- Insert rows into tmp3
315 INSERT INTO tmp3 values (1,10);
316 INSERT INTO tmp3 values (1,20);
317 INSERT INTO tmp3 values (5,50);
318 -- Try (and fail) to add constraint due to invalid source columns
319 ALTER TABLE tmp3 add constraint tmpconstr foreign key(c) references tmp2 match full;
320 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
321 ERROR:  ALTER TABLE: column "c" referenced in foreign key constraint does not exist
322 -- Try (and fail) to add constraint due to invalide destination columns explicitly given
323 ALTER TABLE tmp3 add constraint tmpconstr foreign key(a) references tmp2(b) match full;
324 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
325 ERROR:  ALTER TABLE: column "b" referenced in foreign key constraint does not exist
326 -- Try (and fail) to add constraint due to invalid data
327 ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;
328 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
329 ERROR:  tmpconstr referential integrity violation - key referenced from tmp3 not found in tmp2
330 -- Delete failing row
331 DELETE FROM tmp3 where a=5;
332 -- Try (and succeed)
333 ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;
334 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
335 -- Try (and fail) to create constraint from tmp5(a) to tmp4(a) - unique constraint on
336 -- tmp4 is a,b
337 ALTER TABLE tmp5 add constraint tmpconstr foreign key(a) references tmp4(a) match full;
338 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
339 ERROR:  UNIQUE constraint matching given keys for referenced table "tmp4" not found
340 DROP TABLE tmp5;
341 DROP TABLE tmp4;
342 DROP TABLE tmp3;
343 DROP TABLE tmp2;
344 -- Foreign key adding test with mixed types
345 -- Note: these tables are TEMP to avoid name conflicts when this test
346 -- is run in parallel with foreign_key.sql.
347 CREATE TEMP TABLE PKTABLE (ptest1 int PRIMARY KEY);
348 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
349 CREATE TEMP TABLE FKTABLE (ftest1 inet);
350 -- This next should fail, because inet=int does not exist
351 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
352 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
353 ERROR:  Unable to identify an operator '=' for types 'inet' and 'integer'
354         You will have to retype this query using an explicit cast
355 -- This should also fail for the same reason, but here we
356 -- give the column name
357 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable(ptest1);
358 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
359 ERROR:  Unable to identify an operator '=' for types 'inet' and 'integer'
360         You will have to retype this query using an explicit cast
361 -- This should succeed, even though they are different types
362 -- because varchar=int does exist
363 DROP TABLE FKTABLE;
364 CREATE TEMP TABLE FKTABLE (ftest1 varchar);
365 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
366 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
367 -- As should this
368 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable(ptest1);
369 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
370 DROP TABLE pktable cascade;
371 NOTICE:  Drop cascades to constraint $2 on table fktable
372 NOTICE:  Drop cascades to constraint $1 on table fktable
373 DROP TABLE fktable;
374 CREATE TEMP TABLE PKTABLE (ptest1 int, ptest2 inet,
375                            PRIMARY KEY(ptest1, ptest2));
376 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
377 -- This should fail, because we just chose really odd types
378 CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);
379 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2) references pktable;
380 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
381 ERROR:  Unable to identify an operator '=' for types 'cidr' and 'integer'
382         You will have to retype this query using an explicit cast
383 DROP TABLE FKTABLE;
384 -- Again, so should this...
385 CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);
386 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
387      references pktable(ptest1, ptest2);
388 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
389 ERROR:  Unable to identify an operator '=' for types 'cidr' and 'integer'
390         You will have to retype this query using an explicit cast
391 DROP TABLE FKTABLE;
392 -- This fails because we mixed up the column ordering
393 CREATE TEMP TABLE FKTABLE (ftest1 int, ftest2 inet);
394 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
395      references pktable(ptest2, ptest1);
396 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
397 ERROR:  Unable to identify an operator '=' for types 'integer' and 'inet'
398         You will have to retype this query using an explicit cast
399 -- As does this...
400 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest2, ftest1)
401      references pktable(ptest1, ptest2);
402 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
403 ERROR:  Unable to identify an operator '=' for types 'inet' and 'integer'
404         You will have to retype this query using an explicit cast
405 -- temp tables should go away by themselves, need not drop them.
406 -- test check constraint adding
407 create table atacc1 ( test int );
408 -- add a check constraint
409 alter table atacc1 add constraint atacc_test1 check (test>3);
410 -- should fail
411 insert into atacc1 (test) values (2);
412 ERROR:  ExecInsert: rejected due to CHECK constraint "atacc_test1" on "atacc1"
413 -- should succeed
414 insert into atacc1 (test) values (4);
415 drop table atacc1;
416 -- let's do one where the check fails when added
417 create table atacc1 ( test int );
418 -- insert a soon to be failing row
419 insert into atacc1 (test) values (2);
420 -- add a check constraint (fails)
421 alter table atacc1 add constraint atacc_test1 check (test>3);
422 ERROR:  AlterTableAddConstraint: rejected due to CHECK constraint atacc_test1
423 insert into atacc1 (test) values (4);
424 drop table atacc1;
425 -- let's do one where the check fails because the column doesn't exist
426 create table atacc1 ( test int );
427 -- add a check constraint (fails)
428 alter table atacc1 add constraint atacc_test1 check (test1>3);
429 ERROR:  Attribute "test1" not found
430 drop table atacc1;
431 -- something a little more complicated
432 create table atacc1 ( test int, test2 int, test3 int);
433 -- add a check constraint (fails)
434 alter table atacc1 add constraint atacc_test1 check (test+test2<test3*4);
435 -- should fail
436 insert into atacc1 (test,test2,test3) values (4,4,2);
437 ERROR:  ExecInsert: rejected due to CHECK constraint "atacc_test1" on "atacc1"
438 -- should succeed
439 insert into atacc1 (test,test2,test3) values (4,4,5);
440 drop table atacc1;
441 -- lets do some naming tests
442 create table atacc1 (test int check (test>3), test2 int);
443 alter table atacc1 add check (test2>test);
444 -- should fail for $2
445 insert into atacc1 (test2, test) values (3, 4);
446 ERROR:  ExecInsert: rejected due to CHECK constraint "$1" on "atacc1"
447 drop table atacc1;
448 -- inheritance related tests
449 create table atacc1 (test int);
450 create table atacc2 (test2 int);
451 create table atacc3 (test3 int) inherits (atacc1, atacc2);
452 alter table atacc2 add constraint foo check (test2>0);
453 -- fail and then succeed on atacc2
454 insert into atacc2 (test2) values (-3);
455 ERROR:  ExecInsert: rejected due to CHECK constraint "foo" on "atacc2"
456 insert into atacc2 (test2) values (3);
457 -- fail and then succeed on atacc3
458 insert into atacc3 (test2) values (-3);
459 ERROR:  ExecInsert: rejected due to CHECK constraint "foo" on "atacc3"
460 insert into atacc3 (test2) values (3);
461 drop table atacc3;
462 drop table atacc2;
463 drop table atacc1;
464 -- let's try only to add only to the parent
465 create table atacc1 (test int);
466 create table atacc2 (test2 int);
467 create table atacc3 (test3 int) inherits (atacc1, atacc2);
468 alter table only atacc2 add constraint foo check (test2>0);
469 -- fail and then succeed on atacc2
470 insert into atacc2 (test2) values (-3);
471 ERROR:  ExecInsert: rejected due to CHECK constraint "foo" on "atacc2"
472 insert into atacc2 (test2) values (3);
473 -- both succeed on atacc3
474 insert into atacc3 (test2) values (-3);
475 insert into atacc3 (test2) values (3);
476 drop table atacc3;
477 drop table atacc2;
478 drop table atacc1;
479 -- test unique constraint adding
480 create table atacc1 ( test int );
481 -- add a unique constraint
482 alter table atacc1 add constraint atacc_test1 unique (test);
483 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
484 -- insert first value
485 insert into atacc1 (test) values (2);
486 -- should fail
487 insert into atacc1 (test) values (2);
488 ERROR:  Cannot insert a duplicate key into unique index atacc_test1
489 -- should succeed
490 insert into atacc1 (test) values (4);
491 -- try adding a unique oid constraint
492 alter table atacc1 add constraint atacc_oid1 unique(oid);
493 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_oid1' for table 'atacc1'
494 drop table atacc1;
495 -- let's do one where the unique constraint fails when added
496 create table atacc1 ( test int );
497 -- insert soon to be failing rows
498 insert into atacc1 (test) values (2);
499 insert into atacc1 (test) values (2);
500 -- add a unique constraint (fails)
501 alter table atacc1 add constraint atacc_test1 unique (test);
502 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
503 ERROR:  Cannot create unique index. Table contains non-unique values
504 insert into atacc1 (test) values (3);
505 drop table atacc1;
506 -- let's do one where the unique constraint fails
507 -- because the column doesn't exist
508 create table atacc1 ( test int );
509 -- add a unique constraint (fails)
510 alter table atacc1 add constraint atacc_test1 unique (test1);
511 ERROR:  ALTER TABLE: column "test1" named in key does not exist
512 drop table atacc1;
513 -- something a little more complicated
514 create table atacc1 ( test int, test2 int);
515 -- add a unique constraint
516 alter table atacc1 add constraint atacc_test1 unique (test, test2);
517 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
518 -- insert initial value
519 insert into atacc1 (test,test2) values (4,4);
520 -- should fail
521 insert into atacc1 (test,test2) values (4,4);
522 ERROR:  Cannot insert a duplicate key into unique index atacc_test1
523 -- should all succeed
524 insert into atacc1 (test,test2) values (4,5);
525 insert into atacc1 (test,test2) values (5,4);
526 insert into atacc1 (test,test2) values (5,5);
527 drop table atacc1;
528 -- lets do some naming tests
529 create table atacc1 (test int, test2 int, unique(test));
530 NOTICE:  CREATE TABLE / UNIQUE will create implicit index 'atacc1_test_key' for table 'atacc1'
531 alter table atacc1 add unique (test2);
532 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc1_test2_key' for table 'atacc1'
533 -- should fail for @@ second one @@
534 insert into atacc1 (test2, test) values (3, 3);
535 insert into atacc1 (test2, test) values (2, 3);
536 ERROR:  Cannot insert a duplicate key into unique index atacc1_test_key
537 drop table atacc1;
538 -- test primary key constraint adding
539 create table atacc1 ( test int );
540 -- add a primary key constraint
541 alter table atacc1 add constraint atacc_test1 primary key (test);
542 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_test1' for table 'atacc1'
543 -- insert first value
544 insert into atacc1 (test) values (2);
545 -- should fail
546 insert into atacc1 (test) values (2);
547 ERROR:  Cannot insert a duplicate key into unique index atacc_test1
548 -- should succeed
549 insert into atacc1 (test) values (4);
550 -- inserting NULL should fail
551 insert into atacc1 (test) values(NULL);
552 ERROR:  ExecInsert: Fail to add null value in not null attribute test
553 -- try adding a second primary key (should fail)
554 alter table atacc1 add constraint atacc_oid1 primary key(oid);
555 ERROR:  ALTER TABLE / PRIMARY KEY multiple primary keys for table 'atacc1' are not allowed
556 -- drop first primary key constraint
557 alter table atacc1 drop constraint atacc_test1 restrict;
558 -- try adding a primary key on oid (should succeed)
559 alter table atacc1 add constraint atacc_oid1 primary key(oid);
560 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_oid1' for table 'atacc1'
561 drop table atacc1;
562 -- let's do one where the primary key constraint fails when added
563 create table atacc1 ( test int );
564 -- insert soon to be failing rows
565 insert into atacc1 (test) values (2);
566 insert into atacc1 (test) values (2);
567 -- add a primary key (fails)
568 alter table atacc1 add constraint atacc_test1 primary key (test);
569 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_test1' for table 'atacc1'
570 ERROR:  Cannot create unique index. Table contains non-unique values
571 insert into atacc1 (test) values (3);
572 drop table atacc1;
573 -- let's do another one where the primary key constraint fails when added
574 create table atacc1 ( test int );
575 -- insert soon to be failing row
576 insert into atacc1 (test) values (NULL);
577 -- add a primary key (fails)
578 alter table atacc1 add constraint atacc_test1 primary key (test);
579 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_test1' for table 'atacc1'
580 ERROR:  ALTER TABLE: Attribute "test" contains NULL values
581 insert into atacc1 (test) values (3);
582 drop table atacc1;
583 -- let's do one where the primary key constraint fails
584 -- because the column doesn't exist
585 create table atacc1 ( test int );
586 -- add a primary key constraint (fails)
587 alter table atacc1 add constraint atacc_test1 primary key (test1);
588 ERROR:  ALTER TABLE: column "test1" named in key does not exist
589 drop table atacc1;
590 -- something a little more complicated
591 create table atacc1 ( test int, test2 int);
592 -- add a primary key constraint
593 alter table atacc1 add constraint atacc_test1 primary key (test, test2);
594 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_test1' for table 'atacc1'
595 -- try adding a second primary key - should fail
596 alter table atacc1 add constraint atacc_test2 primary key (test);
597 ERROR:  ALTER TABLE / PRIMARY KEY multiple primary keys for table 'atacc1' are not allowed
598 -- insert initial value
599 insert into atacc1 (test,test2) values (4,4);
600 -- should fail
601 insert into atacc1 (test,test2) values (4,4);
602 ERROR:  Cannot insert a duplicate key into unique index atacc_test1
603 insert into atacc1 (test,test2) values (NULL,3);
604 ERROR:  ExecInsert: Fail to add null value in not null attribute test
605 insert into atacc1 (test,test2) values (3, NULL);
606 ERROR:  ExecInsert: Fail to add null value in not null attribute test2
607 insert into atacc1 (test,test2) values (NULL,NULL);
608 ERROR:  ExecInsert: Fail to add null value in not null attribute test
609 -- should all succeed
610 insert into atacc1 (test,test2) values (4,5);
611 insert into atacc1 (test,test2) values (5,4);
612 insert into atacc1 (test,test2) values (5,5);
613 drop table atacc1;
614 -- lets do some naming tests
615 create table atacc1 (test int, test2 int, primary key(test));
616 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'atacc1_pkey' for table 'atacc1'
617 -- only first should succeed
618 insert into atacc1 (test2, test) values (3, 3);
619 insert into atacc1 (test2, test) values (2, 3);
620 ERROR:  Cannot insert a duplicate key into unique index atacc1_pkey
621 insert into atacc1 (test2, test) values (1, NULL);
622 ERROR:  ExecInsert: Fail to add null value in not null attribute test
623 drop table atacc1;
624 -- alter table / alter column [set/drop] not null tests
625 -- try altering system catalogs, should fail
626 alter table pg_class alter column relname drop not null;
627 ERROR:  ALTER TABLE: relation "pg_class" is a system catalog
628 alter table pg_class alter relname set not null;
629 ERROR:  ALTER TABLE: relation "pg_class" is a system catalog
630 -- try altering non-existent table, should fail
631 alter table non_existent alter column bar set not null;
632 ERROR:  Relation "non_existent" does not exist
633 alter table non_existent alter column bar drop not null;
634 ERROR:  Relation "non_existent" does not exist
635 -- test setting columns to null and not null and vice versa
636 -- test checking for null values and primary key
637 create table atacc1 (test int not null);
638 alter table atacc1 add constraint "atacc1_pkey" primary key (test);
639 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc1_pkey' for table 'atacc1'
640 alter table atacc1 alter column test drop not null;
641 ERROR:  ALTER TABLE: Attribute "test" is in a primary key
642 alter table atacc1 drop constraint "atacc1_pkey";
643 alter table atacc1 alter column test drop not null;
644 insert into atacc1 values (null);
645 alter table atacc1 alter test set not null;
646 ERROR:  ALTER TABLE: Attribute "test" contains NULL values
647 delete from atacc1;
648 alter table atacc1 alter test set not null;
649 -- try altering a non-existent column, should fail
650 alter table atacc1 alter bar set not null;
651 ERROR:  Relation "atacc1" has no column "bar"
652 alter table atacc1 alter bar drop not null;
653 ERROR:  Relation "atacc1" has no column "bar"
654 -- try altering the oid column, should fail
655 alter table atacc1 alter oid set not null;
656 ERROR:  ALTER TABLE: Cannot alter system attribute "oid"
657 alter table atacc1 alter oid drop not null;
658 ERROR:  ALTER TABLE: Cannot alter system attribute "oid"
659 -- try creating a view and altering that, should fail
660 create view myview as select * from atacc1;
661 alter table myview alter column test drop not null;
662 ERROR:  ALTER TABLE: relation "myview" is not a table
663 alter table myview alter column test set not null;
664 ERROR:  ALTER TABLE: relation "myview" is not a table
665 drop view myview;
666 drop table atacc1;
667 -- test inheritance
668 create table parent (a int);
669 create table child (b varchar(255)) inherits (parent);
670 alter table parent alter a set not null;
671 insert into parent values (NULL);
672 ERROR:  ExecInsert: Fail to add null value in not null attribute a
673 insert into child (a, b) values (NULL, 'foo');
674 ERROR:  ExecInsert: Fail to add null value in not null attribute a
675 alter table parent alter a drop not null;
676 insert into parent values (NULL);
677 insert into child (a, b) values (NULL, 'foo');
678 alter table only parent alter a set not null;
679 ERROR:  ALTER TABLE: Attribute "a" contains NULL values
680 alter table child alter a set not null;
681 ERROR:  ALTER TABLE: Attribute "a" contains NULL values
682 delete from parent;
683 alter table only parent alter a set not null;
684 insert into parent values (NULL);
685 ERROR:  ExecInsert: Fail to add null value in not null attribute a
686 alter table child alter a set not null;
687 insert into child (a, b) values (NULL, 'foo');
688 ERROR:  ExecInsert: Fail to add null value in not null attribute a
689 delete from child;
690 alter table child alter a set not null;
691 insert into child (a, b) values (NULL, 'foo');
692 ERROR:  ExecInsert: Fail to add null value in not null attribute a
693 drop table child;
694 drop table parent;
695 -- test setting and removing default values
696 create table def_test (
697         c1      int4 default 5,
698         c2      text default 'initial_default'
699 );
700 insert into def_test default values;
701 alter table def_test alter column c1 drop default;
702 insert into def_test default values;
703 alter table def_test alter column c2 drop default;
704 insert into def_test default values;
705 alter table def_test alter column c1 set default 10;
706 alter table def_test alter column c2 set default 'new_default';
707 insert into def_test default values;
708 select * from def_test;
709  c1 |       c2        
710 ----+-----------------
711   5 | initial_default
712     | initial_default
713     | 
714  10 | new_default
715 (4 rows)
716
717 -- set defaults to an incorrect type: this should fail
718 alter table def_test alter column c1 set default 'wrong_datatype';
719 ERROR:  pg_atoi: error in "wrong_datatype": can't parse "wrong_datatype"
720 alter table def_test alter column c2 set default 20;
721 -- set defaults on a non-existent column: this should fail
722 alter table def_test alter column c3 set default 30;
723 ERROR:  Relation "def_test" has no column "c3"
724 -- set defaults on views: we need to create a view, add a rule
725 -- to allow insertions into it, and then alter the view to add
726 -- a default
727 create view def_view_test as select * from def_test;
728 create rule def_view_test_ins as
729         on insert to def_view_test
730         do instead insert into def_test select new.*;
731 insert into def_view_test default values;
732 alter table def_view_test alter column c1 set default 45;
733 insert into def_view_test default values;
734 alter table def_view_test alter column c2 set default 'view_default';
735 insert into def_view_test default values;
736 select * from def_view_test;
737  c1 |       c2        
738 ----+-----------------
739   5 | initial_default
740     | initial_default
741     | 
742  10 | new_default
743     | 
744  45 | 
745  45 | view_default
746 (7 rows)
747
748 drop rule def_view_test_ins on def_view_test;
749 drop view def_view_test;
750 drop table def_test;
751 -- alter table / drop column tests
752 -- try altering system catalogs, should fail
753 alter table pg_class drop column relname;
754 ERROR:  ALTER TABLE: relation "pg_class" is a system catalog
755 -- try altering non-existent table, should fail
756 alter table foo drop column bar;
757 ERROR:  Relation "foo" does not exist
758 -- test dropping columns
759 create table atacc1 (a int4 not null, b int4, c int4 not null, d int4);
760 insert into atacc1 values (1, 2, 3, 4);
761 alter table atacc1 drop a;
762 alter table atacc1 drop a;
763 ERROR:  Relation "atacc1" has no column "a"
764 -- SELECTs
765 select * from atacc1;
766  b | c | d 
767 ---+---+---
768  2 | 3 | 4
769 (1 row)
770
771 select * from atacc1 order by a;
772 ERROR:  Attribute "a" not found
773 select * from atacc1 order by "........pg.dropped.1........";
774 ERROR:  Attribute "........pg.dropped.1........" not found
775 select * from atacc1 group by a;
776 ERROR:  Attribute "a" not found
777 select * from atacc1 group by "........pg.dropped.1........";
778 ERROR:  Attribute "........pg.dropped.1........" not found
779 select atacc1.* from atacc1;
780  b | c | d 
781 ---+---+---
782  2 | 3 | 4
783 (1 row)
784
785 select a from atacc1;
786 ERROR:  Attribute "a" not found
787 select atacc1.a from atacc1;
788 ERROR:  No such attribute atacc1.a
789 select b,c,d from atacc1;
790  b | c | d 
791 ---+---+---
792  2 | 3 | 4
793 (1 row)
794
795 select a,b,c,d from atacc1;
796 ERROR:  Attribute "a" not found
797 select * from atacc1 where a = 1;
798 ERROR:  Attribute "a" not found
799 select "........pg.dropped.1........" from atacc1;
800 ERROR:  Attribute "........pg.dropped.1........" not found
801 select atacc1."........pg.dropped.1........" from atacc1;
802 ERROR:  No such attribute atacc1.........pg.dropped.1........
803 select "........pg.dropped.1........",b,c,d from atacc1;
804 ERROR:  Attribute "........pg.dropped.1........" not found
805 select * from atacc1 where "........pg.dropped.1........" = 1;
806 ERROR:  Attribute "........pg.dropped.1........" not found
807 -- UPDATEs
808 update atacc1 set a = 3;
809 ERROR:  Relation "atacc1" has no column "a"
810 update atacc1 set b = 2 where a = 3;
811 ERROR:  Attribute "a" not found
812 update atacc1 set "........pg.dropped.1........" = 3;
813 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
814 update atacc1 set b = 2 where "........pg.dropped.1........" = 3;
815 ERROR:  Attribute "........pg.dropped.1........" not found
816 -- INSERTs
817 insert into atacc1 values (10, 11, 12, 13);
818 ERROR:  INSERT has more expressions than target columns
819 insert into atacc1 values (default, 11, 12, 13);
820 ERROR:  INSERT has more expressions than target columns
821 insert into atacc1 values (11, 12, 13);
822 insert into atacc1 (a) values (10);
823 ERROR:  Relation "atacc1" has no column "a"
824 insert into atacc1 (a) values (default);
825 ERROR:  Relation "atacc1" has no column "a"
826 insert into atacc1 (a,b,c,d) values (10,11,12,13);
827 ERROR:  Relation "atacc1" has no column "a"
828 insert into atacc1 (a,b,c,d) values (default,11,12,13);
829 ERROR:  Relation "atacc1" has no column "a"
830 insert into atacc1 (b,c,d) values (11,12,13);
831 insert into atacc1 ("........pg.dropped.1........") values (10);
832 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
833 insert into atacc1 ("........pg.dropped.1........") values (default);
834 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
835 insert into atacc1 ("........pg.dropped.1........",b,c,d) values (10,11,12,13);
836 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
837 insert into atacc1 ("........pg.dropped.1........",b,c,d) values (default,11,12,13);
838 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
839 -- DELETEs
840 delete from atacc1 where a = 3;
841 ERROR:  Attribute "a" not found
842 delete from atacc1 where "........pg.dropped.1........" = 3;
843 ERROR:  Attribute "........pg.dropped.1........" not found
844 delete from atacc1;
845 -- try dropping a non-existent column, should fail
846 alter table atacc1 drop bar;
847 ERROR:  Relation "atacc1" has no column "bar"
848 -- try dropping the oid column, should fail
849 alter table atacc1 drop oid;
850 ERROR:  ALTER TABLE: Cannot drop system attribute "oid"
851 -- try creating a view and altering that, should fail
852 create view myview as select * from atacc1;
853 select * from myview;
854  b | c | d 
855 ---+---+---
856 (0 rows)
857
858 alter table myview drop d;
859 ERROR:  ALTER TABLE: relation "myview" is not a table
860 drop view myview;
861 -- test some commands to make sure they fail on the dropped column
862 analyze atacc1(a);
863 ERROR:  Relation "atacc1" has no column "a"
864 analyze atacc1("........pg.dropped.1........");
865 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
866 vacuum analyze atacc1(a);
867 ERROR:  Relation "atacc1" has no column "a"
868 vacuum analyze atacc1("........pg.dropped.1........");
869 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
870 comment on column atacc1.a is 'testing';
871 ERROR:  Relation "atacc1" has no column "a"
872 comment on column atacc1."........pg.dropped.1........" is 'testing';
873 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
874 alter table atacc1 alter a set storage plain;
875 ERROR:  ALTER TABLE: relation "atacc1" has no column "a"
876 alter table atacc1 alter "........pg.dropped.1........" set storage plain;
877 ERROR:  ALTER TABLE: relation "atacc1" has no column "........pg.dropped.1........"
878 alter table atacc1 alter a set statistics 0;
879 ERROR:  ALTER TABLE: relation "atacc1" has no column "a"
880 alter table atacc1 alter "........pg.dropped.1........" set statistics 0;
881 ERROR:  ALTER TABLE: relation "atacc1" has no column "........pg.dropped.1........"
882 alter table atacc1 alter a set default 3;
883 ERROR:  Relation "atacc1" has no column "a"
884 alter table atacc1 alter "........pg.dropped.1........" set default 3;
885 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
886 alter table atacc1 alter a drop default;
887 ERROR:  Relation "atacc1" has no column "a"
888 alter table atacc1 alter "........pg.dropped.1........" drop default;
889 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
890 alter table atacc1 alter a set not null;
891 ERROR:  Relation "atacc1" has no column "a"
892 alter table atacc1 alter "........pg.dropped.1........" set not null;
893 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
894 alter table atacc1 alter a drop not null;
895 ERROR:  Relation "atacc1" has no column "a"
896 alter table atacc1 alter "........pg.dropped.1........" drop not null;
897 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
898 alter table atacc1 rename a to x;
899 ERROR:  renameatt: attribute "a" does not exist
900 alter table atacc1 rename "........pg.dropped.1........" to x;
901 ERROR:  renameatt: attribute "........pg.dropped.1........" does not exist
902 alter table atacc1 add primary key(a);
903 ERROR:  ALTER TABLE: column "a" named in key does not exist
904 alter table atacc1 add primary key("........pg.dropped.1........");
905 ERROR:  ALTER TABLE: column "........pg.dropped.1........" named in key does not exist
906 alter table atacc1 add unique(a);
907 ERROR:  ALTER TABLE: column "a" named in key does not exist
908 alter table atacc1 add unique("........pg.dropped.1........");
909 ERROR:  ALTER TABLE: column "........pg.dropped.1........" named in key does not exist
910 alter table atacc1 add check (a > 3);
911 ERROR:  Attribute "a" not found
912 alter table atacc1 add check ("........pg.dropped.1........" > 3);
913 ERROR:  Attribute "........pg.dropped.1........" not found
914 create table atacc2 (id int4 unique);
915 NOTICE:  CREATE TABLE / UNIQUE will create implicit index 'atacc2_id_key' for table 'atacc2'
916 alter table atacc1 add foreign key (a) references atacc2(id);
917 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
918 ERROR:  ALTER TABLE: column "a" referenced in foreign key constraint does not exist
919 alter table atacc1 add foreign key ("........pg.dropped.1........") references atacc2(id);
920 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
921 ERROR:  ALTER TABLE: column "........pg.dropped.1........" referenced in foreign key constraint does not exist
922 alter table atacc2 add foreign key (id) references atacc1(a);
923 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
924 ERROR:  ALTER TABLE: column "a" referenced in foreign key constraint does not exist
925 alter table atacc2 add foreign key (id) references atacc1("........pg.dropped.1........");
926 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
927 ERROR:  ALTER TABLE: column "........pg.dropped.1........" referenced in foreign key constraint does not exist
928 drop table atacc2;
929 create index "testing_idx" on atacc1(a);
930 ERROR:  DefineIndex: attribute "a" not found
931 create index "testing_idx" on atacc1("........pg.dropped.1........");
932 ERROR:  DefineIndex: attribute "........pg.dropped.1........" not found
933 -- test create as and select into
934 insert into atacc1 values (21, 22, 23);
935 create table test1 as select * from atacc1;
936 select * from test1;
937  b  | c  | d  
938 ----+----+----
939  21 | 22 | 23
940 (1 row)
941
942 drop table test1;
943 select * into test2 from atacc1;
944 select * from test2;
945  b  | c  | d  
946 ----+----+----
947  21 | 22 | 23
948 (1 row)
949
950 drop table test2;
951 -- try dropping all columns
952 alter table atacc1 drop c;
953 alter table atacc1 drop d;
954 alter table atacc1 drop b;
955 select * from atacc1;
956   
957 --
958 (1 row)
959
960 drop table atacc1;
961 -- test inheritance
962 create table parent (a int, b int, c int);
963 insert into parent values (1, 2, 3);
964 alter table parent drop a;
965 create table child (d varchar(255)) inherits (parent);
966 insert into child values (12, 13, 'testing');
967 select * from parent;
968  b  | c  
969 ----+----
970   2 |  3
971  12 | 13
972 (2 rows)
973
974 select * from child;
975  b  | c  |    d    
976 ----+----+---------
977  12 | 13 | testing
978 (1 row)
979
980 alter table parent drop c;
981 select * from parent;
982  b  
983 ----
984   2
985  12
986 (2 rows)
987
988 select * from child;
989  b  |    d    
990 ----+---------
991  12 | testing
992 (1 row)
993
994 drop table child;
995 drop table parent;
996 -- test copy in/out
997 create table test (a int4, b int4, c int4);
998 insert into test values (1,2,3);
999 alter table test drop a;
1000 copy test to stdout;
1001 2       3
1002 copy test(a) to stdout;
1003 ERROR:  Relation "test" has no column "a"
1004 copy test("........pg.dropped.1........") to stdout;
1005 ERROR:  Relation "test" has no column "........pg.dropped.1........"
1006 copy test from stdin;
1007 ERROR:  copy: line 1, Extra data after last expected column
1008 lost synchronization with server, resetting connection
1009 SET autocommit TO 'on';
1010 select * from test;
1011  b | c 
1012 ---+---
1013  2 | 3
1014 (1 row)
1015
1016 copy test from stdin;
1017 select * from test;
1018  b  | c  
1019 ----+----
1020   2 |  3
1021  21 | 22
1022 (2 rows)
1023
1024 copy test(a) from stdin;
1025 ERROR:  Relation "test" has no column "a"
1026 copy test("........pg.dropped.1........") from stdin;
1027 ERROR:  Relation "test" has no column "........pg.dropped.1........"
1028 copy test(b,c) from stdin;
1029 select * from test;
1030  b  | c  
1031 ----+----
1032   2 |  3
1033  21 | 22
1034  31 | 32
1035 (3 rows)
1036
1037 drop table test;
1038 -- test inheritance
1039 create table dropColumn (a int, b int, e int);
1040 create table dropColumnChild (c int) inherits (dropColumn);
1041 create table dropColumnAnother (d int) inherits (dropColumnChild);
1042 -- these two should fail
1043 alter table dropColumnchild drop column a;
1044 ERROR:  ALTER TABLE: Cannot drop inherited column "a"
1045 alter table only dropColumnChild drop column b;
1046 ERROR:  ALTER TABLE: Cannot drop inherited column "b"
1047 -- these three should work
1048 alter table only dropColumn drop column e;
1049 alter table dropColumnChild drop column c;
1050 alter table dropColumn drop column a;
1051 create table renameColumn (a int);
1052 create table renameColumnChild (b int) inherits (renameColumn);
1053 create table renameColumnAnother (c int) inherits (renameColumnChild);
1054 -- these three should fail
1055 alter table renameColumnChild rename column a to d;
1056 ERROR:  renameatt: inherited attribute "a" may not be renamed
1057 alter table only renameColumnChild rename column a to d;
1058 ERROR:  Inherited attribute "a" must be renamed in child tables too
1059 alter table only renameColumn rename column a to d;
1060 ERROR:  Inherited attribute "a" must be renamed in child tables too
1061 -- these should work
1062 alter table renameColumn rename column a to d;
1063 alter table renameColumnChild rename column b to a;
1064 -- this should work
1065 alter table renameColumn add column w int;
1066 -- this should fail
1067 alter table only renameColumn add column x int;
1068 ERROR:  Attribute must be added to child tables too
1069 -- Test corner cases in dropping of inherited columns
1070 create table p1 (f1 int, f2 int);
1071 create table c1 (f1 int not null) inherits(p1);
1072 NOTICE:  CREATE TABLE: merging attribute "f1" with inherited definition
1073 -- should be rejected since c1.f1 is inherited
1074 alter table c1 drop column f1;
1075 ERROR:  ALTER TABLE: Cannot drop inherited column "f1"
1076 -- should work
1077 alter table p1 drop column f1;
1078 -- c1.f1 is still there, but no longer inherited
1079 select f1 from c1;
1080  f1 
1081 ----
1082 (0 rows)
1083
1084 alter table c1 drop column f1;
1085 select f1 from c1;
1086 ERROR:  Attribute "f1" not found
1087 drop table p1 cascade;
1088 NOTICE:  Drop cascades to table c1
1089 create table p1 (f1 int, f2 int);
1090 create table c1 () inherits(p1);
1091 -- should be rejected since c1.f1 is inherited
1092 alter table c1 drop column f1;
1093 ERROR:  ALTER TABLE: Cannot drop inherited column "f1"
1094 alter table p1 drop column f1;
1095 -- c1.f1 is dropped now, since there is no local definition for it
1096 select f1 from c1;
1097 ERROR:  Attribute "f1" not found
1098 drop table p1 cascade;
1099 NOTICE:  Drop cascades to table c1
1100 create table p1 (f1 int, f2 int);
1101 create table c1 () inherits(p1);
1102 -- should be rejected since c1.f1 is inherited
1103 alter table c1 drop column f1;
1104 ERROR:  ALTER TABLE: Cannot drop inherited column "f1"
1105 alter table only p1 drop column f1;
1106 -- c1.f1 is NOT dropped, but must now be considered non-inherited
1107 alter table c1 drop column f1;
1108 drop table p1 cascade;
1109 NOTICE:  Drop cascades to table c1
1110 create table p1 (f1 int, f2 int);
1111 create table c1 (f1 int not null) inherits(p1);
1112 NOTICE:  CREATE TABLE: merging attribute "f1" with inherited definition
1113 -- should be rejected since c1.f1 is inherited
1114 alter table c1 drop column f1;
1115 ERROR:  ALTER TABLE: Cannot drop inherited column "f1"
1116 alter table only p1 drop column f1;
1117 -- c1.f1 is still there, but no longer inherited
1118 alter table c1 drop column f1;
1119 drop table p1 cascade;
1120 NOTICE:  Drop cascades to table c1
1121 create table p1(id int, name text);
1122 create table p2(id2 int, name text, height int);
1123 create table c1(age int) inherits(p1,p2);
1124 NOTICE:  CREATE TABLE: merging multiple inherited definitions of attribute "name"
1125 create table gc1() inherits (c1);
1126 select relname, attname, attinhcount, attislocal
1127 from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
1128 where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
1129 order by relname, attnum;
1130  relname | attname | attinhcount | attislocal 
1131 ---------+---------+-------------+------------
1132  c1      | id      |           1 | f
1133  c1      | name    |           2 | f
1134  c1      | id2     |           1 | f
1135  c1      | height  |           1 | f
1136  c1      | age     |           0 | t
1137  gc1     | id      |           1 | f
1138  gc1     | name    |           1 | f
1139  gc1     | id2     |           1 | f
1140  gc1     | height  |           1 | f
1141  gc1     | age     |           1 | f
1142  p1      | id      |           0 | t
1143  p1      | name    |           0 | t
1144  p2      | id2     |           0 | t
1145  p2      | name    |           0 | t
1146  p2      | height  |           0 | t
1147 (15 rows)
1148
1149 -- should work
1150 alter table only p1 drop column name;
1151 -- should work. Now c1.name is local and inhcount is 0.
1152 alter table p2 drop column name;
1153 -- should be rejected since its inherited
1154 alter table gc1 drop column name;
1155 ERROR:  ALTER TABLE: Cannot drop inherited column "name"
1156 -- should work, and drop gc1.name along
1157 alter table c1 drop column name;
1158 -- should fail: column does not exist
1159 alter table gc1 drop column name;
1160 ERROR:  Relation "gc1" has no column "name"
1161 -- should work and drop the attribute in all tables
1162 alter table p2 drop column height;
1163 select relname, attname, attinhcount, attislocal
1164 from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
1165 where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
1166 order by relname, attnum;
1167  relname | attname | attinhcount | attislocal 
1168 ---------+---------+-------------+------------
1169  c1      | id      |           1 | f
1170  c1      | id2     |           1 | f
1171  c1      | age     |           0 | t
1172  gc1     | id      |           1 | f
1173  gc1     | id2     |           1 | f
1174  gc1     | age     |           1 | f
1175  p1      | id      |           0 | t
1176  p2      | id2     |           0 | t
1177 (8 rows)
1178
1179 drop table p1, p2 cascade;
1180 NOTICE:  Drop cascades to table c1
1181 NOTICE:  Drop cascades to table gc1
1182 --
1183 -- Test the ALTER TABLE WITHOUT OIDS command
1184 --
1185 create table altstartwith (col integer) with oids;
1186 insert into altstartwith values (1);
1187 select oid > 0, * from altstartwith;
1188  ?column? | col 
1189 ----------+-----
1190  t        |   1
1191 (1 row)
1192
1193 alter table altstartwith set without oids;
1194 select oid > 0, * from altstartwith; -- fails
1195 ERROR:  Attribute "oid" not found
1196 select * from altstartwith;
1197  col 
1198 -----
1199    1
1200 (1 row)
1201
1202 -- Run inheritance tests
1203 create table altwithoid (col integer) with oids;
1204 -- Inherits parents oid column
1205 create table altinhoid () inherits (altwithoid) without oids;
1206 insert into altinhoid values (1);
1207 select oid > 0, * from altwithoid;
1208  ?column? | col 
1209 ----------+-----
1210  t        |   1
1211 (1 row)
1212
1213 select oid > 0, * from altinhoid;
1214  ?column? | col 
1215 ----------+-----
1216  t        |   1
1217 (1 row)
1218
1219 alter table altwithoid set without oids;
1220 alter table altinhoid set without oids; -- fails
1221 ERROR:  ALTER TABLE: Table is already WITHOUT OIDS
1222 select oid > 0, * from altwithoid; -- fails
1223 ERROR:  Attribute "oid" not found
1224 select oid > 0, * from altinhoid; -- fails
1225 ERROR:  Attribute "oid" not found
1226 select * from altwithoid;
1227  col 
1228 -----
1229    1
1230 (1 row)
1231
1232 select * from altinhoid;
1233  col 
1234 -----
1235    1
1236 (1 row)
1237
1238 -- test renumbering of child-table columns in inherited operations
1239 create table p1 (f1 int);
1240 create table c1 (f2 text, f3 int) inherits (p1);
1241 alter table p1 add column a1 int check (a1 > 0);
1242 alter table p1 add column f2 text;
1243 NOTICE:  ALTER TABLE: merging definition of column "f2" for child c1
1244 insert into p1 values (1,2,'abc');
1245 insert into c1 values(11,'xyz',33,0); -- should fail
1246 ERROR:  ExecInsert: rejected due to CHECK constraint "p1_a1" on "c1"
1247 insert into c1 values(11,'xyz',33,22);
1248 select * from p1;
1249  f1 | a1 | f2  
1250 ----+----+-----
1251   1 |  2 | abc
1252  11 | 22 | xyz
1253 (2 rows)
1254
1255 update p1 set a1 = a1 + 1, f2 = upper(f2);
1256 select * from p1;
1257  f1 | a1 | f2  
1258 ----+----+-----
1259   1 |  3 | ABC
1260  11 | 23 | XYZ
1261 (2 rows)
1262
1263 drop table p1 cascade;
1264 NOTICE:  Drop cascades to table c1
1265 NOTICE:  Drop cascades to constraint p1_a1 on table c1