core: HA 2022.4 database migration stuck with MariaDB

The problem

Updating from 2022.3.7 to 2022.4.0, the database migration never finished. CPU and IO utilization was 0. I checked the running MariaDB processes and saw this:

MariaDB [home_assistant]> show processlist;
+-----+------+-----------------+----------------+---------+------+---------------------------------+--------------------------------------------------+----------+
| Id  | User | Host            | db             | Command | Time | State                           | Info                                             | Progress |
+-----+------+-----------------+----------------+---------+------+---------------------------------+--------------------------------------------------+----------+
|  99 | hass | localhost:50986 | home_assistant | Sleep   |  181 |                                 | NULL                                             |    0.000 |
| 100 | hass | localhost:51128 | home_assistant | Sleep   |    7 |                                 | NULL                                             |    0.000 |
| 102 | hass | localhost:51282 | home_assistant | Query   |  180 | Waiting for table metadata lock | ALTER TABLE states ADD attributes_id INTEGER(20) |    0.000 |
| 107 | hass | localhost       | home_assistant | Query   |    0 | Init                            | show processlist                                 |    0.000 |
+-----+------+-----------------+----------------+---------+------+---------------------------------+--------------------------------------------------+----------+
4 rows in set (0.000 sec)

After some googling, I found various queries to run against the INFORMATION_SCHEMA for finding out what was taking the lock, but there wasn’t anything. I stopped HA and started it again, and then it got stuck on another query:

+----+------+-----------------+----------------+---------+------+---------------------------------+----------------------------------------------------------------+----------+
| Id | User | Host            | db             | Command | Time | State                           | Info                                                           | Progress |
+----+------+-----------------+----------------+---------+------+---------------------------------+----------------------------------------------------------------+----------+
| 36 | hass | localhost       | home_assistant | Query   |    0 | Init                            | show processlist                                               |    0.000 |
| 37 | hass | localhost:53270 | home_assistant | Sleep   |  114 |                                 | NULL                                                           |    0.000 |
| 38 | hass | localhost:53330 | home_assistant | Sleep   |   32 |                                 | NULL                                                           |    0.000 |
| 39 | hass | localhost:53500 | home_assistant | Sleep   |  114 |                                 | NULL                                                           |    0.000 |
| 40 | hass | localhost:53502 | home_assistant | Query   |  114 | Waiting for table metadata lock | CREATE INDEX ix_states_attributes_id ON states (attributes_id) |    0.000 |
+----+------+-----------------+----------------+---------+------+---------------------------------+----------------------------------------------------------------+----------+
5 rows in set (0.000 sec)

And I verified that the new column from the previous query had actually been created:

MariaDB [home_assistant]> show create table states;
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| states | CREATE TABLE `states` (
  `state_id` int(11) NOT NULL AUTO_INCREMENT,
  `domain` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `entity_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `state` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `attributes` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `event_id` int(11) DEFAULT NULL,
  `last_changed` datetime(6) DEFAULT NULL,
  `last_updated` datetime(6) DEFAULT NULL,
  `created` datetime(6) DEFAULT NULL,
  `old_state_id` int(11) DEFAULT NULL,
  `attributes_id` int(20) DEFAULT NULL,
  PRIMARY KEY (`state_id`),
  KEY `ix_states_old_state_id` (`old_state_id`),
  KEY `ix_states_last_updated` (`last_updated`),
  KEY `ix_states_event_id` (`event_id`),
  KEY `ix_states_entity_id_last_updated` (`entity_id`,`last_updated`),
  CONSTRAINT `states_ibfk_1` FOREIGN KEY (`event_id`) REFERENCES `events` (`event_id`) ON DELETE CASCADE,
  CONSTRAINT `states_ibfk_2` FOREIGN KEY (`old_state_id`) REFERENCES `states` (`state_id`)
) ENGINE=InnoDB AUTO_INCREMENT=41396921 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.001 sec)

I stopped HA again, and kept running SHOW PROCESSLIST, and immediately after HA had stopped, the Waiting on metadata lock disappeared and the index was actually being created:

MariaDB [home_assistant]> show processlist;
+----+------+-----------------+----------------+---------+------+----------------+----------------------------------------------------------------+----------+
| Id | User | Host            | db             | Command | Time | State          | Info                                                           | Progress |
+----+------+-----------------+----------------+---------+------+----------------+----------------------------------------------------------------+----------+
| 36 | hass | localhost       | home_assistant | Query   |    0 | Init           | show processlist                                               |    0.000 |
| 40 | hass | localhost:53502 | home_assistant | Query   |  119 | Altering table | CREATE INDEX ix_states_attributes_id ON states (attributes_id) |    0.000 |
+----+------+-----------------+----------------+---------+------+----------------+----------------------------------------------------------------+----------+
2 rows in set (0.000 sec)

After the second stop, this was in the log:

2022-04-07 21:22:02 WARNING (Recorder) [homeassistant.components.recorder.migration] Database is about to upgrade. Schema version: 24
2022-04-07 21:22:02 WARNING (Recorder) [homeassistant.components.recorder.migration] Adding columns attributes_id to table states. Note: this can take several minutes on large databases and slow computers. Please be patient!
2022-04-07 21:22:02 WARNING (Recorder) [homeassistant.components.recorder.migration] Column attributes_id already exists on states, continuing
2022-04-07 21:22:02 WARNING (Recorder) [homeassistant.components.recorder.migration] Adding index `ix_states_attributes_id` to database. Note: this can take several minutes on large databases and slow computers. Please be patient!

I started HA again, and at that point, it seems like the migration went through (no more queries stuck in MariaDB at least):

2022-04-07 21:24:55 WARNING (Recorder) [homeassistant.components.recorder.migration] Database is about to upgrade. Schema version: 24
2022-04-07 21:24:55 WARNING (Recorder) [homeassistant.components.recorder.migration] Adding columns attributes_id to table states. Note: this can take several minutes on large databases and slow compute
rs. Please be patient!
2022-04-07 21:24:56 WARNING (Recorder) [homeassistant.components.recorder.migration] Column attributes_id already exists on states, continuing
2022-04-07 21:24:56 WARNING (Recorder) [homeassistant.components.recorder.migration] Adding index `ix_states_attributes_id` to database. Note: this can take several minutes on large databases and slow c
omputers. Please be patient!
2022-04-07 21:24:56 WARNING (Recorder) [homeassistant.components.recorder.migration] Index ix_states_attributes_id already exists on states, continuing

Also reported by another user on Discord: https://discord.com/channels/330944238910963714/332167321311510530/961616149475385455

$ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
$ mysql -V
mysql  Ver 15.1 Distrib 10.3.34-MariaDB, for debian-linux-gnueabihf (armv8l) using readline 5.2

What version of Home Assistant Core has the issue?

core-2022.4.0

What was the last working version of Home Assistant Core?

core-2022.3.7

What type of installation are you running?

Home Assistant Container

Integration causing the issue

recorder

Link to integration documentation on our website

https://www.home-assistant.io/integrations/recorder/

Diagnostics information

No response

Example YAML snippet

recorder:
  db_url: !secret mariadb_url
  purge_keep_days: 30
  exclude:
    entities:
      - sensor.date_time
      - sensor.time

mariadb_url: mysql://hass:xxxx@127.0.0.1/home_assistant?charset=utf8mb4

Anything in the logs that might be useful for us?

2022-04-07 21:10:54 WARNING (Recorder) [homeassistant.components.recorder.migration] Database is about to upgrade. Schema version: 24
2022-04-07 21:10:54 WARNING (Recorder) [homeassistant.components.recorder.migration] Adding columns attributes_id to table states. Note: this can take several minutes on large databases and slow computers. Please be patient!

Additional information

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 55 (16 by maintainers)

Most upvoted comments

Sounds good, this one is all over the place, and there is no definitive single problem here. Let’s close this one out as anyone who comments here is likely not get useful help since there is so much unrelated information and really should open a new issue.

@mojomojoman - for what its worth, here is my MariaDB log

2022-04-11 14:49:40 0 [Note] /usr/bin/mysqld (mysqld 10.4.19-MariaDB) starting as process 1199 … 2022-04-11 14:49:40 0 [Note] InnoDB: Using Linux native AIO 2022-04-11 14:49:40 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2022-04-11 14:49:40 0 [Note] InnoDB: Uses event mutexes 2022-04-11 14:49:40 0 [Note] InnoDB: Compressed tables use zlib 1.2.11 2022-04-11 14:49:40 0 [Note] InnoDB: Number of pools: 1 2022-04-11 14:49:40 0 [Note] InnoDB: Using generic crc32 instructions 2022-04-11 14:49:40 0 [Note] mysqld: O_TMPFILE is not supported on /var/tmp (disabling future attempts) 2022-04-11 14:49:40 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M 2022-04-11 14:49:40 0 [Note] InnoDB: Completed initialization of buffer pool 2022-04-11 14:49:40 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority(). 2022-04-11 14:49:40 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=61843841061 2022-04-11 14:49:40 0 [Note] InnoDB: Starting final batch to recover 9 pages from redo log. 2022-04-11 14:49:41 0 [Note] InnoDB: 128 out of 128 rollback segments are active. 2022-04-11 14:49:41 0 [Note] InnoDB: Removed temporary tablespace data file: “ibtmp1” 2022-04-11 14:49:41 0 [Note] InnoDB: Creating shared tablespace for temporary tables 2022-04-11 14:49:41 0 [Note] InnoDB: Setting file ‘./ibtmp1’ size to 12 MB. Physically writing the file full; Please wait … 2022-04-11 14:49:41 0 [Note] InnoDB: File ‘./ibtmp1’ size is now 12 MB. 2022-04-11 14:49:41 0 [Note] InnoDB: Waiting for purge to start 2022-04-11 14:49:41 0 [Note] InnoDB: 10.4.19 started; log sequence number 61843841102; transaction id 28648684 2022-04-11 14:49:41 0 [Note] InnoDB: Loading buffer pool(s) from /data/databases/ib_buffer_pool 2022-04-11 14:49:41 0 [Note] Plugin ‘FEEDBACK’ is disabled. 2022-04-11 14:49:41 0 [Note] Server socket created on IP: ‘::’. 2022-04-11 14:49:41 0 [Note] Reading of all Master_info entries succeeded 2022-04-11 14:49:41 0 [Note] Added new Master_info ‘’ to hash table 2022-04-11 14:49:41 0 [Note] /usr/bin/mysqld: ready for connections. Version: ‘10.4.19-MariaDB’ socket: ‘/run/mysqld/mysqld.sock’ port: 3306 MariaDB Server 2022-04-11 14:49:44 0 [Note] InnoDB: Buffer pool(s) load completed at 220411 14:49:44 2022-04-11 14:54:25 8 [Warning] InnoDB: Retry attempts for reading partial data failed. 2022-04-11 14:54:25 8 [ERROR] InnoDB: Tried to read 16384 bytes at offset 490094592, but was only able to read 0 2022-04-11 14:54:25 8 [ERROR] InnoDB: Operating system error number 5 in a file operation. 2022-04-11 14:54:25 8 [ERROR] InnoDB: Error number 5 means ‘I/O error’ 2022-04-11 14:54:25 8 [Note] InnoDB: Some operating system error numbers are described at https://mariadb.com/kb/en/library/operating-system-error-codes/ 2022-04-11 14:54:25 8 [ERROR] InnoDB: File (unknown): ‘read’ returned OS error 205. Cannot continue operation 220411 14:54:27 mysqld_safe Number of processes running now: 0 220411 14:54:27 mysqld_safe mysqld restarted 2022-04-11 14:54:27 0 [Note] /usr/bin/mysqld (mysqld 10.4.19-MariaDB) starting as process 1253 … 2022-04-11 14:54:27 0 [Note] InnoDB: Using Linux native AIO 2022-04-11 14:54:27 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2022-04-11 14:54:27 0 [Note] InnoDB: Uses event mutexes 2022-04-11 14:54:27 0 [Note] InnoDB: Compressed tables use zlib 1.2.11 2022-04-11 14:54:27 0 [Note] InnoDB: Number of pools: 1 2022-04-11 14:54:27 0 [Note] InnoDB: Using generic crc32 instructions 2022-04-11 14:54:27 0 [Note] mysqld: O_TMPFILE is not supported on /var/tmp (disabling future attempts) 2022-04-11 14:54:27 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M 2022-04-11 14:54:27 0 [Note] InnoDB: Completed initialization of buffer pool 2022-04-11 14:54:27 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority(). 2022-04-11 14:54:27 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=61843841619 2022-04-11 14:54:27 0 [Note] InnoDB: Starting final batch to recover 5 pages from redo log. 2022-04-11 14:54:28 0 [Note] InnoDB: 128 out of 128 rollback segments are active. 2022-04-11 14:54:28 0 [Note] InnoDB: Removed temporary tablespace data file: “ibtmp1” 2022-04-11 14:54:28 0 [Note] InnoDB: Creating shared tablespace for temporary tables 2022-04-11 14:54:28 0 [Note] InnoDB: Setting file ‘./ibtmp1’ size to 12 MB. Physically writing the file full; Please wait … 2022-04-11 14:54:28 0 [Note] InnoDB: File ‘./ibtmp1’ size is now 12 MB. 2022-04-11 14:54:28 0 [Note] InnoDB: Waiting for purge to start 2022-04-11 14:54:28 0 [Note] InnoDB: 10.4.19 started; log sequence number 61843842212; transaction id 28648692 2022-04-11 14:54:28 0 [Note] InnoDB: Loading buffer pool(s) from /data/databases/ib_buffer_pool 2022-04-11 14:54:28 0 [Note] Plugin ‘FEEDBACK’ is disabled. 2022-04-11 14:54:28 0 [Note] Server socket created on IP: ‘::’. 2022-04-11 14:54:28 0 [Note] Reading of all Master_info entries succeeded 2022-04-11 14:54:28 0 [Note] Added new Master_info ‘’ to hash table 2022-04-11 14:54:28 0 [Note] /usr/bin/mysqld: ready for connections. Version: ‘10.4.19-MariaDB’ socket: ‘/run/mysqld/mysqld.sock’ port: 3306 MariaDB Server 2022-04-11 14:54:28 0 [Note] InnoDB: Buffer pool(s) load completed at 220411 14:54:28 2022-04-11 15:53:34 64 [Warning] InnoDB: Retry attempts for reading partial data failed. 2022-04-11 15:53:34 64 [ERROR] InnoDB: Tried to read 16384 bytes at offset 490094592, but was only able to read 0 2022-04-11 15:53:34 64 [ERROR] InnoDB: Operating system error number 5 in a file operation. 2022-04-11 15:53:34 64 [ERROR] InnoDB: Error number 5 means ‘I/O error’ 2022-04-11 15:53:34 64 [Note] InnoDB: Some operating system error numbers are described at https://mariadb.com/kb/en/library/operating-system-error-codes/ 2022-04-11 15:53:34 64 [ERROR] InnoDB: File (unknown): ‘read’ returned OS error 205. Cannot continue operation 220411 15:53:36 mysqld_safe Number of processes running now: 0 220411 15:53:36 mysqld_safe mysqld restarted 2022-04-11 15:53:36 0 [Note] /usr/bin/mysqld (mysqld 10.4.19-MariaDB) starting as process 1310 … 2022-04-11 15:53:36 0 [Note] InnoDB: Using Linux native AIO 2022-04-11 15:53:36 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2022-04-11 15:53:36 0 [Note] InnoDB: Uses event mutexes 2022-04-11 15:53:36 0 [Note] InnoDB: Compressed tables use zlib 1.2.11 2022-04-11 15:53:36 0 [Note] InnoDB: Number of pools: 1 2022-04-11 15:53:36 0 [Note] InnoDB: Using generic crc32 instructions 2022-04-11 15:53:36 0 [Note] mysqld: O_TMPFILE is not supported on /var/tmp (disabling future attempts) 2022-04-11 15:53:36 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M 2022-04-11 15:53:36 0 [Note] InnoDB: Completed initialization of buffer pool 2022-04-11 15:53:36 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority(). 2022-04-11 15:53:36 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=61843845996 2022-04-11 15:53:36 0 [Note] InnoDB: Starting final batch to recover 5 pages from redo log. 2022-04-11 15:53:36 0 [Note] InnoDB: 128 out of 128 rollback segments are active. 2022-04-11 15:53:36 0 [Note] InnoDB: Removed temporary tablespace data file: “ibtmp1” 2022-04-11 15:53:36 0 [Note] InnoDB: Creating shared tablespace for temporary tables 2022-04-11 15:53:36 0 [Note] InnoDB: Setting file ‘./ibtmp1’ size to 12 MB. Physically writing the file full; Please wait … 2022-04-11 15:53:36 0 [Note] InnoDB: File ‘./ibtmp1’ size is now 12 MB. 2022-04-11 15:53:36 0 [Note] InnoDB: Waiting for purge to start 2022-04-11 15:53:36 0 [Note] InnoDB: 10.4.19 started; log sequence number 61843846594; transaction id 28648734 2022-04-11 15:53:36 0 [Note] InnoDB: Loading buffer pool(s) from /data/databases/ib_buffer_pool 2022-04-11 15:53:36 0 [Note] Plugin ‘FEEDBACK’ is disabled. 2022-04-11 15:53:36 0 [Note] Server socket created on IP: ‘::’. 2022-04-11 15:53:36 0 [Note] Reading of all Master_info entries succeeded 2022-04-11 15:53:36 0 [Note] Added new Master_info ‘’ to hash table 2022-04-11 15:53:36 0 [Note] /usr/bin/mysqld: ready for connections. Version: ‘10.4.19-MariaDB’ socket: ‘/run/mysqld/mysqld.sock’ port: 3306 MariaDB Server 2022-04-11 15:53:37 0 [Note] InnoDB: Buffer pool(s) load completed at 220411 15:53:37