A failed statement in a transaction is not registered as aborting it, so the commit silently turns into a rollback

Open
#1,731 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
java, postgresql, sql
Domain
backend, databases

Research direction

Trace transaction handling for INTERNAL DB, runSQL, batch inserts, service operations, and startup structure synchronization, then compare it with the existing internal retry path. Verify how the connection reports server-side transaction failure and how savepoint rollback clears the broken mark; done means all statement routes mark failures correctly, expected savepoint rollbacks restore health, and broken commits fail loudly.

Written by the indexing model from the issue text.

Description

Description

PostgreSQL aborts the whole transaction on any server error: every following command in it fails, and COMMIT performs a rollback instead of committing, reporting success.

The server knows about this and keeps a per-session mark for it: while a transaction is marked as broken, cleaning up temporary data is skipped and the commit fails loudly instead of silently discarding the work. But the mark is only set for the queries the session builds itself. Statements executed outside that route — DDL issued by service operations and by the structure synchronization at startup, batch inserts, and raw SQL from INTERNAL DB / runSQL — leave the transaction unmarked when they fail.

The result for an application developer: an operation fails somewhere inside, the failure is logged, execution continues, everything afterwards fails too (current transaction is aborted), and the apply ends without an error even though nothing was written.

apply () {
    NEWSESSION {
        // any raw statement that the server rejects - a wrong column, a missing table
        INTERNAL DB <{ ... }>;   // fails, logged, execution goes on

        someProperty() <- 1;     // fails with "current transaction is aborted"
        APPLY;                   // reports success - and commits nothing
    }
}

The same happens with the service operations that drop columns or synchronize the structure: the per-item error is logged, the loop keeps going, and the final commit reports success without having committed anything.

Reason

A failure whose result is thrown away is bad enough; a failure that is reported as success loses data with no trace. The mark exists exactly to prevent that, and the statements most likely to fail against a live database — schema changes and raw application SQL — are the ones that never set it.

Two things make the fix non-trivial, and both need to be part of it:

  • Not every failure carrying an SQL state comes from the server. The driver produces 08006 and 08003 on its own when the socket breaks or the connection is already closed, and a statement that succeeded can still fail while being closed. Marking those would be wrong in the opposite direction: the transaction is alive, and the mark would skip cleaning up a temporary table and put it back to be reused with its rows still in it. The state of the transaction has to be taken from the connection, not guessed from the error.
  • A failure inside a transaction is not always final. The structure synchronization deliberately takes a savepoint before dropping a data column and rolls back to it when the drop fails, because that failure is expected. After such a rollback the transaction is healthy again and the mark has to be lifted, the way it already is for the internal retry path — otherwise the server would stop starting up on a migration.
Fix

A statement that the server rejects inside a transaction marks that transaction as broken, whichever way the statement was executed. Rolling back to a savepoint lifts the mark. The commit of a broken transaction keeps failing loudly instead of silently rolling back.

Dominant language
Java
Stars
214
Forks
46
PR merge metrics
No merged PRs in 30d

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • No 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 lsfusion/platform

All issues in lsfusion/platform

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.