1 |
#!/usr/bin/perl |
2 |
# SlaveInit |
3 |
# Vadim Mikheev, (c) 2000, PostgreSQL Inc. |
4 |
|
5 |
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' |
6 |
& eval 'exec perl -S $0 $argv:q' |
7 |
if 0; |
8 |
|
9 |
use Pg; |
10 |
use Getopt::Long; |
11 |
|
12 |
$| = 1; |
13 |
|
14 |
$result = GetOptions("debug!", "verbose!", "quiet!", "help", |
15 |
"host=s", "user=s", "password=s"); |
16 |
|
17 |
my $debug = $opt_debug || 0; |
18 |
my $verbose = $opt_verbose || 0; |
19 |
my $quiet = $opt_quiet || 0; |
20 |
|
21 |
if (defined($opt_help) || (scalar(@ARGV) < 1)) { |
22 |
print "Usage: $0 --host=name --user=name --password=string slavedb\n"; |
23 |
exit ((scalar(@ARGV) < 1)? 1:0); |
24 |
} |
25 |
|
26 |
my $slave = $ARGV[0] || "slave"; |
27 |
|
28 |
my $sinfo = "dbname=$slave"; |
29 |
$sinfo = "$sinfo host=$opt_host" if (defined($opt_host)); |
30 |
$sinfo = "$sinfo user=$opt_user" if (defined($opt_user)); |
31 |
$sinfo = "$sinfo password=$opt_password" if (defined($opt_password)); |
32 |
|
33 |
sub RollbackAndQuit { |
34 |
my $conn = shift @_; |
35 |
|
36 |
print STDERR $conn->errorMessage; |
37 |
$conn->exec("ROLLBACK"); |
38 |
exit (-1); |
39 |
} |
40 |
|
41 |
print("Connecting to $sinfo\n") if ($debug || $verbose); |
42 |
my $conn = Pg::connectdb($sinfo); |
43 |
if ($conn->status != PGRES_CONNECTION_OK) { |
44 |
print STDERR "Failed opening $sinfo\n"; |
45 |
exit 1; |
46 |
} |
47 |
|
48 |
my $result = $conn->exec("BEGIN"); |
49 |
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK); |
50 |
|
51 |
$result = $conn->exec("set transaction isolation level serializable"); |
52 |
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK); |
53 |
|
54 |
$result = $conn->exec("create table _RSERV_SLAVE_TABLES_" . |
55 |
" (tname name, cname name, reloid oid, key int4)"); |
56 |
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK); |
57 |
|
58 |
$result = $conn->exec("create table _RSERV_SLAVE_SYNC_" . |
59 |
" (syncid int4, synctime timestamp)"); |
60 |
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK); |
61 |
|
62 |
$result = $conn->exec("COMMIT"); |
63 |
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK); |
64 |
|
65 |
exit (0); |