/[rserv]/bin/MasterInit
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/MasterInit

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (show annotations)
Sun Nov 2 10:31:44 2003 UTC (20 years, 5 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +6 -8 lines
another code clean-up

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

  ViewVC Help
Powered by ViewVC 1.1.26