-
Notifications
You must be signed in to change notification settings - Fork 180
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
"SET CLIENT_ENCODING TO" called even though 'encoding' is not defined in database.yml #368
Comments
PG gem executes Lines 3937 to 3939 in 3cc3020
|
@larskanis I did check this condition as well and i've checked the value for |
@larskanis Ignore my earlier comment - the |
You could use the async API of ruby-pg as demonstrated in async_api.rb. That would require some monkey patching of ActiveRecord. On the other hand it's good practise to keep Can you describe why you try to get rid of this statement? |
@larskanis thank you for the suggestion and help. I'll check out the example for async API and also see how it can be patched and tested to see it doesn't have any side-effects on the encoding of the various language strings. Here's the context for why we are trying to get rid of this statement - We are using an AWS service called RDS Proxy, which is a database connection pooling proxy that takes away the responsibility of connection re-use from the application. As per their documentation, statements which involve a Ideally, they have an option to 'EXCLUDE_VARIABLE_SETS' from the pinning behaviour but unfortunately, setting this option has no effect and thus we are having to solve a problem of an external service in our application. Its not ideal but its something that we have to fix on our side, at least in the short-term until AWS comes back to us with a resolution. Thank you again for your reply. |
There's another issue in Rails that I thought I'd mention here - rails/rails#40207 - which is also tied to the 'pinning' behaviour where non-prepared statements are also being executed in an 'extended query' format. This is also something we are having to patch in our app to make sure the queries are 'simple' queries as we are not using prepared statements in our app. Thought this might be relevant to the discussion if anyone comes across here. |
@FrailWords Did you solve this issue? I am still stuck with the pinned session. How is the simple format related to removing the
|
@arkirchner I worked around this by monkey patching all activerecord functions that lead to a |
@FrailWords Thank you very much sharing. I wanted to use RDS Proxy as well. I not feeling to confident with monkey patching ActiveRecord and setting It looks like pgbouncer is more sensible in this area. It can keep track of some set statement and raises errors for others.
|
@arkirchner in my experience, I agree with your decision about using pgbouncer over RDS proxy - it offers much more control and has more documentation and more importantly, works as you'd expect it to without surprises. In my case, we completely removed the proxy altogether because ActiveRecord also does connection pooling and since my app was running on k8s, we could control number of connections via number of instances (internally then managed by ActiveRecord). Adding a proxy meant introducing another connection pool, the only benefit that comes from it is some 'headroom' in terms of 'idle' connections, which is also available at the ActiveRecord level by tweaking the 'idle timeout' parameter. Either way, be sure to evaluate whether you actually need a proxy or not as well. |
@FrailWords do you remember which functions that lead to the SET statement? I was tying to figure out how to solve this issue with RDS Proxy, and see how it behaves before moving out to other solution |
That is because:
|
It looks like this issue was effectively resolved in #542 / #543 and released in ruby-pg v1.5.4. With that change, the pg lib will not set the client encoding if it matches what is already set on the connection, which means this query can be prevented if the encoding is set upstream by the proxy (in RDS Proxy's "Initialization query" or pgbouncer's "connect_query"). |
I am using the following Rails setup -
In the
postgres_adapter.rb
file, if theencoding
is not set, theset_client_encoding
method is not called and I have verified this by stepping through the code base - https://github.com/rails/rails/blob/main/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L776and I have searched across the code base in rails or application and i do not see any other place where this method might be called from.
But still, when I try to run the application, there's a
SET CLIENT_ENCODING TO
query that happens on the initialization of every connection and since this is connection related, thats why I am posting it here for some guidance.The text was updated successfully, but these errors were encountered: