Details
-
Bug
-
Status: Open (View Workflow)
-
Major
-
Resolution: Unresolved
-
13.3
-
None
-
None
Description
Description
MDEV-39563 implemented UPDATE ... RETURNING ... INTO. It does not support UPDATE IGNORE ... RETURNING ... INTO, which fails with ER_NOT_SUPPORTED_YET:
UPDATE IGNORE t1 SET a=a+1 RETURNING a INTO @a; |
-- ERROR 42000: This version of MariaDB doesn't yet support 'UPDATE IGNORE..RETURNING..INTO' |
UPDATE IGNORE ... RETURNING without INTO works as before.
Why it is not supported
RETURNING ... INTO accepts at most one row, so ER_TOO_MANY_ROWS must be raised when a second row is found. In Sql_cmd_update::update_single_table() this is done before ha_update_row() for the second changed row, so that the second row is not updated. The check is updated == 1 && with_returning_into().
IGNORE makes the engine silently skip rows that it rejects (a duplicate key, no partition found, and so on). Before ha_update_row() the server does not know whether the engine will accept or reject the row. So it cannot tell whether a second changed row would be a second updated row, or one that IGNORE would skip.
Example: the first row is updated, and the second row would be rejected by a unique key.
- Without INTO, one row is updated and returned.
- With INTO and the current check, ER_TOO_MANY_ROWS would be raised although only one row would be updated.
Requirement
- ER_TOO_MANY_ROWS is raised only if more than one row would actually be updated. A changed row that the engine rejects, and that IGNORE skips, does not count.
- The second row is never updated. After the statement fails, the table is as it was before the second row was reached, as in the non-IGNORE case.
Approaches that do not meet the requirement
- Treat any second changed row as an error, even one the engine would reject. This raises false errors.
- Update the second row and raise the error after the update. The second row stays updated on non-transactional engines.
- Skip the second row with a warning instead of an error. A statement that would have updated two rows then succeeds.
Possible implementations
A. Update, then undo
Call ha_update_row() for the second changed row. If it succeeds, restore the old row, then raise ER_TOO_MANY_ROWS.
Pros:
- Exact semantics.
- No engine API change.
Cons:
- This was prototyped and found unsafe:
- Overwritten BLOB buffers corrupted a MyISAM/Aria index.
- CSV row positions were wrong.
- Partition-moving updates failed.
- Triggers have already fired, and system-versioned or period history rows have already been created, and the undo does not remove them.
- It needs extra work in the failing case.
- It needs engine-specific handling for each of the cases above.
B. Engine pre-check ("would this update succeed?")
Add a handler method, for example ha_update_row_ignore() or a "check only" mode. It reports whether the engine would accept the row (duplicate key, no partition, foreign key, and so on) without changing it.
Pros:
- Exact semantics.
- Nothing is changed or undone.
- It could also be reused for future INSERT IGNORE ... RETURNING ... INTO and DELETE.
Cons:
- Every engine needs an implementation, including its unique and foreign key checks and locking behavior.
- It is hard to do correctly for InnoDB, partitioned tables and Spider (a remote engine, where the check needs a round trip and could race with another session).
- It is a large API change.
- A default of "unknown" means the statement stays unsupported for that engine.
C. Transactional engines only
Allow UPDATE IGNORE ... RETURNING ... INTO only when all tables are transactional. Update the second row. If the engine accepts it, raise ER_TOO_MANY_ROWS. The statement is rolled back, including the first row, so the table ends up as it was. For non-transactional engines it stays ER_NOT_SUPPORTED_YET.
Pros:
- Exact semantics where it is allowed.
- A small change.
- No new engine API and no undo.
Cons:
- It only covers transactional engines such as InnoDB.
- Triggers and stored functions that change non-transactional tables, or send output, have already run for the second row.
- The statement-level rollback must be verified for each combination of engines.
- The restriction is an extra error condition to document.
D. Keep it unsupported (the current state)
Pros:
- No risk.
- The error is clear.
Cons:
- Users of UPDATE IGNORE cannot use INTO.
Things to test, whichever approach is chosen
- Unique-key violations, HA_ERR_NO_PARTITION_FOUND, foreign key errors, and a row that moves between partitions.
- System-versioned and period tables, triggers, and generated columns.
- Transactional and non-transactional engines, and Spider.
- Statement binlog format: UPDATE IGNORE is already marked unsafe (BINLOG_STMT_UNSAFE_UPDATE_IGNORE), and RETURNING ... INTO is unsafe in statement format.
- Prepared statements and stored routine re-execution.
Related
- MDEV-39563 (UPDATE ... RETURNING ... INTO)
Attachments
Issue Links
- is blocked by
-
MDEV-39563 Implement UPDATE ... RETURNING ... INTO
-
- Stalled
-
- relates to
-
MDEV-42156 UPDATE IGNORE ... RETURNING ... INTO fails with ER_TOO_MANY_ROWS when the second row is skipped by IGNORE (duplicate key)
-
- Closed
-