Skip to content

perf(trace): reduce SQLFlusher inline SQL allocation - #25540

Merged
mergify[bot] merged 4 commits into
matrixorigin:mainfrom
VioletQwQ-0:violet/issue-25505-sqlflusher
Jul 10, 2026
Merged

mergify[bot] merged 4 commits into
matrixorigin:mainfrom
VioletQwQ-0:violet/issue-25505-sqlflusher

Conversation

@VioletQwQ-0

@VioletQwQ-0 VioletQwQ-0 commented Jul 8, 2026 •

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #25505

What this PR does / why we need it:

SQLFlusher.FlushBuffer used to allocate a fixed table.DefaultWriterBufferSize 10MB intermediate buffer for every trace/ETL SQL flush before building the final LOAD DATA INLINE statement. Under high-concurrency sysbench workloads this became a large allocation amplifier even when the actual CSV batch was much smaller than 10MB.

This PR builds the final inline SQL directly with strings.Builder and sizes it from the actual batch length plus the number of bytes that need SQL escaping. This removes the fixed 10MB intermediate buffer and avoids the extra fmt.Sprintf copy.

Changes:

  • Replace the fixed 10MB sqlCsv buffer in SQLFlusher.FlushBuffer with an exactly-sized strings.Builder for the final SQL.
  • Preserve the existing escape behavior for \ and '.
  • Add a regression test for inline SQL escaping.

Validation:

  • go test ./pkg/util/export/etl/...
  • mo-srv-129 clean point_select validation, 1000vu, --db-ps-mode=auto, 8GB SHARED cache, fresh sysbench_ps_sqlflusher_clean database:
metric clean baseline patched run 1 patched run 2
TPS 150,941/s 169,974/s 161,550/s
avg latency 6.61ms 5.87ms 6.18ms
p95 latency 27.17ms 31.94ms 27.17ms
CPU samples / 30s 1602.14s (~53.4 cores) 1620.27s (~54.0 cores) 1604.88s (~53.5 cores)
heap alloc_space total 4.62TB 506.39GB 1019.38GB
SQLFlusher.FlushBuffer 223.18GB flat / 225.06GB cum 0 flat / 30.54MB cum 0 flat / 77.48MB cum

Note: Go heap alloc_space is cumulative since process start. Patched run 2 reused the same patched MO process after run 1, so its heap total includes prior allocations. It is included as a stability check that SQLFlusher.FlushBuffer remains MB-level after another 1000vu run.

Profile paths:

  • baseline: mo-srv-129:/data/mo-prof/pprof/1000vu_clean_auto/
  • patched run 1: mo-srv-129:/data/mo-prof/pprof/sqlflusher_clean_auto_1000vu/
  • patched run 2: mo-srv-129:/data/mo-prof/pprof/sqlflusher_clean_auto_1000vu_rerun/

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@gouhongshen gouhongshen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed the allocation change in SQLFlusher.FlushBuffer.

No blocking issues found. The new builder path preserves the byte-level escaping behavior for \ and ', removes the fixed 10MB intermediate buffer, and leaves the existing file-flush-on-SQL-error path unchanged. PR scope is clean (content.go / content_test.go only), and GitHub checks are green.

Local note: I could not rerun go test ./pkg/util/export/etl/... on this workstation because the MatrixOne third-party cgo headers (xxhash.h, roaring.h, usearch.h) are missing here; the corresponding GitHub UT check passed.

@mergify

mergify Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Queued — the merge queue status continues in this comment ↓.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed this from correctness, unhappy-path behavior, and the specific allocation goal of #25505.

I checked the main risk points:

  • escaping semantics for \ and ' stayed identical while removing the fixed 10MB intermediate buffer
  • the final SQL shape is unchanged except for the allocation strategy
  • failure/backoff / reconnect paths in FlushBuffer are untouched
  • the added regression test covers the inline escaping case that is easiest to break in this refactor

I don't see a blocking issue in this change set. Approving.

@mergify mergify Bot added the queued label Jul 10, 2026
@mergify
mergify Bot merged commit 3ebdb25 into matrixorigin:main Jul 10, 2026
23 of 24 checks passed
@mergify mergify Bot removed the queued label Jul 10, 2026
@VioletQwQ-0
VioletQwQ-0 deleted the violet/issue-25505-sqlflusher branch July 13, 2026 03:06
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
)

`SQLFlusher.FlushBuffer` used to allocate a fixed `table.DefaultWriterBufferSize` 10MB intermediate buffer for every trace/ETL SQL flush before building the final `LOAD DATA INLINE` statement. Under high-concurrency sysbench workloads this became a large allocation amplifier even when the actual CSV batch was much smaller than 10MB.

This PR builds the final inline SQL directly with `strings.Builder` and sizes it from the actual batch length plus the number of bytes that need SQL escaping. This removes the fixed 10MB intermediate buffer and avoids the extra `fmt.Sprintf` copy.

Changes:
- Replace the fixed 10MB `sqlCsv` buffer in `SQLFlusher.FlushBuffer` with an exactly-sized `strings.Builder` for the final SQL.
- Preserve the existing escape behavior for `\` and `'`.
- Add a regression test for inline SQL escaping.

Validation:
- `go test ./pkg/util/export/etl/...`
- mo-srv-129 clean point_select validation, 1000vu, `--db-ps-mode=auto`, 8GB SHARED cache, fresh `sysbench_ps_sqlflusher_clean` database:

| metric | clean baseline | patched run 1 | patched run 2 |
|---|---:|---:|---:|
| TPS | 150,941/s | 169,974/s | 161,550/s |
| avg latency | 6.61ms | 5.87ms | 6.18ms |
| p95 latency | 27.17ms | 31.94ms | 27.17ms |
| CPU samples / 30s | 1602.14s (~53.4 cores) | 1620.27s (~54.0 cores) | 1604.88s (~53.5 cores) |
| heap alloc_space total | 4.62TB | 506.39GB | 1019.38GB |
| `SQLFlusher.FlushBuffer` | 223.18GB flat / 225.06GB cum | 0 flat / 30.54MB cum | 0 flat / 77.48MB cum |

Note: Go heap `alloc_space` is cumulative since process start. Patched run 2 reused the same patched MO process after run 1, so its heap total includes prior allocations. It is included as a stability check that `SQLFlusher.FlushBuffer` remains MB-level after another 1000vu run.

Profile paths:
- baseline: `mo-srv-129:/data/mo-prof/pprof/1000vu_clean_auto/`
- patched run 1: `mo-srv-129:/data/mo-prof/pprof/sqlflusher_clean_auto_1000vu/`
- patched run 2: `mo-srv-129:/data/mo-prof/pprof/sqlflusher_clean_auto_1000vu_rerun/`

Approved by: @gouhongshen, @XuPeng-SH
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
)

`SQLFlusher.FlushBuffer` used to allocate a fixed `table.DefaultWriterBufferSize` 10MB intermediate buffer for every trace/ETL SQL flush before building the final `LOAD DATA INLINE` statement. Under high-concurrency sysbench workloads this became a large allocation amplifier even when the actual CSV batch was much smaller than 10MB.

This PR builds the final inline SQL directly with `strings.Builder` and sizes it from the actual batch length plus the number of bytes that need SQL escaping. This removes the fixed 10MB intermediate buffer and avoids the extra `fmt.Sprintf` copy.

Changes:
- Replace the fixed 10MB `sqlCsv` buffer in `SQLFlusher.FlushBuffer` with an exactly-sized `strings.Builder` for the final SQL.
- Preserve the existing escape behavior for `\` and `'`.
- Add a regression test for inline SQL escaping.

Validation:
- `go test ./pkg/util/export/etl/...`
- mo-srv-129 clean point_select validation, 1000vu, `--db-ps-mode=auto`, 8GB SHARED cache, fresh `sysbench_ps_sqlflusher_clean` database:

| metric | clean baseline | patched run 1 | patched run 2 |
|---|---:|---:|---:|
| TPS | 150,941/s | 169,974/s | 161,550/s |
| avg latency | 6.61ms | 5.87ms | 6.18ms |
| p95 latency | 27.17ms | 31.94ms | 27.17ms |
| CPU samples / 30s | 1602.14s (~53.4 cores) | 1620.27s (~54.0 cores) | 1604.88s (~53.5 cores) |
| heap alloc_space total | 4.62TB | 506.39GB | 1019.38GB |
| `SQLFlusher.FlushBuffer` | 223.18GB flat / 225.06GB cum | 0 flat / 30.54MB cum | 0 flat / 77.48MB cum |

Note: Go heap `alloc_space` is cumulative since process start. Patched run 2 reused the same patched MO process after run 1, so its heap total includes prior allocations. It is included as a stability check that `SQLFlusher.FlushBuffer` remains MB-level after another 1000vu run.

Profile paths:
- baseline: `mo-srv-129:/data/mo-prof/pprof/1000vu_clean_auto/`
- patched run 1: `mo-srv-129:/data/mo-prof/pprof/sqlflusher_clean_auto_1000vu/`
- patched run 2: `mo-srv-129:/data/mo-prof/pprof/sqlflusher_clean_auto_1000vu_rerun/`

Approved by: @gouhongshen, @XuPeng-SH
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
)

`SQLFlusher.FlushBuffer` used to allocate a fixed `table.DefaultWriterBufferSize` 10MB intermediate buffer for every trace/ETL SQL flush before building the final `LOAD DATA INLINE` statement. Under high-concurrency sysbench workloads this became a large allocation amplifier even when the actual CSV batch was much smaller than 10MB.

This PR builds the final inline SQL directly with `strings.Builder` and sizes it from the actual batch length plus the number of bytes that need SQL escaping. This removes the fixed 10MB intermediate buffer and avoids the extra `fmt.Sprintf` copy.

Changes:
- Replace the fixed 10MB `sqlCsv` buffer in `SQLFlusher.FlushBuffer` with an exactly-sized `strings.Builder` for the final SQL.
- Preserve the existing escape behavior for `\` and `'`.
- Add a regression test for inline SQL escaping.

Validation:
- `go test ./pkg/util/export/etl/...`
- mo-srv-129 clean point_select validation, 1000vu, `--db-ps-mode=auto`, 8GB SHARED cache, fresh `sysbench_ps_sqlflusher_clean` database:

| metric | clean baseline | patched run 1 | patched run 2 |
|---|---:|---:|---:|
| TPS | 150,941/s | 169,974/s | 161,550/s |
| avg latency | 6.61ms | 5.87ms | 6.18ms |
| p95 latency | 27.17ms | 31.94ms | 27.17ms |
| CPU samples / 30s | 1602.14s (~53.4 cores) | 1620.27s (~54.0 cores) | 1604.88s (~53.5 cores) |
| heap alloc_space total | 4.62TB | 506.39GB | 1019.38GB |
| `SQLFlusher.FlushBuffer` | 223.18GB flat / 225.06GB cum | 0 flat / 30.54MB cum | 0 flat / 77.48MB cum |

Note: Go heap `alloc_space` is cumulative since process start. Patched run 2 reused the same patched MO process after run 1, so its heap total includes prior allocations. It is included as a stability check that `SQLFlusher.FlushBuffer` remains MB-level after another 1000vu run.

Profile paths:
- baseline: `mo-srv-129:/data/mo-prof/pprof/1000vu_clean_auto/`
- patched run 1: `mo-srv-129:/data/mo-prof/pprof/sqlflusher_clean_auto_1000vu/`
- patched run 2: `mo-srv-129:/data/mo-prof/pprof/sqlflusher_clean_auto_1000vu_rerun/`

Approved by: @gouhongshen, @XuPeng-SH

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/S Denotes a PR that changes [10,99] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance] SQLFlusher trace/ETL path allocates heavily under high-concurrency sysbench workloads

4 participants