-- 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.
\set ECHO ALL
CREATE VIEW settings AS SELECT * FROM _timescaledb_catalog.compression_settings ORDER BY upper(relid::text) COLLATE "C";
CREATE VIEW ht_settings AS SELECT * FROM timescaledb_information.hypertable_compression_settings ORDER BY upper(hypertable::text) COLLATE "C";
CREATE VIEW chunk_settings AS SELECT * FROM timescaledb_information.chunk_compression_settings ORDER BY upper(hypertable::text) COLLATE "C", upper(chunk::text) COLLATE "C";
CREATE TABLE metrics(time timestamptz not null, device text, value float);
SELECT table_name FROM create_hypertable('metrics','time');
 table_name 
------------
 metrics

ALTER TABLE metrics SET (timescaledb.compress, timescaledb.compress_segmentby='device');
SELECT * FROM settings;
  relid  | compress_relid | segmentby | orderby | orderby_desc | orderby_nullsfirst | index 
---------+----------------+-----------+---------+--------------+--------------------+-------
 metrics |                | {device}  |         |              |                    | 

SELECT * FROM ht_settings;
 hypertable | segmentby | orderby | compress_interval_length | index 
------------+-----------+---------+--------------------------+-------
 metrics    | device    |         |                          | 

SELECT * FROM chunk_settings;
 hypertable | chunk | segmentby | orderby | index 
------------+-------+-----------+---------+-------

-- create 2 chunks
INSERT INTO metrics VALUES ('2000-01-01'), ('2001-01-01');
-- no change to settings
SELECT * FROM settings;
  relid  | compress_relid | segmentby | orderby | orderby_desc | orderby_nullsfirst | index 
---------+----------------+-----------+---------+--------------+--------------------+-------
 metrics |                | {device}  |         |              |                    | 

--Enable compression path info
SET timescaledb.debug_compression_path_info= 'on';
SELECT compress_chunk('_timescaledb_internal._hyper_1_1_chunk');
INFO:  using tuplesort to scan rows from "_hyper_1_1_chunk" for converting to columnstore
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_1_1_chunk

RESET timescaledb.debug_compression_path_info;
SELECT * FROM settings;
                 relid                  |                 compress_relid                 | segmentby | orderby | orderby_desc | orderby_nullsfirst |                            index                            
----------------------------------------+------------------------------------------------+-----------+---------+--------------+--------------------+-------------------------------------------------------------
 metrics                                |                                                | {device}  |         |              |                    | 
 _timescaledb_internal._hyper_1_1_chunk | _timescaledb_internal.compress_hyper_2_3_chunk | {device}  | {time}  | {t}          | {t}                | [{"type": "minmax", "column": "time", "source": "orderby"}]

SELECT * FROM chunk_settings;
 hypertable |                 chunk                  | segmentby |   orderby   |                            index                            
------------+----------------------------------------+-----------+-------------+-------------------------------------------------------------
 metrics    | _timescaledb_internal._hyper_1_1_chunk | device    | "time" DESC | [{"type": "minmax", "column": "time", "source": "orderby"}]

SELECT compress_chunk('_timescaledb_internal._hyper_1_2_chunk');
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_1_2_chunk

SELECT * FROM settings;
                 relid                  |                 compress_relid                 | segmentby | orderby | orderby_desc | orderby_nullsfirst |                            index                            
----------------------------------------+------------------------------------------------+-----------+---------+--------------+--------------------+-------------------------------------------------------------
 metrics                                |                                                | {device}  |         |              |                    | 
 _timescaledb_internal._hyper_1_1_chunk | _timescaledb_internal.compress_hyper_2_3_chunk | {device}  | {time}  | {t}          | {t}                | [{"type": "minmax", "column": "time", "source": "orderby"}]
 _timescaledb_internal._hyper_1_2_chunk | _timescaledb_internal.compress_hyper_2_4_chunk | {device}  | {time}  | {t}          | {t}                | [{"type": "minmax", "column": "time", "source": "orderby"}]

SELECT * FROM chunk_settings;
 hypertable |                 chunk                  | segmentby |   orderby   |                            index                            
------------+----------------------------------------+-----------+-------------+-------------------------------------------------------------
 metrics    | _timescaledb_internal._hyper_1_1_chunk | device    | "time" DESC | [{"type": "minmax", "column": "time", "source": "orderby"}]
 metrics    | _timescaledb_internal._hyper_1_2_chunk | device    | "time" DESC | [{"type": "minmax", "column": "time", "source": "orderby"}]

-- dropping chunk should remove that chunks compression settings
DROP TABLE _timescaledb_internal._hyper_1_1_chunk;
SELECT * FROM settings;
                 relid                  |                 compress_relid                 | segmentby | orderby | orderby_desc | orderby_nullsfirst |                            index                            
----------------------------------------+------------------------------------------------+-----------+---------+--------------+--------------------+-------------------------------------------------------------
 metrics                                |                                                | {device}  |         |              |                    | 
 _timescaledb_internal._hyper_1_2_chunk | _timescaledb_internal.compress_hyper_2_4_chunk | {device}  | {time}  | {t}          | {t}                | [{"type": "minmax", "column": "time", "source": "orderby"}]

SELECT * FROM chunk_settings;
 hypertable |                 chunk                  | segmentby |   orderby   |                            index                            
------------+----------------------------------------+-----------+-------------+-------------------------------------------------------------
 metrics    | _timescaledb_internal._hyper_1_2_chunk | device    | "time" DESC | [{"type": "minmax", "column": "time", "source": "orderby"}]

-- decompress_chunk should remove settings for that chunk
SELECT decompress_chunk('_timescaledb_internal._hyper_1_2_chunk');
            decompress_chunk            
----------------------------------------
 _timescaledb_internal._hyper_1_2_chunk

SELECT * FROM settings;
  relid  | compress_relid | segmentby | orderby | orderby_desc | orderby_nullsfirst | index 
---------+----------------+-----------+---------+--------------+--------------------+-------
 metrics |                | {device}  |         |              |                    | 

SELECT * FROM chunk_settings;
 hypertable | chunk | segmentby | orderby | index 
------------+-------+-----------+---------+-------

-- compress_chunk should add settings back
SELECT compress_chunk('_timescaledb_internal._hyper_1_2_chunk');
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_1_2_chunk

SELECT * FROM settings;
                 relid                  |                 compress_relid                 | segmentby | orderby | orderby_desc | orderby_nullsfirst |                            index                            
----------------------------------------+------------------------------------------------+-----------+---------+--------------+--------------------+-------------------------------------------------------------
 metrics                                |                                                | {device}  |         |              |                    | 
 _timescaledb_internal._hyper_1_2_chunk | _timescaledb_internal.compress_hyper_2_5_chunk | {device}  | {time}  | {t}          | {t}                | [{"type": "minmax", "column": "time", "source": "orderby"}]

SELECT * FROM chunk_settings;
 hypertable |                 chunk                  | segmentby |   orderby   |                            index                            
------------+----------------------------------------+-----------+-------------+-------------------------------------------------------------
 metrics    | _timescaledb_internal._hyper_1_2_chunk | device    | "time" DESC | [{"type": "minmax", "column": "time", "source": "orderby"}]

-- dropping hypertable should remove all settings
DROP TABLE metrics;
SELECT * FROM settings;
 relid | compress_relid | segmentby | orderby | orderby_desc | orderby_nullsfirst | index 
-------+----------------+-----------+---------+--------------+--------------------+-------

SELECT * FROM ht_settings;
 hypertable | segmentby | orderby | compress_interval_length | index 
------------+-----------+---------+--------------------------+-------

SELECT * FROM chunk_settings;
 hypertable | chunk | segmentby | orderby | index 
------------+-------+-----------+---------+-------

CREATE TABLE metrics(time timestamptz not null, d1 text, d2 text, value float);
SELECT table_name FROM create_hypertable('metrics','time');
 table_name 
------------
 metrics

ALTER TABLE metrics SET (timescaledb.compress);
-- hypertable should have default settings now
SELECT * FROM settings;
  relid  | compress_relid | segmentby | orderby | orderby_desc | orderby_nullsfirst | index 
---------+----------------+-----------+---------+--------------+--------------------+-------
 metrics |                |           |         |              |                    | 

SELECT * FROM ht_settings;
 hypertable | segmentby | orderby | compress_interval_length | index 
------------+-----------+---------+--------------------------+-------
 metrics    |           |         |                          | 

-- create chunk
INSERT INTO metrics VALUES ('2000-01-01');
ALTER TABLE metrics SET (timescaledb.compress_segmentby='d1');
-- settings should be updated
SELECT * FROM settings;
  relid  | compress_relid | segmentby | orderby | orderby_desc | orderby_nullsfirst | index 
---------+----------------+-----------+---------+--------------+--------------------+-------
 metrics |                | {d1}      |         |              |                    | 

SELECT * FROM ht_settings;
 hypertable | segmentby | orderby | compress_interval_length | index 
------------+-----------+---------+--------------------------+-------
 metrics    | d1        |         |                          | 

SELECT compress_chunk(show_chunks('metrics'));
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_3_6_chunk

-- settings for compressed chunk should be present
SELECT * FROM settings;
                 relid                  |                 compress_relid                 | segmentby | orderby | orderby_desc | orderby_nullsfirst |                            index                            
----------------------------------------+------------------------------------------------+-----------+---------+--------------+--------------------+-------------------------------------------------------------
 metrics                                |                                                | {d1}      |         |              |                    | 
 _timescaledb_internal._hyper_3_6_chunk | _timescaledb_internal.compress_hyper_4_7_chunk | {d1}      | {time}  | {t}          | {t}                | [{"type": "minmax", "column": "time", "source": "orderby"}]

SELECT * FROM chunk_settings;
 hypertable |                 chunk                  | segmentby |   orderby   |                            index                            
------------+----------------------------------------+-----------+-------------+-------------------------------------------------------------
 metrics    | _timescaledb_internal._hyper_3_6_chunk | d1        | "time" DESC | [{"type": "minmax", "column": "time", "source": "orderby"}]

-- changing settings should update settings for hypertable but not existing compressed chunks
ALTER TABLE metrics SET (timescaledb.compress_segmentby='d2');
SELECT * FROM settings;
                 relid                  |                 compress_relid                 | segmentby | orderby | orderby_desc | orderby_nullsfirst |                            index                            
----------------------------------------+------------------------------------------------+-----------+---------+--------------+--------------------+-------------------------------------------------------------
 metrics                                |                                                | {d2}      |         |              |                    | 
 _timescaledb_internal._hyper_3_6_chunk | _timescaledb_internal.compress_hyper_4_7_chunk | {d1}      | {time}  | {t}          | {t}                | [{"type": "minmax", "column": "time", "source": "orderby"}]

SELECT * FROM ht_settings;
 hypertable | segmentby | orderby | compress_interval_length | index 
------------+-----------+---------+--------------------------+-------
 metrics    | d2        |         |                          | 

SELECT * FROM chunk_settings;
 hypertable |                 chunk                  | segmentby |   orderby   |                            index                            
------------+----------------------------------------+-----------+-------------+-------------------------------------------------------------
 metrics    | _timescaledb_internal._hyper_3_6_chunk | d1        | "time" DESC | [{"type": "minmax", "column": "time", "source": "orderby"}]

-- changing settings should update settings for hypertable but not existing compressed chunks
ALTER TABLE metrics SET (timescaledb.compress_segmentby='');
SELECT * FROM settings;
                 relid                  |                 compress_relid                 | segmentby | orderby | orderby_desc | orderby_nullsfirst |                            index                            
----------------------------------------+------------------------------------------------+-----------+---------+--------------+--------------------+-------------------------------------------------------------
 metrics                                |                                                | {}        |         |              |                    | 
 _timescaledb_internal._hyper_3_6_chunk | _timescaledb_internal.compress_hyper_4_7_chunk | {d1}      | {time}  | {t}          | {t}                | [{"type": "minmax", "column": "time", "source": "orderby"}]

SELECT * FROM ht_settings;
 hypertable | segmentby | orderby | compress_interval_length | index 
------------+-----------+---------+--------------------------+-------
 metrics    |           |         |                          | 

SELECT * FROM chunk_settings;
 hypertable |                 chunk                  | segmentby |   orderby   |                            index                            
------------+----------------------------------------+-----------+-------------+-------------------------------------------------------------
 metrics    | _timescaledb_internal._hyper_3_6_chunk | d1        | "time" DESC | [{"type": "minmax", "column": "time", "source": "orderby"}]

-- create another chunk
INSERT INTO metrics VALUES ('2000-02-01');
SELECT compress_chunk(show_chunks('metrics'), true);
NOTICE:  chunk "_hyper_3_6_chunk" is already converted to columnstore
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_3_6_chunk
 _timescaledb_internal._hyper_3_8_chunk

SELECT * FROM settings;
                 relid                  |                 compress_relid                 | segmentby | orderby | orderby_desc | orderby_nullsfirst |                            index                            
----------------------------------------+------------------------------------------------+-----------+---------+--------------+--------------------+-------------------------------------------------------------
 metrics                                |                                                | {}        |         |              |                    | 
 _timescaledb_internal._hyper_3_6_chunk | _timescaledb_internal.compress_hyper_4_7_chunk | {d1}      | {time}  | {t}          | {t}                | [{"type": "minmax", "column": "time", "source": "orderby"}]
 _timescaledb_internal._hyper_3_8_chunk | _timescaledb_internal.compress_hyper_4_9_chunk | {}        | {time}  | {t}          | {t}                | [{"type": "minmax", "column": "time", "source": "orderby"}]

SELECT * FROM ht_settings;
 hypertable | segmentby | orderby | compress_interval_length | index 
------------+-----------+---------+--------------------------+-------
 metrics    |           |         |                          | 

SELECT * FROM chunk_settings;
 hypertable |                 chunk                  | segmentby |   orderby   |                            index                            
------------+----------------------------------------+-----------+-------------+-------------------------------------------------------------
 metrics    | _timescaledb_internal._hyper_3_6_chunk | d1        | "time" DESC | [{"type": "minmax", "column": "time", "source": "orderby"}]
 metrics    | _timescaledb_internal._hyper_3_8_chunk |           | "time" DESC | [{"type": "minmax", "column": "time", "source": "orderby"}]

ALTER TABLE metrics SET (timescaledb.compress_orderby='"time" desc', timescaledb.compress_segmentby='d2', timescaledb.index='bloom(value)');
SELECT format('%I.%I', schema_name, table_name) AS "CHUNK" FROM _timescaledb_catalog.chunk WHERE compressed_chunk_id IS NOT NULL ORDER BY id LIMIT 1 OFFSET 1\gset
-- recompressing chunks should apply current hypertable settings
SELECT compress_chunk(:'CHUNK', recompress:=true);
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_3_8_chunk

SELECT * FROM settings;
                 relid                  |                 compress_relid                  | segmentby | orderby | orderby_desc | orderby_nullsfirst |                                                         index                                                         
----------------------------------------+-------------------------------------------------+-----------+---------+--------------+--------------------+-----------------------------------------------------------------------------------------------------------------------
 metrics                                |                                                 | {d2}      | {time}  | {t}          | {t}                | [{"type": "bloom", "column": "value", "source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]
 _timescaledb_internal._hyper_3_6_chunk | _timescaledb_internal.compress_hyper_4_7_chunk  | {d1}      | {time}  | {t}          | {t}                | [{"type": "minmax", "column": "time", "source": "orderby"}]
 _timescaledb_internal._hyper_3_8_chunk | _timescaledb_internal.compress_hyper_4_10_chunk | {d2}      | {time}  | {t}          | {t}                | [{"type": "bloom", "column": "value", "source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]

SELECT compress_chunk(:'CHUNK', recompress:=true);
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_3_8_chunk

SELECT * FROM settings;
                 relid                  |                 compress_relid                  | segmentby | orderby | orderby_desc | orderby_nullsfirst |                                                         index                                                         
----------------------------------------+-------------------------------------------------+-----------+---------+--------------+--------------------+-----------------------------------------------------------------------------------------------------------------------
 metrics                                |                                                 | {d2}      | {time}  | {t}          | {t}                | [{"type": "bloom", "column": "value", "source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]
 _timescaledb_internal._hyper_3_6_chunk | _timescaledb_internal.compress_hyper_4_7_chunk  | {d1}      | {time}  | {t}          | {t}                | [{"type": "minmax", "column": "time", "source": "orderby"}]
 _timescaledb_internal._hyper_3_8_chunk | _timescaledb_internal.compress_hyper_4_11_chunk | {d2}      | {time}  | {t}          | {t}                | [{"type": "bloom", "column": "value", "source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]

ALTER TABLE metrics SET (timescaledb.compress_orderby='"time" desc', timescaledb.compress_segmentby='d2', timescaledb.index='');
SELECT format('%I.%I', schema_name, table_name) AS "CHUNK" FROM _timescaledb_catalog.chunk WHERE compressed_chunk_id IS NOT NULL ORDER BY id LIMIT 1 OFFSET 1\gset
SELECT compress_chunk(:'CHUNK', recompress:=true);
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_3_8_chunk

SELECT * FROM settings;
                 relid                  |                 compress_relid                  | segmentby | orderby | orderby_desc | orderby_nullsfirst |                                       index                                       
----------------------------------------+-------------------------------------------------+-----------+---------+--------------+--------------------+-----------------------------------------------------------------------------------
 metrics                                |                                                 | {d2}      | {time}  | {t}          | {t}                | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]
 _timescaledb_internal._hyper_3_6_chunk | _timescaledb_internal.compress_hyper_4_7_chunk  | {d1}      | {time}  | {t}          | {t}                | [{"type": "minmax", "column": "time", "source": "orderby"}]
 _timescaledb_internal._hyper_3_8_chunk | _timescaledb_internal.compress_hyper_4_12_chunk | {d2}      | {time}  | {t}          | {t}                | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]

SELECT * FROM ht_settings;
 hypertable | segmentby |   orderby   | compress_interval_length |                                       index                                       
------------+-----------+-------------+--------------------------+-----------------------------------------------------------------------------------
 metrics    | d2        | "time" DESC |                          | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]

SELECT * FROM chunk_settings;
 hypertable |                 chunk                  | segmentby |   orderby   |                                       index                                       
------------+----------------------------------------+-----------+-------------+-----------------------------------------------------------------------------------
 metrics    | _timescaledb_internal._hyper_3_6_chunk | d1        | "time" DESC | [{"type": "minmax", "column": "time", "source": "orderby"}]
 metrics    | _timescaledb_internal._hyper_3_8_chunk | d2        | "time" DESC | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]

-- test different order by flags with compression settings view
CREATE TABLE metrics2(time timestamptz not null, d1 text, d2 text, value float);
SELECT create_hypertable('metrics2','time');
   create_hypertable   
-----------------------
 (5,public,metrics2,t)

SELECT * FROM ht_settings;
 hypertable | segmentby |   orderby   | compress_interval_length |                                       index                                       
------------+-----------+-------------+--------------------------+-----------------------------------------------------------------------------------
 metrics    | d2        | "time" DESC |                          | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]
 metrics2   |           |             |                          | 

ALTER TABLE metrics2 SET (timescaledb.compress_orderby='d1 NULLS FIRST, d2 NULLS LAST, time, value ASC');
SELECT * FROM ht_settings;
 hypertable | segmentby |            orderby             | compress_interval_length |                                       index                                       
------------+-----------+--------------------------------+--------------------------+-----------------------------------------------------------------------------------
 metrics    | d2        | "time" DESC                    |                          | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]
 metrics2   |           | d1 NULLS FIRST,d2,"time",value |                          | 

ALTER TABLE metrics2 SET (timescaledb.compress_orderby='d1 DESC NULLS LAST, d2 ASC NULLS FIRST, value DESC, time ASC NULLS FIRST');
SELECT * FROM ht_settings;
 hypertable | segmentby |                             orderby                             | compress_interval_length |                                       index                                       
------------+-----------+-----------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------------------
 metrics    | d2        | "time" DESC                                                     |                          | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]
 metrics2   |           | d1 DESC NULLS LAST,d2 NULLS FIRST,value DESC,"time" NULLS FIRST |                          | 

-- test decompression uses the correct settings
ALTER TABLE metrics SET (timescaledb.compress_segmentby='');
SELECT compress_chunk(show_chunks('metrics'), recompress:=true);
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_3_6_chunk
 _timescaledb_internal._hyper_3_8_chunk

ALTER TABLE metrics SET (timescaledb.compress_segmentby='d1,d2');
SELECT * FROM chunk_settings;
 hypertable |                 chunk                  | segmentby |   orderby   |                                       index                                       
------------+----------------------------------------+-----------+-------------+-----------------------------------------------------------------------------------
 metrics    | _timescaledb_internal._hyper_3_6_chunk |           | "time" DESC | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]
 metrics    | _timescaledb_internal._hyper_3_8_chunk |           | "time" DESC | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]

SELECT * FROM metrics WHERE d1 = 'foo';
 time | d1 | d2 | value 
------+----+----+-------

SELECT * FROM settings;
                 relid                  |                 compress_relid                  | segmentby |      orderby       | orderby_desc | orderby_nullsfirst |                                       index                                       
----------------------------------------+-------------------------------------------------+-----------+--------------------+--------------+--------------------+-----------------------------------------------------------------------------------
 metrics                                |                                                 | {d1,d2}   | {time}             | {t}          | {t}                | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]
 metrics2                               |                                                 |           | {d1,d2,value,time} | {t,f,t,f}    | {f,t,t,t}          | 
 _timescaledb_internal._hyper_3_6_chunk | _timescaledb_internal.compress_hyper_4_13_chunk | {}        | {time}             | {t}          | {t}                | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]
 _timescaledb_internal._hyper_3_8_chunk | _timescaledb_internal.compress_hyper_4_14_chunk | {}        | {time}             | {t}          | {t}                | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]

-- Check that TRUNCATE <hypertable> also cleans up compression
-- settings for chunks that are dropped when truncating.
TRUNCATE metrics;
SELECT * FROM settings;
  relid   | compress_relid | segmentby |      orderby       | orderby_desc | orderby_nullsfirst |                                       index                                       
----------+----------------+-----------+--------------------+--------------+--------------------+-----------------------------------------------------------------------------------
 metrics  |                | {d1,d2}   | {time}             | {t}          | {t}                | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]
 metrics2 |                |           | {d1,d2,value,time} | {t,f,t,f}    | {f,t,t,t}          | 

SELECT * FROM chunk_settings;
 hypertable | chunk | segmentby | orderby | index 
------------+-------+-----------+---------+-------

-- Recreate chunks
INSERT INTO metrics VALUES ('2000-01-01'), ('2001-01-01');
SELECT compress_chunk(ch) FROM show_chunks('metrics') ch;
             compress_chunk              
-----------------------------------------
 _timescaledb_internal._hyper_3_15_chunk
 _timescaledb_internal._hyper_3_16_chunk

SELECT * FROM settings;
                  relid                  |                 compress_relid                  | segmentby |      orderby       | orderby_desc | orderby_nullsfirst |                                       index                                       
-----------------------------------------+-------------------------------------------------+-----------+--------------------+--------------+--------------------+-----------------------------------------------------------------------------------
 metrics                                 |                                                 | {d1,d2}   | {time}             | {t}          | {t}                | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]
 metrics2                                |                                                 |           | {d1,d2,value,time} | {t,f,t,f}    | {f,t,t,t}          | 
 _timescaledb_internal._hyper_3_15_chunk | _timescaledb_internal.compress_hyper_4_17_chunk | {d1,d2}   | {time}             | {t}          | {t}                | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]
 _timescaledb_internal._hyper_3_16_chunk | _timescaledb_internal.compress_hyper_4_18_chunk | {d1,d2}   | {time}             | {t}          | {t}                | [{"source": "config"}, {"type": "minmax", "column": "time", "source": "orderby"}]

-- DROP TABLE with CASCADE uses a different code path for dropping
-- hypertable so needs to be tested separately.
DROP TABLE metrics CASCADE;
SELECT * FROM settings;
  relid   | compress_relid | segmentby |      orderby       | orderby_desc | orderby_nullsfirst | index 
----------+----------------+-----------+--------------------+--------------+--------------------+-------
 metrics2 |                |           | {d1,d2,value,time} | {t,f,t,f}    | {f,t,t,t}          | 

-- Test column limits for orderby and segmentby
\set ON_ERROR_STOP 0
\set VERBOSITY default
-- Test CREATE TABLE syntax
CREATE TABLE test_column_limit_create (
    time TIMESTAMPTZ NOT NULL,
    device_id INT,
    col1 DOUBLE PRECISION,
    col2 DOUBLE PRECISION,
    col3 DOUBLE PRECISION,
    col4 DOUBLE PRECISION,
    col5 DOUBLE PRECISION,
    col6 DOUBLE PRECISION,
    col7 DOUBLE PRECISION,
    col8 DOUBLE PRECISION,
    col9 DOUBLE PRECISION,
    col10 DOUBLE PRECISION,
    col11 DOUBLE PRECISION,
    col12 DOUBLE PRECISION,
    col13 DOUBLE PRECISION,
    col14 DOUBLE PRECISION,
    col15 DOUBLE PRECISION
) WITH (
    tsdb.hypertable,
    tsdb.partition_column='time',
    tsdb.segmentby='device_id',
    tsdb.orderby= 'col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15'
);
ERROR:  too many segmentby and orderby columns
DETAIL:  Combined segmentby keys (1) and orderby keys (32) cannot exceed 32
-- Test ALTER TABLE syntax
CREATE TABLE test_column_limit_alter (
    time TIMESTAMPTZ NOT NULL,
    device_id INT,
    col1 DOUBLE PRECISION,
    col2 DOUBLE PRECISION,
    col3 DOUBLE PRECISION,
    col4 DOUBLE PRECISION,
    col5 DOUBLE PRECISION,
    col6 DOUBLE PRECISION,
    col7 DOUBLE PRECISION,
    col8 DOUBLE PRECISION,
    col9 DOUBLE PRECISION,
    col10 DOUBLE PRECISION,
    col11 DOUBLE PRECISION,
    col12 DOUBLE PRECISION,
    col13 DOUBLE PRECISION,
    col14 DOUBLE PRECISION,
    col15 DOUBLE PRECISION,
    col16 DOUBLE PRECISION
);
-- Create hypertable
SELECT create_hypertable('test_column_limit_alter', 'time');
          create_hypertable           
--------------------------------------
 (8,public,test_column_limit_alter,t)

-- Enable compression with 1 segmentby and 20 orderby columns (1 + 40 = 41 keys)
ALTER TABLE test_column_limit_alter SET (
    timescaledb.compress,
    timescaledb.compress_segmentby = 'device_id',
    timescaledb.compress_orderby = 'col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15'
);
ERROR:  too many segmentby and orderby columns
DETAIL:  Combined segmentby keys (1) and orderby keys (32) cannot exceed 32
\set ON_ERROR_STOP 1
\set VERBOSITY terse
