1 |
dpavlin |
1.1 |
#!/usr/bin/perl -w |
2 |
|
|
# MasterInit |
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 |
|
|
BEGIN { |
10 |
|
|
my $basedir = $0; $basedir =~ s#/[^/]+$##; |
11 |
|
|
unshift(@INC, "$basedir/../share"); |
12 |
|
|
} |
13 |
|
|
|
14 |
|
|
use strict; |
15 |
|
|
use Pg; |
16 |
|
|
use Getopt::Long; |
17 |
|
|
use RServ; |
18 |
|
|
|
19 |
|
|
my $basedir = $0; $basedir =~ s#/[^/]+$#/../#; |
20 |
|
|
if ($basedir =~ m#^\.#) { |
21 |
|
|
my $pwd = `pwd`; |
22 |
|
|
chomp($pwd); |
23 |
|
|
$basedir = "$pwd/$basedir/"; |
24 |
|
|
} |
25 |
|
|
while ($basedir =~ s#/[^/.]+/\.\./#/#g) {}; |
26 |
|
|
while ($basedir =~ s#/\./#/#g) {}; |
27 |
|
|
$basedir =~ s#//#/#g; |
28 |
|
|
|
29 |
|
|
$| = 1; |
30 |
|
|
|
31 |
|
|
my ($debug,$verbose,$quiet) = (0,0,0); |
32 |
|
|
my ($help,$masterhost,$masterport,$masteruser,$masterpassword, |
33 |
|
|
$slavehost,$slaveport,$slaveuser,$slavepassword); |
34 |
|
|
my $lib; |
35 |
|
|
|
36 |
|
|
my $result = GetOptions( |
37 |
|
|
"debug!" => \$debug, "verbose!" => \$verbose, |
38 |
|
|
"quiet!" => \$quiet, "help" => \$help, |
39 |
|
|
"masterhost=s" => \$masterhost, "masterport=i" => \$masterport, |
40 |
|
|
"masteruser=s" => \$masteruser, "masterpassword=s" => \$masterpassword, |
41 |
|
|
"slavehost=s" => \$slavehost, "slaveport=i" => \$slaveport, |
42 |
|
|
"slaveuser=s" => \$slaveuser, "slavepassword=s" => \$slavepassword, |
43 |
|
|
"lib=s" => \$lib, |
44 |
|
|
); |
45 |
|
|
|
46 |
|
|
if (defined($help) || (scalar(@ARGV) < 2)) { |
47 |
|
|
print "Usage: $0 [options] masterdb slavedb |
48 |
|
|
Options: |
49 |
|
|
--masterhost=hostname --masterport=port |
50 |
|
|
--masteruser=username --masterpassword=string |
51 |
|
|
--slavehost=hostname --slaveport=port |
52 |
|
|
--slaveuser=username --slavepassword=string |
53 |
|
|
--lib=libpath |
54 |
|
|
"; |
55 |
|
|
exit ((scalar(@ARGV) < 2)? 1:0); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
my $master = $ARGV[0] || "master"; |
59 |
|
|
my $slave = $ARGV[1] || "slave"; |
60 |
|
|
|
61 |
dpavlin |
1.2 |
my $minfo = MkInfo($master,$masterhost,$masterport,$masteruser,$masterpassword); |
62 |
|
|
my $sinfo = MkInfo($slave,$slavehost,$slaveport,$slaveuser,$slavepassword); |
63 |
dpavlin |
1.1 |
|
64 |
|
|
$masterhost = "localhost" if (! $masterhost); |
65 |
|
|
$slavehost = "localhost" if (! $slavehost); |
66 |
|
|
|
67 |
|
|
$RServ::debug = $debug; |
68 |
|
|
$RServ::verbose = $verbose; |
69 |
|
|
$RServ::quiet = $quiet; |
70 |
|
|
|
71 |
|
|
if (!defined($lib) || !-e $lib) { |
72 |
|
|
$lib = "$basedir/lib/"; |
73 |
|
|
|
74 |
|
|
if (-e "$lib/rserv.so") { |
75 |
|
|
$lib .= "rserv.so"; |
76 |
|
|
} else { |
77 |
|
|
print STDERR "Can't find compiled rserv.so in $lib. Go there and type make.\n"; |
78 |
|
|
exit 1; |
79 |
|
|
} |
80 |
|
|
} |
81 |
|
|
print "Using lib '$lib'\n" if ($verbose); |
82 |
|
|
|
83 |
|
|
my $mconn = Connect($minfo); |
84 |
|
|
my $sconn = Connect($sinfo); |
85 |
|
|
|
86 |
|
|
Exec2($mconn,$sconn,"BEGIN"); |
87 |
|
|
|
88 |
|
|
Exec2($mconn,$sconn,"set transaction isolation level serializable"); |
89 |
|
|
|
90 |
|
|
# List of slave servers |
91 |
|
|
Exec2($mconn,$sconn, "create table _RSERV_SERVERS_" . |
92 |
|
|
" (server serial primary key, host text not ". |
93 |
|
|
"null, port int4 default 5432, dbase text not ". |
94 |
|
|
"null, unique(host,port,dbase))"); |
95 |
|
|
|
96 |
|
|
# insert master server in list |
97 |
|
|
Exec2($mconn,$sconn,"insert into _RSERV_SERVERS_" . |
98 |
|
|
" (server, host, port, dbase) ". |
99 |
|
|
" values (0,'$masterhost',". |
100 |
|
|
($masterport || 5432).",". |
101 |
|
|
"'$master')"); |
102 |
|
|
|
103 |
|
|
# List of replicated tables |
104 |
|
|
Exec2($mconn,$sconn,"create table _RSERV_TABLES_" . |
105 |
|
|
" (tname name not null, cname name not null, ". |
106 |
|
|
"reloid oid primary key, key int4 not null)"); |
107 |
|
|
|
108 |
|
|
## should always call MasterDelTable |
109 |
|
|
#Exec($mconn,"CREATE RULE _rserv_deltrig_ AS ON delete to _RSERV_TABLES_ DO (DELETE FROM pg_trigger WHERE tgname='_rserv_trigger_t_' AND tgrelid=(SELECT oid FROM pg_class WHERE relname=old.tname);UPDATE pg_class SET reltriggers=reltriggers-1 WHERE relname=old.tname)"); |
110 |
|
|
|
111 |
|
|
# Bookkeeping log for row replication |
112 |
|
|
Exec2($mconn,$sconn,"create table _RSERV_LOG_" . |
113 |
|
|
" (reloid oid REFERENCES _RSERV_TABLES_(reloid) ON ". |
114 |
|
|
"DELETE CASCADE ON UPDATE CASCADE, logid int4 not ". |
115 |
|
|
"null, logtime timestamp not null, insert smallint, ". |
116 |
|
|
"update smallint, delete smallint, key text, ". |
117 |
|
|
"server int4, ". |
118 |
|
|
"CONSTRAINT only_one CHECK (insert+update+delete=1))"); |
119 |
|
|
|
120 |
|
|
# This is to speedup lookup of deleted tuples |
121 |
|
|
Exec2($mconn,$sconn,"create index _RSERV_LOG_INDX_DLT_ID_ on _RSERV_LOG_ ". |
122 |
|
|
"(delete, logid) WHERE delete = 1"); |
123 |
|
|
|
124 |
|
|
# This is to speedup lookup of updated tuples |
125 |
|
|
Exec2($mconn,$sconn,"create index _RSERV_LOG_INDX_UPD_ID_ on _RSERV_LOG_ ". |
126 |
|
|
"(update, logid) WHERE update = 1"); |
127 |
|
|
|
128 |
|
|
# This is to speedup lookup of insert tuples |
129 |
|
|
Exec2($mconn,$sconn,"create index _RSERV_LOG_INDX_INS_ID_ on _RSERV_LOG_ ". |
130 |
|
|
"(insert, logid) WHERE insert = 1"); |
131 |
|
|
|
132 |
|
|
# This is to speedup cleanup |
133 |
|
|
Exec2($mconn,$sconn,"create index _RSERV_LOG_INDX_TM_ID_ on _RSERV_LOG_ ". |
134 |
|
|
"(logtime, logid)"); |
135 |
|
|
|
136 |
|
|
# This is to speedup trigger |
137 |
|
|
Exec2($mconn,$sconn,"create index _RSERV_LOG_INDX_REL_KEY_ on _RSERV_LOG_ ". |
138 |
|
|
"(reloid, key)"); |
139 |
|
|
|
140 |
|
|
# View to help managing _rserv_log_ table |
141 |
|
|
Exec2($mconn,$sconn,"CREATE VIEW _RSERV_HUMAN_LOG_ AS SELECT log.logid, ". |
142 |
|
|
"tab.tname AS table_name, tab.cname AS column_name, ". |
143 |
|
|
"log.key AS column_value, log.insert, log.update, ". |
144 |
|
|
"log.delete, log.logtime FROM _RSERV_LOG_ log, ". |
145 |
|
|
"_RSERV_TABLES_ tab WHERE tab.reloid ". |
146 |
|
|
"= log.reloid ORDER BY log.logtime"); |
147 |
|
|
|
148 |
|
|
# View to help logging daily transactions |
149 |
|
|
Exec2($mconn,$sconn,"CREATE VIEW _RSERV_DAILY_LOG_ AS ". |
150 |
|
|
"SELECT count(*) AS \"# records\", ". |
151 |
|
|
"to_char(_rserv_log_.logtime, 'YYYY-MM-DD') ". |
152 |
|
|
"AS day FROM _rserv_log_ GROUP BY day"); |
153 |
|
|
|
154 |
|
|
# Sync point for each slave server |
155 |
|
|
Exec2($mconn,$sconn,"create table _RSERV_SYNC_ " . |
156 |
|
|
"(server int REFERENCES _RSERV_SERVERS_(server) ". |
157 |
|
|
"ON DELETE CASCADE ON UPDATE CASCADE, syncid int4 ". |
158 |
|
|
"not null, synctime timestamp, status int4 not null,". |
159 |
|
|
" minid int4 not null, maxid int4 not null, active text)"); |
160 |
|
|
|
161 |
|
|
Exec2($mconn,$sconn,"create index _RSERV_SYNC_INDX_SRV_ID_ on _RSERV_SYNC_ ". |
162 |
|
|
"(server, syncid)"); |
163 |
|
|
|
164 |
|
|
# Sync point reference numbers |
165 |
|
|
Exec2($mconn,$sconn,"create sequence _rserv_sync_seq_"); |
166 |
|
|
|
167 |
|
|
Exec2($mconn,$sconn,"CREATE FUNCTION _rserv_log_() RETURNS opaque" . |
168 |
|
|
" AS '$lib' LANGUAGE 'c'"); |
169 |
|
|
|
170 |
|
|
Exec2($mconn,$sconn,"CREATE FUNCTION _rserv_sync_(int4) RETURNS int4" . |
171 |
|
|
" AS '$lib' LANGUAGE 'c'"); |
172 |
|
|
|
173 |
|
|
Exec2($mconn,$sconn,"CREATE FUNCTION _rserv_debug_(int4) RETURNS int4" . |
174 |
|
|
" AS '$lib' LANGUAGE 'c'"); |
175 |
|
|
|
176 |
|
|
# Now, lets add the needed information in the slave database |
177 |
|
|
|
178 |
|
|
Exec2($mconn,$sconn,"create table _RSERV_SLAVE_TABLES_" . |
179 |
|
|
" (tname name not null, cname name not null, reloid oid not null, key int4 not null)"); |
180 |
|
|
|
181 |
|
|
Exec2($mconn,$sconn,"create table _RSERV_SLAVE_SYNC_" . |
182 |
|
|
" (syncid int4 not null, synctime timestamp)"); |
183 |
|
|
|
184 |
|
|
# next, let's tell the master that the slave is set up. |
185 |
|
|
|
186 |
|
|
Exec2($mconn,$sconn,"INSERT INTO _RSERV_SERVERS_ (host,dbase) VALUES ('$slavehost','$slave')"); |
187 |
|
|
|
188 |
|
|
# then commit the slave transaction |
189 |
|
|
|
190 |
|
|
Exec2($mconn,$sconn,"COMMIT"); |
191 |
|
|
|
192 |
|
|
exit (0); |