/[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.9 - (show annotations)
Thu Oct 30 19:58:51 2003 UTC (20 years, 7 months ago) by dpavlin
Branch: MAIN
Changes since 1.8: +45 -47 lines
removed option

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

  ViewVC Help
Powered by ViewVC 1.1.26