--
-- Testing we handle rollbacks properly
--
CREATE TABLE t(a int, b int) USING columnar;
CREATE VIEW t_stripes AS
SELECT * FROM columnar.stripe a, pg_class b
WHERE a.storage_id = columnar.get_storage_id(b.oid) AND b.relname = 't';
BEGIN;
INSERT INTO t SELECT i, i+1 FROM generate_series(1, 10) i;
ROLLBACK;
SELECT count(*) FROM t;
 count
---------------------------------------------------------------------
     0
(1 row)

select
  version_major, version_minor, reserved_stripe_id, reserved_row_number
  from columnar_test_helpers.columnar_storage_info('t');
 version_major | version_minor | reserved_stripe_id | reserved_row_number
---------------------------------------------------------------------
             2 |             0 |                  2 |              150001
(1 row)

-- check stripe metadata also have been rolled-back
SELECT count(*) FROM t_stripes;
 count
---------------------------------------------------------------------
     0
(1 row)

INSERT INTO t SELECT i, i+1 FROM generate_series(1, 10) i;
SELECT count(*) FROM t;
 count
---------------------------------------------------------------------
    10
(1 row)

SELECT count(*) FROM t_stripes;
 count
---------------------------------------------------------------------
     1
(1 row)

-- savepoint rollback
BEGIN;
SAVEPOINT s0;
INSERT INTO t SELECT i, i+1 FROM generate_series(1, 10) i;
SELECT count(*) FROM t;  -- force flush
 count
---------------------------------------------------------------------
    20
(1 row)

SAVEPOINT s1;
INSERT INTO t SELECT i, i+1 FROM generate_series(1, 10) i;
select
  version_major, version_minor, reserved_stripe_id, reserved_row_number
  from columnar_test_helpers.columnar_storage_info('t');
 version_major | version_minor | reserved_stripe_id | reserved_row_number
---------------------------------------------------------------------
             2 |             0 |                  5 |              600001
(1 row)

SELECT count(*) FROM t;
 count
---------------------------------------------------------------------
    30
(1 row)

ROLLBACK TO SAVEPOINT s1;
SELECT count(*) FROM t;
 count
---------------------------------------------------------------------
    20
(1 row)

ROLLBACK TO SAVEPOINT s0;
SELECT count(*) FROM t;
 count
---------------------------------------------------------------------
    10
(1 row)

INSERT INTO t SELECT i, i+1 FROM generate_series(1, 10) i;
COMMIT;
select
  version_major, version_minor, reserved_stripe_id, reserved_row_number
  from columnar_test_helpers.columnar_storage_info('t');
 version_major | version_minor | reserved_stripe_id | reserved_row_number
---------------------------------------------------------------------
             2 |             0 |                  6 |              750001
(1 row)

SELECT count(*) FROM t;
 count
---------------------------------------------------------------------
    20
(1 row)

SELECT count(*) FROM t_stripes;
 count
---------------------------------------------------------------------
     2
(1 row)

DROP TABLE t;
DROP VIEW t_stripes;
