From 71be0358d120288cdcb19592d664c959c18fdbd3 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Date: Mon, 18 Dec 2023 16:27:52 +0530
Subject: [PATCH 12/14] Extra tests for drop idenity

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

diff --git a/src/test/regress/expected/identity_part_extra.out b/src/test/regress/expected/identity_part_extra.out
index 05de0cabab..d4174fd8c0 100644
--- a/src/test/regress/expected/identity_part_extra.out
+++ b/src/test/regress/expected/identity_part_extra.out
@@ -426,6 +426,88 @@ SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
  itest_p4 | 10-03-2016 | from itest_p4     |  8
 (8 rows)
 
+DROP TABLE itest_parted;
+-- changing an identity column to a non-identity column in a partitioned table
+CREATE TABLE itest_parted (f1 date NOT NULL, f2 text, f3 bigint 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');
+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');
+ALTER TABLE ONLY itest_parted ALTER COLUMN f3 DROP IDENTITY; -- fails
+ERROR:  identity can only be dropped from column "f3" across all the partitions of the relation "itest_parted"
+ALTER TABLE itest_parted ALTER COLUMN f3 DROP 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      |             | f         |              | t
+ itest_p1     | f1      |             | f         |              | t
+ itest_p1     | f2      |             | f         |              | f
+ itest_p1     | f3      |             | 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_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)
+(10 rows)
+
+SELECT seqrelid::regclass, seqtypid::regtype FROM pg_sequence;
+ seqrelid | seqtypid 
+----------+----------
+(0 rows)
+
+INSERT into itest_parted(f1, f2) VALUES ('2016-07-4', 'from itest_parted'); -- fails
+ERROR:  null value in column "f3" of relation "itest_p1" violates not-null constraint
+DETAIL:  Failing row contains (07-04-2016, from itest_parted, null).
+INSERT into itest_p1 (f1, f2) VALUES ('2016-07-5', 'from itest_p1'); -- fails
+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_parted(f1, f2, f3) VALUES ('2016-07-4', 'from itest_parted', 3);
+INSERT into itest_p1 (f1, f2, f3) VALUES ('2016-07-5', 'from itest_p1', 4);
+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)
+
 DROP TABLE itest_parted;
 -- scenarios to test
 -- detaching a partition removes identity property - 12th Dec
@@ -438,7 +520,6 @@ DROP TABLE itest_parted;
 -- TODO: test OVERRIDING SYSTEM VALUE OR OVERRIDING USER VALUE
 -- dropping an identity column of parent drops it from all the partitions
 -- TODO: do we need a test for this?
--- TODO: can we change an identity column to a normal column?
 -- Dump and restore tests - that would be a separate file
 -- pg_upgrade tests
 -- multi-level partition tests
diff --git a/src/test/regress/sql/identity_part_extra.sql b/src/test/regress/sql/identity_part_extra.sql
index dc8ba47617..7e7b1f10ca 100644
--- a/src/test/regress/sql/identity_part_extra.sql
+++ b/src/test/regress/sql/identity_part_extra.sql
@@ -160,6 +160,41 @@ SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
 
 DROP TABLE itest_parted;
 
+-- changing an identity column to a non-identity column in a partitioned table
+CREATE TABLE itest_parted (f1 date NOT NULL, f2 text, f3 bigint 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');
+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');
+ALTER TABLE ONLY itest_parted ALTER COLUMN f3 DROP IDENTITY; -- fails
+ALTER TABLE itest_parted ALTER COLUMN f3 DROP 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_parted(f1, f2) VALUES ('2016-07-4', 'from itest_parted'); -- fails
+INSERT into itest_p1 (f1, f2) VALUES ('2016-07-5', 'from itest_p1'); -- fails
+INSERT into itest_parted(f1, f2, f3) VALUES ('2016-07-4', 'from itest_parted', 3);
+INSERT into itest_p1 (f1, f2, f3) VALUES ('2016-07-5', 'from itest_p1', 4);
+SELECT tableoid::regclass, f1, f2, f3 FROM itest_parted;
+
+DROP TABLE itest_parted;
+
 -- scenarios to test
 
 
@@ -182,8 +217,6 @@ DROP TABLE itest_parted;
 -- dropping an identity column of parent drops it from all the partitions
 -- TODO: do we need a test for this?
 
--- TODO: can we change an identity column to a normal column?
-
 -- Dump and restore tests - that would be a separate file
 
 -- pg_upgrade tests
-- 
2.25.1

