@@ -597,6 +597,7 @@ AS $$
597597DECLARE
598598 v_compressed_chunk regclass;
599599 v_uncompressed_chunk regclass;
600+ v_segmentby text [];
600601 v_index regclass;
601602 v_fixed_column_size integer ;
602603 v_num_varlen_columns integer ;
@@ -612,7 +613,7 @@ BEGIN
612613
613614 v_compressed_chunk := $1 ;
614615
615- SELECT relid INTO v_uncompressed_chunk FROM _timescaledb_catalog .compression_settings WHERE compress_relid = v_compressed_chunk;
616+ SELECT relid, segmentby INTO v_uncompressed_chunk, v_segmentby FROM _timescaledb_catalog .compression_settings WHERE compress_relid = v_compressed_chunk;
616617 IF NOT FOUND THEN
617618 RETURN;
618619 END IF;
@@ -632,7 +633,11 @@ BEGIN
632633 v_tuple_data := v_tuple_data + 7 & ~7 ; -- align to 8 bytes
633634
634635 IF v_num_varlen_columns > 0 THEN
635- SELECT ' + (' || string_agg(format(' sum(_timescaledb_functions.compressed_data_column_size(%I,NULL::%s))' , attname, pg_catalog .format_type (atttypid, atttypmod)), ' + ' ) || ' )' FROM pg_attribute INTO v_varlen_query WHERE attrelid = v_uncompressed_chunk AND attnum > 0 AND NOT attisdropped AND attlen = - 1 ;
636+ SELECT ' + (' || string_agg(
637+ CASE WHEN attname = ANY(v_segmentby)
638+ THEN format(' sum(pg_catalog.pg_column_size(%I) * _ts_meta_count)' , attname)
639+ ELSE format(' sum(_timescaledb_functions.compressed_data_column_size(%I,NULL::%s))' , attname, pg_catalog .format_type (atttypid, atttypmod))
640+ END, ' + ' ) || ' )' FROM pg_attribute INTO v_varlen_query WHERE attrelid = v_uncompressed_chunk AND attnum > 0 AND NOT attisdropped AND attlen = - 1 ;
636641 END IF;
637642
638643 EXECUTE format(' SELECT sum(_ts_meta_count) FROM %s' , v_compressed_chunk) INTO tuples;
@@ -643,7 +648,11 @@ BEGIN
643648 FOR v_index, v_varlen_query, v_columns IN
644649 SELECT
645650 i .indexrelid ::regclass,
646- (SELECT ' + (' || string_agg(format(' sum(_timescaledb_functions.compressed_data_column_size(%I,NULL::%s))' , attname, pg_catalog .format_type (atttypid, atttypmod)), ' + ' ORDER BY attnum) || ' )' FROM pg_attribute att WHERE att .attrelid = i .indrelid AND attnum = ANY(i .indkey )),
651+ (SELECT ' + (' || string_agg(
652+ CASE WHEN attname = ANY(v_segmentby)
653+ THEN format(' sum(pg_catalog.pg_column_size(%I) * _ts_meta_count)' , attname)
654+ ELSE format(' sum(_timescaledb_functions.compressed_data_column_size(%I,NULL::%s))' , attname, pg_catalog .format_type (atttypid, atttypmod))
655+ END, ' + ' ORDER BY attnum) || ' )' FROM pg_attribute att WHERE att .attrelid = i .indrelid AND attnum = ANY(i .indkey )),
647656 array_length(i .indkey ,1 ) FROM pg_index i
648657 WHERE i .indrelid = v_uncompressed_chunk
649658 LOOP
0 commit comments