Repository: rails/rails
File: activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb (Ruby)

Code before:
      end

      class StatementPool < ConnectionAdapters::StatementPool # :nodoc:
        def initialize(connection, max)
          super(max)
          @connection = connection
          @counter = 0
        end

        def next_key
          "a#{@counter += 1}"
        end

        private
          if PG::Connection.method_defined?(:close_prepared) # pg 1.6.0 & libpq 17
            def dealloc(key)
              # This is ugly, but safe: the statement pool is only
              # accessed while holding the connection's lock. (And we
              # don't need the complication of with_raw_connection because
              # a reconnect would invalidate the entire statement pool.)
              if conn = @connection.instance_variable_get(:@raw_connection)
                if @connection.supports_close_prepared?
                  conn.close_prepared key if conn.status == PG::CONNECTION_OK
                else
                  conn.query "DEALLOCATE #{key}" if conn.status == PG::CONNECTION_OK
                end
              end
            rescue PG::Error
            end
          else
            def dealloc(key)
              # This is ugly, but safe: the statement pool is only
              # accessed while holding the connection's lock. (And we
              # don't need the complication of with_raw_connection because
              # a reconnect would invalidate the entire statement pool.)
              if conn = @connection.instance_variable_get(:@raw_connection)
                conn.query "DEALLOCATE #{key}" if conn.status == PG::CONNECTION_OK
              end
            rescue PG::Error
            end
          end
      end

      # Initializes and connects a PostgreSQL adapter.
      def initialize(...)
        super

        conn_params = @config.compact

        # Map ActiveRecords param names to PGs.
        conn_params[:user] = conn_params.delete(:username) if conn_params[:username]

Review comment (suggestion):
Also wondering if `conn.status == PG::CONNECTION_OK` is still needed after https://github.com/rails/rails/pull/44527

Code after:
          false
        end
      end

      def index_algorithms
        { concurrently: "CONCURRENTLY" }
      end

      class StatementPool < ConnectionAdapters::StatementPool # :nodoc:
        def initialize(connection, max)
          super(max)
          @connection = connection
          @counter = 0
        end

        def next_key
          "a#{@counter += 1}"
        end

        private
          def dealloc(key)
            # This is ugly, but safe: the statement pool is only
            # accessed while holding the connection's lock. (And we
            # don't need the complication of with_raw_connection because
            # a reconnect would invalidate the entire statement pool.)
            if (conn = @connection.instance_variable_get(:@raw_connection)) && conn.status == PG::CONNECTION_OK
              if @connection.supports_close_prepared?
                conn.close_prepared key
              else
                conn.query "DEALLOCATE #{key}"
              end
            end
          rescue PG::Error
          end
      end

      # Initializes and connects a PostgreSQL adapter.
      def initialize(...)
        super

        conn_params = @config.compact

        # Map ActiveRecords param names to PGs.
        conn_params[:user] = conn_params.delete(:username) if conn_params[:username]
        conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database]

        # Forward only valid config params to PG::Connection.connect.
        valid_conn_param_keys = PG::Connection.conndefaults_hash.keys + [:requiressl]
        conn_params.slice!(*valid_conn_param_keys)

        @connection_parameters = conn_params