From c3123e148ff0fc6a3fb0499c60d18f93e65ee7b0 Mon Sep 17 00:00:00 2001 From: playrest Date: Wed, 21 Jun 2023 12:14:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dpg=5Fpartition=5Fsize()?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=AE=95=E6=9C=BA=E9=97=AE=E9=A2=98=E3=80=82?= =?UTF-8?q?=20=E5=BD=93=E5=8F=82=E6=95=B0=E6=8C=87=E5=AE=9A=E7=9A=84?= =?UTF-8?q?=E8=A1=A8=E4=B8=8D=E6=98=AF=E5=88=86=E5=8C=BA=E8=A1=A8=E6=88=96?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E7=9A=84=E5=88=86=E5=8C=BA=E4=B8=8D=E5=B1=9E?= =?UTF-8?q?=E4=BA=8E=E6=8C=87=E5=AE=9A=E5=88=86=E5=8C=BA=E8=A1=A8=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5=EF=BC=8C=E6=8A=A5=E9=94=99=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/backend/utils/adt/dbsize.cpp | 14 +++ .../storage/access/heap/heapam.cpp | 11 +++ .../regress/expected/hw_partition_size.out | 90 ++++++++++++++++--- .../regress/expected/hw_subpartition_size.out | 78 ++++++++++++++++ src/test/regress/parallel_schedule0 | 2 +- src/test/regress/sql/hw_partition_size.sql | 43 +++++++++ src/test/regress/sql/hw_subpartition_size.sql | 50 +++++++++++ 7 files changed, 275 insertions(+), 13 deletions(-) diff --git a/src/common/backend/utils/adt/dbsize.cpp b/src/common/backend/utils/adt/dbsize.cpp index b3c3f7e416..3fff6ee582 100644 --- a/src/common/backend/utils/adt/dbsize.cpp +++ b/src/common/backend/utils/adt/dbsize.cpp @@ -1508,6 +1508,13 @@ static int64 calculate_partition_size(Oid partTableOid, Oid partOid) return 0; } + if (!RelationIsPartitioned(partTableRel)) { + relation_close(partTableRel, AccessShareLock); + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_TABLE), + errmsg("relation %u is not a partitioned table", partTableOid))); + } + if (!RelationIsSubPartitioned(partTableRel)) { partition = partitionOpen(partTableRel, partOid, AccessShareLock); partRel = partitionGetRelation(partTableRel, partition); @@ -1629,6 +1636,13 @@ static int64 calculate_partition_indexes_size(Oid partTableOid, Oid partOid) return 0; } + if (!RelationIsPartitioned(partTableRel)) { + relation_close(partTableRel, AccessShareLock); + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_TABLE), + errmsg("relation %u is not a partitioned table", partTableOid))); + } + List *partOidList = NIL; if (!RelationIsSubPartitioned(partTableRel)) { partOidList = list_make1_oid(partOid); diff --git a/src/gausskernel/storage/access/heap/heapam.cpp b/src/gausskernel/storage/access/heap/heapam.cpp index a89b6168c9..a29a39f909 100755 --- a/src/gausskernel/storage/access/heap/heapam.cpp +++ b/src/gausskernel/storage/access/heap/heapam.cpp @@ -9790,12 +9790,19 @@ Partition partitionOpen(Relation relation, Oid partitionOid, LOCKMODE lockmode, errdetail("this partition may have already been dropped"))); } + Assert(RelationIsPartitioned(relation)); + /* subpartition table open subpartition, which is judged by partitionOpen() */ if (RelationIsSubPartitioned(relation) && relation->rd_id != parentOid) { /* partitionOid is subpartition oid */ p = SubPartitionOidGetPartition(relation, partitionOid, lockmode); Assert(relation->rd_id == partid_get_parentid(p->pd_part->parentid)); return p; } + if (relation->rd_id != parentOid) + ereport(ERROR, + (errcode(ERRCODE_PARTITION_ERROR), + errmsg("partition %u does not exist on relation \"%s\"", partitionOid, + RelationGetRelationName(relation)))); /* * If we are executing select for update/share operation, @@ -9933,6 +9940,8 @@ Partition tryPartitionOpen(Relation relation, Oid partition_id, LOCKMODE lockmod return NULL; } + Assert(RelationIsPartitioned(relation)); + /* subpartition table open subpartition, which is judged by tryPartitionOpen() */ if (RelationIsSubPartitioned(relation) && relation->rd_id != parentid) { if (!SearchSysCacheExists1(PARTRELID, ObjectIdGetDatum(partition_id))) { return NULL; @@ -9942,6 +9951,8 @@ Partition tryPartitionOpen(Relation relation, Oid partition_id, LOCKMODE lockmod Assert(relation->rd_id == partid_get_parentid(p->pd_part->parentid)); return p; } + if (relation->rd_id != parentid) + return NULL; /* * If we are executing select for update/share operation, diff --git a/src/test/regress/expected/hw_partition_size.out b/src/test/regress/expected/hw_partition_size.out index 07e0971e03..adedc20132 100644 --- a/src/test/regress/expected/hw_partition_size.out +++ b/src/test/regress/expected/hw_partition_size.out @@ -67,7 +67,6 @@ partition test_pg_index_size_ordinary_p2 values less than (4, 4) create index test_pg_index_size_ordinary_index_a on test_pg_index_size_ordinary (a) local; create index test_pg_index_size_ordinary_index_b on test_pg_index_size_ordinary (b) local; create index test_pg_index_size_ordinary_index_hash on test_pg_index_size_ordinary using hash (a) local; -ERROR: access method "hash" does not support row store insert into test_pg_index_size_ordinary values (1, 1), (3, 3); -- size: 294912 = 2*2*32768 + 2*81920 select pg_indexes_size('test_pg_index_size_ordinary') > 0; @@ -87,7 +86,6 @@ partition test_pg_index_size_index_toast_p2 values less than ('D', 'D') create index test_pg_index_size_index_toast_a on test_pg_index_size_index_toast (a) local; create index test_pg_index_size_index_toast_b on test_pg_index_size_index_toast (b) local; create index test_pg_index_size_index_toast_hash on test_pg_index_size_index_toast using hash (a) local; -ERROR: access method "hash" does not support row store insert into test_pg_index_size_index_toast values ('A', 'A'), ('C', 'C'); -- size: 196608 = 2*2*24576 + 2*32768 select pg_indexes_size('test_pg_index_size_index_toast') > 0; @@ -123,7 +121,6 @@ partition test_pg_total_relation_size_index_p2 values less than (4) ); create index test_pg_total_relation_size_index_a on test_pg_total_relation_size_index (a) local; create index test_pg_total_relation_size_index_hash on test_pg_total_relation_size_index using hash (a) local; -ERROR: access method "hash" does not support row store insert into test_pg_total_relation_size_index values (1), (3); -- size: 278528 = 2*24576 + 2*32768 + 2*81920 select pg_total_relation_size ('test_pg_total_relation_size_index') > 0; @@ -158,7 +155,6 @@ partition test_pg_total_relation_size_toast_index_p2 values less than ('D') ); create index test_pg_total_relation_size_toast_index_a on test_pg_total_relation_size_toast_index (a) local; create index test_pg_total_relation_size_toast_index_hash on test_pg_total_relation_size_toast_index using hash (a) local; -ERROR: access method "hash" does not support row store insert into test_pg_total_relation_size_toast_index values ('A'), ('C'); -- size: 262144 = 2*24576 + 2*24576 + 2*32768 + 2*32768 select pg_total_relation_size ('test_pg_total_relation_size_toast_index') > 0; @@ -194,7 +190,6 @@ partition test_pg_relation_size_index_p2 values less than (4) ); create index test_pg_relation_size_index_a on test_pg_relation_size_index (a) local; create index test_pg_relation_size_index_hash on test_pg_relation_size_index using hash (a) local; -ERROR: access method "hash" does not support row store insert into test_pg_relation_size_index values (1), (3); -- size: 65536 = 2*32768 select pg_relation_size ('test_pg_relation_size_index_a') > 0; @@ -205,9 +200,11 @@ select pg_relation_size ('test_pg_relation_size_index_a') > 0; -- size: 163840 = 2*81920 select pg_relation_size ('test_pg_relation_size_index_hash') > 0; -ERROR: relation "test_pg_relation_size_index_hash" does not exist -LINE 1: select pg_relation_size ('test_pg_relation_size_index_hash')... - ^ + ?column? +---------- + t +(1 row) + drop table test_pg_relation_size_index; -- c. test table has toast create table test_pg_relation_size_toast (a text) @@ -234,7 +231,6 @@ partition test_pg_relation_size_main_p1 values less than (4) ); create index test_pg_relation_size_main_a on test_pg_relation_size_main (a) local; create index test_pg_relation_size_main_hash on test_pg_relation_size_main using hash (a) local; -ERROR: access method "hash" does not support row store insert into test_pg_relation_size_main values (1), (3); -- size: 49152 = 2*24576 select pg_relation_size ('test_pg_relation_size_main', 'main') > 0; @@ -252,9 +248,11 @@ select pg_relation_size ('test_pg_relation_size_main_a', 'main') > 0; -- size: 163840 = 2*81920 select pg_relation_size ('test_pg_relation_size_main_hash', 'main') > 0; -ERROR: relation "test_pg_relation_size_main_hash" does not exist -LINE 1: select pg_relation_size ('test_pg_relation_size_main_hash', ... - ^ + ?column? +---------- + t +(1 row) + drop table test_pg_relation_size_main; -- 6. pg_partition_size create table test_pg_partition_size (a int) @@ -283,6 +281,29 @@ LINE 1: select pg_partition_size('test_pg_partition_size', 19000)>0; --ERROR select pg_partition_size('test_pg_partition_size', 'test_pg_partition_size_p2')>0; ERROR: partition "test_pg_partition_size_p2" does not exist +create table test_pg_partition_size2 (a int); +create table test_pg_partition_size3 (a int) +partition by range (a) +( + partition p1 values less than (4) +); +--ERROR regular table +select pg_partition_size('test_pg_partition_size2', 'test_pg_partition_size_p1')>0; +ERROR: partition "test_pg_partition_size_p1" does not exist +select pg_partition_size('test_pg_partition_size2'::regclass::oid, oid)>0 from pg_partition where parentid = 'test_pg_partition_size'::regclass and relname='test_pg_partition_size_p1'; +--?ERROR: relation .* is not a partitioned table +--ERROR partitioned table does not match the partition +select pg_partition_size('test_pg_partition_size3', 'test_pg_partition_size2')>0; +ERROR: partition "test_pg_partition_size2" does not exist +select pg_partition_size('test_pg_partition_size3', 'test_pg_partition_size_p1')>0; +ERROR: partition "test_pg_partition_size_p1" does not exist +select pg_partition_size('test_pg_partition_size3'::regclass::oid, 'test_pg_partition_size2'::regclass::oid)>0; +--?ERROR: partition .* does not exist on relation "test_pg_partition_size3" when find parent oid +DETAIL: this partition may have already been dropped +select pg_partition_size('test_pg_partition_size3'::regclass::oid, oid)>0 from pg_partition where parentid = 'test_pg_partition_size'::regclass and relname='test_pg_partition_size_p1'; +--?ERROR: partition .* does not exist on relation "test_pg_partition_size3" +drop table test_pg_partition_size3; +drop table test_pg_partition_size2; drop table test_pg_partition_size; -- 7. pg_partition_indexes_size create table test_pg_partition_indexes_size (a int) @@ -312,6 +333,51 @@ LINE 1: select pg_partition_indexes_size('test_pg_partition_indexes_... --ERROR select pg_partition_indexes_size('test_pg_partition_indexes_size', 'test_pg_partition_indexes_size_p2')>0; ERROR: partition "test_pg_partition_indexes_size_p2" does not exist +create table test_pg_partition_indexes_size2 (a int); +create table test_pg_partition_indexes_size3 (a int) +partition by range (a) +( + partition p1 values less than (4) +); +insert into test_pg_partition_indexes_size3 values (1); +-- no local partition index, return 0 +select pg_partition_indexes_size('test_pg_partition_indexes_size3', 'p1'); + pg_partition_indexes_size +--------------------------- + 0 +(1 row) + +select pg_partition_indexes_size('test_pg_partition_indexes_size3'::regclass::oid, 12345::oid); + pg_partition_indexes_size +--------------------------- + 0 +(1 row) + +create index test_pg_partition_indexes_size3_index on test_pg_partition_indexes_size3 (a) local; +select pg_partition_indexes_size('test_pg_partition_indexes_size3', 'p1')>0; + ?column? +---------- + t +(1 row) + +--ERROR regular table +select pg_partition_indexes_size('test_pg_partition_indexes_size2', 'test_pg_partition_indexes_size_p1')>0; +ERROR: partition "test_pg_partition_indexes_size_p1" does not exist +select pg_partition_indexes_size('test_pg_partition_indexes_size2'::regclass::oid, oid)>0 from pg_partition where parentid = 'test_pg_partition_indexes_size'::regclass and relname='test_pg_partition_indexes_size_p1'; +--?ERROR: relation .* is not a partitioned table +--ERROR partitioned table does not match the partition +select pg_partition_indexes_size('test_pg_partition_indexes_size3', 'test_pg_partition_indexes_size2')>0; +ERROR: partition "test_pg_partition_indexes_size2" does not exist +select pg_partition_indexes_size('test_pg_partition_indexes_size3', 'test_pg_partition_indexes_size_p1')>0; +ERROR: partition "test_pg_partition_indexes_size_p1" does not exist +select pg_partition_indexes_size('test_pg_partition_indexes_size3'::regclass::oid, 'test_pg_partition_indexes_size2'::regclass::oid)>0; +--?ERROR: The local index .* on the partition .* not exist. +HINT: In redistribution, local parititon index maybe not exists. +select pg_partition_indexes_size('test_pg_partition_indexes_size3'::regclass::oid, oid)>0 from pg_partition where parentid = 'test_pg_partition_indexes_size'::regclass and relname='test_pg_partition_indexes_size_p1'; +--?ERROR: The local index .* on the partition .* not exist. +HINT: In redistribution, local parititon index maybe not exists. +drop table test_pg_partition_indexes_size3; +drop table test_pg_partition_indexes_size2; drop table test_pg_partition_indexes_size; create table test_pg_table_size_toast_ord (a text); create table test_pg_table_size_toast_rt (a text) diff --git a/src/test/regress/expected/hw_subpartition_size.out b/src/test/regress/expected/hw_subpartition_size.out index 452fad5fb3..aa726afc5d 100644 --- a/src/test/regress/expected/hw_subpartition_size.out +++ b/src/test/regress/expected/hw_subpartition_size.out @@ -187,6 +187,15 @@ SELECT pg_partition_size('range_list_sales', 'customer1') = t (1 row) +-- (oid, oid) +SELECT (SELECT pg_partition_size('range_list_sales'::regclass::oid, oid) from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1') + = + (SELECT sum(pg_partition_size('range_list_sales'::regclass::oid, oid)) from pg_partition where parentid in (SELECT oid from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1')); + ?column? +---------- + t +(1 row) + -- should be equal SELECT pg_partition_size('range_list_sales', 'customer4') = pg_partition_size('range_list_sales', 'customer4_channel1'); ?column? @@ -198,6 +207,38 @@ SELECT pg_partition_size('range_list_sales', 'customer4') = pg_partition_size('r SELECT pg_partition_size('range_list_sales', 'parttemp'); ERROR: partition "parttemp" does not exist CONTEXT: referenced column: pg_partition_size +create table test_pg_partition_size2 (a int); +create table test_pg_partition_size3 (a int, b int) +partition by range (a) +subpartition by hash (b) +( + partition p1 values less than (4) + (subpartition sp1) +); +--ERROR regular table +select pg_partition_size('test_pg_partition_size2', 'customer1')>0; +ERROR: partition "customer1" does not exist +select pg_partition_size('test_pg_partition_size2', 'customer1_channel1')>0; +ERROR: partition "customer1_channel1" does not exist +select pg_partition_size('test_pg_partition_size2'::regclass::oid, oid)>0 from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1'; +--?ERROR: relation .* is not a partitioned table +select pg_partition_size('test_pg_partition_size2'::regclass::oid, oid)>0 from pg_partition where parentid = + (select oid from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1') and relname = 'customer1_channel1'; +--?ERROR: relation .* is not a partitioned table +--ERROR partitioned table does not match the partition +select pg_partition_size('test_pg_partition_size3', 'range_list_sales')>0; +ERROR: partition "range_list_sales" does not exist +select pg_partition_size('test_pg_partition_size3', 'customer1')>0; +ERROR: partition "customer1" does not exist +select pg_partition_size('test_pg_partition_size3', 'customer1_channel1')>0; +ERROR: partition "customer1_channel1" does not exist +select pg_partition_size('test_pg_partition_size3'::regclass::oid, 'range_list_sales'::regclass::oid)>0; +--?ERROR: .* is not a partition/subpartition of subpartitioned table .* +select pg_partition_size('test_pg_partition_size3'::regclass::oid, oid)>0 from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1'; +--?ERROR: .* is not a partition/subpartition of subpartitioned table .* +select pg_partition_size('test_pg_partition_size3'::regclass::oid, oid)>0 from pg_partition where parentid = + (select oid from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1') and relname = 'customer1_channel1'; +--?ERROR: .* is not a partition/subpartition of subpartitioned table .* -- 6. pg_partition_indexes_size SELECT pg_partition_indexes_size('range_list_sales', 'customer1'); pg_partition_indexes_size @@ -234,6 +275,15 @@ SELECT pg_partition_indexes_size('range_list_sales', 'customer1') = t (1 row) +-- (oid, oid) +SELECT (SELECT pg_partition_indexes_size('range_list_sales'::regclass::oid, oid) from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1') + = + (SELECT sum(pg_partition_indexes_size('range_list_sales'::regclass::oid, oid)) from pg_partition where parentid in (SELECT oid from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1')); + ?column? +---------- + t +(1 row) + -- should be equal SELECT pg_partition_indexes_size('range_list_sales', 'customer4') = pg_partition_indexes_size('range_list_sales', 'customer4_channel1'); ?column? @@ -241,7 +291,35 @@ SELECT pg_partition_indexes_size('range_list_sales', 'customer4') = pg_partition t (1 row) +CREATE INDEX test_pg_partition_size2_idx ON test_pg_partition_size2(a); +CREATE INDEX test_pg_partition_size3_idx ON test_pg_partition_size3(a) LOCAL; +--ERROR regular table +select pg_partition_indexes_size('test_pg_partition_size2', 'customer1')>0; +ERROR: partition "customer1" does not exist +select pg_partition_indexes_size('test_pg_partition_size2', 'customer1_channel1')>0; +ERROR: partition "customer1_channel1" does not exist +select pg_partition_indexes_size('test_pg_partition_size2'::regclass::oid, oid)>0 from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1'; +--?ERROR: relation .* is not a partitioned table +select pg_partition_indexes_size('test_pg_partition_size2'::regclass::oid, oid)>0 from pg_partition where parentid = + (select oid from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1') and relname = 'customer1_channel1'; +--?ERROR: relation .* is not a partitioned table +--ERROR partitioned table does not match the partition +select pg_partition_indexes_size('test_pg_partition_size3', 'range_list_sales')>0; +ERROR: partition "range_list_sales" does not exist +select pg_partition_indexes_size('test_pg_partition_size3', 'customer1')>0; +ERROR: partition "customer1" does not exist +select pg_partition_indexes_size('test_pg_partition_size3', 'customer1_channel1')>0; +ERROR: partition "customer1_channel1" does not exist +select pg_partition_indexes_size('test_pg_partition_size3'::regclass::oid, 'range_list_sales'::regclass::oid)>0; +--?ERROR: .* is not a partition/subpartition of subpartitioned table .* +select pg_partition_indexes_size('test_pg_partition_size3'::regclass::oid, oid)>0 from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1'; +--?ERROR: .* is not a partition/subpartition of subpartitioned table .* +select pg_partition_indexes_size('test_pg_partition_size3'::regclass::oid, oid)>0 from pg_partition where parentid = + (select oid from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1') and relname = 'customer1_channel1'; +--?ERROR: .* is not a partition/subpartition of subpartitioned table .* -- finish, clean +DROP TABLE test_pg_partition_size2; +DROP TABLE test_pg_partition_size3; DROP TABLE range_list_sales; DROP SCHEMA hw_subpartition_size CASCADE; RESET CURRENT_SCHEMA; diff --git a/src/test/regress/parallel_schedule0 b/src/test/regress/parallel_schedule0 index c5efdcb4e1..0b2f78d475 100644 --- a/src/test/regress/parallel_schedule0 +++ b/src/test/regress/parallel_schedule0 @@ -90,7 +90,7 @@ test: single_node_sha test: test_b_format_collate charset_b_format # test subpartition -test: hw_subpartition_createtable hw_subpartition_scan hw_subpartition_select hw_subpartition_split hw_subpartition_truncate hw_subpartition_update hw_subpartition_gpi hw_subpartition_analyze_vacuum hw_subpartition_alter_table hw_subpartition_index hw_subpartition_add_drop_partition hw_subpartition_tablespace hw_subpartition_ddl_index hw_subpartition_size +test: hw_subpartition_createtable hw_subpartition_scan hw_subpartition_select hw_subpartition_split hw_subpartition_truncate hw_subpartition_update hw_subpartition_gpi hw_subpartition_analyze_vacuum hw_subpartition_alter_table hw_subpartition_index hw_subpartition_add_drop_partition hw_subpartition_tablespace hw_subpartition_ddl_index hw_subpartition_size hw_partition_size hw_cstore_partition_size test: hw_subpartition_vacuum_partition hw_subpartition_tablespace_global test: gs_dump_subpartition test: partition_dml_operations partition_minmax partition_pruning hw_partitionno hw_partition_parallel diff --git a/src/test/regress/sql/hw_partition_size.sql b/src/test/regress/sql/hw_partition_size.sql index a0930dfdbe..17626a5ab8 100644 --- a/src/test/regress/sql/hw_partition_size.sql +++ b/src/test/regress/sql/hw_partition_size.sql @@ -275,6 +275,23 @@ select pg_partition_size('test_pg_partition_size', 19000)>0; --ERROR select pg_partition_size('test_pg_partition_size', 'test_pg_partition_size_p2')>0; +create table test_pg_partition_size2 (a int); +create table test_pg_partition_size3 (a int) +partition by range (a) +( + partition p1 values less than (4) +); +--ERROR regular table +select pg_partition_size('test_pg_partition_size2', 'test_pg_partition_size_p1')>0; +select pg_partition_size('test_pg_partition_size2'::regclass::oid, oid)>0 from pg_partition where parentid = 'test_pg_partition_size'::regclass and relname='test_pg_partition_size_p1'; +--ERROR partitioned table does not match the partition +select pg_partition_size('test_pg_partition_size3', 'test_pg_partition_size2')>0; +select pg_partition_size('test_pg_partition_size3', 'test_pg_partition_size_p1')>0; +select pg_partition_size('test_pg_partition_size3'::regclass::oid, 'test_pg_partition_size2'::regclass::oid)>0; +select pg_partition_size('test_pg_partition_size3'::regclass::oid, oid)>0 from pg_partition where parentid = 'test_pg_partition_size'::regclass and relname='test_pg_partition_size_p1'; + +drop table test_pg_partition_size3; +drop table test_pg_partition_size2; drop table test_pg_partition_size; @@ -296,6 +313,32 @@ select pg_partition_indexes_size('test_pg_partition_indexes_size', 19000)>0; --ERROR select pg_partition_indexes_size('test_pg_partition_indexes_size', 'test_pg_partition_indexes_size_p2')>0; +create table test_pg_partition_indexes_size2 (a int); +create table test_pg_partition_indexes_size3 (a int) +partition by range (a) +( + partition p1 values less than (4) +); +insert into test_pg_partition_indexes_size3 values (1); + +-- no local partition index, return 0 +select pg_partition_indexes_size('test_pg_partition_indexes_size3', 'p1'); +select pg_partition_indexes_size('test_pg_partition_indexes_size3'::regclass::oid, 12345::oid); + +create index test_pg_partition_indexes_size3_index on test_pg_partition_indexes_size3 (a) local; +select pg_partition_indexes_size('test_pg_partition_indexes_size3', 'p1')>0; + +--ERROR regular table +select pg_partition_indexes_size('test_pg_partition_indexes_size2', 'test_pg_partition_indexes_size_p1')>0; +select pg_partition_indexes_size('test_pg_partition_indexes_size2'::regclass::oid, oid)>0 from pg_partition where parentid = 'test_pg_partition_indexes_size'::regclass and relname='test_pg_partition_indexes_size_p1'; +--ERROR partitioned table does not match the partition +select pg_partition_indexes_size('test_pg_partition_indexes_size3', 'test_pg_partition_indexes_size2')>0; +select pg_partition_indexes_size('test_pg_partition_indexes_size3', 'test_pg_partition_indexes_size_p1')>0; +select pg_partition_indexes_size('test_pg_partition_indexes_size3'::regclass::oid, 'test_pg_partition_indexes_size2'::regclass::oid)>0; +select pg_partition_indexes_size('test_pg_partition_indexes_size3'::regclass::oid, oid)>0 from pg_partition where parentid = 'test_pg_partition_indexes_size'::regclass and relname='test_pg_partition_indexes_size_p1'; + +drop table test_pg_partition_indexes_size3; +drop table test_pg_partition_indexes_size2; drop table test_pg_partition_indexes_size; diff --git a/src/test/regress/sql/hw_subpartition_size.sql b/src/test/regress/sql/hw_subpartition_size.sql index 50ca53113e..6e4e18e394 100644 --- a/src/test/regress/sql/hw_subpartition_size.sql +++ b/src/test/regress/sql/hw_subpartition_size.sql @@ -100,12 +100,39 @@ SELECT pg_partition_size('range_list_sales', 'customer1') = pg_partition_size('range_list_sales', 'customer1_channel2') + pg_partition_size('range_list_sales', 'customer1_channel3') + pg_partition_size('range_list_sales', 'customer1_channel4'); +-- (oid, oid) +SELECT (SELECT pg_partition_size('range_list_sales'::regclass::oid, oid) from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1') + = + (SELECT sum(pg_partition_size('range_list_sales'::regclass::oid, oid)) from pg_partition where parentid in (SELECT oid from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1')); -- should be equal SELECT pg_partition_size('range_list_sales', 'customer4') = pg_partition_size('range_list_sales', 'customer4_channel1'); -- invalid parameter, error SELECT pg_partition_size('range_list_sales', 'parttemp'); +create table test_pg_partition_size2 (a int); +create table test_pg_partition_size3 (a int, b int) +partition by range (a) +subpartition by hash (b) +( + partition p1 values less than (4) + (subpartition sp1) +); +--ERROR regular table +select pg_partition_size('test_pg_partition_size2', 'customer1')>0; +select pg_partition_size('test_pg_partition_size2', 'customer1_channel1')>0; +select pg_partition_size('test_pg_partition_size2'::regclass::oid, oid)>0 from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1'; +select pg_partition_size('test_pg_partition_size2'::regclass::oid, oid)>0 from pg_partition where parentid = + (select oid from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1') and relname = 'customer1_channel1'; +--ERROR partitioned table does not match the partition +select pg_partition_size('test_pg_partition_size3', 'range_list_sales')>0; +select pg_partition_size('test_pg_partition_size3', 'customer1')>0; +select pg_partition_size('test_pg_partition_size3', 'customer1_channel1')>0; +select pg_partition_size('test_pg_partition_size3'::regclass::oid, 'range_list_sales'::regclass::oid)>0; +select pg_partition_size('test_pg_partition_size3'::regclass::oid, oid)>0 from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1'; +select pg_partition_size('test_pg_partition_size3'::regclass::oid, oid)>0 from pg_partition where parentid = + (select oid from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1') and relname = 'customer1_channel1'; + -- 6. pg_partition_indexes_size SELECT pg_partition_indexes_size('range_list_sales', 'customer1'); SELECT pg_partition_indexes_size('range_list_sales', 'customer2_channel1'); @@ -122,10 +149,33 @@ SELECT pg_partition_indexes_size('range_list_sales', 'customer1') = pg_partition_indexes_size('range_list_sales', 'customer1_channel2') + pg_partition_indexes_size('range_list_sales', 'customer1_channel3') + pg_partition_indexes_size('range_list_sales', 'customer1_channel4'); +-- (oid, oid) +SELECT (SELECT pg_partition_indexes_size('range_list_sales'::regclass::oid, oid) from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1') + = + (SELECT sum(pg_partition_indexes_size('range_list_sales'::regclass::oid, oid)) from pg_partition where parentid in (SELECT oid from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1')); -- should be equal SELECT pg_partition_indexes_size('range_list_sales', 'customer4') = pg_partition_indexes_size('range_list_sales', 'customer4_channel1'); +CREATE INDEX test_pg_partition_size2_idx ON test_pg_partition_size2(a); +CREATE INDEX test_pg_partition_size3_idx ON test_pg_partition_size3(a) LOCAL; +--ERROR regular table +select pg_partition_indexes_size('test_pg_partition_size2', 'customer1')>0; +select pg_partition_indexes_size('test_pg_partition_size2', 'customer1_channel1')>0; +select pg_partition_indexes_size('test_pg_partition_size2'::regclass::oid, oid)>0 from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1'; +select pg_partition_indexes_size('test_pg_partition_size2'::regclass::oid, oid)>0 from pg_partition where parentid = + (select oid from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1') and relname = 'customer1_channel1'; +--ERROR partitioned table does not match the partition +select pg_partition_indexes_size('test_pg_partition_size3', 'range_list_sales')>0; +select pg_partition_indexes_size('test_pg_partition_size3', 'customer1')>0; +select pg_partition_indexes_size('test_pg_partition_size3', 'customer1_channel1')>0; +select pg_partition_indexes_size('test_pg_partition_size3'::regclass::oid, 'range_list_sales'::regclass::oid)>0; +select pg_partition_indexes_size('test_pg_partition_size3'::regclass::oid, oid)>0 from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1'; +select pg_partition_indexes_size('test_pg_partition_size3'::regclass::oid, oid)>0 from pg_partition where parentid = + (select oid from pg_partition where parentid = 'range_list_sales'::regclass and relname='customer1') and relname = 'customer1_channel1'; + -- finish, clean +DROP TABLE test_pg_partition_size2; +DROP TABLE test_pg_partition_size3; DROP TABLE range_list_sales; DROP SCHEMA hw_subpartition_size CASCADE; RESET CURRENT_SCHEMA; -- Gitee