Function approximate_row_count returns 0 for caggs (#6053)

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
This commit is contained in:
Dipesh Pandit 2023-09-12 11:41:59 +05:30 committed by GitHub
parent ef783c4b55
commit 93519d0af8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 0 deletions

1
.unreleased/bugfix_6053 Normal file
View File

@ -0,0 +1 @@
Fixes: #6053 Function approximate_row_count returns 0 for caggs

View File

@ -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

View File

@ -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)

View File

@ -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');