-- 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 EXPLAIN 'EXPLAIN (buffers off, costs off,timing off,summary off)'
\set EXPLAIN_ANALYZE 'EXPLAIN (analyze,buffers off, costs off,timing off,summary off)'
CREATE TABLE decompress_tracking(time timestamptz not null, device text, value float, primary key(time, device));
SELECT table_name FROM create_hypertable('decompress_tracking','time');
     table_name      
---------------------
 decompress_tracking

ALTER TABLE decompress_tracking SET (timescaledb.compress, timescaledb.compress_segmentby='device');
INSERT INTO decompress_tracking SELECT '2020-01-01'::timestamptz + format('%s hour', g)::interval, 'd1', random() FROM generate_series(1,10) g;
INSERT INTO decompress_tracking SELECT '2020-01-01'::timestamptz + format('%s hour', g)::interval, 'd2', random() FROM generate_series(1,20) g;
INSERT INTO decompress_tracking SELECT '2020-01-01'::timestamptz + format('%s hour', g)::interval, 'd3', random() FROM generate_series(1,30) g;
SELECT count(compress_chunk(ch)) FROM show_chunks('decompress_tracking') ch;
 count 
-------
     2

ANALYZE decompress_tracking;
-- no tracking without analyze
:EXPLAIN UPDATE decompress_tracking SET value = value + 3;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable)
   ->  Update on decompress_tracking
         Update on _hyper_X_X_chunk decompress_tracking_1
         Update on _hyper_X_X_chunk decompress_tracking_2
         ->  Result
               ->  Append
                     ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_1
                     ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_2

BEGIN; :EXPLAIN_ANALYZE UPDATE decompress_tracking SET value = value + 3; ROLLBACK;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable) (actual rows=0.00 loops=1)
   Batches decompressed: 5
   Tuples decompressed: 60
   ->  Update on decompress_tracking (actual rows=0.00 loops=1)
         Update on _hyper_X_X_chunk decompress_tracking_1
         Update on _hyper_X_X_chunk decompress_tracking_2
         ->  Result (actual rows=60.00 loops=1)
               ->  Append (actual rows=60.00 loops=1)
                     ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_1 (actual rows=40.00 loops=1)
                     ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_2 (actual rows=20.00 loops=1)

BEGIN; :EXPLAIN_ANALYZE UPDATE decompress_tracking SET value = value + 3 WHERE device = 'd2'; ROLLBACK;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable) (actual rows=0.00 loops=1)
   Batches decompressed: 2
   Tuples decompressed: 20
   ->  Update on decompress_tracking (actual rows=0.00 loops=1)
         Update on _hyper_X_X_chunk decompress_tracking_1
         Update on _hyper_X_X_chunk decompress_tracking_2
         ->  Result (actual rows=20.00 loops=1)
               ->  Append (actual rows=20.00 loops=1)
                     ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_1 (actual rows=15.00 loops=1)
                           Filter: (device = 'd2'::text)
                     ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_2 (actual rows=5.00 loops=1)
                           Filter: (device = 'd2'::text)

SET timescaledb.enable_compressed_direct_batch_delete TO false;
BEGIN; :EXPLAIN_ANALYZE DELETE FROM decompress_tracking; ROLLBACK;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable) (actual rows=0.00 loops=1)
   Batches decompressed: 5
   Tuples decompressed: 60
   ->  Delete on decompress_tracking (actual rows=0.00 loops=1)
         Delete on _hyper_X_X_chunk decompress_tracking_1
         Delete on _hyper_X_X_chunk decompress_tracking_2
         ->  Append (actual rows=60.00 loops=1)
               ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_1 (actual rows=40.00 loops=1)
               ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_2 (actual rows=20.00 loops=1)

BEGIN; :EXPLAIN_ANALYZE DELETE FROM decompress_tracking WHERE device = 'd3'; ROLLBACK;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable) (actual rows=0.00 loops=1)
   Batches decompressed: 2
   Tuples decompressed: 30
   ->  Delete on decompress_tracking (actual rows=0.00 loops=1)
         Delete on _hyper_X_X_chunk decompress_tracking_1
         Delete on _hyper_X_X_chunk decompress_tracking_2
         ->  Append (actual rows=30.00 loops=1)
               ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_1 (actual rows=15.00 loops=1)
                     Filter: (device = 'd3'::text)
               ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_2 (actual rows=15.00 loops=1)
                     Filter: (device = 'd3'::text)

RESET timescaledb.enable_compressed_direct_batch_delete;
BEGIN; :EXPLAIN_ANALYZE DELETE FROM decompress_tracking; ROLLBACK;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable) (actual rows=0.00 loops=1)
   Batches deleted: 5
   ->  Delete on decompress_tracking (actual rows=0.00 loops=1)
         Delete on _hyper_X_X_chunk decompress_tracking_1
         Delete on _hyper_X_X_chunk decompress_tracking_2
         ->  Append (actual rows=0.00 loops=1)
               ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_1 (actual rows=0.00 loops=1)
               ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_2 (actual rows=0.00 loops=1)

BEGIN; :EXPLAIN_ANALYZE DELETE FROM decompress_tracking WHERE device = 'd3'; ROLLBACK;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable) (actual rows=0.00 loops=1)
   Batches deleted: 2
   ->  Delete on decompress_tracking (actual rows=0.00 loops=1)
         Delete on _hyper_X_X_chunk decompress_tracking_1
         Delete on _hyper_X_X_chunk decompress_tracking_2
         ->  Append (actual rows=0.00 loops=1)
               ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_1 (actual rows=0.00 loops=1)
                     Filter: (device = 'd3'::text)
               ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_2 (actual rows=0.00 loops=1)
                     Filter: (device = 'd3'::text)

BEGIN; :EXPLAIN_ANALYZE INSERT INTO decompress_tracking SELECT '2020-01-01 1:30','d1',random(); ROLLBACK;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable) (actual rows=0.00 loops=1)
   Batches filtered: 1
   ->  Insert on decompress_tracking (actual rows=0.00 loops=1)
         ->  Subquery Scan on "*SELECT*" (actual rows=1.00 loops=1)
               ->  Result (actual rows=1.00 loops=1)

BEGIN; :EXPLAIN_ANALYZE INSERT INTO decompress_tracking SELECT '2020-01-01','d2',random(); ROLLBACK;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable) (actual rows=0.00 loops=1)
   ->  Insert on decompress_tracking (actual rows=0.00 loops=1)
         ->  Subquery Scan on "*SELECT*" (actual rows=1.00 loops=1)
               ->  Result (actual rows=1.00 loops=1)

BEGIN; :EXPLAIN_ANALYZE INSERT INTO decompress_tracking SELECT '2020-01-01','d4',random(); ROLLBACK;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable) (actual rows=0.00 loops=1)
   ->  Insert on decompress_tracking (actual rows=0.00 loops=1)
         ->  Subquery Scan on "*SELECT*" (actual rows=1.00 loops=1)
               ->  Result (actual rows=1.00 loops=1)

BEGIN; :EXPLAIN_ANALYZE INSERT INTO decompress_tracking (VALUES ('2020-01-01 1:30','d1',random()),('2020-01-01 1:30','d2',random())); ROLLBACK;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable) (actual rows=0.00 loops=1)
   Batches filtered: 2
   ->  Insert on decompress_tracking (actual rows=0.00 loops=1)
         ->  Values Scan on "*VALUES*" (actual rows=2.00 loops=1)

-- test prepared statements EXPLAIN still works after execution
SET plan_cache_mode TO force_generic_plan;
PREPARE p1 AS UPDATE decompress_tracking SET value = value + 3 WHERE device = 'd1';
BEGIN;
    EXPLAIN (BUFFERS OFF, COSTS OFF) EXECUTE p1;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable)
   ->  Update on decompress_tracking
         Update on _hyper_X_X_chunk decompress_tracking_1
         Update on _hyper_X_X_chunk decompress_tracking_2
         ->  Result
               ->  Append
                     ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_1
                           Filter: (device = 'd1'::text)
                     ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_2
                           Filter: (device = 'd1'::text)

    EXECUTE p1;
    EXPLAIN (BUFFERS OFF, COSTS OFF) EXECUTE p1;
--- QUERY PLAN ---
 Custom Scan (ModifyHypertable)
   ->  Update on decompress_tracking
         Update on _hyper_X_X_chunk decompress_tracking_1
         Update on _hyper_X_X_chunk decompress_tracking_2
         ->  Result
               ->  Append
                     ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_1
                           Filter: (device = 'd1'::text)
                     ->  Seq Scan on _hyper_X_X_chunk decompress_tracking_2
                           Filter: (device = 'd1'::text)

ROLLBACK;
DROP TABLE decompress_tracking;
