1 -- sanity check of system catalog
2 SELECT attrelid, attname, attgenerated FROM pg_attribute WHERE attgenerated NOT IN ('', 's');
3 attrelid | attname | attgenerated
4 ----------+---------+--------------
7 CREATE TABLE gtest0 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (55) STORED);
8 CREATE TABLE gtest1 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED);
9 SELECT table_name, column_name, column_default, is_nullable, is_generated, generation_expression FROM information_schema.columns WHERE table_name LIKE 'gtest_' ORDER BY 1, 2;
10 table_name | column_name | column_default | is_nullable | is_generated | generation_expression
11 ------------+-------------+----------------+-------------+--------------+-----------------------
12 gtest0 | a | | NO | NEVER |
13 gtest0 | b | | YES | ALWAYS | 55
14 gtest1 | a | | NO | NEVER |
15 gtest1 | b | | YES | ALWAYS | (a * 2)
18 SELECT table_name, column_name, dependent_column FROM information_schema.column_column_usage ORDER BY 1, 2, 3;
19 table_name | column_name | dependent_column
20 ------------+-------------+------------------
26 Column | Type | Collation | Nullable | Default
27 --------+---------+-----------+----------+------------------------------------
28 a | integer | | not null |
29 b | integer | | | generated always as (a * 2) stored
31 "gtest1_pkey" PRIMARY KEY, btree (a)
33 -- duplicate generated
34 CREATE TABLE gtest_err_1 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED GENERATED ALWAYS AS (a * 3) STORED);
35 ERROR: multiple generation clauses specified for column "b" of table "gtest_err_1"
36 LINE 1: ...ARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED GENERATED ...
38 -- references to other generated columns, including self-references
39 CREATE TABLE gtest_err_2a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (b * 2) STORED);
40 ERROR: cannot use generated column "b" in column generation expression
41 LINE 1: ...2a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (b * 2) STO...
43 DETAIL: A generated column cannot reference another generated column.
44 CREATE TABLE gtest_err_2b (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED, c int GENERATED ALWAYS AS (b * 3) STORED);
45 ERROR: cannot use generated column "b" in column generation expression
46 LINE 1: ...AYS AS (a * 2) STORED, c int GENERATED ALWAYS AS (b * 3) STO...
48 DETAIL: A generated column cannot reference another generated column.
49 -- a whole-row var is a self-reference on steroids, so disallow that too
50 CREATE TABLE gtest_err_2c (a int PRIMARY KEY,
51 b int GENERATED ALWAYS AS (num_nulls(gtest_err_2c)) STORED);
52 ERROR: cannot use whole-row variable in column generation expression
53 LINE 2: b int GENERATED ALWAYS AS (num_nulls(gtest_err_2c)) STOR...
55 DETAIL: This would cause the generated column to depend on its own value.
57 CREATE TABLE gtest_err_3 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (c * 2) STORED);
58 ERROR: column "c" does not exist
59 LINE 1: ..._3 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (c * 2) STO...
61 -- generation expression must be immutable
62 CREATE TABLE gtest_err_4 (a int PRIMARY KEY, b double precision GENERATED ALWAYS AS (random()) STORED);
63 ERROR: generation expression is not immutable
64 -- cannot have default/identity and generated
65 CREATE TABLE gtest_err_5a (a int PRIMARY KEY, b int DEFAULT 5 GENERATED ALWAYS AS (a * 2) STORED);
66 ERROR: both default and generation expression specified for column "b" of table "gtest_err_5a"
67 LINE 1: ... gtest_err_5a (a int PRIMARY KEY, b int DEFAULT 5 GENERATED ...
69 CREATE TABLE gtest_err_5b (a int PRIMARY KEY, b int GENERATED ALWAYS AS identity GENERATED ALWAYS AS (a * 2) STORED);
70 ERROR: both identity and generation expression specified for column "b" of table "gtest_err_5b"
71 LINE 1: ...t PRIMARY KEY, b int GENERATED ALWAYS AS identity GENERATED ...
73 -- reference to system column not allowed in generated column
74 -- (except tableoid, which we test below)
75 CREATE TABLE gtest_err_6a (a int PRIMARY KEY, b bool GENERATED ALWAYS AS (xmin <> 37) STORED);
76 ERROR: cannot use system column "xmin" in column generation expression
77 LINE 1: ...a (a int PRIMARY KEY, b bool GENERATED ALWAYS AS (xmin <> 37...
79 -- various prohibited constructs
80 CREATE TABLE gtest_err_7a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (avg(a)) STORED);
81 ERROR: aggregate functions are not allowed in column generation expressions
82 LINE 1: ...7a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (avg(a)) ST...
84 CREATE TABLE gtest_err_7b (a int PRIMARY KEY, b int GENERATED ALWAYS AS (row_number() OVER (ORDER BY a)) STORED);
85 ERROR: window functions are not allowed in column generation expressions
86 LINE 1: ...7b (a int PRIMARY KEY, b int GENERATED ALWAYS AS (row_number...
88 CREATE TABLE gtest_err_7c (a int PRIMARY KEY, b int GENERATED ALWAYS AS ((SELECT a)) STORED);
89 ERROR: cannot use subquery in column generation expression
90 LINE 1: ...7c (a int PRIMARY KEY, b int GENERATED ALWAYS AS ((SELECT a)...
92 CREATE TABLE gtest_err_7d (a int PRIMARY KEY, b int GENERATED ALWAYS AS (generate_series(1, a)) STORED);
93 ERROR: set-returning functions are not allowed in column generation expressions
94 LINE 1: ...7d (a int PRIMARY KEY, b int GENERATED ALWAYS AS (generate_s...
96 -- GENERATED BY DEFAULT not allowed
97 CREATE TABLE gtest_err_8 (a int PRIMARY KEY, b int GENERATED BY DEFAULT AS (a * 2) STORED);
98 ERROR: for a generated column, GENERATED ALWAYS must be specified
99 LINE 1: ...E gtest_err_8 (a int PRIMARY KEY, b int GENERATED BY DEFAULT...
101 INSERT INTO gtest1 VALUES (1);
102 INSERT INTO gtest1 VALUES (2, DEFAULT); -- ok
103 INSERT INTO gtest1 VALUES (3, 33); -- error
104 ERROR: cannot insert a non-DEFAULT value into column "b"
105 DETAIL: Column "b" is a generated column.
106 INSERT INTO gtest1 VALUES (3, 33), (4, 44); -- error
107 ERROR: cannot insert a non-DEFAULT value into column "b"
108 DETAIL: Column "b" is a generated column.
109 INSERT INTO gtest1 VALUES (3, DEFAULT), (4, 44); -- error
110 ERROR: cannot insert a non-DEFAULT value into column "b"
111 DETAIL: Column "b" is a generated column.
112 INSERT INTO gtest1 VALUES (3, 33), (4, DEFAULT); -- error
113 ERROR: cannot insert a non-DEFAULT value into column "b"
114 DETAIL: Column "b" is a generated column.
115 INSERT INTO gtest1 VALUES (3, DEFAULT), (4, DEFAULT); -- ok
116 SELECT * FROM gtest1 ORDER BY a;
125 DELETE FROM gtest1 WHERE a >= 3;
126 UPDATE gtest1 SET b = DEFAULT WHERE a = 1;
127 UPDATE gtest1 SET b = 11 WHERE a = 1; -- error
128 ERROR: column "b" can only be updated to DEFAULT
129 DETAIL: Column "b" is a generated column.
130 SELECT * FROM gtest1 ORDER BY a;
137 SELECT a, b, b * 2 AS b2 FROM gtest1 ORDER BY a;
144 SELECT a, b FROM gtest1 WHERE b = 4 ORDER BY a;
150 -- test that overflow error happens on write
151 INSERT INTO gtest1 VALUES (2000000000);
152 ERROR: integer out of range
153 SELECT * FROM gtest1;
160 DELETE FROM gtest1 WHERE a = 2000000000;
162 CREATE TABLE gtestx (x int, y int);
163 INSERT INTO gtestx VALUES (11, 1), (22, 2), (33, 3);
164 SELECT * FROM gtestx, gtest1 WHERE gtestx.y = gtest1.a;
172 -- test UPDATE/DELETE quals
173 SELECT * FROM gtest1 ORDER BY a;
180 UPDATE gtest1 SET a = 3 WHERE b = 4;
181 SELECT * FROM gtest1 ORDER BY a;
188 DELETE FROM gtest1 WHERE b = 2;
189 SELECT * FROM gtest1 ORDER BY a;
196 CREATE VIEW gtest1v AS SELECT * FROM gtest1;
197 SELECT * FROM gtest1v;
203 INSERT INTO gtest1v VALUES (4, 8); -- error
204 ERROR: cannot insert a non-DEFAULT value into column "b"
205 DETAIL: Column "b" is a generated column.
206 INSERT INTO gtest1v VALUES (5, DEFAULT); -- ok
207 INSERT INTO gtest1v VALUES (6, 66), (7, 77); -- error
208 ERROR: cannot insert a non-DEFAULT value into column "b"
209 DETAIL: Column "b" is a generated column.
210 INSERT INTO gtest1v VALUES (6, DEFAULT), (7, 77); -- error
211 ERROR: cannot insert a non-DEFAULT value into column "b"
212 DETAIL: Column "b" is a generated column.
213 INSERT INTO gtest1v VALUES (6, 66), (7, DEFAULT); -- error
214 ERROR: cannot insert a non-DEFAULT value into column "b"
215 DETAIL: Column "b" is a generated column.
216 INSERT INTO gtest1v VALUES (6, DEFAULT), (7, DEFAULT); -- ok
217 ALTER VIEW gtest1v ALTER COLUMN b SET DEFAULT 100;
218 INSERT INTO gtest1v VALUES (8, DEFAULT); -- error
219 ERROR: cannot insert a non-DEFAULT value into column "b"
220 DETAIL: Column "b" is a generated column.
221 INSERT INTO gtest1v VALUES (8, DEFAULT), (9, DEFAULT); -- error
222 ERROR: cannot insert a non-DEFAULT value into column "b"
223 DETAIL: Column "b" is a generated column.
224 SELECT * FROM gtest1v;
233 DELETE FROM gtest1v WHERE a >= 5;
236 WITH foo AS (SELECT * FROM gtest1) SELECT * FROM foo;
243 CREATE TABLE gtest1_1 () INHERITS (gtest1);
244 SELECT * FROM gtest1_1;
250 Table "public.gtest1_1"
251 Column | Type | Collation | Nullable | Default
252 --------+---------+-----------+----------+------------------------------------
253 a | integer | | not null |
254 b | integer | | | generated always as (a * 2) stored
257 INSERT INTO gtest1_1 VALUES (4);
258 SELECT * FROM gtest1_1;
264 SELECT * FROM gtest1;
271 CREATE TABLE gtest_normal (a int, b int);
272 CREATE TABLE gtest_normal_child (a int, b int GENERATED ALWAYS AS (a * 2) STORED) INHERITS (gtest_normal);
273 NOTICE: merging column "a" with inherited definition
274 NOTICE: merging column "b" with inherited definition
275 \d gtest_normal_child
276 Table "public.gtest_normal_child"
277 Column | Type | Collation | Nullable | Default
278 --------+---------+-----------+----------+------------------------------------
280 b | integer | | | generated always as (a * 2) stored
281 Inherits: gtest_normal
283 INSERT INTO gtest_normal (a) VALUES (1);
284 INSERT INTO gtest_normal_child (a) VALUES (2);
285 SELECT * FROM gtest_normal;
292 CREATE TABLE gtest_normal_child2 (a int, b int GENERATED ALWAYS AS (a * 3) STORED);
293 ALTER TABLE gtest_normal_child2 INHERIT gtest_normal;
294 INSERT INTO gtest_normal_child2 (a) VALUES (3);
295 SELECT * FROM gtest_normal;
303 -- test inheritance mismatches between parent and child
304 CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS (a * 22) STORED) INHERITS (gtest1); -- error
305 NOTICE: merging column "b" with inherited definition
306 ERROR: child column "b" specifies generation expression
307 HINT: Omit the generation expression in the definition of the child table column to inherit the generation expression from the parent table.
308 CREATE TABLE gtestx (x int, b int DEFAULT 10) INHERITS (gtest1); -- error
309 NOTICE: merging column "b" with inherited definition
310 ERROR: column "b" inherits from generated column but specifies default
311 CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS IDENTITY) INHERITS (gtest1); -- error
312 NOTICE: merging column "b" with inherited definition
313 ERROR: column "b" inherits from generated column but specifies identity
314 CREATE TABLE gtestxx_1 (a int NOT NULL, b int);
315 ALTER TABLE gtestxx_1 INHERIT gtest1; -- error
316 ERROR: column "b" in child table must be a generated column
317 CREATE TABLE gtestxx_2 (a int NOT NULL, b int GENERATED ALWAYS AS (a * 22) STORED);
318 ALTER TABLE gtestxx_2 INHERIT gtest1; -- error
319 ERROR: column "b" in child table has a conflicting generation expression
320 CREATE TABLE gtestxx_3 (a int NOT NULL, b int GENERATED ALWAYS AS (a * 2) STORED);
321 ALTER TABLE gtestxx_3 INHERIT gtest1; -- ok
322 CREATE TABLE gtestxx_4 (b int GENERATED ALWAYS AS (a * 2) STORED, a int NOT NULL);
323 ALTER TABLE gtestxx_4 INHERIT gtest1; -- ok
324 -- test multiple inheritance mismatches
325 CREATE TABLE gtesty (x int, b int);
326 CREATE TABLE gtest1_2 () INHERITS (gtest1, gtesty); -- error
327 NOTICE: merging multiple inherited definitions of column "b"
328 ERROR: inherited column "b" has a generation conflict
330 CREATE TABLE gtesty (x int, b int GENERATED ALWAYS AS (x * 22) STORED);
331 CREATE TABLE gtest1_2 () INHERITS (gtest1, gtesty); -- error
332 NOTICE: merging multiple inherited definitions of column "b"
333 ERROR: column "b" inherits conflicting generation expressions
335 CREATE TABLE gtesty (x int, b int DEFAULT 55);
336 CREATE TABLE gtest1_2 () INHERITS (gtest0, gtesty); -- error
337 NOTICE: merging multiple inherited definitions of column "b"
338 ERROR: inherited column "b" has a generation conflict
340 -- test stored update
341 CREATE TABLE gtest3 (a int, b int GENERATED ALWAYS AS (a * 3) STORED);
342 INSERT INTO gtest3 (a) VALUES (1), (2), (3), (NULL);
343 SELECT * FROM gtest3 ORDER BY a;
352 UPDATE gtest3 SET a = 22 WHERE a = 2;
353 SELECT * FROM gtest3 ORDER BY a;
362 CREATE TABLE gtest3a (a text, b text GENERATED ALWAYS AS (a || '+' || a) STORED);
363 INSERT INTO gtest3a (a) VALUES ('a'), ('b'), ('c'), (NULL);
364 SELECT * FROM gtest3a ORDER BY a;
373 UPDATE gtest3a SET a = 'bb' WHERE a = 'b';
374 SELECT * FROM gtest3a ORDER BY a;
385 INSERT INTO gtest1 (a) VALUES (1), (2);
386 COPY gtest1 TO stdout;
389 COPY gtest1 (a, b) TO stdout;
390 ERROR: column "b" is a generated column
391 DETAIL: Generated columns cannot be used in COPY.
392 COPY gtest1 FROM stdin;
393 COPY gtest1 (a, b) FROM stdin;
394 ERROR: column "b" is a generated column
395 DETAIL: Generated columns cannot be used in COPY.
396 SELECT * FROM gtest1 ORDER BY a;
406 INSERT INTO gtest3 (a) VALUES (1), (2);
407 COPY gtest3 TO stdout;
410 COPY gtest3 (a, b) TO stdout;
411 ERROR: column "b" is a generated column
412 DETAIL: Generated columns cannot be used in COPY.
413 COPY gtest3 FROM stdin;
414 COPY gtest3 (a, b) FROM stdin;
415 ERROR: column "b" is a generated column
416 DETAIL: Generated columns cannot be used in COPY.
417 SELECT * FROM gtest3 ORDER BY a;
427 CREATE TABLE gtest2 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (NULL) STORED);
428 INSERT INTO gtest2 VALUES (1);
429 SELECT * FROM gtest2;
435 -- simple column reference for varlena types
436 CREATE TABLE gtest_varlena (a varchar, b varchar GENERATED ALWAYS AS (a) STORED);
437 INSERT INTO gtest_varlena (a) VALUES('01234567890123456789');
438 INSERT INTO gtest_varlena (a) VALUES(NULL);
439 SELECT * FROM gtest_varlena ORDER BY a;
441 ----------------------+----------------------
442 01234567890123456789 | 01234567890123456789
446 DROP TABLE gtest_varlena;
448 CREATE TYPE double_int as (a int, b int);
449 CREATE TABLE gtest4 (
451 b double_int GENERATED ALWAYS AS ((a * 2, a * 3)) STORED
453 INSERT INTO gtest4 VALUES (1), (6);
454 SELECT * FROM gtest4;
462 DROP TYPE double_int;
463 -- using tableoid is allowed
464 CREATE TABLE gtest_tableoid (
466 b bool GENERATED ALWAYS AS (tableoid = 'gtest_tableoid'::regclass) STORED
468 INSERT INTO gtest_tableoid VALUES (1), (2);
469 ALTER TABLE gtest_tableoid ADD COLUMN
470 c regclass GENERATED ALWAYS AS (tableoid) STORED;
471 SELECT * FROM gtest_tableoid;
473 ---+---+----------------
474 1 | t | gtest_tableoid
475 2 | t | gtest_tableoid
478 -- drop column behavior
479 CREATE TABLE gtest10 (a int PRIMARY KEY, b int, c int GENERATED ALWAYS AS (b * 2) STORED);
480 ALTER TABLE gtest10 DROP COLUMN b;
482 Table "public.gtest10"
483 Column | Type | Collation | Nullable | Default
484 --------+---------+-----------+----------+---------
485 a | integer | | not null |
487 "gtest10_pkey" PRIMARY KEY, btree (a)
489 CREATE TABLE gtest10a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED);
490 ALTER TABLE gtest10a DROP COLUMN b;
491 INSERT INTO gtest10a (a) VALUES (1);
493 CREATE USER regress_user11;
494 CREATE TABLE gtest11s (a int PRIMARY KEY, b int, c int GENERATED ALWAYS AS (b * 2) STORED);
495 INSERT INTO gtest11s VALUES (1, 10), (2, 20);
496 GRANT SELECT (a, c) ON gtest11s TO regress_user11;
497 CREATE FUNCTION gf1(a int) RETURNS int AS $$ SELECT a * 3 $$ IMMUTABLE LANGUAGE SQL;
498 REVOKE ALL ON FUNCTION gf1(int) FROM PUBLIC;
499 CREATE TABLE gtest12s (a int PRIMARY KEY, b int, c int GENERATED ALWAYS AS (gf1(b)) STORED);
500 INSERT INTO gtest12s VALUES (1, 10), (2, 20);
501 GRANT SELECT (a, c) ON gtest12s TO regress_user11;
502 SET ROLE regress_user11;
503 SELECT a, b FROM gtest11s; -- not allowed
504 ERROR: permission denied for table gtest11s
505 SELECT a, c FROM gtest11s; -- allowed
512 SELECT gf1(10); -- not allowed
513 ERROR: permission denied for function gf1
514 SELECT a, c FROM gtest12s; -- allowed
522 DROP TABLE gtest11s, gtest12s;
523 DROP FUNCTION gf1(int);
524 DROP USER regress_user11;
526 CREATE TABLE gtest20 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED CHECK (b < 50));
527 INSERT INTO gtest20 (a) VALUES (10); -- ok
528 INSERT INTO gtest20 (a) VALUES (30); -- violates constraint
529 ERROR: new row for relation "gtest20" violates check constraint "gtest20_b_check"
530 DETAIL: Failing row contains (30, 60).
531 CREATE TABLE gtest20a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED);
532 INSERT INTO gtest20a (a) VALUES (10);
533 INSERT INTO gtest20a (a) VALUES (30);
534 ALTER TABLE gtest20a ADD CHECK (b < 50); -- fails on existing row
535 ERROR: check constraint "gtest20a_b_check" of relation "gtest20a" is violated by some row
536 CREATE TABLE gtest20b (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED);
537 INSERT INTO gtest20b (a) VALUES (10);
538 INSERT INTO gtest20b (a) VALUES (30);
539 ALTER TABLE gtest20b ADD CONSTRAINT chk CHECK (b < 50) NOT VALID;
540 ALTER TABLE gtest20b VALIDATE CONSTRAINT chk; -- fails on existing row
541 ERROR: check constraint "chk" of relation "gtest20b" is violated by some row
542 -- not-null constraints
543 CREATE TABLE gtest21a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED NOT NULL);
544 INSERT INTO gtest21a (a) VALUES (1); -- ok
545 INSERT INTO gtest21a (a) VALUES (0); -- violates constraint
546 ERROR: null value in column "b" of relation "gtest21a" violates not-null constraint
547 DETAIL: Failing row contains (0, null).
548 CREATE TABLE gtest21b (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED);
549 ALTER TABLE gtest21b ALTER COLUMN b SET NOT NULL;
550 INSERT INTO gtest21b (a) VALUES (1); -- ok
551 INSERT INTO gtest21b (a) VALUES (0); -- violates constraint
552 ERROR: null value in column "b" of relation "gtest21b" violates not-null constraint
553 DETAIL: Failing row contains (0, null).
554 ALTER TABLE gtest21b ALTER COLUMN b DROP NOT NULL;
555 INSERT INTO gtest21b (a) VALUES (0); -- ok now
557 CREATE TABLE gtest22a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a / 2) STORED UNIQUE);
558 INSERT INTO gtest22a VALUES (2);
559 INSERT INTO gtest22a VALUES (3);
560 ERROR: duplicate key value violates unique constraint "gtest22a_b_key"
561 DETAIL: Key (b)=(1) already exists.
562 INSERT INTO gtest22a VALUES (4);
563 CREATE TABLE gtest22b (a int, b int GENERATED ALWAYS AS (a / 2) STORED, PRIMARY KEY (a, b));
564 INSERT INTO gtest22b VALUES (2);
565 INSERT INTO gtest22b VALUES (2);
566 ERROR: duplicate key value violates unique constraint "gtest22b_pkey"
567 DETAIL: Key (a, b)=(2, 1) already exists.
569 CREATE TABLE gtest22c (a int, b int GENERATED ALWAYS AS (a * 2) STORED);
570 CREATE INDEX gtest22c_b_idx ON gtest22c (b);
571 CREATE INDEX gtest22c_expr_idx ON gtest22c ((b * 3));
572 CREATE INDEX gtest22c_pred_idx ON gtest22c (a) WHERE b > 0;
574 Table "public.gtest22c"
575 Column | Type | Collation | Nullable | Default
576 --------+---------+-----------+----------+------------------------------------
578 b | integer | | | generated always as (a * 2) stored
580 "gtest22c_b_idx" btree (b)
581 "gtest22c_expr_idx" btree ((b * 3))
582 "gtest22c_pred_idx" btree (a) WHERE b > 0
584 INSERT INTO gtest22c VALUES (1), (2), (3);
585 SET enable_seqscan TO off;
586 SET enable_bitmapscan TO off;
587 EXPLAIN (COSTS OFF) SELECT * FROM gtest22c WHERE b = 4;
589 ---------------------------------------------
590 Index Scan using gtest22c_b_idx on gtest22c
594 SELECT * FROM gtest22c WHERE b = 4;
600 EXPLAIN (COSTS OFF) SELECT * FROM gtest22c WHERE b * 3 = 6;
602 ------------------------------------------------
603 Index Scan using gtest22c_expr_idx on gtest22c
604 Index Cond: ((b * 3) = 6)
607 SELECT * FROM gtest22c WHERE b * 3 = 6;
613 EXPLAIN (COSTS OFF) SELECT * FROM gtest22c WHERE a = 1 AND b > 0;
615 ------------------------------------------------
616 Index Scan using gtest22c_pred_idx on gtest22c
620 SELECT * FROM gtest22c WHERE a = 1 AND b > 0;
626 RESET enable_seqscan;
627 RESET enable_bitmapscan;
629 CREATE TABLE gtest23a (x int PRIMARY KEY, y int);
630 INSERT INTO gtest23a VALUES (1, 11), (2, 22), (3, 33);
631 CREATE TABLE gtest23x (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED REFERENCES gtest23a (x) ON UPDATE CASCADE); -- error
632 ERROR: invalid ON UPDATE action for foreign key constraint containing generated column
633 CREATE TABLE gtest23x (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED REFERENCES gtest23a (x) ON DELETE SET NULL); -- error
634 ERROR: invalid ON DELETE action for foreign key constraint containing generated column
635 CREATE TABLE gtest23b (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED REFERENCES gtest23a (x));
637 Table "public.gtest23b"
638 Column | Type | Collation | Nullable | Default
639 --------+---------+-----------+----------+------------------------------------
640 a | integer | | not null |
641 b | integer | | | generated always as (a * 2) stored
643 "gtest23b_pkey" PRIMARY KEY, btree (a)
644 Foreign-key constraints:
645 "gtest23b_b_fkey" FOREIGN KEY (b) REFERENCES gtest23a(x)
647 INSERT INTO gtest23b VALUES (1); -- ok
648 INSERT INTO gtest23b VALUES (5); -- error
649 ERROR: insert or update on table "gtest23b" violates foreign key constraint "gtest23b_b_fkey"
650 DETAIL: Key (b)=(10) is not present in table "gtest23a".
653 CREATE TABLE gtest23p (x int, y int GENERATED ALWAYS AS (x * 2) STORED, PRIMARY KEY (y));
654 INSERT INTO gtest23p VALUES (1), (2), (3);
655 CREATE TABLE gtest23q (a int PRIMARY KEY, b int REFERENCES gtest23p (y));
656 INSERT INTO gtest23q VALUES (1, 2); -- ok
657 INSERT INTO gtest23q VALUES (2, 5); -- error
658 ERROR: insert or update on table "gtest23q" violates foreign key constraint "gtest23q_b_fkey"
659 DETAIL: Key (b)=(5) is not present in table "gtest23p".
661 CREATE DOMAIN gtestdomain1 AS int CHECK (VALUE < 10);
662 CREATE TABLE gtest24 (a int PRIMARY KEY, b gtestdomain1 GENERATED ALWAYS AS (a * 2) STORED);
663 INSERT INTO gtest24 (a) VALUES (4); -- ok
664 INSERT INTO gtest24 (a) VALUES (6); -- error
665 ERROR: value for domain gtestdomain1 violates check constraint "gtestdomain1_check"
666 -- typed tables (currently not supported)
667 CREATE TYPE gtest_type AS (f1 integer, f2 text, f3 bigint);
668 CREATE TABLE gtest28 OF gtest_type (f1 WITH OPTIONS GENERATED ALWAYS AS (f2 *2) STORED);
669 ERROR: generated columns are not supported on typed tables
670 DROP TYPE gtest_type CASCADE;
671 -- table partitions (currently not supported)
672 CREATE TABLE gtest_parent (f1 date NOT NULL, f2 text, f3 bigint) PARTITION BY RANGE (f1);
673 CREATE TABLE gtest_child PARTITION OF gtest_parent (
674 f3 WITH OPTIONS GENERATED ALWAYS AS (f2 * 2) STORED
675 ) FOR VALUES FROM ('2016-07-01') TO ('2016-08-01'); -- error
676 ERROR: generated columns are not supported on partitions
677 DROP TABLE gtest_parent;
679 CREATE TABLE gtest_parent (f1 date NOT NULL, f2 bigint, f3 bigint GENERATED ALWAYS AS (f2 * 2) STORED) PARTITION BY RANGE (f1);
680 CREATE TABLE gtest_child PARTITION OF gtest_parent FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');
681 INSERT INTO gtest_parent (f1, f2) VALUES ('2016-07-15', 1);
682 SELECT * FROM gtest_parent;
684 ------------+----+----
688 SELECT * FROM gtest_child;
690 ------------+----+----
694 DROP TABLE gtest_parent;
695 -- generated columns in partition key (not allowed)
696 CREATE TABLE gtest_parent (f1 date NOT NULL, f2 bigint, f3 bigint GENERATED ALWAYS AS (f2 * 2) STORED) PARTITION BY RANGE (f3);
697 ERROR: cannot use generated column in partition key
698 LINE 1: ...ENERATED ALWAYS AS (f2 * 2) STORED) PARTITION BY RANGE (f3);
700 DETAIL: Column "f3" is a generated column.
701 CREATE TABLE gtest_parent (f1 date NOT NULL, f2 bigint, f3 bigint GENERATED ALWAYS AS (f2 * 2) STORED) PARTITION BY RANGE ((f3 * 3));
702 ERROR: cannot use generated column in partition key
703 LINE 1: ...ED ALWAYS AS (f2 * 2) STORED) PARTITION BY RANGE ((f3 * 3));
705 DETAIL: Column "f3" is a generated column.
706 -- ALTER TABLE ... ADD COLUMN
707 CREATE TABLE gtest25 (a int PRIMARY KEY);
708 INSERT INTO gtest25 VALUES (3), (4);
709 ALTER TABLE gtest25 ADD COLUMN b int GENERATED ALWAYS AS (a * 3) STORED;
710 SELECT * FROM gtest25 ORDER BY a;
717 ALTER TABLE gtest25 ADD COLUMN x int GENERATED ALWAYS AS (b * 4) STORED; -- error
718 ERROR: cannot use generated column "b" in column generation expression
719 DETAIL: A generated column cannot reference another generated column.
720 ALTER TABLE gtest25 ADD COLUMN x int GENERATED ALWAYS AS (z * 4) STORED; -- error
721 ERROR: column "z" does not exist
722 ALTER TABLE gtest25 ADD COLUMN c int DEFAULT 42,
723 ADD COLUMN x int GENERATED ALWAYS AS (c * 4) STORED;
724 ALTER TABLE gtest25 ADD COLUMN d int DEFAULT 101;
725 ALTER TABLE gtest25 ALTER COLUMN d SET DATA TYPE float8,
726 ADD COLUMN y float8 GENERATED ALWAYS AS (d * 4) STORED;
727 SELECT * FROM gtest25 ORDER BY a;
728 a | b | c | x | d | y
729 ---+----+----+-----+-----+-----
730 3 | 9 | 42 | 168 | 101 | 404
731 4 | 12 | 42 | 168 | 101 | 404
735 Table "public.gtest25"
736 Column | Type | Collation | Nullable | Default
737 --------+------------------+-----------+----------+------------------------------------------------------
738 a | integer | | not null |
739 b | integer | | | generated always as (a * 3) stored
741 x | integer | | | generated always as (c * 4) stored
742 d | double precision | | | 101
743 y | double precision | | | generated always as (d * 4::double precision) stored
745 "gtest25_pkey" PRIMARY KEY, btree (a)
747 -- ALTER TABLE ... ALTER COLUMN
748 CREATE TABLE gtest27 (
751 x int GENERATED ALWAYS AS ((a + b) * 2) STORED
753 INSERT INTO gtest27 (a, b) VALUES (3, 7), (4, 11);
754 ALTER TABLE gtest27 ALTER COLUMN a TYPE text; -- error
755 ERROR: cannot alter type of a column used by a generated column
756 DETAIL: Column "a" is used by generated column "x".
757 ALTER TABLE gtest27 ALTER COLUMN x TYPE numeric;
759 Table "public.gtest27"
760 Column | Type | Collation | Nullable | Default
761 --------+---------+-----------+----------+--------------------------------------------
764 x | numeric | | | generated always as (((a + b) * 2)) stored
766 SELECT * FROM gtest27;
773 ALTER TABLE gtest27 ALTER COLUMN x TYPE boolean USING x <> 0; -- error
774 ERROR: generation expression for column "x" cannot be cast automatically to type boolean
775 ALTER TABLE gtest27 ALTER COLUMN x DROP DEFAULT; -- error
776 ERROR: column "x" of relation "gtest27" is a generated column
777 HINT: Use ALTER TABLE ... ALTER COLUMN ... DROP EXPRESSION instead.
778 -- It's possible to alter the column types this way:
781 ALTER COLUMN a TYPE bigint,
782 ALTER COLUMN b TYPE bigint,
783 ADD COLUMN x bigint GENERATED ALWAYS AS ((a + b) * 2) STORED;
785 Table "public.gtest27"
786 Column | Type | Collation | Nullable | Default
787 --------+--------+-----------+----------+------------------------------------------
790 x | bigint | | | generated always as ((a + b) * 2) stored
792 -- Ideally you could just do this, but not today (and should x change type?):
794 ALTER COLUMN a TYPE float8,
795 ALTER COLUMN b TYPE float8; -- error
796 ERROR: cannot alter type of a column used by a generated column
797 DETAIL: Column "a" is used by generated column "x".
799 Table "public.gtest27"
800 Column | Type | Collation | Nullable | Default
801 --------+--------+-----------+----------+------------------------------------------
804 x | bigint | | | generated always as ((a + b) * 2) stored
806 SELECT * FROM gtest27;
813 -- ALTER TABLE ... ALTER COLUMN ... DROP EXPRESSION
814 CREATE TABLE gtest29 (
816 b int GENERATED ALWAYS AS (a * 2) STORED
818 INSERT INTO gtest29 (a) VALUES (3), (4);
819 ALTER TABLE gtest29 ALTER COLUMN a DROP EXPRESSION; -- error
820 ERROR: column "a" of relation "gtest29" is not a stored generated column
821 ALTER TABLE gtest29 ALTER COLUMN a DROP EXPRESSION IF EXISTS; -- notice
822 NOTICE: column "a" of relation "gtest29" is not a stored generated column, skipping
823 ALTER TABLE gtest29 ALTER COLUMN b DROP EXPRESSION;
824 INSERT INTO gtest29 (a) VALUES (5);
825 INSERT INTO gtest29 (a, b) VALUES (6, 66);
826 SELECT * FROM gtest29;
836 Table "public.gtest29"
837 Column | Type | Collation | Nullable | Default
838 --------+---------+-----------+----------+---------
842 -- check that dependencies between columns have also been removed
843 ALTER TABLE gtest29 DROP COLUMN a; -- should not drop b
845 Table "public.gtest29"
846 Column | Type | Collation | Nullable | Default
847 --------+---------+-----------+----------+---------
851 CREATE TABLE gtest30 (
853 b int GENERATED ALWAYS AS (a * 2) STORED
855 CREATE TABLE gtest30_1 () INHERITS (gtest30);
856 ALTER TABLE gtest30 ALTER COLUMN b DROP EXPRESSION;
858 Table "public.gtest30"
859 Column | Type | Collation | Nullable | Default
860 --------+---------+-----------+----------+---------
863 Number of child tables: 1 (Use \d+ to list them.)
866 Table "public.gtest30_1"
867 Column | Type | Collation | Nullable | Default
868 --------+---------+-----------+----------+---------
873 DROP TABLE gtest30 CASCADE;
874 NOTICE: drop cascades to table gtest30_1
875 CREATE TABLE gtest30 (
877 b int GENERATED ALWAYS AS (a * 2) STORED
879 CREATE TABLE gtest30_1 () INHERITS (gtest30);
880 ALTER TABLE ONLY gtest30 ALTER COLUMN b DROP EXPRESSION; -- error
881 ERROR: ALTER TABLE / DROP EXPRESSION must be applied to child tables too
883 Table "public.gtest30"
884 Column | Type | Collation | Nullable | Default
885 --------+---------+-----------+----------+------------------------------------
887 b | integer | | | generated always as (a * 2) stored
888 Number of child tables: 1 (Use \d+ to list them.)
891 Table "public.gtest30_1"
892 Column | Type | Collation | Nullable | Default
893 --------+---------+-----------+----------+------------------------------------
895 b | integer | | | generated always as (a * 2) stored
898 ALTER TABLE gtest30_1 ALTER COLUMN b DROP EXPRESSION; -- error
899 ERROR: cannot drop generation expression from inherited column
901 CREATE TABLE gtest26 (
903 b int GENERATED ALWAYS AS (a * 2) STORED
905 CREATE FUNCTION gtest_trigger_func() RETURNS trigger
909 IF tg_op IN ('DELETE', 'UPDATE') THEN
910 RAISE INFO '%: %: old = %', TG_NAME, TG_WHEN, OLD;
912 IF tg_op IN ('INSERT', 'UPDATE') THEN
913 RAISE INFO '%: %: new = %', TG_NAME, TG_WHEN, NEW;
915 IF tg_op = 'DELETE' THEN
922 CREATE TRIGGER gtest1 BEFORE DELETE OR UPDATE ON gtest26
924 WHEN (OLD.b < 0) -- ok
925 EXECUTE PROCEDURE gtest_trigger_func();
926 CREATE TRIGGER gtest2a BEFORE INSERT OR UPDATE ON gtest26
928 WHEN (NEW.b < 0) -- error
929 EXECUTE PROCEDURE gtest_trigger_func();
930 ERROR: BEFORE trigger's WHEN condition cannot reference NEW generated columns
931 LINE 3: WHEN (NEW.b < 0)
933 DETAIL: Column "b" is a generated column.
934 CREATE TRIGGER gtest2b BEFORE INSERT OR UPDATE ON gtest26
936 WHEN (NEW.* IS NOT NULL) -- error
937 EXECUTE PROCEDURE gtest_trigger_func();
938 ERROR: BEFORE trigger's WHEN condition cannot reference NEW generated columns
939 LINE 3: WHEN (NEW.* IS NOT NULL)
941 DETAIL: A whole-row reference is used and the table contains generated columns.
942 CREATE TRIGGER gtest2 BEFORE INSERT ON gtest26
945 EXECUTE PROCEDURE gtest_trigger_func();
946 CREATE TRIGGER gtest3 AFTER DELETE OR UPDATE ON gtest26
948 WHEN (OLD.b < 0) -- ok
949 EXECUTE PROCEDURE gtest_trigger_func();
950 CREATE TRIGGER gtest4 AFTER INSERT OR UPDATE ON gtest26
952 WHEN (NEW.b < 0) -- ok
953 EXECUTE PROCEDURE gtest_trigger_func();
954 INSERT INTO gtest26 (a) VALUES (-2), (0), (3);
955 INFO: gtest2: BEFORE: new = (-2,)
956 INFO: gtest4: AFTER: new = (-2,-4)
957 SELECT * FROM gtest26 ORDER BY a;
965 UPDATE gtest26 SET a = a * -2;
966 INFO: gtest1: BEFORE: old = (-2,-4)
967 INFO: gtest1: BEFORE: new = (4,)
968 INFO: gtest3: AFTER: old = (-2,-4)
969 INFO: gtest3: AFTER: new = (4,8)
970 INFO: gtest4: AFTER: old = (3,6)
971 INFO: gtest4: AFTER: new = (-6,-12)
972 SELECT * FROM gtest26 ORDER BY a;
980 DELETE FROM gtest26 WHERE a = -6;
981 INFO: gtest1: BEFORE: old = (-6,-12)
982 INFO: gtest3: AFTER: old = (-6,-12)
983 SELECT * FROM gtest26 ORDER BY a;
990 DROP TRIGGER gtest1 ON gtest26;
991 DROP TRIGGER gtest2 ON gtest26;
992 DROP TRIGGER gtest3 ON gtest26;
993 -- Check that an UPDATE of "a" fires the trigger for UPDATE OF b, per
995 CREATE FUNCTION gtest_trigger_func3() RETURNS trigger
1003 CREATE TRIGGER gtest11 BEFORE UPDATE OF b ON gtest26
1005 EXECUTE PROCEDURE gtest_trigger_func3();
1006 UPDATE gtest26 SET a = 1 WHERE a = 0;
1008 DROP TRIGGER gtest11 ON gtest26;
1010 -- check that modifications of stored generated columns in triggers do
1011 -- not get propagated
1012 CREATE FUNCTION gtest_trigger_func4() RETURNS trigger
1021 CREATE TRIGGER gtest12_01 BEFORE UPDATE ON gtest26
1023 EXECUTE PROCEDURE gtest_trigger_func();
1024 CREATE TRIGGER gtest12_02 BEFORE UPDATE ON gtest26
1026 EXECUTE PROCEDURE gtest_trigger_func4();
1027 CREATE TRIGGER gtest12_03 BEFORE UPDATE ON gtest26
1029 EXECUTE PROCEDURE gtest_trigger_func();
1030 INSERT INTO gtest26 (a) VALUES (1);
1031 UPDATE gtest26 SET a = 11 WHERE a = 1;
1032 INFO: gtest12_01: BEFORE: old = (1,2)
1033 INFO: gtest12_01: BEFORE: new = (11,)
1034 INFO: gtest12_03: BEFORE: old = (1,2)
1035 INFO: gtest12_03: BEFORE: new = (10,)
1036 SELECT * FROM gtest26 ORDER BY a;
1042 -- LIKE INCLUDING GENERATED and dropped column handling
1043 CREATE TABLE gtest28a (
1047 x int GENERATED ALWAYS AS (b * 2) STORED
1049 ALTER TABLE gtest28a DROP COLUMN a;
1050 CREATE TABLE gtest28b (LIKE gtest28a INCLUDING GENERATED);
1052 Table "public.gtest28a"
1053 Column | Type | Collation | Nullable | Default
1054 --------+---------+-----------+----------+------------------------------------
1057 x | integer | | | generated always as (b * 2) stored
1059 Table "public.gtest28b"
1060 Column | Type | Collation | Nullable | Default
1061 --------+---------+-----------+----------+------------------------------------
1064 x | integer | | | generated always as (b * 2) stored