Debian Codegraph @

src/bin/pg_ctl/t/003_promote.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 use strict;
5 use warnings FATAL => 'all';
6
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9 use Test::More;
10
11 my $tempdir = PostgreSQL::Test::Utils::tempdir;
12
13 command_fails_like(
14 [ 'pg_ctl', '--pgdata' => "$tempdir/nonexistent", 'promote' ],
15 qr/directory .* does not exist/,
16 'pg_ctl promote with nonexistent directory');
17
18 my $node_primary = PostgreSQL::Test::Cluster->new('primary');
19 $node_primary->init(allows_streaming => 1);
20
21 command_fails_like(
22 [ 'pg_ctl', '--pgdata' => $node_primary->data_dir, 'promote' ],
23 qr/PID file .* does not exist/,
24 'pg_ctl promote of not running instance fails');
25
26 $node_primary->start;
27
28 command_fails_like(
29 [ 'pg_ctl', '--pgdata' => $node_primary->data_dir, 'promote' ],
30 qr/not in standby mode/,
31 'pg_ctl promote of primary instance fails');
32
33 my $node_standby = PostgreSQL::Test::Cluster->new('standby');
34 $node_primary->backup('my_backup');
35 $node_standby->init_from_backup($node_primary, 'my_backup',
36 has_streaming => 1);
37 $node_standby->start;
38
39 is($node_standby->safe_psql('postgres', 'SELECT pg_is_in_recovery()'),
40 't', 'standby is in recovery');
41
42 command_ok(
43 [
44 'pg_ctl',
45 '--pgdata' => $node_standby->data_dir,
46 '--no-wait', 'promote'
47 ],
48 'pg_ctl --no-wait promote of standby runs');
49
50 ok( $node_standby->poll_query_until(
51 'postgres', 'SELECT NOT pg_is_in_recovery()'),
52 'promoted standby is not in recovery');
53
54 # same again with default wait option
55 $node_standby = PostgreSQL::Test::Cluster->new('standby2');
56 $node_standby->init_from_backup($node_primary, 'my_backup',
57 has_streaming => 1);
58 $node_standby->start;
59
60 is($node_standby->safe_psql('postgres', 'SELECT pg_is_in_recovery()'),
61 't', 'standby is in recovery');
62
63 command_ok([ 'pg_ctl', '--pgdata' => $node_standby->data_dir, 'promote' ],
64 'pg_ctl promote of standby runs');
65
66 # no wait here
67
68 is($node_standby->safe_psql('postgres', 'SELECT pg_is_in_recovery()'),
69 'f', 'promoted standby is not in recovery');
70
71 done_testing();