Skip to content

Commit

Permalink
Support for continuous aggregates in hypertable_(detailed_)size
Browse files Browse the repository at this point in the history
This small patch adds support for continuous aggregates to the `hypertable_detailed_size` (and with that `hypertable_size`). It adds a minimal slice of code that checks for a continuous aggregate with the given regclass if a hypertable cannot be found directly.

Signed-off-by: Christoph Engelbert (noctarius) <me@noctarius.com>
  • Loading branch information
noctarius committed Jan 10, 2023
1 parent 7a6101a commit 2ca695e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
16 changes: 13 additions & 3 deletions sql/size_utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,19 @@ BEGIN
INNER JOIN _timescaledb_catalog.hypertable ht ON (ht.schema_name = n.nspname AND ht.table_name = c.relname)
WHERE c.OID = hypertable;

IF table_name IS NULL THEN
RETURN;
END IF;
IF table_name IS NULL THEN
SELECT h.schema_name, h.table_name, replication_factor > 0
INTO schema_name, table_name, is_distributed
FROM pg_class c
INNER JOIN pg_namespace n ON (n.OID = c.relnamespace)
INNER JOIN _timescaledb_catalog.continuous_agg a ON (a.user_view_schema = n.nspname AND a.user_view_name = c.relname)
INNER JOIN _timescaledb_catalog.hypertable h ON h.id = a.mat_hypertable_id
WHERE c.OID = hypertable;

IF table_name IS NULL THEN
RETURN;
END IF;
END IF;

CASE WHEN is_distributed THEN
RETURN QUERY
Expand Down
41 changes: 41 additions & 0 deletions tsl/test/expected/size_utils.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
-- Prepare test data for continuous aggregate size function tests
CREATE TABLE hypersize(time timestamptz, device int);
SELECT * FROM create_hypertable('hypersize', 'time');
NOTICE: adding not-null constraint to column "time"
hypertable_id | schema_name | table_name | created
---------------+-------------+------------+---------
1 | public | hypersize | t
(1 row)

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 * FROM hypertable_size('hypersize_cagg');
hypertable_size
-----------------
16384
(1 row)

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 |
(1 row)

-- 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
(1 row)

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 |
(1 row)

3 changes: 2 additions & 1 deletion tsl/test/sql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ set(TEST_FILES
move.sql
partialize_finalize.sql
reorder.sql
skip_scan.sql)
skip_scan.sql
size_utils.sql)

if(CMAKE_BUILD_TYPE MATCHES Debug)
list(
Expand Down
19 changes: 19 additions & 0 deletions tsl/test/sql/size_utils.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.

-- Prepare test data for continuous aggregate size function tests
CREATE TABLE hypersize(time timestamptz, device int);
SELECT * FROM create_hypertable('hypersize', 'time');
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 * FROM hypertable_size('hypersize_cagg');
SELECT * FROM hypertable_detailed_size('hypersize_cagg') ORDER BY node_name;

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

0 comments on commit 2ca695e

Please sign in to comment.