From 50950964baa11eb451abda954d4893952c95f9f0 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Date: Mon, 11 Dec 2023 16:20:15 +0530
Subject: [PATCH 10/14] Extra tests for adding identity to a column

NOT FOR FINAL COMMIT
---
 .../regress/expected/identity_part_extra.out  | 111 +++++++++++++++++-
 src/test/regress/sql/identity_part_extra.sql  |  53 ++++++++-
 2 files changed, 158 insertions(+), 6 deletions(-)

diff --git a/src/test/regress/expected/identity_part_extra.out b/src/test/regress/expected/identity_part_extra.out
index 644395f89a..05de0cabab 100644
--- a/src/test/regress/expected/identity_part_extra.out
+++ b/src/test/regress/expected/identity_part_extra.out
@@ -320,11 +320,116 @@ SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
  itest_p4 | 10-03-2016 | from itest_p4     | 10
 (10 rows)
 
+DROP TABLE itest_parted;
+-- changing a regular column to identity column in a partitioned table
+CREATE TABLE itest_parted (f1 date NOT NULL, f2 text, f3 int) PARTITION BY RANGE (f1);
+CREATE TABLE itest_p1 PARTITION OF itest_parted FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');
+INSERT into itest_parted VALUES ('2016-07-2', 'from itest_parted', 1);
+INSERT into itest_p1 VALUES ('2016-07-3', 'from itest_p1', 2);
+ALTER TABLE ONLY itest_parted -- ONLY causes the command to fail
+            ALTER COLUMN f3 SET NOT NULL, -- avoids error
+            ALTER COLUMN f3 ADD generated always as identity (start with 3);
+ERROR:  constraint must be added to child tables too
+HINT:  Do not specify the ONLY keyword.
+ALTER TABLE itest_parted
+            ALTER COLUMN f3 SET NOT NULL, -- avoids error
+            ALTER COLUMN f3 ADD generated always as identity (start with 3);
+SELECT attrelid::regclass, attname, attidentity, atthasdef, attgenerated, attnotnull
+    FROM pg_attribute
+    WHERE attrelid IN (SELECT oid FROM pg_class WHERE relname like 'itest_p%'
+                                                    and relkind in ('r', 'p'))
+        AND attnum > 0
+    ORDER BY attrelid, attnum;
+   attrelid   | attname | attidentity | atthasdef | attgenerated | attnotnull 
+--------------+---------+-------------+-----------+--------------+------------
+ itest_parted | f1      |             | f         |              | t
+ itest_parted | f2      |             | f         |              | f
+ itest_parted | f3      | a           | f         |              | t
+ itest_p1     | f1      |             | f         |              | t
+ itest_p1     | f2      |             | f         |              | f
+ itest_p1     | f3      | a           | f         |              | t
+(6 rows)
+
+SELECT conrelid::regclass, conname, connoinherit, conislocal, coninhcount
+    FROM pg_constraint
+    WHERE conrelid in (SELECT oid FROM pg_class WHERE relname like 'itest_p%'
+                                                    and relkind in ('r', 'p'))
+    ORDER BY conrelid, conname;
+   conrelid   |         conname          | connoinherit | conislocal | coninhcount 
+--------------+--------------------------+--------------+------------+-------------
+ itest_parted | itest_parted_f1_not_null | f            | t          |           0
+ itest_parted | itest_parted_f3_not_null | f            | t          |           0
+ itest_p1     | itest_parted_f1_not_null | f            | f          |           1
+ itest_p1     | itest_parted_f3_not_null | f            | f          |           1
+(4 rows)
+
+SELECT classid, objid_name, objsubid, refclassid, refobjid_name, refobjsubid, deptype
+    FROM v_pg_depend
+    WHERE (refobjid in (SELECT oid FROM pg_class WHERE relname like 'itest_p%'
+                                                    and relkind in ('r', 'p'))
+            OR objid in (SELECT oid FROM pg_class WHERE relname like 'itest_p%'
+                                                    and relkind in ('r', 'p')))
+            AND objid_name NOT LIKE 'pg_toast%'
+    ORDER BY classid, objid_name, objsubid, refclassid, refobjid_name, refobjsubid, deptype;
+    classid    |           objid_name            | objsubid |  refclassid  | refobjid_name | refobjsubid |   deptype    
+---------------+---------------------------------+----------+--------------+---------------+-------------+--------------
+ pg_type       | itest_p1                        |        0 | pg_class     | itest_p1      |           0 | internal (i)
+ pg_type       | itest_parted                    |        0 | pg_class     | itest_parted  |           0 | internal (i)
+ pg_class      | itest_p1                        |        0 | pg_class     | itest_parted  |           0 | auto (a)
+ pg_class      | itest_p1                        |        0 | pg_namespace | 2200          |           0 | normal (n)
+ pg_class      | itest_parted                    |        0 | pg_namespace | 2200          |           0 | normal (n)
+ pg_class      | itest_parted                    |        1 | pg_class     | itest_parted  |           0 | internal (i)
+ pg_class      | itest_parted_f3_seq             |        0 | pg_class     | itest_parted  |           3 | internal (i)
+ pg_constraint | public.itest_parted_f1_not_null |        0 | pg_class     | itest_p1      |           1 | auto (a)
+ pg_constraint | public.itest_parted_f1_not_null |        0 | pg_class     | itest_parted  |           1 | auto (a)
+ pg_constraint | public.itest_parted_f3_not_null |        0 | pg_class     | itest_p1      |           3 | auto (a)
+ pg_constraint | public.itest_parted_f3_not_null |        0 | pg_class     | itest_parted  |           3 | auto (a)
+(11 rows)
+
+SELECT seqrelid::regclass, seqtypid::regtype FROM pg_sequence;
+      seqrelid       | seqtypid 
+---------------------+----------
+ itest_parted_f3_seq | integer
+(1 row)
+
+INSERT into itest_parted(f1, f2) VALUES ('2016-07-4', 'from itest_parted');
+INSERT into itest_p1 (f1, f2) VALUES ('2016-07-5', 'from itest_p1');
+SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
+ tableoid |     f1     |        f2         | f3 
+----------+------------+-------------------+----
+ itest_p1 | 07-02-2016 | from itest_parted |  1
+ itest_p1 | 07-03-2016 | from itest_p1     |  2
+ itest_p1 | 07-04-2016 | from itest_parted |  3
+ itest_p1 | 07-05-2016 | from itest_p1     |  4
+(4 rows)
+
+--extra tests - see if those need to be included in the main regression test
+CREATE TABLE itest_p3 PARTITION OF itest_parted FOR VALUES FROM ('2016-09-01') TO ('2016-10-01');
+CREATE TABLE itest_p4 (f1 date NOT NULL, f2 text, f3 int);
+-- TODO: ideally setting an identity column should automatically spawn the NOT NULL constraint. But it doesn't do that right now.
+ALTER TABLE itest_p4 ALTER COLUMN f3 SET NOT NULL;
+ALTER TABLE itest_parted ATTACH PARTITION itest_p4 FOR VALUES FROM ('2016-10-01') TO ('2016-11-01');
+INSERT into itest_parted(f1, f2) VALUES ('2016-09-2', 'from itest_parted');
+INSERT INTO itest_parted (f1, f2) VALUES ('2016-10-2', 'from itest_parted');
+INSERT into itest_p3 (f1, f2) VALUES ('2016-09-3', 'from itest_p3');
+INSERT INTO itest_p4 (f1, f2) VALUES ('2016-10-3', 'from itest_p4');
+SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
+ tableoid |     f1     |        f2         | f3 
+----------+------------+-------------------+----
+ itest_p1 | 07-02-2016 | from itest_parted |  1
+ itest_p1 | 07-03-2016 | from itest_p1     |  2
+ itest_p1 | 07-04-2016 | from itest_parted |  3
+ itest_p1 | 07-05-2016 | from itest_p1     |  4
+ itest_p3 | 09-02-2016 | from itest_parted |  5
+ itest_p3 | 09-03-2016 | from itest_p3     |  7
+ itest_p4 | 10-02-2016 | from itest_parted |  6
+ itest_p4 | 10-03-2016 | from itest_p4     |  8
+(8 rows)
+
 DROP TABLE itest_parted;
 -- scenarios to test
--- changing a normal column to identity column is reflected in all the partitions - 8th Dec
--- detaching a partition removes identity property - 11th Dec
--- attaching table with identity column is not allowed (even when the parent does not have an identity column) - 12th Dec
+-- detaching a partition removes identity property - 12th Dec
+-- attaching table with identity column is not allowed (even when the parent does not have an identity column) - 13th Dec
 -- trying to drop inherited identity of column of partition is not allowed - 13th Dec
 -- trying to change the identity properties of partition? Is that allowed? - 13th Dec
 -- changing NOT NULL attribute of inherited identity column of partition is not allowed
diff --git a/src/test/regress/sql/identity_part_extra.sql b/src/test/regress/sql/identity_part_extra.sql
index 33c5a30a94..dc8ba47617 100644
--- a/src/test/regress/sql/identity_part_extra.sql
+++ b/src/test/regress/sql/identity_part_extra.sql
@@ -112,13 +112,60 @@ SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
 
 DROP TABLE itest_parted;
 
+-- changing a regular column to identity column in a partitioned table
+CREATE TABLE itest_parted (f1 date NOT NULL, f2 text, f3 int) PARTITION BY RANGE (f1);
+CREATE TABLE itest_p1 PARTITION OF itest_parted FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');
+INSERT into itest_parted VALUES ('2016-07-2', 'from itest_parted', 1);
+INSERT into itest_p1 VALUES ('2016-07-3', 'from itest_p1', 2);
+ALTER TABLE ONLY itest_parted -- ONLY causes the command to fail
+            ALTER COLUMN f3 SET NOT NULL, -- avoids error
+            ALTER COLUMN f3 ADD generated always as identity (start with 3);
+ALTER TABLE itest_parted
+            ALTER COLUMN f3 SET NOT NULL, -- avoids error
+            ALTER COLUMN f3 ADD generated always as identity (start with 3);
+SELECT attrelid::regclass, attname, attidentity, atthasdef, attgenerated, attnotnull
+    FROM pg_attribute
+    WHERE attrelid IN (SELECT oid FROM pg_class WHERE relname like 'itest_p%'
+                                                    and relkind in ('r', 'p'))
+        AND attnum > 0
+    ORDER BY attrelid, attnum;
+SELECT conrelid::regclass, conname, connoinherit, conislocal, coninhcount
+    FROM pg_constraint
+    WHERE conrelid in (SELECT oid FROM pg_class WHERE relname like 'itest_p%'
+                                                    and relkind in ('r', 'p'))
+    ORDER BY conrelid, conname;
+SELECT classid, objid_name, objsubid, refclassid, refobjid_name, refobjsubid, deptype
+    FROM v_pg_depend
+    WHERE (refobjid in (SELECT oid FROM pg_class WHERE relname like 'itest_p%'
+                                                    and relkind in ('r', 'p'))
+            OR objid in (SELECT oid FROM pg_class WHERE relname like 'itest_p%'
+                                                    and relkind in ('r', 'p')))
+            AND objid_name NOT LIKE 'pg_toast%'
+    ORDER BY classid, objid_name, objsubid, refclassid, refobjid_name, refobjsubid, deptype;
+SELECT seqrelid::regclass, seqtypid::regtype FROM pg_sequence;
+INSERT into itest_parted(f1, f2) VALUES ('2016-07-4', 'from itest_parted');
+INSERT into itest_p1 (f1, f2) VALUES ('2016-07-5', 'from itest_p1');
+SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
+--extra tests - see if those need to be included in the main regression test
+CREATE TABLE itest_p3 PARTITION OF itest_parted FOR VALUES FROM ('2016-09-01') TO ('2016-10-01');
+CREATE TABLE itest_p4 (f1 date NOT NULL, f2 text, f3 int);
+-- TODO: ideally setting an identity column should automatically spawn the NOT NULL constraint. But it doesn't do that right now.
+ALTER TABLE itest_p4 ALTER COLUMN f3 SET NOT NULL;
+ALTER TABLE itest_parted ATTACH PARTITION itest_p4 FOR VALUES FROM ('2016-10-01') TO ('2016-11-01');
+INSERT into itest_parted(f1, f2) VALUES ('2016-09-2', 'from itest_parted');
+INSERT INTO itest_parted (f1, f2) VALUES ('2016-10-2', 'from itest_parted');
+INSERT into itest_p3 (f1, f2) VALUES ('2016-09-3', 'from itest_p3');
+INSERT INTO itest_p4 (f1, f2) VALUES ('2016-10-3', 'from itest_p4');
+SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
+
+DROP TABLE itest_parted;
+
 -- scenarios to test
 
--- changing a normal column to identity column is reflected in all the partitions - 8th Dec
 
--- detaching a partition removes identity property - 11th Dec
+-- detaching a partition removes identity property - 12th Dec
 
--- attaching table with identity column is not allowed (even when the parent does not have an identity column) - 12th Dec
+-- attaching table with identity column is not allowed (even when the parent does not have an identity column) - 13th Dec
 
 -- trying to drop inherited identity of column of partition is not allowed - 13th Dec
 -- trying to change the identity properties of partition? Is that allowed? - 13th Dec
-- 
2.25.1

