diff --git a/src/pg/brc20/brc20-pg-store.ts b/src/pg/brc20/brc20-pg-store.ts index a7751f0b..60300211 100644 --- a/src/pg/brc20/brc20-pg-store.ts +++ b/src/pg/brc20/brc20-pg-store.ts @@ -47,20 +47,27 @@ export class Brc20PgStore extends BasePgStoreModule { for (let blockHeight = startBlock; blockHeight <= endBlock; blockHeight++) { logger.info(`Brc20PgStore scanning block ${blockHeight}`); await this.sqlWriteTransaction(async sql => { - const cursor = sql` - SELECT - EXISTS(SELECT location_id FROM genesis_locations WHERE location_id = l.id) AS genesis, - l.id, l.inscription_id, l.block_height, l.tx_id, l.tx_index, l.address - FROM locations AS l - INNER JOIN inscriptions AS i ON l.inscription_id = i.id - WHERE l.block_height = ${blockHeight} - AND i.number >= 0 - AND i.mime_type IN ('application/json', 'text/plain') - ORDER BY tx_index ASC - `.cursor(100_000); - for await (const chunk of cursor) { - await this.insertOperations(chunk); - } + const limit = 100_000; + let offset = 0; + do { + const block = await sql` + SELECT + EXISTS(SELECT location_id FROM genesis_locations WHERE location_id = l.id) AS genesis, + l.id, l.inscription_id, l.block_height, l.tx_id, l.tx_index, l.address + FROM locations AS l + INNER JOIN inscriptions AS i ON l.inscription_id = i.id + WHERE l.block_height = ${blockHeight} + AND i.number >= 0 + AND i.mime_type IN ('application/json', 'text/plain') + ORDER BY tx_index ASC + LIMIT ${limit} + OFFSET ${offset} + `; + if (block.count === 0) break; + await this.insertOperations(block); + if (block.count < limit) break; + offset += limit; + } while (true); }); } }