From 20b284fa381041fb842bf61d8a184be6ea84810f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20C=C3=A1rdenas?= Date: Tue, 20 Sep 2022 16:18:20 -0500 Subject: [PATCH] fix: refresh materialized views concurrently in new pg format (#1324) --- src/datastore/pg-write-store.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/datastore/pg-write-store.ts b/src/datastore/pg-write-store.ts index 67243ee2..9ee94495 100644 --- a/src/datastore/pg-write-store.ts +++ b/src/datastore/pg-write-store.ts @@ -134,9 +134,10 @@ export class PgWriteStore extends PgStore { } async getChainTip( - sql: PgSqlClient + sql: PgSqlClient, + useMaterializedView = true ): Promise<{ blockHeight: number; blockHash: string; indexBlockHash: string }> { - if (!this.isEventReplay) { + if (!this.isEventReplay && useMaterializedView) { return super.getChainTip(sql); } // The `chain_tip` materialized view is not available during event replay. @@ -188,7 +189,7 @@ export class PgWriteStore extends PgStore { const tokenMetadataQueueEntries: DbTokenMetadataQueueEntry[] = []; let garbageCollectedMempoolTxs: string[] = []; await this.sql.begin(async sql => { - const chainTip = await this.getChainTip(sql); + const chainTip = await this.getChainTip(sql, false); await this.handleReorg(sql, data.block, chainTip.blockHeight); // If the incoming block is not of greater height than current chain tip, then store data as non-canonical. const isCanonical = data.block.block_height > chainTip.blockHeight; @@ -532,7 +533,7 @@ export class PgWriteStore extends PgStore { // Sanity check: ensure incoming microblocks have a `parent_index_block_hash` that matches the API's // current known canonical chain tip. We assume this holds true so incoming microblock data is always // treated as being built off the current canonical anchor block. - const chainTip = await this.getChainTip(sql); + const chainTip = await this.getChainTip(sql, false); const nonCanonicalMicroblock = data.microblocks.find( mb => mb.parent_index_block_hash !== chainTip.indexBlockHash ); @@ -1270,7 +1271,7 @@ export class PgWriteStore extends PgStore { async updateMempoolTxs({ mempoolTxs: txs }: { mempoolTxs: DbMempoolTx[] }): Promise { const updatedTxs: DbMempoolTx[] = []; await this.sql.begin(async sql => { - const chainTip = await this.getChainTip(sql); + const chainTip = await this.getChainTip(sql, false); for (const tx of txs) { const values: MempoolTxInsertValues = { pruned: tx.pruned, @@ -2441,7 +2442,7 @@ export class PgWriteStore extends PgStore { if (this.isEventReplay && skipDuringEventReplay) { return; } - await sql`REFRESH MATERIALIZED VIEW ${sql(viewName)}`; + await sql`REFRESH MATERIALIZED VIEW ${isProdEnv ? sql`CONCURRENTLY` : sql``} ${sql(viewName)}`; } /**