sqldb: avoid materializing non-terminal payments

(cherry picked from commit 02daf8e96a)
This commit is contained in:
ziggie 2026-05-27 15:30:11 -03:00 committed by github-actions[bot]
parent 653e173b03
commit e895d0f2a4
2 changed files with 48 additions and 48 deletions

View file

@ -392,27 +392,6 @@ func (q *Queries) FetchHtlcAttemptsForPayments(ctx context.Context, paymentIds [
}
const fetchNonTerminalPayments = `-- name: FetchNonTerminalPayments :many
WITH non_terminal_ids AS (
SELECT p.id
FROM payments p
WHERE p.fail_reason IS NULL
AND NOT EXISTS (
SELECT 1 FROM payment_htlc_attempt_resolutions hr
JOIN payment_htlc_attempts ha
ON ha.attempt_index = hr.attempt_index
WHERE ha.payment_id = p.id
AND hr.resolution_type = 1
)
UNION
SELECT DISTINCT ha.payment_id AS id
FROM payment_htlc_attempts ha
WHERE NOT EXISTS (
SELECT 1 FROM payment_htlc_attempt_resolutions hr
WHERE hr.attempt_index = ha.attempt_index
)
)
SELECT
p.id,
p.amount_msat,
@ -421,12 +400,33 @@ SELECT
p.fail_reason,
pi.intent_type,
pi.intent_payload
FROM non_terminal_ids n
JOIN payments p
ON p.id = n.id
FROM payments p
LEFT JOIN payment_intents pi
ON pi.payment_id = p.id
WHERE p.id > $1
AND (
(
p.fail_reason IS NULL
AND NOT EXISTS (
SELECT 1
FROM payment_htlc_attempts ha
JOIN payment_htlc_attempt_resolutions hr
ON hr.attempt_index = ha.attempt_index
WHERE ha.payment_id = p.id
AND hr.resolution_type = 1
)
)
OR EXISTS (
SELECT 1
FROM payment_htlc_attempts ha
WHERE ha.payment_id = p.id
AND NOT EXISTS (
SELECT 1
FROM payment_htlc_attempt_resolutions hr
WHERE hr.attempt_index = ha.attempt_index
)
)
)
ORDER BY p.id ASC
LIMIT $2
`

View file

@ -129,27 +129,6 @@ ORDER BY p.id ASC;
-- Fetch all non-terminal payments using pagination. A payment is
-- non-terminal if it has an unresolved attempt, or if it has not been
-- permanently failed and has no settled attempt yet.
WITH non_terminal_ids AS (
SELECT p.id
FROM payments p
WHERE p.fail_reason IS NULL
AND NOT EXISTS (
SELECT 1 FROM payment_htlc_attempt_resolutions hr
JOIN payment_htlc_attempts ha
ON ha.attempt_index = hr.attempt_index
WHERE ha.payment_id = p.id
AND hr.resolution_type = 1
)
UNION
SELECT DISTINCT ha.payment_id AS id
FROM payment_htlc_attempts ha
WHERE NOT EXISTS (
SELECT 1 FROM payment_htlc_attempt_resolutions hr
WHERE hr.attempt_index = ha.attempt_index
)
)
SELECT
p.id,
p.amount_msat,
@ -158,12 +137,33 @@ SELECT
p.fail_reason,
pi.intent_type,
pi.intent_payload
FROM non_terminal_ids n
JOIN payments p
ON p.id = n.id
FROM payments p
LEFT JOIN payment_intents pi
ON pi.payment_id = p.id
WHERE p.id > $1
AND (
(
p.fail_reason IS NULL
AND NOT EXISTS (
SELECT 1
FROM payment_htlc_attempts ha
JOIN payment_htlc_attempt_resolutions hr
ON hr.attempt_index = ha.attempt_index
WHERE ha.payment_id = p.id
AND hr.resolution_type = 1
)
)
OR EXISTS (
SELECT 1
FROM payment_htlc_attempts ha
WHERE ha.payment_id = p.id
AND NOT EXISTS (
SELECT 1
FROM payment_htlc_attempt_resolutions hr
WHERE hr.attempt_index = ha.attempt_index
)
)
)
ORDER BY p.id ASC
LIMIT $2;