WL#6204: InnoDB persistent max value for autoinc columns

Affects: Server-8.0   —   Status: Complete

See BUG#199 on MySQL bugs. Currently InnoDB does the following when a table
is opened: SELECT MAX(c) FROM t; where c is the AUTOINC column name.
This step is used to initialise the column's next autoinc value and
allocation of autoinc values starts from this point. InnoDB also does this
when it executes 'ALTER TABLE AUTO_INCREMENT=N;'.

Suppose there is a table like this:

(1) CREATE TABLE t(a BIGINT PRIMARY KEY AUTO_INCREMENT) ENGINE=InnoDB
AUTO_INCREMENT=42;
(2) BEGIN; INSERT INTO t VALUES(0),(0),(0); -- inserts 42,43,44
(3) COMMIT; -- let us assume that this will make a checkpoint
(4) BEGIN; INSERT INTO t VALUES(0); -- 45;

Then there would be some different scenarios here:

Scenario 1:
(5) -- do nothing here
(6) -- kill server and restart

Scenario 2:
(5) ROLLBACK;
(6) -- shutdown the server normally then restart, or crash and restart

Scenario 3:
(5) COMMIT; -- let's assume this would flush the redo logs
(6) -- shutdown the server normally then restart, or crash and restart

After server restarts in all these scenaiors, we do the following:
(7) INSERT INTO t VALUES(0); -- should insert 46
(8) SELECT * FROM t; -- should return 42,43,44,46

In scenario 1 and 2, the INSERT at step#7 would currently start the allocation
from 45(ignoring the offset setting), not from whatever was the last value
from step#4. Only in scenario 3, it would start from 46 and that's what we
expect. InnoDB should keep track of the maximum value and on restart preserve
that max value and start from there.

InnoDB should at least make sure the result from scenario 2 is the same with
scenario 3, because there is no crash. And InnoDB should try to make
the result from scenario 1 the same as scenario 3. InnoDB can't guarantee
the later because we should consider the performance issue, which would be
mentioned later.

So we plan to implement following features in this worklog:
1. The AUTOINC counters would get persisted through redo logs
2. All AUTOINC counters would be collected from redo logs and applied to
in-memory counters if they're bigger.
3. There won't be any rollback of AUTOINC counters.
4. 'SELECT MAX(c) FROM t' would not be needed except when IMPORT tablespace.
5. The largest updated counter will be logged and will not change with reboot.
6. Considering performance, we could write some extra redo logs, but no
new MTRs.