Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-42398

Support statement based binary logging for UPDATE ... RETURNING ... INTO

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • None
    • 13.3
    • None
    • None

    Description

      Description

      MDEV-39563 implemented UPDATE ... RETURNING ... INTO. The statement is rejected if it would be written to the binary log in the statement format:

      SET binlog_format=STATEMENT;
      UPDATE t1 SET a=a+1 RETURNING a INTO @a;
      -- ERROR HY000: Incorrect usage of --binlog-format=statement and RETURNING..INTO
      

      With binlog_format=MIXED the statement is written in the row format. With binlog_format=ROW it works as usual. This issue is about making UPDATE ... RETURNING ... INTO work with binlog_format=STATEMENT.

      Why it is rejected now

      The statement is marked with a new unsafe statement type, BINLOG_STMT_UNSAFE_RETURNING_INTO. In THD::decide_logging_format() it is rejected in the statement format with ER_WRONG_USAGE. A statement of a stored procedure is checked when it is executed, so CALL itself is not rejected. There are two reasons for rejecting it.

      1. The target of INTO may not exist on the replica

      A statement like UPDATE t1 SET a=a+1 RETURNING a INTO spvar, where the target is a local variable of a stored routine, cannot be written to the binary log as it is. The code that sets INTO up has a comment about it: "we would need to cut the 'INTO spvar' part".

      If the INTO part is cut, the replica runs a plain UPDATE ... RETURNING, which behaves differently:

      • The primary raises ER_TOO_MANY_ROWS at the second changed row, and does not update that row.
      • The replica has no INTO, so it updates all the rows and raises no error.

      2. The order of the rows is not predictable

      The row that is assigned to the INTO target, and the row at which ER_TOO_MANY_ROWS is raised, depend on the order in which the rows are found. This is the same problem as SELECT ... LIMIT without ORDER BY, which is only an unsafe statement (a warning) in the statement format and not an error.

      Requirement

      If binlog_format=STATEMENT is allowed for UPDATE ... RETURNING ... INTO, the primary and the replica must end up with the same data, and with the same error (or no error) for the statement. This includes the rows that are updated before ER_TOO_MANY_ROWS is raised.

      Possible implementations

      A. Allow it for user variable targets, with the unsafe statement warning

      Allow the statement if all the INTO targets are user variables (@a). Write it as it is, and issue the usual "unsafe statement" warning (ER_BINLOG_UNSAFE_STATEMENT), as for SELECT ... LIMIT without ORDER BY. Keep rejecting stored procedure variables, and statements in stored functions.

      Pros:

      • A small change in decide_logging_format().
      • The replica runs the same statement with the same INTO, so it raises the same ER_TOO_MANY_ROWS for the same rows, if the order of the rows is the same.
      • It matches how other unsafe statements are treated.

      Cons:

      • Order dependent: with a different order of the rows, the replica can assign a different row, or raise the error for a different row. Every statement that has several changed rows is affected, and not only the ones that fail.
      • Does not support SP variables and ROW variables (the common use of INTO).

      B. Rewrite the SP variable target

      When the statement is written to the binary log, replace each INTO target that is an SP variable by a generated user variable (a name that cannot clash with the user's names), so that the logged statement still has an INTO clause and raises ER_TOO_MANY_ROWS on the replica.

      Pros:

      • The replica raises the same error, so the data and the error code stay consistent.
      • It supports the typical use in stored procedures.

      Cons:

      • A new query rewriting step in the binary log code, which is not used for other statements.
      • ROW variables, fields of ROW variables, and associative array elements need to be handled as well.
      • The generated user variables are visible on the replica, and need to be cleaned up.
      • It still has the order dependency of A.

      C. Write the statement in the row format

      If binlog_format=STATEMENT, switch to the row format for this statement.

      Pros:

      • The rows that are really updated are written, in the order in which they were updated, so the replica ends up with the same data.
      • No query rewriting, and it works for every kind of target.

      Cons:

      • The user who chose binlog_format=STATEMENT gets row events. A replica or a tool that does not support row events does not work. The tables must support the row format.
      • ER_TOO_MANY_ROWS raised after some rows were updated (non-transactional engines) needs a row log of the partial update, and the error code.
      • It needs a decision on the semantics of binlog_format=STATEMENT.

      D. Make the result independent of the order of the rows

      Define what the statement does with several changed rows, so that the result does not depend on the order. For example, raise the error whenever more than one row is changed, before any row is updated (a check pass first).

      Pros:

      • Removes the second reason, and the first reason becomes simpler: either no row is updated, or exactly one.

      Cons:

      • A second pass over the rows, or a different way to evaluate the statement. The cost and the locking change.
      • It is a different behavior from the current one, where the first row is updated before the error is raised.
      • It does not remove the need to handle SP variable targets.

      Things to test, whichever approach is chosen

      • User variables, SP variables, ROW variables and their fields, and associative array elements as the targets.
      • One row, several rows with one changed row, and several changed rows (ER_TOO_MANY_ROWS), for transactional and non-transactional engines.
      • The binary log content, and the replica: the same data and the same error code on the primary and on the replica.
      • A statement in a stored procedure, a stored function, a trigger, a prepared statement, and a statement that is re-executed.
      • binlog_format = STATEMENT, MIXED and ROW. --binlog-format=statement with a statement-only engine.
      • System versioned and period tables, partitioned tables, and the IGNORE variant (MDEV-42381).

      Related

      • MDEV-39563 (UPDATE ... RETURNING ... INTO)
      • MDEV-42381 (Support UPDATE IGNORE ... RETURNING ... INTO)

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              bar Alexander Barkov
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.