<?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>Robby on Rails: Tag corruption</title>
    <link>http://www.robbyonrails.com/articles/tag/corruption</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Fixing PostgreSQL corruption with Rails?</title>
      <description>&lt;p&gt;People have been emailing me to let me know that the search on my blog was broken. Today I finally set out to fix the problem, which looked like a complex issue with my PostgreSQL database. I&amp;#8217;m not sure how long ago it started so I am not sure what was the cause at the moment. If you did a search on my blog you&amp;#8217;d get an application error and behind the scenes, you would see the following error.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;PGError: ERROR:  missing chunk number 0 for toast value 58441&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Eek!&lt;/p&gt;


	&lt;p&gt;So, I tried to &lt;span class="caps"&gt;REINDEX&lt;/span&gt; the table and it didn&amp;#8217;t solve the problem&amp;#8230; so I started poking around with different types of queries to see what I could do to generate the error again. Didn&amp;#8217;t take me long to figure out that it had something to do with the body column in the contents table.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
db_name=# SELECT count(id) FROM contents WHERE body ~* 'postgresql' LIMIT 20;
ERROR:  missing chunk number 0 for toast value 58441

db_name_=# SELECT count(id) FROM contents WHERE excerpt ~* 'postgresql' LIMIT 20;
 count 
-------
     0
(1 row)
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;What am I to do? I did some googling (and go figure&amp;#8230; the error being returned was caught on &lt;a href="http://www.google.com/search?&amp;#38;q=missing+chunk+number+0+for+toast+value"&gt;my blog by google&lt;/a&gt;)... which was amusing.&lt;/p&gt;


	&lt;p&gt;In the &lt;code&gt;#postgresql&lt;/code&gt; channel on freenode they recommended that I try and find the specific row in the table that was causing this error. I decided to just run a for loop in &lt;code&gt;script/console&lt;/code&gt; on the Content object in Typo and print out the name of each record until it gave me an error.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
for i in 1..30000
  puts Content.find( i ).title
  i =+ 1
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This began to print out titles of older blog entries and sure enough&amp;#8230; the loop died when it hit the following error. :-)&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  PostgreSQL sequences in Rails
  When TSearch2 Met AJAX
  ActiveRecord::StatementInvalid: PGError: ERROR:  missing chunk number 0 for toast value 58441
  : SELECT * FROM contents WHERE (contents.id = 1678)  LIMIT 1
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/connection_adapters/abstract_adapter.rb:88:in `log'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/connection_adapters/postgresql_adapter.rb:137:in `execute'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/connection_adapters/postgresql_adapter.rb:351:in `select'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/connection_adapters/postgresql_adapter.rb:118:in `select_all'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/base.rb:431:in `find_by_sql'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/base.rb:395:in `find'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/base.rb:393:in `find'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/base.rb:409:in `find'
          from (irb):23
          from (irb):22
  &amp;gt;&amp;gt; exit
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;15.seconds.later I logged into psql and ran &lt;code&gt;DELETE FROM contents WHERE id = 1678&lt;/code&gt;... and all is well!&lt;/p&gt;
</description>
      <pubDate>Sat, 05 Aug 2006 19:16:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:378f2847-e095-4a05-931e-94fce37c0914</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2006/08/05/fixing-postgresql-corruption-with-rails</link>
      <category>console</category>
      <category>corruption</category>
      <category>database</category>
    </item>
  </channel>
</rss>
