-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
\c :TEST_DBNAME :ROLE_SUPERUSER
\set TEST_BASE_NAME ordered_append
SELECT format('include/%s_load.sql', :'TEST_BASE_NAME') AS "TEST_LOAD_NAME",
       format('include/%s.sql', :'TEST_BASE_NAME') as "TEST_QUERY_NAME",
       format('%s/results/%s_results_uncompressed.out', :'TEST_OUTPUT_DIR', :'TEST_BASE_NAME') as "TEST_RESULTS_UNCOMPRESSED",
       format('%s/results/%s_results_compressed.out', :'TEST_OUTPUT_DIR', :'TEST_BASE_NAME') as "TEST_RESULTS_COMPRESSED"
\gset
SELECT format('\! diff -u --label "Uncompressed results" --label "Compressed results" %s %s', :'TEST_RESULTS_UNCOMPRESSED', :'TEST_RESULTS_COMPRESSED') as "DIFF_CMD"
\gset
\ir :TEST_LOAD_NAME
-- 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.
-- create normal hypertable with dropped columns, each chunk will have different attribute numbers
CREATE TABLE metrics(filler_1 int, filler_2 int, filler_3 int, time timestamptz NOT NULL, device_id int, v0 int, v1 int, v2 float, v3 float);
SELECT create_hypertable('metrics','time',create_default_indexes:=false);
  create_hypertable   
----------------------
 (1,public,metrics,t)

ALTER TABLE metrics DROP COLUMN filler_1;
INSERT INTO metrics(time,device_id,v0,v1,v2,v3) SELECT time, device_id, device_id+1,  device_id + 2, device_id + 0.5, NULL FROM generate_series('2000-01-01 0:00:00+0'::timestamptz,'2000-01-05 23:55:00+0','2m') gtime(time), generate_series(1,5,1) gdevice(device_id);
ALTER TABLE metrics DROP COLUMN filler_2;
INSERT INTO metrics(time,device_id,v0,v1,v2,v3) SELECT time, device_id, device_id-1, device_id + 2, device_id + 0.5, NULL FROM generate_series('2000-01-06 0:00:00+0'::timestamptz,'2000-01-12 23:55:00+0','2m') gtime(time), generate_series(1,5,1) gdevice(device_id);
ALTER TABLE metrics DROP COLUMN filler_3;
INSERT INTO metrics(time,device_id,v0,v1,v2,v3) SELECT time, device_id, device_id, device_id + 2, device_id + 0.5, NULL FROM generate_series('2000-01-13 0:00:00+0'::timestamptz,'2000-01-19 23:55:00+0','2m') gtime(time), generate_series(1,5,1) gdevice(device_id);
CREATE INDEX ON metrics(time DESC);
CREATE INDEX ON metrics(device_id,time DESC);
ANALYZE metrics;
-- create identical hypertable with space partitioning
CREATE TABLE metrics_space(filler_1 int, filler_2 int, filler_3 int, time timestamptz NOT NULL, device_id int, v0 int, v1 int, v2 float, v3 float);
SELECT create_hypertable('metrics_space','time','device_id',3,create_default_indexes:=false);
     create_hypertable      
----------------------------
 (2,public,metrics_space,t)

ALTER TABLE metrics_space DROP COLUMN filler_1;
INSERT INTO metrics_space(time,device_id,v0,v1,v2,v3) SELECT time, device_id, device_id+1, device_id + 2, device_id + 0.5, NULL FROM generate_series('2000-01-01 0:00:00+0'::timestamptz,'2000-01-05 23:55:00+0','2m') gtime(time), generate_series(1,5,1) gdevice(device_id);
ALTER TABLE metrics_space DROP COLUMN filler_2;
INSERT INTO metrics_space(time,device_id,v0,v1,v2,v3) SELECT time, device_id, device_id+1, device_id + 2, device_id + 0.5, NULL FROM generate_series('2000-01-06 0:00:00+0'::timestamptz,'2000-01-12 23:55:00+0','2m') gtime(time), generate_series(1,5,1) gdevice(device_id);
ALTER TABLE metrics_space DROP COLUMN filler_3;
INSERT INTO metrics_space(time,device_id,v0,v1,v2,v3) SELECT time, device_id, device_id+1, device_id + 2, device_id + 0.5, NULL FROM generate_series('2000-01-13 0:00:00+0'::timestamptz,'2000-01-19 23:55:00+0','2m') gtime(time), generate_series(1,5,1) gdevice(device_id);
CREATE INDEX ON metrics_space(time);
CREATE INDEX ON metrics_space(device_id,time);
ANALYZE metrics_space;
-- create hypertable with compression
CREATE TABLE metrics_compressed(filler_1 int, filler_2 int, filler_3 int, time timestamptz NOT NULL, device_id int, v0 int, v1 int, v2 float, v3 float);
SELECT create_hypertable('metrics_compressed','time',create_default_indexes:=false);
        create_hypertable        
---------------------------------
 (3,public,metrics_compressed,t)

ALTER TABLE metrics_compressed DROP COLUMN filler_1;
INSERT INTO metrics_compressed(time,device_id,v0,v1,v2,v3) SELECT time, device_id, device_id+1,  device_id + 2, device_id + 0.5, NULL FROM generate_series('2000-01-01 0:00:00+0'::timestamptz,'2000-01-05 23:55:00+0','2m') gtime(time), generate_series(1,5,1) gdevice(device_id);
ALTER TABLE metrics_compressed DROP COLUMN filler_2;
INSERT INTO metrics_compressed(time,device_id,v0,v1,v2,v3) SELECT time, device_id, device_id-1, device_id + 2, device_id + 0.5, NULL FROM generate_series('2000-01-06 0:00:00+0'::timestamptz,'2000-01-12 23:55:00+0','2m') gtime(time), generate_series(1,5,1) gdevice(device_id);
ALTER TABLE metrics_compressed DROP COLUMN filler_3;
INSERT INTO metrics_compressed(time,device_id,v0,v1,v2,v3) SELECT time, device_id, device_id, device_id + 2, device_id + 0.5, NULL FROM generate_series('2000-01-13 0:00:00+0'::timestamptz,'2000-01-19 23:55:00+0','2m') gtime(time), generate_series(1,5,1) gdevice(device_id);
CREATE INDEX ON metrics_compressed(time);
CREATE INDEX ON metrics_compressed(device_id,time);
ANALYZE metrics_compressed;
-- compress chunks
ALTER TABLE metrics_compressed SET (timescaledb.compress, timescaledb.compress_orderby='time DESC', timescaledb.compress_segmentby='device_id');
SELECT compress_chunk(show_chunks('metrics_compressed'));
             compress_chunk              
-----------------------------------------
 _timescaledb_internal._hyper_3_13_chunk
 _timescaledb_internal._hyper_3_14_chunk
 _timescaledb_internal._hyper_3_15_chunk

-- Reindexing compressed hypertable to update statistics
-- this is for planner tests which depend on them
-- necessary because this operation was previously done by compress_chunk
REINDEX TABLE _timescaledb_internal._compressed_hypertable_4;
-- create hypertable with space partitioning and compression
CREATE TABLE metrics_space_compressed(filler_1 int, filler_2 int, filler_3 int, time timestamptz NOT NULL, device_id int, v0 int, v1 int, v2 float, v3 float);
SELECT create_hypertable('metrics_space_compressed','time','device_id',3,create_default_indexes:=false);
           create_hypertable           
---------------------------------------
 (5,public,metrics_space_compressed,t)

ALTER TABLE metrics_space_compressed DROP COLUMN filler_1;
INSERT INTO metrics_space_compressed(time,device_id,v0,v1,v2,v3) SELECT time, device_id, device_id+1, device_id + 2, device_id + 0.5, NULL FROM generate_series('2000-01-01 0:00:00+0'::timestamptz,'2000-01-05 23:55:00+0','2m') gtime(time), generate_series(1,5,1) gdevice(device_id);
ALTER TABLE metrics_space_compressed DROP COLUMN filler_2;
INSERT INTO metrics_space_compressed(time,device_id,v0,v1,v2,v3) SELECT time, device_id, device_id+1, device_id + 2, device_id + 0.5, NULL FROM generate_series('2000-01-06 0:00:00+0'::timestamptz,'2000-01-12 23:55:00+0','2m') gtime(time), generate_series(1,5,1) gdevice(device_id);
ALTER TABLE metrics_space_compressed DROP COLUMN filler_3;
INSERT INTO metrics_space_compressed(time,device_id,v0,v1,v2,v3) SELECT time, device_id, device_id+1, device_id + 2, device_id + 0.5, NULL FROM generate_series('2000-01-13 0:00:00+0'::timestamptz,'2000-01-19 23:55:00+0','2m') gtime(time), generate_series(1,5,1) gdevice(device_id);
CREATE INDEX ON metrics_space_compressed(time);
CREATE INDEX ON metrics_space_compressed(device_id,time);
ANALYZE metrics_space_compressed;
-- compress chunks
ALTER TABLE metrics_space_compressed SET (timescaledb.compress, timescaledb.compress_orderby='time DESC', timescaledb.compress_segmentby='device_id');
SELECT compress_chunk(show_chunks('metrics_space_compressed'));
             compress_chunk              
-----------------------------------------
 _timescaledb_internal._hyper_5_19_chunk
 _timescaledb_internal._hyper_5_20_chunk
 _timescaledb_internal._hyper_5_21_chunk
 _timescaledb_internal._hyper_5_22_chunk
 _timescaledb_internal._hyper_5_23_chunk
 _timescaledb_internal._hyper_5_24_chunk
 _timescaledb_internal._hyper_5_25_chunk
 _timescaledb_internal._hyper_5_26_chunk
 _timescaledb_internal._hyper_5_27_chunk

-- Reindexing compressed hypertable to update statistics
-- this is for planner tests which depend on them
-- necessary because this operation was previously done by compress_chunk
REINDEX TABLE _timescaledb_internal._compressed_hypertable_6;
-- get EXPLAIN output for all variations
\set PREFIX 'EXPLAIN (analyze, buffers off, costs off, timing off, summary off)'
\set PREFIX_VERBOSE 'EXPLAIN (analyze, buffers off, costs off, timing off, summary off, verbose)'
set work_mem to '64MB';
set max_parallel_workers_per_gather to 0;
set enable_nestloop to off;
\set TEST_TABLE 'metrics'
\ir :TEST_QUERY_NAME
-- 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.
-- In the following test cases, we test that certain indexes are used. By using the
-- timescaledb.enable_decompression_sorted_merge optimization, we are pushing a sort node
-- below the DecompressChunk node, which operates on the batches. This could lead to flaky
-- tests because the input data is small and PostgreSQL switches from IndexScans to
-- SequentialScans. Disable the optimization for the following tests to ensure we have
-- stable query plans in all CI environments.
SET timescaledb.enable_decompression_sorted_merge = 0;
vacuum analyze :TEST_TABLE;
-- test ASC for ordered chunks
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
         Order: metrics."time"
         ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
         ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)

-- test DESC for ordered chunks
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
         Order: metrics."time" DESC
         ->  Index Only Scan using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
         ->  Index Only Scan using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (never executed)

-- test query with ORDER BY column not in targetlist
:PREFIX
SELECT pg_typeof(device_id),
  pg_typeof(v2)
FROM :TEST_TABLE
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
               Order: metrics."time"
               ->  Index Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=1.00 loops=1)
               ->  Index Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
               ->  Index Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)

-- ORDER BY may include other columns after time column
:PREFIX
SELECT time,
  device_id,
  v0
FROM :TEST_TABLE
WHERE device_id = 1
ORDER BY time DESC,
  device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
         Order: metrics."time" DESC
         ->  Index Scan using _hyper_1_3_chunk_metrics_device_id_time_idx on _hyper_1_3_chunk (actual rows=1.00 loops=1)
               Index Cond: (device_id = 1)
         ->  Index Scan using _hyper_1_2_chunk_metrics_device_id_time_idx on _hyper_1_2_chunk (never executed)
               Index Cond: (device_id = 1)
         ->  Index Scan using _hyper_1_1_chunk_metrics_device_id_time_idx on _hyper_1_1_chunk (never executed)
               Index Cond: (device_id = 1)

-- test RECORD in targetlist
:PREFIX
SELECT (time,
    device_id,
    v0)
FROM :TEST_TABLE
WHERE device_id = 1
ORDER BY time DESC,
  device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
               Order: metrics."time" DESC
               ->  Index Scan using _hyper_1_3_chunk_metrics_device_id_time_idx on _hyper_1_3_chunk (actual rows=1.00 loops=1)
                     Index Cond: (device_id = 1)
               ->  Index Scan using _hyper_1_2_chunk_metrics_device_id_time_idx on _hyper_1_2_chunk (never executed)
                     Index Cond: (device_id = 1)
               ->  Index Scan using _hyper_1_1_chunk_metrics_device_id_time_idx on _hyper_1_1_chunk (never executed)
                     Index Cond: (device_id = 1)

-- test sort column not in targetlist
:PREFIX
SELECT time_bucket('1h', time)
FROM :TEST_TABLE
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
               Order: metrics."time" DESC
               ->  Index Only Scan using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (actual rows=1.00 loops=1)
               ->  Index Only Scan using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
               ->  Index Only Scan using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (never executed)

-- queries with ORDER BY non-time column shouldn't use ordered append
:PREFIX
SELECT device_id
FROM :TEST_TABLE
ORDER BY device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Merge Append (actual rows=1.00 loops=1)
         Sort Key: metrics.device_id
         ->  Index Only Scan using _hyper_1_1_chunk_metrics_device_id_time_idx on _hyper_1_1_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_1_2_chunk_metrics_device_id_time_idx on _hyper_1_2_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_1_3_chunk_metrics_device_id_time_idx on _hyper_1_3_chunk (actual rows=1.00 loops=1)

-- time column must be primary sort order
SET enable_seqscan TO false;
:PREFIX
SELECT time,
  device_id
FROM :TEST_TABLE
WHERE device_id IN (1, 2)
ORDER BY device_id,
  time
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Incremental Sort (actual rows=1.00 loops=1)
         Sort Key: metrics.device_id, metrics."time"
         Presorted Key: metrics.device_id
         Full-sort Groups: 1  Sort Method: top-N heapsort 
         Pre-sorted Groups: 1  Sort Method: top-N heapsort 
         ->  Merge Append (actual rows=13675.00 loops=1)
               Sort Key: metrics.device_id
               ->  Index Only Scan using _hyper_1_1_chunk_metrics_device_id_time_idx on _hyper_1_1_chunk (actual rows=3599.00 loops=1)
                     Index Cond: (device_id = ANY ('{1,2}'::integer[]))
               ->  Index Only Scan using _hyper_1_2_chunk_metrics_device_id_time_idx on _hyper_1_2_chunk (actual rows=5039.00 loops=1)
                     Index Cond: (device_id = ANY ('{1,2}'::integer[]))
               ->  Index Only Scan using _hyper_1_3_chunk_metrics_device_id_time_idx on _hyper_1_3_chunk (actual rows=5039.00 loops=1)
                     Index Cond: (device_id = ANY ('{1,2}'::integer[]))

RESET enable_seqscan;
-- test equality constraint on ORDER BY prefix
-- currently not optimized
SET enable_seqscan TO false;
SET enable_indexonlyscan TO false;
:PREFIX
SELECT time,
  device_id
FROM :TEST_TABLE
WHERE device_id = 1
ORDER BY device_id,
  time
LIMIT 10;
--- QUERY PLAN ---
 Limit (actual rows=10.00 loops=1)
   ->  Merge Append (actual rows=10.00 loops=1)
         Sort Key: metrics."time"
         ->  Index Scan Backward using _hyper_1_1_chunk_metrics_device_id_time_idx on _hyper_1_1_chunk (actual rows=10.00 loops=1)
               Index Cond: (device_id = 1)
         ->  Index Scan Backward using _hyper_1_2_chunk_metrics_device_id_time_idx on _hyper_1_2_chunk (actual rows=1.00 loops=1)
               Index Cond: (device_id = 1)
         ->  Index Scan Backward using _hyper_1_3_chunk_metrics_device_id_time_idx on _hyper_1_3_chunk (actual rows=1.00 loops=1)
               Index Cond: (device_id = 1)

RESET enable_seqscan;
RESET enable_indexonlyscan;
-- queries without LIMIT should use ordered append
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE device_id IN (1, 2) OR time > '2000-01-06'
ORDER BY time ASC;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on metrics (actual rows=56853.00 loops=1)
   Order: metrics."time"
   ->  Index Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=7196.00 loops=1)
         Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
         Rows Removed by Filter: 10794
   ->  Index Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (actual rows=24467.00 loops=1)
         Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
         Rows Removed by Filter: 723
   ->  Index Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (actual rows=25190.00 loops=1)
         Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))

-- queries without ORDER BY shouldnt use ordered append
SET enable_seqscan TO false;
:PREFIX
SELECT pg_typeof(time)
FROM :TEST_TABLE
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Append (actual rows=1.00 loops=1)
               ->  Index Only Scan using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=1.00 loops=1)
               ->  Index Only Scan using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
               ->  Index Only Scan using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)

RESET enable_seqscan;
-- test interaction with constraint exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-07'
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
         Order: metrics."time"
         ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (actual rows=1.00 loops=1)
               Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
         ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)

:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-07'
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
         Order: metrics."time" DESC
         ->  Index Only Scan using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
               Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)

-- test interaction with runtime exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-08'::text::timestamptz
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
         Order: metrics."time"
         Chunks excluded during startup: 1
         ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (actual rows=1.00 loops=1)
               Index Cond: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
         ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)
               Index Cond: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)

:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time < '2000-01-08'::text::timestamptz
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
         Order: metrics."time"
         Chunks excluded during startup: 1
         ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=1.00 loops=1)
               Index Cond: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
         ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
               Index Cond: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)

-- test constraint exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-08'::text::timestamptz
  AND time < '2000-01-10'
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
         Order: metrics."time"
         Chunks excluded during startup: 1
         ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (actual rows=1.00 loops=1)
               Index Cond: (("time" > ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone))

:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time < '2000-01-08'::text::timestamptz
  AND time > '2000-01-07'
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
         Order: metrics."time"
         Chunks excluded during startup: 1
         ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (actual rows=1.00 loops=1)
               Index Cond: (("time" < ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone))

-- Disable hash aggregation to get a deterministic test output
SET enable_hashagg = OFF;
-- min/max queries
:PREFIX
SELECT max(time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
                 Order: metrics."time" DESC
                 ->  Index Only Scan using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (actual rows=1.00 loops=1)
                 ->  Index Only Scan using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
                 ->  Index Only Scan using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (never executed)

:PREFIX
SELECT min(time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
                 Order: metrics."time"
                 ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=1.00 loops=1)
                 ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
                 ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)

-- test first/last (doesn't use ordered append yet)
:PREFIX
SELECT first(time, time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Result (actual rows=1.00 loops=1)
                 ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
                       Order: metrics."time"
                       ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=1.00 loops=1)
                       ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
                       ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)

:PREFIX
SELECT last(time, time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Result (actual rows=1.00 loops=1)
                 ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
                       Order: metrics."time" DESC
                       ->  Index Only Scan using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (actual rows=1.00 loops=1)
                       ->  Index Only Scan using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
                       ->  Index Only Scan using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (never executed)

-- test query with time_bucket
:PREFIX
SELECT time_bucket('1d', time)
FROM :TEST_TABLE
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
               Order: metrics."time"
               ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=1.00 loops=1)
               ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
               ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)

-- test query with ORDER BY time_bucket
:PREFIX
SELECT time_bucket('1d', time)
FROM :TEST_TABLE
ORDER BY 1
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
               Order: time_bucket('@ 1 day'::interval, metrics."time")
               ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=1.00 loops=1)
               ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
               ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)

-- test query with ORDER BY time_bucket, device_id
-- must not use ordered append
:PREFIX
SELECT time_bucket('1d', time),
  device_id,
  v0
FROM :TEST_TABLE
WHERE device_id IN (1, 2)
ORDER BY time_bucket('1d', time),
  device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Sort (actual rows=1.00 loops=1)
         Sort Key: (time_bucket('@ 1 day'::interval, metrics."time")), metrics.device_id
         Sort Method: top-N heapsort 
         ->  Result (actual rows=27348.00 loops=1)
               ->  Append (actual rows=27348.00 loops=1)
                     ->  Index Scan using _hyper_1_1_chunk_metrics_device_id_time_idx on _hyper_1_1_chunk (actual rows=7196.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Index Scan using _hyper_1_2_chunk_metrics_device_id_time_idx on _hyper_1_2_chunk (actual rows=10076.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Index Scan using _hyper_1_3_chunk_metrics_device_id_time_idx on _hyper_1_3_chunk (actual rows=10076.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))

-- test query with ORDER BY date_trunc
:PREFIX
SELECT time_bucket('1d', time)
FROM :TEST_TABLE
ORDER BY date_trunc('day', time)
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
               Order: date_trunc('day'::text, metrics."time")
               ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=1.00 loops=1)
               ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
               ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)

-- test query with ORDER BY date_trunc
:PREFIX
SELECT date_trunc('day', time)
FROM :TEST_TABLE
ORDER BY 1
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
               Order: date_trunc('day'::text, metrics."time")
               ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=1.00 loops=1)
               ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
               ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)

-- test query with ORDER BY date_trunc, device_id
-- must not use ordered append
:PREFIX
SELECT date_trunc('day', time),
  device_id,
  v0
FROM :TEST_TABLE
WHERE device_id IN (1, 2)
ORDER BY 1,
  2
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Sort (actual rows=1.00 loops=1)
         Sort Key: (date_trunc('day'::text, metrics."time")), metrics.device_id
         Sort Method: top-N heapsort 
         ->  Result (actual rows=27348.00 loops=1)
               ->  Append (actual rows=27348.00 loops=1)
                     ->  Index Scan using _hyper_1_1_chunk_metrics_device_id_time_idx on _hyper_1_1_chunk (actual rows=7196.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Index Scan using _hyper_1_2_chunk_metrics_device_id_time_idx on _hyper_1_2_chunk (actual rows=10076.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Index Scan using _hyper_1_3_chunk_metrics_device_id_time_idx on _hyper_1_3_chunk (actual rows=10076.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))

-- test query with now() should result in ordered ChunkAppend
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time < now() + '1 month'
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=1.00 loops=1)
         Order: metrics."time" DESC
         Chunks excluded during startup: 0
         ->  Index Only Scan using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (actual rows=1.00 loops=1)
               Index Cond: ("time" < (now() + '@ 1 mon'::interval))
         ->  Index Only Scan using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
               Index Cond: ("time" < (now() + '@ 1 mon'::interval))
         ->  Index Only Scan using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (never executed)
               Index Cond: ("time" < (now() + '@ 1 mon'::interval))

-- test CTE
:PREFIX WITH i AS (
  SELECT time
  FROM :TEST_TABLE
  WHERE time < now()
  ORDER BY time DESC
  LIMIT 100
)
SELECT *
FROM i;
--- QUERY PLAN ---
 Limit (actual rows=100.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=100.00 loops=1)
         Order: metrics."time" DESC
         Chunks excluded during startup: 0
         ->  Index Only Scan using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (actual rows=100.00 loops=1)
               Index Cond: ("time" < now())
         ->  Index Only Scan using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
               Index Cond: ("time" < now())
         ->  Index Only Scan using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (never executed)
               Index Cond: ("time" < now())

-- test CTE
-- no chunk exclusion for CTE because cte query is not pulled up
SET enable_seqscan TO false;
:PREFIX WITH cte AS (
  SELECT time
  FROM :TEST_TABLE
  WHERE device_id = 1
  ORDER BY time
)
SELECT *
FROM cte
WHERE time < '2000-02-01'::timestamptz;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on metrics (actual rows=13674.00 loops=1)
   Order: metrics."time"
   ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_device_id_time_idx on _hyper_1_1_chunk (actual rows=3598.00 loops=1)
         Index Cond: (device_id = 1)
   ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_device_id_time_idx on _hyper_1_2_chunk (actual rows=5038.00 loops=1)
         Index Cond: (device_id = 1)
   ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_device_id_time_idx on _hyper_1_3_chunk (actual rows=5038.00 loops=1)
         Index Cond: (device_id = 1)

RESET enable_seqscan;
-- test subquery
-- not ChunkAppend so no chunk exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time = (
    SELECT max(time)
    FROM :TEST_TABLE)
ORDER BY time;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on metrics (actual rows=5.00 loops=1)
   Chunks excluded during runtime: 2
   InitPlan 2
     ->  Result (actual rows=1.00 loops=1)
           InitPlan 1
             ->  Limit (actual rows=1.00 loops=1)
                   ->  Custom Scan (ChunkAppend) on metrics metrics_1 (actual rows=1.00 loops=1)
                         Order: metrics_1."time" DESC
                         ->  Index Only Scan using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk _hyper_1_3_chunk_1 (actual rows=1.00 loops=1)
                         ->  Index Only Scan using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk _hyper_1_2_chunk_1 (never executed)
                         ->  Index Only Scan using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk _hyper_1_1_chunk_1 (never executed)
   ->  Index Only Scan using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (never executed)
         Index Cond: ("time" = (InitPlan 2).col1)
   ->  Index Only Scan using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
         Index Cond: ("time" = (InitPlan 2).col1)
   ->  Index Only Scan using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (actual rows=5.00 loops=1)
         Index Cond: ("time" = (InitPlan 2).col1)

-- test ordered append with limit expression
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT (
  SELECT length('four'));
--- QUERY PLAN ---
 Limit (actual rows=4.00 loops=1)
   InitPlan 1
     ->  Result (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=4.00 loops=1)
         Order: metrics."time"
         ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=4.00 loops=1)
         ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
         ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)

-- test with ordered guc disabled
SET timescaledb.enable_ordered_append TO OFF;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Merge Append (actual rows=3.00 loops=1)
         Sort Key: metrics."time"
         ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=3.00 loops=1)
         ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (actual rows=1.00 loops=1)

RESET timescaledb.enable_ordered_append;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=3.00 loops=1)
         Order: metrics."time"
         ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=3.00 loops=1)
         ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
         ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)

-- test with chunk append disabled
SET timescaledb.enable_chunk_append TO OFF;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Merge Append (actual rows=3.00 loops=1)
         Sort Key: metrics."time"
         ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=3.00 loops=1)
         ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (actual rows=1.00 loops=1)

RESET timescaledb.enable_chunk_append;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics (actual rows=3.00 loops=1)
         Order: metrics."time"
         ->  Index Only Scan Backward using _hyper_1_1_chunk_metrics_time_idx on _hyper_1_1_chunk (actual rows=3.00 loops=1)
         ->  Index Only Scan Backward using _hyper_1_2_chunk_metrics_time_idx on _hyper_1_2_chunk (never executed)
         ->  Index Only Scan Backward using _hyper_1_3_chunk_metrics_time_idx on _hyper_1_3_chunk (never executed)

\set TEST_TABLE 'metrics_space'
\ir :TEST_QUERY_NAME
-- 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.
-- In the following test cases, we test that certain indexes are used. By using the
-- timescaledb.enable_decompression_sorted_merge optimization, we are pushing a sort node
-- below the DecompressChunk node, which operates on the batches. This could lead to flaky
-- tests because the input data is small and PostgreSQL switches from IndexScans to
-- SequentialScans. Disable the optimization for the following tests to ensure we have
-- stable query plans in all CI environments.
SET timescaledb.enable_decompression_sorted_merge = 0;
vacuum analyze :TEST_TABLE;
-- test ASC for ordered chunks
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
         Order: metrics_space."time"
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=1.00 loops=1)
               ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=1.00 loops=1)
               ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
               ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
               ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
               ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
               ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)

-- test DESC for ordered chunks
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
         Order: metrics_space."time" DESC
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space."time" DESC
               ->  Index Only Scan Backward using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (actual rows=1.00 loops=1)
               ->  Index Only Scan Backward using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (actual rows=1.00 loops=1)
               ->  Index Only Scan Backward using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time" DESC
               ->  Index Only Scan Backward using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
               ->  Index Only Scan Backward using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
               ->  Index Only Scan Backward using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time" DESC
               ->  Index Only Scan Backward using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (never executed)
               ->  Index Only Scan Backward using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (never executed)
               ->  Index Only Scan Backward using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (never executed)

-- test query with ORDER BY column not in targetlist
:PREFIX
SELECT pg_typeof(device_id),
  pg_typeof(v2)
FROM :TEST_TABLE
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
               Order: metrics_space."time"
               ->  Merge Append (actual rows=1.00 loops=1)
                     Sort Key: metrics_space."time"
                     ->  Index Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=1.00 loops=1)
                     ->  Index Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=1.00 loops=1)
                     ->  Index Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
               ->  Merge Append (never executed)
                     Sort Key: metrics_space."time"
                     ->  Index Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                     ->  Index Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                     ->  Index Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
               ->  Merge Append (never executed)
                     Sort Key: metrics_space."time"
                     ->  Index Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
                     ->  Index Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
                     ->  Index Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)

-- ORDER BY may include other columns after time column
:PREFIX
SELECT time,
  device_id,
  v0
FROM :TEST_TABLE
WHERE device_id = 1
ORDER BY time DESC,
  device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
         Order: metrics_space."time" DESC
         ->  Index Scan Backward using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
               Filter: (device_id = 1)
         ->  Index Scan Backward using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
               Filter: (device_id = 1)
         ->  Index Scan Backward using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (never executed)
               Filter: (device_id = 1)

-- test RECORD in targetlist
:PREFIX
SELECT (time,
    device_id,
    v0)
FROM :TEST_TABLE
WHERE device_id = 1
ORDER BY time DESC,
  device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
               Order: metrics_space."time" DESC
               ->  Index Scan Backward using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
                     Filter: (device_id = 1)
               ->  Index Scan Backward using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                     Filter: (device_id = 1)
               ->  Index Scan Backward using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (never executed)
                     Filter: (device_id = 1)

-- test sort column not in targetlist
:PREFIX
SELECT time_bucket('1h', time)
FROM :TEST_TABLE
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
               Order: metrics_space."time" DESC
               ->  Merge Append (actual rows=1.00 loops=1)
                     Sort Key: metrics_space."time" DESC
                     ->  Index Only Scan Backward using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (actual rows=1.00 loops=1)
                     ->  Index Only Scan Backward using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (actual rows=1.00 loops=1)
                     ->  Index Only Scan Backward using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
               ->  Merge Append (never executed)
                     Sort Key: metrics_space."time" DESC
                     ->  Index Only Scan Backward using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
                     ->  Index Only Scan Backward using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                     ->  Index Only Scan Backward using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
               ->  Merge Append (never executed)
                     Sort Key: metrics_space."time" DESC
                     ->  Index Only Scan Backward using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (never executed)
                     ->  Index Only Scan Backward using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (never executed)
                     ->  Index Only Scan Backward using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (never executed)

-- queries with ORDER BY non-time column shouldn't use ordered append
:PREFIX
SELECT device_id
FROM :TEST_TABLE
ORDER BY device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Merge Append (actual rows=1.00 loops=1)
         Sort Key: metrics_space.device_id
         ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_device_id_time_idx on _hyper_2_4_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_device_id_time_idx on _hyper_2_5_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_device_id_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_device_id_time_idx on _hyper_2_7_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_device_id_time_idx on _hyper_2_8_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_device_id_time_idx on _hyper_2_9_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_device_id_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_device_id_time_idx on _hyper_2_11_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_device_id_time_idx on _hyper_2_12_chunk (actual rows=1.00 loops=1)

-- time column must be primary sort order
SET enable_seqscan TO false;
:PREFIX
SELECT time,
  device_id
FROM :TEST_TABLE
WHERE device_id IN (1, 2)
ORDER BY device_id,
  time
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Merge Append (actual rows=1.00 loops=1)
         Sort Key: metrics_space.device_id, metrics_space."time"
         ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_device_id_time_idx on _hyper_2_4_chunk (actual rows=1.00 loops=1)
               Index Cond: (device_id = ANY ('{1,2}'::integer[]))
         ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_device_id_time_idx on _hyper_2_5_chunk (actual rows=1.00 loops=1)
               Index Cond: (device_id = ANY ('{1,2}'::integer[]))
         ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_device_id_time_idx on _hyper_2_7_chunk (actual rows=1.00 loops=1)
               Index Cond: (device_id = ANY ('{1,2}'::integer[]))
         ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_device_id_time_idx on _hyper_2_8_chunk (actual rows=1.00 loops=1)
               Index Cond: (device_id = ANY ('{1,2}'::integer[]))
         ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_device_id_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
               Index Cond: (device_id = ANY ('{1,2}'::integer[]))
         ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_device_id_time_idx on _hyper_2_11_chunk (actual rows=1.00 loops=1)
               Index Cond: (device_id = ANY ('{1,2}'::integer[]))

RESET enable_seqscan;
-- test equality constraint on ORDER BY prefix
-- currently not optimized
SET enable_seqscan TO false;
SET enable_indexonlyscan TO false;
:PREFIX
SELECT time,
  device_id
FROM :TEST_TABLE
WHERE device_id = 1
ORDER BY device_id,
  time
LIMIT 10;
--- QUERY PLAN ---
 Limit (actual rows=10.00 loops=1)
   ->  Merge Append (actual rows=10.00 loops=1)
         Sort Key: metrics_space."time"
         ->  Index Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=10.00 loops=1)
               Filter: (device_id = 1)
         ->  Index Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (actual rows=1.00 loops=1)
               Filter: (device_id = 1)
         ->  Index Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
               Filter: (device_id = 1)

RESET enable_seqscan;
RESET enable_indexonlyscan;
-- queries without LIMIT should use ordered append
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE device_id IN (1, 2) OR time > '2000-01-06'
ORDER BY time ASC;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on metrics_space (actual rows=56853.00 loops=1)
   Order: metrics_space."time"
   ->  Merge Append (actual rows=7196.00 loops=1)
         Sort Key: metrics_space."time"
         ->  Index Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=3598.00 loops=1)
               Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
         ->  Index Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=3598.00 loops=1)
               Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
               Rows Removed by Filter: 7196
         ->  Index Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=0.00 loops=1)
               Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
               Rows Removed by Filter: 3598
   ->  Merge Append (actual rows=24467.00 loops=1)
         Sort Key: metrics_space."time"
         ->  Index Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (actual rows=5038.00 loops=1)
               Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
         ->  Index Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (actual rows=14632.00 loops=1)
               Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
               Rows Removed by Filter: 482
         ->  Index Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (actual rows=4797.00 loops=1)
               Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
               Rows Removed by Filter: 241
   ->  Merge Append (actual rows=25190.00 loops=1)
         Sort Key: metrics_space."time"
         ->  Index Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=5038.00 loops=1)
               Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
         ->  Index Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (actual rows=15114.00 loops=1)
               Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
         ->  Index Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (actual rows=5038.00 loops=1)
               Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))

-- queries without ORDER BY shouldnt use ordered append
SET enable_seqscan TO false;
:PREFIX
SELECT pg_typeof(time)
FROM :TEST_TABLE
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Append (actual rows=1.00 loops=1)
               ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=1.00 loops=1)
               ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (never executed)
               ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (never executed)
               ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
               ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
               ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
               ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
               ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
               ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)

RESET enable_seqscan;
-- test interaction with constraint exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-07'
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
         Order: metrics_space."time"
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
                     Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
                     Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)
                     Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)

:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-07'
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
         Order: metrics_space."time" DESC
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space."time" DESC
               ->  Index Only Scan Backward using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Index Only Scan Backward using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Index Only Scan Backward using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time" DESC
               ->  Index Only Scan Backward using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
                     Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Index Only Scan Backward using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                     Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Index Only Scan Backward using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                     Index Cond: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)

-- test interaction with runtime exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-08'::text::timestamptz
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
         Order: metrics_space."time"
         ->  Merge Append (actual rows=0.00 loops=1)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=0.00 loops=1)
                     Index Cond: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=0.00 loops=1)
                     Index Cond: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=0.00 loops=1)
                     Index Cond: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
                     Index Cond: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
                     Index Cond: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)
                     Index Cond: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)

:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time < '2000-01-08'::text::timestamptz
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
         Order: metrics_space."time"
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                     Index Cond: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                     Index Cond: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
                     Index Cond: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
                     Index Cond: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
                     Index Cond: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)
                     Index Cond: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)

-- test constraint exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-08'::text::timestamptz
  AND time < '2000-01-10'
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
         Order: metrics_space."time"
         ->  Merge Append (actual rows=0.00 loops=1)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=0.00 loops=1)
                     Index Cond: (("time" > ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone))
               ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=0.00 loops=1)
                     Index Cond: (("time" > ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone))
               ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=0.00 loops=1)
                     Index Cond: (("time" > ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone))
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (actual rows=1.00 loops=1)
                     Index Cond: (("time" > ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone))
               ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (actual rows=1.00 loops=1)
                     Index Cond: (("time" > ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone))
               ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (actual rows=1.00 loops=1)
                     Index Cond: (("time" > ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone))

:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time < '2000-01-08'::text::timestamptz
  AND time > '2000-01-07'
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
         Order: metrics_space."time"
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (actual rows=1.00 loops=1)
                     Index Cond: (("time" < ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone))
               ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (actual rows=1.00 loops=1)
                     Index Cond: (("time" < ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone))
               ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (actual rows=1.00 loops=1)
                     Index Cond: (("time" < ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone))
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
                     Index Cond: (("time" < ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone))
               ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
                     Index Cond: (("time" < ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone))
               ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)
                     Index Cond: (("time" < ('2000-01-08'::cstring)::timestamp with time zone) AND ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone))

-- Disable hash aggregation to get a deterministic test output
SET enable_hashagg = OFF;
-- min/max queries
:PREFIX
SELECT max(time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
                 Order: metrics_space."time" DESC
                 ->  Merge Append (actual rows=1.00 loops=1)
                       Sort Key: metrics_space."time" DESC
                       ->  Index Only Scan Backward using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (actual rows=1.00 loops=1)
                       ->  Index Only Scan Backward using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (actual rows=1.00 loops=1)
                       ->  Index Only Scan Backward using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
                 ->  Merge Append (never executed)
                       Sort Key: metrics_space."time" DESC
                       ->  Index Only Scan Backward using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
                       ->  Index Only Scan Backward using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                       ->  Index Only Scan Backward using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                 ->  Merge Append (never executed)
                       Sort Key: metrics_space."time" DESC
                       ->  Index Only Scan Backward using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (never executed)
                       ->  Index Only Scan Backward using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (never executed)
                       ->  Index Only Scan Backward using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (never executed)

:PREFIX
SELECT min(time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
                 Order: metrics_space."time"
                 ->  Merge Append (actual rows=1.00 loops=1)
                       Sort Key: metrics_space."time"
                       ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=1.00 loops=1)
                       ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=1.00 loops=1)
                       ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
                 ->  Merge Append (never executed)
                       Sort Key: metrics_space."time"
                       ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                       ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                       ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
                 ->  Merge Append (never executed)
                       Sort Key: metrics_space."time"
                       ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
                       ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
                       ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)

-- test first/last (doesn't use ordered append yet)
:PREFIX
SELECT first(time, time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Result (actual rows=1.00 loops=1)
                 ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
                       Order: metrics_space."time"
                       ->  Merge Append (actual rows=1.00 loops=1)
                             Sort Key: metrics_space."time"
                             ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=1.00 loops=1)
                             ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=1.00 loops=1)
                             ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
                       ->  Merge Append (never executed)
                             Sort Key: metrics_space."time"
                             ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                             ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                             ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
                       ->  Merge Append (never executed)
                             Sort Key: metrics_space."time"
                             ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
                             ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
                             ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)

:PREFIX
SELECT last(time, time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Result (actual rows=1.00 loops=1)
                 ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
                       Order: metrics_space."time" DESC
                       ->  Merge Append (actual rows=1.00 loops=1)
                             Sort Key: metrics_space."time" DESC
                             ->  Index Only Scan Backward using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (actual rows=1.00 loops=1)
                             ->  Index Only Scan Backward using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (actual rows=1.00 loops=1)
                             ->  Index Only Scan Backward using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
                       ->  Merge Append (never executed)
                             Sort Key: metrics_space."time" DESC
                             ->  Index Only Scan Backward using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
                             ->  Index Only Scan Backward using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                             ->  Index Only Scan Backward using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                       ->  Merge Append (never executed)
                             Sort Key: metrics_space."time" DESC
                             ->  Index Only Scan Backward using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (never executed)
                             ->  Index Only Scan Backward using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (never executed)
                             ->  Index Only Scan Backward using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (never executed)

-- test query with time_bucket
:PREFIX
SELECT time_bucket('1d', time)
FROM :TEST_TABLE
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
               Order: metrics_space."time"
               ->  Merge Append (actual rows=1.00 loops=1)
                     Sort Key: metrics_space."time"
                     ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=1.00 loops=1)
                     ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=1.00 loops=1)
                     ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
               ->  Merge Append (never executed)
                     Sort Key: metrics_space."time"
                     ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
               ->  Merge Append (never executed)
                     Sort Key: metrics_space."time"
                     ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)

-- test query with ORDER BY time_bucket
:PREFIX
SELECT time_bucket('1d', time)
FROM :TEST_TABLE
ORDER BY 1
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
               Order: time_bucket('@ 1 day'::interval, metrics_space."time")
               ->  Merge Append (actual rows=1.00 loops=1)
                     Sort Key: (time_bucket('@ 1 day'::interval, metrics_space."time"))
                     ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=1.00 loops=1)
                     ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=1.00 loops=1)
                     ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
               ->  Merge Append (never executed)
                     Sort Key: (time_bucket('@ 1 day'::interval, metrics_space."time"))
                     ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
               ->  Merge Append (never executed)
                     Sort Key: (time_bucket('@ 1 day'::interval, metrics_space."time"))
                     ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)

-- test query with ORDER BY time_bucket, device_id
-- must not use ordered append
:PREFIX
SELECT time_bucket('1d', time),
  device_id,
  v0
FROM :TEST_TABLE
WHERE device_id IN (1, 2)
ORDER BY time_bucket('1d', time),
  device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Sort (actual rows=1.00 loops=1)
         Sort Key: (time_bucket('@ 1 day'::interval, metrics_space."time")), metrics_space.device_id
         Sort Method: top-N heapsort 
         ->  Result (actual rows=27348.00 loops=1)
               ->  Append (actual rows=27348.00 loops=1)
                     ->  Seq Scan on _hyper_2_4_chunk (actual rows=3598.00 loops=1)
                           Filter: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Index Scan using _hyper_2_5_chunk_metrics_space_device_id_time_idx on _hyper_2_5_chunk (actual rows=3598.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Seq Scan on _hyper_2_7_chunk (actual rows=5038.00 loops=1)
                           Filter: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Index Scan using _hyper_2_8_chunk_metrics_space_device_id_time_idx on _hyper_2_8_chunk (actual rows=5038.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Seq Scan on _hyper_2_10_chunk (actual rows=5038.00 loops=1)
                           Filter: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Index Scan using _hyper_2_11_chunk_metrics_space_device_id_time_idx on _hyper_2_11_chunk (actual rows=5038.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))

-- test query with ORDER BY date_trunc
:PREFIX
SELECT time_bucket('1d', time)
FROM :TEST_TABLE
ORDER BY date_trunc('day', time)
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
               Order: date_trunc('day'::text, metrics_space."time")
               ->  Merge Append (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, metrics_space."time"))
                     ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=1.00 loops=1)
                     ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=1.00 loops=1)
                     ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
               ->  Merge Append (never executed)
                     Sort Key: (date_trunc('day'::text, metrics_space."time"))
                     ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
               ->  Merge Append (never executed)
                     Sort Key: (date_trunc('day'::text, metrics_space."time"))
                     ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)

-- test query with ORDER BY date_trunc
:PREFIX
SELECT date_trunc('day', time)
FROM :TEST_TABLE
ORDER BY 1
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
               Order: date_trunc('day'::text, metrics_space."time")
               ->  Merge Append (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, metrics_space."time"))
                     ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=1.00 loops=1)
                     ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=1.00 loops=1)
                     ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
               ->  Merge Append (never executed)
                     Sort Key: (date_trunc('day'::text, metrics_space."time"))
                     ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
               ->  Merge Append (never executed)
                     Sort Key: (date_trunc('day'::text, metrics_space."time"))
                     ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
                     ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)

-- test query with ORDER BY date_trunc, device_id
-- must not use ordered append
:PREFIX
SELECT date_trunc('day', time),
  device_id,
  v0
FROM :TEST_TABLE
WHERE device_id IN (1, 2)
ORDER BY 1,
  2
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Sort (actual rows=1.00 loops=1)
         Sort Key: (date_trunc('day'::text, metrics_space."time")), metrics_space.device_id
         Sort Method: top-N heapsort 
         ->  Result (actual rows=27348.00 loops=1)
               ->  Append (actual rows=27348.00 loops=1)
                     ->  Seq Scan on _hyper_2_4_chunk (actual rows=3598.00 loops=1)
                           Filter: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Index Scan using _hyper_2_5_chunk_metrics_space_device_id_time_idx on _hyper_2_5_chunk (actual rows=3598.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Seq Scan on _hyper_2_7_chunk (actual rows=5038.00 loops=1)
                           Filter: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Index Scan using _hyper_2_8_chunk_metrics_space_device_id_time_idx on _hyper_2_8_chunk (actual rows=5038.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Seq Scan on _hyper_2_10_chunk (actual rows=5038.00 loops=1)
                           Filter: (device_id = ANY ('{1,2}'::integer[]))
                     ->  Index Scan using _hyper_2_11_chunk_metrics_space_device_id_time_idx on _hyper_2_11_chunk (actual rows=5038.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))

-- test query with now() should result in ordered ChunkAppend
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time < now() + '1 month'
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=1.00 loops=1)
         Order: metrics_space."time" DESC
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space."time" DESC
               ->  Index Only Scan Backward using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" < (now() + '@ 1 mon'::interval))
               ->  Index Only Scan Backward using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" < (now() + '@ 1 mon'::interval))
               ->  Index Only Scan Backward using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
                     Index Cond: ("time" < (now() + '@ 1 mon'::interval))
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time" DESC
               ->  Index Only Scan Backward using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
                     Index Cond: ("time" < (now() + '@ 1 mon'::interval))
               ->  Index Only Scan Backward using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                     Index Cond: ("time" < (now() + '@ 1 mon'::interval))
               ->  Index Only Scan Backward using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                     Index Cond: ("time" < (now() + '@ 1 mon'::interval))
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time" DESC
               ->  Index Only Scan Backward using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (never executed)
                     Index Cond: ("time" < (now() + '@ 1 mon'::interval))
               ->  Index Only Scan Backward using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (never executed)
                     Index Cond: ("time" < (now() + '@ 1 mon'::interval))
               ->  Index Only Scan Backward using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (never executed)
                     Index Cond: ("time" < (now() + '@ 1 mon'::interval))

-- test CTE
:PREFIX WITH i AS (
  SELECT time
  FROM :TEST_TABLE
  WHERE time < now()
  ORDER BY time DESC
  LIMIT 100
)
SELECT *
FROM i;
--- QUERY PLAN ---
 Limit (actual rows=100.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=100.00 loops=1)
         Order: metrics_space."time" DESC
         ->  Merge Append (actual rows=100.00 loops=1)
               Sort Key: metrics_space."time" DESC
               ->  Index Only Scan Backward using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (actual rows=21.00 loops=1)
                     Index Cond: ("time" < now())
               ->  Index Only Scan Backward using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (actual rows=60.00 loops=1)
                     Index Cond: ("time" < now())
               ->  Index Only Scan Backward using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=21.00 loops=1)
                     Index Cond: ("time" < now())
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time" DESC
               ->  Index Only Scan Backward using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
                     Index Cond: ("time" < now())
               ->  Index Only Scan Backward using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
                     Index Cond: ("time" < now())
               ->  Index Only Scan Backward using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
                     Index Cond: ("time" < now())
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time" DESC
               ->  Index Only Scan Backward using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (never executed)
                     Index Cond: ("time" < now())
               ->  Index Only Scan Backward using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (never executed)
                     Index Cond: ("time" < now())
               ->  Index Only Scan Backward using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (never executed)
                     Index Cond: ("time" < now())

-- test CTE
-- no chunk exclusion for CTE because cte query is not pulled up
SET enable_seqscan TO false;
:PREFIX WITH cte AS (
  SELECT time
  FROM :TEST_TABLE
  WHERE device_id = 1
  ORDER BY time
)
SELECT *
FROM cte
WHERE time < '2000-02-01'::timestamptz;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on metrics_space (actual rows=13674.00 loops=1)
   Order: metrics_space."time"
   ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_device_id_time_idx on _hyper_2_4_chunk (actual rows=3598.00 loops=1)
         Index Cond: ((device_id = 1) AND ("time" < 'Tue Feb 01 00:00:00 2000 PST'::timestamp with time zone))
   ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_device_id_time_idx on _hyper_2_7_chunk (actual rows=5038.00 loops=1)
         Index Cond: ((device_id = 1) AND ("time" < 'Tue Feb 01 00:00:00 2000 PST'::timestamp with time zone))
   ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_device_id_time_idx on _hyper_2_10_chunk (actual rows=5038.00 loops=1)
         Index Cond: ((device_id = 1) AND ("time" < 'Tue Feb 01 00:00:00 2000 PST'::timestamp with time zone))

RESET enable_seqscan;
-- test subquery
-- not ChunkAppend so no chunk exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time = (
    SELECT max(time)
    FROM :TEST_TABLE)
ORDER BY time;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on metrics_space (actual rows=5.00 loops=1)
   InitPlan 2
     ->  Result (actual rows=1.00 loops=1)
           InitPlan 1
             ->  Limit (actual rows=1.00 loops=1)
                   ->  Custom Scan (ChunkAppend) on metrics_space metrics_space_1 (actual rows=1.00 loops=1)
                         Order: metrics_space_1."time" DESC
                         ->  Merge Append (actual rows=1.00 loops=1)
                               Sort Key: metrics_space_1."time" DESC
                               ->  Index Only Scan Backward using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk _hyper_2_12_chunk_1 (actual rows=1.00 loops=1)
                               ->  Index Only Scan Backward using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk _hyper_2_11_chunk_1 (actual rows=1.00 loops=1)
                               ->  Index Only Scan Backward using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk _hyper_2_10_chunk_1 (actual rows=1.00 loops=1)
                         ->  Merge Append (never executed)
                               Sort Key: metrics_space_1."time" DESC
                               ->  Index Only Scan Backward using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk _hyper_2_9_chunk_1 (never executed)
                               ->  Index Only Scan Backward using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk _hyper_2_8_chunk_1 (never executed)
                               ->  Index Only Scan Backward using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk _hyper_2_7_chunk_1 (never executed)
                         ->  Merge Append (never executed)
                               Sort Key: metrics_space_1."time" DESC
                               ->  Index Only Scan Backward using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk _hyper_2_6_chunk_1 (never executed)
                               ->  Index Only Scan Backward using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk _hyper_2_5_chunk_1 (never executed)
                               ->  Index Only Scan Backward using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk _hyper_2_4_chunk_1 (never executed)
   ->  Merge Append (actual rows=0.00 loops=1)
         ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=0.00 loops=1)
               Index Cond: ("time" = (InitPlan 2).col1)
         ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=0.00 loops=1)
               Index Cond: ("time" = (InitPlan 2).col1)
         ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=0.00 loops=1)
               Index Cond: ("time" = (InitPlan 2).col1)
   ->  Merge Append (actual rows=0.00 loops=1)
         ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (actual rows=0.00 loops=1)
               Index Cond: ("time" = (InitPlan 2).col1)
         ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (actual rows=0.00 loops=1)
               Index Cond: ("time" = (InitPlan 2).col1)
         ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (actual rows=0.00 loops=1)
               Index Cond: ("time" = (InitPlan 2).col1)
   ->  Merge Append (actual rows=5.00 loops=1)
         ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
               Index Cond: ("time" = (InitPlan 2).col1)
         ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (actual rows=3.00 loops=1)
               Index Cond: ("time" = (InitPlan 2).col1)
         ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (actual rows=1.00 loops=1)
               Index Cond: ("time" = (InitPlan 2).col1)

-- test ordered append with limit expression
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT (
  SELECT length('four'));
--- QUERY PLAN ---
 Limit (actual rows=4.00 loops=1)
   InitPlan 1
     ->  Result (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=4.00 loops=1)
         Order: metrics_space."time"
         ->  Merge Append (actual rows=4.00 loops=1)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=2.00 loops=1)
               ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=3.00 loops=1)
               ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
               ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
               ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
               ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
               ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)

-- test with ordered guc disabled
SET timescaledb.enable_ordered_append TO OFF;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Merge Append (actual rows=3.00 loops=1)
         Sort Key: metrics_space."time"
         ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=2.00 loops=1)
         ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=2.00 loops=1)
         ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (actual rows=1.00 loops=1)

RESET timescaledb.enable_ordered_append;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=3.00 loops=1)
         Order: metrics_space."time"
         ->  Merge Append (actual rows=3.00 loops=1)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=2.00 loops=1)
               ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=2.00 loops=1)
               ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
               ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
               ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
               ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
               ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)

-- test with chunk append disabled
SET timescaledb.enable_chunk_append TO OFF;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Merge Append (actual rows=3.00 loops=1)
         Sort Key: metrics_space."time"
         ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=2.00 loops=1)
         ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=2.00 loops=1)
         ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (actual rows=1.00 loops=1)
         ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (actual rows=1.00 loops=1)

RESET timescaledb.enable_chunk_append;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space (actual rows=3.00 loops=1)
         Order: metrics_space."time"
         ->  Merge Append (actual rows=3.00 loops=1)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_4_chunk_metrics_space_time_idx on _hyper_2_4_chunk (actual rows=2.00 loops=1)
               ->  Index Only Scan using _hyper_2_5_chunk_metrics_space_time_idx on _hyper_2_5_chunk (actual rows=2.00 loops=1)
               ->  Index Only Scan using _hyper_2_6_chunk_metrics_space_time_idx on _hyper_2_6_chunk (actual rows=1.00 loops=1)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_7_chunk_metrics_space_time_idx on _hyper_2_7_chunk (never executed)
               ->  Index Only Scan using _hyper_2_8_chunk_metrics_space_time_idx on _hyper_2_8_chunk (never executed)
               ->  Index Only Scan using _hyper_2_9_chunk_metrics_space_time_idx on _hyper_2_9_chunk (never executed)
         ->  Merge Append (never executed)
               Sort Key: metrics_space."time"
               ->  Index Only Scan using _hyper_2_10_chunk_metrics_space_time_idx on _hyper_2_10_chunk (never executed)
               ->  Index Only Scan using _hyper_2_11_chunk_metrics_space_time_idx on _hyper_2_11_chunk (never executed)
               ->  Index Only Scan using _hyper_2_12_chunk_metrics_space_time_idx on _hyper_2_12_chunk (never executed)

\set TEST_TABLE 'metrics_compressed'
\ir :TEST_QUERY_NAME
-- 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.
-- In the following test cases, we test that certain indexes are used. By using the
-- timescaledb.enable_decompression_sorted_merge optimization, we are pushing a sort node
-- below the DecompressChunk node, which operates on the batches. This could lead to flaky
-- tests because the input data is small and PostgreSQL switches from IndexScans to
-- SequentialScans. Disable the optimization for the following tests to ensure we have
-- stable query plans in all CI environments.
SET timescaledb.enable_decompression_sorted_merge = 0;
vacuum analyze :TEST_TABLE;
-- test ASC for ordered chunks
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
         Order: metrics_compressed."time"
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_3_13_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                     ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
         ->  Sort (never executed)
               Sort Key: _hyper_3_14_chunk."time"
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                     ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
         ->  Sort (never executed)
               Sort Key: _hyper_3_15_chunk."time"
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                     ->  Seq Scan on compress_hyper_4_18_chunk (never executed)

-- test DESC for ordered chunks
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
         Order: metrics_compressed."time" DESC
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_3_15_chunk."time" DESC
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=25190.00 loops=1)
                     ->  Seq Scan on compress_hyper_4_18_chunk (actual rows=30.00 loops=1)
         ->  Sort (never executed)
               Sort Key: _hyper_3_14_chunk."time" DESC
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                     ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
         ->  Sort (never executed)
               Sort Key: _hyper_3_13_chunk."time" DESC
               ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (never executed)
                     ->  Seq Scan on compress_hyper_4_16_chunk (never executed)

-- test query with ORDER BY column not in targetlist
:PREFIX
SELECT pg_typeof(device_id),
  pg_typeof(v2)
FROM :TEST_TABLE
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
               Order: metrics_compressed."time"
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_3_13_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                           ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
               ->  Sort (never executed)
                     Sort Key: _hyper_3_14_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                           ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_3_15_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                           ->  Seq Scan on compress_hyper_4_18_chunk (never executed)

-- ORDER BY may include other columns after time column
:PREFIX
SELECT time,
  device_id,
  v0
FROM :TEST_TABLE
WHERE device_id = 1
ORDER BY time DESC,
  device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
         Order: metrics_compressed."time" DESC
         ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_4_18_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_18_chunk (actual rows=1.00 loops=1)
                     Index Cond: (device_id = 1)
         ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
               ->  Index Scan using compress_hyper_4_17_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_17_chunk (never executed)
                     Index Cond: (device_id = 1)
         ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (never executed)
               ->  Index Scan using compress_hyper_4_16_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_16_chunk (never executed)
                     Index Cond: (device_id = 1)

-- test RECORD in targetlist
:PREFIX
SELECT (time,
    device_id,
    v0)
FROM :TEST_TABLE
WHERE device_id = 1
ORDER BY time DESC,
  device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
               Order: metrics_compressed."time" DESC
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=1.00 loops=1)
                     ->  Index Scan using compress_hyper_4_18_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_18_chunk (actual rows=1.00 loops=1)
                           Index Cond: (device_id = 1)
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                     ->  Index Scan using compress_hyper_4_17_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_17_chunk (never executed)
                           Index Cond: (device_id = 1)
               ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (never executed)
                     ->  Index Scan using compress_hyper_4_16_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_16_chunk (never executed)
                           Index Cond: (device_id = 1)

-- test sort column not in targetlist
:PREFIX
SELECT time_bucket('1h', time)
FROM :TEST_TABLE
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
               Order: metrics_compressed."time" DESC
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_3_15_chunk."time" DESC
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=25190.00 loops=1)
                           ->  Seq Scan on compress_hyper_4_18_chunk (actual rows=30.00 loops=1)
               ->  Sort (never executed)
                     Sort Key: _hyper_3_14_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                           ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_3_13_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (never executed)
                           ->  Seq Scan on compress_hyper_4_16_chunk (never executed)

-- queries with ORDER BY non-time column shouldn't use ordered append
:PREFIX
SELECT device_id
FROM :TEST_TABLE
ORDER BY device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Merge Append (actual rows=1.00 loops=1)
         Sort Key: metrics_compressed.device_id
         ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_4_16_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_16_chunk (actual rows=1.00 loops=1)
         ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_4_17_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_17_chunk (actual rows=1.00 loops=1)
         ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_4_18_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_18_chunk (actual rows=1.00 loops=1)

-- time column must be primary sort order
SET enable_seqscan TO false;
:PREFIX
SELECT time,
  device_id
FROM :TEST_TABLE
WHERE device_id IN (1, 2)
ORDER BY device_id,
  time
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Merge Append (actual rows=1.00 loops=1)
         Sort Key: metrics_compressed.device_id, metrics_compressed."time"
         ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=1.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: compress_hyper_4_16_chunk.device_id, compress_hyper_4_16_chunk._ts_meta_min_1, compress_hyper_4_16_chunk._ts_meta_max_1
                     Sort Method: quicksort 
                     ->  Index Scan using compress_hyper_4_16_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_16_chunk (actual rows=8.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
         ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (actual rows=1.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: compress_hyper_4_17_chunk.device_id, compress_hyper_4_17_chunk._ts_meta_min_1, compress_hyper_4_17_chunk._ts_meta_max_1
                     Sort Method: quicksort 
                     ->  Index Scan using compress_hyper_4_17_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_17_chunk (actual rows=12.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
         ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=1.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: compress_hyper_4_18_chunk.device_id, compress_hyper_4_18_chunk._ts_meta_min_1, compress_hyper_4_18_chunk._ts_meta_max_1
                     Sort Method: quicksort 
                     ->  Index Scan using compress_hyper_4_18_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_18_chunk (actual rows=12.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))

RESET enable_seqscan;
-- test equality constraint on ORDER BY prefix
-- currently not optimized
SET enable_seqscan TO false;
SET enable_indexonlyscan TO false;
:PREFIX
SELECT time,
  device_id
FROM :TEST_TABLE
WHERE device_id = 1
ORDER BY device_id,
  time
LIMIT 10;
--- QUERY PLAN ---
 Limit (actual rows=10.00 loops=1)
   ->  Merge Append (actual rows=10.00 loops=1)
         Sort Key: metrics_compressed."time"
         ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=10.00 loops=1)
               ->  Index Scan Backward using compress_hyper_4_16_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_16_chunk (actual rows=1.00 loops=1)
                     Index Cond: (device_id = 1)
         ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (actual rows=1.00 loops=1)
               ->  Index Scan Backward using compress_hyper_4_17_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_17_chunk (actual rows=1.00 loops=1)
                     Index Cond: (device_id = 1)
         ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=1.00 loops=1)
               ->  Index Scan Backward using compress_hyper_4_18_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_18_chunk (actual rows=1.00 loops=1)
                     Index Cond: (device_id = 1)

RESET enable_seqscan;
RESET enable_indexonlyscan;
-- queries without LIMIT should use ordered append
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE device_id IN (1, 2) OR time > '2000-01-06'
ORDER BY time ASC;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on metrics_compressed (actual rows=56853.00 loops=1)
   Order: metrics_compressed."time"
   ->  Sort (actual rows=7196.00 loops=1)
         Sort Key: _hyper_3_13_chunk."time"
         Sort Method: quicksort 
         ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=7196.00 loops=1)
               Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
               ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=8.00 loops=1)
                     Filter: ((device_id = ANY ('{1,2}'::integer[])) OR (_ts_meta_max_1 > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                     Rows Removed by Filter: 12
   ->  Sort (actual rows=24467.00 loops=1)
         Sort Key: _hyper_3_14_chunk."time"
         Sort Method: quicksort 
         ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (actual rows=24467.00 loops=1)
               Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
               Rows Removed by Filter: 609
               ->  Seq Scan on compress_hyper_4_17_chunk (actual rows=27.00 loops=1)
                     Filter: ((device_id = ANY ('{1,2}'::integer[])) OR (_ts_meta_max_1 > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                     Rows Removed by Filter: 3
   ->  Sort (actual rows=25190.00 loops=1)
         Sort Key: _hyper_3_15_chunk."time"
         Sort Method: quicksort 
         ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=25190.00 loops=1)
               Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
               ->  Seq Scan on compress_hyper_4_18_chunk (actual rows=30.00 loops=1)
                     Filter: ((device_id = ANY ('{1,2}'::integer[])) OR (_ts_meta_max_1 > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))

-- queries without ORDER BY shouldnt use ordered append
SET enable_seqscan TO false;
:PREFIX
SELECT pg_typeof(time)
FROM :TEST_TABLE
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Append (actual rows=1.00 loops=1)
               ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=1.00 loops=1)
                     ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=1.00 loops=1)
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                     ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                     ->  Seq Scan on compress_hyper_4_18_chunk (never executed)

RESET enable_seqscan;
-- test interaction with constraint exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-07'
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
         Order: metrics_compressed."time"
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_3_14_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (actual rows=20385.00 loops=1)
                     Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                     Rows Removed by Filter: 4615
                     ->  Seq Scan on compress_hyper_4_17_chunk (actual rows=25.00 loops=1)
                           Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                           Rows Removed by Filter: 5
         ->  Sort (never executed)
               Sort Key: _hyper_3_15_chunk."time"
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                     ->  Seq Scan on compress_hyper_4_18_chunk (never executed)

:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-07'
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
         Order: metrics_compressed."time" DESC
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_3_15_chunk."time" DESC
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=25190.00 loops=1)
                     ->  Seq Scan on compress_hyper_4_18_chunk (actual rows=30.00 loops=1)
         ->  Sort (never executed)
               Sort Key: _hyper_3_14_chunk."time" DESC
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                     Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                     ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
                           Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)

-- test interaction with runtime exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-08'::text::timestamptz
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
         Order: metrics_compressed."time"
         Chunks excluded during startup: 1
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_3_14_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (actual rows=16785.00 loops=1)
                     Vectorized Filter: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
                     Rows Removed by Filter: 3215
                     ->  Seq Scan on compress_hyper_4_17_chunk (actual rows=20.00 loops=1)
                           Filter: (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone)
                           Rows Removed by Filter: 10
         ->  Sort (never executed)
               Sort Key: _hyper_3_15_chunk."time"
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                     Vectorized Filter: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
                     ->  Seq Scan on compress_hyper_4_18_chunk (never executed)
                           Filter: (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone)

:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time < '2000-01-08'::text::timestamptz
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
         Order: metrics_compressed."time"
         Chunks excluded during startup: 1
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_3_13_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                     Vectorized Filter: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
                     ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
                           Filter: (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone)
         ->  Sort (never executed)
               Sort Key: _hyper_3_14_chunk."time"
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                     Vectorized Filter: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
                     ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
                           Filter: (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone)

-- test constraint exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-08'::text::timestamptz
  AND time < '2000-01-10'
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
         Order: metrics_compressed."time"
         Chunks excluded during startup: 1
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_3_14_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (actual rows=7195.00 loops=1)
                     Vectorized Filter: (("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND ("time" > ('2000-01-08'::cstring)::timestamp with time zone))
                     Rows Removed by Filter: 7805
                     ->  Seq Scan on compress_hyper_4_17_chunk (actual rows=15.00 loops=1)
                           Filter: ((_ts_meta_min_1 < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone))
                           Rows Removed by Filter: 15

:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time < '2000-01-08'::text::timestamptz
  AND time > '2000-01-07'
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
         Order: metrics_compressed."time"
         Chunks excluded during startup: 1
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_3_14_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (actual rows=3595.00 loops=1)
                     Vectorized Filter: (("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND ("time" < ('2000-01-08'::cstring)::timestamp with time zone))
                     Rows Removed by Filter: 6405
                     ->  Seq Scan on compress_hyper_4_17_chunk (actual rows=10.00 loops=1)
                           Filter: ((_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone))
                           Rows Removed by Filter: 20

-- Disable hash aggregation to get a deterministic test output
SET enable_hashagg = OFF;
-- min/max queries
:PREFIX
SELECT max(time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
                 Order: metrics_compressed."time" DESC
                 ->  Sort (actual rows=1.00 loops=1)
                       Sort Key: _hyper_3_15_chunk."time" DESC
                       Sort Method: top-N heapsort 
                       ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=25190.00 loops=1)
                             ->  Seq Scan on compress_hyper_4_18_chunk (actual rows=30.00 loops=1)
                 ->  Sort (never executed)
                       Sort Key: _hyper_3_14_chunk."time" DESC
                       ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                             ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
                 ->  Sort (never executed)
                       Sort Key: _hyper_3_13_chunk."time" DESC
                       ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (never executed)
                             ->  Seq Scan on compress_hyper_4_16_chunk (never executed)

:PREFIX
SELECT min(time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
                 Order: metrics_compressed."time"
                 ->  Sort (actual rows=1.00 loops=1)
                       Sort Key: _hyper_3_13_chunk."time"
                       Sort Method: top-N heapsort 
                       ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                             ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
                 ->  Sort (never executed)
                       Sort Key: _hyper_3_14_chunk."time"
                       ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                             ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
                 ->  Sort (never executed)
                       Sort Key: _hyper_3_15_chunk."time"
                       ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                             ->  Seq Scan on compress_hyper_4_18_chunk (never executed)

-- test first/last (doesn't use ordered append yet)
:PREFIX
SELECT first(time, time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Result (actual rows=1.00 loops=1)
                 ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
                       Order: metrics_compressed."time"
                       ->  Sort (actual rows=1.00 loops=1)
                             Sort Key: _hyper_3_13_chunk."time"
                             Sort Method: top-N heapsort 
                             ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                                   ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
                       ->  Sort (never executed)
                             Sort Key: _hyper_3_14_chunk."time"
                             ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
                       ->  Sort (never executed)
                             Sort Key: _hyper_3_15_chunk."time"
                             ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_4_18_chunk (never executed)

:PREFIX
SELECT last(time, time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Result (actual rows=1.00 loops=1)
                 ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
                       Order: metrics_compressed."time" DESC
                       ->  Sort (actual rows=1.00 loops=1)
                             Sort Key: _hyper_3_15_chunk."time" DESC
                             Sort Method: top-N heapsort 
                             ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=25190.00 loops=1)
                                   ->  Seq Scan on compress_hyper_4_18_chunk (actual rows=30.00 loops=1)
                       ->  Sort (never executed)
                             Sort Key: _hyper_3_14_chunk."time" DESC
                             ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
                       ->  Sort (never executed)
                             Sort Key: _hyper_3_13_chunk."time" DESC
                             ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_4_16_chunk (never executed)

-- test query with time_bucket
:PREFIX
SELECT time_bucket('1d', time)
FROM :TEST_TABLE
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
               Order: metrics_compressed."time"
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_3_13_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                           ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
               ->  Sort (never executed)
                     Sort Key: _hyper_3_14_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                           ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_3_15_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                           ->  Seq Scan on compress_hyper_4_18_chunk (never executed)

-- test query with ORDER BY time_bucket
:PREFIX
SELECT time_bucket('1d', time)
FROM :TEST_TABLE
ORDER BY 1
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
               Order: time_bucket('@ 1 day'::interval, metrics_compressed."time")
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (time_bucket('@ 1 day'::interval, _hyper_3_13_chunk."time"))
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=17990.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                                 ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
               ->  Sort (never executed)
                     Sort Key: (time_bucket('@ 1 day'::interval, _hyper_3_14_chunk."time"))
                     ->  Result (never executed)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: (time_bucket('@ 1 day'::interval, _hyper_3_15_chunk."time"))
                     ->  Result (never executed)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_4_18_chunk (never executed)

-- test query with ORDER BY time_bucket, device_id
-- must not use ordered append
:PREFIX
SELECT time_bucket('1d', time),
  device_id,
  v0
FROM :TEST_TABLE
WHERE device_id IN (1, 2)
ORDER BY time_bucket('1d', time),
  device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: (time_bucket('@ 1 day'::interval, metrics_compressed."time")), metrics_compressed.device_id
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (time_bucket('@ 1 day'::interval, _hyper_3_13_chunk."time")), _hyper_3_13_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=7196.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=7196.00 loops=1)
                                 ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=8.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
                                       Rows Removed by Filter: 12
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (time_bucket('@ 1 day'::interval, _hyper_3_14_chunk."time")), _hyper_3_14_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=10076.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (actual rows=10076.00 loops=1)
                                 ->  Seq Scan on compress_hyper_4_17_chunk (actual rows=12.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
                                       Rows Removed by Filter: 18
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (time_bucket('@ 1 day'::interval, _hyper_3_15_chunk."time")), _hyper_3_15_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=10076.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=10076.00 loops=1)
                                 ->  Seq Scan on compress_hyper_4_18_chunk (actual rows=12.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
                                       Rows Removed by Filter: 18

-- test query with ORDER BY date_trunc
:PREFIX
SELECT time_bucket('1d', time)
FROM :TEST_TABLE
ORDER BY date_trunc('day', time)
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
               Order: date_trunc('day'::text, metrics_compressed."time")
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, _hyper_3_13_chunk."time"))
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=17990.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                                 ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
               ->  Sort (never executed)
                     Sort Key: (date_trunc('day'::text, _hyper_3_14_chunk."time"))
                     ->  Result (never executed)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: (date_trunc('day'::text, _hyper_3_15_chunk."time"))
                     ->  Result (never executed)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_4_18_chunk (never executed)

-- test query with ORDER BY date_trunc
:PREFIX
SELECT date_trunc('day', time)
FROM :TEST_TABLE
ORDER BY 1
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
               Order: date_trunc('day'::text, metrics_compressed."time")
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, _hyper_3_13_chunk."time"))
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=17990.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                                 ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
               ->  Sort (never executed)
                     Sort Key: (date_trunc('day'::text, _hyper_3_14_chunk."time"))
                     ->  Result (never executed)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: (date_trunc('day'::text, _hyper_3_15_chunk."time"))
                     ->  Result (never executed)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_4_18_chunk (never executed)

-- test query with ORDER BY date_trunc, device_id
-- must not use ordered append
:PREFIX
SELECT date_trunc('day', time),
  device_id,
  v0
FROM :TEST_TABLE
WHERE device_id IN (1, 2)
ORDER BY 1,
  2
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: (date_trunc('day'::text, metrics_compressed."time")), metrics_compressed.device_id
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, _hyper_3_13_chunk."time")), _hyper_3_13_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=7196.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=7196.00 loops=1)
                                 ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=8.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
                                       Rows Removed by Filter: 12
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, _hyper_3_14_chunk."time")), _hyper_3_14_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=10076.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (actual rows=10076.00 loops=1)
                                 ->  Seq Scan on compress_hyper_4_17_chunk (actual rows=12.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
                                       Rows Removed by Filter: 18
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, _hyper_3_15_chunk."time")), _hyper_3_15_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=10076.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=10076.00 loops=1)
                                 ->  Seq Scan on compress_hyper_4_18_chunk (actual rows=12.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
                                       Rows Removed by Filter: 18

-- test query with now() should result in ordered ChunkAppend
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time < now() + '1 month'
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=1.00 loops=1)
         Order: metrics_compressed."time" DESC
         Chunks excluded during startup: 0
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_3_15_chunk."time" DESC
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=25190.00 loops=1)
                     Vectorized Filter: ("time" < (now() + '@ 1 mon'::interval))
                     ->  Seq Scan on compress_hyper_4_18_chunk (actual rows=30.00 loops=1)
                           Filter: (_ts_meta_min_1 < (now() + '@ 1 mon'::interval))
         ->  Sort (never executed)
               Sort Key: _hyper_3_14_chunk."time" DESC
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                     Vectorized Filter: ("time" < (now() + '@ 1 mon'::interval))
                     ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
                           Filter: (_ts_meta_min_1 < (now() + '@ 1 mon'::interval))
         ->  Sort (never executed)
               Sort Key: _hyper_3_13_chunk."time" DESC
               ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (never executed)
                     Vectorized Filter: ("time" < (now() + '@ 1 mon'::interval))
                     ->  Seq Scan on compress_hyper_4_16_chunk (never executed)
                           Filter: (_ts_meta_min_1 < (now() + '@ 1 mon'::interval))

-- test CTE
:PREFIX WITH i AS (
  SELECT time
  FROM :TEST_TABLE
  WHERE time < now()
  ORDER BY time DESC
  LIMIT 100
)
SELECT *
FROM i;
--- QUERY PLAN ---
 Limit (actual rows=100.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=100.00 loops=1)
         Order: metrics_compressed."time" DESC
         Chunks excluded during startup: 0
         ->  Sort (actual rows=100.00 loops=1)
               Sort Key: _hyper_3_15_chunk."time" DESC
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=25190.00 loops=1)
                     Vectorized Filter: ("time" < now())
                     ->  Seq Scan on compress_hyper_4_18_chunk (actual rows=30.00 loops=1)
                           Filter: (_ts_meta_min_1 < now())
         ->  Sort (never executed)
               Sort Key: _hyper_3_14_chunk."time" DESC
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                     Vectorized Filter: ("time" < now())
                     ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
                           Filter: (_ts_meta_min_1 < now())
         ->  Sort (never executed)
               Sort Key: _hyper_3_13_chunk."time" DESC
               ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (never executed)
                     Vectorized Filter: ("time" < now())
                     ->  Seq Scan on compress_hyper_4_16_chunk (never executed)
                           Filter: (_ts_meta_min_1 < now())

-- test CTE
-- no chunk exclusion for CTE because cte query is not pulled up
SET enable_seqscan TO false;
:PREFIX WITH cte AS (
  SELECT time
  FROM :TEST_TABLE
  WHERE device_id = 1
  ORDER BY time
)
SELECT *
FROM cte
WHERE time < '2000-02-01'::timestamptz;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on metrics_compressed (actual rows=13674.00 loops=1)
   Order: metrics_compressed."time"
   ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=3598.00 loops=1)
         ->  Index Scan Backward using compress_hyper_4_16_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_16_chunk (actual rows=4.00 loops=1)
               Index Cond: (device_id = 1)
   ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (actual rows=5038.00 loops=1)
         ->  Index Scan Backward using compress_hyper_4_17_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_17_chunk (actual rows=6.00 loops=1)
               Index Cond: (device_id = 1)
   ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=5038.00 loops=1)
         ->  Index Scan Backward using compress_hyper_4_18_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_4_18_chunk (actual rows=6.00 loops=1)
               Index Cond: (device_id = 1)

RESET enable_seqscan;
-- test subquery
-- not ChunkAppend so no chunk exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time = (
    SELECT max(time)
    FROM :TEST_TABLE)
ORDER BY time;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on metrics_compressed (actual rows=5.00 loops=1)
   Chunks excluded during runtime: 2
   InitPlan 2
     ->  Result (actual rows=1.00 loops=1)
           InitPlan 1
             ->  Limit (actual rows=1.00 loops=1)
                   ->  Custom Scan (ChunkAppend) on metrics_compressed metrics_compressed_1 (actual rows=1.00 loops=1)
                         Order: metrics_compressed_1."time" DESC
                         ->  Sort (actual rows=1.00 loops=1)
                               Sort Key: _hyper_3_15_chunk_1."time" DESC
                               Sort Method: top-N heapsort 
                               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk _hyper_3_15_chunk_1 (actual rows=25190.00 loops=1)
                                     ->  Seq Scan on compress_hyper_4_18_chunk compress_hyper_4_18_chunk_1 (actual rows=30.00 loops=1)
                         ->  Sort (never executed)
                               Sort Key: _hyper_3_14_chunk_1."time" DESC
                               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk _hyper_3_14_chunk_1 (never executed)
                                     ->  Seq Scan on compress_hyper_4_17_chunk compress_hyper_4_17_chunk_1 (never executed)
                         ->  Sort (never executed)
                               Sort Key: _hyper_3_13_chunk_1."time" DESC
                               ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk _hyper_3_13_chunk_1 (never executed)
                                     ->  Seq Scan on compress_hyper_4_16_chunk compress_hyper_4_16_chunk_1 (never executed)
   ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (never executed)
         Filter: ("time" = (InitPlan 2).col1)
         ->  Seq Scan on compress_hyper_4_16_chunk (never executed)
               Filter: ((_ts_meta_min_1 <= (InitPlan 2).col1) AND (_ts_meta_max_1 >= (InitPlan 2).col1))
   ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
         Filter: ("time" = (InitPlan 2).col1)
         ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
               Filter: ((_ts_meta_min_1 <= (InitPlan 2).col1) AND (_ts_meta_max_1 >= (InitPlan 2).col1))
   ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=5.00 loops=1)
         Filter: ("time" = (InitPlan 2).col1)
         Rows Removed by Filter: 4995
         ->  Seq Scan on compress_hyper_4_18_chunk (actual rows=5.00 loops=1)
               Filter: ((_ts_meta_min_1 <= (InitPlan 2).col1) AND (_ts_meta_max_1 >= (InitPlan 2).col1))
               Rows Removed by Filter: 25

-- test ordered append with limit expression
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT (
  SELECT length('four'));
--- QUERY PLAN ---
 Limit (actual rows=4.00 loops=1)
   InitPlan 1
     ->  Result (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=4.00 loops=1)
         Order: metrics_compressed."time"
         ->  Sort (actual rows=4.00 loops=1)
               Sort Key: _hyper_3_13_chunk."time"
               Sort Method: quicksort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                     ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
         ->  Sort (never executed)
               Sort Key: _hyper_3_14_chunk."time"
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                     ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
         ->  Sort (never executed)
               Sort Key: _hyper_3_15_chunk."time"
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                     ->  Seq Scan on compress_hyper_4_18_chunk (never executed)

-- test with ordered guc disabled
SET timescaledb.enable_ordered_append TO OFF;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Merge Append (actual rows=3.00 loops=1)
         Sort Key: metrics_compressed."time"
         ->  Sort (actual rows=3.00 loops=1)
               Sort Key: _hyper_3_13_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                     ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_3_14_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (actual rows=25190.00 loops=1)
                     ->  Seq Scan on compress_hyper_4_17_chunk (actual rows=30.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_3_15_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=25190.00 loops=1)
                     ->  Seq Scan on compress_hyper_4_18_chunk (actual rows=30.00 loops=1)

RESET timescaledb.enable_ordered_append;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=3.00 loops=1)
         Order: metrics_compressed."time"
         ->  Sort (actual rows=3.00 loops=1)
               Sort Key: _hyper_3_13_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                     ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
         ->  Sort (never executed)
               Sort Key: _hyper_3_14_chunk."time"
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                     ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
         ->  Sort (never executed)
               Sort Key: _hyper_3_15_chunk."time"
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                     ->  Seq Scan on compress_hyper_4_18_chunk (never executed)

-- test with chunk append disabled
SET timescaledb.enable_chunk_append TO OFF;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Merge Append (actual rows=3.00 loops=1)
         Sort Key: metrics_compressed."time"
         ->  Sort (actual rows=3.00 loops=1)
               Sort Key: _hyper_3_13_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                     ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_3_14_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (actual rows=25190.00 loops=1)
                     ->  Seq Scan on compress_hyper_4_17_chunk (actual rows=30.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_3_15_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (actual rows=25190.00 loops=1)
                     ->  Seq Scan on compress_hyper_4_18_chunk (actual rows=30.00 loops=1)

RESET timescaledb.enable_chunk_append;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_compressed (actual rows=3.00 loops=1)
         Order: metrics_compressed."time"
         ->  Sort (actual rows=3.00 loops=1)
               Sort Key: _hyper_3_13_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_3_13_chunk (actual rows=17990.00 loops=1)
                     ->  Seq Scan on compress_hyper_4_16_chunk (actual rows=20.00 loops=1)
         ->  Sort (never executed)
               Sort Key: _hyper_3_14_chunk."time"
               ->  Custom Scan (ColumnarScan) on _hyper_3_14_chunk (never executed)
                     ->  Seq Scan on compress_hyper_4_17_chunk (never executed)
         ->  Sort (never executed)
               Sort Key: _hyper_3_15_chunk."time"
               ->  Custom Scan (ColumnarScan) on _hyper_3_15_chunk (never executed)
                     ->  Seq Scan on compress_hyper_4_18_chunk (never executed)

\set TEST_TABLE 'metrics_space_compressed'
\ir :TEST_QUERY_NAME
-- 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.
-- In the following test cases, we test that certain indexes are used. By using the
-- timescaledb.enable_decompression_sorted_merge optimization, we are pushing a sort node
-- below the DecompressChunk node, which operates on the batches. This could lead to flaky
-- tests because the input data is small and PostgreSQL switches from IndexScans to
-- SequentialScans. Disable the optimization for the following tests to ensure we have
-- stable query plans in all CI environments.
SET timescaledb.enable_decompression_sorted_merge = 0;
vacuum analyze :TEST_TABLE;
-- test ASC for ordered chunks
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
         Order: metrics_space_compressed."time"
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_19_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_20_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_21_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (never executed)
                     Sort Key: _hyper_5_22_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_23_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_24_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (never executed)
                     Sort Key: _hyper_5_25_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_26_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_27_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_36_chunk (never executed)

-- test DESC for ordered chunks
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
         Order: metrics_space_compressed."time" DESC
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space_compressed."time" DESC
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_27_chunk."time" DESC
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (actual rows=5038.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_36_chunk (actual rows=6.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_26_chunk."time" DESC
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=15114.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_35_chunk (actual rows=18.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_25_chunk."time" DESC
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=5038.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time" DESC
               ->  Sort (never executed)
                     Sort Key: _hyper_5_24_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_23_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_22_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time" DESC
               ->  Sort (never executed)
                     Sort Key: _hyper_5_21_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_30_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_20_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_29_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_19_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_28_chunk (never executed)

-- test query with ORDER BY column not in targetlist
:PREFIX
SELECT pg_typeof(device_id),
  pg_typeof(v2)
FROM :TEST_TABLE
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
               Order: metrics_space_compressed."time"
               ->  Merge Append (actual rows=1.00 loops=1)
                     Sort Key: metrics_space_compressed."time"
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: _hyper_5_19_chunk."time"
                           Sort Method: top-N heapsort 
                           ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: _hyper_5_20_chunk."time"
                           Sort Method: top-N heapsort 
                           ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: _hyper_5_21_chunk."time"
                           Sort Method: top-N heapsort 
                           ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
               ->  Merge Append (never executed)
                     Sort Key: metrics_space_compressed."time"
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_22_chunk."time"
                           ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_23_chunk."time"
                           ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_24_chunk."time"
                           ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
               ->  Merge Append (never executed)
                     Sort Key: metrics_space_compressed."time"
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_25_chunk."time"
                           ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_26_chunk."time"
                           ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_27_chunk."time"
                           ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_36_chunk (never executed)

-- ORDER BY may include other columns after time column
:PREFIX
SELECT time,
  device_id,
  v0
FROM :TEST_TABLE
WHERE device_id = 1
ORDER BY time DESC,
  device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
         Order: metrics_space_compressed."time" DESC
         ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_6_34_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_34_chunk (actual rows=1.00 loops=1)
                     Index Cond: (device_id = 1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
               ->  Index Scan using compress_hyper_6_31_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_31_chunk (never executed)
                     Index Cond: (device_id = 1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (never executed)
               ->  Index Scan using compress_hyper_6_28_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_28_chunk (never executed)
                     Index Cond: (device_id = 1)

-- test RECORD in targetlist
:PREFIX
SELECT (time,
    device_id,
    v0)
FROM :TEST_TABLE
WHERE device_id = 1
ORDER BY time DESC,
  device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
               Order: metrics_space_compressed."time" DESC
               ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=1.00 loops=1)
                     ->  Index Scan using compress_hyper_6_34_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_34_chunk (actual rows=1.00 loops=1)
                           Index Cond: (device_id = 1)
               ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                     ->  Index Scan using compress_hyper_6_31_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_31_chunk (never executed)
                           Index Cond: (device_id = 1)
               ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (never executed)
                     ->  Index Scan using compress_hyper_6_28_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_28_chunk (never executed)
                           Index Cond: (device_id = 1)

-- test sort column not in targetlist
:PREFIX
SELECT time_bucket('1h', time)
FROM :TEST_TABLE
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
               Order: metrics_space_compressed."time" DESC
               ->  Merge Append (actual rows=1.00 loops=1)
                     Sort Key: metrics_space_compressed."time" DESC
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: _hyper_5_27_chunk."time" DESC
                           Sort Method: top-N heapsort 
                           ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (actual rows=5038.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_36_chunk (actual rows=6.00 loops=1)
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: _hyper_5_26_chunk."time" DESC
                           Sort Method: top-N heapsort 
                           ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=15114.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_35_chunk (actual rows=18.00 loops=1)
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: _hyper_5_25_chunk."time" DESC
                           Sort Method: top-N heapsort 
                           ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=5038.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
               ->  Merge Append (never executed)
                     Sort Key: metrics_space_compressed."time" DESC
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_24_chunk."time" DESC
                           ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_23_chunk."time" DESC
                           ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_22_chunk."time" DESC
                           ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
               ->  Merge Append (never executed)
                     Sort Key: metrics_space_compressed."time" DESC
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_21_chunk."time" DESC
                           ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_30_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_20_chunk."time" DESC
                           ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_29_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_19_chunk."time" DESC
                           ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_28_chunk (never executed)

-- queries with ORDER BY non-time column shouldn't use ordered append
:PREFIX
SELECT device_id
FROM :TEST_TABLE
ORDER BY device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Merge Append (actual rows=1.00 loops=1)
         Sort Key: metrics_space_compressed.device_id
         ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_6_28_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_28_chunk (actual rows=1.00 loops=1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_6_29_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_29_chunk (actual rows=1.00 loops=1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_6_30_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_30_chunk (actual rows=1.00 loops=1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_6_31_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_31_chunk (actual rows=1.00 loops=1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_6_32_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_32_chunk (actual rows=1.00 loops=1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_6_33_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_33_chunk (actual rows=1.00 loops=1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_6_34_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_34_chunk (actual rows=1.00 loops=1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_6_35_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_35_chunk (actual rows=1.00 loops=1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (actual rows=1.00 loops=1)
               ->  Index Scan using compress_hyper_6_36_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_36_chunk (actual rows=1.00 loops=1)

-- time column must be primary sort order
SET enable_seqscan TO false;
:PREFIX
SELECT time,
  device_id
FROM :TEST_TABLE
WHERE device_id IN (1, 2)
ORDER BY device_id,
  time
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Merge Append (actual rows=1.00 loops=1)
         Sort Key: metrics_space_compressed.device_id, metrics_space_compressed."time"
         ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=1.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: compress_hyper_6_28_chunk.device_id, compress_hyper_6_28_chunk._ts_meta_min_1, compress_hyper_6_28_chunk._ts_meta_max_1
                     Sort Method: quicksort 
                     ->  Index Scan using compress_hyper_6_28_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
         ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=1.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: compress_hyper_6_29_chunk.device_id, compress_hyper_6_29_chunk._ts_meta_min_1, compress_hyper_6_29_chunk._ts_meta_max_1
                     Sort Method: quicksort 
                     ->  Index Scan using compress_hyper_6_29_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_29_chunk (actual rows=4.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
         ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=1.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: compress_hyper_6_31_chunk.device_id, compress_hyper_6_31_chunk._ts_meta_min_1, compress_hyper_6_31_chunk._ts_meta_max_1
                     Sort Method: quicksort 
                     ->  Index Scan using compress_hyper_6_31_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_31_chunk (actual rows=6.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
         ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (actual rows=1.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: compress_hyper_6_32_chunk.device_id, compress_hyper_6_32_chunk._ts_meta_min_1, compress_hyper_6_32_chunk._ts_meta_max_1
                     Sort Method: quicksort 
                     ->  Index Scan using compress_hyper_6_32_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_32_chunk (actual rows=6.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
         ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=1.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: compress_hyper_6_34_chunk.device_id, compress_hyper_6_34_chunk._ts_meta_min_1, compress_hyper_6_34_chunk._ts_meta_max_1
                     Sort Method: quicksort 
                     ->  Index Scan using compress_hyper_6_34_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))
         ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=1.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: compress_hyper_6_35_chunk.device_id, compress_hyper_6_35_chunk._ts_meta_min_1, compress_hyper_6_35_chunk._ts_meta_max_1
                     Sort Method: quicksort 
                     ->  Index Scan using compress_hyper_6_35_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_35_chunk (actual rows=6.00 loops=1)
                           Index Cond: (device_id = ANY ('{1,2}'::integer[]))

RESET enable_seqscan;
-- test equality constraint on ORDER BY prefix
-- currently not optimized
SET enable_seqscan TO false;
SET enable_indexonlyscan TO false;
:PREFIX
SELECT time,
  device_id
FROM :TEST_TABLE
WHERE device_id = 1
ORDER BY device_id,
  time
LIMIT 10;
--- QUERY PLAN ---
 Limit (actual rows=10.00 loops=1)
   ->  Merge Append (actual rows=10.00 loops=1)
         Sort Key: metrics_space_compressed."time"
         ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=10.00 loops=1)
               ->  Index Scan Backward using compress_hyper_6_28_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_28_chunk (actual rows=1.00 loops=1)
                     Index Cond: (device_id = 1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=1.00 loops=1)
               ->  Index Scan Backward using compress_hyper_6_31_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_31_chunk (actual rows=1.00 loops=1)
                     Index Cond: (device_id = 1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=1.00 loops=1)
               ->  Index Scan Backward using compress_hyper_6_34_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_34_chunk (actual rows=1.00 loops=1)
                     Index Cond: (device_id = 1)

RESET enable_seqscan;
RESET enable_indexonlyscan;
-- queries without LIMIT should use ordered append
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE device_id IN (1, 2) OR time > '2000-01-06'
ORDER BY time ASC;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=56853.00 loops=1)
   Order: metrics_space_compressed."time"
   ->  Merge Append (actual rows=7196.00 loops=1)
         Sort Key: metrics_space_compressed."time"
         ->  Sort (actual rows=3598.00 loops=1)
               Sort Key: _hyper_5_19_chunk."time"
               Sort Method: quicksort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                     Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                     ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
                           Filter: ((device_id = ANY ('{1,2}'::integer[])) OR (_ts_meta_max_1 > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
         ->  Sort (actual rows=3598.00 loops=1)
               Sort Key: _hyper_5_20_chunk."time"
               Sort Method: quicksort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=3598.00 loops=1)
                     Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                     ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=4.00 loops=1)
                           Filter: ((device_id = ANY ('{1,2}'::integer[])) OR (_ts_meta_max_1 > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                           Rows Removed by Filter: 8
         ->  Sort (actual rows=0.00 loops=1)
               Sort Key: _hyper_5_21_chunk."time"
               Sort Method: quicksort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=0.00 loops=1)
                     Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                     ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=0.00 loops=1)
                           Filter: ((device_id = ANY ('{1,2}'::integer[])) OR (_ts_meta_max_1 > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                           Rows Removed by Filter: 4
   ->  Merge Append (actual rows=24467.00 loops=1)
         Sort Key: metrics_space_compressed."time"
         ->  Sort (actual rows=5038.00 loops=1)
               Sort Key: _hyper_5_22_chunk."time"
               Sort Method: quicksort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=5038.00 loops=1)
                     Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                     ->  Seq Scan on compress_hyper_6_31_chunk (actual rows=6.00 loops=1)
                           Filter: ((device_id = ANY ('{1,2}'::integer[])) OR (_ts_meta_max_1 > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
         ->  Sort (actual rows=14632.00 loops=1)
               Sort Key: _hyper_5_23_chunk."time"
               Sort Method: quicksort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (actual rows=14632.00 loops=1)
                     Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                     Rows Removed by Filter: 406
                     ->  Seq Scan on compress_hyper_6_32_chunk (actual rows=16.00 loops=1)
                           Filter: ((device_id = ANY ('{1,2}'::integer[])) OR (_ts_meta_max_1 > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                           Rows Removed by Filter: 2
         ->  Sort (actual rows=4797.00 loops=1)
               Sort Key: _hyper_5_24_chunk."time"
               Sort Method: quicksort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (actual rows=4797.00 loops=1)
                     Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                     Rows Removed by Filter: 203
                     ->  Seq Scan on compress_hyper_6_33_chunk (actual rows=5.00 loops=1)
                           Filter: ((device_id = ANY ('{1,2}'::integer[])) OR (_ts_meta_max_1 > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                           Rows Removed by Filter: 1
   ->  Merge Append (actual rows=25190.00 loops=1)
         Sort Key: metrics_space_compressed."time"
         ->  Sort (actual rows=5038.00 loops=1)
               Sort Key: _hyper_5_25_chunk."time"
               Sort Method: quicksort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=5038.00 loops=1)
                     Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                     ->  Seq Scan on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
                           Filter: ((device_id = ANY ('{1,2}'::integer[])) OR (_ts_meta_max_1 > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
         ->  Sort (actual rows=15114.00 loops=1)
               Sort Key: _hyper_5_26_chunk."time"
               Sort Method: quicksort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=15114.00 loops=1)
                     Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                     ->  Seq Scan on compress_hyper_6_35_chunk (actual rows=18.00 loops=1)
                           Filter: ((device_id = ANY ('{1,2}'::integer[])) OR (_ts_meta_max_1 > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
         ->  Sort (actual rows=5038.00 loops=1)
               Sort Key: _hyper_5_27_chunk."time"
               Sort Method: quicksort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (actual rows=5038.00 loops=1)
                     Filter: ((device_id = ANY ('{1,2}'::integer[])) OR ("time" > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))
                     ->  Seq Scan on compress_hyper_6_36_chunk (actual rows=6.00 loops=1)
                           Filter: ((device_id = ANY ('{1,2}'::integer[])) OR (_ts_meta_max_1 > 'Thu Jan 06 00:00:00 2000 PST'::timestamp with time zone))

-- queries without ORDER BY shouldnt use ordered append
SET enable_seqscan TO false;
:PREFIX
SELECT pg_typeof(time)
FROM :TEST_TABLE
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Append (actual rows=1.00 loops=1)
               ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=1.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=1.00 loops=1)
               ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (never executed)
                     ->  Seq Scan on compress_hyper_6_29_chunk (never executed)
               ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (never executed)
                     ->  Seq Scan on compress_hyper_6_30_chunk (never executed)
               ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                     ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
               ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                     ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
               ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                     ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
               ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                     ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
               ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                     ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
               ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                     ->  Seq Scan on compress_hyper_6_36_chunk (never executed)

RESET enable_seqscan;
-- test interaction with constraint exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-07'
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
         Order: metrics_space_compressed."time"
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_22_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=4077.00 loops=1)
                           Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                           Rows Removed by Filter: 923
                           ->  Seq Scan on compress_hyper_6_31_chunk (actual rows=5.00 loops=1)
                                 Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                                 Rows Removed by Filter: 1
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_23_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (actual rows=12231.00 loops=1)
                           Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                           Rows Removed by Filter: 2769
                           ->  Seq Scan on compress_hyper_6_32_chunk (actual rows=15.00 loops=1)
                                 Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                                 Rows Removed by Filter: 3
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_24_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (actual rows=4077.00 loops=1)
                           Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                           Rows Removed by Filter: 923
                           ->  Seq Scan on compress_hyper_6_33_chunk (actual rows=5.00 loops=1)
                                 Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                                 Rows Removed by Filter: 1
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (never executed)
                     Sort Key: _hyper_5_25_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                           Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
                                 Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_26_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                           Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
                                 Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_27_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                           Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_36_chunk (never executed)
                                 Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)

:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-07'
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
         Order: metrics_space_compressed."time" DESC
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space_compressed."time" DESC
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_27_chunk."time" DESC
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (actual rows=5038.00 loops=1)
                           Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_36_chunk (actual rows=6.00 loops=1)
                                 Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_26_chunk."time" DESC
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=15114.00 loops=1)
                           Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_35_chunk (actual rows=18.00 loops=1)
                                 Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_25_chunk."time" DESC
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=5038.00 loops=1)
                           Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
                                 Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time" DESC
               ->  Sort (never executed)
                     Sort Key: _hyper_5_24_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                           Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
                                 Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_23_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                           Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                                 Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_22_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                           Vectorized Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
                                 Filter: (_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone)

-- test interaction with runtime exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-08'::text::timestamptz
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
         Order: metrics_space_compressed."time"
         ->  Merge Append (actual rows=0.00 loops=1)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (actual rows=0.00 loops=1)
                     Sort Key: _hyper_5_19_chunk."time"
                     Sort Method: quicksort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=0.00 loops=1)
                           Vectorized Filter: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=0.00 loops=1)
                                 Filter: (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone)
                                 Rows Removed by Filter: 4
               ->  Sort (actual rows=0.00 loops=1)
                     Sort Key: _hyper_5_20_chunk."time"
                     Sort Method: quicksort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=0.00 loops=1)
                           Vectorized Filter: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=0.00 loops=1)
                                 Filter: (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone)
                                 Rows Removed by Filter: 12
               ->  Sort (actual rows=0.00 loops=1)
                     Sort Key: _hyper_5_21_chunk."time"
                     Sort Method: quicksort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=0.00 loops=1)
                           Vectorized Filter: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=0.00 loops=1)
                                 Filter: (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone)
                                 Rows Removed by Filter: 4
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_22_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=3357.00 loops=1)
                           Vectorized Filter: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
                           Rows Removed by Filter: 643
                           ->  Seq Scan on compress_hyper_6_31_chunk (actual rows=4.00 loops=1)
                                 Filter: (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone)
                                 Rows Removed by Filter: 2
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_23_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (actual rows=10071.00 loops=1)
                           Vectorized Filter: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
                           Rows Removed by Filter: 1929
                           ->  Seq Scan on compress_hyper_6_32_chunk (actual rows=12.00 loops=1)
                                 Filter: (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone)
                                 Rows Removed by Filter: 6
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_24_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (actual rows=3357.00 loops=1)
                           Vectorized Filter: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
                           Rows Removed by Filter: 643
                           ->  Seq Scan on compress_hyper_6_33_chunk (actual rows=4.00 loops=1)
                                 Filter: (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone)
                                 Rows Removed by Filter: 2
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (never executed)
                     Sort Key: _hyper_5_25_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                           Vectorized Filter: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
                                 Filter: (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_26_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                           Vectorized Filter: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
                                 Filter: (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_27_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                           Vectorized Filter: ("time" > ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_36_chunk (never executed)
                                 Filter: (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone)

:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time < '2000-01-08'::text::timestamptz
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
         Order: metrics_space_compressed."time"
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_19_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                           Vectorized Filter: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
                                 Filter: (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_20_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                           Vectorized Filter: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
                                 Filter: (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_21_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                           Vectorized Filter: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
                                 Filter: (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone)
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (never executed)
                     Sort Key: _hyper_5_22_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                           Vectorized Filter: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_23_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                           Vectorized Filter: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_24_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                           Vectorized Filter: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone)
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (never executed)
                     Sort Key: _hyper_5_25_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                           Vectorized Filter: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_26_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                           Vectorized Filter: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_27_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                           Vectorized Filter: ("time" < ('2000-01-08'::cstring)::timestamp with time zone)
                           ->  Seq Scan on compress_hyper_6_36_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone)

-- test constraint exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time > '2000-01-08'::text::timestamptz
  AND time < '2000-01-10'
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
         Order: metrics_space_compressed."time"
         ->  Merge Append (actual rows=0.00 loops=1)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (actual rows=0.00 loops=1)
                     Sort Key: _hyper_5_19_chunk."time"
                     Sort Method: quicksort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=0.00 loops=1)
                           Vectorized Filter: (("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND ("time" > ('2000-01-08'::cstring)::timestamp with time zone))
                           ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=0.00 loops=1)
                                 Filter: ((_ts_meta_min_1 < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone))
                                 Rows Removed by Filter: 4
               ->  Sort (actual rows=0.00 loops=1)
                     Sort Key: _hyper_5_20_chunk."time"
                     Sort Method: quicksort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=0.00 loops=1)
                           Vectorized Filter: (("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND ("time" > ('2000-01-08'::cstring)::timestamp with time zone))
                           ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=0.00 loops=1)
                                 Filter: ((_ts_meta_min_1 < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone))
                                 Rows Removed by Filter: 12
               ->  Sort (actual rows=0.00 loops=1)
                     Sort Key: _hyper_5_21_chunk."time"
                     Sort Method: quicksort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=0.00 loops=1)
                           Vectorized Filter: (("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND ("time" > ('2000-01-08'::cstring)::timestamp with time zone))
                           ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=0.00 loops=1)
                                 Filter: ((_ts_meta_min_1 < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone))
                                 Rows Removed by Filter: 4
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_22_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=1439.00 loops=1)
                           Vectorized Filter: (("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND ("time" > ('2000-01-08'::cstring)::timestamp with time zone))
                           Rows Removed by Filter: 1561
                           ->  Seq Scan on compress_hyper_6_31_chunk (actual rows=3.00 loops=1)
                                 Filter: ((_ts_meta_min_1 < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone))
                                 Rows Removed by Filter: 3
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_23_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (actual rows=4317.00 loops=1)
                           Vectorized Filter: (("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND ("time" > ('2000-01-08'::cstring)::timestamp with time zone))
                           Rows Removed by Filter: 4683
                           ->  Seq Scan on compress_hyper_6_32_chunk (actual rows=9.00 loops=1)
                                 Filter: ((_ts_meta_min_1 < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone))
                                 Rows Removed by Filter: 9
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_24_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (actual rows=1439.00 loops=1)
                           Vectorized Filter: (("time" < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND ("time" > ('2000-01-08'::cstring)::timestamp with time zone))
                           Rows Removed by Filter: 1561
                           ->  Seq Scan on compress_hyper_6_33_chunk (actual rows=3.00 loops=1)
                                 Filter: ((_ts_meta_min_1 < 'Mon Jan 10 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_max_1 > ('2000-01-08'::cstring)::timestamp with time zone))
                                 Rows Removed by Filter: 3

:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time < '2000-01-08'::text::timestamptz
  AND time > '2000-01-07'
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
         Order: metrics_space_compressed."time"
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_22_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=719.00 loops=1)
                           Vectorized Filter: (("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND ("time" < ('2000-01-08'::cstring)::timestamp with time zone))
                           Rows Removed by Filter: 1281
                           ->  Seq Scan on compress_hyper_6_31_chunk (actual rows=2.00 loops=1)
                                 Filter: ((_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone))
                                 Rows Removed by Filter: 4
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_23_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (actual rows=2157.00 loops=1)
                           Vectorized Filter: (("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND ("time" < ('2000-01-08'::cstring)::timestamp with time zone))
                           Rows Removed by Filter: 3843
                           ->  Seq Scan on compress_hyper_6_32_chunk (actual rows=6.00 loops=1)
                                 Filter: ((_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone))
                                 Rows Removed by Filter: 12
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_24_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (actual rows=719.00 loops=1)
                           Vectorized Filter: (("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND ("time" < ('2000-01-08'::cstring)::timestamp with time zone))
                           Rows Removed by Filter: 1281
                           ->  Seq Scan on compress_hyper_6_33_chunk (actual rows=2.00 loops=1)
                                 Filter: ((_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone))
                                 Rows Removed by Filter: 4
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (never executed)
                     Sort Key: _hyper_5_25_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                           Vectorized Filter: (("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND ("time" < ('2000-01-08'::cstring)::timestamp with time zone))
                           ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
                                 Filter: ((_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone))
               ->  Sort (never executed)
                     Sort Key: _hyper_5_26_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                           Vectorized Filter: (("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND ("time" < ('2000-01-08'::cstring)::timestamp with time zone))
                           ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
                                 Filter: ((_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone))
               ->  Sort (never executed)
                     Sort Key: _hyper_5_27_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                           Vectorized Filter: (("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND ("time" < ('2000-01-08'::cstring)::timestamp with time zone))
                           ->  Seq Scan on compress_hyper_6_36_chunk (never executed)
                                 Filter: ((_ts_meta_max_1 > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) AND (_ts_meta_min_1 < ('2000-01-08'::cstring)::timestamp with time zone))

-- Disable hash aggregation to get a deterministic test output
SET enable_hashagg = OFF;
-- min/max queries
:PREFIX
SELECT max(time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
                 Order: metrics_space_compressed."time" DESC
                 ->  Merge Append (actual rows=1.00 loops=1)
                       Sort Key: metrics_space_compressed."time" DESC
                       ->  Sort (actual rows=1.00 loops=1)
                             Sort Key: _hyper_5_27_chunk."time" DESC
                             Sort Method: top-N heapsort 
                             ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (actual rows=5038.00 loops=1)
                                   ->  Seq Scan on compress_hyper_6_36_chunk (actual rows=6.00 loops=1)
                       ->  Sort (actual rows=1.00 loops=1)
                             Sort Key: _hyper_5_26_chunk."time" DESC
                             Sort Method: top-N heapsort 
                             ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=15114.00 loops=1)
                                   ->  Seq Scan on compress_hyper_6_35_chunk (actual rows=18.00 loops=1)
                       ->  Sort (actual rows=1.00 loops=1)
                             Sort Key: _hyper_5_25_chunk."time" DESC
                             Sort Method: top-N heapsort 
                             ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=5038.00 loops=1)
                                   ->  Seq Scan on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
                 ->  Merge Append (never executed)
                       Sort Key: metrics_space_compressed."time" DESC
                       ->  Sort (never executed)
                             Sort Key: _hyper_5_24_chunk."time" DESC
                             ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
                       ->  Sort (never executed)
                             Sort Key: _hyper_5_23_chunk."time" DESC
                             ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                       ->  Sort (never executed)
                             Sort Key: _hyper_5_22_chunk."time" DESC
                             ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
                 ->  Merge Append (never executed)
                       Sort Key: metrics_space_compressed."time" DESC
                       ->  Sort (never executed)
                             Sort Key: _hyper_5_21_chunk."time" DESC
                             ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_6_30_chunk (never executed)
                       ->  Sort (never executed)
                             Sort Key: _hyper_5_20_chunk."time" DESC
                             ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_6_29_chunk (never executed)
                       ->  Sort (never executed)
                             Sort Key: _hyper_5_19_chunk."time" DESC
                             ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_6_28_chunk (never executed)

:PREFIX
SELECT min(time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
                 Order: metrics_space_compressed."time"
                 ->  Merge Append (actual rows=1.00 loops=1)
                       Sort Key: metrics_space_compressed."time"
                       ->  Sort (actual rows=1.00 loops=1)
                             Sort Key: _hyper_5_19_chunk."time"
                             Sort Method: top-N heapsort 
                             ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                                   ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
                       ->  Sort (actual rows=1.00 loops=1)
                             Sort Key: _hyper_5_20_chunk."time"
                             Sort Method: top-N heapsort 
                             ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                                   ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
                       ->  Sort (actual rows=1.00 loops=1)
                             Sort Key: _hyper_5_21_chunk."time"
                             Sort Method: top-N heapsort 
                             ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                                   ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
                 ->  Merge Append (never executed)
                       Sort Key: metrics_space_compressed."time"
                       ->  Sort (never executed)
                             Sort Key: _hyper_5_22_chunk."time"
                             ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
                       ->  Sort (never executed)
                             Sort Key: _hyper_5_23_chunk."time"
                             ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                       ->  Sort (never executed)
                             Sort Key: _hyper_5_24_chunk."time"
                             ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
                 ->  Merge Append (never executed)
                       Sort Key: metrics_space_compressed."time"
                       ->  Sort (never executed)
                             Sort Key: _hyper_5_25_chunk."time"
                             ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
                       ->  Sort (never executed)
                             Sort Key: _hyper_5_26_chunk."time"
                             ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
                       ->  Sort (never executed)
                             Sort Key: _hyper_5_27_chunk."time"
                             ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                                   ->  Seq Scan on compress_hyper_6_36_chunk (never executed)

-- test first/last (doesn't use ordered append yet)
:PREFIX
SELECT first(time, time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Result (actual rows=1.00 loops=1)
                 ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
                       Order: metrics_space_compressed."time"
                       ->  Merge Append (actual rows=1.00 loops=1)
                             Sort Key: metrics_space_compressed."time"
                             ->  Sort (actual rows=1.00 loops=1)
                                   Sort Key: _hyper_5_19_chunk."time"
                                   Sort Method: top-N heapsort 
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                                         ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
                             ->  Sort (actual rows=1.00 loops=1)
                                   Sort Key: _hyper_5_20_chunk."time"
                                   Sort Method: top-N heapsort 
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                                         ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
                             ->  Sort (actual rows=1.00 loops=1)
                                   Sort Key: _hyper_5_21_chunk."time"
                                   Sort Method: top-N heapsort 
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                                         ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
                       ->  Merge Append (never executed)
                             Sort Key: metrics_space_compressed."time"
                             ->  Sort (never executed)
                                   Sort Key: _hyper_5_22_chunk."time"
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                                         ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
                             ->  Sort (never executed)
                                   Sort Key: _hyper_5_23_chunk."time"
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                                         ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                             ->  Sort (never executed)
                                   Sort Key: _hyper_5_24_chunk."time"
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                                         ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
                       ->  Merge Append (never executed)
                             Sort Key: metrics_space_compressed."time"
                             ->  Sort (never executed)
                                   Sort Key: _hyper_5_25_chunk."time"
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                                         ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
                             ->  Sort (never executed)
                                   Sort Key: _hyper_5_26_chunk."time"
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                                         ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
                             ->  Sort (never executed)
                                   Sort Key: _hyper_5_27_chunk."time"
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                                         ->  Seq Scan on compress_hyper_6_36_chunk (never executed)

:PREFIX
SELECT last(time, time)
FROM :TEST_TABLE;
--- QUERY PLAN ---
 Result (actual rows=1.00 loops=1)
   InitPlan 1
     ->  Limit (actual rows=1.00 loops=1)
           ->  Result (actual rows=1.00 loops=1)
                 ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
                       Order: metrics_space_compressed."time" DESC
                       ->  Merge Append (actual rows=1.00 loops=1)
                             Sort Key: metrics_space_compressed."time" DESC
                             ->  Sort (actual rows=1.00 loops=1)
                                   Sort Key: _hyper_5_27_chunk."time" DESC
                                   Sort Method: top-N heapsort 
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (actual rows=5038.00 loops=1)
                                         ->  Seq Scan on compress_hyper_6_36_chunk (actual rows=6.00 loops=1)
                             ->  Sort (actual rows=1.00 loops=1)
                                   Sort Key: _hyper_5_26_chunk."time" DESC
                                   Sort Method: top-N heapsort 
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=15114.00 loops=1)
                                         ->  Seq Scan on compress_hyper_6_35_chunk (actual rows=18.00 loops=1)
                             ->  Sort (actual rows=1.00 loops=1)
                                   Sort Key: _hyper_5_25_chunk."time" DESC
                                   Sort Method: top-N heapsort 
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=5038.00 loops=1)
                                         ->  Seq Scan on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
                       ->  Merge Append (never executed)
                             Sort Key: metrics_space_compressed."time" DESC
                             ->  Sort (never executed)
                                   Sort Key: _hyper_5_24_chunk."time" DESC
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                                         ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
                             ->  Sort (never executed)
                                   Sort Key: _hyper_5_23_chunk."time" DESC
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                                         ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                             ->  Sort (never executed)
                                   Sort Key: _hyper_5_22_chunk."time" DESC
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                                         ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
                       ->  Merge Append (never executed)
                             Sort Key: metrics_space_compressed."time" DESC
                             ->  Sort (never executed)
                                   Sort Key: _hyper_5_21_chunk."time" DESC
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (never executed)
                                         ->  Seq Scan on compress_hyper_6_30_chunk (never executed)
                             ->  Sort (never executed)
                                   Sort Key: _hyper_5_20_chunk."time" DESC
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (never executed)
                                         ->  Seq Scan on compress_hyper_6_29_chunk (never executed)
                             ->  Sort (never executed)
                                   Sort Key: _hyper_5_19_chunk."time" DESC
                                   ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (never executed)
                                         ->  Seq Scan on compress_hyper_6_28_chunk (never executed)

-- test query with time_bucket
:PREFIX
SELECT time_bucket('1d', time)
FROM :TEST_TABLE
ORDER BY time ASC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
               Order: metrics_space_compressed."time"
               ->  Merge Append (actual rows=1.00 loops=1)
                     Sort Key: metrics_space_compressed."time"
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: _hyper_5_19_chunk."time"
                           Sort Method: top-N heapsort 
                           ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: _hyper_5_20_chunk."time"
                           Sort Method: top-N heapsort 
                           ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: _hyper_5_21_chunk."time"
                           Sort Method: top-N heapsort 
                           ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
               ->  Merge Append (never executed)
                     Sort Key: metrics_space_compressed."time"
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_22_chunk."time"
                           ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_23_chunk."time"
                           ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_24_chunk."time"
                           ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
               ->  Merge Append (never executed)
                     Sort Key: metrics_space_compressed."time"
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_25_chunk."time"
                           ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_26_chunk."time"
                           ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: _hyper_5_27_chunk."time"
                           ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                                 ->  Seq Scan on compress_hyper_6_36_chunk (never executed)

-- test query with ORDER BY time_bucket
:PREFIX
SELECT time_bucket('1d', time)
FROM :TEST_TABLE
ORDER BY 1
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
               Order: time_bucket('@ 1 day'::interval, metrics_space_compressed."time")
               ->  Merge Append (actual rows=1.00 loops=1)
                     Sort Key: (time_bucket('@ 1 day'::interval, metrics_space_compressed."time"))
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_19_chunk."time"))
                           Sort Method: top-N heapsort 
                           ->  Result (actual rows=3598.00 loops=1)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                                       ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_20_chunk."time"))
                           Sort Method: top-N heapsort 
                           ->  Result (actual rows=10794.00 loops=1)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                                       ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_21_chunk."time"))
                           Sort Method: top-N heapsort 
                           ->  Result (actual rows=3598.00 loops=1)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                                       ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
               ->  Merge Append (never executed)
                     Sort Key: (time_bucket('@ 1 day'::interval, metrics_space_compressed."time"))
                     ->  Sort (never executed)
                           Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_22_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_23_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_24_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
               ->  Merge Append (never executed)
                     Sort Key: (time_bucket('@ 1 day'::interval, metrics_space_compressed."time"))
                     ->  Sort (never executed)
                           Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_25_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_26_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_27_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_36_chunk (never executed)

-- test query with ORDER BY time_bucket, device_id
-- must not use ordered append
:PREFIX
SELECT time_bucket('1d', time),
  device_id,
  v0
FROM :TEST_TABLE
WHERE device_id IN (1, 2)
ORDER BY time_bucket('1d', time),
  device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: (time_bucket('@ 1 day'::interval, metrics_space_compressed."time")), metrics_space_compressed.device_id
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_19_chunk."time")), _hyper_5_19_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=3598.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_20_chunk."time")), _hyper_5_20_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=3598.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=3598.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=4.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
                                       Rows Removed by Filter: 8
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_22_chunk."time")), _hyper_5_22_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=5038.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=5038.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_31_chunk (actual rows=6.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_23_chunk."time")), _hyper_5_23_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=5038.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (actual rows=5038.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_32_chunk (actual rows=6.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
                                       Rows Removed by Filter: 12
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_25_chunk."time")), _hyper_5_25_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=5038.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=5038.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (time_bucket('@ 1 day'::interval, _hyper_5_26_chunk."time")), _hyper_5_26_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=5038.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=5038.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_35_chunk (actual rows=6.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
                                       Rows Removed by Filter: 12

-- test query with ORDER BY date_trunc
:PREFIX
SELECT time_bucket('1d', time)
FROM :TEST_TABLE
ORDER BY date_trunc('day', time)
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
               Order: date_trunc('day'::text, metrics_space_compressed."time")
               ->  Merge Append (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, metrics_space_compressed."time"))
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: (date_trunc('day'::text, _hyper_5_19_chunk."time"))
                           Sort Method: top-N heapsort 
                           ->  Result (actual rows=3598.00 loops=1)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                                       ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: (date_trunc('day'::text, _hyper_5_20_chunk."time"))
                           Sort Method: top-N heapsort 
                           ->  Result (actual rows=10794.00 loops=1)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                                       ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: (date_trunc('day'::text, _hyper_5_21_chunk."time"))
                           Sort Method: top-N heapsort 
                           ->  Result (actual rows=3598.00 loops=1)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                                       ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
               ->  Merge Append (never executed)
                     Sort Key: (date_trunc('day'::text, metrics_space_compressed."time"))
                     ->  Sort (never executed)
                           Sort Key: (date_trunc('day'::text, _hyper_5_22_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: (date_trunc('day'::text, _hyper_5_23_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: (date_trunc('day'::text, _hyper_5_24_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
               ->  Merge Append (never executed)
                     Sort Key: (date_trunc('day'::text, metrics_space_compressed."time"))
                     ->  Sort (never executed)
                           Sort Key: (date_trunc('day'::text, _hyper_5_25_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: (date_trunc('day'::text, _hyper_5_26_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: (date_trunc('day'::text, _hyper_5_27_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_36_chunk (never executed)

-- test query with ORDER BY date_trunc
:PREFIX
SELECT date_trunc('day', time)
FROM :TEST_TABLE
ORDER BY 1
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
               Order: date_trunc('day'::text, metrics_space_compressed."time")
               ->  Merge Append (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, metrics_space_compressed."time"))
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: (date_trunc('day'::text, _hyper_5_19_chunk."time"))
                           Sort Method: top-N heapsort 
                           ->  Result (actual rows=3598.00 loops=1)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                                       ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: (date_trunc('day'::text, _hyper_5_20_chunk."time"))
                           Sort Method: top-N heapsort 
                           ->  Result (actual rows=10794.00 loops=1)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                                       ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
                     ->  Sort (actual rows=1.00 loops=1)
                           Sort Key: (date_trunc('day'::text, _hyper_5_21_chunk."time"))
                           Sort Method: top-N heapsort 
                           ->  Result (actual rows=3598.00 loops=1)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                                       ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
               ->  Merge Append (never executed)
                     Sort Key: (date_trunc('day'::text, metrics_space_compressed."time"))
                     ->  Sort (never executed)
                           Sort Key: (date_trunc('day'::text, _hyper_5_22_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: (date_trunc('day'::text, _hyper_5_23_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: (date_trunc('day'::text, _hyper_5_24_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
               ->  Merge Append (never executed)
                     Sort Key: (date_trunc('day'::text, metrics_space_compressed."time"))
                     ->  Sort (never executed)
                           Sort Key: (date_trunc('day'::text, _hyper_5_25_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: (date_trunc('day'::text, _hyper_5_26_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
                     ->  Sort (never executed)
                           Sort Key: (date_trunc('day'::text, _hyper_5_27_chunk."time"))
                           ->  Result (never executed)
                                 ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                                       ->  Seq Scan on compress_hyper_6_36_chunk (never executed)

-- test query with ORDER BY date_trunc, device_id
-- must not use ordered append
:PREFIX
SELECT date_trunc('day', time),
  device_id,
  v0
FROM :TEST_TABLE
WHERE device_id IN (1, 2)
ORDER BY 1,
  2
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Result (actual rows=1.00 loops=1)
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: (date_trunc('day'::text, metrics_space_compressed."time")), metrics_space_compressed.device_id
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, _hyper_5_19_chunk."time")), _hyper_5_19_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=3598.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, _hyper_5_20_chunk."time")), _hyper_5_20_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=3598.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=3598.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=4.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
                                       Rows Removed by Filter: 8
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, _hyper_5_22_chunk."time")), _hyper_5_22_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=5038.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=5038.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_31_chunk (actual rows=6.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, _hyper_5_23_chunk."time")), _hyper_5_23_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=5038.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (actual rows=5038.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_32_chunk (actual rows=6.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
                                       Rows Removed by Filter: 12
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, _hyper_5_25_chunk."time")), _hyper_5_25_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=5038.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=5038.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: (date_trunc('day'::text, _hyper_5_26_chunk."time")), _hyper_5_26_chunk.device_id
                     Sort Method: top-N heapsort 
                     ->  Result (actual rows=5038.00 loops=1)
                           ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=5038.00 loops=1)
                                 ->  Seq Scan on compress_hyper_6_35_chunk (actual rows=6.00 loops=1)
                                       Filter: (device_id = ANY ('{1,2}'::integer[]))
                                       Rows Removed by Filter: 12

-- test query with now() should result in ordered ChunkAppend
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time < now() + '1 month'
ORDER BY time DESC
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=1.00 loops=1)
         Order: metrics_space_compressed."time" DESC
         ->  Merge Append (actual rows=1.00 loops=1)
               Sort Key: metrics_space_compressed."time" DESC
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_27_chunk."time" DESC
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (actual rows=5038.00 loops=1)
                           Vectorized Filter: ("time" < (now() + '@ 1 mon'::interval))
                           ->  Seq Scan on compress_hyper_6_36_chunk (actual rows=6.00 loops=1)
                                 Filter: (_ts_meta_min_1 < (now() + '@ 1 mon'::interval))
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_26_chunk."time" DESC
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=15114.00 loops=1)
                           Vectorized Filter: ("time" < (now() + '@ 1 mon'::interval))
                           ->  Seq Scan on compress_hyper_6_35_chunk (actual rows=18.00 loops=1)
                                 Filter: (_ts_meta_min_1 < (now() + '@ 1 mon'::interval))
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_25_chunk."time" DESC
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=5038.00 loops=1)
                           Vectorized Filter: ("time" < (now() + '@ 1 mon'::interval))
                           ->  Seq Scan on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
                                 Filter: (_ts_meta_min_1 < (now() + '@ 1 mon'::interval))
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time" DESC
               ->  Sort (never executed)
                     Sort Key: _hyper_5_24_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                           Vectorized Filter: ("time" < (now() + '@ 1 mon'::interval))
                           ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < (now() + '@ 1 mon'::interval))
               ->  Sort (never executed)
                     Sort Key: _hyper_5_23_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                           Vectorized Filter: ("time" < (now() + '@ 1 mon'::interval))
                           ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < (now() + '@ 1 mon'::interval))
               ->  Sort (never executed)
                     Sort Key: _hyper_5_22_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                           Vectorized Filter: ("time" < (now() + '@ 1 mon'::interval))
                           ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < (now() + '@ 1 mon'::interval))
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time" DESC
               ->  Sort (never executed)
                     Sort Key: _hyper_5_21_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (never executed)
                           Vectorized Filter: ("time" < (now() + '@ 1 mon'::interval))
                           ->  Seq Scan on compress_hyper_6_30_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < (now() + '@ 1 mon'::interval))
               ->  Sort (never executed)
                     Sort Key: _hyper_5_20_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (never executed)
                           Vectorized Filter: ("time" < (now() + '@ 1 mon'::interval))
                           ->  Seq Scan on compress_hyper_6_29_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < (now() + '@ 1 mon'::interval))
               ->  Sort (never executed)
                     Sort Key: _hyper_5_19_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (never executed)
                           Vectorized Filter: ("time" < (now() + '@ 1 mon'::interval))
                           ->  Seq Scan on compress_hyper_6_28_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < (now() + '@ 1 mon'::interval))

-- test CTE
:PREFIX WITH i AS (
  SELECT time
  FROM :TEST_TABLE
  WHERE time < now()
  ORDER BY time DESC
  LIMIT 100
)
SELECT *
FROM i;
--- QUERY PLAN ---
 Limit (actual rows=100.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=100.00 loops=1)
         Order: metrics_space_compressed."time" DESC
         ->  Merge Append (actual rows=100.00 loops=1)
               Sort Key: metrics_space_compressed."time" DESC
               ->  Sort (actual rows=21.00 loops=1)
                     Sort Key: _hyper_5_27_chunk."time" DESC
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (actual rows=5038.00 loops=1)
                           Vectorized Filter: ("time" < now())
                           ->  Seq Scan on compress_hyper_6_36_chunk (actual rows=6.00 loops=1)
                                 Filter: (_ts_meta_min_1 < now())
               ->  Sort (actual rows=60.00 loops=1)
                     Sort Key: _hyper_5_26_chunk."time" DESC
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=15114.00 loops=1)
                           Vectorized Filter: ("time" < now())
                           ->  Seq Scan on compress_hyper_6_35_chunk (actual rows=18.00 loops=1)
                                 Filter: (_ts_meta_min_1 < now())
               ->  Sort (actual rows=21.00 loops=1)
                     Sort Key: _hyper_5_25_chunk."time" DESC
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=5038.00 loops=1)
                           Vectorized Filter: ("time" < now())
                           ->  Seq Scan on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
                                 Filter: (_ts_meta_min_1 < now())
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time" DESC
               ->  Sort (never executed)
                     Sort Key: _hyper_5_24_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                           Vectorized Filter: ("time" < now())
                           ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < now())
               ->  Sort (never executed)
                     Sort Key: _hyper_5_23_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                           Vectorized Filter: ("time" < now())
                           ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < now())
               ->  Sort (never executed)
                     Sort Key: _hyper_5_22_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                           Vectorized Filter: ("time" < now())
                           ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < now())
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time" DESC
               ->  Sort (never executed)
                     Sort Key: _hyper_5_21_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (never executed)
                           Vectorized Filter: ("time" < now())
                           ->  Seq Scan on compress_hyper_6_30_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < now())
               ->  Sort (never executed)
                     Sort Key: _hyper_5_20_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (never executed)
                           Vectorized Filter: ("time" < now())
                           ->  Seq Scan on compress_hyper_6_29_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < now())
               ->  Sort (never executed)
                     Sort Key: _hyper_5_19_chunk."time" DESC
                     ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (never executed)
                           Vectorized Filter: ("time" < now())
                           ->  Seq Scan on compress_hyper_6_28_chunk (never executed)
                                 Filter: (_ts_meta_min_1 < now())

-- test CTE
-- no chunk exclusion for CTE because cte query is not pulled up
SET enable_seqscan TO false;
:PREFIX WITH cte AS (
  SELECT time
  FROM :TEST_TABLE
  WHERE device_id = 1
  ORDER BY time
)
SELECT *
FROM cte
WHERE time < '2000-02-01'::timestamptz;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=13674.00 loops=1)
   Order: metrics_space_compressed."time"
   ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
         Vectorized Filter: ("time" < 'Tue Feb 01 00:00:00 2000 PST'::timestamp with time zone)
         ->  Index Scan Backward using compress_hyper_6_28_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
               Index Cond: ((device_id = 1) AND (_ts_meta_min_1 < 'Tue Feb 01 00:00:00 2000 PST'::timestamp with time zone))
   ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=5038.00 loops=1)
         Vectorized Filter: ("time" < 'Tue Feb 01 00:00:00 2000 PST'::timestamp with time zone)
         ->  Index Scan Backward using compress_hyper_6_31_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_31_chunk (actual rows=6.00 loops=1)
               Index Cond: ((device_id = 1) AND (_ts_meta_min_1 < 'Tue Feb 01 00:00:00 2000 PST'::timestamp with time zone))
   ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=5038.00 loops=1)
         Vectorized Filter: ("time" < 'Tue Feb 01 00:00:00 2000 PST'::timestamp with time zone)
         ->  Index Scan Backward using compress_hyper_6_34_chunk_device_id__ts_meta_min_1__ts_meta_idx on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
               Index Cond: ((device_id = 1) AND (_ts_meta_min_1 < 'Tue Feb 01 00:00:00 2000 PST'::timestamp with time zone))

RESET enable_seqscan;
-- test subquery
-- not ChunkAppend so no chunk exclusion
:PREFIX
SELECT time
FROM :TEST_TABLE
WHERE time = (
    SELECT max(time)
    FROM :TEST_TABLE)
ORDER BY time;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=5.00 loops=1)
   InitPlan 2
     ->  Result (actual rows=1.00 loops=1)
           InitPlan 1
             ->  Limit (actual rows=1.00 loops=1)
                   ->  Custom Scan (ChunkAppend) on metrics_space_compressed metrics_space_compressed_1 (actual rows=1.00 loops=1)
                         Order: metrics_space_compressed_1."time" DESC
                         ->  Merge Append (actual rows=1.00 loops=1)
                               Sort Key: metrics_space_compressed_1."time" DESC
                               ->  Sort (actual rows=1.00 loops=1)
                                     Sort Key: _hyper_5_27_chunk_1."time" DESC
                                     Sort Method: top-N heapsort 
                                     ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk _hyper_5_27_chunk_1 (actual rows=5038.00 loops=1)
                                           ->  Seq Scan on compress_hyper_6_36_chunk compress_hyper_6_36_chunk_1 (actual rows=6.00 loops=1)
                               ->  Sort (actual rows=1.00 loops=1)
                                     Sort Key: _hyper_5_26_chunk_1."time" DESC
                                     Sort Method: top-N heapsort 
                                     ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk _hyper_5_26_chunk_1 (actual rows=15114.00 loops=1)
                                           ->  Seq Scan on compress_hyper_6_35_chunk compress_hyper_6_35_chunk_1 (actual rows=18.00 loops=1)
                               ->  Sort (actual rows=1.00 loops=1)
                                     Sort Key: _hyper_5_25_chunk_1."time" DESC
                                     Sort Method: top-N heapsort 
                                     ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk _hyper_5_25_chunk_1 (actual rows=5038.00 loops=1)
                                           ->  Seq Scan on compress_hyper_6_34_chunk compress_hyper_6_34_chunk_1 (actual rows=6.00 loops=1)
                         ->  Merge Append (never executed)
                               Sort Key: metrics_space_compressed_1."time" DESC
                               ->  Sort (never executed)
                                     Sort Key: _hyper_5_24_chunk_1."time" DESC
                                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk _hyper_5_24_chunk_1 (never executed)
                                           ->  Seq Scan on compress_hyper_6_33_chunk compress_hyper_6_33_chunk_1 (never executed)
                               ->  Sort (never executed)
                                     Sort Key: _hyper_5_23_chunk_1."time" DESC
                                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk _hyper_5_23_chunk_1 (never executed)
                                           ->  Seq Scan on compress_hyper_6_32_chunk compress_hyper_6_32_chunk_1 (never executed)
                               ->  Sort (never executed)
                                     Sort Key: _hyper_5_22_chunk_1."time" DESC
                                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk _hyper_5_22_chunk_1 (never executed)
                                           ->  Seq Scan on compress_hyper_6_31_chunk compress_hyper_6_31_chunk_1 (never executed)
                         ->  Merge Append (never executed)
                               Sort Key: metrics_space_compressed_1."time" DESC
                               ->  Sort (never executed)
                                     Sort Key: _hyper_5_21_chunk_1."time" DESC
                                     ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk _hyper_5_21_chunk_1 (never executed)
                                           ->  Seq Scan on compress_hyper_6_30_chunk compress_hyper_6_30_chunk_1 (never executed)
                               ->  Sort (never executed)
                                     Sort Key: _hyper_5_20_chunk_1."time" DESC
                                     ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk _hyper_5_20_chunk_1 (never executed)
                                           ->  Seq Scan on compress_hyper_6_29_chunk compress_hyper_6_29_chunk_1 (never executed)
                               ->  Sort (never executed)
                                     Sort Key: _hyper_5_19_chunk_1."time" DESC
                                     ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk _hyper_5_19_chunk_1 (never executed)
                                           ->  Seq Scan on compress_hyper_6_28_chunk compress_hyper_6_28_chunk_1 (never executed)
   ->  Merge Append (actual rows=0.00 loops=1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=0.00 loops=1)
               Filter: ("time" = (InitPlan 2).col1)
               ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=0.00 loops=1)
                     Filter: ((_ts_meta_min_1 <= (InitPlan 2).col1) AND (_ts_meta_max_1 >= (InitPlan 2).col1))
                     Rows Removed by Filter: 4
         ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=0.00 loops=1)
               Filter: ("time" = (InitPlan 2).col1)
               ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=0.00 loops=1)
                     Filter: ((_ts_meta_min_1 <= (InitPlan 2).col1) AND (_ts_meta_max_1 >= (InitPlan 2).col1))
                     Rows Removed by Filter: 12
         ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=0.00 loops=1)
               Filter: ("time" = (InitPlan 2).col1)
               ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=0.00 loops=1)
                     Filter: ((_ts_meta_min_1 <= (InitPlan 2).col1) AND (_ts_meta_max_1 >= (InitPlan 2).col1))
                     Rows Removed by Filter: 4
   ->  Merge Append (actual rows=0.00 loops=1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=0.00 loops=1)
               Filter: ("time" = (InitPlan 2).col1)
               ->  Seq Scan on compress_hyper_6_31_chunk (actual rows=0.00 loops=1)
                     Filter: ((_ts_meta_min_1 <= (InitPlan 2).col1) AND (_ts_meta_max_1 >= (InitPlan 2).col1))
                     Rows Removed by Filter: 6
         ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (actual rows=0.00 loops=1)
               Filter: ("time" = (InitPlan 2).col1)
               ->  Seq Scan on compress_hyper_6_32_chunk (actual rows=0.00 loops=1)
                     Filter: ((_ts_meta_min_1 <= (InitPlan 2).col1) AND (_ts_meta_max_1 >= (InitPlan 2).col1))
                     Rows Removed by Filter: 18
         ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (actual rows=0.00 loops=1)
               Filter: ("time" = (InitPlan 2).col1)
               ->  Seq Scan on compress_hyper_6_33_chunk (actual rows=0.00 loops=1)
                     Filter: ((_ts_meta_min_1 <= (InitPlan 2).col1) AND (_ts_meta_max_1 >= (InitPlan 2).col1))
                     Rows Removed by Filter: 6
   ->  Merge Append (actual rows=5.00 loops=1)
         ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=1.00 loops=1)
               Filter: ("time" = (InitPlan 2).col1)
               Rows Removed by Filter: 999
               ->  Seq Scan on compress_hyper_6_34_chunk (actual rows=1.00 loops=1)
                     Filter: ((_ts_meta_min_1 <= (InitPlan 2).col1) AND (_ts_meta_max_1 >= (InitPlan 2).col1))
                     Rows Removed by Filter: 5
         ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=3.00 loops=1)
               Filter: ("time" = (InitPlan 2).col1)
               Rows Removed by Filter: 2997
               ->  Seq Scan on compress_hyper_6_35_chunk (actual rows=3.00 loops=1)
                     Filter: ((_ts_meta_min_1 <= (InitPlan 2).col1) AND (_ts_meta_max_1 >= (InitPlan 2).col1))
                     Rows Removed by Filter: 15
         ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (actual rows=1.00 loops=1)
               Filter: ("time" = (InitPlan 2).col1)
               Rows Removed by Filter: 999
               ->  Seq Scan on compress_hyper_6_36_chunk (actual rows=1.00 loops=1)
                     Filter: ((_ts_meta_min_1 <= (InitPlan 2).col1) AND (_ts_meta_max_1 >= (InitPlan 2).col1))
                     Rows Removed by Filter: 5

-- test ordered append with limit expression
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT (
  SELECT length('four'));
--- QUERY PLAN ---
 Limit (actual rows=4.00 loops=1)
   InitPlan 1
     ->  Result (actual rows=1.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=4.00 loops=1)
         Order: metrics_space_compressed."time"
         ->  Merge Append (actual rows=4.00 loops=1)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (actual rows=2.00 loops=1)
                     Sort Key: _hyper_5_19_chunk."time"
                     Sort Method: quicksort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
               ->  Sort (actual rows=3.00 loops=1)
                     Sort Key: _hyper_5_20_chunk."time"
                     Sort Method: quicksort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_21_chunk."time"
                     Sort Method: quicksort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (never executed)
                     Sort Key: _hyper_5_22_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_23_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_24_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (never executed)
                     Sort Key: _hyper_5_25_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_26_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_27_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_36_chunk (never executed)

-- test with ordered guc disabled
SET timescaledb.enable_ordered_append TO OFF;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Merge Append (actual rows=3.00 loops=1)
         Sort Key: metrics_space_compressed."time"
         ->  Sort (actual rows=2.00 loops=1)
               Sort Key: _hyper_5_19_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
         ->  Sort (actual rows=2.00 loops=1)
               Sort Key: _hyper_5_20_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_21_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_22_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=5038.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_31_chunk (actual rows=6.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_23_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (actual rows=15114.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_32_chunk (actual rows=18.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_24_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (actual rows=5038.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_33_chunk (actual rows=6.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_25_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=5038.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_26_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=15114.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_35_chunk (actual rows=18.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_27_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (actual rows=5038.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_36_chunk (actual rows=6.00 loops=1)

RESET timescaledb.enable_ordered_append;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=3.00 loops=1)
         Order: metrics_space_compressed."time"
         ->  Merge Append (actual rows=3.00 loops=1)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (actual rows=2.00 loops=1)
                     Sort Key: _hyper_5_19_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
               ->  Sort (actual rows=2.00 loops=1)
                     Sort Key: _hyper_5_20_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_21_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (never executed)
                     Sort Key: _hyper_5_22_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_23_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_24_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (never executed)
                     Sort Key: _hyper_5_25_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_26_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_27_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_36_chunk (never executed)

-- test with chunk append disabled
SET timescaledb.enable_chunk_append TO OFF;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Merge Append (actual rows=3.00 loops=1)
         Sort Key: metrics_space_compressed."time"
         ->  Sort (actual rows=2.00 loops=1)
               Sort Key: _hyper_5_19_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
         ->  Sort (actual rows=2.00 loops=1)
               Sort Key: _hyper_5_20_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_21_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_22_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (actual rows=5038.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_31_chunk (actual rows=6.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_23_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (actual rows=15114.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_32_chunk (actual rows=18.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_24_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (actual rows=5038.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_33_chunk (actual rows=6.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_25_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (actual rows=5038.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_34_chunk (actual rows=6.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_26_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (actual rows=15114.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_35_chunk (actual rows=18.00 loops=1)
         ->  Sort (actual rows=1.00 loops=1)
               Sort Key: _hyper_5_27_chunk."time"
               Sort Method: top-N heapsort 
               ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (actual rows=5038.00 loops=1)
                     ->  Seq Scan on compress_hyper_6_36_chunk (actual rows=6.00 loops=1)

RESET timescaledb.enable_chunk_append;
:PREFIX
SELECT time
FROM :TEST_TABLE
ORDER BY time
LIMIT 3;
--- QUERY PLAN ---
 Limit (actual rows=3.00 loops=1)
   ->  Custom Scan (ChunkAppend) on metrics_space_compressed (actual rows=3.00 loops=1)
         Order: metrics_space_compressed."time"
         ->  Merge Append (actual rows=3.00 loops=1)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (actual rows=2.00 loops=1)
                     Sort Key: _hyper_5_19_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_19_chunk (actual rows=3598.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_28_chunk (actual rows=4.00 loops=1)
               ->  Sort (actual rows=2.00 loops=1)
                     Sort Key: _hyper_5_20_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_20_chunk (actual rows=10794.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_29_chunk (actual rows=12.00 loops=1)
               ->  Sort (actual rows=1.00 loops=1)
                     Sort Key: _hyper_5_21_chunk."time"
                     Sort Method: top-N heapsort 
                     ->  Custom Scan (ColumnarScan) on _hyper_5_21_chunk (actual rows=3598.00 loops=1)
                           ->  Seq Scan on compress_hyper_6_30_chunk (actual rows=4.00 loops=1)
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (never executed)
                     Sort Key: _hyper_5_22_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_22_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_31_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_23_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_23_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_32_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_24_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_24_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_33_chunk (never executed)
         ->  Merge Append (never executed)
               Sort Key: metrics_space_compressed."time"
               ->  Sort (never executed)
                     Sort Key: _hyper_5_25_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_25_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_34_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_26_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_26_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_35_chunk (never executed)
               ->  Sort (never executed)
                     Sort Key: _hyper_5_27_chunk."time"
                     ->  Custom Scan (ColumnarScan) on _hyper_5_27_chunk (never executed)
                           ->  Seq Scan on compress_hyper_6_36_chunk (never executed)

-- get results for all the queries
-- run queries on uncompressed hypertable and store result
\set PREFIX ''
\set PREFIX_VERBOSE ''
\set ECHO none
