-- 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.
\o /dev/null
\ir include/insert_two_partitions.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."two_Partitions" (
  "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."two_Partitions" (device_id, "timeCustom" DESC NULLS LAST) WHERE device_id IS NOT NULL;
CREATE INDEX ON PUBLIC."two_Partitions" ("timeCustom" DESC NULLS LAST, series_0) WHERE series_0 IS NOT NULL;
CREATE INDEX ON PUBLIC."two_Partitions" ("timeCustom" DESC NULLS LAST, series_1)  WHERE series_1 IS NOT NULL;
CREATE INDEX ON PUBLIC."two_Partitions" ("timeCustom" DESC NULLS LAST, series_2) WHERE series_2 IS NOT NULL;
CREATE INDEX ON PUBLIC."two_Partitions" ("timeCustom" DESC NULLS LAST, series_bool) WHERE series_bool IS NOT NULL;
CREATE INDEX ON PUBLIC."two_Partitions" ("timeCustom" DESC NULLS LAST, device_id);
SELECT * FROM create_hypertable('"public"."two_Partitions"'::regclass, 'timeCustom'::name, 'device_id'::name, associated_schema_name=>'_timescaledb_internal'::text, number_partitions => 2, chunk_time_interval=>_timescaledb_functions.interval_to_usec('1 month'));
\set QUIET off
BEGIN;
\COPY public."two_Partitions" FROM 'data/ds1_dev1_1.tsv' NULL AS '';
COMMIT;
INSERT INTO public."two_Partitions"("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 INTO "two_Partitions"("timeCustom", device_id, series_0, series_1) VALUES
(1257894000000000000, 'dev2', 1.5, 2);
\set QUIET on
\o
SELECT * FROM test.show_columnsp('_timescaledb_internal.%_hyper%');
                                      Relation                                      | Kind |   Column    |   Column type    | NotNull 
------------------------------------------------------------------------------------+------+-------------+------------------+---------
 _timescaledb_internal._hyper_1_1_chunk                                             | r    | timeCustom  | bigint           | t
 _timescaledb_internal._hyper_1_1_chunk                                             | r    | device_id   | text             | t
 _timescaledb_internal._hyper_1_1_chunk                                             | r    | series_0    | double precision | f
 _timescaledb_internal._hyper_1_1_chunk                                             | r    | series_1    | double precision | f
 _timescaledb_internal._hyper_1_1_chunk                                             | r    | series_2    | double precision | f
 _timescaledb_internal._hyper_1_1_chunk                                             | r    | series_bool | boolean          | f
 _timescaledb_internal."_hyper_1_1_chunk_two_Partitions_device_id_timeCustom_idx"   | i    | device_id   | text             | f
 _timescaledb_internal."_hyper_1_1_chunk_two_Partitions_device_id_timeCustom_idx"   | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_1_chunk_two_Partitions_timeCustom_device_id_idx"   | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_1_chunk_two_Partitions_timeCustom_device_id_idx"   | i    | device_id   | text             | f
 _timescaledb_internal."_hyper_1_1_chunk_two_Partitions_timeCustom_idx"             | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_1_chunk_two_Partitions_timeCustom_series_0_idx"    | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_1_chunk_two_Partitions_timeCustom_series_0_idx"    | i    | series_0    | double precision | f
 _timescaledb_internal."_hyper_1_1_chunk_two_Partitions_timeCustom_series_1_idx"    | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_1_chunk_two_Partitions_timeCustom_series_1_idx"    | i    | series_1    | double precision | f
 _timescaledb_internal."_hyper_1_1_chunk_two_Partitions_timeCustom_series_2_idx"    | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_1_chunk_two_Partitions_timeCustom_series_2_idx"    | i    | series_2    | double precision | f
 _timescaledb_internal."_hyper_1_1_chunk_two_Partitions_timeCustom_series_bool_idx" | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_1_chunk_two_Partitions_timeCustom_series_bool_idx" | i    | series_bool | boolean          | f
 _timescaledb_internal._hyper_1_2_chunk                                             | r    | timeCustom  | bigint           | t
 _timescaledb_internal._hyper_1_2_chunk                                             | r    | device_id   | text             | t
 _timescaledb_internal._hyper_1_2_chunk                                             | r    | series_0    | double precision | f
 _timescaledb_internal._hyper_1_2_chunk                                             | r    | series_1    | double precision | f
 _timescaledb_internal._hyper_1_2_chunk                                             | r    | series_2    | double precision | f
 _timescaledb_internal._hyper_1_2_chunk                                             | r    | series_bool | boolean          | f
 _timescaledb_internal."_hyper_1_2_chunk_two_Partitions_device_id_timeCustom_idx"   | i    | device_id   | text             | f
 _timescaledb_internal."_hyper_1_2_chunk_two_Partitions_device_id_timeCustom_idx"   | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_2_chunk_two_Partitions_timeCustom_device_id_idx"   | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_2_chunk_two_Partitions_timeCustom_device_id_idx"   | i    | device_id   | text             | f
 _timescaledb_internal."_hyper_1_2_chunk_two_Partitions_timeCustom_idx"             | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_2_chunk_two_Partitions_timeCustom_series_0_idx"    | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_2_chunk_two_Partitions_timeCustom_series_0_idx"    | i    | series_0    | double precision | f
 _timescaledb_internal."_hyper_1_2_chunk_two_Partitions_timeCustom_series_1_idx"    | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_2_chunk_two_Partitions_timeCustom_series_1_idx"    | i    | series_1    | double precision | f
 _timescaledb_internal."_hyper_1_2_chunk_two_Partitions_timeCustom_series_2_idx"    | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_2_chunk_two_Partitions_timeCustom_series_2_idx"    | i    | series_2    | double precision | f
 _timescaledb_internal."_hyper_1_2_chunk_two_Partitions_timeCustom_series_bool_idx" | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_2_chunk_two_Partitions_timeCustom_series_bool_idx" | i    | series_bool | boolean          | f
 _timescaledb_internal._hyper_1_3_chunk                                             | r    | timeCustom  | bigint           | t
 _timescaledb_internal._hyper_1_3_chunk                                             | r    | device_id   | text             | t
 _timescaledb_internal._hyper_1_3_chunk                                             | r    | series_0    | double precision | f
 _timescaledb_internal._hyper_1_3_chunk                                             | r    | series_1    | double precision | f
 _timescaledb_internal._hyper_1_3_chunk                                             | r    | series_2    | double precision | f
 _timescaledb_internal._hyper_1_3_chunk                                             | r    | series_bool | boolean          | f
 _timescaledb_internal."_hyper_1_3_chunk_two_Partitions_device_id_timeCustom_idx"   | i    | device_id   | text             | f
 _timescaledb_internal."_hyper_1_3_chunk_two_Partitions_device_id_timeCustom_idx"   | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_3_chunk_two_Partitions_timeCustom_device_id_idx"   | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_3_chunk_two_Partitions_timeCustom_device_id_idx"   | i    | device_id   | text             | f
 _timescaledb_internal."_hyper_1_3_chunk_two_Partitions_timeCustom_idx"             | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_3_chunk_two_Partitions_timeCustom_series_0_idx"    | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_3_chunk_two_Partitions_timeCustom_series_0_idx"    | i    | series_0    | double precision | f
 _timescaledb_internal."_hyper_1_3_chunk_two_Partitions_timeCustom_series_1_idx"    | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_3_chunk_two_Partitions_timeCustom_series_1_idx"    | i    | series_1    | double precision | f
 _timescaledb_internal."_hyper_1_3_chunk_two_Partitions_timeCustom_series_2_idx"    | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_3_chunk_two_Partitions_timeCustom_series_2_idx"    | i    | series_2    | double precision | f
 _timescaledb_internal."_hyper_1_3_chunk_two_Partitions_timeCustom_series_bool_idx" | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_3_chunk_two_Partitions_timeCustom_series_bool_idx" | i    | series_bool | boolean          | f
 _timescaledb_internal._hyper_1_4_chunk                                             | r    | timeCustom  | bigint           | t
 _timescaledb_internal._hyper_1_4_chunk                                             | r    | device_id   | text             | t
 _timescaledb_internal._hyper_1_4_chunk                                             | r    | series_0    | double precision | f
 _timescaledb_internal._hyper_1_4_chunk                                             | r    | series_1    | double precision | f
 _timescaledb_internal._hyper_1_4_chunk                                             | r    | series_2    | double precision | f
 _timescaledb_internal._hyper_1_4_chunk                                             | r    | series_bool | boolean          | f
 _timescaledb_internal."_hyper_1_4_chunk_two_Partitions_device_id_timeCustom_idx"   | i    | device_id   | text             | f
 _timescaledb_internal."_hyper_1_4_chunk_two_Partitions_device_id_timeCustom_idx"   | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_4_chunk_two_Partitions_timeCustom_device_id_idx"   | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_4_chunk_two_Partitions_timeCustom_device_id_idx"   | i    | device_id   | text             | f
 _timescaledb_internal."_hyper_1_4_chunk_two_Partitions_timeCustom_idx"             | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_4_chunk_two_Partitions_timeCustom_series_0_idx"    | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_4_chunk_two_Partitions_timeCustom_series_0_idx"    | i    | series_0    | double precision | f
 _timescaledb_internal."_hyper_1_4_chunk_two_Partitions_timeCustom_series_1_idx"    | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_4_chunk_two_Partitions_timeCustom_series_1_idx"    | i    | series_1    | double precision | f
 _timescaledb_internal."_hyper_1_4_chunk_two_Partitions_timeCustom_series_2_idx"    | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_4_chunk_two_Partitions_timeCustom_series_2_idx"    | i    | series_2    | double precision | f
 _timescaledb_internal."_hyper_1_4_chunk_two_Partitions_timeCustom_series_bool_idx" | i    | timeCustom  | bigint           | f
 _timescaledb_internal."_hyper_1_4_chunk_two_Partitions_timeCustom_series_bool_idx" | i    | series_bool | boolean          | f

-- Test that renaming hypertable works
SELECT * FROM test.show_columns('_timescaledb_internal._hyper_1_1_chunk');
   Column    |       Type       | NotNull 
-------------+------------------+---------
 timeCustom  | bigint           | t
 device_id   | text             | t
 series_0    | double precision | f
 series_1    | double precision | f
 series_2    | double precision | f
 series_bool | boolean          | f

ALTER TABLE "two_Partitions" RENAME TO "newname";
SELECT * FROM "newname";
     timeCustom      | device_id | series_0 | series_1 | series_2 | series_bool 
---------------------+-----------+----------+----------+----------+-------------
 1257894000000000000 | dev1      |      1.5 |        1 |        2 | t
 1257894000000000000 | dev1      |      1.5 |        2 |          | 
 1257894000000001000 | dev1      |      2.5 |        3 |          | 
 1257894001000000000 | dev1      |      3.5 |        4 |          | 
 1257894002000000000 | dev1      |      5.5 |        6 |          | t
 1257894002000000000 | dev1      |      5.5 |        7 |          | f
 1257894002000000000 | dev1      |      2.5 |        3 |          | 
 1257897600000000000 | dev1      |      4.5 |        5 |          | f
 1257987600000000000 | dev1      |      1.5 |        1 |          | 
 1257987600000000000 | dev1      |      1.5 |        2 |          | 
 1257894000000000000 | dev2      |      1.5 |        1 |          | 
 1257894000000000000 | dev2      |      1.5 |        2 |          | 

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 | status 
----+-------------+------------+------------------------+-------------------------+----------------+--------------------------+--------------------------+-------------------+-------------------+--------------------------+--------
  1 | public      | newname    | _timescaledb_internal  | _hyper_1                |              2 | _timescaledb_functions   | calculate_chunk_interval |                 0 |                 0 |                          |      0

\c :TEST_DBNAME :ROLE_SUPERUSER
CREATE SCHEMA "newschema" AUTHORIZATION :ROLE_DEFAULT_PERM_USER;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
ALTER TABLE "newname" SET SCHEMA "newschema";
SELECT * FROM "newschema"."newname";
     timeCustom      | device_id | series_0 | series_1 | series_2 | series_bool 
---------------------+-----------+----------+----------+----------+-------------
 1257894000000000000 | dev1      |      1.5 |        1 |        2 | t
 1257894000000000000 | dev1      |      1.5 |        2 |          | 
 1257894000000001000 | dev1      |      2.5 |        3 |          | 
 1257894001000000000 | dev1      |      3.5 |        4 |          | 
 1257894002000000000 | dev1      |      5.5 |        6 |          | t
 1257894002000000000 | dev1      |      5.5 |        7 |          | f
 1257894002000000000 | dev1      |      2.5 |        3 |          | 
 1257897600000000000 | dev1      |      4.5 |        5 |          | f
 1257987600000000000 | dev1      |      1.5 |        1 |          | 
 1257987600000000000 | dev1      |      1.5 |        2 |          | 
 1257894000000000000 | dev2      |      1.5 |        1 |          | 
 1257894000000000000 | dev2      |      1.5 |        2 |          | 

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 | status 
----+-------------+------------+------------------------+-------------------------+----------------+--------------------------+--------------------------+-------------------+-------------------+--------------------------+--------
  1 | newschema   | newname    | _timescaledb_internal  | _hyper_1                |              2 | _timescaledb_functions   | calculate_chunk_interval |                 0 |                 0 |                          |      0

DROP TABLE "newschema"."newname";
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 | status 
----+-------------+------------+------------------------+-------------------------+----------------+--------------------------+------------------------+-------------------+-------------------+--------------------------+--------

SELECT schema, name FROM test.relation WHERE schema IN ('public', '_timescaledb_catalog', '_timescaledb_internal');
        schema         |                       name                       
-----------------------+--------------------------------------------------
 _timescaledb_catalog  | bgw_job
 _timescaledb_catalog  | chunk
 _timescaledb_catalog  | chunk_column_stats
 _timescaledb_catalog  | chunk_constraint
 _timescaledb_catalog  | chunk_rewrite
 _timescaledb_catalog  | compression_algorithm
 _timescaledb_catalog  | compression_chunk_size
 _timescaledb_catalog  | compression_settings
 _timescaledb_catalog  | continuous_agg
 _timescaledb_catalog  | continuous_agg_migrate_plan
 _timescaledb_catalog  | continuous_agg_migrate_plan_step
 _timescaledb_catalog  | continuous_aggs_bucket_function
 _timescaledb_catalog  | continuous_aggs_hypertable_invalidation_log
 _timescaledb_catalog  | continuous_aggs_invalidation_threshold
 _timescaledb_catalog  | continuous_aggs_materialization_invalidation_log
 _timescaledb_catalog  | continuous_aggs_materialization_ranges
 _timescaledb_catalog  | continuous_aggs_watermark
 _timescaledb_catalog  | dimension
 _timescaledb_catalog  | dimension_slice
 _timescaledb_catalog  | hypertable
 _timescaledb_catalog  | metadata
 _timescaledb_catalog  | tablespace
 _timescaledb_catalog  | telemetry_event
 _timescaledb_internal | bgw_job_stat
 _timescaledb_internal | bgw_job_stat_history
 _timescaledb_internal | bgw_policy_chunk_stats
 _timescaledb_internal | compressed_chunk_stats
 _timescaledb_internal | hypertable_chunk_local_size

-- Test that renaming ordinary table works
CREATE TABLE renametable (foo int);
ALTER TABLE "renametable" RENAME TO "newname_none_ht";
SELECT * FROM "newname_none_ht";
 foo 
-----

