From a203c08d54947925541af1ea448f9984c7d3344c Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Date: Thu, 7 Dec 2023 11:08:17 +0530
Subject: [PATCH 07/14] Extra tests for adding column to a partitioned table

NOT FOR FINAL COMMIT
---
 .../regress/expected/identity_part_extra.out  | 130 +++++++++++++++++-
 src/test/regress/sql/identity_part_extra.sql  |  51 ++++++-
 2 files changed, 178 insertions(+), 3 deletions(-)

diff --git a/src/test/regress/expected/identity_part_extra.out b/src/test/regress/expected/identity_part_extra.out
index 81dfca2acc..644395f89a 100644
--- a/src/test/regress/expected/identity_part_extra.out
+++ b/src/test/regress/expected/identity_part_extra.out
@@ -101,6 +101,12 @@ SELECT classid, objid_name, objsubid, refclassid, refobjid_name, refobjsubid, de
  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 | bigint
+(1 row)
+
 INSERT into itest_parted(f1, f2) VALUES ('2016-07-2', 'from itest_parted');
 INSERT into itest_p1 (f1, f2) VALUES ('2016-07-3', 'from itest_p1');
 SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
@@ -179,6 +185,12 @@ SELECT classid, objid_name, objsubid, refclassid, refobjid_name, refobjsubid, de
  pg_constraint | public.itest_parted_f3_not_null |        0 | pg_class     | itest_parted  |           3 | auto (a)
 (16 rows)
 
+SELECT seqrelid::regclass, seqtypid::regtype FROM pg_sequence;
+      seqrelid       | seqtypid 
+---------------------+----------
+ itest_parted_f3_seq | bigint
+(1 row)
+
 INSERT INTO itest_p2 (f1, f2) VALUES ('2016-08-3', 'from itest_p2');
 INSERT INTO itest_parted (f1, f2) VALUES ('2016-08-4', 'from itest_parted');
 SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
@@ -191,9 +203,125 @@ SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
  itest_p2 | 08-04-2016 | from itest_parted |   4
 (5 rows)
 
+DROP TABLE itest_parted;
+-- adding an identity column to a partitioned table adds it to all the
+-- partitions, including the ones added after this operation
+CREATE TABLE itest_parted (f1 date NOT NULL, f2 text) PARTITION BY RANGE (f1);
+CREATE TABLE itest_p1 PARTITION OF itest_parted FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');
+CREATE TABLE itest_p2 PARTITION OF itest_parted FOR VALUES FROM ('2016-08-01') TO ('2016-09-01');
+INSERT into itest_parted(f1, f2) VALUES ('2016-07-2', 'from itest_parted');
+INSERT INTO itest_parted (f1, f2) VALUES ('2016-08-2', 'from itest_parted');
+ALTER TABLE itest_parted ADD COLUMN f3 int generated always as identity;
+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
+ itest_p2     | f1      |             | f         |              | t
+ itest_p2     | f2      |             | f         |              | f
+ itest_p2     | f3      | a           | f         |              | t
+(9 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
+ itest_p2     | itest_parted_f1_not_null | f            | f          |           1
+ itest_p2     | itest_parted_f3_not_null | f            | f          |           1
+(6 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_p2                        |        0 | pg_class     | itest_p2      |           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_p2                        |        0 | pg_class     | itest_parted  |           0 | auto (a)
+ pg_class      | itest_p2                        |        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_p2      |           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_p2      |           3 | auto (a)
+ pg_constraint | public.itest_parted_f3_not_null |        0 | pg_class     | itest_parted  |           3 | auto (a)
+(16 rows)
+
+SELECT seqrelid::regclass, seqtypid::regtype FROM pg_sequence;
+      seqrelid       | seqtypid 
+---------------------+----------
+ itest_parted_f3_seq | integer
+(1 row)
+
+INSERT into itest_p1 (f1, f2) VALUES ('2016-07-3', 'from itest_p1');
+INSERT INTO itest_p2 (f1, f2) VALUES ('2016-08-3', 'from itest_p2');
+INSERT into itest_parted(f1, f2) VALUES ('2016-07-4', 'from itest_parted');
+INSERT INTO itest_parted (f1, f2) VALUES ('2016-08-4', 'from itest_parted');
+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     |  3
+ itest_p1 | 07-04-2016 | from itest_parted |  5
+ itest_p2 | 08-02-2016 | from itest_parted |  2
+ itest_p2 | 08-03-2016 | from itest_p2     |  4
+ itest_p2 | 08-04-2016 | from itest_parted |  6
+(6 rows)
+
+-- extra tests - consider if we want to add it to the main test file
+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     |  3
+ itest_p1 | 07-04-2016 | from itest_parted |  5
+ itest_p2 | 08-02-2016 | from itest_parted |  2
+ itest_p2 | 08-03-2016 | from itest_p2     |  4
+ itest_p2 | 08-04-2016 | from itest_parted |  6
+ itest_p3 | 09-02-2016 | from itest_parted |  7
+ itest_p3 | 09-03-2016 | from itest_p3     |  9
+ itest_p4 | 10-02-2016 | from itest_parted |  8
+ itest_p4 | 10-03-2016 | from itest_p4     | 10
+(10 rows)
+
 DROP TABLE itest_parted;
 -- scenarios to test
--- adding an identity column to a partitioned table adds it to all the partitions, including the ones added after this operation - 7th Dec
 -- 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
diff --git a/src/test/regress/sql/identity_part_extra.sql b/src/test/regress/sql/identity_part_extra.sql
index 7925ce6205..33c5a30a94 100644
--- a/src/test/regress/sql/identity_part_extra.sql
+++ b/src/test/regress/sql/identity_part_extra.sql
@@ -27,6 +27,7 @@ SELECT classid, objid_name, objsubid, refclassid, refobjid_name, refobjsubid, de
                                                     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-2', 'from itest_parted');
 INSERT into itest_p1 (f1, f2) VALUES ('2016-07-3', 'from itest_p1');
 SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
@@ -57,15 +58,61 @@ SELECT classid, objid_name, objsubid, refclassid, refobjid_name, refobjsubid, de
                                                     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_p2 (f1, f2) VALUES ('2016-08-3', 'from itest_p2');
 INSERT INTO itest_parted (f1, f2) VALUES ('2016-08-4', 'from itest_parted');
 SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
 
 DROP TABLE itest_parted;
 
--- scenarios to test
+-- adding an identity column to a partitioned table adds it to all the
+-- partitions, including the ones added after this operation
+CREATE TABLE itest_parted (f1 date NOT NULL, f2 text) PARTITION BY RANGE (f1);
+CREATE TABLE itest_p1 PARTITION OF itest_parted FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');
+CREATE TABLE itest_p2 PARTITION OF itest_parted FOR VALUES FROM ('2016-08-01') TO ('2016-09-01');
+INSERT into itest_parted(f1, f2) VALUES ('2016-07-2', 'from itest_parted');
+INSERT INTO itest_parted (f1, f2) VALUES ('2016-08-2', 'from itest_parted');
+ALTER TABLE itest_parted ADD COLUMN f3 int generated always as identity;
+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_p1 (f1, f2) VALUES ('2016-07-3', 'from itest_p1');
+INSERT INTO itest_p2 (f1, f2) VALUES ('2016-08-3', 'from itest_p2');
+INSERT into itest_parted(f1, f2) VALUES ('2016-07-4', 'from itest_parted');
+INSERT INTO itest_parted (f1, f2) VALUES ('2016-08-4', 'from itest_parted');
+SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
+-- extra tests - consider if we want to add it to the main test file
+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;
 
--- adding an identity column to a partitioned table adds it to all the partitions, including the ones added after this operation - 7th Dec
+DROP TABLE itest_parted;
+
+-- scenarios to test
 
 -- changing a normal column to identity column is reflected in all the partitions - 8th Dec
 
-- 
2.25.1

