diff --git a/.unreleased/bugfix_6053 b/.unreleased/bugfix_6053 new file mode 100644 index 00000000000..09a8f4fad43 --- /dev/null +++ b/.unreleased/bugfix_6053 @@ -0,0 +1 @@ +Fixes: #6053 Function approximate_row_count returns 0 for caggs diff --git a/sql/size_utils.sql b/sql/size_utils.sql index 5d9bbe6d1db..d2a3c22f6ad 100644 --- a/sql/size_utils.sql +++ b/sql/size_utils.sql @@ -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; @@ -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 diff --git a/tsl/test/expected/size_utils_tsl.out b/tsl/test/expected/size_utils_tsl.out index b84991879e3..f0c1f67dd73 100644 --- a/tsl/test/expected/size_utils_tsl.out +++ b/tsl/test/expected/size_utils_tsl.out @@ -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'); @@ -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) + diff --git a/tsl/test/sql/size_utils_tsl.sql b/tsl/test/sql/size_utils_tsl.sql index 9f0d771a27f..77b32392d00 100644 --- a/tsl/test/sql/size_utils_tsl.sql +++ b/tsl/test/sql/size_utils_tsl.sql @@ -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); @@ -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');