mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
Fix several DDL issues of generated columns versus inheritance
Several combinations of generated columns and inheritance in CREATE TABLE were not handled correctly. Specifically: - Disallow a child column specifying a generation expression if the parent column is a generated column. The child column definition must be unadorned and the parent column's generation expression will be copied. - Prohibit a child column of a generated parent column specifying default values or identity. - Allow a child column of a not-generated parent column specifying itself as a generated column. This previously did not work, but it was possible to arrive at the state via other means (involving ALTER TABLE), so it seems sensible to support it. Add tests for each case. Also add documentation about the rules involving generated columns and inheritance. Discussion: https://www.postgresql.org/message-id/flat/15830.1575468847%40sss.pgh.pa.us https://www.postgresql.org/message-id/flat/2678bad1-048f-519a-ef24-b12962f41807%40enterprisedb.com https://www.postgresql.org/message-id/flat/CAJvUf_u4h0DxkCMCeEKAWCuzGUTnDP-G5iVmSwxLQSXn0_FWNQ%40mail.gmail.com
This commit is contained in:
@ -219,12 +219,54 @@ SELECT * FROM gtest1;
|
||||
4 | 8
|
||||
(2 rows)
|
||||
|
||||
-- test inheritance mismatch
|
||||
CREATE TABLE gtest_normal (a int, b int);
|
||||
CREATE TABLE gtest_normal_child (a int, b int GENERATED ALWAYS AS (a * 2) STORED) INHERITS (gtest_normal);
|
||||
NOTICE: merging column "a" with inherited definition
|
||||
NOTICE: merging column "b" with inherited definition
|
||||
\d gtest_normal_child
|
||||
Table "public.gtest_normal_child"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
--------+---------+-----------+----------+------------------------------------
|
||||
a | integer | | |
|
||||
b | integer | | | generated always as (a * 2) stored
|
||||
Inherits: gtest_normal
|
||||
|
||||
INSERT INTO gtest_normal (a) VALUES (1);
|
||||
INSERT INTO gtest_normal_child (a) VALUES (2);
|
||||
SELECT * FROM gtest_normal;
|
||||
a | b
|
||||
---+---
|
||||
1 |
|
||||
2 | 4
|
||||
(2 rows)
|
||||
|
||||
-- test inheritance mismatches between parent and child
|
||||
CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS (a * 22) STORED) INHERITS (gtest1); -- error
|
||||
NOTICE: merging column "b" with inherited definition
|
||||
ERROR: child column "b" specifies generation expression
|
||||
HINT: Omit the generation expression in the definition of the child table column to inherit the generation expression from the parent table.
|
||||
CREATE TABLE gtestx (x int, b int DEFAULT 10) INHERITS (gtest1); -- error
|
||||
NOTICE: merging column "b" with inherited definition
|
||||
ERROR: column "b" inherits from generated column but specifies default
|
||||
CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS IDENTITY) INHERITS (gtest1); -- error
|
||||
NOTICE: merging column "b" with inherited definition
|
||||
ERROR: column "b" inherits from generated column but specifies identity
|
||||
-- test multiple inheritance mismatches
|
||||
CREATE TABLE gtesty (x int, b int);
|
||||
CREATE TABLE gtest1_2 () INHERITS (gtest1, gtesty); -- error
|
||||
NOTICE: merging multiple inherited definitions of column "b"
|
||||
ERROR: inherited column "b" has a generation conflict
|
||||
DROP TABLE gtesty;
|
||||
CREATE TABLE gtesty (x int, b int GENERATED ALWAYS AS (x * 22) STORED);
|
||||
CREATE TABLE gtest1_2 () INHERITS (gtest1, gtesty); -- error
|
||||
NOTICE: merging multiple inherited definitions of column "b"
|
||||
ERROR: column "b" inherits conflicting generation expressions
|
||||
DROP TABLE gtesty;
|
||||
CREATE TABLE gtesty (x int, b int DEFAULT 55);
|
||||
CREATE TABLE gtest1_2 () INHERITS (gtest0, gtesty); -- error
|
||||
NOTICE: merging multiple inherited definitions of column "b"
|
||||
ERROR: inherited column "b" has a generation conflict
|
||||
DROP TABLE gtesty;
|
||||
-- test stored update
|
||||
CREATE TABLE gtest3 (a int, b int GENERATED ALWAYS AS (a * 3) STORED);
|
||||
INSERT INTO gtest3 (a) VALUES (1), (2), (3), (NULL);
|
||||
|
@ -89,11 +89,31 @@ INSERT INTO gtest1_1 VALUES (4);
|
||||
SELECT * FROM gtest1_1;
|
||||
SELECT * FROM gtest1;
|
||||
|
||||
-- test inheritance mismatch
|
||||
CREATE TABLE gtest_normal (a int, b int);
|
||||
CREATE TABLE gtest_normal_child (a int, b int GENERATED ALWAYS AS (a * 2) STORED) INHERITS (gtest_normal);
|
||||
\d gtest_normal_child
|
||||
INSERT INTO gtest_normal (a) VALUES (1);
|
||||
INSERT INTO gtest_normal_child (a) VALUES (2);
|
||||
SELECT * FROM gtest_normal;
|
||||
|
||||
-- test inheritance mismatches between parent and child
|
||||
CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS (a * 22) STORED) INHERITS (gtest1); -- error
|
||||
CREATE TABLE gtestx (x int, b int DEFAULT 10) INHERITS (gtest1); -- error
|
||||
CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS IDENTITY) INHERITS (gtest1); -- error
|
||||
|
||||
-- test multiple inheritance mismatches
|
||||
CREATE TABLE gtesty (x int, b int);
|
||||
CREATE TABLE gtest1_2 () INHERITS (gtest1, gtesty); -- error
|
||||
DROP TABLE gtesty;
|
||||
|
||||
CREATE TABLE gtesty (x int, b int GENERATED ALWAYS AS (x * 22) STORED);
|
||||
CREATE TABLE gtest1_2 () INHERITS (gtest1, gtesty); -- error
|
||||
DROP TABLE gtesty;
|
||||
|
||||
CREATE TABLE gtesty (x int, b int DEFAULT 55);
|
||||
CREATE TABLE gtest1_2 () INHERITS (gtest0, gtesty); -- error
|
||||
DROP TABLE gtesty;
|
||||
|
||||
-- test stored update
|
||||
CREATE TABLE gtest3 (a int, b int GENERATED ALWAYS AS (a * 3) STORED);
|
||||
INSERT INTO gtest3 (a) VALUES (1), (2), (3), (NULL);
|
||||
|
Reference in New Issue
Block a user