Fixing PostgreSQL corruption with Rails? 3
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’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’d get an application error and behind the scenes, you would see the following error.
PGError: ERROR: missing chunk number 0 for toast value 58441
Eek!
So, I tried to REINDEX the table and it didn’t solve the problem… so I started poking around with different types of queries to see what I could do to generate the error again. Didn’t take me long to figure out that it had something to do with the body column in the contents table.
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)
What am I to do? I did some googling (and go figure… the error being returned was caught on my blog by google)... which was amusing.
In the #postgresql 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 script/console on the Content object in Typo and print out the name of each record until it gave me an error.
for i in 1..30000
puts Content.find( i ).title
i =+ 1
end
This began to print out titles of older blog entries and sure enough… the loop died when it hit the following error. :-)
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
>> exit
15.seconds.later I logged into psql and ran DELETE FROM contents WHERE id = 1678... and all is well!
irb history hack
Okay, I’ve posted a few things about console recently and I know that you’re all using it… right?
Well, Jeremy Kemper (bitsweat) mentioned at the PDX.rb meeting last night that there was a way to keep history of your irb commands. (more than just readline… up-arrow’n).
You can actually quit irb, restart it and still see your history. This makes Robby happy.
Thanks Jeremy!
Master, console, and servant part two
This is a brief addition to the post from a few days ago, Master, console, and servant.
Noradio on IRC pointed out one thing that I did not mention in my previous blog entry… that you can bind a subsession of irb to a specific object. What??
Okay, let me explain this a bit more. Instead of always setting a variable to hold an instance of an object, you can be a bit more creative with irb.
I present…

So, for example. You can perform the following in script/console.
If there is an attribute/method called name in your model, then it will call it there. See my screen shot for some more details of how I used the jobs and fg # commands to switch jobs…. and I even found out what happens when you attempt to switch to the current thread. :-)
Until next time…
Master, console, and servant
I love console in Rails. It is absolutely one of my favorite tools in Rails. I probably spend more time testing stuff in console than I do in a browser, which is a good thing. Right?
Today, I was reading through a part of Programming Ruby (I like to open up to a random page and read something every once in a while) and came across a section on page 189. Subsessions and Bindings I was not aware that I could do this within irb and immediately thought to myself, “can I do that with console?” Console is afterall…just irb with all the Rails features pre-configured. So, I decided that I would try this out the next time I was in console.
That time came and passed… so, does it work?
The answer: YES!
I even have proof… :-)

This should eliminate a few tabs in iterm for me and works just like the Unix jobs and fg commands do.
If you are still not using console yet, why?
Perhaps you enjoy the pain of constant browser reloads because you keep trying to figure out if that method works. Often times, you can copy and paste your code right into console. It’s beautiful and gives you a quicker response than your browser will ever. But, you already knew that. Right?
Don’t get me wrong, I can totally understand your justifications for the extra pain. Who wants to work in their terminal more than they have to? (I do… but I do it because I am at home in my terminal.) There is nothing pretty about the terminal… but I feel in control. Are you a servant of your pretty GUI IDE and browser or are you the master of your terminal? ;-)
Okay, I will end this nonsense with my new console marketing campaign. I have employeed three pigeons to help me work on this one and the end result of our hard work?
Have you ruby script/console‘d today?
Yes. Classy and Original. :-)
Back to writing…
Are you a console master? 6
I have a few questions.
1.) Do you know what ./script/console does?
2.) If not, why not?
3.) If so, do you have any fun tips and tricks to share with the masses?
It occured to me earlier that many people, who might have came from the PHP camp, may have never really tested their object-oriented code from some sort of interactive program. (irb) If you are coming from the Python, Java, etc worlds, interactive testing isn’t anything new. Rails is nice enough to bundle a console script right within it!
I meet people online who have never even tried to run it. There are not many tutorials on the wiki that show console… and in my opinion, its one of the coolest things about Ruby and Rails. (but, I come from the php world…)
So, if you aren’t using it… why not? got a moment? try this from the root path of your Rails application.
./script/console
It start up okay? If so, what is the name of one of your models? Let’s say that I have a model structure like:
class Customer < ActiveRecord::Base
has_many :orders, :dependent => true
end
class Order < ActiveRecord::Base
belongs_to :customer
endconsole, you can access your models and do all sorts of fun things.
>> y = Customer.find(16)
=> #<Customer:0x2743ea4 @attributes={"name"=>"Robby", "id"=>"16"}>
>> y.orders
=> [#<Order:0x27416b8 @attributes={"id"=>"18", "amount"=>"12.00", "customer_id"=>"16"}>, #<Order:0x274167c @attributes={"id"=>"19", "amount"=>"12.50", "customer_id"=>"16"}>]
Pretty neat, huh?
>> o = Order.find(18)
=> #<Order:0x273da68 @attributes={"id"=>"18", "amount"=>"12.00", "customer_id"=>"16"}>
>> o.customer.name
=> "Robby"
If you are remotely a console wizard, please share some tips and tricks for those who are not sure what to do with it. I personally find myself in console all the time that I am working with Rails, testing stuff out with my models, before I move any of the code to my application.
It sure beats, hitting refresh in your browser all day. :-)




