def query(self, sql: str, *params: Any) -> List[Tuple[Any, ...]]:
        """Execute *sql* query with *params* and optionally return results.

        .. note::
            Prefer to use own connection to postgres and fallback to ``heartbeat`` when own isn't available.

        :param sql: the SQL statement to be run.
        :param params: positional arguments to be used as parameters for *sql*.

        :returns: a list of rows that were fetched from the database.

        :raises:
            :class:`psycopg.Error`: if had issues while executing *sql*.
            :class:`~patroni.exceptions.PostgresConnectionException`: if had issues while connecting to the database.
        """
        # We first try to get a heartbeat connection because it is always required for the main thread.
        try:
            heartbeat_connection = self.patroni.postgresql.connection_pool.get('heartbeat')
            heartbeat_connection.get()  # try to open psycopg connection to postgres
        except psycopg.Error as exc:
            raise PostgresConnectionException('connection problems') from exc

        try:
            connection = self.patroni.postgresql.connection_pool.get('restapi')
            connection.get()  # try to open psycopg connection to postgres
        except psycopg.Error:
            logger.debug('restapi connection to postgres is not available')
            connection = heartbeat_connection

        return connection.query(sql, *params)
Execute *sql* query with *params* and optionally return results.

        .. note::
            Prefer to use own connection to postgres and fallback to ``heartbeat`` when own isn't available.

        :param sql: the SQL statement to be run.
        :param params: positional arguments to be used as parameters for *sql*.

        :returns: a list of rows that were fetched from the database.

        :raises:
            :class:`psycopg.Error`: if had issues while executing *sql*.
            :class:`~patroni.exceptions.PostgresConnectionException`: if had issues while connecting to the database.
        
patroni/api.py