-- 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.
\ir include/insert_single.sql
-- 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.
CREATE TABLE PUBLIC."one_Partition" (
  "timeCustom" BIGINT NOT NULL,
  device_id TEXT NOT NULL,
  series_0 DOUBLE PRECISION NULL,
  series_1 DOUBLE PRECISION NULL,
  series_2 DOUBLE PRECISION NULL,
  series_bool BOOLEAN NULL
);
CREATE INDEX ON PUBLIC."one_Partition" (device_id, "timeCustom" DESC NULLS LAST) WHERE device_id IS NOT NULL;
CREATE INDEX ON PUBLIC."one_Partition" ("timeCustom" DESC NULLS LAST, series_0) WHERE series_0 IS NOT NULL;
CREATE INDEX ON PUBLIC."one_Partition" ("timeCustom" DESC NULLS LAST, series_1)  WHERE series_1 IS NOT NULL;
CREATE INDEX ON PUBLIC."one_Partition" ("timeCustom" DESC NULLS LAST, series_2) WHERE series_2 IS NOT NULL;
CREATE INDEX ON PUBLIC."one_Partition" ("timeCustom" DESC NULLS LAST, series_bool) WHERE series_bool IS NOT NULL;
\c :DBNAME :ROLE_SUPERUSER
CREATE SCHEMA "one_Partition" AUTHORIZATION :ROLE_DEFAULT_PERM_USER;
\c :DBNAME :ROLE_DEFAULT_PERM_USER;
SELECT * FROM create_hypertable('"public"."one_Partition"', 'timeCustom', associated_schema_name=>'one_Partition', chunk_time_interval=>_timescaledb_functions.interval_to_usec('1 month'));
 hypertable_id | schema_name |  table_name   | created 
---------------+-------------+---------------+---------
             1 | public      | one_Partition | t

--output command tags
\set QUIET off
BEGIN;
BEGIN
\COPY "one_Partition" FROM 'data/ds1_dev1_1.tsv' NULL AS '';
COPY 7
COMMIT;
COMMIT
INSERT INTO "one_Partition"("timeCustom", device_id, series_0, series_1) VALUES
(1257987600000000000, 'dev1', 1.5, 1),
(1257987600000000000, 'dev1', 1.5, 2),
(1257894000000000000, 'dev2', 1.5, 1),
(1257894002000000000, 'dev1', 2.5, 3);
INSERT 0 4
INSERT INTO "one_Partition"("timeCustom", device_id, series_0, series_1) VALUES
(1257894000000000000, 'dev2', 1.5, 2);
INSERT 0 1
\set QUIET on
SELECT * FROM test.show_columnsp('"one_Partition".%');
                                  Relation                                   | Kind |   Column    |   Column type    | NotNull 
-----------------------------------------------------------------------------+------+-------------+------------------+---------
 "one_Partition"._hyper_1_1_chunk                                            | r    | timeCustom  | bigint           | t
 "one_Partition"._hyper_1_1_chunk                                            | r    | device_id   | text             | t
 "one_Partition"._hyper_1_1_chunk                                            | r    | series_0    | double precision | f
 "one_Partition"._hyper_1_1_chunk                                            | r    | series_1    | double precision | f
 "one_Partition"._hyper_1_1_chunk                                            | r    | series_2    | double precision | f
 "one_Partition"._hyper_1_1_chunk                                            | r    | series_bool | boolean          | f
 "one_Partition"."_hyper_1_1_chunk_one_Partition_device_id_timeCustom_idx"   | i    | device_id   | text             | f
 "one_Partition"."_hyper_1_1_chunk_one_Partition_device_id_timeCustom_idx"   | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_1_chunk_one_Partition_timeCustom_idx"             | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_1_chunk_one_Partition_timeCustom_series_0_idx"    | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_1_chunk_one_Partition_timeCustom_series_0_idx"    | i    | series_0    | double precision | f
 "one_Partition"."_hyper_1_1_chunk_one_Partition_timeCustom_series_1_idx"    | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_1_chunk_one_Partition_timeCustom_series_1_idx"    | i    | series_1    | double precision | f
 "one_Partition"."_hyper_1_1_chunk_one_Partition_timeCustom_series_2_idx"    | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_1_chunk_one_Partition_timeCustom_series_2_idx"    | i    | series_2    | double precision | f
 "one_Partition"."_hyper_1_1_chunk_one_Partition_timeCustom_series_bool_idx" | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_1_chunk_one_Partition_timeCustom_series_bool_idx" | i    | series_bool | boolean          | f
 "one_Partition"._hyper_1_2_chunk                                            | r    | timeCustom  | bigint           | t
 "one_Partition"._hyper_1_2_chunk                                            | r    | device_id   | text             | t
 "one_Partition"._hyper_1_2_chunk                                            | r    | series_0    | double precision | f
 "one_Partition"._hyper_1_2_chunk                                            | r    | series_1    | double precision | f
 "one_Partition"._hyper_1_2_chunk                                            | r    | series_2    | double precision | f
 "one_Partition"._hyper_1_2_chunk                                            | r    | series_bool | boolean          | f
 "one_Partition"."_hyper_1_2_chunk_one_Partition_device_id_timeCustom_idx"   | i    | device_id   | text             | f
 "one_Partition"."_hyper_1_2_chunk_one_Partition_device_id_timeCustom_idx"   | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_2_chunk_one_Partition_timeCustom_idx"             | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_2_chunk_one_Partition_timeCustom_series_0_idx"    | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_2_chunk_one_Partition_timeCustom_series_0_idx"    | i    | series_0    | double precision | f
 "one_Partition"."_hyper_1_2_chunk_one_Partition_timeCustom_series_1_idx"    | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_2_chunk_one_Partition_timeCustom_series_1_idx"    | i    | series_1    | double precision | f
 "one_Partition"."_hyper_1_2_chunk_one_Partition_timeCustom_series_2_idx"    | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_2_chunk_one_Partition_timeCustom_series_2_idx"    | i    | series_2    | double precision | f
 "one_Partition"."_hyper_1_2_chunk_one_Partition_timeCustom_series_bool_idx" | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_2_chunk_one_Partition_timeCustom_series_bool_idx" | i    | series_bool | boolean          | f
 "one_Partition"._hyper_1_3_chunk                                            | r    | timeCustom  | bigint           | t
 "one_Partition"._hyper_1_3_chunk                                            | r    | device_id   | text             | t
 "one_Partition"._hyper_1_3_chunk                                            | r    | series_0    | double precision | f
 "one_Partition"._hyper_1_3_chunk                                            | r    | series_1    | double precision | f
 "one_Partition"._hyper_1_3_chunk                                            | r    | series_2    | double precision | f
 "one_Partition"._hyper_1_3_chunk                                            | r    | series_bool | boolean          | f
 "one_Partition"."_hyper_1_3_chunk_one_Partition_device_id_timeCustom_idx"   | i    | device_id   | text             | f
 "one_Partition"."_hyper_1_3_chunk_one_Partition_device_id_timeCustom_idx"   | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_3_chunk_one_Partition_timeCustom_idx"             | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_3_chunk_one_Partition_timeCustom_series_0_idx"    | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_3_chunk_one_Partition_timeCustom_series_0_idx"    | i    | series_0    | double precision | f
 "one_Partition"."_hyper_1_3_chunk_one_Partition_timeCustom_series_1_idx"    | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_3_chunk_one_Partition_timeCustom_series_1_idx"    | i    | series_1    | double precision | f
 "one_Partition"."_hyper_1_3_chunk_one_Partition_timeCustom_series_2_idx"    | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_3_chunk_one_Partition_timeCustom_series_2_idx"    | i    | series_2    | double precision | f
 "one_Partition"."_hyper_1_3_chunk_one_Partition_timeCustom_series_bool_idx" | i    | timeCustom  | bigint           | f
 "one_Partition"."_hyper_1_3_chunk_one_Partition_timeCustom_series_bool_idx" | i    | series_bool | boolean          | f

SELECT * FROM "one_Partition" ORDER BY "timeCustom", device_id, series_0, series_1, series_2;
     timeCustom      | device_id | series_0 | series_1 | series_2 | series_bool 
---------------------+-----------+----------+----------+----------+-------------
 1257894000000000000 | dev1      |      1.5 |        1 |        2 | t
 1257894000000000000 | dev1      |      1.5 |        2 |          | 
 1257894000000000000 | dev2      |      1.5 |        1 |          | 
 1257894000000000000 | dev2      |      1.5 |        2 |          | 
 1257894000000001000 | dev1      |      2.5 |        3 |          | 
 1257894001000000000 | dev1      |      3.5 |        4 |          | 
 1257894002000000000 | dev1      |      2.5 |        3 |          | 
 1257894002000000000 | dev1      |      5.5 |        6 |          | t
 1257894002000000000 | dev1      |      5.5 |        7 |          | f
 1257897600000000000 | dev1      |      4.5 |        5 |          | f
 1257987600000000000 | dev1      |      1.5 |        1 |          | 
 1257987600000000000 | dev1      |      1.5 |        2 |          | 

--test that we can insert data into a 1-dimensional table (only time partitioning)
CREATE TABLE "1dim"(time timestamp PRIMARY KEY, temp float);
SELECT create_hypertable('"1dim"', 'time');
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
 create_hypertable 
-------------------
 (2,public,1dim,t)

INSERT INTO "1dim" VALUES('2017-01-20T09:00:01', 22.5) RETURNING *;
           time           | temp 
--------------------------+------
 Fri Jan 20 09:00:01 2017 | 22.5

INSERT INTO "1dim" VALUES('2017-01-20T09:00:21', 21.2);
INSERT INTO "1dim" VALUES('2017-01-20T09:00:47', 25.1);
SELECT * FROM "1dim";
           time           | temp 
--------------------------+------
 Fri Jan 20 09:00:01 2017 | 22.5
 Fri Jan 20 09:00:21 2017 | 21.2
 Fri Jan 20 09:00:47 2017 | 25.1

CREATE TABLE regular_table (time timestamp, temp float);
INSERT INTO regular_table SELECT * FROM "1dim";
SELECT * FROM regular_table;
           time           | temp 
--------------------------+------
 Fri Jan 20 09:00:01 2017 | 22.5
 Fri Jan 20 09:00:21 2017 | 21.2
 Fri Jan 20 09:00:47 2017 | 25.1

TRUNCATE TABLE regular_table;
INSERT INTO regular_table VALUES('2017-01-20T09:00:59', 29.2);
INSERT INTO "1dim" SELECT * FROM regular_table;
SELECT * FROM "1dim";
           time           | temp 
--------------------------+------
 Fri Jan 20 09:00:01 2017 | 22.5
 Fri Jan 20 09:00:21 2017 | 21.2
 Fri Jan 20 09:00:47 2017 | 25.1
 Fri Jan 20 09:00:59 2017 | 29.2

SELECT "1dim" FROM "1dim";
               1dim                
-----------------------------------
 ("Fri Jan 20 09:00:01 2017",22.5)
 ("Fri Jan 20 09:00:21 2017",21.2)
 ("Fri Jan 20 09:00:47 2017",25.1)
 ("Fri Jan 20 09:00:59 2017",29.2)

--test that we can insert pre-1970 dates
CREATE TABLE "1dim_pre1970"(time timestamp PRIMARY KEY, temp float);
SELECT create_hypertable('"1dim_pre1970"', 'time', chunk_time_interval=> INTERVAL '1 Month');
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
     create_hypertable     
---------------------------
 (3,public,1dim_pre1970,t)

INSERT INTO "1dim_pre1970" VALUES('1969-12-01T19:00:00', 21.2);
INSERT INTO "1dim_pre1970" VALUES('1969-12-20T09:00:00', 25.1);
INSERT INTO "1dim_pre1970" VALUES('1970-01-20T09:00:00', 26.6);
INSERT INTO "1dim_pre1970" VALUES('1969-02-20T09:00:00', 29.9);
--should show warning
BEGIN;
CREATE TABLE "1dim_usec_interval"(time timestamp PRIMARY KEY, temp float);
SELECT create_hypertable('"1dim_usec_interval"', 'time', chunk_time_interval=> 10);
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
WARNING:  unexpected interval: smaller than one second
        create_hypertable        
---------------------------------
 (4,public,1dim_usec_interval,t)

INSERT INTO "1dim_usec_interval" VALUES('1969-12-01T19:00:00', 21.2);
ROLLBACK;
CREATE TABLE "1dim_usec_interval"(time timestamp PRIMARY KEY, temp float);
SELECT create_hypertable('"1dim_usec_interval"', 'time', chunk_time_interval=> 1000000);
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
        create_hypertable        
---------------------------------
 (5,public,1dim_usec_interval,t)

INSERT INTO "1dim_usec_interval" VALUES('1969-12-01T19:00:00', 21.2);
CREATE TABLE "1dim_neg"(time INTEGER, temp float);
SELECT create_hypertable('"1dim_neg"', 'time', chunk_time_interval=>10);
   create_hypertable   
-----------------------
 (6,public,1dim_neg,t)

INSERT INTO "1dim_neg" VALUES (-20, 21.2);
INSERT INTO "1dim_neg" VALUES (-19, 21.2);
INSERT INTO "1dim_neg" VALUES (-1, 21.2);
INSERT INTO "1dim_neg" VALUES (0, 21.2);
INSERT INTO "1dim_neg" VALUES (1, 21.2);
INSERT INTO "1dim_neg" VALUES (19, 21.2);
INSERT INTO "1dim_neg" VALUES (20, 21.2);
SELECT * FROM "1dim_pre1970";
           time           | temp 
--------------------------+------
 Mon Dec 01 19:00:00 1969 | 21.2
 Sat Dec 20 09:00:00 1969 | 25.1
 Tue Jan 20 09:00:00 1970 | 26.6
 Thu Feb 20 09:00:00 1969 | 29.9

SELECT * FROM "1dim_neg";
 time | temp 
------+------
  -20 | 21.2
  -19 | 21.2
   -1 | 21.2
    0 | 21.2
    1 | 21.2
   19 | 21.2
   20 | 21.2

SELECT id, hypertable_id, schema_name, table_name, compressed_chunk_id, dropped, status, osm_chunk FROM _timescaledb_catalog.chunk;
 id | hypertable_id |      schema_name      |    table_name     | compressed_chunk_id | dropped | status | osm_chunk 
----+---------------+-----------------------+-------------------+---------------------+---------+--------+-----------
  1 |             1 | one_Partition         | _hyper_1_1_chunk  |                     | f       |      0 | f
  2 |             1 | one_Partition         | _hyper_1_2_chunk  |                     | f       |      0 | f
  3 |             1 | one_Partition         | _hyper_1_3_chunk  |                     | f       |      0 | f
  4 |             2 | _timescaledb_internal | _hyper_2_4_chunk  |                     | f       |      0 | f
  5 |             3 | _timescaledb_internal | _hyper_3_5_chunk  |                     | f       |      0 | f
  6 |             3 | _timescaledb_internal | _hyper_3_6_chunk  |                     | f       |      0 | f
  7 |             3 | _timescaledb_internal | _hyper_3_7_chunk  |                     | f       |      0 | f
  8 |             3 | _timescaledb_internal | _hyper_3_8_chunk  |                     | f       |      0 | f
 10 |             5 | _timescaledb_internal | _hyper_5_10_chunk |                     | f       |      0 | f
 11 |             6 | _timescaledb_internal | _hyper_6_11_chunk |                     | f       |      0 | f
 12 |             6 | _timescaledb_internal | _hyper_6_12_chunk |                     | f       |      0 | f
 13 |             6 | _timescaledb_internal | _hyper_6_13_chunk |                     | f       |      0 | f
 14 |             6 | _timescaledb_internal | _hyper_6_14_chunk |                     | f       |      0 | f
 15 |             6 | _timescaledb_internal | _hyper_6_15_chunk |                     | f       |      0 | f

SELECT * FROM _timescaledb_catalog.dimension_slice;
 id | dimension_id |     range_start     |      range_end      
----+--------------+---------------------+---------------------
  1 |            1 | 1257892416000000000 | 1257895008000000000
  2 |            1 | 1257897600000000000 | 1257900192000000000
  3 |            1 | 1257985728000000000 | 1257988320000000000
  4 |            2 |    1484784000000000 |    1485388800000000
  5 |            3 |      -5184000000000 |      -2592000000000
  6 |            3 |      -2592000000000 |                   0
  7 |            3 |                   0 |       2592000000000
  8 |            3 |     -28512000000000 |     -25920000000000
 10 |            5 |      -2610000000000 |      -2609999000000
 11 |            6 |                 -20 |                 -10
 12 |            6 |                 -10 |                   0
 13 |            6 |                   0 |                  10
 14 |            6 |                  10 |                  20
 15 |            6 |                  20 |                  30

-- Create a three-dimensional table
CREATE TABLE "3dim" (time timestamp, temp float, device text, location text);
SELECT create_hypertable('"3dim"', 'time', 'device', 2);
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
 create_hypertable 
-------------------
 (7,public,3dim,t)

SELECT add_dimension('"3dim"', 'location', 2);
       add_dimension        
----------------------------
 (9,public,3dim,location,t)

INSERT INTO "3dim" VALUES('2017-01-20T09:00:01', 22.5, 'blue', 'nyc');
INSERT INTO "3dim" VALUES('2017-01-20T09:00:21', 21.2, 'brown', 'sthlm');
INSERT INTO "3dim" VALUES('2017-01-20T09:00:47', 25.1, 'yellow', 'la');
--show the constraints on the three-dimensional chunk
SELECT * FROM test.show_constraints('_timescaledb_internal._hyper_7_16_chunk');
  Constraint   | Type |  Columns   | Index |                                                                     Expr                                                                     | Deferrable | Deferred | Validated 
---------------+------+------------+-------+----------------------------------------------------------------------------------------------------------------------------------------------+------------+----------+-----------
 constraint_16 | c    | {time}     | -     | (("time" >= 'Thu Jan 19 00:00:00 2017'::timestamp without time zone) AND ("time" < 'Thu Jan 26 00:00:00 2017'::timestamp without time zone)) | f          | f        | t
 constraint_17 | c    | {device}   | -     | (_timescaledb_functions.get_partition_hash(device) < 1073741823)                                                                             | f          | f        | t
 constraint_18 | c    | {location} | -     | (_timescaledb_functions.get_partition_hash(location) >= 1073741823)                                                                          | f          | f        | t

--queries should work in three dimensions
SELECT * FROM "3dim";
           time           | temp | device | location 
--------------------------+------+--------+----------
 Fri Jan 20 09:00:01 2017 | 22.5 | blue   | nyc
 Fri Jan 20 09:00:47 2017 | 25.1 | yellow | la
 Fri Jan 20 09:00:21 2017 | 21.2 | brown  | sthlm

-- test that explain works
EXPLAIN (BUFFERS FALSE, COSTS FALSE)
INSERT INTO "3dim" VALUES('2017-01-21T09:00:01', 32.9, 'green', 'nyc'),
                         ('2017-01-21T09:00:47', 27.3, 'purple', 'la') RETURNING *;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable)
   ->  Insert on "3dim"
         ->  Values Scan on "*VALUES*"

EXPLAIN (BUFFERS FALSE, COSTS FALSE)
WITH "3dim_insert" AS (
     INSERT INTO "3dim" VALUES('2017-01-21T09:01:44', 19.3, 'black', 'la') RETURNING time, temp
), regular_insert AS (
   INSERT INTO regular_table VALUES('2017-01-21T10:00:51', 14.3) RETURNING time, temp
) INSERT INTO "1dim" (SELECT time, temp FROM "3dim_insert" UNION SELECT time, temp FROM regular_insert);
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable)
   CTE 3dim_insert
     ->  Custom Scan (ModifyHypertable)
           ->  Insert on "3dim"
                 ->  Result
   CTE regular_insert
     ->  Insert on regular_table
           ->  Result
   ->  Insert on "1dim"
         ->  Unique
               ->  Sort
                     Sort Key: "3dim_insert"."time", "3dim_insert".temp
                     ->  Append
                           ->  CTE Scan on "3dim_insert"
                           ->  CTE Scan on regular_insert

-- test prepared statement INSERT
PREPARE "1dim_plan" (timestamp, float) AS
INSERT INTO "1dim" VALUES($1, $2) ON CONFLICT (time) DO NOTHING;
EXECUTE "1dim_plan" ('2017-04-17 23:35', 31.4);
EXECUTE "1dim_plan" ('2017-04-17 23:35', 32.6);
-- test prepared statement with generic plan (forced when no parameters)
PREPARE "1dim_plan_generic" AS
INSERT INTO "1dim" VALUES('2017-05-18 17:24', 18.3);
EXECUTE "1dim_plan_generic";
SELECT * FROM "1dim" ORDER BY time;
           time           | temp 
--------------------------+------
 Fri Jan 20 09:00:01 2017 | 22.5
 Fri Jan 20 09:00:21 2017 | 21.2
 Fri Jan 20 09:00:47 2017 | 25.1
 Fri Jan 20 09:00:59 2017 | 29.2
 Mon Apr 17 23:35:00 2017 | 31.4
 Thu May 18 17:24:00 2017 | 18.3

SELECT * FROM "3dim" ORDER BY (time, device);
           time           | temp | device | location 
--------------------------+------+--------+----------
 Fri Jan 20 09:00:01 2017 | 22.5 | blue   | nyc
 Fri Jan 20 09:00:21 2017 | 21.2 | brown  | sthlm
 Fri Jan 20 09:00:47 2017 | 25.1 | yellow | la

-- Test large intervals as default interval for integer is
-- supported as part of hypertable generalization
\set ON_ERROR_STOP 0
CREATE TABLE "inttime_err"(time INTEGER PRIMARY KEY, temp float);
SELECT create_hypertable('"inttime_err"', 'time', chunk_time_interval=>2147483648);
ERROR:  invalid interval: must be between 1 and 2147483647
\set ON_ERROR_STOP 1
SELECT create_hypertable('"inttime_err"', 'time', chunk_time_interval=>2147483647);
    create_hypertable     
--------------------------
 (8,public,inttime_err,t)

-- Test large intervals as default interval is supported
-- for integer types as part of hypertable generalization.
\set ON_ERROR_STOP 0
CREATE TABLE "smallinttime_err"(time SMALLINT PRIMARY KEY, temp float);
SELECT create_hypertable('"smallinttime_err"', 'time', chunk_time_interval=>32768);
ERROR:  invalid interval: must be between 1 and 32767
\set ON_ERROR_STOP 1
SELECT create_hypertable('"smallinttime_err"', 'time', chunk_time_interval=>32767);
       create_hypertable       
-------------------------------
 (9,public,smallinttime_err,t)

--make sure date inserts work even when the timezone changes the
CREATE TABLE hyper_date(time date, temp float);
SELECT create_hypertable('"hyper_date"', 'time');
    create_hypertable     
--------------------------
 (10,public,hyper_date,t)

SET timezone=+1;
INSERT INTO "hyper_date" VALUES('2011-01-26', 22.5);
RESET timezone;
--make sure timestamp inserts work even when the timezone changes the
SET timezone = 'UTC';
CREATE TABLE "test_tz"(time timestamp PRIMARY KEY, temp float);
SELECT create_hypertable('"test_tz"', 'time', chunk_time_interval=> INTERVAL '1 day');
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
   create_hypertable   
-----------------------
 (11,public,test_tz,t)

INSERT INTO "test_tz" VALUES('2017-09-22 10:00:00', 21.2);
INSERT INTO "test_tz" VALUES('2017-09-21 19:00:00', 21.2);
SET timezone = 'US/central';
INSERT INTO "test_tz" VALUES('2017-09-21 19:01:00', 21.2);
SELECT * FROM test.show_constraints('_timescaledb_internal._hyper_10_20_chunk');
  Constraint   | Type | Columns | Index |                                Expr                                | Deferrable | Deferred | Validated 
---------------+------+---------+-------+--------------------------------------------------------------------+------------+----------+-----------
 constraint_23 | c    | {time}  | -     | (("time" >= '01-20-2011'::date) AND ("time" < '01-27-2011'::date)) | f          | f        | t

SELECT * FROM test_tz;
           time           | temp 
--------------------------+------
 Fri Sep 22 10:00:00 2017 | 21.2
 Thu Sep 21 19:00:00 2017 | 21.2
 Thu Sep 21 19:01:00 2017 | 21.2

-- test various memory settings --
SET timescaledb.max_open_chunks_per_insert = 10;
SET timescaledb.max_cached_chunks_per_hypertable = 10;
CREATE TABLE "nondefault_mem_settings"(time timestamp PRIMARY KEY, temp float);
SELECT create_hypertable('"nondefault_mem_settings"', 'time', chunk_time_interval=> INTERVAL '1 Month');
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
           create_hypertable           
---------------------------------------
 (12,public,nondefault_mem_settings,t)

INSERT INTO "nondefault_mem_settings" VALUES('2000-12-01T19:00:00', 21.2);
INSERT INTO "nondefault_mem_settings" VALUES('2001-12-20T09:00:00', 25.1);
--lowest possible
SET timescaledb.max_open_chunks_per_insert = 1;
SET timescaledb.max_cached_chunks_per_hypertable = 1;
INSERT INTO "nondefault_mem_settings" VALUES
('2001-01-20T09:00:00', 26.6),
('2002-02-20T09:00:00', 27.9),
('2003-02-20T09:00:00', 28.9);
INSERT INTO "nondefault_mem_settings" VALUES
('2001-03-20T09:00:00', 30.6),
('2002-03-20T09:00:00', 31.9),
('2003-03-20T09:00:00', 32.9);
--warning about mismatched cache sizes
SET timescaledb.max_open_chunks_per_insert = 100;
WARNING:  insert cache size is larger than hypertable chunk cache size
SET timescaledb.max_cached_chunks_per_hypertable = 10;
WARNING:  insert cache size is larger than hypertable chunk cache size
INSERT INTO "nondefault_mem_settings" VALUES
('2001-05-20T09:00:00', 36.6),
('2002-05-20T09:00:00', 37.9),
('2003-05-20T09:00:00', 38.9);
--unlimited
SET timescaledb.max_open_chunks_per_insert = 0;
SET timescaledb.max_cached_chunks_per_hypertable = 0;
INSERT INTO "nondefault_mem_settings" VALUES
('2001-04-20T09:00:00', 33.6),
('2002-04-20T09:00:00', 34.9),
('2003-04-20T09:00:00', 35.9);
SELECT * FROM "nondefault_mem_settings";
           time           | temp 
--------------------------+------
 Fri Dec 01 19:00:00 2000 | 21.2
 Thu Dec 20 09:00:00 2001 | 25.1
 Sat Jan 20 09:00:00 2001 | 26.6
 Wed Feb 20 09:00:00 2002 | 27.9
 Thu Feb 20 09:00:00 2003 | 28.9
 Tue Mar 20 09:00:00 2001 | 30.6
 Wed Mar 20 09:00:00 2002 | 31.9
 Thu Mar 20 09:00:00 2003 | 32.9
 Sun May 20 09:00:00 2001 | 36.6
 Mon May 20 09:00:00 2002 | 37.9
 Tue May 20 09:00:00 2003 | 38.9
 Fri Apr 20 09:00:00 2001 | 33.6
 Sat Apr 20 09:00:00 2002 | 34.9
 Sun Apr 20 09:00:00 2003 | 35.9

--test rollback
BEGIN;
\set QUIET off
CREATE TABLE "data_records" ("time" bigint NOT NULL, "value" integer CHECK (VALUE >= 0));
CREATE TABLE
SELECT create_hypertable('data_records', 'time', chunk_time_interval => 2592000000);
     create_hypertable      
----------------------------
 (13,public,data_records,t)

INSERT INTO "data_records" ("time", "value") VALUES (0, 1);
INSERT 0 1
SAVEPOINT savepoint_1;
SAVEPOINT
INSERT INTO "data_records" ("time", "value") VALUES (1, 0);
INSERT 0 1
ROLLBACK TO SAVEPOINT savepoint_1;
ROLLBACK
INSERT INTO "data_records" ("time", "value") VALUES (2, 1);
INSERT 0 1
SAVEPOINT savepoint_2;
SAVEPOINT
\set ON_ERROR_STOP 0
INSERT INTO "data_records" ("time", "value") VALUES (3, -1);
ERROR:  new row for relation "_hyper_13_37_chunk" violates check constraint "data_records_value_check"
\set ON_ERROR_STOP 1
ROLLBACK TO SAVEPOINT savepoint_2;
ROLLBACK
INSERT INTO "data_records" ("time", "value") VALUES (4, 1);
INSERT 0 1
SAVEPOINT savepoint_3;
SAVEPOINT
INSERT INTO "data_records" ("time", "value") VALUES (5, 0);
INSERT 0 1
ROLLBACK TO SAVEPOINT savepoint_3;
ROLLBACK
SELECT * FROM data_records;
 time | value 
------+-------
    0 |     1
    2 |     1
    4 |     1

\set QUIET on
ROLLBACK;
