-- cleanup from failed prior invocation
DROP TABLE foo;
ROLLBACK PREPARED 'f';

SELECT txid_current();

\set ntuples 10000000

CREATE UNLOGGED TABLE foo AS SELECT generate_series(1, :ntuples) i;

VACUUM foo;
SELECT n_live_tup, n_dead_tup, reltuples::int8 FROM pg_stat_user_tables pst JOIN pg_class pc ON (pc.oid = pst.relid) WHERE relid = 'foo'::regclass;

-- prevent further cleanup
BEGIN;
SELECT txid_current();
PREPARE TRANSACTION 'f';

DELETE FROM foo WHERE i > (:ntuples * 0.5);
SELECT txid_current();

-- show sane stats
VACUUM foo;
SELECT n_live_tup, n_dead_tup, reltuples::int8 FROM pg_stat_user_tables pst JOIN pg_class pc ON (pc.oid = pst.relid) WHERE relid = 'foo'::regclass;

-- show increasingly less sane stats
VACUUM foo;
SELECT n_live_tup, n_dead_tup, reltuples::int8 FROM pg_stat_user_tables pst JOIN pg_class pc ON (pc.oid = pst.relid) WHERE relid = 'foo'::regclass;

VACUUM foo;
SELECT n_live_tup, n_dead_tup, reltuples::int8 FROM pg_stat_user_tables pst JOIN pg_class pc ON (pc.oid = pst.relid) WHERE relid = 'foo'::regclass;

VACUUM foo;
SELECT n_live_tup, n_dead_tup, reltuples::int8 FROM pg_stat_user_tables pst JOIN pg_class pc ON (pc.oid = pst.relid) WHERE relid = 'foo'::regclass;

VACUUM foo;
SELECT n_live_tup, n_dead_tup, reltuples::int8 FROM pg_stat_user_tables pst JOIN pg_class pc ON (pc.oid = pst.relid) WHERE relid = 'foo'::regclass;

VACUUM foo;
VACUUM foo;
VACUUM foo;
VACUUM foo;
VACUUM foo;
SELECT n_live_tup, n_dead_tup, reltuples::int8 FROM pg_stat_user_tables pst JOIN pg_class pc ON (pc.oid = pst.relid) WHERE relid = 'foo'::regclass;


-- cleanup
DROP TABLE foo;
ROLLBACK PREPARED 'f';
