Skip to content

Commit

Permalink
Function approximate_row_count returns 0 for caggs (#6053)
Browse files Browse the repository at this point in the history
The approximate_row_count function is executed directly on the user view
instead of corresponding materialized hypertable which returns 0 for
caggs. The function is updated to fetch the details for materialized
hypertable information corresponding to cagg and then get the
approximate_row_count for the materialized hypertable.

Fixes #6051
  • Loading branch information
pdipesh02 authored Sep 12, 2023
1 parent ef783c4 commit 93519d0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
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');

0 comments on commit 93519d0

Please sign in to comment.