From 0b579d027ab946344d8e392a18fd64778c1a69f0 Mon Sep 17 00:00:00 2001 From: Marco Slot Date: Sat, 29 Apr 2017 01:39:39 +0200 Subject: [PATCH] Check whether relation ID exists in citus_relation_size --- .../distributed/master/master_metadata_utility.c | 5 +++++ src/test/regress/expected/multi_size_queries.out | 16 ++++++++++++++++ src/test/regress/sql/multi_size_queries.sql | 12 ++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/backend/distributed/master/master_metadata_utility.c b/src/backend/distributed/master/master_metadata_utility.c index 10997e7ef..e6bfa574b 100644 --- a/src/backend/distributed/master/master_metadata_utility.c +++ b/src/backend/distributed/master/master_metadata_utility.c @@ -134,6 +134,7 @@ citus_relation_size(PG_FUNCTION_ARGS) static uint64 DistributedTableSize(Oid relationId, char *sizeQuery) { + Relation relation = NULL; Relation pgDistNode = NULL; List *workerNodeList = NULL; ListCell *workerNodeCell = NULL; @@ -146,6 +147,9 @@ DistributedTableSize(Oid relationId, char *sizeQuery) " blocks which contain multi-shard data modifications"))); } + /* try to open relation, will error out if the relation does not exist */ + relation = relation_open(relationId, AccessShareLock); + ErrorIfNotSuitableToGetSize(relationId); pgDistNode = heap_open(DistNodeRelationId(), AccessShareLock); @@ -161,6 +165,7 @@ DistributedTableSize(Oid relationId, char *sizeQuery) } heap_close(pgDistNode, NoLock); + heap_close(relation, AccessShareLock); return totalRelationSize; } diff --git a/src/test/regress/expected/multi_size_queries.out b/src/test/regress/expected/multi_size_queries.out index aae4ea52b..758819434 100644 --- a/src/test/regress/expected/multi_size_queries.out +++ b/src/test/regress/expected/multi_size_queries.out @@ -6,6 +6,22 @@ -- citus_total_relation_size are also tested. ALTER SEQUENCE pg_catalog.pg_dist_shardid_seq RESTART 1390000; ALTER SEQUENCE pg_catalog.pg_dist_jobid_seq RESTART 1390000; +-- Tests with invalid relation IDs +SELECT citus_table_size(1); +ERROR: could not open relation with OID 1 +SELECT citus_relation_size(1); +ERROR: could not open relation with OID 1 +SELECT citus_total_relation_size(1); +ERROR: could not open relation with OID 1 +-- Tests with non-distributed table +CREATE TABLE non_distributed_table (x int); +SELECT citus_table_size('non_distributed_table'); +ERROR: cannot calculate the size because relation 'non_distributed_table' is not distributed +SELECT citus_relation_size('non_distributed_table'); +ERROR: cannot calculate the size because relation 'non_distributed_table' is not distributed +SELECT citus_total_relation_size('non_distributed_table'); +ERROR: cannot calculate the size because relation 'non_distributed_table' is not distributed +DROP TABLE non_distributed_table; -- Tests on distributed table with replication factor > 1 SELECT citus_table_size('lineitem_hash_part'); ERROR: cannot calculate the size because replication factor is greater than 1 diff --git a/src/test/regress/sql/multi_size_queries.sql b/src/test/regress/sql/multi_size_queries.sql index 24afe7997..27331361f 100644 --- a/src/test/regress/sql/multi_size_queries.sql +++ b/src/test/regress/sql/multi_size_queries.sql @@ -8,6 +8,18 @@ ALTER SEQUENCE pg_catalog.pg_dist_shardid_seq RESTART 1390000; ALTER SEQUENCE pg_catalog.pg_dist_jobid_seq RESTART 1390000; +-- Tests with invalid relation IDs +SELECT citus_table_size(1); +SELECT citus_relation_size(1); +SELECT citus_total_relation_size(1); + +-- Tests with non-distributed table +CREATE TABLE non_distributed_table (x int); +SELECT citus_table_size('non_distributed_table'); +SELECT citus_relation_size('non_distributed_table'); +SELECT citus_total_relation_size('non_distributed_table'); +DROP TABLE non_distributed_table; + -- Tests on distributed table with replication factor > 1 SELECT citus_table_size('lineitem_hash_part'); SELECT citus_relation_size('lineitem_hash_part');