/[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

Annotation of /bin/MasterInit

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Tue Aug 5 21:10:28 2003 UTC (20 years, 10 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 dpavlin 1.5 #!/usr/bin/perl -w
2 dpavlin 1.1 # 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 dpavlin 1.5 use strict;
10 dpavlin 1.1 use Pg;
11     use Getopt::Long;
12    
13 dpavlin 1.5 my $lib = '/usr/lib/postgresql/lib/rserv.so';
14 dpavlin 1.3
15 dpavlin 1.1 $| = 1;
16    
17 dpavlin 1.5 my ($debug,$verbose) = (0,0);
18     my ($help,$masterhost,$masterport,$masteruser,$masterpassword);
19 dpavlin 1.1
20 dpavlin 1.5 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 dpavlin 1.1 exit ((scalar(@ARGV) < 1)? 1:0);
34     }
35    
36     my $master = $ARGV[0] || "master";
37    
38     my $minfo = "dbname=$master";
39 dpavlin 1.5 $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 dpavlin 1.3
44 dpavlin 1.1 sub RollbackAndQuit {
45 dpavlin 1.5 my $conn = shift @_;
46 dpavlin 1.1
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 dpavlin 1.5 $result = $conn->exec("BEGIN");
59 dpavlin 1.1 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 dpavlin 1.3 " (server serial primary key, host text not ".
67     "null, port int4 default 5432, dbase text not ".
68     "null, unique(host,port,dbase))");
69 dpavlin 1.1 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
70    
71     # List of replicated tables
72     $result = $conn->exec("create table _RSERV_TABLES_" .
73 dpavlin 1.3 " (tname name not null, cname name not null, ".
74     "reloid oid primary key, key int4 not null)");
75 dpavlin 1.1 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
76    
77 dpavlin 1.3 ## 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 dpavlin 1.1 $result = $conn->exec("create table _RSERV_LOG_" .
83 dpavlin 1.3 " (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 dpavlin 1.1 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
89    
90     # This is to speedup lookup of deleted tuples
91 dpavlin 1.3 $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 dpavlin 1.1 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
104    
105     # This is to speedup cleanup
106 dpavlin 1.3 $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 dpavlin 1.1 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
123    
124 dpavlin 1.3 # 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 dpavlin 1.1 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
130    
131     # Sync point for each slave server
132 dpavlin 1.3 $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 dpavlin 1.1 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
138    
139 dpavlin 1.3 $result = $conn->exec("create index _RSERV_SYNC_INDX_SRV_ID_ on _RSERV_SYNC_ ".
140     "(server, syncid)");
141 dpavlin 1.1 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 dpavlin 1.3 $result = $conn->exec("CREATE FUNCTION _rserv_log_() RETURNS opaque" .
148     " AS '$lib' LANGUAGE 'c'");
149 dpavlin 1.1 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
150    
151     $result = $conn->exec("CREATE FUNCTION _rserv_sync_(int4) RETURNS int4" .
152 dpavlin 1.3 " AS '$lib' LANGUAGE 'c'");
153 dpavlin 1.1 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
154    
155     $result = $conn->exec("CREATE FUNCTION _rserv_debug_(int4) RETURNS int4" .
156 dpavlin 1.3 " AS '$lib' LANGUAGE 'c'");
157 dpavlin 1.1 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