Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres: allow enabling prepared statements for specific queries when globally disabled. #36795

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -642,13 +642,14 @@ def execute_and_clear(sql, name, binds, prepare: false)
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
end

if without_prepared_statement?(binds)
if prepare
result = exec_cache(sql, name, binds)
elsif without_prepared_statement?(binds)
result = exec_no_cache(sql, name, [])
elsif !prepare
result = exec_no_cache(sql, name, binds)
else
result = exec_cache(sql, name, binds)
result = exec_no_cache(sql, name, binds)
end

ret = yield result
result.clear
ret
Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/cases/adapters/postgresql/connection_test.rb
Expand Up @@ -138,6 +138,17 @@ def test_statement_key_is_logged
end
end

def test_force_prepare_statement
@connection.unprepared_statement do
@connection.exec_query("SELECT 1::integer", "SQL", [], prepare: true)
name = @subscriber.payloads.last[:statement_name]
assert name
res = @connection.exec_query("EXPLAIN (FORMAT JSON) EXECUTE #{name}(1)")
plan = res.column_types["QUERY PLAN"].deserialize res.rows.first.first
assert_operator plan.length, :>, 0
end
end

def test_reconnection_after_actual_disconnection_with_verify
original_connection_pid = @connection.query("select pg_backend_pid()")

Expand Down