class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::StatementPool

def [](key); cache[key]; end

def [](key);      cache[key]; end

def []=(sql, key)

def []=(sql, key)
  while @max <= cache.size
    dealloc(cache.shift.last)
  end
  @counter += 1
  cache[sql] = key
end

def cache

def cache
  @cache[Process.pid]
end

def clear

def clear
  cache.each_value do |stmt_key|
    dealloc stmt_key
  end
  cache.clear
end

def connection_active?

def connection_active?
  @connection.status == PGconn::CONNECTION_OK
rescue PGError
  false
end

def dealloc(key)

def dealloc(key)
  @connection.query "DEALLOCATE #{key}" if connection_active?
end

def delete(sql_key)

def delete(sql_key)
  dealloc cache[sql_key]
  cache.delete sql_key
end

def each(&block); cache.each(&block); end

def each(&block); cache.each(&block); end

def initialize(connection, max)

def initialize(connection, max)
  super
  @counter = 0
  @cache   = Hash.new { |h,pid| h[pid] = {} }
end

def key?(key); cache.key?(key); end

def key?(key);    cache.key?(key); end

def length; cache.length; end

def length;       cache.length; end

def next_key

def next_key
  "a#{@counter + 1}"
end