Last active
January 4, 2016 04:59
-
-
Save puyo/8572465 to your computer and use it in GitHub Desktop.
Getting lots of PG:: errors with Rails 3.2.16 (last major stable release) and ruby-pg 0.17.1 (up to date).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' rescue nil | |
gem 'activerecord', '3.2.16' | |
gem 'pg', '0.17.1' | |
require 'active_record' | |
require 'logger' | |
puts ActiveRecord::VERSION::STRING | |
db_config = { | |
adapter: 'postgresql', | |
host: 'localhost', | |
port: 5432, | |
database: 'db_test', | |
username: ENV['USER'], | |
password: '', | |
pool: 5, | |
timeout: 5000, | |
encoding: 'UTF8' | |
} | |
ActiveRecord::Base.logger = Logger.new($stdout) | |
ActiveRecord::Base.establish_connection(db_config.merge(database: 'postgres', schema_search_path: 'public')) | |
ActiveRecord::Base.connection.create_database(db_config[:database]) rescue ActiveRecord::StatementInvalid | |
ActiveRecord::Base.establish_connection(db_config) # back to db_test to create the table and use it | |
puts "PostgreSQL #{ActiveRecord::Base.connection.send(:postgresql_version)}" # => PostgreSQL 90302 | |
ActiveRecord::Migration.class_eval do | |
create_table :posts, force: true do |t| | |
t.string :title | |
t.text :body | |
end | |
end | |
class Post < ActiveRecord::Base | |
end | |
pid = fork do | |
ActiveRecord::Base.establish_connection(db_config) # my mother said I never should keep using a db connection after a fork | |
keep_going = true | |
Signal.trap('HUP') { keep_going = false } | |
while keep_going | |
Post.where(title: 'Subprocess').first_or_create | |
end | |
end | |
#ActiveRecord::Base.connection.reconnect! # Riddle me this: why is this necessary? why does this make it work? | |
begin | |
20000.times do | |
ActiveRecord::Base.connection.execute('TRUNCATE posts CASCADE') | |
Post.where(title: 'Main').first_or_create | |
# activerecord-3.2.16/lib/active_record/connection_adapters/postgresql_adapter.rb:1163:in `async_exec': PG::ConnectionBad: PQconsumeInput() : SELECT a.attname, format_type(a.atttypid, a.atttypmod), (ActiveRecord::StatementInvalid) | |
# pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod | |
# FROM pg_attribute a LEFT JOIN pg_attrdef d | |
# ON a.attrelid = d.adrelid AND a.attnum = d.adnum | |
# WHERE a.attrelid = '"posts"'::regclass | |
# AND a.attnum > 0 AND NOT a.attisdropped | |
# ORDER BY a.attnum | |
end | |
ensure | |
Process.kill('HUP', pid) | |
Process.wait | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment