FIX: PostgreSQL fallback was broken due to Rails masking exception (PR #9633)

The PR https://github.com/rails/rails/pull/36612 changes the raised exception if the error message includes the target database name.

Since the error message contains the hostname, this could be triggered when the hostname contains the database name.

GitHub

This is something I’ve observed on our dev DCs; I’ve accidentally triggered it by stopping the master pgbouncer.

Some more information about what exact errors are returned under various conditions:

postgres down:

[84] pry(main)> ActiveRecord::Base.postgresql_connection(ActiveRecord::Base.connection_config)
PG::ConnectionBad: ERROR:  pgbouncer cannot connect to server

pgbouncer down:

[85] pry(main)> ActiveRecord::Base.postgresql_connection(ActiveRecord::Base.connection_config)
ActiveRecord::NoDatabaseError: could not connect to server: Connection refused
	Is the server running on host "cluster-pgbouncer-master" (fd2f:111:2d2f:61:0:2d4:c4de:d9e9) and accepting
	TCP/IP connections on port 6432?

from /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:50:in `rescue in postgresql_connection'
Caused by PG::ConnectionBad: could not connect to server: Connection refused
	Is the server running on host "cluster-pgbouncer-master" (fd2f:111:2d2f:61:0:2d4:c4de:d9e9) and accepting
	TCP/IP connections on port 6432?

from /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/pg-1.2.3/lib/pg.rb:58:in `initialize'

root container down:

[89] pry(main)> ActiveRecord::Base.postgresql_connection(ActiveRecord::Base.connection_config)
PG::ConnectionBad: timeout expired

from /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/pg-1.2.3/lib/pg.rb:58:in `initialize'

or

[91] pry(main)> ActiveRecord::Base.postgresql_connection(ActiveRecord::Base.connection_config)
ActiveRecord::NoDatabaseError: could not connect to server: No route to host
	Is the server running on host "cluster-pgbouncer-master" (fd2f:111:2d2f:61:0:2d4:c4de:d9e9) and accepting
	TCP/IP connections on port 6432?

from /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:50:in `rescue in postgresql_connection'
Caused by PG::ConnectionBad: could not connect to server: No route to host
	Is the server running on host "cluster-pgbouncer-master" (fd2f:111:2d2f:61:0:2d4:c4de:d9e9) and accepting
	TCP/IP connections on port 6432?

looks good to me!

1 Like