Skip to content

Commit

Permalink
fix: filter subquery
Browse files Browse the repository at this point in the history
Signed-off-by: Clément Salaün <[email protected]>
  • Loading branch information
altitude committed Feb 5, 2024
1 parent b37dff6 commit 2daaad0
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions pkg/storage/sqlstorage/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,26 @@ func (s *Store) buildTransactionsQuery(flavor Flavor, p ledger.TransactionsQuery
t.StartTime = startTime
}
if !endTime.IsZero() {
// while we narrow down the query in the next step, we still
// need to strictly filter the resuling set by the end time
sb.Where(sb.L("timestamp", endTime.UTC()))
sub := sqlbuilder.NewSelectBuilder()
sb.Where(
sb.In(
"id",
sub.Select("txid").
From(s.schema.Table("accounts_checkpoints")).
Where(sub.LE("last_tx_at", endTime.UTC())).
OrderBy("last_tx_at").Desc(),
),
)
t.EndTime = endTime

if flavor == PostgreSQL {
// use checkpoints table to nudge the query planner in the right direction
// we have to use a raw SQL query here because the go-sqlbuilder library
// does not support subqueries in the WHERE clause
sb.SQL(fmt.Sprintf(
`AND id <= (
SELECT txid
FROM "%s".accounts_checkpoints
WHERE last_tx_at <= '%s'::timestamptz
ORDER BY last_tx_at
DESC LIMIT 1
)`,
s.schema.Name(), endTime.UTC().Format(time.RFC3339),
))
}
}

for key, value := range metadata {
Expand Down

0 comments on commit 2daaad0

Please sign in to comment.