Fix for inconsistent num_chunks

Different num_chunks values reported by
timescaledb_information.hypertables and
timescaledb_information.chunks.
View definition of hypertables was
not filtering dropped and osm_chunks.

Fixes #5338
This commit is contained in:
shhnwz 2023-02-26 22:18:02 +05:30 committed by Mohammed Shahnawaz
parent 2f7e0433a9
commit e6f6eb3ab8
5 changed files with 117 additions and 1 deletions

View File

@ -7,6 +7,7 @@ accidentally triggering the load of a previous DB version.**
## Unreleased ## Unreleased
**Bugfixes** **Bugfixes**
* #5364 Fix num_chunks inconsistency in hypertables view
* #5336 Use NameData and namestrcpy for names * #5336 Use NameData and namestrcpy for names
* #5317 Fix some incorrect memory handling * #5317 Fix some incorrect memory handling

View File

@ -11,7 +11,7 @@ SELECT ht.schema_name AS hypertable_schema,
( (
SELECT count(1) SELECT count(1)
FROM _timescaledb_catalog.chunk ch FROM _timescaledb_catalog.chunk ch
WHERE ch.hypertable_id = ht.id) AS num_chunks, WHERE ch.hypertable_id = ht.id AND ch.dropped IS FALSE AND ch.osm_chunk IS FALSE) AS num_chunks,
( (
CASE WHEN compression_state = 1 THEN CASE WHEN compression_state = 1 THEN
TRUE TRUE

View File

@ -0,0 +1,68 @@
-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
CREATE TABLE sensor_data(
time timestamptz not null,
sensor_id integer not null,
cpu double precision null,
temperature double precision null);
SELECT from create_hypertable('sensor_data','time');
--
(1 row)
INSERT INTO sensor_data
SELECT time + (INTERVAL '1 minute' * random()) AS time,
sensor_id,
random() AS cpu,
random() * 100 AS temperature
FROM
generate_series('2018-03-02 1:00'::TIMESTAMPTZ, '2018-03-28 1:00', '1 hour') AS g1(time),
generate_series(1, 100, 1 ) AS g2(sensor_id)
ORDER BY time;
CREATE materialized VIEW sensor_data_v WITH(timescaledb.continuous) AS
SELECT sensor_id, time_bucket(INTERVAL '1 day', time) AS bucket,
AVG(temperature) FROM sensor_data
GROUP BY sensor_id, bucket;
NOTICE: refreshing continuous aggregate "sensor_data_v"
INSERT INTO sensor_data
SELECT time + (INTERVAL '1 minute' * random()) AS time,
sensor_id, random() AS cpu, random()* 100 AS temperature
FROM
generate_series('2018-03-03 1:00'::TIMESTAMPTZ, '2018-03-31 1:00', '1 hour') AS g1(time),
generate_series(1, 100, 1 ) AS g2(sensor_id)
ORDER BY time;
SELECT count(*) AS num_chunks FROM show_chunks('sensor_data');
num_chunks
------------
5
(1 row)
SELECT drop_chunks('sensor_data','2018-03-28'::timestamp);
drop_chunks
----------------------------------------
_timescaledb_internal._hyper_1_1_chunk
_timescaledb_internal._hyper_1_2_chunk
_timescaledb_internal._hyper_1_3_chunk
(3 rows)
SELECT count(*) AS num_chunks from timescaledb_information.chunks where hypertable_name = 'sensor_data';
num_chunks
------------
2
(1 row)
SELECT num_chunks from timescaledb_information.hypertables where hypertable_name = 'sensor_data';
num_chunks
------------
2
(1 row)
SELECT count(*) AS num_chunks FROM show_chunks('sensor_data');
num_chunks
------------
2
(1 row)
DROP TABLE sensor_data CASCADE;
NOTICE: drop cascades to 3 other objects
NOTICE: drop cascades to table _timescaledb_internal._hyper_2_5_chunk

View File

@ -87,6 +87,7 @@ if(CMAKE_BUILD_TYPE MATCHES Debug)
dist_triggers.sql dist_triggers.sql
dist_backup.sql dist_backup.sql
insert_memory_usage.sql insert_memory_usage.sql
information_view_chunk_count.sql
read_only.sql read_only.sql
remote_connection_cache.sql remote_connection_cache.sql
remote_connection.sql remote_connection.sql

View File

@ -0,0 +1,46 @@
-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
CREATE TABLE sensor_data(
time timestamptz not null,
sensor_id integer not null,
cpu double precision null,
temperature double precision null);
SELECT from create_hypertable('sensor_data','time');
INSERT INTO sensor_data
SELECT time + (INTERVAL '1 minute' * random()) AS time,
sensor_id,
random() AS cpu,
random() * 100 AS temperature
FROM
generate_series('2018-03-02 1:00'::TIMESTAMPTZ, '2018-03-28 1:00', '1 hour') AS g1(time),
generate_series(1, 100, 1 ) AS g2(sensor_id)
ORDER BY time;
CREATE materialized VIEW sensor_data_v WITH(timescaledb.continuous) AS
SELECT sensor_id, time_bucket(INTERVAL '1 day', time) AS bucket,
AVG(temperature) FROM sensor_data
GROUP BY sensor_id, bucket;
INSERT INTO sensor_data
SELECT time + (INTERVAL '1 minute' * random()) AS time,
sensor_id, random() AS cpu, random()* 100 AS temperature
FROM
generate_series('2018-03-03 1:00'::TIMESTAMPTZ, '2018-03-31 1:00', '1 hour') AS g1(time),
generate_series(1, 100, 1 ) AS g2(sensor_id)
ORDER BY time;
SELECT count(*) AS num_chunks FROM show_chunks('sensor_data');
SELECT drop_chunks('sensor_data','2018-03-28'::timestamp);
SELECT count(*) AS num_chunks from timescaledb_information.chunks where hypertable_name = 'sensor_data';
SELECT num_chunks from timescaledb_information.hypertables where hypertable_name = 'sensor_data';
SELECT count(*) AS num_chunks FROM show_chunks('sensor_data');
DROP TABLE sensor_data CASCADE;