create or replace function random_bytea(p_len int) returns bytea as $$
declare
  r bytea;
begin

  select ('\x' || string_agg(md5(random()::text),''))::bytea into r
    from generate_series(1, 2 * p_len / 32) s(i);

  return r;

end;
$$ language plpgsql;

drop table test_random_bytea_1k;
drop table test_random_bytea_4k;
drop table test_random_bytea_1m;

create unlogged table test_random_bytea_1k (v bytea);
create unlogged table test_random_bytea_4k (v bytea);
create unlogged table test_random_bytea_1m (v bytea);

insert into test_random_bytea_1k select random_bytea(1024) from generate_series(1,1024*1024) s(i);
insert into test_random_bytea_4k select random_bytea(4096) from generate_series(1,1024*1024/4) s(i);
insert into test_random_bytea_1m select random_bytea(1024*1024) from generate_series(1,1024) s(i);

copy test_random_bytea_1k to '/mnt/raid/compress/test_random_bytea_1k.data';
copy test_random_bytea_4k to '/mnt/raid/compress/test_random_bytea_4k.data';
copy test_random_bytea_1m to '/mnt/raid/compress/test_random_bytea_1m.data';

create or replace function redundant_bytea(p_len int) returns bytea as $$
declare
  r bytea;
begin

  select ('\x' || repeat(md5(random()::text), 2 * p_len / 32))::bytea into r;

  return r;

end;
$$ language plpgsql;

drop table test_redundant_bytea_1k;
drop table test_redundant_bytea_4k;
drop table test_redundant_bytea_1m;

create unlogged table test_redundant_bytea_1k (v bytea);
create unlogged table test_redundant_bytea_4k (v bytea);
create unlogged table test_redundant_bytea_1m (v bytea);

insert into test_redundant_bytea_1k select redundant_bytea(1024) from generate_series(1,1024*1024) s(i);
insert into test_redundant_bytea_4k select redundant_bytea(4096) from generate_series(1,1024*1024/4) s(i);
insert into test_redundant_bytea_1m select redundant_bytea(1024*1024) from generate_series(1,1024) s(i);

copy test_redundant_bytea_1k to '/mnt/raid/compress/test_redundant_bytea_1k.data';
copy test_redundant_bytea_4k to '/mnt/raid/compress/test_redundant_bytea_4k.data';
copy test_redundant_bytea_1m to '/mnt/raid/compress/test_redundant_bytea_1m.data';
