Skip to content

Commit

Permalink
Fix for inconsistent num_chunks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shhnwz authored and shhnwz committed Feb 28, 2023
1 parent 2f7e043 commit e6f6eb3
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ accidentally triggering the load of a previous DB version.**
## Unreleased

**Bugfixes**
* #5364 Fix num_chunks inconsistency in hypertables view
* #5336 Use NameData and namestrcpy for names
* #5317 Fix some incorrect memory handling

Expand Down
2 changes: 1 addition & 1 deletion sql/views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SELECT ht.schema_name AS hypertable_schema,
(
SELECT count(1)
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
TRUE
Expand Down
68 changes: 68 additions & 0 deletions tsl/test/expected/information_view_chunk_count.out
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tsl/test/sql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ if(CMAKE_BUILD_TYPE MATCHES Debug)
dist_triggers.sql
dist_backup.sql
insert_memory_usage.sql
information_view_chunk_count.sql
read_only.sql
remote_connection_cache.sql
remote_connection.sql
Expand Down
46 changes: 46 additions & 0 deletions tsl/test/sql/information_view_chunk_count.sql
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit e6f6eb3

Please sign in to comment.