/[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.3 - (show annotations)
Mon Nov 3 21:30:35 2003 UTC (20 years, 5 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +3 -0 lines
added _rserv_xid_ function to return current transaction xid and removed
all perl cludgery about keys (as well as KEYS directive from snapshots)

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 = MkInfo($master,$masterhost,$masterport,$masteruser,$masterpassword);
62 my $sinfo = MkInfo($slave,$slavehost,$slaveport,$slaveuser,$slavepassword);
63
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 Exec2($mconn,$sconn,"CREATE FUNCTION _rserv_xid_() RETURNS int4" .
177 " AS '$lib' LANGUAGE 'c'");
178
179 # Now, lets add the needed information in the slave database
180
181 Exec2($mconn,$sconn,"create table _RSERV_SLAVE_TABLES_" .
182 " (tname name not null, cname name not null, reloid oid not null, key int4 not null)");
183
184 Exec2($mconn,$sconn,"create table _RSERV_SLAVE_SYNC_" .
185 " (syncid int4 not null, synctime timestamp)");
186
187 # next, let's tell the master that the slave is set up.
188
189 Exec2($mconn,$sconn,"INSERT INTO _RSERV_SERVERS_ (host,dbase) VALUES ('$slavehost','$slave')");
190
191 # then commit the slave transaction
192
193 Exec2($mconn,$sconn,"COMMIT");
194
195 exit (0);

  ViewVC Help
Powered by ViewVC 1.1.26