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

Don't set the server's client_encoding if it's unnecessary #543

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions ext/pg_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -4171,17 +4171,23 @@ static VALUE
pgconn_set_default_encoding( VALUE self )
{
PGconn *conn = pg_get_pgconn( self );
rb_encoding *enc;
const char *encname;
rb_encoding *rb_enc;

rb_check_frozen(self);
if (( enc = rb_default_internal_encoding() )) {
encname = pg_get_rb_encoding_as_pg_encoding( enc );
if ( pgconn_set_client_encoding_async(self, rb_str_new_cstr(encname)) != 0 )
rb_warning( "Failed to set the default_internal encoding to %s: '%s'",
encname, PQerrorMessage(conn) );
if (( rb_enc = rb_default_internal_encoding() )) {
rb_encoding * conn_encoding = pg_conn_enc_get( conn );

/* Don't set the server encoding, if it's unnecessary.
* This is important for connection proxies, who disallow configuration settings.
*/
if ( conn_encoding != rb_enc ) {
const char *encname = pg_get_rb_encoding_as_pg_encoding( rb_enc );
if ( pgconn_set_client_encoding_async(self, rb_str_new_cstr(encname)) != 0 )
rb_warning( "Failed to set the default_internal encoding to %s: '%s'",
encname, PQerrorMessage(conn) );
}
pgconn_set_internal_encoding_index( self );
return rb_enc_from_encoding( enc );
return rb_enc_from_encoding( rb_enc );
} else {
pgconn_set_internal_encoding_index( self );
return Qnil;
Expand Down
16 changes: 16 additions & 0 deletions spec/pg/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2368,6 +2368,22 @@ def wait_check_socket(conn)
end
end

it "doesn't change anything if Encoding.default_internal it set to DB default encoding", :without_transaction do
begin
prev_encoding = Encoding.default_internal
Encoding.default_internal = Encoding::UTF_8

# PG.connect shouldn't emit a "set client_encoding" for UTF_8, since the server is already on UTF8.
conn = PG.connect( @conninfo )
expect( conn.internal_encoding ).to eq( Encoding::UTF_8 )
res = conn.exec( "SELECT setting, source FROM pg_settings WHERE name='client_encoding'" )
expect( res[0].values ).to eq( ['UTF8', 'default'] )
ensure
conn.finish if conn
Encoding.default_internal = prev_encoding
end
end

it "allows users of the async interface to set the client_encoding to the default_internal" do
begin
prev_encoding = Encoding.default_internal
Expand Down