Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function approximate_row_count returns 0 for caggs #6053

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .unreleased/bugfix_6053
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes: #6053 Function approximate_row_count returns 0 for caggs
15 changes: 15 additions & 0 deletions sql/size_utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ RETURNS BIGINT
LANGUAGE PLPGSQL VOLATILE STRICT AS
$BODY$
DECLARE
mat_ht REGCLASS = NULL;
local_table_name NAME = NULL;
local_schema_name NAME = NULL;
is_distributed BOOL = FALSE;
Expand All @@ -428,6 +429,20 @@ DECLARE
max_compressed_row_count BIGINT = 1000;
is_compressed_chunk INTEGER;
BEGIN
-- Check if input relation is continuous aggregate view then
-- get the corresponding materialized hypertable and schema name
SELECT format('%I.%I', ht.schema_name, ht.table_name)::regclass
INTO mat_ht
FROM pg_class c
JOIN pg_namespace n ON (n.OID = c.relnamespace)
JOIN _timescaledb_catalog.continuous_agg a ON (a.user_view_schema = n.nspname AND a.user_view_name = c.relname)
JOIN _timescaledb_catalog.hypertable ht ON (a.mat_hypertable_id = ht.id)
WHERE c.OID = relation;

IF mat_ht IS NOT NULL THEN
relation = mat_ht;
END IF;

SELECT relname, nspname FROM pg_class c
INNER JOIN pg_namespace n ON (n.OID = c.relnamespace)
INTO local_table_name, local_schema_name
Expand Down
13 changes: 13 additions & 0 deletions tsl/test/expected/size_utils_tsl.out
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ SELECT * FROM chunks_detailed_size('hypersize_cagg') ORDER BY node_name;
--------------+------------+-------------+-------------+-------------+-------------+-----------
(0 rows)

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

-- Test size functions on non-empty countinuous aggregate
CALL refresh_continuous_aggregate('hypersize_cagg', NULL, NULL);
SELECT * FROM hypertable_size('hypersize_cagg');
Expand Down Expand Up @@ -90,3 +96,10 @@ SELECT * FROM chunks_detailed_size(:'MAT_HYPERTABLE_NAME') ORDER BY node_name;
_timescaledb_internal | _hyper_2_2_chunk | 8192 | 16384 | 8192 | 32768 |
(1 row)

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

3 changes: 3 additions & 0 deletions tsl/test/sql/size_utils_tsl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ SELECT * FROM chunks_detailed_size(:'MAT_HYPERTABLE_NAME') ORDER BY node_name;
SELECT * FROM hypertable_size('hypersize_cagg');
SELECT * FROM hypertable_detailed_size('hypersize_cagg') ORDER BY node_name;
SELECT * FROM chunks_detailed_size('hypersize_cagg') ORDER BY node_name;
SELECT * FROM approximate_row_count('hypersize_cagg');

-- Test size functions on non-empty countinuous aggregate
CALL refresh_continuous_aggregate('hypersize_cagg', NULL, NULL);
Expand All @@ -30,3 +31,5 @@ SELECT * FROM chunks_detailed_size('hypersize_cagg') ORDER BY node_name;
SELECT * FROM hypertable_size(:'MAT_HYPERTABLE_NAME');
SELECT * FROM hypertable_detailed_size(:'MAT_HYPERTABLE_NAME') ORDER BY node_name;
SELECT * FROM chunks_detailed_size(:'MAT_HYPERTABLE_NAME') ORDER BY node_name;
ANALYZE :MAT_HYPERTABLE_NAME;
SELECT * FROM approximate_row_count('hypersize_cagg');