1
0
mirror of https://github.com/MariaDB/server.git synced 2025-04-21 20:06:07 +03:00

5144 Commits

Author SHA1 Message Date
Monty
b159f05a63 MDEV-31957 Concurrent ALTER and ANALYZE collecting statistics can result in stale statistical data
Fixed hang when renaming index to original name
2023-10-10 11:12:26 +03:00
Monty
e3b36b8f1b MDEV-31957 Concurrent ALTER and ANALYZE collecting statistics can result in stale statistical data
Example of what causes the problem:
T1: ANALYZE TABLE starts to collect statistics
T2: ALTER TABLE starts by deleting statistics for all changed fields,
    then creates a temp table and copies data to it.
T1: ANALYZE ends and writes to the statistics tables.
T2: ALTER TABLE renames temp table in place of the old table.

Now the statistics from analyze matches the old deleted tables.

Fixed by waiting to delete old statistics until ALTER TABLE is
the only one using the old table and ensure that rename of columns
can handle swapping of column names.

rename_columns_in_stat_table() (former rename_column_in_stat_tables())
now takes a list of columns to rename. It uses the following algorithm
to update column_stats to be able to handle circular renames

- While there are columns to be renamed and it is the first loop or
  last rename loop did change something.
  - Loop over all columns to be renamed
    - Change column name in column_stat
      - If fail because of duplicate key
      - If this is first change attempt for this column
         - Change column name to a temporary column name
         - If there was a conflicting row, replace it with the current row.
    else
     - Remove entry from column list

- Loop over all remaining columns in the list
 - Remove the conflicting row
 - Change column from temporary name to final name in column_stat

Other things:
- Don't flush tables for every operation. Only flush when all updates
  are done.
- Rename of columns was not handled in case of ALGORITHM=copy (old bug).
  - Fixed that we do not collect statistics for hidden hash columns
    used by UNIQUE constraint on long values.
  - Fixed that we do not collect statistics for blob columns referred by
    generated virtual columns. This was achieved by storing the fields for
    which we want to have statistics in table->has_value_set instead of
    in table->read_set.
- Rename of indexes was not handled for persistent statistics.
  - This is now handled similar as rename of columns. Renamed columns
    are now stored in 'rename_stat_indexes' and handled in
    Alter_info::delete_statistics() together with drooped indexes.
- ALTER TABLE .. ADD INDEX may instead of creating a new index rename
  an existing generated foreign key index. This was not reflected in
  the index_stats table because this was handled in
  mysql_prepare_create_table instead instead of in the mysql_alter() code.
  Fixed by adding a call in mysql_prepare_create_table() to drop the
  changed index.
  I also had to change the code that 'marked the index' to be ignored
  with code that would not destroy the original index name.

Reviewer: Sergei Petrunia <sergey@mariadb.com>
2023-10-03 08:25:30 +03:00
Sergei Golubchik
3928c7e29a Merge branch '11.2' into 11.3 2023-09-30 14:12:12 +02:00
Sergei Golubchik
37e854f34a Merge branch '11.1' into 11.2 2023-09-29 16:01:59 +02:00
Jan Lindström
f57deb314f MDEV-31660 : Assertion `client_state.transaction().active() in wsrep_append_key
At the moment we cannot support
wsrep_forced_binlog_format=[MIXED|STATEMENT]
during CREATE TABLE AS SELECT.
Statement will use ROW instead and give
a warning.

Signed-off-by: Julius Goryavsky <julius.goryavsky@mariadb.com>
2023-09-29 12:54:04 +02:00
Sergei Golubchik
3f6bccb888 Merge branch '10.11' into 11.0 2023-09-29 12:24:54 +02:00
Oleksandr Byelkin
2d29ccda1a MDEV-29771 Server crashes in check_sequence_fields upon CREATE TABLE .. SEQUENCE=1 AS SELECT ..
Pass name separately for sequence check because sequence can be created with
CREATE TABLE (see https://mariadb.com/kb/en/create-table/#sequence )
2023-09-27 08:54:26 +02:00
Sergei Golubchik
034848c6c2 Merge branch '10.10' into 10.11 2023-09-24 19:41:43 +02:00
Nikita Malyavin
28b4037242 Merge branch '11.2' into 11.3 2023-09-21 14:15:04 +04:00
Alexander Barkov
f5aae71661 MDEV-31606 Refactor check_db_name() to get a const argument
Problem:
Under terms of MDEV-27490, we'll update Unicode version used
to compare identifiers to 14.0.0. Unlike in the old Unicode version,
in the new version a string can grow during lower-case. We cannot
perform check_db_name() inplace any more.

Change summary:

- Allocate memory to store lower-cased identifiers in memory root

- Removing check_db_name() performing both in-place lower-casing and validation
  at the same time. Splitting it into two separate stages:
  * creating a memory-root lower-cased copy of an identifier
    (using new MEM_ROOT functions and Query_arena wrapper methods)
  * performing validation on a constant string
    (using Lex_ident_fs methods)

Implementation details:

- Adding a mysys helper function to allocate lower-cased strings on MEM_ROOT:

    lex_string_casedn_root()

  and a Query_arena wrappers for it:

    make_ident_casedn()
    make_ident_opt_casedn()

- Adding a Query_arena method to perform both MEM_ROOT lower-casing and
  database name validation at the same time:

    to_ident_db_internal_with_error()

  This method is very close to the old (pre-11.3) check_db_name(),
  but performs lower-casing to a newly allocated MEM_ROOT
  memory (instead of performing lower-casing the original string in-place).

- Adding a Table_ident method which additionally handles derived table names:

    to_ident_db_internal_with_error()

- Removing the old check_db_name()
2023-09-13 11:04:27 +04:00
Marko Mäkelä
0dd25f28f7 Merge 10.5 into 10.6 2023-09-11 14:46:39 +03:00
Marko Mäkelä
f8f7d9de2c Merge 10.4 into 10.5 2023-09-11 11:29:31 +03:00
Sergei Golubchik
65b3c89430 MDEV-32015 insert into an empty table fails with hash unique
don't enable bulk insert when table->s->long_unique_table
2023-09-06 22:38:41 +02:00
Alexander Barkov
cb37c99dd8 MDEV-32019 Replace my_casedn_str(local_buffer) to CharBuffer::copy_casedn()
Replacing my_casedn_str() called on local char[] buffer variables
to CharBuffer::copy_casedn() calls.

This is a sub-task for MDEV-31531 Remove my_casedn_str()

Details:
- Adding a helper template class IdentBuffer (a CharBuffer descendant),
  which assumes utf8 data. Like CharBuffer, it's initialized to an empty
  string in the constructor, but can be populated with lower-cased data
  later.

- Adding a helper template class IdentBufferCasedn, which initializes
  to lower case right in the constructor.

- Removing char[] buffers, replacing them to IdentBuffer and IdentBufferCasedn.

- Changing the data type of "db" and "table" parameters from
  "const char*" to LEX_CSTRING in the following functions:

    find_field_in_table_ref()
    insert_fields()
    set_thd_db()
    mysql_grant()

  to reuse IdentBuffer easeir.
2023-08-26 17:46:14 +04:00
Thirunarayanan Balathandayuthapani
bf3b787e02 MDEV-31835 Remove unnecessary extra HA_EXTRA_IGNORE_INSERT call
- This commit is different from 10.6 commit c438284863db2ccba8a04437c941a5c8a2d9225b.
Due to Commit 045757af4c301757ba449269351cc27b1691a7d6 (MDEV-24621),
InnoDB does buffer and pre-sort the records for each index, and build
the indexes one page at a time.

Multiple large insert ignore statment aborts the server during bulk
insert operation. Problem is that InnoDB merge record exceeds
the page size. To avoid this scenario, InnoDB should catch
too big record while buffering the insert operation itself.

row_merge_buf_encode(): returns length of the encoded index record

row_merge_buf_write(): Catches the DB_TOO_BIG_RECORD earlier and
returns error
2023-08-25 23:13:05 +05:30
Thirunarayanan Balathandayuthapani
c438284863 MDEV-31835 Remove unnecesary extra HA_EXTRA_IGNORE_INSERT call
- HA_EXTRA_IGNORE_INSERT call is being called for every inserted row,
and on partitioned tables on every row * every partition.
This leads to slowness during load..data operation

- Under bulk operation, multiple insert statement error handling
will end up emptying the table. This behaviour introduced by the
commit 8ea923f55b7666a359ac2c54f6c10e8609d16846 (MDEV-24818).
This makes the HA_EXTRA_IGNORE_INSERT call redundant. We can
use the same behavior for insert..ignore statement as well.

- Removed the extra call HA_EXTRA_IGNORE_INSERT as the solution
to improve the performance of load command.
2023-08-25 17:22:17 +05:30
Sergei Golubchik
18ddde4826 Merge branch '11.1' into 11.2 2023-08-18 00:59:16 +02:00
Nikita Malyavin
8aa1a9e6a7 MDEV-31812 Add switch to old_mode to disable non-locking ALTER
Add LOCK_ALTER_TABE_COPY bit to old_mode. Disables online copy by default,
but still allows to force it with explicit lock=none
2023-08-15 14:00:28 +02:00
Nikita Malyavin
a1af525588 MDEV-31804 Assertion `thd->m_transaction_psi == __null' fails
... upon replicating online ALTER

When an online event is applied and slave_exec_mode is idempotent,
Write_rows_log_event::do_before_row_operations had reset
thd->lex->sql_command to SQLCOM_REPLACE.

This led to that a statement was detected as a row-type during binlogging,
and was logged as not standalone.

So the corresponding Gtid_log_event, when applied on replica, did not exit
early and created a new PSI transaction. Hence the difference with
non-online ALTER.
2023-08-15 14:00:28 +02:00
Nikita Malyavin
30c965f866 MDEV-31777 ER_GET_ERRNO upon online alter on CONNECT table
Forbid Online for CONNECT.
2023-08-15 14:00:28 +02:00
Nikita Malyavin
44ca37ef17 MDEV-31631 Adding auto-increment to table with history online misbehaves
Adding an auto_increment column online leads to an undefined behavior.
Basically any DEFAULTs that depend on a row order in the table, or on
the non-deterministic (in scope of the ALTER TABLE statement) function
is UB.

For example, NOW() is considered generally non-deterministic
(Item_func_now_utc is marked with VCOL_NON_DETERMINISTIC), but it's fixed
in scope of a single statement.

Same for any other function that depends only on the session/status vars
apart from its arguments.

Only two UB cases are known:
* adding new AUTO_INCREMENT column. Modifying the existing column may be
fine under certain circumstances, see MDEV-31058.
* adding new column with DEFAULT(nextval(...)). Modifying the existing
column is possible, since its value will be always present in the online
event, except for the NULL -> NOT NULL modification
2023-08-15 14:00:28 +02:00
Nikita Malyavin
e026a366bf MDEV-31776 Online ALTER reports the number of affected rows incorrectly
Add a new virtual function that will increase the inserted rows count
for the insert log event and decrease it for the delete event.

Reuses Rows_log_event::m_row_count on the replication side, which was only
set on the logging side.
2023-08-15 14:00:28 +02:00
Nikita Malyavin
9c8554259a MDEV-31775 Server crash upon online alter on sequence
Forbid online for sequences. It doesn't work now and sequence tables are
always only one row, so not worth the extra complexity.
2023-08-15 14:00:28 +02:00
Nikita Malyavin
2952274ac4 make a proper cleanup if online_alter_binlog is failed to create 2023-08-15 14:00:28 +02:00
Andrei
bac8f189ed MDEV-31755 Replica's DML event deadlocks wit online alter table
The deadlock was caused by too strong MDL acquired by the start ALTER.

Replica's ALTER TABLE replication consists of two phases:
1. Start ALTER (SA) -- the event is emittd in the very beginning,
allowing replication start ALTER in parallel
2. Commit ALTER (CA) -- ensures that master finishes successfully

CA is normally received by wait_for_master call.
If parallel DML was run, the following sequence will take place:

|- SA
|- DML
|- CA

If CA is handled after MDL upgrade, it'll will deadlock with DML.

While MDL is shared by the start ALTER wait for its 2nd part
to allow concurrent DMLs to grab the lock.

The fix uses wait_for_master reentrancy -- no need to avoid a second call
in the end of mysql_alter_table.

Since SA and CA are marked with FL_DDL, the DML issued in-between cannot be
rescheduled before or after them. However, SA "commits" (by he call of
write_bin_log_start_alter and, subsequently,
thd->wakeup_subsequent_commits) before the copy stage begins, unlocking
the DMLs to run on this table. That is, these DMLs will be executed
concurrently with the copy stage, making Online alter effective on replicas
as well

Co-authored-by: Nikita Malyavin (nikitamalyavin@gmail.com)
2023-08-15 14:00:28 +02:00
Nikita Malyavin
a539fac8bd MDEV-31646 untie from max_allowed_packet and opt_binlog_rows_event_max_size 2023-08-15 14:00:28 +02:00
Nikita Malyavin
d5e59c983f MDEV-31646 Online alter applies binlog cache limit to cache writes
1. Make online disk writes unlimited, same as filesort does.
2. Make proper error handling -- in 32-bit build IO_CACHE capacity limit is
4GB, so it is quite possible to overfill there.
3. Event_log::write_cache complicated with event reparsing, and as it was
proven by QA, contains some mistakes. Rewrite introbuce a simpler and much
faster version, not featuring reparsing and therefore copying a whole
buffer at once. This also disables checksums and crypto.
4. Handle read_log_event errors correctly: error returned is -1 (eof
signal for alter table), and my_error is not called. Call my_error and
always return 1. There's no test for this, since it shouldn't happen,
see the next bullet.
5. An event could be written partially in case of error, if it's bigger
than the IO_CACHE buffer. Restore the position where it was before the
error was emitted.

As a result, online alter is untied of several binlog variables, which was
a second aim of this patch.
2023-08-15 13:59:07 +02:00
Nikita Malyavin
2cecb5a638 MDEV-31601 Some ALTER TABLEs fail ... with a wrong error message
Report correct algorithm in the error message.
2023-08-15 10:16:14 +02:00
Nikita Malyavin
43cb98b420 fix main.mysql57_virtual, main.alter_table, innodb.alter_algorithm
The correct (best) algorithm is now chosen for ALGORITHM=DEFAULT
and alter_algorithm=DEFAULT

See also MDEV-30906
2023-08-15 10:16:13 +02:00
Nikita Malyavin
c382de72ea MDEV-30984 Online ALTER table is denied with non-informative error messages
Group all the checks in online_alter_check_supported().

There is now two groups of checks:
1. A technical availability of online, that is checked before open_tables,
and affects table_list->lock_type. It's supposed to be safe to make it
TL_READ even if COPY algorithm will fall back to not-online, since MDL is
SHARED_UPGRADEABLE anyway.
2. An 'online' availability for a COPY algorithm. It can be done as late as
just before the copy_data_between_tables call. The lock_type influence is
disclosed above, so the only other place it affects is
Alter_info::supports_lock, where `online` flag is only used to decide
whether to report the error at the inplace preparation stage. We'd want to
make that at the last resort, which is COPY preparation, if no algorithm is
chosen by the user. So it's event better now.

Some changes are required to the autoinc support detection, as the check
now happens after mysql_prepare_alter_table:
* alter_info->drop_list is empty
* instead, dropped columns are in tmp_set
* alter_info->create_list now has every field that's in the new table.
* the column definition's change.str will be nonnull whether the column
  remains in the new table (vs whether it was changed, as before).
  But it also has `field` field set.
* IF EXISTS doesn't have to be dealt anymore

This infers that the changes are now checked in more detail: a field's
definition shouldn't be changed, vs a field shouldn't be mentioned in
the CHANGE list, as it was before. This is reflected by the line 193 test.
2023-08-15 10:16:13 +02:00
Nikita Malyavin
500379cf49 unpack_row: unpack a correct number of fields 2023-08-15 10:16:13 +02:00
Nikita Malyavin
da277396bd MDEV-31058 ER_KEY_NOT_FOUND upon concurrent CHANGE column autoinc and DML
When column is changed to autoinc, ALTER TABLE may update zero/NULL values,
if NO_AUTO_VALUE_ON_ZERO mode is not enabled.

Forbid this for LOCK=NONE for the unreliable cases.
The cases are described in online_alter_check_autoinc.
2023-08-15 10:16:13 +02:00
Nikita Malyavin
e1f5c58ac7 MDEV-30891 Assertion `!table->versioned(VERS_TRX_ID)' failed
Assertion `!table->versioned(VERS_TRX_ID)' failed in
Write_rows_log_event::binlog_row_logging_function during ONLINE ALTER.

trxid-versioned tables can't be replicated.
ONLINE ALTER will also be forbidden for these tables.
2023-08-15 10:16:13 +02:00
Nikita Malyavin
b08b78c5b9 MDEV-29068 Cascade foreign key updates do not apply in online alter 2023-08-15 10:16:13 +02:00
Sergei Golubchik
6ecc90ae36 set table->pos_in_table_list in online alter 2023-08-15 10:16:13 +02:00
Sergei Golubchik
8a8f71b8b8 cleanup: ifdefs 2023-08-15 10:16:12 +02:00
Nikita Malyavin
6e0f456090 MDEV-29013 ER_KEY_NOT_FOUND/lock timeout upon online alter with long unique
1. ER_KEY_NOT_FOUND
general replcation problem, already fixed earlier.
test added.

2. ER_LOCK_WAIT_TIMEOUT
This is a long unique specific problem.

Sometimes, lookup_handler is created for to->file. To properly free it,
ha_reset should be called. It is usually done by calling
close_thread_table, but ALTER TABLE makes it differently. Hence, a single
ha_reset call is added to mysql_alter_table.

Also, event_mem_root is removed. Normally, no per-event data should be
allocated on thd->mem_root, that would mean a leak. And otherwise,
lookup_handler is lazily allocated, but its lifetime matches statement,
not event.
2023-08-15 10:16:12 +02:00
Sergei Golubchik
472c3d082f don't do ALTER IGNORE TABLE online
because online means we'll apply events from the binlog, and
ignore means that bad rows will be skipped. So a bad Write_row_log_event
will be skipped and a following Update_row_log_event will fail to
apply.
2023-08-15 10:16:12 +02:00
Sergei Golubchik
ea46fdcea4 cleanup, remove dead code 2023-08-15 10:16:12 +02:00
Sergei Golubchik
845c939601 MDEV-28943 Online alter fails under LOCK TABLE with ER_ALTER_OPERATION_NOT_SUPPORTED_REASON
if ALTER TABLE ... LOCK=xxx is executed under LOCK TABLES,
ignore the LOCK clause, because ALTER should not downgrade
already taken EXCLUSIVE table lock to SHARED or NONE.

This commit preserves the existing behavior (LOCK was de facto ignored),
but makes it explicit.
2023-08-15 10:16:12 +02:00
Nikita Malyavin
2ed03a41e6 MDEV-28930 ALTER TABLE Deadlocks with parallel TL_WRITE
ALTER ONLINE TABLE acquires table with TL_READ. Myisam normally acquires
TL_WRITE for DML, which makes it hang until table is freed.

We deadlock once ALTER upgrades its MDL lock.

Solution:
Unlock table earlier. We don't need to hold TL_READ once we finished
copying. Relay log replication requires no data locks on `from` table.
2023-08-15 10:16:12 +02:00
Sergei Golubchik
8fbdc76038 MDEV-28967 Assertion marked_for_write_or_computed()' failed in Field_new_decimal::store_value / online_alter_read_from_binlog
in the catch-up phase of the online alter we apply row events,
they're unpacked into `from->record[0]` and then converted
to `to->record[0]`.

This needs all fields of `from` to be in the `write_set`.

Although practically `Field::unpack()` does not assert the `write_set`,
and `Field::reset()` - used when a field value is not present in the
after-image - also doesn't assert the `write_set` for many types,
`Field_new_decimal::reset()` does.
2023-08-15 10:16:12 +02:00
Sergei Golubchik
93049e3de6 MDEV-28959 Online alter ignores strict table mode
* don't disable warnings when catching up
* do propagate warnings up like copy_data_between_tables() does
2023-08-15 10:16:12 +02:00
Sergei Golubchik
13f1e970a1 MDEV-28944 XA assertions failing in binlog_rollback and binlog_commit 2023-08-15 10:16:12 +02:00
Nikita Malyavin
8f6f219a68 control Cache_flip_event_log lifetime with reference count
If online alter fails, TABLE_SHARE can be freed while concurrent
transactions still have row events in their online_alter_cache_data.
On commit they try'll to flush them, writing to TABLE_SHARE's
Cache_flip_event_log, which is already freed.
This causes a crash in main.alter_table_online_debug test
2023-08-15 10:16:12 +02:00
Sergei Golubchik
62a1e282d0 MDEV-28771 Assertion `table->in_use&&tdc->flushed' failed after ALTER
don't simply set tdc->flushed, use flush_unused(1) that removes opened
but unused TABLE instances (that would otherwise prevent TABLE_SHARE from
being closed by keeping the ref_count>0).
2023-08-15 10:16:12 +02:00
Sergei Golubchik
b2be2e39a6 don't crash if ALTER TABLE fails and a long transaction blocks MDL upgrade 2023-08-15 10:16:11 +02:00
Sergei Golubchik
3099a756ab don't do DROP SYSTEM VERSIONING online
because ALTER TABLE ... DROP SYSTEM VERSIONING
is not just a change in the table structure, it also deletes
all historical rows
2023-08-15 10:16:11 +02:00
Sergei Golubchik
df0771c6a2 no ALTER TABLE should return ER_NO_DEFAULT_FOR_FIELD 2023-08-15 10:16:11 +02:00
Sergei Golubchik
a5776aa341 online alter always uses ALGORITHM=COPY, LOCK=NONE
so any other value of ALGORITHM or LOCK disables online alter
2023-08-15 10:16:11 +02:00