Repository navigation
Description
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 everyarcadedb.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 = 0So 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:
- if a transaction is already active when
TRUNCATEruns, do not commit/begin inside the scan and do not
drop the indexes - delete the records in the caller's transaction and let the index maintenance happen per record,
the wayDELETE FROMalready does. The batching and the index drop/rebuild exist for throughput ([SQL]TRUNCATE TYPE <TypeName>;is very slow and incomplete #4817, TRUNCATE TYPE leaves UNIQUE indexes in an inconsistent state; subsequent MERGE/INSERT fails until DROP TYPE is used #4352)
and for HA log-entry size, and both are worth keeping for the no-transaction case, where TRUNCATE owns its own
transaction and the batching is harmless; - if no transaction is active, keep today's behaviour exactly.
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
fix(#6220): a failed fast-path truncate keeps its indexes, and a fail…
fix(#6220): the own-transaction lifecycle is written once, and a bad …
fix(#6220): TRUNCATE joins the caller's transaction instead of commit…
fix(engine): five defects found by analysis (ArcadeData#7918, ArcadeD…