mirror of
https://github.com/timescale/timescaledb.git
synced 2025-05-16 18:43:18 +08:00
The schema of base table on which hypertables are created, should define columns with proper data types. As per postgres best practices Wiki (https://wiki.postgresql.org/wiki/Don't_Do_This), one should not define columns with CHAR, VARCHAR, VARCHAR(N), instead use TEXT data type. Similarly instead of using timestamp, one should use timestamptz. This patch reports a WARNING to end user when creating hypertables, if underlying parent table, has columns of above mentioned data types. Fixes #4335
356 lines
16 KiB
Plaintext
356 lines
16 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
|
|
CREATE OR REPLACE FUNCTION ts_test_ddl_command_hook_reg() RETURNS VOID
|
|
AS :TSL_MODULE_PATHNAME, 'ts_test_ddl_command_hook_reg'
|
|
LANGUAGE C VOLATILE STRICT;
|
|
CREATE OR REPLACE FUNCTION ts_test_ddl_command_hook_unreg() RETURNS VOID
|
|
AS :TSL_MODULE_PATHNAME, 'ts_test_ddl_command_hook_unreg'
|
|
LANGUAGE C VOLATILE STRICT;
|
|
SET client_min_messages TO ERROR;
|
|
DROP SCHEMA IF EXISTS htable_schema CASCADE;
|
|
DROP TABLE IF EXISTS htable;
|
|
DROP TABLE IF EXISTS non_htable;
|
|
SET client_min_messages TO NOTICE;
|
|
CREATE SCHEMA htable_schema;
|
|
-- Install test hooks
|
|
SELECT ts_test_ddl_command_hook_reg();
|
|
ts_test_ddl_command_hook_reg
|
|
------------------------------
|
|
|
|
(1 row)
|
|
|
|
CREATE TABLE htable(time timestamptz, device int, color int CONSTRAINT color_check CHECK (color > 0), temp float);
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CREATE TABLE htable(time timestamptz, device int, color int CONSTRAINT color_check CHECK (color > 0), temp float);
|
|
NOTICE: test_ddl_command_end: CREATE TABLE
|
|
CREATE UNIQUE INDEX htable_pk ON htable(time);
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CREATE UNIQUE INDEX htable_pk ON htable(time);
|
|
NOTICE: test_ddl_command_end: CREATE INDEX
|
|
-- CREATE TABLE
|
|
SELECT * FROM create_hypertable('htable', 'time');
|
|
NOTICE: adding not-null constraint to column "time"
|
|
hypertable_id | schema_name | table_name | created
|
|
---------------+-------------+------------+---------
|
|
1 | public | htable | t
|
|
(1 row)
|
|
|
|
SELECT * FROM test.show_columns('htable');
|
|
Column | Type | NotNull
|
|
--------+--------------------------+---------
|
|
time | timestamp with time zone | t
|
|
device | integer | f
|
|
color | integer | f
|
|
temp | double precision | f
|
|
(4 rows)
|
|
|
|
SELECT * FROM test.show_constraints('htable');
|
|
Constraint | Type | Columns | Index | Expr | Deferrable | Deferred | Validated
|
|
-------------+------+---------+-------+-------------+------------+----------+-----------
|
|
color_check | c | {color} | - | (color > 0) | f | f | t
|
|
(1 row)
|
|
|
|
SELECT * FROM test.show_indexes('htable');
|
|
Index | Columns | Expr | Unique | Primary | Exclusion | Tablespace
|
|
-----------+---------+------+--------+---------+-----------+------------
|
|
htable_pk | {time} | | t | f | f |
|
|
(1 row)
|
|
|
|
-- ADD CONSTRAINT
|
|
ALTER TABLE htable ADD CONSTRAINT device_check CHECK (device > 0);
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: ALTER TABLE htable ADD CONSTRAINT device_check CHECK (device > 0);
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
NOTICE: test_ddl_command_end: ALTER TABLE
|
|
SELECT * FROM test.show_constraints('htable');
|
|
Constraint | Type | Columns | Index | Expr | Deferrable | Deferred | Validated
|
|
--------------+------+----------+-------+--------------+------------+----------+-----------
|
|
color_check | c | {color} | - | (color > 0) | f | f | t
|
|
device_check | c | {device} | - | (device > 0) | f | f | t
|
|
(2 rows)
|
|
|
|
-- DROP CONSTRAINT
|
|
ALTER TABLE htable DROP CONSTRAINT device_check;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: ALTER TABLE htable DROP CONSTRAINT device_check;
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
NOTICE: test_sql_drop: constraint: public.htable.device_check
|
|
NOTICE: test_ddl_command_end: ALTER TABLE
|
|
SELECT * FROM test.show_constraints('htable');
|
|
Constraint | Type | Columns | Index | Expr | Deferrable | Deferred | Validated
|
|
-------------+------+---------+-------+-------------+------------+----------+-----------
|
|
color_check | c | {color} | - | (color > 0) | f | f | t
|
|
(1 row)
|
|
|
|
-- DROP COLUMN
|
|
ALTER TABLE htable DROP COLUMN color;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: ALTER TABLE htable DROP COLUMN color;
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
NOTICE: test_sql_drop: constraint: public.htable.color_check
|
|
NOTICE: test_ddl_command_end: ALTER TABLE
|
|
SELECT * FROM test.show_columns('htable');
|
|
Column | Type | NotNull
|
|
--------+--------------------------+---------
|
|
time | timestamp with time zone | t
|
|
device | integer | f
|
|
temp | double precision | f
|
|
(3 rows)
|
|
|
|
-- ADD COLUMN
|
|
ALTER TABLE htable ADD COLUMN description text;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: ALTER TABLE htable ADD COLUMN description text;
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
NOTICE: test_ddl_command_end: ALTER TABLE
|
|
SELECT * FROM test.show_columns('htable');
|
|
Column | Type | NotNull
|
|
-------------+--------------------------+---------
|
|
time | timestamp with time zone | t
|
|
device | integer | f
|
|
temp | double precision | f
|
|
description | text | f
|
|
(4 rows)
|
|
|
|
-- CREATE INDEX
|
|
CREATE INDEX htable_description_idx ON htable (description);
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: CREATE INDEX htable_description_idx ON htable (description);
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
SELECT * FROM test.show_indexes('htable');
|
|
Index | Columns | Expr | Unique | Primary | Exclusion | Tablespace
|
|
------------------------+---------------+------+--------+---------+-----------+------------
|
|
htable_description_idx | {description} | | f | f | f |
|
|
htable_pk | {time} | | t | f | f |
|
|
(2 rows)
|
|
|
|
-- CREATE/DROP TRIGGER
|
|
CREATE OR REPLACE FUNCTION test_trigger()
|
|
RETURNS TRIGGER LANGUAGE PLPGSQL AS
|
|
$BODY$
|
|
BEGIN
|
|
RETURN OLD;
|
|
END
|
|
$BODY$;
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CREATE OR REPLACE FUNCTION test_trigger()
|
|
RETURNS TRIGGER LANGUAGE PLPGSQL AS
|
|
$BODY$
|
|
BEGIN
|
|
RETURN OLD;
|
|
END
|
|
$BODY$;
|
|
CREATE TRIGGER htable_trigger_test
|
|
BEFORE INSERT ON htable
|
|
FOR EACH ROW EXECUTE FUNCTION test_trigger();
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: CREATE TRIGGER htable_trigger_test
|
|
BEFORE INSERT ON htable
|
|
FOR EACH ROW EXECUTE FUNCTION test_trigger();
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
DROP TRIGGER htable_trigger_test on htable;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: DROP TRIGGER htable_trigger_test on htable;
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
NOTICE: test_sql_drop: trigger
|
|
DROP FUNCTION test_trigger();
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: DROP FUNCTION test_trigger();
|
|
-- CLUSTER, TRUNCATE, REINDEX, VACUUM (should not call event hooks)
|
|
CREATE TABLE non_htable (id int);
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CREATE TABLE non_htable (id int);
|
|
NOTICE: test_ddl_command_end: CREATE TABLE
|
|
CLUSTER htable USING htable_description_idx;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: CLUSTER htable USING htable_description_idx;
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
TRUNCATE non_htable, htable;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: TRUNCATE non_htable, htable;
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
REINDEX TABLE htable;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: REINDEX TABLE htable;
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
VACUUM htable;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: VACUUM htable;
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
-- ALTER TABLE
|
|
ALTER TABLE htable ADD CONSTRAINT temp_check CHECK (temp > 0.0);
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: ALTER TABLE htable ADD CONSTRAINT temp_check CHECK (temp > 0.0);
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
NOTICE: test_ddl_command_end: ALTER TABLE
|
|
SELECT * FROM test.show_constraints('htable');
|
|
Constraint | Type | Columns | Index | Expr | Deferrable | Deferred | Validated
|
|
------------+------+---------+-------+----------------------------------+------------+----------+-----------
|
|
temp_check | c | {temp} | - | (temp > (0.0)::double precision) | f | f | t
|
|
(1 row)
|
|
|
|
ALTER TABLE htable RENAME CONSTRAINT temp_check TO temp_chk;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: ALTER TABLE htable RENAME CONSTRAINT temp_check TO temp_chk;
|
|
NOTICE: test_ddl_command_start: wait for ddl_command_end
|
|
NOTICE: test_ddl_command_end: ALTER TABLE
|
|
NOTICE: test_ddl_command_end: 1 hypertables scheduled
|
|
NOTICE: test_ddl_command_end: public.htable
|
|
ALTER TABLE htable RENAME COLUMN description TO descr;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: ALTER TABLE htable RENAME COLUMN description TO descr;
|
|
NOTICE: test_ddl_command_start: wait for ddl_command_end
|
|
NOTICE: test_ddl_command_end: ALTER TABLE
|
|
NOTICE: test_ddl_command_end: 1 hypertables scheduled
|
|
NOTICE: test_ddl_command_end: public.htable
|
|
ALTER INDEX htable_description_idx RENAME to htable_descr_idx;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: ALTER INDEX htable_description_idx RENAME to htable_descr_idx;
|
|
NOTICE: test_ddl_command_start: wait for ddl_command_end
|
|
NOTICE: test_ddl_command_end: ALTER INDEX
|
|
NOTICE: test_ddl_command_end: 1 hypertables scheduled
|
|
NOTICE: test_ddl_command_end: public.htable
|
|
ALTER TABLE htable SET SCHEMA htable_schema;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: ALTER TABLE htable SET SCHEMA htable_schema;
|
|
NOTICE: test_ddl_command_start: wait for ddl_command_end
|
|
NOTICE: test_ddl_command_end: ALTER TABLE
|
|
NOTICE: test_ddl_command_end: 1 hypertables scheduled
|
|
NOTICE: test_ddl_command_end: htable_schema.htable
|
|
ALTER TABLE htable_schema.htable SET SCHEMA public;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: ALTER TABLE htable_schema.htable SET SCHEMA public;
|
|
NOTICE: test_ddl_command_start: wait for ddl_command_end
|
|
NOTICE: test_ddl_command_end: ALTER TABLE
|
|
NOTICE: test_ddl_command_end: 1 hypertables scheduled
|
|
NOTICE: test_ddl_command_end: public.htable
|
|
ALTER TABLE public.htable RENAME TO htable2;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: ALTER TABLE public.htable RENAME TO htable2;
|
|
NOTICE: test_ddl_command_start: wait for ddl_command_end
|
|
NOTICE: test_ddl_command_end: ALTER TABLE
|
|
NOTICE: test_ddl_command_end: 1 hypertables scheduled
|
|
NOTICE: test_ddl_command_end: public.htable2
|
|
ALTER TABLE htable2 RENAME TO htable;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: ALTER TABLE htable2 RENAME TO htable;
|
|
NOTICE: test_ddl_command_start: wait for ddl_command_end
|
|
NOTICE: test_ddl_command_end: ALTER TABLE
|
|
NOTICE: test_ddl_command_end: 1 hypertables scheduled
|
|
NOTICE: test_ddl_command_end: public.htable
|
|
-- DROP INDEX, TABLE
|
|
\set ON_ERROR_STOP 0
|
|
DROP INDEX htable_description_idx, htable_pk;
|
|
ERROR: cannot drop a hypertable index along with other objects
|
|
DROP TABLE htable, non_htable;
|
|
ERROR: cannot drop a hypertable along with other objects
|
|
\set ON_ERROR_STOP 1
|
|
DROP INDEX htable_descr_idx;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: DROP INDEX htable_descr_idx;
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
NOTICE: test_sql_drop: index
|
|
NOTICE: test_ddl_command_end: DROP INDEX
|
|
DROP TABLE htable;
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: DROP TABLE htable;
|
|
NOTICE: test_sql_drop: constraint: public.htable.temp_chk
|
|
NOTICE: test_sql_drop: index
|
|
NOTICE: test_sql_drop: index
|
|
NOTICE: test_sql_drop: table: public.htable
|
|
NOTICE: test_sql_drop: trigger
|
|
NOTICE: test_ddl_command_end: DROP TABLE
|
|
DROP TABLE non_htable;
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: DROP TABLE non_htable;
|
|
NOTICE: test_sql_drop: table: public.non_htable
|
|
NOTICE: test_ddl_command_end: DROP TABLE
|
|
-- DROP TABLE within procedure
|
|
CREATE TABLE test (time timestamp, v int);
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CREATE TABLE test (time timestamp, v int);
|
|
NOTICE: test_ddl_command_end: CREATE TABLE
|
|
SELECT create_hypertable('test','time');
|
|
WARNING: column type "timestamp without time zone" used for "time" does not follow best practices
|
|
NOTICE: adding not-null constraint to column "time"
|
|
create_hypertable
|
|
-------------------
|
|
(2,public,test,t)
|
|
(1 row)
|
|
|
|
CREATE PROCEDURE test_drop() LANGUAGE PLPGSQL AS $$
|
|
BEGIN
|
|
DROP TABLE test;
|
|
END
|
|
$$;
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CREATE PROCEDURE test_drop() LANGUAGE PLPGSQL AS $$
|
|
BEGIN
|
|
DROP TABLE test;
|
|
END
|
|
$$;
|
|
CALL test_drop();
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CALL test_drop();
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: DROP TABLE test
|
|
NOTICE: test_sql_drop: index
|
|
NOTICE: test_sql_drop: table: public.test
|
|
NOTICE: test_sql_drop: trigger
|
|
NOTICE: test_ddl_command_end: DROP TABLE
|
|
-- DROP CASCADE cases
|
|
-- DROP schema
|
|
CREATE TABLE htable_schema.non_htable (id int);
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CREATE TABLE htable_schema.non_htable (id int);
|
|
NOTICE: test_ddl_command_end: CREATE TABLE
|
|
CREATE TABLE htable_schema.htable(time timestamptz, device int, color int, temp float);
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CREATE TABLE htable_schema.htable(time timestamptz, device int, color int, temp float);
|
|
NOTICE: test_ddl_command_end: CREATE TABLE
|
|
SELECT * FROM create_hypertable('htable_schema.htable', 'time');
|
|
NOTICE: adding not-null constraint to column "time"
|
|
hypertable_id | schema_name | table_name | created
|
|
---------------+---------------+------------+---------
|
|
3 | htable_schema | htable | t
|
|
(1 row)
|
|
|
|
DROP SCHEMA htable_schema CASCADE;
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: DROP SCHEMA htable_schema CASCADE;
|
|
NOTICE: drop cascades to 2 other objects
|
|
NOTICE: test_sql_drop: index
|
|
NOTICE: test_sql_drop: table: htable_schema.non_htable
|
|
NOTICE: test_sql_drop: table: htable_schema.htable
|
|
NOTICE: test_sql_drop: schema: htable_schema
|
|
NOTICE: test_sql_drop: trigger
|
|
NOTICE: test_ddl_command_end: DROP SCHEMA
|
|
-- DROP column cascades to index drop
|
|
CREATE TABLE htable(time timestamptz, device int, color int, temp float);
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CREATE TABLE htable(time timestamptz, device int, color int, temp float);
|
|
NOTICE: test_ddl_command_end: CREATE TABLE
|
|
SELECT * FROM create_hypertable('htable', 'time');
|
|
NOTICE: adding not-null constraint to column "time"
|
|
hypertable_id | schema_name | table_name | created
|
|
---------------+-------------+------------+---------
|
|
4 | public | htable | t
|
|
(1 row)
|
|
|
|
CREATE INDEX htable_device_idx ON htable (device);
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: CREATE INDEX htable_device_idx ON htable (device);
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
ALTER TABLE htable DROP COLUMN device;
|
|
NOTICE: test_ddl_command_start: 1 hypertables, query: ALTER TABLE htable DROP COLUMN device;
|
|
NOTICE: test_ddl_command_start: public.htable
|
|
NOTICE: test_sql_drop: index
|
|
NOTICE: test_ddl_command_end: ALTER TABLE
|
|
DROP TABLE htable;
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: DROP TABLE htable;
|
|
NOTICE: test_sql_drop: index
|
|
NOTICE: test_sql_drop: table: public.htable
|
|
NOTICE: test_sql_drop: trigger
|
|
NOTICE: test_ddl_command_end: DROP TABLE
|
|
-- DROP foreign key
|
|
CREATE TABLE non_htable (id int PRIMARY KEY);
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CREATE TABLE non_htable (id int PRIMARY KEY);
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CREATE TABLE non_htable (id int PRIMARY KEY);
|
|
NOTICE: test_ddl_command_end: CREATE TABLE
|
|
CREATE TABLE htable(time timestamptz, device int REFERENCES non_htable(id));
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CREATE TABLE htable(time timestamptz, device int REFERENCES non_htable(id));
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: CREATE TABLE htable(time timestamptz, device int REFERENCES non_htable(id));
|
|
NOTICE: test_ddl_command_end: CREATE TABLE
|
|
SELECT * FROM create_hypertable('htable', 'time');
|
|
NOTICE: adding not-null constraint to column "time"
|
|
hypertable_id | schema_name | table_name | created
|
|
---------------+-------------+------------+---------
|
|
5 | public | htable | t
|
|
(1 row)
|
|
|
|
DROP TABLE non_htable CASCADE;
|
|
NOTICE: test_ddl_command_start: 0 hypertables, query: DROP TABLE non_htable CASCADE;
|
|
NOTICE: drop cascades to constraint htable_device_fkey on table htable
|
|
NOTICE: test_sql_drop: constraint: public.non_htable.non_htable_pkey
|
|
NOTICE: test_sql_drop: constraint: public.htable.htable_device_fkey
|
|
NOTICE: test_sql_drop: index
|
|
NOTICE: test_sql_drop: table: public.non_htable
|
|
NOTICE: test_sql_drop: trigger
|
|
NOTICE: test_sql_drop: trigger
|
|
NOTICE: test_sql_drop: trigger
|
|
NOTICE: test_sql_drop: trigger
|
|
NOTICE: test_ddl_command_end: DROP TABLE
|
|
-- cleanup
|
|
SELECT ts_test_ddl_command_hook_unreg();
|
|
ts_test_ddl_command_hook_unreg
|
|
--------------------------------
|
|
|
|
(1 row)
|
|
|