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

Avoid sending client encoding query at startup if not necessary #542

Conversation

drdrsh
Copy link
Contributor

@drdrsh drdrsh commented Aug 30, 2023

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 by pgconn_set_default_encoding.

Now, there are a couple of ways to get rid of this startup query

  1. Leaving Encoding.default_internal set to nil 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.
  2. Disable 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 method pgconn_set_internal_encoding_index which is not exposed to Ruby land code so we cannot call it

I 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

@drdrsh drdrsh marked this pull request as ready for review August 30, 2023 23:11
larskanis added a commit to larskanis/ruby-pg that referenced this pull request Aug 31, 2023
This is important for connection proxies, who disallow configuration settings.

Closes ged#542
larskanis added a commit to larskanis/ruby-pg that referenced this pull request Aug 31, 2023
This is important for connection proxies, who disallow configuration settings.

Closes ged#542
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant