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

fix: parallelize re-org update queries #1835

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
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
21 changes: 20 additions & 1 deletion src/datastore/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseEnum, unwrapOptionalProp } from '../helpers';
import { getUintEnvOrDefault, parseEnum, unwrapOptionalProp } from '../helpers';
import {
BlockQueryResult,
ContractTxQueryResult,
Expand Down Expand Up @@ -66,6 +66,7 @@ import { PgStoreEventEmitter } from './pg-store-event-emitter';
import { SyntheticPoxEventName } from '../pox-helpers';
import { logger } from '../logger';
import { PgSqlClient } from '@hirosystems/api-toolkit';
import PQueue from 'p-queue';

export const TX_COLUMNS = [
'tx_id',
Expand Down Expand Up @@ -1335,3 +1336,21 @@ export function newReOrgUpdatedEntities(): ReOrgUpdatedEntities {
restoredMempoolTxs: 0,
};
}

/**
* Priority queue for parallel Postgres write query execution. This helps performance because it
* parallelizes the work postgres.js has to do when serializing JS types to PG types.
*/
export class PgWriteQueue {
readonly queue: PQueue;
constructor() {
const concurrency = Math.max(1, getUintEnvOrDefault('STACKS_BLOCK_DATA_INSERT_CONCURRENCY', 4));
this.queue = new PQueue({ concurrency, autoStart: true });
}
enqueue(task: Parameters<PQueue['add']>[0]): void {
void this.queue.add(task);
}
done(): Promise<void> {
return this.queue.onIdle();
}
}
Loading
Loading