From f909361f703dc9ad12644f601633243d835a9993 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Date: Mon, 4 Dec 2023 15:43:58 +0530
Subject: [PATCH 03/14] Identity column support in partitioned tables extra
 tests

These tests are more like white box tests. Not to be included in the final set of patches.

Ashutosh Bapat
---
 .../regress/expected/identity_part_extra.out  | 133 ++++++++++++++++++
 src/test/regress/sql/identity_part_extra.sql  |  70 +++++++++
 2 files changed, 203 insertions(+)
 create mode 100644 src/test/regress/expected/identity_part_extra.out
 create mode 100644 src/test/regress/sql/identity_part_extra.sql

diff --git a/src/test/regress/expected/identity_part_extra.out b/src/test/regress/expected/identity_part_extra.out
new file mode 100644
index 0000000000..0c8af1c658
--- /dev/null
+++ b/src/test/regress/expected/identity_part_extra.out
@@ -0,0 +1,133 @@
+-- sanity check of system catalog
+SELECT attrelid, attname, attidentity FROM pg_attribute WHERE attidentity NOT IN ('', 'a', 'd');
+ attrelid | attname | attidentity 
+----------+---------+-------------
+(0 rows)
+
+\i /home/ashutosh/scripts/postgresql/readable_pg_catalog.sql
+create function object_name(classid oid, objid oid)
+returns name
+language plpgsql
+as $$
+declare
+	object_name name;
+begin
+	case classid
+		when 'pg_catalog.pg_class'::regclass then
+			object_name := objid::regclass;
+		when 'pg_catalog.pg_constraint'::regclass then
+			select connamespace::regnamespace || '.' || conname
+				into object_name
+				from pg_constraint where oid = objid;
+		when 'pg_catalog.pg_type'::regclass then
+			object_name := objid::regtype;
+		else
+			object_name := objid::text::name;
+	end case;
+	return object_name;
+end;
+$$;
+create view v_pg_depend as
+	select classid::regclass,
+			object_name(classid, objid) objid_name,
+			objid,
+			objsubid,
+			refclassid::regclass,
+			object_name(refclassid, refobjid) refobjid_name,
+			refobjid,
+			refobjsubid,
+			(case deptype when 'i' then 'internal'
+							when 'a' then 'auto'
+							when 'n' then 'normal'
+							when 'P' then 'partition primary'
+							when 'S' then 'partition secondary'
+							when 'e' then 'extension'
+							when 'x' then 'auto extension'
+			 	end) || ' (' || deptype::text || ')' as deptype
+		from pg_depend;
+-- table partitions
+CREATE TABLE itest_parted (f1 date NOT NULL, f2 text, f3 bigint generated always as identity) PARTITION BY RANGE (f1);
+-- partition inherits identity and shares the sequence
+CREATE TABLE itest_p1 PARTITION OF itest_parted FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');
+SELECT attrelid::regclass, attname, attidentity, atthasdef, attgenerated, attnotnull
+    FROM pg_attribute
+    WHERE attrelid IN ('itest_parted'::regclass, 'itest_p1'::regclass)
+        AND attnum > 0;
+   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 ('itest_parted'::regclass, 'itest_p1'::regclass);
+   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 *
+    FROM v_pg_depend
+    WHERE refobjid in ('itest_parted'::regclass, 'itest_p1'::regclass)
+            OR objid in ('itest_parted'::regclass, 'itest_p1'::regclass);
+    classid    |           objid_name            | objid | objsubid |  refclassid  | refobjid_name | refobjid | refobjsubid |   deptype    
+---------------+---------------------------------+-------+----------+--------------+---------------+----------+-------------+--------------
+ pg_type       | itest_parted                    | 16619 |        0 | pg_class     | itest_parted  |    16617 |           0 | internal (i)
+ pg_class      | itest_parted                    | 16617 |        0 | pg_namespace | 2200          |     2200 |           0 | normal (n)
+ pg_class      | itest_parted                    | 16617 |        1 | pg_class     | itest_parted  |    16617 |           0 | internal (i)
+ pg_constraint | public.itest_parted_f1_not_null | 16620 |        0 | pg_class     | itest_parted  |    16617 |           1 | auto (a)
+ pg_constraint | public.itest_parted_f3_not_null | 16621 |        0 | pg_class     | itest_parted  |    16617 |           3 | auto (a)
+ pg_class      | itest_parted_f3_seq             | 16616 |        0 | pg_class     | itest_parted  |    16617 |           3 | internal (i)
+ pg_type       | itest_p1                        | 16624 |        0 | pg_class     | itest_p1      |    16622 |           0 | internal (i)
+ pg_class      | itest_p1                        | 16622 |        0 | pg_namespace | 2200          |     2200 |           0 | normal (n)
+ pg_class      | itest_p1                        | 16622 |        0 | pg_class     | itest_parted  |    16617 |           0 | auto (a)
+ pg_constraint | public.itest_parted_f1_not_null | 16625 |        0 | pg_class     | itest_p1      |    16622 |           1 | auto (a)
+ pg_constraint | public.itest_parted_f3_not_null | 16626 |        0 | pg_class     | itest_p1      |    16622 |           3 | auto (a)
+ pg_class      | pg_toast.pg_toast_16622         | 16627 |        0 | pg_class     | itest_p1      |    16622 |           0 | internal (i)
+(12 rows)
+
+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;
+ tableoid |     f1     |        f2         | f3 
+----------+------------+-------------------+----
+ itest_p1 | 07-02-2016 | from itest_parted |  1
+ itest_p1 | 07-03-2016 | from itest_p1     |  2
+(2 rows)
+
+DROP TABLE itest_parted;
+-- scenarios to test
+-- changing NOT NULL attribute of inherited identity column of partition is not allowed
+-- trying to change default value of inherited identity column of partition is not allowed
+-- trying to add identity to an inherited identity column of partition is not allowed
+-- trying to drop inherited identity column of partition is not allowed
+-- trying to change the identity properties of partition? Is that allowed?
+-- TODO: test OVERRIDING SYSTEM VALUE OR OVERRIDING USER VALUE
+-- attaching table with identity column is not allowed
+-- attaching a table as partition inherits identity property
+-- detaching a partition removes identity property
+-- attaching a table with identity column is not allowed even when the parent table does not have an identity column
+-- dropping an identity column of parent drops it from all the partitions
+-- TODO: do we need a test for this?
+-- adding an identity column to a partitioned table adds it to all the partitions, including the ones added after this operation
+-- changing a normal column to identity column is reflected in all the partitions
+-- 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 test
+-- \d tests
+-- creating a child table with identity column is not allowed when the parent does not have it
+CREATE TABLE itest_parent (f1 date NOT NULL, f2 text, f3 bigint) PARTITION BY RANGE (f1);
+CREATE TABLE itest_child PARTITION OF itest_parent(
+    f3 WITH OPTIONS GENERATED ALWAYS AS IDENTITY
+) FOR VALUES FROM ('2016-07-01') TO ('2016-08-01'); -- error
+ERROR:  identity columns are not supported on partitions
+DROP TABLE itest_parent;
diff --git a/src/test/regress/sql/identity_part_extra.sql b/src/test/regress/sql/identity_part_extra.sql
new file mode 100644
index 0000000000..1ab883e5e1
--- /dev/null
+++ b/src/test/regress/sql/identity_part_extra.sql
@@ -0,0 +1,70 @@
+-- sanity check of system catalog
+SELECT attrelid, attname, attidentity FROM pg_attribute WHERE attidentity NOT IN ('', 'a', 'd');
+
+\i /home/ashutosh/scripts/postgresql/readable_pg_catalog.sql
+
+-- table partitions
+CREATE TABLE itest_parted (f1 date NOT NULL, f2 text, f3 bigint generated always as identity) PARTITION BY RANGE (f1);
+-- partition inherits identity and shares the sequence
+CREATE TABLE itest_p1 PARTITION OF itest_parted FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');
+SELECT attrelid::regclass, attname, attidentity, atthasdef, attgenerated, attnotnull
+    FROM pg_attribute
+    WHERE attrelid IN ('itest_parted'::regclass, 'itest_p1'::regclass)
+        AND attnum > 0;
+SELECT conrelid::regclass, conname, connoinherit, conislocal, coninhcount
+    FROM pg_constraint
+    WHERE conrelid in ('itest_parted'::regclass, 'itest_p1'::regclass);
+SELECT *
+    FROM v_pg_depend
+    WHERE refobjid in ('itest_parted'::regclass, 'itest_p1'::regclass)
+            OR objid in ('itest_parted'::regclass, 'itest_p1'::regclass);
+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;
+DROP TABLE itest_parted;
+
+-- scenarios to test
+
+-- changing NOT NULL attribute of inherited identity column of partition is not allowed
+
+-- trying to change default value of inherited identity column of partition is not allowed
+
+-- trying to add identity to an inherited identity column of partition is not allowed
+
+-- trying to drop inherited identity column of partition is not allowed
+
+-- trying to change the identity properties of partition? Is that allowed?
+
+-- TODO: test OVERRIDING SYSTEM VALUE OR OVERRIDING USER VALUE
+
+-- attaching table with identity column is not allowed
+
+-- attaching a table as partition inherits identity property
+
+-- detaching a partition removes identity property
+
+-- attaching a table with identity column is not allowed even when the parent table does not have an identity column
+
+-- dropping an identity column of parent drops it from all the partitions
+-- TODO: do we need a test for this?
+
+-- adding an identity column to a partitioned table adds it to all the partitions, including the ones added after this operation
+
+-- changing a normal column to identity column is reflected in all the partitions
+
+-- 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 test
+
+-- \d tests
+
+-- creating a child table with identity column is not allowed when the parent does not have it
+CREATE TABLE itest_parent (f1 date NOT NULL, f2 text, f3 bigint) PARTITION BY RANGE (f1);
+CREATE TABLE itest_child PARTITION OF itest_parent(
+    f3 WITH OPTIONS GENERATED ALWAYS AS IDENTITY
+) FOR VALUES FROM ('2016-07-01') TO ('2016-08-01'); -- error
+DROP TABLE itest_parent;
\ No newline at end of file
-- 
2.25.1

