Skip to content

Batch WAL operations in stream mode#774

Merged
tsg merged 7 commits intomainfrom
batch_stream_operations
Apr 8, 2026
Merged

Batch WAL operations in stream mode#774
tsg merged 7 commits intomainfrom
batch_stream_operations

Conversation

@tsg
Copy link
Copy Markdown
Member

@tsg tsg commented Mar 12, 2026

Description

This is the implementation for the plan in #770

Before this PR, we were batching write operations against the target in snapshot mode, but not in stream mode. This adds batching also in stream mode, as a performance optimisation.

Related Issue(s)

Type of Change

Please select the relevant option(s):

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🔧 Refactoring (no functional changes)
  • ⚡ Performance improvement
  • 🧪 Test coverage improvement
  • 🔨 Build/CI changes
  • 🧹 Code cleanup

Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing performed
  • All existing tests pass

Checklist

  • Code follows project style guidelines
  • Self-review completed
  • Code is well-commented
  • Documentation updated where necessary

}
return w.dmlAdapter.buildBulkInsertQueries(events, run[0].schemaInfo), nil
default:
// UPDATE, TRUNCATE, and anything else: individual queries
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means we can still have the same type of issue with UPDATE ... WHERE ... statements that updates a lot of rows. Hopefully less common than the commit case.

@tsg tsg requested a review from kvch March 12, 2026 17:48
Comment thread pkg/wal/processor/postgres/postgres_wal_dml_adapter_bulk.go Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the Postgres stream-mode writer to batch/coalesce WAL DML operations (similar to snapshot mode) by deferring query building until batch flush time, enabling bulk SQL generation for higher throughput during catch-up.

Changes:

  • Introduce walMessage batching and a walEventToMessage adapter path to defer DML query construction until batch send time.
  • Add bulk/coalesced DML query builders for streaming (notably multi-row INSERT and coalesced DELETE).
  • Add unit tests for walMessage sizing/emptiness, bulk DML adapters, updated batch-writer behavior, plus new pg→pg integration tests for coalescing.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
pkg/wal/processor/postgres/postgres_writer.go Adds a batch-sender interface for walMessage batching.
pkg/wal/processor/postgres/postgres_wal_message.go Introduces walMessage wrapper with size estimation for batching.
pkg/wal/processor/postgres/postgres_wal_message_test.go Unit tests for walMessage IsEmpty and Size.
pkg/wal/processor/postgres/postgres_wal_dml_adapter_bulk.go Adds bulk INSERT and coalesced DELETE query builders used during streaming batch flush.
pkg/wal/processor/postgres/postgres_wal_dml_adapter_bulk_test.go Unit tests for bulk delete/insert query builders and parameter splitting.
pkg/wal/processor/postgres/postgres_wal_adapter.go Adds walEventToMessage for message-based batching.
pkg/wal/processor/postgres/postgres_wal_adapter_test.go Adds tests covering walEventToMessage behavior.
pkg/wal/processor/postgres/postgres_batch_writer.go Switches stream batch writer to batch walMessage and coalesce consecutive same-table DML.
pkg/wal/processor/postgres/postgres_batch_writer_test.go Updates tests to match message-based batching and validates coalescing behavior.
pkg/wal/processor/postgres/instrumented_wal_adapter.go Instruments walEventToMessage with OTel spans.
pkg/wal/processor/postgres/helper_test.go Extends mocks to support walEventToMessage.
pkg/stream/integration/pg_pg_batch_coalesce_integration_test.go New integration tests validating coalescing for inserts/deletes and composite keys.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/wal/processor/postgres/postgres_wal_dml_adapter_bulk.go
Comment thread pkg/wal/processor/postgres/postgres_wal_dml_adapter_bulk.go
Comment thread pkg/wal/processor/postgres/postgres_wal_dml_adapter_bulk.go
Comment thread pkg/wal/processor/postgres/postgres_wal_dml_adapter_bulk.go Outdated
Comment thread pkg/wal/processor/postgres/postgres_batch_writer.go
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2026

Merging this branch will increase overall coverage

Impacted Packages Coverage Δ 🤖
github.com/xataio/pgstream/pkg/stream/integration 0.00% (ø)
github.com/xataio/pgstream/pkg/wal/processor/postgres 80.43% (+1.54%) 👍

Coverage by file

Changed files (no unit tests)

Changed File Coverage Δ Total Covered Missed 🤖
github.com/xataio/pgstream/pkg/wal/processor/postgres/instrumented_wal_adapter.go 0.00% (ø) 10 (+3) 0 10 (+3)
github.com/xataio/pgstream/pkg/wal/processor/postgres/postgres_batch_writer.go 74.42% (-4.85%) 129 (+47) 96 (+31) 33 (+16) 👎
github.com/xataio/pgstream/pkg/wal/processor/postgres/postgres_wal_adapter.go 73.91% (+13.91%) 46 (+16) 34 (+16) 12 🎉
github.com/xataio/pgstream/pkg/wal/processor/postgres/postgres_wal_dml_adapter.go 94.74% (ø) 152 144 8
github.com/xataio/pgstream/pkg/wal/processor/postgres/postgres_wal_dml_adapter_bulk.go 90.77% (+90.77%) 130 (+130) 118 (+118) 12 (+12) 🌟
github.com/xataio/pgstream/pkg/wal/processor/postgres/postgres_wal_message.go 85.71% (+85.71%) 14 (+14) 12 (+12) 2 (+2) 🌟
github.com/xataio/pgstream/pkg/wal/processor/postgres/postgres_writer.go 37.14% (ø) 35 13 22

Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code.

Changed unit test files

  • github.com/xataio/pgstream/pkg/stream/integration/pg_pg_batch_coalesce_integration_test.go
  • github.com/xataio/pgstream/pkg/wal/processor/postgres/helper_test.go
  • github.com/xataio/pgstream/pkg/wal/processor/postgres/postgres_batch_writer_test.go
  • github.com/xataio/pgstream/pkg/wal/processor/postgres/postgres_wal_adapter_test.go
  • github.com/xataio/pgstream/pkg/wal/processor/postgres/postgres_wal_dml_adapter_bulk_test.go
  • github.com/xataio/pgstream/pkg/wal/processor/postgres/postgres_wal_dml_adapter_test.go
  • github.com/xataio/pgstream/pkg/wal/processor/postgres/postgres_wal_message_test.go

@tsg tsg merged commit 59a723d into main Apr 8, 2026
7 checks passed
@tsg tsg deleted the batch_stream_operations branch April 8, 2026 10:34
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.

WAL streaming batch writer is slow for high-churn tables during catch-up

3 participants