timescaledb/tsl/test/expected/chunk_api.out
Sven Klemm c87be4ab84 Remove get_chunk_colstats and get_chunk_relstats
These 2 functions were used in the multinode context and are no longer
used now.
2024-03-03 23:14:02 +01:00

554 lines
30 KiB
Plaintext

-- 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.
\c :TEST_DBNAME :ROLE_SUPERUSER
GRANT CREATE ON DATABASE :"TEST_DBNAME" TO :ROLE_DEFAULT_PERM_USER;
SET ROLE :ROLE_DEFAULT_PERM_USER;
CREATE SCHEMA "ChunkSchema";
-- Use range types as well for columns
CREATE TABLE chunkapi(time timestamptz not null, device int, temp float, rng int8range);
SELECT * FROM create_hypertable('chunkapi', 'time', 'device', 2);
hypertable_id | schema_name | table_name | created
---------------+-------------+------------+---------
1 | public | chunkapi | t
(1 row)
INSERT INTO chunkapi VALUES ('2018-01-01 05:00:00-8', 1, 23.4, int8range(4, 10));
SELECT (_timescaledb_functions.show_chunk(show_chunks)).*
FROM show_chunks('chunkapi')
ORDER BY chunk_id;
chunk_id | hypertable_id | schema_name | table_name | relkind | slices
----------+---------------+-----------------------+------------------+---------+----------------------------------------------------------------------------------------------
1 | 1 | _timescaledb_internal | _hyper_1_1_chunk | r | {"time": [1514419200000000, 1515024000000000], "device": [-9223372036854775808, 1073741823]}
(1 row)
-- Creating a chunk with the constraints of an existing chunk should
-- return the existing chunk
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi',' {"time": [1514419200000000, 1515024000000000], "device": [-9223372036854775808, 1073741823]}');
chunk_id | hypertable_id | schema_name | table_name | relkind | slices | created
----------+---------------+-----------------------+------------------+---------+----------------------------------------------------------------------------------------------+---------
1 | 1 | _timescaledb_internal | _hyper_1_1_chunk | r | {"time": [1514419200000000, 1515024000000000], "device": [-9223372036854775808, 1073741823]} | f
(1 row)
\set VERBOSITY default
\set ON_ERROR_STOP 0
-- Modified time constraint should fail with collision
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi',' {"time": [1514419600000000, 1515024000000000], "device": [-9223372036854775808, 1073741823]}');
ERROR: chunk creation failed due to collision
-- Missing dimension
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi',' {"time": [1514419600000000, 1515024000000000]}');
ERROR: invalid hypercube for hypertable "chunkapi"
DETAIL: invalid number of hypercube dimensions
-- Extra dimension
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi',' {"time": [1514419600000000, 1515024000000000], "device": [-9223372036854775808, 1073741823], "time2": [1514419600000000, 1515024000000000]}');
ERROR: invalid hypercube for hypertable "chunkapi"
DETAIL: invalid number of hypercube dimensions
-- Bad dimension name
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi',' {"time": [1514419600000000, 1515024000000000], "dev": [-9223372036854775808, 1073741823]}');
ERROR: invalid hypercube for hypertable "chunkapi"
DETAIL: dimension "dev" does not exist in hypertable
-- Same dimension twice
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi',' {"time": [1514419600000000, 1515024000000000], "time": [1514419600000000, 1515024000000000]}');
ERROR: invalid hypercube for hypertable "chunkapi"
DETAIL: invalid number of hypercube dimensions
-- Bad bounds format
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi',' {"time": ["1514419200000000", 1515024000000000], "device": [-9223372036854775808, 1073741823]}');
ERROR: invalid hypercube for hypertable "chunkapi"
DETAIL: constraint for dimension "time" is not numeric
-- Bad slices format
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi',' {"time": [1515024000000000], "device": [-9223372036854775808, 1073741823]}');
ERROR: invalid hypercube for hypertable "chunkapi"
DETAIL: unexpected number of dimensional bounds for dimension "time"
-- Bad slices json
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi',' {"time: [1515024000000000] "device": [-9223372036854775808, 1073741823]}');
ERROR: invalid input syntax for type json
LINE 1: ...OM _timescaledb_functions.create_chunk('chunkapi',' {"time: ...
^
DETAIL: Token "device" is invalid.
CONTEXT: JSON data, line 1: {"time: [1515024000000000] "device...
\set ON_ERROR_STOP 1
SET ROLE :ROLE_DEFAULT_PERM_USER;
-- Test create_chunk_table for errors
\set ON_ERROR_STOP 0
-- Test create_chunk_table for NULL input
SELECT * FROM _timescaledb_functions.create_chunk_table(NULL,' {"time": [1515024000000000, 1519024000000000], "device": [-9223372036854775808, 1073741823]}', '_timescaledb_internal','_hyper_1_1_chunk');
ERROR: hypertable cannot be NULL
SELECT * FROM _timescaledb_functions.create_chunk_table('chunkapi', NULL, '_timescaledb_internal','_hyper_1_1_chunk');
ERROR: slices cannot be NULL
SELECT * FROM _timescaledb_functions.create_chunk_table('chunkapi',' {"time": [1515024000000000, 1519024000000000], "device": [-9223372036854775808, 1073741823]}', NULL,'_hyper_1_1_chunk');
ERROR: chunk schema name cannot be NULL
SELECT * FROM _timescaledb_functions.create_chunk_table('chunkapi',' {"time": [1515024000000000, 1519024000000000], "device": [-9223372036854775808, 1073741823]}', '_timescaledb_internal',NULL);
ERROR: chunk table name cannot be NULL
-- Modified time constraint should fail with collision
SELECT * FROM _timescaledb_functions.create_chunk_table('chunkapi',' {"time": [1514419600000000, 1515024000000000], "device": [-9223372036854775808, 1073741823]}', '_timescaledb_internal','_hyper_1_1_chunk');
ERROR: chunk table creation failed due to dimension slice collision
-- Missing dimension
SELECT * FROM _timescaledb_functions.create_chunk_table('chunkapi',' {"time": [1514419600000000, 1515024000000000]}', '_timescaledb_internal','_hyper_1_1_chunk');
ERROR: invalid hypercube for hypertable "chunkapi"
DETAIL: invalid number of hypercube dimensions
-- Extra dimension
SELECT * FROM _timescaledb_functions.create_chunk_table('chunkapi',' {"time": [1514419600000000, 1515024000000000], "device": [-9223372036854775808, 1073741823], "time2": [1514419600000000, 1515024000000000]}', '_timescaledb_internal','_hyper_1_1_chunk');
ERROR: invalid hypercube for hypertable "chunkapi"
DETAIL: invalid number of hypercube dimensions
-- Bad dimension name
SELECT * FROM _timescaledb_functions.create_chunk_table('chunkapi',' {"time": [1514419600000000, 1515024000000000], "dev": [-9223372036854775808, 1073741823]}', '_timescaledb_internal','_hyper_1_1_chunk');
ERROR: invalid hypercube for hypertable "chunkapi"
DETAIL: dimension "dev" does not exist in hypertable
-- Same dimension twice
SELECT * FROM _timescaledb_functions.create_chunk_table('chunkapi',' {"time": [1514419600000000, 1515024000000000], "time": [1514419600000000, 1515024000000000]}', '_timescaledb_internal','_hyper_1_1_chunk');
ERROR: invalid hypercube for hypertable "chunkapi"
DETAIL: invalid number of hypercube dimensions
-- Bad bounds format
SELECT * FROM _timescaledb_functions.create_chunk_table('chunkapi',' {"time": ["1514419200000000", 1515024000000000], "device": [-9223372036854775808, 1073741823]}', '_timescaledb_internal','_hyper_1_1_chunk');
ERROR: invalid hypercube for hypertable "chunkapi"
DETAIL: constraint for dimension "time" is not numeric
-- Bad slices format
SELECT * FROM _timescaledb_functions.create_chunk_table('chunkapi',' {"time": [1515024000000000], "device": [-9223372036854775808, 1073741823]}', '_timescaledb_internal','_hyper_1_1_chunk');
ERROR: invalid hypercube for hypertable "chunkapi"
DETAIL: unexpected number of dimensional bounds for dimension "time"
-- Bad slices json
SELECT * FROM _timescaledb_functions.create_chunk_table('chunkapi',' {"time: [1515024000000000] "device": [-9223372036854775808, 1073741823]}', '_timescaledb_internal','_hyper_1_1_chunk');
ERROR: invalid input syntax for type json
LINE 1: ...mescaledb_functions.create_chunk_table('chunkapi',' {"time: ...
^
DETAIL: Token "device" is invalid.
CONTEXT: JSON data, line 1: {"time: [1515024000000000] "device...
\set ON_ERROR_STOP 1
-- Test that granting insert on tables allow create_chunk to be
-- called. This will also create a chunk that does not collide and has
-- a custom schema and name.
SET ROLE :ROLE_SUPERUSER;
GRANT INSERT ON chunkapi TO :ROLE_DEFAULT_PERM_USER_2;
SET ROLE :ROLE_DEFAULT_PERM_USER_2;
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi',' {"time": [1515024000000000, 1519024000000000], "device": [-9223372036854775808, 1073741823]}', 'ChunkSchema', 'My_chunk_Table_name');
chunk_id | hypertable_id | schema_name | table_name | relkind | slices | created
----------+---------------+-------------+---------------------+---------+----------------------------------------------------------------------------------------------+---------
2 | 1 | ChunkSchema | My_chunk_Table_name | r | {"time": [1515024000000000, 1519024000000000], "device": [-9223372036854775808, 1073741823]} | t
(1 row)
SET ROLE :ROLE_DEFAULT_PERM_USER;
-- Test create_chunk_table to recreate the chunk table and show dimension slices
SET ROLE :ROLE_DEFAULT_PERM_USER;
SELECT * FROM chunkapi ORDER BY time;
time | device | temp | rng
------------------------------+--------+------+--------
Mon Jan 01 05:00:00 2018 PST | 1 | 23.4 | [4,10)
(1 row)
SELECT chunk_schema AS "CHUNK_SCHEMA", chunk_name AS "CHUNK_NAME"
FROM timescaledb_information.chunks c
ORDER BY chunk_name DESC
LIMIT 1 \gset
SELECT slices AS "SLICES"
FROM _timescaledb_functions.show_chunk(:'CHUNK_SCHEMA'||'.'||:'CHUNK_NAME') \gset
SELECT relname
FROM pg_catalog.pg_inherits, pg_class
WHERE inhrelid = (:'CHUNK_SCHEMA'||'.'||:'CHUNK_NAME')::regclass AND inhparent = oid;
relname
----------
chunkapi
(1 row)
SELECT * FROM _timescaledb_catalog.dimension_slice ORDER BY id;
id | dimension_id | range_start | range_end
----+--------------+----------------------+------------------
1 | 1 | 1514419200000000 | 1515024000000000
2 | 2 | -9223372036854775808 | 1073741823
3 | 1 | 1515024000000000 | 1519024000000000
(3 rows)
DROP TABLE :CHUNK_SCHEMA.:CHUNK_NAME;
SELECT * FROM _timescaledb_catalog.dimension_slice ORDER BY id;
id | dimension_id | range_start | range_end
----+--------------+----------------------+------------------
2 | 2 | -9223372036854775808 | 1073741823
3 | 1 | 1515024000000000 | 1519024000000000
(2 rows)
SELECT count(*) FROM
_timescaledb_functions.create_chunk_table('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME');
count
-------
1
(1 row)
SELECT * FROM _timescaledb_catalog.dimension_slice ORDER BY id;
id | dimension_id | range_start | range_end
----+--------------+----------------------+------------------
2 | 2 | -9223372036854775808 | 1073741823
3 | 1 | 1515024000000000 | 1519024000000000
(2 rows)
SELECT relname
FROM pg_catalog.pg_inherits, pg_class
WHERE inhrelid = (:'CHUNK_SCHEMA'||'.'||:'CHUNK_NAME')::regclass AND inhparent = oid;
relname
---------
(0 rows)
-- Test that creat_chunk fails since chunk table already exists
\set ON_ERROR_STOP 0
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME');
ERROR: relation "_hyper_1_1_chunk" already exists
\set ON_ERROR_STOP 1
-- Test create_chunk_table on a hypertable where the chunk didn't exist before
DROP TABLE chunkapi;
DROP TABLE :CHUNK_SCHEMA.:CHUNK_NAME;
CREATE TABLE chunkapi(time timestamptz not null, device int, temp float);
SELECT * FROM create_hypertable('chunkapi', 'time', 'device', 2);
hypertable_id | schema_name | table_name | created
---------------+-------------+------------+---------
2 | public | chunkapi | t
(1 row)
SELECT count(*) FROM
_timescaledb_functions.create_chunk_table('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME');
count
-------
1
(1 row)
-- Demonstrate that current settings for dimensions don't affect create_chunk_table
DROP TABLE chunkapi;
DROP TABLE :CHUNK_SCHEMA.:CHUNK_NAME;
CREATE TABLE chunkapi (time timestamptz not null, device int, temp float);
SELECT * FROM create_hypertable('chunkapi', 'time', 'device', 2, '3d');
hypertable_id | schema_name | table_name | created
---------------+-------------+------------+---------
3 | public | chunkapi | t
(1 row)
SELECT count(*) FROM
_timescaledb_functions.create_chunk_table('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME');
count
-------
1
(1 row)
DROP TABLE chunkapi;
DROP TABLE :CHUNK_SCHEMA.:CHUNK_NAME;
CREATE TABLE chunkapi (time timestamptz not null, device int, temp float);
SELECT * FROM create_hypertable('chunkapi', 'time', 'device', 3);
hypertable_id | schema_name | table_name | created
---------------+-------------+------------+---------
4 | public | chunkapi | t
(1 row)
SELECT count(*) FROM
_timescaledb_functions.create_chunk_table('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME');
count
-------
1
(1 row)
-- Test create_chunk_table if a colliding chunk exists
DROP TABLE chunkapi;
DROP TABLE :CHUNK_SCHEMA.:CHUNK_NAME;
CREATE TABLE chunkapi (time timestamptz not null, device int, temp float);
SELECT * FROM create_hypertable('chunkapi', 'time', 'device', 3);
hypertable_id | schema_name | table_name | created
---------------+-------------+------------+---------
5 | public | chunkapi | t
(1 row)
INSERT INTO chunkapi VALUES ('2018-01-01 05:00:00-8', 1, 23.4);
\set ON_ERROR_STOP 0
SELECT _timescaledb_functions.create_chunk_table('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME');
ERROR: chunk table creation failed due to dimension slice collision
\set ON_ERROR_STOP 1
-- Test create_chunk_table when a chunk exists in different space partition and thus doesn't collide
DROP TABLE chunkapi;
CREATE TABLE chunkapi (time timestamptz not null, device int, temp float);
SELECT * FROM create_hypertable('chunkapi', 'time', 'device', 2);
hypertable_id | schema_name | table_name | created
---------------+-------------+------------+---------
6 | public | chunkapi | t
(1 row)
INSERT INTO chunkapi VALUES ('2018-01-01 05:00:00-8', 2, 23.4);
SELECT _timescaledb_functions.create_chunk_table('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME');
create_chunk_table
--------------------
t
(1 row)
-- Test create_chunk_table when a chunk exists in different time partition and thus doesn't collide
DROP TABLE chunkapi;
DROP TABLE :CHUNK_SCHEMA.:CHUNK_NAME;
CREATE TABLE chunkapi (time timestamptz not null, device int, temp float);
SELECT * FROM create_hypertable('chunkapi', 'time', 'device', 2);
hypertable_id | schema_name | table_name | created
---------------+-------------+------------+---------
7 | public | chunkapi | t
(1 row)
INSERT INTO chunkapi VALUES ('2018-02-01 05:00:00-8', 1, 23.4);
SELECT _timescaledb_functions.create_chunk_table('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME');
create_chunk_table
--------------------
t
(1 row)
-- Test create_chunk_table with tablespaces
\c :TEST_DBNAME :ROLE_SUPERUSER
SET client_min_messages = ERROR;
DROP TABLESPACE IF EXISTS tablespace1;
DROP TABLESPACE IF EXISTS tablespace2;
SET client_min_messages = NOTICE;
CREATE TABLESPACE tablespace1 OWNER :ROLE_DEFAULT_PERM_USER LOCATION :TEST_TABLESPACE1_PATH;
CREATE TABLESPACE tablespace2 OWNER :ROLE_DEFAULT_PERM_USER LOCATION :TEST_TABLESPACE2_PATH;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
-- Use the space partition to calculate the tablespace id to use
DROP TABLE chunkapi;
DROP TABLE :CHUNK_SCHEMA.:CHUNK_NAME;
CREATE TABLE chunkapi (time timestamptz not null, device int, temp float);
SELECT * FROM create_hypertable('chunkapi', 'time', 'device', 3);
hypertable_id | schema_name | table_name | created
---------------+-------------+------------+---------
8 | public | chunkapi | t
(1 row)
SELECT attach_tablespace('tablespace1', 'chunkapi');
attach_tablespace
-------------------
(1 row)
SELECT attach_tablespace('tablespace2', 'chunkapi');
attach_tablespace
-------------------
(1 row)
SELECT count(*) FROM
_timescaledb_functions.create_chunk_table('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME');
count
-------
1
(1 row)
SELECT tablespace FROM pg_tables WHERE tablename = :'CHUNK_NAME';
tablespace
-------------
tablespace1
(1 row)
-- Use the time partition to calculate the tablespace id to use
DROP TABLE chunkapi;
DROP TABLE :CHUNK_SCHEMA.:CHUNK_NAME;
CREATE TABLE devices (id int PRIMARY KEY);
INSERT INTO devices VALUES (1);
CREATE TABLE chunkapi (time timestamptz NOT NULL PRIMARY KEY, device int REFERENCES devices(id), temp float CHECK (temp > 0));
SELECT * FROM create_hypertable('chunkapi', 'time');
hypertable_id | schema_name | table_name | created
---------------+-------------+------------+---------
9 | public | chunkapi | t
(1 row)
INSERT INTO chunkapi VALUES ('2018-01-01 05:00:00-8', 1, 23.4);
SELECT chunk_schema AS "CHUNK_SCHEMA", chunk_name AS "CHUNK_NAME"
FROM timescaledb_information.chunks c
ORDER BY chunk_name DESC
LIMIT 1 \gset
SELECT slices AS "SLICES"
FROM _timescaledb_functions.show_chunk(:'CHUNK_SCHEMA'||'.'||:'CHUNK_NAME') \gset
-- Save the constraints info in a table for later comparison
CREATE TABLE original_chunk_constraints AS
SELECT "Constraint", "Type", "Columns", "Index"::text, "Expr", "Deferrable", "Deferred", "Validated"
FROM test.show_constraints(format('%I.%I', :'CHUNK_SCHEMA', :'CHUNK_NAME')::regclass);
-- Save constraints metadata
CREATE TABLE original_chunk_constraints_metadata AS
SELECT
chunk_id,
dimension_slice_id,
constraint_name,
hypertable_constraint_name
FROM _timescaledb_catalog.chunk_constraint con
INNER JOIN _timescaledb_catalog.chunk ch ON (con.chunk_id = ch.id)
WHERE ch.schema_name = :'CHUNK_SCHEMA' AND ch.table_name = :'CHUNK_NAME';
DROP TABLE :CHUNK_SCHEMA.:CHUNK_NAME;
SELECT attach_tablespace('tablespace1', 'chunkapi');
attach_tablespace
-------------------
(1 row)
SELECT attach_tablespace('tablespace2', 'chunkapi');
attach_tablespace
-------------------
(1 row)
SELECT count(*) FROM
_timescaledb_functions.create_chunk_table('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME');
count
-------
1
(1 row)
SELECT tablespace FROM pg_tables WHERE tablename = :'CHUNK_NAME';
tablespace
-------------
tablespace2
(1 row)
-- Now create the complete chunk from the chunk table
SELECT _timescaledb_functions.create_chunk('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME',
format('%I.%I', :'CHUNK_SCHEMA', :'CHUNK_NAME')::regclass);
create_chunk
-----------------------------------------------------------------------------------------------------
(8,9,_timescaledb_internal,_hyper_9_7_chunk,r,"{""time"": [1514419200000000, 1515024000000000]}",t)
(1 row)
-- Compare original and new constraints
SELECT * FROM original_chunk_constraints;
Constraint | Type | Columns | Index | Expr | Deferrable | Deferred | Validated
--------------------------+------+----------+-------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+------------+----------+-----------
7_1_chunkapi_device_fkey | f | {device} | devices_pkey | | f | f | t
7_2_chunkapi_pkey | p | {time} | _timescaledb_internal."7_2_chunkapi_pkey" | | f | f | t
chunkapi_temp_check | c | {temp} | - | (temp > (0)::double precision) | f | f | t
constraint_11 | c | {time} | - | (("time" >= 'Wed Dec 27 16:00:00 2017 PST'::timestamp with time zone) AND ("time" < 'Wed Jan 03 16:00:00 2018 PST'::timestamp with time zone)) | f | f | t
(4 rows)
SELECT * FROM test.show_constraints(format('%I.%I', :'CHUNK_SCHEMA', :'CHUNK_NAME')::regclass);
Constraint | Type | Columns | Index | Expr | Deferrable | Deferred | Validated
--------------------------+------+----------+-------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+------------+----------+-----------
8_3_chunkapi_device_fkey | f | {device} | devices_pkey | | f | f | t
8_4_chunkapi_pkey | p | {time} | _timescaledb_internal."8_4_chunkapi_pkey" | | f | f | t
chunkapi_temp_check | c | {temp} | - | (temp > (0)::double precision) | f | f | t
constraint_12 | c | {time} | - | (("time" >= 'Wed Dec 27 16:00:00 2017 PST'::timestamp with time zone) AND ("time" < 'Wed Jan 03 16:00:00 2018 PST'::timestamp with time zone)) | f | f | t
(4 rows)
-- Compare original and new chunk constraints metadata
SELECT * FROM original_chunk_constraints_metadata;
chunk_id | dimension_slice_id | constraint_name | hypertable_constraint_name
----------+--------------------+--------------------------+----------------------------
7 | | 7_1_chunkapi_device_fkey | chunkapi_device_fkey
7 | | 7_2_chunkapi_pkey | chunkapi_pkey
7 | 11 | constraint_11 |
(3 rows)
SELECT
chunk_id,
dimension_slice_id,
constraint_name,
hypertable_constraint_name
FROM _timescaledb_catalog.chunk_constraint con
INNER JOIN _timescaledb_catalog.chunk ch ON (con.chunk_id = ch.id)
WHERE ch.schema_name = :'CHUNK_SCHEMA' AND ch.table_name = :'CHUNK_NAME';
chunk_id | dimension_slice_id | constraint_name | hypertable_constraint_name
----------+--------------------+--------------------------+----------------------------
8 | | 8_3_chunkapi_device_fkey | chunkapi_device_fkey
8 | | 8_4_chunkapi_pkey | chunkapi_pkey
8 | 12 | constraint_12 |
(3 rows)
DROP TABLE original_chunk_constraints;
DROP TABLE original_chunk_constraints_metadata;
-- The chunk should inherit the hypertable
SELECT relname
FROM pg_catalog.pg_inherits, pg_class
WHERE inhrelid = (:'CHUNK_SCHEMA'||'.'||:'CHUNK_NAME')::regclass AND inhparent = oid;
relname
----------
chunkapi
(1 row)
-- Show chunk's attached to the table
SELECT
:'CHUNK_SCHEMA' AS expected_schema,
:'CHUNK_NAME' AS expected_table_name,
(_timescaledb_functions.show_chunk(ch)).*
FROM show_chunks('chunkapi') ch;
expected_schema | expected_table_name | chunk_id | hypertable_id | schema_name | table_name | relkind | slices
-----------------------+---------------------+----------+---------------+-----------------------+------------------+---------+------------------------------------------------
_timescaledb_internal | _hyper_9_7_chunk | 8 | 9 | _timescaledb_internal | _hyper_9_7_chunk | r | {"time": [1514419200000000, 1515024000000000]}
(1 row)
DROP TABLE chunkapi;
DROP TABLE devices;
-- Test creating a chunk from an existing chunk table which was not
-- created via create_chunk_table and having a different name.
CREATE TABLE devices (id int PRIMARY KEY);
INSERT INTO devices VALUES (1);
CREATE TABLE chunkapi (time timestamptz NOT NULL PRIMARY KEY, device int REFERENCES devices(id), temp float CHECK(temp > 0));
SELECT * FROM create_hypertable('chunkapi', 'time');
hypertable_id | schema_name | table_name | created
---------------+-------------+------------+---------
10 | public | chunkapi | t
(1 row)
CREATE TABLE newchunk (time timestamptz NOT NULL, device int, temp float);
SELECT * FROM test.show_constraints('newchunk');
Constraint | Type | Columns | Index | Expr | Deferrable | Deferred | Validated
------------+------+---------+-------+------+------------+----------+-----------
(0 rows)
INSERT INTO newchunk VALUES ('2018-01-01 05:00:00-8', 1, 23.4);
\set ON_ERROR_STOP 0
-- Creating the chunk without required CHECK constraints on a table
-- should fail. Currently, PostgreSQL only enforces presence of CHECK
-- constraints, but not foreign key, unique, or primary key
-- constraints. We should probably add checks to enforce the latter
-- too or auto-create all constraints.
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME', 'newchunk');
ERROR: child table is missing constraint "chunkapi_temp_check"
\set ON_ERROR_STOP 1
-- Add the missing CHECK constraint. Note that the name must be the
-- same as on the parent table.
ALTER TABLE newchunk ADD CONSTRAINT chunkapi_temp_check CHECK (temp > 0);
CREATE TABLE newchunk2 as select * from newchunk;
ALTER TABLE newchunk2 ADD CONSTRAINT chunkapi_temp_check CHECK (temp > 0);
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME', 'newchunk');
chunk_id | hypertable_id | schema_name | table_name | relkind | slices | created
----------+---------------+-----------------------+------------------+---------+------------------------------------------------+---------
10 | 10 | _timescaledb_internal | _hyper_9_7_chunk | r | {"time": [1514419200000000, 1515024000000000]} | t
(1 row)
-- adding an existing table to an exiting range must fail
\set ON_ERROR_STOP 0
SELECT * FROM _timescaledb_functions.create_chunk('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME', 'newchunk2');
ERROR: chunk creation failed due to collision
\set ON_ERROR_STOP 1
-- Show the chunk and that names are what we'd expect
SELECT
:'CHUNK_SCHEMA' AS expected_schema,
:'CHUNK_NAME' AS expected_table_name,
(_timescaledb_functions.show_chunk(ch)).*
FROM show_chunks('chunkapi') ch;
expected_schema | expected_table_name | chunk_id | hypertable_id | schema_name | table_name | relkind | slices
-----------------------+---------------------+----------+---------------+-----------------------+------------------+---------+------------------------------------------------
_timescaledb_internal | _hyper_9_7_chunk | 10 | 10 | _timescaledb_internal | _hyper_9_7_chunk | r | {"time": [1514419200000000, 1515024000000000]}
(1 row)
-- The chunk should inherit the hypertable
SELECT relname
FROM pg_catalog.pg_inherits, pg_class
WHERE inhrelid = (:'CHUNK_SCHEMA'||'.'||:'CHUNK_NAME')::regclass AND inhparent = oid;
relname
----------
chunkapi
(1 row)
-- Test that it is possible to query the data via the hypertable
SELECT * FROM chunkapi ORDER BY 1,2,3;
time | device | temp
------------------------------+--------+------
Mon Jan 01 05:00:00 2018 PST | 1 | 23.4
(1 row)
-- Show that the chunk has all the necessary constraints. These
-- include inheritable constraints and dimensional constraints, which
-- are specific to the chunk. Currently, foreign key, unique, and
-- primary key constraints are not inherited or auto-created.
SELECT * FROM test.show_constraints(format('%I.%I', :'CHUNK_SCHEMA', :'CHUNK_NAME')::regclass);
Constraint | Type | Columns | Index | Expr | Deferrable | Deferred | Validated
---------------------------+------+----------+--------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+------------+----------+-----------
10_7_chunkapi_device_fkey | f | {device} | devices_pkey | | f | f | t
10_8_chunkapi_pkey | p | {time} | _timescaledb_internal."10_8_chunkapi_pkey" | | f | f | t
chunkapi_temp_check | c | {temp} | - | (temp > (0)::double precision) | f | f | t
constraint_14 | c | {time} | - | (("time" >= 'Wed Dec 27 16:00:00 2017 PST'::timestamp with time zone) AND ("time" < 'Wed Jan 03 16:00:00 2018 PST'::timestamp with time zone)) | f | f | t
(4 rows)
DROP TABLE chunkapi;