Bug #110042 Optimize table and corruption?
Submitted: 13 Feb 9:14 Modified: 13 Feb 13:51
Reporter: Pedro Ferreira Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:8.0.32 OS:Ubuntu (22.04)
Assigned to: CPU Architecture:x86 (x86_64)
Tags: innodb, Optimize, shutdown

[13 Feb 9:14] Pedro Ferreira
Description:
Not sure if this is an issue.

Create the database:

mysqld --initialize-insecure --user=mysql --basedir=... --datadir=... --plugin-dir=... --log-error=... --log-error-verbosity=3 --pid-file=... --port=3306 --mysqlx-port=3307 --default-authentication-plugin=mysql_native_password

Then start it:

mysqld --no-defaults --user=mysql --basedir=... --datadir=... --plugin-dir=... --log-error=... --log-error-verbosity=3 --pid-file=... --port=3306 --mysqlx-port=3307 --slave-parallel-type=LOGICAL_CLOCK --slave-parallel-workers=4 --slave-preserve-commit-order=ON --enforce-gtid-consistency --gtid-mode=ON --binlog-format=ROW --server-id=1 --core-file --default-authentication-plugin=mysql_native_password

Then run these commands in quick succession with this C++ program

```
#include <mysql.h>
#include <vector>
#include <iostream>

static int
SendQuery(MYSQL *hdl, MYSQL_RES **res, const std::string &query) {
	int ok = 0;

	if ((ok = mysql_query(hdl, query.c_str()))) {
		std::cerr << "Error while processing query: " << mysql_error(hdl) << std::endl;
	} else {
		*res = mysql_store_result(hdl);
		if (mysql_errno(hdl)) {
			std::cerr << "Error while retrieving query result: " << mysql_error(hdl) << std::endl;
		}
	}
	return ok;
}

static int
CleanResult(MYSQL *hdl, MYSQL_RES **res) {
	int ok = 0;

	while (!ok) {
		mysql_free_result(*res);
		if ((ok = mysql_next_result(hdl)) > 0) {
			std::cerr << "Error while retrieving query result: " << mysql_error(hdl) << std::endl;
		} else if (!ok) {
			*res = mysql_store_result(hdl);
			if (mysql_errno(hdl)) {
				std::cerr << "Error while retrieving query result: " << mysql_error(hdl) << std::endl;
				ok = 1;
			}
		}
	}
	*res = nullptr;
	return ok;
}

std::vector<std::string> statements = {"CREATE TABLE t0 (c0 INT);","SET GLOBAL INNODB_LOG_CHECKSUMS = 0;",
"SET GLOBAL INNODB_FAST_SHUTDOWN = 2;","OPTIMIZE TABLE t0;","SHUTDOWN;"};

int
main(void) {
	int ok = 0;
	MYSQL *hdl = nullptr;
	MYSQL_RES *res = nullptr;

	if (mysql_library_init(0, nullptr, nullptr)) {
		return 1;
	}

	if (mysql_thread_init()) {
		mysql_library_end();
		return 1;
	}

	if (!(hdl = mysql_init(nullptr))) {
		mysql_thread_end();
		mysql_library_end();
		return 1;
	}
	if (!mysql_real_connect(hdl, "localhost", "root", nullptr, "sys", 3306, nullptr, 0)) {
		mysql_close(hdl);
		mysql_thread_end();
		mysql_library_end();
		return 1;
	}

	for (auto st : statements) {
		(void) SendQuery(hdl, &res, st);
		(void) CleanResult(hdl, &res);
	}

	mysql_close(hdl);
	mysql_thread_end();
	mysql_library_end();
	return 0;
}
```

Later the database won't start again. This appears on the log:

[ERROR] [MY-012545] [InnoDB] Log block 38041 at lsn 19476480 has valid header, but checksum field contains 3735928559, should be 2086148976.

I disabled the checksum log beforehand, so I am not sure if this can be considered a bug. Also, I can only reproduce this in a very tight time constraint. If you don't consider this a bug, please close this issue.

The compilation parameters are the same as issue 108148:

-DWITH_DEBUG=1 -DWITH_ASAN=ON -DWITH_UBSAN=ON and boost library version 1.77

How to repeat:
Run the statements above.
[13 Feb 13:51] MySQL Verification Team
Hi Mr. Ferreira,

Thank you for your bug report.

However, it is not a bug.

That is expected behaviour. If you are setting any of ACID attributes to the unsafe values, you can expect exactly the kind of behaviour that you have described.

This is particularly true for innodb_fast_shutdown. As our Manual says:

"
Use innodb_fast_shutdown=2 in emergency or troubleshooting situations, to get the absolute fastest shutdown if data is at risk of corruption.
"

Combination of these two, particularly when you perform a full-table DDL can truly lead to problems.

Not a bug.