Skip to content

perf: switch write_blocks to COPY BINARY#128

Merged
kamsz merged 1 commit intomainfrom
kamil/blocks-copy-binary
Mar 20, 2026
Merged

perf: switch write_blocks to COPY BINARY#128
kamsz merged 1 commit intomainfrom
kamil/blocks-copy-binary

Conversation

@decofe
Copy link
Copy Markdown
Member

@decofe decofe commented Mar 20, 2026

Replaces dynamic multi-row INSERT with VALUES ($1...$N*9) with COPY BINARY via a staging temp table.

  • COPY BINARY into _staging_blocks temp table (dropped on commit)
  • INSERT INTO blocks SELECT * FROM _staging_blocks ON CONFLICT (timestamp, num) DO NOTHING
  • Removes std::fmt::Write import (no longer needed)

Gives PG a stable execution plan and avoids O(N) query string formatting. Consistent with write_txs/write_logs/write_receipts.

Prompted by: kamil

- write_blocks: replace manual BEGIN/COMMIT with conn.transaction()
- write_txs/write_logs/write_receipts: wrap DELETE+COPY in a transaction
- SET statement_timeout -> SET LOCAL (scoped to transaction, not connection)

Prevents pool poisoning on error/cancellation and makes DELETE+COPY atomic.

Co-Authored-By: Kamil Szczygieł <2961450+kamsz@users.noreply.github.com>
Amp-Thread-ID: https://ampcode.com/threads/T-019d0c20-7f67-770d-9b18-050510968357
@decofe decofe force-pushed the kamil/blocks-copy-binary branch from 97d6f31 to d8040cd Compare March 20, 2026 17:05
@kamsz kamsz merged commit 11ddef4 into main Mar 20, 2026
1 check passed
o-az pushed a commit that referenced this pull request Apr 27, 2026
- write_blocks: replace manual BEGIN/COMMIT with conn.transaction()
- write_txs/write_logs/write_receipts: wrap DELETE+COPY in a transaction
- SET statement_timeout -> SET LOCAL (scoped to transaction, not connection)

Prevents pool poisoning on error/cancellation and makes DELETE+COPY atomic.


Amp-Thread-ID: https://ampcode.com/threads/T-019d0c20-7f67-770d-9b18-050510968357

Co-authored-by: Kamil Szczygieł <2961450+kamsz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants