Skip to content

[r2dbc] getRowsUpdated() returns 0 for successful INSERT…SELECT — needs reliable written_rows #2860

Open

Description

@thiagohora

Summary

ClickHouseResult.getRowsUpdated() in clickhouse-r2dbc returns 0 for
successful INSERT INTO … SELECT FROM … queries that did write rows. This
makes it impossible to reliably distinguish "INSERT…SELECT wrote 0 rows
because the SELECT produced none" from "INSERT…SELECT wrote N rows but the
driver reported 0" at the application layer.

The same row count appears correctly in system.query_log.written_rows
server-side, and the HTTP X-ClickHouse-Summary header also carries an
accurate written_rows once the query finishes. The information exists; the
driver just doesn't expose it via the standard R2DBC Result.getRowsUpdated()
contract for this query shape.

Reproduction

  • Driver: com.clickhouse:clickhouse-r2dbc:0.9.0 (also reproduces on 0.8.x)
  • Server: ClickHouse 25.3
  • Query shape: INSERT INTO target_table (...) SELECT ... FROM source_table WHERE ...
  • Connection settings: async_insert=1, wait_for_async_insert=1
    (per docs, async_insert is a no-op for INSERT…SELECT, but we set it
    globally for the INSERT VALUES path on the same connection)
Flux.from(statement.execute())
    .flatMap(Result::getRowsUpdated)   // emits 0 even when N rows were inserted
    .reduce(0L, Long::sum)
    // observed: returns 0

Verifying server-side after the query finishes:

SELECT written_rows
FROM system.query_log
WHERE query_id = '...' AND type = 'QueryFinish';
-- returns N (the correct count)

Root cause

Looking at ClickHouseResult constructor (current main):

Mono<? extends UpdateCount> updatedCount = Mono.just(response)
    .map(ClickHouseResponse::getSummary)
    .map(ClickHouseResponseSummary::getProgress)
    .map(ClickHouseResponseSummary.Progress::getWrittenRows)
    .map(UpdateCount::new);

The driver reads written_rows from Summary.getProgress(), which is the
interim progress event snapshot — not the final summary. For
INSERT…SELECT queries, a definitive post-completion written_rows is
typically reflected in Summary.getStatistics() (or in a final progress
event that doesn't always land before the subscriber observes completion).

ClickHouseResponseSummary exposes both getProgress() and getStatistics(),
and the top-level getWrittenRows() delegates to progress.

Use case

Detecting at the application layer when an INSERT…SELECT wrote zero rows
(to surface inconsistency conditions before committing dependent state).
Since getRowsUpdated() can return 0 even on success, the check fires
false positives.

Asks

Any one of the following would unblock us:

  1. Source getRowsUpdated() from Summary.getStatistics() (or whichever
    field is authoritative post-completion) instead of from Summary.getProgress().
  2. Expose the raw ClickHouseResponseSummary from ClickHouseResult (or a
    similar handle), so callers can read the final fields themselves.
  3. Document the current semantics so applications know not to rely on
    getRowsUpdated() for INSERT…SELECT.

Happy to send a PR for (1) or (2) — please confirm which direction you'd
prefer.

Environment

  • clickhouse-r2dbc: 0.9.0
  • clickhouse-client / clickhouse-http-client: 0.9.0
  • ClickHouse server: 25.3.x
  • Connection: HTTP transport

Activity

  1. chernser commented on May 26, 2026

    @chernser
    Contributor

    Good day, @thiagohora!

    Thank you for reporting thing.
    The problem with 1 and 2 is that progress not supported in HTTP (it requires another way of reading data and we will eventually implement it. But in case of async_insert=1 server doesn't provide reliable count anyway because of many reasons. There is an issue ClickHouse/ClickHouse#57768 about the problem)

    I have an open PR that should document that behavior. In the next release we will remove hardcoded async_insert=0 to let change it.

  2. thiagohora commented on May 27, 2026

    @thiagohora
    Author

    Thanks, each something like written_rows or accepted_rows would be great.

  3. chernser commented on May 27, 2026

    @chernser
    Contributor

    @thiagohora

    the problem is that stats are hard to calculate in case of async insert. Beside there can be deduplication in work what will make number not accurate.
    DB guarantees persistent by default but I see there is need something like "everything ok" signal. Am I right?

  4. thiagohora commented on May 27, 2026

    @thiagohora
    Author

    Yes, in my case, I don't need precision, but to know if something was written or not.

  5. added this to the 0.11.0 milestone on May 27, 2026
  6. removed this from the 0.11.0 milestone on Jun 24, 2026
  7. thiagohora commented on Jul 2, 2026

    @thiagohora
    Author

    Hi @chernser,

    Any update on this one? Thank you very much.

  8. chernser commented on Jul 2, 2026

    @chernser
    Contributor

    Good day, @thiagohora !

    We will look into it next release (month). But I think, we will not be able to provide more accurate value - so we have to document it and may be return 1 always on success. But it will be not accurate still.

  9. added this to the 0.12.0 milestone on Jul 22, 2026
  10. removed this from the 0.12.0 milestone on Nov 10, 2026
  11. polyglotAI-bot commented on Nov 27, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — needs info from the reporter
    getRowsUpdated() gives the correct count for INSERT … SELECT on main and on 0.9.0, with servers 26.11 and 25.3. Ask the reporter for the items in the Analysis.

    • Category: Bug
    • Classification: impact: none · area: API & diagnostics (R2DBC update count)
    • Status on main: Not reproduced — INSERT … SELECT returns 1000 and 0 as expected in all runs, also with async_insert=1 and with 20,000,000 source rows.
    • Breaking change: No for exposing the summary (additive); Yes for a fixed count of 1 on success: callers that read the count get 1.
    • Duplicates / related: None found

    Analysis: ClickHouseResult.java:32-35 reads the count from written_rows of the server's X-ClickHouse-Summary, and that value is correct for INSERT … SELECT in all runs. The only 0 comes from the server: with async_insert=1, 25.3 sends written_rows: 0 for INSERT … VALUES (curl shows the same header), and 26.11 sends 3. Missing for a reproduction: the CREATE TABLE of the target table (engine, materialized views), how the connection sets the settings, and the summary header of a query that returns 0. docs/clickhouse-docs/r2dbc.mdx does not say where the count comes from (docs/features.md:131 says it for jdbc-v2 only).

    Reproduction: complete program and output

    Changed from the issue's steps: added the tables and the data, and an INSERT … VALUES case; the second connection sets the issue's settings with custom_settings.

    import io.r2dbc.spi.Connection;
    import io.r2dbc.spi.ConnectionFactories;
    import io.r2dbc.spi.Result;
    import reactor.core.publisher.Flux;
    import reactor.core.publisher.Mono;
    
    public class Repro {
        static final String URL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
        static final String PASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
    
        static long rowsUpdated(Connection conn, String sql) {
            return Flux.from(conn.createStatement(sql).execute())
                    .flatMap(Result::getRowsUpdated)
                    .reduce(0L, Long::sum)
                    .block();
        }
    
        static Connection connect(String params) {
            // compress=0: v1 compressed reads fail on server 26.9+ (clickhouse-java#3107)
            String url = "r2dbc:clickhouse:" + URL.replaceFirst("^https?:", "").replaceFirst("//", "//default:" + PASSWORD + "@")
                    + "/default?compress=0" + params;
            return Mono.from(ConnectionFactories.get(url).create()).block();
        }
    
        public static void main(String[] args) {
            Connection setup = connect("");
            rowsUpdated(setup, "DROP TABLE IF EXISTS triage_2860_src");
            rowsUpdated(setup, "DROP TABLE IF EXISTS triage_2860_dst");
            rowsUpdated(setup, "CREATE TABLE triage_2860_src (id UInt64) ENGINE = MergeTree ORDER BY id");
            rowsUpdated(setup, "CREATE TABLE triage_2860_dst (id UInt64) ENGINE = MergeTree ORDER BY id");
            rowsUpdated(setup, "INSERT INTO triage_2860_src SELECT number FROM numbers(1000)");
    
            String[][] connections = {
                {"default settings", ""},
                {"async_insert=1, wait_for_async_insert=1", "&custom_settings=async_insert%3D1%2Cwait_for_async_insert%3D1"},
            };
            String[][] cases = {
                {"INSERT ... SELECT, 1000 rows", "INSERT INTO triage_2860_dst SELECT id FROM triage_2860_src"},
                {"INSERT ... SELECT, 0 rows", "INSERT INTO triage_2860_dst SELECT id FROM triage_2860_src WHERE id > 5000"},
                {"INSERT ... VALUES, 3 rows", "INSERT INTO triage_2860_dst VALUES (1), (2), (3)"},
            };
            for (String[] cn : connections) {
                Connection conn = connect(cn[1]);
                for (String[] c : cases) {
                    try {
                        System.out.println(cn[0] + ": " + c[0] + ": getRowsUpdated=" + rowsUpdated(conn, c[1]));
                    } catch (Exception e) {
                        System.out.println(cn[0] + ": " + c[0] + ": " + e.getClass().getName() + ": " + e.getMessage());
                    }
                }
                Mono.from(conn.close()).block();
            }
            rowsUpdated(setup, "DROP TABLE triage_2860_src");
            rowsUpdated(setup, "DROP TABLE triage_2860_dst");
            Mono.from(setup.close()).block();
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.2.4:

    default settings: INSERT ... SELECT, 1000 rows: getRowsUpdated=1000
    default settings: INSERT ... SELECT, 0 rows: getRowsUpdated=0
    default settings: INSERT ... VALUES, 3 rows: getRowsUpdated=3
    async_insert=1, wait_for_async_insert=1: INSERT ... SELECT, 1000 rows: getRowsUpdated=1000
    async_insert=1, wait_for_async_insert=1: INSERT ... SELECT, 0 rows: getRowsUpdated=0
    async_insert=1, wait_for_async_insert=1: INSERT ... VALUES, 3 rows: getRowsUpdated=3
    

    Output on main (8480ca001) and on release 0.9.0 (the same for both), ClickHouse 25.3.14.14:

    default settings: INSERT ... SELECT, 1000 rows: getRowsUpdated=1000
    default settings: INSERT ... SELECT, 0 rows: getRowsUpdated=0
    default settings: INSERT ... VALUES, 3 rows: getRowsUpdated=3
    async_insert=1, wait_for_async_insert=1: INSERT ... SELECT, 1000 rows: getRowsUpdated=1000
    async_insert=1, wait_for_async_insert=1: INSERT ... SELECT, 0 rows: getRowsUpdated=0
    async_insert=1, wait_for_async_insert=1: INSERT ... VALUES, 3 rows: getRowsUpdated=0
    

    Automated triage by Polyglot AI. A maintainer makes the final decision.

  12. added
    api-diagnostics
    documentation
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-diagnosticsbugdocumentationr2dbc

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions