Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move pending blocks query out of recursive CTE #116

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/db/sql/events-actions/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ function fullChainCTE(db_client: postgres.Sql, from?: string, to?: string) {
INNER JOIN pending_chain ON b.id = pending_chain.parent_id
AND pending_chain.id <> pending_chain.parent_id
AND pending_chain.chain_status <> 'canonical'
WHERE 1=1
${
// If fromAsNum is not undefined, then we have also set toAsNum and can safely query the range
// If no params ar provided, then we query the last BLOCK_RANGE_SIZE blocks
fromAsNum
? db_client`AND b.height >= ${fromAsNum} AND b.height < ${toAsNum!}`
: db_client`AND b.height >= (
SELECT MAX(b2.height)
FROM blocks b2
) - ${BLOCK_RANGE_SIZE}`
}
),
full_chain AS (
SELECT
Expand All @@ -51,6 +40,17 @@ function fullChainCTE(db_client: postgres.Sql, from?: string, to?: string) {
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
FROM
pending_chain
WHERE 1=1
${
// If fromAsNum is not undefined, then we have also set toAsNum and can safely query the range
// If no params ar provided, then we query the last BLOCK_RANGE_SIZE blocks
fromAsNum
? db_client`AND height >= ${fromAsNum} AND height < ${toAsNum!}`
: db_client`AND height >= (
SELECT MAX(height)
FROM pending_chain
) - ${BLOCK_RANGE_SIZE}`
}
UNION ALL
SELECT
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
Expand Down