Skip to content

Postgres extended protocol never enters skip-until-Sync outside an explicit transaction, so a failed pipeline keeps executing and Sync commits the rows it should discard #7775

Closed

Description

@ruispereira

Summary

setErrorInTx() raises the "an error happened" flag ONLY when the session is inside an explicit BEGIN block:

  private void setErrorInTx() {
    if (explicitTransactionStarted)
      errorInTransaction = true;
  }

errorInTransaction is the single flag that drives both (a) "discard every message until Sync", which every handler
implements as an early return, and (b) Sync's rollback-vs-commit decision. So in autocommit - the mode every JDBC /
psycopg / asyncpg client is in unless it explicitly opened a transaction - an ErrorResponse in the middle of a pipeline
leaves the connection fully live:

  1. The Parse/Bind/Execute messages that follow the failing one in the same pipeline are executed and answered, instead of
    being discarded until Sync as the protocol requires ("the backend discards messages until a Sync is reached", and
    pipelining clients such as psycopg3 rely on it to mark the rest of the pipeline aborted).
  2. syncCommand() then takes its else if (!explicitTransactionStarted) arm and commits:
    if (errorInTransaction) {
      database.rollback();
      errorInTransaction = false;
    } else if (!explicitTransactionStarted) {
      if (database.isTransactionActive())
        database.commit();
    }

PostgreSQL treats a run of extended-protocol messages terminated by one Sync as an implicit transaction block, and an
error anywhere in it rolls the WHOLE block back. Here the block is committed, so the statements that succeeded before
the failure - and the ones that wrongly ran after it - are persisted. That is the classic JDBC executeBatch() in
autocommit shape: pgjdbc sends the whole batch and one Sync, and callers (and BatchUpdateException handling) assume
a failed batch left nothing behind.

Where (HEAD 417314c)

postgresw/src/main/java/com/arcadedb/postgres/PostgresNetworkExecutor.java:3165

  private void setErrorInTx() {
    if (explicitTransactionStarted)
      errorInTransaction = true;
  }

postgresw/src/main/java/com/arcadedb/postgres/PostgresNetworkExecutor.java:344

    if (errorInTransaction) {
      // DISCARDED PREVIOUS MESSAGES TILL THIS POINT
      database.rollback();
      errorInTransaction = false;
    } else if (!explicitTransactionStarted) {
      if (database.isTransactionActive())
        database.commit();
    }
    writeReadyForQueryMessage();

The early-return guards that implement "skip until Sync" and are therefore dead in autocommit:
describeCommand() :392, executeCommand() :583, closeCommand() :368, bindCommand() :2269, parseCommand() :2368.

Repro

Raw-socket integration test (standalone, outside the source tree), one pipeline with NO explicit BEGIN:

parseBindExecute("s1", "INSERT INTO PipeT SET id = 1");
parseBindExecute("s2", "SELECT FROM ThisTypeDoesNotExist");   // errors
parseBindExecute("s3", "INSERT INTO PipeT SET id = 3");
sendSync();

Run against the worktree classes:

$ java -cp <worktree classes + deps> RunIt com.arcadedb.postgres.PipelineSkipUntilSyncIT
REPRO: backend messages after the pipeline = 1 2 T D C 1 2 E 1 2 T D C Z
REPRO: ErrorResponse fields = {M=Error on executing query: Type with name 'ThisTypeDoesNotExist' was not found, S=ERROR, C=42P01}
REPRO: ReadyForQuery status = I
REPRO: rows persisted in PipeT after the failed pipeline = 2 -> ids: 3 1
[         1 tests successful      ]

The message trace is the whole finding: after the E (ErrorResponse) the backend answers 1 2 T D C for s3 - it parsed,
bound, described, returned a row and completed the statement that PostgreSQL would have skipped. And both rows are on
disk afterwards; real PostgreSQL leaves zero.

Suggested fix

Separate the two meanings the one flag carries today:

  • a skipUntilSync flag set by EVERY ErrorResponse in the extended protocol (i.e. setErrorInTx() unconditionally sets
    it), cleared only by syncCommand(). That is what the existing early-return guards should read.
  • keep errorInTransaction (the aborted-block state, SQLSTATE 25P02, ReadyForQuery 'E') gated on
    explicitTransactionStarted, as it is now.
    Then make syncCommand() roll back rather than commit whenever skipUntilSync is set, so the implicit transaction block
    is discarded the way PostgreSQL discards it. The simple-query ('Q') path is unaffected: it has its own
    ReadyForQuery per statement and no implicit multi-statement block.

Scope / confidence

Tried to refute it three ways. (1) Maybe the SQL engine autocommits each statement so no block exists: no - the comment
at :2483 ("No explicit database.commit() here ... the Sync that follows this Execute take its implicit-commit branch")
says the transaction deliberately spans the messages up to Sync, and the repro's two rows appearing together confirms it.
(2) Maybe some other guard stops the pipeline: the trace shows 1 2 T D C after the E, so no. (3) Maybe pgjdbc sends a
Sync per statement so this is unreachable: it does for single statements, but not for executeBatch(), and psycopg3
pipeline mode and asyncpg batch paths deliberately pipeline many messages per Sync. Falsified if a client is found that
always Syncs per statement AND the project decides the implicit-block rollback semantics are out of scope - but the
"statements after an ErrorResponse still execute" half is a plain protocol violation either way.


Activity

  1. added this to the 26.10.1 milestone on Sep 17, 2026
  2. added
    in progress
    on Sep 18, 2026
  3. self-assigned this
    on Sep 18, 2026
  4. added a commit that references this issue on Sep 18, 2026
  5. lvca commented on Sep 18, 2026

    @lvca
    Member

    Fixed in #7853, milestone 26.10.1.

    Two things changed for pipelined clients (JDBC executeBatch(), psycopg3 pipeline mode, asyncpg):

    • After an error, the remaining messages are discarded until your Sync, instead of being executed and answered.
    • A run of statements between two Sync messages is now one transaction. If anything in it fails, the whole run is rolled back; before, the statements that had already run were kept.

    Worth knowing when you upgrade: a failed batch in autocommit now leaves nothing behind, which is what PostgreSQL does and what BatchUpdateException handling assumes, but it differs from the previous per-statement behaviour.

    While fixing this we also found and fixed two related problems: a BEGIN sent over the extended protocol never actually opened a transaction (so the matching ROLLBACK silently kept the writes), and a COMMIT you had already been told succeeded could still be rolled back by a later failure in the same pipeline.


    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

  6. removed
    in progress
    on Sep 18, 2026
  7. added a commit that references this issue on Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions