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->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 $psql = $node->background_psql('postgres', on_error_stop => 0);

$psql->query_safe(q[begin]);
for my $i (1 .. 1000)
{
    $psql->query_safe(q[savepoint a]);
    $psql->query_safe(q[insert into test select i, md5(i::text) from generate_series(1, 1000) i]);
    $psql->query_safe(q[release savepoint a]);
}

$psql->query_safe(q[commit]);
$psql->quit;

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

$node->stop;

done_testing();

