:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(mysql): treat MariaDB's max_statement_time timeout as a bad-plan symptom (#113010)
Tom Owersee8b32ad2b53
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.pyindex 0959553f5271..8b15836380e8 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py@@ -147,6 +147,13 @@ _OUT_OF_SORT_MEMORY_CODE = 1038# filesort entirely, so the same FORCE INDEX fallback resolves it._QUERY_EXECUTION_TIME_EXCEEDED_CODE = 3024+# pymysql error code for MariaDB's "Query execution was interrupted (max_statement_time+# exceeded)" — MariaDB's own `max_statement_time` session/global cap, the same bad plan+# (full scan + filesort over the incremental field) seen from a fourth side. It's MariaDB's+# equivalent of MySQL's `max_execution_time` (3024) above, so the same FORCE INDEX fallback+# resolves it.+_MAX_STATEMENT_TIME_EXCEEDED_CODE = 1969+# Raised in place of the raw pymysql 2013 when a lost-connection bad plan can't be dodged by the# FORCE INDEX fallback because the incremental field has no usable index. The un-indexed full-table# sort re-times-out on every run, so it's deterministic — distinct from a genuine transient mid-query@@ -340,7 +347,7 @@ def _is_bad_plan_error(e: pymysql.err.OperationalError) -> bool:"""Return True if the error is a symptom of MySQL filesorting the incremental`ORDER BY` instead of using an index — recoverable via the FORCE INDEX fallback.- Matches three codes, all signalling the optimizer picked a full scan + filesort+ Matches four codes, all signalling the optimizer picked a full scan + filesortover the incremental field:- `2013` (lost connection during query): the filesort preparation outran a@@ -349,13 +356,21 @@ def _is_bad_plan_error(e: pymysql.err.OperationalError) -> bool:`sort_buffer_size`.- `3024` (query execution was interrupted): the server's own `max_execution_time`cap killed the query before the filesort could finish.+ - `1969` (query execution was interrupted): MariaDB's own `max_statement_time`+ cap killed the query before the filesort could finish — MariaDB's equivalent+ of 3024.Forcing the incremental-field index makes MySQL read rows in index order and- skip the filesort, resolving all three. Other `OperationalError`s (access denied,+ skip the filesort, resolving all four. Other `OperationalError`s (access denied,table missing, etc.) should propagate untouched."""code = e.args[0] if e.args else None- return code in (_LOST_CONNECTION_DURING_QUERY_CODE, _OUT_OF_SORT_MEMORY_CODE, _QUERY_EXECUTION_TIME_EXCEEDED_CODE)+ return code in (+ _LOST_CONNECTION_DURING_QUERY_CODE,+ _OUT_OF_SORT_MEMORY_CODE,+ _QUERY_EXECUTION_TIME_EXCEEDED_CODE,+ _MAX_STATEMENT_TIME_EXCEEDED_CODE,+ )# Number of times `connect` will open a fresh pymysql connection before giving up. Matches the@@ -2102,9 +2117,9 @@ class MySQLImplementation(SQLSourceImplementation[MySQLSourceConfig, pymysql.Con)# A lost connection here recurs every run: with no usable index the incremental# sort is unavoidable and re-times-out. Re-raise it as a deterministic error so- # the schema is paused with guidance instead of looping. Out-of-sort-memory (1038)- # and query-execution-time-exceeded (3024) already carry their own stable, locale-- # independent codes, so leave those raw.+ # the schema is paused with guidance instead of looping. Out-of-sort-memory (1038),+ # query-execution-time-exceeded (3024), and MariaDB's max_statement_time-exceeded+ # (1969) already carry their own stable, locale-independent codes, so leave those raw.if e.args and e.args[0] == _LOST_CONNECTION_DURING_QUERY_CODE:raise MySQLUnavoidableFilesortError() from eraisediff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/source.pyindex 259869c83bcf..8e705cbe8a8f 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/source.py@@ -425,6 +425,16 @@ class MySQLSource(# fails identically. Match the locale-independent error code (the trailing message# text is translated on non-English servers)."(3024,": "Your MySQL/MariaDB server's maximum statement execution time was exceeded while ordering this table by its incremental field (error 3024). We try to avoid the sort by forcing the incremental field's index, but this table has no usable index on that field. Add an index on the incremental field, raise the server's 'max_execution_time', or switch this table to a full re-sync, then resync.",+ # MariaDB error 1969 (ER_STATEMENT_TIMEOUT): MariaDB's own `max_statement_time` cap+ # killed the `ORDER BY <incremental_field>` query before the filesort could finish —+ # the same symptom as 3024 above, just MariaDB's variant of the setting. We already+ # try to dodge the sort with the in-activity FORCE INDEX fallback (see+ # `_is_bad_plan_error`); this only escapes once that fallback can't apply — no usable+ # index on the incremental field. Both `max_statement_time` and the missing index are+ # static server-side state, so every retry filesorts the same rows and fails+ # identically. Match the locale-independent error code (the trailing message text is+ # translated on non-English servers).+ "(1969,": "Your MariaDB server's maximum statement execution time was exceeded while ordering this table by its incremental field (error 1969). We try to avoid the sort by forcing the incremental field's index, but this table has no usable index on that field. Add an index on the incremental field, raise the server's 'max_statement_time', or switch this table to a full re-sync, then resync.",# MySQL/MariaDB error 2013 (lost connection during query) that escapes the in-activity# FORCE INDEX fallback because the incremental field has no usable index (see# `MySQLUnavoidableFilesortError` in mysql.py). The un-indexed full-table sort re-times-outdiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.pyindex 75b7564049a2..812487fb588c 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py@@ -1215,6 +1215,14 @@ class TestIsBadPlanError:))+ def test_matches_error_1969_max_statement_time_exceeded(self):+ # MariaDB's max_statement_time cap killing the query is a fourth symptom+ # of the same full-scan-and-filesort plan — the FORCE INDEX fallback+ # resolves it too.+ assert _is_bad_plan_error(+ pymysql.err.OperationalError(1969, "Query execution was interrupted (max_statement_time exceeded)")+ )+@pytest.mark.parametrize("code,message",[@@ -2610,6 +2618,21 @@ class TestMySQLSourceNonRetryableErrors:is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())assert is_non_retryable, f"Query-execution-time-exceeded error should be non-retryable: {error_msg}"+ @pytest.mark.parametrize(+ "error_msg",+ [+ # Raw pymysql str(error) form (single-quoted tuple repr).+ str(pymysql.err.OperationalError(1969, "Query execution was interrupted (max_statement_time exceeded)")),+ # Temporal-wrapped form (double-quoted).+ 'OperationalError: (1969, "Query execution was interrupted (max_statement_time exceeded)")',+ ],+ )+ def test_max_statement_time_exceeded_is_non_retryable(self, source, error_msg):+ # MariaDB's variant of query-execution-time-exceeded (error 1969, rather than MySQL's 3024).+ non_retryable = source.get_non_retryable_errors()+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())+ assert is_non_retryable, f"Max-statement-time-exceeded error should be non-retryable: {error_msg}"+@pytest.mark.parametrize("error_msg",[