Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error executing post migrate states entity_ids to states_meta #90892

Closed
vturekhanov opened this issue Apr 6, 2023 · 12 comments · Fixed by #90895
Closed

Error executing post migrate states entity_ids to states_meta #90892

vturekhanov opened this issue Apr 6, 2023 · 12 comments · Fixed by #90895

Comments

@vturekhanov
Copy link

The problem

During first start after upgrading from 2023.3.6 to 2023.4.0 got a following errors in HA log.
I use external MySQL server, version 8.0.30.

What version of Home Assistant Core has the issue?

core-2023.4.0

What was the last working version of Home Assistant Core?

core-2023.3.6

What type of installation are you running?

Home Assistant OS

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

No response

Anything in the logs that might be useful for us?

2023-04-06 08:36:47.152 ERROR (Recorder) [homeassistant.components.recorder.util] Error executing query: (MySQLdb.OperationalError) (1093, "You can't specify target table 'states' for update in FROM clause")
[SQL: UPDATE states SET entity_id=%s WHERE states.state_id IN (SELECT states.state_id 
FROM states INNER JOIN (SELECT states.state_id AS state_id_with_entity_id 
FROM states 
WHERE states.entity_id IS NOT NULL 
 LIMIT %s) AS anon_1 ON states.state_id = anon_1.state_id_with_entity_id)]
[parameters: (None, 5000)]
(Background on this error at: https://sqlalche.me/e/20/e3q8)
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 748, in do_execute
    cursor.execute(statement, parameters)
  File "/usr/local/lib/python3.10/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/usr/local/lib/python3.10/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/usr/local/lib/python3.10/site-packages/MySQLdb/connections.py", line 254, in query
    _mysql.connection.query(self, query)
MySQLdb.OperationalError: (1093, "You can't specify target table 'states' for update in FROM clause")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/recorder/util.py", line 129, in session_scope
    yield session
  File "/usr/src/homeassistant/homeassistant/components/recorder/migration.py", line 1580, in post_migrate_entity_ids
    cursor_result = session.connection().execute(batch_cleanup_entity_ids())
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1414, in execute
    return meth(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/sql/lambdas.py", line 605, in _execute_on_connection
    return connection._execute_clauseelement(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1638, in _execute_clauseelement
    ret = self._execute_context(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1842, in _execute_context
    return self._exec_single_context(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1983, in _exec_single_context
    self._handle_dbapi_exception(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2326, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 748, in do_execute
    cursor.execute(statement, parameters)
  File "/usr/local/lib/python3.10/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/usr/local/lib/python3.10/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/usr/local/lib/python3.10/site-packages/MySQLdb/connections.py", line 254, in query
    _mysql.connection.query(self, query)
sqlalchemy.exc.OperationalError: (MySQLdb.OperationalError) (1093, "You can't specify target table 'states' for update in FROM clause")
[SQL: UPDATE states SET entity_id=%s WHERE states.state_id IN (SELECT states.state_id 
FROM states INNER JOIN (SELECT states.state_id AS state_id_with_entity_id 
FROM states 
WHERE states.entity_id IS NOT NULL 
 LIMIT %s) AS anon_1 ON states.state_id = anon_1.state_id_with_entity_id)]
[parameters: (None, 5000)]
(Background on this error at: https://sqlalche.me/e/20/e3q8)
2023-04-06 08:36:47.222 WARNING (Recorder) [homeassistant.components.recorder.util] Error executing post migrate states entity_ids to states_meta: (MySQLdb.OperationalError) (1093, "You can't specify target table 'states' for update in FROM clause")
[SQL: UPDATE states SET entity_id=%s WHERE states.state_id IN (SELECT states.state_id 
FROM states INNER JOIN (SELECT states.state_id AS state_id_with_entity_id 
FROM states 
WHERE states.entity_id IS NOT NULL 
 LIMIT %s) AS anon_1 ON states.state_id = anon_1.state_id_with_entity_id)]
[parameters: (None, 5000)]
(Background on this error at: https://sqlalche.me/e/20/e3q8)
2023-04-06 08:42:24.241 ERROR (MainThread) [frontend.js.latest.202304050] :0:0 ResizeObserver loop completed with undelivered notifications.

Additional information

No response

@home-assistant
Copy link

home-assistant bot commented Apr 6, 2023

Hey there @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (recorder) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of recorder can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign recorder Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


recorder documentation
recorder source
(message by IssueLinks)

@bdraco
Copy link
Member

bdraco commented Apr 6, 2023

I just tried that query on MariaDB and it works fine

MariaDB [hass]> UPDATE 
    ->   states 
    -> SET 
    ->   entity_id = NULL 
    -> WHERE 
    ->   states.state_id IN (
    ->     SELECT 
    ->       states.state_id 
    ->     FROM 
    ->       states 
    ->       INNER JOIN (
    ->         SELECT 
    ->           states.state_id AS state_id_with_entity_id 
    ->         FROM 
    ->           states 
    ->         WHERE 
    ->           states.entity_id IS NOT NULL 
    ->         LIMIT 
    ->           5000
    ->       ) AS anon_1 ON states.state_id = anon_1.state_id_with_entity_id
    ->   );

Query OK, 5000 rows affected (2.540 sec)
Rows matched: 5000  Changed: 5000  Warnings: 0

@bdraco
Copy link
Member

bdraco commented Apr 6, 2023

Does it fail if you run it manually?

UPDATE 
  states 
SET 
  entity_id = NULL 
WHERE 
  states.state_id IN (
    SELECT 
      states.state_id 
    FROM 
      states 
      INNER JOIN (
        SELECT 
          states.state_id AS state_id_with_entity_id 
        FROM 
          states 
        WHERE 
          states.entity_id IS NOT NULL 
        LIMIT 
          5000
      ) AS anon_1 ON states.state_id = anon_1.state_id_with_entity_id
  );

@bdraco
Copy link
Member

bdraco commented Apr 6, 2023

Also if it does fail,

    SELECT 
      states.state_id 
    FROM 
      states 
      INNER JOIN (
        SELECT 
          states.state_id AS state_id_with_entity_id 
        FROM 
          states 
        WHERE 
          states.entity_id IS NOT NULL 
        LIMIT 
          5000
      ) AS anon_1 ON states.state_id = anon_1.state_id_with_entity_id

fail as well?

@bdraco
Copy link
Member

bdraco commented Apr 6, 2023

@bdraco
Copy link
Member

bdraco commented Apr 6, 2023

Similar issue from the past #11713

bdraco added a commit that referenced this issue Apr 6, 2023
This query worked fine on MariaDB but failed on MySQL 8.0.30

fixes #90892
@vturekhanov
Copy link
Author

Does it fail if you run it manually?

UPDATE 
  states 
SET 
  entity_id = NULL 
WHERE 
  states.state_id IN (
    SELECT 
      states.state_id 
    FROM 
      states 
      INNER JOIN (
        SELECT 
          states.state_id AS state_id_with_entity_id 
        FROM 
          states 
        WHERE 
          states.entity_id IS NOT NULL 
        LIMIT 
          5000
      ) AS anon_1 ON states.state_id = anon_1.state_id_with_entity_id
  );

Fail

@vturekhanov
Copy link
Author

Also if it does fail,

    SELECT 
      states.state_id 
    FROM 
      states 
      INNER JOIN (
        SELECT 
          states.state_id AS state_id_with_entity_id 
        FROM 
          states 
        WHERE 
          states.entity_id IS NOT NULL 
        LIMIT 
          5000
      ) AS anon_1 ON states.state_id = anon_1.state_id_with_entity_id

fail as well?

It works.

@bdraco
Copy link
Member

bdraco commented Apr 6, 2023

Well thats annoying that it works on MariaDB but fails on MySQL

@bdraco
Copy link
Member

bdraco commented Apr 6, 2023

This problem won't actually cause any breakage. The worst that happens is there is some data that is not cleaned up until old rows are purged during normal nightly purge.

@bdraco
Copy link
Member

bdraco commented Apr 6, 2023

We will get the query adjusted to work with MySQL. Thanks for the report

@bdraco
Copy link
Member

bdraco commented Apr 6, 2023

I also opened #90898 to add MySQL to the CI since we currently only test against MariaDB

@github-actions github-actions bot locked and limited conversation to collaborators May 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants