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