Repository: MystenLabs/sui
File: crates/sui-indexer-alt-reader/src/pg_reader.rs (Rust)

Code before:
        <Q::Output as Query>::SqlType: CompatibleType<U, Pg, SqlType = ST>,
        U: Send + FromSqlRow<ST, Pg> + 'static,
        Pg: QueryMetadata<<Q::Output as Query>::SqlType>,
        ST: 'static,
    {
        let query = query.limit(1);

        let query_debug = diesel::debug_query(&query).to_string();
        debug!("{query_debug}");

        self.metrics.requests_received.inc();
        let _guard = self.metrics.latency.start_timer();

        match query.get_result(&mut self.conn).await {
            Ok(results) => {
                self.metrics.requests_succeeded.inc();
                Ok(results)
            }
            Err(err) => {
                self.metrics.requests_failed.inc();
                let message = if is_timeout(&err) {
                    format!("First query timed out conn_id={}", self.conn.id)
                } else {
                    format!("First query error conn_id={}", self.conn.id)
                };
                Err(err).context(message)
            }
        }
    }

    pub async fn results<'q, Q, ST, U>(&mut self, query: Q) -> anyhow::Result<Vec<U>>
    where
        Q: Query + QueryFragment<Pg> + QueryId + Send + 'q,
        Q::SqlType: CompatibleType<U, Pg, SqlType = ST>,
        U: Send + FromSqlRow<ST, Pg> + 'static,
        Pg: QueryMetadata<Q::SqlType>,
        ST: 'static,
    {
        let query_debug = diesel::debug_query(&query).to_string();
        debug!("{query_debug}");

        self.metrics.requests_received.inc();
        let _guard = self.metrics.latency.start_timer();

        match query.get_results(&mut self.conn).await {
            Ok(results) => {
                self.metrics.requests_succeeded.inc();
                Ok(results)
            }
            Err(err) => {
                self.metrics.requests_failed.inc();

Review comment (bug):
Do you need to propagate this information into the error that gets returned to the client, or is it sufficient to log a `warn!` message here with the ID? We can also include the full query in the output because we still have access to `query_debug` from above.

Code after:
    pub async fn first<'q, Q, ST, U>(&mut self, query: Q) -> anyhow::Result<U>
    where
        Q: LimitDsl,
        Q::Output: Query + QueryFragment<Pg> + QueryId + Send + 'q,
        <Q::Output as Query>::SqlType: CompatibleType<U, Pg, SqlType = ST>,
        U: Send + FromSqlRow<ST, Pg> + 'static,
        Pg: QueryMetadata<<Q::Output as Query>::SqlType>,
        ST: 'static,
    {
        let query = query.limit(1);

        self.metrics.requests_received.inc();
        let _guard = self.metrics.latency.start_timer();

        let pid = self.conn.pid;
        let query_debug = diesel::debug_query(&query).to_string();
        match query.get_result(&mut self.conn).await {
            Ok(results) => {
                self.metrics.requests_succeeded.inc();
                debug!(pid, "{query_debug}");
                Ok(results)
            }
            Err(err) => {
                self.metrics.requests_failed.inc();
                if is_timeout(&err) {
                    warn!(pid, "Timed out: {query_debug}");
                } else {
                    warn!(pid, "Failed with '{err:?}': {query_debug}");
                };
                Err(err).with_context(|| format!("First error from DB request pid={pid}"))
            }
        }
    }

    pub async fn results<'q, Q, ST, U>(&mut self, query: Q) -> anyhow::Result<Vec<U>>
    where
        Q: Query + QueryFragment<Pg> + QueryId + Send + 'q,
        Q::SqlType: CompatibleType<U, Pg, SqlType = ST>,
        U: Send + FromSqlRow<ST, Pg> + 'static,
        Pg: QueryMetadata<Q::SqlType>,
        ST: 'static,
    {
        self.metrics.requests_received.inc();
        let _guard = self.metrics.latency.start_timer();

        let pid = self.conn.pid;
        let query_debug = diesel::debug_query(&query).to_string();
        match query.get_results(&mut self.conn).await {
            Ok(results) => {
                self.metrics.requests_succeeded.inc();
                debug!(pid, "{query_debug}");