# Test for crash recovery as a standby and promotion with prepared transactions
use strict;
use warnings;
use PostgresNode;
use TestLib;
use Test::More tests => 2;

my $node = get_new_node('primary');
$node->init(has_archiving => 1, allows_streaming => 1, hot_standby => 'on');
$node->append_conf(
	'postgresql.conf', qq{
max_prepared_transactions = 10});
$node->start;

# Workload with a prepared transaction
$node->psql(
	'postgres', qq{
CREATE TABLE foo(i int);
BEGIN;
INSERT INTO foo VALUES(1);
PREPARE TRANSACTION 'fooinsert';
});

# crash node, restart as standby
$node->stop('immediate');
$node->append_conf(
	'recovery.conf', qq{
standby_mode = on});
$node->start;

$node->promote;

my $logfile = slurp_file($node->logfile());
ok( $logfile =~
	  qr/recovering prepared transaction .* from shared memory/,
	'want to see that a prepared transaction was recovered');

# # verify that recovery and promotion finished and that the prepared transaction still exists.
my $psql_rc = $node->psql('postgres', "COMMIT PREPARED 'fooinsert'");
is($psql_rc, '0', 'Commit prepared transaction after crash recovery');
