-- 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.
-- test query planning with hypertable which contains
-- compressed chunks that depend on sequence number optimization
-- which is removed in latest schema revision
\c :TEST_DBNAME :ROLE_SUPERUSER
SET ROLE :ROLE_DEFAULT_PERM_USER;
\set EXPLAIN 'EXPLAIN (VERBOSE, BUFFERS OFF, COSTS OFF)'
CREATE TABLE hyper(
    time INT NOT NULL,
    device_id INT,
    val INT);
SELECT * FROM create_hypertable('hyper', 'time', chunk_time_interval => 10);
 hypertable_id | schema_name | table_name | created 
---------------+-------------+------------+---------
             1 | public      | hyper      | t

-- test case with segmentby
ALTER TABLE hyper SET (
    timescaledb.compress,
    timescaledb.compress_orderby = 'time',
    timescaledb.compress_segmentby = 'device_id');
INSERT INTO hyper VALUES (1, 1, 1), (2, 2, 1), (3, 3, 1), (10, 3, 2), (11, 4, 2), (11, 5, 2), (21, 2, 3), (22, 3, 3), (23, 4, 3), (30, 1, 4), (31, 3, 4), (31, 5, 4);
SELECT compress_chunk(show_chunks('hyper'));
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_1_1_chunk
 _timescaledb_internal._hyper_1_2_chunk
 _timescaledb_internal._hyper_1_3_chunk
 _timescaledb_internal._hyper_1_4_chunk

VACUUM ANALYZE hyper;
-- output without sequence number chunks, should match output with sequence numbers below
:EXPLAIN SELECT * FROM hyper
ORDER BY device_id, time;
--- QUERY PLAN ---
 Merge Append
   Sort Key: hyper.device_id, hyper."time"
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_1_chunk
         Output: _hyper_1_1_chunk."time", _hyper_1_1_chunk.device_id, _hyper_1_1_chunk.val
         ->  Sort
               Output: compress_hyper_2_5_chunk._ts_meta_count, compress_hyper_2_5_chunk.device_id, compress_hyper_2_5_chunk._ts_meta_min_1, compress_hyper_2_5_chunk._ts_meta_max_1, compress_hyper_2_5_chunk."time", compress_hyper_2_5_chunk.val
               Sort Key: compress_hyper_2_5_chunk.device_id, compress_hyper_2_5_chunk._ts_meta_min_1, compress_hyper_2_5_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_5_chunk
                     Output: compress_hyper_2_5_chunk._ts_meta_count, compress_hyper_2_5_chunk.device_id, compress_hyper_2_5_chunk._ts_meta_min_1, compress_hyper_2_5_chunk._ts_meta_max_1, compress_hyper_2_5_chunk."time", compress_hyper_2_5_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_2_chunk
         Output: _hyper_1_2_chunk."time", _hyper_1_2_chunk.device_id, _hyper_1_2_chunk.val
         ->  Sort
               Output: compress_hyper_2_6_chunk._ts_meta_count, compress_hyper_2_6_chunk.device_id, compress_hyper_2_6_chunk._ts_meta_min_1, compress_hyper_2_6_chunk._ts_meta_max_1, compress_hyper_2_6_chunk."time", compress_hyper_2_6_chunk.val
               Sort Key: compress_hyper_2_6_chunk.device_id, compress_hyper_2_6_chunk._ts_meta_min_1, compress_hyper_2_6_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_6_chunk
                     Output: compress_hyper_2_6_chunk._ts_meta_count, compress_hyper_2_6_chunk.device_id, compress_hyper_2_6_chunk._ts_meta_min_1, compress_hyper_2_6_chunk._ts_meta_max_1, compress_hyper_2_6_chunk."time", compress_hyper_2_6_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_3_chunk
         Output: _hyper_1_3_chunk."time", _hyper_1_3_chunk.device_id, _hyper_1_3_chunk.val
         ->  Sort
               Output: compress_hyper_2_7_chunk._ts_meta_count, compress_hyper_2_7_chunk.device_id, compress_hyper_2_7_chunk._ts_meta_min_1, compress_hyper_2_7_chunk._ts_meta_max_1, compress_hyper_2_7_chunk."time", compress_hyper_2_7_chunk.val
               Sort Key: compress_hyper_2_7_chunk.device_id, compress_hyper_2_7_chunk._ts_meta_min_1, compress_hyper_2_7_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_7_chunk
                     Output: compress_hyper_2_7_chunk._ts_meta_count, compress_hyper_2_7_chunk.device_id, compress_hyper_2_7_chunk._ts_meta_min_1, compress_hyper_2_7_chunk._ts_meta_max_1, compress_hyper_2_7_chunk."time", compress_hyper_2_7_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_4_chunk
         Output: _hyper_1_4_chunk."time", _hyper_1_4_chunk.device_id, _hyper_1_4_chunk.val
         ->  Sort
               Output: compress_hyper_2_8_chunk._ts_meta_count, compress_hyper_2_8_chunk.device_id, compress_hyper_2_8_chunk._ts_meta_min_1, compress_hyper_2_8_chunk._ts_meta_max_1, compress_hyper_2_8_chunk."time", compress_hyper_2_8_chunk.val
               Sort Key: compress_hyper_2_8_chunk.device_id, compress_hyper_2_8_chunk._ts_meta_min_1, compress_hyper_2_8_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_8_chunk
                     Output: compress_hyper_2_8_chunk._ts_meta_count, compress_hyper_2_8_chunk.device_id, compress_hyper_2_8_chunk._ts_meta_min_1, compress_hyper_2_8_chunk._ts_meta_max_1, compress_hyper_2_8_chunk."time", compress_hyper_2_8_chunk.val

SELECT * FROM hyper
ORDER BY device_id, time;
 time | device_id | val 
------+-----------+-----
    1 |         1 |   1
   30 |         1 |   4
    2 |         2 |   1
   21 |         2 |   3
    3 |         3 |   1
   10 |         3 |   2
   22 |         3 |   3
   31 |         3 |   4
   11 |         4 |   2
   23 |         4 |   3
   11 |         5 |   2
   31 |         5 |   4

-- backwards index scan
:EXPLAIN SELECT * FROM hyper
ORDER BY device_id DESC, time DESC;
--- QUERY PLAN ---
 Merge Append
   Sort Key: hyper.device_id DESC, hyper."time" DESC
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_1_chunk
         Output: _hyper_1_1_chunk."time", _hyper_1_1_chunk.device_id, _hyper_1_1_chunk.val
         Reverse: true
         ->  Sort
               Output: compress_hyper_2_5_chunk._ts_meta_count, compress_hyper_2_5_chunk.device_id, compress_hyper_2_5_chunk._ts_meta_min_1, compress_hyper_2_5_chunk._ts_meta_max_1, compress_hyper_2_5_chunk."time", compress_hyper_2_5_chunk.val
               Sort Key: compress_hyper_2_5_chunk.device_id DESC, compress_hyper_2_5_chunk._ts_meta_min_1 DESC, compress_hyper_2_5_chunk._ts_meta_max_1 DESC
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_5_chunk
                     Output: compress_hyper_2_5_chunk._ts_meta_count, compress_hyper_2_5_chunk.device_id, compress_hyper_2_5_chunk._ts_meta_min_1, compress_hyper_2_5_chunk._ts_meta_max_1, compress_hyper_2_5_chunk."time", compress_hyper_2_5_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_2_chunk
         Output: _hyper_1_2_chunk."time", _hyper_1_2_chunk.device_id, _hyper_1_2_chunk.val
         Reverse: true
         ->  Sort
               Output: compress_hyper_2_6_chunk._ts_meta_count, compress_hyper_2_6_chunk.device_id, compress_hyper_2_6_chunk._ts_meta_min_1, compress_hyper_2_6_chunk._ts_meta_max_1, compress_hyper_2_6_chunk."time", compress_hyper_2_6_chunk.val
               Sort Key: compress_hyper_2_6_chunk.device_id DESC, compress_hyper_2_6_chunk._ts_meta_min_1 DESC, compress_hyper_2_6_chunk._ts_meta_max_1 DESC
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_6_chunk
                     Output: compress_hyper_2_6_chunk._ts_meta_count, compress_hyper_2_6_chunk.device_id, compress_hyper_2_6_chunk._ts_meta_min_1, compress_hyper_2_6_chunk._ts_meta_max_1, compress_hyper_2_6_chunk."time", compress_hyper_2_6_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_3_chunk
         Output: _hyper_1_3_chunk."time", _hyper_1_3_chunk.device_id, _hyper_1_3_chunk.val
         Reverse: true
         ->  Sort
               Output: compress_hyper_2_7_chunk._ts_meta_count, compress_hyper_2_7_chunk.device_id, compress_hyper_2_7_chunk._ts_meta_min_1, compress_hyper_2_7_chunk._ts_meta_max_1, compress_hyper_2_7_chunk."time", compress_hyper_2_7_chunk.val
               Sort Key: compress_hyper_2_7_chunk.device_id DESC, compress_hyper_2_7_chunk._ts_meta_min_1 DESC, compress_hyper_2_7_chunk._ts_meta_max_1 DESC
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_7_chunk
                     Output: compress_hyper_2_7_chunk._ts_meta_count, compress_hyper_2_7_chunk.device_id, compress_hyper_2_7_chunk._ts_meta_min_1, compress_hyper_2_7_chunk._ts_meta_max_1, compress_hyper_2_7_chunk."time", compress_hyper_2_7_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_4_chunk
         Output: _hyper_1_4_chunk."time", _hyper_1_4_chunk.device_id, _hyper_1_4_chunk.val
         Reverse: true
         ->  Sort
               Output: compress_hyper_2_8_chunk._ts_meta_count, compress_hyper_2_8_chunk.device_id, compress_hyper_2_8_chunk._ts_meta_min_1, compress_hyper_2_8_chunk._ts_meta_max_1, compress_hyper_2_8_chunk."time", compress_hyper_2_8_chunk.val
               Sort Key: compress_hyper_2_8_chunk.device_id DESC, compress_hyper_2_8_chunk._ts_meta_min_1 DESC, compress_hyper_2_8_chunk._ts_meta_max_1 DESC
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_8_chunk
                     Output: compress_hyper_2_8_chunk._ts_meta_count, compress_hyper_2_8_chunk.device_id, compress_hyper_2_8_chunk._ts_meta_min_1, compress_hyper_2_8_chunk._ts_meta_max_1, compress_hyper_2_8_chunk."time", compress_hyper_2_8_chunk.val

SELECT * FROM hyper
ORDER BY device_id DESC, time DESC;
 time | device_id | val 
------+-----------+-----
   31 |         5 |   4
   11 |         5 |   2
   23 |         4 |   3
   11 |         4 |   2
   31 |         3 |   4
   22 |         3 |   3
   10 |         3 |   2
    3 |         3 |   1
   21 |         2 |   3
    2 |         2 |   1
   30 |         1 |   4
    1 |         1 |   1

-- modify two chunks by adding sequence number to the segments
-- and rebuild the index based on that column
SELECT comp_ch.table_name AS "CHUNK_NAME", comp_ch.schema_name|| '.' || comp_ch.table_name AS "CHUNK_FULL_NAME"
FROM _timescaledb_catalog.chunk ch1, _timescaledb_catalog.chunk comp_ch, _timescaledb_catalog.hypertable ht
WHERE ch1.hypertable_id = ht.id AND ht.table_name LIKE 'hyper'
AND ch1.compressed_chunk_id = comp_ch.id
ORDER BY ch1.id LIMIT 1 \gset
SELECT schemaname || '.' || indexname AS "CHUNK_INDEX" FROM pg_indexes where tablename = :'CHUNK_NAME'
LIMIT 1 \gset
SET ROLE :ROLE_SUPERUSER;
SET timescaledb.restoring TO ON;
-- add sequence number column and fill in the correct sequences
ALTER TABLE :CHUNK_FULL_NAME ADD COLUMN _ts_meta_sequence_num int;
INSERT INTO :CHUNK_FULL_NAME (device_id, time, val, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_sequence_num)
SELECT device_id, time, val, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1 , 10*row_number() over (partition by device_id) as _ts_meta_sequence_num
FROM :CHUNK_FULL_NAME
ORDER BY device_id, _ts_meta_min_1, _ts_meta_max_1;
DELETE FROM :CHUNK_FULL_NAME WHERE _ts_meta_sequence_num IS NULL;
-- drop exising index
DROP INDEX :CHUNK_INDEX;
-- create index based on sequence numbers
CREATE INDEX ON :CHUNK_FULL_NAME (device_id, _ts_meta_sequence_num);
SET timescaledb.restoring TO OFF;
SET ROLE :ROLE_DEFAULT_PERM_USER;
SELECT comp_ch.table_name AS "CHUNK_NAME", comp_ch.schema_name|| '.' || comp_ch.table_name AS "CHUNK_FULL_NAME"
FROM _timescaledb_catalog.chunk ch1, _timescaledb_catalog.chunk comp_ch, _timescaledb_catalog.hypertable ht
WHERE ch1.hypertable_id = ht.id AND ht.table_name LIKE 'hyper'
AND ch1.compressed_chunk_id = comp_ch.id
ORDER BY ch1.id OFFSET 2 LIMIT 1 \gset
SELECT schemaname || '.' || indexname AS "CHUNK_INDEX" FROM pg_indexes where tablename = :'CHUNK_NAME'
LIMIT 1 \gset
SET ROLE :ROLE_SUPERUSER;
SET timescaledb.restoring TO ON;
-- add sequence number column and fill in the correct sequences
ALTER TABLE :CHUNK_FULL_NAME ADD COLUMN _ts_meta_sequence_num int;
INSERT INTO :CHUNK_FULL_NAME (device_id, time, val, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_sequence_num)
SELECT device_id, time, val, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1 , 10*row_number() over (partition by device_id) as _ts_meta_sequence_num
FROM :CHUNK_FULL_NAME
ORDER BY device_id, _ts_meta_min_1, _ts_meta_max_1;
DELETE FROM :CHUNK_FULL_NAME WHERE _ts_meta_sequence_num IS NULL;
-- drop exising index
DROP INDEX :CHUNK_INDEX;
-- create index based on sequence numbers
CREATE INDEX ON :CHUNK_FULL_NAME (device_id, _ts_meta_sequence_num);
SET timescaledb.restoring TO OFF;
SET ROLE :ROLE_DEFAULT_PERM_USER;
-- from this point, two chunks should use sequence numbers and the rest will work without them
-- output with sequence number chunks, should match output without sequence numbers above
:EXPLAIN SELECT * FROM hyper
ORDER BY device_id, time;
--- QUERY PLAN ---
 Merge Append
   Sort Key: hyper.device_id, hyper."time"
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_1_chunk
         Output: _hyper_1_1_chunk."time", _hyper_1_1_chunk.device_id, _hyper_1_1_chunk.val
         ->  Sort
               Output: compress_hyper_2_5_chunk._ts_meta_count, compress_hyper_2_5_chunk.device_id, compress_hyper_2_5_chunk._ts_meta_min_1, compress_hyper_2_5_chunk._ts_meta_max_1, compress_hyper_2_5_chunk."time", compress_hyper_2_5_chunk.val, compress_hyper_2_5_chunk._ts_meta_sequence_num
               Sort Key: compress_hyper_2_5_chunk.device_id, compress_hyper_2_5_chunk._ts_meta_sequence_num
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_5_chunk
                     Output: compress_hyper_2_5_chunk._ts_meta_count, compress_hyper_2_5_chunk.device_id, compress_hyper_2_5_chunk._ts_meta_min_1, compress_hyper_2_5_chunk._ts_meta_max_1, compress_hyper_2_5_chunk."time", compress_hyper_2_5_chunk.val, compress_hyper_2_5_chunk._ts_meta_sequence_num
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_2_chunk
         Output: _hyper_1_2_chunk."time", _hyper_1_2_chunk.device_id, _hyper_1_2_chunk.val
         ->  Sort
               Output: compress_hyper_2_6_chunk._ts_meta_count, compress_hyper_2_6_chunk.device_id, compress_hyper_2_6_chunk._ts_meta_min_1, compress_hyper_2_6_chunk._ts_meta_max_1, compress_hyper_2_6_chunk."time", compress_hyper_2_6_chunk.val
               Sort Key: compress_hyper_2_6_chunk.device_id, compress_hyper_2_6_chunk._ts_meta_min_1, compress_hyper_2_6_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_6_chunk
                     Output: compress_hyper_2_6_chunk._ts_meta_count, compress_hyper_2_6_chunk.device_id, compress_hyper_2_6_chunk._ts_meta_min_1, compress_hyper_2_6_chunk._ts_meta_max_1, compress_hyper_2_6_chunk."time", compress_hyper_2_6_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_3_chunk
         Output: _hyper_1_3_chunk."time", _hyper_1_3_chunk.device_id, _hyper_1_3_chunk.val
         ->  Sort
               Output: compress_hyper_2_7_chunk._ts_meta_count, compress_hyper_2_7_chunk.device_id, compress_hyper_2_7_chunk._ts_meta_min_1, compress_hyper_2_7_chunk._ts_meta_max_1, compress_hyper_2_7_chunk."time", compress_hyper_2_7_chunk.val, compress_hyper_2_7_chunk._ts_meta_sequence_num
               Sort Key: compress_hyper_2_7_chunk.device_id, compress_hyper_2_7_chunk._ts_meta_sequence_num
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_7_chunk
                     Output: compress_hyper_2_7_chunk._ts_meta_count, compress_hyper_2_7_chunk.device_id, compress_hyper_2_7_chunk._ts_meta_min_1, compress_hyper_2_7_chunk._ts_meta_max_1, compress_hyper_2_7_chunk."time", compress_hyper_2_7_chunk.val, compress_hyper_2_7_chunk._ts_meta_sequence_num
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_4_chunk
         Output: _hyper_1_4_chunk."time", _hyper_1_4_chunk.device_id, _hyper_1_4_chunk.val
         ->  Sort
               Output: compress_hyper_2_8_chunk._ts_meta_count, compress_hyper_2_8_chunk.device_id, compress_hyper_2_8_chunk._ts_meta_min_1, compress_hyper_2_8_chunk._ts_meta_max_1, compress_hyper_2_8_chunk."time", compress_hyper_2_8_chunk.val
               Sort Key: compress_hyper_2_8_chunk.device_id, compress_hyper_2_8_chunk._ts_meta_min_1, compress_hyper_2_8_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_8_chunk
                     Output: compress_hyper_2_8_chunk._ts_meta_count, compress_hyper_2_8_chunk.device_id, compress_hyper_2_8_chunk._ts_meta_min_1, compress_hyper_2_8_chunk._ts_meta_max_1, compress_hyper_2_8_chunk."time", compress_hyper_2_8_chunk.val

SELECT * FROM hyper
ORDER BY device_id, time;
 time | device_id | val 
------+-----------+-----
    1 |         1 |   1
   30 |         1 |   4
    2 |         2 |   1
   21 |         2 |   3
    3 |         3 |   1
   10 |         3 |   2
   22 |         3 |   3
   31 |         3 |   4
   11 |         4 |   2
   23 |         4 |   3
   11 |         5 |   2
   31 |         5 |   4

-- backwards index scan
:EXPLAIN SELECT * FROM hyper
ORDER BY device_id DESC, time DESC;
--- QUERY PLAN ---
 Merge Append
   Sort Key: hyper.device_id DESC, hyper."time" DESC
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_1_chunk
         Output: _hyper_1_1_chunk."time", _hyper_1_1_chunk.device_id, _hyper_1_1_chunk.val
         Reverse: true
         ->  Sort
               Output: compress_hyper_2_5_chunk._ts_meta_count, compress_hyper_2_5_chunk.device_id, compress_hyper_2_5_chunk._ts_meta_min_1, compress_hyper_2_5_chunk._ts_meta_max_1, compress_hyper_2_5_chunk."time", compress_hyper_2_5_chunk.val, compress_hyper_2_5_chunk._ts_meta_sequence_num
               Sort Key: compress_hyper_2_5_chunk.device_id DESC, compress_hyper_2_5_chunk._ts_meta_sequence_num DESC
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_5_chunk
                     Output: compress_hyper_2_5_chunk._ts_meta_count, compress_hyper_2_5_chunk.device_id, compress_hyper_2_5_chunk._ts_meta_min_1, compress_hyper_2_5_chunk._ts_meta_max_1, compress_hyper_2_5_chunk."time", compress_hyper_2_5_chunk.val, compress_hyper_2_5_chunk._ts_meta_sequence_num
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_2_chunk
         Output: _hyper_1_2_chunk."time", _hyper_1_2_chunk.device_id, _hyper_1_2_chunk.val
         Reverse: true
         ->  Sort
               Output: compress_hyper_2_6_chunk._ts_meta_count, compress_hyper_2_6_chunk.device_id, compress_hyper_2_6_chunk._ts_meta_min_1, compress_hyper_2_6_chunk._ts_meta_max_1, compress_hyper_2_6_chunk."time", compress_hyper_2_6_chunk.val
               Sort Key: compress_hyper_2_6_chunk.device_id DESC, compress_hyper_2_6_chunk._ts_meta_min_1 DESC, compress_hyper_2_6_chunk._ts_meta_max_1 DESC
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_6_chunk
                     Output: compress_hyper_2_6_chunk._ts_meta_count, compress_hyper_2_6_chunk.device_id, compress_hyper_2_6_chunk._ts_meta_min_1, compress_hyper_2_6_chunk._ts_meta_max_1, compress_hyper_2_6_chunk."time", compress_hyper_2_6_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_3_chunk
         Output: _hyper_1_3_chunk."time", _hyper_1_3_chunk.device_id, _hyper_1_3_chunk.val
         Reverse: true
         ->  Sort
               Output: compress_hyper_2_7_chunk._ts_meta_count, compress_hyper_2_7_chunk.device_id, compress_hyper_2_7_chunk._ts_meta_min_1, compress_hyper_2_7_chunk._ts_meta_max_1, compress_hyper_2_7_chunk."time", compress_hyper_2_7_chunk.val, compress_hyper_2_7_chunk._ts_meta_sequence_num
               Sort Key: compress_hyper_2_7_chunk.device_id DESC, compress_hyper_2_7_chunk._ts_meta_sequence_num DESC
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_7_chunk
                     Output: compress_hyper_2_7_chunk._ts_meta_count, compress_hyper_2_7_chunk.device_id, compress_hyper_2_7_chunk._ts_meta_min_1, compress_hyper_2_7_chunk._ts_meta_max_1, compress_hyper_2_7_chunk."time", compress_hyper_2_7_chunk.val, compress_hyper_2_7_chunk._ts_meta_sequence_num
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_4_chunk
         Output: _hyper_1_4_chunk."time", _hyper_1_4_chunk.device_id, _hyper_1_4_chunk.val
         Reverse: true
         ->  Sort
               Output: compress_hyper_2_8_chunk._ts_meta_count, compress_hyper_2_8_chunk.device_id, compress_hyper_2_8_chunk._ts_meta_min_1, compress_hyper_2_8_chunk._ts_meta_max_1, compress_hyper_2_8_chunk."time", compress_hyper_2_8_chunk.val
               Sort Key: compress_hyper_2_8_chunk.device_id DESC, compress_hyper_2_8_chunk._ts_meta_min_1 DESC, compress_hyper_2_8_chunk._ts_meta_max_1 DESC
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_8_chunk
                     Output: compress_hyper_2_8_chunk._ts_meta_count, compress_hyper_2_8_chunk.device_id, compress_hyper_2_8_chunk._ts_meta_min_1, compress_hyper_2_8_chunk._ts_meta_max_1, compress_hyper_2_8_chunk."time", compress_hyper_2_8_chunk.val

SELECT * FROM hyper
ORDER BY device_id DESC, time DESC;
 time | device_id | val 
------+-----------+-----
   31 |         5 |   4
   11 |         5 |   2
   23 |         4 |   3
   11 |         4 |   2
   31 |         3 |   4
   22 |         3 |   3
   10 |         3 |   2
    3 |         3 |   1
   21 |         2 |   3
    2 |         2 |   1
   30 |         1 |   4
    1 |         1 |   1

-- test recompression which should do a full recompress of the chunk
-- while dropping sequence numbers
INSERT INTO hyper VALUES (20, 1, 1);
SELECT compress_chunk(show_chunks('hyper'));
NOTICE:  chunk "_hyper_1_1_chunk" is already converted to columnstore
NOTICE:  chunk "_hyper_1_2_chunk" is already converted to columnstore
NOTICE:  in-memory recompression is disabled due to no compressed chunk index, performing decompress/compress on chunk "_timescaledb_internal._hyper_1_3_chunk"
NOTICE:  chunk "_hyper_1_4_chunk" is already converted to columnstore
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_1_1_chunk
 _timescaledb_internal._hyper_1_2_chunk
 _timescaledb_internal._hyper_1_3_chunk
 _timescaledb_internal._hyper_1_4_chunk

VACUUM ANALYZE hyper;
-- removal of sequence numbers from the chunk should be
-- reflected in this plan
:EXPLAIN SELECT * FROM hyper
ORDER BY device_id, time;
--- QUERY PLAN ---
 Merge Append
   Sort Key: hyper.device_id, hyper."time"
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_1_chunk
         Output: _hyper_1_1_chunk."time", _hyper_1_1_chunk.device_id, _hyper_1_1_chunk.val
         ->  Sort
               Output: compress_hyper_2_5_chunk._ts_meta_count, compress_hyper_2_5_chunk.device_id, compress_hyper_2_5_chunk._ts_meta_min_1, compress_hyper_2_5_chunk._ts_meta_max_1, compress_hyper_2_5_chunk."time", compress_hyper_2_5_chunk.val, compress_hyper_2_5_chunk._ts_meta_sequence_num
               Sort Key: compress_hyper_2_5_chunk.device_id, compress_hyper_2_5_chunk._ts_meta_sequence_num
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_5_chunk
                     Output: compress_hyper_2_5_chunk._ts_meta_count, compress_hyper_2_5_chunk.device_id, compress_hyper_2_5_chunk._ts_meta_min_1, compress_hyper_2_5_chunk._ts_meta_max_1, compress_hyper_2_5_chunk."time", compress_hyper_2_5_chunk.val, compress_hyper_2_5_chunk._ts_meta_sequence_num
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_2_chunk
         Output: _hyper_1_2_chunk."time", _hyper_1_2_chunk.device_id, _hyper_1_2_chunk.val
         ->  Sort
               Output: compress_hyper_2_6_chunk._ts_meta_count, compress_hyper_2_6_chunk.device_id, compress_hyper_2_6_chunk._ts_meta_min_1, compress_hyper_2_6_chunk._ts_meta_max_1, compress_hyper_2_6_chunk."time", compress_hyper_2_6_chunk.val
               Sort Key: compress_hyper_2_6_chunk.device_id, compress_hyper_2_6_chunk._ts_meta_min_1, compress_hyper_2_6_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_6_chunk
                     Output: compress_hyper_2_6_chunk._ts_meta_count, compress_hyper_2_6_chunk.device_id, compress_hyper_2_6_chunk._ts_meta_min_1, compress_hyper_2_6_chunk._ts_meta_max_1, compress_hyper_2_6_chunk."time", compress_hyper_2_6_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_3_chunk
         Output: _hyper_1_3_chunk."time", _hyper_1_3_chunk.device_id, _hyper_1_3_chunk.val
         ->  Sort
               Output: compress_hyper_2_9_chunk._ts_meta_count, compress_hyper_2_9_chunk.device_id, compress_hyper_2_9_chunk._ts_meta_min_1, compress_hyper_2_9_chunk._ts_meta_max_1, compress_hyper_2_9_chunk."time", compress_hyper_2_9_chunk.val
               Sort Key: compress_hyper_2_9_chunk.device_id, compress_hyper_2_9_chunk._ts_meta_min_1, compress_hyper_2_9_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_9_chunk
                     Output: compress_hyper_2_9_chunk._ts_meta_count, compress_hyper_2_9_chunk.device_id, compress_hyper_2_9_chunk._ts_meta_min_1, compress_hyper_2_9_chunk._ts_meta_max_1, compress_hyper_2_9_chunk."time", compress_hyper_2_9_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_4_chunk
         Output: _hyper_1_4_chunk."time", _hyper_1_4_chunk.device_id, _hyper_1_4_chunk.val
         ->  Sort
               Output: compress_hyper_2_8_chunk._ts_meta_count, compress_hyper_2_8_chunk.device_id, compress_hyper_2_8_chunk._ts_meta_min_1, compress_hyper_2_8_chunk._ts_meta_max_1, compress_hyper_2_8_chunk."time", compress_hyper_2_8_chunk.val
               Sort Key: compress_hyper_2_8_chunk.device_id, compress_hyper_2_8_chunk._ts_meta_min_1, compress_hyper_2_8_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_8_chunk
                     Output: compress_hyper_2_8_chunk._ts_meta_count, compress_hyper_2_8_chunk.device_id, compress_hyper_2_8_chunk._ts_meta_min_1, compress_hyper_2_8_chunk._ts_meta_max_1, compress_hyper_2_8_chunk."time", compress_hyper_2_8_chunk.val

-- test case without segmentby
TRUNCATE hyper;
ALTER TABLE hyper SET (
    timescaledb.compress,
    timescaledb.compress_orderby = 'time',
    timescaledb.compress_segmentby = '');
INSERT INTO hyper VALUES (1, 1, 1), (2, 2, 1), (3, 3, 1), (10, 3, 2), (11, 4, 2), (11, 5, 2), (21, 2, 3), (22, 3, 3), (23, 4, 3), (30, 1, 4), (31, 3, 4), (31, 5, 4);
SELECT compress_chunk(show_chunks('hyper'));
             compress_chunk              
-----------------------------------------
 _timescaledb_internal._hyper_1_10_chunk
 _timescaledb_internal._hyper_1_11_chunk
 _timescaledb_internal._hyper_1_12_chunk
 _timescaledb_internal._hyper_1_13_chunk

VACUUM ANALYZE hyper;
:EXPLAIN SELECT * FROM hyper
ORDER BY time;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on public.hyper
   Output: hyper."time", hyper.device_id, hyper.val
   Order: hyper."time"
   Startup Exclusion: false
   Runtime Exclusion: false
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_10_chunk
         Output: _hyper_1_10_chunk."time", _hyper_1_10_chunk.device_id, _hyper_1_10_chunk.val
         ->  Sort
               Output: compress_hyper_2_14_chunk._ts_meta_count, compress_hyper_2_14_chunk._ts_meta_min_1, compress_hyper_2_14_chunk._ts_meta_max_1, compress_hyper_2_14_chunk."time", compress_hyper_2_14_chunk.device_id, compress_hyper_2_14_chunk.val
               Sort Key: compress_hyper_2_14_chunk._ts_meta_min_1, compress_hyper_2_14_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_14_chunk
                     Output: compress_hyper_2_14_chunk._ts_meta_count, compress_hyper_2_14_chunk._ts_meta_min_1, compress_hyper_2_14_chunk._ts_meta_max_1, compress_hyper_2_14_chunk."time", compress_hyper_2_14_chunk.device_id, compress_hyper_2_14_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_11_chunk
         Output: _hyper_1_11_chunk."time", _hyper_1_11_chunk.device_id, _hyper_1_11_chunk.val
         ->  Sort
               Output: compress_hyper_2_15_chunk._ts_meta_count, compress_hyper_2_15_chunk._ts_meta_min_1, compress_hyper_2_15_chunk._ts_meta_max_1, compress_hyper_2_15_chunk."time", compress_hyper_2_15_chunk.device_id, compress_hyper_2_15_chunk.val
               Sort Key: compress_hyper_2_15_chunk._ts_meta_min_1, compress_hyper_2_15_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_15_chunk
                     Output: compress_hyper_2_15_chunk._ts_meta_count, compress_hyper_2_15_chunk._ts_meta_min_1, compress_hyper_2_15_chunk._ts_meta_max_1, compress_hyper_2_15_chunk."time", compress_hyper_2_15_chunk.device_id, compress_hyper_2_15_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_12_chunk
         Output: _hyper_1_12_chunk."time", _hyper_1_12_chunk.device_id, _hyper_1_12_chunk.val
         ->  Sort
               Output: compress_hyper_2_16_chunk._ts_meta_count, compress_hyper_2_16_chunk._ts_meta_min_1, compress_hyper_2_16_chunk._ts_meta_max_1, compress_hyper_2_16_chunk."time", compress_hyper_2_16_chunk.device_id, compress_hyper_2_16_chunk.val
               Sort Key: compress_hyper_2_16_chunk._ts_meta_min_1, compress_hyper_2_16_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_16_chunk
                     Output: compress_hyper_2_16_chunk._ts_meta_count, compress_hyper_2_16_chunk._ts_meta_min_1, compress_hyper_2_16_chunk._ts_meta_max_1, compress_hyper_2_16_chunk."time", compress_hyper_2_16_chunk.device_id, compress_hyper_2_16_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_13_chunk
         Output: _hyper_1_13_chunk."time", _hyper_1_13_chunk.device_id, _hyper_1_13_chunk.val
         ->  Sort
               Output: compress_hyper_2_17_chunk._ts_meta_count, compress_hyper_2_17_chunk._ts_meta_min_1, compress_hyper_2_17_chunk._ts_meta_max_1, compress_hyper_2_17_chunk."time", compress_hyper_2_17_chunk.device_id, compress_hyper_2_17_chunk.val
               Sort Key: compress_hyper_2_17_chunk._ts_meta_min_1, compress_hyper_2_17_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_17_chunk
                     Output: compress_hyper_2_17_chunk._ts_meta_count, compress_hyper_2_17_chunk._ts_meta_min_1, compress_hyper_2_17_chunk._ts_meta_max_1, compress_hyper_2_17_chunk."time", compress_hyper_2_17_chunk.device_id, compress_hyper_2_17_chunk.val

-- modify two chunks by adding sequence number to the segments
SELECT comp_ch.table_name AS "CHUNK_NAME", comp_ch.schema_name|| '.' || comp_ch.table_name AS "CHUNK_FULL_NAME"
FROM _timescaledb_catalog.chunk ch1, _timescaledb_catalog.chunk comp_ch, _timescaledb_catalog.hypertable ht
WHERE ch1.hypertable_id = ht.id AND ht.table_name LIKE 'hyper'
AND ch1.compressed_chunk_id = comp_ch.id
ORDER BY ch1.id LIMIT 1 \gset
SET ROLE :ROLE_SUPERUSER;
SET timescaledb.restoring TO ON;
-- add sequence number column and fill in the correct sequences
ALTER TABLE :CHUNK_FULL_NAME ADD COLUMN _ts_meta_sequence_num int;
INSERT INTO :CHUNK_FULL_NAME (device_id, time, val, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_sequence_num)
SELECT device_id, time, val, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1 , 10*row_number() over () as _ts_meta_sequence_num
FROM :CHUNK_FULL_NAME
ORDER BY _ts_meta_min_1, _ts_meta_max_1;
DELETE FROM :CHUNK_FULL_NAME WHERE _ts_meta_sequence_num IS NULL;
SET timescaledb.restoring TO OFF;
SET ROLE :ROLE_DEFAULT_PERM_USER;
SELECT comp_ch.table_name AS "CHUNK_NAME", comp_ch.schema_name|| '.' || comp_ch.table_name AS "CHUNK_FULL_NAME"
FROM _timescaledb_catalog.chunk ch1, _timescaledb_catalog.chunk comp_ch, _timescaledb_catalog.hypertable ht
WHERE ch1.hypertable_id = ht.id AND ht.table_name LIKE 'hyper'
AND ch1.compressed_chunk_id = comp_ch.id
ORDER BY ch1.id OFFSET 3 LIMIT 1 \gset
SET ROLE :ROLE_SUPERUSER;
SET timescaledb.restoring TO ON;
-- add sequence number column and fill in the correct sequences
ALTER TABLE :CHUNK_FULL_NAME ADD COLUMN _ts_meta_sequence_num int;
INSERT INTO :CHUNK_FULL_NAME (device_id, time, val, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1, _ts_meta_sequence_num)
SELECT device_id, time, val, _ts_meta_count, _ts_meta_min_1, _ts_meta_max_1 , 10*row_number() over () as _ts_meta_sequence_num
FROM :CHUNK_FULL_NAME
ORDER BY _ts_meta_min_1, _ts_meta_max_1;
DELETE FROM :CHUNK_FULL_NAME WHERE _ts_meta_sequence_num IS NULL;
SET timescaledb.restoring TO OFF;
SET ROLE :ROLE_DEFAULT_PERM_USER;
-- from this point, two chunks should use sequence numbers and the rest will work without them
:EXPLAIN SELECT * FROM hyper
ORDER BY time;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on public.hyper
   Output: hyper."time", hyper.device_id, hyper.val
   Order: hyper."time"
   Startup Exclusion: false
   Runtime Exclusion: false
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_10_chunk
         Output: _hyper_1_10_chunk."time", _hyper_1_10_chunk.device_id, _hyper_1_10_chunk.val
         ->  Sort
               Output: compress_hyper_2_14_chunk._ts_meta_count, compress_hyper_2_14_chunk._ts_meta_min_1, compress_hyper_2_14_chunk._ts_meta_max_1, compress_hyper_2_14_chunk."time", compress_hyper_2_14_chunk.device_id, compress_hyper_2_14_chunk.val, compress_hyper_2_14_chunk._ts_meta_sequence_num
               Sort Key: compress_hyper_2_14_chunk._ts_meta_sequence_num
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_14_chunk
                     Output: compress_hyper_2_14_chunk._ts_meta_count, compress_hyper_2_14_chunk._ts_meta_min_1, compress_hyper_2_14_chunk._ts_meta_max_1, compress_hyper_2_14_chunk."time", compress_hyper_2_14_chunk.device_id, compress_hyper_2_14_chunk.val, compress_hyper_2_14_chunk._ts_meta_sequence_num
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_11_chunk
         Output: _hyper_1_11_chunk."time", _hyper_1_11_chunk.device_id, _hyper_1_11_chunk.val
         ->  Sort
               Output: compress_hyper_2_15_chunk._ts_meta_count, compress_hyper_2_15_chunk._ts_meta_min_1, compress_hyper_2_15_chunk._ts_meta_max_1, compress_hyper_2_15_chunk."time", compress_hyper_2_15_chunk.device_id, compress_hyper_2_15_chunk.val
               Sort Key: compress_hyper_2_15_chunk._ts_meta_min_1, compress_hyper_2_15_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_15_chunk
                     Output: compress_hyper_2_15_chunk._ts_meta_count, compress_hyper_2_15_chunk._ts_meta_min_1, compress_hyper_2_15_chunk._ts_meta_max_1, compress_hyper_2_15_chunk."time", compress_hyper_2_15_chunk.device_id, compress_hyper_2_15_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_12_chunk
         Output: _hyper_1_12_chunk."time", _hyper_1_12_chunk.device_id, _hyper_1_12_chunk.val
         ->  Sort
               Output: compress_hyper_2_16_chunk._ts_meta_count, compress_hyper_2_16_chunk._ts_meta_min_1, compress_hyper_2_16_chunk._ts_meta_max_1, compress_hyper_2_16_chunk."time", compress_hyper_2_16_chunk.device_id, compress_hyper_2_16_chunk.val
               Sort Key: compress_hyper_2_16_chunk._ts_meta_min_1, compress_hyper_2_16_chunk._ts_meta_max_1
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_16_chunk
                     Output: compress_hyper_2_16_chunk._ts_meta_count, compress_hyper_2_16_chunk._ts_meta_min_1, compress_hyper_2_16_chunk._ts_meta_max_1, compress_hyper_2_16_chunk."time", compress_hyper_2_16_chunk.device_id, compress_hyper_2_16_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_13_chunk
         Output: _hyper_1_13_chunk."time", _hyper_1_13_chunk.device_id, _hyper_1_13_chunk.val
         ->  Sort
               Output: compress_hyper_2_17_chunk._ts_meta_count, compress_hyper_2_17_chunk._ts_meta_min_1, compress_hyper_2_17_chunk._ts_meta_max_1, compress_hyper_2_17_chunk."time", compress_hyper_2_17_chunk.device_id, compress_hyper_2_17_chunk.val, compress_hyper_2_17_chunk._ts_meta_sequence_num
               Sort Key: compress_hyper_2_17_chunk._ts_meta_sequence_num
               ->  Seq Scan on _timescaledb_internal.compress_hyper_2_17_chunk
                     Output: compress_hyper_2_17_chunk._ts_meta_count, compress_hyper_2_17_chunk._ts_meta_min_1, compress_hyper_2_17_chunk._ts_meta_max_1, compress_hyper_2_17_chunk."time", compress_hyper_2_17_chunk.device_id, compress_hyper_2_17_chunk.val, compress_hyper_2_17_chunk._ts_meta_sequence_num

-- test rolling up chunks during compression
-- this will not work with chunks which have sequence numbers
-- since the last chunk has sequence numbers, new chunks will be only rolled up together
ALTER TABLE hyper SET (timescaledb.compress_chunk_time_interval = '30');
INSERT INTO hyper VALUES (41, 1, 1), (42, 2, 1), (43, 3, 1), (50, 3, 2), (51, 4, 2), (51, 5, 2);
-- compression should leave the previous 4 chunks alone
-- and roll up the two new chunks into a single chunk
SELECT compress_chunk(show_chunks('hyper'));
NOTICE:  chunk "_hyper_1_10_chunk" is already converted to columnstore
NOTICE:  chunk "_hyper_1_11_chunk" is already converted to columnstore
NOTICE:  chunk "_hyper_1_12_chunk" is already converted to columnstore
NOTICE:  chunk "_hyper_1_13_chunk" is already converted to columnstore
             compress_chunk              
-----------------------------------------
 _timescaledb_internal._hyper_1_10_chunk
 _timescaledb_internal._hyper_1_11_chunk
 _timescaledb_internal._hyper_1_12_chunk
 _timescaledb_internal._hyper_1_13_chunk
 _timescaledb_internal._hyper_1_18_chunk
 _timescaledb_internal._hyper_1_18_chunk

-- gh issue #7384
-- modifying compressed chunk OID to over INT32_MAX
-- to detect if we handle those properly
TRUNCATE hyper;
ALTER TABLE hyper SET (
    timescaledb.compress,
    timescaledb.compress_orderby = 'time',
    timescaledb.compress_segmentby = 'device_id');
INSERT INTO hyper VALUES (1, 1, 1), (2, 2, 1), (3, 3, 1), (10, 3, 2), (11, 4, 2), (11, 5, 2), (21, 2, 3), (22, 3, 3), (23, 4, 3), (30, 1, 4), (31, 3, 4), (31, 5, 4);
SELECT compress_chunk(show_chunks('hyper'));
             compress_chunk              
-----------------------------------------
 _timescaledb_internal._hyper_1_21_chunk
 _timescaledb_internal._hyper_1_21_chunk
 _timescaledb_internal._hyper_1_21_chunk
 _timescaledb_internal._hyper_1_21_chunk

VACUUM ANALYZE hyper;
SELECT comp_ch.table_name AS "CHUNK_NAME", comp_ch.schema_name|| '.' || comp_ch.table_name AS "CHUNK_FULL_NAME"
FROM _timescaledb_catalog.chunk ch1, _timescaledb_catalog.chunk comp_ch, _timescaledb_catalog.hypertable ht
WHERE ch1.hypertable_id = ht.id AND ht.table_name LIKE 'hyper'
AND ch1.compressed_chunk_id = comp_ch.id
ORDER BY ch1.id LIMIT 1 \gset
SET ROLE :ROLE_SUPERUSER;
SELECT :'CHUNK_FULL_NAME'::regclass::oid as "CHUNK_OID" \gset
SELECT (power(2,31)+1)::bigint as "CHUNK_NEW_OID" \gset
UPDATE pg_class SET oid = :CHUNK_NEW_OID
WHERE oid = :CHUNK_OID;
UPDATE pg_attribute SET attrelid = :CHUNK_NEW_OID
WHERE attrelid = :CHUNK_OID;
UPDATE _timescaledb_catalog.compression_settings SET compress_relid = :CHUNK_NEW_OID
WHERE compress_relid = :CHUNK_OID;
:EXPLAIN SELECT * FROM hyper
WHERE device_id = 1 ORDER BY time;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_21_chunk
   Output: _hyper_1_21_chunk."time", _hyper_1_21_chunk.device_id, _hyper_1_21_chunk.val
   ->  Sort
         Output: compress_hyper_2_25_chunk._ts_meta_count, compress_hyper_2_25_chunk.device_id, compress_hyper_2_25_chunk._ts_meta_min_1, compress_hyper_2_25_chunk._ts_meta_max_1, compress_hyper_2_25_chunk."time", compress_hyper_2_25_chunk.val
         Sort Key: compress_hyper_2_25_chunk._ts_meta_min_1, compress_hyper_2_25_chunk._ts_meta_max_1
         ->  Seq Scan on _timescaledb_internal.compress_hyper_2_25_chunk
               Output: compress_hyper_2_25_chunk._ts_meta_count, compress_hyper_2_25_chunk.device_id, compress_hyper_2_25_chunk._ts_meta_min_1, compress_hyper_2_25_chunk._ts_meta_max_1, compress_hyper_2_25_chunk."time", compress_hyper_2_25_chunk.val
               Filter: (compress_hyper_2_25_chunk.device_id = 1)

SET ROLE :ROLE_DEFAULT_PERM_USER;
DROP TABLE hyper;
-- Test type coercion during metadata EquivalenceClass creation
CREATE TABLE varchar(
    time INT NOT NULL,
    device_id varchar,
	text varchar,
    val INT);
SELECT * FROM create_hypertable('varchar', 'time', chunk_time_interval => 10);
WARNING:  column type "character varying" used for "device_id" does not follow best practices
WARNING:  column type "character varying" used for "text" does not follow best practices
 hypertable_id | schema_name | table_name | created 
---------------+-------------+------------+---------
             3 | public      | varchar    | t

-- test case with segmentby
ALTER TABLE varchar SET (
    timescaledb.compress,
	timescaledb.compress_segmentby = 'device_id',
    timescaledb.compress_orderby = 'text, time');
INSERT INTO varchar SELECT t, t::text, t::text, 1 FROM generate_series(1, 10, 1) t;
SELECT compress_chunk(show_chunks('varchar'));
             compress_chunk              
-----------------------------------------
 _timescaledb_internal._hyper_3_26_chunk
 _timescaledb_internal._hyper_3_27_chunk

VACUUM ANALYZE varchar;
SELECT * FROM varchar WHERE device_id = '1' ORDER BY text, time LIMIT 5;
 time | device_id | text | val 
------+-----------+------+-----
    1 | 1         | 1    |   1

-- index scan
SET enable_seqscan TO OFF;
:EXPLAIN SELECT device_id, text FROM varchar
GROUP BY device_id, text;
--- QUERY PLAN ---
 Finalize GroupAggregate
   Output: "varchar".device_id, "varchar".text
   Group Key: "varchar".device_id, "varchar".text
   ->  Sort
         Output: "varchar".device_id, "varchar".text
         Sort Key: "varchar".device_id, "varchar".text
         ->  Append
               ->  Partial GroupAggregate
                     Output: _hyper_3_26_chunk.device_id, _hyper_3_26_chunk.text
                     Group Key: _hyper_3_26_chunk.device_id, _hyper_3_26_chunk.text
                     ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_3_26_chunk
                           Output: _hyper_3_26_chunk.device_id, _hyper_3_26_chunk.text
                           ->  Index Scan using compress_hyper_4_28_chunk_device_id__ts_meta_min_1__ts_meta_idx on _timescaledb_internal.compress_hyper_4_28_chunk
                                 Output: compress_hyper_4_28_chunk._ts_meta_count, compress_hyper_4_28_chunk.device_id, compress_hyper_4_28_chunk._ts_meta_min_2, compress_hyper_4_28_chunk._ts_meta_max_2, compress_hyper_4_28_chunk."time", compress_hyper_4_28_chunk._ts_meta_min_1, compress_hyper_4_28_chunk._ts_meta_max_1, compress_hyper_4_28_chunk.text, compress_hyper_4_28_chunk.val
               ->  Partial GroupAggregate
                     Output: _hyper_3_27_chunk.device_id, _hyper_3_27_chunk.text
                     Group Key: _hyper_3_27_chunk.device_id, _hyper_3_27_chunk.text
                     ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_3_27_chunk
                           Output: _hyper_3_27_chunk.device_id, _hyper_3_27_chunk.text
                           ->  Index Scan using compress_hyper_4_29_chunk_device_id__ts_meta_min_1__ts_meta_idx on _timescaledb_internal.compress_hyper_4_29_chunk
                                 Output: compress_hyper_4_29_chunk._ts_meta_count, compress_hyper_4_29_chunk.device_id, compress_hyper_4_29_chunk._ts_meta_min_2, compress_hyper_4_29_chunk._ts_meta_max_2, compress_hyper_4_29_chunk."time", compress_hyper_4_29_chunk._ts_meta_min_1, compress_hyper_4_29_chunk._ts_meta_max_1, compress_hyper_4_29_chunk.text, compress_hyper_4_29_chunk.val

SELECT device_id, text FROM varchar
GROUP BY device_id, text;
 device_id | text 
-----------+------
 1         | 1
 10        | 10
 2         | 2
 3         | 3
 4         | 4
 5         | 5
 6         | 6
 7         | 7
 8         | 8
 9         | 9

-- backwards index scan
:EXPLAIN SELECT * FROM varchar
ORDER BY device_id DESC, text DESC;
--- QUERY PLAN ---
 Merge Append
   Sort Key: "varchar".device_id DESC, "varchar".text DESC
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_3_26_chunk
         Output: _hyper_3_26_chunk."time", _hyper_3_26_chunk.device_id, _hyper_3_26_chunk.text, _hyper_3_26_chunk.val
         Reverse: true
         ->  Index Scan Backward using compress_hyper_4_28_chunk_device_id__ts_meta_min_1__ts_meta_idx on _timescaledb_internal.compress_hyper_4_28_chunk
               Output: compress_hyper_4_28_chunk._ts_meta_count, compress_hyper_4_28_chunk.device_id, compress_hyper_4_28_chunk._ts_meta_min_2, compress_hyper_4_28_chunk._ts_meta_max_2, compress_hyper_4_28_chunk."time", compress_hyper_4_28_chunk._ts_meta_min_1, compress_hyper_4_28_chunk._ts_meta_max_1, compress_hyper_4_28_chunk.text, compress_hyper_4_28_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_3_27_chunk
         Output: _hyper_3_27_chunk."time", _hyper_3_27_chunk.device_id, _hyper_3_27_chunk.text, _hyper_3_27_chunk.val
         Reverse: true
         ->  Index Scan Backward using compress_hyper_4_29_chunk_device_id__ts_meta_min_1__ts_meta_idx on _timescaledb_internal.compress_hyper_4_29_chunk
               Output: compress_hyper_4_29_chunk._ts_meta_count, compress_hyper_4_29_chunk.device_id, compress_hyper_4_29_chunk._ts_meta_min_2, compress_hyper_4_29_chunk._ts_meta_max_2, compress_hyper_4_29_chunk."time", compress_hyper_4_29_chunk._ts_meta_min_1, compress_hyper_4_29_chunk._ts_meta_max_1, compress_hyper_4_29_chunk.text, compress_hyper_4_29_chunk.val

SELECT * FROM varchar
ORDER BY device_id DESC, text DESC;
 time | device_id | text | val 
------+-----------+------+-----
    9 | 9         | 9    |   1
    8 | 8         | 8    |   1
    7 | 7         | 7    |   1
    6 | 6         | 6    |   1
    5 | 5         | 5    |   1
    4 | 4         | 4    |   1
    3 | 3         | 3    |   1
    2 | 2         | 2    |   1
   10 | 10        | 10   |   1
    1 | 1         | 1    |   1

SET enable_seqscan TO DEFAULT;
-- seq scan
:EXPLAIN SELECT * FROM varchar
ORDER BY device_id DESC, text DESC;
--- QUERY PLAN ---
 Merge Append
   Sort Key: "varchar".device_id DESC, "varchar".text DESC
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_3_26_chunk
         Output: _hyper_3_26_chunk."time", _hyper_3_26_chunk.device_id, _hyper_3_26_chunk.text, _hyper_3_26_chunk.val
         Reverse: true
         ->  Sort
               Output: compress_hyper_4_28_chunk._ts_meta_count, compress_hyper_4_28_chunk.device_id, compress_hyper_4_28_chunk._ts_meta_min_2, compress_hyper_4_28_chunk._ts_meta_max_2, compress_hyper_4_28_chunk."time", compress_hyper_4_28_chunk._ts_meta_min_1, compress_hyper_4_28_chunk._ts_meta_max_1, compress_hyper_4_28_chunk.text, compress_hyper_4_28_chunk.val
               Sort Key: compress_hyper_4_28_chunk.device_id DESC, compress_hyper_4_28_chunk._ts_meta_min_1 DESC, compress_hyper_4_28_chunk._ts_meta_max_1 DESC
               ->  Seq Scan on _timescaledb_internal.compress_hyper_4_28_chunk
                     Output: compress_hyper_4_28_chunk._ts_meta_count, compress_hyper_4_28_chunk.device_id, compress_hyper_4_28_chunk._ts_meta_min_2, compress_hyper_4_28_chunk._ts_meta_max_2, compress_hyper_4_28_chunk."time", compress_hyper_4_28_chunk._ts_meta_min_1, compress_hyper_4_28_chunk._ts_meta_max_1, compress_hyper_4_28_chunk.text, compress_hyper_4_28_chunk.val
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_3_27_chunk
         Output: _hyper_3_27_chunk."time", _hyper_3_27_chunk.device_id, _hyper_3_27_chunk.text, _hyper_3_27_chunk.val
         Reverse: true
         ->  Sort
               Output: compress_hyper_4_29_chunk._ts_meta_count, compress_hyper_4_29_chunk.device_id, compress_hyper_4_29_chunk._ts_meta_min_2, compress_hyper_4_29_chunk._ts_meta_max_2, compress_hyper_4_29_chunk."time", compress_hyper_4_29_chunk._ts_meta_min_1, compress_hyper_4_29_chunk._ts_meta_max_1, compress_hyper_4_29_chunk.text, compress_hyper_4_29_chunk.val
               Sort Key: compress_hyper_4_29_chunk.device_id DESC, compress_hyper_4_29_chunk._ts_meta_min_1 DESC, compress_hyper_4_29_chunk._ts_meta_max_1 DESC
               ->  Seq Scan on _timescaledb_internal.compress_hyper_4_29_chunk
                     Output: compress_hyper_4_29_chunk._ts_meta_count, compress_hyper_4_29_chunk.device_id, compress_hyper_4_29_chunk._ts_meta_min_2, compress_hyper_4_29_chunk._ts_meta_max_2, compress_hyper_4_29_chunk."time", compress_hyper_4_29_chunk._ts_meta_min_1, compress_hyper_4_29_chunk._ts_meta_max_1, compress_hyper_4_29_chunk.text, compress_hyper_4_29_chunk.val

SELECT * FROM varchar
ORDER BY device_id DESC, text DESC;
 time | device_id | text | val 
------+-----------+------+-----
    9 | 9         | 9    |   1
    8 | 8         | 8    |   1
    7 | 7         | 7    |   1
    6 | 6         | 6    |   1
    5 | 5         | 5    |   1
    4 | 4         | 4    |   1
    3 | 3         | 3    |   1
    2 | 2         | 2    |   1
   10 | 10        | 10   |   1
    1 | 1         | 1    |   1

DROP TABLE varchar;
