bug: consuming a stream in batches (`WITH (MAX_BATCH_SIZE = n)` / `stream_consume_batch_size_hint`) records the latest table version with the batch's snapshot, so rows after the batch boundary are never delivered once compaction rewrites their blocks

Open
#21,167 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@zhyass is already working on this.

Since Nov 23, 2026.

Assessment

This issue has not been assessed yet.

Description

C-bug
Search before asking
  • I had searched in the issues and found no similar issues.
Version

main 057efa66e6 (nightly docker image v1.2.961-057efa66e6, 2026-11-08); also v1.2.960-nightly-3c9357c866.

What's Wrong?

When a stream is consumed in batches (FROM <stream> WITH (MAX_BATCH_SIZE = n), or the session setting
stream_consume_batch_size_hint), the new offset written at commit pairs the batch boundary snapshot with the
latest table version. Rows inserted after the boundary are still delivered as long as their blocks are never
rewritten (their _origin_version is NULL). But if a compaction (OPTIMIZE TABLE … COMPACT, or the automatic
compaction that runs after small inserts by default), a recluster or a mutation rewrites those blocks before the
next batch, the rows get a non-NULL _origin_version (the table version the rewrite started from), which is below
the recorded (too new) version, and the change query filters them out as already consumed. The consumer never
receives them, the offset moves past them, and SELECT count(*) FROM <stream> reports 0 pending. No error.

Example: with only INSERTs and default compaction settings, consuming with stream_consume_batch_size_hint = 2
lost 26 of 48 rows. APPEND_ONLY = true and false (when the changes after the boundary are insert-only) are both
affected. Without batching, or when no block is rewritten between batches, every row is delivered.

Cause:

  1. StreamTable asks FuseTable::find_stream_batch_snapshot for the batch boundary. It returns the batch table and
    the table version of the boundary snapshot (source_seq = snapshot.prev_table_seq + 1,
    stream_batch.rs#L246-L253),
    but the caller drops the version:
    stream_table.rs#L172-L181
    let Some((batch_table, _)) = fuse_table.find_stream_batch_snapshot(…).
  2. The batch table comes from load_table_by_snapshot, whose ident.seq is "JUST a place holder"
    (navigate.rs#L205),
    i.e. the latest table version.
  3. At commit, build_update_stream_reqs writes OPT_KEY_TABLE_VER = inner_fuse.get_table_info().ident.seq (the
    placeholder) and OPT_KEY_SNAPSHOT_LOCATION = inner_fuse.snapshot_loc() (the boundary)
    (interpreters/common/stream.rs#L59-L65).
  4. The next read computes changes from the boundary snapshot with the append-only filter
    not(is_not_null(_origin_version) and (_origin_version < {seq} or …))
    (changes.rs#L163-L170),
    which drops rewritten rows whose _origin_version is below the recorded version.

The materialized-view refresh uses the same batch search and keeps the returned version (source_seq = batch_seq,
refresh.rs#L260-L274);
the stream path should do the same and record the boundary snapshot's version.

How to Reproduce?
CREATE TABLE orders (id INT, item STRING);
INSERT INTO orders VALUES (0, 'before the stream');
CREATE STREAM orders_s ON TABLE orders APPEND_ONLY = true;     -- same with APPEND_ONLY = false
CREATE STREAM orders_all ON TABLE orders APPEND_ONLY = true;   -- control, consumed without batching
CREATE TABLE orders_copy (id INT, item STRING);
CREATE TABLE orders_copy_all (id INT, item STRING);
INSERT INTO orders VALUES (1, 'order 1');
INSERT INTO orders VALUES (2, 'order 2');
INSERT INTO orders VALUES (3, 'order 3');
INSERT INTO orders_copy SELECT id, item FROM orders_s WITH (MAX_BATCH_SIZE = 1);    -- 1 row (id 1)
INSERT INTO orders_copy_all SELECT id, item FROM orders_all;                        -- 3 rows
SELECT name, table_version, snapshot_location FROM system.streams WHERE database = 'default' ORDER BY name;
OPTIMIZE TABLE orders COMPACT;
INSERT INTO orders_copy SELECT id, item FROM orders_s WITH (MAX_BATCH_SIZE = 1);    -- 1 row (id 2)
INSERT INTO orders_copy SELECT id, item FROM orders_s WITH (MAX_BATCH_SIZE = 1);    -- 0 rows
SELECT count(*) FROM orders_s;
SELECT id FROM orders_copy ORDER BY id;

Result on main 057efa66e6 (the same on v1.2.960-nightly apart from the snapshot names):

orders_all | 50 | 1/10/_ss/h01a1bdb4ddfc763996c4c568019c398b_v4.mpk   <- consumed fully: the latest snapshot
orders_s   | 50 | 1/10/_ss/h01a1bdb4dcca7ecbad65fcf58ddb3662_v4.mpk   <- after the 1-row batch: the boundary snapshot,
                                                                         but the same (latest) table version
-- after OPTIMIZE: the next batch delivers id 2, then 0 rows forever
SELECT count(*) FROM orders_s: 0
orders_copy: 1, 2                  <- id 3 is never delivered

Expected: orders_copy ends with ids 1, 2, 3 (as orders_copy_all does), and the batch offset records the table
version of the boundary snapshot, not the version of the latest one.

With the setting instead of the hint and only INSERTs, no explicit OPTIMIZE: 12 rounds of four single-row
INSERT INTO events … (ids 1–48), each round followed by one INSERT INTO events_copy SELECT id, payload FROM events_s with stream_consume_batch_size_hint = 2, then draining the stream the same way. The default automatic
compaction (auto_compaction_imperfect_blocks_threshold = 25) rewrites the blocks: 22 rows are delivered, ids
23–48 never are, the stream reports 0 pending, and the table has one compacted block.

Single node, default settings, HTTP handler.

Are you willing to submit PR?
  • Yes I am willing to submit a PR!
Dominant language
Rust
Stars
9.5k
Forks
903
Avg merge
2d 13h
Merged PRs (30d)
204

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from databendlabs/databend

All issues in databendlabs/databend

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.