2 -- ALTER TABLE ADD COLUMN DEFAULT test
4 SET search_path = fast_default;
5 CREATE SCHEMA fast_default;
6 CREATE TABLE m(id OID);
7 INSERT INTO m VALUES (NULL::OID);
8 CREATE FUNCTION set(tabname name) RETURNS VOID
12 SET id = (SELECT c.relfilenode
13 FROM pg_class AS c, pg_namespace AS s
14 WHERE c.relname = tabname
15 AND c.relnamespace = s.oid
16 AND s.nspname = 'fast_default');
18 $$ LANGUAGE 'plpgsql';
19 CREATE FUNCTION comp() RETURNS TEXT
23 WHEN m.id = c.relfilenode THEN 'Unchanged'
26 FROM m, pg_class AS c, pg_namespace AS s
28 AND c.relnamespace = s.oid
29 AND s.nspname = 'fast_default');
31 $$ LANGUAGE 'plpgsql';
32 CREATE FUNCTION log_rewrite() RETURNS event_trigger
39 select into this_schema relnamespace::regnamespace::text
41 where oid = pg_event_trigger_table_rewrite_oid();
42 if this_schema = 'fast_default'
44 RAISE NOTICE 'rewriting table % for reason %',
45 pg_event_trigger_table_rewrite_oid()::regclass,
46 pg_event_trigger_table_rewrite_reason();
50 CREATE TABLE has_volatile AS
51 SELECT * FROM generate_series(1,10) id;
52 CREATE EVENT TRIGGER has_volatile_rewrite
54 EXECUTE PROCEDURE log_rewrite();
55 -- only the last of these should trigger a rewrite
56 ALTER TABLE has_volatile ADD col1 int;
57 ALTER TABLE has_volatile ADD col2 int DEFAULT 1;
58 ALTER TABLE has_volatile ADD col3 timestamptz DEFAULT current_timestamp;
59 ALTER TABLE has_volatile ADD col4 int DEFAULT (random() * 10000)::int;
60 NOTICE: rewriting table has_volatile for reason 2
61 -- Test a large sample of different datatypes
62 CREATE TABLE T(pk INT NOT NULL PRIMARY KEY, c_int INT DEFAULT 1);
69 INSERT INTO T VALUES (1), (2);
70 ALTER TABLE T ADD COLUMN c_bpchar BPCHAR(5) DEFAULT 'hello',
71 ALTER COLUMN c_int SET DEFAULT 2;
72 INSERT INTO T VALUES (3), (4);
73 ALTER TABLE T ADD COLUMN c_text TEXT DEFAULT 'world',
74 ALTER COLUMN c_bpchar SET DEFAULT 'dog';
75 INSERT INTO T VALUES (5), (6);
76 ALTER TABLE T ADD COLUMN c_date DATE DEFAULT '2016-06-02',
77 ALTER COLUMN c_text SET DEFAULT 'cat';
78 INSERT INTO T VALUES (7), (8);
79 ALTER TABLE T ADD COLUMN c_timestamp TIMESTAMP DEFAULT '2016-09-01 12:00:00',
80 ADD COLUMN c_timestamp_null TIMESTAMP,
81 ALTER COLUMN c_date SET DEFAULT '2010-01-01';
82 INSERT INTO T VALUES (9), (10);
83 ALTER TABLE T ADD COLUMN c_array TEXT[]
84 DEFAULT '{"This", "is", "the", "real", "world"}',
85 ALTER COLUMN c_timestamp SET DEFAULT '1970-12-31 11:12:13',
86 ALTER COLUMN c_timestamp_null SET DEFAULT '2016-09-29 12:00:00';
87 INSERT INTO T VALUES (11), (12);
88 ALTER TABLE T ADD COLUMN c_small SMALLINT DEFAULT -5,
89 ADD COLUMN c_small_null SMALLINT,
91 SET DEFAULT '{"This", "is", "no", "fantasy"}';
92 INSERT INTO T VALUES (13), (14);
93 ALTER TABLE T ADD COLUMN c_big BIGINT DEFAULT 180000000000018,
94 ALTER COLUMN c_small SET DEFAULT 9,
95 ALTER COLUMN c_small_null SET DEFAULT 13;
96 INSERT INTO T VALUES (15), (16);
97 ALTER TABLE T ADD COLUMN c_num NUMERIC DEFAULT 1.00000000001,
98 ALTER COLUMN c_big SET DEFAULT -9999999999999999;
99 INSERT INTO T VALUES (17), (18);
100 ALTER TABLE T ADD COLUMN c_time TIME DEFAULT '12:00:00',
101 ALTER COLUMN c_num SET DEFAULT 2.000000000000002;
102 INSERT INTO T VALUES (19), (20);
103 ALTER TABLE T ADD COLUMN c_interval INTERVAL DEFAULT '1 day',
104 ALTER COLUMN c_time SET DEFAULT '23:59:59';
105 INSERT INTO T VALUES (21), (22);
106 ALTER TABLE T ADD COLUMN c_hugetext TEXT DEFAULT repeat('abcdefg',1000),
107 ALTER COLUMN c_interval SET DEFAULT '3 hours';
108 INSERT INTO T VALUES (23), (24);
109 ALTER TABLE T ALTER COLUMN c_interval DROP DEFAULT,
110 ALTER COLUMN c_hugetext SET DEFAULT repeat('poiuyt', 1000);
111 INSERT INTO T VALUES (25), (26);
112 ALTER TABLE T ALTER COLUMN c_bpchar DROP DEFAULT,
113 ALTER COLUMN c_date DROP DEFAULT,
114 ALTER COLUMN c_text DROP DEFAULT,
115 ALTER COLUMN c_timestamp DROP DEFAULT,
116 ALTER COLUMN c_array DROP DEFAULT,
117 ALTER COLUMN c_small DROP DEFAULT,
118 ALTER COLUMN c_big DROP DEFAULT,
119 ALTER COLUMN c_num DROP DEFAULT,
120 ALTER COLUMN c_time DROP DEFAULT,
121 ALTER COLUMN c_hugetext DROP DEFAULT;
122 INSERT INTO T VALUES (27), (28);
123 SELECT pk, c_int, c_bpchar, c_text, c_date, c_timestamp,
124 c_timestamp_null, c_array, c_small, c_small_null,
125 c_big, c_num, c_time, c_interval,
126 c_hugetext = repeat('abcdefg',1000) as c_hugetext_origdef,
127 c_hugetext = repeat('poiuyt', 1000) as c_hugetext_newdef
129 pk | c_int | c_bpchar | c_text | c_date | c_timestamp | c_timestamp_null | c_array | c_small | c_small_null | c_big | c_num | c_time | c_interval | c_hugetext_origdef | c_hugetext_newdef
130 ----+-------+----------+--------+------------+--------------------------+--------------------------+--------------------------+---------+--------------+-------------------+-------------------+----------+------------+--------------------+-------------------
131 1 | 1 | hello | world | 06-02-2016 | Thu Sep 01 12:00:00 2016 | | {This,is,the,real,world} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
132 2 | 1 | hello | world | 06-02-2016 | Thu Sep 01 12:00:00 2016 | | {This,is,the,real,world} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
133 3 | 2 | hello | world | 06-02-2016 | Thu Sep 01 12:00:00 2016 | | {This,is,the,real,world} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
134 4 | 2 | hello | world | 06-02-2016 | Thu Sep 01 12:00:00 2016 | | {This,is,the,real,world} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
135 5 | 2 | dog | world | 06-02-2016 | Thu Sep 01 12:00:00 2016 | | {This,is,the,real,world} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
136 6 | 2 | dog | world | 06-02-2016 | Thu Sep 01 12:00:00 2016 | | {This,is,the,real,world} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
137 7 | 2 | dog | cat | 06-02-2016 | Thu Sep 01 12:00:00 2016 | | {This,is,the,real,world} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
138 8 | 2 | dog | cat | 06-02-2016 | Thu Sep 01 12:00:00 2016 | | {This,is,the,real,world} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
139 9 | 2 | dog | cat | 01-01-2010 | Thu Sep 01 12:00:00 2016 | | {This,is,the,real,world} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
140 10 | 2 | dog | cat | 01-01-2010 | Thu Sep 01 12:00:00 2016 | | {This,is,the,real,world} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
141 11 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,the,real,world} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
142 12 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,the,real,world} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
143 13 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
144 14 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | -5 | | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
145 15 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | 9 | 13 | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
146 16 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | 9 | 13 | 180000000000018 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
147 17 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | 9 | 13 | -9999999999999999 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
148 18 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | 9 | 13 | -9999999999999999 | 1.00000000001 | 12:00:00 | @ 1 day | t | f
149 19 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | 9 | 13 | -9999999999999999 | 2.000000000000002 | 12:00:00 | @ 1 day | t | f
150 20 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | 9 | 13 | -9999999999999999 | 2.000000000000002 | 12:00:00 | @ 1 day | t | f
151 21 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | 9 | 13 | -9999999999999999 | 2.000000000000002 | 23:59:59 | @ 1 day | t | f
152 22 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | 9 | 13 | -9999999999999999 | 2.000000000000002 | 23:59:59 | @ 1 day | t | f
153 23 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | 9 | 13 | -9999999999999999 | 2.000000000000002 | 23:59:59 | @ 3 hours | t | f
154 24 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | 9 | 13 | -9999999999999999 | 2.000000000000002 | 23:59:59 | @ 3 hours | t | f
155 25 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | 9 | 13 | -9999999999999999 | 2.000000000000002 | 23:59:59 | | f | t
156 26 | 2 | dog | cat | 01-01-2010 | Thu Dec 31 11:12:13 1970 | Thu Sep 29 12:00:00 2016 | {This,is,no,fantasy} | 9 | 13 | -9999999999999999 | 2.000000000000002 | 23:59:59 | | f | t
157 27 | 2 | | | | | Thu Sep 29 12:00:00 2016 | | | 13 | | | | | |
158 28 | 2 | | | | | Thu Sep 29 12:00:00 2016 | | | 13 | | | | | |
168 -- Test expressions in the defaults
169 CREATE OR REPLACE FUNCTION foo(a INT) RETURNS TEXT AS $$
170 DECLARE res TEXT := '';
175 res := res || chr(ascii('a') + i);
179 END; $$ LANGUAGE PLPGSQL STABLE;
180 CREATE TABLE T(pk INT NOT NULL PRIMARY KEY, c_int INT DEFAULT LENGTH(foo(6)));
187 INSERT INTO T VALUES (1), (2);
188 ALTER TABLE T ADD COLUMN c_bpchar BPCHAR(5) DEFAULT foo(4),
189 ALTER COLUMN c_int SET DEFAULT LENGTH(foo(8));
190 INSERT INTO T VALUES (3), (4);
191 ALTER TABLE T ADD COLUMN c_text TEXT DEFAULT foo(6),
192 ALTER COLUMN c_bpchar SET DEFAULT foo(3);
193 INSERT INTO T VALUES (5), (6);
194 ALTER TABLE T ADD COLUMN c_date DATE
195 DEFAULT '2016-06-02'::DATE + LENGTH(foo(10)),
196 ALTER COLUMN c_text SET DEFAULT foo(12);
197 INSERT INTO T VALUES (7), (8);
198 ALTER TABLE T ADD COLUMN c_timestamp TIMESTAMP
199 DEFAULT '2016-09-01'::DATE + LENGTH(foo(10)),
201 SET DEFAULT '2010-01-01'::DATE - LENGTH(foo(4));
202 INSERT INTO T VALUES (9), (10);
203 ALTER TABLE T ADD COLUMN c_array TEXT[]
204 DEFAULT ('{"This", "is", "' || foo(4) ||
205 '","the", "real", "world"}')::TEXT[],
206 ALTER COLUMN c_timestamp
207 SET DEFAULT '1970-12-31'::DATE + LENGTH(foo(30));
208 INSERT INTO T VALUES (11), (12);
209 ALTER TABLE T ALTER COLUMN c_int DROP DEFAULT,
211 SET DEFAULT ('{"This", "is", "' || foo(1) ||
212 '", "fantasy"}')::text[];
213 INSERT INTO T VALUES (13), (14);
214 ALTER TABLE T ALTER COLUMN c_bpchar DROP DEFAULT,
215 ALTER COLUMN c_date DROP DEFAULT,
216 ALTER COLUMN c_text DROP DEFAULT,
217 ALTER COLUMN c_timestamp DROP DEFAULT,
218 ALTER COLUMN c_array DROP DEFAULT;
219 INSERT INTO T VALUES (15), (16);
221 pk | c_int | c_bpchar | c_text | c_date | c_timestamp | c_array
222 ----+-------+----------+--------------+------------+--------------------------+-------------------------------
223 1 | 6 | abcd | abcdef | 06-12-2016 | Sun Sep 11 00:00:00 2016 | {This,is,abcd,the,real,world}
224 2 | 6 | abcd | abcdef | 06-12-2016 | Sun Sep 11 00:00:00 2016 | {This,is,abcd,the,real,world}
225 3 | 8 | abcd | abcdef | 06-12-2016 | Sun Sep 11 00:00:00 2016 | {This,is,abcd,the,real,world}
226 4 | 8 | abcd | abcdef | 06-12-2016 | Sun Sep 11 00:00:00 2016 | {This,is,abcd,the,real,world}
227 5 | 8 | abc | abcdef | 06-12-2016 | Sun Sep 11 00:00:00 2016 | {This,is,abcd,the,real,world}
228 6 | 8 | abc | abcdef | 06-12-2016 | Sun Sep 11 00:00:00 2016 | {This,is,abcd,the,real,world}
229 7 | 8 | abc | abcdefghijkl | 06-12-2016 | Sun Sep 11 00:00:00 2016 | {This,is,abcd,the,real,world}
230 8 | 8 | abc | abcdefghijkl | 06-12-2016 | Sun Sep 11 00:00:00 2016 | {This,is,abcd,the,real,world}
231 9 | 8 | abc | abcdefghijkl | 12-28-2009 | Sun Sep 11 00:00:00 2016 | {This,is,abcd,the,real,world}
232 10 | 8 | abc | abcdefghijkl | 12-28-2009 | Sun Sep 11 00:00:00 2016 | {This,is,abcd,the,real,world}
233 11 | 8 | abc | abcdefghijkl | 12-28-2009 | Sat Jan 30 00:00:00 1971 | {This,is,abcd,the,real,world}
234 12 | 8 | abc | abcdefghijkl | 12-28-2009 | Sat Jan 30 00:00:00 1971 | {This,is,abcd,the,real,world}
235 13 | | abc | abcdefghijkl | 12-28-2009 | Sat Jan 30 00:00:00 1971 | {This,is,a,fantasy}
236 14 | | abc | abcdefghijkl | 12-28-2009 | Sat Jan 30 00:00:00 1971 | {This,is,a,fantasy}
248 DROP FUNCTION foo(INT);
249 -- Fall back to full rewrite for volatile expressions
250 CREATE TABLE T(pk INT NOT NULL PRIMARY KEY);
251 INSERT INTO T VALUES (1);
258 -- now() is stable, because it returns the transaction timestamp
259 ALTER TABLE T ADD COLUMN c1 TIMESTAMP DEFAULT now();
266 -- clock_timestamp() is volatile
267 ALTER TABLE T ADD COLUMN c2 TIMESTAMP DEFAULT clock_timestamp();
268 NOTICE: rewriting table t for reason 2
277 CREATE TABLE T (pk INT NOT NULL PRIMARY KEY);
284 INSERT INTO T SELECT * FROM generate_series(1, 10) a;
285 ALTER TABLE T ADD COLUMN c_bigint BIGINT NOT NULL DEFAULT -1;
286 INSERT INTO T SELECT b, b - 10 FROM generate_series(11, 20) a(b);
287 ALTER TABLE T ADD COLUMN c_text TEXT DEFAULT 'hello';
288 INSERT INTO T SELECT b, b - 10, (b + 10)::text FROM generate_series(21, 30) a(b);
290 SELECT c_bigint, c_text FROM T WHERE c_bigint = -1 LIMIT 1;
296 EXPLAIN (VERBOSE TRUE, COSTS FALSE)
297 SELECT c_bigint, c_text FROM T WHERE c_bigint = -1 LIMIT 1;
299 ----------------------------------------------
301 Output: c_bigint, c_text
302 -> Seq Scan on fast_default.t
303 Output: c_bigint, c_text
304 Filter: (t.c_bigint = '-1'::integer)
307 SELECT c_bigint, c_text FROM T WHERE c_text = 'hello' LIMIT 1;
313 EXPLAIN (VERBOSE TRUE, COSTS FALSE) SELECT c_bigint, c_text FROM T WHERE c_text = 'hello' LIMIT 1;
315 --------------------------------------------
317 Output: c_bigint, c_text
318 -> Seq Scan on fast_default.t
319 Output: c_bigint, c_text
320 Filter: (t.c_text = 'hello'::text)
324 SELECT COALESCE(c_bigint, pk), COALESCE(c_text, pk::text)
326 ORDER BY pk LIMIT 10;
328 ----------+----------
341 -- Aggregate function
342 SELECT SUM(c_bigint), MAX(c_text COLLATE "C" ), MIN(c_text COLLATE "C") FROM T;
349 SELECT * FROM T ORDER BY c_bigint, c_text, pk LIMIT 10;
350 pk | c_bigint | c_text
351 ----+----------+--------
364 EXPLAIN (VERBOSE TRUE, COSTS FALSE)
365 SELECT * FROM T ORDER BY c_bigint, c_text, pk LIMIT 10;
367 ----------------------------------------------
369 Output: pk, c_bigint, c_text
371 Output: pk, c_bigint, c_text
372 Sort Key: t.c_bigint, t.c_text, t.pk
373 -> Seq Scan on fast_default.t
374 Output: pk, c_bigint, c_text
378 SELECT * FROM T WHERE c_bigint > -1 ORDER BY c_bigint, c_text, pk LIMIT 10;
379 pk | c_bigint | c_text
380 ----+----------+--------
393 EXPLAIN (VERBOSE TRUE, COSTS FALSE)
394 SELECT * FROM T WHERE c_bigint > -1 ORDER BY c_bigint, c_text, pk LIMIT 10;
396 ----------------------------------------------------
398 Output: pk, c_bigint, c_text
400 Output: pk, c_bigint, c_text
401 Sort Key: t.c_bigint, t.c_text, t.pk
402 -> Seq Scan on fast_default.t
403 Output: pk, c_bigint, c_text
404 Filter: (t.c_bigint > '-1'::integer)
407 -- DELETE with RETURNING
408 DELETE FROM T WHERE pk BETWEEN 10 AND 20 RETURNING *;
409 pk | c_bigint | c_text
410 ----+----------+--------
424 EXPLAIN (VERBOSE TRUE, COSTS FALSE)
425 DELETE FROM T WHERE pk BETWEEN 10 AND 20 RETURNING *;
427 -----------------------------------------------------------
428 Delete on fast_default.t
429 Output: pk, c_bigint, c_text
430 -> Bitmap Heap Scan on fast_default.t
432 Recheck Cond: ((t.pk >= 10) AND (t.pk <= 20))
433 -> Bitmap Index Scan on t_pkey
434 Index Cond: ((t.pk >= 10) AND (t.pk <= 20))
438 UPDATE T SET c_text = '"' || c_text || '"' WHERE pk < 10;
439 SELECT * FROM T WHERE c_text LIKE '"%"' ORDER BY PK;
440 pk | c_bigint | c_text
441 ----+----------+---------
460 -- Combine with other DDL
461 CREATE TABLE T(pk INT NOT NULL PRIMARY KEY);
468 INSERT INTO T VALUES (1), (2);
469 ALTER TABLE T ADD COLUMN c_int INT NOT NULL DEFAULT -1;
470 INSERT INTO T VALUES (3), (4);
471 ALTER TABLE T ADD COLUMN c_text TEXT DEFAULT 'Hello';
472 INSERT INTO T VALUES (5), (6);
473 ALTER TABLE T ALTER COLUMN c_text SET DEFAULT 'world',
474 ALTER COLUMN c_int SET DEFAULT 1;
475 INSERT INTO T VALUES (7), (8);
476 SELECT * FROM T ORDER BY pk;
478 ----+-------+--------
490 CREATE INDEX i ON T(c_int, c_text);
491 SELECT c_text FROM T WHERE c_int = -1;
508 -- query to exercise expand_tuple function
510 SELECT 1::int AS a , 2::int AS b
511 FROM generate_series(1,20) q;
512 ALTER TABLE t1 ADD COLUMN c text;
514 stddev(cast((SELECT sum(1) FROM generate_series(1,20) x) AS float4))
515 OVER (PARTITION BY a,b,c ORDER BY b)
543 -- test that we account for missing columns without defaults correctly
544 -- in expand_tuple, and that rows are correctly expanded for triggers
545 CREATE FUNCTION test_trigger()
551 raise notice 'old tuple: %', to_json(OLD)::text;
561 -- 2 new columns, both have defaults
562 CREATE TABLE t (id serial PRIMARY KEY, a int, b int, c int);
563 INSERT INTO t (a,b,c) VALUES (1,2,3);
564 ALTER TABLE t ADD COLUMN x int NOT NULL DEFAULT 4;
565 ALTER TABLE t ADD COLUMN y int NOT NULL DEFAULT 5;
566 CREATE TRIGGER a BEFORE UPDATE ON t FOR EACH ROW EXECUTE PROCEDURE test_trigger();
568 id | a | b | c | x | y
569 ----+---+---+---+---+---
570 1 | 1 | 2 | 3 | 4 | 5
574 NOTICE: old tuple: {"id":1,"a":1,"b":2,"c":3,"x":4,"y":5}
576 id | a | b | c | x | y
577 ----+---+---+---+---+---
578 1 | 1 | 2 | 3 | 4 | 2
582 -- 2 new columns, first has default
583 CREATE TABLE t (id serial PRIMARY KEY, a int, b int, c int);
584 INSERT INTO t (a,b,c) VALUES (1,2,3);
585 ALTER TABLE t ADD COLUMN x int NOT NULL DEFAULT 4;
586 ALTER TABLE t ADD COLUMN y int;
587 CREATE TRIGGER a BEFORE UPDATE ON t FOR EACH ROW EXECUTE PROCEDURE test_trigger();
589 id | a | b | c | x | y
590 ----+---+---+---+---+---
595 NOTICE: old tuple: {"id":1,"a":1,"b":2,"c":3,"x":4,"y":null}
597 id | a | b | c | x | y
598 ----+---+---+---+---+---
599 1 | 1 | 2 | 3 | 4 | 2
603 -- 2 new columns, second has default
604 CREATE TABLE t (id serial PRIMARY KEY, a int, b int, c int);
605 INSERT INTO t (a,b,c) VALUES (1,2,3);
606 ALTER TABLE t ADD COLUMN x int;
607 ALTER TABLE t ADD COLUMN y int NOT NULL DEFAULT 5;
608 CREATE TRIGGER a BEFORE UPDATE ON t FOR EACH ROW EXECUTE PROCEDURE test_trigger();
610 id | a | b | c | x | y
611 ----+---+---+---+---+---
616 NOTICE: old tuple: {"id":1,"a":1,"b":2,"c":3,"x":null,"y":5}
618 id | a | b | c | x | y
619 ----+---+---+---+---+---
624 -- 2 new columns, neither has default
625 CREATE TABLE t (id serial PRIMARY KEY, a int, b int, c int);
626 INSERT INTO t (a,b,c) VALUES (1,2,3);
627 ALTER TABLE t ADD COLUMN x int;
628 ALTER TABLE t ADD COLUMN y int;
629 CREATE TRIGGER a BEFORE UPDATE ON t FOR EACH ROW EXECUTE PROCEDURE test_trigger();
631 id | a | b | c | x | y
632 ----+---+---+---+---+---
637 NOTICE: old tuple: {"id":1,"a":1,"b":2,"c":3,"x":null,"y":null}
639 id | a | b | c | x | y
640 ----+---+---+---+---+---
645 -- same as last 4 tests but here the last original column has a NULL value
646 -- 2 new columns, both have defaults
647 CREATE TABLE t (id serial PRIMARY KEY, a int, b int, c int);
648 INSERT INTO t (a,b,c) VALUES (1,2,NULL);
649 ALTER TABLE t ADD COLUMN x int NOT NULL DEFAULT 4;
650 ALTER TABLE t ADD COLUMN y int NOT NULL DEFAULT 5;
651 CREATE TRIGGER a BEFORE UPDATE ON t FOR EACH ROW EXECUTE PROCEDURE test_trigger();
653 id | a | b | c | x | y
654 ----+---+---+---+---+---
659 NOTICE: old tuple: {"id":1,"a":1,"b":2,"c":null,"x":4,"y":5}
661 id | a | b | c | x | y
662 ----+---+---+---+---+---
667 -- 2 new columns, first has default
668 CREATE TABLE t (id serial PRIMARY KEY, a int, b int, c int);
669 INSERT INTO t (a,b,c) VALUES (1,2,NULL);
670 ALTER TABLE t ADD COLUMN x int NOT NULL DEFAULT 4;
671 ALTER TABLE t ADD COLUMN y int;
672 CREATE TRIGGER a BEFORE UPDATE ON t FOR EACH ROW EXECUTE PROCEDURE test_trigger();
674 id | a | b | c | x | y
675 ----+---+---+---+---+---
680 NOTICE: old tuple: {"id":1,"a":1,"b":2,"c":null,"x":4,"y":null}
682 id | a | b | c | x | y
683 ----+---+---+---+---+---
688 -- 2 new columns, second has default
689 CREATE TABLE t (id serial PRIMARY KEY, a int, b int, c int);
690 INSERT INTO t (a,b,c) VALUES (1,2,NULL);
691 ALTER TABLE t ADD COLUMN x int;
692 ALTER TABLE t ADD COLUMN y int NOT NULL DEFAULT 5;
693 CREATE TRIGGER a BEFORE UPDATE ON t FOR EACH ROW EXECUTE PROCEDURE test_trigger();
695 id | a | b | c | x | y
696 ----+---+---+---+---+---
701 NOTICE: old tuple: {"id":1,"a":1,"b":2,"c":null,"x":null,"y":5}
703 id | a | b | c | x | y
704 ----+---+---+---+---+---
709 -- 2 new columns, neither has default
710 CREATE TABLE t (id serial PRIMARY KEY, a int, b int, c int);
711 INSERT INTO t (a,b,c) VALUES (1,2,NULL);
712 ALTER TABLE t ADD COLUMN x int;
713 ALTER TABLE t ADD COLUMN y int;
714 CREATE TRIGGER a BEFORE UPDATE ON t FOR EACH ROW EXECUTE PROCEDURE test_trigger();
716 id | a | b | c | x | y
717 ----+---+---+---+---+---
722 NOTICE: old tuple: {"id":1,"a":1,"b":2,"c":null,"x":null,"y":null}
724 id | a | b | c | x | y
725 ----+---+---+---+---+---
730 -- make sure expanded tuple has correct self pointer
731 -- it will be required by the RI trigger doing the cascading delete
732 CREATE TABLE leader (a int PRIMARY KEY, b int);
733 CREATE TABLE follower (a int REFERENCES leader ON DELETE CASCADE, b int);
734 INSERT INTO leader VALUES (1, 1), (2, 2);
735 ALTER TABLE leader ADD c int;
736 ALTER TABLE leader DROP c;
738 -- check that ALTER TABLE ... ALTER TYPE does the right thing
739 CREATE TABLE vtype( a integer);
740 INSERT INTO vtype VALUES (1);
741 ALTER TABLE vtype ADD COLUMN b DOUBLE PRECISION DEFAULT 0.2;
742 ALTER TABLE vtype ADD COLUMN c BOOLEAN DEFAULT true;
750 ALTER b TYPE text USING b::text,
751 ALTER c TYPE text USING c::text;
752 NOTICE: rewriting table vtype for reason 4
759 -- also check the case that doesn't rewrite the table
760 CREATE TABLE vtype2 (a int);
761 INSERT INTO vtype2 VALUES (1);
762 ALTER TABLE vtype2 ADD COLUMN b varchar(10) DEFAULT 'xxx';
763 ALTER TABLE vtype2 ALTER COLUMN b SET DEFAULT 'yyy';
764 INSERT INTO vtype2 VALUES (2);
765 ALTER TABLE vtype2 ALTER COLUMN b TYPE varchar(20) USING b::varchar(20);
766 SELECT * FROM vtype2;
773 -- Ensure that defaults are checked when evaluating whether HOT update
774 -- is possible, this was broken for a while:
775 -- https://postgr.es/m/20190202133521.ylauh3ckqa7colzj%40alap3.anarazel.de
778 INSERT INTO t DEFAULT VALUES;
779 ALTER TABLE t ADD COLUMN a int DEFAULT 1;
780 CREATE INDEX ON t(a);
781 -- set column with a default 1 to NULL, due to a bug that wasn't
782 -- noticed has heap_getattr buggily returned NULL for default columns
783 UPDATE t SET a = NULL;
784 -- verify that index and non-index scans show the same result
785 SET LOCAL enable_seqscan = true;
786 SELECT * FROM t WHERE a IS NULL;
792 SET LOCAL enable_seqscan = false;
793 SELECT * FROM t WHERE a IS NULL;
800 -- verify that a default set on a non-plain table doesn't set a missing
801 -- value on the attribute
802 CREATE FOREIGN DATA WRAPPER dummy;
803 CREATE SERVER s0 FOREIGN DATA WRAPPER dummy;
804 CREATE FOREIGN TABLE ft1 (c1 integer NOT NULL) SERVER s0;
805 ALTER FOREIGN TABLE ft1 ADD COLUMN c8 integer DEFAULT 0;
806 ALTER FOREIGN TABLE ft1 ALTER COLUMN c8 TYPE char(10);
809 WHERE attrelid = 'ft1'::regclass AND
810 (attmissingval IS NOT NULL OR atthasmissing);
817 DROP FOREIGN TABLE ft1;
819 DROP FOREIGN DATA WRAPPER dummy;
824 DROP FUNCTION test_trigger();
826 DROP FUNCTION set(name);
827 DROP FUNCTION comp();
829 DROP TABLE has_volatile;
830 DROP EVENT TRIGGER has_volatile_rewrite;
831 DROP FUNCTION log_rewrite;
832 DROP SCHEMA fast_default;
833 -- Leave a table with an active fast default in place, for pg_upgrade testing
834 set search_path = public;
835 create table has_fast_default(f1 int);
836 insert into has_fast_default values(1);
837 alter table has_fast_default add column f2 int default 42;
838 table has_fast_default;