I have some workloads that need to increase WORK_MEM
and TEMP_BUFFERS
. The problem is, once I do this, the connection (session) is not closed and is returned to the pool. This means the extra memory usage isn't temporary because I can't change TEMP_BUFFERS
back without destroying the session.
I imagine the workflow going something like this:
conn = ActiveRecord.create_new_connection
conn.execute <<-SQL
SET TEMP_BUFFERS TO '512MB';
-- do some memory intensive
CREATE TEMPORARY TABLE .....;
UPDATE ....;
SQL
conn.close
Or maybe there is a way to close the current connection and have the pool crate a new one...
Seems like new_connection
is a private method, which is what we need to manually create a connection outside being part of the pool,
Then, upon inspecting the source code, you can do something like this:
connection_specification = ActiveRecord::Base.connection_pool.spec
# i.e.
# connection_specification.adapter_method == 'postgresql_connection'
# connection_specification.config == {:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"someapp_development"}
connection = ActiveRecord::Base.send(
connection_specification.adapter_method,
connection_specification.config
)
# DEBUG:
puts connection.active?
# => true
connection.execute <<-SQL
SET TEMP_BUFFERS TO '512MB';
-- do some memory intensive
CREATE TEMPORARY TABLE .....;
UPDATE ....;
SQL
connection.disconnect!
# DEBUG:
puts connection.active?
# => false
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments