-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
\set ON_ERROR_STOP 0
\c :TEST_DBNAME :ROLE_SUPERUSER
SET client_min_messages = ERROR;
DROP TABLESPACE IF EXISTS tablespace1;
DROP TABLESPACE IF EXISTS tablespace2;
DROP TABLESPACE IF EXISTS tablespace3;
SET client_min_messages = NOTICE;
CREATE TABLESPACE tablespace1 OWNER :ROLE_DEFAULT_PERM_USER LOCATION :TEST_TABLESPACE1_PATH;
CREATE TABLESPACE tablespace2 OWNER :ROLE_DEFAULT_PERM_USER_2 LOCATION :TEST_TABLESPACE2_PATH;
--Running some of the same tests as we do for reorder/cluster because that's how move is implemented
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
\set ON_ERROR_STOP 1
\ir include/cluster_test_setup.sql
-- 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 TABLE cluster_test(time INTEGER, temp float, location int, value TEXT);
SELECT create_hypertable('cluster_test', 'time', chunk_time_interval => 5);
     create_hypertable     
---------------------------
 (1,public,cluster_test,t)

CREATE INDEX on cluster_test (location);
CREATE OR REPLACE FUNCTION ensure_scans_work(table_name TEXT, should_output REGCLASS=NULL, seqscan BOOLEAN=FALSE, indexscan BOOLEAN=FALSE, bitmapscan BOOLEAN=FALSE) RETURNS TABLE (a TEXT, b TEXT)
LANGUAGE SQL STABLE AS
$BODY$
    SELECT format($INNER$
            SET LOCAL enable_seqscan=%1$s;
            SET LOCAL enable_indexscan=%2$s;
            SET LOCAL enable_bitmapscan=%3$s;
            EXPLAIN (buffers off, costs off) SELECT * FROM %4$I WHERE time=7
        $INNER$, seqscan, indexscan, bitmapscan, table_name) as a,
        format($INNER$
            WITH T1 as (SELECT * FROM %1$I WHERE time=7),
                 T2 as (SELECT * FROM %2$I WHERE time=7)
            SELECT T1.time, T2.time, T1.temp, T2.temp, T1.location, T2.location,
                   substring(T1.value for 30), substring(T2.value for 30),
                   length(T1.value), length(T2.value)
            FROM T1 FULL OUTER JOIN T2 ON T1 = T2
            WHERE T1 IS NULL OR T2 IS NULL
        $INNER$, table_name, should_output) as b;
$BODY$;
CREATE OR REPLACE FUNCTION ensure_scans_work_no_val(table_name TEXT, should_output REGCLASS=NULL, seqscan BOOLEAN=FALSE, indexscan BOOLEAN=FALSE, bitmapscan BOOLEAN=FALSE) RETURNS TABLE (a TEXT, b TEXT)
LANGUAGE SQL STABLE AS
$BODY$
    SELECT format($INNER$
            SET LOCAL enable_seqscan=%1$s;
            SET LOCAL enable_indexscan=%2$s;
            SET LOCAL enable_bitmapscan=%3$s;
            EXPLAIN (buffers off, costs off) SELECT * FROM %4$I WHERE time=6
        $INNER$, seqscan, indexscan, bitmapscan, table_name) as a,
        format($INNER$
            WITH T1 as (SELECT * FROM %1$I WHERE time=7),
                 T2 as (SELECT * FROM %2$I WHERE time=7)
            SELECT T1.time, T2.time, T1.temp, T2.temp, T1.location, T2.location
            FROM T1 FULL OUTER JOIN T2 ON T1 = T2
            WHERE T1 IS NULL OR T2 IS NULL
        $INNER$, table_name, should_output) as b;
$BODY$;
-- Show default indexes
SELECT * FROM test.show_indexes('cluster_test');
           Index           |  Columns   | Expr | Unique | Primary | Exclusion | Tablespace 
---------------------------+------------+------+--------+---------+-----------+------------
 cluster_test_location_idx | {location} |      | f      | f       | f         | 
 cluster_test_time_idx     | {time}     |      | f      | f       | f         | 

-- Show clustered indexes
SELECT indexrelid::regclass, indisclustered
FROM pg_index
WHERE indisclustered = true ORDER BY 1;
 indexrelid | indisclustered 
------------+----------------

-- Create two chunks
INSERT INTO cluster_test VALUES
    ( 0, 18.9, 3, 'a'),
    ( 8, 13.3, 7, 'b'),
    ( 3, 19.4, 3, 'c'),
    ( 6, 27.3, 9, 'd'),
    ( 5, 25.4, 1, 'e'),
    ( 4, 18.3, 2, 'f'),
    ( 2, 12.4, 8, 'g'),
    ( 7, 20.3, 4, repeat('xyzzy', 100000)), -- toasted value
    ( 9, 11.4, 1, 'h'),
    ( 1, 23.4, 5, 'i');
CREATE TABLE expected AS SELECT * FROM cluster_test;
-- original results to be compared against
SELECT ctid, time, temp, location, substring(value for 30), length(value)
FROM _timescaledb_internal._hyper_1_1_chunk ORDER BY ctid;
 ctid  | time | temp | location | substring | length 
-------+------+------+----------+-----------+--------
 (0,1) |    0 | 18.9 |        3 | a         |      1
 (0,2) |    3 | 19.4 |        3 | c         |      1
 (0,3) |    4 | 18.3 |        2 | f         |      1
 (0,4) |    2 | 12.4 |        8 | g         |      1
 (0,5) |    1 | 23.4 |        5 | i         |      1

SELECT ctid, time, temp, location, substring(value for 30), length(value)
FROM _timescaledb_internal._hyper_1_2_chunk ORDER BY ctid;
 ctid  | time | temp | location |           substring            | length 
-------+------+------+----------+--------------------------------+--------
 (0,1) |    8 | 13.3 |        7 | b                              |      1
 (0,2) |    6 | 27.3 |        9 | d                              |      1
 (0,3) |    5 | 25.4 |        1 | e                              |      1
 (0,4) |    7 | 20.3 |        4 | xyzzyxyzzyxyzzyxyzzyxyzzyxyzzy | 500000
 (0,5) |    9 | 11.4 |        1 | h                              |      1

BEGIN;
SELECT * FROM ensure_scans_work('cluster_test', should_output => 'expected', seqscan => true) \gexec

            SET LOCAL enable_seqscan=t;
            SET LOCAL enable_indexscan=f;
            SET LOCAL enable_bitmapscan=f;
            EXPLAIN (buffers off, costs off) SELECT * FROM cluster_test WHERE time=7
        
--- QUERY PLAN ---
 Seq Scan on _hyper_1_2_chunk
   Filter: ("time" = 7)


            WITH T1 as (SELECT * FROM cluster_test WHERE time=7),
                 T2 as (SELECT * FROM expected WHERE time=7)
            SELECT T1.time, T2.time, T1.temp, T2.temp, T1.location, T2.location,
                   substring(T1.value for 30), substring(T2.value for 30),
                   length(T1.value), length(T2.value)
            FROM T1 FULL OUTER JOIN T2 ON T1 = T2
            WHERE T1 IS NULL OR T2 IS NULL
        
 time | time | temp | temp | location | location | substring | substring | length | length 
------+------+------+------+----------+----------+-----------+-----------+--------+--------

SELECT * FROM ensure_scans_work('cluster_test', should_output => 'expected', indexscan => true) \gexec

            SET LOCAL enable_seqscan=f;
            SET LOCAL enable_indexscan=t;
            SET LOCAL enable_bitmapscan=f;
            EXPLAIN (buffers off, costs off) SELECT * FROM cluster_test WHERE time=7
        
--- QUERY PLAN ---
 Index Scan using _hyper_1_2_chunk_cluster_test_time_idx on _hyper_1_2_chunk
   Index Cond: ("time" = 7)


            WITH T1 as (SELECT * FROM cluster_test WHERE time=7),
                 T2 as (SELECT * FROM expected WHERE time=7)
            SELECT T1.time, T2.time, T1.temp, T2.temp, T1.location, T2.location,
                   substring(T1.value for 30), substring(T2.value for 30),
                   length(T1.value), length(T2.value)
            FROM T1 FULL OUTER JOIN T2 ON T1 = T2
            WHERE T1 IS NULL OR T2 IS NULL
        
 time | time | temp | temp | location | location | substring | substring | length | length 
------+------+------+------+----------+----------+-----------+-----------+--------+--------

SELECT * FROM ensure_scans_work('cluster_test', should_output => 'expected', bitmapscan => true) \gexec

            SET LOCAL enable_seqscan=f;
            SET LOCAL enable_indexscan=f;
            SET LOCAL enable_bitmapscan=t;
            EXPLAIN (buffers off, costs off) SELECT * FROM cluster_test WHERE time=7
        
--- QUERY PLAN ---
 Bitmap Heap Scan on _hyper_1_2_chunk
   Recheck Cond: ("time" = 7)
   ->  Bitmap Index Scan on _hyper_1_2_chunk_cluster_test_time_idx
         Index Cond: ("time" = 7)


            WITH T1 as (SELECT * FROM cluster_test WHERE time=7),
                 T2 as (SELECT * FROM expected WHERE time=7)
            SELECT T1.time, T2.time, T1.temp, T2.temp, T1.location, T2.location,
                   substring(T1.value for 30), substring(T2.value for 30),
                   length(T1.value), length(T2.value)
            FROM T1 FULL OUTER JOIN T2 ON T1 = T2
            WHERE T1 IS NULL OR T2 IS NULL
        
 time | time | temp | temp | location | location | substring | substring | length | length 
------+------+------+------+----------+----------+-----------+-----------+--------+--------

COMMIT;
-- Show chunk indexes
SELECT * FROM test.show_indexes('_timescaledb_internal._hyper_1_1_chunk');
                              Index                               |  Columns   | Expr | Unique | Primary | Exclusion | Tablespace 
------------------------------------------------------------------+------------+------+--------+---------+-----------+------------
 _timescaledb_internal._hyper_1_1_chunk_cluster_test_location_idx | {location} |      | f      | f       | f         | 
 _timescaledb_internal._hyper_1_1_chunk_cluster_test_time_idx     | {time}     |      | f      | f       | f         | 

SELECT * FROM test.show_indexes('_timescaledb_internal._hyper_1_2_chunk');
                              Index                               |  Columns   | Expr | Unique | Primary | Exclusion | Tablespace 
------------------------------------------------------------------+------------+------+--------+---------+-----------+------------
 _timescaledb_internal._hyper_1_2_chunk_cluster_test_location_idx | {location} |      | f      | f       | f         | 
 _timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx     | {time}     |      | f      | f       | f         | 

\set ON_ERROR_STOP 0
-- cannot move a chunk with no reorder index on first call
SELECT move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>'tablespace1', index_destination_tablespace=>'tablespace1');
ERROR:  there is no previously clustered index for table "_hyper_1_2_chunk"
-- cannot move a chunk without a destination tablespace set
SELECT move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>NULL, index_destination_tablespace=>'tablespace1', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx');
ERROR:  valid chunk, destination_tablespace, and index_destination_tablespaces are required
-- cannot move a chunk without an index_destination_tablespace set
SELECT move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>'tablespace1', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx');
ERROR:  valid chunk, destination_tablespace, and index_destination_tablespaces are required
-- cannot move a chunk or an index to a tablespace we do not have create permissions on
SELECT move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>'tablespace2', index_destination_tablespace=>'tablespace1', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx');
ERROR:  permission denied for tablespace "tablespace2"
SELECT move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>'tablespace1', index_destination_tablespace=>'tablespace2', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx');
ERROR:  permission denied for tablespace "tablespace2"
-- cannot move to a nonexistent tablespace
SELECT move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>'tablespace3', index_destination_tablespace=>'tablespace1', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx');
ERROR:  tablespace "tablespace3" does not exist
SELECT move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>'tablespace1', index_destination_tablespace=>'tablespace3', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx');
ERROR:  tablespace "tablespace3" does not exist
-- cannot move a hypertable (must specify a chunk)
SELECT move_chunk(chunk=>'cluster_test', destination_tablespace=>'tablespace1', index_destination_tablespace=>'tablespace2', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx');
ERROR:  "cluster_test" is not a chunk
-- cannot move a NULL chunk
SELECT move_chunk(chunk=>NULL, destination_tablespace=>'tablespace1', index_destination_tablespace=>'tablespace2', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx');
ERROR:  valid chunk, destination_tablespace, and index_destination_tablespaces are required
-- cannot move within a transaction
BEGIN;
SELECT move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>'tablespace1', index_destination_tablespace=>'tablespace2', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx');
ERROR:  move cannot run inside a transaction block
END;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER_2
-- must be hypertable owner to move a chunk
SELECT move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>'tablespace2', index_destination_tablespace=>'tablespace2', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx', verbose=>TRUE);
ERROR:  must be owner of hypertable "cluster_test"
\set ON_ERROR_STOP 1
-- grant create permissions on tablespace2 so we can use it later.
SET client_min_messages TO error;
GRANT CREATE ON TABLESPACE tablespace2 TO :ROLE_DEFAULT_PERM_USER;
RESET client_min_messages;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
-- move with chunk index for reorder
SELECT FROM move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>'tablespace1', index_destination_tablespace=>'tablespace1', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx');
--

SELECT * FROM test.show_subtables('cluster_test');
                 Child                  | Tablespace  
----------------------------------------+-------------
 _timescaledb_internal._hyper_1_1_chunk | 
 _timescaledb_internal._hyper_1_2_chunk | tablespace1

SELECT * FROM test.show_indexesp('_timescaledb_internal._hyper%_chunk');
                 Table                  |                              Index                               |  Columns   | Expr | Unique | Primary | Exclusion | Tablespace  
----------------------------------------+------------------------------------------------------------------+------------+------+--------+---------+-----------+-------------
 _timescaledb_internal._hyper_1_1_chunk | _timescaledb_internal._hyper_1_1_chunk_cluster_test_time_idx     | {time}     |      | f      | f       | f         | 
 _timescaledb_internal._hyper_1_1_chunk | _timescaledb_internal._hyper_1_1_chunk_cluster_test_location_idx | {location} |      | f      | f       | f         | 
 _timescaledb_internal._hyper_1_2_chunk | _timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx     | {time}     |      | f      | f       | f         | tablespace1
 _timescaledb_internal._hyper_1_2_chunk | _timescaledb_internal._hyper_1_2_chunk_cluster_test_location_idx | {location} |      | f      | f       | f         | tablespace1

BEGIN;
SELECT * FROM ensure_scans_work('cluster_test', should_output => 'expected', seqscan => true) \gexec

            SET LOCAL enable_seqscan=t;
            SET LOCAL enable_indexscan=f;
            SET LOCAL enable_bitmapscan=f;
            EXPLAIN (buffers off, costs off) SELECT * FROM cluster_test WHERE time=7
        
--- QUERY PLAN ---
 Seq Scan on _hyper_1_2_chunk
   Filter: ("time" = 7)


            WITH T1 as (SELECT * FROM cluster_test WHERE time=7),
                 T2 as (SELECT * FROM expected WHERE time=7)
            SELECT T1.time, T2.time, T1.temp, T2.temp, T1.location, T2.location,
                   substring(T1.value for 30), substring(T2.value for 30),
                   length(T1.value), length(T2.value)
            FROM T1 FULL OUTER JOIN T2 ON T1 = T2
            WHERE T1 IS NULL OR T2 IS NULL
        
 time | time | temp | temp | location | location | substring | substring | length | length 
------+------+------+------+----------+----------+-----------+-----------+--------+--------

SELECT * FROM ensure_scans_work('cluster_test', should_output => 'expected', indexscan => true) \gexec

            SET LOCAL enable_seqscan=f;
            SET LOCAL enable_indexscan=t;
            SET LOCAL enable_bitmapscan=f;
            EXPLAIN (buffers off, costs off) SELECT * FROM cluster_test WHERE time=7
        
--- QUERY PLAN ---
 Index Scan using _hyper_1_2_chunk_cluster_test_time_idx on _hyper_1_2_chunk
   Index Cond: ("time" = 7)


            WITH T1 as (SELECT * FROM cluster_test WHERE time=7),
                 T2 as (SELECT * FROM expected WHERE time=7)
            SELECT T1.time, T2.time, T1.temp, T2.temp, T1.location, T2.location,
                   substring(T1.value for 30), substring(T2.value for 30),
                   length(T1.value), length(T2.value)
            FROM T1 FULL OUTER JOIN T2 ON T1 = T2
            WHERE T1 IS NULL OR T2 IS NULL
        
 time | time | temp | temp | location | location | substring | substring | length | length 
------+------+------+------+----------+----------+-----------+-----------+--------+--------

SELECT * FROM ensure_scans_work('cluster_test', should_output => 'expected', bitmapscan => true) \gexec

            SET LOCAL enable_seqscan=f;
            SET LOCAL enable_indexscan=f;
            SET LOCAL enable_bitmapscan=t;
            EXPLAIN (buffers off, costs off) SELECT * FROM cluster_test WHERE time=7
        
--- QUERY PLAN ---
 Bitmap Heap Scan on _hyper_1_2_chunk
   Recheck Cond: ("time" = 7)
   ->  Bitmap Index Scan on _hyper_1_2_chunk_cluster_test_time_idx
         Index Cond: ("time" = 7)


            WITH T1 as (SELECT * FROM cluster_test WHERE time=7),
                 T2 as (SELECT * FROM expected WHERE time=7)
            SELECT T1.time, T2.time, T1.temp, T2.temp, T1.location, T2.location,
                   substring(T1.value for 30), substring(T2.value for 30),
                   length(T1.value), length(T2.value)
            FROM T1 FULL OUTER JOIN T2 ON T1 = T2
            WHERE T1 IS NULL OR T2 IS NULL
        
 time | time | temp | temp | location | location | substring | substring | length | length 
------+------+------+------+----------+----------+-----------+-----------+--------+--------

COMMIT;
SET enable_seqscan=Default;
SET enable_indexscan=Default;
SET enable_bitmapscan=Default;
-- check that move puts things in the correct order, as it also reorders
SELECT ctid, time, temp, location, substring(value for 30), length(value)
FROM _timescaledb_internal._hyper_1_1_chunk ORDER BY ctid;
 ctid  | time | temp | location | substring | length 
-------+------+------+----------+-----------+--------
 (0,1) |    0 | 18.9 |        3 | a         |      1
 (0,2) |    3 | 19.4 |        3 | c         |      1
 (0,3) |    4 | 18.3 |        2 | f         |      1
 (0,4) |    2 | 12.4 |        8 | g         |      1
 (0,5) |    1 | 23.4 |        5 | i         |      1

SELECT ctid, time, temp, location, substring(value for 30), length(value)
FROM _timescaledb_internal._hyper_1_2_chunk ORDER BY ctid;
 ctid  | time | temp | location |           substring            | length 
-------+------+------+----------+--------------------------------+--------
 (0,1) |    9 | 11.4 |        1 | h                              |      1
 (0,2) |    8 | 13.3 |        7 | b                              |      1
 (0,3) |    7 | 20.3 |        4 | xyzzyxyzzyxyzzyxyzzyxyzzyxyzzy | 500000
 (0,4) |    6 | 27.3 |        9 | d                              |      1
 (0,5) |    5 | 25.4 |        1 | e                              |      1

-- move back to pg_default
SELECT move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>'pg_default', index_destination_tablespace=>'pg_default', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx', verbose=>TRUE);
INFO:  reordering "_timescaledb_internal._hyper_1_2_chunk" using sequential scan and sort
 move_chunk 
------------
 

SELECT * FROM test.show_subtables('cluster_test');
                 Child                  | Tablespace 
----------------------------------------+------------
 _timescaledb_internal._hyper_1_1_chunk | 
 _timescaledb_internal._hyper_1_2_chunk | 

SELECT * FROM test.show_indexesp('_timescaledb_internal._hyper%_chunk');
                 Table                  |                              Index                               |  Columns   | Expr | Unique | Primary | Exclusion | Tablespace 
----------------------------------------+------------------------------------------------------------------+------------+------+--------+---------+-----------+------------
 _timescaledb_internal._hyper_1_1_chunk | _timescaledb_internal._hyper_1_1_chunk_cluster_test_time_idx     | {time}     |      | f      | f       | f         | 
 _timescaledb_internal._hyper_1_1_chunk | _timescaledb_internal._hyper_1_1_chunk_cluster_test_location_idx | {location} |      | f      | f       | f         | 
 _timescaledb_internal._hyper_1_2_chunk | _timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx     | {time}     |      | f      | f       | f         | 
 _timescaledb_internal._hyper_1_2_chunk | _timescaledb_internal._hyper_1_2_chunk_cluster_test_location_idx | {location} |      | f      | f       | f         | 

-- move chunk and indexes to different tablespaces
SELECT move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>'tablespace1', index_destination_tablespace=>'tablespace2', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx', verbose=>TRUE);
INFO:  reordering "_timescaledb_internal._hyper_1_2_chunk" using sequential scan and sort
 move_chunk 
------------
 

SELECT * FROM test.show_subtables('cluster_test');
                 Child                  | Tablespace  
----------------------------------------+-------------
 _timescaledb_internal._hyper_1_1_chunk | 
 _timescaledb_internal._hyper_1_2_chunk | tablespace1

SELECT * FROM test.show_indexesp('_timescaledb_internal._hyper%_chunk');
                 Table                  |                              Index                               |  Columns   | Expr | Unique | Primary | Exclusion | Tablespace  
----------------------------------------+------------------------------------------------------------------+------------+------+--------+---------+-----------+-------------
 _timescaledb_internal._hyper_1_1_chunk | _timescaledb_internal._hyper_1_1_chunk_cluster_test_time_idx     | {time}     |      | f      | f       | f         | 
 _timescaledb_internal._hyper_1_1_chunk | _timescaledb_internal._hyper_1_1_chunk_cluster_test_location_idx | {location} |      | f      | f       | f         | 
 _timescaledb_internal._hyper_1_2_chunk | _timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx     | {time}     |      | f      | f       | f         | tablespace2
 _timescaledb_internal._hyper_1_2_chunk | _timescaledb_internal._hyper_1_2_chunk_cluster_test_location_idx | {location} |      | f      | f       | f         | tablespace2

-- keep chunk in same space and move index tablespace
SELECT move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>'pg_default', index_destination_tablespace=>'tablespace2', reorder_index=>'_timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx', verbose=>TRUE);
INFO:  reordering "_timescaledb_internal._hyper_1_2_chunk" using sequential scan and sort
 move_chunk 
------------
 

SELECT * FROM test.show_subtables('cluster_test');
                 Child                  | Tablespace 
----------------------------------------+------------
 _timescaledb_internal._hyper_1_1_chunk | 
 _timescaledb_internal._hyper_1_2_chunk | 

SELECT * FROM test.show_indexesp('_timescaledb_internal._hyper%_chunk');
                 Table                  |                              Index                               |  Columns   | Expr | Unique | Primary | Exclusion | Tablespace  
----------------------------------------+------------------------------------------------------------------+------------+------+--------+---------+-----------+-------------
 _timescaledb_internal._hyper_1_1_chunk | _timescaledb_internal._hyper_1_1_chunk_cluster_test_time_idx     | {time}     |      | f      | f       | f         | 
 _timescaledb_internal._hyper_1_1_chunk | _timescaledb_internal._hyper_1_1_chunk_cluster_test_location_idx | {location} |      | f      | f       | f         | 
 _timescaledb_internal._hyper_1_2_chunk | _timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx     | {time}     |      | f      | f       | f         | tablespace2
 _timescaledb_internal._hyper_1_2_chunk | _timescaledb_internal._hyper_1_2_chunk_cluster_test_location_idx | {location} |      | f      | f       | f         | tablespace2

--TEST with compression bug 4000
--compress chunk and then  move chunk and index to different tablespaces
ALTER TABLE cluster_test SET (timescaledb.compress, timescaledb.compress_segmentby = 'location');
SELECT compress_chunk('_timescaledb_internal._hyper_1_2_chunk') as ch;
                   ch                   
----------------------------------------
 _timescaledb_internal._hyper_1_2_chunk

SELECT true INTO move_chunk_compressed FROM move_chunk(chunk=>'_timescaledb_internal._hyper_1_2_chunk', destination_tablespace=>'tablespace2', index_destination_tablespace=>'tablespace1', verbose=>TRUE);
SELECT * FROM test.show_subtables('cluster_test');
                 Child                  | Tablespace  
----------------------------------------+-------------
 _timescaledb_internal._hyper_1_1_chunk | 
 _timescaledb_internal._hyper_1_2_chunk | tablespace2

SELECT * FROM test.show_indexesp('_timescaledb_internal._hyper%_chunk');
                 Table                  |                              Index                               |  Columns   | Expr | Unique | Primary | Exclusion | Tablespace  
----------------------------------------+------------------------------------------------------------------+------------+------+--------+---------+-----------+-------------
 _timescaledb_internal._hyper_1_1_chunk | _timescaledb_internal._hyper_1_1_chunk_cluster_test_time_idx     | {time}     |      | f      | f       | f         | 
 _timescaledb_internal._hyper_1_1_chunk | _timescaledb_internal._hyper_1_1_chunk_cluster_test_location_idx | {location} |      | f      | f       | f         | 
 _timescaledb_internal._hyper_1_2_chunk | _timescaledb_internal._hyper_1_2_chunk_cluster_test_time_idx     | {time}     |      | f      | f       | f         | tablespace1
 _timescaledb_internal._hyper_1_2_chunk | _timescaledb_internal._hyper_1_2_chunk_cluster_test_location_idx | {location} |      | f      | f       | f         | tablespace1

SELECT * FROM test.show_indexesp('_timescaledb_internal.compress_hyper%_chunk');
                     Table                      |                                         Index                                         |                 Columns                  | Expr | Unique | Primary | Exclusion | Tablespace  
------------------------------------------------+---------------------------------------------------------------------------------------+------------------------------------------+------+--------+---------+-----------+-------------
 _timescaledb_internal.compress_hyper_2_3_chunk | _timescaledb_internal.compress_hyper_2_3_chunk_location__ts_meta_min_1__ts_meta_m_idx | {location,_ts_meta_min_1,_ts_meta_max_1} |      | f      | f       | f         | tablespace1

