Avoid sending client encoding query at startup if not necessary #542
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We are using a Postgres proxy with ruby-pg that routes queries based on the presence of a comment, we want to add that routing comment to all queries, we are using Rails so we have control over the queries sent by ActiveRecord but we cannot control queries triggered by
ruby-pg
. The query in question is the query triggered bypgconn_set_default_encoding
.Now, there are a couple of ways to get rid of this startup query
Encoding.default_internal
set tonil
but that is something we cannot really do because it is set by Rails and it does not seem safe to unset a value set by Rails core.set_default_encoding
by monkeypatching the method in Ruby land. This can only be done in versions after the introduction of the async connect apis. However, doing this will bypass a call to the all-so-important methodpgconn_set_internal_encoding_index
which is not exposed to Ruby land code so we cannot call itI think this query can be eliminated by checking the client encoding on the connection and comparing that against the Ruby encoding. Checking the client encoding on the connection is an offline operation.
I was able to test this with connections that had client encoding set in the url or those that are not but I am not sure where I should add tests for this in this repo