I am trying to turn on and turn off postgresql enable_nestloop for a single query in activerecord using raw SQL. Here is the command
class Segment < ApplicationRecord
def self.count_payload_kind
s = "
SET LOCAL enable_nestloop = off;
SELECT count(*)
FROM segments s
WHERE s.payload @> '[{\"kind\":\"person\"}]';
SET LOCAL enable_nestloop = on;
"
ActiveRecord::Base.connection.execute(s).to_a
end
end
When I call ActiveRecord::Base.connection.execute(s).to_a, it doesn't return any record and throws no error. However, it works correctly if i remove the 2 calls to SET LOCAL enable_nestloop
How can I turn enable_nestloop on and off for one query.