paymentsdb: add query to only fetch resolution type for HTLCs
What changed, and why it matters
This commit adds a new, more efficient database query that retrieves only the final resolution status of HTLC (payment attempt) records for a single payment, rather than fetching all attempt details. It is a straightforward performance and code-organization change with no visible security implications.
No security action required. Review as normal code-quality/performance change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces FetchHtlcAttemptResolutionsForPayment, a SQLC-generated query that selects only hr.resolution_type from payment_htlc_attempts left-joined with payment_htlc_attempt_resolutions for a given payment_id. It updates the generated Go code, the querier interface, and the SQL store interface. No existing behavior is modified; no input is newly exposed, no access control is changed, and no bug is fixed.
Changed components
payments/db/sql_store.gosqldb/sqlc/payments.sql.gosqldb/sqlc/querier.gosqldb/sqlc/queries/payments.sqlInspect captured patch +45 / −0
diff --git a/payments/db/sql_store.go b/payments/db/sql_store.go
index 4e494c8..d2500d6 100644
--- a/payments/db/sql_store.go
+++ b/payments/db/sql_store.go
@@ -49,6 +49,7 @@ type SQLQueries interface {
CountPayments(ctx context.Context) (int64, error)
FetchHtlcAttemptsForPayments(ctx context.Context, paymentIDs []int64) ([]sqlc.FetchHtlcAttemptsForPaymentsRow, error)
+ FetchHtlcAttemptResolutionsForPayment(ctx context.Context, paymentID int64) ([]sql.NullInt32, error)
FetchAllInflightAttempts(ctx context.Context) ([]sqlc.PaymentHtlcAttempt, error)
FetchHopsForAttempts(ctx context.Context, htlcAttemptIndices []int64) ([]sqlc.FetchHopsForAttemptsRow, error)
diff --git a/sqldb/sqlc/payments.sql.go b/sqldb/sqlc/payments.sql.go
index ae92aa1..dd135e3 100644
--- a/sqldb/sqlc/payments.sql.go
+++ b/sqldb/sqlc/payments.sql.go
@@ -242,6 +242,39 @@ func (q *Queries) FetchHopsForAttempts(ctx context.Context, htlcAttemptIndices [
return items, nil
}
+const fetchHtlcAttemptResolutionsForPayment = `-- name: FetchHtlcAttemptResolutionsForPayment :many
+SELECT
+ hr.resolution_type
+FROM payment_htlc_attempts ha
+LEFT JOIN payment_htlc_attempt_resolutions hr ON hr.attempt_index = ha.attempt_index
+WHERE ha.payment_id = $1
+ORDER BY ha.attempt_time ASC
+`
+
+// Lightweight query to fetch only HTLC resolution status.
+func (q *Queries) FetchHtlcAttemptResolutionsForPayment(ctx context.Context, paymentID int64) ([]sql.NullInt32, error) {
+ rows, err := q.db.QueryContext(ctx, fetchHtlcAttemptResolutionsForPayment, paymentID)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ var items []sql.NullInt32
+ for rows.Next() {
+ var resolution_type sql.NullInt32
+ if err := rows.Scan(&resolution_type); err != nil {
+ return nil, err
+ }
+ items = append(items, resolution_type)
+ }
+ if err := rows.Close(); err != nil {
+ return nil, err
+ }
+ if err := rows.Err(); err != nil {
+ return nil, err
+ }
+ return items, nil
+}
+
const fetchHtlcAttemptsForPayments = `-- name: FetchHtlcAttemptsForPayments :many
SELECT
ha.id,
diff --git a/sqldb/sqlc/querier.go b/sqldb/sqlc/querier.go
index d1605a0..008624a 100644
--- a/sqldb/sqlc/querier.go
+++ b/sqldb/sqlc/querier.go
@@ -38,6 +38,8 @@ type Querier interface {
FetchAllInflightAttempts(ctx context.Context) ([]PaymentHtlcAttempt, error)
FetchHopLevelCustomRecords(ctx context.Context, hopIds []int64) ([]PaymentHopCustomRecord, error)
FetchHopsForAttempts(ctx context.Context, htlcAttemptIndices []int64) ([]FetchHopsForAttemptsRow, error)
+ // Lightweight query to fetch only HTLC resolution status.
+ FetchHtlcAttemptResolutionsForPayment(ctx context.Context, paymentID int64) ([]sql.NullInt32, error)
FetchHtlcAttemptsForPayments(ctx context.Context, paymentIds []int64) ([]FetchHtlcAttemptsForPaymentsRow, error)
FetchPayment(ctx context.Context, paymentIdentifier []byte) (FetchPaymentRow, error)
FetchPaymentLevelFirstHopCustomRecords(ctx context.Context, paymentIds []int64) ([]PaymentFirstHopCustomRecord, error)
diff --git a/sqldb/sqlc/queries/payments.sql b/sqldb/sqlc/queries/payments.sql
index a70631a..b35919c 100644
--- a/sqldb/sqlc/queries/payments.sql
+++ b/sqldb/sqlc/queries/payments.sql
@@ -75,6 +75,15 @@ LEFT JOIN payment_htlc_attempt_resolutions hr ON hr.attempt_index = ha.attempt_i
WHERE ha.payment_id IN (sqlc.slice('payment_ids')/*SLICE:payment_ids*/)
ORDER BY ha.payment_id ASC, ha.attempt_time ASC;
+-- name: FetchHtlcAttemptResolutionsForPayment :many
+-- Lightweight query to fetch only HTLC resolution status.
+SELECT
+ hr.resolution_type
+FROM payment_htlc_attempts ha
+LEFT JOIN payment_htlc_attempt_resolutions hr ON hr.attempt_index = ha.attempt_index
+WHERE ha.payment_id = $1
+ORDER BY ha.attempt_time ASC;
+
-- name: FetchAllInflightAttempts :many
-- Fetch all inflight attempts across all payments
SELECT
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.