Skip to content

TRUNCATE TYPE inside an explicit transaction commits it: ROLLBACK does not put the records back #6220

Closed
ArcadeData/arcadedb-docs
#442

Description

@lvca

Found while fixing #6203 (PR #6208). The materialized-view refresher wrapped TRUNCATE TYPE ... UNSAFE in
database.transaction() and believed the pass was atomic; it was not, and a failed refresh destroyed the view. That
was fixed by removing TRUNCATE from the refresh path - but the statement itself behaves the same way for
everybody, and nothing tells a user so.

BEGIN; TRUNCATE TYPE ...; ROLLBACK; destroys records

TruncateTypeStatement.executeDDL commits the caller's transaction from the inside, three ways:

  • schema.dropIndex(def.name) for every index on the type, before a single record is touched;
  • db.commit(); db.begin() inside the scan callback every arcadedb.truncateBatchSize (default 1000) records;
  • another db.commit(); db.begin() before it rebuilds the indexes it dropped, so the rebuild sees empty buckets.

SQLQueryEngine.executionDatabase()'s Javadoc already documents that TRUNCATE "commits mid-execution", so this is
known internally - but only as a replication concern (#5492), never as a transactional-semantics one, and it is not
documented anywhere a user would look.

Measured on main (8b9ec35), two throwaway tests:

// 1100 rows, no index on the type
database.begin();
database.command("sql", "TRUNCATE TYPE `T` UNSAFE").close();
database.rollback();
// before = 1100, after ROLLBACK = 100
// 5 rows, one NOTUNIQUE index on the type
database.begin();
database.command("sql", "TRUNCATE TYPE `U` UNSAFE").close();
database.rollback();
// before = 5, after ROLLBACK = 0

So an explicit ROLLBACK recovers only whatever was in the last, uncommitted batch. Unindexed and under one batch,
nothing is lost (which is why this has never been noticed). Over one batch, everything but the tail is gone. With
any index on the type, everything is gone at any size, because the dropIndex and the pre-rebuild commit land
either side of the deletes.

Nothing reports this. The statement returns operation: truncate type and the rollback returns normally.

Why it matters beyond the obvious

A user cannot write BEGIN; TRUNCATE TYPE Staging UNSAFE; INSERT ...; COMMIT; and expect the old rows back if the
insert fails - which is the single most natural use of TRUNCATE, and exactly the shape that made #6203 a data-loss
bug rather than a cosmetic one. It is also the shape a "reload this table" job takes.

Suggested fix

Make the deletes part of the caller's transaction when there is one, rather than committing through it:

That makes the statement mean what a user reading BEGIN ... ROLLBACK expects, without giving up the fast path.
The cost is the one #6203 accepted for the same reason: a truncate inside a transaction holds the pages it deletes
until commit.

A weaker alternative - refuse TRUNCATE TYPE when a transaction is active, with an error naming DELETE FROM as
the transactional equivalent - is worth considering if the above is judged too large. It is strictly worse (it
removes a capability rather than fixing it) but it is loud, which is the property today's behaviour lacks entirely.

Related, not verified here

SQLQueryEngine.executionDatabase() names two other statements that commit mid-execution: REBUILD INDEX and
BatchStep. Neither is destructive the way TRUNCATE is - a half-rebuilt index is recoverable and BatchStep is an
explicitly requested batching - so they are probably fine, but somebody touching this should look at them.

Activity

  1. self-assigned this
    on Aug 15, 2026
  2. added this to the 26.9.1 milestone on Aug 16, 2026
  3. added theissue type on Aug 16, 2026
  4. added 3 commits that reference this issue on Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions