From f8b161f8fdc5cfc59a0cfd6f4ac5226a5cc6eb2c Mon Sep 17 00:00:00 2001 From: Gary Malouf <982483+gmalouf@users.noreply.github.com> Date: Mon, 6 Jan 2025 10:22:11 -0500 Subject: [PATCH] Convert before-time/after-time filters for transactions endpoint to rounds (major performance boost/avoid excessive query timeouts. (#1644) --- idb/postgres/postgres.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/idb/postgres/postgres.go b/idb/postgres/postgres.go index 0b8ff2e7..d1e88158 100644 --- a/idb/postgres/postgres.go +++ b/idb/postgres/postgres.go @@ -575,13 +575,15 @@ func buildTransactionQuery(tf idb.TransactionFilter) (query string, whereArgs [] } if !tf.BeforeTime.IsZero() { convertedTime := tf.BeforeTime.In(time.UTC) - whereParts = append(whereParts, fmt.Sprintf("h.realtime < $%d", partNumber)) + whereParts = append(whereParts, fmt.Sprintf("t.round <= ("+ + "SELECT round from block_header WHERE realtime < $%d ORDER BY realtime DESC LIMIT 1)", partNumber)) whereArgs = append(whereArgs, convertedTime) partNumber++ } if !tf.AfterTime.IsZero() { convertedTime := tf.AfterTime.In(time.UTC) - whereParts = append(whereParts, fmt.Sprintf("h.realtime > $%d", partNumber)) + whereParts = append(whereParts, fmt.Sprintf("t.round >= ("+ + "SELECT round from block_header WHERE realtime > $%d ORDER BY realtime ASC LIMIT 1)", partNumber)) whereArgs = append(whereArgs, convertedTime) partNumber++ }