-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
SET timezone TO 'America/Los_Angeles';
-- Prepare test data for continuous aggregate size function tests
CREATE TABLE hypersize(time timestamptz, device int);
SELECT * FROM create_hypertable('hypersize', 'time');
 hypertable_id | schema_name | table_name | created 
---------------+-------------+------------+---------
             1 | public      | hypersize  | t

INSERT INTO hypersize VALUES('2021-02-25', 1);
-- Test size functions on empty continuous aggregate
CREATE MATERIALIZED VIEW hypersize_cagg WITH (timescaledb.continuous) AS SELECT time_bucket('1 day', "time") AS bucket, AVG(device) AS value FROM hypersize GROUP BY 1 WITH NO DATA;
SELECT format('%I.%I', h.schema_name, h.table_name) AS "MAT_HYPERTABLE_NAME"
FROM _timescaledb_catalog.continuous_agg ca
INNER JOIN _timescaledb_catalog.hypertable h ON(h.id = ca.mat_hypertable_id)
WHERE user_view_name = 'hypersize_cagg'
\gset
SELECT * FROM hypertable_size(:'MAT_HYPERTABLE_NAME');
 hypertable_size 
-----------------
           16384

SELECT * FROM hypertable_detailed_size(:'MAT_HYPERTABLE_NAME') ORDER BY node_name;
 table_bytes | index_bytes | toast_bytes | total_bytes | node_name 
-------------+-------------+-------------+-------------+-----------
           0 |        8192 |        8192 |       16384 | 

SELECT * FROM chunks_detailed_size(:'MAT_HYPERTABLE_NAME') ORDER BY node_name;
 chunk_schema | chunk_name | table_bytes | index_bytes | toast_bytes | total_bytes | node_name 
--------------+------------+-------------+-------------+-------------+-------------+-----------

SELECT * FROM hypertable_size('hypersize_cagg');
 hypertable_size 
-----------------
           16384

SELECT * FROM hypertable_detailed_size('hypersize_cagg') ORDER BY node_name;
 table_bytes | index_bytes | toast_bytes | total_bytes | node_name 
-------------+-------------+-------------+-------------+-----------
           0 |        8192 |        8192 |       16384 | 

SELECT * FROM chunks_detailed_size('hypersize_cagg') ORDER BY node_name;
 chunk_schema | chunk_name | table_bytes | index_bytes | toast_bytes | total_bytes | node_name 
--------------+------------+-------------+-------------+-------------+-------------+-----------

SELECT * FROM approximate_row_count('hypersize_cagg');
 approximate_row_count 
-----------------------
                     0

-- Test size functions on non-empty countinuous aggregate
CALL refresh_continuous_aggregate('hypersize_cagg', NULL, NULL);
SELECT * FROM hypertable_size('hypersize_cagg');
 hypertable_size 
-----------------
           49152

SELECT * FROM hypertable_detailed_size('hypersize_cagg') ORDER BY node_name;
 table_bytes | index_bytes | toast_bytes | total_bytes | node_name 
-------------+-------------+-------------+-------------+-----------
        8192 |       24576 |       16384 |       49152 | 

SELECT * FROM chunks_detailed_size('hypersize_cagg') ORDER BY node_name;
     chunk_schema      |    chunk_name    | table_bytes | index_bytes | toast_bytes | total_bytes | node_name 
-----------------------+------------------+-------------+-------------+-------------+-------------+-----------
 _timescaledb_internal | _hyper_2_2_chunk |        8192 |       16384 |        8192 |       32768 | 

SELECT * FROM hypertable_size(:'MAT_HYPERTABLE_NAME');
 hypertable_size 
-----------------
           49152

SELECT * FROM hypertable_detailed_size(:'MAT_HYPERTABLE_NAME') ORDER BY node_name;
 table_bytes | index_bytes | toast_bytes | total_bytes | node_name 
-------------+-------------+-------------+-------------+-----------
        8192 |       24576 |       16384 |       49152 | 

SELECT * FROM chunks_detailed_size(:'MAT_HYPERTABLE_NAME') ORDER BY node_name;
     chunk_schema      |    chunk_name    | table_bytes | index_bytes | toast_bytes | total_bytes | node_name 
-----------------------+------------------+-------------+-------------+-------------+-------------+-----------
 _timescaledb_internal | _hyper_2_2_chunk |        8192 |       16384 |        8192 |       32768 | 

ANALYZE :MAT_HYPERTABLE_NAME;
SELECT * FROM approximate_row_count('hypersize_cagg');
 approximate_row_count 
-----------------------
                     1

