Received: from mail.postgresql.org (webmail.postgresql.org [216.126.85.28]) by mail.postgresql.org (8.11.1/8.11.1) with SMTP id f02MPB707190 for ; Tue, 2 Jan 2001 17:25:11 -0500 (EST) (envelope-from pgsql-admin-owner@postgresql.org) Received: from veritime.com (dennis.veritime.com [64.240.90.194]) by mail.postgresql.org (8.11.1/8.11.1) with SMTP id f01GEDL75650 for ; Mon, 1 Jan 2001 11:14:13 -0500 (EST) (envelope-from rhampton@veritime.com) Received: (qmail 28401 invoked by uid 105); 1 Jan 2001 16:13:54 -0000 Received: from rhampton@veritime.com by dennis.veritime.com with qmail-scanner-0.90 (fsecure: 4.08/2030/2000-12-27/2000-12-28/2000-00-08. . Clean. Processed in 1.958305 secs); 01/01/2001 11:13:52 Received: from nic-30-c33-207.mw.mediaone.net (HELO pavilion) (24.30.33.207) by dennis.veritime.com with SMTP; 1 Jan 2001 16:13:52 -0000 Message-ID: <002d01c07426$96c13e80$cf211e18@mw.mediaone.net> From: "rhampton" To: "Lockhurst" Cc: Subject: Fw: Redundant databases/real-time backup Date: Mon, 1 Jan 2001 11:11:04 -0800 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_002A_01C073E3.87EE84A0" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 X-Archive-Number: 200101/10 X-Sequence-Number: 380 This is a multi-part message in MIME format. ------=_NextPart_000_002A_01C073E3.87EE84A0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Lockhurst, I looked at your startup line and it looks like you are missing an '>' in the middle of it. I have sent you my original message because I see that you responded to what Ragnar said later in the thread. Ragnar had also suggested that I could use fifo, named pipe instead of processing the transactions in batch, but I believe that is incorrect. It is crucial to know whether or not the statement was executed properly by the redundant database server, thus my perl script sends one sql statement at a time, gets a response, decides whether there was an error, and continues. Good luck. I realize that there are many shortcomings to the approach I have taken, including the inability of the script to test a connection to the redundant database before processing, high server overhead, etc. Maybe somebody could do it better in C. I don't know C but I'm sure that a binary would work a heck of a lot better than this little perl script. _______________________________ Hi, Tried by modifying /etc/rc.d/init.d/postgresql with following changes: su -l postgres -c '/usr/bin/postmaster -i -D /var/lib/pgsql /var/lib/pgsql/query_log 2>&1 &' The file query_log is created. But it is remaining empty even after restart and all sorts of transactions including insert statements. Early thanks for any help Wish you all a vey happy new 2001. Lockhurst ----- Original Message ----- From: "root" To: Sent: Thursday, November 16, 2000 8:14 AM Subject: [ADMIN] Redundant databases/real-time backup > Several people have expressed an interest in having the capability to have real > time redundancy. I am releasing my kludge solution to the mailing list in hope > that others might expand upon it. > > First, you should dump your database, drop it, and recreate it on the computer > to be mirrored. You should also create a fresh copy on the mirroring computer. > Most likely, your OIDs are still not going to be in synch. For those of you > that use OIDs as a poor man's primary key, it will be necessary for you to > write a script that can sync up the oids on both computers (probably best run > as a cron script too). I have thought about ways to do this, but I'll leave > that to someone else to complete. (PLEASE POST YOUR RESULTS THOUGH!) > > It is necessary to create/alter the postgresql startup script. I have included > a copy of mine. The database to be mirrored must start up with logging > enabled: > > su -l postgres -c '/usr/bin/postmaster -i -D/home/postgres/data >/home/postgres/data/query_log 2>&1 &' > > In this case I have specified a file called query_log that will maintain a copy > of all of the queries that have been executed. I have included a complete > copy of my startup script called (unimaginatively) postgresql. For linux users, > it should be in /etc/rc.d/init.d > > I have also attached my pg_options file. For me, this resides in > /home/postgres/data. I have found that this file does not seem to affect my > query_log, but I incude it for reference for others to use. > > Next, you should create a line in your /etc/crontab or /var/spool/cron/root file > to execute the redundancy script with root level permissions: > > 0-59/5 * * * * /root/redundancy.pl > > You should install the redundancy.pl and rederhandler.pl scripts in the same > directory. > > You will, of course, need to modify these scripts to work. Several of my dead > ends are still in the scripts commented out. > > rederhandler.pl is set up to work with qmail instead of sendmail. You should > be able to substitute the path to your sendmail program and it should work > fine, i.e. /usr/sbin/sendmail instead of /var/qmail/bin/qmail-inject > > Other points: > The query_log can get large rather quickly. You cannot simply issue a rm -rf > query_log, touch query_log and chmod. Even with the appropriate permissions > the daemon will not write to a new file, for some reason you must restart > postgres using the startup script. Perhaps one of the developers has an answer > to this problem..... > > Also, my script does not check for network problems.... > > The script takes a lot of overhead. For high volume inserts and deletes, > depending on how often you run redundancy.pl, sometimes it just cannot keep up. > BEWARE....it can bog down and crash your server if the backlog becomes too > large. > > Also, I chose to allow it to pass SELECT statements and get back the results > from the remote database......I beleive that if you do not use sub-selects, you > may grep -v 'SELECT' and improve your performance significantly. > > Please email comments and suggestions/modifications to me at > rhampton@veritime.com or root@veritime.com > ------=_NextPart_000_002A_01C073E3.87EE84A0 Content-Type: application/octet-stream; name="postgresql.dat" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="postgresql.dat" #! /bin/sh # postgresql This is the init script for starting up the PostgreSQL # server #Changed by Rodney Hampton 7/24/00 # Version 6.5.3-2 Lamar Owen # Added code to determine if PGDATA exists, whether it is current version # or not, and initdb if no PGDATA (initdb will not overwrite a database= ). # chkconfig: 345 85 15 # description: Starts and stops the PostgreSQL backend daemon that handles \ # all database requests. # processname: postmaster # pidfile: /var/run/postmaster.pid # # Source function library. . /etc/rc.d/init.d/functions # Get config. . /etc/sysconfig/network # Check that networking is up. # Pretty much need it for postmaster. [ ${NETWORKING} =3D "no" ] && exit 0 [ -f /usr/bin/postmaster ] || exit 0 # This script is slightly unusual in that the name of the daemon (postmaste= r) # is not the same as the name of the subsystem (postgresql) # See how we were called. case "$1" in start) echo -n "Checking postgresql installation: " # Check for the PGDATA structure if [ -f /home/postgres/data/PG_VERSION ] && [ -d /home/postgres/data/base/= template1 ] then # Check version of existing PGDATA if [ `cat /home/postgres/data/PG_VERSION` !=3D '6.5' ] then echo "old version. Need to Upgrade." echo "See /usr/doc/postgresql-6.5.3/README.rpm for more information." exit 1 else echo "looks good!" fi # No existing PGDATA! Initdb it. else echo "no database files found." if [ ! -d /home/postgres/data ] then mkdir -p /home/postgres/data chown postgres.postgres /home/postgres/data fi su -l postgres -c '/usr/bin/initdb --pglib=3D/usr/lib/pgsql --pgdata=3D/h= ome/postgres/data' fi # Check for postmaster already running... pid=3D`pidof postmaster` if [ $pid ] then echo "Postmaster already running." else #all systems go -- remove any stale lock files rm -f /tmp/.s.PGSQL.* > /dev/null echo -n "Starting postgresql service: " su -l postgres -c '/usr/bin/postmaster -i -D/home/postgres/data >/home/po= stgres/data/query_log 2>&1 &' sleep 1 pid=3D`pidof postmaster` if [ $pid ] then echo -n "postmaster [$pid]" touch /var/lock/subsys/postgresql echo $pid > /var/run/postmaster.pid echo else echo "failed." fi fi ;; stop) echo -n "Stopping postgresql service: " killproc postmaster sleep 2 rm -f /var/run/postmaster.pid rm -f /var/lock/subsys/postgresql echo ;; status) status postmaster ;; restart) $0 stop $0 start ;; *) echo "Usage: postgresql {start|stop|status|restart}" exit 1 esac exit 0 ------=_NextPart_000_002A_01C073E3.87EE84A0 Content-Type: text/plain; name="pg_options.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="pg_options.txt" all=3D1 verbose=3D2 query=3D4 syslog=3D2 ------=_NextPart_000_002A_01C073E3.87EE84A0 Content-Type: application/octet-stream; name="rederhandler.pl" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="rederhandler.pl" #!/usr/bin/perl # # Error handler routines for Postgres Redundancy Program # # usage is Er-Handler(comma seperated message list) sub Er_Handler { #get date $date=3D&ctime(time); chomp($date); chomp($_[0]); #write message to error file open(ERRF,">>/home/postgres/data/error")||die"could not open error file\n"; print ERRF "\-\>Error on $_[1] Postgres Redundancy Program:$date\n"; print ERRF "$_[0]\<\-\n"; close(ERRF); #write message to E-mail # open(MAIL,"|/var/qmail/bin/qmail-inject") || die "could not mail\n"; open(MAIL,"|/var/qmail/bin/qmail-inject") || die "could not mail\n"; print MAIL "To: root\@veritime.com\n"; print MAIL "From: root\@veritime.com\n"; print MAIL "Subject: Postgres Redundancy Error\n"; print MAIL "\n\n"; print MAIL "The following Error has occured in the $_[1] \n"; print MAIL "On $date\n"; print MAIL "$_[0]\n"; print MAIL ".\n"; close(MAIL); =09 print "Redundancy Error\n"; print "An error has occurred\n"; print "A message has been sent to the administrator.\n"; print "If error continues please contact root\@veritime.com.\n"; print "\n"; # exit; } 1;#return true ------=_NextPart_000_002A_01C073E3.87EE84A0 Content-Type: application/octet-stream; name="redundancy.pl" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="redundancy.pl" #!/usr/bin/perl require "ctime.pl";=20 #will return the process ID's belonging to postmaster, must suspend them all system("rm -f postmasterids"); system("ps -C postmaster -o pid=3D > postmasterids"); # system("sed -n '1,1p' postmasterids > postmasterid"); open (FILE,"postmasterids") || die "could not open postmasterids file\n$!";= =20 $PID=3D"";=20 while() {=20 $PID=3D$_;=09 # print $PID; #kill -19 PID SIGSTOP #stop the process to ensure no more writes to the database system ("kill -19 $PID"); }=20 close(FILE);=20 system ("cp -f /home/postgres/data/query_log /home/postgres/data/temp_pass= _file"); #read every line with sed, find the queries, exclude the system messages an= d database vacuuming, pipe into pass_file system("sed -n '1~1p' /home/postgres/data/temp_pass_file | grep -a 'query:= ' |grep -v 'vacuum' | grep -v 'SQL_ASCII' | grep -v 'getdatabaseencoding' |= grep -v 'version' >> /home/postgres/data/pass_file"); #make sure that there aren't any lingering queries in the log file that did= n't get sent to the pass_file system("diff /home/postgres/data/query_log /home/postgres/data/temp_pass_f= ile > /home/postgres/data/new_query_log"); #clean up a bit system("rm -f /home/postgres/data/temp_pass_file"); #one big log file # system("touch /var/log/postgreslog"); # system("cat /home/postgres/data/query_log >> /var/log/postgreslog"); #suck out the contents but leave the file intact, postgres daemon is lookin= g for only this file # system("sed 's/ *$//' /home/postgres/data/query_log > /home/postgres/data= /query_log"); =09 #make sure that the query_log is initialized with the results of the diff system("cat /home/postgres/data/new_query_log > /home/postgres/data/query_= log"); system("rm -f /home/postgres/data/new_query_log"); system("chown postgres:postgres /home/postgres/data/query_log"); system("chmod 664 /home/postgres/data/query_log"); open (FILE,"postmasterids") || die "could not open postmasterids file\n$!";= =20 $PID=3D"";=20 while() {=20 $PID=3D$_;=09 # print $PID; #let the process continue #kill -18 PID SIGCONT system ("kill -18 $PID"); }=20 close(FILE);=20 system("rm -f postmasterids"); #now read the pass_file into the remote database use Pg; require "./rederhandler.pl";=20 $title=3D"Postgres Redundancy Program"; $conn=3DPg::connectdb("host=3Dflintstone port=3D5432 user=3Dveritime dbname= =3Dveritime"); die $conn->errorMessage unless PGRES_CONNECTION_OK eq $conn->status; eval {=20 open (PASS_FILE,"/home/postgres/data/pass_file") || die "could not open pa= ss_file\n$!";=20 };=20 if($@)=20 {=20 &Er_Handler($@,$title);=20 }=20 $errors=3D0; while() {=20 $line=3D$_; if ($line =3D~ /query:/) { #query sting equals string following the regular expression match #print $line; #print "\n"; $query_string=3D$'; #strip leading spaces $query_string =3D~ s/^\s+// ; #strip trailing spaces $query_string =3D~ s/\s+$// ; print $query_string; print "\n"; =09=09 $error_message=3D""; $result_status=3D""; if ($errors eq 0) { eval {=09 # $result=3D$conn->exec("SELECT * from distributor_table ORDER BY distri= butor_id LIMIT 1;"); $result=3D$conn->exec("$query_string"); die $conn->errorMessage unless PGRES_TUPLES_OK eq $result->resultStatus; }; if($@)=20 {=20 &Er_Handler($@,$title); $error_message=3D$conn->errorMessage; if ($error_message eq "PGRES_CONNECTION_BAD") { $errors++; } $result_status=3D$result->resultStatus; if ($result_status eq "PGRES_FATAL_ERROR") { $errors++; } }=20 while(@row=3D$result->fetchrow)=20 { print @row, "\n"; } #end while } #end if errors=3D0 begin else else { open(ERROR_FILE, ">>/home/postgres/data/error_file")|| die "could not op= en error_file\n$!"; # Open for appending print ERROR_FILE $query_string; close(ERROR_FILE); print "appending query string to error file"; } #end if then else } #end if }=20 #end while close(PASS_FILE); system("rm -f /home/postgres/data/pass_file"); if ($errors > 0) { system ("cat /home/postgres/data/error_file > /home/postgres/data/pass_fil= e"); system("rm -f/home/postgres/data/error_file"); system("touch /home/postgres/data/error_file"); } system("touch /home/postgres/data/pass_file"); ------=_NextPart_000_002A_01C073E3.87EE84A0--