You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5090 lines
138 KiB

MDEV-22456 Dropping the adaptive hash index may cause DDL to lock up InnoDB If the InnoDB buffer pool contains many pages for a table or index that is being dropped or rebuilt, and if many of such pages are pointed to by the adaptive hash index, dropping the adaptive hash index may consume a lot of time. The time-consuming operation of dropping the adaptive hash index entries is being executed while the InnoDB data dictionary cache dict_sys is exclusively locked. It is not actually necessary to drop all adaptive hash index entries at the time a table or index is being dropped or rebuilt. We can let the LRU replacement policy of the buffer pool take care of this gradually. For this to work, we must detach the dict_table_t and dict_index_t objects from the main dict_sys cache, and once the last adaptive hash index entry for the detached table is removed (when the garbage page is evicted from the buffer pool) we can free the dict_table_t and dict_index_t object. Related to this, in MDEV-16283, we made ALTER TABLE...DISCARD TABLESPACE skip both the buffer pool eviction and the drop of the adaptive hash index. We shifted the burden to ALTER TABLE...IMPORT TABLESPACE or DROP TABLE. We can remove the eviction from DROP TABLE. We must retain the eviction in the ALTER TABLE...IMPORT TABLESPACE code path, so that in case the discarded table is being re-imported with the same tablespace identifier, the fresh data from the imported tablespace will replace any stale pages in the buffer pool. rpl.rpl_failed_drop_tbl_binlog: Remove the test. DROP TABLE can no longer be interrupted inside InnoDB. fseg_free_page(), fseg_free_step(), fseg_free_step_not_header(), fseg_free_page_low(), fseg_free_extent(): Remove the parameter that specifies whether the adaptive hash index should be dropped. btr_search_lazy_free(): Lazily free an index when the last reference to it is dropped from the adaptive hash index. buf_pool_clear_hash_index(): Declare static, and move to the same compilation unit with the bulk of the adaptive hash index code. dict_index_t::clone(), dict_index_t::clone_if_needed(): Clone an index that is being rebuilt while adaptive hash index entries exist. The original index will be inserted into dict_table_t::freed_indexes and dict_index_t::set_freed() will be called. dict_index_t::set_freed(), dict_index_t::freed(): Note that or check whether the index has been freed. We will use the impossible page number 1 to denote this condition. dict_index_t::n_ahi_pages(): Replaces btr_search_info_get_ref_count(). dict_index_t::detach_columns(): Move the assignment n_fields=0 to ha_innobase_inplace_ctx::clear_added_indexes(). We must have access to the columns when freeing the adaptive hash index. Note: dict_table_t::v_cols[] will remain valid. If virtual columns are dropped or added, the table definition will be reloaded in ha_innobase::commit_inplace_alter_table(). buf_page_mtr_lock(): Drop a stale adaptive hash index if needed. We will also reduce the number of btr_get_search_latch() calls and enclose some more code inside #ifdef BTR_CUR_HASH_ADAPT in order to benefit cmake -DWITH_INNODB_AHI=OFF.
6 years ago
12 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
12 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
11 years ago
10 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-20950 Reduce size of record offsets offset_t: this is a type which represents one record offset. It's unsigned short int. a lot of functions: replace ulint with offset_t btr_pcur_restore_position_func(), page_validate(), row_ins_scan_sec_index_for_duplicate(), row_upd_clust_rec_by_insert_inherit_func(), row_vers_impl_x_locked_low(), trx_undo_prev_version_build(): allocate record offsets on the stack instead of waiting for rec_get_offsets() to allocate it from mem_heap_t. So, reducing memory allocations. RECORD_OFFSET, INDEX_OFFSET: now it's less convenient to store pointers in offset_t* array. One pointer occupies now several offset_t. And those constant are start indexes into array to places where to store pointer values REC_OFFS_HEADER_SIZE: adjusted for the new reality REC_OFFS_NORMAL_SIZE: increase size from 100 to 300 which means less heap allocations. And sizeof(offset_t[REC_OFFS_NORMAL_SIZE]) now is 600 bytes which is smaller than previous 800 bytes. REC_OFFS_SEC_INDEX_SIZE: adjusted for the new reality rem0rec.h, rem0rec.ic, rem0rec.cc: various arguments, return values and local variables types were changed to fix numerous integer conversions issues. enum field_type_t: offset types concept was introduces which replaces old offset flags stuff. Like in earlier version, 2 upper bits are used to store offset type. And this enum represents those types. REC_OFFS_SQL_NULL, REC_OFFS_MASK: removed get_type(), set_type(), get_value(), combine(): these are convenience functions to work with offsets and it's types rec_offs_base()[0]: still uses an old scheme with flags REC_OFFS_COMPACT and REC_OFFS_EXTERNAL rec_offs_base()[i]: these have type offset_t now. Two upper bits contains type.
6 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-20950 Reduce size of record offsets offset_t: this is a type which represents one record offset. It's unsigned short int. a lot of functions: replace ulint with offset_t btr_pcur_restore_position_func(), page_validate(), row_ins_scan_sec_index_for_duplicate(), row_upd_clust_rec_by_insert_inherit_func(), row_vers_impl_x_locked_low(), trx_undo_prev_version_build(): allocate record offsets on the stack instead of waiting for rec_get_offsets() to allocate it from mem_heap_t. So, reducing memory allocations. RECORD_OFFSET, INDEX_OFFSET: now it's less convenient to store pointers in offset_t* array. One pointer occupies now several offset_t. And those constant are start indexes into array to places where to store pointer values REC_OFFS_HEADER_SIZE: adjusted for the new reality REC_OFFS_NORMAL_SIZE: increase size from 100 to 300 which means less heap allocations. And sizeof(offset_t[REC_OFFS_NORMAL_SIZE]) now is 600 bytes which is smaller than previous 800 bytes. REC_OFFS_SEC_INDEX_SIZE: adjusted for the new reality rem0rec.h, rem0rec.ic, rem0rec.cc: various arguments, return values and local variables types were changed to fix numerous integer conversions issues. enum field_type_t: offset types concept was introduces which replaces old offset flags stuff. Like in earlier version, 2 upper bits are used to store offset type. And this enum represents those types. REC_OFFS_SQL_NULL, REC_OFFS_MASK: removed get_type(), set_type(), get_value(), combine(): these are convenience functions to work with offsets and it's types rec_offs_base()[0]: still uses an old scheme with flags REC_OFFS_COMPACT and REC_OFFS_EXTERNAL rec_offs_base()[i]: these have type offset_t now. Two upper bits contains type.
6 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-20377: Make WITH_MSAN more usable MemorySanitizer (clang -fsanitize=memory) requires that all code be compiled with instrumentation enabled. The only exception is the C runtime library. Failure to use instrumented libraries will cause bogus messages about memory being uninitialized. In WITH_MSAN builds, we must avoid calling getservbyname(), because even though it is a standard library function, it is not instrumented, not even in clang 10. Note: Before MariaDB Server 10.5, ./mtr will typically fail due to the old PCRE library, which was updated in MDEV-14024. The following cmake options were tested on 10.5 in commit 94d0bb4dbeb28a94d1f87fdd55f4297ff3df0157: cmake \ -DCMAKE_C_FLAGS='-march=native -O2' \ -DCMAKE_CXX_FLAGS='-stdlib=libc++ -march=native -O2' \ -DWITH_EMBEDDED_SERVER=OFF -DWITH_UNIT_TESTS=OFF -DCMAKE_BUILD_TYPE=Debug \ -DWITH_INNODB_{BZIP2,LZ4,LZMA,LZO,SNAPPY}=OFF \ -DPLUGIN_{ARCHIVE,TOKUDB,MROONGA,OQGRAPH,ROCKSDB,CONNECT,SPIDER}=NO \ -DWITH_SAFEMALLOC=OFF \ -DWITH_{ZLIB,SSL,PCRE}=bundled \ -DHAVE_LIBAIO_H=0 \ -DWITH_MSAN=ON MEM_MAKE_DEFINED(): An alias for VALGRIND_MAKE_MEM_DEFINED() and __msan_unpoison(). MEM_GET_VBITS(), MEM_SET_VBITS(): Aliases for VALGRIND_GET_VBITS(), VALGRIND_SET_VBITS(), __msan_copy_shadow(). InnoDB: Replace the UNIV_MEM_ macros with corresponding MEM_ macros. ut_crc32_8_hw(), ut_crc32_64_low_hw(): Use the compiler built-in functions instead of inline assembler when building WITH_MSAN. This will require at least -msse4.2 when building for IA-32 or AMD64. The inline assembler would not be instrumented, and would thus cause bogus failures.
5 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-20377: Make WITH_MSAN more usable MemorySanitizer (clang -fsanitize=memory) requires that all code be compiled with instrumentation enabled. The only exception is the C runtime library. Failure to use instrumented libraries will cause bogus messages about memory being uninitialized. In WITH_MSAN builds, we must avoid calling getservbyname(), because even though it is a standard library function, it is not instrumented, not even in clang 10. Note: Before MariaDB Server 10.5, ./mtr will typically fail due to the old PCRE library, which was updated in MDEV-14024. The following cmake options were tested on 10.5 in commit 94d0bb4dbeb28a94d1f87fdd55f4297ff3df0157: cmake \ -DCMAKE_C_FLAGS='-march=native -O2' \ -DCMAKE_CXX_FLAGS='-stdlib=libc++ -march=native -O2' \ -DWITH_EMBEDDED_SERVER=OFF -DWITH_UNIT_TESTS=OFF -DCMAKE_BUILD_TYPE=Debug \ -DWITH_INNODB_{BZIP2,LZ4,LZMA,LZO,SNAPPY}=OFF \ -DPLUGIN_{ARCHIVE,TOKUDB,MROONGA,OQGRAPH,ROCKSDB,CONNECT,SPIDER}=NO \ -DWITH_SAFEMALLOC=OFF \ -DWITH_{ZLIB,SSL,PCRE}=bundled \ -DHAVE_LIBAIO_H=0 \ -DWITH_MSAN=ON MEM_MAKE_DEFINED(): An alias for VALGRIND_MAKE_MEM_DEFINED() and __msan_unpoison(). MEM_GET_VBITS(), MEM_SET_VBITS(): Aliases for VALGRIND_GET_VBITS(), VALGRIND_SET_VBITS(), __msan_copy_shadow(). InnoDB: Replace the UNIV_MEM_ macros with corresponding MEM_ macros. ut_crc32_8_hw(), ut_crc32_64_low_hw(): Use the compiler built-in functions instead of inline assembler when building WITH_MSAN. This will require at least -msse4.2 when building for IA-32 or AMD64. The inline assembler would not be instrumented, and would thus cause bogus failures.
5 years ago
MDEV-20377: Make WITH_MSAN more usable MemorySanitizer (clang -fsanitize=memory) requires that all code be compiled with instrumentation enabled. The only exception is the C runtime library. Failure to use instrumented libraries will cause bogus messages about memory being uninitialized. In WITH_MSAN builds, we must avoid calling getservbyname(), because even though it is a standard library function, it is not instrumented, not even in clang 10. Note: Before MariaDB Server 10.5, ./mtr will typically fail due to the old PCRE library, which was updated in MDEV-14024. The following cmake options were tested on 10.5 in commit 94d0bb4dbeb28a94d1f87fdd55f4297ff3df0157: cmake \ -DCMAKE_C_FLAGS='-march=native -O2' \ -DCMAKE_CXX_FLAGS='-stdlib=libc++ -march=native -O2' \ -DWITH_EMBEDDED_SERVER=OFF -DWITH_UNIT_TESTS=OFF -DCMAKE_BUILD_TYPE=Debug \ -DWITH_INNODB_{BZIP2,LZ4,LZMA,LZO,SNAPPY}=OFF \ -DPLUGIN_{ARCHIVE,TOKUDB,MROONGA,OQGRAPH,ROCKSDB,CONNECT,SPIDER}=NO \ -DWITH_SAFEMALLOC=OFF \ -DWITH_{ZLIB,SSL,PCRE}=bundled \ -DHAVE_LIBAIO_H=0 \ -DWITH_MSAN=ON MEM_MAKE_DEFINED(): An alias for VALGRIND_MAKE_MEM_DEFINED() and __msan_unpoison(). MEM_GET_VBITS(), MEM_SET_VBITS(): Aliases for VALGRIND_GET_VBITS(), VALGRIND_SET_VBITS(), __msan_copy_shadow(). InnoDB: Replace the UNIV_MEM_ macros with corresponding MEM_ macros. ut_crc32_8_hw(), ut_crc32_64_low_hw(): Use the compiler built-in functions instead of inline assembler when building WITH_MSAN. This will require at least -msse4.2 when building for IA-32 or AMD64. The inline assembler would not be instrumented, and would thus cause bogus failures.
5 years ago
MDEV-20377: Make WITH_MSAN more usable MemorySanitizer (clang -fsanitize=memory) requires that all code be compiled with instrumentation enabled. The only exception is the C runtime library. Failure to use instrumented libraries will cause bogus messages about memory being uninitialized. In WITH_MSAN builds, we must avoid calling getservbyname(), because even though it is a standard library function, it is not instrumented, not even in clang 10. Note: Before MariaDB Server 10.5, ./mtr will typically fail due to the old PCRE library, which was updated in MDEV-14024. The following cmake options were tested on 10.5 in commit 94d0bb4dbeb28a94d1f87fdd55f4297ff3df0157: cmake \ -DCMAKE_C_FLAGS='-march=native -O2' \ -DCMAKE_CXX_FLAGS='-stdlib=libc++ -march=native -O2' \ -DWITH_EMBEDDED_SERVER=OFF -DWITH_UNIT_TESTS=OFF -DCMAKE_BUILD_TYPE=Debug \ -DWITH_INNODB_{BZIP2,LZ4,LZMA,LZO,SNAPPY}=OFF \ -DPLUGIN_{ARCHIVE,TOKUDB,MROONGA,OQGRAPH,ROCKSDB,CONNECT,SPIDER}=NO \ -DWITH_SAFEMALLOC=OFF \ -DWITH_{ZLIB,SSL,PCRE}=bundled \ -DHAVE_LIBAIO_H=0 \ -DWITH_MSAN=ON MEM_MAKE_DEFINED(): An alias for VALGRIND_MAKE_MEM_DEFINED() and __msan_unpoison(). MEM_GET_VBITS(), MEM_SET_VBITS(): Aliases for VALGRIND_GET_VBITS(), VALGRIND_SET_VBITS(), __msan_copy_shadow(). InnoDB: Replace the UNIV_MEM_ macros with corresponding MEM_ macros. ut_crc32_8_hw(), ut_crc32_64_low_hw(): Use the compiler built-in functions instead of inline assembler when building WITH_MSAN. This will require at least -msse4.2 when building for IA-32 or AMD64. The inline assembler would not be instrumented, and would thus cause bogus failures.
5 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-11369 Instant ADD COLUMN for InnoDB For InnoDB tables, adding, dropping and reordering columns has required a rebuild of the table and all its indexes. Since MySQL 5.6 (and MariaDB 10.0) this has been supported online (LOCK=NONE), allowing concurrent modification of the tables. This work revises the InnoDB ROW_FORMAT=REDUNDANT, ROW_FORMAT=COMPACT and ROW_FORMAT=DYNAMIC so that columns can be appended instantaneously, with only minor changes performed to the table structure. The counter innodb_instant_alter_column in INFORMATION_SCHEMA.GLOBAL_STATUS is incremented whenever a table rebuild operation is converted into an instant ADD COLUMN operation. ROW_FORMAT=COMPRESSED tables will not support instant ADD COLUMN. Some usability limitations will be addressed in subsequent work: MDEV-13134 Introduce ALTER TABLE attributes ALGORITHM=NOCOPY and ALGORITHM=INSTANT MDEV-14016 Allow instant ADD COLUMN, ADD INDEX, LOCK=NONE The format of the clustered index (PRIMARY KEY) is changed as follows: (1) The FIL_PAGE_TYPE of the root page will be FIL_PAGE_TYPE_INSTANT, and a new field PAGE_INSTANT will contain the original number of fields in the clustered index ('core' fields). If instant ADD COLUMN has not been used or the table becomes empty, or the very first instant ADD COLUMN operation is rolled back, the fields PAGE_INSTANT and FIL_PAGE_TYPE will be reset to 0 and FIL_PAGE_INDEX. (2) A special 'default row' record is inserted into the leftmost leaf, between the page infimum and the first user record. This record is distinguished by the REC_INFO_MIN_REC_FLAG, and it is otherwise in the same format as records that contain values for the instantly added columns. This 'default row' always has the same number of fields as the clustered index according to the table definition. The values of 'core' fields are to be ignored. For other fields, the 'default row' will contain the default values as they were during the ALTER TABLE statement. (If the column default values are changed later, those values will only be stored in the .frm file. The 'default row' will contain the original evaluated values, which must be the same for every row.) The 'default row' must be completely hidden from higher-level access routines. Assertions have been added to ensure that no 'default row' is ever present in the adaptive hash index or in locked records. The 'default row' is never delete-marked. (3) In clustered index leaf page records, the number of fields must reside between the number of 'core' fields (dict_index_t::n_core_fields introduced in this work) and dict_index_t::n_fields. If the number of fields is less than dict_index_t::n_fields, the missing fields are replaced with the column value of the 'default row'. Note: The number of fields in the record may shrink if some of the last instantly added columns are updated to the value that is in the 'default row'. The function btr_cur_trim() implements this 'compression' on update and rollback; dtuple::trim() implements it on insert. (4) In ROW_FORMAT=COMPACT and ROW_FORMAT=DYNAMIC records, the new status value REC_STATUS_COLUMNS_ADDED will indicate the presence of a new record header that will encode n_fields-n_core_fields-1 in 1 or 2 bytes. (In ROW_FORMAT=REDUNDANT records, the record header always explicitly encodes the number of fields.) We introduce the undo log record type TRX_UNDO_INSERT_DEFAULT for covering the insert of the 'default row' record when instant ADD COLUMN is used for the first time. Subsequent instant ADD COLUMN can use TRX_UNDO_UPD_EXIST_REC. This is joint work with Vin Chen (陈福荣) from Tencent. The design that was discussed in April 2017 would not have allowed import or export of data files, because instead of the 'default row' it would have introduced a data dictionary table. The test rpl.rpl_alter_instant is exactly as contributed in pull request #408. The test innodb.instant_alter is based on a contributed test. The redo log record format changes for ROW_FORMAT=DYNAMIC and ROW_FORMAT=COMPACT are as contributed. (With this change present, crash recovery from MariaDB 10.3.1 will fail in spectacular ways!) Also the semantics of higher-level redo log records that modify the PAGE_INSTANT field is changed. The redo log format version identifier was already changed to LOG_HEADER_FORMAT_CURRENT=103 in MariaDB 10.3.1. Everything else has been rewritten by me. Thanks to Elena Stepanova, the code has been tested extensively. When rolling back an instant ADD COLUMN operation, we must empty the PAGE_FREE list after deleting or shortening the 'default row' record, by calling either btr_page_empty() or btr_page_reorganize(). We must know the size of each entry in the PAGE_FREE list. If rollback left a freed copy of the 'default row' in the PAGE_FREE list, we would be unable to determine its size (if it is in ROW_FORMAT=COMPACT or ROW_FORMAT=DYNAMIC) because it would contain more fields than the rolled-back definition of the clustered index. UNIV_SQL_DEFAULT: A new special constant that designates an instantly added column that is not present in the clustered index record. len_is_stored(): Check if a length is an actual length. There are two magic length values: UNIV_SQL_DEFAULT, UNIV_SQL_NULL. dict_col_t::def_val: The 'default row' value of the column. If the column is not added instantly, def_val.len will be UNIV_SQL_DEFAULT. dict_col_t: Add the accessors is_virtual(), is_nullable(), is_instant(), instant_value(). dict_col_t::remove_instant(): Remove the 'instant ADD' status of a column. dict_col_t::name(const dict_table_t& table): Replaces dict_table_get_col_name(). dict_index_t::n_core_fields: The original number of fields. For secondary indexes and if instant ADD COLUMN has not been used, this will be equal to dict_index_t::n_fields. dict_index_t::n_core_null_bytes: Number of bytes needed to represent the null flags; usually equal to UT_BITS_IN_BYTES(n_nullable). dict_index_t::NO_CORE_NULL_BYTES: Magic value signalling that n_core_null_bytes was not initialized yet from the clustered index root page. dict_index_t: Add the accessors is_instant(), is_clust(), get_n_nullable(), instant_field_value(). dict_index_t::instant_add_field(): Adjust clustered index metadata for instant ADD COLUMN. dict_index_t::remove_instant(): Remove the 'instant ADD' status of a clustered index when the table becomes empty, or the very first instant ADD COLUMN operation is rolled back. dict_table_t: Add the accessors is_instant(), is_temporary(), supports_instant(). dict_table_t::instant_add_column(): Adjust metadata for instant ADD COLUMN. dict_table_t::rollback_instant(): Adjust metadata on the rollback of instant ADD COLUMN. prepare_inplace_alter_table_dict(): First create the ctx->new_table, and only then decide if the table really needs to be rebuilt. We must split the creation of table or index metadata from the creation of the dictionary table records and the creation of the data. In this way, we can transform a table-rebuilding operation into an instant ADD COLUMN operation. Dictionary objects will only be added to cache when table rebuilding or index creation is needed. The ctx->instant_table will never be added to cache. dict_table_t::add_to_cache(): Modified and renamed from dict_table_add_to_cache(). Do not modify the table metadata. Let the callers invoke dict_table_add_system_columns() and if needed, set can_be_evicted. dict_create_sys_tables_tuple(), dict_create_table_step(): Omit the system columns (which will now exist in the dict_table_t object already at this point). dict_create_table_step(): Expect the callers to invoke dict_table_add_system_columns(). pars_create_table(): Before creating the table creation execution graph, invoke dict_table_add_system_columns(). row_create_table_for_mysql(): Expect all callers to invoke dict_table_add_system_columns(). create_index_dict(): Replaces row_merge_create_index_graph(). innodb_update_n_cols(): Renamed from innobase_update_n_virtual(). Call my_error() if an error occurs. btr_cur_instant_init(), btr_cur_instant_init_low(), btr_cur_instant_root_init(): Load additional metadata from the clustered index and set dict_index_t::n_core_null_bytes. This is invoked when table metadata is first loaded into the data dictionary. dict_boot(): Initialize n_core_null_bytes for the four hard-coded dictionary tables. dict_create_index_step(): Initialize n_core_null_bytes. This is executed as part of CREATE TABLE. dict_index_build_internal_clust(): Initialize n_core_null_bytes to NO_CORE_NULL_BYTES if table->supports_instant(). row_create_index_for_mysql(): Initialize n_core_null_bytes for CREATE TEMPORARY TABLE. commit_cache_norebuild(): Call the code to rename or enlarge columns in the cache only if instant ADD COLUMN is not being used. (Instant ADD COLUMN would copy all column metadata from instant_table to old_table, including the names and lengths.) PAGE_INSTANT: A new 13-bit field for storing dict_index_t::n_core_fields. This is repurposing the 16-bit field PAGE_DIRECTION, of which only the least significant 3 bits were used. The original byte containing PAGE_DIRECTION will be accessible via the new constant PAGE_DIRECTION_B. page_get_instant(), page_set_instant(): Accessors for the PAGE_INSTANT. page_ptr_get_direction(), page_get_direction(), page_ptr_set_direction(): Accessors for PAGE_DIRECTION. page_direction_reset(): Reset PAGE_DIRECTION, PAGE_N_DIRECTION. page_direction_increment(): Increment PAGE_N_DIRECTION and set PAGE_DIRECTION. rec_get_offsets(): Use the 'leaf' parameter for non-debug purposes, and assume that heap_no is always set. Initialize all dict_index_t::n_fields for ROW_FORMAT=REDUNDANT records, even if the record contains fewer fields. rec_offs_make_valid(): Add the parameter 'leaf'. rec_copy_prefix_to_dtuple(): Assert that the tuple is only built on the core fields. Instant ADD COLUMN only applies to the clustered index, and we should never build a search key that has more than the PRIMARY KEY and possibly DB_TRX_ID,DB_ROLL_PTR. All these columns are always present. dict_index_build_data_tuple(): Remove assertions that would be duplicated in rec_copy_prefix_to_dtuple(). rec_init_offsets(): Support ROW_FORMAT=REDUNDANT records whose number of fields is between n_core_fields and n_fields. cmp_rec_rec_with_match(): Implement the comparison between two MIN_REC_FLAG records. trx_t::in_rollback: Make the field available in non-debug builds. trx_start_for_ddl_low(): Remove dangerous error-tolerance. A dictionary transaction must be flagged as such before it has generated any undo log records. This is because trx_undo_assign_undo() will mark the transaction as a dictionary transaction in the undo log header right before the very first undo log record is being written. btr_index_rec_validate(): Account for instant ADD COLUMN row_undo_ins_remove_clust_rec(): On the rollback of an insert into SYS_COLUMNS, revert instant ADD COLUMN in the cache by removing the last column from the table and the clustered index. row_search_on_row_ref(), row_undo_mod_parse_undo_rec(), row_undo_mod(), trx_undo_update_rec_get_update(): Handle the 'default row' as a special case. dtuple_t::trim(index): Omit a redundant suffix of an index tuple right before insert or update. After instant ADD COLUMN, if the last fields of a clustered index tuple match the 'default row', there is no need to store them. While trimming the entry, we must hold a page latch, so that the table cannot be emptied and the 'default row' be deleted. btr_cur_optimistic_update(), btr_cur_pessimistic_update(), row_upd_clust_rec_by_insert(), row_ins_clust_index_entry_low(): Invoke dtuple_t::trim() if needed. row_ins_clust_index_entry(): Restore dtuple_t::n_fields after calling row_ins_clust_index_entry_low(). rec_get_converted_size(), rec_get_converted_size_comp(): Allow the number of fields to be between n_core_fields and n_fields. Do not support infimum,supremum. They are never supposed to be stored in dtuple_t, because page creation nowadays uses a lower-level method for initializing them. rec_convert_dtuple_to_rec_comp(): Assign the status bits based on the number of fields. btr_cur_trim(): In an update, trim the index entry as needed. For the 'default row', handle rollback specially. For user records, omit fields that match the 'default row'. btr_cur_optimistic_delete_func(), btr_cur_pessimistic_delete(): Skip locking and adaptive hash index for the 'default row'. row_log_table_apply_convert_mrec(): Replace 'default row' values if needed. In the temporary file that is applied by row_log_table_apply(), we must identify whether the records contain the extra header for instantly added columns. For now, we will allocate an additional byte for this for ROW_T_INSERT and ROW_T_UPDATE records when the source table has been subject to instant ADD COLUMN. The ROW_T_DELETE records are fine, as they will be converted and will only contain 'core' columns (PRIMARY KEY and some system columns) that are converted from dtuple_t. rec_get_converted_size_temp(), rec_init_offsets_temp(), rec_convert_dtuple_to_temp(): Add the parameter 'status'. REC_INFO_DEFAULT_ROW = REC_INFO_MIN_REC_FLAG | REC_STATUS_COLUMNS_ADDED: An info_bits constant for distinguishing the 'default row' record. rec_comp_status_t: An enum of the status bit values. rec_leaf_format: An enum that replaces the bool parameter of rec_init_offsets_comp_ordinary().
8 years ago
MDEV-11369 Instant ADD COLUMN for InnoDB For InnoDB tables, adding, dropping and reordering columns has required a rebuild of the table and all its indexes. Since MySQL 5.6 (and MariaDB 10.0) this has been supported online (LOCK=NONE), allowing concurrent modification of the tables. This work revises the InnoDB ROW_FORMAT=REDUNDANT, ROW_FORMAT=COMPACT and ROW_FORMAT=DYNAMIC so that columns can be appended instantaneously, with only minor changes performed to the table structure. The counter innodb_instant_alter_column in INFORMATION_SCHEMA.GLOBAL_STATUS is incremented whenever a table rebuild operation is converted into an instant ADD COLUMN operation. ROW_FORMAT=COMPRESSED tables will not support instant ADD COLUMN. Some usability limitations will be addressed in subsequent work: MDEV-13134 Introduce ALTER TABLE attributes ALGORITHM=NOCOPY and ALGORITHM=INSTANT MDEV-14016 Allow instant ADD COLUMN, ADD INDEX, LOCK=NONE The format of the clustered index (PRIMARY KEY) is changed as follows: (1) The FIL_PAGE_TYPE of the root page will be FIL_PAGE_TYPE_INSTANT, and a new field PAGE_INSTANT will contain the original number of fields in the clustered index ('core' fields). If instant ADD COLUMN has not been used or the table becomes empty, or the very first instant ADD COLUMN operation is rolled back, the fields PAGE_INSTANT and FIL_PAGE_TYPE will be reset to 0 and FIL_PAGE_INDEX. (2) A special 'default row' record is inserted into the leftmost leaf, between the page infimum and the first user record. This record is distinguished by the REC_INFO_MIN_REC_FLAG, and it is otherwise in the same format as records that contain values for the instantly added columns. This 'default row' always has the same number of fields as the clustered index according to the table definition. The values of 'core' fields are to be ignored. For other fields, the 'default row' will contain the default values as they were during the ALTER TABLE statement. (If the column default values are changed later, those values will only be stored in the .frm file. The 'default row' will contain the original evaluated values, which must be the same for every row.) The 'default row' must be completely hidden from higher-level access routines. Assertions have been added to ensure that no 'default row' is ever present in the adaptive hash index or in locked records. The 'default row' is never delete-marked. (3) In clustered index leaf page records, the number of fields must reside between the number of 'core' fields (dict_index_t::n_core_fields introduced in this work) and dict_index_t::n_fields. If the number of fields is less than dict_index_t::n_fields, the missing fields are replaced with the column value of the 'default row'. Note: The number of fields in the record may shrink if some of the last instantly added columns are updated to the value that is in the 'default row'. The function btr_cur_trim() implements this 'compression' on update and rollback; dtuple::trim() implements it on insert. (4) In ROW_FORMAT=COMPACT and ROW_FORMAT=DYNAMIC records, the new status value REC_STATUS_COLUMNS_ADDED will indicate the presence of a new record header that will encode n_fields-n_core_fields-1 in 1 or 2 bytes. (In ROW_FORMAT=REDUNDANT records, the record header always explicitly encodes the number of fields.) We introduce the undo log record type TRX_UNDO_INSERT_DEFAULT for covering the insert of the 'default row' record when instant ADD COLUMN is used for the first time. Subsequent instant ADD COLUMN can use TRX_UNDO_UPD_EXIST_REC. This is joint work with Vin Chen (陈福荣) from Tencent. The design that was discussed in April 2017 would not have allowed import or export of data files, because instead of the 'default row' it would have introduced a data dictionary table. The test rpl.rpl_alter_instant is exactly as contributed in pull request #408. The test innodb.instant_alter is based on a contributed test. The redo log record format changes for ROW_FORMAT=DYNAMIC and ROW_FORMAT=COMPACT are as contributed. (With this change present, crash recovery from MariaDB 10.3.1 will fail in spectacular ways!) Also the semantics of higher-level redo log records that modify the PAGE_INSTANT field is changed. The redo log format version identifier was already changed to LOG_HEADER_FORMAT_CURRENT=103 in MariaDB 10.3.1. Everything else has been rewritten by me. Thanks to Elena Stepanova, the code has been tested extensively. When rolling back an instant ADD COLUMN operation, we must empty the PAGE_FREE list after deleting or shortening the 'default row' record, by calling either btr_page_empty() or btr_page_reorganize(). We must know the size of each entry in the PAGE_FREE list. If rollback left a freed copy of the 'default row' in the PAGE_FREE list, we would be unable to determine its size (if it is in ROW_FORMAT=COMPACT or ROW_FORMAT=DYNAMIC) because it would contain more fields than the rolled-back definition of the clustered index. UNIV_SQL_DEFAULT: A new special constant that designates an instantly added column that is not present in the clustered index record. len_is_stored(): Check if a length is an actual length. There are two magic length values: UNIV_SQL_DEFAULT, UNIV_SQL_NULL. dict_col_t::def_val: The 'default row' value of the column. If the column is not added instantly, def_val.len will be UNIV_SQL_DEFAULT. dict_col_t: Add the accessors is_virtual(), is_nullable(), is_instant(), instant_value(). dict_col_t::remove_instant(): Remove the 'instant ADD' status of a column. dict_col_t::name(const dict_table_t& table): Replaces dict_table_get_col_name(). dict_index_t::n_core_fields: The original number of fields. For secondary indexes and if instant ADD COLUMN has not been used, this will be equal to dict_index_t::n_fields. dict_index_t::n_core_null_bytes: Number of bytes needed to represent the null flags; usually equal to UT_BITS_IN_BYTES(n_nullable). dict_index_t::NO_CORE_NULL_BYTES: Magic value signalling that n_core_null_bytes was not initialized yet from the clustered index root page. dict_index_t: Add the accessors is_instant(), is_clust(), get_n_nullable(), instant_field_value(). dict_index_t::instant_add_field(): Adjust clustered index metadata for instant ADD COLUMN. dict_index_t::remove_instant(): Remove the 'instant ADD' status of a clustered index when the table becomes empty, or the very first instant ADD COLUMN operation is rolled back. dict_table_t: Add the accessors is_instant(), is_temporary(), supports_instant(). dict_table_t::instant_add_column(): Adjust metadata for instant ADD COLUMN. dict_table_t::rollback_instant(): Adjust metadata on the rollback of instant ADD COLUMN. prepare_inplace_alter_table_dict(): First create the ctx->new_table, and only then decide if the table really needs to be rebuilt. We must split the creation of table or index metadata from the creation of the dictionary table records and the creation of the data. In this way, we can transform a table-rebuilding operation into an instant ADD COLUMN operation. Dictionary objects will only be added to cache when table rebuilding or index creation is needed. The ctx->instant_table will never be added to cache. dict_table_t::add_to_cache(): Modified and renamed from dict_table_add_to_cache(). Do not modify the table metadata. Let the callers invoke dict_table_add_system_columns() and if needed, set can_be_evicted. dict_create_sys_tables_tuple(), dict_create_table_step(): Omit the system columns (which will now exist in the dict_table_t object already at this point). dict_create_table_step(): Expect the callers to invoke dict_table_add_system_columns(). pars_create_table(): Before creating the table creation execution graph, invoke dict_table_add_system_columns(). row_create_table_for_mysql(): Expect all callers to invoke dict_table_add_system_columns(). create_index_dict(): Replaces row_merge_create_index_graph(). innodb_update_n_cols(): Renamed from innobase_update_n_virtual(). Call my_error() if an error occurs. btr_cur_instant_init(), btr_cur_instant_init_low(), btr_cur_instant_root_init(): Load additional metadata from the clustered index and set dict_index_t::n_core_null_bytes. This is invoked when table metadata is first loaded into the data dictionary. dict_boot(): Initialize n_core_null_bytes for the four hard-coded dictionary tables. dict_create_index_step(): Initialize n_core_null_bytes. This is executed as part of CREATE TABLE. dict_index_build_internal_clust(): Initialize n_core_null_bytes to NO_CORE_NULL_BYTES if table->supports_instant(). row_create_index_for_mysql(): Initialize n_core_null_bytes for CREATE TEMPORARY TABLE. commit_cache_norebuild(): Call the code to rename or enlarge columns in the cache only if instant ADD COLUMN is not being used. (Instant ADD COLUMN would copy all column metadata from instant_table to old_table, including the names and lengths.) PAGE_INSTANT: A new 13-bit field for storing dict_index_t::n_core_fields. This is repurposing the 16-bit field PAGE_DIRECTION, of which only the least significant 3 bits were used. The original byte containing PAGE_DIRECTION will be accessible via the new constant PAGE_DIRECTION_B. page_get_instant(), page_set_instant(): Accessors for the PAGE_INSTANT. page_ptr_get_direction(), page_get_direction(), page_ptr_set_direction(): Accessors for PAGE_DIRECTION. page_direction_reset(): Reset PAGE_DIRECTION, PAGE_N_DIRECTION. page_direction_increment(): Increment PAGE_N_DIRECTION and set PAGE_DIRECTION. rec_get_offsets(): Use the 'leaf' parameter for non-debug purposes, and assume that heap_no is always set. Initialize all dict_index_t::n_fields for ROW_FORMAT=REDUNDANT records, even if the record contains fewer fields. rec_offs_make_valid(): Add the parameter 'leaf'. rec_copy_prefix_to_dtuple(): Assert that the tuple is only built on the core fields. Instant ADD COLUMN only applies to the clustered index, and we should never build a search key that has more than the PRIMARY KEY and possibly DB_TRX_ID,DB_ROLL_PTR. All these columns are always present. dict_index_build_data_tuple(): Remove assertions that would be duplicated in rec_copy_prefix_to_dtuple(). rec_init_offsets(): Support ROW_FORMAT=REDUNDANT records whose number of fields is between n_core_fields and n_fields. cmp_rec_rec_with_match(): Implement the comparison between two MIN_REC_FLAG records. trx_t::in_rollback: Make the field available in non-debug builds. trx_start_for_ddl_low(): Remove dangerous error-tolerance. A dictionary transaction must be flagged as such before it has generated any undo log records. This is because trx_undo_assign_undo() will mark the transaction as a dictionary transaction in the undo log header right before the very first undo log record is being written. btr_index_rec_validate(): Account for instant ADD COLUMN row_undo_ins_remove_clust_rec(): On the rollback of an insert into SYS_COLUMNS, revert instant ADD COLUMN in the cache by removing the last column from the table and the clustered index. row_search_on_row_ref(), row_undo_mod_parse_undo_rec(), row_undo_mod(), trx_undo_update_rec_get_update(): Handle the 'default row' as a special case. dtuple_t::trim(index): Omit a redundant suffix of an index tuple right before insert or update. After instant ADD COLUMN, if the last fields of a clustered index tuple match the 'default row', there is no need to store them. While trimming the entry, we must hold a page latch, so that the table cannot be emptied and the 'default row' be deleted. btr_cur_optimistic_update(), btr_cur_pessimistic_update(), row_upd_clust_rec_by_insert(), row_ins_clust_index_entry_low(): Invoke dtuple_t::trim() if needed. row_ins_clust_index_entry(): Restore dtuple_t::n_fields after calling row_ins_clust_index_entry_low(). rec_get_converted_size(), rec_get_converted_size_comp(): Allow the number of fields to be between n_core_fields and n_fields. Do not support infimum,supremum. They are never supposed to be stored in dtuple_t, because page creation nowadays uses a lower-level method for initializing them. rec_convert_dtuple_to_rec_comp(): Assign the status bits based on the number of fields. btr_cur_trim(): In an update, trim the index entry as needed. For the 'default row', handle rollback specially. For user records, omit fields that match the 'default row'. btr_cur_optimistic_delete_func(), btr_cur_pessimistic_delete(): Skip locking and adaptive hash index for the 'default row'. row_log_table_apply_convert_mrec(): Replace 'default row' values if needed. In the temporary file that is applied by row_log_table_apply(), we must identify whether the records contain the extra header for instantly added columns. For now, we will allocate an additional byte for this for ROW_T_INSERT and ROW_T_UPDATE records when the source table has been subject to instant ADD COLUMN. The ROW_T_DELETE records are fine, as they will be converted and will only contain 'core' columns (PRIMARY KEY and some system columns) that are converted from dtuple_t. rec_get_converted_size_temp(), rec_init_offsets_temp(), rec_convert_dtuple_to_temp(): Add the parameter 'status'. REC_INFO_DEFAULT_ROW = REC_INFO_MIN_REC_FLAG | REC_STATUS_COLUMNS_ADDED: An info_bits constant for distinguishing the 'default row' record. rec_comp_status_t: An enum of the status bit values. rec_leaf_format: An enum that replaces the bool parameter of rec_init_offsets_comp_ordinary().
8 years ago
MDEV-11369 Instant ADD COLUMN for InnoDB For InnoDB tables, adding, dropping and reordering columns has required a rebuild of the table and all its indexes. Since MySQL 5.6 (and MariaDB 10.0) this has been supported online (LOCK=NONE), allowing concurrent modification of the tables. This work revises the InnoDB ROW_FORMAT=REDUNDANT, ROW_FORMAT=COMPACT and ROW_FORMAT=DYNAMIC so that columns can be appended instantaneously, with only minor changes performed to the table structure. The counter innodb_instant_alter_column in INFORMATION_SCHEMA.GLOBAL_STATUS is incremented whenever a table rebuild operation is converted into an instant ADD COLUMN operation. ROW_FORMAT=COMPRESSED tables will not support instant ADD COLUMN. Some usability limitations will be addressed in subsequent work: MDEV-13134 Introduce ALTER TABLE attributes ALGORITHM=NOCOPY and ALGORITHM=INSTANT MDEV-14016 Allow instant ADD COLUMN, ADD INDEX, LOCK=NONE The format of the clustered index (PRIMARY KEY) is changed as follows: (1) The FIL_PAGE_TYPE of the root page will be FIL_PAGE_TYPE_INSTANT, and a new field PAGE_INSTANT will contain the original number of fields in the clustered index ('core' fields). If instant ADD COLUMN has not been used or the table becomes empty, or the very first instant ADD COLUMN operation is rolled back, the fields PAGE_INSTANT and FIL_PAGE_TYPE will be reset to 0 and FIL_PAGE_INDEX. (2) A special 'default row' record is inserted into the leftmost leaf, between the page infimum and the first user record. This record is distinguished by the REC_INFO_MIN_REC_FLAG, and it is otherwise in the same format as records that contain values for the instantly added columns. This 'default row' always has the same number of fields as the clustered index according to the table definition. The values of 'core' fields are to be ignored. For other fields, the 'default row' will contain the default values as they were during the ALTER TABLE statement. (If the column default values are changed later, those values will only be stored in the .frm file. The 'default row' will contain the original evaluated values, which must be the same for every row.) The 'default row' must be completely hidden from higher-level access routines. Assertions have been added to ensure that no 'default row' is ever present in the adaptive hash index or in locked records. The 'default row' is never delete-marked. (3) In clustered index leaf page records, the number of fields must reside between the number of 'core' fields (dict_index_t::n_core_fields introduced in this work) and dict_index_t::n_fields. If the number of fields is less than dict_index_t::n_fields, the missing fields are replaced with the column value of the 'default row'. Note: The number of fields in the record may shrink if some of the last instantly added columns are updated to the value that is in the 'default row'. The function btr_cur_trim() implements this 'compression' on update and rollback; dtuple::trim() implements it on insert. (4) In ROW_FORMAT=COMPACT and ROW_FORMAT=DYNAMIC records, the new status value REC_STATUS_COLUMNS_ADDED will indicate the presence of a new record header that will encode n_fields-n_core_fields-1 in 1 or 2 bytes. (In ROW_FORMAT=REDUNDANT records, the record header always explicitly encodes the number of fields.) We introduce the undo log record type TRX_UNDO_INSERT_DEFAULT for covering the insert of the 'default row' record when instant ADD COLUMN is used for the first time. Subsequent instant ADD COLUMN can use TRX_UNDO_UPD_EXIST_REC. This is joint work with Vin Chen (陈福荣) from Tencent. The design that was discussed in April 2017 would not have allowed import or export of data files, because instead of the 'default row' it would have introduced a data dictionary table. The test rpl.rpl_alter_instant is exactly as contributed in pull request #408. The test innodb.instant_alter is based on a contributed test. The redo log record format changes for ROW_FORMAT=DYNAMIC and ROW_FORMAT=COMPACT are as contributed. (With this change present, crash recovery from MariaDB 10.3.1 will fail in spectacular ways!) Also the semantics of higher-level redo log records that modify the PAGE_INSTANT field is changed. The redo log format version identifier was already changed to LOG_HEADER_FORMAT_CURRENT=103 in MariaDB 10.3.1. Everything else has been rewritten by me. Thanks to Elena Stepanova, the code has been tested extensively. When rolling back an instant ADD COLUMN operation, we must empty the PAGE_FREE list after deleting or shortening the 'default row' record, by calling either btr_page_empty() or btr_page_reorganize(). We must know the size of each entry in the PAGE_FREE list. If rollback left a freed copy of the 'default row' in the PAGE_FREE list, we would be unable to determine its size (if it is in ROW_FORMAT=COMPACT or ROW_FORMAT=DYNAMIC) because it would contain more fields than the rolled-back definition of the clustered index. UNIV_SQL_DEFAULT: A new special constant that designates an instantly added column that is not present in the clustered index record. len_is_stored(): Check if a length is an actual length. There are two magic length values: UNIV_SQL_DEFAULT, UNIV_SQL_NULL. dict_col_t::def_val: The 'default row' value of the column. If the column is not added instantly, def_val.len will be UNIV_SQL_DEFAULT. dict_col_t: Add the accessors is_virtual(), is_nullable(), is_instant(), instant_value(). dict_col_t::remove_instant(): Remove the 'instant ADD' status of a column. dict_col_t::name(const dict_table_t& table): Replaces dict_table_get_col_name(). dict_index_t::n_core_fields: The original number of fields. For secondary indexes and if instant ADD COLUMN has not been used, this will be equal to dict_index_t::n_fields. dict_index_t::n_core_null_bytes: Number of bytes needed to represent the null flags; usually equal to UT_BITS_IN_BYTES(n_nullable). dict_index_t::NO_CORE_NULL_BYTES: Magic value signalling that n_core_null_bytes was not initialized yet from the clustered index root page. dict_index_t: Add the accessors is_instant(), is_clust(), get_n_nullable(), instant_field_value(). dict_index_t::instant_add_field(): Adjust clustered index metadata for instant ADD COLUMN. dict_index_t::remove_instant(): Remove the 'instant ADD' status of a clustered index when the table becomes empty, or the very first instant ADD COLUMN operation is rolled back. dict_table_t: Add the accessors is_instant(), is_temporary(), supports_instant(). dict_table_t::instant_add_column(): Adjust metadata for instant ADD COLUMN. dict_table_t::rollback_instant(): Adjust metadata on the rollback of instant ADD COLUMN. prepare_inplace_alter_table_dict(): First create the ctx->new_table, and only then decide if the table really needs to be rebuilt. We must split the creation of table or index metadata from the creation of the dictionary table records and the creation of the data. In this way, we can transform a table-rebuilding operation into an instant ADD COLUMN operation. Dictionary objects will only be added to cache when table rebuilding or index creation is needed. The ctx->instant_table will never be added to cache. dict_table_t::add_to_cache(): Modified and renamed from dict_table_add_to_cache(). Do not modify the table metadata. Let the callers invoke dict_table_add_system_columns() and if needed, set can_be_evicted. dict_create_sys_tables_tuple(), dict_create_table_step(): Omit the system columns (which will now exist in the dict_table_t object already at this point). dict_create_table_step(): Expect the callers to invoke dict_table_add_system_columns(). pars_create_table(): Before creating the table creation execution graph, invoke dict_table_add_system_columns(). row_create_table_for_mysql(): Expect all callers to invoke dict_table_add_system_columns(). create_index_dict(): Replaces row_merge_create_index_graph(). innodb_update_n_cols(): Renamed from innobase_update_n_virtual(). Call my_error() if an error occurs. btr_cur_instant_init(), btr_cur_instant_init_low(), btr_cur_instant_root_init(): Load additional metadata from the clustered index and set dict_index_t::n_core_null_bytes. This is invoked when table metadata is first loaded into the data dictionary. dict_boot(): Initialize n_core_null_bytes for the four hard-coded dictionary tables. dict_create_index_step(): Initialize n_core_null_bytes. This is executed as part of CREATE TABLE. dict_index_build_internal_clust(): Initialize n_core_null_bytes to NO_CORE_NULL_BYTES if table->supports_instant(). row_create_index_for_mysql(): Initialize n_core_null_bytes for CREATE TEMPORARY TABLE. commit_cache_norebuild(): Call the code to rename or enlarge columns in the cache only if instant ADD COLUMN is not being used. (Instant ADD COLUMN would copy all column metadata from instant_table to old_table, including the names and lengths.) PAGE_INSTANT: A new 13-bit field for storing dict_index_t::n_core_fields. This is repurposing the 16-bit field PAGE_DIRECTION, of which only the least significant 3 bits were used. The original byte containing PAGE_DIRECTION will be accessible via the new constant PAGE_DIRECTION_B. page_get_instant(), page_set_instant(): Accessors for the PAGE_INSTANT. page_ptr_get_direction(), page_get_direction(), page_ptr_set_direction(): Accessors for PAGE_DIRECTION. page_direction_reset(): Reset PAGE_DIRECTION, PAGE_N_DIRECTION. page_direction_increment(): Increment PAGE_N_DIRECTION and set PAGE_DIRECTION. rec_get_offsets(): Use the 'leaf' parameter for non-debug purposes, and assume that heap_no is always set. Initialize all dict_index_t::n_fields for ROW_FORMAT=REDUNDANT records, even if the record contains fewer fields. rec_offs_make_valid(): Add the parameter 'leaf'. rec_copy_prefix_to_dtuple(): Assert that the tuple is only built on the core fields. Instant ADD COLUMN only applies to the clustered index, and we should never build a search key that has more than the PRIMARY KEY and possibly DB_TRX_ID,DB_ROLL_PTR. All these columns are always present. dict_index_build_data_tuple(): Remove assertions that would be duplicated in rec_copy_prefix_to_dtuple(). rec_init_offsets(): Support ROW_FORMAT=REDUNDANT records whose number of fields is between n_core_fields and n_fields. cmp_rec_rec_with_match(): Implement the comparison between two MIN_REC_FLAG records. trx_t::in_rollback: Make the field available in non-debug builds. trx_start_for_ddl_low(): Remove dangerous error-tolerance. A dictionary transaction must be flagged as such before it has generated any undo log records. This is because trx_undo_assign_undo() will mark the transaction as a dictionary transaction in the undo log header right before the very first undo log record is being written. btr_index_rec_validate(): Account for instant ADD COLUMN row_undo_ins_remove_clust_rec(): On the rollback of an insert into SYS_COLUMNS, revert instant ADD COLUMN in the cache by removing the last column from the table and the clustered index. row_search_on_row_ref(), row_undo_mod_parse_undo_rec(), row_undo_mod(), trx_undo_update_rec_get_update(): Handle the 'default row' as a special case. dtuple_t::trim(index): Omit a redundant suffix of an index tuple right before insert or update. After instant ADD COLUMN, if the last fields of a clustered index tuple match the 'default row', there is no need to store them. While trimming the entry, we must hold a page latch, so that the table cannot be emptied and the 'default row' be deleted. btr_cur_optimistic_update(), btr_cur_pessimistic_update(), row_upd_clust_rec_by_insert(), row_ins_clust_index_entry_low(): Invoke dtuple_t::trim() if needed. row_ins_clust_index_entry(): Restore dtuple_t::n_fields after calling row_ins_clust_index_entry_low(). rec_get_converted_size(), rec_get_converted_size_comp(): Allow the number of fields to be between n_core_fields and n_fields. Do not support infimum,supremum. They are never supposed to be stored in dtuple_t, because page creation nowadays uses a lower-level method for initializing them. rec_convert_dtuple_to_rec_comp(): Assign the status bits based on the number of fields. btr_cur_trim(): In an update, trim the index entry as needed. For the 'default row', handle rollback specially. For user records, omit fields that match the 'default row'. btr_cur_optimistic_delete_func(), btr_cur_pessimistic_delete(): Skip locking and adaptive hash index for the 'default row'. row_log_table_apply_convert_mrec(): Replace 'default row' values if needed. In the temporary file that is applied by row_log_table_apply(), we must identify whether the records contain the extra header for instantly added columns. For now, we will allocate an additional byte for this for ROW_T_INSERT and ROW_T_UPDATE records when the source table has been subject to instant ADD COLUMN. The ROW_T_DELETE records are fine, as they will be converted and will only contain 'core' columns (PRIMARY KEY and some system columns) that are converted from dtuple_t. rec_get_converted_size_temp(), rec_init_offsets_temp(), rec_convert_dtuple_to_temp(): Add the parameter 'status'. REC_INFO_DEFAULT_ROW = REC_INFO_MIN_REC_FLAG | REC_STATUS_COLUMNS_ADDED: An info_bits constant for distinguishing the 'default row' record. rec_comp_status_t: An enum of the status bit values. rec_leaf_format: An enum that replaces the bool parameter of rec_init_offsets_comp_ordinary().
8 years ago
11 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
11 years ago
11 years ago
11 years ago
12 years ago
12 years ago
12 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
10 years ago
MDEV-20377: Make WITH_MSAN more usable MemorySanitizer (clang -fsanitize=memory) requires that all code be compiled with instrumentation enabled. The only exception is the C runtime library. Failure to use instrumented libraries will cause bogus messages about memory being uninitialized. In WITH_MSAN builds, we must avoid calling getservbyname(), because even though it is a standard library function, it is not instrumented, not even in clang 10. Note: Before MariaDB Server 10.5, ./mtr will typically fail due to the old PCRE library, which was updated in MDEV-14024. The following cmake options were tested on 10.5 in commit 94d0bb4dbeb28a94d1f87fdd55f4297ff3df0157: cmake \ -DCMAKE_C_FLAGS='-march=native -O2' \ -DCMAKE_CXX_FLAGS='-stdlib=libc++ -march=native -O2' \ -DWITH_EMBEDDED_SERVER=OFF -DWITH_UNIT_TESTS=OFF -DCMAKE_BUILD_TYPE=Debug \ -DWITH_INNODB_{BZIP2,LZ4,LZMA,LZO,SNAPPY}=OFF \ -DPLUGIN_{ARCHIVE,TOKUDB,MROONGA,OQGRAPH,ROCKSDB,CONNECT,SPIDER}=NO \ -DWITH_SAFEMALLOC=OFF \ -DWITH_{ZLIB,SSL,PCRE}=bundled \ -DHAVE_LIBAIO_H=0 \ -DWITH_MSAN=ON MEM_MAKE_DEFINED(): An alias for VALGRIND_MAKE_MEM_DEFINED() and __msan_unpoison(). MEM_GET_VBITS(), MEM_SET_VBITS(): Aliases for VALGRIND_GET_VBITS(), VALGRIND_SET_VBITS(), __msan_copy_shadow(). InnoDB: Replace the UNIV_MEM_ macros with corresponding MEM_ macros. ut_crc32_8_hw(), ut_crc32_64_low_hw(): Use the compiler built-in functions instead of inline assembler when building WITH_MSAN. This will require at least -msse4.2 when building for IA-32 or AMD64. The inline assembler would not be instrumented, and would thus cause bogus failures.
5 years ago
11 years ago
11 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-20377: Make WITH_MSAN more usable MemorySanitizer (clang -fsanitize=memory) requires that all code be compiled with instrumentation enabled. The only exception is the C runtime library. Failure to use instrumented libraries will cause bogus messages about memory being uninitialized. In WITH_MSAN builds, we must avoid calling getservbyname(), because even though it is a standard library function, it is not instrumented, not even in clang 10. Note: Before MariaDB Server 10.5, ./mtr will typically fail due to the old PCRE library, which was updated in MDEV-14024. The following cmake options were tested on 10.5 in commit 94d0bb4dbeb28a94d1f87fdd55f4297ff3df0157: cmake \ -DCMAKE_C_FLAGS='-march=native -O2' \ -DCMAKE_CXX_FLAGS='-stdlib=libc++ -march=native -O2' \ -DWITH_EMBEDDED_SERVER=OFF -DWITH_UNIT_TESTS=OFF -DCMAKE_BUILD_TYPE=Debug \ -DWITH_INNODB_{BZIP2,LZ4,LZMA,LZO,SNAPPY}=OFF \ -DPLUGIN_{ARCHIVE,TOKUDB,MROONGA,OQGRAPH,ROCKSDB,CONNECT,SPIDER}=NO \ -DWITH_SAFEMALLOC=OFF \ -DWITH_{ZLIB,SSL,PCRE}=bundled \ -DHAVE_LIBAIO_H=0 \ -DWITH_MSAN=ON MEM_MAKE_DEFINED(): An alias for VALGRIND_MAKE_MEM_DEFINED() and __msan_unpoison(). MEM_GET_VBITS(), MEM_SET_VBITS(): Aliases for VALGRIND_GET_VBITS(), VALGRIND_SET_VBITS(), __msan_copy_shadow(). InnoDB: Replace the UNIV_MEM_ macros with corresponding MEM_ macros. ut_crc32_8_hw(), ut_crc32_64_low_hw(): Use the compiler built-in functions instead of inline assembler when building WITH_MSAN. This will require at least -msse4.2 when building for IA-32 or AMD64. The inline assembler would not be instrumented, and would thus cause bogus failures.
5 years ago
MDEV-20377: Make WITH_MSAN more usable MemorySanitizer (clang -fsanitize=memory) requires that all code be compiled with instrumentation enabled. The only exception is the C runtime library. Failure to use instrumented libraries will cause bogus messages about memory being uninitialized. In WITH_MSAN builds, we must avoid calling getservbyname(), because even though it is a standard library function, it is not instrumented, not even in clang 10. Note: Before MariaDB Server 10.5, ./mtr will typically fail due to the old PCRE library, which was updated in MDEV-14024. The following cmake options were tested on 10.5 in commit 94d0bb4dbeb28a94d1f87fdd55f4297ff3df0157: cmake \ -DCMAKE_C_FLAGS='-march=native -O2' \ -DCMAKE_CXX_FLAGS='-stdlib=libc++ -march=native -O2' \ -DWITH_EMBEDDED_SERVER=OFF -DWITH_UNIT_TESTS=OFF -DCMAKE_BUILD_TYPE=Debug \ -DWITH_INNODB_{BZIP2,LZ4,LZMA,LZO,SNAPPY}=OFF \ -DPLUGIN_{ARCHIVE,TOKUDB,MROONGA,OQGRAPH,ROCKSDB,CONNECT,SPIDER}=NO \ -DWITH_SAFEMALLOC=OFF \ -DWITH_{ZLIB,SSL,PCRE}=bundled \ -DHAVE_LIBAIO_H=0 \ -DWITH_MSAN=ON MEM_MAKE_DEFINED(): An alias for VALGRIND_MAKE_MEM_DEFINED() and __msan_unpoison(). MEM_GET_VBITS(), MEM_SET_VBITS(): Aliases for VALGRIND_GET_VBITS(), VALGRIND_SET_VBITS(), __msan_copy_shadow(). InnoDB: Replace the UNIV_MEM_ macros with corresponding MEM_ macros. ut_crc32_8_hw(), ut_crc32_64_low_hw(): Use the compiler built-in functions instead of inline assembler when building WITH_MSAN. This will require at least -msse4.2 when building for IA-32 or AMD64. The inline assembler would not be instrumented, and would thus cause bogus failures.
5 years ago
MDEV-20377: Make WITH_MSAN more usable MemorySanitizer (clang -fsanitize=memory) requires that all code be compiled with instrumentation enabled. The only exception is the C runtime library. Failure to use instrumented libraries will cause bogus messages about memory being uninitialized. In WITH_MSAN builds, we must avoid calling getservbyname(), because even though it is a standard library function, it is not instrumented, not even in clang 10. Note: Before MariaDB Server 10.5, ./mtr will typically fail due to the old PCRE library, which was updated in MDEV-14024. The following cmake options were tested on 10.5 in commit 94d0bb4dbeb28a94d1f87fdd55f4297ff3df0157: cmake \ -DCMAKE_C_FLAGS='-march=native -O2' \ -DCMAKE_CXX_FLAGS='-stdlib=libc++ -march=native -O2' \ -DWITH_EMBEDDED_SERVER=OFF -DWITH_UNIT_TESTS=OFF -DCMAKE_BUILD_TYPE=Debug \ -DWITH_INNODB_{BZIP2,LZ4,LZMA,LZO,SNAPPY}=OFF \ -DPLUGIN_{ARCHIVE,TOKUDB,MROONGA,OQGRAPH,ROCKSDB,CONNECT,SPIDER}=NO \ -DWITH_SAFEMALLOC=OFF \ -DWITH_{ZLIB,SSL,PCRE}=bundled \ -DHAVE_LIBAIO_H=0 \ -DWITH_MSAN=ON MEM_MAKE_DEFINED(): An alias for VALGRIND_MAKE_MEM_DEFINED() and __msan_unpoison(). MEM_GET_VBITS(), MEM_SET_VBITS(): Aliases for VALGRIND_GET_VBITS(), VALGRIND_SET_VBITS(), __msan_copy_shadow(). InnoDB: Replace the UNIV_MEM_ macros with corresponding MEM_ macros. ut_crc32_8_hw(), ut_crc32_64_low_hw(): Use the compiler built-in functions instead of inline assembler when building WITH_MSAN. This will require at least -msse4.2 when building for IA-32 or AMD64. The inline assembler would not be instrumented, and would thus cause bogus failures.
5 years ago
MDEV-20377: Make WITH_MSAN more usable MemorySanitizer (clang -fsanitize=memory) requires that all code be compiled with instrumentation enabled. The only exception is the C runtime library. Failure to use instrumented libraries will cause bogus messages about memory being uninitialized. In WITH_MSAN builds, we must avoid calling getservbyname(), because even though it is a standard library function, it is not instrumented, not even in clang 10. Note: Before MariaDB Server 10.5, ./mtr will typically fail due to the old PCRE library, which was updated in MDEV-14024. The following cmake options were tested on 10.5 in commit 94d0bb4dbeb28a94d1f87fdd55f4297ff3df0157: cmake \ -DCMAKE_C_FLAGS='-march=native -O2' \ -DCMAKE_CXX_FLAGS='-stdlib=libc++ -march=native -O2' \ -DWITH_EMBEDDED_SERVER=OFF -DWITH_UNIT_TESTS=OFF -DCMAKE_BUILD_TYPE=Debug \ -DWITH_INNODB_{BZIP2,LZ4,LZMA,LZO,SNAPPY}=OFF \ -DPLUGIN_{ARCHIVE,TOKUDB,MROONGA,OQGRAPH,ROCKSDB,CONNECT,SPIDER}=NO \ -DWITH_SAFEMALLOC=OFF \ -DWITH_{ZLIB,SSL,PCRE}=bundled \ -DHAVE_LIBAIO_H=0 \ -DWITH_MSAN=ON MEM_MAKE_DEFINED(): An alias for VALGRIND_MAKE_MEM_DEFINED() and __msan_unpoison(). MEM_GET_VBITS(), MEM_SET_VBITS(): Aliases for VALGRIND_GET_VBITS(), VALGRIND_SET_VBITS(), __msan_copy_shadow(). InnoDB: Replace the UNIV_MEM_ macros with corresponding MEM_ macros. ut_crc32_8_hw(), ut_crc32_64_low_hw(): Use the compiler built-in functions instead of inline assembler when building WITH_MSAN. This will require at least -msse4.2 when building for IA-32 or AMD64. The inline assembler would not be instrumented, and would thus cause bogus failures.
5 years ago
MDEV-20377: Make WITH_MSAN more usable MemorySanitizer (clang -fsanitize=memory) requires that all code be compiled with instrumentation enabled. The only exception is the C runtime library. Failure to use instrumented libraries will cause bogus messages about memory being uninitialized. In WITH_MSAN builds, we must avoid calling getservbyname(), because even though it is a standard library function, it is not instrumented, not even in clang 10. Note: Before MariaDB Server 10.5, ./mtr will typically fail due to the old PCRE library, which was updated in MDEV-14024. The following cmake options were tested on 10.5 in commit 94d0bb4dbeb28a94d1f87fdd55f4297ff3df0157: cmake \ -DCMAKE_C_FLAGS='-march=native -O2' \ -DCMAKE_CXX_FLAGS='-stdlib=libc++ -march=native -O2' \ -DWITH_EMBEDDED_SERVER=OFF -DWITH_UNIT_TESTS=OFF -DCMAKE_BUILD_TYPE=Debug \ -DWITH_INNODB_{BZIP2,LZ4,LZMA,LZO,SNAPPY}=OFF \ -DPLUGIN_{ARCHIVE,TOKUDB,MROONGA,OQGRAPH,ROCKSDB,CONNECT,SPIDER}=NO \ -DWITH_SAFEMALLOC=OFF \ -DWITH_{ZLIB,SSL,PCRE}=bundled \ -DHAVE_LIBAIO_H=0 \ -DWITH_MSAN=ON MEM_MAKE_DEFINED(): An alias for VALGRIND_MAKE_MEM_DEFINED() and __msan_unpoison(). MEM_GET_VBITS(), MEM_SET_VBITS(): Aliases for VALGRIND_GET_VBITS(), VALGRIND_SET_VBITS(), __msan_copy_shadow(). InnoDB: Replace the UNIV_MEM_ macros with corresponding MEM_ macros. ut_crc32_8_hw(), ut_crc32_64_low_hw(): Use the compiler built-in functions instead of inline assembler when building WITH_MSAN. This will require at least -msse4.2 when building for IA-32 or AMD64. The inline assembler would not be instrumented, and would thus cause bogus failures.
5 years ago
MDEV-20950 Reduce size of record offsets offset_t: this is a type which represents one record offset. It's unsigned short int. a lot of functions: replace ulint with offset_t btr_pcur_restore_position_func(), page_validate(), row_ins_scan_sec_index_for_duplicate(), row_upd_clust_rec_by_insert_inherit_func(), row_vers_impl_x_locked_low(), trx_undo_prev_version_build(): allocate record offsets on the stack instead of waiting for rec_get_offsets() to allocate it from mem_heap_t. So, reducing memory allocations. RECORD_OFFSET, INDEX_OFFSET: now it's less convenient to store pointers in offset_t* array. One pointer occupies now several offset_t. And those constant are start indexes into array to places where to store pointer values REC_OFFS_HEADER_SIZE: adjusted for the new reality REC_OFFS_NORMAL_SIZE: increase size from 100 to 300 which means less heap allocations. And sizeof(offset_t[REC_OFFS_NORMAL_SIZE]) now is 600 bytes which is smaller than previous 800 bytes. REC_OFFS_SEC_INDEX_SIZE: adjusted for the new reality rem0rec.h, rem0rec.ic, rem0rec.cc: various arguments, return values and local variables types were changed to fix numerous integer conversions issues. enum field_type_t: offset types concept was introduces which replaces old offset flags stuff. Like in earlier version, 2 upper bits are used to store offset type. And this enum represents those types. REC_OFFS_SQL_NULL, REC_OFFS_MASK: removed get_type(), set_type(), get_value(), combine(): these are convenience functions to work with offsets and it's types rec_offs_base()[0]: still uses an old scheme with flags REC_OFFS_COMPACT and REC_OFFS_EXTERNAL rec_offs_base()[i]: these have type offset_t now. Two upper bits contains type.
6 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
MDEV-13485 MTR tests fail massively with --innodb-sync-debug The parameter --innodb-sync-debug, which is disabled by default, aims to find potential deadlocks in InnoDB. When the parameter is enabled, lots of tests failed. Most of these failures were due to bogus diagnostics. But, as part of this fix, we are also fixing a bug in error handling code and removing dead code, and fixing cases where an uninitialized mutex was being locked and unlocked. dict_create_foreign_constraints_low(): Remove an extraneous mutex_exit() call that could cause corruption in an error handling path. Also, do not unnecessarily acquire dict_foreign_err_mutex. Its only purpose is to control concurrent access to dict_foreign_err_file. row_ins_foreign_trx_print(): Replace a redundant condition with a debug assertion. srv_dict_tmpfile, srv_dict_tmpfile_mutex: Remove. The temporary file is never being written to or read from. log_free_check(): Allow SYNC_FTS_CACHE (fts_cache_t::lock) to be held. ha_innobase::inplace_alter_table(), row_merge_insert_index_tuples(): Assert that no unexpected latches are being held. sync_latch_meta_init(): Properly initialize dict_operation_lock_key at SYNC_DICT_OPERATION. dict_sys->mutex is SYNC_DICT, and the now-removed SRV_DICT_TMPFILE was wrongly registered at SYNC_DICT_OPERATION. buf_block_init(): Correctly register buf_block_t::debug_latch. It was previously misleadingly reported as LATCH_ID_DICT_FOREIGN_ERR. latch_level_t: Correct the relative latching order of SYNC_IBUF_PESS_INSERT_MUTEX,SYNC_INDEX_TREE and SYNC_FILE_FORMAT_TAG,SYNC_DICT_OPERATION to avoid bogus failures. row_drop_table_for_mysql(): Avoid accessing btr_defragment_mutex if the defragmentation thread has not been started. This is the case during fts_drop_orphaned_tables() in recv_recovery_rollback_active(). fil_space_destroy_crypt_data(): Avoid acquiring fil_crypt_threads_mutex when it is uninitialized. We may have created crypt_data before the mutex was created, and the mutex creation would be skipped if InnoDB startup failed or --innodb-read-only was specified.
8 years ago
MDEV-13485 MTR tests fail massively with --innodb-sync-debug The parameter --innodb-sync-debug, which is disabled by default, aims to find potential deadlocks in InnoDB. When the parameter is enabled, lots of tests failed. Most of these failures were due to bogus diagnostics. But, as part of this fix, we are also fixing a bug in error handling code and removing dead code, and fixing cases where an uninitialized mutex was being locked and unlocked. dict_create_foreign_constraints_low(): Remove an extraneous mutex_exit() call that could cause corruption in an error handling path. Also, do not unnecessarily acquire dict_foreign_err_mutex. Its only purpose is to control concurrent access to dict_foreign_err_file. row_ins_foreign_trx_print(): Replace a redundant condition with a debug assertion. srv_dict_tmpfile, srv_dict_tmpfile_mutex: Remove. The temporary file is never being written to or read from. log_free_check(): Allow SYNC_FTS_CACHE (fts_cache_t::lock) to be held. ha_innobase::inplace_alter_table(), row_merge_insert_index_tuples(): Assert that no unexpected latches are being held. sync_latch_meta_init(): Properly initialize dict_operation_lock_key at SYNC_DICT_OPERATION. dict_sys->mutex is SYNC_DICT, and the now-removed SRV_DICT_TMPFILE was wrongly registered at SYNC_DICT_OPERATION. buf_block_init(): Correctly register buf_block_t::debug_latch. It was previously misleadingly reported as LATCH_ID_DICT_FOREIGN_ERR. latch_level_t: Correct the relative latching order of SYNC_IBUF_PESS_INSERT_MUTEX,SYNC_INDEX_TREE and SYNC_FILE_FORMAT_TAG,SYNC_DICT_OPERATION to avoid bogus failures. row_drop_table_for_mysql(): Avoid accessing btr_defragment_mutex if the defragmentation thread has not been started. This is the case during fts_drop_orphaned_tables() in recv_recovery_rollback_active(). fil_space_destroy_crypt_data(): Avoid acquiring fil_crypt_threads_mutex when it is uninitialized. We may have created crypt_data before the mutex was created, and the mutex creation would be skipped if InnoDB startup failed or --innodb-read-only was specified.
8 years ago
MDEV-24971 InnoDB access freed virtual column after rollback of secondary index Problem: ======== InnoDB fails to clean the index stub if it fails to add the virtual index which contains new virtual column. But it clears the newly virtual column from index in clear_added_indexes() during inplace_alter_table. On commit, InnoDB evicts and reload the table. In case of rollback, it doesn't happen. InnoDB clears the ABORTED index while opening the table or doing the DDL. In the mean time, InnoDB can access the dropped virtual index columns while creating prebuilt or rollback of concurrent DML. Solution: ========== (1) InnoDB should maintain newly added virtual column while rollbacking the newly added virtual index. (2) InnoDB must not defer the index removal if the alter table is executed with LOCK=EXCLUSIVE. (3) For LOCK=SHARED, InnoDB should check whether the table has any other transaction lock other than alter transaction before deferring the index stub. Replaced has_new_v_col with dict_add_vcol_info in dict_index_t to indicate whether the index has any new virtual column. dict_index_t::has_new_v_col(): Returns whether the index has newly added virtual column, it doesn't say which columns are newly added virtual column ha_innobase_inplace_ctx::is_new_vcol(): Return whether the given column is added as a part of the current alter. ha_innobase_inplace_ctx::clean_new_vcol_index(): Copy the newly added virtual column to new_vcol_info in dict_index_t. Replace the column in the index fields with virtual column stored in new_vcol_info. dict_index_t::assign_new_v_col(): Store the number of virtual column added in index as a part of alter table. dict_index_t::get_n_new_vcol(): Get the number of newly added virtual column dict_index_t::assign_drop_v_col(): Allocate the memory for adding new virtual column in new_vcol_info. dict_index_t::add_drop_v_col(): Add the newly added virtual column in new_vcol_info. dict_table_t::has_lock_for_other_trx(): Whether the table has any other transaction lock than given transaction. row_merge_drop_indexes(): Add parameter alter_trx and check whether the table has any other lock than alter transaction.
5 years ago
MDEV-24971 InnoDB access freed virtual column after rollback of secondary index Problem: ======== InnoDB fails to clean the index stub if it fails to add the virtual index which contains new virtual column. But it clears the newly virtual column from index in clear_added_indexes() during inplace_alter_table. On commit, InnoDB evicts and reload the table. In case of rollback, it doesn't happen. InnoDB clears the ABORTED index while opening the table or doing the DDL. In the mean time, InnoDB can access the dropped virtual index columns while creating prebuilt or rollback of concurrent DML. Solution: ========== (1) InnoDB should maintain newly added virtual column while rollbacking the newly added virtual index. (2) InnoDB must not defer the index removal if the alter table is executed with LOCK=EXCLUSIVE. (3) For LOCK=SHARED, InnoDB should check whether the table has any other transaction lock other than alter transaction before deferring the index stub. Replaced has_new_v_col with dict_add_vcol_info in dict_index_t to indicate whether the index has any new virtual column. dict_index_t::has_new_v_col(): Returns whether the index has newly added virtual column, it doesn't say which columns are newly added virtual column ha_innobase_inplace_ctx::is_new_vcol(): Return whether the given column is added as a part of the current alter. ha_innobase_inplace_ctx::clean_new_vcol_index(): Copy the newly added virtual column to new_vcol_info in dict_index_t. Replace the column in the index fields with virtual column stored in new_vcol_info. dict_index_t::assign_new_v_col(): Store the number of virtual column added in index as a part of alter table. dict_index_t::get_n_new_vcol(): Get the number of newly added virtual column dict_index_t::assign_drop_v_col(): Allocate the memory for adding new virtual column in new_vcol_info. dict_index_t::add_drop_v_col(): Add the newly added virtual column in new_vcol_info. dict_table_t::has_lock_for_other_trx(): Whether the table has any other transaction lock than given transaction. row_merge_drop_indexes(): Add parameter alter_trx and check whether the table has any other lock than alter transaction.
5 years ago
MDEV-24971 InnoDB access freed virtual column after rollback of secondary index Problem: ======== InnoDB fails to clean the index stub if it fails to add the virtual index which contains new virtual column. But it clears the newly virtual column from index in clear_added_indexes() during inplace_alter_table. On commit, InnoDB evicts and reload the table. In case of rollback, it doesn't happen. InnoDB clears the ABORTED index while opening the table or doing the DDL. In the mean time, InnoDB can access the dropped virtual index columns while creating prebuilt or rollback of concurrent DML. Solution: ========== (1) InnoDB should maintain newly added virtual column while rollbacking the newly added virtual index. (2) InnoDB must not defer the index removal if the alter table is executed with LOCK=EXCLUSIVE. (3) For LOCK=SHARED, InnoDB should check whether the table has any other transaction lock other than alter transaction before deferring the index stub. Replaced has_new_v_col with dict_add_vcol_info in dict_index_t to indicate whether the index has any new virtual column. dict_index_t::has_new_v_col(): Returns whether the index has newly added virtual column, it doesn't say which columns are newly added virtual column ha_innobase_inplace_ctx::is_new_vcol(): Return whether the given column is added as a part of the current alter. ha_innobase_inplace_ctx::clean_new_vcol_index(): Copy the newly added virtual column to new_vcol_info in dict_index_t. Replace the column in the index fields with virtual column stored in new_vcol_info. dict_index_t::assign_new_v_col(): Store the number of virtual column added in index as a part of alter table. dict_index_t::get_n_new_vcol(): Get the number of newly added virtual column dict_index_t::assign_drop_v_col(): Allocate the memory for adding new virtual column in new_vcol_info. dict_index_t::add_drop_v_col(): Add the newly added virtual column in new_vcol_info. dict_table_t::has_lock_for_other_trx(): Whether the table has any other transaction lock than given transaction. row_merge_drop_indexes(): Add parameter alter_trx and check whether the table has any other lock than alter transaction.
5 years ago
MDEV-24971 InnoDB access freed virtual column after rollback of secondary index Problem: ======== InnoDB fails to clean the index stub if it fails to add the virtual index which contains new virtual column. But it clears the newly virtual column from index in clear_added_indexes() during inplace_alter_table. On commit, InnoDB evicts and reload the table. In case of rollback, it doesn't happen. InnoDB clears the ABORTED index while opening the table or doing the DDL. In the mean time, InnoDB can access the dropped virtual index columns while creating prebuilt or rollback of concurrent DML. Solution: ========== (1) InnoDB should maintain newly added virtual column while rollbacking the newly added virtual index. (2) InnoDB must not defer the index removal if the alter table is executed with LOCK=EXCLUSIVE. (3) For LOCK=SHARED, InnoDB should check whether the table has any other transaction lock other than alter transaction before deferring the index stub. Replaced has_new_v_col with dict_add_vcol_info in dict_index_t to indicate whether the index has any new virtual column. dict_index_t::has_new_v_col(): Returns whether the index has newly added virtual column, it doesn't say which columns are newly added virtual column ha_innobase_inplace_ctx::is_new_vcol(): Return whether the given column is added as a part of the current alter. ha_innobase_inplace_ctx::clean_new_vcol_index(): Copy the newly added virtual column to new_vcol_info in dict_index_t. Replace the column in the index fields with virtual column stored in new_vcol_info. dict_index_t::assign_new_v_col(): Store the number of virtual column added in index as a part of alter table. dict_index_t::get_n_new_vcol(): Get the number of newly added virtual column dict_index_t::assign_drop_v_col(): Allocate the memory for adding new virtual column in new_vcol_info. dict_index_t::add_drop_v_col(): Add the newly added virtual column in new_vcol_info. dict_table_t::has_lock_for_other_trx(): Whether the table has any other transaction lock than given transaction. row_merge_drop_indexes(): Add parameter alter_trx and check whether the table has any other lock than alter transaction.
5 years ago
MDEV-22456 Dropping the adaptive hash index may cause DDL to lock up InnoDB If the InnoDB buffer pool contains many pages for a table or index that is being dropped or rebuilt, and if many of such pages are pointed to by the adaptive hash index, dropping the adaptive hash index may consume a lot of time. The time-consuming operation of dropping the adaptive hash index entries is being executed while the InnoDB data dictionary cache dict_sys is exclusively locked. It is not actually necessary to drop all adaptive hash index entries at the time a table or index is being dropped or rebuilt. We can let the LRU replacement policy of the buffer pool take care of this gradually. For this to work, we must detach the dict_table_t and dict_index_t objects from the main dict_sys cache, and once the last adaptive hash index entry for the detached table is removed (when the garbage page is evicted from the buffer pool) we can free the dict_table_t and dict_index_t object. Related to this, in MDEV-16283, we made ALTER TABLE...DISCARD TABLESPACE skip both the buffer pool eviction and the drop of the adaptive hash index. We shifted the burden to ALTER TABLE...IMPORT TABLESPACE or DROP TABLE. We can remove the eviction from DROP TABLE. We must retain the eviction in the ALTER TABLE...IMPORT TABLESPACE code path, so that in case the discarded table is being re-imported with the same tablespace identifier, the fresh data from the imported tablespace will replace any stale pages in the buffer pool. rpl.rpl_failed_drop_tbl_binlog: Remove the test. DROP TABLE can no longer be interrupted inside InnoDB. fseg_free_page(), fseg_free_step(), fseg_free_step_not_header(), fseg_free_page_low(), fseg_free_extent(): Remove the parameter that specifies whether the adaptive hash index should be dropped. btr_search_lazy_free(): Lazily free an index when the last reference to it is dropped from the adaptive hash index. buf_pool_clear_hash_index(): Declare static, and move to the same compilation unit with the bulk of the adaptive hash index code. dict_index_t::clone(), dict_index_t::clone_if_needed(): Clone an index that is being rebuilt while adaptive hash index entries exist. The original index will be inserted into dict_table_t::freed_indexes and dict_index_t::set_freed() will be called. dict_index_t::set_freed(), dict_index_t::freed(): Note that or check whether the index has been freed. We will use the impossible page number 1 to denote this condition. dict_index_t::n_ahi_pages(): Replaces btr_search_info_get_ref_count(). dict_index_t::detach_columns(): Move the assignment n_fields=0 to ha_innobase_inplace_ctx::clear_added_indexes(). We must have access to the columns when freeing the adaptive hash index. Note: dict_table_t::v_cols[] will remain valid. If virtual columns are dropped or added, the table definition will be reloaded in ha_innobase::commit_inplace_alter_table(). buf_page_mtr_lock(): Drop a stale adaptive hash index if needed. We will also reduce the number of btr_get_search_latch() calls and enclose some more code inside #ifdef BTR_CUR_HASH_ADAPT in order to benefit cmake -DWITH_INNODB_AHI=OFF.
6 years ago
10 years ago
10 years ago
10 years ago
9 years ago
MDEV-12266: Change dict_table_t::space to fil_space_t* InnoDB always keeps all tablespaces in the fil_system cache. The fil_system.LRU is only for closing file handles; the fil_space_t and fil_node_t for all data files will remain in main memory. Between startup to shutdown, they can only be created and removed by DDL statements. Therefore, we can let dict_table_t::space point directly to the fil_space_t. dict_table_t::space_id: A numeric tablespace ID for the corner cases where we do not have a tablespace. The most prominent examples are ALTER TABLE...DISCARD TABLESPACE or a missing or corrupted file. There are a few functional differences; most notably: (1) DROP TABLE will delete matching .ibd and .cfg files, even if they were not attached to the data dictionary. (2) Some error messages will report file names instead of numeric IDs. There still are many functions that use numeric tablespace IDs instead of fil_space_t*, and many functions could be converted to fil_space_t member functions. Also, Tablespace and Datafile should be merged with fil_space_t and fil_node_t. page_id_t and buf_page_get_gen() could use fil_space_t& instead of a numeric ID, and after moving to a single buffer pool (MDEV-15058), buf_pool_t::page_hash could be moved to fil_space_t::page_hash. FilSpace: Remove. Only few calls to fil_space_acquire() will remain, and gradually they should be removed. mtr_t::set_named_space_id(ulint): Renamed from set_named_space(), to prevent accidental calls to this slower function. Very few callers remain. fseg_create(), fsp_reserve_free_extents(): Take fil_space_t* as a parameter instead of a space_id. fil_space_t::rename(): Wrapper for fil_rename_tablespace_check(), fil_name_write_rename(), fil_rename_tablespace(). Mariabackup passes the parameter log=false; InnoDB passes log=true. dict_mem_table_create(): Take fil_space_t* instead of space_id as parameter. dict_process_sys_tables_rec_and_mtr_commit(): Replace the parameter 'status' with 'bool cached'. dict_get_and_save_data_dir_path(): Avoid copying the fil_node_t::name. fil_ibd_open(): Return the tablespace. fil_space_t::set_imported(): Replaces fil_space_set_imported(). truncate_t: Change many member function parameters to fil_space_t*, and remove page_size parameters. row_truncate_prepare(): Merge to its only caller. row_drop_table_from_cache(): Assert that the table is persistent. dict_create_sys_indexes_tuple(): Write SYS_INDEXES.SPACE=FIL_NULL if the tablespace has been discarded. row_import_update_discarded_flag(): Remove a constant parameter.
8 years ago
MDEV-12266: Change dict_table_t::space to fil_space_t* InnoDB always keeps all tablespaces in the fil_system cache. The fil_system.LRU is only for closing file handles; the fil_space_t and fil_node_t for all data files will remain in main memory. Between startup to shutdown, they can only be created and removed by DDL statements. Therefore, we can let dict_table_t::space point directly to the fil_space_t. dict_table_t::space_id: A numeric tablespace ID for the corner cases where we do not have a tablespace. The most prominent examples are ALTER TABLE...DISCARD TABLESPACE or a missing or corrupted file. There are a few functional differences; most notably: (1) DROP TABLE will delete matching .ibd and .cfg files, even if they were not attached to the data dictionary. (2) Some error messages will report file names instead of numeric IDs. There still are many functions that use numeric tablespace IDs instead of fil_space_t*, and many functions could be converted to fil_space_t member functions. Also, Tablespace and Datafile should be merged with fil_space_t and fil_node_t. page_id_t and buf_page_get_gen() could use fil_space_t& instead of a numeric ID, and after moving to a single buffer pool (MDEV-15058), buf_pool_t::page_hash could be moved to fil_space_t::page_hash. FilSpace: Remove. Only few calls to fil_space_acquire() will remain, and gradually they should be removed. mtr_t::set_named_space_id(ulint): Renamed from set_named_space(), to prevent accidental calls to this slower function. Very few callers remain. fseg_create(), fsp_reserve_free_extents(): Take fil_space_t* as a parameter instead of a space_id. fil_space_t::rename(): Wrapper for fil_rename_tablespace_check(), fil_name_write_rename(), fil_rename_tablespace(). Mariabackup passes the parameter log=false; InnoDB passes log=true. dict_mem_table_create(): Take fil_space_t* instead of space_id as parameter. dict_process_sys_tables_rec_and_mtr_commit(): Replace the parameter 'status' with 'bool cached'. dict_get_and_save_data_dir_path(): Avoid copying the fil_node_t::name. fil_ibd_open(): Return the tablespace. fil_space_t::set_imported(): Replaces fil_space_set_imported(). truncate_t: Change many member function parameters to fil_space_t*, and remove page_size parameters. row_truncate_prepare(): Merge to its only caller. row_drop_table_from_cache(): Assert that the table is persistent. dict_create_sys_indexes_tuple(): Write SYS_INDEXES.SPACE=FIL_NULL if the tablespace has been discarded. row_import_update_discarded_flag(): Remove a constant parameter.
8 years ago
MDEV-12266: Change dict_table_t::space to fil_space_t* InnoDB always keeps all tablespaces in the fil_system cache. The fil_system.LRU is only for closing file handles; the fil_space_t and fil_node_t for all data files will remain in main memory. Between startup to shutdown, they can only be created and removed by DDL statements. Therefore, we can let dict_table_t::space point directly to the fil_space_t. dict_table_t::space_id: A numeric tablespace ID for the corner cases where we do not have a tablespace. The most prominent examples are ALTER TABLE...DISCARD TABLESPACE or a missing or corrupted file. There are a few functional differences; most notably: (1) DROP TABLE will delete matching .ibd and .cfg files, even if they were not attached to the data dictionary. (2) Some error messages will report file names instead of numeric IDs. There still are many functions that use numeric tablespace IDs instead of fil_space_t*, and many functions could be converted to fil_space_t member functions. Also, Tablespace and Datafile should be merged with fil_space_t and fil_node_t. page_id_t and buf_page_get_gen() could use fil_space_t& instead of a numeric ID, and after moving to a single buffer pool (MDEV-15058), buf_pool_t::page_hash could be moved to fil_space_t::page_hash. FilSpace: Remove. Only few calls to fil_space_acquire() will remain, and gradually they should be removed. mtr_t::set_named_space_id(ulint): Renamed from set_named_space(), to prevent accidental calls to this slower function. Very few callers remain. fseg_create(), fsp_reserve_free_extents(): Take fil_space_t* as a parameter instead of a space_id. fil_space_t::rename(): Wrapper for fil_rename_tablespace_check(), fil_name_write_rename(), fil_rename_tablespace(). Mariabackup passes the parameter log=false; InnoDB passes log=true. dict_mem_table_create(): Take fil_space_t* instead of space_id as parameter. dict_process_sys_tables_rec_and_mtr_commit(): Replace the parameter 'status' with 'bool cached'. dict_get_and_save_data_dir_path(): Avoid copying the fil_node_t::name. fil_ibd_open(): Return the tablespace. fil_space_t::set_imported(): Replaces fil_space_set_imported(). truncate_t: Change many member function parameters to fil_space_t*, and remove page_size parameters. row_truncate_prepare(): Merge to its only caller. row_drop_table_from_cache(): Assert that the table is persistent. dict_create_sys_indexes_tuple(): Write SYS_INDEXES.SPACE=FIL_NULL if the tablespace has been discarded. row_import_update_discarded_flag(): Remove a constant parameter.
8 years ago
MDEV-24971 InnoDB access freed virtual column after rollback of secondary index Problem: ======== InnoDB fails to clean the index stub if it fails to add the virtual index which contains new virtual column. But it clears the newly virtual column from index in clear_added_indexes() during inplace_alter_table. On commit, InnoDB evicts and reload the table. In case of rollback, it doesn't happen. InnoDB clears the ABORTED index while opening the table or doing the DDL. In the mean time, InnoDB can access the dropped virtual index columns while creating prebuilt or rollback of concurrent DML. Solution: ========== (1) InnoDB should maintain newly added virtual column while rollbacking the newly added virtual index. (2) InnoDB must not defer the index removal if the alter table is executed with LOCK=EXCLUSIVE. (3) For LOCK=SHARED, InnoDB should check whether the table has any other transaction lock other than alter transaction before deferring the index stub. Replaced has_new_v_col with dict_add_vcol_info in dict_index_t to indicate whether the index has any new virtual column. dict_index_t::has_new_v_col(): Returns whether the index has newly added virtual column, it doesn't say which columns are newly added virtual column ha_innobase_inplace_ctx::is_new_vcol(): Return whether the given column is added as a part of the current alter. ha_innobase_inplace_ctx::clean_new_vcol_index(): Copy the newly added virtual column to new_vcol_info in dict_index_t. Replace the column in the index fields with virtual column stored in new_vcol_info. dict_index_t::assign_new_v_col(): Store the number of virtual column added in index as a part of alter table. dict_index_t::get_n_new_vcol(): Get the number of newly added virtual column dict_index_t::assign_drop_v_col(): Allocate the memory for adding new virtual column in new_vcol_info. dict_index_t::add_drop_v_col(): Add the newly added virtual column in new_vcol_info. dict_table_t::has_lock_for_other_trx(): Whether the table has any other transaction lock than given transaction. row_merge_drop_indexes(): Add parameter alter_trx and check whether the table has any other lock than alter transaction.
5 years ago
MDEV-24971 InnoDB access freed virtual column after rollback of secondary index Problem: ======== InnoDB fails to clean the index stub if it fails to add the virtual index which contains new virtual column. But it clears the newly virtual column from index in clear_added_indexes() during inplace_alter_table. On commit, InnoDB evicts and reload the table. In case of rollback, it doesn't happen. InnoDB clears the ABORTED index while opening the table or doing the DDL. In the mean time, InnoDB can access the dropped virtual index columns while creating prebuilt or rollback of concurrent DML. Solution: ========== (1) InnoDB should maintain newly added virtual column while rollbacking the newly added virtual index. (2) InnoDB must not defer the index removal if the alter table is executed with LOCK=EXCLUSIVE. (3) For LOCK=SHARED, InnoDB should check whether the table has any other transaction lock other than alter transaction before deferring the index stub. Replaced has_new_v_col with dict_add_vcol_info in dict_index_t to indicate whether the index has any new virtual column. dict_index_t::has_new_v_col(): Returns whether the index has newly added virtual column, it doesn't say which columns are newly added virtual column ha_innobase_inplace_ctx::is_new_vcol(): Return whether the given column is added as a part of the current alter. ha_innobase_inplace_ctx::clean_new_vcol_index(): Copy the newly added virtual column to new_vcol_info in dict_index_t. Replace the column in the index fields with virtual column stored in new_vcol_info. dict_index_t::assign_new_v_col(): Store the number of virtual column added in index as a part of alter table. dict_index_t::get_n_new_vcol(): Get the number of newly added virtual column dict_index_t::assign_drop_v_col(): Allocate the memory for adding new virtual column in new_vcol_info. dict_index_t::add_drop_v_col(): Add the newly added virtual column in new_vcol_info. dict_table_t::has_lock_for_other_trx(): Whether the table has any other transaction lock than given transaction. row_merge_drop_indexes(): Add parameter alter_trx and check whether the table has any other lock than alter transaction.
5 years ago
MDEV-13564 Mariabackup does not work with TRUNCATE Implement undo tablespace truncation via normal redo logging. Implement TRUNCATE TABLE as a combination of RENAME to #sql-ib name, CREATE, and DROP. Note: Orphan #sql-ib*.ibd may be left behind if MariaDB Server 10.2 is killed before the DROP operation is committed. If MariaDB Server 10.2 is killed during TRUNCATE, it is also possible that the old table was renamed to #sql-ib*.ibd but the data dictionary will refer to the table using the original name. In MariaDB Server 10.3, RENAME inside InnoDB is transactional, and #sql-* tables will be dropped on startup. So, this new TRUNCATE will be fully crash-safe in 10.3. ha_mroonga::wrapper_truncate(): Pass table options to the underlying storage engine, now that ha_innobase::truncate() will need them. rpl_slave_state::truncate_state_table(): Before truncating mysql.gtid_slave_pos, evict any cached table handles from the table definition cache, so that there will be no stale references to the old table after truncating. == TRUNCATE TABLE == WL#6501 in MySQL 5.7 introduced separate log files for implementing atomic and crash-safe TRUNCATE TABLE, instead of using the InnoDB undo and redo log. Some convoluted logic was added to the InnoDB crash recovery, and some extra synchronization (including a redo log checkpoint) was introduced to make this work. This synchronization has caused performance problems and race conditions, and the extra log files cannot be copied or applied by external backup programs. In order to support crash-upgrade from MariaDB 10.2, we will keep the logic for parsing and applying the extra log files, but we will no longer generate those files in TRUNCATE TABLE. A prerequisite for crash-safe TRUNCATE is a crash-safe RENAME TABLE (with full redo and undo logging and proper rollback). This will be implemented in MDEV-14717. ha_innobase::truncate(): Invoke RENAME, create(), delete_table(). Because RENAME cannot be fully rolled back before MariaDB 10.3 due to missing undo logging, add some explicit rename-back in case the operation fails. ha_innobase::delete(): Introduce a variant that takes sqlcom as a parameter. In TRUNCATE TABLE, we do not want to touch any FOREIGN KEY constraints. ha_innobase::create(): Add the parameters file_per_table, trx. In TRUNCATE, the new table must be created in the same transaction that renames the old table. create_table_info_t::create_table_info_t(): Add the parameters file_per_table, trx. row_drop_table_for_mysql(): Replace a bool parameter with sqlcom. row_drop_table_after_create_fail(): New function, wrapping row_drop_table_for_mysql(). dict_truncate_index_tree_in_mem(), fil_truncate_tablespace(), fil_prepare_for_truncate(), fil_reinit_space_header_for_table(), row_truncate_table_for_mysql(), TruncateLogger, row_truncate_prepare(), row_truncate_rollback(), row_truncate_complete(), row_truncate_fts(), row_truncate_update_system_tables(), row_truncate_foreign_key_checks(), row_truncate_sanity_checks(): Remove. row_upd_check_references_constraints(): Remove a check for TRUNCATE, now that the table is no longer truncated in place. The new test innodb.truncate_foreign uses DEBUG_SYNC to cover some race-condition like scenarios. The test innodb-innodb.truncate does not use any synchronization. We add a redo log subformat to indicate backup-friendly format. MariaDB 10.4 will remove support for the old TRUNCATE logging, so crash-upgrade from old 10.2 or 10.3 to 10.4 will involve limitations. == Undo tablespace truncation == MySQL 5.7 implements undo tablespace truncation. It is only possible when innodb_undo_tablespaces is set to at least 2. The logging is implemented similar to the WL#6501 TRUNCATE, that is, using separate log files and a redo log checkpoint. We can simply implement undo tablespace truncation within a single mini-transaction that reinitializes the undo log tablespace file. Unfortunately, due to the redo log format of some operations, currently, the total redo log written by undo tablespace truncation will be more than the combined size of the truncated undo tablespace. It should be acceptable to have a little more than 1 megabyte of log in a single mini-transaction. This will be fixed in MDEV-17138 in MariaDB Server 10.4. recv_sys_t: Add truncated_undo_spaces[] to remember for which undo tablespaces a MLOG_FILE_CREATE2 record was seen. namespace undo: Remove some unnecessary declarations. fil_space_t::is_being_truncated: Document that this flag now only applies to undo tablespaces. Remove some references. fil_space_t::is_stopping(): Do not refer to is_being_truncated. This check is for tablespaces of tables. Potentially used tablespaces are never truncated any more. buf_dblwr_process(): Suppress the out-of-bounds warning for undo tablespaces. fil_truncate_log(): Write a MLOG_FILE_CREATE2 with a nonzero page number (new size of the tablespace in pages) to inform crash recovery that the undo tablespace size has been reduced. fil_op_write_log(): Relax assertions, so that MLOG_FILE_CREATE2 can be written for undo tablespaces (without .ibd file suffix) for a nonzero page number. os_file_truncate(): Add the parameter allow_shrink=false so that undo tablespaces can actually be shrunk using this function. fil_name_parse(): For undo tablespace truncation, buffer MLOG_FILE_CREATE2 in truncated_undo_spaces[]. recv_read_in_area(): Avoid reading pages for which no redo log records remain buffered, after recv_addr_trim() removed them. trx_rseg_header_create(): Add a FIXME comment that we could write much less redo log. trx_undo_truncate_tablespace(): Reinitialize the undo tablespace in a single mini-transaction, which will be flushed to the redo log before the file size is trimmed. recv_addr_trim(): Discard any redo logs for pages that were logged after the new end of a file, before the truncation LSN. If the rec_list becomes empty, reduce n_addrs. After removing any affected records, actually truncate the file. recv_apply_hashed_log_recs(): Invoke recv_addr_trim() right before applying any log records. The undo tablespace files must be open at this point. buf_flush_or_remove_pages(), buf_flush_dirty_pages(), buf_LRU_flush_or_remove_pages(): Add a parameter for specifying the number of the first page to flush or remove (default 0). trx_purge_initiate_truncate(): Remove the log checkpoints, the extra logging, and some unnecessary crash points. Merge the code from trx_undo_truncate_tablespace(). First, flush all to-be-discarded pages (beyond the new end of the file), then trim the space->size to make the page allocation deterministic. At the only remaining crash injection point, flush the redo log, so that the recovery can be tested.
7 years ago
MDEV-11738: Mariadb uses 100% of several of my 8 cpus doing nothing MDEV-11581: Mariadb starts InnoDB encryption threads when key has not changed or data scrubbing turned off Background: Key rotation is based on background threads (innodb-encryption-threads) periodically going through all tablespaces on fil_system. For each tablespace current used key version is compared to max key age (innodb-encryption-rotate-key-age). This process naturally takes CPU. Similarly, in same time need for scrubbing is investigated. Currently, key rotation is fully supported on Amazon AWS key management plugin only but InnoDB does not have knowledge what key management plugin is used. This patch re-purposes innodb-encryption-rotate-key-age=0 to disable key rotation and background data scrubbing. All new tables are added to special list for key rotation and key rotation is based on sending a event to background encryption threads instead of using periodic checking (i.e. timeout). fil0fil.cc: Added functions fil_space_acquire_low() to acquire a tablespace when it could be dropped concurrently. This function is used from fil_space_acquire() or fil_space_acquire_silent() that will not print any messages if we try to acquire space that does not exist. fil_space_release() to release a acquired tablespace. fil_space_next() to iterate tablespaces in fil_system using fil_space_acquire() and fil_space_release(). Similarly, fil_space_keyrotation_next() to iterate new list fil_system->rotation_list where new tables. are added if key rotation is disabled. Removed unnecessary functions fil_get_first_space_safe() fil_get_next_space_safe() fil_node_open_file(): After page 0 is read read also crypt_info if it is not yet read. btr_scrub_lock_dict_func() buf_page_check_corrupt() buf_page_encrypt_before_write() buf_merge_or_delete_for_page() lock_print_info_all_transactions() row_fts_psort_info_init() row_truncate_table_for_mysql() row_drop_table_for_mysql() Use fil_space_acquire()/release() to access fil_space_t. buf_page_decrypt_after_read(): Use fil_space_get_crypt_data() because at this point we might not yet have read page 0. fil0crypt.cc/fil0fil.h: Lot of changes. Pass fil_space_t* directly to functions needing it and store fil_space_t* to rotation state. Use fil_space_acquire()/release() when iterating tablespaces and removed unnecessary is_closing from fil_crypt_t. Use fil_space_t::is_stopping() to detect when access to tablespace should be stopped. Removed unnecessary fil_space_get_crypt_data(). fil_space_create(): Inform key rotation that there could be something to do if key rotation is disabled and new table with encryption enabled is created. Remove unnecessary functions fil_get_first_space_safe() and fil_get_next_space_safe(). fil_space_acquire() and fil_space_release() are used instead. Moved fil_space_get_crypt_data() and fil_space_set_crypt_data() to fil0crypt.cc. fsp_header_init(): Acquire fil_space_t*, write crypt_data and release space. check_table_options() Renamed FIL_SPACE_ENCRYPTION_* TO FIL_ENCRYPTION_* i_s.cc: Added ROTATING_OR_FLUSHING field to information_schema.innodb_tablespace_encryption to show current status of key rotation.
9 years ago
12 years ago
MDEV-12634: Uninitialised ROW_MERGE_RESERVE_SIZE bytes written to tem… …porary file Fixed by removing writing key version to start of every block that was encrypted. Instead we will use single key version from log_sys crypt info. After this MDEV also blocks writen to row log are encrypted and blocks read from row log aren decrypted if encryption is configured for the table. innodb_status_variables[], struct srv_stats_t Added status variables for merge block and row log block encryption and decryption amounts. Removed ROW_MERGE_RESERVE_SIZE define. row_merge_fts_doc_tokenize Remove ROW_MERGE_RESERVE_SIZE row_log_t Add index, crypt_tail, crypt_head to be used in case of encryption. row_log_online_op, row_log_table_close_func Before writing a block encrypt it if encryption is enabled row_log_table_apply_ops, row_log_apply_ops After reading a block decrypt it if encryption is enabled row_log_allocate Allocate temporary buffers crypt_head and crypt_tail if needed. row_log_free Free temporary buffers crypt_head and crypt_tail if they exist. row_merge_encrypt_buf, row_merge_decrypt_buf Removed. row_merge_buf_create, row_merge_buf_write Remove ROW_MERGE_RESERVE_SIZE row_merge_build_indexes Allocate temporary buffer used in decryption and encryption if needed. log_tmp_blocks_crypt, log_tmp_block_encrypt, log_temp_block_decrypt New functions used in block encryption and decryption log_tmp_is_encrypted New function to check is encryption enabled. Added test case innodb-rowlog to force creating a row log and verify that operations are done using introduced status variables.
8 years ago
12 years ago
12 years ago
MDEV-12253: Buffer pool blocks are accessed after they have been freed Problem was that bpage was referenced after it was already freed from LRU. Fixed by adding a new variable encrypted that is passed down to buf_page_check_corrupt() and used in buf_page_get_gen() to stop processing page read. This patch should also address following test failures and bugs: MDEV-12419: IMPORT should not look up tablespace in PageConverter::validate(). This is now removed. MDEV-10099: encryption.innodb_onlinealter_encryption fails sporadically in buildbot MDEV-11420: encryption.innodb_encryption-page-compression failed in buildbot MDEV-11222: encryption.encrypt_and_grep failed in buildbot on P8 Removed dict_table_t::is_encrypted and dict_table_t::ibd_file_missing and replaced these with dict_table_t::file_unreadable. Table ibd file is missing if fil_get_space(space_id) returns NULL and encrypted if not. Removed dict_table_t::is_corrupted field. Ported FilSpace class from 10.2 and using that on buf_page_check_corrupt(), buf_page_decrypt_after_read(), buf_page_encrypt_before_write(), buf_dblwr_process(), buf_read_page(), dict_stats_save_defrag_stats(). Added test cases when enrypted page could be read while doing redo log crash recovery. Also added test case for row compressed blobs. btr_cur_open_at_index_side_func(), btr_cur_open_at_rnd_pos_func(): Avoid referencing block that is NULL. buf_page_get_zip(): Issue error if page read fails. buf_page_get_gen(): Use dberr_t for error detection and do not reference bpage after we hare freed it. buf_mark_space_corrupt(): remove bpage from LRU also when it is encrypted. buf_page_check_corrupt(): @return DB_SUCCESS if page has been read and is not corrupted, DB_PAGE_CORRUPTED if page based on checksum check is corrupted, DB_DECRYPTION_FAILED if page post encryption checksum matches but after decryption normal page checksum does not match. In read case only DB_SUCCESS is possible. buf_page_io_complete(): use dberr_t for error handling. buf_flush_write_block_low(), buf_read_ahead_random(), buf_read_page_async(), buf_read_ahead_linear(), buf_read_ibuf_merge_pages(), buf_read_recv_pages(), fil_aio_wait(): Issue error if page read fails. btr_pcur_move_to_next_page(): Do not reference page if it is NULL. Introduced dict_table_t::is_readable() and dict_index_t::is_readable() that will return true if tablespace exists and pages read from tablespace are not corrupted or page decryption failed. Removed buf_page_t::key_version. After page decryption the key version is not removed from page frame. For unencrypted pages, old key_version is removed at buf_page_encrypt_before_write() dict_stats_update_transient_for_index(), dict_stats_update_transient() Do not continue if table decryption failed or table is corrupted. dict0stats.cc: Introduced a dict_stats_report_error function to avoid code duplication. fil_parse_write_crypt_data(): Check that key read from redo log entry is found from encryption plugin and if it is not, refuse to start. PageConverter::validate(): Removed access to fil_space_t as tablespace is not available during import. Fixed error code on innodb.innodb test. Merged test cased innodb-bad-key-change5 and innodb-bad-key-shutdown to innodb-bad-key-change2. Removed innodb-bad-key-change5 test. Decreased unnecessary complexity on some long lasting tests. Removed fil_inc_pending_ops(), fil_decr_pending_ops(), fil_get_first_space(), fil_get_next_space(), fil_get_first_space_safe(), fil_get_next_space_safe() functions. fil_space_verify_crypt_checksum(): Fixed bug found using ASAN where FIL_PAGE_END_LSN_OLD_CHECKSUM field was incorrectly accessed from row compressed tables. Fixed out of page frame bug for row compressed tables in fil_space_verify_crypt_checksum() found using ASAN. Incorrect function was called for compressed table. Added new tests for discard, rename table and drop (we should allow them even when page decryption fails). Alter table rename is not allowed. Added test for restart with innodb-force-recovery=1 when page read on redo-recovery cant be decrypted. Added test for corrupted table where both page data and FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION is corrupted. Adjusted the test case innodb_bug14147491 so that it does not anymore expect crash. Instead table is just mostly not usable. fil0fil.h: fil_space_acquire_low is not visible function and fil_space_acquire and fil_space_acquire_silent are inline functions. FilSpace class uses fil_space_acquire_low directly. recv_apply_hashed_log_recs() does not return anything.
9 years ago
MDEV-12266: Change dict_table_t::space to fil_space_t* InnoDB always keeps all tablespaces in the fil_system cache. The fil_system.LRU is only for closing file handles; the fil_space_t and fil_node_t for all data files will remain in main memory. Between startup to shutdown, they can only be created and removed by DDL statements. Therefore, we can let dict_table_t::space point directly to the fil_space_t. dict_table_t::space_id: A numeric tablespace ID for the corner cases where we do not have a tablespace. The most prominent examples are ALTER TABLE...DISCARD TABLESPACE or a missing or corrupted file. There are a few functional differences; most notably: (1) DROP TABLE will delete matching .ibd and .cfg files, even if they were not attached to the data dictionary. (2) Some error messages will report file names instead of numeric IDs. There still are many functions that use numeric tablespace IDs instead of fil_space_t*, and many functions could be converted to fil_space_t member functions. Also, Tablespace and Datafile should be merged with fil_space_t and fil_node_t. page_id_t and buf_page_get_gen() could use fil_space_t& instead of a numeric ID, and after moving to a single buffer pool (MDEV-15058), buf_pool_t::page_hash could be moved to fil_space_t::page_hash. FilSpace: Remove. Only few calls to fil_space_acquire() will remain, and gradually they should be removed. mtr_t::set_named_space_id(ulint): Renamed from set_named_space(), to prevent accidental calls to this slower function. Very few callers remain. fseg_create(), fsp_reserve_free_extents(): Take fil_space_t* as a parameter instead of a space_id. fil_space_t::rename(): Wrapper for fil_rename_tablespace_check(), fil_name_write_rename(), fil_rename_tablespace(). Mariabackup passes the parameter log=false; InnoDB passes log=true. dict_mem_table_create(): Take fil_space_t* instead of space_id as parameter. dict_process_sys_tables_rec_and_mtr_commit(): Replace the parameter 'status' with 'bool cached'. dict_get_and_save_data_dir_path(): Avoid copying the fil_node_t::name. fil_ibd_open(): Return the tablespace. fil_space_t::set_imported(): Replaces fil_space_set_imported(). truncate_t: Change many member function parameters to fil_space_t*, and remove page_size parameters. row_truncate_prepare(): Merge to its only caller. row_drop_table_from_cache(): Assert that the table is persistent. dict_create_sys_indexes_tuple(): Write SYS_INDEXES.SPACE=FIL_NULL if the tablespace has been discarded. row_import_update_discarded_flag(): Remove a constant parameter.
8 years ago
MDEV-13654 Various crashes due to DB_TRX_ID mismatch in table-rebuilding ALTER TABLE…LOCK=NONE After MDEV-12288 and MDEV-13536, the DB_TRX_ID of old clustered index records for which no history is available should be reset to 0. This caused crashes in online table-rebuilding ALTER, because the row_log_table_apply() is built on the assumption that the PRIMARY KEY together with DB_TRX_ID,DB_ROLL_PTR identifies the record. Both when copying the old table and when writing log about changes to the old table, we must map "old" DB_TRX_ID to 0. "old" here is simply "older than the trx_id of the ALTER TABLE transaction", because the MDL_EXCLUSIVE (and exclusive InnoDB table lock) in ha_innobase::prepare_inplace_alter_table() forces any transactions accessing the table to commit or rollback. So, we know that we can safely reset any DB_TRX_ID in the table that is older than the transaction ID of the ALTER TABLE, because the undo log history would be lost in a table-rebuilding ALTER. Note: After a table-rebuilding online ALTER TABLE, the rebuilt table may end up containing some nonzero DB_TRX_ID columns. The apply logic identifies the rows by the combination of PRIMARY KEY and DB_TRX_ID. These nonzero DB_TRX_ID would necessarily refer to concurrent DML operations that were started during ha_innobase::inplace_alter_table(). row_log_allocate(): Add a parameter for the ALTER TABLE transaction. row_log_t::min_trx: The ALTER TABLE transaction ID. trx_id_check(): A debug function to check that DB_TRX_ID makes sense (is either 0 or bigger than the ALTER TABLE transaction ID). reset_trx_id[]: The reset DB_TRX_ID,DB_ROLL_PTR columns. row_log_table_delete(), row_log_table_get_pk(): Reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. row_log_table_apply_delete(), row_log_table_apply_update(): Assert trx_id_check(). row_merge_insert_index_tuples(): Remove the unused parameter trx_id. row_merge_read_clustered_index(): In a table-rebuilding ALTER, reset the DB_TRX_ID,DB_ROLL_PTR when they precede the ALTER TABLE transaction. Assert trx_id_check() on clustered index records that are being buffered.
8 years ago
MDEV-12266: Change dict_table_t::space to fil_space_t* InnoDB always keeps all tablespaces in the fil_system cache. The fil_system.LRU is only for closing file handles; the fil_space_t and fil_node_t for all data files will remain in main memory. Between startup to shutdown, they can only be created and removed by DDL statements. Therefore, we can let dict_table_t::space point directly to the fil_space_t. dict_table_t::space_id: A numeric tablespace ID for the corner cases where we do not have a tablespace. The most prominent examples are ALTER TABLE...DISCARD TABLESPACE or a missing or corrupted file. There are a few functional differences; most notably: (1) DROP TABLE will delete matching .ibd and .cfg files, even if they were not attached to the data dictionary. (2) Some error messages will report file names instead of numeric IDs. There still are many functions that use numeric tablespace IDs instead of fil_space_t*, and many functions could be converted to fil_space_t member functions. Also, Tablespace and Datafile should be merged with fil_space_t and fil_node_t. page_id_t and buf_page_get_gen() could use fil_space_t& instead of a numeric ID, and after moving to a single buffer pool (MDEV-15058), buf_pool_t::page_hash could be moved to fil_space_t::page_hash. FilSpace: Remove. Only few calls to fil_space_acquire() will remain, and gradually they should be removed. mtr_t::set_named_space_id(ulint): Renamed from set_named_space(), to prevent accidental calls to this slower function. Very few callers remain. fseg_create(), fsp_reserve_free_extents(): Take fil_space_t* as a parameter instead of a space_id. fil_space_t::rename(): Wrapper for fil_rename_tablespace_check(), fil_name_write_rename(), fil_rename_tablespace(). Mariabackup passes the parameter log=false; InnoDB passes log=true. dict_mem_table_create(): Take fil_space_t* instead of space_id as parameter. dict_process_sys_tables_rec_and_mtr_commit(): Replace the parameter 'status' with 'bool cached'. dict_get_and_save_data_dir_path(): Avoid copying the fil_node_t::name. fil_ibd_open(): Return the tablespace. fil_space_t::set_imported(): Replaces fil_space_set_imported(). truncate_t: Change many member function parameters to fil_space_t*, and remove page_size parameters. row_truncate_prepare(): Merge to its only caller. row_drop_table_from_cache(): Assert that the table is persistent. dict_create_sys_indexes_tuple(): Write SYS_INDEXES.SPACE=FIL_NULL if the tablespace has been discarded. row_import_update_discarded_flag(): Remove a constant parameter.
8 years ago
12 years ago
12 years ago
  1. /*****************************************************************************
  2. Copyright (c) 2005, 2017, Oracle and/or its affiliates. All Rights Reserved.
  3. Copyright (c) 2014, 2022, MariaDB Corporation.
  4. This program is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free Software
  6. Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. this program; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
  13. *****************************************************************************/
  14. /**************************************************//**
  15. @file row/row0merge.cc
  16. New index creation routines using a merge sort
  17. Created 12/4/2005 Jan Lindstrom
  18. Completed by Sunny Bains and Marko Makela
  19. *******************************************************/
  20. #include <my_global.h>
  21. #include <log.h>
  22. #include <sql_class.h>
  23. #include <math.h>
  24. #include "row0merge.h"
  25. #include "row0ext.h"
  26. #include "row0log.h"
  27. #include "row0ins.h"
  28. #include "row0row.h"
  29. #include "row0sel.h"
  30. #include "log0crypt.h"
  31. #include "dict0crea.h"
  32. #include "trx0purge.h"
  33. #include "lock0lock.h"
  34. #include "pars0pars.h"
  35. #include "ut0sort.h"
  36. #include "row0ftsort.h"
  37. #include "row0import.h"
  38. #include "row0vers.h"
  39. #include "handler0alter.h"
  40. #include "btr0bulk.h"
  41. #ifdef BTR_CUR_ADAPT
  42. # include "btr0sea.h"
  43. #endif /* BTR_CUR_ADAPT */
  44. #include "ut0stage.h"
  45. #include "fil0crypt.h"
  46. /* Ignore posix_fadvise() on those platforms where it does not exist */
  47. #if defined _WIN32
  48. # define posix_fadvise(fd, offset, len, advice) /* nothing */
  49. #endif /* _WIN32 */
  50. /* Whether to disable file system cache */
  51. char srv_disable_sort_file_cache;
  52. /** Class that caches index row tuples made from a single cluster
  53. index page scan, and then insert into corresponding index tree */
  54. class index_tuple_info_t {
  55. public:
  56. /** constructor
  57. @param[in] heap memory heap
  58. @param[in] index index to be created */
  59. index_tuple_info_t(
  60. mem_heap_t* heap,
  61. dict_index_t* index) UNIV_NOTHROW
  62. {
  63. m_heap = heap;
  64. m_index = index;
  65. m_dtuple_vec = UT_NEW_NOKEY(idx_tuple_vec());
  66. }
  67. /** destructor */
  68. ~index_tuple_info_t()
  69. {
  70. UT_DELETE(m_dtuple_vec);
  71. }
  72. /** Get the index object
  73. @return the index object */
  74. dict_index_t* get_index() UNIV_NOTHROW
  75. {
  76. return(m_index);
  77. }
  78. /** Caches an index row into index tuple vector
  79. @param[in] row table row
  80. @param[in] ext externally stored column
  81. prefixes, or NULL */
  82. void add(
  83. const dtuple_t* row,
  84. const row_ext_t* ext) UNIV_NOTHROW
  85. {
  86. dtuple_t* dtuple;
  87. dtuple = row_build_index_entry(row, ext, m_index, m_heap);
  88. ut_ad(dtuple);
  89. m_dtuple_vec->push_back(dtuple);
  90. }
  91. /** Insert spatial index rows cached in vector into spatial index
  92. @param[in] trx_id transaction id
  93. @param[in,out] row_heap memory heap
  94. @param[in] pcur cluster index scanning cursor
  95. @param[in,out] scan_mtr mini-transaction for pcur
  96. @return DB_SUCCESS if successful, else error number */
  97. inline dberr_t insert(
  98. trx_id_t trx_id,
  99. mem_heap_t* row_heap,
  100. btr_pcur_t* pcur,
  101. mtr_t* scan_mtr)
  102. {
  103. big_rec_t* big_rec;
  104. rec_t* rec;
  105. btr_cur_t ins_cur;
  106. mtr_t mtr;
  107. rtr_info_t rtr_info;
  108. rec_offs* ins_offsets = NULL;
  109. dberr_t error = DB_SUCCESS;
  110. dtuple_t* dtuple;
  111. ulint count = 0;
  112. const ulint flag = BTR_NO_UNDO_LOG_FLAG
  113. | BTR_NO_LOCKING_FLAG
  114. | BTR_KEEP_SYS_FLAG | BTR_CREATE_FLAG;
  115. ut_ad(dict_index_is_spatial(m_index));
  116. DBUG_EXECUTE_IF("row_merge_instrument_log_check_flush",
  117. log_sys.check_flush_or_checkpoint = true;
  118. );
  119. for (idx_tuple_vec::iterator it = m_dtuple_vec->begin();
  120. it != m_dtuple_vec->end();
  121. ++it) {
  122. dtuple = *it;
  123. ut_ad(dtuple);
  124. if (log_sys.check_flush_or_checkpoint) {
  125. if (scan_mtr->is_active()) {
  126. btr_pcur_move_to_prev_on_page(pcur);
  127. btr_pcur_store_position(pcur, scan_mtr);
  128. scan_mtr->commit();
  129. }
  130. log_free_check();
  131. }
  132. mtr.start();
  133. m_index->set_modified(mtr);
  134. ins_cur.index = m_index;
  135. rtr_init_rtr_info(&rtr_info, false, &ins_cur, m_index,
  136. false);
  137. rtr_info_update_btr(&ins_cur, &rtr_info);
  138. btr_cur_search_to_nth_level(m_index, 0, dtuple,
  139. PAGE_CUR_RTREE_INSERT,
  140. BTR_MODIFY_LEAF, &ins_cur,
  141. 0, __FILE__, __LINE__,
  142. &mtr);
  143. /* It need to update MBR in parent entry,
  144. so change search mode to BTR_MODIFY_TREE */
  145. if (rtr_info.mbr_adj) {
  146. mtr_commit(&mtr);
  147. rtr_clean_rtr_info(&rtr_info, true);
  148. rtr_init_rtr_info(&rtr_info, false, &ins_cur,
  149. m_index, false);
  150. rtr_info_update_btr(&ins_cur, &rtr_info);
  151. mtr_start(&mtr);
  152. m_index->set_modified(mtr);
  153. btr_cur_search_to_nth_level(
  154. m_index, 0, dtuple,
  155. PAGE_CUR_RTREE_INSERT,
  156. BTR_MODIFY_TREE, &ins_cur, 0,
  157. __FILE__, __LINE__, &mtr);
  158. }
  159. error = btr_cur_optimistic_insert(
  160. flag, &ins_cur, &ins_offsets, &row_heap,
  161. dtuple, &rec, &big_rec, 0, NULL, &mtr);
  162. if (error == DB_FAIL) {
  163. ut_ad(!big_rec);
  164. mtr.commit();
  165. mtr.start();
  166. m_index->set_modified(mtr);
  167. rtr_clean_rtr_info(&rtr_info, true);
  168. rtr_init_rtr_info(&rtr_info, false,
  169. &ins_cur, m_index, false);
  170. rtr_info_update_btr(&ins_cur, &rtr_info);
  171. btr_cur_search_to_nth_level(
  172. m_index, 0, dtuple,
  173. PAGE_CUR_RTREE_INSERT,
  174. BTR_MODIFY_TREE,
  175. &ins_cur, 0,
  176. __FILE__, __LINE__, &mtr);
  177. error = btr_cur_pessimistic_insert(
  178. flag, &ins_cur, &ins_offsets,
  179. &row_heap, dtuple, &rec,
  180. &big_rec, 0, NULL, &mtr);
  181. }
  182. DBUG_EXECUTE_IF(
  183. "row_merge_ins_spatial_fail",
  184. error = DB_FAIL;
  185. );
  186. if (error == DB_SUCCESS) {
  187. if (rtr_info.mbr_adj) {
  188. error = rtr_ins_enlarge_mbr(
  189. &ins_cur, &mtr);
  190. }
  191. if (error == DB_SUCCESS) {
  192. page_update_max_trx_id(
  193. btr_cur_get_block(&ins_cur),
  194. btr_cur_get_page_zip(&ins_cur),
  195. trx_id, &mtr);
  196. }
  197. }
  198. mtr_commit(&mtr);
  199. rtr_clean_rtr_info(&rtr_info, true);
  200. count++;
  201. }
  202. m_dtuple_vec->clear();
  203. return(error);
  204. }
  205. private:
  206. /** Cache index rows made from a cluster index scan. Usually
  207. for rows on single cluster index page */
  208. typedef std::vector<dtuple_t*, ut_allocator<dtuple_t*> >
  209. idx_tuple_vec;
  210. /** vector used to cache index rows made from cluster index scan */
  211. idx_tuple_vec* m_dtuple_vec;
  212. /** the index being built */
  213. dict_index_t* m_index;
  214. /** memory heap for creating index tuples */
  215. mem_heap_t* m_heap;
  216. };
  217. /* Maximum pending doc memory limit in bytes for a fts tokenization thread */
  218. #define FTS_PENDING_DOC_MEMORY_LIMIT 1000000
  219. /** Insert sorted data tuples to the index.
  220. @param[in] index index to be inserted
  221. @param[in] old_table old table
  222. @param[in] fd file descriptor
  223. @param[in,out] block file buffer
  224. @param[in] row_buf row_buf the sorted data tuples,
  225. or NULL if fd, block will be used instead
  226. @param[in,out] btr_bulk btr bulk instance
  227. @param[in,out] stage performance schema accounting object, used by
  228. ALTER TABLE. If not NULL stage->begin_phase_insert() will be called initially
  229. and then stage->inc() will be called for each record that is processed.
  230. @return DB_SUCCESS or error number */
  231. static MY_ATTRIBUTE((warn_unused_result))
  232. dberr_t
  233. row_merge_insert_index_tuples(
  234. dict_index_t* index,
  235. const dict_table_t* old_table,
  236. const pfs_os_file_t& fd,
  237. row_merge_block_t* block,
  238. const row_merge_buf_t* row_buf,
  239. BtrBulk* btr_bulk,
  240. const ib_uint64_t table_total_rows, /*!< in: total rows of old table */
  241. const double pct_progress, /*!< in: total progress
  242. percent until now */
  243. const double pct_cost, /*!< in: current progress percent
  244. */
  245. row_merge_block_t* crypt_block, /*!< in: crypt buf or NULL */
  246. ulint space, /*!< in: space id */
  247. ut_stage_alter_t* stage = NULL);
  248. /******************************************************//**
  249. Encode an index record. */
  250. static MY_ATTRIBUTE((nonnull))
  251. void
  252. row_merge_buf_encode(
  253. /*=================*/
  254. byte** b, /*!< in/out: pointer to
  255. current end of output buffer */
  256. const dict_index_t* index, /*!< in: index */
  257. const mtuple_t* entry, /*!< in: index fields
  258. of the record to encode */
  259. ulint n_fields) /*!< in: number of fields
  260. in the entry */
  261. {
  262. ulint size;
  263. ulint extra_size;
  264. size = rec_get_converted_size_temp<false>(
  265. index, entry->fields, n_fields, &extra_size);
  266. ut_ad(size >= extra_size);
  267. /* Encode extra_size + 1 */
  268. if (extra_size + 1 < 0x80) {
  269. *(*b)++ = (byte) (extra_size + 1);
  270. } else {
  271. ut_ad((extra_size + 1) < 0x8000);
  272. *(*b)++ = (byte) (0x80 | ((extra_size + 1) >> 8));
  273. *(*b)++ = (byte) (extra_size + 1);
  274. }
  275. rec_convert_dtuple_to_temp<false>(*b + extra_size, index,
  276. entry->fields, n_fields);
  277. *b += size;
  278. }
  279. /******************************************************//**
  280. Allocate a sort buffer.
  281. @return own: sort buffer */
  282. static MY_ATTRIBUTE((malloc, nonnull))
  283. row_merge_buf_t*
  284. row_merge_buf_create_low(
  285. /*=====================*/
  286. mem_heap_t* heap, /*!< in: heap where allocated */
  287. dict_index_t* index, /*!< in: secondary index */
  288. ulint max_tuples, /*!< in: maximum number of
  289. data tuples */
  290. ulint buf_size) /*!< in: size of the buffer,
  291. in bytes */
  292. {
  293. row_merge_buf_t* buf;
  294. ut_ad(max_tuples > 0);
  295. ut_ad(max_tuples <= srv_sort_buf_size);
  296. buf = static_cast<row_merge_buf_t*>(mem_heap_zalloc(heap, buf_size));
  297. buf->heap = heap;
  298. buf->index = index;
  299. buf->max_tuples = max_tuples;
  300. buf->tuples = static_cast<mtuple_t*>(
  301. ut_malloc_nokey(2 * max_tuples * sizeof *buf->tuples));
  302. buf->tmp_tuples = buf->tuples + max_tuples;
  303. return(buf);
  304. }
  305. /******************************************************//**
  306. Allocate a sort buffer.
  307. @return own: sort buffer */
  308. row_merge_buf_t*
  309. row_merge_buf_create(
  310. /*=================*/
  311. dict_index_t* index) /*!< in: secondary index */
  312. {
  313. row_merge_buf_t* buf;
  314. ulint max_tuples;
  315. ulint buf_size;
  316. mem_heap_t* heap;
  317. max_tuples = srv_sort_buf_size
  318. / ut_max(static_cast<ulint>(1),
  319. dict_index_get_min_size(index));
  320. buf_size = (sizeof *buf);
  321. heap = mem_heap_create(buf_size);
  322. buf = row_merge_buf_create_low(heap, index, max_tuples, buf_size);
  323. return(buf);
  324. }
  325. /******************************************************//**
  326. Empty a sort buffer.
  327. @return sort buffer */
  328. row_merge_buf_t*
  329. row_merge_buf_empty(
  330. /*================*/
  331. row_merge_buf_t* buf) /*!< in,own: sort buffer */
  332. {
  333. ulint buf_size = sizeof *buf;
  334. ulint max_tuples = buf->max_tuples;
  335. mem_heap_t* heap = buf->heap;
  336. dict_index_t* index = buf->index;
  337. mtuple_t* tuples = buf->tuples;
  338. mem_heap_empty(heap);
  339. buf = static_cast<row_merge_buf_t*>(mem_heap_zalloc(heap, buf_size));
  340. buf->heap = heap;
  341. buf->index = index;
  342. buf->max_tuples = max_tuples;
  343. buf->tuples = tuples;
  344. buf->tmp_tuples = buf->tuples + max_tuples;
  345. return(buf);
  346. }
  347. /******************************************************//**
  348. Deallocate a sort buffer. */
  349. void
  350. row_merge_buf_free(
  351. /*===============*/
  352. row_merge_buf_t* buf) /*!< in,own: sort buffer to be freed */
  353. {
  354. ut_free(buf->tuples);
  355. mem_heap_free(buf->heap);
  356. }
  357. /** Convert the field data from compact to redundant format.
  358. @param[in] row_field field to copy from
  359. @param[out] field field to copy to
  360. @param[in] len length of the field data
  361. @param[in] zip_size compressed BLOB page size,
  362. zero for uncompressed BLOBs
  363. @param[in,out] heap memory heap where to allocate data when
  364. converting to ROW_FORMAT=REDUNDANT, or NULL
  365. when not to invoke
  366. row_merge_buf_redundant_convert(). */
  367. static
  368. void
  369. row_merge_buf_redundant_convert(
  370. const dfield_t* row_field,
  371. dfield_t* field,
  372. ulint len,
  373. const page_size_t& page_size,
  374. mem_heap_t* heap)
  375. {
  376. ut_ad(field->type.mbminlen == 1);
  377. ut_ad(field->type.mbmaxlen > 1);
  378. byte* buf = (byte*) mem_heap_alloc(heap, len);
  379. ulint field_len = row_field->len;
  380. ut_ad(field_len <= len);
  381. if (row_field->ext) {
  382. const byte* field_data = static_cast<const byte*>(
  383. dfield_get_data(row_field));
  384. ulint ext_len;
  385. ut_a(field_len >= BTR_EXTERN_FIELD_REF_SIZE);
  386. ut_a(memcmp(field_data + field_len - BTR_EXTERN_FIELD_REF_SIZE,
  387. field_ref_zero, BTR_EXTERN_FIELD_REF_SIZE));
  388. byte* data = btr_copy_externally_stored_field(
  389. &ext_len, field_data, page_size, field_len, heap);
  390. ut_ad(ext_len < len);
  391. memcpy(buf, data, ext_len);
  392. field_len = ext_len;
  393. } else {
  394. memcpy(buf, row_field->data, field_len);
  395. }
  396. memset(buf + field_len, 0x20, len - field_len);
  397. dfield_set_data(field, buf, len);
  398. }
  399. /** Insert a data tuple into a sort buffer.
  400. @param[in,out] buf sort buffer
  401. @param[in] fts_index fts index to be created
  402. @param[in] old_table original table
  403. @param[in] new_table new table
  404. @param[in,out] psort_info parallel sort info
  405. @param[in,out] row table row
  406. @param[in] ext cache of externally stored
  407. column prefixes, or NULL
  408. @param[in,out] doc_id Doc ID if we are creating
  409. FTS index
  410. @param[in,out] conv_heap memory heap where to allocate data when
  411. converting to ROW_FORMAT=REDUNDANT, or NULL
  412. when not to invoke
  413. row_merge_buf_redundant_convert()
  414. @param[in,out] err set if error occurs
  415. @param[in,out] v_heap heap memory to process data for virtual column
  416. @param[in,out] my_table mysql table object
  417. @param[in] trx transaction object
  418. @return number of rows added, 0 if out of space */
  419. static
  420. ulint
  421. row_merge_buf_add(
  422. row_merge_buf_t* buf,
  423. dict_index_t* fts_index,
  424. const dict_table_t* old_table,
  425. const dict_table_t* new_table,
  426. fts_psort_t* psort_info,
  427. dtuple_t* row,
  428. const row_ext_t* ext,
  429. doc_id_t* doc_id,
  430. mem_heap_t* conv_heap,
  431. dberr_t* err,
  432. mem_heap_t** v_heap,
  433. TABLE* my_table,
  434. trx_t* trx)
  435. {
  436. ulint i;
  437. const dict_index_t* index;
  438. mtuple_t* entry;
  439. dfield_t* field;
  440. const dict_field_t* ifield;
  441. ulint n_fields;
  442. ulint data_size;
  443. ulint extra_size;
  444. ulint bucket = 0;
  445. doc_id_t write_doc_id;
  446. ulint n_row_added = 0;
  447. VCOL_STORAGE vcol_storage;
  448. DBUG_ENTER("row_merge_buf_add");
  449. if (buf->n_tuples >= buf->max_tuples) {
  450. error:
  451. n_row_added = 0;
  452. goto end;
  453. }
  454. DBUG_EXECUTE_IF(
  455. "ib_row_merge_buf_add_two",
  456. if (buf->n_tuples >= 2) DBUG_RETURN(0););
  457. UNIV_PREFETCH_R(row->fields);
  458. /* If we are building FTS index, buf->index points to
  459. the 'fts_sort_idx', and real FTS index is stored in
  460. fts_index */
  461. index = (buf->index->type & DICT_FTS) ? fts_index : buf->index;
  462. /* create spatial index should not come here */
  463. ut_ad(!dict_index_is_spatial(index));
  464. n_fields = dict_index_get_n_fields(index);
  465. entry = &buf->tuples[buf->n_tuples];
  466. field = entry->fields = static_cast<dfield_t*>(
  467. mem_heap_alloc(buf->heap, n_fields * sizeof *entry->fields));
  468. data_size = 0;
  469. extra_size = UT_BITS_IN_BYTES(unsigned(index->n_nullable));
  470. ifield = dict_index_get_nth_field(index, 0);
  471. for (i = 0; i < n_fields; i++, field++, ifield++) {
  472. ulint len;
  473. ulint fixed_len;
  474. const dfield_t* row_field;
  475. const dict_col_t* const col = ifield->col;
  476. const dict_v_col_t* const v_col = col->is_virtual()
  477. ? reinterpret_cast<const dict_v_col_t*>(col)
  478. : NULL;
  479. /* Process the Doc ID column */
  480. if (!v_col && *doc_id
  481. && col->ind == index->table->fts->doc_col) {
  482. fts_write_doc_id((byte*) &write_doc_id, *doc_id);
  483. /* Note: field->data now points to a value on the
  484. stack: &write_doc_id after dfield_set_data(). Because
  485. there is only one doc_id per row, it shouldn't matter.
  486. We allocate a new buffer before we leave the function
  487. later below. */
  488. dfield_set_data(
  489. field, &write_doc_id, sizeof(write_doc_id));
  490. field->type.mtype = ifield->col->mtype;
  491. field->type.prtype = ifield->col->prtype;
  492. field->type.mbminlen = 0;
  493. field->type.mbmaxlen = 0;
  494. field->type.len = ifield->col->len;
  495. } else {
  496. /* Use callback to get the virtual column value */
  497. if (v_col) {
  498. dict_index_t* clust_index
  499. = dict_table_get_first_index(new_table);
  500. if (!vcol_storage.innobase_record &&
  501. !innobase_allocate_row_for_vcol(
  502. trx->mysql_thd, clust_index,
  503. v_heap, &my_table,
  504. &vcol_storage)) {
  505. *err = DB_OUT_OF_MEMORY;
  506. goto error;
  507. }
  508. row_field = innobase_get_computed_value(
  509. row, v_col, clust_index,
  510. v_heap, NULL, ifield, trx->mysql_thd,
  511. my_table, vcol_storage.innobase_record,
  512. old_table, NULL);
  513. if (row_field == NULL) {
  514. *err = DB_COMPUTE_VALUE_FAILED;
  515. goto error;
  516. }
  517. dfield_copy(field, row_field);
  518. } else {
  519. row_field = dtuple_get_nth_field(row,
  520. col->ind);
  521. dfield_copy(field, row_field);
  522. }
  523. /* Tokenize and process data for FTS */
  524. if (index->type & DICT_FTS) {
  525. fts_doc_item_t* doc_item;
  526. byte* value;
  527. void* ptr;
  528. const ulint max_trial_count = 10000;
  529. ulint trial_count = 0;
  530. /* fetch Doc ID if it already exists
  531. in the row, and not supplied by the
  532. caller. Even if the value column is
  533. NULL, we still need to get the Doc
  534. ID so to maintain the correct max
  535. Doc ID */
  536. if (*doc_id == 0) {
  537. const dfield_t* doc_field;
  538. doc_field = dtuple_get_nth_field(
  539. row,
  540. index->table->fts->doc_col);
  541. *doc_id = (doc_id_t) mach_read_from_8(
  542. static_cast<const byte*>(
  543. dfield_get_data(doc_field)));
  544. if (*doc_id == 0) {
  545. ib::warn() << "FTS Doc ID is"
  546. " zero. Record"
  547. " skipped";
  548. goto error;
  549. }
  550. }
  551. if (dfield_is_null(field)) {
  552. n_row_added = 1;
  553. continue;
  554. }
  555. ptr = ut_malloc_nokey(sizeof(*doc_item)
  556. + field->len);
  557. doc_item = static_cast<fts_doc_item_t*>(ptr);
  558. value = static_cast<byte*>(ptr)
  559. + sizeof(*doc_item);
  560. memcpy(value, field->data, field->len);
  561. field->data = value;
  562. doc_item->field = field;
  563. doc_item->doc_id = *doc_id;
  564. bucket = *doc_id % fts_sort_pll_degree;
  565. /* Add doc item to fts_doc_list */
  566. mutex_enter(&psort_info[bucket].mutex);
  567. if (psort_info[bucket].error == DB_SUCCESS) {
  568. UT_LIST_ADD_LAST(
  569. psort_info[bucket].fts_doc_list,
  570. doc_item);
  571. psort_info[bucket].memory_used +=
  572. sizeof(*doc_item) + field->len;
  573. } else {
  574. ut_free(doc_item);
  575. }
  576. mutex_exit(&psort_info[bucket].mutex);
  577. /* Sleep when memory used exceeds limit*/
  578. while (psort_info[bucket].memory_used
  579. > FTS_PENDING_DOC_MEMORY_LIMIT
  580. && trial_count++ < max_trial_count) {
  581. os_thread_sleep(1000);
  582. }
  583. n_row_added = 1;
  584. continue;
  585. }
  586. /* innobase_get_computed_value() sets the
  587. length of the virtual column field. */
  588. if (v_col == NULL
  589. && field->len != UNIV_SQL_NULL
  590. && col->mtype == DATA_MYSQL
  591. && col->len != field->len) {
  592. if (conv_heap != NULL) {
  593. row_merge_buf_redundant_convert(
  594. row_field, field, col->len,
  595. dict_table_page_size(old_table),
  596. conv_heap);
  597. } else {
  598. /* Field length mismatch should not
  599. happen when rebuilding redundant row
  600. format table. */
  601. ut_ad(dict_table_is_comp(index->table));
  602. }
  603. }
  604. }
  605. len = dfield_get_len(field);
  606. if (dfield_is_null(field)) {
  607. ut_ad(!(col->prtype & DATA_NOT_NULL));
  608. continue;
  609. } else if (!ext) {
  610. } else if (dict_index_is_clust(index)) {
  611. /* Flag externally stored fields. */
  612. const byte* buf = row_ext_lookup(ext, col->ind,
  613. &len);
  614. if (UNIV_LIKELY_NULL(buf)) {
  615. ut_a(buf != field_ref_zero);
  616. if (i < dict_index_get_n_unique(index)) {
  617. dfield_set_data(field, buf, len);
  618. } else {
  619. dfield_set_ext(field);
  620. len = dfield_get_len(field);
  621. }
  622. }
  623. } else if (!v_col) {
  624. /* Only non-virtual column are stored externally */
  625. const byte* buf = row_ext_lookup(ext, col->ind,
  626. &len);
  627. if (UNIV_LIKELY_NULL(buf)) {
  628. ut_a(buf != field_ref_zero);
  629. dfield_set_data(field, buf, len);
  630. }
  631. }
  632. /* If a column prefix index, take only the prefix */
  633. if (ifield->prefix_len) {
  634. len = dtype_get_at_most_n_mbchars(
  635. col->prtype,
  636. col->mbminlen, col->mbmaxlen,
  637. ifield->prefix_len,
  638. len,
  639. static_cast<char*>(dfield_get_data(field)));
  640. dfield_set_len(field, len);
  641. }
  642. ut_ad(len <= col->len
  643. || DATA_LARGE_MTYPE(col->mtype));
  644. fixed_len = ifield->fixed_len;
  645. if (fixed_len && !dict_table_is_comp(index->table)
  646. && col->mbminlen != col->mbmaxlen) {
  647. /* CHAR in ROW_FORMAT=REDUNDANT is always
  648. fixed-length, but in the temporary file it is
  649. variable-length for variable-length character
  650. sets. */
  651. fixed_len = 0;
  652. }
  653. if (fixed_len) {
  654. #ifdef UNIV_DEBUG
  655. /* len should be between size calcualted base on
  656. mbmaxlen and mbminlen */
  657. ut_ad(len <= fixed_len);
  658. ut_ad(!col->mbmaxlen || len >= col->mbminlen
  659. * (fixed_len / col->mbmaxlen));
  660. ut_ad(!dfield_is_ext(field));
  661. #endif /* UNIV_DEBUG */
  662. } else if (dfield_is_ext(field)) {
  663. extra_size += 2;
  664. } else if (len < 128
  665. || (!DATA_BIG_COL(col))) {
  666. extra_size++;
  667. } else {
  668. /* For variable-length columns, we look up the
  669. maximum length from the column itself. If this
  670. is a prefix index column shorter than 256 bytes,
  671. this will waste one byte. */
  672. extra_size += 2;
  673. }
  674. data_size += len;
  675. }
  676. /* If this is FTS index, we already populated the sort buffer, return
  677. here */
  678. if (index->type & DICT_FTS) {
  679. goto end;
  680. }
  681. #ifdef UNIV_DEBUG
  682. {
  683. ulint size;
  684. ulint extra;
  685. size = rec_get_converted_size_temp<false>(
  686. index, entry->fields, n_fields, &extra);
  687. ut_ad(data_size + extra_size == size);
  688. ut_ad(extra_size == extra);
  689. }
  690. #endif /* UNIV_DEBUG */
  691. /* Add to the total size of the record in row_merge_block_t
  692. the encoded length of extra_size and the extra bytes (extra_size).
  693. See row_merge_buf_write() for the variable-length encoding
  694. of extra_size. */
  695. data_size += (extra_size + 1) + ((extra_size + 1) >= 0x80);
  696. /* Record size can exceed page size while converting to
  697. redundant row format. But there is assert
  698. ut_ad(size < srv_page_size) in rec_offs_data_size().
  699. It may hit the assert before attempting to insert the row. */
  700. if (conv_heap != NULL && data_size > srv_page_size) {
  701. *err = DB_TOO_BIG_RECORD;
  702. }
  703. ut_ad(data_size < srv_sort_buf_size);
  704. /* Reserve bytes for the end marker of row_merge_block_t. */
  705. if (buf->total_size + data_size >= srv_sort_buf_size) {
  706. goto error;
  707. }
  708. buf->total_size += data_size;
  709. buf->n_tuples++;
  710. n_row_added++;
  711. field = entry->fields;
  712. /* Copy the data fields. */
  713. do {
  714. dfield_dup(field++, buf->heap);
  715. } while (--n_fields);
  716. if (conv_heap != NULL) {
  717. mem_heap_empty(conv_heap);
  718. }
  719. end:
  720. if (vcol_storage.innobase_record)
  721. innobase_free_row_for_vcol(&vcol_storage);
  722. DBUG_RETURN(n_row_added);
  723. }
  724. /*************************************************************//**
  725. Report a duplicate key. */
  726. void
  727. row_merge_dup_report(
  728. /*=================*/
  729. row_merge_dup_t* dup, /*!< in/out: for reporting duplicates */
  730. const dfield_t* entry) /*!< in: duplicate index entry */
  731. {
  732. if (!dup->n_dup++) {
  733. /* Only report the first duplicate record,
  734. but count all duplicate records. */
  735. innobase_fields_to_mysql(dup->table, dup->index, entry);
  736. }
  737. }
  738. /*************************************************************//**
  739. Compare two tuples.
  740. @return positive, 0, negative if a is greater, equal, less, than b,
  741. respectively */
  742. static MY_ATTRIBUTE((warn_unused_result))
  743. int
  744. row_merge_tuple_cmp(
  745. /*================*/
  746. ulint n_uniq, /*!< in: number of unique fields */
  747. ulint n_field,/*!< in: number of fields */
  748. const mtuple_t& a, /*!< in: first tuple to be compared */
  749. const mtuple_t& b, /*!< in: second tuple to be compared */
  750. row_merge_dup_t* dup) /*!< in/out: for reporting duplicates,
  751. NULL if non-unique index */
  752. {
  753. int cmp;
  754. const dfield_t* af = a.fields;
  755. const dfield_t* bf = b.fields;
  756. ulint n = n_uniq;
  757. ut_ad(n_uniq > 0);
  758. ut_ad(n_uniq <= n_field);
  759. /* Compare the fields of the tuples until a difference is
  760. found or we run out of fields to compare. If !cmp at the
  761. end, the tuples are equal. */
  762. do {
  763. cmp = cmp_dfield_dfield(af++, bf++);
  764. } while (!cmp && --n);
  765. if (cmp) {
  766. return(cmp);
  767. }
  768. if (dup) {
  769. /* Report a duplicate value error if the tuples are
  770. logically equal. NULL columns are logically inequal,
  771. although they are equal in the sorting order. Find
  772. out if any of the fields are NULL. */
  773. for (const dfield_t* df = a.fields; df != af; df++) {
  774. if (dfield_is_null(df)) {
  775. goto no_report;
  776. }
  777. }
  778. row_merge_dup_report(dup, a.fields);
  779. }
  780. no_report:
  781. /* The n_uniq fields were equal, but we compare all fields so
  782. that we will get the same (internal) order as in the B-tree. */
  783. for (n = n_field - n_uniq + 1; --n; ) {
  784. cmp = cmp_dfield_dfield(af++, bf++);
  785. if (cmp) {
  786. return(cmp);
  787. }
  788. }
  789. /* This should never be reached, except in a secondary index
  790. when creating a secondary index and a PRIMARY KEY, and there
  791. is a duplicate in the PRIMARY KEY that has not been detected
  792. yet. Internally, an index must never contain duplicates. */
  793. return(cmp);
  794. }
  795. /** Wrapper for row_merge_tuple_sort() to inject some more context to
  796. UT_SORT_FUNCTION_BODY().
  797. @param tuples array of tuples that being sorted
  798. @param aux work area, same size as tuples[]
  799. @param low lower bound of the sorting area, inclusive
  800. @param high upper bound of the sorting area, inclusive */
  801. #define row_merge_tuple_sort_ctx(tuples, aux, low, high) \
  802. row_merge_tuple_sort(n_uniq, n_field, dup, tuples, aux, low, high)
  803. /** Wrapper for row_merge_tuple_cmp() to inject some more context to
  804. UT_SORT_FUNCTION_BODY().
  805. @param a first tuple to be compared
  806. @param b second tuple to be compared
  807. @return positive, 0, negative, if a is greater, equal, less, than b,
  808. respectively */
  809. #define row_merge_tuple_cmp_ctx(a,b) \
  810. row_merge_tuple_cmp(n_uniq, n_field, a, b, dup)
  811. /**********************************************************************//**
  812. Merge sort the tuple buffer in main memory. */
  813. static
  814. void
  815. row_merge_tuple_sort(
  816. /*=================*/
  817. ulint n_uniq, /*!< in: number of unique fields */
  818. ulint n_field,/*!< in: number of fields */
  819. row_merge_dup_t* dup, /*!< in/out: reporter of duplicates
  820. (NULL if non-unique index) */
  821. mtuple_t* tuples, /*!< in/out: tuples */
  822. mtuple_t* aux, /*!< in/out: work area */
  823. ulint low, /*!< in: lower bound of the
  824. sorting area, inclusive */
  825. ulint high) /*!< in: upper bound of the
  826. sorting area, exclusive */
  827. {
  828. ut_ad(n_field > 0);
  829. ut_ad(n_uniq <= n_field);
  830. UT_SORT_FUNCTION_BODY(row_merge_tuple_sort_ctx,
  831. tuples, aux, low, high, row_merge_tuple_cmp_ctx);
  832. }
  833. /******************************************************//**
  834. Sort a buffer. */
  835. void
  836. row_merge_buf_sort(
  837. /*===============*/
  838. row_merge_buf_t* buf, /*!< in/out: sort buffer */
  839. row_merge_dup_t* dup) /*!< in/out: reporter of duplicates
  840. (NULL if non-unique index) */
  841. {
  842. ut_ad(!dict_index_is_spatial(buf->index));
  843. row_merge_tuple_sort(dict_index_get_n_unique(buf->index),
  844. dict_index_get_n_fields(buf->index),
  845. dup,
  846. buf->tuples, buf->tmp_tuples, 0, buf->n_tuples);
  847. }
  848. /******************************************************//**
  849. Write a buffer to a block. */
  850. void
  851. row_merge_buf_write(
  852. /*================*/
  853. const row_merge_buf_t* buf, /*!< in: sorted buffer */
  854. const merge_file_t* of UNIV_UNUSED,
  855. /*!< in: output file */
  856. row_merge_block_t* block) /*!< out: buffer for writing to file */
  857. {
  858. const dict_index_t* index = buf->index;
  859. ulint n_fields= dict_index_get_n_fields(index);
  860. byte* b = &block[0];
  861. DBUG_ENTER("row_merge_buf_write");
  862. for (ulint i = 0; i < buf->n_tuples; i++) {
  863. const mtuple_t* entry = &buf->tuples[i];
  864. row_merge_buf_encode(&b, index, entry, n_fields);
  865. ut_ad(b < &block[srv_sort_buf_size]);
  866. DBUG_LOG("ib_merge_sort",
  867. reinterpret_cast<const void*>(b) << ','
  868. << of->fd << ',' << of->offset << ' ' <<
  869. i << ": " <<
  870. rec_printer(entry->fields, n_fields).str());
  871. }
  872. /* Write an "end-of-chunk" marker. */
  873. ut_a(b < &block[srv_sort_buf_size]);
  874. ut_a(b == &block[0] + buf->total_size);
  875. *b++ = 0;
  876. #ifdef HAVE_valgrind
  877. /* The rest of the block is uninitialized. Initialize it
  878. to avoid bogus warnings. */
  879. memset(b, 0xff, &block[srv_sort_buf_size] - b);
  880. #endif /* HAVE_valgrind */
  881. DBUG_LOG("ib_merge_sort",
  882. "write " << reinterpret_cast<const void*>(b) << ','
  883. << of->fd << ',' << of->offset << " EOF");
  884. DBUG_VOID_RETURN;
  885. }
  886. /******************************************************//**
  887. Create a memory heap and allocate space for row_merge_rec_offsets()
  888. and mrec_buf_t[3].
  889. @return memory heap */
  890. static
  891. mem_heap_t*
  892. row_merge_heap_create(
  893. /*==================*/
  894. const dict_index_t* index, /*!< in: record descriptor */
  895. mrec_buf_t** buf, /*!< out: 3 buffers */
  896. rec_offs** offsets1, /*!< out: offsets */
  897. rec_offs** offsets2) /*!< out: offsets */
  898. {
  899. ulint i = 1 + REC_OFFS_HEADER_SIZE
  900. + dict_index_get_n_fields(index);
  901. mem_heap_t* heap = mem_heap_create(2 * i * sizeof **offsets1
  902. + 3 * sizeof **buf);
  903. *buf = static_cast<mrec_buf_t*>(
  904. mem_heap_alloc(heap, 3 * sizeof **buf));
  905. *offsets1 = static_cast<rec_offs*>(
  906. mem_heap_alloc(heap, i * sizeof **offsets1));
  907. *offsets2 = static_cast<rec_offs*>(
  908. mem_heap_alloc(heap, i * sizeof **offsets2));
  909. rec_offs_set_n_alloc(*offsets1, i);
  910. rec_offs_set_n_alloc(*offsets2, i);
  911. rec_offs_set_n_fields(*offsets1, dict_index_get_n_fields(index));
  912. rec_offs_set_n_fields(*offsets2, dict_index_get_n_fields(index));
  913. return(heap);
  914. }
  915. /** Read a merge block from the file system.
  916. @return whether the request was completed successfully */
  917. bool
  918. row_merge_read(
  919. /*===========*/
  920. const pfs_os_file_t& fd, /*!< in: file descriptor */
  921. ulint offset, /*!< in: offset where to read
  922. in number of row_merge_block_t
  923. elements */
  924. row_merge_block_t* buf, /*!< out: data */
  925. row_merge_block_t* crypt_buf, /*!< in: crypt buf or NULL */
  926. ulint space) /*!< in: space id */
  927. {
  928. os_offset_t ofs = ((os_offset_t) offset) * srv_sort_buf_size;
  929. DBUG_ENTER("row_merge_read");
  930. DBUG_LOG("ib_merge_sort", "fd=" << fd << " ofs=" << ofs);
  931. DBUG_EXECUTE_IF("row_merge_read_failure", DBUG_RETURN(FALSE););
  932. IORequest request(IORequest::READ);
  933. const bool success = DB_SUCCESS == os_file_read_no_error_handling(
  934. request, fd, buf, ofs, srv_sort_buf_size, 0);
  935. /* If encryption is enabled decrypt buffer */
  936. if (success && log_tmp_is_encrypted()) {
  937. if (!log_tmp_block_decrypt(buf, srv_sort_buf_size,
  938. crypt_buf, ofs)) {
  939. return (FALSE);
  940. }
  941. srv_stats.n_merge_blocks_decrypted.inc();
  942. memcpy(buf, crypt_buf, srv_sort_buf_size);
  943. }
  944. #ifdef POSIX_FADV_DONTNEED
  945. /* Each block is read exactly once. Free up the file cache. */
  946. posix_fadvise(fd, ofs, srv_sort_buf_size, POSIX_FADV_DONTNEED);
  947. #endif /* POSIX_FADV_DONTNEED */
  948. if (!success) {
  949. ib::error() << "Failed to read merge block at " << ofs;
  950. }
  951. DBUG_RETURN(success);
  952. }
  953. /********************************************************************//**
  954. Write a merge block to the file system.
  955. @return whether the request was completed successfully
  956. @retval false on error
  957. @retval true on success */
  958. UNIV_INTERN
  959. bool
  960. row_merge_write(
  961. /*============*/
  962. const pfs_os_file_t& fd, /*!< in: file descriptor */
  963. ulint offset, /*!< in: offset where to write,
  964. in number of row_merge_block_t elements */
  965. const void* buf, /*!< in: data */
  966. void* crypt_buf, /*!< in: crypt buf or NULL */
  967. ulint space) /*!< in: space id */
  968. {
  969. size_t buf_len = srv_sort_buf_size;
  970. os_offset_t ofs = buf_len * (os_offset_t) offset;
  971. void* out_buf = (void *)buf;
  972. DBUG_ENTER("row_merge_write");
  973. DBUG_LOG("ib_merge_sort", "fd=" << fd << " ofs=" << ofs);
  974. DBUG_EXECUTE_IF("row_merge_write_failure", DBUG_RETURN(FALSE););
  975. /* For encrypted tables, encrypt data before writing */
  976. if (log_tmp_is_encrypted()) {
  977. if (!log_tmp_block_encrypt(static_cast<const byte*>(buf),
  978. buf_len,
  979. static_cast<byte*>(crypt_buf),
  980. ofs)) {
  981. return false;
  982. }
  983. srv_stats.n_merge_blocks_encrypted.inc();
  984. out_buf = crypt_buf;
  985. }
  986. IORequest request(IORequest::WRITE);
  987. const bool success = DB_SUCCESS == os_file_write(
  988. request, "(merge)", fd, out_buf, ofs, buf_len);
  989. #ifdef POSIX_FADV_DONTNEED
  990. /* The block will be needed on the next merge pass,
  991. but it can be evicted from the file cache meanwhile. */
  992. posix_fadvise(fd, ofs, buf_len, POSIX_FADV_DONTNEED);
  993. #endif /* POSIX_FADV_DONTNEED */
  994. DBUG_RETURN(success);
  995. }
  996. /********************************************************************//**
  997. Read a merge record.
  998. @return pointer to next record, or NULL on I/O error or end of list */
  999. const byte*
  1000. row_merge_read_rec(
  1001. /*===============*/
  1002. row_merge_block_t* block, /*!< in/out: file buffer */
  1003. mrec_buf_t* buf, /*!< in/out: secondary buffer */
  1004. const byte* b, /*!< in: pointer to record */
  1005. const dict_index_t* index, /*!< in: index of the record */
  1006. const pfs_os_file_t& fd, /*!< in: file descriptor */
  1007. ulint* foffs, /*!< in/out: file offset */
  1008. const mrec_t** mrec, /*!< out: pointer to merge record,
  1009. or NULL on end of list
  1010. (non-NULL on I/O error) */
  1011. rec_offs* offsets,/*!< out: offsets of mrec */
  1012. row_merge_block_t* crypt_block, /*!< in: crypt buf or NULL */
  1013. ulint space) /*!< in: space id */
  1014. {
  1015. ulint extra_size;
  1016. ulint data_size;
  1017. ulint avail_size;
  1018. ut_ad(b >= &block[0]);
  1019. ut_ad(b < &block[srv_sort_buf_size]);
  1020. ut_ad(rec_offs_get_n_alloc(offsets) == 1 + REC_OFFS_HEADER_SIZE
  1021. + dict_index_get_n_fields(index));
  1022. DBUG_ENTER("row_merge_read_rec");
  1023. extra_size = *b++;
  1024. if (UNIV_UNLIKELY(!extra_size)) {
  1025. /* End of list */
  1026. *mrec = NULL;
  1027. DBUG_LOG("ib_merge_sort",
  1028. "read " << reinterpret_cast<const void*>(b) << ',' <<
  1029. reinterpret_cast<const void*>(block) << ',' <<
  1030. fd << ',' << *foffs << " EOF");
  1031. DBUG_RETURN(NULL);
  1032. }
  1033. if (extra_size >= 0x80) {
  1034. /* Read another byte of extra_size. */
  1035. if (UNIV_UNLIKELY(b >= &block[srv_sort_buf_size])) {
  1036. if (!row_merge_read(fd, ++(*foffs), block,
  1037. crypt_block,
  1038. space)) {
  1039. err_exit:
  1040. /* Signal I/O error. */
  1041. *mrec = b;
  1042. DBUG_RETURN(NULL);
  1043. }
  1044. /* Wrap around to the beginning of the buffer. */
  1045. b = &block[0];
  1046. }
  1047. extra_size = (extra_size & 0x7f) << 8;
  1048. extra_size |= *b++;
  1049. }
  1050. /* Normalize extra_size. Above, value 0 signals "end of list". */
  1051. extra_size--;
  1052. /* Read the extra bytes. */
  1053. if (UNIV_UNLIKELY(b + extra_size >= &block[srv_sort_buf_size])) {
  1054. /* The record spans two blocks. Copy the entire record
  1055. to the auxiliary buffer and handle this as a special
  1056. case. */
  1057. avail_size = ulint(&block[srv_sort_buf_size] - b);
  1058. ut_ad(avail_size < sizeof *buf);
  1059. memcpy(*buf, b, avail_size);
  1060. if (!row_merge_read(fd, ++(*foffs), block,
  1061. crypt_block,
  1062. space)) {
  1063. goto err_exit;
  1064. }
  1065. /* Wrap around to the beginning of the buffer. */
  1066. b = &block[0];
  1067. /* Copy the record. */
  1068. memcpy(*buf + avail_size, b, extra_size - avail_size);
  1069. b += extra_size - avail_size;
  1070. *mrec = *buf + extra_size;
  1071. rec_init_offsets_temp(*mrec, index, offsets);
  1072. data_size = rec_offs_data_size(offsets);
  1073. /* These overflows should be impossible given that
  1074. records are much smaller than either buffer, and
  1075. the record starts near the beginning of each buffer. */
  1076. ut_a(extra_size + data_size < sizeof *buf);
  1077. ut_a(b + data_size < &block[srv_sort_buf_size]);
  1078. /* Copy the data bytes. */
  1079. memcpy(*buf + extra_size, b, data_size);
  1080. b += data_size;
  1081. goto func_exit;
  1082. }
  1083. *mrec = b + extra_size;
  1084. rec_init_offsets_temp(*mrec, index, offsets);
  1085. data_size = rec_offs_data_size(offsets);
  1086. ut_ad(extra_size + data_size < sizeof *buf);
  1087. b += extra_size + data_size;
  1088. if (UNIV_LIKELY(b < &block[srv_sort_buf_size])) {
  1089. /* The record fits entirely in the block.
  1090. This is the normal case. */
  1091. goto func_exit;
  1092. }
  1093. /* The record spans two blocks. Copy it to buf. */
  1094. b -= extra_size + data_size;
  1095. avail_size = ulint(&block[srv_sort_buf_size] - b);
  1096. memcpy(*buf, b, avail_size);
  1097. *mrec = *buf + extra_size;
  1098. rec_init_offsets_temp(*mrec, index, offsets);
  1099. if (!row_merge_read(fd, ++(*foffs), block,
  1100. crypt_block,
  1101. space)) {
  1102. goto err_exit;
  1103. }
  1104. /* Wrap around to the beginning of the buffer. */
  1105. b = &block[0];
  1106. /* Copy the rest of the record. */
  1107. memcpy(*buf + avail_size, b, extra_size + data_size - avail_size);
  1108. b += extra_size + data_size - avail_size;
  1109. func_exit:
  1110. DBUG_LOG("ib_merge_sort",
  1111. reinterpret_cast<const void*>(b) << ',' <<
  1112. reinterpret_cast<const void*>(block)
  1113. << ",fd=" << fd << ',' << *foffs << ": "
  1114. << rec_printer(*mrec, 0, offsets).str());
  1115. DBUG_RETURN(b);
  1116. }
  1117. /********************************************************************//**
  1118. Write a merge record. */
  1119. static
  1120. void
  1121. row_merge_write_rec_low(
  1122. /*====================*/
  1123. byte* b, /*!< out: buffer */
  1124. ulint e, /*!< in: encoded extra_size */
  1125. #ifndef DBUG_OFF
  1126. ulint size, /*!< in: total size to write */
  1127. const pfs_os_file_t& fd, /*!< in: file descriptor */
  1128. ulint foffs, /*!< in: file offset */
  1129. #endif /* !DBUG_OFF */
  1130. const mrec_t* mrec, /*!< in: record to write */
  1131. const rec_offs* offsets)/*!< in: offsets of mrec */
  1132. #ifdef DBUG_OFF
  1133. # define row_merge_write_rec_low(b, e, size, fd, foffs, mrec, offsets) \
  1134. row_merge_write_rec_low(b, e, mrec, offsets)
  1135. #endif /* DBUG_OFF */
  1136. {
  1137. DBUG_ENTER("row_merge_write_rec_low");
  1138. #ifndef DBUG_OFF
  1139. const byte* const end = b + size;
  1140. #endif /* DBUG_OFF */
  1141. DBUG_ASSERT(e == rec_offs_extra_size(offsets) + 1);
  1142. DBUG_LOG("ib_merge_sort",
  1143. reinterpret_cast<const void*>(b) << ",fd=" << fd << ','
  1144. << foffs << ": " << rec_printer(mrec, 0, offsets).str());
  1145. if (e < 0x80) {
  1146. *b++ = (byte) e;
  1147. } else {
  1148. *b++ = (byte) (0x80 | (e >> 8));
  1149. *b++ = (byte) e;
  1150. }
  1151. memcpy(b, mrec - rec_offs_extra_size(offsets), rec_offs_size(offsets));
  1152. DBUG_SLOW_ASSERT(b + rec_offs_size(offsets) == end);
  1153. DBUG_VOID_RETURN;
  1154. }
  1155. /********************************************************************//**
  1156. Write a merge record.
  1157. @return pointer to end of block, or NULL on error */
  1158. static
  1159. byte*
  1160. row_merge_write_rec(
  1161. /*================*/
  1162. row_merge_block_t* block, /*!< in/out: file buffer */
  1163. mrec_buf_t* buf, /*!< in/out: secondary buffer */
  1164. byte* b, /*!< in: pointer to end of block */
  1165. const pfs_os_file_t& fd, /*!< in: file descriptor */
  1166. ulint* foffs, /*!< in/out: file offset */
  1167. const mrec_t* mrec, /*!< in: record to write */
  1168. const rec_offs* offsets,/*!< in: offsets of mrec */
  1169. row_merge_block_t* crypt_block, /*!< in: crypt buf or NULL */
  1170. ulint space) /*!< in: space id */
  1171. {
  1172. ulint extra_size;
  1173. ulint size;
  1174. ulint avail_size;
  1175. ut_ad(block);
  1176. ut_ad(buf);
  1177. ut_ad(b >= &block[0]);
  1178. ut_ad(b < &block[srv_sort_buf_size]);
  1179. ut_ad(mrec);
  1180. ut_ad(foffs);
  1181. ut_ad(mrec < &block[0] || mrec > &block[srv_sort_buf_size]);
  1182. ut_ad(mrec < buf[0] || mrec > buf[1]);
  1183. /* Normalize extra_size. Value 0 signals "end of list". */
  1184. extra_size = rec_offs_extra_size(offsets) + 1;
  1185. size = extra_size + (extra_size >= 0x80)
  1186. + rec_offs_data_size(offsets);
  1187. if (UNIV_UNLIKELY(b + size >= &block[srv_sort_buf_size])) {
  1188. /* The record spans two blocks.
  1189. Copy it to the temporary buffer first. */
  1190. avail_size = ulint(&block[srv_sort_buf_size] - b);
  1191. row_merge_write_rec_low(buf[0],
  1192. extra_size, size, fd, *foffs,
  1193. mrec, offsets);
  1194. /* Copy the head of the temporary buffer, write
  1195. the completed block, and copy the tail of the
  1196. record to the head of the new block. */
  1197. memcpy(b, buf[0], avail_size);
  1198. if (!row_merge_write(fd, (*foffs)++, block,
  1199. crypt_block,
  1200. space)) {
  1201. return(NULL);
  1202. }
  1203. MEM_UNDEFINED(&block[0], srv_sort_buf_size);
  1204. /* Copy the rest. */
  1205. b = &block[0];
  1206. memcpy(b, buf[0] + avail_size, size - avail_size);
  1207. b += size - avail_size;
  1208. } else {
  1209. row_merge_write_rec_low(b, extra_size, size, fd, *foffs,
  1210. mrec, offsets);
  1211. b += size;
  1212. }
  1213. return(b);
  1214. }
  1215. /********************************************************************//**
  1216. Write an end-of-list marker.
  1217. @return pointer to end of block, or NULL on error */
  1218. static
  1219. byte*
  1220. row_merge_write_eof(
  1221. /*================*/
  1222. row_merge_block_t* block, /*!< in/out: file buffer */
  1223. byte* b, /*!< in: pointer to end of block */
  1224. const pfs_os_file_t& fd, /*!< in: file descriptor */
  1225. ulint* foffs, /*!< in/out: file offset */
  1226. row_merge_block_t* crypt_block, /*!< in: crypt buf or NULL */
  1227. ulint space) /*!< in: space id */
  1228. {
  1229. ut_ad(block);
  1230. ut_ad(b >= &block[0]);
  1231. ut_ad(b < &block[srv_sort_buf_size]);
  1232. ut_ad(foffs);
  1233. DBUG_ENTER("row_merge_write_eof");
  1234. DBUG_LOG("ib_merge_sort",
  1235. reinterpret_cast<const void*>(b) << ',' <<
  1236. reinterpret_cast<const void*>(block) <<
  1237. ",fd=" << fd << ',' << *foffs);
  1238. *b++ = 0;
  1239. MEM_CHECK_DEFINED(&block[0], b - &block[0]);
  1240. MEM_CHECK_ADDRESSABLE(&block[0], srv_sort_buf_size);
  1241. /* The rest of the block is uninitialized. Silence warnings. */
  1242. MEM_MAKE_DEFINED(b, &block[srv_sort_buf_size] - b);
  1243. if (!row_merge_write(fd, (*foffs)++, block, crypt_block, space)) {
  1244. DBUG_RETURN(NULL);
  1245. }
  1246. MEM_UNDEFINED(&block[0], srv_sort_buf_size);
  1247. DBUG_RETURN(&block[0]);
  1248. }
  1249. /** Create a temporary file if it has not been created already.
  1250. @param[in,out] tmpfd temporary file handle
  1251. @param[in] path location for creating temporary file
  1252. @return true on success, false on error */
  1253. static MY_ATTRIBUTE((warn_unused_result))
  1254. bool
  1255. row_merge_tmpfile_if_needed(
  1256. pfs_os_file_t* tmpfd,
  1257. const char* path)
  1258. {
  1259. if (*tmpfd == OS_FILE_CLOSED) {
  1260. *tmpfd = row_merge_file_create_low(path);
  1261. if (*tmpfd != OS_FILE_CLOSED) {
  1262. MONITOR_ATOMIC_INC(MONITOR_ALTER_TABLE_SORT_FILES);
  1263. }
  1264. }
  1265. return(*tmpfd != OS_FILE_CLOSED);
  1266. }
  1267. /** Create a temporary file for merge sort if it was not created already.
  1268. @param[in,out] file merge file structure
  1269. @param[in] nrec number of records in the file
  1270. @param[in] path location for creating temporary file
  1271. @return true on success, false on error */
  1272. static MY_ATTRIBUTE((warn_unused_result))
  1273. bool
  1274. row_merge_file_create_if_needed(
  1275. merge_file_t* file,
  1276. pfs_os_file_t* tmpfd,
  1277. ulint nrec,
  1278. const char* path)
  1279. {
  1280. ut_ad(file->fd == OS_FILE_CLOSED || *tmpfd != OS_FILE_CLOSED);
  1281. if (file->fd == OS_FILE_CLOSED && row_merge_file_create(file, path)!= OS_FILE_CLOSED) {
  1282. MONITOR_ATOMIC_INC(MONITOR_ALTER_TABLE_SORT_FILES);
  1283. if (!row_merge_tmpfile_if_needed(tmpfd, path) ) {
  1284. return(false);
  1285. }
  1286. file->n_rec = nrec;
  1287. }
  1288. ut_ad(file->fd == OS_FILE_CLOSED || *tmpfd != OS_FILE_CLOSED);
  1289. return(file->fd != OS_FILE_CLOSED);
  1290. }
  1291. /** Copy the merge data tuple from another merge data tuple.
  1292. @param[in] mtuple source merge data tuple
  1293. @param[in,out] prev_mtuple destination merge data tuple
  1294. @param[in] n_unique number of unique fields exist in the mtuple
  1295. @param[in,out] heap memory heap where last_mtuple allocated */
  1296. static
  1297. void
  1298. row_mtuple_create(
  1299. const mtuple_t* mtuple,
  1300. mtuple_t* prev_mtuple,
  1301. ulint n_unique,
  1302. mem_heap_t* heap)
  1303. {
  1304. memcpy(prev_mtuple->fields, mtuple->fields,
  1305. n_unique * sizeof *mtuple->fields);
  1306. dfield_t* field = prev_mtuple->fields;
  1307. for (ulint i = 0; i < n_unique; i++) {
  1308. dfield_dup(field++, heap);
  1309. }
  1310. }
  1311. /** Compare two merge data tuples.
  1312. @param[in] prev_mtuple merge data tuple
  1313. @param[in] current_mtuple merge data tuple
  1314. @param[in,out] dup reporter of duplicates
  1315. @retval positive, 0, negative if current_mtuple is greater, equal, less, than
  1316. last_mtuple. */
  1317. static
  1318. int
  1319. row_mtuple_cmp(
  1320. const mtuple_t* prev_mtuple,
  1321. const mtuple_t* current_mtuple,
  1322. row_merge_dup_t* dup)
  1323. {
  1324. ut_ad(dict_index_is_clust(dup->index));
  1325. const ulint n_unique = dict_index_get_n_unique(dup->index);
  1326. return(row_merge_tuple_cmp(
  1327. n_unique, n_unique, *current_mtuple, *prev_mtuple, dup));
  1328. }
  1329. /** Insert cached spatial index rows.
  1330. @param[in] trx_id transaction id
  1331. @param[in] sp_tuples cached spatial rows
  1332. @param[in] num_spatial number of spatial indexes
  1333. @param[in,out] row_heap heap for insert
  1334. @param[in,out] sp_heap heap for tuples
  1335. @param[in,out] pcur cluster index cursor
  1336. @param[in,out] mtr mini transaction
  1337. @return DB_SUCCESS or error number */
  1338. static
  1339. dberr_t
  1340. row_merge_spatial_rows(
  1341. trx_id_t trx_id,
  1342. index_tuple_info_t** sp_tuples,
  1343. ulint num_spatial,
  1344. mem_heap_t* row_heap,
  1345. mem_heap_t* sp_heap,
  1346. btr_pcur_t* pcur,
  1347. mtr_t* mtr)
  1348. {
  1349. dberr_t err = DB_SUCCESS;
  1350. if (sp_tuples == NULL) {
  1351. return(DB_SUCCESS);
  1352. }
  1353. ut_ad(sp_heap != NULL);
  1354. for (ulint j = 0; j < num_spatial; j++) {
  1355. err = sp_tuples[j]->insert(trx_id, row_heap, pcur, mtr);
  1356. if (err != DB_SUCCESS) {
  1357. return(err);
  1358. }
  1359. }
  1360. mem_heap_empty(sp_heap);
  1361. return(err);
  1362. }
  1363. /** Check if the geometry field is valid.
  1364. @param[in] row the row
  1365. @param[in] index spatial index
  1366. @return true if it's valid, false if it's invalid. */
  1367. static
  1368. bool
  1369. row_geo_field_is_valid(
  1370. const dtuple_t* row,
  1371. dict_index_t* index)
  1372. {
  1373. const dict_field_t* ind_field
  1374. = dict_index_get_nth_field(index, 0);
  1375. const dict_col_t* col
  1376. = ind_field->col;
  1377. ulint col_no
  1378. = dict_col_get_no(col);
  1379. const dfield_t* dfield
  1380. = dtuple_get_nth_field(row, col_no);
  1381. if (dfield_is_null(dfield)
  1382. || dfield_get_len(dfield) < GEO_DATA_HEADER_SIZE) {
  1383. return(false);
  1384. }
  1385. return(true);
  1386. }
  1387. /** Reads clustered index of the table and create temporary files
  1388. containing the index entries for the indexes to be built.
  1389. @param[in] trx transaction
  1390. @param[in,out] table MySQL table object, for reporting erroneous
  1391. records
  1392. @param[in] old_table table where rows are read from
  1393. @param[in] new_table table where indexes are created; identical to
  1394. old_table unless creating a PRIMARY KEY
  1395. @param[in] online true if creating indexes online
  1396. @param[in] index indexes to be created
  1397. @param[in] fts_sort_idx full-text index to be created, or NULL
  1398. @param[in] psort_info parallel sort info for fts_sort_idx creation,
  1399. or NULL
  1400. @param[in] files temporary files
  1401. @param[in] key_numbers MySQL key numbers to create
  1402. @param[in] n_index number of indexes to create
  1403. @param[in] defaults default values of added, changed columns, or NULL
  1404. @param[in] add_v newly added virtual columns along with indexes
  1405. @param[in] col_map mapping of old column numbers to new ones, or
  1406. NULL if old_table == new_table
  1407. @param[in] add_autoinc number of added AUTO_INCREMENT columns, or
  1408. ULINT_UNDEFINED if none is added
  1409. @param[in,out] sequence autoinc sequence
  1410. @param[in,out] block file buffer
  1411. @param[in] skip_pk_sort whether the new PRIMARY KEY will follow
  1412. existing order
  1413. @param[in,out] tmpfd temporary file handle
  1414. @param[in,out] stage performance schema accounting object, used by
  1415. ALTER TABLE. stage->n_pk_recs_inc() will be called for each record read and
  1416. stage->inc() will be called for each page read.
  1417. @param[in] pct_cost percent of task weight out of total alter job
  1418. @param[in,out] crypt_block crypted file buffer
  1419. @param[in] eval_table mysql table used to evaluate virtual column
  1420. value, see innobase_get_computed_value().
  1421. @param[in] allow_not_null allow null to not-null conversion
  1422. @return DB_SUCCESS or error */
  1423. static MY_ATTRIBUTE((warn_unused_result))
  1424. dberr_t
  1425. row_merge_read_clustered_index(
  1426. trx_t* trx,
  1427. struct TABLE* table,
  1428. const dict_table_t* old_table,
  1429. dict_table_t* new_table,
  1430. bool online,
  1431. dict_index_t** index,
  1432. dict_index_t* fts_sort_idx,
  1433. fts_psort_t* psort_info,
  1434. merge_file_t* files,
  1435. const ulint* key_numbers,
  1436. ulint n_index,
  1437. const dtuple_t* defaults,
  1438. const dict_add_v_col_t* add_v,
  1439. const ulint* col_map,
  1440. ulint add_autoinc,
  1441. ib_sequence_t& sequence,
  1442. row_merge_block_t* block,
  1443. bool skip_pk_sort,
  1444. pfs_os_file_t* tmpfd,
  1445. ut_stage_alter_t* stage,
  1446. double pct_cost,
  1447. row_merge_block_t* crypt_block,
  1448. struct TABLE* eval_table,
  1449. bool allow_not_null)
  1450. {
  1451. dict_index_t* clust_index; /* Clustered index */
  1452. mem_heap_t* row_heap = NULL;/* Heap memory to create
  1453. clustered index tuples */
  1454. row_merge_buf_t** merge_buf; /* Temporary list for records*/
  1455. mem_heap_t* v_heap = NULL; /* Heap memory to process large
  1456. data for virtual column */
  1457. btr_pcur_t pcur; /* Cursor on the clustered
  1458. index */
  1459. mtr_t mtr; /* Mini transaction */
  1460. dberr_t err = DB_SUCCESS;/* Return code */
  1461. ulint n_nonnull = 0; /* number of columns
  1462. changed to NOT NULL */
  1463. ulint* nonnull = NULL; /* NOT NULL columns */
  1464. dict_index_t* fts_index = NULL;/* FTS index */
  1465. doc_id_t doc_id = 0;
  1466. doc_id_t max_doc_id = 0;
  1467. ibool add_doc_id = FALSE;
  1468. os_event_t fts_parallel_sort_event = NULL;
  1469. ibool fts_pll_sort = FALSE;
  1470. int64_t sig_count = 0;
  1471. index_tuple_info_t** sp_tuples = NULL;
  1472. mem_heap_t* sp_heap = NULL;
  1473. ulint num_spatial = 0;
  1474. BtrBulk* clust_btr_bulk = NULL;
  1475. bool clust_temp_file = false;
  1476. mem_heap_t* mtuple_heap = NULL;
  1477. mtuple_t prev_mtuple;
  1478. mem_heap_t* conv_heap = NULL;
  1479. double curr_progress = 0.0;
  1480. ib_uint64_t read_rows = 0;
  1481. ib_uint64_t table_total_rows = 0;
  1482. char new_sys_trx_start[8];
  1483. char new_sys_trx_end[8];
  1484. byte any_autoinc_data[8] = {0};
  1485. bool vers_update_trt = false;
  1486. DBUG_ENTER("row_merge_read_clustered_index");
  1487. ut_ad((old_table == new_table) == !col_map);
  1488. ut_ad(!defaults || col_map);
  1489. ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE));
  1490. ut_ad(trx->id);
  1491. table_total_rows = dict_table_get_n_rows(old_table);
  1492. if(table_total_rows == 0) {
  1493. /* We don't know total row count */
  1494. table_total_rows = 1;
  1495. }
  1496. trx->op_info = "reading clustered index";
  1497. #ifdef FTS_INTERNAL_DIAG_PRINT
  1498. DEBUG_FTS_SORT_PRINT("FTS_SORT: Start Create Index\n");
  1499. #endif
  1500. /* Create and initialize memory for record buffers */
  1501. merge_buf = static_cast<row_merge_buf_t**>(
  1502. ut_malloc_nokey(n_index * sizeof *merge_buf));
  1503. row_merge_dup_t clust_dup = {index[0], table, col_map, 0};
  1504. dfield_t* prev_fields;
  1505. const ulint n_uniq = dict_index_get_n_unique(index[0]);
  1506. ut_ad(trx->mysql_thd != NULL);
  1507. const char* path = thd_innodb_tmpdir(trx->mysql_thd);
  1508. ut_ad(!skip_pk_sort || dict_index_is_clust(index[0]));
  1509. /* There is no previous tuple yet. */
  1510. prev_mtuple.fields = NULL;
  1511. for (ulint i = 0; i < n_index; i++) {
  1512. if (index[i]->type & DICT_FTS) {
  1513. /* We are building a FT index, make sure
  1514. we have the temporary 'fts_sort_idx' */
  1515. ut_a(fts_sort_idx);
  1516. fts_index = index[i];
  1517. merge_buf[i] = row_merge_buf_create(fts_sort_idx);
  1518. add_doc_id = DICT_TF2_FLAG_IS_SET(
  1519. new_table, DICT_TF2_FTS_ADD_DOC_ID);
  1520. /* If Doc ID does not exist in the table itself,
  1521. fetch the first FTS Doc ID */
  1522. if (add_doc_id) {
  1523. fts_get_next_doc_id(
  1524. (dict_table_t*) new_table,
  1525. &doc_id);
  1526. ut_ad(doc_id > 0);
  1527. }
  1528. fts_pll_sort = TRUE;
  1529. row_fts_start_psort(psort_info);
  1530. fts_parallel_sort_event =
  1531. psort_info[0].psort_common->sort_event;
  1532. } else {
  1533. if (dict_index_is_spatial(index[i])) {
  1534. num_spatial++;
  1535. }
  1536. merge_buf[i] = row_merge_buf_create(index[i]);
  1537. }
  1538. }
  1539. if (num_spatial > 0) {
  1540. ulint count = 0;
  1541. sp_heap = mem_heap_create(512);
  1542. sp_tuples = static_cast<index_tuple_info_t**>(
  1543. ut_malloc_nokey(num_spatial
  1544. * sizeof(*sp_tuples)));
  1545. for (ulint i = 0; i < n_index; i++) {
  1546. if (dict_index_is_spatial(index[i])) {
  1547. sp_tuples[count]
  1548. = UT_NEW_NOKEY(
  1549. index_tuple_info_t(
  1550. sp_heap,
  1551. index[i]));
  1552. count++;
  1553. }
  1554. }
  1555. ut_ad(count == num_spatial);
  1556. }
  1557. mtr_start(&mtr);
  1558. /* Find the clustered index and create a persistent cursor
  1559. based on that. */
  1560. clust_index = dict_table_get_first_index(old_table);
  1561. const ulint old_trx_id_col = DATA_TRX_ID - DATA_N_SYS_COLS
  1562. + ulint(old_table->n_cols);
  1563. ut_ad(old_table->cols[old_trx_id_col].mtype == DATA_SYS);
  1564. ut_ad(old_table->cols[old_trx_id_col].prtype
  1565. == (DATA_TRX_ID | DATA_NOT_NULL));
  1566. ut_ad(old_table->cols[old_trx_id_col + 1].mtype == DATA_SYS);
  1567. ut_ad(old_table->cols[old_trx_id_col + 1].prtype
  1568. == (DATA_ROLL_PTR | DATA_NOT_NULL));
  1569. const ulint new_trx_id_col = col_map
  1570. ? col_map[old_trx_id_col] : old_trx_id_col;
  1571. btr_pcur_open_at_index_side(
  1572. true, clust_index, BTR_SEARCH_LEAF, &pcur, true, 0, &mtr);
  1573. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  1574. if (rec_is_metadata(btr_pcur_get_rec(&pcur), clust_index)) {
  1575. ut_ad(btr_pcur_is_on_user_rec(&pcur));
  1576. /* Skip the metadata pseudo-record. */
  1577. } else {
  1578. ut_ad(!clust_index->is_instant());
  1579. btr_pcur_move_to_prev_on_page(&pcur);
  1580. }
  1581. if (old_table != new_table) {
  1582. /* The table is being rebuilt. Identify the columns
  1583. that were flagged NOT NULL in the new table, so that
  1584. we can quickly check that the records in the old table
  1585. do not violate the added NOT NULL constraints. */
  1586. nonnull = static_cast<ulint*>(
  1587. ut_malloc_nokey(dict_table_get_n_cols(new_table)
  1588. * sizeof *nonnull));
  1589. for (ulint i = 0; i < dict_table_get_n_cols(old_table); i++) {
  1590. if (dict_table_get_nth_col(old_table, i)->prtype
  1591. & DATA_NOT_NULL) {
  1592. continue;
  1593. }
  1594. const ulint j = col_map[i];
  1595. if (j == ULINT_UNDEFINED) {
  1596. /* The column was dropped. */
  1597. continue;
  1598. }
  1599. if (dict_table_get_nth_col(new_table, j)->prtype
  1600. & DATA_NOT_NULL) {
  1601. nonnull[n_nonnull++] = j;
  1602. }
  1603. }
  1604. if (!n_nonnull) {
  1605. ut_free(nonnull);
  1606. nonnull = NULL;
  1607. }
  1608. }
  1609. row_heap = mem_heap_create(sizeof(mrec_buf_t));
  1610. if (dict_table_is_comp(old_table)
  1611. && !dict_table_is_comp(new_table)) {
  1612. conv_heap = mem_heap_create(sizeof(mrec_buf_t));
  1613. }
  1614. if (skip_pk_sort) {
  1615. prev_fields = static_cast<dfield_t*>(
  1616. ut_malloc_nokey(n_uniq * sizeof *prev_fields));
  1617. mtuple_heap = mem_heap_create(sizeof(mrec_buf_t));
  1618. } else {
  1619. prev_fields = NULL;
  1620. }
  1621. mach_write_to_8(new_sys_trx_start, trx->id);
  1622. mach_write_to_8(new_sys_trx_end, TRX_ID_MAX);
  1623. uint64_t n_rows = 0;
  1624. /* Scan the clustered index. */
  1625. for (;;) {
  1626. /* Do not continue if table pages are still encrypted */
  1627. if (!old_table->is_readable() || !new_table->is_readable()) {
  1628. err = DB_DECRYPTION_FAILED;
  1629. trx->error_key_num = 0;
  1630. goto func_exit;
  1631. }
  1632. const rec_t* rec;
  1633. trx_id_t rec_trx_id;
  1634. rec_offs* offsets;
  1635. dtuple_t* row;
  1636. row_ext_t* ext;
  1637. page_cur_t* cur = btr_pcur_get_page_cur(&pcur);
  1638. mem_heap_empty(row_heap);
  1639. page_cur_move_to_next(cur);
  1640. stage->n_pk_recs_inc();
  1641. if (page_cur_is_after_last(cur)) {
  1642. stage->inc();
  1643. if (UNIV_UNLIKELY(trx_is_interrupted(trx))) {
  1644. err = DB_INTERRUPTED;
  1645. trx->error_key_num = 0;
  1646. goto func_exit;
  1647. }
  1648. if (online && old_table != new_table) {
  1649. err = row_log_table_get_error(clust_index);
  1650. if (err != DB_SUCCESS) {
  1651. trx->error_key_num = 0;
  1652. goto func_exit;
  1653. }
  1654. }
  1655. /* Insert the cached spatial index rows. */
  1656. err = row_merge_spatial_rows(
  1657. trx->id, sp_tuples, num_spatial,
  1658. row_heap, sp_heap, &pcur, &mtr);
  1659. if (err != DB_SUCCESS) {
  1660. goto func_exit;
  1661. }
  1662. if (!mtr.is_active()) {
  1663. goto scan_next;
  1664. }
  1665. if (my_atomic_load32_explicit(&clust_index->lock.waiters,
  1666. MY_MEMORY_ORDER_RELAXED)) {
  1667. /* There are waiters on the clustered
  1668. index tree lock, likely the purge
  1669. thread. Store and restore the cursor
  1670. position, and yield so that scanning a
  1671. large table will not starve other
  1672. threads. */
  1673. /* Store the cursor position on the last user
  1674. record on the page. */
  1675. btr_pcur_move_to_prev_on_page(&pcur);
  1676. /* Leaf pages must never be empty, unless
  1677. this is the only page in the index tree. */
  1678. ut_ad(btr_pcur_is_on_user_rec(&pcur)
  1679. || btr_pcur_get_block(
  1680. &pcur)->page.id.page_no()
  1681. == clust_index->page);
  1682. btr_pcur_store_position(&pcur, &mtr);
  1683. mtr_commit(&mtr);
  1684. /* Give the waiters a chance to proceed. */
  1685. os_thread_yield();
  1686. scan_next:
  1687. mtr_start(&mtr);
  1688. /* Restore position on the record, or its
  1689. predecessor if the record was purged
  1690. meanwhile. */
  1691. btr_pcur_restore_position(
  1692. BTR_SEARCH_LEAF, &pcur, &mtr);
  1693. /* Move to the successor of the
  1694. original record. */
  1695. if (!btr_pcur_move_to_next_user_rec(
  1696. &pcur, &mtr)) {
  1697. end_of_index:
  1698. row = NULL;
  1699. mtr_commit(&mtr);
  1700. mem_heap_free(row_heap);
  1701. row_heap = NULL;
  1702. ut_free(nonnull);
  1703. nonnull = NULL;
  1704. goto write_buffers;
  1705. }
  1706. } else {
  1707. ulint next_page_no;
  1708. buf_block_t* block;
  1709. next_page_no = btr_page_get_next(
  1710. page_cur_get_page(cur));
  1711. if (next_page_no == FIL_NULL) {
  1712. goto end_of_index;
  1713. }
  1714. block = page_cur_get_block(cur);
  1715. block = btr_block_get(
  1716. page_id_t(block->page.id.space(),
  1717. next_page_no),
  1718. block->page.size,
  1719. BTR_SEARCH_LEAF,
  1720. clust_index, &mtr);
  1721. btr_leaf_page_release(page_cur_get_block(cur),
  1722. BTR_SEARCH_LEAF, &mtr);
  1723. page_cur_set_before_first(block, cur);
  1724. page_cur_move_to_next(cur);
  1725. ut_ad(!page_cur_is_after_last(cur));
  1726. }
  1727. }
  1728. rec = page_cur_get_rec(cur);
  1729. if (online) {
  1730. offsets = rec_get_offsets(rec, clust_index, NULL,
  1731. clust_index->n_core_fields,
  1732. ULINT_UNDEFINED, &row_heap);
  1733. rec_trx_id = row_get_rec_trx_id(rec, clust_index,
  1734. offsets);
  1735. /* Perform a REPEATABLE READ.
  1736. When rebuilding the table online,
  1737. row_log_table_apply() must not see a newer
  1738. state of the table when applying the log.
  1739. This is mainly to prevent false duplicate key
  1740. errors, because the log will identify records
  1741. by the PRIMARY KEY, and also to prevent unsafe
  1742. BLOB access.
  1743. When creating a secondary index online, this
  1744. table scan must not see records that have only
  1745. been inserted to the clustered index, but have
  1746. not been written to the online_log of
  1747. index[]. If we performed READ UNCOMMITTED, it
  1748. could happen that the ADD INDEX reaches
  1749. ONLINE_INDEX_COMPLETE state between the time
  1750. the DML thread has updated the clustered index
  1751. but has not yet accessed secondary index. */
  1752. ut_ad(trx->read_view.is_open());
  1753. ut_ad(rec_trx_id != trx->id);
  1754. if (!trx->read_view.changes_visible(
  1755. rec_trx_id, old_table->name)) {
  1756. rec_t* old_vers;
  1757. row_vers_build_for_consistent_read(
  1758. rec, &mtr, clust_index, &offsets,
  1759. &trx->read_view, &row_heap,
  1760. row_heap, &old_vers, NULL);
  1761. if (!old_vers) {
  1762. continue;
  1763. }
  1764. /* The old version must necessarily be
  1765. in the "prehistory", because the
  1766. exclusive lock in
  1767. ha_innobase::prepare_inplace_alter_table()
  1768. forced the completion of any transactions
  1769. that accessed this table. */
  1770. ut_ad(row_get_rec_trx_id(old_vers, clust_index,
  1771. offsets) < trx->id);
  1772. rec = old_vers;
  1773. rec_trx_id = 0;
  1774. }
  1775. if (rec_get_deleted_flag(
  1776. rec,
  1777. dict_table_is_comp(old_table))) {
  1778. /* In delete-marked records, DB_TRX_ID must
  1779. always refer to an existing undo log record.
  1780. Above, we did reset rec_trx_id = 0
  1781. for rec = old_vers.*/
  1782. ut_ad(rec == page_cur_get_rec(cur)
  1783. ? rec_trx_id
  1784. : !rec_trx_id);
  1785. /* This record was deleted in the latest
  1786. committed version, or it was deleted and
  1787. then reinserted-by-update before purge
  1788. kicked in. Skip it. */
  1789. continue;
  1790. }
  1791. ut_ad(!rec_offs_any_null_extern(rec, offsets));
  1792. } else if (rec_get_deleted_flag(
  1793. rec, dict_table_is_comp(old_table))) {
  1794. /* In delete-marked records, DB_TRX_ID must
  1795. always refer to an existing undo log record. */
  1796. ut_d(rec_trx_id = rec_get_trx_id(rec, clust_index));
  1797. ut_ad(rec_trx_id);
  1798. /* This must be a purgeable delete-marked record,
  1799. and the transaction that delete-marked the record
  1800. must have been committed before this
  1801. !online ALTER TABLE transaction. */
  1802. ut_ad(rec_trx_id < trx->id);
  1803. /* Skip delete-marked records.
  1804. Skipping delete-marked records will make the
  1805. created indexes unuseable for transactions
  1806. whose read views were created before the index
  1807. creation completed, but an attempt to preserve
  1808. the history would make it tricky to detect
  1809. duplicate keys. */
  1810. continue;
  1811. } else {
  1812. offsets = rec_get_offsets(rec, clust_index, NULL,
  1813. clust_index->n_core_fields,
  1814. ULINT_UNDEFINED, &row_heap);
  1815. /* This is a locking ALTER TABLE.
  1816. If we are not rebuilding the table, the
  1817. DB_TRX_ID does not matter, as it is not being
  1818. written to any secondary indexes; see
  1819. if (old_table == new_table) below.
  1820. If we are rebuilding the table, the
  1821. DB_TRX_ID,DB_ROLL_PTR should be reset, because
  1822. there will be no history available. */
  1823. ut_ad(rec_get_trx_id(rec, clust_index) < trx->id);
  1824. rec_trx_id = 0;
  1825. }
  1826. /* When !online, we are holding a lock on old_table, preventing
  1827. any inserts that could have written a record 'stub' before
  1828. writing out off-page columns. */
  1829. ut_ad(!rec_offs_any_null_extern(rec, offsets));
  1830. /* Build a row based on the clustered index. */
  1831. row = row_build_w_add_vcol(ROW_COPY_POINTERS, clust_index,
  1832. rec, offsets, new_table,
  1833. defaults, add_v, col_map, &ext,
  1834. row_heap);
  1835. ut_ad(row);
  1836. for (ulint i = 0; i < n_nonnull; i++) {
  1837. dfield_t* field = &row->fields[nonnull[i]];
  1838. ut_ad(dfield_get_type(field)->prtype & DATA_NOT_NULL);
  1839. if (dfield_is_null(field)) {
  1840. Field* null_field =
  1841. table->field[nonnull[i]];
  1842. null_field->set_warning(
  1843. Sql_condition::WARN_LEVEL_WARN,
  1844. WARN_DATA_TRUNCATED, 1,
  1845. ulong(n_rows + 1));
  1846. if (!allow_not_null) {
  1847. err = DB_INVALID_NULL;
  1848. trx->error_key_num = 0;
  1849. goto func_exit;
  1850. }
  1851. const dfield_t& default_field
  1852. = defaults->fields[nonnull[i]];
  1853. *field = default_field;
  1854. }
  1855. }
  1856. /* Get the next Doc ID */
  1857. if (add_doc_id) {
  1858. doc_id++;
  1859. } else {
  1860. doc_id = 0;
  1861. }
  1862. ut_ad(row->fields[new_trx_id_col].type.mtype == DATA_SYS);
  1863. ut_ad(row->fields[new_trx_id_col].type.prtype
  1864. == (DATA_TRX_ID | DATA_NOT_NULL));
  1865. ut_ad(row->fields[new_trx_id_col].len == DATA_TRX_ID_LEN);
  1866. ut_ad(row->fields[new_trx_id_col + 1].type.mtype == DATA_SYS);
  1867. ut_ad(row->fields[new_trx_id_col + 1].type.prtype
  1868. == (DATA_ROLL_PTR | DATA_NOT_NULL));
  1869. ut_ad(row->fields[new_trx_id_col + 1].len == DATA_ROLL_PTR_LEN);
  1870. if (old_table == new_table) {
  1871. /* Do not bother touching DB_TRX_ID,DB_ROLL_PTR
  1872. because they are not going to be written into
  1873. secondary indexes. */
  1874. } else if (rec_trx_id < trx->id) {
  1875. /* Reset the DB_TRX_ID,DB_ROLL_PTR of old rows
  1876. for which history is not going to be
  1877. available after the rebuild operation.
  1878. This essentially mimics row_purge_reset_trx_id(). */
  1879. row->fields[new_trx_id_col].data
  1880. = const_cast<byte*>(reset_trx_id);
  1881. row->fields[new_trx_id_col + 1].data
  1882. = const_cast<byte*>(reset_trx_id
  1883. + DATA_TRX_ID_LEN);
  1884. }
  1885. if (add_autoinc != ULINT_UNDEFINED) {
  1886. ut_ad(add_autoinc
  1887. < dict_table_get_n_user_cols(new_table));
  1888. bool history_row = false;
  1889. if (new_table->versioned()) {
  1890. const dfield_t* dfield = dtuple_get_nth_field(
  1891. row, new_table->vers_end);
  1892. history_row = dfield->vers_history_row();
  1893. }
  1894. dfield_t* dfield = dtuple_get_nth_field(row,
  1895. add_autoinc);
  1896. if (new_table->versioned()) {
  1897. if (history_row) {
  1898. if (dfield_get_type(dfield)->prtype & DATA_NOT_NULL) {
  1899. err = DB_UNSUPPORTED;
  1900. my_error(ER_UNSUPPORTED_EXTENSION, MYF(0),
  1901. old_table->name.m_name);
  1902. goto func_exit;
  1903. }
  1904. dfield_set_null(dfield);
  1905. } else {
  1906. // set not null
  1907. ulint len = dfield_get_type(dfield)->len;
  1908. dfield_set_data(dfield, any_autoinc_data, len);
  1909. }
  1910. }
  1911. if (dfield_is_null(dfield)) {
  1912. goto write_buffers;
  1913. }
  1914. const dtype_t* dtype = dfield_get_type(dfield);
  1915. byte* b = static_cast<byte*>(dfield_get_data(dfield));
  1916. if (sequence.eof()) {
  1917. err = DB_ERROR;
  1918. trx->error_key_num = 0;
  1919. ib_errf(trx->mysql_thd, IB_LOG_LEVEL_ERROR,
  1920. ER_AUTOINC_READ_FAILED, "[NULL]");
  1921. goto func_exit;
  1922. }
  1923. ulonglong value = sequence++;
  1924. switch (dtype_get_mtype(dtype)) {
  1925. case DATA_INT: {
  1926. ibool usign;
  1927. ulint len = dfield_get_len(dfield);
  1928. usign = dtype_get_prtype(dtype) & DATA_UNSIGNED;
  1929. mach_write_ulonglong(b, value, len, usign);
  1930. break;
  1931. }
  1932. case DATA_FLOAT:
  1933. mach_float_write(
  1934. b, static_cast<float>(value));
  1935. break;
  1936. case DATA_DOUBLE:
  1937. mach_double_write(
  1938. b, static_cast<double>(value));
  1939. break;
  1940. default:
  1941. ut_ad(0);
  1942. }
  1943. }
  1944. if (old_table->versioned()) {
  1945. if (!new_table->versioned()
  1946. && clust_index->vers_history_row(rec, offsets)) {
  1947. continue;
  1948. }
  1949. } else if (new_table->versioned()) {
  1950. dfield_t* start =
  1951. dtuple_get_nth_field(row, new_table->vers_start);
  1952. dfield_t* end =
  1953. dtuple_get_nth_field(row, new_table->vers_end);
  1954. dfield_set_data(start, new_sys_trx_start, 8);
  1955. dfield_set_data(end, new_sys_trx_end, 8);
  1956. vers_update_trt = true;
  1957. }
  1958. write_buffers:
  1959. /* Build all entries for all the indexes to be created
  1960. in a single scan of the clustered index. */
  1961. n_rows++;
  1962. ulint s_idx_cnt = 0;
  1963. bool skip_sort = skip_pk_sort
  1964. && dict_index_is_clust(merge_buf[0]->index);
  1965. for (ulint k = 0, i = 0; i < n_index; i++, skip_sort = false) {
  1966. row_merge_buf_t* buf = merge_buf[i];
  1967. ulint rows_added = 0;
  1968. if (dict_index_is_spatial(buf->index)) {
  1969. if (!row) {
  1970. continue;
  1971. }
  1972. ut_ad(sp_tuples[s_idx_cnt]->get_index()
  1973. == buf->index);
  1974. /* If the geometry field is invalid, report
  1975. error. */
  1976. if (!row_geo_field_is_valid(row, buf->index)) {
  1977. err = DB_CANT_CREATE_GEOMETRY_OBJECT;
  1978. break;
  1979. }
  1980. sp_tuples[s_idx_cnt]->add(row, ext);
  1981. s_idx_cnt++;
  1982. continue;
  1983. }
  1984. ut_ad(!row
  1985. || !dict_index_is_clust(buf->index)
  1986. || trx_id_check(row->fields[new_trx_id_col].data,
  1987. trx->id));
  1988. merge_file_t* file = &files[k++];
  1989. if (UNIV_LIKELY
  1990. (row && (rows_added = row_merge_buf_add(
  1991. buf, fts_index, old_table, new_table,
  1992. psort_info, row, ext, &doc_id,
  1993. conv_heap, &err,
  1994. &v_heap, eval_table, trx)))) {
  1995. /* Set the page flush observer for the
  1996. transaction when buffering the very first
  1997. record for a non-redo-logged operation. */
  1998. if (file->n_rec == 0 && i == 0
  1999. && innodb_log_optimize_ddl) {
  2000. trx->set_flush_observer(
  2001. new_table->space, stage);
  2002. }
  2003. /* If we are creating FTS index,
  2004. a single row can generate more
  2005. records for tokenized word */
  2006. file->n_rec += rows_added;
  2007. if (err != DB_SUCCESS) {
  2008. ut_ad(err == DB_TOO_BIG_RECORD);
  2009. break;
  2010. }
  2011. if (doc_id > max_doc_id) {
  2012. max_doc_id = doc_id;
  2013. }
  2014. if (buf->index->type & DICT_FTS) {
  2015. /* Check if error occurs in child thread */
  2016. for (ulint j = 0;
  2017. j < fts_sort_pll_degree; j++) {
  2018. if (psort_info[j].error
  2019. != DB_SUCCESS) {
  2020. err = psort_info[j].error;
  2021. trx->error_key_num = i;
  2022. break;
  2023. }
  2024. }
  2025. if (err != DB_SUCCESS) {
  2026. break;
  2027. }
  2028. }
  2029. if (skip_sort) {
  2030. ut_ad(buf->n_tuples > 0);
  2031. const mtuple_t* curr =
  2032. &buf->tuples[buf->n_tuples - 1];
  2033. ut_ad(i == 0);
  2034. ut_ad(dict_index_is_clust(merge_buf[0]->index));
  2035. /* Detect duplicates by comparing the
  2036. current record with previous record.
  2037. When temp file is not used, records
  2038. should be in sorted order. */
  2039. if (prev_mtuple.fields != NULL
  2040. && (row_mtuple_cmp(
  2041. &prev_mtuple, curr,
  2042. &clust_dup) == 0)) {
  2043. err = DB_DUPLICATE_KEY;
  2044. trx->error_key_num
  2045. = key_numbers[0];
  2046. goto func_exit;
  2047. }
  2048. prev_mtuple.fields = curr->fields;
  2049. }
  2050. continue;
  2051. }
  2052. if (err == DB_COMPUTE_VALUE_FAILED) {
  2053. trx->error_key_num = i;
  2054. goto func_exit;
  2055. }
  2056. if (buf->index->type & DICT_FTS) {
  2057. if (!row || !doc_id) {
  2058. continue;
  2059. }
  2060. }
  2061. /* The buffer must be sufficiently large
  2062. to hold at least one record. It may only
  2063. be empty when we reach the end of the
  2064. clustered index. row_merge_buf_add()
  2065. must not have been called in this loop. */
  2066. ut_ad(buf->n_tuples || row == NULL);
  2067. /* We have enough data tuples to form a block.
  2068. Sort them and write to disk if temp file is used
  2069. or insert into index if temp file is not used. */
  2070. ut_ad(old_table == new_table
  2071. ? !dict_index_is_clust(buf->index)
  2072. : (i == 0) == dict_index_is_clust(buf->index));
  2073. /* We have enough data tuples to form a block.
  2074. Sort them (if !skip_sort) and write to disk. */
  2075. if (buf->n_tuples) {
  2076. if (skip_sort) {
  2077. /* Temporary File is not used.
  2078. so insert sorted block to the index */
  2079. if (row != NULL) {
  2080. /* We have to do insert the
  2081. cached spatial index rows, since
  2082. after the mtr_commit, the cluster
  2083. index page could be updated, then
  2084. the data in cached rows become
  2085. invalid. */
  2086. err = row_merge_spatial_rows(
  2087. trx->id, sp_tuples,
  2088. num_spatial,
  2089. row_heap, sp_heap,
  2090. &pcur, &mtr);
  2091. if (err != DB_SUCCESS) {
  2092. goto func_exit;
  2093. }
  2094. /* We are not at the end of
  2095. the scan yet. We must
  2096. mtr_commit() in order to be
  2097. able to call log_free_check()
  2098. in row_merge_insert_index_tuples().
  2099. Due to mtr_commit(), the
  2100. current row will be invalid, and
  2101. we must reread it on the next
  2102. loop iteration. */
  2103. if (mtr.is_active()) {
  2104. btr_pcur_move_to_prev_on_page(
  2105. &pcur);
  2106. btr_pcur_store_position(
  2107. &pcur, &mtr);
  2108. mtr.commit();
  2109. }
  2110. }
  2111. mem_heap_empty(mtuple_heap);
  2112. prev_mtuple.fields = prev_fields;
  2113. row_mtuple_create(
  2114. &buf->tuples[buf->n_tuples - 1],
  2115. &prev_mtuple, n_uniq,
  2116. mtuple_heap);
  2117. if (clust_btr_bulk == NULL) {
  2118. clust_btr_bulk = UT_NEW_NOKEY(
  2119. BtrBulk(index[i],
  2120. trx,
  2121. trx->get_flush_observer()));
  2122. } else {
  2123. clust_btr_bulk->latch();
  2124. }
  2125. err = row_merge_insert_index_tuples(
  2126. index[i], old_table,
  2127. OS_FILE_CLOSED, NULL, buf,
  2128. clust_btr_bulk,
  2129. table_total_rows,
  2130. curr_progress,
  2131. pct_cost,
  2132. crypt_block,
  2133. new_table->space_id);
  2134. if (row == NULL) {
  2135. err = clust_btr_bulk->finish(
  2136. err);
  2137. UT_DELETE(clust_btr_bulk);
  2138. clust_btr_bulk = NULL;
  2139. } else {
  2140. /* Release latches for possible
  2141. log_free_chck in spatial index
  2142. build. */
  2143. clust_btr_bulk->release();
  2144. }
  2145. if (err != DB_SUCCESS) {
  2146. break;
  2147. }
  2148. if (row != NULL) {
  2149. /* Restore the cursor on the
  2150. previous clustered index record,
  2151. and empty the buffer. The next
  2152. iteration of the outer loop will
  2153. advance the cursor and read the
  2154. next record (the one which we
  2155. had to ignore due to the buffer
  2156. overflow). */
  2157. mtr_start(&mtr);
  2158. btr_pcur_restore_position(
  2159. BTR_SEARCH_LEAF, &pcur,
  2160. &mtr);
  2161. buf = row_merge_buf_empty(buf);
  2162. merge_buf[i] = buf;
  2163. /* Restart the outer loop on the
  2164. record. We did not insert it
  2165. into any index yet. */
  2166. ut_ad(i == 0);
  2167. break;
  2168. }
  2169. } else if (dict_index_is_unique(buf->index)) {
  2170. row_merge_dup_t dup = {
  2171. buf->index, table, col_map, 0};
  2172. row_merge_buf_sort(buf, &dup);
  2173. if (dup.n_dup) {
  2174. err = DB_DUPLICATE_KEY;
  2175. trx->error_key_num
  2176. = key_numbers[i];
  2177. break;
  2178. }
  2179. } else {
  2180. row_merge_buf_sort(buf, NULL);
  2181. }
  2182. } else if (online && new_table == old_table) {
  2183. /* Note the newest transaction that
  2184. modified this index when the scan was
  2185. completed. We prevent older readers
  2186. from accessing this index, to ensure
  2187. read consistency. */
  2188. trx_id_t max_trx_id;
  2189. ut_a(row == NULL);
  2190. rw_lock_x_lock(
  2191. dict_index_get_lock(buf->index));
  2192. ut_a(dict_index_get_online_status(buf->index)
  2193. == ONLINE_INDEX_CREATION);
  2194. max_trx_id = row_log_get_max_trx(buf->index);
  2195. if (max_trx_id > buf->index->trx_id) {
  2196. buf->index->trx_id = max_trx_id;
  2197. }
  2198. rw_lock_x_unlock(
  2199. dict_index_get_lock(buf->index));
  2200. }
  2201. /* Secondary index and clustered index which is
  2202. not in sorted order can use the temporary file.
  2203. Fulltext index should not use the temporary file. */
  2204. if (!skip_sort && !(buf->index->type & DICT_FTS)) {
  2205. /* In case we can have all rows in sort buffer,
  2206. we can insert directly into the index without
  2207. temporary file if clustered index does not uses
  2208. temporary file. */
  2209. if (row == NULL && file->fd == OS_FILE_CLOSED
  2210. && !clust_temp_file) {
  2211. DBUG_EXECUTE_IF(
  2212. "row_merge_write_failure",
  2213. err = DB_TEMP_FILE_WRITE_FAIL;
  2214. trx->error_key_num = i;
  2215. goto all_done;);
  2216. DBUG_EXECUTE_IF(
  2217. "row_merge_tmpfile_fail",
  2218. err = DB_OUT_OF_MEMORY;
  2219. trx->error_key_num = i;
  2220. goto all_done;);
  2221. BtrBulk btr_bulk(
  2222. index[i], trx,
  2223. trx->get_flush_observer());
  2224. err = row_merge_insert_index_tuples(
  2225. index[i], old_table,
  2226. OS_FILE_CLOSED, NULL, buf,
  2227. &btr_bulk,
  2228. table_total_rows,
  2229. curr_progress,
  2230. pct_cost,
  2231. crypt_block,
  2232. new_table->space_id);
  2233. err = btr_bulk.finish(err);
  2234. DBUG_EXECUTE_IF(
  2235. "row_merge_insert_big_row",
  2236. err = DB_TOO_BIG_RECORD;);
  2237. if (err != DB_SUCCESS) {
  2238. break;
  2239. }
  2240. } else {
  2241. if (!row_merge_file_create_if_needed(
  2242. file, tmpfd,
  2243. buf->n_tuples, path)) {
  2244. err = DB_OUT_OF_MEMORY;
  2245. trx->error_key_num = i;
  2246. break;
  2247. }
  2248. /* Ensure that duplicates in the
  2249. clustered index will be detected before
  2250. inserting secondary index records. */
  2251. if (dict_index_is_clust(buf->index)) {
  2252. clust_temp_file = true;
  2253. }
  2254. ut_ad(file->n_rec > 0);
  2255. row_merge_buf_write(buf, file, block);
  2256. if (!row_merge_write(
  2257. file->fd, file->offset++,
  2258. block, crypt_block,
  2259. new_table->space_id)) {
  2260. err = DB_TEMP_FILE_WRITE_FAIL;
  2261. trx->error_key_num = i;
  2262. break;
  2263. }
  2264. MEM_UNDEFINED(
  2265. &block[0], srv_sort_buf_size);
  2266. }
  2267. }
  2268. merge_buf[i] = row_merge_buf_empty(buf);
  2269. buf = merge_buf[i];
  2270. if (UNIV_LIKELY(row != NULL)) {
  2271. /* Try writing the record again, now
  2272. that the buffer has been written out
  2273. and emptied. */
  2274. if (UNIV_UNLIKELY
  2275. (!(rows_added = row_merge_buf_add(
  2276. buf, fts_index, old_table,
  2277. new_table, psort_info, row, ext,
  2278. &doc_id, conv_heap,
  2279. &err, &v_heap, eval_table, trx)))) {
  2280. /* An empty buffer should have enough
  2281. room for at least one record. */
  2282. ut_ad(err == DB_COMPUTE_VALUE_FAILED
  2283. || err == DB_OUT_OF_MEMORY
  2284. || err == DB_TOO_BIG_RECORD);
  2285. } else if (err == DB_SUCCESS) {
  2286. file->n_rec += rows_added;
  2287. continue;
  2288. }
  2289. trx->error_key_num = i;
  2290. break;
  2291. }
  2292. }
  2293. if (row == NULL) {
  2294. if (old_table != new_table) {
  2295. new_table->stat_n_rows = n_rows;
  2296. }
  2297. goto all_done;
  2298. }
  2299. if (err != DB_SUCCESS) {
  2300. goto func_exit;
  2301. }
  2302. if (v_heap) {
  2303. mem_heap_empty(v_heap);
  2304. }
  2305. /* Increment innodb_onlineddl_pct_progress status variable */
  2306. read_rows++;
  2307. if(read_rows % 1000 == 0) {
  2308. /* Update progress for each 1000 rows */
  2309. curr_progress = (read_rows >= table_total_rows) ?
  2310. pct_cost :
  2311. ((pct_cost * read_rows) / table_total_rows);
  2312. /* presenting 10.12% as 1012 integer */
  2313. onlineddl_pct_progress = (ulint) (curr_progress * 100);
  2314. }
  2315. }
  2316. func_exit:
  2317. if (mtr.is_active()) {
  2318. mtr_commit(&mtr);
  2319. }
  2320. if (row_heap) {
  2321. mem_heap_free(row_heap);
  2322. }
  2323. ut_free(nonnull);
  2324. all_done:
  2325. if (clust_btr_bulk != NULL) {
  2326. ut_ad(err != DB_SUCCESS);
  2327. clust_btr_bulk->latch();
  2328. err = clust_btr_bulk->finish(
  2329. err);
  2330. UT_DELETE(clust_btr_bulk);
  2331. }
  2332. if (prev_fields != NULL) {
  2333. ut_free(prev_fields);
  2334. mem_heap_free(mtuple_heap);
  2335. }
  2336. if (v_heap) {
  2337. mem_heap_free(v_heap);
  2338. }
  2339. if (conv_heap != NULL) {
  2340. mem_heap_free(conv_heap);
  2341. }
  2342. #ifdef FTS_INTERNAL_DIAG_PRINT
  2343. DEBUG_FTS_SORT_PRINT("FTS_SORT: Complete Scan Table\n");
  2344. #endif
  2345. if (fts_pll_sort) {
  2346. bool all_exit = false;
  2347. ulint trial_count = 0;
  2348. const ulint max_trial_count = 10000;
  2349. wait_again:
  2350. /* Check if error occurs in child thread */
  2351. for (ulint j = 0; j < fts_sort_pll_degree; j++) {
  2352. if (psort_info[j].error != DB_SUCCESS) {
  2353. err = psort_info[j].error;
  2354. trx->error_key_num = j;
  2355. break;
  2356. }
  2357. }
  2358. /* Tell all children that parent has done scanning */
  2359. for (ulint i = 0; i < fts_sort_pll_degree; i++) {
  2360. if (err == DB_SUCCESS) {
  2361. psort_info[i].state = FTS_PARENT_COMPLETE;
  2362. } else {
  2363. psort_info[i].state = FTS_PARENT_EXITING;
  2364. }
  2365. }
  2366. /* Now wait all children to report back to be completed */
  2367. os_event_wait_time_low(fts_parallel_sort_event,
  2368. 1000000, sig_count);
  2369. for (ulint i = 0; i < fts_sort_pll_degree; i++) {
  2370. if (psort_info[i].child_status != FTS_CHILD_COMPLETE
  2371. && psort_info[i].child_status != FTS_CHILD_EXITING) {
  2372. sig_count = os_event_reset(
  2373. fts_parallel_sort_event);
  2374. goto wait_again;
  2375. }
  2376. }
  2377. /* Now all children should complete, wait a bit until
  2378. they all finish setting the event, before we free everything.
  2379. This has a 10 second timeout */
  2380. do {
  2381. all_exit = true;
  2382. for (ulint j = 0; j < fts_sort_pll_degree; j++) {
  2383. if (psort_info[j].child_status
  2384. != FTS_CHILD_EXITING) {
  2385. all_exit = false;
  2386. os_thread_sleep(1000);
  2387. break;
  2388. }
  2389. }
  2390. trial_count++;
  2391. } while (!all_exit && trial_count < max_trial_count);
  2392. if (!all_exit) {
  2393. ib::fatal() << "Not all child sort threads exited"
  2394. " when creating FTS index '"
  2395. << fts_sort_idx->name << "'";
  2396. }
  2397. }
  2398. #ifdef FTS_INTERNAL_DIAG_PRINT
  2399. DEBUG_FTS_SORT_PRINT("FTS_SORT: Complete Tokenization\n");
  2400. #endif
  2401. for (ulint i = 0; i < n_index; i++) {
  2402. row_merge_buf_free(merge_buf[i]);
  2403. }
  2404. row_fts_free_pll_merge_buf(psort_info);
  2405. ut_free(merge_buf);
  2406. btr_pcur_close(&pcur);
  2407. if (sp_tuples != NULL) {
  2408. for (ulint i = 0; i < num_spatial; i++) {
  2409. UT_DELETE(sp_tuples[i]);
  2410. }
  2411. ut_free(sp_tuples);
  2412. if (sp_heap) {
  2413. mem_heap_free(sp_heap);
  2414. }
  2415. }
  2416. /* Update the next Doc ID we used. Table should be locked, so
  2417. no concurrent DML */
  2418. if (max_doc_id && err == DB_SUCCESS) {
  2419. /* Sync fts cache for other fts indexes to keep all
  2420. fts indexes consistent in sync_doc_id. */
  2421. err = fts_sync_table(const_cast<dict_table_t*>(new_table));
  2422. if (err == DB_SUCCESS) {
  2423. new_table->fts->cache->synced_doc_id = max_doc_id;
  2424. /* Update the max value as next FTS_DOC_ID */
  2425. if (max_doc_id >= new_table->fts->cache->next_doc_id) {
  2426. new_table->fts->cache->next_doc_id =
  2427. max_doc_id + 1;
  2428. }
  2429. new_table->fts->cache->first_doc_id =
  2430. new_table->fts->cache->next_doc_id;
  2431. err= fts_update_sync_doc_id(
  2432. new_table,
  2433. new_table->fts->cache->synced_doc_id,
  2434. NULL);
  2435. }
  2436. }
  2437. if (vers_update_trt) {
  2438. trx_mod_table_time_t& time =
  2439. trx->mod_tables
  2440. .insert(trx_mod_tables_t::value_type(
  2441. const_cast<dict_table_t*>(new_table), 0))
  2442. .first->second;
  2443. time.set_versioned(0);
  2444. }
  2445. trx->op_info = "";
  2446. DBUG_RETURN(err);
  2447. }
  2448. /** Write a record via buffer 2 and read the next record to buffer N.
  2449. @param N number of the buffer (0 or 1)
  2450. @param INDEX record descriptor
  2451. @param AT_END statement to execute at end of input */
  2452. #define ROW_MERGE_WRITE_GET_NEXT_LOW(N, INDEX, AT_END) \
  2453. do { \
  2454. b2 = row_merge_write_rec(&block[2 * srv_sort_buf_size], \
  2455. &buf[2], b2, \
  2456. of->fd, &of->offset, \
  2457. mrec##N, offsets##N, \
  2458. crypt_block ? &crypt_block[2 * srv_sort_buf_size] : NULL , \
  2459. space); \
  2460. if (UNIV_UNLIKELY(!b2 || ++of->n_rec > file->n_rec)) { \
  2461. goto corrupt; \
  2462. } \
  2463. b##N = row_merge_read_rec(&block[N * srv_sort_buf_size],\
  2464. &buf[N], b##N, INDEX, \
  2465. file->fd, foffs##N, \
  2466. &mrec##N, offsets##N, \
  2467. crypt_block ? &crypt_block[N * srv_sort_buf_size] : NULL, \
  2468. space); \
  2469. \
  2470. if (UNIV_UNLIKELY(!b##N)) { \
  2471. if (mrec##N) { \
  2472. goto corrupt; \
  2473. } \
  2474. AT_END; \
  2475. } \
  2476. } while (0)
  2477. #ifdef HAVE_PSI_STAGE_INTERFACE
  2478. #define ROW_MERGE_WRITE_GET_NEXT(N, INDEX, AT_END) \
  2479. do { \
  2480. if (stage != NULL) { \
  2481. stage->inc(); \
  2482. } \
  2483. ROW_MERGE_WRITE_GET_NEXT_LOW(N, INDEX, AT_END); \
  2484. } while (0)
  2485. #else /* HAVE_PSI_STAGE_INTERFACE */
  2486. #define ROW_MERGE_WRITE_GET_NEXT(N, INDEX, AT_END) \
  2487. ROW_MERGE_WRITE_GET_NEXT_LOW(N, INDEX, AT_END)
  2488. #endif /* HAVE_PSI_STAGE_INTERFACE */
  2489. /** Merge two blocks of records on disk and write a bigger block.
  2490. @param[in] dup descriptor of index being created
  2491. @param[in] file file containing index entries
  2492. @param[in,out] block 3 buffers
  2493. @param[in,out] foffs0 offset of first source list in the file
  2494. @param[in,out] foffs1 offset of second source list in the file
  2495. @param[in,out] of output file
  2496. @param[in,out] stage performance schema accounting object, used by
  2497. ALTER TABLE. If not NULL stage->inc() will be called for each record
  2498. processed.
  2499. @param[in,out] crypt_block encryption buffer
  2500. @param[in] space tablespace ID for encryption
  2501. @return DB_SUCCESS or error code */
  2502. static MY_ATTRIBUTE((warn_unused_result))
  2503. dberr_t
  2504. row_merge_blocks(
  2505. const row_merge_dup_t* dup,
  2506. const merge_file_t* file,
  2507. row_merge_block_t* block,
  2508. ulint* foffs0,
  2509. ulint* foffs1,
  2510. merge_file_t* of,
  2511. ut_stage_alter_t* stage MY_ATTRIBUTE((unused)),
  2512. row_merge_block_t* crypt_block,
  2513. ulint space)
  2514. {
  2515. mem_heap_t* heap; /*!< memory heap for offsets0, offsets1 */
  2516. mrec_buf_t* buf; /*!< buffer for handling
  2517. split mrec in block[] */
  2518. const byte* b0; /*!< pointer to block[0] */
  2519. const byte* b1; /*!< pointer to block[srv_sort_buf_size] */
  2520. byte* b2; /*!< pointer to block[2 * srv_sort_buf_size] */
  2521. const mrec_t* mrec0; /*!< merge rec, points to block[0] or buf[0] */
  2522. const mrec_t* mrec1; /*!< merge rec, points to
  2523. block[srv_sort_buf_size] or buf[1] */
  2524. rec_offs* offsets0;/* offsets of mrec0 */
  2525. rec_offs* offsets1;/* offsets of mrec1 */
  2526. DBUG_ENTER("row_merge_blocks");
  2527. DBUG_LOG("ib_merge_sort",
  2528. "fd=" << file->fd << ',' << *foffs0 << '+' << *foffs1
  2529. << " to fd=" << of->fd << ',' << of->offset);
  2530. heap = row_merge_heap_create(dup->index, &buf, &offsets0, &offsets1);
  2531. /* Write a record and read the next record. Split the output
  2532. file in two halves, which can be merged on the following pass. */
  2533. if (!row_merge_read(file->fd, *foffs0, &block[0],
  2534. crypt_block ? &crypt_block[0] : NULL,
  2535. space) ||
  2536. !row_merge_read(file->fd, *foffs1, &block[srv_sort_buf_size],
  2537. crypt_block ? &crypt_block[srv_sort_buf_size] : NULL,
  2538. space)) {
  2539. corrupt:
  2540. mem_heap_free(heap);
  2541. DBUG_RETURN(DB_CORRUPTION);
  2542. }
  2543. b0 = &block[0];
  2544. b1 = &block[srv_sort_buf_size];
  2545. b2 = &block[2 * srv_sort_buf_size];
  2546. b0 = row_merge_read_rec(
  2547. &block[0], &buf[0], b0, dup->index,
  2548. file->fd, foffs0, &mrec0, offsets0,
  2549. crypt_block ? &crypt_block[0] : NULL,
  2550. space);
  2551. b1 = row_merge_read_rec(
  2552. &block[srv_sort_buf_size],
  2553. &buf[srv_sort_buf_size], b1, dup->index,
  2554. file->fd, foffs1, &mrec1, offsets1,
  2555. crypt_block ? &crypt_block[srv_sort_buf_size] : NULL,
  2556. space);
  2557. if (UNIV_UNLIKELY(!b0 && mrec0)
  2558. || UNIV_UNLIKELY(!b1 && mrec1)) {
  2559. goto corrupt;
  2560. }
  2561. while (mrec0 && mrec1) {
  2562. int cmp = cmp_rec_rec_simple(
  2563. mrec0, mrec1, offsets0, offsets1,
  2564. dup->index, dup->table);
  2565. if (cmp < 0) {
  2566. ROW_MERGE_WRITE_GET_NEXT(0, dup->index, goto merged);
  2567. } else if (cmp) {
  2568. ROW_MERGE_WRITE_GET_NEXT(1, dup->index, goto merged);
  2569. } else {
  2570. mem_heap_free(heap);
  2571. DBUG_RETURN(DB_DUPLICATE_KEY);
  2572. }
  2573. }
  2574. merged:
  2575. if (mrec0) {
  2576. /* append all mrec0 to output */
  2577. for (;;) {
  2578. ROW_MERGE_WRITE_GET_NEXT(0, dup->index, goto done0);
  2579. }
  2580. }
  2581. done0:
  2582. if (mrec1) {
  2583. /* append all mrec1 to output */
  2584. for (;;) {
  2585. ROW_MERGE_WRITE_GET_NEXT(1, dup->index, goto done1);
  2586. }
  2587. }
  2588. done1:
  2589. mem_heap_free(heap);
  2590. b2 = row_merge_write_eof(
  2591. &block[2 * srv_sort_buf_size],
  2592. b2, of->fd, &of->offset,
  2593. crypt_block ? &crypt_block[2 * srv_sort_buf_size] : NULL,
  2594. space);
  2595. DBUG_RETURN(b2 ? DB_SUCCESS : DB_CORRUPTION);
  2596. }
  2597. /** Copy a block of index entries.
  2598. @param[in] index index being created
  2599. @param[in] file input file
  2600. @param[in,out] block 3 buffers
  2601. @param[in,out] foffs0 input file offset
  2602. @param[in,out] of output file
  2603. @param[in,out] stage performance schema accounting object, used by
  2604. ALTER TABLE. If not NULL stage->inc() will be called for each record
  2605. processed.
  2606. @param[in,out] crypt_block encryption buffer
  2607. @param[in] space tablespace ID for encryption
  2608. @return TRUE on success, FALSE on failure */
  2609. static MY_ATTRIBUTE((warn_unused_result))
  2610. ibool
  2611. row_merge_blocks_copy(
  2612. const dict_index_t* index,
  2613. const merge_file_t* file,
  2614. row_merge_block_t* block,
  2615. ulint* foffs0,
  2616. merge_file_t* of,
  2617. ut_stage_alter_t* stage MY_ATTRIBUTE((unused)),
  2618. row_merge_block_t* crypt_block,
  2619. ulint space)
  2620. {
  2621. mem_heap_t* heap; /*!< memory heap for offsets0, offsets1 */
  2622. mrec_buf_t* buf; /*!< buffer for handling
  2623. split mrec in block[] */
  2624. const byte* b0; /*!< pointer to block[0] */
  2625. byte* b2; /*!< pointer to block[2 * srv_sort_buf_size] */
  2626. const mrec_t* mrec0; /*!< merge rec, points to block[0] */
  2627. rec_offs* offsets0;/* offsets of mrec0 */
  2628. rec_offs* offsets1;/* dummy offsets */
  2629. DBUG_ENTER("row_merge_blocks_copy");
  2630. DBUG_LOG("ib_merge_sort",
  2631. "fd=" << file->fd << ',' << foffs0
  2632. << " to fd=" << of->fd << ',' << of->offset);
  2633. heap = row_merge_heap_create(index, &buf, &offsets0, &offsets1);
  2634. /* Write a record and read the next record. Split the output
  2635. file in two halves, which can be merged on the following pass. */
  2636. if (!row_merge_read(file->fd, *foffs0, &block[0],
  2637. crypt_block ? &crypt_block[0] : NULL,
  2638. space)) {
  2639. corrupt:
  2640. mem_heap_free(heap);
  2641. DBUG_RETURN(FALSE);
  2642. }
  2643. b0 = &block[0];
  2644. b2 = &block[2 * srv_sort_buf_size];
  2645. b0 = row_merge_read_rec(&block[0], &buf[0], b0, index,
  2646. file->fd, foffs0, &mrec0, offsets0,
  2647. crypt_block ? &crypt_block[0] : NULL,
  2648. space);
  2649. if (UNIV_UNLIKELY(!b0 && mrec0)) {
  2650. goto corrupt;
  2651. }
  2652. if (mrec0) {
  2653. /* append all mrec0 to output */
  2654. for (;;) {
  2655. ROW_MERGE_WRITE_GET_NEXT(0, index, goto done0);
  2656. }
  2657. }
  2658. done0:
  2659. /* The file offset points to the beginning of the last page
  2660. that has been read. Update it to point to the next block. */
  2661. (*foffs0)++;
  2662. mem_heap_free(heap);
  2663. DBUG_RETURN(row_merge_write_eof(
  2664. &block[2 * srv_sort_buf_size],
  2665. b2, of->fd, &of->offset,
  2666. crypt_block
  2667. ? &crypt_block[2 * srv_sort_buf_size]
  2668. : NULL, space)
  2669. != NULL);
  2670. }
  2671. /** Merge disk files.
  2672. @param[in] trx transaction
  2673. @param[in] dup descriptor of index being created
  2674. @param[in,out] file file containing index entries
  2675. @param[in,out] block 3 buffers
  2676. @param[in,out] tmpfd temporary file handle
  2677. @param[in,out] num_run Number of runs that remain to be merged
  2678. @param[in,out] run_offset Array that contains the first offset number
  2679. for each merge run
  2680. @param[in,out] stage performance schema accounting object, used by
  2681. @param[in,out] crypt_block encryption buffer
  2682. @param[in] space tablespace ID for encryption
  2683. ALTER TABLE. If not NULL stage->inc() will be called for each record
  2684. processed.
  2685. @return DB_SUCCESS or error code */
  2686. static
  2687. dberr_t
  2688. row_merge(
  2689. trx_t* trx,
  2690. const row_merge_dup_t* dup,
  2691. merge_file_t* file,
  2692. row_merge_block_t* block,
  2693. pfs_os_file_t* tmpfd,
  2694. ulint* num_run,
  2695. ulint* run_offset,
  2696. ut_stage_alter_t* stage,
  2697. row_merge_block_t* crypt_block,
  2698. ulint space)
  2699. {
  2700. ulint foffs0; /*!< first input offset */
  2701. ulint foffs1; /*!< second input offset */
  2702. dberr_t error; /*!< error code */
  2703. merge_file_t of; /*!< output file */
  2704. const ulint ihalf = run_offset[*num_run / 2];
  2705. /*!< half the input file */
  2706. ulint n_run = 0;
  2707. /*!< num of runs generated from this merge */
  2708. MEM_CHECK_ADDRESSABLE(&block[0], 3 * srv_sort_buf_size);
  2709. if (crypt_block) {
  2710. MEM_CHECK_ADDRESSABLE(&crypt_block[0], 3 * srv_sort_buf_size);
  2711. }
  2712. ut_ad(ihalf < file->offset);
  2713. of.fd = *tmpfd;
  2714. of.offset = 0;
  2715. of.n_rec = 0;
  2716. #ifdef POSIX_FADV_SEQUENTIAL
  2717. /* The input file will be read sequentially, starting from the
  2718. beginning and the middle. In Linux, the POSIX_FADV_SEQUENTIAL
  2719. affects the entire file. Each block will be read exactly once. */
  2720. posix_fadvise(file->fd, 0, 0,
  2721. POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE);
  2722. #endif /* POSIX_FADV_SEQUENTIAL */
  2723. /* Merge blocks to the output file. */
  2724. foffs0 = 0;
  2725. foffs1 = ihalf;
  2726. MEM_UNDEFINED(run_offset, *num_run * sizeof *run_offset);
  2727. for (; foffs0 < ihalf && foffs1 < file->offset; foffs0++, foffs1++) {
  2728. if (trx_is_interrupted(trx)) {
  2729. return(DB_INTERRUPTED);
  2730. }
  2731. /* Remember the offset number for this run */
  2732. run_offset[n_run++] = of.offset;
  2733. error = row_merge_blocks(dup, file, block,
  2734. &foffs0, &foffs1, &of, stage,
  2735. crypt_block, space);
  2736. if (error != DB_SUCCESS) {
  2737. return(error);
  2738. }
  2739. }
  2740. /* Copy the last blocks, if there are any. */
  2741. while (foffs0 < ihalf) {
  2742. if (UNIV_UNLIKELY(trx_is_interrupted(trx))) {
  2743. return(DB_INTERRUPTED);
  2744. }
  2745. /* Remember the offset number for this run */
  2746. run_offset[n_run++] = of.offset;
  2747. if (!row_merge_blocks_copy(dup->index, file, block,
  2748. &foffs0, &of, stage,
  2749. crypt_block, space)) {
  2750. return(DB_CORRUPTION);
  2751. }
  2752. }
  2753. ut_ad(foffs0 == ihalf);
  2754. while (foffs1 < file->offset) {
  2755. if (trx_is_interrupted(trx)) {
  2756. return(DB_INTERRUPTED);
  2757. }
  2758. /* Remember the offset number for this run */
  2759. run_offset[n_run++] = of.offset;
  2760. if (!row_merge_blocks_copy(dup->index, file, block,
  2761. &foffs1, &of, stage,
  2762. crypt_block, space)) {
  2763. return(DB_CORRUPTION);
  2764. }
  2765. }
  2766. ut_ad(foffs1 == file->offset);
  2767. if (UNIV_UNLIKELY(of.n_rec != file->n_rec)) {
  2768. return(DB_CORRUPTION);
  2769. }
  2770. ut_ad(n_run <= *num_run);
  2771. *num_run = n_run;
  2772. /* Each run can contain one or more offsets. As merge goes on,
  2773. the number of runs (to merge) will reduce until we have one
  2774. single run. So the number of runs will always be smaller than
  2775. the number of offsets in file */
  2776. ut_ad((*num_run) <= file->offset);
  2777. /* The number of offsets in output file is always equal or
  2778. smaller than input file */
  2779. ut_ad(of.offset <= file->offset);
  2780. /* Swap file descriptors for the next pass. */
  2781. *tmpfd = file->fd;
  2782. *file = of;
  2783. MEM_UNDEFINED(&block[0], 3 * srv_sort_buf_size);
  2784. return(DB_SUCCESS);
  2785. }
  2786. /** Merge disk files.
  2787. @param[in] trx transaction
  2788. @param[in] dup descriptor of index being created
  2789. @param[in,out] file file containing index entries
  2790. @param[in,out] block 3 buffers
  2791. @param[in,out] tmpfd temporary file handle
  2792. @param[in,out] stage performance schema accounting object, used by
  2793. ALTER TABLE. If not NULL, stage->begin_phase_sort() will be called initially
  2794. and then stage->inc() will be called for each record processed.
  2795. @return DB_SUCCESS or error code */
  2796. dberr_t
  2797. row_merge_sort(
  2798. trx_t* trx,
  2799. const row_merge_dup_t* dup,
  2800. merge_file_t* file,
  2801. row_merge_block_t* block,
  2802. pfs_os_file_t* tmpfd,
  2803. const bool update_progress,
  2804. /*!< in: update progress
  2805. status variable or not */
  2806. const double pct_progress,
  2807. /*!< in: total progress percent
  2808. until now */
  2809. const double pct_cost, /*!< in: current progress percent */
  2810. row_merge_block_t* crypt_block, /*!< in: crypt buf or NULL */
  2811. ulint space, /*!< in: space id */
  2812. ut_stage_alter_t* stage)
  2813. {
  2814. const ulint half = file->offset / 2;
  2815. ulint num_runs;
  2816. ulint* run_offset;
  2817. dberr_t error = DB_SUCCESS;
  2818. ulint merge_count = 0;
  2819. ulint total_merge_sort_count;
  2820. double curr_progress = 0;
  2821. DBUG_ENTER("row_merge_sort");
  2822. /* Record the number of merge runs we need to perform */
  2823. num_runs = file->offset;
  2824. if (stage != NULL) {
  2825. stage->begin_phase_sort(log2(num_runs));
  2826. }
  2827. /* If num_runs are less than 1, nothing to merge */
  2828. if (num_runs <= 1) {
  2829. DBUG_RETURN(error);
  2830. }
  2831. total_merge_sort_count = ulint(ceil(log2(double(num_runs))));
  2832. /* "run_offset" records each run's first offset number */
  2833. run_offset = (ulint*) ut_malloc_nokey(file->offset * sizeof(ulint));
  2834. /* This tells row_merge() where to start for the first round
  2835. of merge. */
  2836. run_offset[half] = half;
  2837. /* The file should always contain at least one byte (the end
  2838. of file marker). Thus, it must be at least one block. */
  2839. ut_ad(file->offset > 0);
  2840. /* These thd_progress* calls will crash on sol10-64 when innodb_plugin
  2841. is used. MDEV-9356: innodb.innodb_bug53290 fails (crashes) on
  2842. sol10-64 in buildbot.
  2843. */
  2844. #ifndef UNIV_SOLARIS
  2845. /* Progress report only for "normal" indexes. */
  2846. if (!(dup->index->type & DICT_FTS)) {
  2847. thd_progress_init(trx->mysql_thd, 1);
  2848. }
  2849. #endif /* UNIV_SOLARIS */
  2850. if (global_system_variables.log_warnings > 2) {
  2851. sql_print_information("InnoDB: Online DDL : merge-sorting"
  2852. " has estimated " ULINTPF " runs",
  2853. num_runs);
  2854. }
  2855. /* Merge the runs until we have one big run */
  2856. do {
  2857. /* Report progress of merge sort to MySQL for
  2858. show processlist progress field */
  2859. /* Progress report only for "normal" indexes. */
  2860. #ifndef UNIV_SOLARIS
  2861. if (!(dup->index->type & DICT_FTS)) {
  2862. thd_progress_report(trx->mysql_thd, file->offset - num_runs, file->offset);
  2863. }
  2864. #endif /* UNIV_SOLARIS */
  2865. error = row_merge(trx, dup, file, block, tmpfd,
  2866. &num_runs, run_offset, stage,
  2867. crypt_block, space);
  2868. if(update_progress) {
  2869. merge_count++;
  2870. curr_progress = (merge_count >= total_merge_sort_count) ?
  2871. pct_cost :
  2872. ((pct_cost * merge_count) / total_merge_sort_count);
  2873. /* presenting 10.12% as 1012 integer */;
  2874. onlineddl_pct_progress = (ulint) ((pct_progress + curr_progress) * 100);
  2875. }
  2876. if (error != DB_SUCCESS) {
  2877. break;
  2878. }
  2879. MEM_CHECK_DEFINED(run_offset, num_runs * sizeof *run_offset);
  2880. } while (num_runs > 1);
  2881. ut_free(run_offset);
  2882. /* Progress report only for "normal" indexes. */
  2883. #ifndef UNIV_SOLARIS
  2884. if (!(dup->index->type & DICT_FTS)) {
  2885. thd_progress_end(trx->mysql_thd);
  2886. }
  2887. #endif /* UNIV_SOLARIS */
  2888. DBUG_RETURN(error);
  2889. }
  2890. /** Copy externally stored columns to the data tuple.
  2891. @param[in] mrec record containing BLOB pointers,
  2892. or NULL to use tuple instead
  2893. @param[in] offsets offsets of mrec
  2894. @param[in] zip_size compressed page size in bytes, or 0
  2895. @param[in,out] tuple data tuple
  2896. @param[in,out] heap memory heap */
  2897. static
  2898. void
  2899. row_merge_copy_blobs(
  2900. const mrec_t* mrec,
  2901. const rec_offs* offsets,
  2902. const page_size_t& page_size,
  2903. dtuple_t* tuple,
  2904. mem_heap_t* heap)
  2905. {
  2906. ut_ad(mrec == NULL || rec_offs_any_extern(offsets));
  2907. for (ulint i = 0; i < dtuple_get_n_fields(tuple); i++) {
  2908. ulint len;
  2909. const void* data;
  2910. dfield_t* field = dtuple_get_nth_field(tuple, i);
  2911. ulint field_len;
  2912. const byte* field_data;
  2913. if (!dfield_is_ext(field)) {
  2914. continue;
  2915. }
  2916. ut_ad(!dfield_is_null(field));
  2917. /* During the creation of a PRIMARY KEY, the table is
  2918. X-locked, and we skip copying records that have been
  2919. marked for deletion. Therefore, externally stored
  2920. columns cannot possibly be freed between the time the
  2921. BLOB pointers are read (row_merge_read_clustered_index())
  2922. and dereferenced (below). */
  2923. if (mrec == NULL) {
  2924. field_data
  2925. = static_cast<byte*>(dfield_get_data(field));
  2926. field_len = dfield_get_len(field);
  2927. ut_a(field_len >= BTR_EXTERN_FIELD_REF_SIZE);
  2928. ut_a(memcmp(field_data + field_len
  2929. - BTR_EXTERN_FIELD_REF_SIZE,
  2930. field_ref_zero,
  2931. BTR_EXTERN_FIELD_REF_SIZE));
  2932. data = btr_copy_externally_stored_field(
  2933. &len, field_data, page_size, field_len, heap);
  2934. } else {
  2935. data = btr_rec_copy_externally_stored_field(
  2936. mrec, offsets, page_size, i, &len, heap);
  2937. }
  2938. /* Because we have locked the table, any records
  2939. written by incomplete transactions must have been
  2940. rolled back already. There must not be any incomplete
  2941. BLOB columns. */
  2942. ut_a(data);
  2943. dfield_set_data(field, data, len);
  2944. }
  2945. }
  2946. /** Convert a merge record to a typed data tuple. Note that externally
  2947. stored fields are not copied to heap.
  2948. @param[in,out] index index on the table
  2949. @param[in] mtuple merge record
  2950. @param[in] heap memory heap from which memory needed is allocated
  2951. @return index entry built. */
  2952. static
  2953. void
  2954. row_merge_mtuple_to_dtuple(
  2955. dict_index_t* index,
  2956. dtuple_t* dtuple,
  2957. const mtuple_t* mtuple)
  2958. {
  2959. ut_ad(!dict_index_is_ibuf(index));
  2960. memcpy(dtuple->fields, mtuple->fields,
  2961. dtuple->n_fields * sizeof *mtuple->fields);
  2962. }
  2963. /** Insert sorted data tuples to the index.
  2964. @param[in] index index to be inserted
  2965. @param[in] old_table old table
  2966. @param[in] fd file descriptor
  2967. @param[in,out] block file buffer
  2968. @param[in] row_buf row_buf the sorted data tuples,
  2969. or NULL if fd, block will be used instead
  2970. @param[in,out] btr_bulk btr bulk instance
  2971. @param[in,out] stage performance schema accounting object, used by
  2972. ALTER TABLE. If not NULL stage->begin_phase_insert() will be called initially
  2973. and then stage->inc() will be called for each record that is processed.
  2974. @return DB_SUCCESS or error number */
  2975. static MY_ATTRIBUTE((warn_unused_result))
  2976. dberr_t
  2977. row_merge_insert_index_tuples(
  2978. dict_index_t* index,
  2979. const dict_table_t* old_table,
  2980. const pfs_os_file_t& fd,
  2981. row_merge_block_t* block,
  2982. const row_merge_buf_t* row_buf,
  2983. BtrBulk* btr_bulk,
  2984. const ib_uint64_t table_total_rows, /*!< in: total rows of old table */
  2985. const double pct_progress, /*!< in: total progress
  2986. percent until now */
  2987. const double pct_cost, /*!< in: current progress percent
  2988. */
  2989. row_merge_block_t* crypt_block, /*!< in: crypt buf or NULL */
  2990. ulint space, /*!< in: space id */
  2991. ut_stage_alter_t* stage)
  2992. {
  2993. const byte* b;
  2994. mem_heap_t* heap;
  2995. mem_heap_t* tuple_heap;
  2996. dberr_t error = DB_SUCCESS;
  2997. ulint foffs = 0;
  2998. rec_offs* offsets;
  2999. mrec_buf_t* buf;
  3000. ulint n_rows = 0;
  3001. dtuple_t* dtuple;
  3002. ib_uint64_t inserted_rows = 0;
  3003. double curr_progress = 0;
  3004. dict_index_t* old_index = NULL;
  3005. const mrec_t* mrec = NULL;
  3006. mtr_t mtr;
  3007. DBUG_ENTER("row_merge_insert_index_tuples");
  3008. ut_ad(!srv_read_only_mode);
  3009. ut_ad(!(index->type & DICT_FTS));
  3010. ut_ad(!dict_index_is_spatial(index));
  3011. if (stage != NULL) {
  3012. stage->begin_phase_insert();
  3013. }
  3014. tuple_heap = mem_heap_create(1000);
  3015. {
  3016. ulint i = 1 + REC_OFFS_HEADER_SIZE
  3017. + dict_index_get_n_fields(index);
  3018. heap = mem_heap_create(sizeof *buf + i * sizeof *offsets);
  3019. offsets = static_cast<rec_offs*>(
  3020. mem_heap_alloc(heap, i * sizeof *offsets));
  3021. rec_offs_set_n_alloc(offsets, i);
  3022. rec_offs_set_n_fields(offsets, dict_index_get_n_fields(index));
  3023. }
  3024. if (row_buf != NULL) {
  3025. ut_ad(fd == OS_FILE_CLOSED);
  3026. ut_ad(block == NULL);
  3027. DBUG_EXECUTE_IF("row_merge_read_failure",
  3028. error = DB_CORRUPTION;
  3029. goto err_exit;);
  3030. buf = NULL;
  3031. b = NULL;
  3032. dtuple = dtuple_create(
  3033. heap, dict_index_get_n_fields(index));
  3034. dtuple_set_n_fields_cmp(
  3035. dtuple, dict_index_get_n_unique_in_tree(index));
  3036. } else {
  3037. b = block;
  3038. dtuple = NULL;
  3039. if (!row_merge_read(fd, foffs, block, crypt_block, space)) {
  3040. error = DB_CORRUPTION;
  3041. goto err_exit;
  3042. } else {
  3043. buf = static_cast<mrec_buf_t*>(
  3044. mem_heap_alloc(heap, sizeof *buf));
  3045. }
  3046. }
  3047. for (;;) {
  3048. if (stage != NULL) {
  3049. stage->inc();
  3050. }
  3051. if (row_buf != NULL) {
  3052. if (n_rows >= row_buf->n_tuples) {
  3053. break;
  3054. }
  3055. /* Convert merge tuple record from
  3056. row buffer to data tuple record */
  3057. row_merge_mtuple_to_dtuple(
  3058. index, dtuple, &row_buf->tuples[n_rows]);
  3059. n_rows++;
  3060. /* BLOB pointers must be copied from dtuple */
  3061. mrec = NULL;
  3062. } else {
  3063. b = row_merge_read_rec(block, buf, b, index,
  3064. fd, &foffs, &mrec, offsets,
  3065. crypt_block,
  3066. space);
  3067. if (UNIV_UNLIKELY(!b)) {
  3068. /* End of list, or I/O error */
  3069. if (mrec) {
  3070. error = DB_CORRUPTION;
  3071. }
  3072. break;
  3073. }
  3074. dtuple = row_rec_to_index_entry_low(
  3075. mrec, index, offsets, tuple_heap);
  3076. }
  3077. old_index = dict_table_get_first_index(old_table);
  3078. if (dict_index_is_clust(index)
  3079. && dict_index_is_online_ddl(old_index)) {
  3080. error = row_log_table_get_error(old_index);
  3081. if (error != DB_SUCCESS) {
  3082. break;
  3083. }
  3084. }
  3085. if (dict_index_is_clust(index) && dtuple_get_n_ext(dtuple)) {
  3086. /* Off-page columns can be fetched safely
  3087. when concurrent modifications to the table
  3088. are disabled. (Purge can process delete-marked
  3089. records, but row_merge_read_clustered_index()
  3090. would have skipped them.)
  3091. When concurrent modifications are enabled,
  3092. row_merge_read_clustered_index() will
  3093. only see rows from transactions that were
  3094. committed before the ALTER TABLE started
  3095. (REPEATABLE READ).
  3096. Any modifications after the
  3097. row_merge_read_clustered_index() scan
  3098. will go through row_log_table_apply().
  3099. Any modifications to off-page columns
  3100. will be tracked by
  3101. row_log_table_blob_alloc() and
  3102. row_log_table_blob_free(). */
  3103. row_merge_copy_blobs(
  3104. mrec, offsets,
  3105. dict_table_page_size(old_table),
  3106. dtuple, tuple_heap);
  3107. }
  3108. #ifdef UNIV_DEBUG
  3109. static const latch_level_t latches[] = {
  3110. SYNC_INDEX_TREE, /* index->lock */
  3111. SYNC_LEVEL_VARYING /* btr_bulk->m_page_bulks */
  3112. };
  3113. #endif /* UNIV_DEBUG */
  3114. ut_ad(dtuple_validate(dtuple));
  3115. ut_ad(!sync_check_iterate(sync_allowed_latches(latches,
  3116. latches + 2)));
  3117. error = btr_bulk->insert(dtuple);
  3118. if (error != DB_SUCCESS) {
  3119. goto err_exit;
  3120. }
  3121. mem_heap_empty(tuple_heap);
  3122. /* Increment innodb_onlineddl_pct_progress status variable */
  3123. inserted_rows++;
  3124. if(inserted_rows % 1000 == 0) {
  3125. /* Update progress for each 1000 rows */
  3126. curr_progress = (inserted_rows >= table_total_rows ||
  3127. table_total_rows <= 0) ?
  3128. pct_cost :
  3129. ((pct_cost * inserted_rows) / table_total_rows);
  3130. /* presenting 10.12% as 1012 integer */;
  3131. onlineddl_pct_progress = (ulint) ((pct_progress + curr_progress) * 100);
  3132. }
  3133. }
  3134. err_exit:
  3135. mem_heap_free(tuple_heap);
  3136. mem_heap_free(heap);
  3137. DBUG_RETURN(error);
  3138. }
  3139. /*********************************************************************//**
  3140. Sets an exclusive lock on a table, for the duration of creating indexes.
  3141. @return error code or DB_SUCCESS */
  3142. dberr_t
  3143. row_merge_lock_table(
  3144. /*=================*/
  3145. trx_t* trx, /*!< in/out: transaction */
  3146. dict_table_t* table, /*!< in: table to lock */
  3147. enum lock_mode mode) /*!< in: LOCK_X or LOCK_S */
  3148. {
  3149. ut_ad(!srv_read_only_mode);
  3150. ut_ad(mode == LOCK_X || mode == LOCK_S);
  3151. trx->op_info = "setting table lock for creating or dropping index";
  3152. trx->ddl = true;
  3153. return(lock_table_for_trx(table, trx, mode));
  3154. }
  3155. /*********************************************************************//**
  3156. Drop an index that was created before an error occurred.
  3157. The data dictionary must have been locked exclusively by the caller,
  3158. because the transaction will not be committed. */
  3159. static
  3160. void
  3161. row_merge_drop_index_dict(
  3162. /*======================*/
  3163. trx_t* trx, /*!< in/out: dictionary transaction */
  3164. index_id_t index_id)/*!< in: index identifier */
  3165. {
  3166. static const char sql[] =
  3167. "PROCEDURE DROP_INDEX_PROC () IS\n"
  3168. "BEGIN\n"
  3169. "DELETE FROM SYS_FIELDS WHERE INDEX_ID=:indexid;\n"
  3170. "DELETE FROM SYS_INDEXES WHERE ID=:indexid;\n"
  3171. "END;\n";
  3172. dberr_t error;
  3173. pars_info_t* info;
  3174. ut_ad(!srv_read_only_mode);
  3175. ut_ad(mutex_own(&dict_sys->mutex));
  3176. ut_ad(trx->dict_operation_lock_mode == RW_X_LATCH);
  3177. ut_ad(trx_get_dict_operation(trx) == TRX_DICT_OP_INDEX);
  3178. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_X));
  3179. info = pars_info_create();
  3180. pars_info_add_ull_literal(info, "indexid", index_id);
  3181. trx->op_info = "dropping index from dictionary";
  3182. error = que_eval_sql(info, sql, FALSE, trx);
  3183. if (error != DB_SUCCESS) {
  3184. /* Even though we ensure that DDL transactions are WAIT
  3185. and DEADLOCK free, we could encounter other errors e.g.,
  3186. DB_TOO_MANY_CONCURRENT_TRXS. */
  3187. trx->error_state = DB_SUCCESS;
  3188. ib::error() << "row_merge_drop_index_dict failed with error "
  3189. << error;
  3190. }
  3191. trx->op_info = "";
  3192. }
  3193. /*********************************************************************//**
  3194. Drop indexes that were created before an error occurred.
  3195. The data dictionary must have been locked exclusively by the caller,
  3196. because the transaction will not be committed. */
  3197. void
  3198. row_merge_drop_indexes_dict(
  3199. /*========================*/
  3200. trx_t* trx, /*!< in/out: dictionary transaction */
  3201. table_id_t table_id)/*!< in: table identifier */
  3202. {
  3203. static const char sql[] =
  3204. "PROCEDURE DROP_INDEXES_PROC () IS\n"
  3205. "ixid CHAR;\n"
  3206. "found INT;\n"
  3207. "DECLARE CURSOR index_cur IS\n"
  3208. " SELECT ID FROM SYS_INDEXES\n"
  3209. " WHERE TABLE_ID=:tableid AND\n"
  3210. " SUBSTR(NAME,0,1)='" TEMP_INDEX_PREFIX_STR "'\n"
  3211. "FOR UPDATE;\n"
  3212. "BEGIN\n"
  3213. "found := 1;\n"
  3214. "OPEN index_cur;\n"
  3215. "WHILE found = 1 LOOP\n"
  3216. " FETCH index_cur INTO ixid;\n"
  3217. " IF (SQL % NOTFOUND) THEN\n"
  3218. " found := 0;\n"
  3219. " ELSE\n"
  3220. " DELETE FROM SYS_FIELDS WHERE INDEX_ID=ixid;\n"
  3221. " DELETE FROM SYS_INDEXES WHERE CURRENT OF index_cur;\n"
  3222. " END IF;\n"
  3223. "END LOOP;\n"
  3224. "CLOSE index_cur;\n"
  3225. "END;\n";
  3226. dberr_t error;
  3227. pars_info_t* info;
  3228. ut_ad(!srv_read_only_mode);
  3229. ut_ad(mutex_own(&dict_sys->mutex));
  3230. ut_ad(trx->dict_operation_lock_mode == RW_X_LATCH);
  3231. ut_ad(trx_get_dict_operation(trx) == TRX_DICT_OP_INDEX);
  3232. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_X));
  3233. /* It is possible that table->n_ref_count > 1 when
  3234. locked=TRUE. In this case, all code that should have an open
  3235. handle to the table be waiting for the next statement to execute,
  3236. or waiting for a meta-data lock.
  3237. A concurrent purge will be prevented by dict_operation_lock. */
  3238. info = pars_info_create();
  3239. pars_info_add_ull_literal(info, "tableid", table_id);
  3240. trx->op_info = "dropping indexes";
  3241. error = que_eval_sql(info, sql, FALSE, trx);
  3242. switch (error) {
  3243. case DB_SUCCESS:
  3244. break;
  3245. default:
  3246. /* Even though we ensure that DDL transactions are WAIT
  3247. and DEADLOCK free, we could encounter other errors e.g.,
  3248. DB_TOO_MANY_CONCURRENT_TRXS. */
  3249. ib::error() << "row_merge_drop_indexes_dict failed with error "
  3250. << error;
  3251. /* fall through */
  3252. case DB_TOO_MANY_CONCURRENT_TRXS:
  3253. trx->error_state = DB_SUCCESS;
  3254. }
  3255. trx->op_info = "";
  3256. }
  3257. /** Drop indexes that were created before an error occurred.
  3258. The data dictionary must have been locked exclusively by the caller,
  3259. because the transaction will not be committed.
  3260. @param trx dictionary transaction
  3261. @param table table containing the indexes
  3262. @param locked True if table is locked,
  3263. false - may need to do lazy drop
  3264. @param alter_trx Alter table transaction */
  3265. void
  3266. row_merge_drop_indexes(
  3267. trx_t* trx,
  3268. dict_table_t* table,
  3269. bool locked,
  3270. const trx_t* alter_trx)
  3271. {
  3272. dict_index_t* index;
  3273. dict_index_t* next_index;
  3274. ut_ad(!srv_read_only_mode);
  3275. ut_ad(mutex_own(&dict_sys->mutex));
  3276. ut_ad(trx->dict_operation_lock_mode == RW_X_LATCH);
  3277. ut_ad(trx_get_dict_operation(trx) == TRX_DICT_OP_INDEX);
  3278. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_X));
  3279. index = dict_table_get_first_index(table);
  3280. ut_ad(dict_index_is_clust(index));
  3281. ut_ad(dict_index_get_online_status(index) == ONLINE_INDEX_COMPLETE);
  3282. /* the caller should have an open handle to the table */
  3283. ut_ad(table->get_ref_count() >= 1);
  3284. /* It is possible that table->n_ref_count > 1 when
  3285. locked=TRUE. In this case, all code that should have an open
  3286. handle to the table be waiting for the next statement to execute,
  3287. or waiting for a meta-data lock.
  3288. A concurrent purge will be prevented by dict_operation_lock. */
  3289. if (!locked && (table->get_ref_count() > 1
  3290. || table->has_lock_other_than(alter_trx))) {
  3291. /* We will have to drop the indexes later, when the
  3292. table is guaranteed to be no longer in use. Mark the
  3293. indexes as incomplete and corrupted, so that other
  3294. threads will stop using them. Let dict_table_close()
  3295. or crash recovery or the next invocation of
  3296. prepare_inplace_alter_table() take care of dropping
  3297. the indexes. */
  3298. while ((index = dict_table_get_next_index(index)) != NULL) {
  3299. ut_ad(!dict_index_is_clust(index));
  3300. switch (dict_index_get_online_status(index)) {
  3301. case ONLINE_INDEX_ABORTED_DROPPED:
  3302. continue;
  3303. case ONLINE_INDEX_COMPLETE:
  3304. if (index->is_committed()) {
  3305. /* Do nothing to already
  3306. published indexes. */
  3307. } else if (index->type & DICT_FTS) {
  3308. /* Drop a completed FULLTEXT
  3309. index, due to a timeout during
  3310. MDL upgrade for
  3311. commit_inplace_alter_table().
  3312. Because only concurrent reads
  3313. are allowed (and they are not
  3314. seeing this index yet) we
  3315. are safe to drop the index. */
  3316. dict_index_t* prev = UT_LIST_GET_PREV(
  3317. indexes, index);
  3318. /* At least there should be
  3319. the clustered index before
  3320. this one. */
  3321. ut_ad(prev);
  3322. ut_a(table->fts);
  3323. fts_drop_index(table, index, trx);
  3324. row_merge_drop_index_dict(
  3325. trx, index->id);
  3326. /* We can remove a DICT_FTS
  3327. index from the cache, because
  3328. we do not allow ADD FULLTEXT INDEX
  3329. with LOCK=NONE. If we allowed that,
  3330. we should exclude FTS entries from
  3331. prebuilt->ins_node->entry_list
  3332. in ins_node_create_entry_list(). */
  3333. #ifdef BTR_CUR_HASH_ADAPT
  3334. ut_ad(!index->search_info->ref_count);
  3335. #endif /* BTR_CUR_HASH_ADAPT */
  3336. dict_index_remove_from_cache(
  3337. table, index);
  3338. index = prev;
  3339. } else {
  3340. rw_lock_x_lock(
  3341. dict_index_get_lock(index));
  3342. dict_index_set_online_status(
  3343. index, ONLINE_INDEX_ABORTED);
  3344. index->type |= DICT_CORRUPT;
  3345. table->drop_aborted = TRUE;
  3346. goto drop_aborted;
  3347. }
  3348. continue;
  3349. case ONLINE_INDEX_CREATION:
  3350. rw_lock_x_lock(dict_index_get_lock(index));
  3351. ut_ad(!index->is_committed());
  3352. row_log_abort_sec(index);
  3353. drop_aborted:
  3354. rw_lock_x_unlock(dict_index_get_lock(index));
  3355. DEBUG_SYNC_C("merge_drop_index_after_abort");
  3356. /* covered by dict_sys->mutex */
  3357. MONITOR_INC(MONITOR_BACKGROUND_DROP_INDEX);
  3358. /* fall through */
  3359. case ONLINE_INDEX_ABORTED:
  3360. /* Drop the index tree from the
  3361. data dictionary and free it from
  3362. the tablespace, but keep the object
  3363. in the data dictionary cache. */
  3364. row_merge_drop_index_dict(trx, index->id);
  3365. rw_lock_x_lock(dict_index_get_lock(index));
  3366. dict_index_set_online_status(
  3367. index, ONLINE_INDEX_ABORTED_DROPPED);
  3368. rw_lock_x_unlock(dict_index_get_lock(index));
  3369. table->drop_aborted = TRUE;
  3370. continue;
  3371. }
  3372. ut_error;
  3373. }
  3374. fts_clear_all(table, trx);
  3375. return;
  3376. }
  3377. row_merge_drop_indexes_dict(trx, table->id);
  3378. /* Invalidate all row_prebuilt_t::ins_graph that are referring
  3379. to this table. That is, force row_get_prebuilt_insert_row() to
  3380. rebuild prebuilt->ins_node->entry_list). */
  3381. ut_ad(table->def_trx_id <= trx->id);
  3382. table->def_trx_id = trx->id;
  3383. next_index = dict_table_get_next_index(index);
  3384. while ((index = next_index) != NULL) {
  3385. /* read the next pointer before freeing the index */
  3386. next_index = dict_table_get_next_index(index);
  3387. ut_ad(!dict_index_is_clust(index));
  3388. if (!index->is_committed()) {
  3389. /* If it is FTS index, drop from table->fts
  3390. and also drop its auxiliary tables */
  3391. if (index->type & DICT_FTS) {
  3392. ut_a(table->fts);
  3393. fts_drop_index(table, index, trx);
  3394. }
  3395. switch (dict_index_get_online_status(index)) {
  3396. case ONLINE_INDEX_CREATION:
  3397. /* This state should only be possible
  3398. when prepare_inplace_alter_table() fails
  3399. after invoking row_merge_create_index().
  3400. In inplace_alter_table(),
  3401. row_merge_build_indexes()
  3402. should never leave the index in this state.
  3403. It would invoke row_log_abort_sec() on
  3404. failure. */
  3405. case ONLINE_INDEX_COMPLETE:
  3406. /* In these cases, we are able to drop
  3407. the index straight. The DROP INDEX was
  3408. never deferred. */
  3409. break;
  3410. case ONLINE_INDEX_ABORTED:
  3411. case ONLINE_INDEX_ABORTED_DROPPED:
  3412. /* covered by dict_sys->mutex */
  3413. MONITOR_DEC(MONITOR_BACKGROUND_DROP_INDEX);
  3414. }
  3415. dict_index_remove_from_cache(table, index);
  3416. }
  3417. }
  3418. fts_clear_all(table, trx);
  3419. table->drop_aborted = FALSE;
  3420. ut_d(dict_table_check_for_dup_indexes(table, CHECK_ALL_COMPLETE));
  3421. }
  3422. /*********************************************************************//**
  3423. Drop all partially created indexes during crash recovery. */
  3424. void
  3425. row_merge_drop_temp_indexes(void)
  3426. /*=============================*/
  3427. {
  3428. static const char sql[] =
  3429. "PROCEDURE DROP_TEMP_INDEXES_PROC () IS\n"
  3430. "ixid CHAR;\n"
  3431. "found INT;\n"
  3432. "DECLARE CURSOR index_cur IS\n"
  3433. " SELECT ID FROM SYS_INDEXES\n"
  3434. " WHERE SUBSTR(NAME,0,1)='" TEMP_INDEX_PREFIX_STR "'\n"
  3435. "FOR UPDATE;\n"
  3436. "BEGIN\n"
  3437. "found := 1;\n"
  3438. "OPEN index_cur;\n"
  3439. "WHILE found = 1 LOOP\n"
  3440. " FETCH index_cur INTO ixid;\n"
  3441. " IF (SQL % NOTFOUND) THEN\n"
  3442. " found := 0;\n"
  3443. " ELSE\n"
  3444. " DELETE FROM SYS_FIELDS WHERE INDEX_ID=ixid;\n"
  3445. " DELETE FROM SYS_INDEXES WHERE CURRENT OF index_cur;\n"
  3446. " END IF;\n"
  3447. "END LOOP;\n"
  3448. "CLOSE index_cur;\n"
  3449. "END;\n";
  3450. trx_t* trx;
  3451. dberr_t error;
  3452. /* Load the table definitions that contain partially defined
  3453. indexes, so that the data dictionary information can be checked
  3454. when accessing the tablename.ibd files. */
  3455. trx = trx_create();
  3456. trx->op_info = "dropping partially created indexes";
  3457. row_mysql_lock_data_dictionary(trx);
  3458. /* Ensure that this transaction will be rolled back and locks
  3459. will be released, if the server gets killed before the commit
  3460. gets written to the redo log. */
  3461. trx_set_dict_operation(trx, TRX_DICT_OP_INDEX);
  3462. trx->op_info = "dropping indexes";
  3463. error = que_eval_sql(NULL, sql, FALSE, trx);
  3464. if (error != DB_SUCCESS) {
  3465. /* Even though we ensure that DDL transactions are WAIT
  3466. and DEADLOCK free, we could encounter other errors e.g.,
  3467. DB_TOO_MANY_CONCURRENT_TRXS. */
  3468. trx->error_state = DB_SUCCESS;
  3469. ib::error() << "row_merge_drop_temp_indexes failed with error"
  3470. << error;
  3471. }
  3472. trx_commit_for_mysql(trx);
  3473. row_mysql_unlock_data_dictionary(trx);
  3474. trx->free();
  3475. }
  3476. /** Create temporary merge files in the given paramater path, and if
  3477. UNIV_PFS_IO defined, register the file descriptor with Performance Schema.
  3478. @param[in] path location for creating temporary merge files, or NULL
  3479. @return File descriptor */
  3480. pfs_os_file_t
  3481. row_merge_file_create_low(
  3482. const char* path)
  3483. {
  3484. #ifdef UNIV_PFS_IO
  3485. /* This temp file open does not go through normal
  3486. file APIs, add instrumentation to register with
  3487. performance schema */
  3488. struct PSI_file_locker* locker;
  3489. PSI_file_locker_state state;
  3490. if (!path) {
  3491. path = mysql_tmpdir;
  3492. }
  3493. static const char label[] = "/Innodb Merge Temp File";
  3494. char* name = static_cast<char*>(
  3495. ut_malloc_nokey(strlen(path) + sizeof label));
  3496. strcpy(name, path);
  3497. strcat(name, label);
  3498. register_pfs_file_open_begin(
  3499. &state, locker, innodb_temp_file_key,
  3500. PSI_FILE_CREATE, path ? name : label, __FILE__, __LINE__);
  3501. #endif
  3502. pfs_os_file_t fd = innobase_mysql_tmpfile(path);
  3503. #ifdef UNIV_PFS_IO
  3504. register_pfs_file_open_end(locker, fd,
  3505. (fd == OS_FILE_CLOSED)?NULL:&fd);
  3506. ut_free(name);
  3507. #endif
  3508. if (fd == OS_FILE_CLOSED) {
  3509. ib::error() << "Cannot create temporary merge file";
  3510. }
  3511. return(fd);
  3512. }
  3513. /** Create a merge file in the given location.
  3514. @param[out] merge_file merge file structure
  3515. @param[in] path location for creating temporary file, or NULL
  3516. @return file descriptor, or OS_FILE_CLOSED on error */
  3517. pfs_os_file_t
  3518. row_merge_file_create(
  3519. merge_file_t* merge_file,
  3520. const char* path)
  3521. {
  3522. merge_file->fd = row_merge_file_create_low(path);
  3523. merge_file->offset = 0;
  3524. merge_file->n_rec = 0;
  3525. if (merge_file->fd != OS_FILE_CLOSED) {
  3526. if (srv_disable_sort_file_cache) {
  3527. os_file_set_nocache(merge_file->fd,
  3528. "row0merge.cc", "sort");
  3529. }
  3530. }
  3531. return(merge_file->fd);
  3532. }
  3533. /*********************************************************************//**
  3534. Destroy a merge file. And de-register the file from Performance Schema
  3535. if UNIV_PFS_IO is defined. */
  3536. void
  3537. row_merge_file_destroy_low(
  3538. /*=======================*/
  3539. const pfs_os_file_t& fd) /*!< in: merge file descriptor */
  3540. {
  3541. if (fd != OS_FILE_CLOSED) {
  3542. os_file_close(fd);
  3543. }
  3544. }
  3545. /*********************************************************************//**
  3546. Destroy a merge file. */
  3547. void
  3548. row_merge_file_destroy(
  3549. /*===================*/
  3550. merge_file_t* merge_file) /*!< in/out: merge file structure */
  3551. {
  3552. ut_ad(!srv_read_only_mode);
  3553. if (merge_file->fd != OS_FILE_CLOSED) {
  3554. row_merge_file_destroy_low(merge_file->fd);
  3555. merge_file->fd = OS_FILE_CLOSED;
  3556. }
  3557. }
  3558. /*********************************************************************//**
  3559. Rename an index in the dictionary that was created. The data
  3560. dictionary must have been locked exclusively by the caller, because
  3561. the transaction will not be committed.
  3562. @return DB_SUCCESS if all OK */
  3563. dberr_t
  3564. row_merge_rename_index_to_add(
  3565. /*==========================*/
  3566. trx_t* trx, /*!< in/out: transaction */
  3567. table_id_t table_id, /*!< in: table identifier */
  3568. index_id_t index_id) /*!< in: index identifier */
  3569. {
  3570. dberr_t err = DB_SUCCESS;
  3571. pars_info_t* info = pars_info_create();
  3572. /* We use the private SQL parser of Innobase to generate the
  3573. query graphs needed in renaming indexes. */
  3574. static const char rename_index[] =
  3575. "PROCEDURE RENAME_INDEX_PROC () IS\n"
  3576. "BEGIN\n"
  3577. "UPDATE SYS_INDEXES SET NAME=SUBSTR(NAME,1,LENGTH(NAME)-1)\n"
  3578. "WHERE TABLE_ID = :tableid AND ID = :indexid;\n"
  3579. "END;\n";
  3580. ut_a(trx->dict_operation_lock_mode == RW_X_LATCH);
  3581. ut_ad(trx_get_dict_operation(trx) == TRX_DICT_OP_INDEX);
  3582. trx->op_info = "renaming index to add";
  3583. pars_info_add_ull_literal(info, "tableid", table_id);
  3584. pars_info_add_ull_literal(info, "indexid", index_id);
  3585. err = que_eval_sql(info, rename_index, FALSE, trx);
  3586. if (err != DB_SUCCESS) {
  3587. /* Even though we ensure that DDL transactions are WAIT
  3588. and DEADLOCK free, we could encounter other errors e.g.,
  3589. DB_TOO_MANY_CONCURRENT_TRXS. */
  3590. trx->error_state = DB_SUCCESS;
  3591. ib::error() << "row_merge_rename_index_to_add failed with"
  3592. " error " << err;
  3593. }
  3594. trx->op_info = "";
  3595. return(err);
  3596. }
  3597. /*********************************************************************//**
  3598. Rename an index in the dictionary that is to be dropped. The data
  3599. dictionary must have been locked exclusively by the caller, because
  3600. the transaction will not be committed.
  3601. @return DB_SUCCESS if all OK */
  3602. dberr_t
  3603. row_merge_rename_index_to_drop(
  3604. /*===========================*/
  3605. trx_t* trx, /*!< in/out: transaction */
  3606. table_id_t table_id, /*!< in: table identifier */
  3607. index_id_t index_id) /*!< in: index identifier */
  3608. {
  3609. dberr_t err;
  3610. pars_info_t* info = pars_info_create();
  3611. ut_ad(!srv_read_only_mode);
  3612. /* We use the private SQL parser of Innobase to generate the
  3613. query graphs needed in renaming indexes. */
  3614. static const char rename_index[] =
  3615. "PROCEDURE RENAME_INDEX_PROC () IS\n"
  3616. "BEGIN\n"
  3617. "UPDATE SYS_INDEXES SET NAME=CONCAT('"
  3618. TEMP_INDEX_PREFIX_STR "',NAME)\n"
  3619. "WHERE TABLE_ID = :tableid AND ID = :indexid;\n"
  3620. "END;\n";
  3621. ut_a(trx->dict_operation_lock_mode == RW_X_LATCH);
  3622. ut_ad(trx_get_dict_operation(trx) == TRX_DICT_OP_INDEX);
  3623. trx->op_info = "renaming index to drop";
  3624. pars_info_add_ull_literal(info, "tableid", table_id);
  3625. pars_info_add_ull_literal(info, "indexid", index_id);
  3626. err = que_eval_sql(info, rename_index, FALSE, trx);
  3627. if (err != DB_SUCCESS) {
  3628. /* Even though we ensure that DDL transactions are WAIT
  3629. and DEADLOCK free, we could encounter other errors e.g.,
  3630. DB_TOO_MANY_CONCURRENT_TRXS. */
  3631. trx->error_state = DB_SUCCESS;
  3632. ib::error() << "row_merge_rename_index_to_drop failed with"
  3633. " error " << err;
  3634. }
  3635. trx->op_info = "";
  3636. return(err);
  3637. }
  3638. /*********************************************************************//**
  3639. Provide a new pathname for a table that is being renamed if it belongs to
  3640. a file-per-table tablespace. The caller is responsible for freeing the
  3641. memory allocated for the return value.
  3642. @return new pathname of tablespace file, or NULL if space = 0 */
  3643. static
  3644. char*
  3645. row_make_new_pathname(
  3646. /*==================*/
  3647. dict_table_t* table, /*!< in: table to be renamed */
  3648. const char* new_name) /*!< in: new name */
  3649. {
  3650. ut_ad(!is_system_tablespace(table->space_id));
  3651. return os_file_make_new_pathname(table->space->chain.start->name,
  3652. new_name);
  3653. }
  3654. /*********************************************************************//**
  3655. Rename the tables in the data dictionary. The data dictionary must
  3656. have been locked exclusively by the caller, because the transaction
  3657. will not be committed.
  3658. @return error code or DB_SUCCESS */
  3659. dberr_t
  3660. row_merge_rename_tables_dict(
  3661. /*=========================*/
  3662. dict_table_t* old_table, /*!< in/out: old table, renamed to
  3663. tmp_name */
  3664. dict_table_t* new_table, /*!< in/out: new table, renamed to
  3665. old_table->name */
  3666. const char* tmp_name, /*!< in: new name for old_table */
  3667. trx_t* trx) /*!< in/out: dictionary transaction */
  3668. {
  3669. dberr_t err = DB_ERROR;
  3670. pars_info_t* info;
  3671. ut_ad(!srv_read_only_mode);
  3672. ut_ad(old_table != new_table);
  3673. ut_ad(mutex_own(&dict_sys->mutex));
  3674. ut_a(trx->dict_operation_lock_mode == RW_X_LATCH);
  3675. ut_ad(trx_get_dict_operation(trx) == TRX_DICT_OP_TABLE
  3676. || trx_get_dict_operation(trx) == TRX_DICT_OP_INDEX);
  3677. trx->op_info = "renaming tables";
  3678. /* We use the private SQL parser of Innobase to generate the query
  3679. graphs needed in updating the dictionary data in system tables. */
  3680. info = pars_info_create();
  3681. pars_info_add_str_literal(info, "new_name", new_table->name.m_name);
  3682. pars_info_add_str_literal(info, "old_name", old_table->name.m_name);
  3683. pars_info_add_str_literal(info, "tmp_name", tmp_name);
  3684. err = que_eval_sql(info,
  3685. "PROCEDURE RENAME_TABLES () IS\n"
  3686. "BEGIN\n"
  3687. "UPDATE SYS_TABLES SET NAME = :tmp_name\n"
  3688. " WHERE NAME = :old_name;\n"
  3689. "UPDATE SYS_TABLES SET NAME = :old_name\n"
  3690. " WHERE NAME = :new_name;\n"
  3691. "END;\n", FALSE, trx);
  3692. /* Update SYS_TABLESPACES and SYS_DATAFILES if the old table being
  3693. renamed is a single-table tablespace, which must be implicitly
  3694. renamed along with the table. */
  3695. if (err == DB_SUCCESS
  3696. && old_table->space_id) {
  3697. /* Make pathname to update SYS_DATAFILES. */
  3698. char* tmp_path = row_make_new_pathname(old_table, tmp_name);
  3699. info = pars_info_create();
  3700. pars_info_add_str_literal(info, "tmp_name", tmp_name);
  3701. pars_info_add_str_literal(info, "tmp_path", tmp_path);
  3702. pars_info_add_int4_literal(info, "old_space",
  3703. old_table->space_id);
  3704. err = que_eval_sql(info,
  3705. "PROCEDURE RENAME_OLD_SPACE () IS\n"
  3706. "BEGIN\n"
  3707. "UPDATE SYS_TABLESPACES"
  3708. " SET NAME = :tmp_name\n"
  3709. " WHERE SPACE = :old_space;\n"
  3710. "UPDATE SYS_DATAFILES"
  3711. " SET PATH = :tmp_path\n"
  3712. " WHERE SPACE = :old_space;\n"
  3713. "END;\n", FALSE, trx);
  3714. ut_free(tmp_path);
  3715. }
  3716. /* Update SYS_TABLESPACES and SYS_DATAFILES if the new table being
  3717. renamed is a single-table tablespace, which must be implicitly
  3718. renamed along with the table. */
  3719. if (err == DB_SUCCESS
  3720. && dict_table_is_file_per_table(new_table)) {
  3721. /* Make pathname to update SYS_DATAFILES. */
  3722. char* old_path = row_make_new_pathname(
  3723. new_table, old_table->name.m_name);
  3724. info = pars_info_create();
  3725. pars_info_add_str_literal(info, "old_name",
  3726. old_table->name.m_name);
  3727. pars_info_add_str_literal(info, "old_path", old_path);
  3728. pars_info_add_int4_literal(info, "new_space",
  3729. new_table->space_id);
  3730. err = que_eval_sql(info,
  3731. "PROCEDURE RENAME_NEW_SPACE () IS\n"
  3732. "BEGIN\n"
  3733. "UPDATE SYS_TABLESPACES"
  3734. " SET NAME = :old_name\n"
  3735. " WHERE SPACE = :new_space;\n"
  3736. "UPDATE SYS_DATAFILES"
  3737. " SET PATH = :old_path\n"
  3738. " WHERE SPACE = :new_space;\n"
  3739. "END;\n", FALSE, trx);
  3740. ut_free(old_path);
  3741. }
  3742. if (err == DB_SUCCESS && (new_table->flags2 & DICT_TF2_DISCARDED)) {
  3743. err = row_import_update_discarded_flag(
  3744. trx, new_table->id, true);
  3745. }
  3746. trx->op_info = "";
  3747. return(err);
  3748. }
  3749. /** Create the index and load in to the dictionary.
  3750. @param[in,out] table the index is on this table
  3751. @param[in] index_def the index definition
  3752. @param[in] add_v new virtual columns added along with add
  3753. index call
  3754. @return index, or NULL on error */
  3755. dict_index_t*
  3756. row_merge_create_index(
  3757. dict_table_t* table,
  3758. const index_def_t* index_def,
  3759. const dict_add_v_col_t* add_v)
  3760. {
  3761. dict_index_t* index;
  3762. ulint n_fields = index_def->n_fields;
  3763. ulint i;
  3764. ulint n_add_vcol = 0;
  3765. DBUG_ENTER("row_merge_create_index");
  3766. ut_ad(!srv_read_only_mode);
  3767. /* Create the index prototype, using the passed in def, this is not
  3768. a persistent operation. We pass 0 as the space id, and determine at
  3769. a lower level the space id where to store the table. */
  3770. index = dict_mem_index_create(table, index_def->name,
  3771. index_def->ind_type, n_fields);
  3772. index->set_committed(index_def->rebuild);
  3773. for (i = 0; i < n_fields; i++) {
  3774. const char* name;
  3775. index_field_t* ifield = &index_def->fields[i];
  3776. if (ifield->is_v_col) {
  3777. if (ifield->col_no >= table->n_v_def) {
  3778. ut_ad(ifield->col_no < table->n_v_def
  3779. + add_v->n_v_col);
  3780. ut_ad(ifield->col_no >= table->n_v_def);
  3781. name = add_v->v_col_name[
  3782. ifield->col_no - table->n_v_def];
  3783. n_add_vcol++;
  3784. } else {
  3785. name = dict_table_get_v_col_name(
  3786. table, ifield->col_no);
  3787. }
  3788. } else {
  3789. name = dict_table_get_col_name(table, ifield->col_no);
  3790. }
  3791. dict_mem_index_add_field(index, name, ifield->prefix_len);
  3792. }
  3793. if (n_add_vcol) {
  3794. index->assign_new_v_col(n_add_vcol);
  3795. }
  3796. DBUG_RETURN(index);
  3797. }
  3798. /*********************************************************************//**
  3799. Check if a transaction can use an index. */
  3800. bool
  3801. row_merge_is_index_usable(
  3802. /*======================*/
  3803. const trx_t* trx, /*!< in: transaction */
  3804. const dict_index_t* index) /*!< in: index to check */
  3805. {
  3806. if (!index->is_primary()
  3807. && dict_index_is_online_ddl(index)) {
  3808. /* Indexes that are being created are not useable. */
  3809. return(false);
  3810. }
  3811. return(!index->is_corrupted()
  3812. && (index->table->is_temporary() || index->table->no_rollback()
  3813. || index->trx_id == 0
  3814. || !trx->read_view.is_open()
  3815. || trx->read_view.changes_visible(
  3816. index->trx_id,
  3817. index->table->name)));
  3818. }
  3819. /*********************************************************************//**
  3820. Drop a table. The caller must have ensured that the background stats
  3821. thread is not processing the table. This can be done by calling
  3822. dict_stats_wait_bg_to_stop_using_table() after locking the dictionary and
  3823. before calling this function.
  3824. @return DB_SUCCESS or error code */
  3825. dberr_t
  3826. row_merge_drop_table(
  3827. /*=================*/
  3828. trx_t* trx, /*!< in: transaction */
  3829. dict_table_t* table) /*!< in: table to drop */
  3830. {
  3831. ut_ad(!srv_read_only_mode);
  3832. /* There must be no open transactions on the table. */
  3833. ut_a(table->get_ref_count() == 0);
  3834. return(row_drop_table_for_mysql(table->name.m_name,
  3835. trx, SQLCOM_DROP_TABLE, false, false));
  3836. }
  3837. /** Write an MLOG_INDEX_LOAD record to indicate in the redo-log
  3838. that redo-logging of individual index pages was disabled, and
  3839. the flushing of such pages to the data files was completed.
  3840. @param[in] index an index tree on which redo logging was disabled */
  3841. void row_merge_write_redo(const dict_index_t* index)
  3842. {
  3843. ut_ad(!index->table->is_temporary());
  3844. ut_ad(!(index->type & (DICT_SPATIAL | DICT_FTS)));
  3845. mtr_t mtr;
  3846. mtr.start();
  3847. byte* log_ptr = mlog_open(&mtr, 11 + 8);
  3848. log_ptr = mlog_write_initial_log_record_low(
  3849. MLOG_INDEX_LOAD,
  3850. index->table->space_id, index->page, log_ptr, &mtr);
  3851. mach_write_to_8(log_ptr, index->id);
  3852. mlog_close(&mtr, log_ptr + 8);
  3853. mtr.commit();
  3854. }
  3855. /** Build indexes on a table by reading a clustered index, creating a temporary
  3856. file containing index entries, merge sorting these index entries and inserting
  3857. sorted index entries to indexes.
  3858. @param[in] trx transaction
  3859. @param[in] old_table table where rows are read from
  3860. @param[in] new_table table where indexes are created; identical to
  3861. old_table unless creating a PRIMARY KEY
  3862. @param[in] online true if creating indexes online
  3863. @param[in] indexes indexes to be created
  3864. @param[in] key_numbers MySQL key numbers
  3865. @param[in] n_indexes size of indexes[]
  3866. @param[in,out] table MySQL table, for reporting erroneous key value
  3867. if applicable
  3868. @param[in] defaults default values of added, changed columns, or NULL
  3869. @param[in] col_map mapping of old column numbers to new ones, or
  3870. NULL if old_table == new_table
  3871. @param[in] add_autoinc number of added AUTO_INCREMENT columns, or
  3872. ULINT_UNDEFINED if none is added
  3873. @param[in,out] sequence autoinc sequence
  3874. @param[in] skip_pk_sort whether the new PRIMARY KEY will follow
  3875. existing order
  3876. @param[in,out] stage performance schema accounting object, used by
  3877. ALTER TABLE. stage->begin_phase_read_pk() will be called at the beginning of
  3878. this function and it will be passed to other functions for further accounting.
  3879. @param[in] add_v new virtual columns added along with indexes
  3880. @param[in] eval_table mysql table used to evaluate virtual column
  3881. value, see innobase_get_computed_value().
  3882. @param[in] allow_not_null allow the conversion from null to not-null
  3883. @return DB_SUCCESS or error code */
  3884. dberr_t
  3885. row_merge_build_indexes(
  3886. trx_t* trx,
  3887. dict_table_t* old_table,
  3888. dict_table_t* new_table,
  3889. bool online,
  3890. dict_index_t** indexes,
  3891. const ulint* key_numbers,
  3892. ulint n_indexes,
  3893. struct TABLE* table,
  3894. const dtuple_t* defaults,
  3895. const ulint* col_map,
  3896. ulint add_autoinc,
  3897. ib_sequence_t& sequence,
  3898. bool skip_pk_sort,
  3899. ut_stage_alter_t* stage,
  3900. const dict_add_v_col_t* add_v,
  3901. struct TABLE* eval_table,
  3902. bool allow_not_null)
  3903. {
  3904. merge_file_t* merge_files;
  3905. row_merge_block_t* block;
  3906. ut_new_pfx_t block_pfx;
  3907. size_t block_size;
  3908. ut_new_pfx_t crypt_pfx;
  3909. row_merge_block_t* crypt_block = NULL;
  3910. ulint i;
  3911. ulint j;
  3912. dberr_t error;
  3913. pfs_os_file_t tmpfd = OS_FILE_CLOSED;
  3914. dict_index_t* fts_sort_idx = NULL;
  3915. fts_psort_t* psort_info = NULL;
  3916. fts_psort_t* merge_info = NULL;
  3917. int64_t sig_count = 0;
  3918. bool fts_psort_initiated = false;
  3919. double total_static_cost = 0;
  3920. double total_dynamic_cost = 0;
  3921. ulint total_index_blocks = 0;
  3922. double pct_cost=0;
  3923. double pct_progress=0;
  3924. DBUG_ENTER("row_merge_build_indexes");
  3925. ut_ad(!srv_read_only_mode);
  3926. ut_ad((old_table == new_table) == !col_map);
  3927. ut_ad(!defaults || col_map);
  3928. stage->begin_phase_read_pk(skip_pk_sort && new_table != old_table
  3929. ? n_indexes - 1
  3930. : n_indexes);
  3931. /* Allocate memory for merge file data structure and initialize
  3932. fields */
  3933. ut_allocator<row_merge_block_t> alloc(mem_key_row_merge_sort);
  3934. /* This will allocate "3 * srv_sort_buf_size" elements of type
  3935. row_merge_block_t. The latter is defined as byte. */
  3936. block_size = 3 * srv_sort_buf_size;
  3937. block = alloc.allocate_large(block_size, &block_pfx);
  3938. if (block == NULL) {
  3939. DBUG_RETURN(DB_OUT_OF_MEMORY);
  3940. }
  3941. crypt_pfx.m_size = 0; /* silence bogus -Wmaybe-uninitialized */
  3942. TRASH_ALLOC(&crypt_pfx, sizeof crypt_pfx);
  3943. if (log_tmp_is_encrypted()) {
  3944. crypt_block = static_cast<row_merge_block_t*>(
  3945. alloc.allocate_large(block_size,
  3946. &crypt_pfx));
  3947. if (crypt_block == NULL) {
  3948. DBUG_RETURN(DB_OUT_OF_MEMORY);
  3949. }
  3950. }
  3951. trx_start_if_not_started_xa(trx, true);
  3952. ulint n_merge_files = 0;
  3953. for (ulint i = 0; i < n_indexes; i++)
  3954. {
  3955. if (!dict_index_is_spatial(indexes[i])) {
  3956. n_merge_files++;
  3957. }
  3958. }
  3959. merge_files = static_cast<merge_file_t*>(
  3960. ut_malloc_nokey(n_merge_files * sizeof *merge_files));
  3961. /* Initialize all the merge file descriptors, so that we
  3962. don't call row_merge_file_destroy() on uninitialized
  3963. merge file descriptor */
  3964. for (i = 0; i < n_merge_files; i++) {
  3965. merge_files[i].fd = OS_FILE_CLOSED;
  3966. merge_files[i].offset = 0;
  3967. merge_files[i].n_rec = 0;
  3968. }
  3969. total_static_cost = COST_BUILD_INDEX_STATIC * n_indexes + COST_READ_CLUSTERED_INDEX;
  3970. total_dynamic_cost = COST_BUILD_INDEX_DYNAMIC * n_indexes;
  3971. for (i = 0; i < n_indexes; i++) {
  3972. if (indexes[i]->type & DICT_FTS) {
  3973. ibool opt_doc_id_size = FALSE;
  3974. /* To build FTS index, we would need to extract
  3975. doc's word, Doc ID, and word's position, so
  3976. we need to build a "fts sort index" indexing
  3977. on above three 'fields' */
  3978. fts_sort_idx = row_merge_create_fts_sort_index(
  3979. indexes[i], old_table, &opt_doc_id_size);
  3980. row_merge_dup_t* dup
  3981. = static_cast<row_merge_dup_t*>(
  3982. ut_malloc_nokey(sizeof *dup));
  3983. dup->index = fts_sort_idx;
  3984. dup->table = table;
  3985. dup->col_map = col_map;
  3986. dup->n_dup = 0;
  3987. /* This can fail e.g. if temporal files can't be
  3988. created */
  3989. if (!row_fts_psort_info_init(
  3990. trx, dup, new_table, opt_doc_id_size,
  3991. dict_table_page_size(old_table),
  3992. &psort_info, &merge_info)) {
  3993. error = DB_CORRUPTION;
  3994. goto func_exit;
  3995. }
  3996. /* We need to ensure that we free the resources
  3997. allocated */
  3998. fts_psort_initiated = true;
  3999. }
  4000. }
  4001. if (global_system_variables.log_warnings > 2) {
  4002. sql_print_information("InnoDB: Online DDL : Start reading"
  4003. " clustered index of the table"
  4004. " and create temporary files");
  4005. }
  4006. pct_cost = COST_READ_CLUSTERED_INDEX * 100 / (total_static_cost + total_dynamic_cost);
  4007. /* Do not continue if we can't encrypt table pages */
  4008. if (!old_table->is_readable() ||
  4009. !new_table->is_readable()) {
  4010. error = DB_DECRYPTION_FAILED;
  4011. ib_push_warning(trx->mysql_thd, DB_DECRYPTION_FAILED,
  4012. "Table %s is encrypted but encryption service or"
  4013. " used key_id is not available. "
  4014. " Can't continue reading table.",
  4015. !old_table->is_readable() ? old_table->name.m_name :
  4016. new_table->name.m_name);
  4017. goto func_exit;
  4018. }
  4019. /* Read clustered index of the table and create files for
  4020. secondary index entries for merge sort */
  4021. error = row_merge_read_clustered_index(
  4022. trx, table, old_table, new_table, online, indexes,
  4023. fts_sort_idx, psort_info, merge_files, key_numbers,
  4024. n_indexes, defaults, add_v, col_map, add_autoinc,
  4025. sequence, block, skip_pk_sort, &tmpfd, stage,
  4026. pct_cost, crypt_block, eval_table, allow_not_null);
  4027. stage->end_phase_read_pk();
  4028. pct_progress += pct_cost;
  4029. if (global_system_variables.log_warnings > 2) {
  4030. sql_print_information("InnoDB: Online DDL : End of reading "
  4031. "clustered index of the table"
  4032. " and create temporary files");
  4033. }
  4034. for (i = 0; i < n_merge_files; i++) {
  4035. total_index_blocks += merge_files[i].offset;
  4036. }
  4037. if (error != DB_SUCCESS) {
  4038. goto func_exit;
  4039. }
  4040. DEBUG_SYNC_C("row_merge_after_scan");
  4041. /* Now we have files containing index entries ready for
  4042. sorting and inserting. */
  4043. for (ulint k = 0, i = 0; i < n_indexes; i++) {
  4044. dict_index_t* sort_idx = indexes[i];
  4045. if (dict_index_is_spatial(sort_idx)) {
  4046. continue;
  4047. }
  4048. if (indexes[i]->type & DICT_FTS) {
  4049. os_event_t fts_parallel_merge_event;
  4050. sort_idx = fts_sort_idx;
  4051. fts_parallel_merge_event
  4052. = merge_info[0].psort_common->merge_event;
  4053. if (FTS_PLL_MERGE) {
  4054. ulint trial_count = 0;
  4055. bool all_exit = false;
  4056. os_event_reset(fts_parallel_merge_event);
  4057. row_fts_start_parallel_merge(merge_info);
  4058. wait_again:
  4059. os_event_wait_time_low(
  4060. fts_parallel_merge_event, 1000000,
  4061. sig_count);
  4062. for (j = 0; j < FTS_NUM_AUX_INDEX; j++) {
  4063. if (merge_info[j].child_status
  4064. != FTS_CHILD_COMPLETE
  4065. && merge_info[j].child_status
  4066. != FTS_CHILD_EXITING) {
  4067. sig_count = os_event_reset(
  4068. fts_parallel_merge_event);
  4069. goto wait_again;
  4070. }
  4071. }
  4072. /* Now all children should complete, wait
  4073. a bit until they all finish using event */
  4074. while (!all_exit && trial_count < 10000) {
  4075. all_exit = true;
  4076. for (j = 0; j < FTS_NUM_AUX_INDEX;
  4077. j++) {
  4078. if (merge_info[j].child_status
  4079. != FTS_CHILD_EXITING) {
  4080. all_exit = false;
  4081. os_thread_sleep(1000);
  4082. break;
  4083. }
  4084. }
  4085. trial_count++;
  4086. }
  4087. if (!all_exit) {
  4088. ib::error() << "Not all child merge"
  4089. " threads exited when creating"
  4090. " FTS index '"
  4091. << indexes[i]->name << "'";
  4092. } else {
  4093. for (j = 0; j < FTS_NUM_AUX_INDEX;
  4094. j++) {
  4095. os_thread_join(merge_info[j]
  4096. .thread_hdl);
  4097. }
  4098. }
  4099. } else {
  4100. /* This cannot report duplicates; an
  4101. assertion would fail in that case. */
  4102. error = row_fts_merge_insert(
  4103. sort_idx, new_table,
  4104. psort_info, 0);
  4105. }
  4106. #ifdef FTS_INTERNAL_DIAG_PRINT
  4107. DEBUG_FTS_SORT_PRINT("FTS_SORT: Complete Insert\n");
  4108. #endif
  4109. } else if (merge_files[k].fd != OS_FILE_CLOSED) {
  4110. char buf[NAME_LEN + 1];
  4111. row_merge_dup_t dup = {
  4112. sort_idx, table, col_map, 0};
  4113. pct_cost = (COST_BUILD_INDEX_STATIC +
  4114. (total_dynamic_cost * merge_files[k].offset /
  4115. total_index_blocks)) /
  4116. (total_static_cost + total_dynamic_cost)
  4117. * PCT_COST_MERGESORT_INDEX * 100;
  4118. char* bufend = innobase_convert_name(
  4119. buf, sizeof buf,
  4120. indexes[i]->name,
  4121. strlen(indexes[i]->name),
  4122. trx->mysql_thd);
  4123. buf[bufend - buf]='\0';
  4124. if (global_system_variables.log_warnings > 2) {
  4125. sql_print_information("InnoDB: Online DDL :"
  4126. " Start merge-sorting"
  4127. " index %s"
  4128. " (" ULINTPF
  4129. " / " ULINTPF "),"
  4130. " estimated cost :"
  4131. " %2.4f",
  4132. buf, i + 1, n_indexes,
  4133. pct_cost);
  4134. }
  4135. error = row_merge_sort(
  4136. trx, &dup, &merge_files[k],
  4137. block, &tmpfd, true,
  4138. pct_progress, pct_cost,
  4139. crypt_block, new_table->space_id,
  4140. stage);
  4141. pct_progress += pct_cost;
  4142. if (global_system_variables.log_warnings > 2) {
  4143. sql_print_information("InnoDB: Online DDL :"
  4144. " End of "
  4145. " merge-sorting index %s"
  4146. " (" ULINTPF
  4147. " / " ULINTPF ")",
  4148. buf, i + 1, n_indexes);
  4149. }
  4150. if (error == DB_SUCCESS) {
  4151. BtrBulk btr_bulk(sort_idx, trx,
  4152. trx->get_flush_observer());
  4153. pct_cost = (COST_BUILD_INDEX_STATIC +
  4154. (total_dynamic_cost * merge_files[k].offset /
  4155. total_index_blocks)) /
  4156. (total_static_cost + total_dynamic_cost) *
  4157. PCT_COST_INSERT_INDEX * 100;
  4158. if (global_system_variables.log_warnings > 2) {
  4159. sql_print_information(
  4160. "InnoDB: Online DDL : Start "
  4161. "building index %s"
  4162. " (" ULINTPF
  4163. " / " ULINTPF "), estimated "
  4164. "cost : %2.4f", buf, i + 1,
  4165. n_indexes, pct_cost);
  4166. }
  4167. error = row_merge_insert_index_tuples(
  4168. sort_idx, old_table,
  4169. merge_files[k].fd, block, NULL,
  4170. &btr_bulk,
  4171. merge_files[k].n_rec, pct_progress, pct_cost,
  4172. crypt_block, new_table->space_id,
  4173. stage);
  4174. error = btr_bulk.finish(error);
  4175. pct_progress += pct_cost;
  4176. if (global_system_variables.log_warnings > 2) {
  4177. sql_print_information(
  4178. "InnoDB: Online DDL : "
  4179. "End of building index %s"
  4180. " (" ULINTPF " / " ULINTPF ")",
  4181. buf, i + 1, n_indexes);
  4182. }
  4183. }
  4184. }
  4185. /* Close the temporary file to free up space. */
  4186. row_merge_file_destroy(&merge_files[k++]);
  4187. if (indexes[i]->type & DICT_FTS) {
  4188. row_fts_psort_info_destroy(psort_info, merge_info);
  4189. fts_psort_initiated = false;
  4190. } else if (dict_index_is_spatial(indexes[i])) {
  4191. /* We never disable redo logging for
  4192. creating SPATIAL INDEX. Avoid writing any
  4193. unnecessary MLOG_INDEX_LOAD record. */
  4194. } else if (old_table != new_table) {
  4195. ut_ad(!sort_idx->online_log);
  4196. ut_ad(sort_idx->online_status
  4197. == ONLINE_INDEX_COMPLETE);
  4198. } else if (FlushObserver* flush_observer =
  4199. trx->get_flush_observer()) {
  4200. if (error != DB_SUCCESS) {
  4201. flush_observer->interrupted();
  4202. }
  4203. flush_observer->flush();
  4204. row_merge_write_redo(indexes[i]);
  4205. }
  4206. if (old_table != new_table
  4207. || (indexes[i]->type & (DICT_FTS | DICT_SPATIAL))
  4208. || error != DB_SUCCESS || !online) {
  4209. /* Do not apply any online log. */
  4210. } else {
  4211. if (global_system_variables.log_warnings > 2) {
  4212. sql_print_information(
  4213. "InnoDB: Online DDL : Applying"
  4214. " log to index");
  4215. }
  4216. DEBUG_SYNC_C("row_log_apply_before");
  4217. error = row_log_apply(trx, sort_idx, table, stage);
  4218. DEBUG_SYNC_C("row_log_apply_after");
  4219. }
  4220. if (error != DB_SUCCESS) {
  4221. trx->error_key_num = key_numbers[i];
  4222. goto func_exit;
  4223. }
  4224. if (indexes[i]->type & DICT_FTS
  4225. && UNIV_UNLIKELY(fts_enable_diag_print)) {
  4226. ib::info() << "Finished building full-text index "
  4227. << indexes[i]->name;
  4228. }
  4229. }
  4230. func_exit:
  4231. DBUG_EXECUTE_IF(
  4232. "ib_build_indexes_too_many_concurrent_trxs",
  4233. error = DB_TOO_MANY_CONCURRENT_TRXS;
  4234. trx->error_state = error;);
  4235. if (fts_psort_initiated) {
  4236. /* Clean up FTS psort related resource */
  4237. row_fts_psort_info_destroy(psort_info, merge_info);
  4238. fts_psort_initiated = false;
  4239. }
  4240. row_merge_file_destroy_low(tmpfd);
  4241. for (i = 0; i < n_merge_files; i++) {
  4242. row_merge_file_destroy(&merge_files[i]);
  4243. }
  4244. if (fts_sort_idx) {
  4245. dict_mem_index_free(fts_sort_idx);
  4246. }
  4247. ut_free(merge_files);
  4248. alloc.deallocate_large(block, &block_pfx, block_size);
  4249. if (crypt_block) {
  4250. alloc.deallocate_large(crypt_block, &crypt_pfx, block_size);
  4251. }
  4252. DICT_TF2_FLAG_UNSET(new_table, DICT_TF2_FTS_ADD_DOC_ID);
  4253. if (online && old_table == new_table && error != DB_SUCCESS) {
  4254. /* On error, flag all online secondary index creation
  4255. as aborted. */
  4256. for (i = 0; i < n_indexes; i++) {
  4257. ut_ad(!(indexes[i]->type & DICT_FTS));
  4258. ut_ad(!indexes[i]->is_committed());
  4259. ut_ad(!dict_index_is_clust(indexes[i]));
  4260. /* Completed indexes should be dropped as
  4261. well, and indexes whose creation was aborted
  4262. should be dropped from the persistent
  4263. storage. However, at this point we can only
  4264. set some flags in the not-yet-published
  4265. indexes. These indexes will be dropped later
  4266. in row_merge_drop_indexes(), called by
  4267. rollback_inplace_alter_table(). */
  4268. switch (dict_index_get_online_status(indexes[i])) {
  4269. case ONLINE_INDEX_COMPLETE:
  4270. break;
  4271. case ONLINE_INDEX_CREATION:
  4272. rw_lock_x_lock(
  4273. dict_index_get_lock(indexes[i]));
  4274. row_log_abort_sec(indexes[i]);
  4275. indexes[i]->type |= DICT_CORRUPT;
  4276. rw_lock_x_unlock(
  4277. dict_index_get_lock(indexes[i]));
  4278. new_table->drop_aborted = TRUE;
  4279. /* fall through */
  4280. case ONLINE_INDEX_ABORTED_DROPPED:
  4281. case ONLINE_INDEX_ABORTED:
  4282. MONITOR_ATOMIC_INC(
  4283. MONITOR_BACKGROUND_DROP_INDEX);
  4284. }
  4285. }
  4286. }
  4287. DBUG_EXECUTE_IF("ib_index_crash_after_bulk_load", DBUG_SUICIDE(););
  4288. if (FlushObserver* flush_observer = trx->get_flush_observer()) {
  4289. DBUG_EXECUTE_IF("ib_index_build_fail_before_flush",
  4290. error = DB_INTERRUPTED;
  4291. );
  4292. if (error != DB_SUCCESS) {
  4293. flush_observer->interrupted();
  4294. }
  4295. flush_observer->flush();
  4296. if (old_table != new_table) {
  4297. for (const dict_index_t* index
  4298. = dict_table_get_first_index(new_table);
  4299. index != NULL;
  4300. index = dict_table_get_next_index(index)) {
  4301. if (!(index->type
  4302. & (DICT_FTS | DICT_SPATIAL))) {
  4303. row_merge_write_redo(index);
  4304. }
  4305. }
  4306. }
  4307. trx->remove_flush_observer();
  4308. if (trx_is_interrupted(trx)) {
  4309. error = DB_INTERRUPTED;
  4310. }
  4311. }
  4312. DBUG_RETURN(error);
  4313. }