--
-- MULTI_SIZE_QUERIES
--
-- Test checks whether size of distributed tables can be obtained with citus_table_size.
-- To find the relation size and total relation size citus_relation_size and
-- citus_total_relation_size are also tested.
SET citus.next_shard_id TO 1390000;
-- Tests with invalid relation IDs
SELECT citus_table_size(1);
ERROR:  could not compute table size: relation does not exist
SELECT citus_relation_size(1);
ERROR:  could not compute table size: relation does not exist
SELECT citus_total_relation_size(1);
ERROR:  could not compute table size: relation does not exist
-- 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
SELECT citus_relation_size('lineitem_hash_part');
ERROR:  cannot calculate the size because replication factor is greater than 1
SELECT citus_total_relation_size('lineitem_hash_part');
ERROR:  cannot calculate the size because replication factor is greater than 1
VACUUM (FULL) customer_copy_hash;
-- Tests on distributed tables with streaming replication.
SELECT citus_table_size('customer_copy_hash');
 citus_table_size
---------------------------------------------------------------------
           548864
(1 row)

SELECT citus_relation_size('customer_copy_hash');
 citus_relation_size
---------------------------------------------------------------------
              548864
(1 row)

SELECT citus_total_relation_size('customer_copy_hash');
 citus_total_relation_size
---------------------------------------------------------------------
                   1597440
(1 row)

-- Make sure we can get multiple sizes in a single query
SELECT citus_table_size('customer_copy_hash'),
       citus_table_size('customer_copy_hash'),
       citus_table_size('supplier');
 citus_table_size | citus_table_size | citus_table_size
---------------------------------------------------------------------
           548864 |           548864 |           401408
(1 row)

CREATE INDEX index_1 on customer_copy_hash(c_custkey);
VACUUM (FULL) customer_copy_hash;
-- Tests on distributed table with index.
SELECT citus_table_size('customer_copy_hash');
 citus_table_size
---------------------------------------------------------------------
           548864
(1 row)

SELECT citus_relation_size('customer_copy_hash');
 citus_relation_size
---------------------------------------------------------------------
              548864
(1 row)

SELECT citus_total_relation_size('customer_copy_hash');
 citus_total_relation_size
---------------------------------------------------------------------
                   2646016
(1 row)

-- Tests on reference table
VACUUM (FULL) supplier;
SELECT citus_table_size('supplier');
 citus_table_size
---------------------------------------------------------------------
           376832
(1 row)

SELECT citus_relation_size('supplier');
 citus_relation_size
---------------------------------------------------------------------
              376832
(1 row)

SELECT citus_total_relation_size('supplier');
 citus_total_relation_size
---------------------------------------------------------------------
                    376832
(1 row)

CREATE INDEX index_2 on supplier(s_suppkey);
VACUUM (FULL) supplier;
SELECT citus_table_size('supplier');
 citus_table_size
---------------------------------------------------------------------
           376832
(1 row)

SELECT citus_relation_size('supplier');
 citus_relation_size
---------------------------------------------------------------------
              376832
(1 row)

SELECT citus_total_relation_size('supplier');
 citus_total_relation_size
---------------------------------------------------------------------
                    458752
(1 row)

-- Test inside the transaction
BEGIN;
ALTER TABLE supplier ALTER COLUMN s_suppkey SET NOT NULL;
select citus_table_size('supplier');
ERROR:  citus size functions cannot be called in transaction blocks which contain multi-shard data modifications
END;
show citus.node_conninfo;
 citus.node_conninfo
---------------------------------------------------------------------
 sslmode=require
(1 row)

ALTER SYSTEM SET citus.node_conninfo = 'sslmode=require';
SELECT pg_reload_conf();
 pg_reload_conf
---------------------------------------------------------------------
 t
(1 row)

-- make sure that any invalidation to the connection info
-- wouldn't prevent future commands to fail
SELECT citus_total_relation_size('customer_copy_hash');
 citus_total_relation_size
---------------------------------------------------------------------
                   2646016
(1 row)

SELECT pg_reload_conf();
 pg_reload_conf
---------------------------------------------------------------------
 t
(1 row)

SELECT citus_total_relation_size('customer_copy_hash');
 citus_total_relation_size
---------------------------------------------------------------------
                   2646016
(1 row)

-- reset back to the original node_conninfo
ALTER SYSTEM RESET citus.node_conninfo;
SELECT pg_reload_conf();
 pg_reload_conf
---------------------------------------------------------------------
 t
(1 row)

DROP INDEX index_1;
DROP INDEX index_2;
