I would like to enter a large amount of data in my file. To do this, I am thinking of using postgresql's asynchronous processing to do the query. The code is as follows.
raw_conn = ActiveRecord::Base.connection.raw_connection
raw_conn.send_query("SELECT id FROM users")
raw_conn.set_single_row_mode
begin
raw_conn.get_result.stream_each do |f|
# writing process to file
end
end
raw_conn.send_query("SELECT id FROM logins")
raw_conn.set_single_row_mode
begin
raw_conn.get_result.stream_each do |f|
# writing process to file
end
end
When the second send_query is executed, the following error occurs.
another command is already in progress
I had a feeling that the connection pool was the cause, but the documentation did not mention it. Is there any way to run send_query more than once and get results?
CodePudding user response:
I Resolved. I tried to reset the connection before sending a new query, and it worked.
begin
raw_conn.get_result.stream_each do |f|
# writing process to file
end
ensure
raw_conn.reset
end