<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>blog@insidesystems.net: DELETE FROM important WHERE overly broad condition;</title>
    <link>http://blog.insidesystems.net/articles/2007/03/13/delete-from-important-where-overly-broad-condition</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>DELETE FROM important WHERE overly broad condition;</title>
      <description>&lt;p&gt;A client had an interesting problem.  They had a critical PostgreSQL database, and they accidentally ran an overly broad delete on a table.&lt;/p&gt;


	&lt;p&gt;And their newest backup was two weeks old.&lt;/p&gt;


	&lt;p&gt;Could we help?  Of course we could help.&lt;/p&gt;&lt;p&gt;The procedure used was as follows:&lt;/p&gt;


	&lt;p&gt;1) Stop the database.&lt;/p&gt;


	&lt;p&gt;2) Backup the database files to a secure location.&lt;/p&gt;


	&lt;p&gt;3) Make a second copy of the database files, to a new, empty postgresql instance.&lt;/p&gt;


	&lt;p&gt;4) Now if you have a dump prior to the error, and xlogs for the entire time period, you can do a Point-In-Time Recovery, with a properly configured recovery.conf.  Unfortunately we didn&amp;#8217;t have that.  We had a two-week old dump and 6 hours of xlogs.&lt;/p&gt;


	&lt;p&gt;5) Try to determine an approximate transaction ID of the mistake.  We did this by looking at timestamps of files on the newest pg_clog file, finding one that happened before the mistake, and then multiplying it&amp;#8217;s name by 1048576.&lt;/p&gt;


	&lt;p&gt;6) Then we used that xid with &lt;a href="http://www.postgresql.org/docs/current/static/app-pgresetxlog.html"&gt;pg_resetxlog&lt;/a&gt;, on the secondary database that we had created.  It worked, and we had a view of the data.  We then played a bit, to find an xid that was acceptably close to the point of failure.&lt;/p&gt;


	&lt;p&gt;7) From there, we just did a straight &lt;span class="caps"&gt;SQL COPY&lt;/span&gt;, to get the data from the db server we had just setup onto the original database, and to get it loaded.&lt;/p&gt;


	&lt;p&gt;It wasn&amp;#8217;t the most elegant solution, but it worked beautifully, and the client was pleased.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s to hoping you never need this information!&lt;/p&gt;</description>
      <pubDate>Tue, 13 Mar 2007 23:20:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:a692a8c8-35c8-4c18-a571-876210c20ca3</guid>
      <author>Kevin Way</author>
      <link>http://blog.insidesystems.net/articles/2007/03/13/delete-from-important-where-overly-broad-condition</link>
      <category>Databases</category>
      <category>System Administration</category>
      <category>PostgreSQL</category>
    </item>
  </channel>
</rss>
