use strict;
use warnings;
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;

my $node = PostgreSQL::Test::Cluster->new('test');
$node->init(allows_streaming => 'logical');
$node->append_conf('postgresql.conf', 'logical_decoding_work_mem = 1MB');
$node->start;

$node->safe_psql('postgres', 'create table test (a int, b text)');
$node->safe_psql('postgres',
		 q[select pg_create_logical_replication_slot('s', 'test_decoding')]);
my $psql1 = $node->background_psql('postgres', on_error_stop => 0);
my $psql2 = $node->background_psql('postgres', on_error_stop => 0);

$psql2->query_safe(q[begin]);
for my $i (1 .. 500)
{
    $psql1->query_safe(q[
begin;
insert into test select c, repeat(md5(c::text), 10) from generate_series(1, 20950) c;
commit;
]);

    $psql2->query_safe(q[insert into test select c, repeat(md5(c::text), 10) from generate_series(1, 150) c;]);
}

$psql2->query_safe(q[commit]);

$psql1->quit;
$psql2->quit;

$node->safe_psql('postgres',
		 q[select count(*) from pg_logical_slot_peek_changes('s', null, null)]);

$node->stop;

is(1, 1, "test is finished");

done_testing();

