Skip to content

Commit

Permalink
Postgres: allow enabling prepared statements for specific queries whe…
Browse files Browse the repository at this point in the history
…n disabled globally.

There is no way to do this otherwise.
  • Loading branch information
xaviershay committed Aug 2, 2019
1 parent d880fae commit a3b355e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
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

0 comments on commit a3b355e

Please sign in to comment.