1#
2# Tests relating to PostgreSQL crash recovery and redo
3#
4use strict;
5use warnings;
6use PostgresNode;
7use TestLib;
8use Test::More;
9use Config;
10if ($Config{osname} eq 'MSWin32')
11{
12
13   # some Windows Perls at least don't like IPC::Run's start/kill_kill regime.
14	plan skip_all => "Test fails on Windows perl";
15}
16else
17{
18	plan tests => 3;
19}
20
21my $node = get_new_node('master');
22$node->init(allows_streaming => 1);
23$node->start;
24
25my ($stdin, $stdout, $stderr) = ('', '', '');
26
27# Ensure that txid_status reports 'aborted' for xacts
28# that were in-progress during crash. To do that, we need
29# an xact to be in-progress when we crash and we need to know
30# its xid.
31my $tx = IPC::Run::start(
32	[   'psql', '-X', '-qAt', '-v', 'ON_ERROR_STOP=1', '-f', '-', '-d',
33		$node->connstr('postgres') ],
34	'<',
35	\$stdin,
36	'>',
37	\$stdout,
38	'2>',
39	\$stderr);
40$stdin .= q[
41BEGIN;
42CREATE TABLE mine(x integer);
43SELECT txid_current();
44];
45$tx->pump until $stdout =~ /[[:digit:]]+[\r\n]$/;
46
47# Status should be in-progress
48my $xid = $stdout;
49chomp($xid);
50
51is($node->safe_psql('postgres', qq[SELECT txid_status('$xid');]),
52	'in progress', 'own xid is in-progres');
53
54# Crash and restart the postmaster
55$node->stop('immediate');
56$node->start;
57
58# Make sure we really got a new xid
59cmp_ok($node->safe_psql('postgres', 'SELECT txid_current()'),
60	'>', $xid, 'new xid after restart is greater');
61
62# and make sure we show the in-progress xact as aborted
63is($node->safe_psql('postgres', qq[SELECT txid_status('$xid');]),
64	'aborted', 'xid is aborted after crash');
65
66$tx->kill_kill;
67