]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/alter_table.out
ALTER <thing> [IF EXISTS] ... allows silent DDL if required,
[postgresql] / src / test / regress / expected / alter_table.out
1 --
2 -- ALTER_TABLE
3 -- add attribute
4 --
5 CREATE TABLE tmp (initial int4);
6 COMMENT ON TABLE tmp_wrong IS 'table comment';
7 ERROR:  relation "tmp_wrong" does not exist
8 COMMENT ON TABLE tmp IS 'table comment';
9 COMMENT ON TABLE tmp IS NULL;
10 ALTER TABLE tmp ADD COLUMN a int4 default 3;
11 ALTER TABLE tmp ADD COLUMN b name;
12 ALTER TABLE tmp ADD COLUMN c text;
13 ALTER TABLE tmp ADD COLUMN d float8;
14 ALTER TABLE tmp ADD COLUMN e float4;
15 ALTER TABLE tmp ADD COLUMN f int2;
16 ALTER TABLE tmp ADD COLUMN g polygon;
17 ALTER TABLE tmp ADD COLUMN h abstime;
18 ALTER TABLE tmp ADD COLUMN i char;
19 ALTER TABLE tmp ADD COLUMN j abstime[];
20 ALTER TABLE tmp ADD COLUMN k int4;
21 ALTER TABLE tmp ADD COLUMN l tid;
22 ALTER TABLE tmp ADD COLUMN m xid;
23 ALTER TABLE tmp ADD COLUMN n oidvector;
24 --ALTER TABLE tmp ADD COLUMN o lock;
25 ALTER TABLE tmp ADD COLUMN p smgr;
26 ALTER TABLE tmp ADD COLUMN q point;
27 ALTER TABLE tmp ADD COLUMN r lseg;
28 ALTER TABLE tmp ADD COLUMN s path;
29 ALTER TABLE tmp ADD COLUMN t box;
30 ALTER TABLE tmp ADD COLUMN u tinterval;
31 ALTER TABLE tmp ADD COLUMN v timestamp;
32 ALTER TABLE tmp ADD COLUMN w interval;
33 ALTER TABLE tmp ADD COLUMN x float8[];
34 ALTER TABLE tmp ADD COLUMN y float4[];
35 ALTER TABLE tmp ADD COLUMN z int2[];
36 INSERT INTO tmp (a, b, c, d, e, f, g, h, i, j, k, l, m, n, p, q, r, s, t, u,
37         v, w, x, y, z)
38    VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)',
39         'Mon May  1 00:30:30 1995', 'c', '{Mon May  1 00:30:30 1995, Monday Aug 24 14:43:07 1992, epoch}',
40         314159, '(1,1)', '512',
41         '1 2 3 4 5 6 7 8', 'magnetic disk', '(1.1,1.1)', '(4.1,4.1,3.1,3.1)',
42         '(0,2,4.1,4.1,3.1,3.1)', '(4.1,4.1,3.1,3.1)', '["epoch" "infinity"]',
43         'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
44 SELECT * FROM tmp;
45  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     
46 ---------+---+------+------+-----+-----+---+-----------------------+------------------------------+---+------------------------------------------------------------------------------------------------+--------+-------+-----+-----------------+---------------+-----------+-----------------------+-----------------------------+---------------------+---------------------------------------------+--------------------------+------------------+-----------+-----------+-----------
47          | 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}
48 (1 row)
49
50 DROP TABLE tmp;
51 -- the wolf bug - schema mods caused inconsistent row descriptors
52 CREATE TABLE tmp (
53         initial         int4
54 );
55 ALTER TABLE tmp ADD COLUMN a int4;
56 ALTER TABLE tmp ADD COLUMN b name;
57 ALTER TABLE tmp ADD COLUMN c text;
58 ALTER TABLE tmp ADD COLUMN d float8;
59 ALTER TABLE tmp ADD COLUMN e float4;
60 ALTER TABLE tmp ADD COLUMN f int2;
61 ALTER TABLE tmp ADD COLUMN g polygon;
62 ALTER TABLE tmp ADD COLUMN h abstime;
63 ALTER TABLE tmp ADD COLUMN i char;
64 ALTER TABLE tmp ADD COLUMN j abstime[];
65 ALTER TABLE tmp ADD COLUMN k int4;
66 ALTER TABLE tmp ADD COLUMN l tid;
67 ALTER TABLE tmp ADD COLUMN m xid;
68 ALTER TABLE tmp ADD COLUMN n oidvector;
69 --ALTER TABLE tmp ADD COLUMN o lock;
70 ALTER TABLE tmp ADD COLUMN p smgr;
71 ALTER TABLE tmp ADD COLUMN q point;
72 ALTER TABLE tmp ADD COLUMN r lseg;
73 ALTER TABLE tmp ADD COLUMN s path;
74 ALTER TABLE tmp ADD COLUMN t box;
75 ALTER TABLE tmp ADD COLUMN u tinterval;
76 ALTER TABLE tmp ADD COLUMN v timestamp;
77 ALTER TABLE tmp ADD COLUMN w interval;
78 ALTER TABLE tmp ADD COLUMN x float8[];
79 ALTER TABLE tmp ADD COLUMN y float4[];
80 ALTER TABLE tmp ADD COLUMN z int2[];
81 INSERT INTO tmp (a, b, c, d, e, f, g, h, i, j, k, l, m, n, p, q, r, s, t, u,
82         v, w, x, y, z)
83    VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)',
84         'Mon May  1 00:30:30 1995', 'c', '{Mon May  1 00:30:30 1995, Monday Aug 24 14:43:07 1992, epoch}',
85         314159, '(1,1)', '512',
86         '1 2 3 4 5 6 7 8', 'magnetic disk', '(1.1,1.1)', '(4.1,4.1,3.1,3.1)',
87         '(0,2,4.1,4.1,3.1,3.1)', '(4.1,4.1,3.1,3.1)', '["epoch" "infinity"]',
88         'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
89 SELECT * FROM tmp;
90  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     
91 ---------+---+------+------+-----+-----+---+-----------------------+------------------------------+---+------------------------------------------------------------------------------------------------+--------+-------+-----+-----------------+---------------+-----------+-----------------------+-----------------------------+---------------------+---------------------------------------------+--------------------------+------------------+-----------+-----------+-----------
92          | 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}
93 (1 row)
94
95 DROP TABLE tmp;
96 --
97 -- rename - check on both non-temp and temp tables
98 --
99 CREATE TABLE tmp (regtable int);
100 CREATE TEMP TABLE tmp (tmptable int);
101 ALTER TABLE tmp RENAME TO tmp_new;
102 SELECT * FROM tmp;
103  regtable 
104 ----------
105 (0 rows)
106
107 SELECT * FROM tmp_new;
108  tmptable 
109 ----------
110 (0 rows)
111
112 ALTER TABLE tmp RENAME TO tmp_new2;
113 SELECT * FROM tmp;              -- should fail
114 ERROR:  relation "tmp" does not exist
115 LINE 1: SELECT * FROM tmp;
116                       ^
117 SELECT * FROM tmp_new;
118  tmptable 
119 ----------
120 (0 rows)
121
122 SELECT * FROM tmp_new2;
123  regtable 
124 ----------
125 (0 rows)
126
127 DROP TABLE tmp_new;
128 DROP TABLE tmp_new2;
129 -- ALTER TABLE ... RENAME on non-table relations
130 -- renaming indexes (FIXME: this should probably test the index's functionality)
131 ALTER INDEX IF EXISTS __onek_unique1 RENAME TO tmp_onek_unique1;
132 NOTICE:  relation "__onek_unique1" does not exist, skipping
133 ALTER INDEX IF EXISTS __tmp_onek_unique1 RENAME TO onek_unique1;
134 NOTICE:  relation "__tmp_onek_unique1" does not exist, skipping
135 ALTER INDEX onek_unique1 RENAME TO tmp_onek_unique1;
136 ALTER INDEX tmp_onek_unique1 RENAME TO onek_unique1;
137 -- renaming views
138 CREATE VIEW tmp_view (unique1) AS SELECT unique1 FROM tenk1;
139 ALTER TABLE tmp_view RENAME TO tmp_view_new;
140 -- hack to ensure we get an indexscan here
141 ANALYZE tenk1;
142 set enable_seqscan to off;
143 set enable_bitmapscan to off;
144 -- 5 values, sorted
145 SELECT unique1 FROM tenk1 WHERE unique1 < 5;
146  unique1 
147 ---------
148        0
149        1
150        2
151        3
152        4
153 (5 rows)
154
155 reset enable_seqscan;
156 reset enable_bitmapscan;
157 DROP VIEW tmp_view_new;
158 -- toast-like relation name
159 alter table stud_emp rename to pg_toast_stud_emp;
160 alter table pg_toast_stud_emp rename to stud_emp;
161 -- FOREIGN KEY CONSTRAINT adding TEST
162 CREATE TABLE tmp2 (a int primary key);
163 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "tmp2_pkey" for table "tmp2"
164 CREATE TABLE tmp3 (a int, b int);
165 CREATE TABLE tmp4 (a int, b int, unique(a,b));
166 NOTICE:  CREATE TABLE / UNIQUE will create implicit index "tmp4_a_b_key" for table "tmp4"
167 CREATE TABLE tmp5 (a int, b int);
168 -- Insert rows into tmp2 (pktable)
169 INSERT INTO tmp2 values (1);
170 INSERT INTO tmp2 values (2);
171 INSERT INTO tmp2 values (3);
172 INSERT INTO tmp2 values (4);
173 -- Insert rows into tmp3
174 INSERT INTO tmp3 values (1,10);
175 INSERT INTO tmp3 values (1,20);
176 INSERT INTO tmp3 values (5,50);
177 -- Try (and fail) to add constraint due to invalid source columns
178 ALTER TABLE tmp3 add constraint tmpconstr foreign key(c) references tmp2 match full;
179 ERROR:  column "c" referenced in foreign key constraint does not exist
180 -- Try (and fail) to add constraint due to invalide destination columns explicitly given
181 ALTER TABLE tmp3 add constraint tmpconstr foreign key(a) references tmp2(b) match full;
182 ERROR:  column "b" referenced in foreign key constraint does not exist
183 -- Try (and fail) to add constraint due to invalid data
184 ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;
185 ERROR:  insert or update on table "tmp3" violates foreign key constraint "tmpconstr"
186 DETAIL:  Key (a)=(5) is not present in table "tmp2".
187 -- Delete failing row
188 DELETE FROM tmp3 where a=5;
189 -- Try (and succeed)
190 ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;
191 ALTER TABLE tmp3 drop constraint tmpconstr;
192 INSERT INTO tmp3 values (5,50);
193 -- Try NOT VALID and then VALIDATE CONSTRAINT, but fails. Delete failure then re-validate
194 ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full NOT VALID;
195 ALTER TABLE tmp3 validate constraint tmpconstr;
196 ERROR:  insert or update on table "tmp3" violates foreign key constraint "tmpconstr"
197 DETAIL:  Key (a)=(5) is not present in table "tmp2".
198 -- Delete failing row
199 DELETE FROM tmp3 where a=5;
200 -- Try (and succeed) and repeat to show it works on already valid constraint
201 ALTER TABLE tmp3 validate constraint tmpconstr;
202 ALTER TABLE tmp3 validate constraint tmpconstr;
203 -- Try a non-verified CHECK constraint
204 ALTER TABLE tmp3 ADD CONSTRAINT b_greater_than_ten CHECK (b > 10); -- fail
205 ERROR:  check constraint "b_greater_than_ten" is violated by some row
206 ALTER TABLE tmp3 ADD CONSTRAINT b_greater_than_ten CHECK (b > 10) NOT VALID; -- succeeds
207 ALTER TABLE tmp3 VALIDATE CONSTRAINT b_greater_than_ten; -- fails
208 ERROR:  check constraint "b_greater_than_ten" is violated by some row
209 DELETE FROM tmp3 WHERE NOT b > 10;
210 ALTER TABLE tmp3 VALIDATE CONSTRAINT b_greater_than_ten; -- succeeds
211 ALTER TABLE tmp3 VALIDATE CONSTRAINT b_greater_than_ten; -- succeeds
212 -- Test inherited NOT VALID CHECK constraints
213 select * from tmp3;
214  a | b  
215 ---+----
216  1 | 20
217 (1 row)
218
219 CREATE TABLE tmp6 () INHERITS (tmp3);
220 CREATE TABLE tmp7 () INHERITS (tmp3);
221 INSERT INTO tmp6 VALUES (6, 30), (7, 16);
222 ALTER TABLE tmp3 ADD CONSTRAINT b_le_20 CHECK (b <= 20) NOT VALID;
223 ALTER TABLE tmp3 VALIDATE CONSTRAINT b_le_20;   -- fails
224 ERROR:  check constraint "b_le_20" is violated by some row
225 DELETE FROM tmp6 WHERE b > 20;
226 ALTER TABLE tmp3 VALIDATE CONSTRAINT b_le_20;   -- succeeds
227 -- An already validated constraint must not be revalidated
228 CREATE FUNCTION boo(int) RETURNS int IMMUTABLE STRICT LANGUAGE plpgsql AS $$ BEGIN RAISE NOTICE 'boo: %', $1; RETURN $1; END; $$;
229 INSERT INTO tmp7 VALUES (8, 18);
230 ALTER TABLE tmp7 ADD CONSTRAINT identity CHECK (b = boo(b));
231 NOTICE:  boo: 18
232 ALTER TABLE tmp3 ADD CONSTRAINT IDENTITY check (b = boo(b)) NOT VALID;
233 NOTICE:  merging constraint "identity" with inherited definition
234 ALTER TABLE tmp3 VALIDATE CONSTRAINT identity;
235 NOTICE:  boo: 16
236 NOTICE:  boo: 20
237 -- Try (and fail) to create constraint from tmp5(a) to tmp4(a) - unique constraint on
238 -- tmp4 is a,b
239 ALTER TABLE tmp5 add constraint tmpconstr foreign key(a) references tmp4(a) match full;
240 ERROR:  there is no unique constraint matching given keys for referenced table "tmp4"
241 DROP TABLE tmp7;
242 DROP TABLE tmp6;
243 DROP TABLE tmp5;
244 DROP TABLE tmp4;
245 DROP TABLE tmp3;
246 DROP TABLE tmp2;
247 -- NOT VALID with plan invalidation -- ensure we don't use a constraint for
248 -- exclusion until validated
249 set constraint_exclusion TO 'partition';
250 create table nv_parent (d date); 
251 create table nv_child_2010 () inherits (nv_parent);
252 create table nv_child_2011 () inherits (nv_parent);
253 alter table nv_child_2010 add check (d between '2010-01-01'::date and '2010-12-31'::date) not valid;
254 alter table nv_child_2011 add check (d between '2011-01-01'::date and '2011-12-31'::date) not valid;
255 explain (costs off) select * from nv_parent where d between '2011-08-01' and '2011-08-31';
256                                    QUERY PLAN                                    
257 ---------------------------------------------------------------------------------
258  Result
259    ->  Append
260          ->  Seq Scan on nv_parent
261                Filter: ((d >= '08-01-2011'::date) AND (d <= '08-31-2011'::date))
262          ->  Seq Scan on nv_child_2010 nv_parent
263                Filter: ((d >= '08-01-2011'::date) AND (d <= '08-31-2011'::date))
264          ->  Seq Scan on nv_child_2011 nv_parent
265                Filter: ((d >= '08-01-2011'::date) AND (d <= '08-31-2011'::date))
266 (8 rows)
267
268 create table nv_child_2009 (check (d between '2009-01-01'::date and '2009-12-31'::date)) inherits (nv_parent);
269 explain (costs off) select * from nv_parent where d between '2011-08-01'::date and '2011-08-31'::date;
270                                    QUERY PLAN                                    
271 ---------------------------------------------------------------------------------
272  Result
273    ->  Append
274          ->  Seq Scan on nv_parent
275                Filter: ((d >= '08-01-2011'::date) AND (d <= '08-31-2011'::date))
276          ->  Seq Scan on nv_child_2010 nv_parent
277                Filter: ((d >= '08-01-2011'::date) AND (d <= '08-31-2011'::date))
278          ->  Seq Scan on nv_child_2011 nv_parent
279                Filter: ((d >= '08-01-2011'::date) AND (d <= '08-31-2011'::date))
280 (8 rows)
281
282 explain (costs off) select * from nv_parent where d between '2009-08-01'::date and '2009-08-31'::date;
283                                    QUERY PLAN                                    
284 ---------------------------------------------------------------------------------
285  Result
286    ->  Append
287          ->  Seq Scan on nv_parent
288                Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
289          ->  Seq Scan on nv_child_2010 nv_parent
290                Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
291          ->  Seq Scan on nv_child_2011 nv_parent
292                Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
293          ->  Seq Scan on nv_child_2009 nv_parent
294                Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
295 (10 rows)
296
297 -- after validation, the constraint should be used
298 alter table nv_child_2011 VALIDATE CONSTRAINT nv_child_2011_d_check;
299 explain (costs off) select * from nv_parent where d between '2009-08-01'::date and '2009-08-31'::date;
300                                    QUERY PLAN                                    
301 ---------------------------------------------------------------------------------
302  Result
303    ->  Append
304          ->  Seq Scan on nv_parent
305                Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
306          ->  Seq Scan on nv_child_2010 nv_parent
307                Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
308          ->  Seq Scan on nv_child_2009 nv_parent
309                Filter: ((d >= '08-01-2009'::date) AND (d <= '08-31-2009'::date))
310 (8 rows)
311
312 -- Foreign key adding test with mixed types
313 -- Note: these tables are TEMP to avoid name conflicts when this test
314 -- is run in parallel with foreign_key.sql.
315 CREATE TEMP TABLE PKTABLE (ptest1 int PRIMARY KEY);
316 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
317 INSERT INTO PKTABLE VALUES(42);
318 CREATE TEMP TABLE FKTABLE (ftest1 inet);
319 -- This next should fail, because int=inet does not exist
320 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
321 ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
322 DETAIL:  Key columns "ftest1" and "ptest1" are of incompatible types: inet and integer.
323 -- This should also fail for the same reason, but here we
324 -- give the column name
325 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable(ptest1);
326 ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
327 DETAIL:  Key columns "ftest1" and "ptest1" are of incompatible types: inet and integer.
328 DROP TABLE FKTABLE;
329 -- This should succeed, even though they are different types,
330 -- because int=int8 exists and is a member of the integer opfamily
331 CREATE TEMP TABLE FKTABLE (ftest1 int8);
332 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
333 -- Check it actually works
334 INSERT INTO FKTABLE VALUES(42);         -- should succeed
335 INSERT INTO FKTABLE VALUES(43);         -- should fail
336 ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
337 DETAIL:  Key (ftest1)=(43) is not present in table "pktable".
338 DROP TABLE FKTABLE;
339 -- This should fail, because we'd have to cast numeric to int which is
340 -- not an implicit coercion (or use numeric=numeric, but that's not part
341 -- of the integer opfamily)
342 CREATE TEMP TABLE FKTABLE (ftest1 numeric);
343 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
344 ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
345 DETAIL:  Key columns "ftest1" and "ptest1" are of incompatible types: numeric and integer.
346 DROP TABLE FKTABLE;
347 DROP TABLE PKTABLE;
348 -- On the other hand, this should work because int implicitly promotes to
349 -- numeric, and we allow promotion on the FK side
350 CREATE TEMP TABLE PKTABLE (ptest1 numeric PRIMARY KEY);
351 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
352 INSERT INTO PKTABLE VALUES(42);
353 CREATE TEMP TABLE FKTABLE (ftest1 int);
354 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
355 -- Check it actually works
356 INSERT INTO FKTABLE VALUES(42);         -- should succeed
357 INSERT INTO FKTABLE VALUES(43);         -- should fail
358 ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
359 DETAIL:  Key (ftest1)=(43) is not present in table "pktable".
360 DROP TABLE FKTABLE;
361 DROP TABLE PKTABLE;
362 CREATE TEMP TABLE PKTABLE (ptest1 int, ptest2 inet,
363                            PRIMARY KEY(ptest1, ptest2));
364 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
365 -- This should fail, because we just chose really odd types
366 CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);
367 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2) references pktable;
368 ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
369 DETAIL:  Key columns "ftest1" and "ptest1" are of incompatible types: cidr and integer.
370 DROP TABLE FKTABLE;
371 -- Again, so should this...
372 CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);
373 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
374      references pktable(ptest1, ptest2);
375 ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
376 DETAIL:  Key columns "ftest1" and "ptest1" are of incompatible types: cidr and integer.
377 DROP TABLE FKTABLE;
378 -- This fails because we mixed up the column ordering
379 CREATE TEMP TABLE FKTABLE (ftest1 int, ftest2 inet);
380 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
381      references pktable(ptest2, ptest1);
382 ERROR:  foreign key constraint "fktable_ftest1_fkey" cannot be implemented
383 DETAIL:  Key columns "ftest1" and "ptest2" are of incompatible types: integer and inet.
384 -- As does this...
385 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest2, ftest1)
386      references pktable(ptest1, ptest2);
387 ERROR:  foreign key constraint "fktable_ftest2_fkey" cannot be implemented
388 DETAIL:  Key columns "ftest2" and "ptest1" are of incompatible types: inet and integer.
389 -- temp tables should go away by themselves, need not drop them.
390 -- test check constraint adding
391 create table atacc1 ( test int );
392 -- add a check constraint
393 alter table atacc1 add constraint atacc_test1 check (test>3);
394 -- should fail
395 insert into atacc1 (test) values (2);
396 ERROR:  new row for relation "atacc1" violates check constraint "atacc_test1"
397 DETAIL:  Failing row contains (2).
398 -- should succeed
399 insert into atacc1 (test) values (4);
400 drop table atacc1;
401 -- let's do one where the check fails when added
402 create table atacc1 ( test int );
403 -- insert a soon to be failing row
404 insert into atacc1 (test) values (2);
405 -- add a check constraint (fails)
406 alter table atacc1 add constraint atacc_test1 check (test>3);
407 ERROR:  check constraint "atacc_test1" is violated by some row
408 insert into atacc1 (test) values (4);
409 drop table atacc1;
410 -- let's do one where the check fails because the column doesn't exist
411 create table atacc1 ( test int );
412 -- add a check constraint (fails)
413 alter table atacc1 add constraint atacc_test1 check (test1>3);
414 ERROR:  column "test1" does not exist
415 drop table atacc1;
416 -- something a little more complicated
417 create table atacc1 ( test int, test2 int, test3 int);
418 -- add a check constraint (fails)
419 alter table atacc1 add constraint atacc_test1 check (test+test2<test3*4);
420 -- should fail
421 insert into atacc1 (test,test2,test3) values (4,4,2);
422 ERROR:  new row for relation "atacc1" violates check constraint "atacc_test1"
423 DETAIL:  Failing row contains (4, 4, 2).
424 -- should succeed
425 insert into atacc1 (test,test2,test3) values (4,4,5);
426 drop table atacc1;
427 -- lets do some naming tests
428 create table atacc1 (test int check (test>3), test2 int);
429 alter table atacc1 add check (test2>test);
430 -- should fail for $2
431 insert into atacc1 (test2, test) values (3, 4);
432 ERROR:  new row for relation "atacc1" violates check constraint "atacc1_check"
433 DETAIL:  Failing row contains (4, 3).
434 drop table atacc1;
435 -- inheritance related tests
436 create table atacc1 (test int);
437 create table atacc2 (test2 int);
438 create table atacc3 (test3 int) inherits (atacc1, atacc2);
439 alter table atacc2 add constraint foo check (test2>0);
440 -- fail and then succeed on atacc2
441 insert into atacc2 (test2) values (-3);
442 ERROR:  new row for relation "atacc2" violates check constraint "foo"
443 DETAIL:  Failing row contains (-3).
444 insert into atacc2 (test2) values (3);
445 -- fail and then succeed on atacc3
446 insert into atacc3 (test2) values (-3);
447 ERROR:  new row for relation "atacc3" violates check constraint "foo"
448 DETAIL:  Failing row contains (null, -3, null).
449 insert into atacc3 (test2) values (3);
450 drop table atacc3;
451 drop table atacc2;
452 drop table atacc1;
453 -- same things with one created with INHERIT
454 create table atacc1 (test int);
455 create table atacc2 (test2 int);
456 create table atacc3 (test3 int) inherits (atacc1, atacc2);
457 alter table atacc3 no inherit atacc2;
458 -- fail
459 alter table atacc3 no inherit atacc2;
460 ERROR:  relation "atacc2" is not a parent of relation "atacc3"
461 -- make sure it really isn't a child
462 insert into atacc3 (test2) values (3);
463 select test2 from atacc2;
464  test2 
465 -------
466 (0 rows)
467
468 -- fail due to missing constraint
469 alter table atacc2 add constraint foo check (test2>0);
470 alter table atacc3 inherit atacc2;
471 ERROR:  child table is missing constraint "foo"
472 -- fail due to missing column
473 alter table atacc3 rename test2 to testx;
474 alter table atacc3 inherit atacc2;
475 ERROR:  child table is missing column "test2"
476 -- fail due to mismatched data type
477 alter table atacc3 add test2 bool;
478 alter table atacc3 inherit atacc2;
479 ERROR:  child table "atacc3" has different type for column "test2"
480 alter table atacc3 drop test2;
481 -- succeed
482 alter table atacc3 add test2 int;
483 update atacc3 set test2 = 4 where test2 is null;
484 alter table atacc3 add constraint foo check (test2>0);
485 alter table atacc3 inherit atacc2;
486 -- fail due to duplicates and circular inheritance
487 alter table atacc3 inherit atacc2;
488 ERROR:  relation "atacc2" would be inherited from more than once
489 alter table atacc2 inherit atacc3;
490 ERROR:  circular inheritance not allowed
491 DETAIL:  "atacc3" is already a child of "atacc2".
492 alter table atacc2 inherit atacc2;
493 ERROR:  circular inheritance not allowed
494 DETAIL:  "atacc2" is already a child of "atacc2".
495 -- test that we really are a child now (should see 4 not 3 and cascade should go through)
496 select test2 from atacc2;
497  test2 
498 -------
499      4
500 (1 row)
501
502 drop table atacc2 cascade;
503 NOTICE:  drop cascades to table atacc3
504 drop table atacc1;
505 -- adding only to a parent is allowed as of 9.2
506 create table atacc1 (test int);
507 create table atacc2 (test2 int) inherits (atacc1);
508 -- ok:
509 alter table only atacc1 add constraint foo check (test>0);
510 -- check constraint is not there on child
511 insert into atacc2 (test) values (-3);
512 -- check constraint is there on parent
513 insert into atacc1 (test) values (-3);
514 ERROR:  new row for relation "atacc1" violates check constraint "foo"
515 DETAIL:  Failing row contains (-3).
516 insert into atacc1 (test) values (3);
517 -- fail, violating row:
518 alter table only atacc2 add constraint foo check (test>0);
519 ERROR:  check constraint "foo" is violated by some row
520 drop table atacc2;
521 drop table atacc1;
522 -- test unique constraint adding
523 create table atacc1 ( test int ) with oids;
524 -- add a unique constraint
525 alter table atacc1 add constraint atacc_test1 unique (test);
526 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index "atacc_test1" for table "atacc1"
527 -- insert first value
528 insert into atacc1 (test) values (2);
529 -- should fail
530 insert into atacc1 (test) values (2);
531 ERROR:  duplicate key value violates unique constraint "atacc_test1"
532 DETAIL:  Key (test)=(2) already exists.
533 -- should succeed
534 insert into atacc1 (test) values (4);
535 -- try adding a unique oid constraint
536 alter table atacc1 add constraint atacc_oid1 unique(oid);
537 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index "atacc_oid1" for table "atacc1"
538 -- try to create duplicates via alter table using - should fail
539 alter table atacc1 alter column test type integer using 0;
540 ERROR:  could not create unique index "atacc_test1"
541 DETAIL:  Key (test)=(0) is duplicated.
542 drop table atacc1;
543 -- let's do one where the unique constraint fails when added
544 create table atacc1 ( test int );
545 -- insert soon to be failing rows
546 insert into atacc1 (test) values (2);
547 insert into atacc1 (test) values (2);
548 -- add a unique constraint (fails)
549 alter table atacc1 add constraint atacc_test1 unique (test);
550 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index "atacc_test1" for table "atacc1"
551 ERROR:  could not create unique index "atacc_test1"
552 DETAIL:  Key (test)=(2) is duplicated.
553 insert into atacc1 (test) values (3);
554 drop table atacc1;
555 -- let's do one where the unique constraint fails
556 -- because the column doesn't exist
557 create table atacc1 ( test int );
558 -- add a unique constraint (fails)
559 alter table atacc1 add constraint atacc_test1 unique (test1);
560 ERROR:  column "test1" named in key does not exist
561 drop table atacc1;
562 -- something a little more complicated
563 create table atacc1 ( test int, test2 int);
564 -- add a unique constraint
565 alter table atacc1 add constraint atacc_test1 unique (test, test2);
566 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index "atacc_test1" for table "atacc1"
567 -- insert initial value
568 insert into atacc1 (test,test2) values (4,4);
569 -- should fail
570 insert into atacc1 (test,test2) values (4,4);
571 ERROR:  duplicate key value violates unique constraint "atacc_test1"
572 DETAIL:  Key (test, test2)=(4, 4) already exists.
573 -- should all succeed
574 insert into atacc1 (test,test2) values (4,5);
575 insert into atacc1 (test,test2) values (5,4);
576 insert into atacc1 (test,test2) values (5,5);
577 drop table atacc1;
578 -- lets do some naming tests
579 create table atacc1 (test int, test2 int, unique(test));
580 NOTICE:  CREATE TABLE / UNIQUE will create implicit index "atacc1_test_key" for table "atacc1"
581 alter table atacc1 add unique (test2);
582 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index "atacc1_test2_key" for table "atacc1"
583 -- should fail for @@ second one @@
584 insert into atacc1 (test2, test) values (3, 3);
585 insert into atacc1 (test2, test) values (2, 3);
586 ERROR:  duplicate key value violates unique constraint "atacc1_test_key"
587 DETAIL:  Key (test)=(3) already exists.
588 drop table atacc1;
589 -- test primary key constraint adding
590 create table atacc1 ( test int ) with oids;
591 -- add a primary key constraint
592 alter table atacc1 add constraint atacc_test1 primary key (test);
593 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "atacc_test1" for table "atacc1"
594 -- insert first value
595 insert into atacc1 (test) values (2);
596 -- should fail
597 insert into atacc1 (test) values (2);
598 ERROR:  duplicate key value violates unique constraint "atacc_test1"
599 DETAIL:  Key (test)=(2) already exists.
600 -- should succeed
601 insert into atacc1 (test) values (4);
602 -- inserting NULL should fail
603 insert into atacc1 (test) values(NULL);
604 ERROR:  null value in column "test" violates not-null constraint
605 DETAIL:  Failing row contains (null).
606 -- try adding a second primary key (should fail)
607 alter table atacc1 add constraint atacc_oid1 primary key(oid);
608 ERROR:  multiple primary keys for table "atacc1" are not allowed
609 -- drop first primary key constraint
610 alter table atacc1 drop constraint atacc_test1 restrict;
611 -- try adding a primary key on oid (should succeed)
612 alter table atacc1 add constraint atacc_oid1 primary key(oid);
613 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "atacc_oid1" for table "atacc1"
614 drop table atacc1;
615 -- let's do one where the primary key constraint fails when added
616 create table atacc1 ( test int );
617 -- insert soon to be failing rows
618 insert into atacc1 (test) values (2);
619 insert into atacc1 (test) values (2);
620 -- add a primary key (fails)
621 alter table atacc1 add constraint atacc_test1 primary key (test);
622 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "atacc_test1" for table "atacc1"
623 ERROR:  could not create unique index "atacc_test1"
624 DETAIL:  Key (test)=(2) is duplicated.
625 insert into atacc1 (test) values (3);
626 drop table atacc1;
627 -- let's do another one where the primary key constraint fails when added
628 create table atacc1 ( test int );
629 -- insert soon to be failing row
630 insert into atacc1 (test) values (NULL);
631 -- add a primary key (fails)
632 alter table atacc1 add constraint atacc_test1 primary key (test);
633 ERROR:  column "test" contains null values
634 insert into atacc1 (test) values (3);
635 drop table atacc1;
636 -- let's do one where the primary key constraint fails
637 -- because the column doesn't exist
638 create table atacc1 ( test int );
639 -- add a primary key constraint (fails)
640 alter table atacc1 add constraint atacc_test1 primary key (test1);
641 ERROR:  column "test1" named in key does not exist
642 drop table atacc1;
643 -- adding a new column as primary key to a non-empty table.
644 -- should fail unless the column has a non-null default value.
645 create table atacc1 ( test int );
646 insert into atacc1 (test) values (0);
647 -- add a primary key column without a default (fails).
648 alter table atacc1 add column test2 int primary key;
649 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "atacc1_pkey" for table "atacc1"
650 ERROR:  column "test2" contains null values
651 -- now add a primary key column with a default (succeeds).
652 alter table atacc1 add column test2 int default 0 primary key;
653 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "atacc1_pkey" for table "atacc1"
654 drop table atacc1;
655 -- something a little more complicated
656 create table atacc1 ( test int, test2 int);
657 -- add a primary key constraint
658 alter table atacc1 add constraint atacc_test1 primary key (test, test2);
659 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "atacc_test1" for table "atacc1"
660 -- try adding a second primary key - should fail
661 alter table atacc1 add constraint atacc_test2 primary key (test);
662 ERROR:  multiple primary keys for table "atacc1" are not allowed
663 -- insert initial value
664 insert into atacc1 (test,test2) values (4,4);
665 -- should fail
666 insert into atacc1 (test,test2) values (4,4);
667 ERROR:  duplicate key value violates unique constraint "atacc_test1"
668 DETAIL:  Key (test, test2)=(4, 4) already exists.
669 insert into atacc1 (test,test2) values (NULL,3);
670 ERROR:  null value in column "test" violates not-null constraint
671 DETAIL:  Failing row contains (null, 3).
672 insert into atacc1 (test,test2) values (3, NULL);
673 ERROR:  null value in column "test2" violates not-null constraint
674 DETAIL:  Failing row contains (3, null).
675 insert into atacc1 (test,test2) values (NULL,NULL);
676 ERROR:  null value in column "test" violates not-null constraint
677 DETAIL:  Failing row contains (null, null).
678 -- should all succeed
679 insert into atacc1 (test,test2) values (4,5);
680 insert into atacc1 (test,test2) values (5,4);
681 insert into atacc1 (test,test2) values (5,5);
682 drop table atacc1;
683 -- lets do some naming tests
684 create table atacc1 (test int, test2 int, primary key(test));
685 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "atacc1_pkey" for table "atacc1"
686 -- only first should succeed
687 insert into atacc1 (test2, test) values (3, 3);
688 insert into atacc1 (test2, test) values (2, 3);
689 ERROR:  duplicate key value violates unique constraint "atacc1_pkey"
690 DETAIL:  Key (test)=(3) already exists.
691 insert into atacc1 (test2, test) values (1, NULL);
692 ERROR:  null value in column "test" violates not-null constraint
693 DETAIL:  Failing row contains (null, 1).
694 drop table atacc1;
695 -- alter table / alter column [set/drop] not null tests
696 -- try altering system catalogs, should fail
697 alter table pg_class alter column relname drop not null;
698 ERROR:  permission denied: "pg_class" is a system catalog
699 alter table pg_class alter relname set not null;
700 ERROR:  permission denied: "pg_class" is a system catalog
701 -- try altering non-existent table, should fail
702 alter table non_existent alter column bar set not null;
703 ERROR:  relation "non_existent" does not exist
704 alter table non_existent alter column bar drop not null;
705 ERROR:  relation "non_existent" does not exist
706 -- test setting columns to null and not null and vice versa
707 -- test checking for null values and primary key
708 create table atacc1 (test int not null) with oids;
709 alter table atacc1 add constraint "atacc1_pkey" primary key (test);
710 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "atacc1_pkey" for table "atacc1"
711 alter table atacc1 alter column test drop not null;
712 ERROR:  column "test" is in a primary key
713 alter table atacc1 drop constraint "atacc1_pkey";
714 alter table atacc1 alter column test drop not null;
715 insert into atacc1 values (null);
716 alter table atacc1 alter test set not null;
717 ERROR:  column "test" contains null values
718 delete from atacc1;
719 alter table atacc1 alter test set not null;
720 -- try altering a non-existent column, should fail
721 alter table atacc1 alter bar set not null;
722 ERROR:  column "bar" of relation "atacc1" does not exist
723 alter table atacc1 alter bar drop not null;
724 ERROR:  column "bar" of relation "atacc1" does not exist
725 -- try altering the oid column, should fail
726 alter table atacc1 alter oid set not null;
727 ERROR:  cannot alter system column "oid"
728 alter table atacc1 alter oid drop not null;
729 ERROR:  cannot alter system column "oid"
730 -- try creating a view and altering that, should fail
731 create view myview as select * from atacc1;
732 alter table myview alter column test drop not null;
733 ERROR:  "myview" is not a table or foreign table
734 alter table myview alter column test set not null;
735 ERROR:  "myview" is not a table or foreign table
736 drop view myview;
737 drop table atacc1;
738 -- test inheritance
739 create table parent (a int);
740 create table child (b varchar(255)) inherits (parent);
741 alter table parent alter a set not null;
742 insert into parent values (NULL);
743 ERROR:  null value in column "a" violates not-null constraint
744 DETAIL:  Failing row contains (null).
745 insert into child (a, b) values (NULL, 'foo');
746 ERROR:  null value in column "a" violates not-null constraint
747 DETAIL:  Failing row contains (null, foo).
748 alter table parent alter a drop not null;
749 insert into parent values (NULL);
750 insert into child (a, b) values (NULL, 'foo');
751 alter table only parent alter a set not null;
752 ERROR:  column "a" contains null values
753 alter table child alter a set not null;
754 ERROR:  column "a" contains null values
755 delete from parent;
756 alter table only parent alter a set not null;
757 insert into parent values (NULL);
758 ERROR:  null value in column "a" violates not-null constraint
759 DETAIL:  Failing row contains (null).
760 alter table child alter a set not null;
761 insert into child (a, b) values (NULL, 'foo');
762 ERROR:  null value in column "a" violates not-null constraint
763 DETAIL:  Failing row contains (null, foo).
764 delete from child;
765 alter table child alter a set not null;
766 insert into child (a, b) values (NULL, 'foo');
767 ERROR:  null value in column "a" violates not-null constraint
768 DETAIL:  Failing row contains (null, foo).
769 drop table child;
770 drop table parent;
771 -- test setting and removing default values
772 create table def_test (
773         c1      int4 default 5,
774         c2      text default 'initial_default'
775 );
776 insert into def_test default values;
777 alter table def_test alter column c1 drop default;
778 insert into def_test default values;
779 alter table def_test alter column c2 drop default;
780 insert into def_test default values;
781 alter table def_test alter column c1 set default 10;
782 alter table def_test alter column c2 set default 'new_default';
783 insert into def_test default values;
784 select * from def_test;
785  c1 |       c2        
786 ----+-----------------
787   5 | initial_default
788     | initial_default
789     | 
790  10 | new_default
791 (4 rows)
792
793 -- set defaults to an incorrect type: this should fail
794 alter table def_test alter column c1 set default 'wrong_datatype';
795 ERROR:  invalid input syntax for integer: "wrong_datatype"
796 alter table def_test alter column c2 set default 20;
797 -- set defaults on a non-existent column: this should fail
798 alter table def_test alter column c3 set default 30;
799 ERROR:  column "c3" of relation "def_test" does not exist
800 -- set defaults on views: we need to create a view, add a rule
801 -- to allow insertions into it, and then alter the view to add
802 -- a default
803 create view def_view_test as select * from def_test;
804 create rule def_view_test_ins as
805         on insert to def_view_test
806         do instead insert into def_test select new.*;
807 insert into def_view_test default values;
808 alter table def_view_test alter column c1 set default 45;
809 insert into def_view_test default values;
810 alter table def_view_test alter column c2 set default 'view_default';
811 insert into def_view_test default values;
812 select * from def_view_test;
813  c1 |       c2        
814 ----+-----------------
815   5 | initial_default
816     | initial_default
817     | 
818  10 | new_default
819     | 
820  45 | 
821  45 | view_default
822 (7 rows)
823
824 drop rule def_view_test_ins on def_view_test;
825 drop view def_view_test;
826 drop table def_test;
827 -- alter table / drop column tests
828 -- try altering system catalogs, should fail
829 alter table pg_class drop column relname;
830 ERROR:  permission denied: "pg_class" is a system catalog
831 -- try altering non-existent table, should fail
832 alter table nosuchtable drop column bar;
833 ERROR:  relation "nosuchtable" does not exist
834 -- test dropping columns
835 create table atacc1 (a int4 not null, b int4, c int4 not null, d int4) with oids;
836 insert into atacc1 values (1, 2, 3, 4);
837 alter table atacc1 drop a;
838 alter table atacc1 drop a;
839 ERROR:  column "a" of relation "atacc1" does not exist
840 -- SELECTs
841 select * from atacc1;
842  b | c | d 
843 ---+---+---
844  2 | 3 | 4
845 (1 row)
846
847 select * from atacc1 order by a;
848 ERROR:  column "a" does not exist
849 LINE 1: select * from atacc1 order by a;
850                                       ^
851 select * from atacc1 order by "........pg.dropped.1........";
852 ERROR:  column "........pg.dropped.1........" does not exist
853 LINE 1: select * from atacc1 order by "........pg.dropped.1........"...
854                                       ^
855 select * from atacc1 group by a;
856 ERROR:  column "a" does not exist
857 LINE 1: select * from atacc1 group by a;
858                                       ^
859 select * from atacc1 group by "........pg.dropped.1........";
860 ERROR:  column "........pg.dropped.1........" does not exist
861 LINE 1: select * from atacc1 group by "........pg.dropped.1........"...
862                                       ^
863 select atacc1.* from atacc1;
864  b | c | d 
865 ---+---+---
866  2 | 3 | 4
867 (1 row)
868
869 select a from atacc1;
870 ERROR:  column "a" does not exist
871 LINE 1: select a from atacc1;
872                ^
873 select atacc1.a from atacc1;
874 ERROR:  column atacc1.a does not exist
875 LINE 1: select atacc1.a from atacc1;
876                ^
877 select b,c,d from atacc1;
878  b | c | d 
879 ---+---+---
880  2 | 3 | 4
881 (1 row)
882
883 select a,b,c,d from atacc1;
884 ERROR:  column "a" does not exist
885 LINE 1: select a,b,c,d from atacc1;
886                ^
887 select * from atacc1 where a = 1;
888 ERROR:  column "a" does not exist
889 LINE 1: select * from atacc1 where a = 1;
890                                    ^
891 select "........pg.dropped.1........" from atacc1;
892 ERROR:  column "........pg.dropped.1........" does not exist
893 LINE 1: select "........pg.dropped.1........" from atacc1;
894                ^
895 select atacc1."........pg.dropped.1........" from atacc1;
896 ERROR:  column atacc1.........pg.dropped.1........ does not exist
897 LINE 1: select atacc1."........pg.dropped.1........" from atacc1;
898                ^
899 select "........pg.dropped.1........",b,c,d from atacc1;
900 ERROR:  column "........pg.dropped.1........" does not exist
901 LINE 1: select "........pg.dropped.1........",b,c,d from atacc1;
902                ^
903 select * from atacc1 where "........pg.dropped.1........" = 1;
904 ERROR:  column "........pg.dropped.1........" does not exist
905 LINE 1: select * from atacc1 where "........pg.dropped.1........" = ...
906                                    ^
907 -- UPDATEs
908 update atacc1 set a = 3;
909 ERROR:  column "a" of relation "atacc1" does not exist
910 LINE 1: update atacc1 set a = 3;
911                           ^
912 update atacc1 set b = 2 where a = 3;
913 ERROR:  column "a" does not exist
914 LINE 1: update atacc1 set b = 2 where a = 3;
915                                       ^
916 update atacc1 set "........pg.dropped.1........" = 3;
917 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
918 LINE 1: update atacc1 set "........pg.dropped.1........" = 3;
919                           ^
920 update atacc1 set b = 2 where "........pg.dropped.1........" = 3;
921 ERROR:  column "........pg.dropped.1........" does not exist
922 LINE 1: update atacc1 set b = 2 where "........pg.dropped.1........"...
923                                       ^
924 -- INSERTs
925 insert into atacc1 values (10, 11, 12, 13);
926 ERROR:  INSERT has more expressions than target columns
927 LINE 1: insert into atacc1 values (10, 11, 12, 13);
928                                                ^
929 insert into atacc1 values (default, 11, 12, 13);
930 ERROR:  INSERT has more expressions than target columns
931 LINE 1: insert into atacc1 values (default, 11, 12, 13);
932                                                     ^
933 insert into atacc1 values (11, 12, 13);
934 insert into atacc1 (a) values (10);
935 ERROR:  column "a" of relation "atacc1" does not exist
936 LINE 1: insert into atacc1 (a) values (10);
937                             ^
938 insert into atacc1 (a) values (default);
939 ERROR:  column "a" of relation "atacc1" does not exist
940 LINE 1: insert into atacc1 (a) values (default);
941                             ^
942 insert into atacc1 (a,b,c,d) values (10,11,12,13);
943 ERROR:  column "a" of relation "atacc1" does not exist
944 LINE 1: insert into atacc1 (a,b,c,d) values (10,11,12,13);
945                             ^
946 insert into atacc1 (a,b,c,d) values (default,11,12,13);
947 ERROR:  column "a" of relation "atacc1" does not exist
948 LINE 1: insert into atacc1 (a,b,c,d) values (default,11,12,13);
949                             ^
950 insert into atacc1 (b,c,d) values (11,12,13);
951 insert into atacc1 ("........pg.dropped.1........") values (10);
952 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
953 LINE 1: insert into atacc1 ("........pg.dropped.1........") values (...
954                             ^
955 insert into atacc1 ("........pg.dropped.1........") values (default);
956 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
957 LINE 1: insert into atacc1 ("........pg.dropped.1........") values (...
958                             ^
959 insert into atacc1 ("........pg.dropped.1........",b,c,d) values (10,11,12,13);
960 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
961 LINE 1: insert into atacc1 ("........pg.dropped.1........",b,c,d) va...
962                             ^
963 insert into atacc1 ("........pg.dropped.1........",b,c,d) values (default,11,12,13);
964 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
965 LINE 1: insert into atacc1 ("........pg.dropped.1........",b,c,d) va...
966                             ^
967 -- DELETEs
968 delete from atacc1 where a = 3;
969 ERROR:  column "a" does not exist
970 LINE 1: delete from atacc1 where a = 3;
971                                  ^
972 delete from atacc1 where "........pg.dropped.1........" = 3;
973 ERROR:  column "........pg.dropped.1........" does not exist
974 LINE 1: delete from atacc1 where "........pg.dropped.1........" = 3;
975                                  ^
976 delete from atacc1;
977 -- try dropping a non-existent column, should fail
978 alter table atacc1 drop bar;
979 ERROR:  column "bar" of relation "atacc1" does not exist
980 -- try dropping the oid column, should succeed
981 alter table atacc1 drop oid;
982 -- try dropping the xmin column, should fail
983 alter table atacc1 drop xmin;
984 ERROR:  cannot drop system column "xmin"
985 -- try creating a view and altering that, should fail
986 create view myview as select * from atacc1;
987 select * from myview;
988  b | c | d 
989 ---+---+---
990 (0 rows)
991
992 alter table myview drop d;
993 ERROR:  "myview" is not a table, composite type, or foreign table
994 drop view myview;
995 -- test some commands to make sure they fail on the dropped column
996 analyze atacc1(a);
997 ERROR:  column "a" of relation "atacc1" does not exist
998 analyze atacc1("........pg.dropped.1........");
999 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
1000 vacuum analyze atacc1(a);
1001 ERROR:  column "a" of relation "atacc1" does not exist
1002 vacuum analyze atacc1("........pg.dropped.1........");
1003 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
1004 comment on column atacc1.a is 'testing';
1005 ERROR:  column "a" of relation "atacc1" does not exist
1006 comment on column atacc1."........pg.dropped.1........" is 'testing';
1007 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
1008 alter table atacc1 alter a set storage plain;
1009 ERROR:  column "a" of relation "atacc1" does not exist
1010 alter table atacc1 alter "........pg.dropped.1........" set storage plain;
1011 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
1012 alter table atacc1 alter a set statistics 0;
1013 ERROR:  column "a" of relation "atacc1" does not exist
1014 alter table atacc1 alter "........pg.dropped.1........" set statistics 0;
1015 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
1016 alter table atacc1 alter a set default 3;
1017 ERROR:  column "a" of relation "atacc1" does not exist
1018 alter table atacc1 alter "........pg.dropped.1........" set default 3;
1019 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
1020 alter table atacc1 alter a drop default;
1021 ERROR:  column "a" of relation "atacc1" does not exist
1022 alter table atacc1 alter "........pg.dropped.1........" drop default;
1023 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
1024 alter table atacc1 alter a set not null;
1025 ERROR:  column "a" of relation "atacc1" does not exist
1026 alter table atacc1 alter "........pg.dropped.1........" set not null;
1027 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
1028 alter table atacc1 alter a drop not null;
1029 ERROR:  column "a" of relation "atacc1" does not exist
1030 alter table atacc1 alter "........pg.dropped.1........" drop not null;
1031 ERROR:  column "........pg.dropped.1........" of relation "atacc1" does not exist
1032 alter table atacc1 rename a to x;
1033 ERROR:  column "a" does not exist
1034 alter table atacc1 rename "........pg.dropped.1........" to x;
1035 ERROR:  column "........pg.dropped.1........" does not exist
1036 alter table atacc1 add primary key(a);
1037 ERROR:  column "a" named in key does not exist
1038 alter table atacc1 add primary key("........pg.dropped.1........");
1039 ERROR:  column "........pg.dropped.1........" named in key does not exist
1040 alter table atacc1 add unique(a);
1041 ERROR:  column "a" named in key does not exist
1042 alter table atacc1 add unique("........pg.dropped.1........");
1043 ERROR:  column "........pg.dropped.1........" named in key does not exist
1044 alter table atacc1 add check (a > 3);
1045 ERROR:  column "a" does not exist
1046 alter table atacc1 add check ("........pg.dropped.1........" > 3);
1047 ERROR:  column "........pg.dropped.1........" does not exist
1048 create table atacc2 (id int4 unique);
1049 NOTICE:  CREATE TABLE / UNIQUE will create implicit index "atacc2_id_key" for table "atacc2"
1050 alter table atacc1 add foreign key (a) references atacc2(id);
1051 ERROR:  column "a" referenced in foreign key constraint does not exist
1052 alter table atacc1 add foreign key ("........pg.dropped.1........") references atacc2(id);
1053 ERROR:  column "........pg.dropped.1........" referenced in foreign key constraint does not exist
1054 alter table atacc2 add foreign key (id) references atacc1(a);
1055 ERROR:  column "a" referenced in foreign key constraint does not exist
1056 alter table atacc2 add foreign key (id) references atacc1("........pg.dropped.1........");
1057 ERROR:  column "........pg.dropped.1........" referenced in foreign key constraint does not exist
1058 drop table atacc2;
1059 create index "testing_idx" on atacc1(a);
1060 ERROR:  column "a" does not exist
1061 create index "testing_idx" on atacc1("........pg.dropped.1........");
1062 ERROR:  column "........pg.dropped.1........" does not exist
1063 -- test create as and select into
1064 insert into atacc1 values (21, 22, 23);
1065 create table test1 as select * from atacc1;
1066 select * from test1;
1067  b  | c  | d  
1068 ----+----+----
1069  21 | 22 | 23
1070 (1 row)
1071
1072 drop table test1;
1073 select * into test2 from atacc1;
1074 select * from test2;
1075  b  | c  | d  
1076 ----+----+----
1077  21 | 22 | 23
1078 (1 row)
1079
1080 drop table test2;
1081 -- try dropping all columns
1082 alter table atacc1 drop c;
1083 alter table atacc1 drop d;
1084 alter table atacc1 drop b;
1085 select * from atacc1;
1086 --
1087 (1 row)
1088
1089 drop table atacc1;
1090 -- test inheritance
1091 create table parent (a int, b int, c int);
1092 insert into parent values (1, 2, 3);
1093 alter table parent drop a;
1094 create table child (d varchar(255)) inherits (parent);
1095 insert into child values (12, 13, 'testing');
1096 select * from parent;
1097  b  | c  
1098 ----+----
1099   2 |  3
1100  12 | 13
1101 (2 rows)
1102
1103 select * from child;
1104  b  | c  |    d    
1105 ----+----+---------
1106  12 | 13 | testing
1107 (1 row)
1108
1109 alter table parent drop c;
1110 select * from parent;
1111  b  
1112 ----
1113   2
1114  12
1115 (2 rows)
1116
1117 select * from child;
1118  b  |    d    
1119 ----+---------
1120  12 | testing
1121 (1 row)
1122
1123 drop table child;
1124 drop table parent;
1125 -- test copy in/out
1126 create table test (a int4, b int4, c int4);
1127 insert into test values (1,2,3);
1128 alter table test drop a;
1129 copy test to stdout;
1130 2       3
1131 copy test(a) to stdout;
1132 ERROR:  column "a" of relation "test" does not exist
1133 copy test("........pg.dropped.1........") to stdout;
1134 ERROR:  column "........pg.dropped.1........" of relation "test" does not exist
1135 copy test from stdin;
1136 ERROR:  extra data after last expected column
1137 CONTEXT:  COPY test, line 1: "10        11      12"
1138 select * from test;
1139  b | c 
1140 ---+---
1141  2 | 3
1142 (1 row)
1143
1144 copy test from stdin;
1145 select * from test;
1146  b  | c  
1147 ----+----
1148   2 |  3
1149  21 | 22
1150 (2 rows)
1151
1152 copy test(a) from stdin;
1153 ERROR:  column "a" of relation "test" does not exist
1154 copy test("........pg.dropped.1........") from stdin;
1155 ERROR:  column "........pg.dropped.1........" of relation "test" does not exist
1156 copy test(b,c) from stdin;
1157 select * from test;
1158  b  | c  
1159 ----+----
1160   2 |  3
1161  21 | 22
1162  31 | 32
1163 (3 rows)
1164
1165 drop table test;
1166 -- test inheritance
1167 create table dropColumn (a int, b int, e int);
1168 create table dropColumnChild (c int) inherits (dropColumn);
1169 create table dropColumnAnother (d int) inherits (dropColumnChild);
1170 -- these two should fail
1171 alter table dropColumnchild drop column a;
1172 ERROR:  cannot drop inherited column "a"
1173 alter table only dropColumnChild drop column b;
1174 ERROR:  cannot drop inherited column "b"
1175 -- these three should work
1176 alter table only dropColumn drop column e;
1177 alter table dropColumnChild drop column c;
1178 alter table dropColumn drop column a;
1179 create table renameColumn (a int);
1180 create table renameColumnChild (b int) inherits (renameColumn);
1181 create table renameColumnAnother (c int) inherits (renameColumnChild);
1182 -- these three should fail
1183 alter table renameColumnChild rename column a to d;
1184 ERROR:  cannot rename inherited column "a"
1185 alter table only renameColumnChild rename column a to d;
1186 ERROR:  inherited column "a" must be renamed in child tables too
1187 alter table only renameColumn rename column a to d;
1188 ERROR:  inherited column "a" must be renamed in child tables too
1189 -- these should work
1190 alter table renameColumn rename column a to d;
1191 alter table renameColumnChild rename column b to a;
1192 -- these should work
1193 alter table if exists doesnt_exist_tab rename column a to d;
1194 NOTICE:  relation "doesnt_exist_tab" does not exist, skipping
1195 alter table if exists doesnt_exist_tab rename column b to a;
1196 NOTICE:  relation "doesnt_exist_tab" does not exist, skipping
1197 -- this should work
1198 alter table renameColumn add column w int;
1199 -- this should fail
1200 alter table only renameColumn add column x int;
1201 ERROR:  column must be added to child tables too
1202 -- Test corner cases in dropping of inherited columns
1203 create table p1 (f1 int, f2 int);
1204 create table c1 (f1 int not null) inherits(p1);
1205 NOTICE:  merging column "f1" with inherited definition
1206 -- should be rejected since c1.f1 is inherited
1207 alter table c1 drop column f1;
1208 ERROR:  cannot drop inherited column "f1"
1209 -- should work
1210 alter table p1 drop column f1;
1211 -- c1.f1 is still there, but no longer inherited
1212 select f1 from c1;
1213  f1 
1214 ----
1215 (0 rows)
1216
1217 alter table c1 drop column f1;
1218 select f1 from c1;
1219 ERROR:  column "f1" does not exist
1220 LINE 1: select f1 from c1;
1221                ^
1222 drop table p1 cascade;
1223 NOTICE:  drop cascades to table c1
1224 create table p1 (f1 int, f2 int);
1225 create table c1 () inherits(p1);
1226 -- should be rejected since c1.f1 is inherited
1227 alter table c1 drop column f1;
1228 ERROR:  cannot drop inherited column "f1"
1229 alter table p1 drop column f1;
1230 -- c1.f1 is dropped now, since there is no local definition for it
1231 select f1 from c1;
1232 ERROR:  column "f1" does not exist
1233 LINE 1: select f1 from c1;
1234                ^
1235 drop table p1 cascade;
1236 NOTICE:  drop cascades to table c1
1237 create table p1 (f1 int, f2 int);
1238 create table c1 () inherits(p1);
1239 -- should be rejected since c1.f1 is inherited
1240 alter table c1 drop column f1;
1241 ERROR:  cannot drop inherited column "f1"
1242 alter table only p1 drop column f1;
1243 -- c1.f1 is NOT dropped, but must now be considered non-inherited
1244 alter table c1 drop column f1;
1245 drop table p1 cascade;
1246 NOTICE:  drop cascades to table c1
1247 create table p1 (f1 int, f2 int);
1248 create table c1 (f1 int not null) inherits(p1);
1249 NOTICE:  merging column "f1" with inherited definition
1250 -- should be rejected since c1.f1 is inherited
1251 alter table c1 drop column f1;
1252 ERROR:  cannot drop inherited column "f1"
1253 alter table only p1 drop column f1;
1254 -- c1.f1 is still there, but no longer inherited
1255 alter table c1 drop column f1;
1256 drop table p1 cascade;
1257 NOTICE:  drop cascades to table c1
1258 create table p1(id int, name text);
1259 create table p2(id2 int, name text, height int);
1260 create table c1(age int) inherits(p1,p2);
1261 NOTICE:  merging multiple inherited definitions of column "name"
1262 create table gc1() inherits (c1);
1263 select relname, attname, attinhcount, attislocal
1264 from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
1265 where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
1266 order by relname, attnum;
1267  relname | attname | attinhcount | attislocal 
1268 ---------+---------+-------------+------------
1269  c1      | id      |           1 | f
1270  c1      | name    |           2 | f
1271  c1      | id2     |           1 | f
1272  c1      | height  |           1 | f
1273  c1      | age     |           0 | t
1274  gc1     | id      |           1 | f
1275  gc1     | name    |           1 | f
1276  gc1     | id2     |           1 | f
1277  gc1     | height  |           1 | f
1278  gc1     | age     |           1 | f
1279  p1      | id      |           0 | t
1280  p1      | name    |           0 | t
1281  p2      | id2     |           0 | t
1282  p2      | name    |           0 | t
1283  p2      | height  |           0 | t
1284 (15 rows)
1285
1286 -- should work
1287 alter table only p1 drop column name;
1288 -- should work. Now c1.name is local and inhcount is 0.
1289 alter table p2 drop column name;
1290 -- should be rejected since its inherited
1291 alter table gc1 drop column name;
1292 ERROR:  cannot drop inherited column "name"
1293 -- should work, and drop gc1.name along
1294 alter table c1 drop column name;
1295 -- should fail: column does not exist
1296 alter table gc1 drop column name;
1297 ERROR:  column "name" of relation "gc1" does not exist
1298 -- should work and drop the attribute in all tables
1299 alter table p2 drop column height;
1300 -- IF EXISTS test
1301 create table dropColumnExists ();
1302 alter table dropColumnExists drop column non_existing; --fail
1303 ERROR:  column "non_existing" of relation "dropcolumnexists" does not exist
1304 alter table dropColumnExists drop column if exists non_existing; --succeed
1305 NOTICE:  column "non_existing" of relation "dropcolumnexists" does not exist, skipping
1306 select relname, attname, attinhcount, attislocal
1307 from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
1308 where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
1309 order by relname, attnum;
1310  relname | attname | attinhcount | attislocal 
1311 ---------+---------+-------------+------------
1312  c1      | id      |           1 | f
1313  c1      | id2     |           1 | f
1314  c1      | age     |           0 | t
1315  gc1     | id      |           1 | f
1316  gc1     | id2     |           1 | f
1317  gc1     | age     |           1 | f
1318  p1      | id      |           0 | t
1319  p2      | id2     |           0 | t
1320 (8 rows)
1321
1322 drop table p1, p2 cascade;
1323 NOTICE:  drop cascades to 2 other objects
1324 DETAIL:  drop cascades to table c1
1325 drop cascades to table gc1
1326 -- test attinhcount tracking with merged columns
1327 create table depth0();
1328 create table depth1(c text) inherits (depth0);
1329 create table depth2() inherits (depth1);
1330 alter table depth0 add c text;
1331 NOTICE:  merging definition of column "c" for child "depth1"
1332 select attrelid::regclass, attname, attinhcount, attislocal
1333 from pg_attribute
1334 where attnum > 0 and attrelid::regclass in ('depth0', 'depth1', 'depth2')
1335 order by attrelid::regclass::text, attnum;
1336  attrelid | attname | attinhcount | attislocal 
1337 ----------+---------+-------------+------------
1338  depth0   | c       |           0 | t
1339  depth1   | c       |           1 | t
1340  depth2   | c       |           1 | f
1341 (3 rows)
1342
1343 --
1344 -- Test the ALTER TABLE SET WITH/WITHOUT OIDS command
1345 --
1346 create table altstartwith (col integer) with oids;
1347 insert into altstartwith values (1);
1348 select oid > 0, * from altstartwith;
1349  ?column? | col 
1350 ----------+-----
1351  t        |   1
1352 (1 row)
1353
1354 alter table altstartwith set without oids;
1355 select oid > 0, * from altstartwith; -- fails
1356 ERROR:  column "oid" does not exist
1357 LINE 1: select oid > 0, * from altstartwith;
1358                ^
1359 select * from altstartwith;
1360  col 
1361 -----
1362    1
1363 (1 row)
1364
1365 alter table altstartwith set with oids;
1366 select oid > 0, * from altstartwith;
1367  ?column? | col 
1368 ----------+-----
1369  t        |   1
1370 (1 row)
1371
1372 drop table altstartwith;
1373 -- Check inheritance cases
1374 create table altwithoid (col integer) with oids;
1375 -- Inherits parents oid column anyway
1376 create table altinhoid () inherits (altwithoid) without oids;
1377 insert into altinhoid values (1);
1378 select oid > 0, * from altwithoid;
1379  ?column? | col 
1380 ----------+-----
1381  t        |   1
1382 (1 row)
1383
1384 select oid > 0, * from altinhoid;
1385  ?column? | col 
1386 ----------+-----
1387  t        |   1
1388 (1 row)
1389
1390 alter table altwithoid set without oids;
1391 select oid > 0, * from altwithoid; -- fails
1392 ERROR:  column "oid" does not exist
1393 LINE 1: select oid > 0, * from altwithoid;
1394                ^
1395 select oid > 0, * from altinhoid; -- fails
1396 ERROR:  column "oid" does not exist
1397 LINE 1: select oid > 0, * from altinhoid;
1398                ^
1399 select * from altwithoid;
1400  col 
1401 -----
1402    1
1403 (1 row)
1404
1405 select * from altinhoid;
1406  col 
1407 -----
1408    1
1409 (1 row)
1410
1411 alter table altwithoid set with oids;
1412 select oid > 0, * from altwithoid;
1413  ?column? | col 
1414 ----------+-----
1415  t        |   1
1416 (1 row)
1417
1418 select oid > 0, * from altinhoid;
1419  ?column? | col 
1420 ----------+-----
1421  t        |   1
1422 (1 row)
1423
1424 drop table altwithoid cascade;
1425 NOTICE:  drop cascades to table altinhoid
1426 create table altwithoid (col integer) without oids;
1427 -- child can have local oid column
1428 create table altinhoid () inherits (altwithoid) with oids;
1429 insert into altinhoid values (1);
1430 select oid > 0, * from altwithoid; -- fails
1431 ERROR:  column "oid" does not exist
1432 LINE 1: select oid > 0, * from altwithoid;
1433                ^
1434 select oid > 0, * from altinhoid;
1435  ?column? | col 
1436 ----------+-----
1437  t        |   1
1438 (1 row)
1439
1440 alter table altwithoid set with oids;
1441 NOTICE:  merging definition of column "oid" for child "altinhoid"
1442 select oid > 0, * from altwithoid;
1443  ?column? | col 
1444 ----------+-----
1445  t        |   1
1446 (1 row)
1447
1448 select oid > 0, * from altinhoid;
1449  ?column? | col 
1450 ----------+-----
1451  t        |   1
1452 (1 row)
1453
1454 -- the child's local definition should remain
1455 alter table altwithoid set without oids;
1456 select oid > 0, * from altwithoid; -- fails
1457 ERROR:  column "oid" does not exist
1458 LINE 1: select oid > 0, * from altwithoid;
1459                ^
1460 select oid > 0, * from altinhoid;
1461  ?column? | col 
1462 ----------+-----
1463  t        |   1
1464 (1 row)
1465
1466 drop table altwithoid cascade;
1467 NOTICE:  drop cascades to table altinhoid
1468 -- test renumbering of child-table columns in inherited operations
1469 create table p1 (f1 int);
1470 create table c1 (f2 text, f3 int) inherits (p1);
1471 alter table p1 add column a1 int check (a1 > 0);
1472 alter table p1 add column f2 text;
1473 NOTICE:  merging definition of column "f2" for child "c1"
1474 insert into p1 values (1,2,'abc');
1475 insert into c1 values(11,'xyz',33,0); -- should fail
1476 ERROR:  new row for relation "c1" violates check constraint "p1_a1_check"
1477 DETAIL:  Failing row contains (11, xyz, 33, 0).
1478 insert into c1 values(11,'xyz',33,22);
1479 select * from p1;
1480  f1 | a1 | f2  
1481 ----+----+-----
1482   1 |  2 | abc
1483  11 | 22 | xyz
1484 (2 rows)
1485
1486 update p1 set a1 = a1 + 1, f2 = upper(f2);
1487 select * from p1;
1488  f1 | a1 | f2  
1489 ----+----+-----
1490   1 |  3 | ABC
1491  11 | 23 | XYZ
1492 (2 rows)
1493
1494 drop table p1 cascade;
1495 NOTICE:  drop cascades to table c1
1496 -- test that operations with a dropped column do not try to reference
1497 -- its datatype
1498 create domain mytype as text;
1499 create temp table foo (f1 text, f2 mytype, f3 text);
1500 insert into foo values('bb','cc','dd');
1501 select * from foo;
1502  f1 | f2 | f3 
1503 ----+----+----
1504  bb | cc | dd
1505 (1 row)
1506
1507 drop domain mytype cascade;
1508 NOTICE:  drop cascades to table foo column f2
1509 select * from foo;
1510  f1 | f3 
1511 ----+----
1512  bb | dd
1513 (1 row)
1514
1515 insert into foo values('qq','rr');
1516 select * from foo;
1517  f1 | f3 
1518 ----+----
1519  bb | dd
1520  qq | rr
1521 (2 rows)
1522
1523 update foo set f3 = 'zz';
1524 select * from foo;
1525  f1 | f3 
1526 ----+----
1527  bb | zz
1528  qq | zz
1529 (2 rows)
1530
1531 select f3,max(f1) from foo group by f3;
1532  f3 | max 
1533 ----+-----
1534  zz | qq
1535 (1 row)
1536
1537 -- Simple tests for alter table column type
1538 alter table foo alter f1 TYPE integer; -- fails
1539 ERROR:  column "f1" cannot be cast to type integer
1540 alter table foo alter f1 TYPE varchar(10);
1541 create table anothertab (atcol1 serial8, atcol2 boolean,
1542         constraint anothertab_chk check (atcol1 <= 3));
1543 NOTICE:  CREATE TABLE will create implicit sequence "anothertab_atcol1_seq" for serial column "anothertab.atcol1"
1544 insert into anothertab (atcol1, atcol2) values (default, true);
1545 insert into anothertab (atcol1, atcol2) values (default, false);
1546 select * from anothertab;
1547  atcol1 | atcol2 
1548 --------+--------
1549       1 | t
1550       2 | f
1551 (2 rows)
1552
1553 alter table anothertab alter column atcol1 type boolean; -- fails
1554 ERROR:  column "atcol1" cannot be cast to type boolean
1555 alter table anothertab alter column atcol1 type integer;
1556 select * from anothertab;
1557  atcol1 | atcol2 
1558 --------+--------
1559       1 | t
1560       2 | f
1561 (2 rows)
1562
1563 insert into anothertab (atcol1, atcol2) values (45, null); -- fails
1564 ERROR:  new row for relation "anothertab" violates check constraint "anothertab_chk"
1565 DETAIL:  Failing row contains (45, null).
1566 insert into anothertab (atcol1, atcol2) values (default, null);
1567 select * from anothertab;
1568  atcol1 | atcol2 
1569 --------+--------
1570       1 | t
1571       2 | f
1572       3 | 
1573 (3 rows)
1574
1575 alter table anothertab alter column atcol2 type text
1576       using case when atcol2 is true then 'IT WAS TRUE'
1577                  when atcol2 is false then 'IT WAS FALSE'
1578                  else 'IT WAS NULL!' end;
1579 select * from anothertab;
1580  atcol1 |    atcol2    
1581 --------+--------------
1582       1 | IT WAS TRUE
1583       2 | IT WAS FALSE
1584       3 | IT WAS NULL!
1585 (3 rows)
1586
1587 alter table anothertab alter column atcol1 type boolean
1588         using case when atcol1 % 2 = 0 then true else false end; -- fails
1589 ERROR:  default for column "atcol1" cannot be cast to type boolean
1590 alter table anothertab alter column atcol1 drop default;
1591 alter table anothertab alter column atcol1 type boolean
1592         using case when atcol1 % 2 = 0 then true else false end; -- fails
1593 ERROR:  operator does not exist: boolean <= integer
1594 HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
1595 alter table anothertab drop constraint anothertab_chk;
1596 alter table anothertab drop constraint anothertab_chk; -- fails
1597 ERROR:  constraint "anothertab_chk" of relation "anothertab" does not exist
1598 alter table anothertab drop constraint IF EXISTS anothertab_chk; -- succeeds
1599 NOTICE:  constraint "anothertab_chk" of relation "anothertab" does not exist, skipping
1600 alter table anothertab alter column atcol1 type boolean
1601         using case when atcol1 % 2 = 0 then true else false end;
1602 select * from anothertab;
1603  atcol1 |    atcol2    
1604 --------+--------------
1605  f      | IT WAS TRUE
1606  t      | IT WAS FALSE
1607  f      | IT WAS NULL!
1608 (3 rows)
1609
1610 drop table anothertab;
1611 create table another (f1 int, f2 text);
1612 insert into another values(1, 'one');
1613 insert into another values(2, 'two');
1614 insert into another values(3, 'three');
1615 select * from another;
1616  f1 |  f2   
1617 ----+-------
1618   1 | one
1619   2 | two
1620   3 | three
1621 (3 rows)
1622
1623 alter table another
1624   alter f1 type text using f2 || ' more',
1625   alter f2 type bigint using f1 * 10;
1626 select * from another;
1627      f1     | f2 
1628 ------------+----
1629  one more   | 10
1630  two more   | 20
1631  three more | 30
1632 (3 rows)
1633
1634 drop table another;
1635 -- table's row type
1636 create table tab1 (a int, b text);
1637 create table tab2 (x int, y tab1);
1638 alter table tab1 alter column b type varchar; -- fails
1639 ERROR:  cannot alter table "tab1" because column "tab2.y" uses its row type
1640 -- disallow recursive containment of row types
1641 create temp table recur1 (f1 int);
1642 alter table recur1 add column f2 recur1; -- fails
1643 ERROR:  composite type recur1 cannot be made a member of itself
1644 alter table recur1 add column f2 recur1[]; -- fails
1645 ERROR:  composite type recur1 cannot be made a member of itself
1646 create domain array_of_recur1 as recur1[];
1647 alter table recur1 add column f2 array_of_recur1; -- fails
1648 ERROR:  composite type recur1 cannot be made a member of itself
1649 create temp table recur2 (f1 int, f2 recur1);
1650 alter table recur1 add column f2 recur2; -- fails
1651 ERROR:  composite type recur1 cannot be made a member of itself
1652 alter table recur1 add column f2 int;
1653 alter table recur1 alter column f2 type recur2; -- fails
1654 ERROR:  composite type recur1 cannot be made a member of itself
1655 -- SET STORAGE may need to add a TOAST table
1656 create table test_storage (a text);
1657 alter table test_storage alter a set storage plain;
1658 alter table test_storage add b int default 0; -- rewrite table to remove its TOAST table
1659 alter table test_storage alter a set storage extended; -- re-add TOAST table
1660 select reltoastrelid <> 0 as has_toast_table
1661 from pg_class
1662 where oid = 'test_storage'::regclass;
1663  has_toast_table 
1664 -----------------
1665  t
1666 (1 row)
1667
1668 --
1669 -- lock levels
1670 --
1671 drop type lockmodes;
1672 ERROR:  type "lockmodes" does not exist
1673 create type lockmodes as enum (
1674  'AccessShareLock'
1675 ,'RowShareLock'
1676 ,'RowExclusiveLock'
1677 ,'ShareUpdateExclusiveLock'
1678 ,'ShareLock'
1679 ,'ShareRowExclusiveLock'
1680 ,'ExclusiveLock'
1681 ,'AccessExclusiveLock'
1682 );
1683 drop view my_locks;
1684 ERROR:  view "my_locks" does not exist
1685 create or replace view my_locks as
1686 select case when c.relname like 'pg_toast%' then 'pg_toast' else c.relname end, max(mode::lockmodes) as max_lockmode
1687 from pg_locks l join pg_class c on l.relation = c.oid
1688 where virtualtransaction = (
1689         select virtualtransaction
1690         from pg_locks
1691         where transactionid = txid_current()::integer)
1692 and locktype = 'relation'
1693 and relnamespace != (select oid from pg_namespace where nspname = 'pg_catalog')
1694 and c.relname != 'my_locks'
1695 group by c.relname;
1696 create table alterlock (f1 int primary key, f2 text);
1697 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "alterlock_pkey" for table "alterlock"
1698 begin; alter table alterlock alter column f2 set statistics 150;
1699 select * from my_locks order by 1;
1700   relname  |    max_lockmode     
1701 -----------+---------------------
1702  alterlock | AccessExclusiveLock
1703 (1 row)
1704
1705 rollback;
1706 begin; alter table alterlock cluster on alterlock_pkey;
1707 select * from my_locks order by 1;
1708     relname     |    max_lockmode     
1709 ----------------+---------------------
1710  alterlock      | AccessExclusiveLock
1711  alterlock_pkey | AccessExclusiveLock
1712 (2 rows)
1713
1714 commit;
1715 begin; alter table alterlock set without cluster;
1716 select * from my_locks order by 1;
1717   relname  |    max_lockmode     
1718 -----------+---------------------
1719  alterlock | AccessExclusiveLock
1720 (1 row)
1721
1722 commit;
1723 begin; alter table alterlock set (fillfactor = 100);
1724 select * from my_locks order by 1;
1725   relname  |    max_lockmode     
1726 -----------+---------------------
1727  alterlock | AccessExclusiveLock
1728  pg_toast  | AccessExclusiveLock
1729 (2 rows)
1730
1731 commit;
1732 begin; alter table alterlock reset (fillfactor);
1733 select * from my_locks order by 1;
1734   relname  |    max_lockmode     
1735 -----------+---------------------
1736  alterlock | AccessExclusiveLock
1737  pg_toast  | AccessExclusiveLock
1738 (2 rows)
1739
1740 commit;
1741 begin; alter table alterlock set (toast.autovacuum_enabled = off);
1742 select * from my_locks order by 1;
1743   relname  |    max_lockmode     
1744 -----------+---------------------
1745  alterlock | AccessExclusiveLock
1746  pg_toast  | AccessExclusiveLock
1747 (2 rows)
1748
1749 commit;
1750 begin; alter table alterlock set (autovacuum_enabled = off);
1751 select * from my_locks order by 1;
1752   relname  |    max_lockmode     
1753 -----------+---------------------
1754  alterlock | AccessExclusiveLock
1755  pg_toast  | AccessExclusiveLock
1756 (2 rows)
1757
1758 commit;
1759 begin; alter table alterlock alter column f2 set (n_distinct = 1);
1760 select * from my_locks order by 1;
1761   relname  |    max_lockmode     
1762 -----------+---------------------
1763  alterlock | AccessExclusiveLock
1764 (1 row)
1765
1766 rollback;
1767 begin; alter table alterlock alter column f2 set storage extended;
1768 select * from my_locks order by 1;
1769   relname  |    max_lockmode     
1770 -----------+---------------------
1771  alterlock | AccessExclusiveLock
1772 (1 row)
1773
1774 rollback;
1775 begin; alter table alterlock alter column f2 set default 'x';
1776 select * from my_locks order by 1;
1777   relname  |    max_lockmode     
1778 -----------+---------------------
1779  alterlock | AccessExclusiveLock
1780 (1 row)
1781
1782 rollback;
1783 -- cleanup
1784 drop table alterlock;
1785 drop view my_locks;
1786 drop type lockmodes;
1787 --
1788 -- alter function
1789 --
1790 create function test_strict(text) returns text as
1791     'select coalesce($1, ''got passed a null'');'
1792     language sql returns null on null input;
1793 select test_strict(NULL);
1794  test_strict 
1795 -------------
1796  
1797 (1 row)
1798
1799 alter function test_strict(text) called on null input;
1800 select test_strict(NULL);
1801     test_strict    
1802 -------------------
1803  got passed a null
1804 (1 row)
1805
1806 create function non_strict(text) returns text as
1807     'select coalesce($1, ''got passed a null'');'
1808     language sql called on null input;
1809 select non_strict(NULL);
1810     non_strict     
1811 -------------------
1812  got passed a null
1813 (1 row)
1814
1815 alter function non_strict(text) returns null on null input;
1816 select non_strict(NULL);
1817  non_strict 
1818 ------------
1819  
1820 (1 row)
1821
1822 --
1823 -- alter object set schema
1824 --
1825 create schema alter1;
1826 create schema alter2;
1827 create table alter1.t1(f1 serial primary key, f2 int check (f2 > 0));
1828 NOTICE:  CREATE TABLE will create implicit sequence "t1_f1_seq" for serial column "t1.f1"
1829 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1"
1830 create view alter1.v1 as select * from alter1.t1;
1831 create function alter1.plus1(int) returns int as 'select $1+1' language sql;
1832 create domain alter1.posint integer check (value > 0);
1833 create type alter1.ctype as (f1 int, f2 text);
1834 create function alter1.same(alter1.ctype, alter1.ctype) returns boolean language sql 
1835 as 'select $1.f1 is not distinct from $2.f1 and $1.f2 is not distinct from $2.f2';
1836 create operator alter1.=(procedure = alter1.same, leftarg  = alter1.ctype, rightarg = alter1.ctype);
1837 create operator class alter1.ctype_hash_ops default for type alter1.ctype using hash as
1838   operator 1 alter1.=(alter1.ctype, alter1.ctype);
1839 create conversion alter1.ascii_to_utf8 for 'sql_ascii' to 'utf8' from ascii_to_utf8;
1840 create text search parser alter1.prs(start = prsd_start, gettoken = prsd_nexttoken, end = prsd_end, lextypes = prsd_lextype);
1841 create text search configuration alter1.cfg(parser = alter1.prs);
1842 create text search template alter1.tmpl(init = dsimple_init, lexize = dsimple_lexize);
1843 create text search dictionary alter1.dict(template = alter1.tmpl);
1844 insert into alter1.t1(f2) values(11);
1845 insert into alter1.t1(f2) values(12);
1846 alter table alter1.t1 set schema alter2;
1847 alter table alter1.v1 set schema alter2;
1848 alter function alter1.plus1(int) set schema alter2;
1849 alter domain alter1.posint set schema alter2;
1850 alter operator class alter1.ctype_hash_ops using hash set schema alter2;
1851 alter operator family alter1.ctype_hash_ops using hash set schema alter2;
1852 alter operator alter1.=(alter1.ctype, alter1.ctype) set schema alter2;
1853 alter function alter1.same(alter1.ctype, alter1.ctype) set schema alter2;
1854 alter type alter1.ctype set schema alter2;
1855 alter conversion alter1.ascii_to_utf8 set schema alter2;
1856 alter text search parser alter1.prs set schema alter2;
1857 alter text search configuration alter1.cfg set schema alter2;
1858 alter text search template alter1.tmpl set schema alter2;
1859 alter text search dictionary alter1.dict set schema alter2;
1860 -- this should succeed because nothing is left in alter1
1861 drop schema alter1;
1862 insert into alter2.t1(f2) values(13);
1863 insert into alter2.t1(f2) values(14);
1864 select * from alter2.t1;
1865  f1 | f2 
1866 ----+----
1867   1 | 11
1868   2 | 12
1869   3 | 13
1870   4 | 14
1871 (4 rows)
1872
1873 select * from alter2.v1;
1874  f1 | f2 
1875 ----+----
1876   1 | 11
1877   2 | 12
1878   3 | 13
1879   4 | 14
1880 (4 rows)
1881
1882 select alter2.plus1(41);
1883  plus1 
1884 -------
1885     42
1886 (1 row)
1887
1888 -- clean up
1889 drop schema alter2 cascade;
1890 NOTICE:  drop cascades to 13 other objects
1891 DETAIL:  drop cascades to table alter2.t1
1892 drop cascades to view alter2.v1
1893 drop cascades to function alter2.plus1(integer)
1894 drop cascades to type alter2.posint
1895 drop cascades to operator family alter2.ctype_hash_ops for access method hash
1896 drop cascades to type alter2.ctype
1897 drop cascades to function alter2.same(alter2.ctype,alter2.ctype)
1898 drop cascades to operator alter2.=(alter2.ctype,alter2.ctype)
1899 drop cascades to conversion ascii_to_utf8
1900 drop cascades to text search parser prs
1901 drop cascades to text search configuration cfg
1902 drop cascades to text search template tmpl
1903 drop cascades to text search dictionary dict
1904 --
1905 -- composite types
1906 --
1907 CREATE TYPE test_type AS (a int);
1908 \d test_type
1909 Composite type "public.test_type"
1910  Column |  Type   | Modifiers 
1911 --------+---------+-----------
1912  a      | integer | 
1913
1914 ALTER TYPE nosuchtype ADD ATTRIBUTE b text; -- fails
1915 ERROR:  relation "nosuchtype" does not exist
1916 ALTER TYPE test_type ADD ATTRIBUTE b text;
1917 \d test_type
1918 Composite type "public.test_type"
1919  Column |  Type   | Modifiers 
1920 --------+---------+-----------
1921  a      | integer | 
1922  b      | text    | 
1923
1924 ALTER TYPE test_type ADD ATTRIBUTE b text; -- fails
1925 ERROR:  column "b" of relation "test_type" already exists
1926 ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE varchar;
1927 \d test_type
1928    Composite type "public.test_type"
1929  Column |       Type        | Modifiers 
1930 --------+-------------------+-----------
1931  a      | integer           | 
1932  b      | character varying | 
1933
1934 ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE integer;
1935 \d test_type
1936 Composite type "public.test_type"
1937  Column |  Type   | Modifiers 
1938 --------+---------+-----------
1939  a      | integer | 
1940  b      | integer | 
1941
1942 ALTER TYPE test_type DROP ATTRIBUTE b;
1943 \d test_type
1944 Composite type "public.test_type"
1945  Column |  Type   | Modifiers 
1946 --------+---------+-----------
1947  a      | integer | 
1948
1949 ALTER TYPE test_type DROP ATTRIBUTE c; -- fails
1950 ERROR:  column "c" of relation "test_type" does not exist
1951 ALTER TYPE test_type DROP ATTRIBUTE IF EXISTS c;
1952 NOTICE:  column "c" of relation "test_type" does not exist, skipping
1953 ALTER TYPE test_type DROP ATTRIBUTE a, ADD ATTRIBUTE d boolean;
1954 \d test_type
1955 Composite type "public.test_type"
1956  Column |  Type   | Modifiers 
1957 --------+---------+-----------
1958  d      | boolean | 
1959
1960 ALTER TYPE test_type RENAME ATTRIBUTE a TO aa;
1961 ERROR:  column "a" does not exist
1962 ALTER TYPE test_type RENAME ATTRIBUTE d TO dd;
1963 \d test_type
1964 Composite type "public.test_type"
1965  Column |  Type   | Modifiers 
1966 --------+---------+-----------
1967  dd     | boolean | 
1968
1969 DROP TYPE test_type;
1970 CREATE TYPE test_type1 AS (a int, b text);
1971 CREATE TABLE test_tbl1 (x int, y test_type1);
1972 ALTER TYPE test_type1 ALTER ATTRIBUTE b TYPE varchar; -- fails
1973 ERROR:  cannot alter type "test_type1" because column "test_tbl1.y" uses it
1974 CREATE TYPE test_type2 AS (a int, b text);
1975 CREATE TABLE test_tbl2 OF test_type2;
1976 CREATE TABLE test_tbl2_subclass () INHERITS (test_tbl2);
1977 \d test_type2
1978 Composite type "public.test_type2"
1979  Column |  Type   | Modifiers 
1980 --------+---------+-----------
1981  a      | integer | 
1982  b      | text    | 
1983
1984 \d test_tbl2
1985    Table "public.test_tbl2"
1986  Column |  Type   | Modifiers 
1987 --------+---------+-----------
1988  a      | integer | 
1989  b      | text    | 
1990 Number of child tables: 1 (Use \d+ to list them.)
1991 Typed table of type: test_type2
1992
1993 ALTER TYPE test_type2 ADD ATTRIBUTE c text; -- fails
1994 ERROR:  cannot alter type "test_type2" because it is the type of a typed table
1995 HINT:  Use ALTER ... CASCADE to alter the typed tables too.
1996 ALTER TYPE test_type2 ADD ATTRIBUTE c text CASCADE;
1997 \d test_type2
1998 Composite type "public.test_type2"
1999  Column |  Type   | Modifiers 
2000 --------+---------+-----------
2001  a      | integer | 
2002  b      | text    | 
2003  c      | text    | 
2004
2005 \d test_tbl2
2006    Table "public.test_tbl2"
2007  Column |  Type   | Modifiers 
2008 --------+---------+-----------
2009  a      | integer | 
2010  b      | text    | 
2011  c      | text    | 
2012 Number of child tables: 1 (Use \d+ to list them.)
2013 Typed table of type: test_type2
2014
2015 ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar; -- fails
2016 ERROR:  cannot alter type "test_type2" because it is the type of a typed table
2017 HINT:  Use ALTER ... CASCADE to alter the typed tables too.
2018 ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar CASCADE;
2019 \d test_type2
2020    Composite type "public.test_type2"
2021  Column |       Type        | Modifiers 
2022 --------+-------------------+-----------
2023  a      | integer           | 
2024  b      | character varying | 
2025  c      | text              | 
2026
2027 \d test_tbl2
2028         Table "public.test_tbl2"
2029  Column |       Type        | Modifiers 
2030 --------+-------------------+-----------
2031  a      | integer           | 
2032  b      | character varying | 
2033  c      | text              | 
2034 Number of child tables: 1 (Use \d+ to list them.)
2035 Typed table of type: test_type2
2036
2037 ALTER TYPE test_type2 DROP ATTRIBUTE b; -- fails
2038 ERROR:  cannot alter type "test_type2" because it is the type of a typed table
2039 HINT:  Use ALTER ... CASCADE to alter the typed tables too.
2040 ALTER TYPE test_type2 DROP ATTRIBUTE b CASCADE;
2041 \d test_type2
2042 Composite type "public.test_type2"
2043  Column |  Type   | Modifiers 
2044 --------+---------+-----------
2045  a      | integer | 
2046  c      | text    | 
2047
2048 \d test_tbl2
2049    Table "public.test_tbl2"
2050  Column |  Type   | Modifiers 
2051 --------+---------+-----------
2052  a      | integer | 
2053  c      | text    | 
2054 Number of child tables: 1 (Use \d+ to list them.)
2055 Typed table of type: test_type2
2056
2057 ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa; -- fails
2058 ERROR:  cannot alter type "test_type2" because it is the type of a typed table
2059 HINT:  Use ALTER ... CASCADE to alter the typed tables too.
2060 ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
2061 \d test_type2
2062 Composite type "public.test_type2"
2063  Column |  Type   | Modifiers 
2064 --------+---------+-----------
2065  aa     | integer | 
2066  c      | text    | 
2067
2068 \d test_tbl2
2069    Table "public.test_tbl2"
2070  Column |  Type   | Modifiers 
2071 --------+---------+-----------
2072  aa     | integer | 
2073  c      | text    | 
2074 Number of child tables: 1 (Use \d+ to list them.)
2075 Typed table of type: test_type2
2076
2077 \d test_tbl2_subclass
2078 Table "public.test_tbl2_subclass"
2079  Column |  Type   | Modifiers 
2080 --------+---------+-----------
2081  aa     | integer | 
2082  c      | text    | 
2083 Inherits: test_tbl2
2084
2085 DROP TABLE test_tbl2_subclass;
2086 -- This test isn't that interesting on its own, but the purpose is to leave
2087 -- behind a table to test pg_upgrade with. The table has a composite type
2088 -- column in it, and the composite type has a dropped attribute.
2089 CREATE TYPE test_type3 AS (a int);
2090 CREATE TABLE test_tbl3 (c) AS SELECT '(1)'::test_type3;
2091 ALTER TYPE test_type3 DROP ATTRIBUTE a, ADD ATTRIBUTE b int;
2092 CREATE TYPE test_type_empty AS ();
2093 DROP TYPE test_type_empty;
2094 --
2095 -- typed tables: OF / NOT OF
2096 --
2097 CREATE TYPE tt_t0 AS (z inet, x int, y numeric(8,2));
2098 ALTER TYPE tt_t0 DROP ATTRIBUTE z;
2099 CREATE TABLE tt0 (x int NOT NULL, y numeric(8,2));      -- OK
2100 CREATE TABLE tt1 (x int, y bigint);                                     -- wrong base type
2101 CREATE TABLE tt2 (x int, y numeric(9,2));                       -- wrong typmod
2102 CREATE TABLE tt3 (y numeric(8,2), x int);                       -- wrong column order
2103 CREATE TABLE tt4 (x int);                                                       -- too few columns
2104 CREATE TABLE tt5 (x int, y numeric(8,2), z int);        -- too few columns
2105 CREATE TABLE tt6 () INHERITS (tt0);                                     -- can't have a parent
2106 CREATE TABLE tt7 (x int, q text, y numeric(8,2)) WITH OIDS;
2107 ALTER TABLE tt7 DROP q;                                                         -- OK
2108 ALTER TABLE tt0 OF tt_t0;
2109 ALTER TABLE tt1 OF tt_t0;
2110 ERROR:  table "tt1" has different type for column "y"
2111 ALTER TABLE tt2 OF tt_t0;
2112 ERROR:  table "tt2" has different type for column "y"
2113 ALTER TABLE tt3 OF tt_t0;
2114 ERROR:  table has column "y" where type requires "x"
2115 ALTER TABLE tt4 OF tt_t0;
2116 ERROR:  table is missing column "y"
2117 ALTER TABLE tt5 OF tt_t0;
2118 ERROR:  table has extra column "z"
2119 ALTER TABLE tt6 OF tt_t0;
2120 ERROR:  typed tables cannot inherit
2121 ALTER TABLE tt7 OF tt_t0;
2122 CREATE TYPE tt_t1 AS (x int, y numeric(8,2));
2123 ALTER TABLE tt7 OF tt_t1;                       -- reassign an already-typed table
2124 ALTER TABLE tt7 NOT OF;
2125 \d tt7
2126         Table "public.tt7"
2127  Column |     Type     | Modifiers 
2128 --------+--------------+-----------
2129  x      | integer      | 
2130  y      | numeric(8,2) | 
2131
2132 -- make sure we can drop a constraint on the parent but it remains on the child
2133 CREATE TABLE test_drop_constr_parent (c text CHECK (c IS NOT NULL));
2134 CREATE TABLE test_drop_constr_child () INHERITS (test_drop_constr_parent);
2135 ALTER TABLE ONLY test_drop_constr_parent DROP CONSTRAINT "test_drop_constr_parent_c_check";
2136 -- should fail
2137 INSERT INTO test_drop_constr_child (c) VALUES (NULL);
2138 ERROR:  new row for relation "test_drop_constr_child" violates check constraint "test_drop_constr_parent_c_check"
2139 DETAIL:  Failing row contains (null).
2140 DROP TABLE test_drop_constr_parent CASCADE;
2141 NOTICE:  drop cascades to table test_drop_constr_child
2142 --
2143 -- IF EXISTS test
2144 --
2145 ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
2146 NOTICE:  relation "tt8" does not exist, skipping
2147 ALTER TABLE IF EXISTS tt8 ADD CONSTRAINT xxx PRIMARY KEY(f);
2148 NOTICE:  relation "tt8" does not exist, skipping
2149 ALTER TABLE IF EXISTS tt8 ADD CHECK (f BETWEEN 0 AND 10);
2150 NOTICE:  relation "tt8" does not exist, skipping
2151 ALTER TABLE IF EXISTS tt8 ALTER COLUMN f SET DEFAULT 0;
2152 NOTICE:  relation "tt8" does not exist, skipping
2153 ALTER TABLE IF EXISTS tt8 RENAME COLUMN f TO f1;
2154 NOTICE:  relation "tt8" does not exist, skipping
2155 ALTER TABLE IF EXISTS tt8 SET SCHEMA alter2;
2156 NOTICE:  relation "tt8" does not exist, skipping
2157 CREATE TABLE tt8(a int);
2158 CREATE SCHEMA alter2;
2159 ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
2160 ALTER TABLE IF EXISTS tt8 ADD CONSTRAINT xxx PRIMARY KEY(f);
2161 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "xxx" for table "tt8"
2162 ALTER TABLE IF EXISTS tt8 ADD CHECK (f BETWEEN 0 AND 10);
2163 ALTER TABLE IF EXISTS tt8 ALTER COLUMN f SET DEFAULT 0;
2164 ALTER TABLE IF EXISTS tt8 RENAME COLUMN f TO f1;
2165 ALTER TABLE IF EXISTS tt8 SET SCHEMA alter2;
2166 \d alter2.tt8
2167           Table "alter2.tt8"
2168  Column |  Type   |     Modifiers      
2169 --------+---------+--------------------
2170  a      | integer | 
2171  f1     | integer | not null default 0
2172 Indexes:
2173     "xxx" PRIMARY KEY, btree (f1)
2174 Check constraints:
2175     "tt8_f_check" CHECK (f1 >= 0 AND f1 <= 10)
2176
2177 DROP TABLE alter2.tt8;
2178 DROP SCHEMA alter2;