/[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.5 - (show annotations)
Tue Aug 5 21:10:28 2003 UTC (20 years, 9 months ago) by dpavlin
Branch: MAIN
Changes since 1.4: +24 -18 lines
command line options cleanup and cosistency fix, modification to work
under "use strict;"

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

  ViewVC Help
Powered by ViewVC 1.1.26