mirror of
https://github.com/timescale/timescaledb.git
synced 2025-05-31 01:15:18 +08:00
Fix a number of issues with size and stats functions: * Return `0` size instead of `NULL` in several functions when hypertables have no chunks (e.g., `hypertable_size`, `hypertable_detailed_size`). * Return `NULL` when functions are called on non-hypertables instead of simply failing with generic error `query returned no rows`. * Include size of "root" hypertable, which can have non-zero size indexes and other objects even if the root table holds no data. * Make `hypertable_detailed_size` include one additional row for storage size of objects on the access node. While the access node stores no data, the empty hypertable may still take up some disk space. * Improve test coverage for all size utility functions. In particular, add tests on regular tables as well as empty and compressed hypertables. * Several size utility functions that were defined as `PL/pgSQL` functions have been converted to simple `SQL` functions since they ran only a single SQL query. The `dist_util` test is moved to the solo test group because, otherwise, it gives different size output when run in parallel vs. in isolation. Fixes #2871
184 lines
8.2 KiB
Plaintext
184 lines
8.2 KiB
Plaintext
-- This file and its contents are licensed under the Apache License 2.0.
|
|
-- Please see the included NOTICE for copyright information and
|
|
-- LICENSE-APACHE for a copy of the license.
|
|
-- Set this variable to avoid using a hard-coded path each time query
|
|
-- results are compared
|
|
\set QUERY_RESULT_TEST_EQUAL_RELPATH 'include/query_result_test_equal.sql'
|
|
\c postgres :ROLE_SUPERUSER
|
|
DROP DATABASE :TEST_DBNAME;
|
|
CREATE DATABASE :TEST_DBNAME;
|
|
\c :TEST_DBNAME
|
|
CREATE SCHEMA "testSchema0";
|
|
SET client_min_messages=error;
|
|
CREATE EXTENSION IF NOT EXISTS timescaledb SCHEMA "testSchema0";
|
|
RESET client_min_messages;
|
|
CREATE TABLE test_ts(time timestamp, temp float8, device text);
|
|
CREATE TABLE test_tz(time timestamptz, temp float8, device text);
|
|
CREATE TABLE test_dt(time date, temp float8, device text);
|
|
SELECT "testSchema0".create_hypertable('test_ts', 'time', 'device', 2);
|
|
NOTICE: adding not-null constraint to column "time"
|
|
create_hypertable
|
|
----------------------
|
|
(1,public,test_ts,t)
|
|
(1 row)
|
|
|
|
SELECT "testSchema0".create_hypertable('test_tz', 'time', 'device', 2);
|
|
NOTICE: adding not-null constraint to column "time"
|
|
create_hypertable
|
|
----------------------
|
|
(2,public,test_tz,t)
|
|
(1 row)
|
|
|
|
SELECT "testSchema0".create_hypertable('test_dt', 'time', 'device', 2);
|
|
NOTICE: adding not-null constraint to column "time"
|
|
create_hypertable
|
|
----------------------
|
|
(3,public,test_dt,t)
|
|
(1 row)
|
|
|
|
SELECT * FROM _timescaledb_catalog.hypertable;
|
|
id | schema_name | table_name | associated_schema_name | associated_table_prefix | num_dimensions | chunk_sizing_func_schema | chunk_sizing_func_name | chunk_target_size | compression_state | compressed_hypertable_id | replication_factor
|
|
----+-------------+------------+------------------------+-------------------------+----------------+--------------------------+--------------------------+-------------------+-------------------+--------------------------+--------------------
|
|
1 | public | test_ts | _timescaledb_internal | _hyper_1 | 2 | _timescaledb_internal | calculate_chunk_interval | 0 | 0 | |
|
|
2 | public | test_tz | _timescaledb_internal | _hyper_2 | 2 | _timescaledb_internal | calculate_chunk_interval | 0 | 0 | |
|
|
3 | public | test_dt | _timescaledb_internal | _hyper_3 | 2 | _timescaledb_internal | calculate_chunk_interval | 0 | 0 | |
|
|
(3 rows)
|
|
|
|
INSERT INTO test_ts VALUES('Mon Mar 20 09:17:00.936242 2017', 23.4, 'dev1');
|
|
INSERT INTO test_ts VALUES('Mon Mar 20 09:27:00.936242 2017', 22, 'dev2');
|
|
INSERT INTO test_ts VALUES('Mon Mar 20 09:28:00.936242 2017', 21.2, 'dev1');
|
|
INSERT INTO test_ts VALUES('Mon Mar 20 09:37:00.936242 2017', 30, 'dev3');
|
|
SELECT * FROM test_ts ORDER BY time;
|
|
time | temp | device
|
|
---------------------------------+------+--------
|
|
Mon Mar 20 09:17:00.936242 2017 | 23.4 | dev1
|
|
Mon Mar 20 09:27:00.936242 2017 | 22 | dev2
|
|
Mon Mar 20 09:28:00.936242 2017 | 21.2 | dev1
|
|
Mon Mar 20 09:37:00.936242 2017 | 30 | dev3
|
|
(4 rows)
|
|
|
|
INSERT INTO test_tz VALUES('Mon Mar 20 09:17:00.936242 2017', 23.4, 'dev1');
|
|
INSERT INTO test_tz VALUES('Mon Mar 20 09:27:00.936242 2017', 22, 'dev2');
|
|
INSERT INTO test_tz VALUES('Mon Mar 20 09:28:00.936242 2017', 21.2, 'dev1');
|
|
INSERT INTO test_tz VALUES('Mon Mar 20 09:37:00.936242 2017', 30, 'dev3');
|
|
SELECT * FROM test_tz ORDER BY time;
|
|
time | temp | device
|
|
-------------------------------------+------+--------
|
|
Mon Mar 20 09:17:00.936242 2017 PDT | 23.4 | dev1
|
|
Mon Mar 20 09:27:00.936242 2017 PDT | 22 | dev2
|
|
Mon Mar 20 09:28:00.936242 2017 PDT | 21.2 | dev1
|
|
Mon Mar 20 09:37:00.936242 2017 PDT | 30 | dev3
|
|
(4 rows)
|
|
|
|
INSERT INTO test_dt VALUES('Mon Mar 20 09:17:00.936242 2017', 23.4, 'dev1');
|
|
INSERT INTO test_dt VALUES('Mon Mar 21 09:27:00.936242 2017', 22, 'dev2');
|
|
INSERT INTO test_dt VALUES('Mon Mar 22 09:28:00.936242 2017', 21.2, 'dev1');
|
|
INSERT INTO test_dt VALUES('Mon Mar 23 09:37:00.936242 2017', 30, 'dev3');
|
|
SELECT * FROM test_dt ORDER BY time;
|
|
time | temp | device
|
|
------------+------+--------
|
|
03-20-2017 | 23.4 | dev1
|
|
03-21-2017 | 22 | dev2
|
|
03-22-2017 | 21.2 | dev1
|
|
03-23-2017 | 30 | dev3
|
|
(4 rows)
|
|
|
|
-- testing time_bucket START
|
|
SELECT AVG(temp) AS avg_tmp, "testSchema0".time_bucket('5 minutes', time, INTERVAL '1 minutes') AS ten_min FROM test_ts GROUP BY ten_min ORDER BY avg_tmp;
|
|
avg_tmp | ten_min
|
|
---------+--------------------------
|
|
21.6 | Mon Mar 20 09:26:00 2017
|
|
23.4 | Mon Mar 20 09:16:00 2017
|
|
30 | Mon Mar 20 09:36:00 2017
|
|
(3 rows)
|
|
|
|
SELECT AVG(temp) AS avg_tmp, "testSchema0".time_bucket('5 minutes', time, INTERVAL '1 minutes') AS ten_min FROM test_tz GROUP BY ten_min ORDER BY avg_tmp;
|
|
avg_tmp | ten_min
|
|
---------+------------------------------
|
|
21.6 | Mon Mar 20 09:26:00 2017 PDT
|
|
23.4 | Mon Mar 20 09:16:00 2017 PDT
|
|
30 | Mon Mar 20 09:36:00 2017 PDT
|
|
(3 rows)
|
|
|
|
SELECT AVG(temp) AS avg_tmp, "testSchema0".time_bucket('1 day', time, INTERVAL '-0.5 day') AS ten_min FROM test_dt GROUP BY ten_min ORDER BY avg_tmp;
|
|
avg_tmp | ten_min
|
|
---------+------------
|
|
21.2 | 03-21-2017
|
|
22 | 03-20-2017
|
|
23.4 | 03-19-2017
|
|
30 | 03-22-2017
|
|
(4 rows)
|
|
|
|
-- testing time_bucket END
|
|
-- testing drop_chunks START
|
|
-- show_chunks and drop_chunks output should be the same
|
|
\set QUERY1 'SELECT "testSchema0".show_chunks(older_than => \'2017-03-01\'::timestamp, relation => \'test_ts\')::REGCLASS::TEXT'
|
|
\set QUERY2 'SELECT "testSchema0".drop_chunks(\'test_ts\', \'2017-03-01\'::timestamp)::TEXT'
|
|
\set ECHO errors
|
|
Different Rows | Total Rows from Query 1 | Total Rows from Query 2
|
|
----------------+-------------------------+-------------------------
|
|
0 | 0 | 0
|
|
(1 row)
|
|
|
|
SELECT * FROM test_ts ORDER BY time;
|
|
time | temp | device
|
|
---------------------------------+------+--------
|
|
Mon Mar 20 09:17:00.936242 2017 | 23.4 | dev1
|
|
Mon Mar 20 09:27:00.936242 2017 | 22 | dev2
|
|
Mon Mar 20 09:28:00.936242 2017 | 21.2 | dev1
|
|
Mon Mar 20 09:37:00.936242 2017 | 30 | dev3
|
|
(4 rows)
|
|
|
|
\set QUERY1 'SELECT "testSchema0".show_chunks(older_than => interval \'1 minutes\', relation => \'test_tz\')::REGCLASS::TEXT'
|
|
\set QUERY2 'SELECT "testSchema0".drop_chunks(\'test_tz\', interval \'1 minutes\')::TEXT'
|
|
\set ECHO errors
|
|
Different Rows | Total Rows from Query 1 | Total Rows from Query 2
|
|
----------------+-------------------------+-------------------------
|
|
0 | 2 | 2
|
|
(1 row)
|
|
|
|
SELECT * FROM test_tz ORDER BY time;
|
|
time | temp | device
|
|
------+------+--------
|
|
(0 rows)
|
|
|
|
\set QUERY1 'SELECT "testSchema0".show_chunks(older_than => interval \'1 minutes\', relation => \'test_dt\')::REGCLASS::TEXT'
|
|
\set QUERY2 'SELECT "testSchema0".drop_chunks(\'test_dt\', interval \'1 minutes\')::TEXT'
|
|
\set ECHO errors
|
|
Different Rows | Total Rows from Query 1 | Total Rows from Query 2
|
|
----------------+-------------------------+-------------------------
|
|
0 | 3 | 3
|
|
(1 row)
|
|
|
|
SELECT * FROM test_dt ORDER BY time;
|
|
time | temp | device
|
|
------+------+--------
|
|
(0 rows)
|
|
|
|
-- testing drop_chunks END
|
|
-- testing hypertable_detailed_size START
|
|
SELECT * FROM "testSchema0".hypertable_detailed_size('test_ts');
|
|
table_bytes | index_bytes | toast_bytes | total_bytes | node_name
|
|
-------------+-------------+-------------+-------------+-----------
|
|
16384 | 81920 | 16384 | 122880 |
|
|
(1 row)
|
|
|
|
-- testing hypertable_detailed_size END
|
|
SELECT * FROM "testSchema0".hypertable_index_size('test_ts_time_idx');
|
|
hypertable_index_size
|
|
-----------------------
|
|
40960
|
|
(1 row)
|
|
|
|
SELECT * FROM "testSchema0".hypertable_index_size('test_ts_device_time_idx');
|
|
hypertable_index_size
|
|
-----------------------
|
|
40960
|
|
(1 row)
|
|
|
|
CREATE SCHEMA "testSchema";
|
|
\set ON_ERROR_STOP 0
|
|
ALTER EXTENSION timescaledb SET SCHEMA "testSchema";
|
|
ERROR: extension "timescaledb" does not support SET SCHEMA
|
|
\set ON_ERROR_STOP 1
|