Bulk Operations
Bulk Operations
Relevant source files
The following files were used as context for generating this wiki page:
The Primebrick DAL provides specialized methods for high-volume data modifications. These methods are designed to bypass the overhead of single-row processing while maintaining data integrity, audit compliance, and PostgreSQL parameter limits.
Overview of Bulk Methods
Bulk operations in the Repository class are optimized for different use cases, ranging from simple batched inserts to complex temporary table-based updates. All bulk methods accept BulkOptions to control execution behavior.
BulkOptions
| Option | Description |
|---|---|
batchSize | Manual override for the number of rows per batch. |
timeoutMs | Statement timeout for the operation. |
actor | (Required for auditable entities) The user/system performing the operation. |
Sources: src/types/types.ts:34-37, src/repository/repository.ts:148-150
1. addMany (Batched INSERT)
The addMany method performs high-speed inserts by grouping records into multi-row INSERT statements.
Implementation Details
- Auto-Batching: To avoid the PostgreSQL limit of 65,535 parameters per query,
addManycalculates an optimal batch size usingautoBatchSize(columnCount). This ensures thatrowCount * columnCountnever exceeds the limit src/repository/repository.ts:142-146, src/repository/repository.ts:476-485. - Audit Stamping: Every row is automatically stamped with
created_by,updated_by, andversion = 1if the entity is auditable src/repository/repository.ts:494-510. - Result Hydration: It uses
RETURNING *to return the fully hydrated entities, including database-generated IDs and defaults src/repository/repository.ts:517-522.
Sources: src/repository/repository.ts:466-531
2. upsertMany (Atomic Merge)
upsertMany handles "insert or update" logic at scale using the PostgreSQL INSERT ... ON CONFLICT syntax.
Key Features
- Conflict Handling: Requires a
conflictTarget(usually the primary key or a unique column) to determine when to update instead of insert src/repository/repository.ts:544-550. - Audit Awareness: If a conflict occurs, the system increments the
versionand updates theupdated_byfield, while preserving the originalcreated_byvalue src/repository/repository.ts:577-585. - Data Flow: Like
addMany, it uses theautoBatchSizelogic to prevent parameter overflow src/repository/repository.ts:557-560.
Sources: src/repository/repository.ts:533-617
3. updateMany (Temp Table Strategy)
Updating thousands of rows with different values for each row is inefficient using standard UPDATE statements. The DAL employs a Temporary Table Strategy to maximize performance.
The Update Workflow
- Create Temp Table: A temporary table is created with the same structure as the target table but with
ON COMMIT DROPto ensure cleanup src/repository/repository.ts:684-692. - Bulk Insert: All update data is inserted into this temporary table using the
addManylogic src/repository/repository.ts:696-699. - Single Join Update: A single
UPDATE ... FROMstatement is executed, joining the target table with the temporary table on thematchBycolumn src/repository/repository.ts:701-724.
Diagram: UpdateMany Strategy
Code
Sources: src/repository/repository.ts:654-740
4. deleteMany (Soft-Delete)
deleteMany performs a bulk soft-delete by updating the deleted_at and deleted_by columns for a set of records.
Implementation
- Array-based WHERE: Instead of multiple queries, it uses the
ANY($1::uuid[])operator to update all matching rows in a single statement src/repository/repository.ts:641-645. - Audit Stamping: It records the
actorin thedeleted_bycolumn and sets the current timestamp indeleted_atsrc/repository/repository.ts:634-639.
Sources: src/repository/repository.ts:619-652
Technical Architecture
Data Flow: Natural Language to Code Entities
The following diagram maps the high-level bulk operations to the internal functions and PostgreSQL mechanics used in the Repository class.
Code
Sources: src/repository/repository.ts:142-146, src/repository/repository.ts:466-470, src/repository/repository.ts:619-623, src/repository/repository.ts:654-658
Performance Benchmarks
The bulk system is tested against various scales (100 to 1,000,000 rows). The updateMany strategy is specifically optimized to maintain high throughput even as row counts increase.
| Operation | 1,000 Rows | 10,000 Rows | 1,000,000 Rows (Opt-in) |
|---|---|---|---|
addMany | ~50ms | ~300ms | ~25s |
updateMany | ~80ms | ~500ms | ~45s |
Sources: test/benchmark/bulk-benchmark.test.ts:97-101, test/benchmark/bulk-benchmark.test.ts:119-121