Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 30F851336971 for ; Thu, 16 Dec 2010 10:39:05 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 15765-10 for ; Thu, 16 Dec 2010 14:38:57 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from vms173009pub.verizon.net (vms173009pub.verizon.net [206.46.173.9]) by mail.postgresql.org (Postfix) with ESMTP id BE6F11337BB7 for ; Thu, 16 Dec 2010 10:38:57 -0400 (AST) Received: from trillian.localdomain ([unknown] [173.70.23.209]) by vms173009.mailsrvcs.net (Sun Java(tm) System Messaging Server 7u2-7.02 32bit (built Apr 16 2009)) with ESMTPA id <0LDI00L9VZCIWIB0@vms173009.mailsrvcs.net> for pgsql-sql@postgresql.org; Thu, 16 Dec 2010 08:38:43 -0600 (CST) Message-id: <4D0A2472.7030304@verizon.net> Date: Thu, 16 Dec 2010 09:38:42 -0500 From: Jean-David Beyer Organization: Institute for Regimented Whimsey User-Agent: Thunderbird 2.0.0.24 (X11/20100318) MIME-version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: Database consistency after a power shortage References: In-reply-to: X-Enigmail-Version: 0.96.0 Content-type: text/plain; charset=ISO-8859-1 Content-transfer-encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.909 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RFC_ABUSE_POST=0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201012/81 X-Sequence-Number: 34917 Samuel Gendler wrote: > > > On Wed, Dec 15, 2010 at 11:38 PM, Scott Marlowe > wrote: > > On Wed, Dec 15, 2010 at 8:12 AM, Alberto > wrote: > > > > > Is there any way to make the 3 operations be one transaction for the > > database, so that it keeps them all consistent in case a power > shortage > > occurs in the middle? > > Yes, put them in a transaction. > > begin; > insert into head_invoice ... > insert into detail_invocie ... > insert into payments_x_header_invoice ... > commit; > > Then they either all go or none go. > > > But if the database transaction concept is new to you, I highly > recommend you do a little reading about database transactions in general > and postgres' implementation specifics as well. It can be very easy for > you to make mistakes that can cause the database to get slow or use up a > lot of disk if you use transactions without understanding at least a > little of what is happening in the database while the transaction is > open but uncommitted. > > Incidentally, any error on a query within the transaction will cause the > transaction to automatically 'rollback' when the transaction completes, > undoing all of the changes, or you can manually cancel a transaction by > issuing a 'rollback;' statement instead of 'commit;' at the end. > > You can also (or more appropriately, in addition) equip your system with an uninterruptable power supply with enough capacity to coast over the power shortage interval, or to perform a controlled shutdown. I do not know how long it takes to do such a shutdown with postgreSQL, but it could involve stopping all new transactions from entering the system, and allowing those in process to complete. A UPS to allow 10 minutes of run-time is not normally considered too expensive. Mine will run for about an hour with new batteries, but after a few years it dwindles to about 1/2 hour. Then I get new ones. -- .~. Jean-David Beyer Registered Linux User 85642. /V\ PGP-Key: 9A2FC99A Registered Machine 241939. /( )\ Shrewsbury, New Jersey http://counter.li.org ^^-^^ 09:30:01 up 14 days, 23:16, 4 users, load average: 5.61, 4.98, 4.89