From 5a291187eec6ec066dc080ae979dd916d8395c73 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Date: Tue, 19 Dec 2023 11:33:21 +0530
Subject: [PATCH 14/14] Extra tests for Detach partition

Not for final commit
---
 .../regress/expected/identity_part_extra.out  | 96 ++++++++++++++++++-
 src/test/regress/sql/identity_part_extra.sql  | 44 ++++++++-
 2 files changed, 134 insertions(+), 6 deletions(-)

diff --git a/src/test/regress/expected/identity_part_extra.out b/src/test/regress/expected/identity_part_extra.out
index d4174fd8c0..cbff2cf137 100644
--- a/src/test/regress/expected/identity_part_extra.out
+++ b/src/test/regress/expected/identity_part_extra.out
@@ -509,8 +509,102 @@ SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
 (4 rows)
 
 DROP TABLE itest_parted;
+-- detaching a partition removes identity property
+CREATE TABLE itest_parted (f1 date NOT NULL, f2 text, f3 int generated always as identity) 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 VALUES ('2016-07-2', 'from itest_parted');
+INSERT into itest_parted VALUES ('2016-08-2', 'from itest_parted');
+ALTER TABLE itest_parted DETACH PARTITION itest_p1;
+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      |             | 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            | t          |           0
+ itest_p1     | itest_parted_f3_not_null | f            | t          |           0
+ 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_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)
+(15 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-08-4', 'from itest_parted');
+INSERT into itest_p1 (f1, f2) VALUES ('2016-07-5', 'from itest_p1'); -- error
+ERROR:  null value in column "f3" of relation "itest_p1" violates not-null constraint
+DETAIL:  Failing row contains (07-05-2016, from itest_p1, null).
+INSERT into itest_p1 (f1, f2, f3) VALUES ('2016-07-5', 'from itest_p1', 100);
+SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
+ tableoid |     f1     |        f2         | f3 
+----------+------------+-------------------+----
+ itest_p2 | 08-02-2016 | from itest_parted |  2
+ itest_p2 | 08-04-2016 | from itest_parted |  3
+(2 rows)
+
+SELECT tableoid::regclass, f1, f2, f3 FROM itest_p1;
+ tableoid |     f1     |        f2         | f3  
+----------+------------+-------------------+-----
+ itest_p1 | 07-02-2016 | from itest_parted |   1
+ itest_p1 | 07-05-2016 | from itest_p1     | 100
+(2 rows)
+
+DROP TABLE itest_parted;
+DROP TABLE itest_p1;
 -- scenarios to test
--- 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
diff --git a/src/test/regress/sql/identity_part_extra.sql b/src/test/regress/sql/identity_part_extra.sql
index 7e7b1f10ca..b151bfb9ab 100644
--- a/src/test/regress/sql/identity_part_extra.sql
+++ b/src/test/regress/sql/identity_part_extra.sql
@@ -195,15 +195,49 @@ SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
 
 DROP TABLE itest_parted;
 
--- scenarios to test
+-- detaching a partition removes identity property
+CREATE TABLE itest_parted (f1 date NOT NULL, f2 text, f3 int generated always as identity) 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 VALUES ('2016-07-2', 'from itest_parted');
+INSERT into itest_parted VALUES ('2016-08-2', 'from itest_parted');
+ALTER TABLE itest_parted DETACH PARTITION itest_p1;
+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-08-4', 'from itest_parted');
+INSERT into itest_p1 (f1, f2) VALUES ('2016-07-5', 'from itest_p1'); -- error
+INSERT into itest_p1 (f1, f2, f3) VALUES ('2016-07-5', 'from itest_p1', 100);
+SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
+SELECT tableoid::regclass, f1, f2, f3 FROM itest_p1;
 
+DROP TABLE itest_parted;
+DROP TABLE itest_p1;
+
+-- scenarios to test
 
--- 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)
 
--- 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
 
--- 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
+-- trying to change the identity properties of partition? Is that allowed?
 
 -- changing NOT NULL attribute of inherited identity column of partition is not allowed
 
-- 
2.25.1

