Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions databases/mysql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ Reconnecting costs a TCP connect, the TLS handshake and authentication: measured

A release is refused, with the reason, while the session holds anything a reconnect would destroy: an open transaction, a temporary table, a prepared statement, a `GET_LOCK`, `LOCK TABLES`, `FLUSH TABLES WITH READ LOCK`, an open `HANDLER`, a user variable, a changed session setting, a database selected with `USE`, or a stored routine call, whose body is opaque. The statements a dump runs inside `/*!40101 ... */` count, since the server executes them. The transaction comes from the server itself, so one opened by `SET autocommit = 0` or by an `XA START` counts as well.

## Rows affected

An `UPDATE` counts every row it matched, a row that already held the new value included: `UPDATE t SET a = 1` over two rows that both hold `1` reports 2 rows affected, and `ROW_COUNT()` returns 2. `INSERT ... ON DUPLICATE KEY UPDATE` onto a row that already holds the values reports 1, and 2 when it changes the row. The session is opened with `CLIENT_FOUND_ROWS`, which is how a [save](/features/change-tracking#saving) tells a row that held the value from a row that is gone. MariaDB, TiDB and OceanBase count the same way.

## When the server drops the connection

A read that fails because the server closed the connection is run once more on a new one, and only from a session holding nothing: the same list as above. Every other statement reports the error, because the second session answers from different state. Measured on MySQL 8.4.11, replaying regardless: `SELECT @total` came back `NULL` where it had come back 42, and `SELECT DATABASE()` came back as the connection's own database rather than the one `USE` had selected, neither of them raising anything. `LAST_INSERT_ID()`, `ROW_COUNT()`, `FOUND_ROWS()` and `CONNECTION_ID()` are never replayed either, whatever the session holds: their answer belongs to the connection that is gone.
Expand Down
4 changes: 2 additions & 2 deletions features/change-tracking.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ The statements a driver writes for itself, as SQL Server, Oracle and ClickHouse

Each generated SQL statement is held to the number of rows it was written for. On a table with no primary key, two identical rows cannot be told apart, so a statement meant for one of them matches both; the save stops there and reports what happened instead of rewriting the other row.

Such a statement is held to the other end too. Finding fewer rows than it was written for means the row it meant to write is no longer there, so the save rolls back and keeps the edits rather than reporting a success that wrote nothing. An edit keyed on a primary key is held to it on PostgreSQL, CockroachDB, Redshift, KingbaseES, SQLite, libSQL, Turso and DuckDB, whose count is the rows the statement matched: a row deleted or rekeyed since it was read, hidden from updates by a row-level security policy, or skipped by a trigger stops the save. An edit that follows a delete in the same save is not, since the delete can cascade to its row. MySQL, MariaDB, TiDB and OceanBase count only rows whose value changed, so there an edit is not held to it. Engines whose drivers report no real count, ClickHouse among them, are not held to it. Of the drivers that write their own statements, SQL Server and Oracle are held to it on a table without a primary key, and MongoDB and Redis are not. SQL Server bounds that match with `TOP (1)` and Oracle with `ROWNUM = 1`, so of two identical rows they write one.
Such a statement is held to the other end too. Finding fewer rows than it was written for means the row it meant to write is no longer there, so the save rolls back and keeps the edits rather than reporting a success that wrote nothing. An edit keyed on a primary key is held to it where the count is the rows the statement matched, a row that already held the value included: PostgreSQL, CockroachDB, Redshift, KingbaseES, SQLite, libSQL, Turso, DuckDB, MySQL, MariaDB, TiDB, OceanBase, SQL Server, Oracle, Snowflake and Dameng. There, a row deleted or rekeyed since it was read, hidden from updates by a row-level security policy, or skipped by a trigger stops the save. A delete whose row is already gone is not held to it. Neither is an edit that follows a delete in the same save, since the delete can cascade to its row, or an edit through a view, whose count comes from the view's trigger or rule. Engines whose drivers report no real count, ClickHouse among them, are not held to it. Of the drivers that write their own statements, SQL Server and Oracle hold an edit and a delete to it on a table without a primary key, and MongoDB and Redis do not. SQL Server bounds that match with `TOP (1)` and Oracle with `ROWNUM = 1`, so of two identical rows they write one.

Where a count cannot hold a statement to its row, the row is looked up right before the statement runs instead: on ClickHouse, Snowflake and Trino for every edit and delete, and on Dameng for a table without a primary key. Finding no row, or more than the change was made on, stops the save before anything is written and keeps the queue. A ClickHouse statement is also limited to the data part its row was found in, and the row is checked after it runs. See [ClickHouse](/databases/clickhouse#running-queries).
Where the key alone cannot name one row, the row is looked up right before the statement runs: on ClickHouse, Snowflake and Trino for every edit and delete, and on Dameng for a table without a primary key. Finding no row, or more than the change was made on, stops the save before anything is written and keeps the queue. Snowflake and Dameng then hold the statement to its count as well. A ClickHouse statement is also limited to the data part its row was found in, and the row is checked after it runs. See [ClickHouse](/databases/clickhouse#running-queries).

To drop the whole queue, choose **File > Discard Changes…** and confirm; a staged truncate or drop goes with it. Undoing the edits clears them too, and so does the **Discard Unsaved Changes?** prompt that appears when you refresh, sort, filter, change page, or close the tab with edits pending.

Expand Down
Loading