/[rserv]/bin/MultiMasterInit
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /bin/MultiMasterInit

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Fri Oct 31 00:07:57 2003 UTC (20 years, 6 months ago) by dpavlin
Branch: MAIN
new init script for multi-master configuration

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 my $minfo = "dbname=$master";
62 $minfo = "$minfo host=$masterhost" if (defined($masterhost));
63 $minfo = "$minfo port=$masterport" if (defined($masterport));
64 $minfo = "$minfo user=$masteruser" if (defined($masteruser));
65 $minfo = "$minfo password=$masterpassword" if (defined($masterpassword));
66
67 my $sinfo = "dbname=$slave";
68 $sinfo = "$sinfo host=$slavehost" if (defined($slavehost));
69 $sinfo = "$sinfo port=$slaveport" if (defined($slaveport));
70 $sinfo = "$sinfo user=$slaveuser" if (defined($slaveuser));
71 $sinfo = "$sinfo password=$slavepassword" if (defined($slavepassword));
72
73 $masterhost = "localhost" if (! $masterhost);
74 $slavehost = "localhost" if (! $slavehost);
75
76 $RServ::debug = $debug;
77 $RServ::verbose = $verbose;
78 $RServ::quiet = $quiet;
79
80 if (!defined($lib) || !-e $lib) {
81 $lib = "$basedir/lib/";
82
83 if (-e "$lib/rserv.so") {
84 $lib .= "rserv.so";
85 } else {
86 print STDERR "Can't find compiled rserv.so in $lib. Go there and type make.\n";
87 exit 1;
88 }
89 }
90 print "Using lib '$lib'\n" if ($verbose);
91
92 my $mconn = Connect($minfo);
93 my $sconn = Connect($sinfo);
94
95 Exec2($mconn,$sconn,"BEGIN");
96
97 Exec2($mconn,$sconn,"set transaction isolation level serializable");
98
99 # List of slave servers
100 Exec2($mconn,$sconn, "create table _RSERV_SERVERS_" .
101 " (server serial primary key, host text not ".
102 "null, port int4 default 5432, dbase text not ".
103 "null, unique(host,port,dbase))");
104
105 # insert master server in list
106 Exec2($mconn,$sconn,"insert into _RSERV_SERVERS_" .
107 " (server, host, port, dbase) ".
108 " values (0,'$masterhost',".
109 ($masterport || 5432).",".
110 "'$master')");
111
112 # List of replicated tables
113 Exec2($mconn,$sconn,"create table _RSERV_TABLES_" .
114 " (tname name not null, cname name not null, ".
115 "reloid oid primary key, key int4 not null)");
116
117 ## should always call MasterDelTable
118 #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)");
119
120 # Bookkeeping log for row replication
121 Exec2($mconn,$sconn,"create table _RSERV_LOG_" .
122 " (reloid oid REFERENCES _RSERV_TABLES_(reloid) ON ".
123 "DELETE CASCADE ON UPDATE CASCADE, logid int4 not ".
124 "null, logtime timestamp not null, insert smallint, ".
125 "update smallint, delete smallint, key text, ".
126 "server int4, ".
127 "CONSTRAINT only_one CHECK (insert+update+delete=1))");
128
129 # This is to speedup lookup of deleted tuples
130 Exec2($mconn,$sconn,"create index _RSERV_LOG_INDX_DLT_ID_ on _RSERV_LOG_ ".
131 "(delete, logid) WHERE delete = 1");
132
133 # This is to speedup lookup of updated tuples
134 Exec2($mconn,$sconn,"create index _RSERV_LOG_INDX_UPD_ID_ on _RSERV_LOG_ ".
135 "(update, logid) WHERE update = 1");
136
137 # This is to speedup lookup of insert tuples
138 Exec2($mconn,$sconn,"create index _RSERV_LOG_INDX_INS_ID_ on _RSERV_LOG_ ".
139 "(insert, logid) WHERE insert = 1");
140
141 # This is to speedup cleanup
142 Exec2($mconn,$sconn,"create index _RSERV_LOG_INDX_TM_ID_ on _RSERV_LOG_ ".
143 "(logtime, logid)");
144
145 # This is to speedup trigger
146 Exec2($mconn,$sconn,"create index _RSERV_LOG_INDX_REL_KEY_ on _RSERV_LOG_ ".
147 "(reloid, key)");
148
149 # View to help managing _rserv_log_ table
150 Exec2($mconn,$sconn,"CREATE VIEW _RSERV_HUMAN_LOG_ AS SELECT log.logid, ".
151 "tab.tname AS table_name, tab.cname AS column_name, ".
152 "log.key AS column_value, log.insert, log.update, ".
153 "log.delete, log.logtime FROM _RSERV_LOG_ log, ".
154 "_RSERV_TABLES_ tab WHERE tab.reloid ".
155 "= log.reloid ORDER BY log.logtime");
156
157 # View to help logging daily transactions
158 Exec2($mconn,$sconn,"CREATE VIEW _RSERV_DAILY_LOG_ AS ".
159 "SELECT count(*) AS \"# records\", ".
160 "to_char(_rserv_log_.logtime, 'YYYY-MM-DD') ".
161 "AS day FROM _rserv_log_ GROUP BY day");
162
163 # Sync point for each slave server
164 Exec2($mconn,$sconn,"create table _RSERV_SYNC_ " .
165 "(server int REFERENCES _RSERV_SERVERS_(server) ".
166 "ON DELETE CASCADE ON UPDATE CASCADE, syncid int4 ".
167 "not null, synctime timestamp, status int4 not null,".
168 " minid int4 not null, maxid int4 not null, active text)");
169
170 Exec2($mconn,$sconn,"create index _RSERV_SYNC_INDX_SRV_ID_ on _RSERV_SYNC_ ".
171 "(server, syncid)");
172
173 # Sync point reference numbers
174 Exec2($mconn,$sconn,"create sequence _rserv_sync_seq_");
175
176 Exec2($mconn,$sconn,"CREATE FUNCTION _rserv_log_() RETURNS opaque" .
177 " AS '$lib' LANGUAGE 'c'");
178
179 Exec2($mconn,$sconn,"CREATE FUNCTION _rserv_sync_(int4) RETURNS int4" .
180 " AS '$lib' LANGUAGE 'c'");
181
182 Exec2($mconn,$sconn,"CREATE FUNCTION _rserv_debug_(int4) RETURNS int4" .
183 " AS '$lib' LANGUAGE 'c'");
184
185 # Now, lets add the needed information in the slave database
186
187 Exec2($mconn,$sconn,"create table _RSERV_SLAVE_TABLES_" .
188 " (tname name not null, cname name not null, reloid oid not null, key int4 not null)");
189
190 Exec2($mconn,$sconn,"create table _RSERV_SLAVE_SYNC_" .
191 " (syncid int4 not null, synctime timestamp)");
192
193 # next, let's tell the master that the slave is set up.
194
195 Exec2($mconn,$sconn,"INSERT INTO _RSERV_SERVERS_ (host,dbase) VALUES ('$slavehost','$slave')");
196
197 # then commit the slave transaction
198
199 Exec2($mconn,$sconn,"COMMIT");
200
201 exit (0);

  ViewVC Help
Powered by ViewVC 1.1.26