古いサーバでRailsを動かそうとしてハマった

古いサーバでRuby on Railsのアプリケーションを動かそうとしたときに

failed to allocate memory (NoMemoryError)

とだけ表示されてアプリケーションが動かなかった。
いろいろ調べてやっと原因がわかったのでメモ。


いろいろ試してPostgreSQLとデータをやり取りしようとする際に
以下のようなエラーメッセージが出ていることがわかった。

/usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/buffer.rb:64:in `read': failed to allocate memory (NoMemoryError)
        from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/buffer.rb:64:in `copy_from_stream'
        from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/postgres-pr/message.rb:41:in `read'
        from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/postgres-pr/connection.rb:30:in `initialize'
        from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/postgres-pr/connection.rb:29:in `loop'
        from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/postgres-pr/connection.rb:29:in `initialize'
        from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/postgres-pr/postgres-compat.rb:23:in `new'
        from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/postgres-pr/postgres-compat.rb:23:in `initialize'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/postgresql_adapter.rb:24:in `connect'
         ... 17 levels...
        from script/setup_database:99:in `create_tables'
        from script/setup_database:19:in `run'
        from script/setup_database:9:in `run'
        from script/setup_database:297

どうやら原因はPostgreSQLのバージョンが古すぎたためらしい。
ちなみにこの時使用したPostgreSQLのバージョンは7.2.4。


PostgreSQLはバージョン7.4を境にプロトコルが変更されている。

PostgreSQLのバージョン プロトコルバージョン
7.4未満 2.0
7.4以降 3.0


Rubyのpostgres-prはプロトコル3.0しか対応していないため
古いバージョンのPostgreSQLで動かないという問題が起こるらしい。


わざわざこんな古いサーバで
Ruby on Rails使おうとする人なんていないと思うけど一応。


参考:http://www.sagewire.org/ruby-programming/postgrespr-170919.aspx