Skip to content

Commit 9565918

Browse files
authoredMar 10, 2025
move pending blocks query out of recursive CTE (#116)
Follow on to #115 ..... This PR moves the query our of the recursive CTE and into the full chain union query. The height referenced in the recursive CTE is not usable as I was trying previously. Example screenshots from the old code: As you can see, it is possible to query the right event, but the height filter is not the expected number. ![image](https://github.com/user-attachments/assets/6132de6f-79d9-4607-9a0c-9918ecc5bda7) ![image](https://github.com/user-attachments/assets/2689338b-d5c4-494f-8005-e7a84b08f9b2) New code: As you see, the specific query now selects the correct actions. ![image](https://github.com/user-attachments/assets/5b307d30-2b3e-42e7-af1f-917e6fed700b)
1 parent 0473534 commit 9565918

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed
 

‎src/db/sql/events-actions/queries.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ function fullChainCTE(db_client: postgres.Sql, from?: string, to?: string) {
3030
INNER JOIN pending_chain ON b.id = pending_chain.parent_id
3131
AND pending_chain.id <> pending_chain.parent_id
3232
AND pending_chain.chain_status <> 'canonical'
33-
WHERE 1=1
34-
${
35-
// If fromAsNum is not undefined, then we have also set toAsNum and can safely query the range
36-
// If no params ar provided, then we query the last BLOCK_RANGE_SIZE blocks
37-
fromAsNum
38-
? db_client`AND b.height >= ${fromAsNum} AND b.height < ${toAsNum!}`
39-
: db_client`AND b.height >= (
40-
SELECT MAX(b2.height)
41-
FROM blocks b2
42-
) - ${BLOCK_RANGE_SIZE}`
43-
}
4433
),
4534
full_chain AS (
4635
SELECT
@@ -51,6 +40,17 @@ function fullChainCTE(db_client: postgres.Sql, from?: string, to?: string) {
5140
id, state_hash, parent_id, parent_hash, height, global_slot_since_genesis, global_slot_since_hard_fork, timestamp, chain_status, ledger_hash, last_vrf_output
5241
FROM
5342
pending_chain
43+
WHERE 1=1
44+
${
45+
// If fromAsNum is not undefined, then we have also set toAsNum and can safely query the range
46+
// If no params ar provided, then we query the last BLOCK_RANGE_SIZE blocks
47+
fromAsNum
48+
? db_client`AND height >= ${fromAsNum} AND height < ${toAsNum!}`
49+
: db_client`AND height >= (
50+
SELECT MAX(height)
51+
FROM pending_chain
52+
) - ${BLOCK_RANGE_SIZE}`
53+
}
5454
UNION ALL
5555
SELECT
5656
id, state_hash, parent_id, parent_hash, height, global_slot_since_genesis, global_slot_since_hard_fork, timestamp, chain_status, ledger_hash, last_vrf_output

0 commit comments

Comments
 (0)