]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/alter_table.out
655a4174168f4350e22c9053612428c1839406dd
[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 -- toast-like relation name
290 alter table stud_emp rename to pg_toast_stud_emp;
291 alter table pg_toast_stud_emp rename to stud_emp;
292 -- FOREIGN KEY CONSTRAINT adding TEST
293 CREATE TABLE tmp2 (a int primary key);
294 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'tmp2_pkey' for table 'tmp2'
295 CREATE TABLE tmp3 (a int, b int);
296 CREATE TABLE tmp4 (a int, b int, unique(a,b));
297 NOTICE:  CREATE TABLE / UNIQUE will create implicit index 'tmp4_a_key' for table 'tmp4'
298 CREATE TABLE tmp5 (a int, b int);
299 -- Insert rows into tmp2 (pktable)
300 INSERT INTO tmp2 values (1);
301 INSERT INTO tmp2 values (2);
302 INSERT INTO tmp2 values (3);
303 INSERT INTO tmp2 values (4);
304 -- Insert rows into tmp3
305 INSERT INTO tmp3 values (1,10);
306 INSERT INTO tmp3 values (1,20);
307 INSERT INTO tmp3 values (5,50);
308 -- Try (and fail) to add constraint due to invalid source columns
309 ALTER TABLE tmp3 add constraint tmpconstr foreign key(c) references tmp2 match full;
310 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
311 ERROR:  ALTER TABLE: column "c" referenced in foreign key constraint does not exist
312 -- Try (and fail) to add constraint due to invalide destination columns explicitly given
313 ALTER TABLE tmp3 add constraint tmpconstr foreign key(a) references tmp2(b) match full;
314 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
315 ERROR:  ALTER TABLE: column "b" referenced in foreign key constraint does not exist
316 -- Try (and fail) to add constraint due to invalid data
317 ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;
318 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
319 ERROR:  tmpconstr referential integrity violation - key (a)=(5) referenced from tmp3 not found in tmp2
320 -- Delete failing row
321 DELETE FROM tmp3 where a=5;
322 -- Try (and succeed)
323 ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;
324 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
325 -- Try (and fail) to create constraint from tmp5(a) to tmp4(a) - unique constraint on
326 -- tmp4 is a,b
327 ALTER TABLE tmp5 add constraint tmpconstr foreign key(a) references tmp4(a) match full;
328 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
329 ERROR:  UNIQUE constraint matching given keys for referenced table "tmp4" not found
330 DROP TABLE tmp5;
331 DROP TABLE tmp4;
332 DROP TABLE tmp3;
333 DROP TABLE tmp2;
334 -- Foreign key adding test with mixed types
335 -- Note: these tables are TEMP to avoid name conflicts when this test
336 -- is run in parallel with foreign_key.sql.
337 CREATE TEMP TABLE PKTABLE (ptest1 int PRIMARY KEY);
338 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
339 CREATE TEMP TABLE FKTABLE (ftest1 inet);
340 -- This next should fail, because inet=int does not exist
341 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
342 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
343 ERROR:  operator does not exist: inet = integer
344 HINT:  No operator matches the given name and argument type(s). You may need to add explicit typecasts.
345 -- This should also fail for the same reason, but here we
346 -- give the column name
347 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable(ptest1);
348 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
349 ERROR:  operator does not exist: inet = integer
350 HINT:  No operator matches the given name and argument type(s). You may need to add explicit typecasts.
351 -- This should succeed, even though they are different types
352 -- because varchar=int does exist
353 DROP TABLE FKTABLE;
354 CREATE TEMP TABLE FKTABLE (ftest1 varchar);
355 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
356 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
357 -- As should this
358 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable(ptest1);
359 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
360 DROP TABLE pktable cascade;
361 NOTICE:  Drop cascades to constraint $2 on table fktable
362 NOTICE:  Drop cascades to constraint $1 on table fktable
363 DROP TABLE fktable;
364 CREATE TEMP TABLE PKTABLE (ptest1 int, ptest2 inet,
365                            PRIMARY KEY(ptest1, ptest2));
366 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
367 -- This should fail, because we just chose really odd types
368 CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);
369 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2) references pktable;
370 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
371 ERROR:  operator does not exist: cidr = integer
372 HINT:  No operator matches the given name and argument type(s). You may need to add explicit typecasts.
373 DROP TABLE FKTABLE;
374 -- Again, so should this...
375 CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);
376 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
377      references pktable(ptest1, ptest2);
378 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
379 ERROR:  operator does not exist: cidr = integer
380 HINT:  No operator matches the given name and argument type(s). You may need to add explicit typecasts.
381 DROP TABLE FKTABLE;
382 -- This fails because we mixed up the column ordering
383 CREATE TEMP TABLE FKTABLE (ftest1 int, ftest2 inet);
384 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
385      references pktable(ptest2, ptest1);
386 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
387 ERROR:  operator does not exist: integer = inet
388 HINT:  No operator matches the given name and argument type(s). You may need to add explicit typecasts.
389 -- As does this...
390 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest2, ftest1)
391      references pktable(ptest1, ptest2);
392 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
393 ERROR:  operator does not exist: inet = integer
394 HINT:  No operator matches the given name and argument type(s). You may need to add explicit typecasts.
395 -- temp tables should go away by themselves, need not drop them.
396 -- test check constraint adding
397 create table atacc1 ( test int );
398 -- add a check constraint
399 alter table atacc1 add constraint atacc_test1 check (test>3);
400 -- should fail
401 insert into atacc1 (test) values (2);
402 ERROR:  ExecInsert: rejected due to CHECK constraint "atacc_test1" on "atacc1"
403 -- should succeed
404 insert into atacc1 (test) values (4);
405 drop table atacc1;
406 -- let's do one where the check fails when added
407 create table atacc1 ( test int );
408 -- insert a soon to be failing row
409 insert into atacc1 (test) values (2);
410 -- add a check constraint (fails)
411 alter table atacc1 add constraint atacc_test1 check (test>3);
412 ERROR:  AlterTableAddConstraint: rejected due to CHECK constraint atacc_test1
413 insert into atacc1 (test) values (4);
414 drop table atacc1;
415 -- let's do one where the check fails because the column doesn't exist
416 create table atacc1 ( test int );
417 -- add a check constraint (fails)
418 alter table atacc1 add constraint atacc_test1 check (test1>3);
419 ERROR:  Attribute "test1" not found
420 drop table atacc1;
421 -- something a little more complicated
422 create table atacc1 ( test int, test2 int, test3 int);
423 -- add a check constraint (fails)
424 alter table atacc1 add constraint atacc_test1 check (test+test2<test3*4);
425 -- should fail
426 insert into atacc1 (test,test2,test3) values (4,4,2);
427 ERROR:  ExecInsert: rejected due to CHECK constraint "atacc_test1" on "atacc1"
428 -- should succeed
429 insert into atacc1 (test,test2,test3) values (4,4,5);
430 drop table atacc1;
431 -- lets do some naming tests
432 create table atacc1 (test int check (test>3), test2 int);
433 alter table atacc1 add check (test2>test);
434 -- should fail for $2
435 insert into atacc1 (test2, test) values (3, 4);
436 ERROR:  ExecInsert: rejected due to CHECK constraint "$1" on "atacc1"
437 drop table atacc1;
438 -- inheritance related tests
439 create table atacc1 (test int);
440 create table atacc2 (test2 int);
441 create table atacc3 (test3 int) inherits (atacc1, atacc2);
442 alter table atacc2 add constraint foo check (test2>0);
443 -- fail and then succeed on atacc2
444 insert into atacc2 (test2) values (-3);
445 ERROR:  ExecInsert: rejected due to CHECK constraint "foo" on "atacc2"
446 insert into atacc2 (test2) values (3);
447 -- fail and then succeed on atacc3
448 insert into atacc3 (test2) values (-3);
449 ERROR:  ExecInsert: rejected due to CHECK constraint "foo" on "atacc3"
450 insert into atacc3 (test2) values (3);
451 drop table atacc3;
452 drop table atacc2;
453 drop table atacc1;
454 -- let's try only to add only to the parent
455 create table atacc1 (test int);
456 create table atacc2 (test2 int);
457 create table atacc3 (test3 int) inherits (atacc1, atacc2);
458 alter table only atacc2 add constraint foo check (test2>0);
459 -- fail and then succeed on atacc2
460 insert into atacc2 (test2) values (-3);
461 ERROR:  ExecInsert: rejected due to CHECK constraint "foo" on "atacc2"
462 insert into atacc2 (test2) values (3);
463 -- both succeed on atacc3
464 insert into atacc3 (test2) values (-3);
465 insert into atacc3 (test2) values (3);
466 drop table atacc3;
467 drop table atacc2;
468 drop table atacc1;
469 -- test unique constraint adding
470 create table atacc1 ( test int );
471 -- add a unique constraint
472 alter table atacc1 add constraint atacc_test1 unique (test);
473 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
474 -- insert first value
475 insert into atacc1 (test) values (2);
476 -- should fail
477 insert into atacc1 (test) values (2);
478 ERROR:  Cannot insert a duplicate key into unique index atacc_test1
479 -- should succeed
480 insert into atacc1 (test) values (4);
481 -- try adding a unique oid constraint
482 alter table atacc1 add constraint atacc_oid1 unique(oid);
483 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_oid1' for table 'atacc1'
484 drop table atacc1;
485 -- let's do one where the unique constraint fails when added
486 create table atacc1 ( test int );
487 -- insert soon to be failing rows
488 insert into atacc1 (test) values (2);
489 insert into atacc1 (test) values (2);
490 -- add a unique constraint (fails)
491 alter table atacc1 add constraint atacc_test1 unique (test);
492 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
493 ERROR:  Cannot create unique index. Table contains non-unique values
494 insert into atacc1 (test) values (3);
495 drop table atacc1;
496 -- let's do one where the unique constraint fails
497 -- because the column doesn't exist
498 create table atacc1 ( test int );
499 -- add a unique constraint (fails)
500 alter table atacc1 add constraint atacc_test1 unique (test1);
501 ERROR:  ALTER TABLE: column "test1" named in key does not exist
502 drop table atacc1;
503 -- something a little more complicated
504 create table atacc1 ( test int, test2 int);
505 -- add a unique constraint
506 alter table atacc1 add constraint atacc_test1 unique (test, test2);
507 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
508 -- insert initial value
509 insert into atacc1 (test,test2) values (4,4);
510 -- should fail
511 insert into atacc1 (test,test2) values (4,4);
512 ERROR:  Cannot insert a duplicate key into unique index atacc_test1
513 -- should all succeed
514 insert into atacc1 (test,test2) values (4,5);
515 insert into atacc1 (test,test2) values (5,4);
516 insert into atacc1 (test,test2) values (5,5);
517 drop table atacc1;
518 -- lets do some naming tests
519 create table atacc1 (test int, test2 int, unique(test));
520 NOTICE:  CREATE TABLE / UNIQUE will create implicit index 'atacc1_test_key' for table 'atacc1'
521 alter table atacc1 add unique (test2);
522 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc1_test2_key' for table 'atacc1'
523 -- should fail for @@ second one @@
524 insert into atacc1 (test2, test) values (3, 3);
525 insert into atacc1 (test2, test) values (2, 3);
526 ERROR:  Cannot insert a duplicate key into unique index atacc1_test_key
527 drop table atacc1;
528 -- test primary key constraint adding
529 create table atacc1 ( test int );
530 -- add a primary key constraint
531 alter table atacc1 add constraint atacc_test1 primary key (test);
532 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_test1' for table 'atacc1'
533 -- insert first value
534 insert into atacc1 (test) values (2);
535 -- should fail
536 insert into atacc1 (test) values (2);
537 ERROR:  Cannot insert a duplicate key into unique index atacc_test1
538 -- should succeed
539 insert into atacc1 (test) values (4);
540 -- inserting NULL should fail
541 insert into atacc1 (test) values(NULL);
542 ERROR:  ExecInsert: Fail to add null value in not null attribute test
543 -- try adding a second primary key (should fail)
544 alter table atacc1 add constraint atacc_oid1 primary key(oid);
545 ERROR:  ALTER TABLE / PRIMARY KEY multiple primary keys for table 'atacc1' are not allowed
546 -- drop first primary key constraint
547 alter table atacc1 drop constraint atacc_test1 restrict;
548 -- try adding a primary key on oid (should succeed)
549 alter table atacc1 add constraint atacc_oid1 primary key(oid);
550 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_oid1' for table 'atacc1'
551 drop table atacc1;
552 -- let's do one where the primary key constraint fails when added
553 create table atacc1 ( test int );
554 -- insert soon to be failing rows
555 insert into atacc1 (test) values (2);
556 insert into atacc1 (test) values (2);
557 -- add a primary key (fails)
558 alter table atacc1 add constraint atacc_test1 primary key (test);
559 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_test1' for table 'atacc1'
560 ERROR:  Cannot create unique index. Table contains non-unique values
561 insert into atacc1 (test) values (3);
562 drop table atacc1;
563 -- let's do another one where the primary key constraint fails when added
564 create table atacc1 ( test int );
565 -- insert soon to be failing row
566 insert into atacc1 (test) values (NULL);
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:  ALTER TABLE: Attribute "test" contains NULL values
571 insert into atacc1 (test) values (3);
572 drop table atacc1;
573 -- let's do one where the primary key constraint fails
574 -- because the column doesn't exist
575 create table atacc1 ( test int );
576 -- add a primary key constraint (fails)
577 alter table atacc1 add constraint atacc_test1 primary key (test1);
578 ERROR:  ALTER TABLE: column "test1" named in key does not exist
579 drop table atacc1;
580 -- something a little more complicated
581 create table atacc1 ( test int, test2 int);
582 -- add a primary key constraint
583 alter table atacc1 add constraint atacc_test1 primary key (test, test2);
584 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_test1' for table 'atacc1'
585 -- try adding a second primary key - should fail
586 alter table atacc1 add constraint atacc_test2 primary key (test);
587 ERROR:  ALTER TABLE / PRIMARY KEY multiple primary keys for table 'atacc1' are not allowed
588 -- insert initial value
589 insert into atacc1 (test,test2) values (4,4);
590 -- should fail
591 insert into atacc1 (test,test2) values (4,4);
592 ERROR:  Cannot insert a duplicate key into unique index atacc_test1
593 insert into atacc1 (test,test2) values (NULL,3);
594 ERROR:  ExecInsert: Fail to add null value in not null attribute test
595 insert into atacc1 (test,test2) values (3, NULL);
596 ERROR:  ExecInsert: Fail to add null value in not null attribute test2
597 insert into atacc1 (test,test2) values (NULL,NULL);
598 ERROR:  ExecInsert: Fail to add null value in not null attribute test
599 -- should all succeed
600 insert into atacc1 (test,test2) values (4,5);
601 insert into atacc1 (test,test2) values (5,4);
602 insert into atacc1 (test,test2) values (5,5);
603 drop table atacc1;
604 -- lets do some naming tests
605 create table atacc1 (test int, test2 int, primary key(test));
606 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'atacc1_pkey' for table 'atacc1'
607 -- only first should succeed
608 insert into atacc1 (test2, test) values (3, 3);
609 insert into atacc1 (test2, test) values (2, 3);
610 ERROR:  Cannot insert a duplicate key into unique index atacc1_pkey
611 insert into atacc1 (test2, test) values (1, NULL);
612 ERROR:  ExecInsert: Fail to add null value in not null attribute test
613 drop table atacc1;
614 -- alter table / alter column [set/drop] not null tests
615 -- try altering system catalogs, should fail
616 alter table pg_class alter column relname drop not null;
617 ERROR:  ALTER TABLE: relation "pg_class" is a system catalog
618 alter table pg_class alter relname set not null;
619 ERROR:  ALTER TABLE: relation "pg_class" is a system catalog
620 -- try altering non-existent table, should fail
621 alter table non_existent alter column bar set not null;
622 ERROR:  Relation "non_existent" does not exist
623 alter table non_existent alter column bar drop not null;
624 ERROR:  Relation "non_existent" does not exist
625 -- test setting columns to null and not null and vice versa
626 -- test checking for null values and primary key
627 create table atacc1 (test int not null);
628 alter table atacc1 add constraint "atacc1_pkey" primary key (test);
629 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc1_pkey' for table 'atacc1'
630 alter table atacc1 alter column test drop not null;
631 ERROR:  ALTER TABLE: Attribute "test" is in a primary key
632 alter table atacc1 drop constraint "atacc1_pkey";
633 alter table atacc1 alter column test drop not null;
634 insert into atacc1 values (null);
635 alter table atacc1 alter test set not null;
636 ERROR:  ALTER TABLE: Attribute "test" contains NULL values
637 delete from atacc1;
638 alter table atacc1 alter test set not null;
639 -- try altering a non-existent column, should fail
640 alter table atacc1 alter bar set not null;
641 ERROR:  Relation "atacc1" has no column "bar"
642 alter table atacc1 alter bar drop not null;
643 ERROR:  Relation "atacc1" has no column "bar"
644 -- try altering the oid column, should fail
645 alter table atacc1 alter oid set not null;
646 ERROR:  ALTER TABLE: Cannot alter system attribute "oid"
647 alter table atacc1 alter oid drop not null;
648 ERROR:  ALTER TABLE: Cannot alter system attribute "oid"
649 -- try creating a view and altering that, should fail
650 create view myview as select * from atacc1;
651 alter table myview alter column test drop not null;
652 ERROR:  ALTER TABLE: relation "myview" is not a table
653 alter table myview alter column test set not null;
654 ERROR:  ALTER TABLE: relation "myview" is not a table
655 drop view myview;
656 drop table atacc1;
657 -- test inheritance
658 create table parent (a int);
659 create table child (b varchar(255)) inherits (parent);
660 alter table parent alter a set not null;
661 insert into parent values (NULL);
662 ERROR:  ExecInsert: Fail to add null value in not null attribute a
663 insert into child (a, b) values (NULL, 'foo');
664 ERROR:  ExecInsert: Fail to add null value in not null attribute a
665 alter table parent alter a drop not null;
666 insert into parent values (NULL);
667 insert into child (a, b) values (NULL, 'foo');
668 alter table only parent alter a set not null;
669 ERROR:  ALTER TABLE: Attribute "a" contains NULL values
670 alter table child alter a set not null;
671 ERROR:  ALTER TABLE: Attribute "a" contains NULL values
672 delete from parent;
673 alter table only parent alter a set not null;
674 insert into parent values (NULL);
675 ERROR:  ExecInsert: Fail to add null value in not null attribute a
676 alter table child alter a set not null;
677 insert into child (a, b) values (NULL, 'foo');
678 ERROR:  ExecInsert: Fail to add null value in not null attribute a
679 delete from child;
680 alter table child alter a set not null;
681 insert into child (a, b) values (NULL, 'foo');
682 ERROR:  ExecInsert: Fail to add null value in not null attribute a
683 drop table child;
684 drop table parent;
685 -- test setting and removing default values
686 create table def_test (
687         c1      int4 default 5,
688         c2      text default 'initial_default'
689 );
690 insert into def_test default values;
691 alter table def_test alter column c1 drop default;
692 insert into def_test default values;
693 alter table def_test alter column c2 drop default;
694 insert into def_test default values;
695 alter table def_test alter column c1 set default 10;
696 alter table def_test alter column c2 set default 'new_default';
697 insert into def_test default values;
698 select * from def_test;
699  c1 |       c2        
700 ----+-----------------
701   5 | initial_default
702     | initial_default
703     | 
704  10 | new_default
705 (4 rows)
706
707 -- set defaults to an incorrect type: this should fail
708 alter table def_test alter column c1 set default 'wrong_datatype';
709 ERROR:  pg_atoi: error in "wrong_datatype": can't parse "wrong_datatype"
710 alter table def_test alter column c2 set default 20;
711 -- set defaults on a non-existent column: this should fail
712 alter table def_test alter column c3 set default 30;
713 ERROR:  Relation "def_test" has no column "c3"
714 -- set defaults on views: we need to create a view, add a rule
715 -- to allow insertions into it, and then alter the view to add
716 -- a default
717 create view def_view_test as select * from def_test;
718 create rule def_view_test_ins as
719         on insert to def_view_test
720         do instead insert into def_test select new.*;
721 insert into def_view_test default values;
722 alter table def_view_test alter column c1 set default 45;
723 insert into def_view_test default values;
724 alter table def_view_test alter column c2 set default 'view_default';
725 insert into def_view_test default values;
726 select * from def_view_test;
727  c1 |       c2        
728 ----+-----------------
729   5 | initial_default
730     | initial_default
731     | 
732  10 | new_default
733     | 
734  45 | 
735  45 | view_default
736 (7 rows)
737
738 drop rule def_view_test_ins on def_view_test;
739 drop view def_view_test;
740 drop table def_test;
741 -- alter table / drop column tests
742 -- try altering system catalogs, should fail
743 alter table pg_class drop column relname;
744 ERROR:  ALTER TABLE: relation "pg_class" is a system catalog
745 -- try altering non-existent table, should fail
746 alter table foo drop column bar;
747 ERROR:  Relation "foo" does not exist
748 -- test dropping columns
749 create table atacc1 (a int4 not null, b int4, c int4 not null, d int4);
750 insert into atacc1 values (1, 2, 3, 4);
751 alter table atacc1 drop a;
752 alter table atacc1 drop a;
753 ERROR:  Relation "atacc1" has no column "a"
754 -- SELECTs
755 select * from atacc1;
756  b | c | d 
757 ---+---+---
758  2 | 3 | 4
759 (1 row)
760
761 select * from atacc1 order by a;
762 ERROR:  Attribute "a" not found
763 select * from atacc1 order by "........pg.dropped.1........";
764 ERROR:  Attribute "........pg.dropped.1........" not found
765 select * from atacc1 group by a;
766 ERROR:  Attribute "a" not found
767 select * from atacc1 group by "........pg.dropped.1........";
768 ERROR:  Attribute "........pg.dropped.1........" not found
769 select atacc1.* from atacc1;
770  b | c | d 
771 ---+---+---
772  2 | 3 | 4
773 (1 row)
774
775 select a from atacc1;
776 ERROR:  Attribute "a" not found
777 select atacc1.a from atacc1;
778 ERROR:  no such attribute atacc1.a
779 select b,c,d from atacc1;
780  b | c | d 
781 ---+---+---
782  2 | 3 | 4
783 (1 row)
784
785 select a,b,c,d from atacc1;
786 ERROR:  Attribute "a" not found
787 select * from atacc1 where a = 1;
788 ERROR:  Attribute "a" not found
789 select "........pg.dropped.1........" from atacc1;
790 ERROR:  Attribute "........pg.dropped.1........" not found
791 select atacc1."........pg.dropped.1........" from atacc1;
792 ERROR:  no such attribute atacc1.........pg.dropped.1........
793 select "........pg.dropped.1........",b,c,d from atacc1;
794 ERROR:  Attribute "........pg.dropped.1........" not found
795 select * from atacc1 where "........pg.dropped.1........" = 1;
796 ERROR:  Attribute "........pg.dropped.1........" not found
797 -- UPDATEs
798 update atacc1 set a = 3;
799 ERROR:  Relation "atacc1" has no column "a"
800 update atacc1 set b = 2 where a = 3;
801 ERROR:  Attribute "a" not found
802 update atacc1 set "........pg.dropped.1........" = 3;
803 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
804 update atacc1 set b = 2 where "........pg.dropped.1........" = 3;
805 ERROR:  Attribute "........pg.dropped.1........" not found
806 -- INSERTs
807 insert into atacc1 values (10, 11, 12, 13);
808 ERROR:  INSERT has more expressions than target columns
809 insert into atacc1 values (default, 11, 12, 13);
810 ERROR:  INSERT has more expressions than target columns
811 insert into atacc1 values (11, 12, 13);
812 insert into atacc1 (a) values (10);
813 ERROR:  Relation "atacc1" has no column "a"
814 insert into atacc1 (a) values (default);
815 ERROR:  Relation "atacc1" has no column "a"
816 insert into atacc1 (a,b,c,d) values (10,11,12,13);
817 ERROR:  Relation "atacc1" has no column "a"
818 insert into atacc1 (a,b,c,d) values (default,11,12,13);
819 ERROR:  Relation "atacc1" has no column "a"
820 insert into atacc1 (b,c,d) values (11,12,13);
821 insert into atacc1 ("........pg.dropped.1........") values (10);
822 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
823 insert into atacc1 ("........pg.dropped.1........") values (default);
824 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
825 insert into atacc1 ("........pg.dropped.1........",b,c,d) values (10,11,12,13);
826 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
827 insert into atacc1 ("........pg.dropped.1........",b,c,d) values (default,11,12,13);
828 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
829 -- DELETEs
830 delete from atacc1 where a = 3;
831 ERROR:  Attribute "a" not found
832 delete from atacc1 where "........pg.dropped.1........" = 3;
833 ERROR:  Attribute "........pg.dropped.1........" not found
834 delete from atacc1;
835 -- try dropping a non-existent column, should fail
836 alter table atacc1 drop bar;
837 ERROR:  Relation "atacc1" has no column "bar"
838 -- try dropping the oid column, should fail
839 alter table atacc1 drop oid;
840 ERROR:  ALTER TABLE: Cannot drop system attribute "oid"
841 -- try creating a view and altering that, should fail
842 create view myview as select * from atacc1;
843 select * from myview;
844  b | c | d 
845 ---+---+---
846 (0 rows)
847
848 alter table myview drop d;
849 ERROR:  ALTER TABLE: relation "myview" is not a table
850 drop view myview;
851 -- test some commands to make sure they fail on the dropped column
852 analyze atacc1(a);
853 ERROR:  Relation "atacc1" has no column "a"
854 analyze atacc1("........pg.dropped.1........");
855 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
856 vacuum analyze atacc1(a);
857 ERROR:  Relation "atacc1" has no column "a"
858 vacuum analyze atacc1("........pg.dropped.1........");
859 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
860 comment on column atacc1.a is 'testing';
861 ERROR:  Relation "atacc1" has no column "a"
862 comment on column atacc1."........pg.dropped.1........" is 'testing';
863 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
864 alter table atacc1 alter a set storage plain;
865 ERROR:  ALTER TABLE: relation "atacc1" has no column "a"
866 alter table atacc1 alter "........pg.dropped.1........" set storage plain;
867 ERROR:  ALTER TABLE: relation "atacc1" has no column "........pg.dropped.1........"
868 alter table atacc1 alter a set statistics 0;
869 ERROR:  ALTER TABLE: relation "atacc1" has no column "a"
870 alter table atacc1 alter "........pg.dropped.1........" set statistics 0;
871 ERROR:  ALTER TABLE: relation "atacc1" has no column "........pg.dropped.1........"
872 alter table atacc1 alter a set default 3;
873 ERROR:  Relation "atacc1" has no column "a"
874 alter table atacc1 alter "........pg.dropped.1........" set default 3;
875 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
876 alter table atacc1 alter a drop default;
877 ERROR:  Relation "atacc1" has no column "a"
878 alter table atacc1 alter "........pg.dropped.1........" drop default;
879 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
880 alter table atacc1 alter a set not null;
881 ERROR:  Relation "atacc1" has no column "a"
882 alter table atacc1 alter "........pg.dropped.1........" set not null;
883 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
884 alter table atacc1 alter a drop not null;
885 ERROR:  Relation "atacc1" has no column "a"
886 alter table atacc1 alter "........pg.dropped.1........" drop not null;
887 ERROR:  Relation "atacc1" has no column "........pg.dropped.1........"
888 alter table atacc1 rename a to x;
889 ERROR:  renameatt: attribute "a" does not exist
890 alter table atacc1 rename "........pg.dropped.1........" to x;
891 ERROR:  renameatt: attribute "........pg.dropped.1........" does not exist
892 alter table atacc1 add primary key(a);
893 ERROR:  ALTER TABLE: column "a" named in key does not exist
894 alter table atacc1 add primary key("........pg.dropped.1........");
895 ERROR:  ALTER TABLE: column "........pg.dropped.1........" named in key does not exist
896 alter table atacc1 add unique(a);
897 ERROR:  ALTER TABLE: column "a" named in key does not exist
898 alter table atacc1 add unique("........pg.dropped.1........");
899 ERROR:  ALTER TABLE: column "........pg.dropped.1........" named in key does not exist
900 alter table atacc1 add check (a > 3);
901 ERROR:  Attribute "a" not found
902 alter table atacc1 add check ("........pg.dropped.1........" > 3);
903 ERROR:  Attribute "........pg.dropped.1........" not found
904 create table atacc2 (id int4 unique);
905 NOTICE:  CREATE TABLE / UNIQUE will create implicit index 'atacc2_id_key' for table 'atacc2'
906 alter table atacc1 add foreign key (a) references atacc2(id);
907 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
908 ERROR:  ALTER TABLE: column "a" referenced in foreign key constraint does not exist
909 alter table atacc1 add foreign key ("........pg.dropped.1........") references atacc2(id);
910 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
911 ERROR:  ALTER TABLE: column "........pg.dropped.1........" referenced in foreign key constraint does not exist
912 alter table atacc2 add foreign key (id) references atacc1(a);
913 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
914 ERROR:  ALTER TABLE: column "a" referenced in foreign key constraint does not exist
915 alter table atacc2 add foreign key (id) references atacc1("........pg.dropped.1........");
916 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
917 ERROR:  ALTER TABLE: column "........pg.dropped.1........" referenced in foreign key constraint does not exist
918 drop table atacc2;
919 create index "testing_idx" on atacc1(a);
920 ERROR:  DefineIndex: attribute "a" not found
921 create index "testing_idx" on atacc1("........pg.dropped.1........");
922 ERROR:  DefineIndex: attribute "........pg.dropped.1........" not found
923 -- test create as and select into
924 insert into atacc1 values (21, 22, 23);
925 create table test1 as select * from atacc1;
926 select * from test1;
927  b  | c  | d  
928 ----+----+----
929  21 | 22 | 23
930 (1 row)
931
932 drop table test1;
933 select * into test2 from atacc1;
934 select * from test2;
935  b  | c  | d  
936 ----+----+----
937  21 | 22 | 23
938 (1 row)
939
940 drop table test2;
941 -- try dropping all columns
942 alter table atacc1 drop c;
943 alter table atacc1 drop d;
944 alter table atacc1 drop b;
945 select * from atacc1;
946   
947 --
948 (1 row)
949
950 drop table atacc1;
951 -- test inheritance
952 create table parent (a int, b int, c int);
953 insert into parent values (1, 2, 3);
954 alter table parent drop a;
955 create table child (d varchar(255)) inherits (parent);
956 insert into child values (12, 13, 'testing');
957 select * from parent;
958  b  | c  
959 ----+----
960   2 |  3
961  12 | 13
962 (2 rows)
963
964 select * from child;
965  b  | c  |    d    
966 ----+----+---------
967  12 | 13 | testing
968 (1 row)
969
970 alter table parent drop c;
971 select * from parent;
972  b  
973 ----
974   2
975  12
976 (2 rows)
977
978 select * from child;
979  b  |    d    
980 ----+---------
981  12 | testing
982 (1 row)
983
984 drop table child;
985 drop table parent;
986 -- test copy in/out
987 create table test (a int4, b int4, c int4);
988 insert into test values (1,2,3);
989 alter table test drop a;
990 copy test to stdout;
991 2       3
992 copy test(a) to stdout;
993 ERROR:  Relation "test" has no column "a"
994 copy test("........pg.dropped.1........") to stdout;
995 ERROR:  Relation "test" has no column "........pg.dropped.1........"
996 copy test from stdin;
997 ERROR:  Extra data after last expected column
998 CONTEXT:  COPY FROM, line 1
999 select * from test;
1000  b | c 
1001 ---+---
1002  2 | 3
1003 (1 row)
1004
1005 copy test from stdin;
1006 select * from test;
1007  b  | c  
1008 ----+----
1009   2 |  3
1010  21 | 22
1011 (2 rows)
1012
1013 copy test(a) from stdin;
1014 ERROR:  Relation "test" has no column "a"
1015 copy test("........pg.dropped.1........") from stdin;
1016 ERROR:  Relation "test" has no column "........pg.dropped.1........"
1017 copy test(b,c) from stdin;
1018 select * from test;
1019  b  | c  
1020 ----+----
1021   2 |  3
1022  21 | 22
1023  31 | 32
1024 (3 rows)
1025
1026 drop table test;
1027 -- test inheritance
1028 create table dropColumn (a int, b int, e int);
1029 create table dropColumnChild (c int) inherits (dropColumn);
1030 create table dropColumnAnother (d int) inherits (dropColumnChild);
1031 -- these two should fail
1032 alter table dropColumnchild drop column a;
1033 ERROR:  ALTER TABLE: Cannot drop inherited column "a"
1034 alter table only dropColumnChild drop column b;
1035 ERROR:  ALTER TABLE: Cannot drop inherited column "b"
1036 -- these three should work
1037 alter table only dropColumn drop column e;
1038 alter table dropColumnChild drop column c;
1039 alter table dropColumn drop column a;
1040 create table renameColumn (a int);
1041 create table renameColumnChild (b int) inherits (renameColumn);
1042 create table renameColumnAnother (c int) inherits (renameColumnChild);
1043 -- these three should fail
1044 alter table renameColumnChild rename column a to d;
1045 ERROR:  renameatt: inherited attribute "a" may not be renamed
1046 alter table only renameColumnChild rename column a to d;
1047 ERROR:  Inherited attribute "a" must be renamed in child tables too
1048 alter table only renameColumn rename column a to d;
1049 ERROR:  Inherited attribute "a" must be renamed in child tables too
1050 -- these should work
1051 alter table renameColumn rename column a to d;
1052 alter table renameColumnChild rename column b to a;
1053 -- this should work
1054 alter table renameColumn add column w int;
1055 -- this should fail
1056 alter table only renameColumn add column x int;
1057 ERROR:  Attribute must be added to child tables too
1058 -- Test corner cases in dropping of inherited columns
1059 create table p1 (f1 int, f2 int);
1060 create table c1 (f1 int not null) inherits(p1);
1061 NOTICE:  CREATE TABLE: merging attribute "f1" with inherited definition
1062 -- should be rejected since c1.f1 is inherited
1063 alter table c1 drop column f1;
1064 ERROR:  ALTER TABLE: Cannot drop inherited column "f1"
1065 -- should work
1066 alter table p1 drop column f1;
1067 -- c1.f1 is still there, but no longer inherited
1068 select f1 from c1;
1069  f1 
1070 ----
1071 (0 rows)
1072
1073 alter table c1 drop column f1;
1074 select f1 from c1;
1075 ERROR:  Attribute "f1" not found
1076 drop table p1 cascade;
1077 NOTICE:  Drop cascades to table c1
1078 create table p1 (f1 int, f2 int);
1079 create table c1 () inherits(p1);
1080 -- should be rejected since c1.f1 is inherited
1081 alter table c1 drop column f1;
1082 ERROR:  ALTER TABLE: Cannot drop inherited column "f1"
1083 alter table p1 drop column f1;
1084 -- c1.f1 is dropped now, since there is no local definition for it
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 only p1 drop column f1;
1095 -- c1.f1 is NOT dropped, but must now be considered non-inherited
1096 alter table c1 drop column f1;
1097 drop table p1 cascade;
1098 NOTICE:  Drop cascades to table c1
1099 create table p1 (f1 int, f2 int);
1100 create table c1 (f1 int not null) inherits(p1);
1101 NOTICE:  CREATE TABLE: merging attribute "f1" with inherited definition
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 still there, but no longer inherited
1107 alter table c1 drop column f1;
1108 drop table p1 cascade;
1109 NOTICE:  Drop cascades to table c1
1110 create table p1(id int, name text);
1111 create table p2(id2 int, name text, height int);
1112 create table c1(age int) inherits(p1,p2);
1113 NOTICE:  CREATE TABLE: merging multiple inherited definitions of attribute "name"
1114 create table gc1() inherits (c1);
1115 select relname, attname, attinhcount, attislocal
1116 from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
1117 where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
1118 order by relname, attnum;
1119  relname | attname | attinhcount | attislocal 
1120 ---------+---------+-------------+------------
1121  c1      | id      |           1 | f
1122  c1      | name    |           2 | f
1123  c1      | id2     |           1 | f
1124  c1      | height  |           1 | f
1125  c1      | age     |           0 | t
1126  gc1     | id      |           1 | f
1127  gc1     | name    |           1 | f
1128  gc1     | id2     |           1 | f
1129  gc1     | height  |           1 | f
1130  gc1     | age     |           1 | f
1131  p1      | id      |           0 | t
1132  p1      | name    |           0 | t
1133  p2      | id2     |           0 | t
1134  p2      | name    |           0 | t
1135  p2      | height  |           0 | t
1136 (15 rows)
1137
1138 -- should work
1139 alter table only p1 drop column name;
1140 -- should work. Now c1.name is local and inhcount is 0.
1141 alter table p2 drop column name;
1142 -- should be rejected since its inherited
1143 alter table gc1 drop column name;
1144 ERROR:  ALTER TABLE: Cannot drop inherited column "name"
1145 -- should work, and drop gc1.name along
1146 alter table c1 drop column name;
1147 -- should fail: column does not exist
1148 alter table gc1 drop column name;
1149 ERROR:  Relation "gc1" has no column "name"
1150 -- should work and drop the attribute in all tables
1151 alter table p2 drop column height;
1152 select relname, attname, attinhcount, attislocal
1153 from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
1154 where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
1155 order by relname, attnum;
1156  relname | attname | attinhcount | attislocal 
1157 ---------+---------+-------------+------------
1158  c1      | id      |           1 | f
1159  c1      | id2     |           1 | f
1160  c1      | age     |           0 | t
1161  gc1     | id      |           1 | f
1162  gc1     | id2     |           1 | f
1163  gc1     | age     |           1 | f
1164  p1      | id      |           0 | t
1165  p2      | id2     |           0 | t
1166 (8 rows)
1167
1168 drop table p1, p2 cascade;
1169 NOTICE:  Drop cascades to table c1
1170 NOTICE:  Drop cascades to table gc1
1171 --
1172 -- Test the ALTER TABLE WITHOUT OIDS command
1173 --
1174 create table altstartwith (col integer) with oids;
1175 insert into altstartwith values (1);
1176 select oid > 0, * from altstartwith;
1177  ?column? | col 
1178 ----------+-----
1179  t        |   1
1180 (1 row)
1181
1182 alter table altstartwith set without oids;
1183 select oid > 0, * from altstartwith; -- fails
1184 ERROR:  Attribute "oid" not found
1185 select * from altstartwith;
1186  col 
1187 -----
1188    1
1189 (1 row)
1190
1191 -- Run inheritance tests
1192 create table altwithoid (col integer) with oids;
1193 -- Inherits parents oid column
1194 create table altinhoid () inherits (altwithoid) without oids;
1195 insert into altinhoid values (1);
1196 select oid > 0, * from altwithoid;
1197  ?column? | col 
1198 ----------+-----
1199  t        |   1
1200 (1 row)
1201
1202 select oid > 0, * from altinhoid;
1203  ?column? | col 
1204 ----------+-----
1205  t        |   1
1206 (1 row)
1207
1208 alter table altwithoid set without oids;
1209 alter table altinhoid set without oids; -- fails
1210 ERROR:  ALTER TABLE: Table is already WITHOUT OIDS
1211 select oid > 0, * from altwithoid; -- fails
1212 ERROR:  Attribute "oid" not found
1213 select oid > 0, * from altinhoid; -- fails
1214 ERROR:  Attribute "oid" not found
1215 select * from altwithoid;
1216  col 
1217 -----
1218    1
1219 (1 row)
1220
1221 select * from altinhoid;
1222  col 
1223 -----
1224    1
1225 (1 row)
1226
1227 -- test renumbering of child-table columns in inherited operations
1228 create table p1 (f1 int);
1229 create table c1 (f2 text, f3 int) inherits (p1);
1230 alter table p1 add column a1 int check (a1 > 0);
1231 alter table p1 add column f2 text;
1232 NOTICE:  ALTER TABLE: merging definition of column "f2" for child c1
1233 insert into p1 values (1,2,'abc');
1234 insert into c1 values(11,'xyz',33,0); -- should fail
1235 ERROR:  ExecInsert: rejected due to CHECK constraint "p1_a1" on "c1"
1236 insert into c1 values(11,'xyz',33,22);
1237 select * from p1;
1238  f1 | a1 | f2  
1239 ----+----+-----
1240   1 |  2 | abc
1241  11 | 22 | xyz
1242 (2 rows)
1243
1244 update p1 set a1 = a1 + 1, f2 = upper(f2);
1245 select * from p1;
1246  f1 | a1 | f2  
1247 ----+----+-----
1248   1 |  3 | ABC
1249  11 | 23 | XYZ
1250 (2 rows)
1251
1252 drop table p1 cascade;
1253 NOTICE:  Drop cascades to table c1
1254 NOTICE:  Drop cascades to constraint p1_a1 on table c1
1255 -- test that operations with a dropped column do not try to reference
1256 -- its datatype
1257 create domain mytype as text;
1258 create temp table foo (f1 text, f2 mytype, f3 text);
1259 insert into foo values('aa','bb','cc');
1260 select * from foo;
1261  f1 | f2 | f3 
1262 ----+----+----
1263  aa | bb | cc
1264 (1 row)
1265
1266 drop domain mytype cascade;
1267 NOTICE:  Drop cascades to table foo column f2
1268 select * from foo;
1269  f1 | f3 
1270 ----+----
1271  aa | cc
1272 (1 row)
1273
1274 insert into foo values('qq','rr');
1275 select * from foo;
1276  f1 | f3 
1277 ----+----
1278  aa | cc
1279  qq | rr
1280 (2 rows)
1281
1282 update foo set f3 = 'zz';
1283 select * from foo;
1284  f1 | f3 
1285 ----+----
1286  aa | zz
1287  qq | zz
1288 (2 rows)
1289
1290 select f3,max(f1) from foo group by f3;
1291  f3 | max 
1292 ----+-----
1293  zz | qq
1294 (1 row)
1295