Skip to content

Instantly share code, notes, and snippets.

@puyo
Last active Jan 4, 2016
Embed
What would you like to do?
Getting lots of PG:: errors with Rails 3.2.16 (last major stable release) and ruby-pg 0.17.1 (up to date).
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