drop table if exists xlogpos;
create temp table xlogpos (id integer primary key, pos text);
insert into xlogpos values (1, pg_current_xlog_insert_location());

drop table if exists fastcopy1;
create table fastcopy1
(col1 integer
,col2 text);

checkpoint;
select pg_current_xlog_insert_location();
update xlogpos set pos = pg_current_xlog_insert_location() where id = 1;
\echo slow COPY
copy fastcopy1
from '/tmp/fastcopy1' binary;
select wasWALwritten(true);
select pg_current_xlog_insert_location();

\echo slow vacuum
vacuum fastcopy1;
drop table if exists fastcopy1;

checkpoint;
begin;
create table fastcopy1
(col1 integer
,col2 text);
select pg_current_xlog_insert_location();
update xlogpos set pos = pg_current_xlog_insert_location() where id = 1;
\echo fast COPY
copy fastcopy1
from '/tmp/fastcopy1' binary;
select wasWALwritten(false) as waswalavoided;
select pg_current_xlog_insert_location();
commit;

checkpoint;
begin;
truncate fastcopy1;
select pg_current_xlog_insert_location();
update xlogpos set pos = pg_current_xlog_insert_location() where id = 1;
\echo fast COPY
copy fastcopy1
from '/tmp/fastcopy1' binary;
select wasWALwritten(false) as waswalavoided;
select pg_current_xlog_insert_location();
commit;
select count(*) from fastcopy1;

checkpoint;
begin;
truncate fastcopy1;
savepoint sp;
select pg_current_xlog_insert_location();
update xlogpos set pos = pg_current_xlog_insert_location() where id = 1;
\echo fast COPY
copy fastcopy1
from '/tmp/fastcopy1' binary;
select wasWALwritten(false) as waswalavoided;
select pg_current_xlog_insert_location();
rollback to sp;
commit;
select count(*) from fastcopy1;

drop table if exists fastcopy2;
create table fastcopy2
(col1 integer);

checkpoint;
select pg_current_xlog_insert_location();
update xlogpos set pos = pg_current_xlog_insert_location() where id = 1;
\echo slow COPY
copy fastcopy2
from '/tmp/fastcopy2' binary;
select wasWALwritten(true);
select pg_current_xlog_insert_location();

\echo slow vacuum
vacuum fastcopy2;
drop table if exists fastcopy2;

checkpoint;
begin;
create table fastcopy2
(col1 integer);
select pg_current_xlog_insert_location();
update xlogpos set pos = pg_current_xlog_insert_location() where id = 1;
\echo fast COPY
copy fastcopy2
from '/tmp/fastcopy2' binary;
select wasWALwritten(false) as waswalavoided;
select pg_current_xlog_insert_location();
commit;

checkpoint;
begin;
truncate fastcopy2;
select pg_current_xlog_insert_location();
update xlogpos set pos = pg_current_xlog_insert_location() where id = 1;
\echo fast COPY
copy fastcopy2
from '/tmp/fastcopy2' binary;
select wasWALwritten(false) as waswalavoided;
select pg_current_xlog_insert_location();
commit;
select count(*) from fastcopy2;

checkpoint;
begin;
truncate fastcopy2;
savepoint sp;
select pg_current_xlog_insert_location();
update xlogpos set pos = pg_current_xlog_insert_location() where id = 1;
\echo fast COPY
copy fastcopy2
from '/tmp/fastcopy2' binary;
select wasWALwritten(false) as waswalavoided;
select pg_current_xlog_insert_location();
rollback to sp;
commit;
select count(*) from fastcopy2;
