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.

7265 lines
194 KiB

17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
11 years ago
16 years ago
16 years ago
16 years ago
16 years ago
14 years ago
14 years ago
14 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
11 years ago
16 years ago
16 years ago
11 years ago
16 years ago
16 years ago
11 years ago
10 years ago
16 years ago
16 years ago
16 years ago
11 years ago
16 years ago
16 years ago
11 years ago
16 years ago
16 years ago
11 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
14 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
14 years ago
14 years ago
10 years ago
10 years ago
16 years ago
16 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-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-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-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-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-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-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-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-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
16 years ago
16 years ago
16 years ago
16 years ago
14 years ago
17 years ago
17 years ago
17 years ago
14 years ago
17 years ago
10 years ago
16 years ago
16 years ago
10 years ago
10 years ago
MDEV-16283 ALTER TABLE...DISCARD TABLESPACE still takes long on a large buffer pool Also fixes MDEV-14727, MDEV-14491 InnoDB: Error: Waited for 5 secs for hash index ref_count (1) to drop to 0 by replacing the flawed wait logic in dict_index_remove_from_cache_low(). On DISCARD TABLESPACE, there is no need to drop the adaptive hash index. We must drop it on IMPORT TABLESPACE, and eventually on DROP TABLE or DROP INDEX. As long as the dict_index_t object remains in the cache and the table remains inaccessible, the adaptive hash index entries to orphaned pages would not do any harm. They would be dropped when buffer pool pages are reused for something else. btr_search_drop_page_hash_when_freed(), buf_LRU_drop_page_hash_batch(): Remove the parameter zip_size, and pass 0 to buf_page_get_gen(). buf_page_get_gen(): Ignore zip_size if mode==BUF_PEEK_IF_IN_POOL. buf_LRU_drop_page_hash_for_tablespace(): Drop the adaptive hash index even if the tablespace is inaccessible. buf_LRU_drop_page_hash_for_tablespace(): New global function, to drop the adaptive hash index. buf_LRU_flush_or_remove_pages(), fil_delete_tablespace(): Remove the parameter drop_ahi. dict_index_remove_from_cache_low(): Actively drop the adaptive hash index if entries exist. This should prevent InnoDB hangs on DROP TABLE or DROP INDEX. row_import_for_mysql(): Drop any adaptive hash index entries for the table. row_drop_table_for_mysql(): Drop any adaptive hash index for the table, except if the table resides in the system tablespace. (DISCARD TABLESPACE does not apply to the system tablespace, and we do no want to drop the adaptive hash index for other tables than the one that is being dropped.) row_truncate_table_for_mysql(): Drop any adaptive hash index entries for the table, except if the table resides in the system tablespace.
8 years ago
10 years ago
11 years ago
11 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
14 years ago
11 years ago
14 years ago
14 years ago
16 years ago
16 years ago
14 years ago
14 years ago
14 years ago
16 years ago
8 years ago
8 years ago
8 years ago
14 years ago
10 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
11 years ago
11 years ago
11 years ago
11 years ago
16 years ago
14 years ago
16 years ago
16 years ago
MDEV-16283 ALTER TABLE...DISCARD TABLESPACE still takes long on a large buffer pool Also fixes MDEV-14727, MDEV-14491 InnoDB: Error: Waited for 5 secs for hash index ref_count (1) to drop to 0 by replacing the flawed wait logic in dict_index_remove_from_cache_low(). On DISCARD TABLESPACE, there is no need to drop the adaptive hash index. We must drop it on IMPORT TABLESPACE, and eventually on DROP TABLE or DROP INDEX. As long as the dict_index_t object remains in the cache and the table remains inaccessible, the adaptive hash index entries to orphaned pages would not do any harm. They would be dropped when buffer pool pages are reused for something else. btr_search_drop_page_hash_when_freed(), buf_LRU_drop_page_hash_batch(): Remove the parameter zip_size, and pass 0 to buf_page_get_gen(). buf_page_get_gen(): Ignore zip_size if mode==BUF_PEEK_IF_IN_POOL. buf_LRU_drop_page_hash_for_tablespace(): Drop the adaptive hash index even if the tablespace is inaccessible. buf_LRU_drop_page_hash_for_tablespace(): New global function, to drop the adaptive hash index. buf_LRU_flush_or_remove_pages(), fil_delete_tablespace(): Remove the parameter drop_ahi. dict_index_remove_from_cache_low(): Actively drop the adaptive hash index if entries exist. This should prevent InnoDB hangs on DROP TABLE or DROP INDEX. row_import_for_mysql(): Drop any adaptive hash index entries for the table. row_drop_table_for_mysql(): Drop any adaptive hash index for the table, except if the table resides in the system tablespace. (DISCARD TABLESPACE does not apply to the system tablespace, and we do no want to drop the adaptive hash index for other tables than the one that is being dropped.) row_truncate_table_for_mysql(): Drop any adaptive hash index entries for the table, except if the table resides in the system tablespace.
8 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
11 years ago
16 years ago
16 years ago
10 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
14 years ago
14 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
14 years ago
16 years ago
14 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
14 years ago
16 years ago
14 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
14 years ago
16 years ago
16 years ago
16 years ago
11 years ago
16 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-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
16 years ago
11 years ago
11 years ago
16 years ago
9 years ago
9 years ago
16 years ago
16 years ago
11 years ago
16 years ago
16 years ago
11 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
14 years ago
16 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
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
16 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1996, 2017, Oracle and/or its affiliates. All Rights Reserved.
  3. Copyright (c) 2012, Facebook Inc.
  4. Copyright (c) 2013, 2019, MariaDB Corporation.
  5. This program is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free Software
  7. Foundation; version 2 of the License.
  8. This program is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  14. *****************************************************************************/
  15. /******************************************************************//**
  16. @file dict/dict0dict.cc
  17. Data dictionary system
  18. Created 1/8/1996 Heikki Tuuri
  19. ***********************************************************************/
  20. #include "dict0dict.h"
  21. #include "fts0fts.h"
  22. #include "fil0fil.h"
  23. #include <algorithm>
  24. #include <string>
  25. #ifdef UNIV_NONINL
  26. #include "dict0dict.ic"
  27. #include "dict0priv.ic"
  28. #endif
  29. /** dummy index for ROW_FORMAT=REDUNDANT supremum and infimum records */
  30. UNIV_INTERN dict_index_t* dict_ind_redundant;
  31. /** dummy index for ROW_FORMAT=COMPACT supremum and infimum records */
  32. UNIV_INTERN dict_index_t* dict_ind_compact;
  33. #if defined UNIV_DEBUG || defined UNIV_IBUF_DEBUG
  34. /** Flag to control insert buffer debugging. */
  35. extern UNIV_INTERN uint ibuf_debug;
  36. #endif /* UNIV_DEBUG || UNIV_IBUF_DEBUG */
  37. /**********************************************************************
  38. Issue a warning that the row is too big. */
  39. void
  40. ib_warn_row_too_big(const dict_table_t* table);
  41. #ifndef UNIV_HOTBACKUP
  42. #include "buf0buf.h"
  43. #include "data0type.h"
  44. #include "mach0data.h"
  45. #include "dict0boot.h"
  46. #include "dict0mem.h"
  47. #include "dict0crea.h"
  48. #include "dict0stats.h"
  49. #include "trx0undo.h"
  50. #include "btr0btr.h"
  51. #include "btr0cur.h"
  52. #include "btr0sea.h"
  53. #include "os0once.h"
  54. #include "page0zip.h"
  55. #include "page0page.h"
  56. #include "pars0pars.h"
  57. #include "pars0sym.h"
  58. #include "que0que.h"
  59. #include "rem0cmp.h"
  60. #include "fts0fts.h"
  61. #include "fts0types.h"
  62. #include "m_ctype.h" /* my_isspace() */
  63. #include "ha_prototypes.h" /* innobase_strcasecmp(), innobase_casedn_str() */
  64. #include "srv0mon.h"
  65. #include "srv0start.h"
  66. #include "lock0lock.h"
  67. #include "dict0priv.h"
  68. #include "row0upd.h"
  69. #include "row0mysql.h"
  70. #include "row0merge.h"
  71. #include "row0log.h"
  72. #include "ut0ut.h" /* ut_format_name() */
  73. #include "m_string.h"
  74. #include "my_sys.h"
  75. #include "mysqld.h" /* system_charset_info */
  76. #include "strfunc.h" /* strconvert() */
  77. #include <ctype.h>
  78. /** the dictionary system */
  79. UNIV_INTERN dict_sys_t* dict_sys = NULL;
  80. /** @brief the data dictionary rw-latch protecting dict_sys
  81. table create, drop, etc. reserve this in X-mode; implicit or
  82. backround operations purge, rollback, foreign key checks reserve this
  83. in S-mode; we cannot trust that MySQL protects implicit or background
  84. operations a table drop since MySQL does not know of them; therefore
  85. we need this; NOTE: a transaction which reserves this must keep book
  86. on the mode in trx_t::dict_operation_lock_mode */
  87. UNIV_INTERN rw_lock_t dict_operation_lock;
  88. /** Percentage of compression failures that are allowed in a single
  89. round */
  90. UNIV_INTERN ulong zip_failure_threshold_pct = 5;
  91. /** Maximum percentage of a page that can be allowed as a pad to avoid
  92. compression failures */
  93. UNIV_INTERN ulong zip_pad_max = 50;
  94. /* Keys to register rwlocks and mutexes with performance schema */
  95. #ifdef UNIV_PFS_RWLOCK
  96. UNIV_INTERN mysql_pfs_key_t dict_operation_lock_key;
  97. UNIV_INTERN mysql_pfs_key_t index_tree_rw_lock_key;
  98. UNIV_INTERN mysql_pfs_key_t index_online_log_key;
  99. UNIV_INTERN mysql_pfs_key_t dict_table_stats_key;
  100. #endif /* UNIV_PFS_RWLOCK */
  101. #ifdef UNIV_PFS_MUTEX
  102. UNIV_INTERN mysql_pfs_key_t zip_pad_mutex_key;
  103. UNIV_INTERN mysql_pfs_key_t dict_sys_mutex_key;
  104. UNIV_INTERN mysql_pfs_key_t dict_foreign_err_mutex_key;
  105. #endif /* UNIV_PFS_MUTEX */
  106. #define DICT_HEAP_SIZE 100 /*!< initial memory heap size when
  107. creating a table or index object */
  108. #define DICT_POOL_PER_TABLE_HASH 512 /*!< buffer pool max size per table
  109. hash table fixed size in bytes */
  110. #define DICT_POOL_PER_VARYING 4 /*!< buffer pool max size per data
  111. dictionary varying size in bytes */
  112. /** Identifies generated InnoDB foreign key names */
  113. static char dict_ibfk[] = "_ibfk_";
  114. bool innodb_table_stats_not_found = false;
  115. bool innodb_index_stats_not_found = false;
  116. static bool innodb_table_stats_not_found_reported = false;
  117. static bool innodb_index_stats_not_found_reported = false;
  118. /*******************************************************************//**
  119. Tries to find column names for the index and sets the col field of the
  120. index.
  121. @return TRUE if the column names were found */
  122. static
  123. ibool
  124. dict_index_find_cols(
  125. /*=================*/
  126. dict_table_t* table, /*!< in: table */
  127. dict_index_t* index); /*!< in: index */
  128. /*******************************************************************//**
  129. Builds the internal dictionary cache representation for a clustered
  130. index, containing also system fields not defined by the user.
  131. @return own: the internal representation of the clustered index */
  132. static
  133. dict_index_t*
  134. dict_index_build_internal_clust(
  135. /*============================*/
  136. const dict_table_t* table, /*!< in: table */
  137. dict_index_t* index); /*!< in: user representation of
  138. a clustered index */
  139. /*******************************************************************//**
  140. Builds the internal dictionary cache representation for a non-clustered
  141. index, containing also system fields not defined by the user.
  142. @return own: the internal representation of the non-clustered index */
  143. static
  144. dict_index_t*
  145. dict_index_build_internal_non_clust(
  146. /*================================*/
  147. const dict_table_t* table, /*!< in: table */
  148. dict_index_t* index); /*!< in: user representation of
  149. a non-clustered index */
  150. /**********************************************************************//**
  151. Builds the internal dictionary cache representation for an FTS index.
  152. @return own: the internal representation of the FTS index */
  153. static
  154. dict_index_t*
  155. dict_index_build_internal_fts(
  156. /*==========================*/
  157. dict_table_t* table, /*!< in: table */
  158. dict_index_t* index); /*!< in: user representation of an FTS index */
  159. /**********************************************************************//**
  160. Prints a column data. */
  161. static
  162. void
  163. dict_col_print_low(
  164. /*===============*/
  165. const dict_table_t* table, /*!< in: table */
  166. const dict_col_t* col); /*!< in: column */
  167. /**********************************************************************//**
  168. Prints an index data. */
  169. static
  170. void
  171. dict_index_print_low(
  172. /*=================*/
  173. dict_index_t* index); /*!< in: index */
  174. /**********************************************************************//**
  175. Prints a field data. */
  176. static
  177. void
  178. dict_field_print_low(
  179. /*=================*/
  180. const dict_field_t* field); /*!< in: field */
  181. /**********************************************************************//**
  182. Removes an index from the dictionary cache. */
  183. static
  184. void
  185. dict_index_remove_from_cache_low(
  186. /*=============================*/
  187. dict_table_t* table, /*!< in/out: table */
  188. dict_index_t* index, /*!< in, own: index */
  189. ibool lru_evict); /*!< in: TRUE if page being evicted
  190. to make room in the table LRU list */
  191. #ifdef UNIV_DEBUG
  192. /**********************************************************************//**
  193. Validate the dictionary table LRU list.
  194. @return TRUE if validate OK */
  195. static
  196. ibool
  197. dict_lru_validate(void);
  198. /*===================*/
  199. /**********************************************************************//**
  200. Check if table is in the dictionary table LRU list.
  201. @return TRUE if table found */
  202. static
  203. ibool
  204. dict_lru_find_table(
  205. /*================*/
  206. const dict_table_t* find_table); /*!< in: table to find */
  207. /**********************************************************************//**
  208. Check if a table exists in the dict table non-LRU list.
  209. @return TRUE if table found */
  210. static
  211. ibool
  212. dict_non_lru_find_table(
  213. /*====================*/
  214. const dict_table_t* find_table); /*!< in: table to find */
  215. #endif /* UNIV_DEBUG */
  216. /* Stream for storing detailed information about the latest foreign key
  217. and unique key errors. Only created if !srv_read_only_mode */
  218. UNIV_INTERN FILE* dict_foreign_err_file = NULL;
  219. /* mutex protecting the foreign and unique error buffers */
  220. UNIV_INTERN ib_mutex_t dict_foreign_err_mutex;
  221. /******************************************************************//**
  222. Makes all characters in a NUL-terminated UTF-8 string lower case. */
  223. UNIV_INTERN
  224. void
  225. dict_casedn_str(
  226. /*============*/
  227. char* a) /*!< in/out: string to put in lower case */
  228. {
  229. innobase_casedn_str(a);
  230. }
  231. /********************************************************************//**
  232. Checks if the database name in two table names is the same.
  233. @return TRUE if same db name */
  234. UNIV_INTERN
  235. ibool
  236. dict_tables_have_same_db(
  237. /*=====================*/
  238. const char* name1, /*!< in: table name in the form
  239. dbname '/' tablename */
  240. const char* name2) /*!< in: table name in the form
  241. dbname '/' tablename */
  242. {
  243. for (; *name1 == *name2; name1++, name2++) {
  244. if (*name1 == '/') {
  245. return(TRUE);
  246. }
  247. ut_a(*name1); /* the names must contain '/' */
  248. }
  249. return(FALSE);
  250. }
  251. /********************************************************************//**
  252. Return the end of table name where we have removed dbname and '/'.
  253. @return table name */
  254. UNIV_INTERN
  255. const char*
  256. dict_remove_db_name(
  257. /*================*/
  258. const char* name) /*!< in: table name in the form
  259. dbname '/' tablename */
  260. {
  261. const char* s = strchr(name, '/');
  262. ut_a(s);
  263. return(s + 1);
  264. }
  265. /********************************************************************//**
  266. Get the database name length in a table name.
  267. @return database name length */
  268. UNIV_INTERN
  269. ulint
  270. dict_get_db_name_len(
  271. /*=================*/
  272. const char* name) /*!< in: table name in the form
  273. dbname '/' tablename */
  274. {
  275. const char* s;
  276. s = strchr(name, '/');
  277. ut_a(s);
  278. return(s - name);
  279. }
  280. /********************************************************************//**
  281. Reserves the dictionary system mutex for MySQL. */
  282. UNIV_INTERN
  283. void
  284. dict_mutex_enter_for_mysql_func(const char * file, ulint line)
  285. /*============================*/
  286. {
  287. mutex_enter_func(&(dict_sys->mutex), file, line);
  288. }
  289. /********************************************************************//**
  290. Releases the dictionary system mutex for MySQL. */
  291. UNIV_INTERN
  292. void
  293. dict_mutex_exit_for_mysql(void)
  294. /*===========================*/
  295. {
  296. mutex_exit(&(dict_sys->mutex));
  297. }
  298. /** Allocate and init a dict_table_t's stats latch.
  299. This function must not be called concurrently on the same table object.
  300. @param[in,out] table_void table whose stats latch to create */
  301. static
  302. void
  303. dict_table_stats_latch_alloc(
  304. void* table_void)
  305. {
  306. dict_table_t* table = static_cast<dict_table_t*>(table_void);
  307. table->stats_latch = new(std::nothrow) rw_lock_t;
  308. ut_a(table->stats_latch != NULL);
  309. rw_lock_create(dict_table_stats_key, table->stats_latch,
  310. SYNC_INDEX_TREE);
  311. }
  312. /** Deinit and free a dict_table_t's stats latch.
  313. This function must not be called concurrently on the same table object.
  314. @param[in,out] table table whose stats latch to free */
  315. static
  316. void
  317. dict_table_stats_latch_free(
  318. dict_table_t* table)
  319. {
  320. rw_lock_free(table->stats_latch);
  321. delete table->stats_latch;
  322. }
  323. /** Create a dict_table_t's stats latch or delay for lazy creation.
  324. This function is only called from either single threaded environment
  325. or from a thread that has not shared the table object with other threads.
  326. @param[in,out] table table whose stats latch to create
  327. @param[in] enabled if false then the latch is disabled
  328. and dict_table_stats_lock()/unlock() become noop on this table. */
  329. void
  330. dict_table_stats_latch_create(
  331. dict_table_t* table,
  332. bool enabled)
  333. {
  334. if (!enabled) {
  335. table->stats_latch = NULL;
  336. table->stats_latch_created = os_once::DONE;
  337. return;
  338. }
  339. #ifdef HAVE_ATOMIC_BUILTINS
  340. /* We create this lazily the first time it is used. */
  341. table->stats_latch = NULL;
  342. table->stats_latch_created = os_once::NEVER_DONE;
  343. #else /* HAVE_ATOMIC_BUILTINS */
  344. dict_table_stats_latch_alloc(table);
  345. table->stats_latch_created = os_once::DONE;
  346. #endif /* HAVE_ATOMIC_BUILTINS */
  347. }
  348. /** Destroy a dict_table_t's stats latch.
  349. This function is only called from either single threaded environment
  350. or from a thread that has not shared the table object with other threads.
  351. @param[in,out] table table whose stats latch to destroy */
  352. void
  353. dict_table_stats_latch_destroy(
  354. dict_table_t* table)
  355. {
  356. if (table->stats_latch_created == os_once::DONE
  357. && table->stats_latch != NULL) {
  358. dict_table_stats_latch_free(table);
  359. }
  360. }
  361. /**********************************************************************//**
  362. Lock the appropriate latch to protect a given table's statistics. */
  363. UNIV_INTERN
  364. void
  365. dict_table_stats_lock(
  366. /*==================*/
  367. dict_table_t* table, /*!< in: table */
  368. ulint latch_mode) /*!< in: RW_S_LATCH or RW_X_LATCH */
  369. {
  370. ut_ad(table != NULL);
  371. ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
  372. #ifdef HAVE_ATOMIC_BUILTINS
  373. os_once::do_or_wait_for_done(
  374. &table->stats_latch_created,
  375. dict_table_stats_latch_alloc, table);
  376. #else /* HAVE_ATOMIC_BUILTINS */
  377. ut_ad(table->stats_latch_created == os_once::DONE);
  378. #endif /* HAVE_ATOMIC_BUILTINS */
  379. if (table->stats_latch == NULL) {
  380. /* This is a dummy table object that is private in the current
  381. thread and is not shared between multiple threads, thus we
  382. skip any locking. */
  383. return;
  384. }
  385. switch (latch_mode) {
  386. case RW_S_LATCH:
  387. rw_lock_s_lock(table->stats_latch);
  388. break;
  389. case RW_X_LATCH:
  390. rw_lock_x_lock(table->stats_latch);
  391. break;
  392. case RW_NO_LATCH:
  393. /* fall through */
  394. default:
  395. ut_error;
  396. }
  397. }
  398. /**********************************************************************//**
  399. Unlock the latch that has been locked by dict_table_stats_lock() */
  400. UNIV_INTERN
  401. void
  402. dict_table_stats_unlock(
  403. /*====================*/
  404. dict_table_t* table, /*!< in: table */
  405. ulint latch_mode) /*!< in: RW_S_LATCH or
  406. RW_X_LATCH */
  407. {
  408. ut_ad(table != NULL);
  409. ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
  410. if (table->stats_latch == NULL) {
  411. /* This is a dummy table object that is private in the current
  412. thread and is not shared between multiple threads, thus we
  413. skip any locking. */
  414. return;
  415. }
  416. switch (latch_mode) {
  417. case RW_S_LATCH:
  418. rw_lock_s_unlock(table->stats_latch);
  419. break;
  420. case RW_X_LATCH:
  421. rw_lock_x_unlock(table->stats_latch);
  422. break;
  423. case RW_NO_LATCH:
  424. /* fall through */
  425. default:
  426. ut_error;
  427. }
  428. }
  429. /**********************************************************************//**
  430. Try to drop any indexes after an aborted index creation.
  431. This can also be after a server kill during DROP INDEX. */
  432. static
  433. void
  434. dict_table_try_drop_aborted(
  435. /*========================*/
  436. dict_table_t* table, /*!< in: table, or NULL if it
  437. needs to be looked up again */
  438. table_id_t table_id, /*!< in: table identifier */
  439. ulint ref_count) /*!< in: expected table->n_ref_count */
  440. {
  441. trx_t* trx;
  442. trx = trx_allocate_for_background();
  443. trx->op_info = "try to drop any indexes after an aborted index creation";
  444. row_mysql_lock_data_dictionary(trx);
  445. trx_set_dict_operation(trx, TRX_DICT_OP_INDEX);
  446. if (table == NULL) {
  447. table = dict_table_open_on_id_low(
  448. table_id, DICT_ERR_IGNORE_NONE, FALSE);
  449. } else {
  450. ut_ad(table->id == table_id);
  451. }
  452. if (table && table->n_ref_count == ref_count && table->drop_aborted
  453. && !UT_LIST_GET_FIRST(table->locks)) {
  454. /* Silence a debug assertion in row_merge_drop_indexes(). */
  455. ut_d(table->n_ref_count++);
  456. row_merge_drop_indexes(trx, table, TRUE);
  457. ut_d(table->n_ref_count--);
  458. ut_ad(table->n_ref_count == ref_count);
  459. trx_commit_for_mysql(trx);
  460. }
  461. row_mysql_unlock_data_dictionary(trx);
  462. trx_free_for_background(trx);
  463. }
  464. /**********************************************************************//**
  465. When opening a table,
  466. try to drop any indexes after an aborted index creation.
  467. Release the dict_sys->mutex. */
  468. static
  469. void
  470. dict_table_try_drop_aborted_and_mutex_exit(
  471. /*=======================================*/
  472. dict_table_t* table, /*!< in: table (may be NULL) */
  473. ibool try_drop) /*!< in: FALSE if should try to
  474. drop indexes whose online creation
  475. was aborted */
  476. {
  477. if (try_drop
  478. && table != NULL
  479. && table->drop_aborted
  480. && table->n_ref_count == 1
  481. && dict_table_get_first_index(table)) {
  482. /* Attempt to drop the indexes whose online creation
  483. was aborted. */
  484. table_id_t table_id = table->id;
  485. mutex_exit(&dict_sys->mutex);
  486. dict_table_try_drop_aborted(table, table_id, 1);
  487. } else {
  488. mutex_exit(&dict_sys->mutex);
  489. }
  490. }
  491. /********************************************************************//**
  492. Decrements the count of open handles to a table. */
  493. UNIV_INTERN
  494. void
  495. dict_table_close(
  496. /*=============*/
  497. dict_table_t* table, /*!< in/out: table */
  498. ibool dict_locked, /*!< in: TRUE=data dictionary locked */
  499. ibool try_drop) /*!< in: TRUE=try to drop any orphan
  500. indexes after an aborted online
  501. index creation */
  502. {
  503. if (!dict_locked) {
  504. mutex_enter(&dict_sys->mutex);
  505. }
  506. ut_ad(mutex_own(&dict_sys->mutex));
  507. ut_a(table->n_ref_count > 0);
  508. const bool last_handle = !--table->n_ref_count;
  509. /* Force persistent stats re-read upon next open of the table
  510. so that FLUSH TABLE can be used to forcibly fetch stats from disk
  511. if they have been manually modified. We reset table->stat_initialized
  512. only if table reference count is 0 because we do not want too frequent
  513. stats re-reads (e.g. in other cases than FLUSH TABLE). */
  514. if (last_handle && strchr(table->name, '/') != NULL
  515. && dict_stats_is_persistent_enabled(table)) {
  516. dict_stats_deinit(table);
  517. }
  518. MONITOR_DEC(MONITOR_TABLE_REFERENCE);
  519. ut_ad(dict_lru_validate());
  520. #ifdef UNIV_DEBUG
  521. if (table->can_be_evicted) {
  522. ut_ad(dict_lru_find_table(table));
  523. } else {
  524. ut_ad(dict_non_lru_find_table(table));
  525. }
  526. #endif /* UNIV_DEBUG */
  527. if (!dict_locked) {
  528. table_id_t table_id = table->id;
  529. const bool drop_aborted = last_handle && try_drop
  530. && table->drop_aborted
  531. && dict_table_get_first_index(table);
  532. mutex_exit(&dict_sys->mutex);
  533. if (drop_aborted) {
  534. dict_table_try_drop_aborted(NULL, table_id, 0);
  535. }
  536. }
  537. }
  538. #endif /* !UNIV_HOTBACKUP */
  539. /**********************************************************************//**
  540. Returns a column's name.
  541. @return column name. NOTE: not guaranteed to stay valid if table is
  542. modified in any way (columns added, etc.). */
  543. UNIV_INTERN
  544. const char*
  545. dict_table_get_col_name(
  546. /*====================*/
  547. const dict_table_t* table, /*!< in: table */
  548. ulint col_nr) /*!< in: column number */
  549. {
  550. ulint i;
  551. const char* s;
  552. ut_ad(table);
  553. ut_ad(col_nr < table->n_def);
  554. ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
  555. s = table->col_names;
  556. if (s) {
  557. for (i = 0; i < col_nr; i++) {
  558. s += strlen(s) + 1;
  559. }
  560. }
  561. return(s);
  562. }
  563. #ifndef UNIV_HOTBACKUP
  564. /** Allocate and init the autoinc latch of a given table.
  565. This function must not be called concurrently on the same table object.
  566. @param[in,out] table_void table whose autoinc latch to create */
  567. void
  568. dict_table_autoinc_alloc(
  569. void* table_void)
  570. {
  571. dict_table_t* table = static_cast<dict_table_t*>(table_void);
  572. table->autoinc_mutex = new (std::nothrow) ib_mutex_t();
  573. ut_a(table->autoinc_mutex != NULL);
  574. mutex_create(autoinc_mutex_key,
  575. table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
  576. }
  577. /** Allocate and init the zip_pad_mutex of a given index.
  578. This function must not be called concurrently on the same index object.
  579. @param[in,out] index_void index whose zip_pad_mutex to create */
  580. void
  581. dict_index_zip_pad_alloc(
  582. void* index_void)
  583. {
  584. dict_index_t* index = static_cast<dict_index_t*>(index_void);
  585. index->zip_pad.mutex = new (std::nothrow) os_fast_mutex_t;
  586. ut_a(index->zip_pad.mutex != NULL);
  587. os_fast_mutex_init(zip_pad_mutex_key, index->zip_pad.mutex);
  588. }
  589. /********************************************************************//**
  590. Acquire the autoinc lock. */
  591. UNIV_INTERN
  592. void
  593. dict_table_autoinc_lock(
  594. /*====================*/
  595. dict_table_t* table) /*!< in/out: table */
  596. {
  597. #ifdef HAVE_ATOMIC_BUILTINS
  598. os_once::do_or_wait_for_done(
  599. &table->autoinc_mutex_created,
  600. dict_table_autoinc_alloc, table);
  601. #else /* HAVE_ATOMIC_BUILTINS */
  602. ut_ad(table->autoinc_mutex_created == os_once::DONE);
  603. #endif /* HAVE_ATOMIC_BUILTINS */
  604. mutex_enter(table->autoinc_mutex);
  605. }
  606. /** Acquire the zip_pad_mutex latch.
  607. @param[in,out] index the index whose zip_pad_mutex to acquire.*/
  608. void
  609. dict_index_zip_pad_lock(
  610. dict_index_t* index)
  611. {
  612. #ifdef HAVE_ATOMIC_BUILTINS
  613. os_once::do_or_wait_for_done(
  614. &index->zip_pad.mutex_created,
  615. dict_index_zip_pad_alloc, index);
  616. #else /* HAVE_ATOMIC_BUILTINS */
  617. ut_ad(index->zip_pad.mutex_created == os_once::DONE);
  618. #endif /* HAVE_ATOMIC_BUILTINS */
  619. os_fast_mutex_lock(index->zip_pad.mutex);
  620. }
  621. /********************************************************************//**
  622. Unconditionally set the autoinc counter. */
  623. UNIV_INTERN
  624. void
  625. dict_table_autoinc_initialize(
  626. /*==========================*/
  627. dict_table_t* table, /*!< in/out: table */
  628. ib_uint64_t value) /*!< in: next value to assign to a row */
  629. {
  630. ut_ad(dict_table_autoinc_own(table));
  631. table->autoinc = value;
  632. }
  633. /************************************************************************
  634. Get all the FTS indexes on a table.
  635. @return number of FTS indexes */
  636. UNIV_INTERN
  637. ulint
  638. dict_table_get_all_fts_indexes(
  639. /*===========================*/
  640. dict_table_t* table, /*!< in: table */
  641. ib_vector_t* indexes) /*!< out: all FTS indexes on this
  642. table */
  643. {
  644. dict_index_t* index;
  645. ut_a(ib_vector_size(indexes) == 0);
  646. for (index = dict_table_get_first_index(table);
  647. index;
  648. index = dict_table_get_next_index(index)) {
  649. if (index->type == DICT_FTS) {
  650. ib_vector_push(indexes, &index);
  651. }
  652. }
  653. return(ib_vector_size(indexes));
  654. }
  655. /** Store autoinc value when the table is evicted.
  656. @param[in] table table evicted */
  657. UNIV_INTERN
  658. void
  659. dict_table_autoinc_store(
  660. const dict_table_t* table)
  661. {
  662. ut_ad(mutex_own(&dict_sys->mutex));
  663. if (table->autoinc != 0) {
  664. ut_ad(dict_sys->autoinc_map->find(table->id)
  665. == dict_sys->autoinc_map->end());
  666. dict_sys->autoinc_map->insert(
  667. std::pair<table_id_t, ib_uint64_t>(
  668. table->id, table->autoinc));
  669. }
  670. }
  671. /** Restore autoinc value when the table is loaded.
  672. @param[in] table table loaded */
  673. UNIV_INTERN
  674. void
  675. dict_table_autoinc_restore(
  676. dict_table_t* table)
  677. {
  678. ut_ad(mutex_own(&dict_sys->mutex));
  679. autoinc_map_t::iterator it;
  680. it = dict_sys->autoinc_map->find(table->id);
  681. if (it != dict_sys->autoinc_map->end()) {
  682. table->autoinc = it->second;
  683. ut_ad(table->autoinc != 0);
  684. dict_sys->autoinc_map->erase(it);
  685. }
  686. }
  687. /********************************************************************//**
  688. Reads the next autoinc value (== autoinc counter value), 0 if not yet
  689. initialized.
  690. @return value for a new row, or 0 */
  691. UNIV_INTERN
  692. ib_uint64_t
  693. dict_table_autoinc_read(
  694. /*====================*/
  695. const dict_table_t* table) /*!< in: table */
  696. {
  697. ut_ad(dict_table_autoinc_own(table));
  698. return(table->autoinc);
  699. }
  700. /********************************************************************//**
  701. Updates the autoinc counter if the value supplied is greater than the
  702. current value. */
  703. UNIV_INTERN
  704. void
  705. dict_table_autoinc_update_if_greater(
  706. /*=================================*/
  707. dict_table_t* table, /*!< in/out: table */
  708. ib_uint64_t value) /*!< in: value which was assigned to a row */
  709. {
  710. ut_ad(dict_table_autoinc_own(table));
  711. if (value > table->autoinc) {
  712. table->autoinc = value;
  713. }
  714. }
  715. /********************************************************************//**
  716. Release the autoinc lock. */
  717. UNIV_INTERN
  718. void
  719. dict_table_autoinc_unlock(
  720. /*======================*/
  721. dict_table_t* table) /*!< in/out: table */
  722. {
  723. mutex_exit(table->autoinc_mutex);
  724. }
  725. #endif /* !UNIV_HOTBACKUP */
  726. /********************************************************************//**
  727. Looks for column n in an index.
  728. @return position in internal representation of the index;
  729. ULINT_UNDEFINED if not contained */
  730. UNIV_INTERN
  731. ulint
  732. dict_index_get_nth_col_or_prefix_pos(
  733. /*=================================*/
  734. const dict_index_t* index, /*!< in: index */
  735. ulint n, /*!< in: column number */
  736. ibool inc_prefix, /*!< in: TRUE=consider
  737. column prefixes too */
  738. ulint* prefix_col_pos) /*!< out: col num if prefix */
  739. {
  740. const dict_field_t* field;
  741. const dict_col_t* col;
  742. ulint pos;
  743. ulint n_fields;
  744. ulint prefixed_pos_dummy;
  745. ut_ad(index);
  746. ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
  747. ut_ad((inc_prefix && !prefix_col_pos) || (!inc_prefix));
  748. if (!prefix_col_pos) {
  749. prefix_col_pos = &prefixed_pos_dummy;
  750. }
  751. *prefix_col_pos = ULINT_UNDEFINED;
  752. if (!prefix_col_pos) {
  753. prefix_col_pos = &prefixed_pos_dummy;
  754. }
  755. *prefix_col_pos = ULINT_UNDEFINED;
  756. col = dict_table_get_nth_col(index->table, n);
  757. if (dict_index_is_clust(index)) {
  758. return(dict_col_get_clust_pos(col, index));
  759. }
  760. n_fields = dict_index_get_n_fields(index);
  761. for (pos = 0; pos < n_fields; pos++) {
  762. field = dict_index_get_nth_field(index, pos);
  763. if (col == field->col) {
  764. *prefix_col_pos = pos;
  765. if (inc_prefix || field->prefix_len == 0) {
  766. return(pos);
  767. }
  768. }
  769. }
  770. return(ULINT_UNDEFINED);
  771. }
  772. #ifndef UNIV_HOTBACKUP
  773. /********************************************************************//**
  774. Returns TRUE if the index contains a column or a prefix of that column.
  775. @return TRUE if contains the column or its prefix */
  776. UNIV_INTERN
  777. ibool
  778. dict_index_contains_col_or_prefix(
  779. /*==============================*/
  780. const dict_index_t* index, /*!< in: index */
  781. ulint n) /*!< in: column number */
  782. {
  783. const dict_field_t* field;
  784. const dict_col_t* col;
  785. ulint pos;
  786. ulint n_fields;
  787. ut_ad(index);
  788. ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
  789. if (dict_index_is_clust(index)) {
  790. return(TRUE);
  791. }
  792. col = dict_table_get_nth_col(index->table, n);
  793. n_fields = dict_index_get_n_fields(index);
  794. for (pos = 0; pos < n_fields; pos++) {
  795. field = dict_index_get_nth_field(index, pos);
  796. if (col == field->col) {
  797. return(TRUE);
  798. }
  799. }
  800. return(FALSE);
  801. }
  802. /********************************************************************//**
  803. Looks for a matching field in an index. The column has to be the same. The
  804. column in index must be complete, or must contain a prefix longer than the
  805. column in index2. That is, we must be able to construct the prefix in index2
  806. from the prefix in index.
  807. @return position in internal representation of the index;
  808. ULINT_UNDEFINED if not contained */
  809. UNIV_INTERN
  810. ulint
  811. dict_index_get_nth_field_pos(
  812. /*=========================*/
  813. const dict_index_t* index, /*!< in: index from which to search */
  814. const dict_index_t* index2, /*!< in: index */
  815. ulint n) /*!< in: field number in index2 */
  816. {
  817. const dict_field_t* field;
  818. const dict_field_t* field2;
  819. ulint n_fields;
  820. ulint pos;
  821. ut_ad(index);
  822. ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
  823. field2 = dict_index_get_nth_field(index2, n);
  824. n_fields = dict_index_get_n_fields(index);
  825. for (pos = 0; pos < n_fields; pos++) {
  826. field = dict_index_get_nth_field(index, pos);
  827. if (field->col == field2->col
  828. && (field->prefix_len == 0
  829. || (field->prefix_len >= field2->prefix_len
  830. && field2->prefix_len != 0))) {
  831. return(pos);
  832. }
  833. }
  834. return(ULINT_UNDEFINED);
  835. }
  836. /**********************************************************************//**
  837. Returns a table object based on table id.
  838. @return table, NULL if does not exist */
  839. UNIV_INTERN
  840. dict_table_t*
  841. dict_table_open_on_id(
  842. /*==================*/
  843. table_id_t table_id, /*!< in: table id */
  844. ibool dict_locked, /*!< in: TRUE=data dictionary locked */
  845. dict_table_op_t table_op) /*!< in: operation to perform */
  846. {
  847. dict_table_t* table;
  848. if (!dict_locked) {
  849. mutex_enter(&dict_sys->mutex);
  850. }
  851. ut_ad(mutex_own(&dict_sys->mutex));
  852. table = dict_table_open_on_id_low(
  853. table_id,
  854. table_op == DICT_TABLE_OP_LOAD_TABLESPACE
  855. ? DICT_ERR_IGNORE_RECOVER_LOCK
  856. : DICT_ERR_IGNORE_NONE,
  857. table_op == DICT_TABLE_OP_OPEN_ONLY_IF_CACHED);
  858. if (table != NULL) {
  859. if (table->can_be_evicted) {
  860. dict_move_to_mru(table);
  861. }
  862. ++table->n_ref_count;
  863. MONITOR_INC(MONITOR_TABLE_REFERENCE);
  864. }
  865. if (!dict_locked) {
  866. dict_table_try_drop_aborted_and_mutex_exit(
  867. table, table_op == DICT_TABLE_OP_DROP_ORPHAN);
  868. }
  869. return(table);
  870. }
  871. /********************************************************************//**
  872. Looks for column n position in the clustered index.
  873. @return position in internal representation of the clustered index */
  874. UNIV_INTERN
  875. ulint
  876. dict_table_get_nth_col_pos(
  877. /*=======================*/
  878. const dict_table_t* table, /*!< in: table */
  879. ulint n) /*!< in: column number */
  880. {
  881. return(dict_index_get_nth_col_pos(dict_table_get_first_index(table),
  882. n, NULL));
  883. }
  884. /********************************************************************//**
  885. Checks if a column is in the ordering columns of the clustered index of a
  886. table. Column prefixes are treated like whole columns.
  887. @return TRUE if the column, or its prefix, is in the clustered key */
  888. UNIV_INTERN
  889. ibool
  890. dict_table_col_in_clustered_key(
  891. /*============================*/
  892. const dict_table_t* table, /*!< in: table */
  893. ulint n) /*!< in: column number */
  894. {
  895. const dict_index_t* index;
  896. const dict_field_t* field;
  897. const dict_col_t* col;
  898. ulint pos;
  899. ulint n_fields;
  900. ut_ad(table);
  901. col = dict_table_get_nth_col(table, n);
  902. index = dict_table_get_first_index(table);
  903. n_fields = dict_index_get_n_unique(index);
  904. for (pos = 0; pos < n_fields; pos++) {
  905. field = dict_index_get_nth_field(index, pos);
  906. if (col == field->col) {
  907. return(TRUE);
  908. }
  909. }
  910. return(FALSE);
  911. }
  912. /**********************************************************************//**
  913. Inits the data dictionary module. */
  914. UNIV_INTERN
  915. void
  916. dict_init(void)
  917. /*===========*/
  918. {
  919. dict_sys = static_cast<dict_sys_t*>(mem_zalloc(sizeof(*dict_sys)));
  920. mutex_create(dict_sys_mutex_key, &dict_sys->mutex, SYNC_DICT);
  921. dict_sys->table_hash = hash_create(buf_pool_get_curr_size()
  922. / (DICT_POOL_PER_TABLE_HASH
  923. * UNIV_WORD_SIZE));
  924. dict_sys->table_id_hash = hash_create(buf_pool_get_curr_size()
  925. / (DICT_POOL_PER_TABLE_HASH
  926. * UNIV_WORD_SIZE));
  927. rw_lock_create(dict_operation_lock_key,
  928. &dict_operation_lock, SYNC_DICT_OPERATION);
  929. if (!srv_read_only_mode) {
  930. dict_foreign_err_file = os_file_create_tmpfile(NULL);
  931. ut_a(dict_foreign_err_file);
  932. mutex_create(dict_foreign_err_mutex_key,
  933. &dict_foreign_err_mutex, SYNC_NO_ORDER_CHECK);
  934. }
  935. dict_sys->autoinc_map = new autoinc_map_t();
  936. }
  937. /**********************************************************************//**
  938. Move to the most recently used segment of the LRU list. */
  939. UNIV_INTERN
  940. void
  941. dict_move_to_mru(
  942. /*=============*/
  943. dict_table_t* table) /*!< in: table to move to MRU */
  944. {
  945. ut_ad(mutex_own(&dict_sys->mutex));
  946. ut_ad(dict_lru_validate());
  947. ut_ad(dict_lru_find_table(table));
  948. ut_a(table->can_be_evicted);
  949. UT_LIST_REMOVE(table_LRU, dict_sys->table_LRU, table);
  950. UT_LIST_ADD_FIRST(table_LRU, dict_sys->table_LRU, table);
  951. ut_ad(dict_lru_validate());
  952. }
  953. /**********************************************************************//**
  954. Returns a table object and increment its open handle count.
  955. NOTE! This is a high-level function to be used mainly from outside the
  956. 'dict' module. Inside this directory dict_table_get_low
  957. is usually the appropriate function.
  958. @return table, NULL if does not exist */
  959. UNIV_INTERN
  960. dict_table_t*
  961. dict_table_open_on_name(
  962. /*====================*/
  963. const char* table_name, /*!< in: table name */
  964. ibool dict_locked, /*!< in: TRUE=data dictionary locked */
  965. ibool try_drop, /*!< in: TRUE=try to drop any orphan
  966. indexes after an aborted online
  967. index creation */
  968. dict_err_ignore_t
  969. ignore_err) /*!< in: error to be ignored when
  970. loading a table definition */
  971. {
  972. dict_table_t* table;
  973. if (!dict_locked) {
  974. mutex_enter(&(dict_sys->mutex));
  975. }
  976. ut_ad(table_name);
  977. ut_ad(mutex_own(&dict_sys->mutex));
  978. table = dict_table_check_if_in_cache_low(table_name);
  979. if (table == NULL) {
  980. table = dict_load_table(table_name, TRUE, ignore_err);
  981. }
  982. ut_ad(!table || table->cached);
  983. if (table != NULL) {
  984. /* If table is encrypted or corrupted */
  985. if (ignore_err == DICT_ERR_IGNORE_NONE
  986. && !table->is_readable()) {
  987. /* Make life easy for drop table. */
  988. if (table->can_be_evicted) {
  989. dict_table_move_from_lru_to_non_lru(table);
  990. }
  991. if (table->corrupted) {
  992. if (!dict_locked) {
  993. mutex_exit(&dict_sys->mutex);
  994. }
  995. char buf[MAX_FULL_NAME_LEN];
  996. ut_format_name(table->name, TRUE, buf, sizeof(buf));
  997. ib_logf(IB_LOG_LEVEL_ERROR,
  998. "Table %s is corrupted. Please "
  999. "drop the table and recreate.",
  1000. buf);
  1001. return(NULL);
  1002. }
  1003. if (table->can_be_evicted) {
  1004. dict_move_to_mru(table);
  1005. }
  1006. ++table->n_ref_count;
  1007. if (!dict_locked) {
  1008. mutex_exit(&dict_sys->mutex);
  1009. }
  1010. return (table);
  1011. }
  1012. if (table->can_be_evicted) {
  1013. dict_move_to_mru(table);
  1014. }
  1015. ++table->n_ref_count;
  1016. MONITOR_INC(MONITOR_TABLE_REFERENCE);
  1017. }
  1018. ut_ad(dict_lru_validate());
  1019. if (!dict_locked) {
  1020. dict_table_try_drop_aborted_and_mutex_exit(table, try_drop);
  1021. }
  1022. return(table);
  1023. }
  1024. #endif /* !UNIV_HOTBACKUP */
  1025. /**********************************************************************//**
  1026. Adds system columns to a table object. */
  1027. UNIV_INTERN
  1028. void
  1029. dict_table_add_system_columns(
  1030. /*==========================*/
  1031. dict_table_t* table, /*!< in/out: table */
  1032. mem_heap_t* heap) /*!< in: temporary heap */
  1033. {
  1034. ut_ad(table);
  1035. ut_ad(table->n_def == table->n_cols - DATA_N_SYS_COLS);
  1036. ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
  1037. ut_ad(!table->cached);
  1038. /* NOTE: the system columns MUST be added in the following order
  1039. (so that they can be indexed by the numerical value of DATA_ROW_ID,
  1040. etc.) and as the last columns of the table memory object.
  1041. The clustered index will not always physically contain all
  1042. system columns. */
  1043. dict_mem_table_add_col(table, heap, "DB_ROW_ID", DATA_SYS,
  1044. DATA_ROW_ID | DATA_NOT_NULL,
  1045. DATA_ROW_ID_LEN);
  1046. #if DATA_ROW_ID != 0
  1047. #error "DATA_ROW_ID != 0"
  1048. #endif
  1049. dict_mem_table_add_col(table, heap, "DB_TRX_ID", DATA_SYS,
  1050. DATA_TRX_ID | DATA_NOT_NULL,
  1051. DATA_TRX_ID_LEN);
  1052. #if DATA_TRX_ID != 1
  1053. #error "DATA_TRX_ID != 1"
  1054. #endif
  1055. dict_mem_table_add_col(table, heap, "DB_ROLL_PTR", DATA_SYS,
  1056. DATA_ROLL_PTR | DATA_NOT_NULL,
  1057. DATA_ROLL_PTR_LEN);
  1058. #if DATA_ROLL_PTR != 2
  1059. #error "DATA_ROLL_PTR != 2"
  1060. #endif
  1061. /* This check reminds that if a new system column is added to
  1062. the program, it should be dealt with here */
  1063. #if DATA_N_SYS_COLS != 3
  1064. #error "DATA_N_SYS_COLS != 3"
  1065. #endif
  1066. }
  1067. #ifndef UNIV_HOTBACKUP
  1068. /**********************************************************************//**
  1069. Adds a table object to the dictionary cache. */
  1070. UNIV_INTERN
  1071. void
  1072. dict_table_add_to_cache(
  1073. /*====================*/
  1074. dict_table_t* table, /*!< in: table */
  1075. ibool can_be_evicted, /*!< in: TRUE if can be evicted */
  1076. mem_heap_t* heap) /*!< in: temporary heap */
  1077. {
  1078. ulint fold;
  1079. ulint id_fold;
  1080. ulint i;
  1081. ulint row_len;
  1082. ut_ad(dict_lru_validate());
  1083. /* The lower limit for what we consider a "big" row */
  1084. #define BIG_ROW_SIZE 1024
  1085. ut_ad(mutex_own(&(dict_sys->mutex)));
  1086. dict_table_add_system_columns(table, heap);
  1087. table->cached = TRUE;
  1088. fold = ut_fold_string(table->name);
  1089. id_fold = ut_fold_ull(table->id);
  1090. row_len = 0;
  1091. for (i = 0; i < table->n_def; i++) {
  1092. ulint col_len = dict_col_get_max_size(
  1093. dict_table_get_nth_col(table, i));
  1094. row_len += col_len;
  1095. /* If we have a single unbounded field, or several gigantic
  1096. fields, mark the maximum row size as BIG_ROW_SIZE. */
  1097. if (row_len >= BIG_ROW_SIZE || col_len >= BIG_ROW_SIZE) {
  1098. row_len = BIG_ROW_SIZE;
  1099. break;
  1100. }
  1101. }
  1102. table->big_rows = row_len >= BIG_ROW_SIZE;
  1103. /* Look for a table with the same name: error if such exists */
  1104. {
  1105. dict_table_t* table2;
  1106. HASH_SEARCH(name_hash, dict_sys->table_hash, fold,
  1107. dict_table_t*, table2, ut_ad(table2->cached),
  1108. ut_strcmp(table2->name, table->name) == 0);
  1109. ut_a(table2 == NULL);
  1110. #ifdef UNIV_DEBUG
  1111. /* Look for the same table pointer with a different name */
  1112. HASH_SEARCH_ALL(name_hash, dict_sys->table_hash,
  1113. dict_table_t*, table2, ut_ad(table2->cached),
  1114. table2 == table);
  1115. ut_ad(table2 == NULL);
  1116. #endif /* UNIV_DEBUG */
  1117. }
  1118. /* Look for a table with the same id: error if such exists */
  1119. {
  1120. dict_table_t* table2;
  1121. HASH_SEARCH(id_hash, dict_sys->table_id_hash, id_fold,
  1122. dict_table_t*, table2, ut_ad(table2->cached),
  1123. table2->id == table->id);
  1124. ut_a(table2 == NULL);
  1125. #ifdef UNIV_DEBUG
  1126. /* Look for the same table pointer with a different id */
  1127. HASH_SEARCH_ALL(id_hash, dict_sys->table_id_hash,
  1128. dict_table_t*, table2, ut_ad(table2->cached),
  1129. table2 == table);
  1130. ut_ad(table2 == NULL);
  1131. #endif /* UNIV_DEBUG */
  1132. }
  1133. /* Add table to hash table of tables */
  1134. HASH_INSERT(dict_table_t, name_hash, dict_sys->table_hash, fold,
  1135. table);
  1136. /* Add table to hash table of tables based on table id */
  1137. HASH_INSERT(dict_table_t, id_hash, dict_sys->table_id_hash, id_fold,
  1138. table);
  1139. table->can_be_evicted = can_be_evicted;
  1140. if (table->can_be_evicted) {
  1141. UT_LIST_ADD_FIRST(table_LRU, dict_sys->table_LRU, table);
  1142. } else {
  1143. UT_LIST_ADD_FIRST(table_LRU, dict_sys->table_non_LRU, table);
  1144. }
  1145. dict_table_autoinc_restore(table);
  1146. ut_ad(dict_lru_validate());
  1147. }
  1148. /**********************************************************************//**
  1149. Test whether a table can be evicted from the LRU cache.
  1150. @return TRUE if table can be evicted. */
  1151. static
  1152. ibool
  1153. dict_table_can_be_evicted(
  1154. /*======================*/
  1155. const dict_table_t* table) /*!< in: table to test */
  1156. {
  1157. ut_ad(mutex_own(&dict_sys->mutex));
  1158. #ifdef UNIV_SYNC_DEBUG
  1159. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  1160. #endif /* UNIV_SYNC_DEBUG */
  1161. ut_a(table->can_be_evicted);
  1162. ut_a(table->foreign_set.empty());
  1163. ut_a(table->referenced_set.empty());
  1164. if (table->n_ref_count == 0) {
  1165. dict_index_t* index;
  1166. /* The transaction commit and rollback are called from
  1167. outside the handler interface. This means that there is
  1168. a window where the table->n_ref_count can be zero but
  1169. the table instance is in "use". */
  1170. if (lock_table_has_locks(table)) {
  1171. return(FALSE);
  1172. }
  1173. for (index = dict_table_get_first_index(table);
  1174. index != NULL;
  1175. index = dict_table_get_next_index(index)) {
  1176. btr_search_t* info = btr_search_get_info(index);
  1177. /* We are not allowed to free the in-memory index
  1178. struct dict_index_t until all entries in the adaptive
  1179. hash index that point to any of the page belonging to
  1180. his b-tree index are dropped. This is so because
  1181. dropping of these entries require access to
  1182. dict_index_t struct. To avoid such scenario we keep
  1183. a count of number of such pages in the search_info and
  1184. only free the dict_index_t struct when this count
  1185. drops to zero.
  1186. See also: dict_index_remove_from_cache_low() */
  1187. if (btr_search_info_get_ref_count(info, index) > 0) {
  1188. return(FALSE);
  1189. }
  1190. }
  1191. return(TRUE);
  1192. }
  1193. return(FALSE);
  1194. }
  1195. /**********************************************************************//**
  1196. Make room in the table cache by evicting an unused table. The unused table
  1197. should not be part of FK relationship and currently not used in any user
  1198. transaction. There is no guarantee that it will remove a table.
  1199. @return number of tables evicted. If the number of tables in the dict_LRU
  1200. is less than max_tables it will not do anything. */
  1201. UNIV_INTERN
  1202. ulint
  1203. dict_make_room_in_cache(
  1204. /*====================*/
  1205. ulint max_tables, /*!< in: max tables allowed in cache */
  1206. ulint pct_check) /*!< in: max percent to check */
  1207. {
  1208. ulint i;
  1209. ulint len;
  1210. dict_table_t* table;
  1211. ulint check_up_to;
  1212. ulint n_evicted = 0;
  1213. ut_a(pct_check > 0);
  1214. ut_a(pct_check <= 100);
  1215. ut_ad(mutex_own(&dict_sys->mutex));
  1216. #ifdef UNIV_SYNC_DEBUG
  1217. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  1218. #endif /* UNIV_SYNC_DEBUG */
  1219. ut_ad(dict_lru_validate());
  1220. i = len = UT_LIST_GET_LEN(dict_sys->table_LRU);
  1221. if (len < max_tables) {
  1222. return(0);
  1223. }
  1224. check_up_to = len - ((len * pct_check) / 100);
  1225. /* Check for overflow */
  1226. ut_a(i == 0 || check_up_to <= i);
  1227. /* Find a suitable candidate to evict from the cache. Don't scan the
  1228. entire LRU list. Only scan pct_check list entries. */
  1229. for (table = UT_LIST_GET_LAST(dict_sys->table_LRU);
  1230. table != NULL
  1231. && i > check_up_to
  1232. && (len - n_evicted) > max_tables;
  1233. --i) {
  1234. dict_table_t* prev_table;
  1235. prev_table = UT_LIST_GET_PREV(table_LRU, table);
  1236. if (dict_table_can_be_evicted(table)) {
  1237. dict_table_remove_from_cache_low(table, TRUE);
  1238. ++n_evicted;
  1239. }
  1240. table = prev_table;
  1241. }
  1242. return(n_evicted);
  1243. }
  1244. /**********************************************************************//**
  1245. Move a table to the non-LRU list from the LRU list. */
  1246. UNIV_INTERN
  1247. void
  1248. dict_table_move_from_lru_to_non_lru(
  1249. /*================================*/
  1250. dict_table_t* table) /*!< in: table to move from LRU to non-LRU */
  1251. {
  1252. ut_ad(mutex_own(&dict_sys->mutex));
  1253. ut_ad(dict_lru_find_table(table));
  1254. ut_a(table->can_be_evicted);
  1255. UT_LIST_REMOVE(table_LRU, dict_sys->table_LRU, table);
  1256. UT_LIST_ADD_LAST(table_LRU, dict_sys->table_non_LRU, table);
  1257. table->can_be_evicted = FALSE;
  1258. }
  1259. /**********************************************************************//**
  1260. Move a table to the LRU list from the non-LRU list. */
  1261. UNIV_INTERN
  1262. void
  1263. dict_table_move_from_non_lru_to_lru(
  1264. /*================================*/
  1265. dict_table_t* table) /*!< in: table to move from non-LRU to LRU */
  1266. {
  1267. ut_ad(mutex_own(&dict_sys->mutex));
  1268. ut_ad(dict_non_lru_find_table(table));
  1269. ut_a(!table->can_be_evicted);
  1270. UT_LIST_REMOVE(table_LRU, dict_sys->table_non_LRU, table);
  1271. UT_LIST_ADD_LAST(table_LRU, dict_sys->table_LRU, table);
  1272. table->can_be_evicted = TRUE;
  1273. }
  1274. /**********************************************************************//**
  1275. Looks for an index with the given id given a table instance.
  1276. @return index or NULL */
  1277. UNIV_INTERN
  1278. dict_index_t*
  1279. dict_table_find_index_on_id(
  1280. /*========================*/
  1281. const dict_table_t* table, /*!< in: table instance */
  1282. index_id_t id) /*!< in: index id */
  1283. {
  1284. dict_index_t* index;
  1285. for (index = dict_table_get_first_index(table);
  1286. index != NULL;
  1287. index = dict_table_get_next_index(index)) {
  1288. if (id == index->id) {
  1289. /* Found */
  1290. return(index);
  1291. }
  1292. }
  1293. return(NULL);
  1294. }
  1295. /**********************************************************************//**
  1296. Looks for an index with the given id. NOTE that we do not reserve
  1297. the dictionary mutex: this function is for emergency purposes like
  1298. printing info of a corrupt database page!
  1299. @return index or NULL if not found in cache */
  1300. UNIV_INTERN
  1301. dict_index_t*
  1302. dict_index_find_on_id_low(
  1303. /*======================*/
  1304. index_id_t id) /*!< in: index id */
  1305. {
  1306. dict_table_t* table;
  1307. /* This can happen if the system tablespace is the wrong page size */
  1308. if (dict_sys == NULL) {
  1309. return(NULL);
  1310. }
  1311. for (table = UT_LIST_GET_FIRST(dict_sys->table_LRU);
  1312. table != NULL;
  1313. table = UT_LIST_GET_NEXT(table_LRU, table)) {
  1314. dict_index_t* index = dict_table_find_index_on_id(table, id);
  1315. if (index != NULL) {
  1316. return(index);
  1317. }
  1318. }
  1319. for (table = UT_LIST_GET_FIRST(dict_sys->table_non_LRU);
  1320. table != NULL;
  1321. table = UT_LIST_GET_NEXT(table_LRU, table)) {
  1322. dict_index_t* index = dict_table_find_index_on_id(table, id);
  1323. if (index != NULL) {
  1324. return(index);
  1325. }
  1326. }
  1327. return(NULL);
  1328. }
  1329. /** Function object to remove a foreign key constraint from the
  1330. referenced_set of the referenced table. The foreign key object is
  1331. also removed from the dictionary cache. The foreign key constraint
  1332. is not removed from the foreign_set of the table containing the
  1333. constraint. */
  1334. struct dict_foreign_remove_partial
  1335. {
  1336. void operator()(dict_foreign_t* foreign) {
  1337. dict_table_t* table = foreign->referenced_table;
  1338. if (table != NULL) {
  1339. table->referenced_set.erase(foreign);
  1340. }
  1341. dict_foreign_free(foreign);
  1342. }
  1343. };
  1344. /**********************************************************************//**
  1345. Renames a table object.
  1346. @return TRUE if success */
  1347. UNIV_INTERN
  1348. dberr_t
  1349. dict_table_rename_in_cache(
  1350. /*=======================*/
  1351. dict_table_t* table, /*!< in/out: table */
  1352. const char* new_name, /*!< in: new name */
  1353. ibool rename_also_foreigns)/*!< in: in ALTER TABLE we want
  1354. to preserve the original table name
  1355. in constraints which reference it */
  1356. {
  1357. dberr_t err;
  1358. dict_foreign_t* foreign;
  1359. dict_index_t* index;
  1360. ulint fold;
  1361. char old_name[MAX_FULL_NAME_LEN + 1];
  1362. os_file_type_t ftype;
  1363. ibool exists;
  1364. ut_ad(mutex_own(&(dict_sys->mutex)));
  1365. /* store the old/current name to an automatic variable */
  1366. if (strlen(table->name) + 1 <= sizeof(old_name)) {
  1367. memcpy(old_name, table->name, strlen(table->name) + 1);
  1368. } else {
  1369. ut_print_timestamp(stderr);
  1370. fprintf(stderr, "InnoDB: too long table name: '%s', "
  1371. "max length is %d\n", table->name,
  1372. MAX_FULL_NAME_LEN);
  1373. ut_error;
  1374. }
  1375. fold = ut_fold_string(new_name);
  1376. /* Look for a table with the same name: error if such exists */
  1377. dict_table_t* table2;
  1378. HASH_SEARCH(name_hash, dict_sys->table_hash, fold,
  1379. dict_table_t*, table2, ut_ad(table2->cached),
  1380. (ut_strcmp(table2->name, new_name) == 0));
  1381. DBUG_EXECUTE_IF("dict_table_rename_in_cache_failure",
  1382. if (table2 == NULL) {
  1383. table2 = (dict_table_t*) -1;
  1384. } );
  1385. if (table2) {
  1386. ib_logf(IB_LOG_LEVEL_ERROR,
  1387. "Cannot rename table '%s' to '%s' since the "
  1388. "dictionary cache already contains '%s'.",
  1389. old_name, new_name, new_name);
  1390. return(DB_ERROR);
  1391. }
  1392. /* If the table is stored in a single-table tablespace, rename the
  1393. .ibd file and rebuild the .isl file if needed. */
  1394. if (dict_table_is_discarded(table)) {
  1395. char* filepath;
  1396. ut_ad(table->space != TRX_SYS_SPACE);
  1397. if (DICT_TF_HAS_DATA_DIR(table->flags)) {
  1398. dict_get_and_save_data_dir_path(table, true);
  1399. ut_a(table->data_dir_path);
  1400. filepath = os_file_make_remote_pathname(
  1401. table->data_dir_path, table->name, "ibd");
  1402. } else {
  1403. filepath = fil_make_ibd_name(table->name, false);
  1404. }
  1405. fil_delete_tablespace(table->space);
  1406. /* Delete any temp file hanging around. */
  1407. if (os_file_status(filepath, &exists, &ftype)
  1408. && exists
  1409. && !os_file_delete_if_exists(innodb_file_temp_key,
  1410. filepath)) {
  1411. ib_logf(IB_LOG_LEVEL_INFO,
  1412. "Delete of %s failed.", filepath);
  1413. }
  1414. mem_free(filepath);
  1415. } else if (table->space != TRX_SYS_SPACE) {
  1416. if (DICT_TF2_FLAG_IS_SET(table, DICT_TF2_TEMPORARY)) {
  1417. ut_print_timestamp(stderr);
  1418. fputs(" InnoDB: Error: trying to rename a"
  1419. " TEMPORARY TABLE ", stderr);
  1420. ut_print_name(stderr, NULL, TRUE, old_name);
  1421. if (table->dir_path_of_temp_table != NULL) {
  1422. fputs(" (", stderr);
  1423. ut_print_filename(
  1424. stderr, table->dir_path_of_temp_table);
  1425. fputs(" )\n", stderr);
  1426. }
  1427. return(DB_ERROR);
  1428. }
  1429. char* new_path = NULL;
  1430. char* old_path = fil_space_get_first_path(table->space);
  1431. if (DICT_TF_HAS_DATA_DIR(table->flags)) {
  1432. new_path = os_file_make_new_pathname(
  1433. old_path, new_name);
  1434. err = fil_create_link_file(new_name, new_path);
  1435. if (err != DB_SUCCESS) {
  1436. mem_free(new_path);
  1437. mem_free(old_path);
  1438. return(DB_TABLESPACE_EXISTS);
  1439. }
  1440. } else {
  1441. new_path = fil_make_ibd_name(new_name, false);
  1442. }
  1443. /* New filepath must not exist. */
  1444. err = fil_rename_tablespace_check(
  1445. table->space, old_path, new_path, false);
  1446. if (err != DB_SUCCESS) {
  1447. mem_free(old_path);
  1448. mem_free(new_path);
  1449. return(err);
  1450. }
  1451. ibool success = fil_rename_tablespace(
  1452. old_name, table->space, new_name, new_path);
  1453. mem_free(old_path);
  1454. mem_free(new_path);
  1455. /* If the tablespace is remote, a new .isl file was created
  1456. If success, delete the old one. If not, delete the new one. */
  1457. if (DICT_TF_HAS_DATA_DIR(table->flags)) {
  1458. fil_delete_link_file(success ? old_name : new_name);
  1459. }
  1460. if (!success) {
  1461. return(DB_ERROR);
  1462. }
  1463. }
  1464. /* Remove table from the hash tables of tables */
  1465. HASH_DELETE(dict_table_t, name_hash, dict_sys->table_hash,
  1466. ut_fold_string(old_name), table);
  1467. if (strlen(new_name) > strlen(table->name)) {
  1468. /* We allocate MAX_FULL_NAME_LEN + 1 bytes here to avoid
  1469. memory fragmentation, we assume a repeated calls of
  1470. ut_realloc() with the same size do not cause fragmentation */
  1471. ut_a(strlen(new_name) <= MAX_FULL_NAME_LEN);
  1472. table->name = static_cast<char*>(
  1473. ut_realloc(table->name, MAX_FULL_NAME_LEN + 1));
  1474. }
  1475. memcpy(table->name, new_name, strlen(new_name) + 1);
  1476. /* Add table to hash table of tables */
  1477. HASH_INSERT(dict_table_t, name_hash, dict_sys->table_hash, fold,
  1478. table);
  1479. /* Update the table_name field in indexes */
  1480. for (index = dict_table_get_first_index(table);
  1481. index != NULL;
  1482. index = dict_table_get_next_index(index)) {
  1483. index->table_name = table->name;
  1484. }
  1485. if (!rename_also_foreigns) {
  1486. /* In ALTER TABLE we think of the rename table operation
  1487. in the direction table -> temporary table (#sql...)
  1488. as dropping the table with the old name and creating
  1489. a new with the new name. Thus we kind of drop the
  1490. constraints from the dictionary cache here. The foreign key
  1491. constraints will be inherited to the new table from the
  1492. system tables through a call of dict_load_foreigns. */
  1493. /* Remove the foreign constraints from the cache */
  1494. std::for_each(table->foreign_set.begin(),
  1495. table->foreign_set.end(),
  1496. dict_foreign_remove_partial());
  1497. table->foreign_set.clear();
  1498. /* Reset table field in referencing constraints */
  1499. for (dict_foreign_set::iterator it
  1500. = table->referenced_set.begin();
  1501. it != table->referenced_set.end();
  1502. ++it) {
  1503. foreign = *it;
  1504. foreign->referenced_table = NULL;
  1505. foreign->referenced_index = NULL;
  1506. }
  1507. /* Make the set of referencing constraints empty */
  1508. table->referenced_set.clear();
  1509. return(DB_SUCCESS);
  1510. }
  1511. /* Update the table name fields in foreign constraints, and update also
  1512. the constraint id of new format >= 4.0.18 constraints. Note that at
  1513. this point we have already changed table->name to the new name. */
  1514. dict_foreign_set fk_set;
  1515. for (;;) {
  1516. dict_foreign_set::iterator it
  1517. = table->foreign_set.begin();
  1518. if (it == table->foreign_set.end()) {
  1519. break;
  1520. }
  1521. foreign = *it;
  1522. if (foreign->referenced_table) {
  1523. foreign->referenced_table->referenced_set.erase(foreign);
  1524. }
  1525. if (ut_strlen(foreign->foreign_table_name)
  1526. < ut_strlen(table->name)) {
  1527. /* Allocate a longer name buffer;
  1528. TODO: store buf len to save memory */
  1529. foreign->foreign_table_name = mem_heap_strdup(
  1530. foreign->heap, table->name);
  1531. dict_mem_foreign_table_name_lookup_set(foreign, TRUE);
  1532. } else {
  1533. strcpy(foreign->foreign_table_name, table->name);
  1534. dict_mem_foreign_table_name_lookup_set(foreign, FALSE);
  1535. }
  1536. if (strchr(foreign->id, '/')) {
  1537. /* This is a >= 4.0.18 format id */
  1538. ulint db_len;
  1539. char* old_id;
  1540. char old_name_cs_filename[MAX_TABLE_NAME_LEN+20];
  1541. uint errors = 0;
  1542. /* All table names are internally stored in charset
  1543. my_charset_filename (except the temp tables and the
  1544. partition identifier suffix in partition tables). The
  1545. foreign key constraint names are internally stored
  1546. in UTF-8 charset. The variable fkid here is used
  1547. to store foreign key constraint name in charset
  1548. my_charset_filename for comparison further below. */
  1549. char fkid[MAX_TABLE_NAME_LEN+20];
  1550. ibool on_tmp = FALSE;
  1551. /* The old table name in my_charset_filename is stored
  1552. in old_name_cs_filename */
  1553. strncpy(old_name_cs_filename, old_name,
  1554. MAX_TABLE_NAME_LEN);
  1555. if (strstr(old_name, TEMP_TABLE_PATH_PREFIX) == NULL) {
  1556. innobase_convert_to_system_charset(
  1557. strchr(old_name_cs_filename, '/') + 1,
  1558. strchr(old_name, '/') + 1,
  1559. MAX_TABLE_NAME_LEN, &errors);
  1560. if (errors) {
  1561. /* There has been an error to convert
  1562. old table into UTF-8. This probably
  1563. means that the old table name is
  1564. actually in UTF-8. */
  1565. innobase_convert_to_filename_charset(
  1566. strchr(old_name_cs_filename,
  1567. '/') + 1,
  1568. strchr(old_name, '/') + 1,
  1569. MAX_TABLE_NAME_LEN);
  1570. } else {
  1571. /* Old name already in
  1572. my_charset_filename */
  1573. strncpy(old_name_cs_filename, old_name,
  1574. MAX_TABLE_NAME_LEN);
  1575. }
  1576. }
  1577. strncpy(fkid, foreign->id, MAX_TABLE_NAME_LEN);
  1578. if (strstr(fkid, TEMP_TABLE_PATH_PREFIX) == NULL) {
  1579. innobase_convert_to_filename_charset(
  1580. strchr(fkid, '/') + 1,
  1581. strchr(foreign->id, '/') + 1,
  1582. MAX_TABLE_NAME_LEN+20);
  1583. } else {
  1584. on_tmp = TRUE;
  1585. }
  1586. old_id = mem_strdup(foreign->id);
  1587. if (ut_strlen(fkid) > ut_strlen(old_name_cs_filename)
  1588. + ((sizeof dict_ibfk) - 1)
  1589. && !memcmp(fkid, old_name_cs_filename,
  1590. ut_strlen(old_name_cs_filename))
  1591. && !memcmp(fkid + ut_strlen(old_name_cs_filename),
  1592. dict_ibfk, (sizeof dict_ibfk) - 1)) {
  1593. /* This is a generated >= 4.0.18 format id */
  1594. char table_name[MAX_TABLE_NAME_LEN] = "";
  1595. uint errors = 0;
  1596. if (strlen(table->name) > strlen(old_name)) {
  1597. foreign->id = static_cast<char*>(
  1598. mem_heap_alloc(
  1599. foreign->heap,
  1600. strlen(table->name)
  1601. + strlen(old_id) + 1));
  1602. }
  1603. /* Convert the table name to UTF-8 */
  1604. strncpy(table_name, table->name,
  1605. MAX_TABLE_NAME_LEN);
  1606. innobase_convert_to_system_charset(
  1607. strchr(table_name, '/') + 1,
  1608. strchr(table->name, '/') + 1,
  1609. MAX_TABLE_NAME_LEN, &errors);
  1610. if (errors) {
  1611. /* Table name could not be converted
  1612. from charset my_charset_filename to
  1613. UTF-8. This means that the table name
  1614. is already in UTF-8 (#mysql#50). */
  1615. strncpy(table_name, table->name,
  1616. MAX_TABLE_NAME_LEN);
  1617. }
  1618. /* Replace the prefix 'databasename/tablename'
  1619. with the new names */
  1620. strcpy(foreign->id, table_name);
  1621. if (on_tmp) {
  1622. strcat(foreign->id,
  1623. old_id + ut_strlen(old_name));
  1624. } else {
  1625. sprintf(strchr(foreign->id, '/') + 1,
  1626. "%s%s",
  1627. strchr(table_name, '/') +1,
  1628. strstr(old_id, "_ibfk_") );
  1629. }
  1630. } else {
  1631. /* This is a >= 4.0.18 format id where the user
  1632. gave the id name */
  1633. db_len = dict_get_db_name_len(table->name) + 1;
  1634. if (dict_get_db_name_len(table->name)
  1635. > dict_get_db_name_len(foreign->id)) {
  1636. foreign->id = static_cast<char*>(
  1637. mem_heap_alloc(
  1638. foreign->heap,
  1639. db_len + strlen(old_id) + 1));
  1640. }
  1641. /* Replace the database prefix in id with the
  1642. one from table->name */
  1643. ut_memcpy(foreign->id, table->name, db_len);
  1644. strcpy(foreign->id + db_len,
  1645. dict_remove_db_name(old_id));
  1646. }
  1647. mem_free(old_id);
  1648. }
  1649. table->foreign_set.erase(it);
  1650. fk_set.insert(foreign);
  1651. if (foreign->referenced_table) {
  1652. foreign->referenced_table->referenced_set.insert(foreign);
  1653. }
  1654. }
  1655. ut_a(table->foreign_set.empty());
  1656. table->foreign_set.swap(fk_set);
  1657. for (dict_foreign_set::iterator it = table->referenced_set.begin();
  1658. it != table->referenced_set.end();
  1659. ++it) {
  1660. foreign = *it;
  1661. if (ut_strlen(foreign->referenced_table_name)
  1662. < ut_strlen(table->name)) {
  1663. /* Allocate a longer name buffer;
  1664. TODO: store buf len to save memory */
  1665. foreign->referenced_table_name = mem_heap_strdup(
  1666. foreign->heap, table->name);
  1667. dict_mem_referenced_table_name_lookup_set(
  1668. foreign, TRUE);
  1669. } else {
  1670. /* Use the same buffer */
  1671. strcpy(foreign->referenced_table_name, table->name);
  1672. dict_mem_referenced_table_name_lookup_set(
  1673. foreign, FALSE);
  1674. }
  1675. }
  1676. return(DB_SUCCESS);
  1677. }
  1678. /**********************************************************************//**
  1679. Change the id of a table object in the dictionary cache. This is used in
  1680. DISCARD TABLESPACE. */
  1681. UNIV_INTERN
  1682. void
  1683. dict_table_change_id_in_cache(
  1684. /*==========================*/
  1685. dict_table_t* table, /*!< in/out: table object already in cache */
  1686. table_id_t new_id) /*!< in: new id to set */
  1687. {
  1688. ut_ad(table);
  1689. ut_ad(mutex_own(&(dict_sys->mutex)));
  1690. ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
  1691. /* Remove the table from the hash table of id's */
  1692. HASH_DELETE(dict_table_t, id_hash, dict_sys->table_id_hash,
  1693. ut_fold_ull(table->id), table);
  1694. table->id = new_id;
  1695. /* Add the table back to the hash table */
  1696. HASH_INSERT(dict_table_t, id_hash, dict_sys->table_id_hash,
  1697. ut_fold_ull(table->id), table);
  1698. }
  1699. /**********************************************************************//**
  1700. Removes a table object from the dictionary cache. */
  1701. void
  1702. dict_table_remove_from_cache_low(
  1703. /*=============================*/
  1704. dict_table_t* table, /*!< in, own: table */
  1705. ibool lru_evict) /*!< in: TRUE if table being evicted
  1706. to make room in the table LRU list */
  1707. {
  1708. dict_foreign_t* foreign;
  1709. dict_index_t* index;
  1710. ut_ad(table);
  1711. ut_ad(dict_lru_validate());
  1712. ut_a(table->n_ref_count == 0);
  1713. ut_a(table->n_rec_locks == 0);
  1714. ut_ad(mutex_own(&(dict_sys->mutex)));
  1715. ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
  1716. /* Remove the foreign constraints from the cache */
  1717. std::for_each(table->foreign_set.begin(), table->foreign_set.end(),
  1718. dict_foreign_remove_partial());
  1719. table->foreign_set.clear();
  1720. /* Reset table field in referencing constraints */
  1721. for (dict_foreign_set::iterator it = table->referenced_set.begin();
  1722. it != table->referenced_set.end();
  1723. ++it) {
  1724. foreign = *it;
  1725. foreign->referenced_table = NULL;
  1726. foreign->referenced_index = NULL;
  1727. }
  1728. /* The check for dropped index should happen before we release
  1729. all the indexes */
  1730. if (lru_evict && table->drop_aborted) {
  1731. /* When evicting the table definition,
  1732. drop the orphan indexes from the data dictionary
  1733. and free the index pages. */
  1734. trx_t* trx = trx_allocate_for_background();
  1735. ut_ad(mutex_own(&dict_sys->mutex));
  1736. #ifdef UNIV_SYNC_DEBUG
  1737. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  1738. #endif /* UNIV_SYNC_DEBUG */
  1739. /* Mimic row_mysql_lock_data_dictionary(). */
  1740. trx->dict_operation_lock_mode = RW_X_LATCH;
  1741. trx_set_dict_operation(trx, TRX_DICT_OP_INDEX);
  1742. row_merge_drop_indexes_dict(trx, table->id);
  1743. trx_commit_for_mysql(trx);
  1744. trx->dict_operation_lock_mode = 0;
  1745. trx_free_for_background(trx);
  1746. }
  1747. /* Remove the indexes from the cache */
  1748. for (index = UT_LIST_GET_LAST(table->indexes);
  1749. index != NULL;
  1750. index = UT_LIST_GET_LAST(table->indexes)) {
  1751. dict_index_remove_from_cache_low(table, index, lru_evict);
  1752. }
  1753. /* Remove table from the hash tables of tables */
  1754. HASH_DELETE(dict_table_t, name_hash, dict_sys->table_hash,
  1755. ut_fold_string(table->name), table);
  1756. HASH_DELETE(dict_table_t, id_hash, dict_sys->table_id_hash,
  1757. ut_fold_ull(table->id), table);
  1758. /* Remove table from LRU or non-LRU list. */
  1759. if (table->can_be_evicted) {
  1760. ut_ad(dict_lru_find_table(table));
  1761. UT_LIST_REMOVE(table_LRU, dict_sys->table_LRU, table);
  1762. } else {
  1763. ut_ad(dict_non_lru_find_table(table));
  1764. UT_LIST_REMOVE(table_LRU, dict_sys->table_non_LRU, table);
  1765. }
  1766. ut_ad(dict_lru_validate());
  1767. if (lru_evict) {
  1768. dict_table_autoinc_store(table);
  1769. }
  1770. dict_mem_table_free(table);
  1771. }
  1772. /**********************************************************************//**
  1773. Removes a table object from the dictionary cache. */
  1774. UNIV_INTERN
  1775. void
  1776. dict_table_remove_from_cache(
  1777. /*=========================*/
  1778. dict_table_t* table) /*!< in, own: table */
  1779. {
  1780. dict_table_remove_from_cache_low(table, FALSE);
  1781. }
  1782. /****************************************************************//**
  1783. If the given column name is reserved for InnoDB system columns, return
  1784. TRUE.
  1785. @return TRUE if name is reserved */
  1786. UNIV_INTERN
  1787. ibool
  1788. dict_col_name_is_reserved(
  1789. /*======================*/
  1790. const char* name) /*!< in: column name */
  1791. {
  1792. /* This check reminds that if a new system column is added to
  1793. the program, it should be dealt with here. */
  1794. #if DATA_N_SYS_COLS != 3
  1795. #error "DATA_N_SYS_COLS != 3"
  1796. #endif
  1797. static const char* reserved_names[] = {
  1798. "DB_ROW_ID", "DB_TRX_ID", "DB_ROLL_PTR"
  1799. };
  1800. ulint i;
  1801. for (i = 0; i < UT_ARR_SIZE(reserved_names); i++) {
  1802. if (innobase_strcasecmp(name, reserved_names[i]) == 0) {
  1803. return(TRUE);
  1804. }
  1805. }
  1806. return(FALSE);
  1807. }
  1808. #if 1 /* This function is not very accurate at determining
  1809. whether an UNDO record will be too big. See innodb_4k.test,
  1810. Bug 13336585, for a testcase that shows an index that can
  1811. be created but cannot be updated. */
  1812. /****************************************************************//**
  1813. If an undo log record for this table might not fit on a single page,
  1814. return TRUE.
  1815. @return TRUE if the undo log record could become too big */
  1816. static
  1817. ibool
  1818. dict_index_too_big_for_undo(
  1819. /*========================*/
  1820. const dict_table_t* table, /*!< in: table */
  1821. const dict_index_t* new_index) /*!< in: index */
  1822. {
  1823. /* Make sure that all column prefixes will fit in the undo log record
  1824. in trx_undo_page_report_modify() right after trx_undo_page_init(). */
  1825. ulint i;
  1826. const dict_index_t* clust_index
  1827. = dict_table_get_first_index(table);
  1828. ulint undo_page_len
  1829. = TRX_UNDO_PAGE_HDR - TRX_UNDO_PAGE_HDR_SIZE
  1830. + 2 /* next record pointer */
  1831. + 1 /* type_cmpl */
  1832. + 11 /* trx->undo_no */ + 11 /* table->id */
  1833. + 1 /* rec_get_info_bits() */
  1834. + 11 /* DB_TRX_ID */
  1835. + 11 /* DB_ROLL_PTR */
  1836. + 10 + FIL_PAGE_DATA_END /* trx_undo_left() */
  1837. + 2/* pointer to previous undo log record */;
  1838. /* FTS index consists of auxiliary tables, they shall be excluded from
  1839. index row size check */
  1840. if (new_index->type & DICT_FTS) {
  1841. return(false);
  1842. }
  1843. if (!clust_index) {
  1844. ut_a(dict_index_is_clust(new_index));
  1845. clust_index = new_index;
  1846. }
  1847. /* Add the size of the ordering columns in the
  1848. clustered index. */
  1849. for (i = 0; i < clust_index->n_uniq; i++) {
  1850. const dict_col_t* col
  1851. = dict_index_get_nth_col(clust_index, i);
  1852. /* Use the maximum output size of
  1853. mach_write_compressed(), although the encoded
  1854. length should always fit in 2 bytes. */
  1855. undo_page_len += 5 + dict_col_get_max_size(col);
  1856. }
  1857. /* Add the old values of the columns to be updated.
  1858. First, the amount and the numbers of the columns.
  1859. These are written by mach_write_compressed() whose
  1860. maximum output length is 5 bytes. However, given that
  1861. the quantities are below REC_MAX_N_FIELDS (10 bits),
  1862. the maximum length is 2 bytes per item. */
  1863. undo_page_len += 2 * (dict_table_get_n_cols(table) + 1);
  1864. for (i = 0; i < clust_index->n_def; i++) {
  1865. const dict_col_t* col
  1866. = dict_index_get_nth_col(clust_index, i);
  1867. ulint max_size
  1868. = dict_col_get_max_size(col);
  1869. ulint fixed_size
  1870. = dict_col_get_fixed_size(col,
  1871. dict_table_is_comp(table));
  1872. ulint max_prefix
  1873. = col->max_prefix;
  1874. if (fixed_size) {
  1875. /* Fixed-size columns are stored locally. */
  1876. max_size = fixed_size;
  1877. } else if (max_size <= BTR_EXTERN_FIELD_REF_SIZE * 2) {
  1878. /* Short columns are stored locally. */
  1879. } else if (!col->ord_part
  1880. || (col->max_prefix
  1881. < (ulint) DICT_MAX_FIELD_LEN_BY_FORMAT(table))) {
  1882. /* See if col->ord_part would be set
  1883. because of new_index. Also check if the new
  1884. index could have longer prefix on columns
  1885. that already had ord_part set */
  1886. ulint j;
  1887. for (j = 0; j < new_index->n_uniq; j++) {
  1888. if (dict_index_get_nth_col(
  1889. new_index, j) == col) {
  1890. const dict_field_t* field
  1891. = dict_index_get_nth_field(
  1892. new_index, j);
  1893. if (field->prefix_len
  1894. > col->max_prefix) {
  1895. max_prefix =
  1896. field->prefix_len;
  1897. }
  1898. goto is_ord_part;
  1899. }
  1900. }
  1901. if (col->ord_part) {
  1902. goto is_ord_part;
  1903. }
  1904. /* This is not an ordering column in any index.
  1905. Thus, it can be stored completely externally. */
  1906. max_size = BTR_EXTERN_FIELD_REF_SIZE;
  1907. } else {
  1908. ulint max_field_len;
  1909. is_ord_part:
  1910. max_field_len = DICT_MAX_FIELD_LEN_BY_FORMAT(table);
  1911. /* This is an ordering column in some index.
  1912. A long enough prefix must be written to the
  1913. undo log. See trx_undo_page_fetch_ext(). */
  1914. max_size = ut_min(max_size, max_field_len);
  1915. /* We only store the needed prefix length in undo log */
  1916. if (max_prefix) {
  1917. ut_ad(dict_table_get_format(table)
  1918. >= UNIV_FORMAT_B);
  1919. max_size = ut_min(max_prefix, max_size);
  1920. }
  1921. max_size += BTR_EXTERN_FIELD_REF_SIZE;
  1922. }
  1923. undo_page_len += 5 + max_size;
  1924. }
  1925. return(undo_page_len >= UNIV_PAGE_SIZE);
  1926. }
  1927. #endif
  1928. /****************************************************************//**
  1929. If a record of this index might not fit on a single B-tree page,
  1930. return TRUE.
  1931. @return TRUE if the index record could become too big */
  1932. static
  1933. ibool
  1934. dict_index_too_big_for_tree(
  1935. /*========================*/
  1936. const dict_table_t* table, /*!< in: table */
  1937. const dict_index_t* new_index) /*!< in: index */
  1938. {
  1939. ulint zip_size;
  1940. ulint comp;
  1941. ulint i;
  1942. /* maximum possible storage size of a record */
  1943. ulint rec_max_size;
  1944. /* maximum allowed size of a record on a leaf page */
  1945. ulint page_rec_max;
  1946. /* maximum allowed size of a node pointer record */
  1947. ulint page_ptr_max;
  1948. /* FTS index consists of auxiliary tables, they shall be excluded from
  1949. index row size check */
  1950. if (new_index->type & DICT_FTS) {
  1951. return(false);
  1952. }
  1953. DBUG_EXECUTE_IF(
  1954. "ib_force_create_table",
  1955. return(FALSE););
  1956. comp = dict_table_is_comp(table);
  1957. zip_size = dict_table_zip_size(table);
  1958. if (zip_size && zip_size < UNIV_PAGE_SIZE) {
  1959. /* On a compressed page, two records must fit in the
  1960. uncompressed page modification log. On compressed
  1961. pages with zip_size == UNIV_PAGE_SIZE, this limit will
  1962. never be reached. */
  1963. ut_ad(comp);
  1964. /* The maximum allowed record size is the size of
  1965. an empty page, minus a byte for recoding the heap
  1966. number in the page modification log. The maximum
  1967. allowed node pointer size is half that. */
  1968. page_rec_max = page_zip_empty_size(new_index->n_fields,
  1969. zip_size);
  1970. if (page_rec_max) {
  1971. page_rec_max--;
  1972. }
  1973. page_ptr_max = page_rec_max / 2;
  1974. /* On a compressed page, there is a two-byte entry in
  1975. the dense page directory for every record. But there
  1976. is no record header. */
  1977. rec_max_size = 2;
  1978. } else {
  1979. /* The maximum allowed record size is half a B-tree
  1980. page(16k for 64k page size). No additional sparse
  1981. page directory entry will be generated for the first
  1982. few user records. */
  1983. page_rec_max = (comp || UNIV_PAGE_SIZE < UNIV_PAGE_SIZE_MAX)
  1984. ? page_get_free_space_of_empty(comp) / 2
  1985. : REDUNDANT_REC_MAX_DATA_SIZE;
  1986. page_ptr_max = page_rec_max;
  1987. /* Each record has a header. */
  1988. rec_max_size = comp
  1989. ? REC_N_NEW_EXTRA_BYTES
  1990. : REC_N_OLD_EXTRA_BYTES;
  1991. }
  1992. if (comp) {
  1993. /* Include the "null" flags in the
  1994. maximum possible record size. */
  1995. rec_max_size += UT_BITS_IN_BYTES(new_index->n_nullable);
  1996. } else {
  1997. /* For each column, include a 2-byte offset and a
  1998. "null" flag. The 1-byte format is only used in short
  1999. records that do not contain externally stored columns.
  2000. Such records could never exceed the page limit, even
  2001. when using the 2-byte format. */
  2002. rec_max_size += 2 * new_index->n_fields;
  2003. }
  2004. /* Compute the maximum possible record size. */
  2005. for (i = 0; i < new_index->n_fields; i++) {
  2006. const dict_field_t* field
  2007. = dict_index_get_nth_field(new_index, i);
  2008. const dict_col_t* col
  2009. = dict_field_get_col(field);
  2010. ulint field_max_size;
  2011. ulint field_ext_max_size;
  2012. /* In dtuple_convert_big_rec(), variable-length columns
  2013. that are longer than BTR_EXTERN_FIELD_REF_SIZE * 2
  2014. may be chosen for external storage.
  2015. Fixed-length columns, and all columns of secondary
  2016. index records are always stored inline. */
  2017. /* Determine the maximum length of the index field.
  2018. The field_ext_max_size should be computed as the worst
  2019. case in rec_get_converted_size_comp() for
  2020. REC_STATUS_ORDINARY records. */
  2021. field_max_size = dict_col_get_fixed_size(col, comp);
  2022. if (field_max_size) {
  2023. /* dict_index_add_col() should guarantee this */
  2024. ut_ad(!field->prefix_len
  2025. || field->fixed_len == field->prefix_len);
  2026. /* Fixed lengths are not encoded
  2027. in ROW_FORMAT=COMPACT. */
  2028. field_ext_max_size = 0;
  2029. goto add_field_size;
  2030. }
  2031. field_max_size = dict_col_get_max_size(col);
  2032. field_ext_max_size = field_max_size < 256 ? 1 : 2;
  2033. if (field->prefix_len) {
  2034. if (field->prefix_len < field_max_size) {
  2035. field_max_size = field->prefix_len;
  2036. }
  2037. } else if (field_max_size > BTR_EXTERN_FIELD_REF_SIZE * 2
  2038. && dict_index_is_clust(new_index)) {
  2039. /* In the worst case, we have a locally stored
  2040. column of BTR_EXTERN_FIELD_REF_SIZE * 2 bytes.
  2041. The length can be stored in one byte. If the
  2042. column were stored externally, the lengths in
  2043. the clustered index page would be
  2044. BTR_EXTERN_FIELD_REF_SIZE and 2. */
  2045. field_max_size = BTR_EXTERN_FIELD_REF_SIZE * 2;
  2046. field_ext_max_size = 1;
  2047. }
  2048. if (comp) {
  2049. /* Add the extra size for ROW_FORMAT=COMPACT.
  2050. For ROW_FORMAT=REDUNDANT, these bytes were
  2051. added to rec_max_size before this loop. */
  2052. rec_max_size += field_ext_max_size;
  2053. }
  2054. add_field_size:
  2055. rec_max_size += field_max_size;
  2056. /* Check the size limit on leaf pages. */
  2057. if (UNIV_UNLIKELY(rec_max_size >= page_rec_max)) {
  2058. return(TRUE);
  2059. }
  2060. /* Check the size limit on non-leaf pages. Records
  2061. stored in non-leaf B-tree pages consist of the unique
  2062. columns of the record (the key columns of the B-tree)
  2063. and a node pointer field. When we have processed the
  2064. unique columns, rec_max_size equals the size of the
  2065. node pointer record minus the node pointer column. */
  2066. if (i + 1 == dict_index_get_n_unique_in_tree(new_index)
  2067. && rec_max_size + REC_NODE_PTR_SIZE >= page_ptr_max) {
  2068. return(TRUE);
  2069. }
  2070. }
  2071. return(FALSE);
  2072. }
  2073. /**********************************************************************//**
  2074. Adds an index to the dictionary cache.
  2075. @return DB_SUCCESS, DB_TOO_BIG_RECORD, or DB_CORRUPTION */
  2076. UNIV_INTERN
  2077. dberr_t
  2078. dict_index_add_to_cache(
  2079. /*====================*/
  2080. dict_table_t* table, /*!< in: table on which the index is */
  2081. dict_index_t* index, /*!< in, own: index; NOTE! The index memory
  2082. object is freed in this function! */
  2083. ulint page_no,/*!< in: root page number of the index */
  2084. ibool strict) /*!< in: TRUE=refuse to create the index
  2085. if records could be too big to fit in
  2086. an B-tree page */
  2087. {
  2088. dict_index_t* new_index;
  2089. ulint n_ord;
  2090. ulint i;
  2091. ut_ad(index);
  2092. ut_ad(mutex_own(&(dict_sys->mutex)));
  2093. ut_ad(index->n_def == index->n_fields);
  2094. ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
  2095. ut_ad(!dict_index_is_online_ddl(index));
  2096. ut_ad(mem_heap_validate(index->heap));
  2097. ut_a(!dict_index_is_clust(index)
  2098. || UT_LIST_GET_LEN(table->indexes) == 0);
  2099. if (!dict_index_find_cols(table, index)) {
  2100. dict_mem_index_free(index);
  2101. return(DB_CORRUPTION);
  2102. }
  2103. /* Build the cache internal representation of the index,
  2104. containing also the added system fields */
  2105. if (index->type == DICT_FTS) {
  2106. new_index = dict_index_build_internal_fts(table, index);
  2107. } else if (dict_index_is_clust(index)) {
  2108. new_index = dict_index_build_internal_clust(table, index);
  2109. } else {
  2110. new_index = dict_index_build_internal_non_clust(table, index);
  2111. }
  2112. /* Set the n_fields value in new_index to the actual defined
  2113. number of fields in the cache internal representation */
  2114. new_index->n_fields = new_index->n_def;
  2115. new_index->trx_id = index->trx_id;
  2116. if (dict_index_too_big_for_tree(table, new_index)) {
  2117. if (strict) {
  2118. too_big:
  2119. dict_mem_index_free(new_index);
  2120. dict_mem_index_free(index);
  2121. return(DB_TOO_BIG_RECORD);
  2122. } else if (current_thd != NULL) {
  2123. /* Avoid the warning to be printed
  2124. during recovery. */
  2125. ib_warn_row_too_big(table);
  2126. }
  2127. }
  2128. if (dict_index_is_univ(index)) {
  2129. n_ord = new_index->n_fields;
  2130. } else {
  2131. n_ord = new_index->n_uniq;
  2132. }
  2133. #if 1 /* The following code predetermines whether to call
  2134. dict_index_too_big_for_undo(). This function is not
  2135. accurate. See innodb_4k.test, Bug 13336585, for a
  2136. testcase that shows an index that can be created but
  2137. cannot be updated. */
  2138. switch (dict_table_get_format(table)) {
  2139. case UNIV_FORMAT_A:
  2140. /* ROW_FORMAT=REDUNDANT and ROW_FORMAT=COMPACT store
  2141. prefixes of externally stored columns locally within
  2142. the record. There are no special considerations for
  2143. the undo log record size. */
  2144. goto undo_size_ok;
  2145. case UNIV_FORMAT_B:
  2146. /* In ROW_FORMAT=DYNAMIC and ROW_FORMAT=COMPRESSED,
  2147. column prefix indexes require that prefixes of
  2148. externally stored columns are written to the undo log.
  2149. This may make the undo log record bigger than the
  2150. record on the B-tree page. The maximum size of an
  2151. undo log record is the page size. That must be
  2152. checked for below. */
  2153. break;
  2154. #if UNIV_FORMAT_B != UNIV_FORMAT_MAX
  2155. # error "UNIV_FORMAT_B != UNIV_FORMAT_MAX"
  2156. #endif
  2157. }
  2158. for (i = 0; i < n_ord; i++) {
  2159. const dict_field_t* field
  2160. = dict_index_get_nth_field(new_index, i);
  2161. const dict_col_t* col
  2162. = dict_field_get_col(field);
  2163. /* In dtuple_convert_big_rec(), variable-length columns
  2164. that are longer than BTR_EXTERN_FIELD_REF_SIZE * 2
  2165. may be chosen for external storage. If the column appears
  2166. in an ordering column of an index, a longer prefix determined
  2167. by dict_max_field_len_store_undo() will be copied to the undo
  2168. log by trx_undo_page_report_modify() and
  2169. trx_undo_page_fetch_ext(). It suffices to check the
  2170. capacity of the undo log whenever new_index includes
  2171. a column prefix on a column that may be stored externally. */
  2172. if (field->prefix_len /* prefix index */
  2173. && (!col->ord_part /* not yet ordering column */
  2174. || field->prefix_len > col->max_prefix)
  2175. && !dict_col_get_fixed_size(col, TRUE) /* variable-length */
  2176. && dict_col_get_max_size(col)
  2177. > BTR_EXTERN_FIELD_REF_SIZE * 2 /* long enough */) {
  2178. if (dict_index_too_big_for_undo(table, new_index)) {
  2179. /* An undo log record might not fit in
  2180. a single page. Refuse to create this index. */
  2181. goto too_big;
  2182. }
  2183. break;
  2184. }
  2185. }
  2186. undo_size_ok:
  2187. #endif
  2188. /* Flag the ordering columns and also set column max_prefix */
  2189. for (i = 0; i < n_ord; i++) {
  2190. const dict_field_t* field
  2191. = dict_index_get_nth_field(new_index, i);
  2192. field->col->ord_part = 1;
  2193. if (field->prefix_len > field->col->max_prefix) {
  2194. field->col->max_prefix = field->prefix_len;
  2195. }
  2196. }
  2197. if (!dict_index_is_univ(new_index)) {
  2198. new_index->stat_n_diff_key_vals =
  2199. static_cast<ib_uint64_t*>(mem_heap_zalloc(
  2200. new_index->heap,
  2201. dict_index_get_n_unique(new_index)
  2202. * sizeof(*new_index->stat_n_diff_key_vals)));
  2203. new_index->stat_n_sample_sizes =
  2204. static_cast<ib_uint64_t*>(mem_heap_zalloc(
  2205. new_index->heap,
  2206. dict_index_get_n_unique(new_index)
  2207. * sizeof(*new_index->stat_n_sample_sizes)));
  2208. new_index->stat_n_non_null_key_vals =
  2209. static_cast<ib_uint64_t*>(mem_heap_zalloc(
  2210. new_index->heap,
  2211. dict_index_get_n_unique(new_index)
  2212. * sizeof(*new_index->stat_n_non_null_key_vals)));
  2213. }
  2214. new_index->stat_index_size = 1;
  2215. new_index->stat_n_leaf_pages = 1;
  2216. new_index->stat_defrag_n_pages_freed = 0;
  2217. new_index->stat_defrag_n_page_split = 0;
  2218. new_index->stat_defrag_sample_next_slot = 0;
  2219. memset(&new_index->stat_defrag_data_size_sample,
  2220. 0x0, sizeof(ulint) * STAT_DEFRAG_DATA_SIZE_N_SAMPLE);
  2221. /* Add the new index as the last index for the table */
  2222. UT_LIST_ADD_LAST(indexes, table->indexes, new_index);
  2223. new_index->table = table;
  2224. new_index->table_name = table->name;
  2225. new_index->search_info = btr_search_info_create(new_index->heap);
  2226. new_index->page = page_no;
  2227. rw_lock_create(index_tree_rw_lock_key, &new_index->lock,
  2228. dict_index_is_ibuf(index)
  2229. ? SYNC_IBUF_INDEX_TREE : SYNC_INDEX_TREE);
  2230. dict_mem_index_free(index);
  2231. return(DB_SUCCESS);
  2232. }
  2233. /**********************************************************************//**
  2234. Removes an index from the dictionary cache. */
  2235. static
  2236. void
  2237. dict_index_remove_from_cache_low(
  2238. /*=============================*/
  2239. dict_table_t* table, /*!< in/out: table */
  2240. dict_index_t* index, /*!< in, own: index */
  2241. ibool lru_evict) /*!< in: TRUE if index being evicted
  2242. to make room in the table LRU list */
  2243. {
  2244. ulint retries = 0;
  2245. btr_search_t* info;
  2246. ut_ad(table && index);
  2247. ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
  2248. ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
  2249. ut_ad(mutex_own(&(dict_sys->mutex)));
  2250. /* No need to acquire the dict_index_t::lock here because
  2251. there can't be any active operations on this index (or table). */
  2252. if (index->online_log) {
  2253. ut_ad(index->online_status == ONLINE_INDEX_CREATION);
  2254. row_log_free(index->online_log);
  2255. }
  2256. /* We always create search info whether or not adaptive
  2257. hash index is enabled or not. */
  2258. info = btr_search_get_info(index);
  2259. ut_ad(info);
  2260. /* We are not allowed to free the in-memory index struct
  2261. dict_index_t until all entries in the adaptive hash index
  2262. that point to any of the page belonging to his b-tree index
  2263. are dropped. This is so because dropping of these entries
  2264. require access to dict_index_t struct. To avoid such scenario
  2265. We keep a count of number of such pages in the search_info and
  2266. only free the dict_index_t struct when this count drops to
  2267. zero. See also: dict_table_can_be_evicted() */
  2268. do {
  2269. if (!btr_search_info_get_ref_count(info, index)
  2270. || !buf_LRU_drop_page_hash_for_tablespace(table)) {
  2271. break;
  2272. }
  2273. ut_a(++retries < 10000);
  2274. } while (srv_shutdown_state == SRV_SHUTDOWN_NONE || !lru_evict);
  2275. rw_lock_free(&index->lock);
  2276. /* Remove the index from the list of indexes of the table */
  2277. UT_LIST_REMOVE(indexes, table->indexes, index);
  2278. dict_mem_index_free(index);
  2279. }
  2280. /**********************************************************************//**
  2281. Removes an index from the dictionary cache. */
  2282. UNIV_INTERN
  2283. void
  2284. dict_index_remove_from_cache(
  2285. /*=========================*/
  2286. dict_table_t* table, /*!< in/out: table */
  2287. dict_index_t* index) /*!< in, own: index */
  2288. {
  2289. dict_index_remove_from_cache_low(table, index, FALSE);
  2290. }
  2291. /*******************************************************************//**
  2292. Tries to find column names for the index and sets the col field of the
  2293. index.
  2294. @return TRUE if the column names were found */
  2295. static
  2296. ibool
  2297. dict_index_find_cols(
  2298. /*=================*/
  2299. dict_table_t* table, /*!< in: table */
  2300. dict_index_t* index) /*!< in: index */
  2301. {
  2302. ulint i;
  2303. ut_ad(table && index);
  2304. ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
  2305. ut_ad(mutex_own(&(dict_sys->mutex)));
  2306. for (i = 0; i < index->n_fields; i++) {
  2307. ulint j;
  2308. dict_field_t* field = dict_index_get_nth_field(index, i);
  2309. for (j = 0; j < table->n_cols; j++) {
  2310. if (!innobase_strcasecmp(dict_table_get_col_name(table, j),
  2311. field->name)) {
  2312. field->col = dict_table_get_nth_col(table, j);
  2313. goto found;
  2314. }
  2315. }
  2316. #ifdef UNIV_DEBUG
  2317. /* It is an error not to find a matching column. */
  2318. fputs("InnoDB: Error: no matching column for ", stderr);
  2319. ut_print_name(stderr, NULL, FALSE, field->name);
  2320. fputs(" in ", stderr);
  2321. dict_index_name_print(stderr, NULL, index);
  2322. fputs("!\n", stderr);
  2323. #endif /* UNIV_DEBUG */
  2324. return(FALSE);
  2325. found:
  2326. ;
  2327. }
  2328. return(TRUE);
  2329. }
  2330. #endif /* !UNIV_HOTBACKUP */
  2331. /*******************************************************************//**
  2332. Adds a column to index. */
  2333. UNIV_INTERN
  2334. void
  2335. dict_index_add_col(
  2336. /*===============*/
  2337. dict_index_t* index, /*!< in/out: index */
  2338. const dict_table_t* table, /*!< in: table */
  2339. dict_col_t* col, /*!< in: column */
  2340. ulint prefix_len) /*!< in: column prefix length */
  2341. {
  2342. dict_field_t* field;
  2343. const char* col_name;
  2344. col_name = dict_table_get_col_name(table, dict_col_get_no(col));
  2345. dict_mem_index_add_field(index, col_name, prefix_len);
  2346. field = dict_index_get_nth_field(index, index->n_def - 1);
  2347. field->col = col;
  2348. field->fixed_len = (unsigned int) dict_col_get_fixed_size(
  2349. col, dict_table_is_comp(table));
  2350. if (prefix_len && field->fixed_len > prefix_len) {
  2351. field->fixed_len = (unsigned int) prefix_len;
  2352. }
  2353. /* Long fixed-length fields that need external storage are treated as
  2354. variable-length fields, so that the extern flag can be embedded in
  2355. the length word. */
  2356. if (field->fixed_len > DICT_MAX_FIXED_COL_LEN) {
  2357. field->fixed_len = 0;
  2358. }
  2359. #if DICT_MAX_FIXED_COL_LEN != 768
  2360. /* The comparison limit above must be constant. If it were
  2361. changed, the disk format of some fixed-length columns would
  2362. change, which would be a disaster. */
  2363. # error "DICT_MAX_FIXED_COL_LEN != 768"
  2364. #endif
  2365. if (!(col->prtype & DATA_NOT_NULL)) {
  2366. index->n_nullable++;
  2367. }
  2368. }
  2369. #ifndef UNIV_HOTBACKUP
  2370. /*******************************************************************//**
  2371. Copies fields contained in index2 to index1. */
  2372. static
  2373. void
  2374. dict_index_copy(
  2375. /*============*/
  2376. dict_index_t* index1, /*!< in: index to copy to */
  2377. dict_index_t* index2, /*!< in: index to copy from */
  2378. const dict_table_t* table, /*!< in: table */
  2379. ulint start, /*!< in: first position to copy */
  2380. ulint end) /*!< in: last position to copy */
  2381. {
  2382. dict_field_t* field;
  2383. ulint i;
  2384. /* Copy fields contained in index2 */
  2385. for (i = start; i < end; i++) {
  2386. field = dict_index_get_nth_field(index2, i);
  2387. dict_index_add_col(index1, table, field->col,
  2388. field->prefix_len);
  2389. }
  2390. }
  2391. /*******************************************************************//**
  2392. Copies types of fields contained in index to tuple. */
  2393. UNIV_INTERN
  2394. void
  2395. dict_index_copy_types(
  2396. /*==================*/
  2397. dtuple_t* tuple, /*!< in/out: data tuple */
  2398. const dict_index_t* index, /*!< in: index */
  2399. ulint n_fields) /*!< in: number of
  2400. field types to copy */
  2401. {
  2402. ulint i;
  2403. if (dict_index_is_univ(index)) {
  2404. dtuple_set_types_binary(tuple, n_fields);
  2405. return;
  2406. }
  2407. for (i = 0; i < n_fields; i++) {
  2408. const dict_field_t* ifield;
  2409. dtype_t* dfield_type;
  2410. ifield = dict_index_get_nth_field(index, i);
  2411. dfield_type = dfield_get_type(dtuple_get_nth_field(tuple, i));
  2412. dict_col_copy_type(dict_field_get_col(ifield), dfield_type);
  2413. }
  2414. }
  2415. /*******************************************************************//**
  2416. Copies types of columns contained in table to tuple and sets all
  2417. fields of the tuple to the SQL NULL value. This function should
  2418. be called right after dtuple_create(). */
  2419. UNIV_INTERN
  2420. void
  2421. dict_table_copy_types(
  2422. /*==================*/
  2423. dtuple_t* tuple, /*!< in/out: data tuple */
  2424. const dict_table_t* table) /*!< in: table */
  2425. {
  2426. ulint i;
  2427. for (i = 0; i < dtuple_get_n_fields(tuple); i++) {
  2428. dfield_t* dfield = dtuple_get_nth_field(tuple, i);
  2429. dtype_t* dtype = dfield_get_type(dfield);
  2430. dfield_set_null(dfield);
  2431. dict_col_copy_type(dict_table_get_nth_col(table, i), dtype);
  2432. }
  2433. }
  2434. /********************************************************************
  2435. Wait until all the background threads of the given table have exited, i.e.,
  2436. bg_threads == 0. Note: bg_threads_mutex must be reserved when
  2437. calling this. */
  2438. UNIV_INTERN
  2439. void
  2440. dict_table_wait_for_bg_threads_to_exit(
  2441. /*===================================*/
  2442. dict_table_t* table, /*< in: table */
  2443. ulint delay) /*< in: time in microseconds to wait between
  2444. checks of bg_threads. */
  2445. {
  2446. fts_t* fts = table->fts;
  2447. #ifdef UNIV_SYNC_DEBUG
  2448. ut_ad(mutex_own(&fts->bg_threads_mutex));
  2449. #endif /* UNIV_SYNC_DEBUG */
  2450. while (fts->bg_threads > 0) {
  2451. mutex_exit(&fts->bg_threads_mutex);
  2452. os_thread_sleep(delay);
  2453. mutex_enter(&fts->bg_threads_mutex);
  2454. }
  2455. }
  2456. /*******************************************************************//**
  2457. Builds the internal dictionary cache representation for a clustered
  2458. index, containing also system fields not defined by the user.
  2459. @return own: the internal representation of the clustered index */
  2460. static
  2461. dict_index_t*
  2462. dict_index_build_internal_clust(
  2463. /*============================*/
  2464. const dict_table_t* table, /*!< in: table */
  2465. dict_index_t* index) /*!< in: user representation of
  2466. a clustered index */
  2467. {
  2468. dict_index_t* new_index;
  2469. dict_field_t* field;
  2470. ulint trx_id_pos;
  2471. ulint i;
  2472. ibool* indexed;
  2473. ut_ad(table && index);
  2474. ut_ad(dict_index_is_clust(index));
  2475. ut_ad(mutex_own(&(dict_sys->mutex)));
  2476. ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
  2477. /* Create a new index object with certainly enough fields */
  2478. new_index = dict_mem_index_create(table->name,
  2479. index->name, table->space,
  2480. index->type,
  2481. index->n_fields + table->n_cols);
  2482. /* Copy other relevant data from the old index struct to the new
  2483. struct: it inherits the values */
  2484. new_index->n_user_defined_cols = index->n_fields;
  2485. new_index->id = index->id;
  2486. btr_search_index_init(new_index);
  2487. /* Copy the fields of index */
  2488. dict_index_copy(new_index, index, table, 0, index->n_fields);
  2489. if (dict_index_is_univ(index)) {
  2490. /* No fixed number of fields determines an entry uniquely */
  2491. new_index->n_uniq = REC_MAX_N_FIELDS;
  2492. } else if (dict_index_is_unique(index)) {
  2493. /* Only the fields defined so far are needed to identify
  2494. the index entry uniquely */
  2495. new_index->n_uniq = new_index->n_def;
  2496. } else {
  2497. /* Also the row id is needed to identify the entry */
  2498. new_index->n_uniq = 1 + new_index->n_def;
  2499. }
  2500. new_index->trx_id_offset = 0;
  2501. if (!dict_index_is_ibuf(index)) {
  2502. /* Add system columns, trx id first */
  2503. trx_id_pos = new_index->n_def;
  2504. #if DATA_ROW_ID != 0
  2505. # error "DATA_ROW_ID != 0"
  2506. #endif
  2507. #if DATA_TRX_ID != 1
  2508. # error "DATA_TRX_ID != 1"
  2509. #endif
  2510. #if DATA_ROLL_PTR != 2
  2511. # error "DATA_ROLL_PTR != 2"
  2512. #endif
  2513. if (!dict_index_is_unique(index)) {
  2514. dict_index_add_col(new_index, table,
  2515. dict_table_get_sys_col(
  2516. table, DATA_ROW_ID),
  2517. 0);
  2518. trx_id_pos++;
  2519. }
  2520. dict_index_add_col(new_index, table,
  2521. dict_table_get_sys_col(table, DATA_TRX_ID),
  2522. 0);
  2523. dict_index_add_col(new_index, table,
  2524. dict_table_get_sys_col(table,
  2525. DATA_ROLL_PTR),
  2526. 0);
  2527. for (i = 0; i < trx_id_pos; i++) {
  2528. ulint fixed_size = dict_col_get_fixed_size(
  2529. dict_index_get_nth_col(new_index, i),
  2530. dict_table_is_comp(table));
  2531. if (fixed_size == 0) {
  2532. new_index->trx_id_offset = 0;
  2533. break;
  2534. }
  2535. if (dict_index_get_nth_field(new_index, i)->prefix_len
  2536. > 0) {
  2537. new_index->trx_id_offset = 0;
  2538. break;
  2539. }
  2540. /* Add fixed_size to new_index->trx_id_offset.
  2541. Because the latter is a bit-field, an overflow
  2542. can theoretically occur. Check for it. */
  2543. fixed_size += new_index->trx_id_offset;
  2544. new_index->trx_id_offset = fixed_size;
  2545. if (new_index->trx_id_offset != fixed_size) {
  2546. /* Overflow. Pretend that this is a
  2547. variable-length PRIMARY KEY. */
  2548. ut_ad(0);
  2549. new_index->trx_id_offset = 0;
  2550. break;
  2551. }
  2552. }
  2553. }
  2554. /* Remember the table columns already contained in new_index */
  2555. indexed = static_cast<ibool*>(
  2556. mem_zalloc(table->n_cols * sizeof *indexed));
  2557. /* Mark the table columns already contained in new_index */
  2558. for (i = 0; i < new_index->n_def; i++) {
  2559. field = dict_index_get_nth_field(new_index, i);
  2560. /* If there is only a prefix of the column in the index
  2561. field, do not mark the column as contained in the index */
  2562. if (field->prefix_len == 0) {
  2563. indexed[field->col->ind] = TRUE;
  2564. }
  2565. }
  2566. /* Add to new_index non-system columns of table not yet included
  2567. there */
  2568. for (i = 0; i + DATA_N_SYS_COLS < (ulint) table->n_cols; i++) {
  2569. dict_col_t* col = dict_table_get_nth_col(table, i);
  2570. ut_ad(col->mtype != DATA_SYS);
  2571. if (!indexed[col->ind]) {
  2572. dict_index_add_col(new_index, table, col, 0);
  2573. }
  2574. }
  2575. mem_free(indexed);
  2576. ut_ad(dict_index_is_ibuf(index)
  2577. || (UT_LIST_GET_LEN(table->indexes) == 0));
  2578. new_index->cached = TRUE;
  2579. return(new_index);
  2580. }
  2581. /*******************************************************************//**
  2582. Builds the internal dictionary cache representation for a non-clustered
  2583. index, containing also system fields not defined by the user.
  2584. @return own: the internal representation of the non-clustered index */
  2585. static
  2586. dict_index_t*
  2587. dict_index_build_internal_non_clust(
  2588. /*================================*/
  2589. const dict_table_t* table, /*!< in: table */
  2590. dict_index_t* index) /*!< in: user representation of
  2591. a non-clustered index */
  2592. {
  2593. dict_field_t* field;
  2594. dict_index_t* new_index;
  2595. dict_index_t* clust_index;
  2596. ulint i;
  2597. ibool* indexed;
  2598. ut_ad(table && index);
  2599. ut_ad(!dict_index_is_clust(index));
  2600. ut_ad(mutex_own(&(dict_sys->mutex)));
  2601. ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
  2602. /* The clustered index should be the first in the list of indexes */
  2603. clust_index = UT_LIST_GET_FIRST(table->indexes);
  2604. ut_ad(clust_index);
  2605. ut_ad(dict_index_is_clust(clust_index));
  2606. ut_ad(!dict_index_is_univ(clust_index));
  2607. /* Create a new index */
  2608. new_index = dict_mem_index_create(
  2609. table->name, index->name, index->space, index->type,
  2610. index->n_fields + 1 + clust_index->n_uniq);
  2611. /* Copy other relevant data from the old index
  2612. struct to the new struct: it inherits the values */
  2613. new_index->n_user_defined_cols = index->n_fields;
  2614. new_index->id = index->id;
  2615. btr_search_index_init(new_index);
  2616. /* Copy fields from index to new_index */
  2617. dict_index_copy(new_index, index, table, 0, index->n_fields);
  2618. /* Remember the table columns already contained in new_index */
  2619. indexed = static_cast<ibool*>(
  2620. mem_zalloc(table->n_cols * sizeof *indexed));
  2621. /* Mark the table columns already contained in new_index */
  2622. for (i = 0; i < new_index->n_def; i++) {
  2623. field = dict_index_get_nth_field(new_index, i);
  2624. /* If there is only a prefix of the column in the index
  2625. field, do not mark the column as contained in the index */
  2626. if (field->prefix_len == 0) {
  2627. indexed[field->col->ind] = TRUE;
  2628. }
  2629. }
  2630. /* Add to new_index the columns necessary to determine the clustered
  2631. index entry uniquely */
  2632. for (i = 0; i < clust_index->n_uniq; i++) {
  2633. field = dict_index_get_nth_field(clust_index, i);
  2634. if (!indexed[field->col->ind]) {
  2635. dict_index_add_col(new_index, table, field->col,
  2636. field->prefix_len);
  2637. }
  2638. }
  2639. mem_free(indexed);
  2640. if (dict_index_is_unique(index)) {
  2641. new_index->n_uniq = index->n_fields;
  2642. } else {
  2643. new_index->n_uniq = new_index->n_def;
  2644. }
  2645. /* Set the n_fields value in new_index to the actual defined
  2646. number of fields */
  2647. new_index->n_fields = new_index->n_def;
  2648. new_index->cached = TRUE;
  2649. return(new_index);
  2650. }
  2651. /***********************************************************************
  2652. Builds the internal dictionary cache representation for an FTS index.
  2653. @return own: the internal representation of the FTS index */
  2654. static
  2655. dict_index_t*
  2656. dict_index_build_internal_fts(
  2657. /*==========================*/
  2658. dict_table_t* table, /*!< in: table */
  2659. dict_index_t* index) /*!< in: user representation of an FTS index */
  2660. {
  2661. dict_index_t* new_index;
  2662. ut_ad(table && index);
  2663. ut_ad(index->type == DICT_FTS);
  2664. #ifdef UNIV_SYNC_DEBUG
  2665. ut_ad(mutex_own(&(dict_sys->mutex)));
  2666. #endif /* UNIV_SYNC_DEBUG */
  2667. ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
  2668. /* Create a new index */
  2669. new_index = dict_mem_index_create(
  2670. table->name, index->name, index->space, index->type,
  2671. index->n_fields);
  2672. /* Copy other relevant data from the old index struct to the new
  2673. struct: it inherits the values */
  2674. new_index->n_user_defined_cols = index->n_fields;
  2675. new_index->id = index->id;
  2676. btr_search_index_init(new_index);
  2677. /* Copy fields from index to new_index */
  2678. dict_index_copy(new_index, index, table, 0, index->n_fields);
  2679. new_index->n_uniq = 0;
  2680. new_index->cached = TRUE;
  2681. if (table->fts->cache == NULL) {
  2682. table->fts->cache = fts_cache_create(table);
  2683. }
  2684. rw_lock_x_lock(&table->fts->cache->init_lock);
  2685. /* Notify the FTS cache about this index. */
  2686. fts_cache_index_cache_create(table, new_index);
  2687. rw_lock_x_unlock(&table->fts->cache->init_lock);
  2688. return(new_index);
  2689. }
  2690. /*====================== FOREIGN KEY PROCESSING ========================*/
  2691. #define DB_FOREIGN_KEY_IS_PREFIX_INDEX 200
  2692. #define DB_FOREIGN_KEY_COL_NOT_NULL 201
  2693. #define DB_FOREIGN_KEY_COLS_NOT_EQUAL 202
  2694. #define DB_FOREIGN_KEY_INDEX_NOT_FOUND 203
  2695. /*********************************************************************//**
  2696. Checks if a table is referenced by foreign keys.
  2697. @return TRUE if table is referenced by a foreign key */
  2698. UNIV_INTERN
  2699. ibool
  2700. dict_table_is_referenced_by_foreign_key(
  2701. /*====================================*/
  2702. const dict_table_t* table) /*!< in: InnoDB table */
  2703. {
  2704. return(!table->referenced_set.empty());
  2705. }
  2706. /**********************************************************************//**
  2707. Removes a foreign constraint struct from the dictionary cache. */
  2708. UNIV_INTERN
  2709. void
  2710. dict_foreign_remove_from_cache(
  2711. /*===========================*/
  2712. dict_foreign_t* foreign) /*!< in, own: foreign constraint */
  2713. {
  2714. ut_ad(mutex_own(&(dict_sys->mutex)));
  2715. ut_a(foreign);
  2716. if (foreign->referenced_table != NULL) {
  2717. foreign->referenced_table->referenced_set.erase(foreign);
  2718. }
  2719. if (foreign->foreign_table != NULL) {
  2720. foreign->foreign_table->foreign_set.erase(foreign);
  2721. }
  2722. dict_foreign_free(foreign);
  2723. }
  2724. /**********************************************************************//**
  2725. Looks for the foreign constraint from the foreign and referenced lists
  2726. of a table.
  2727. @return foreign constraint */
  2728. static
  2729. dict_foreign_t*
  2730. dict_foreign_find(
  2731. /*==============*/
  2732. dict_table_t* table, /*!< in: table object */
  2733. dict_foreign_t* foreign) /*!< in: foreign constraint */
  2734. {
  2735. ut_ad(mutex_own(&(dict_sys->mutex)));
  2736. ut_ad(dict_foreign_set_validate(table->foreign_set));
  2737. ut_ad(dict_foreign_set_validate(table->referenced_set));
  2738. dict_foreign_set::iterator it = table->foreign_set.find(foreign);
  2739. if (it != table->foreign_set.end()) {
  2740. return(*it);
  2741. }
  2742. it = table->referenced_set.find(foreign);
  2743. if (it != table->referenced_set.end()) {
  2744. return(*it);
  2745. }
  2746. return(NULL);
  2747. }
  2748. /*********************************************************************//**
  2749. Tries to find an index whose first fields are the columns in the array,
  2750. in the same order and is not marked for deletion and is not the same
  2751. as types_idx.
  2752. @return matching index, NULL if not found */
  2753. UNIV_INTERN
  2754. dict_index_t*
  2755. dict_foreign_find_index(
  2756. /*====================*/
  2757. const dict_table_t* table, /*!< in: table */
  2758. const char** col_names,
  2759. /*!< in: column names, or NULL
  2760. to use table->col_names */
  2761. const char** columns,/*!< in: array of column names */
  2762. ulint n_cols, /*!< in: number of columns */
  2763. const dict_index_t* types_idx,
  2764. /*!< in: NULL or an index
  2765. whose types the column types
  2766. must match */
  2767. bool check_charsets,
  2768. /*!< in: whether to check
  2769. charsets. only has an effect
  2770. if types_idx != NULL */
  2771. ulint check_null,
  2772. /*!< in: nonzero if none of
  2773. the columns must be declared
  2774. NOT NULL */
  2775. ulint* error, /*!< out: error code */
  2776. ulint* err_col_no,
  2777. /*!< out: column number where
  2778. error happened */
  2779. dict_index_t** err_index)
  2780. /*!< out: index where error
  2781. happened */
  2782. {
  2783. dict_index_t* index;
  2784. ut_ad(mutex_own(&dict_sys->mutex));
  2785. if (error) {
  2786. *error = DB_FOREIGN_KEY_INDEX_NOT_FOUND;
  2787. }
  2788. index = dict_table_get_first_index(table);
  2789. while (index != NULL) {
  2790. if (types_idx != index
  2791. && !(index->type & DICT_FTS)
  2792. && !index->to_be_dropped
  2793. && !dict_index_is_online_ddl(index)
  2794. && dict_foreign_qualify_index(
  2795. table, col_names, columns, n_cols,
  2796. index, types_idx,
  2797. check_charsets, check_null,
  2798. error, err_col_no,err_index)) {
  2799. if (error) {
  2800. *error = DB_SUCCESS;
  2801. }
  2802. return(index);
  2803. }
  2804. index = dict_table_get_next_index(index);
  2805. }
  2806. return(NULL);
  2807. }
  2808. #ifdef WITH_WSREP
  2809. dict_index_t*
  2810. wsrep_dict_foreign_find_index(
  2811. /*====================*/
  2812. dict_table_t* table, /*!< in: table */
  2813. const char** col_names, /*!< in: column names, or NULL
  2814. to use table->col_names */
  2815. const char** columns,/*!< in: array of column names */
  2816. ulint n_cols, /*!< in: number of columns */
  2817. dict_index_t* types_idx, /*!< in: NULL or an index to whose types the
  2818. column types must match */
  2819. ibool check_charsets,
  2820. /*!< in: whether to check charsets.
  2821. only has an effect if types_idx != NULL */
  2822. ulint check_null)
  2823. /*!< in: nonzero if none of the columns must
  2824. be declared NOT NULL */
  2825. {
  2826. return dict_foreign_find_index(
  2827. table, col_names, columns, n_cols, types_idx, check_charsets,
  2828. check_null, NULL, NULL, NULL);
  2829. }
  2830. #endif /* WITH_WSREP */
  2831. /**********************************************************************//**
  2832. Report an error in a foreign key definition. */
  2833. static
  2834. void
  2835. dict_foreign_error_report_low(
  2836. /*==========================*/
  2837. FILE* file, /*!< in: output stream */
  2838. const char* name) /*!< in: table name */
  2839. {
  2840. rewind(file);
  2841. ut_print_timestamp(file);
  2842. fprintf(file, " Error in foreign key constraint of table %s:\n",
  2843. name);
  2844. }
  2845. /**********************************************************************//**
  2846. Report an error in a foreign key definition. */
  2847. static
  2848. void
  2849. dict_foreign_error_report(
  2850. /*======================*/
  2851. FILE* file, /*!< in: output stream */
  2852. dict_foreign_t* fk, /*!< in: foreign key constraint */
  2853. const char* msg) /*!< in: the error message */
  2854. {
  2855. std::string fk_str;
  2856. mutex_enter(&dict_foreign_err_mutex);
  2857. dict_foreign_error_report_low(file, fk->foreign_table_name);
  2858. fputs(msg, file);
  2859. fputs(" Constraint:\n", file);
  2860. fk_str = dict_print_info_on_foreign_key_in_create_format(NULL, fk, TRUE);
  2861. fputs(fk_str.c_str(), file);
  2862. putc('\n', file);
  2863. if (fk->foreign_index) {
  2864. fputs("The index in the foreign key in table is ", file);
  2865. ut_print_name(file, NULL, FALSE, fk->foreign_index->name);
  2866. fputs("\n"
  2867. "See " REFMAN "innodb-foreign-key-constraints.html\n"
  2868. "for correct foreign key definition.\n",
  2869. file);
  2870. }
  2871. mutex_exit(&dict_foreign_err_mutex);
  2872. }
  2873. /**********************************************************************//**
  2874. Adds a foreign key constraint object to the dictionary cache. May free
  2875. the object if there already is an object with the same identifier in.
  2876. At least one of the foreign table and the referenced table must already
  2877. be in the dictionary cache!
  2878. @return DB_SUCCESS or error code */
  2879. UNIV_INTERN
  2880. dberr_t
  2881. dict_foreign_add_to_cache(
  2882. /*======================*/
  2883. dict_foreign_t* foreign,
  2884. /*!< in, own: foreign key constraint */
  2885. const char** col_names,
  2886. /*!< in: column names, or NULL to use
  2887. foreign->foreign_table->col_names */
  2888. bool check_charsets,
  2889. /*!< in: whether to check charset
  2890. compatibility */
  2891. dict_err_ignore_t ignore_err)
  2892. /*!< in: error to be ignored */
  2893. {
  2894. dict_table_t* for_table;
  2895. dict_table_t* ref_table;
  2896. dict_foreign_t* for_in_cache = NULL;
  2897. dict_index_t* index;
  2898. ibool added_to_referenced_list= FALSE;
  2899. FILE* ef = dict_foreign_err_file;
  2900. ut_ad(mutex_own(&(dict_sys->mutex)));
  2901. for_table = dict_table_check_if_in_cache_low(
  2902. foreign->foreign_table_name_lookup);
  2903. ref_table = dict_table_check_if_in_cache_low(
  2904. foreign->referenced_table_name_lookup);
  2905. ut_a(for_table || ref_table);
  2906. if (for_table) {
  2907. for_in_cache = dict_foreign_find(for_table, foreign);
  2908. }
  2909. if (!for_in_cache && ref_table) {
  2910. for_in_cache = dict_foreign_find(ref_table, foreign);
  2911. }
  2912. if (for_in_cache) {
  2913. dict_foreign_free(foreign);
  2914. } else {
  2915. for_in_cache = foreign;
  2916. }
  2917. if (ref_table && !for_in_cache->referenced_table) {
  2918. index = dict_foreign_find_index(
  2919. ref_table, NULL,
  2920. for_in_cache->referenced_col_names,
  2921. for_in_cache->n_fields, for_in_cache->foreign_index,
  2922. check_charsets, false, NULL, NULL, NULL);
  2923. if (index == NULL
  2924. && !(ignore_err & DICT_ERR_IGNORE_FK_NOKEY)) {
  2925. dict_foreign_error_report(
  2926. ef, for_in_cache,
  2927. "there is no index in referenced table"
  2928. " which would contain\n"
  2929. "the columns as the first columns,"
  2930. " or the data types in the\n"
  2931. "referenced table do not match"
  2932. " the ones in table.");
  2933. if (for_in_cache == foreign) {
  2934. dict_foreign_free(foreign);
  2935. }
  2936. return(DB_CANNOT_ADD_CONSTRAINT);
  2937. }
  2938. for_in_cache->referenced_table = ref_table;
  2939. for_in_cache->referenced_index = index;
  2940. std::pair<dict_foreign_set::iterator, bool> ret
  2941. = ref_table->referenced_set.insert(for_in_cache);
  2942. ut_a(ret.second); /* second is true if the insertion
  2943. took place */
  2944. added_to_referenced_list = TRUE;
  2945. }
  2946. if (for_table && !for_in_cache->foreign_table) {
  2947. ulint index_error;
  2948. ulint err_col;
  2949. dict_index_t *err_index=NULL;
  2950. index = dict_foreign_find_index(
  2951. for_table, col_names,
  2952. for_in_cache->foreign_col_names,
  2953. for_in_cache->n_fields,
  2954. for_in_cache->referenced_index, check_charsets,
  2955. for_in_cache->type
  2956. & (DICT_FOREIGN_ON_DELETE_SET_NULL
  2957. | DICT_FOREIGN_ON_UPDATE_SET_NULL),
  2958. &index_error, &err_col, &err_index);
  2959. if (index == NULL
  2960. && !(ignore_err & DICT_ERR_IGNORE_FK_NOKEY)) {
  2961. dict_foreign_error_report(
  2962. ef, for_in_cache,
  2963. "there is no index in the table"
  2964. " which would contain\n"
  2965. "the columns as the first columns,"
  2966. " or the data types in the\n"
  2967. "table do not match"
  2968. " the ones in the referenced table\n"
  2969. "or one of the ON ... SET NULL columns"
  2970. " is declared NOT NULL.");
  2971. if (for_in_cache == foreign) {
  2972. if (added_to_referenced_list) {
  2973. const dict_foreign_set::size_type n
  2974. = ref_table->referenced_set
  2975. .erase(for_in_cache);
  2976. ut_a(n == 1); /* the number of
  2977. elements removed must
  2978. be one */
  2979. }
  2980. dict_foreign_free(foreign);
  2981. }
  2982. return(DB_CANNOT_ADD_CONSTRAINT);
  2983. }
  2984. for_in_cache->foreign_table = for_table;
  2985. for_in_cache->foreign_index = index;
  2986. std::pair<dict_foreign_set::iterator, bool> ret
  2987. = for_table->foreign_set.insert(for_in_cache);
  2988. ut_a(ret.second); /* second is true if the insertion
  2989. took place */
  2990. }
  2991. /* We need to move the table to the non-LRU end of the table LRU
  2992. list. Otherwise it will be evicted from the cache. */
  2993. if (ref_table != NULL && ref_table->can_be_evicted) {
  2994. dict_table_move_from_lru_to_non_lru(ref_table);
  2995. }
  2996. if (for_table != NULL && for_table->can_be_evicted) {
  2997. dict_table_move_from_lru_to_non_lru(for_table);
  2998. }
  2999. ut_ad(dict_lru_validate());
  3000. return(DB_SUCCESS);
  3001. }
  3002. /*********************************************************************//**
  3003. Scans from pointer onwards. Stops if is at the start of a copy of
  3004. 'string' where characters are compared without case sensitivity, and
  3005. only outside `` or "" quotes. Stops also at NUL.
  3006. @return scanned up to this */
  3007. static
  3008. const char*
  3009. dict_scan_to(
  3010. /*=========*/
  3011. const char* ptr, /*!< in: scan from */
  3012. const char* string) /*!< in: look for this */
  3013. {
  3014. char quote = '\0';
  3015. bool escape = false;
  3016. for (; *ptr; ptr++) {
  3017. if (*ptr == quote) {
  3018. /* Closing quote character: do not look for
  3019. starting quote or the keyword. */
  3020. /* If the quote character is escaped by a
  3021. backslash, ignore it. */
  3022. if (escape) {
  3023. escape = false;
  3024. } else {
  3025. quote = '\0';
  3026. }
  3027. } else if (quote) {
  3028. /* Within quotes: do nothing. */
  3029. if (escape) {
  3030. escape = false;
  3031. } else if (*ptr == '\\') {
  3032. escape = true;
  3033. }
  3034. } else if (*ptr == '`' || *ptr == '"' || *ptr == '\'') {
  3035. /* Starting quote: remember the quote character. */
  3036. quote = *ptr;
  3037. } else {
  3038. /* Outside quotes: look for the keyword. */
  3039. ulint i;
  3040. for (i = 0; string[i]; i++) {
  3041. if (toupper((int)(unsigned char)(ptr[i]))
  3042. != toupper((int)(unsigned char)
  3043. (string[i]))) {
  3044. goto nomatch;
  3045. }
  3046. }
  3047. break;
  3048. nomatch:
  3049. ;
  3050. }
  3051. }
  3052. return(ptr);
  3053. }
  3054. /*********************************************************************//**
  3055. Accepts a specified string. Comparisons are case-insensitive.
  3056. @return if string was accepted, the pointer is moved after that, else
  3057. ptr is returned */
  3058. static
  3059. const char*
  3060. dict_accept(
  3061. /*========*/
  3062. struct charset_info_st* cs,/*!< in: the character set of ptr */
  3063. const char* ptr, /*!< in: scan from this */
  3064. const char* string, /*!< in: accept only this string as the next
  3065. non-whitespace string */
  3066. ibool* success)/*!< out: TRUE if accepted */
  3067. {
  3068. const char* old_ptr = ptr;
  3069. const char* old_ptr2;
  3070. *success = FALSE;
  3071. while (my_isspace(cs, *ptr)) {
  3072. ptr++;
  3073. }
  3074. old_ptr2 = ptr;
  3075. ptr = dict_scan_to(ptr, string);
  3076. if (*ptr == '\0' || old_ptr2 != ptr) {
  3077. return(old_ptr);
  3078. }
  3079. *success = TRUE;
  3080. return(ptr + ut_strlen(string));
  3081. }
  3082. /*********************************************************************//**
  3083. Scans an id. For the lexical definition of an 'id', see the code below.
  3084. Strips backquotes or double quotes from around the id.
  3085. @return scanned to */
  3086. static
  3087. const char*
  3088. dict_scan_id(
  3089. /*=========*/
  3090. struct charset_info_st* cs,/*!< in: the character set of ptr */
  3091. const char* ptr, /*!< in: scanned to */
  3092. mem_heap_t* heap, /*!< in: heap where to allocate the id
  3093. (NULL=id will not be allocated, but it
  3094. will point to string near ptr) */
  3095. const char** id, /*!< out,own: the id; NULL if no id was
  3096. scannable */
  3097. ibool table_id,/*!< in: TRUE=convert the allocated id
  3098. as a table name; FALSE=convert to UTF-8 */
  3099. ibool accept_also_dot)
  3100. /*!< in: TRUE if also a dot can appear in a
  3101. non-quoted id; in a quoted id it can appear
  3102. always */
  3103. {
  3104. char quote = '\0';
  3105. ulint len = 0;
  3106. const char* s;
  3107. char* str;
  3108. char* dst;
  3109. *id = NULL;
  3110. while (my_isspace(cs, *ptr)) {
  3111. ptr++;
  3112. }
  3113. if (*ptr == '\0') {
  3114. return(ptr);
  3115. }
  3116. if (*ptr == '`' || *ptr == '"') {
  3117. quote = *ptr++;
  3118. }
  3119. s = ptr;
  3120. if (quote) {
  3121. for (;;) {
  3122. if (!*ptr) {
  3123. /* Syntax error */
  3124. return(ptr);
  3125. }
  3126. if (*ptr == quote) {
  3127. ptr++;
  3128. if (*ptr != quote) {
  3129. break;
  3130. }
  3131. }
  3132. ptr++;
  3133. len++;
  3134. }
  3135. } else {
  3136. while (!my_isspace(cs, *ptr) && *ptr != '(' && *ptr != ')'
  3137. && (accept_also_dot || *ptr != '.')
  3138. && *ptr != ',' && *ptr != '\0') {
  3139. ptr++;
  3140. }
  3141. len = ptr - s;
  3142. }
  3143. if (UNIV_UNLIKELY(!heap)) {
  3144. /* no heap given: id will point to source string */
  3145. *id = s;
  3146. return(ptr);
  3147. }
  3148. if (quote) {
  3149. char* d;
  3150. str = d = static_cast<char*>(
  3151. mem_heap_alloc(heap, len + 1));
  3152. while (len--) {
  3153. if ((*d++ = *s++) == quote) {
  3154. s++;
  3155. }
  3156. }
  3157. *d++ = 0;
  3158. len = d - str;
  3159. ut_ad(*s == quote);
  3160. ut_ad(s + 1 == ptr);
  3161. } else {
  3162. str = mem_heap_strdupl(heap, s, len);
  3163. }
  3164. if (!table_id) {
  3165. convert_id:
  3166. /* Convert the identifier from connection character set
  3167. to UTF-8. */
  3168. len = 3 * len + 1;
  3169. *id = dst = static_cast<char*>(mem_heap_alloc(heap, len));
  3170. innobase_convert_from_id(cs, dst, str, len);
  3171. } else if (!strncmp(str, srv_mysql50_table_name_prefix,
  3172. sizeof(srv_mysql50_table_name_prefix) - 1)) {
  3173. /* This is a pre-5.1 table name
  3174. containing chars other than [A-Za-z0-9].
  3175. Discard the prefix and use raw UTF-8 encoding. */
  3176. str += sizeof(srv_mysql50_table_name_prefix) - 1;
  3177. len -= sizeof(srv_mysql50_table_name_prefix) - 1;
  3178. goto convert_id;
  3179. } else {
  3180. /* Encode using filename-safe characters. */
  3181. len = 5 * len + 1;
  3182. *id = dst = static_cast<char*>(mem_heap_alloc(heap, len));
  3183. innobase_convert_from_table_id(cs, dst, str, len);
  3184. }
  3185. return(ptr);
  3186. }
  3187. /*********************************************************************//**
  3188. Tries to scan a column name.
  3189. @return scanned to */
  3190. static
  3191. const char*
  3192. dict_scan_col(
  3193. /*==========*/
  3194. struct charset_info_st* cs, /*!< in: the character set of ptr */
  3195. const char* ptr, /*!< in: scanned to */
  3196. ibool* success,/*!< out: TRUE if success */
  3197. dict_table_t* table, /*!< in: table in which the column is */
  3198. const dict_col_t** column, /*!< out: pointer to column if success */
  3199. mem_heap_t* heap, /*!< in: heap where to allocate */
  3200. const char** name) /*!< out,own: the column name;
  3201. NULL if no name was scannable */
  3202. {
  3203. ulint i;
  3204. *success = FALSE;
  3205. ptr = dict_scan_id(cs, ptr, heap, name, FALSE, TRUE);
  3206. if (*name == NULL) {
  3207. return(ptr); /* Syntax error */
  3208. }
  3209. if (table == NULL) {
  3210. *success = TRUE;
  3211. *column = NULL;
  3212. } else {
  3213. for (i = 0; i < dict_table_get_n_cols(table); i++) {
  3214. const char* col_name = dict_table_get_col_name(
  3215. table, i);
  3216. if (0 == innobase_strcasecmp(col_name, *name)) {
  3217. /* Found */
  3218. *success = TRUE;
  3219. *column = dict_table_get_nth_col(table, i);
  3220. strcpy((char*) *name, col_name);
  3221. break;
  3222. }
  3223. }
  3224. }
  3225. return(ptr);
  3226. }
  3227. /*********************************************************************//**
  3228. Open a table from its database and table name, this is currently used by
  3229. foreign constraint parser to get the referenced table.
  3230. @return complete table name with database and table name, allocated from
  3231. heap memory passed in */
  3232. UNIV_INTERN
  3233. char*
  3234. dict_get_referenced_table(
  3235. /*======================*/
  3236. const char* name, /*!< in: foreign key table name */
  3237. const char* database_name, /*!< in: table db name */
  3238. ulint database_name_len, /*!< in: db name length */
  3239. const char* table_name, /*!< in: table name */
  3240. ulint table_name_len, /*!< in: table name length */
  3241. dict_table_t** table, /*!< out: table object or NULL */
  3242. mem_heap_t* heap) /*!< in/out: heap memory */
  3243. {
  3244. char* ref;
  3245. const char* db_name;
  3246. if (!database_name) {
  3247. /* Use the database name of the foreign key table */
  3248. db_name = name;
  3249. database_name_len = dict_get_db_name_len(name);
  3250. } else {
  3251. db_name = database_name;
  3252. }
  3253. /* Copy database_name, '/', table_name, '\0' */
  3254. ref = static_cast<char*>(
  3255. mem_heap_alloc(heap, database_name_len + table_name_len + 2));
  3256. memcpy(ref, db_name, database_name_len);
  3257. ref[database_name_len] = '/';
  3258. memcpy(ref + database_name_len + 1, table_name, table_name_len + 1);
  3259. /* Values; 0 = Store and compare as given; case sensitive
  3260. 1 = Store and compare in lower; case insensitive
  3261. 2 = Store as given, compare in lower; case semi-sensitive */
  3262. if (innobase_get_lower_case_table_names() == 2) {
  3263. innobase_casedn_str(ref);
  3264. *table = dict_table_get_low(ref);
  3265. memcpy(ref, db_name, database_name_len);
  3266. ref[database_name_len] = '/';
  3267. memcpy(ref + database_name_len + 1, table_name, table_name_len + 1);
  3268. } else {
  3269. #ifndef __WIN__
  3270. if (innobase_get_lower_case_table_names() == 1) {
  3271. innobase_casedn_str(ref);
  3272. }
  3273. #else
  3274. innobase_casedn_str(ref);
  3275. #endif /* !__WIN__ */
  3276. *table = dict_table_get_low(ref);
  3277. }
  3278. return(ref);
  3279. }
  3280. /*********************************************************************//**
  3281. Scans a table name from an SQL string.
  3282. @return scanned to */
  3283. static
  3284. const char*
  3285. dict_scan_table_name(
  3286. /*=================*/
  3287. struct charset_info_st* cs,/*!< in: the character set of ptr */
  3288. const char* ptr, /*!< in: scanned to */
  3289. dict_table_t** table, /*!< out: table object or NULL */
  3290. const char* name, /*!< in: foreign key table name */
  3291. ibool* success,/*!< out: TRUE if ok name found */
  3292. mem_heap_t* heap, /*!< in: heap where to allocate the id */
  3293. const char** ref_name)/*!< out,own: the table name;
  3294. NULL if no name was scannable */
  3295. {
  3296. const char* database_name = NULL;
  3297. ulint database_name_len = 0;
  3298. const char* table_name = NULL;
  3299. const char* scan_name;
  3300. *success = FALSE;
  3301. *table = NULL;
  3302. ptr = dict_scan_id(cs, ptr, heap, &scan_name, TRUE, FALSE);
  3303. if (scan_name == NULL) {
  3304. return(ptr); /* Syntax error */
  3305. }
  3306. if (*ptr == '.') {
  3307. /* We scanned the database name; scan also the table name */
  3308. ptr++;
  3309. database_name = scan_name;
  3310. database_name_len = strlen(database_name);
  3311. ptr = dict_scan_id(cs, ptr, heap, &table_name, TRUE, FALSE);
  3312. if (table_name == NULL) {
  3313. return(ptr); /* Syntax error */
  3314. }
  3315. } else {
  3316. /* To be able to read table dumps made with InnoDB-4.0.17 or
  3317. earlier, we must allow the dot separator between the database
  3318. name and the table name also to appear within a quoted
  3319. identifier! InnoDB used to print a constraint as:
  3320. ... REFERENCES `databasename.tablename` ...
  3321. starting from 4.0.18 it is
  3322. ... REFERENCES `databasename`.`tablename` ... */
  3323. const char* s;
  3324. for (s = scan_name; *s; s++) {
  3325. if (*s == '.') {
  3326. database_name = scan_name;
  3327. database_name_len = s - scan_name;
  3328. scan_name = ++s;
  3329. break;/* to do: multiple dots? */
  3330. }
  3331. }
  3332. table_name = scan_name;
  3333. }
  3334. *ref_name = dict_get_referenced_table(
  3335. name, database_name, database_name_len,
  3336. table_name, strlen(table_name), table, heap);
  3337. *success = TRUE;
  3338. return(ptr);
  3339. }
  3340. /*********************************************************************//**
  3341. Skips one id. The id is allowed to contain also '.'.
  3342. @return scanned to */
  3343. static
  3344. const char*
  3345. dict_skip_word(
  3346. /*===========*/
  3347. struct charset_info_st* cs,/*!< in: the character set of ptr */
  3348. const char* ptr, /*!< in: scanned to */
  3349. ibool* success)/*!< out: TRUE if success, FALSE if just spaces
  3350. left in string or a syntax error */
  3351. {
  3352. const char* start;
  3353. *success = FALSE;
  3354. ptr = dict_scan_id(cs, ptr, NULL, &start, FALSE, TRUE);
  3355. if (start) {
  3356. *success = TRUE;
  3357. }
  3358. return(ptr);
  3359. }
  3360. /*********************************************************************//**
  3361. Removes MySQL comments from an SQL string. A comment is either
  3362. (a) '#' to the end of the line,
  3363. (b) '--[space]' to the end of the line, or
  3364. (c) '[slash][asterisk]' till the next '[asterisk][slash]' (like the familiar
  3365. C comment syntax).
  3366. @return own: SQL string stripped from comments; the caller must free
  3367. this with mem_free()! */
  3368. static
  3369. char*
  3370. dict_strip_comments(
  3371. /*================*/
  3372. const char* sql_string, /*!< in: SQL string */
  3373. size_t sql_length) /*!< in: length of sql_string */
  3374. {
  3375. char* str;
  3376. const char* sptr;
  3377. const char* eptr = sql_string + sql_length;
  3378. char* ptr;
  3379. /* unclosed quote character (0 if none) */
  3380. char quote = 0;
  3381. bool escape = false;
  3382. DBUG_ENTER("dict_strip_comments");
  3383. DBUG_PRINT("dict_strip_comments", ("%s", sql_string));
  3384. str = static_cast<char*>(mem_alloc(sql_length + 1));
  3385. sptr = sql_string;
  3386. ptr = str;
  3387. for (;;) {
  3388. scan_more:
  3389. if (sptr >= eptr || *sptr == '\0') {
  3390. end_of_string:
  3391. *ptr = '\0';
  3392. ut_a(ptr <= str + sql_length);
  3393. DBUG_PRINT("dict_strip_comments", ("%s", str));
  3394. DBUG_RETURN(str);
  3395. }
  3396. if (*sptr == quote) {
  3397. /* Closing quote character: do not look for
  3398. starting quote or comments. */
  3399. /* If the quote character is escaped by a
  3400. backslash, ignore it. */
  3401. if (escape) {
  3402. escape = false;
  3403. } else {
  3404. quote = 0;
  3405. }
  3406. } else if (quote) {
  3407. /* Within quotes: do not look for
  3408. starting quotes or comments. */
  3409. if (escape) {
  3410. escape = false;
  3411. } else if (*sptr == '\\') {
  3412. escape = true;
  3413. }
  3414. } else if (*sptr == '"' || *sptr == '`' || *sptr == '\'') {
  3415. /* Starting quote: remember the quote character. */
  3416. quote = *sptr;
  3417. } else if (*sptr == '#'
  3418. || (sptr[0] == '-' && sptr[1] == '-'
  3419. && sptr[2] == ' ')) {
  3420. for (;;) {
  3421. if (++sptr >= eptr) {
  3422. goto end_of_string;
  3423. }
  3424. /* In Unix a newline is 0x0A while in Windows
  3425. it is 0x0D followed by 0x0A */
  3426. switch (*sptr) {
  3427. case (char) 0X0A:
  3428. case (char) 0x0D:
  3429. case '\0':
  3430. goto scan_more;
  3431. }
  3432. }
  3433. } else if (!quote && *sptr == '/' && *(sptr + 1) == '*') {
  3434. sptr += 2;
  3435. for (;;) {
  3436. if (sptr >= eptr) {
  3437. goto end_of_string;
  3438. }
  3439. switch (*sptr) {
  3440. case '\0':
  3441. goto scan_more;
  3442. case '*':
  3443. if (sptr[1] == '/') {
  3444. sptr += 2;
  3445. goto scan_more;
  3446. }
  3447. }
  3448. sptr++;
  3449. }
  3450. }
  3451. *ptr = *sptr;
  3452. ptr++;
  3453. sptr++;
  3454. }
  3455. }
  3456. /*********************************************************************//**
  3457. Finds the highest [number] for foreign key constraints of the table. Looks
  3458. only at the >= 4.0.18-format id's, which are of the form
  3459. databasename/tablename_ibfk_[number].
  3460. @return highest number, 0 if table has no new format foreign key constraints */
  3461. UNIV_INTERN
  3462. ulint
  3463. dict_table_get_highest_foreign_id(
  3464. /*==============================*/
  3465. dict_table_t* table) /*!< in: table in the dictionary memory cache */
  3466. {
  3467. dict_foreign_t* foreign;
  3468. char* endp;
  3469. ulint biggest_id = 0;
  3470. ulint id;
  3471. ulint len;
  3472. ut_a(table);
  3473. len = ut_strlen(table->name);
  3474. for (dict_foreign_set::iterator it = table->foreign_set.begin();
  3475. it != table->foreign_set.end();
  3476. ++it) {
  3477. char fkid[MAX_TABLE_NAME_LEN+20];
  3478. foreign = *it;
  3479. strcpy(fkid, foreign->id);
  3480. /* Convert foreign key identifier on dictionary memory
  3481. cache to filename charset. */
  3482. innobase_convert_to_filename_charset(
  3483. strchr(fkid, '/') + 1,
  3484. strchr(foreign->id, '/') + 1,
  3485. MAX_TABLE_NAME_LEN);
  3486. if (ut_strlen(fkid) > ((sizeof dict_ibfk) - 1) + len
  3487. && 0 == ut_memcmp(fkid, table->name, len)
  3488. && 0 == ut_memcmp(fkid + len,
  3489. dict_ibfk, (sizeof dict_ibfk) - 1)
  3490. && fkid[len + ((sizeof dict_ibfk) - 1)] != '0') {
  3491. /* It is of the >= 4.0.18 format */
  3492. id = strtoul(fkid + len
  3493. + ((sizeof dict_ibfk) - 1),
  3494. &endp, 10);
  3495. if (*endp == '\0') {
  3496. ut_a(id != biggest_id);
  3497. if (id > biggest_id) {
  3498. biggest_id = id;
  3499. }
  3500. }
  3501. }
  3502. }
  3503. return(biggest_id);
  3504. }
  3505. /*********************************************************************//**
  3506. Reports a simple foreign key create clause syntax error. */
  3507. static
  3508. void
  3509. dict_foreign_report_syntax_err(
  3510. /*===========================*/
  3511. const char* fmt, /*!< in: syntax err msg */
  3512. const char* oper, /*!< in: operation */
  3513. const char* name, /*!< in: table name */
  3514. const char* start_of_latest_foreign,
  3515. /*!< in: start of the foreign key clause
  3516. in the SQL string */
  3517. const char* ptr) /*!< in: place of the syntax error */
  3518. {
  3519. ut_ad(!srv_read_only_mode);
  3520. FILE* ef = dict_foreign_err_file;
  3521. mutex_enter(&dict_foreign_err_mutex);
  3522. dict_foreign_error_report_low(ef, name);
  3523. fprintf(ef, fmt, oper, name, start_of_latest_foreign, ptr);
  3524. mutex_exit(&dict_foreign_err_mutex);
  3525. }
  3526. /*********************************************************************//**
  3527. Push warning message to SQL-layer based on foreign key constraint
  3528. index match error. */
  3529. static
  3530. void
  3531. dict_foreign_push_index_error(
  3532. /*==========================*/
  3533. trx_t* trx, /*!< in: trx */
  3534. const char* operation, /*!< in: operation create or alter
  3535. */
  3536. const char* create_name, /*!< in: table name in create or
  3537. alter table */
  3538. const char* latest_foreign, /*!< in: start of latest foreign key
  3539. constraint name */
  3540. const char** columns, /*!< in: foreign key columns */
  3541. ulint index_error, /*!< in: error code */
  3542. ulint err_col, /*!< in: column where error happened
  3543. */
  3544. dict_index_t* err_index, /*!< in: index where error happened
  3545. */
  3546. dict_table_t* table, /*!< in: table */
  3547. FILE* ef) /*!< in: output stream */
  3548. {
  3549. switch (index_error) {
  3550. case DB_FOREIGN_KEY_INDEX_NOT_FOUND: {
  3551. fprintf(ef,
  3552. "%s table '%s' with foreign key constraint"
  3553. " failed. There is no index in the referenced"
  3554. " table where the referenced columns appear"
  3555. " as the first columns near '%s'.\n",
  3556. operation, create_name, latest_foreign);
  3557. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  3558. "%s table '%s' with foreign key constraint"
  3559. " failed. There is no index in the referenced"
  3560. " table where the referenced columns appear"
  3561. " as the first columns near '%s'.",
  3562. operation, create_name, latest_foreign);
  3563. break;
  3564. }
  3565. case DB_FOREIGN_KEY_IS_PREFIX_INDEX: {
  3566. fprintf(ef,
  3567. "%s table '%s' with foreign key constraint"
  3568. " failed. There is only prefix index in the referenced"
  3569. " table where the referenced columns appear"
  3570. " as the first columns near '%s'.\n",
  3571. operation, create_name, latest_foreign);
  3572. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  3573. "%s table '%s' with foreign key constraint"
  3574. " failed. There is only prefix index in the referenced"
  3575. " table where the referenced columns appear"
  3576. " as the first columns near '%s'.",
  3577. operation, create_name, latest_foreign);
  3578. break;
  3579. }
  3580. case DB_FOREIGN_KEY_COL_NOT_NULL: {
  3581. fprintf(ef,
  3582. "%s table %s with foreign key constraint"
  3583. " failed. You have defined a SET NULL condition but "
  3584. "column '%s' on index is defined as NOT NULL near '%s'.\n",
  3585. operation, create_name, columns[err_col], latest_foreign);
  3586. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  3587. "%s table %s with foreign key constraint"
  3588. " failed. You have defined a SET NULL condition but "
  3589. "column '%s' on index is defined as NOT NULL near '%s'.",
  3590. operation, create_name, columns[err_col], latest_foreign);
  3591. break;
  3592. }
  3593. case DB_FOREIGN_KEY_COLS_NOT_EQUAL: {
  3594. dict_field_t* field;
  3595. const char* col_name;
  3596. field = dict_index_get_nth_field(err_index, err_col);
  3597. col_name = dict_table_get_col_name(
  3598. table, dict_col_get_no(field->col));
  3599. fprintf(ef,
  3600. "%s table %s with foreign key constraint"
  3601. " failed. Field type or character set for column '%s' "
  3602. "does not mach referenced column '%s' near '%s'.\n",
  3603. operation, create_name, columns[err_col], col_name, latest_foreign);
  3604. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  3605. "%s table %s with foreign key constraint"
  3606. " failed. Field type or character set for column '%s' "
  3607. "does not mach referenced column '%s' near '%s'.",
  3608. operation, create_name, columns[err_col], col_name, latest_foreign);
  3609. break;
  3610. }
  3611. default:
  3612. ut_error;
  3613. }
  3614. }
  3615. /*********************************************************************//**
  3616. Scans a table create SQL string and adds to the data dictionary the foreign
  3617. key constraints declared in the string. This function should be called after
  3618. the indexes for a table have been created. Each foreign key constraint must
  3619. be accompanied with indexes in both participating tables. The indexes are
  3620. allowed to contain more fields than mentioned in the constraint.
  3621. @return error code or DB_SUCCESS */
  3622. static
  3623. dberr_t
  3624. dict_create_foreign_constraints_low(
  3625. /*================================*/
  3626. trx_t* trx, /*!< in: transaction */
  3627. mem_heap_t* heap, /*!< in: memory heap */
  3628. struct charset_info_st* cs,/*!< in: the character set of sql_string */
  3629. const char* sql_string,
  3630. /*!< in: CREATE TABLE or ALTER TABLE statement
  3631. where foreign keys are declared like:
  3632. FOREIGN KEY (a, b) REFERENCES table2(c, d),
  3633. table2 can be written also with the database
  3634. name before it: test.table2; the default
  3635. database is the database of parameter name */
  3636. const char* name, /*!< in: table full name in the normalized form
  3637. database_name/table_name */
  3638. ibool reject_fks)
  3639. /*!< in: if TRUE, fail with error code
  3640. DB_CANNOT_ADD_CONSTRAINT if any foreign
  3641. keys are found. */
  3642. {
  3643. dict_table_t* table = NULL;
  3644. dict_table_t* referenced_table = NULL;
  3645. dict_table_t* table_to_alter = NULL;
  3646. dict_table_t* table_to_create = NULL;
  3647. ulint highest_id_so_far = 0;
  3648. ulint number = 1;
  3649. dict_index_t* index = NULL;
  3650. dict_foreign_t* foreign = NULL;
  3651. const char* ptr = sql_string;
  3652. const char* start_of_latest_foreign = sql_string;
  3653. const char* start_of_latest_set = NULL;
  3654. FILE* ef = dict_foreign_err_file;
  3655. ulint index_error = DB_SUCCESS;
  3656. dict_index_t* err_index = NULL;
  3657. ulint err_col;
  3658. const char* constraint_name;
  3659. ibool success;
  3660. dberr_t error;
  3661. const char* ptr1;
  3662. const char* ptr2;
  3663. ulint i;
  3664. ulint j;
  3665. ibool is_on_delete;
  3666. ulint n_on_deletes;
  3667. ulint n_on_updates;
  3668. const dict_col_t*columns[500];
  3669. const char* column_names[500];
  3670. const char* ref_column_names[500];
  3671. const char* referenced_table_name;
  3672. dict_foreign_set local_fk_set;
  3673. dict_foreign_set_free local_fk_set_free(local_fk_set);
  3674. const char* create_table_name;
  3675. const char* orig;
  3676. char create_name[MAX_TABLE_NAME_LEN + 1];
  3677. char operation[8];
  3678. ut_ad(!srv_read_only_mode);
  3679. ut_ad(mutex_own(&(dict_sys->mutex)));
  3680. table = dict_table_get_low(name);
  3681. /* First check if we are actually doing an ALTER TABLE, and in that
  3682. case look for the table being altered */
  3683. orig = ptr;
  3684. ptr = dict_accept(cs, ptr, "ALTER", &success);
  3685. strcpy((char *)operation, success ? "Alter " : "Create ");
  3686. if (!success) {
  3687. orig = ptr;
  3688. ptr = dict_scan_to(ptr, "CREATE");
  3689. ptr = dict_scan_to(ptr, "TABLE");
  3690. ptr = dict_accept(cs, ptr, "TABLE", &success);
  3691. if (success) {
  3692. ptr = dict_scan_table_name(cs, ptr, &table_to_create, name,
  3693. &success, heap, &create_table_name);
  3694. }
  3695. if (success) {
  3696. char *bufend;
  3697. bufend = innobase_convert_name((char *)create_name, MAX_TABLE_NAME_LEN,
  3698. create_table_name, strlen(create_table_name),
  3699. trx->mysql_thd, TRUE);
  3700. create_name[bufend-create_name]='\0';
  3701. ptr = orig;
  3702. } else {
  3703. char *bufend;
  3704. ptr = orig;
  3705. bufend = innobase_convert_name((char *)create_name, MAX_TABLE_NAME_LEN,
  3706. name, strlen(name), trx->mysql_thd, TRUE);
  3707. create_name[bufend-create_name]='\0';
  3708. }
  3709. goto loop;
  3710. }
  3711. if (table == NULL) {
  3712. mutex_enter(&dict_foreign_err_mutex);
  3713. dict_foreign_error_report_low(ef, create_name);
  3714. dict_foreign_error_report_low(ef, create_name);
  3715. fprintf(ef, "%s table %s with foreign key constraint"
  3716. " failed. Table %s not found from data dictionary."
  3717. " Error close to %s.\n",
  3718. operation, create_name, create_name, start_of_latest_foreign);
  3719. mutex_exit(&dict_foreign_err_mutex);
  3720. ib_push_warning(trx, DB_ERROR,
  3721. "%s table %s with foreign key constraint"
  3722. " failed. Table %s not found from data dictionary."
  3723. " Error close to %s.",
  3724. operation, create_name, create_name, start_of_latest_foreign);
  3725. return(DB_ERROR);
  3726. }
  3727. /* If not alter table jump to loop */
  3728. if (!success) {
  3729. goto loop;
  3730. }
  3731. orig = ptr;
  3732. ptr = dict_accept(cs, ptr, "TABLE", &success);
  3733. if (!success) {
  3734. goto loop;
  3735. }
  3736. /* We are doing an ALTER TABLE: scan the table name we are altering */
  3737. orig = ptr;
  3738. ptr = dict_scan_table_name(cs, ptr, &table_to_alter, name,
  3739. &success, heap, &referenced_table_name);
  3740. if (table_to_alter) {
  3741. char *bufend;
  3742. bufend = innobase_convert_name((char *)create_name, MAX_TABLE_NAME_LEN,
  3743. table_to_alter->name, strlen(table_to_alter->name),
  3744. trx->mysql_thd, TRUE);
  3745. create_name[bufend-create_name]='\0';
  3746. } else {
  3747. char *bufend;
  3748. bufend = innobase_convert_name((char *)create_name, MAX_TABLE_NAME_LEN,
  3749. referenced_table_name, strlen(referenced_table_name),
  3750. trx->mysql_thd, TRUE);
  3751. create_name[bufend-create_name]='\0';
  3752. }
  3753. if (!success) {
  3754. mutex_enter(&dict_foreign_err_mutex);
  3755. dict_foreign_error_report_low(ef, create_name);
  3756. fprintf(ef,
  3757. "%s table %s with foreign key constraint"
  3758. " failed. Table %s not found from data dictionary."
  3759. " Error close to %s.\n",
  3760. operation, create_name, create_name, orig);
  3761. mutex_exit(&dict_foreign_err_mutex);
  3762. ib_push_warning(trx, DB_ERROR,
  3763. "%s table %s with foreign key constraint"
  3764. " failed. Table %s not found from data dictionary."
  3765. " Error close to %s.",
  3766. operation, create_name, create_name, orig);
  3767. return(DB_ERROR);
  3768. }
  3769. /* Starting from 4.0.18 and 4.1.2, we generate foreign key id's in the
  3770. format databasename/tablename_ibfk_[number], where [number] is local
  3771. to the table; look for the highest [number] for table_to_alter, so
  3772. that we can assign to new constraints higher numbers. */
  3773. /* If we are altering a temporary table, the table name after ALTER
  3774. TABLE does not correspond to the internal table name, and
  3775. table_to_alter is NULL. TODO: should we fix this somehow? */
  3776. if (table_to_alter == NULL) {
  3777. highest_id_so_far = 0;
  3778. } else {
  3779. highest_id_so_far = dict_table_get_highest_foreign_id(
  3780. table_to_alter);
  3781. }
  3782. number = highest_id_so_far + 1;
  3783. /* Scan for foreign key declarations in a loop */
  3784. loop:
  3785. /* Scan either to "CONSTRAINT" or "FOREIGN", whichever is closer */
  3786. ptr1 = dict_scan_to(ptr, "CONSTRAINT");
  3787. ptr2 = dict_scan_to(ptr, "FOREIGN");
  3788. constraint_name = NULL;
  3789. if (ptr1 < ptr2) {
  3790. /* The user may have specified a constraint name. Pick it so
  3791. that we can store 'databasename/constraintname' as the id of
  3792. of the constraint to system tables. */
  3793. ptr = ptr1;
  3794. orig = ptr;
  3795. ptr = dict_accept(cs, ptr, "CONSTRAINT", &success);
  3796. ut_a(success);
  3797. if (!my_isspace(cs, *ptr) && *ptr != '"' && *ptr != '`') {
  3798. goto loop;
  3799. }
  3800. while (my_isspace(cs, *ptr)) {
  3801. ptr++;
  3802. }
  3803. /* read constraint name unless got "CONSTRAINT FOREIGN" */
  3804. if (ptr != ptr2) {
  3805. ptr = dict_scan_id(cs, ptr, heap,
  3806. &constraint_name, FALSE, FALSE);
  3807. }
  3808. } else {
  3809. ptr = ptr2;
  3810. }
  3811. if (*ptr == '\0') {
  3812. /* The proper way to reject foreign keys for temporary
  3813. tables would be to split the lexing and syntactical
  3814. analysis of foreign key clauses from the actual adding
  3815. of them, so that ha_innodb.cc could first parse the SQL
  3816. command, determine if there are any foreign keys, and
  3817. if so, immediately reject the command if the table is a
  3818. temporary one. For now, this kludge will work. */
  3819. if (reject_fks && !local_fk_set.empty()) {
  3820. mutex_enter(&dict_foreign_err_mutex);
  3821. dict_foreign_error_report_low(ef, create_name);
  3822. fprintf(ef, "%s table %s with foreign key constraint"
  3823. " failed. Temporary tables can't have foreign key constraints."
  3824. " Error close to %s.\n",
  3825. operation, create_name, start_of_latest_foreign);
  3826. mutex_exit(&dict_foreign_err_mutex);
  3827. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  3828. "%s table %s with foreign key constraint"
  3829. " failed. Temporary tables can't have foreign key constraints."
  3830. " Error close to %s.",
  3831. operation, create_name, start_of_latest_foreign);
  3832. return(DB_CANNOT_ADD_CONSTRAINT);
  3833. }
  3834. /**********************************************************/
  3835. /* The following call adds the foreign key constraints
  3836. to the data dictionary system tables on disk */
  3837. error = dict_create_add_foreigns_to_dictionary(
  3838. local_fk_set, table, trx);
  3839. if (error == DB_SUCCESS) {
  3840. table->foreign_set.insert(local_fk_set.begin(),
  3841. local_fk_set.end());
  3842. std::for_each(local_fk_set.begin(),
  3843. local_fk_set.end(),
  3844. dict_foreign_add_to_referenced_table());
  3845. local_fk_set.clear();
  3846. }
  3847. return(error);
  3848. }
  3849. start_of_latest_foreign = ptr;
  3850. orig = ptr;
  3851. ptr = dict_accept(cs, ptr, "FOREIGN", &success);
  3852. if (!success) {
  3853. goto loop;
  3854. }
  3855. if (!my_isspace(cs, *ptr)) {
  3856. goto loop;
  3857. }
  3858. orig = ptr;
  3859. ptr = dict_accept(cs, ptr, "KEY", &success);
  3860. if (!success) {
  3861. goto loop;
  3862. }
  3863. if (my_isspace(cs, *ptr)) {
  3864. ptr1 = dict_accept(cs, ptr, "IF", &success);
  3865. if (success) {
  3866. if (!my_isspace(cs, *ptr1)) {
  3867. goto loop;
  3868. }
  3869. ptr1 = dict_accept(cs, ptr1, "NOT", &success);
  3870. if (!success) {
  3871. goto loop;
  3872. }
  3873. ptr1 = dict_accept(cs, ptr1, "EXISTS", &success);
  3874. if (!success) {
  3875. goto loop;
  3876. }
  3877. ptr = ptr1;
  3878. }
  3879. }
  3880. orig = ptr;
  3881. ptr = dict_accept(cs, ptr, "(", &success);
  3882. if (!success) {
  3883. if (constraint_name) {
  3884. /* MySQL allows also an index id before the '('; we
  3885. skip it */
  3886. ptr = dict_skip_word(cs, ptr, &success);
  3887. if (!success) {
  3888. dict_foreign_report_syntax_err(
  3889. "%s table %s with foreign key constraint"
  3890. " failed. Parse error in '%s'"
  3891. " near '%s'.\n",
  3892. operation, create_name, start_of_latest_foreign, orig);
  3893. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  3894. "%s table %s with foreign key constraint"
  3895. " failed. Parse error in '%s'"
  3896. " near '%s'.",
  3897. operation, create_name, start_of_latest_foreign, orig);
  3898. return(DB_CANNOT_ADD_CONSTRAINT);
  3899. }
  3900. }
  3901. else {
  3902. while (my_isspace(cs, *ptr)) {
  3903. ptr++;
  3904. }
  3905. ptr = dict_scan_id(cs, ptr, heap,
  3906. &constraint_name, FALSE, FALSE);
  3907. }
  3908. ptr = dict_accept(cs, ptr, "(", &success);
  3909. if (!success) {
  3910. /* We do not flag a syntax error here because in an
  3911. ALTER TABLE we may also have DROP FOREIGN KEY abc */
  3912. goto loop;
  3913. }
  3914. }
  3915. i = 0;
  3916. /* Scan the columns in the first list */
  3917. col_loop1:
  3918. ut_a(i < (sizeof column_names) / sizeof *column_names);
  3919. orig = ptr;
  3920. ptr = dict_scan_col(cs, ptr, &success, table, columns + i,
  3921. heap, column_names + i);
  3922. if (!success) {
  3923. mutex_enter(&dict_foreign_err_mutex);
  3924. dict_foreign_error_report_low(ef, create_name);
  3925. fprintf(ef,
  3926. "%s table %s with foreign key constraint"
  3927. " failed. Parse error in '%s'"
  3928. " near '%s'.\n",
  3929. operation, create_name, start_of_latest_foreign, orig);
  3930. mutex_exit(&dict_foreign_err_mutex);
  3931. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  3932. "%s table %s with foreign key constraint"
  3933. " failed. Parse error in '%s'"
  3934. " near '%s'.",
  3935. operation, create_name, start_of_latest_foreign, orig);
  3936. return(DB_CANNOT_ADD_CONSTRAINT);
  3937. }
  3938. i++;
  3939. ptr = dict_accept(cs, ptr, ",", &success);
  3940. if (success) {
  3941. goto col_loop1;
  3942. }
  3943. orig = ptr;
  3944. ptr = dict_accept(cs, ptr, ")", &success);
  3945. if (!success) {
  3946. dict_foreign_report_syntax_err(
  3947. "%s table %s with foreign key constraint"
  3948. " failed. Parse error in '%s'"
  3949. " near '%s'.\n",
  3950. operation, create_name, start_of_latest_foreign, orig);
  3951. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  3952. "%s table %s with foreign key constraint"
  3953. " failed. Parse error in '%s'"
  3954. " near '%s'.",
  3955. operation, create_name, start_of_latest_foreign, orig);
  3956. return(DB_CANNOT_ADD_CONSTRAINT);
  3957. }
  3958. /* Try to find an index which contains the columns
  3959. as the first fields and in the right order. There is
  3960. no need to check column type match (on types_idx), since
  3961. the referenced table can be NULL if foreign_key_checks is
  3962. set to 0 */
  3963. index = dict_foreign_find_index(
  3964. table, NULL, column_names, i,
  3965. NULL, TRUE, FALSE, &index_error, &err_col, &err_index);
  3966. if (!index) {
  3967. mutex_enter(&dict_foreign_err_mutex);
  3968. dict_foreign_error_report_low(ef, create_name);
  3969. fputs("There is no index in table ", ef);
  3970. ut_print_name(ef, NULL, TRUE, create_name);
  3971. fprintf(ef, " where the columns appear\n"
  3972. "as the first columns. Constraint:\n%s\n"
  3973. "See " REFMAN "innodb-foreign-key-constraints.html\n"
  3974. "for correct foreign key definition.\n",
  3975. start_of_latest_foreign);
  3976. dict_foreign_push_index_error(trx, operation, create_name, start_of_latest_foreign,
  3977. column_names, index_error, err_col, err_index, table, ef);
  3978. mutex_exit(&dict_foreign_err_mutex);
  3979. return(DB_CANNOT_ADD_CONSTRAINT);
  3980. }
  3981. orig = ptr;
  3982. ptr = dict_accept(cs, ptr, "REFERENCES", &success);
  3983. if (!success || !my_isspace(cs, *ptr)) {
  3984. dict_foreign_report_syntax_err(
  3985. "%s table %s with foreign key constraint"
  3986. " failed. Parse error in '%s'"
  3987. " near '%s'.\n",
  3988. operation, create_name, start_of_latest_foreign, orig);
  3989. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  3990. "%s table %s with foreign key constraint"
  3991. " failed. Parse error in '%s'"
  3992. " near '%s'.",
  3993. operation, create_name, start_of_latest_foreign, orig);
  3994. return(DB_CANNOT_ADD_CONSTRAINT);
  3995. }
  3996. /* Let us create a constraint struct */
  3997. foreign = dict_mem_foreign_create();
  3998. if (constraint_name) {
  3999. ulint db_len;
  4000. /* Catenate 'databasename/' to the constraint name specified
  4001. by the user: we conceive the constraint as belonging to the
  4002. same MySQL 'database' as the table itself. We store the name
  4003. to foreign->id. */
  4004. db_len = dict_get_db_name_len(table->name);
  4005. foreign->id = static_cast<char*>(mem_heap_alloc(
  4006. foreign->heap, db_len + strlen(constraint_name) + 2));
  4007. ut_memcpy(foreign->id, table->name, db_len);
  4008. foreign->id[db_len] = '/';
  4009. strcpy(foreign->id + db_len + 1, constraint_name);
  4010. }
  4011. if (foreign->id == NULL) {
  4012. error = dict_create_add_foreign_id(&number,
  4013. table->name, foreign);
  4014. if (error != DB_SUCCESS) {
  4015. dict_foreign_free(foreign);
  4016. return(error);
  4017. }
  4018. }
  4019. std::pair<dict_foreign_set::iterator, bool> ret
  4020. = local_fk_set.insert(foreign);
  4021. if (!ret.second) {
  4022. /* A duplicate foreign key name has been found */
  4023. dict_foreign_free(foreign);
  4024. return(DB_CANNOT_ADD_CONSTRAINT);
  4025. }
  4026. foreign->foreign_table = table;
  4027. foreign->foreign_table_name = mem_heap_strdup(
  4028. foreign->heap, table->name);
  4029. dict_mem_foreign_table_name_lookup_set(foreign, TRUE);
  4030. foreign->foreign_index = index;
  4031. foreign->n_fields = (unsigned int) i;
  4032. foreign->foreign_col_names = static_cast<const char**>(
  4033. mem_heap_alloc(foreign->heap, i * sizeof(void*)));
  4034. for (i = 0; i < foreign->n_fields; i++) {
  4035. foreign->foreign_col_names[i] = mem_heap_strdup(
  4036. foreign->heap,
  4037. dict_table_get_col_name(table,
  4038. dict_col_get_no(columns[i])));
  4039. }
  4040. ptr = dict_scan_table_name(cs, ptr, &referenced_table, name,
  4041. &success, heap, &referenced_table_name);
  4042. /* Note that referenced_table can be NULL if the user has suppressed
  4043. checking of foreign key constraints! */
  4044. if (!success || (!referenced_table && trx->check_foreigns)) {
  4045. char buf[MAX_TABLE_NAME_LEN + 1] = "";
  4046. char* bufend;
  4047. bufend = innobase_convert_name(buf, MAX_TABLE_NAME_LEN,
  4048. referenced_table_name, strlen(referenced_table_name),
  4049. trx->mysql_thd, TRUE);
  4050. buf[bufend - buf] = '\0';
  4051. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  4052. "%s table %s with foreign key constraint failed. Referenced table %s not found in the data dictionary "
  4053. "near '%s'.",
  4054. operation, create_name, buf, start_of_latest_foreign);
  4055. mutex_enter(&dict_foreign_err_mutex);
  4056. dict_foreign_error_report_low(ef, create_name);
  4057. fprintf(ef,
  4058. "%s table %s with foreign key constraint failed. Referenced table %s not found in the data dictionary "
  4059. "near '%s'.\n",
  4060. operation, create_name, buf, start_of_latest_foreign);
  4061. mutex_exit(&dict_foreign_err_mutex);
  4062. return(DB_CANNOT_ADD_CONSTRAINT);
  4063. }
  4064. orig = ptr;
  4065. ptr = dict_accept(cs, ptr, "(", &success);
  4066. if (!success) {
  4067. dict_foreign_report_syntax_err(
  4068. "%s table %s with foreign key constraint"
  4069. " failed. Parse error in '%s'"
  4070. " near '%s'.\n",
  4071. operation, create_name, start_of_latest_foreign, orig);
  4072. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  4073. "%s table %s with foreign key constraint"
  4074. " failed. Parse error in '%s'"
  4075. " near '%s'.",
  4076. operation, create_name, start_of_latest_foreign, orig);
  4077. return(DB_CANNOT_ADD_CONSTRAINT);
  4078. }
  4079. /* Scan the columns in the second list */
  4080. i = 0;
  4081. col_loop2:
  4082. orig = ptr;
  4083. ptr = dict_scan_col(cs, ptr, &success, referenced_table, columns + i,
  4084. heap, ref_column_names + i);
  4085. i++;
  4086. if (!success) {
  4087. mutex_enter(&dict_foreign_err_mutex);
  4088. dict_foreign_error_report_low(ef, create_name);
  4089. fprintf(ef,
  4090. "%s table %s with foreign key constraint"
  4091. " failed. Parse error in '%s'"
  4092. " near '%s'.\n",
  4093. operation, create_name, start_of_latest_foreign, orig);
  4094. mutex_exit(&dict_foreign_err_mutex);
  4095. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  4096. "%s table %s with foreign key constraint"
  4097. " failed. Parse error in '%s'"
  4098. " near '%s'.",
  4099. operation, create_name, start_of_latest_foreign, orig);
  4100. return(DB_CANNOT_ADD_CONSTRAINT);
  4101. }
  4102. orig = ptr;
  4103. ptr = dict_accept(cs, ptr, ",", &success);
  4104. if (success) {
  4105. goto col_loop2;
  4106. }
  4107. orig = ptr;
  4108. ptr = dict_accept(cs, ptr, ")", &success);
  4109. if (!success || foreign->n_fields != i) {
  4110. dict_foreign_report_syntax_err(
  4111. "%s table %s with foreign key constraint"
  4112. " failed. Parse error in '%s' near '%s'. Referencing column count does not match referenced column count.\n",
  4113. operation, create_name, start_of_latest_foreign, orig);
  4114. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  4115. "%s table %s with foreign key constraint"
  4116. " failed. Parse error in '%s' near '%s'. Referencing column count %d does not match referenced column count %d.\n",
  4117. operation, create_name, start_of_latest_foreign, orig, i, foreign->n_fields);
  4118. return(DB_CANNOT_ADD_CONSTRAINT);
  4119. }
  4120. n_on_deletes = 0;
  4121. n_on_updates = 0;
  4122. scan_on_conditions:
  4123. /* Loop here as long as we can find ON ... conditions */
  4124. start_of_latest_set = ptr;
  4125. ptr = dict_accept(cs, ptr, "ON", &success);
  4126. if (!success) {
  4127. goto try_find_index;
  4128. }
  4129. orig = ptr;
  4130. ptr = dict_accept(cs, ptr, "DELETE", &success);
  4131. if (!success) {
  4132. orig = ptr;
  4133. ptr = dict_accept(cs, ptr, "UPDATE", &success);
  4134. if (!success) {
  4135. dict_foreign_report_syntax_err(
  4136. "%s table %s with foreign key constraint"
  4137. " failed. Parse error in '%s'"
  4138. " near '%s'.\n",
  4139. operation, create_name, start_of_latest_foreign, start_of_latest_set);
  4140. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  4141. "%s table %s with foreign key constraint"
  4142. " failed. Parse error in '%s'"
  4143. " near '%s'.",
  4144. operation, create_name, start_of_latest_foreign, start_of_latest_set);
  4145. return(DB_CANNOT_ADD_CONSTRAINT);
  4146. }
  4147. is_on_delete = FALSE;
  4148. n_on_updates++;
  4149. } else {
  4150. is_on_delete = TRUE;
  4151. n_on_deletes++;
  4152. }
  4153. orig = ptr;
  4154. ptr = dict_accept(cs, ptr, "RESTRICT", &success);
  4155. if (success) {
  4156. goto scan_on_conditions;
  4157. }
  4158. orig = ptr;
  4159. ptr = dict_accept(cs, ptr, "CASCADE", &success);
  4160. if (success) {
  4161. if (is_on_delete) {
  4162. foreign->type |= DICT_FOREIGN_ON_DELETE_CASCADE;
  4163. } else {
  4164. foreign->type |= DICT_FOREIGN_ON_UPDATE_CASCADE;
  4165. }
  4166. goto scan_on_conditions;
  4167. }
  4168. orig = ptr;
  4169. ptr = dict_accept(cs, ptr, "NO", &success);
  4170. if (success) {
  4171. orig = ptr;
  4172. ptr = dict_accept(cs, ptr, "ACTION", &success);
  4173. if (!success) {
  4174. dict_foreign_report_syntax_err(
  4175. "%s table %s with foreign key constraint"
  4176. " failed. Parse error in '%s'"
  4177. " near '%s'.\n",
  4178. operation, create_name, start_of_latest_foreign, start_of_latest_set);
  4179. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  4180. "%s table %s with foreign key constraint"
  4181. " failed. Parse error in '%s'"
  4182. " near '%s'.",
  4183. operation, create_name, start_of_latest_foreign, start_of_latest_set);
  4184. return(DB_CANNOT_ADD_CONSTRAINT);
  4185. }
  4186. if (is_on_delete) {
  4187. foreign->type |= DICT_FOREIGN_ON_DELETE_NO_ACTION;
  4188. } else {
  4189. foreign->type |= DICT_FOREIGN_ON_UPDATE_NO_ACTION;
  4190. }
  4191. goto scan_on_conditions;
  4192. }
  4193. orig = ptr;
  4194. ptr = dict_accept(cs, ptr, "SET", &success);
  4195. if (!success) {
  4196. dict_foreign_report_syntax_err(
  4197. "%s table %s with foreign key constraint"
  4198. " failed. Parse error in '%s'"
  4199. " near '%s'.\n",
  4200. operation, create_name, start_of_latest_foreign, start_of_latest_set);
  4201. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  4202. "%s table %s with foreign key constraint"
  4203. " failed. Parse error in '%s'"
  4204. " near '%s'.",
  4205. operation, create_name, start_of_latest_foreign, start_of_latest_set);
  4206. return(DB_CANNOT_ADD_CONSTRAINT);
  4207. }
  4208. orig = ptr;
  4209. ptr = dict_accept(cs, ptr, "NULL", &success);
  4210. if (!success) {
  4211. dict_foreign_report_syntax_err(
  4212. "%s table %s with foreign key constraint"
  4213. " failed. Parse error in '%s'"
  4214. " near '%s'.\n",
  4215. operation, create_name, start_of_latest_foreign, start_of_latest_set);
  4216. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  4217. "%s table %s with foreign key constraint"
  4218. " failed. Parse error in '%s'"
  4219. " near '%s'.",
  4220. operation, create_name, start_of_latest_foreign, start_of_latest_set);
  4221. return(DB_CANNOT_ADD_CONSTRAINT);
  4222. }
  4223. for (j = 0; j < foreign->n_fields; j++) {
  4224. if ((dict_index_get_nth_col(foreign->foreign_index, j)->prtype)
  4225. & DATA_NOT_NULL) {
  4226. const dict_col_t* col
  4227. = dict_index_get_nth_col(foreign->foreign_index, j);
  4228. const char* col_name = dict_table_get_col_name(foreign->foreign_index->table,
  4229. dict_col_get_no(col));
  4230. /* It is not sensible to define SET NULL
  4231. if the column is not allowed to be NULL! */
  4232. mutex_enter(&dict_foreign_err_mutex);
  4233. dict_foreign_error_report_low(ef, create_name);
  4234. fprintf(ef,
  4235. "%s table %s with foreign key constraint"
  4236. " failed. You have defined a SET NULL condition but column '%s' is defined as NOT NULL"
  4237. " in '%s' near '%s'.\n",
  4238. operation, create_name, col_name, start_of_latest_foreign, start_of_latest_set);
  4239. mutex_exit(&dict_foreign_err_mutex);
  4240. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  4241. "%s table %s with foreign key constraint"
  4242. " failed. You have defined a SET NULL condition but column '%s' is defined as NOT NULL"
  4243. " in '%s' near '%s'.",
  4244. operation, create_name, col_name, start_of_latest_foreign, start_of_latest_set);
  4245. return(DB_CANNOT_ADD_CONSTRAINT);
  4246. }
  4247. }
  4248. if (is_on_delete) {
  4249. foreign->type |= DICT_FOREIGN_ON_DELETE_SET_NULL;
  4250. } else {
  4251. foreign->type |= DICT_FOREIGN_ON_UPDATE_SET_NULL;
  4252. }
  4253. goto scan_on_conditions;
  4254. try_find_index:
  4255. if (n_on_deletes > 1 || n_on_updates > 1) {
  4256. /* It is an error to define more than 1 action */
  4257. mutex_enter(&dict_foreign_err_mutex);
  4258. dict_foreign_error_report_low(ef, create_name);
  4259. fprintf(ef,
  4260. "%s table %s with foreign key constraint"
  4261. " failed. You have more than one on delete or on update clause"
  4262. " in '%s' near '%s'.\n",
  4263. operation, create_name, start_of_latest_foreign, start_of_latest_set);
  4264. ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
  4265. "%s table %s with foreign key constraint"
  4266. " failed. You have more than one on delete or on update clause"
  4267. " in '%s' near '%s'.",
  4268. operation, create_name, start_of_latest_foreign, start_of_latest_set);
  4269. dict_foreign_free(foreign);
  4270. mutex_exit(&dict_foreign_err_mutex);
  4271. return(DB_CANNOT_ADD_CONSTRAINT);
  4272. }
  4273. /* Try to find an index which contains the columns as the first fields
  4274. and in the right order, and the types are the same as in
  4275. foreign->foreign_index */
  4276. if (referenced_table) {
  4277. index = dict_foreign_find_index(referenced_table, NULL,
  4278. ref_column_names, i,
  4279. foreign->foreign_index,
  4280. TRUE, FALSE, &index_error, &err_col, &err_index);
  4281. if (!index) {
  4282. mutex_enter(&dict_foreign_err_mutex);
  4283. dict_foreign_error_report_low(ef, create_name);
  4284. fprintf(ef, "%s:\n"
  4285. "Cannot find an index in the"
  4286. " referenced table where the\n"
  4287. "referenced columns appear as the"
  4288. " first columns, or column types\n"
  4289. "in the table and the referenced table"
  4290. " do not match for constraint.\n"
  4291. "Note that the internal storage type of"
  4292. " ENUM and SET changed in\n"
  4293. "tables created with >= InnoDB-4.1.12,"
  4294. " and such columns in old tables\n"
  4295. "cannot be referenced by such columns"
  4296. " in new tables.\n"
  4297. "See " REFMAN
  4298. "innodb-foreign-key-constraints.html\n"
  4299. "for correct foreign key definition.\n",
  4300. start_of_latest_foreign);
  4301. dict_foreign_push_index_error(trx, operation, create_name, start_of_latest_foreign,
  4302. column_names, index_error, err_col, err_index, referenced_table, ef);
  4303. mutex_exit(&dict_foreign_err_mutex);
  4304. return(DB_CANNOT_ADD_CONSTRAINT);
  4305. }
  4306. } else {
  4307. ut_a(trx->check_foreigns == FALSE);
  4308. index = NULL;
  4309. }
  4310. foreign->referenced_index = index;
  4311. foreign->referenced_table = referenced_table;
  4312. foreign->referenced_table_name = mem_heap_strdup(
  4313. foreign->heap, referenced_table_name);
  4314. dict_mem_referenced_table_name_lookup_set(foreign, TRUE);
  4315. foreign->referenced_col_names = static_cast<const char**>(
  4316. mem_heap_alloc(foreign->heap, i * sizeof(void*)));
  4317. for (i = 0; i < foreign->n_fields; i++) {
  4318. foreign->referenced_col_names[i]
  4319. = mem_heap_strdup(foreign->heap, ref_column_names[i]);
  4320. }
  4321. goto loop;
  4322. }
  4323. /**************************************************************************
  4324. Determines whether a string starts with the specified keyword.
  4325. @return TRUE if str starts with keyword */
  4326. UNIV_INTERN
  4327. ibool
  4328. dict_str_starts_with_keyword(
  4329. /*=========================*/
  4330. THD* thd, /*!< in: MySQL thread handle */
  4331. const char* str, /*!< in: string to scan for keyword */
  4332. const char* keyword) /*!< in: keyword to look for */
  4333. {
  4334. struct charset_info_st* cs = innobase_get_charset(thd);
  4335. ibool success;
  4336. dict_accept(cs, str, keyword, &success);
  4337. return(success);
  4338. }
  4339. /*********************************************************************//**
  4340. Scans a table create SQL string and adds to the data dictionary the foreign
  4341. key constraints declared in the string. This function should be called after
  4342. the indexes for a table have been created. Each foreign key constraint must
  4343. be accompanied with indexes in both participating tables. The indexes are
  4344. allowed to contain more fields than mentioned in the constraint.
  4345. @return error code or DB_SUCCESS */
  4346. UNIV_INTERN
  4347. dberr_t
  4348. dict_create_foreign_constraints(
  4349. /*============================*/
  4350. trx_t* trx, /*!< in: transaction */
  4351. const char* sql_string, /*!< in: table create statement where
  4352. foreign keys are declared like:
  4353. FOREIGN KEY (a, b) REFERENCES
  4354. table2(c, d), table2 can be written
  4355. also with the database
  4356. name before it: test.table2; the
  4357. default database id the database of
  4358. parameter name */
  4359. size_t sql_length, /*!< in: length of sql_string */
  4360. const char* name, /*!< in: table full name in the
  4361. normalized form
  4362. database_name/table_name */
  4363. ibool reject_fks) /*!< in: if TRUE, fail with error
  4364. code DB_CANNOT_ADD_CONSTRAINT if
  4365. any foreign keys are found. */
  4366. {
  4367. char* str;
  4368. dberr_t err;
  4369. mem_heap_t* heap;
  4370. ut_a(trx);
  4371. ut_a(trx->mysql_thd);
  4372. str = dict_strip_comments(sql_string, sql_length);
  4373. heap = mem_heap_create(10000);
  4374. err = dict_create_foreign_constraints_low(
  4375. trx, heap, innobase_get_charset(trx->mysql_thd), str, name,
  4376. reject_fks);
  4377. mem_heap_free(heap);
  4378. mem_free(str);
  4379. return(err);
  4380. }
  4381. /**********************************************************************//**
  4382. Parses the CONSTRAINT id's to be dropped in an ALTER TABLE statement.
  4383. @return DB_SUCCESS or DB_CANNOT_DROP_CONSTRAINT if syntax error or the
  4384. constraint id does not match */
  4385. UNIV_INTERN
  4386. dberr_t
  4387. dict_foreign_parse_drop_constraints(
  4388. /*================================*/
  4389. mem_heap_t* heap, /*!< in: heap from which we can
  4390. allocate memory */
  4391. trx_t* trx, /*!< in: transaction */
  4392. dict_table_t* table, /*!< in: table */
  4393. ulint* n, /*!< out: number of constraints
  4394. to drop */
  4395. const char*** constraints_to_drop) /*!< out: id's of the
  4396. constraints to drop */
  4397. {
  4398. ibool success;
  4399. char* str;
  4400. size_t len;
  4401. const char* ptr;
  4402. const char* ptr1;
  4403. const char* id;
  4404. struct charset_info_st* cs;
  4405. ut_a(trx);
  4406. ut_a(trx->mysql_thd);
  4407. cs = innobase_get_charset(trx->mysql_thd);
  4408. *n = 0;
  4409. *constraints_to_drop = static_cast<const char**>(
  4410. mem_heap_alloc(heap, 1000 * sizeof(char*)));
  4411. ptr = innobase_get_stmt(trx->mysql_thd, &len);
  4412. str = dict_strip_comments(ptr, len);
  4413. ptr = str;
  4414. ut_ad(mutex_own(&(dict_sys->mutex)));
  4415. loop:
  4416. ptr = dict_scan_to(ptr, "DROP");
  4417. if (*ptr == '\0') {
  4418. mem_free(str);
  4419. return(DB_SUCCESS);
  4420. }
  4421. ptr = dict_accept(cs, ptr, "DROP", &success);
  4422. if (!my_isspace(cs, *ptr)) {
  4423. goto loop;
  4424. }
  4425. ptr = dict_accept(cs, ptr, "FOREIGN", &success);
  4426. if (!success || !my_isspace(cs, *ptr)) {
  4427. goto loop;
  4428. }
  4429. ptr = dict_accept(cs, ptr, "KEY", &success);
  4430. if (!success) {
  4431. goto syntax_error;
  4432. }
  4433. ptr1 = dict_accept(cs, ptr, "IF", &success);
  4434. if (success && my_isspace(cs, *ptr1)) {
  4435. ptr1 = dict_accept(cs, ptr1, "EXISTS", &success);
  4436. if (success) {
  4437. ptr = ptr1;
  4438. }
  4439. }
  4440. ptr = dict_scan_id(cs, ptr, heap, &id, FALSE, TRUE);
  4441. if (id == NULL) {
  4442. goto syntax_error;
  4443. }
  4444. ut_a(*n < 1000);
  4445. (*constraints_to_drop)[*n] = id;
  4446. (*n)++;
  4447. if (std::find_if(table->foreign_set.begin(),
  4448. table->foreign_set.end(),
  4449. dict_foreign_matches_id(id))
  4450. == table->foreign_set.end()) {
  4451. if (!srv_read_only_mode) {
  4452. FILE* ef = dict_foreign_err_file;
  4453. mutex_enter(&dict_foreign_err_mutex);
  4454. rewind(ef);
  4455. ut_print_timestamp(ef);
  4456. fputs(" Error in dropping of a foreign key "
  4457. "constraint of table ", ef);
  4458. ut_print_name(ef, NULL, TRUE, table->name);
  4459. fputs(",\nin SQL command\n", ef);
  4460. fputs(str, ef);
  4461. fputs("\nCannot find a constraint with the "
  4462. "given id ", ef);
  4463. ut_print_name(ef, NULL, FALSE, id);
  4464. fputs(".\n", ef);
  4465. mutex_exit(&dict_foreign_err_mutex);
  4466. }
  4467. mem_free(str);
  4468. return(DB_CANNOT_DROP_CONSTRAINT);
  4469. }
  4470. goto loop;
  4471. syntax_error:
  4472. if (!srv_read_only_mode) {
  4473. FILE* ef = dict_foreign_err_file;
  4474. mutex_enter(&dict_foreign_err_mutex);
  4475. rewind(ef);
  4476. ut_print_timestamp(ef);
  4477. fputs(" Syntax error in dropping of a"
  4478. " foreign key constraint of table ", ef);
  4479. ut_print_name(ef, NULL, TRUE, table->name);
  4480. fprintf(ef, ",\n"
  4481. "close to:\n%s\n in SQL command\n%s\n", ptr, str);
  4482. mutex_exit(&dict_foreign_err_mutex);
  4483. }
  4484. mem_free(str);
  4485. return(DB_CANNOT_DROP_CONSTRAINT);
  4486. }
  4487. /*==================== END OF FOREIGN KEY PROCESSING ====================*/
  4488. /**********************************************************************//**
  4489. Returns an index object if it is found in the dictionary cache.
  4490. Assumes that dict_sys->mutex is already being held.
  4491. @return index, NULL if not found */
  4492. UNIV_INTERN
  4493. dict_index_t*
  4494. dict_index_get_if_in_cache_low(
  4495. /*===========================*/
  4496. index_id_t index_id) /*!< in: index id */
  4497. {
  4498. ut_ad(mutex_own(&(dict_sys->mutex)));
  4499. return(dict_index_find_on_id_low(index_id));
  4500. }
  4501. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  4502. /**********************************************************************//**
  4503. Returns an index object if it is found in the dictionary cache.
  4504. @return index, NULL if not found */
  4505. UNIV_INTERN
  4506. dict_index_t*
  4507. dict_index_get_if_in_cache(
  4508. /*=======================*/
  4509. index_id_t index_id) /*!< in: index id */
  4510. {
  4511. dict_index_t* index;
  4512. if (dict_sys == NULL) {
  4513. return(NULL);
  4514. }
  4515. mutex_enter(&(dict_sys->mutex));
  4516. index = dict_index_get_if_in_cache_low(index_id);
  4517. mutex_exit(&(dict_sys->mutex));
  4518. return(index);
  4519. }
  4520. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  4521. #ifdef UNIV_DEBUG
  4522. /**********************************************************************//**
  4523. Checks that a tuple has n_fields_cmp value in a sensible range, so that
  4524. no comparison can occur with the page number field in a node pointer.
  4525. @return TRUE if ok */
  4526. UNIV_INTERN
  4527. ibool
  4528. dict_index_check_search_tuple(
  4529. /*==========================*/
  4530. const dict_index_t* index, /*!< in: index tree */
  4531. const dtuple_t* tuple) /*!< in: tuple used in a search */
  4532. {
  4533. ut_a(index);
  4534. ut_a(dtuple_get_n_fields_cmp(tuple)
  4535. <= dict_index_get_n_unique_in_tree(index));
  4536. return(TRUE);
  4537. }
  4538. #endif /* UNIV_DEBUG */
  4539. /**********************************************************************//**
  4540. Builds a node pointer out of a physical record and a page number.
  4541. @return own: node pointer */
  4542. UNIV_INTERN
  4543. dtuple_t*
  4544. dict_index_build_node_ptr(
  4545. /*======================*/
  4546. const dict_index_t* index, /*!< in: index */
  4547. const rec_t* rec, /*!< in: record for which to build node
  4548. pointer */
  4549. ulint page_no,/*!< in: page number to put in node
  4550. pointer */
  4551. mem_heap_t* heap, /*!< in: memory heap where pointer
  4552. created */
  4553. ulint level) /*!< in: level of rec in tree:
  4554. 0 means leaf level */
  4555. {
  4556. dtuple_t* tuple;
  4557. dfield_t* field;
  4558. byte* buf;
  4559. ulint n_unique;
  4560. if (dict_index_is_univ(index)) {
  4561. /* In a universal index tree, we take the whole record as
  4562. the node pointer if the record is on the leaf level,
  4563. on non-leaf levels we remove the last field, which
  4564. contains the page number of the child page */
  4565. ut_a(!dict_table_is_comp(index->table));
  4566. n_unique = rec_get_n_fields_old(rec);
  4567. if (level > 0) {
  4568. ut_a(n_unique > 1);
  4569. n_unique--;
  4570. }
  4571. } else {
  4572. n_unique = dict_index_get_n_unique_in_tree(index);
  4573. }
  4574. tuple = dtuple_create(heap, n_unique + 1);
  4575. /* When searching in the tree for the node pointer, we must not do
  4576. comparison on the last field, the page number field, as on upper
  4577. levels in the tree there may be identical node pointers with a
  4578. different page number; therefore, we set the n_fields_cmp to one
  4579. less: */
  4580. dtuple_set_n_fields_cmp(tuple, n_unique);
  4581. dict_index_copy_types(tuple, index, n_unique);
  4582. buf = static_cast<byte*>(mem_heap_alloc(heap, 4));
  4583. mach_write_to_4(buf, page_no);
  4584. field = dtuple_get_nth_field(tuple, n_unique);
  4585. dfield_set_data(field, buf, 4);
  4586. dtype_set(dfield_get_type(field), DATA_SYS_CHILD, DATA_NOT_NULL, 4);
  4587. rec_copy_prefix_to_dtuple(tuple, rec, index, n_unique, heap);
  4588. dtuple_set_info_bits(tuple, dtuple_get_info_bits(tuple)
  4589. | REC_STATUS_NODE_PTR);
  4590. ut_ad(dtuple_check_typed(tuple));
  4591. return(tuple);
  4592. }
  4593. /**********************************************************************//**
  4594. Copies an initial segment of a physical record, long enough to specify an
  4595. index entry uniquely.
  4596. @return pointer to the prefix record */
  4597. UNIV_INTERN
  4598. rec_t*
  4599. dict_index_copy_rec_order_prefix(
  4600. /*=============================*/
  4601. const dict_index_t* index, /*!< in: index */
  4602. const rec_t* rec, /*!< in: record for which to
  4603. copy prefix */
  4604. ulint* n_fields,/*!< out: number of fields copied */
  4605. byte** buf, /*!< in/out: memory buffer for the
  4606. copied prefix, or NULL */
  4607. ulint* buf_size)/*!< in/out: buffer size */
  4608. {
  4609. ulint n;
  4610. UNIV_PREFETCH_R(rec);
  4611. if (dict_index_is_univ(index)) {
  4612. ut_a(!dict_table_is_comp(index->table));
  4613. n = rec_get_n_fields_old(rec);
  4614. } else {
  4615. n = dict_index_get_n_unique_in_tree(index);
  4616. }
  4617. *n_fields = n;
  4618. return(rec_copy_prefix_to_buf(rec, index, n, buf, buf_size));
  4619. }
  4620. /**********************************************************************//**
  4621. Builds a typed data tuple out of a physical record.
  4622. @return own: data tuple */
  4623. UNIV_INTERN
  4624. dtuple_t*
  4625. dict_index_build_data_tuple(
  4626. /*========================*/
  4627. dict_index_t* index, /*!< in: index tree */
  4628. rec_t* rec, /*!< in: record for which to build data tuple */
  4629. ulint n_fields,/*!< in: number of data fields */
  4630. mem_heap_t* heap) /*!< in: memory heap where tuple created */
  4631. {
  4632. dtuple_t* tuple;
  4633. ut_ad(dict_table_is_comp(index->table)
  4634. || n_fields <= rec_get_n_fields_old(rec));
  4635. tuple = dtuple_create(heap, n_fields);
  4636. dict_index_copy_types(tuple, index, n_fields);
  4637. rec_copy_prefix_to_dtuple(tuple, rec, index, n_fields, heap);
  4638. ut_ad(dtuple_check_typed(tuple));
  4639. return(tuple);
  4640. }
  4641. /*********************************************************************//**
  4642. Calculates the minimum record length in an index. */
  4643. UNIV_INTERN
  4644. ulint
  4645. dict_index_calc_min_rec_len(
  4646. /*========================*/
  4647. const dict_index_t* index) /*!< in: index */
  4648. {
  4649. ulint sum = 0;
  4650. ulint i;
  4651. ulint comp = dict_table_is_comp(index->table);
  4652. if (comp) {
  4653. ulint nullable = 0;
  4654. sum = REC_N_NEW_EXTRA_BYTES;
  4655. for (i = 0; i < dict_index_get_n_fields(index); i++) {
  4656. const dict_col_t* col
  4657. = dict_index_get_nth_col(index, i);
  4658. ulint size = dict_col_get_fixed_size(col, comp);
  4659. sum += size;
  4660. if (!size) {
  4661. size = col->len;
  4662. sum += size < 128 ? 1 : 2;
  4663. }
  4664. if (!(col->prtype & DATA_NOT_NULL)) {
  4665. nullable++;
  4666. }
  4667. }
  4668. /* round the NULL flags up to full bytes */
  4669. sum += UT_BITS_IN_BYTES(nullable);
  4670. return(sum);
  4671. }
  4672. for (i = 0; i < dict_index_get_n_fields(index); i++) {
  4673. sum += dict_col_get_fixed_size(
  4674. dict_index_get_nth_col(index, i), comp);
  4675. }
  4676. if (sum > 127) {
  4677. sum += 2 * dict_index_get_n_fields(index);
  4678. } else {
  4679. sum += dict_index_get_n_fields(index);
  4680. }
  4681. sum += REC_N_OLD_EXTRA_BYTES;
  4682. return(sum);
  4683. }
  4684. /**********************************************************************//**
  4685. Prints info of a foreign key constraint. */
  4686. static
  4687. void
  4688. dict_foreign_print_low(
  4689. /*===================*/
  4690. dict_foreign_t* foreign) /*!< in: foreign key constraint */
  4691. {
  4692. ulint i;
  4693. ut_ad(mutex_own(&(dict_sys->mutex)));
  4694. fprintf(stderr, " FOREIGN KEY CONSTRAINT %s: %s (",
  4695. foreign->id, foreign->foreign_table_name);
  4696. for (i = 0; i < foreign->n_fields; i++) {
  4697. fprintf(stderr, " %s", foreign->foreign_col_names[i]);
  4698. }
  4699. fprintf(stderr, " )\n"
  4700. " REFERENCES %s (",
  4701. foreign->referenced_table_name);
  4702. for (i = 0; i < foreign->n_fields; i++) {
  4703. fprintf(stderr, " %s", foreign->referenced_col_names[i]);
  4704. }
  4705. fputs(" )\n", stderr);
  4706. }
  4707. /**********************************************************************//**
  4708. Prints a table data. */
  4709. UNIV_INTERN
  4710. void
  4711. dict_table_print(
  4712. /*=============*/
  4713. dict_table_t* table) /*!< in: table */
  4714. {
  4715. dict_index_t* index;
  4716. ulint i;
  4717. ut_ad(mutex_own(&(dict_sys->mutex)));
  4718. dict_table_stats_lock(table, RW_X_LATCH);
  4719. if (!table->stat_initialized) {
  4720. dict_stats_update_transient(table);
  4721. }
  4722. fprintf(stderr,
  4723. "--------------------------------------\n"
  4724. "TABLE: name %s, id %llu, flags %lx, columns %lu,"
  4725. " indexes %lu, appr.rows " UINT64PF "\n"
  4726. " COLUMNS: ",
  4727. table->name,
  4728. (ullint) table->id,
  4729. (ulong) table->flags,
  4730. (ulong) table->n_cols,
  4731. (ulong) UT_LIST_GET_LEN(table->indexes),
  4732. table->stat_n_rows);
  4733. for (i = 0; i < (ulint) table->n_cols; i++) {
  4734. dict_col_print_low(table, dict_table_get_nth_col(table, i));
  4735. fputs("; ", stderr);
  4736. }
  4737. putc('\n', stderr);
  4738. index = UT_LIST_GET_FIRST(table->indexes);
  4739. while (index != NULL) {
  4740. dict_index_print_low(index);
  4741. index = UT_LIST_GET_NEXT(indexes, index);
  4742. }
  4743. dict_table_stats_unlock(table, RW_X_LATCH);
  4744. std::for_each(table->foreign_set.begin(),
  4745. table->foreign_set.end(),
  4746. dict_foreign_print_low);
  4747. std::for_each(table->referenced_set.begin(),
  4748. table->referenced_set.end(),
  4749. dict_foreign_print_low);
  4750. }
  4751. /**********************************************************************//**
  4752. Prints a column data. */
  4753. static
  4754. void
  4755. dict_col_print_low(
  4756. /*===============*/
  4757. const dict_table_t* table, /*!< in: table */
  4758. const dict_col_t* col) /*!< in: column */
  4759. {
  4760. dtype_t type;
  4761. ut_ad(mutex_own(&(dict_sys->mutex)));
  4762. dict_col_copy_type(col, &type);
  4763. fprintf(stderr, "%s: ", dict_table_get_col_name(table,
  4764. dict_col_get_no(col)));
  4765. dtype_print(&type);
  4766. }
  4767. /**********************************************************************//**
  4768. Prints an index data. */
  4769. static
  4770. void
  4771. dict_index_print_low(
  4772. /*=================*/
  4773. dict_index_t* index) /*!< in: index */
  4774. {
  4775. ib_int64_t n_vals;
  4776. ulint i;
  4777. ut_a(index->table->stat_initialized);
  4778. ut_ad(mutex_own(&(dict_sys->mutex)));
  4779. if (index->n_user_defined_cols > 0) {
  4780. n_vals = index->stat_n_diff_key_vals[
  4781. index->n_user_defined_cols - 1];
  4782. } else {
  4783. n_vals = index->stat_n_diff_key_vals[0];
  4784. }
  4785. fprintf(stderr,
  4786. " INDEX: name %s, id %llu, fields %lu/%lu,"
  4787. " uniq %lu, type %lu\n"
  4788. " root page %lu, appr.key vals %lu,"
  4789. " leaf pages %lu, size pages %lu\n"
  4790. " FIELDS: ",
  4791. index->name,
  4792. (ullint) index->id,
  4793. (ulong) index->n_user_defined_cols,
  4794. (ulong) index->n_fields,
  4795. (ulong) index->n_uniq,
  4796. (ulong) index->type,
  4797. (ulong) index->page,
  4798. (ulong) n_vals,
  4799. (ulong) index->stat_n_leaf_pages,
  4800. (ulong) index->stat_index_size);
  4801. for (i = 0; i < index->n_fields; i++) {
  4802. dict_field_print_low(dict_index_get_nth_field(index, i));
  4803. }
  4804. putc('\n', stderr);
  4805. #ifdef UNIV_BTR_PRINT
  4806. btr_print_size(index);
  4807. btr_print_index(index, 7);
  4808. #endif /* UNIV_BTR_PRINT */
  4809. }
  4810. /**********************************************************************//**
  4811. Prints a field data. */
  4812. static
  4813. void
  4814. dict_field_print_low(
  4815. /*=================*/
  4816. const dict_field_t* field) /*!< in: field */
  4817. {
  4818. ut_ad(mutex_own(&(dict_sys->mutex)));
  4819. fprintf(stderr, " %s", field->name);
  4820. if (field->prefix_len != 0) {
  4821. fprintf(stderr, "(%lu)", (ulong) field->prefix_len);
  4822. }
  4823. }
  4824. /**********************************************************************//**
  4825. Outputs info on a foreign key of a table in a format suitable for
  4826. CREATE TABLE. */
  4827. UNIV_INTERN
  4828. std::string
  4829. dict_print_info_on_foreign_key_in_create_format(
  4830. /*============================================*/
  4831. trx_t* trx, /*!< in: transaction */
  4832. dict_foreign_t* foreign, /*!< in: foreign key constraint */
  4833. ibool add_newline) /*!< in: whether to add a newline */
  4834. {
  4835. const char* stripped_id;
  4836. ulint i;
  4837. std::string str;
  4838. if (strchr(foreign->id, '/')) {
  4839. /* Strip the preceding database name from the constraint id */
  4840. stripped_id = foreign->id + 1
  4841. + dict_get_db_name_len(foreign->id);
  4842. } else {
  4843. stripped_id = foreign->id;
  4844. }
  4845. str.append(",");
  4846. if (add_newline) {
  4847. /* SHOW CREATE TABLE wants constraints each printed nicely
  4848. on its own line, while error messages want no newlines
  4849. inserted. */
  4850. str.append("\n ");
  4851. }
  4852. str.append(" CONSTRAINT ");
  4853. str.append(ut_get_name(trx, FALSE, stripped_id));
  4854. str.append(" FOREIGN KEY (");
  4855. for (i = 0;;) {
  4856. str.append(ut_get_name(trx, FALSE, foreign->foreign_col_names[i]));
  4857. if (++i < foreign->n_fields) {
  4858. str.append(", ");
  4859. } else {
  4860. break;
  4861. }
  4862. }
  4863. str.append(") REFERENCES ");
  4864. if (dict_tables_have_same_db(foreign->foreign_table_name_lookup,
  4865. foreign->referenced_table_name_lookup)) {
  4866. /* Do not print the database name of the referenced table */
  4867. str.append(ut_get_name(trx, TRUE,
  4868. dict_remove_db_name(
  4869. foreign->referenced_table_name)));
  4870. } else {
  4871. str.append(ut_get_name(trx, TRUE,
  4872. foreign->referenced_table_name));
  4873. }
  4874. str.append(" (");
  4875. for (i = 0;;) {
  4876. str.append(ut_get_name(trx, FALSE,
  4877. foreign->referenced_col_names[i]));
  4878. if (++i < foreign->n_fields) {
  4879. str.append(", ");
  4880. } else {
  4881. break;
  4882. }
  4883. }
  4884. str.append(")");
  4885. if (foreign->type & DICT_FOREIGN_ON_DELETE_CASCADE) {
  4886. str.append(" ON DELETE CASCADE");
  4887. }
  4888. if (foreign->type & DICT_FOREIGN_ON_DELETE_SET_NULL) {
  4889. str.append(" ON DELETE SET NULL");
  4890. }
  4891. if (foreign->type & DICT_FOREIGN_ON_DELETE_NO_ACTION) {
  4892. str.append(" ON DELETE NO ACTION");
  4893. }
  4894. if (foreign->type & DICT_FOREIGN_ON_UPDATE_CASCADE) {
  4895. str.append(" ON UPDATE CASCADE");
  4896. }
  4897. if (foreign->type & DICT_FOREIGN_ON_UPDATE_SET_NULL) {
  4898. str.append(" ON UPDATE SET NULL");
  4899. }
  4900. if (foreign->type & DICT_FOREIGN_ON_UPDATE_NO_ACTION) {
  4901. str.append(" ON UPDATE NO ACTION");
  4902. }
  4903. return str;
  4904. }
  4905. /**********************************************************************//**
  4906. Outputs info on foreign keys of a table. */
  4907. UNIV_INTERN
  4908. std::string
  4909. dict_print_info_on_foreign_keys(
  4910. /*============================*/
  4911. ibool create_table_format, /*!< in: if TRUE then print in
  4912. a format suitable to be inserted into
  4913. a CREATE TABLE, otherwise in the format
  4914. of SHOW TABLE STATUS */
  4915. trx_t* trx, /*!< in: transaction */
  4916. dict_table_t* table) /*!< in: table */
  4917. {
  4918. dict_foreign_t* foreign;
  4919. std::string str;
  4920. mutex_enter(&(dict_sys->mutex));
  4921. for (dict_foreign_set::iterator it = table->foreign_set.begin();
  4922. it != table->foreign_set.end();
  4923. ++it) {
  4924. foreign = *it;
  4925. if (create_table_format) {
  4926. str.append(
  4927. dict_print_info_on_foreign_key_in_create_format(
  4928. trx, foreign, TRUE));
  4929. } else {
  4930. ulint i;
  4931. str.append("; (");
  4932. for (i = 0; i < foreign->n_fields; i++) {
  4933. if (i) {
  4934. str.append(" ");
  4935. }
  4936. str.append(ut_get_name(trx, FALSE,
  4937. foreign->foreign_col_names[i]));
  4938. }
  4939. str.append(") REFER ");
  4940. str.append(ut_get_name(trx, TRUE,
  4941. foreign->referenced_table_name));
  4942. str.append(")");
  4943. for (i = 0; i < foreign->n_fields; i++) {
  4944. if (i) {
  4945. str.append(" ");
  4946. }
  4947. str.append(ut_get_name(
  4948. trx, FALSE,
  4949. foreign->referenced_col_names[i]));
  4950. }
  4951. str.append(")");
  4952. if (foreign->type == DICT_FOREIGN_ON_DELETE_CASCADE) {
  4953. str.append(" ON DELETE CASCADE");
  4954. }
  4955. if (foreign->type == DICT_FOREIGN_ON_DELETE_SET_NULL) {
  4956. str.append(" ON DELETE SET NULL");
  4957. }
  4958. if (foreign->type & DICT_FOREIGN_ON_DELETE_NO_ACTION) {
  4959. str.append(" ON DELETE NO ACTION");
  4960. }
  4961. if (foreign->type & DICT_FOREIGN_ON_UPDATE_CASCADE) {
  4962. str.append(" ON UPDATE CASCADE");
  4963. }
  4964. if (foreign->type & DICT_FOREIGN_ON_UPDATE_SET_NULL) {
  4965. str.append(" ON UPDATE SET NULL");
  4966. }
  4967. if (foreign->type & DICT_FOREIGN_ON_UPDATE_NO_ACTION) {
  4968. str.append(" ON UPDATE NO ACTION");
  4969. }
  4970. }
  4971. }
  4972. mutex_exit(&(dict_sys->mutex));
  4973. return str;
  4974. }
  4975. /********************************************************************//**
  4976. Displays the names of the index and the table. */
  4977. UNIV_INTERN
  4978. void
  4979. dict_index_name_print(
  4980. /*==================*/
  4981. FILE* file, /*!< in: output stream */
  4982. const trx_t* trx, /*!< in: transaction */
  4983. const dict_index_t* index) /*!< in: index to print */
  4984. {
  4985. fputs("index ", file);
  4986. ut_print_name(file, trx, FALSE, index->name);
  4987. fputs(" of table ", file);
  4988. ut_print_name(file, trx, TRUE, index->table_name);
  4989. }
  4990. /**********************************************************************//**
  4991. Find a table in dict_sys->table_LRU list with specified space id
  4992. @return table if found, NULL if not */
  4993. static
  4994. dict_table_t*
  4995. dict_find_table_by_space(
  4996. /*=====================*/
  4997. ulint space_id) /*!< in: space ID */
  4998. {
  4999. dict_table_t* table;
  5000. ulint num_item;
  5001. ulint count = 0;
  5002. ut_ad(space_id > 0);
  5003. if (dict_sys == NULL) {
  5004. /* This could happen when it's in redo processing. */
  5005. return(NULL);
  5006. }
  5007. table = UT_LIST_GET_FIRST(dict_sys->table_LRU);
  5008. num_item = UT_LIST_GET_LEN(dict_sys->table_LRU);
  5009. /* This function intentionally does not acquire mutex as it is used
  5010. by error handling code in deep call stack as last means to avoid
  5011. killing the server, so it worth to risk some consequencies for
  5012. the action. */
  5013. while (table && count < num_item) {
  5014. if (table->space == space_id) {
  5015. return(table);
  5016. }
  5017. table = UT_LIST_GET_NEXT(table_LRU, table);
  5018. count++;
  5019. }
  5020. return(NULL);
  5021. }
  5022. /**********************************************************************//**
  5023. Flags a table with specified space_id corrupted in the data dictionary
  5024. cache
  5025. @return TRUE if successful */
  5026. UNIV_INTERN
  5027. ibool
  5028. dict_set_corrupted_by_space(
  5029. /*========================*/
  5030. ulint space_id) /*!< in: space ID */
  5031. {
  5032. dict_table_t* table;
  5033. table = dict_find_table_by_space(space_id);
  5034. if (!table) {
  5035. return(FALSE);
  5036. }
  5037. /* mark the table->corrupted bit only, since the caller
  5038. could be too deep in the stack for SYS_INDEXES update */
  5039. table->corrupted = true;
  5040. table->file_unreadable = true;
  5041. return(TRUE);
  5042. }
  5043. /** Flags a table with specified space_id encrypted in the data dictionary
  5044. cache
  5045. @param[in] space_id Tablespace id */
  5046. UNIV_INTERN
  5047. void
  5048. dict_set_encrypted_by_space(ulint space_id)
  5049. {
  5050. dict_table_t* table;
  5051. table = dict_find_table_by_space(space_id);
  5052. if (table) {
  5053. table->file_unreadable = true;
  5054. }
  5055. }
  5056. /**********************************************************************//**
  5057. Flags an index corrupted both in the data dictionary cache
  5058. and in the SYS_INDEXES */
  5059. UNIV_INTERN
  5060. void
  5061. dict_set_corrupted(
  5062. /*===============*/
  5063. dict_index_t* index, /*!< in/out: index */
  5064. trx_t* trx, /*!< in/out: transaction */
  5065. const char* ctx) /*!< in: context */
  5066. {
  5067. mem_heap_t* heap;
  5068. mtr_t mtr;
  5069. dict_index_t* sys_index;
  5070. dtuple_t* tuple;
  5071. dfield_t* dfield;
  5072. byte* buf;
  5073. char* table_name;
  5074. const char* status;
  5075. btr_cur_t cursor;
  5076. bool locked = RW_X_LATCH == trx->dict_operation_lock_mode;
  5077. if (!locked) {
  5078. row_mysql_lock_data_dictionary(trx);
  5079. }
  5080. ut_ad(mutex_own(&dict_sys->mutex));
  5081. ut_ad(!dict_table_is_comp(dict_sys->sys_tables));
  5082. ut_ad(!dict_table_is_comp(dict_sys->sys_indexes));
  5083. #ifdef UNIV_SYNC_DEBUG
  5084. ut_ad(sync_thread_levels_empty_except_dict());
  5085. #endif
  5086. /* Mark the table as corrupted only if the clustered index
  5087. is corrupted */
  5088. if (dict_index_is_clust(index)) {
  5089. index->table->corrupted = TRUE;
  5090. }
  5091. if (index->type & DICT_CORRUPT) {
  5092. /* The index was already flagged corrupted. */
  5093. ut_ad(!dict_index_is_clust(index) || index->table->corrupted);
  5094. goto func_exit;
  5095. }
  5096. heap = mem_heap_create(sizeof(dtuple_t) + 2 * (sizeof(dfield_t)
  5097. + sizeof(que_fork_t) + sizeof(upd_node_t)
  5098. + sizeof(upd_t) + 12));
  5099. mtr_start(&mtr);
  5100. index->type |= DICT_CORRUPT;
  5101. sys_index = UT_LIST_GET_FIRST(dict_sys->sys_indexes->indexes);
  5102. /* Find the index row in SYS_INDEXES */
  5103. tuple = dtuple_create(heap, 2);
  5104. dfield = dtuple_get_nth_field(tuple, 0);
  5105. buf = static_cast<byte*>(mem_heap_alloc(heap, 8));
  5106. mach_write_to_8(buf, index->table->id);
  5107. dfield_set_data(dfield, buf, 8);
  5108. dfield = dtuple_get_nth_field(tuple, 1);
  5109. buf = static_cast<byte*>(mem_heap_alloc(heap, 8));
  5110. mach_write_to_8(buf, index->id);
  5111. dfield_set_data(dfield, buf, 8);
  5112. dict_index_copy_types(tuple, sys_index, 2);
  5113. btr_cur_search_to_nth_level(sys_index, 0, tuple, PAGE_CUR_LE,
  5114. BTR_MODIFY_LEAF,
  5115. &cursor, 0, __FILE__, __LINE__, &mtr);
  5116. if (cursor.low_match == dtuple_get_n_fields(tuple)) {
  5117. /* UPDATE SYS_INDEXES SET TYPE=index->type
  5118. WHERE TABLE_ID=index->table->id AND INDEX_ID=index->id */
  5119. ulint len;
  5120. byte* field = rec_get_nth_field_old(
  5121. btr_cur_get_rec(&cursor),
  5122. DICT_FLD__SYS_INDEXES__TYPE, &len);
  5123. if (len != 4) {
  5124. goto fail;
  5125. }
  5126. mlog_write_ulint(field, index->type, MLOG_4BYTES, &mtr);
  5127. status = "Flagged";
  5128. } else {
  5129. fail:
  5130. status = "Unable to flag";
  5131. }
  5132. mtr_commit(&mtr);
  5133. mem_heap_empty(heap);
  5134. table_name = static_cast<char*>(mem_heap_alloc(heap, FN_REFLEN + 1));
  5135. *innobase_convert_name(
  5136. table_name, FN_REFLEN,
  5137. index->table_name, strlen(index->table_name),
  5138. NULL, TRUE) = 0;
  5139. ib_logf(IB_LOG_LEVEL_ERROR, "%s corruption of %s in table %s in %s",
  5140. status, index->name, table_name, ctx);
  5141. mem_heap_free(heap);
  5142. func_exit:
  5143. if (!locked) {
  5144. row_mysql_unlock_data_dictionary(trx);
  5145. }
  5146. }
  5147. /**********************************************************************//**
  5148. Flags an index corrupted in the data dictionary cache only. This
  5149. is used mostly to mark a corrupted index when index's own dictionary
  5150. is corrupted, and we force to load such index for repair purpose */
  5151. UNIV_INTERN
  5152. void
  5153. dict_set_corrupted_index_cache_only(
  5154. /*================================*/
  5155. dict_index_t* index, /*!< in/out: index */
  5156. dict_table_t* table) /*!< in/out: table */
  5157. {
  5158. ut_ad(index != NULL);
  5159. ut_ad(mutex_own(&dict_sys->mutex));
  5160. ut_ad(!dict_table_is_comp(dict_sys->sys_tables));
  5161. ut_ad(!dict_table_is_comp(dict_sys->sys_indexes));
  5162. /* Mark the table as corrupted only if the clustered index
  5163. is corrupted */
  5164. if (dict_index_is_clust(index)) {
  5165. ut_ad((index->table != NULL) || (table != NULL)
  5166. || index->table == table);
  5167. table->corrupted = TRUE;
  5168. }
  5169. index->type |= DICT_CORRUPT;
  5170. }
  5171. #endif /* !UNIV_HOTBACKUP */
  5172. /**********************************************************************//**
  5173. Inits dict_ind_redundant and dict_ind_compact. */
  5174. UNIV_INTERN
  5175. void
  5176. dict_ind_init(void)
  5177. /*===============*/
  5178. {
  5179. dict_table_t* table;
  5180. /* create dummy table and index for REDUNDANT infimum and supremum */
  5181. table = dict_mem_table_create("SYS_DUMMY1", DICT_HDR_SPACE, 1, 0, 0);
  5182. dict_mem_table_add_col(table, NULL, NULL, DATA_CHAR,
  5183. DATA_ENGLISH | DATA_NOT_NULL, 8);
  5184. dict_ind_redundant = dict_mem_index_create("SYS_DUMMY1", "SYS_DUMMY1",
  5185. DICT_HDR_SPACE, 0, 1);
  5186. dict_index_add_col(dict_ind_redundant, table,
  5187. dict_table_get_nth_col(table, 0), 0);
  5188. dict_ind_redundant->table = table;
  5189. /* create dummy table and index for COMPACT infimum and supremum */
  5190. table = dict_mem_table_create("SYS_DUMMY2",
  5191. DICT_HDR_SPACE, 1,
  5192. DICT_TF_COMPACT, 0);
  5193. dict_mem_table_add_col(table, NULL, NULL, DATA_CHAR,
  5194. DATA_ENGLISH | DATA_NOT_NULL, 8);
  5195. dict_ind_compact = dict_mem_index_create("SYS_DUMMY2", "SYS_DUMMY2",
  5196. DICT_HDR_SPACE, 0, 1);
  5197. dict_index_add_col(dict_ind_compact, table,
  5198. dict_table_get_nth_col(table, 0), 0);
  5199. dict_ind_compact->table = table;
  5200. /* avoid ut_ad(index->cached) in dict_index_get_n_unique_in_tree */
  5201. dict_ind_redundant->cached = dict_ind_compact->cached = TRUE;
  5202. }
  5203. #ifndef UNIV_HOTBACKUP
  5204. /**********************************************************************//**
  5205. Frees dict_ind_redundant and dict_ind_compact. */
  5206. static
  5207. void
  5208. dict_ind_free(void)
  5209. /*===============*/
  5210. {
  5211. dict_table_t* table;
  5212. table = dict_ind_compact->table;
  5213. dict_mem_index_free(dict_ind_compact);
  5214. dict_ind_compact = NULL;
  5215. dict_mem_table_free(table);
  5216. table = dict_ind_redundant->table;
  5217. dict_mem_index_free(dict_ind_redundant);
  5218. dict_ind_redundant = NULL;
  5219. dict_mem_table_free(table);
  5220. }
  5221. /**********************************************************************//**
  5222. Get index by name
  5223. @return index, NULL if does not exist */
  5224. UNIV_INTERN
  5225. dict_index_t*
  5226. dict_table_get_index_on_name(
  5227. /*=========================*/
  5228. dict_table_t* table, /*!< in: table */
  5229. const char* name) /*!< in: name of the index to find */
  5230. {
  5231. dict_index_t* index;
  5232. index = dict_table_get_first_index(table);
  5233. while (index != NULL) {
  5234. if (innobase_strcasecmp(index->name, name) == 0) {
  5235. return(index);
  5236. }
  5237. index = dict_table_get_next_index(index);
  5238. }
  5239. return(NULL);
  5240. }
  5241. /**********************************************************************//**
  5242. Replace the index passed in with another equivalent index in the
  5243. foreign key lists of the table.
  5244. @return whether all replacements were found */
  5245. UNIV_INTERN
  5246. bool
  5247. dict_foreign_replace_index(
  5248. /*=======================*/
  5249. dict_table_t* table, /*!< in/out: table */
  5250. const char** col_names,
  5251. /*!< in: column names, or NULL
  5252. to use table->col_names */
  5253. const dict_index_t* index) /*!< in: index to be replaced */
  5254. {
  5255. bool found = true;
  5256. dict_foreign_t* foreign;
  5257. ut_ad(index->to_be_dropped);
  5258. ut_ad(index->table == table);
  5259. for (dict_foreign_set::iterator it = table->foreign_set.begin();
  5260. it != table->foreign_set.end();
  5261. ++it) {
  5262. foreign = *it;
  5263. if (foreign->foreign_index == index) {
  5264. ut_ad(foreign->foreign_table == index->table);
  5265. dict_index_t* new_index = dict_foreign_find_index(
  5266. foreign->foreign_table, col_names,
  5267. foreign->foreign_col_names,
  5268. foreign->n_fields, index,
  5269. /*check_charsets=*/TRUE, /*check_null=*/FALSE,
  5270. NULL, NULL, NULL);
  5271. if (new_index) {
  5272. ut_ad(new_index->table == index->table);
  5273. ut_ad(!new_index->to_be_dropped);
  5274. } else {
  5275. found = false;
  5276. }
  5277. foreign->foreign_index = new_index;
  5278. }
  5279. }
  5280. for (dict_foreign_set::iterator it = table->referenced_set.begin();
  5281. it != table->referenced_set.end();
  5282. ++it) {
  5283. foreign = *it;
  5284. if (foreign->referenced_index == index) {
  5285. ut_ad(foreign->referenced_table == index->table);
  5286. dict_index_t* new_index = dict_foreign_find_index(
  5287. foreign->referenced_table, NULL,
  5288. foreign->referenced_col_names,
  5289. foreign->n_fields, index,
  5290. /*check_charsets=*/TRUE, /*check_null=*/FALSE,
  5291. NULL, NULL, NULL);
  5292. /* There must exist an alternative index,
  5293. since this must have been checked earlier. */
  5294. if (new_index) {
  5295. ut_ad(new_index->table == index->table);
  5296. ut_ad(!new_index->to_be_dropped);
  5297. } else {
  5298. found = false;
  5299. }
  5300. foreign->referenced_index = new_index;
  5301. }
  5302. }
  5303. return(found);
  5304. }
  5305. /**********************************************************************//**
  5306. In case there is more than one index with the same name return the index
  5307. with the min(id).
  5308. @return index, NULL if does not exist */
  5309. UNIV_INTERN
  5310. dict_index_t*
  5311. dict_table_get_index_on_name_and_min_id(
  5312. /*=====================================*/
  5313. dict_table_t* table, /*!< in: table */
  5314. const char* name) /*!< in: name of the index to find */
  5315. {
  5316. dict_index_t* index;
  5317. dict_index_t* min_index; /* Index with matching name and min(id) */
  5318. min_index = NULL;
  5319. index = dict_table_get_first_index(table);
  5320. while (index != NULL) {
  5321. if (ut_strcmp(index->name, name) == 0) {
  5322. if (!min_index || index->id < min_index->id) {
  5323. min_index = index;
  5324. }
  5325. }
  5326. index = dict_table_get_next_index(index);
  5327. }
  5328. return(min_index);
  5329. }
  5330. #ifdef UNIV_DEBUG
  5331. /**********************************************************************//**
  5332. Check for duplicate index entries in a table [using the index name] */
  5333. UNIV_INTERN
  5334. void
  5335. dict_table_check_for_dup_indexes(
  5336. /*=============================*/
  5337. const dict_table_t* table, /*!< in: Check for dup indexes
  5338. in this table */
  5339. enum check_name check) /*!< in: whether and when to allow
  5340. temporary index names */
  5341. {
  5342. /* Check for duplicates, ignoring indexes that are marked
  5343. as to be dropped */
  5344. const dict_index_t* index1;
  5345. const dict_index_t* index2;
  5346. ut_ad(mutex_own(&dict_sys->mutex));
  5347. /* The primary index _must_ exist */
  5348. ut_a(UT_LIST_GET_LEN(table->indexes) > 0);
  5349. index1 = UT_LIST_GET_FIRST(table->indexes);
  5350. do {
  5351. if (*index1->name == TEMP_INDEX_PREFIX) {
  5352. ut_a(!dict_index_is_clust(index1));
  5353. switch (check) {
  5354. case CHECK_ALL_COMPLETE:
  5355. ut_error;
  5356. case CHECK_ABORTED_OK:
  5357. switch (dict_index_get_online_status(index1)) {
  5358. case ONLINE_INDEX_COMPLETE:
  5359. case ONLINE_INDEX_CREATION:
  5360. ut_error;
  5361. break;
  5362. case ONLINE_INDEX_ABORTED:
  5363. case ONLINE_INDEX_ABORTED_DROPPED:
  5364. break;
  5365. }
  5366. /* fall through */
  5367. case CHECK_PARTIAL_OK:
  5368. break;
  5369. }
  5370. }
  5371. for (index2 = UT_LIST_GET_NEXT(indexes, index1);
  5372. index2 != NULL;
  5373. index2 = UT_LIST_GET_NEXT(indexes, index2)) {
  5374. ut_ad(ut_strcmp(index1->name, index2->name));
  5375. }
  5376. index1 = UT_LIST_GET_NEXT(indexes, index1);
  5377. } while (index1);
  5378. }
  5379. #endif /* UNIV_DEBUG */
  5380. /** Auxiliary macro used inside dict_table_schema_check(). */
  5381. #define CREATE_TYPES_NAMES() \
  5382. dtype_sql_name((unsigned) req_schema->columns[i].mtype, \
  5383. (unsigned) req_schema->columns[i].prtype_mask, \
  5384. (unsigned) req_schema->columns[i].len, \
  5385. req_type, sizeof(req_type)); \
  5386. dtype_sql_name(table->cols[j].mtype, \
  5387. table->cols[j].prtype, \
  5388. table->cols[j].len, \
  5389. actual_type, sizeof(actual_type))
  5390. /*********************************************************************//**
  5391. Checks whether a table exists and whether it has the given structure.
  5392. The table must have the same number of columns with the same names and
  5393. types. The order of the columns does not matter.
  5394. The caller must own the dictionary mutex.
  5395. dict_table_schema_check() @{
  5396. @return DB_SUCCESS if the table exists and contains the necessary columns */
  5397. UNIV_INTERN
  5398. dberr_t
  5399. dict_table_schema_check(
  5400. /*====================*/
  5401. dict_table_schema_t* req_schema, /*!< in/out: required table
  5402. schema */
  5403. char* errstr, /*!< out: human readable error
  5404. message if != DB_SUCCESS is
  5405. returned */
  5406. size_t errstr_sz) /*!< in: errstr size */
  5407. {
  5408. char buf[MAX_FULL_NAME_LEN];
  5409. char req_type[64];
  5410. char actual_type[64];
  5411. dict_table_t* table;
  5412. ulint i;
  5413. ut_ad(mutex_own(&dict_sys->mutex));
  5414. table = dict_table_get_low(req_schema->table_name);
  5415. if (table == NULL) {
  5416. bool should_print=true;
  5417. /* no such table */
  5418. if (innobase_strcasecmp(req_schema->table_name, "mysql/innodb_table_stats") == 0) {
  5419. if (innodb_table_stats_not_found_reported == false) {
  5420. innodb_table_stats_not_found = true;
  5421. innodb_table_stats_not_found_reported = true;
  5422. } else {
  5423. should_print = false;
  5424. }
  5425. } else if (innobase_strcasecmp(req_schema->table_name, "mysql/innodb_index_stats") == 0 ) {
  5426. if (innodb_index_stats_not_found_reported == false) {
  5427. innodb_index_stats_not_found = true;
  5428. innodb_index_stats_not_found_reported = true;
  5429. } else {
  5430. should_print = false;
  5431. }
  5432. }
  5433. if (should_print) {
  5434. ut_snprintf(errstr, errstr_sz,
  5435. "Table %s not found.",
  5436. ut_format_name(req_schema->table_name,
  5437. TRUE, buf, sizeof(buf)));
  5438. return(DB_TABLE_NOT_FOUND);
  5439. } else {
  5440. return(DB_STATS_DO_NOT_EXIST);
  5441. }
  5442. }
  5443. if (!table->is_readable() &&
  5444. fil_space_get(table->space) == NULL) {
  5445. /* missing tablespace */
  5446. ut_snprintf(errstr, errstr_sz,
  5447. "Tablespace for table %s is missing.",
  5448. ut_format_name(req_schema->table_name,
  5449. TRUE, buf, sizeof(buf)));
  5450. return(DB_TABLE_NOT_FOUND);
  5451. }
  5452. if ((ulint) table->n_def - DATA_N_SYS_COLS != req_schema->n_cols) {
  5453. /* the table has a different number of columns than
  5454. required */
  5455. ut_snprintf(errstr, errstr_sz,
  5456. "%s has %d columns but should have %lu.",
  5457. ut_format_name(req_schema->table_name,
  5458. TRUE, buf, sizeof(buf)),
  5459. table->n_def - DATA_N_SYS_COLS,
  5460. req_schema->n_cols);
  5461. return(DB_ERROR);
  5462. }
  5463. /* For each column from req_schema->columns[] search
  5464. whether it is present in table->cols[].
  5465. The following algorithm is O(n_cols^2), but is optimized to
  5466. be O(n_cols) if the columns are in the same order in both arrays. */
  5467. for (i = 0; i < req_schema->n_cols; i++) {
  5468. ulint j;
  5469. /* check if i'th column is the same in both arrays */
  5470. if (innobase_strcasecmp(req_schema->columns[i].name,
  5471. dict_table_get_col_name(table, i)) == 0) {
  5472. /* we found the column in table->cols[] quickly */
  5473. j = i;
  5474. } else {
  5475. /* columns in both arrays are not in the same order,
  5476. do a full scan of the second array */
  5477. for (j = 0; j < table->n_def; j++) {
  5478. const char* name;
  5479. name = dict_table_get_col_name(table, j);
  5480. if (innobase_strcasecmp(name,
  5481. req_schema->columns[i].name) == 0) {
  5482. /* found the column on j'th
  5483. position */
  5484. break;
  5485. }
  5486. }
  5487. if (j == table->n_def) {
  5488. ut_snprintf(errstr, errstr_sz,
  5489. "required column %s "
  5490. "not found in table %s.",
  5491. req_schema->columns[i].name,
  5492. ut_format_name(
  5493. req_schema->table_name,
  5494. TRUE, buf, sizeof(buf)));
  5495. return(DB_ERROR);
  5496. }
  5497. }
  5498. /* we found a column with the same name on j'th position,
  5499. compare column types and flags */
  5500. /* check length for exact match */
  5501. if (req_schema->columns[i].len != table->cols[j].len) {
  5502. CREATE_TYPES_NAMES();
  5503. ut_snprintf(errstr, errstr_sz,
  5504. "Column %s in table %s is %s "
  5505. "but should be %s (length mismatch).",
  5506. req_schema->columns[i].name,
  5507. ut_format_name(req_schema->table_name,
  5508. TRUE, buf, sizeof(buf)),
  5509. actual_type, req_type);
  5510. return(DB_ERROR);
  5511. }
  5512. /* check mtype for exact match */
  5513. if (req_schema->columns[i].mtype != table->cols[j].mtype) {
  5514. CREATE_TYPES_NAMES();
  5515. ut_snprintf(errstr, errstr_sz,
  5516. "Column %s in table %s is %s "
  5517. "but should be %s (type mismatch).",
  5518. req_schema->columns[i].name,
  5519. ut_format_name(req_schema->table_name,
  5520. TRUE, buf, sizeof(buf)),
  5521. actual_type, req_type);
  5522. return(DB_ERROR);
  5523. }
  5524. /* check whether required prtype mask is set */
  5525. if (req_schema->columns[i].prtype_mask != 0
  5526. && (table->cols[j].prtype
  5527. & req_schema->columns[i].prtype_mask)
  5528. != req_schema->columns[i].prtype_mask) {
  5529. CREATE_TYPES_NAMES();
  5530. ut_snprintf(errstr, errstr_sz,
  5531. "Column %s in table %s is %s "
  5532. "but should be %s (flags mismatch).",
  5533. req_schema->columns[i].name,
  5534. ut_format_name(req_schema->table_name,
  5535. TRUE, buf, sizeof(buf)),
  5536. actual_type, req_type);
  5537. return(DB_ERROR);
  5538. }
  5539. }
  5540. if (req_schema->n_foreign != table->foreign_set.size()) {
  5541. ut_snprintf(
  5542. errstr, errstr_sz,
  5543. "Table %s has " ULINTPF " foreign key(s) pointing"
  5544. " to other tables, but it must have %lu.",
  5545. ut_format_name(req_schema->table_name,
  5546. TRUE, buf, sizeof(buf)),
  5547. static_cast<ulint>(table->foreign_set.size()),
  5548. req_schema->n_foreign);
  5549. return(DB_ERROR);
  5550. }
  5551. if (req_schema->n_referenced != table->referenced_set.size()) {
  5552. ut_snprintf(
  5553. errstr, errstr_sz,
  5554. "There are " ULINTPF " foreign key(s) pointing to %s, "
  5555. "but there must be %lu.",
  5556. static_cast<ulint>(table->referenced_set.size()),
  5557. ut_format_name(req_schema->table_name,
  5558. TRUE, buf, sizeof(buf)),
  5559. req_schema->n_referenced);
  5560. return(DB_ERROR);
  5561. }
  5562. return(DB_SUCCESS);
  5563. }
  5564. /* @} */
  5565. /*********************************************************************//**
  5566. Converts a database and table name from filesystem encoding
  5567. (e.g. d@i1b/a@q1b@1Kc, same format as used in dict_table_t::name) in two
  5568. strings in UTF8 encoding (e.g. dцb and aюbØc). The output buffers must be
  5569. at least MAX_DB_UTF8_LEN and MAX_TABLE_UTF8_LEN bytes. */
  5570. UNIV_INTERN
  5571. void
  5572. dict_fs2utf8(
  5573. /*=========*/
  5574. const char* db_and_table, /*!< in: database and table names,
  5575. e.g. d@i1b/a@q1b@1Kc */
  5576. char* db_utf8, /*!< out: database name, e.g. dцb */
  5577. size_t db_utf8_size, /*!< in: dbname_utf8 size */
  5578. char* table_utf8, /*!< out: table name, e.g. aюbØc */
  5579. size_t table_utf8_size)/*!< in: table_utf8 size */
  5580. {
  5581. char db[MAX_DATABASE_NAME_LEN + 1];
  5582. ulint db_len;
  5583. uint errors;
  5584. db_len = dict_get_db_name_len(db_and_table);
  5585. ut_a(db_len <= sizeof(db));
  5586. memcpy(db, db_and_table, db_len);
  5587. db[db_len] = '\0';
  5588. strconvert(
  5589. &my_charset_filename, db, db_len, system_charset_info,
  5590. db_utf8, static_cast<uint>(db_utf8_size), &errors);
  5591. /* convert each # to @0023 in table name and store the result in buf */
  5592. const char* table = dict_remove_db_name(db_and_table);
  5593. const char* table_p;
  5594. char buf[MAX_TABLE_NAME_LEN * 5 + 1];
  5595. char* buf_p;
  5596. for (table_p = table, buf_p = buf; table_p[0] != '\0'; table_p++) {
  5597. if (table_p[0] != '#') {
  5598. buf_p[0] = table_p[0];
  5599. buf_p++;
  5600. } else {
  5601. buf_p[0] = '@';
  5602. buf_p[1] = '0';
  5603. buf_p[2] = '0';
  5604. buf_p[3] = '2';
  5605. buf_p[4] = '3';
  5606. buf_p += 5;
  5607. }
  5608. ut_a((size_t) (buf_p - buf) < sizeof(buf));
  5609. }
  5610. buf_p[0] = '\0';
  5611. errors = 0;
  5612. strconvert(
  5613. &my_charset_filename, buf, buf_p - buf, system_charset_info,
  5614. table_utf8, static_cast<uint>(table_utf8_size),
  5615. &errors);
  5616. if (errors != 0) {
  5617. ut_snprintf(table_utf8, table_utf8_size, "%s%s",
  5618. srv_mysql50_table_name_prefix, table);
  5619. }
  5620. }
  5621. /**********************************************************************//**
  5622. Closes the data dictionary module. */
  5623. UNIV_INTERN
  5624. void
  5625. dict_close(void)
  5626. /*============*/
  5627. {
  5628. ulint i;
  5629. /* Free the hash elements. We don't remove them from the table
  5630. because we are going to destroy the table anyway. */
  5631. for (i = 0; i < hash_get_n_cells(dict_sys->table_hash); i++) {
  5632. dict_table_t* table;
  5633. table = static_cast<dict_table_t*>(
  5634. HASH_GET_FIRST(dict_sys->table_hash, i));
  5635. while (table) {
  5636. dict_table_t* prev_table = table;
  5637. table = static_cast<dict_table_t*>(
  5638. HASH_GET_NEXT(name_hash, prev_table));
  5639. #ifdef UNIV_DEBUG
  5640. ut_a(prev_table->magic_n == DICT_TABLE_MAGIC_N);
  5641. #endif
  5642. /* Acquire only because it's a pre-condition. */
  5643. mutex_enter(&dict_sys->mutex);
  5644. dict_table_remove_from_cache(prev_table);
  5645. mutex_exit(&dict_sys->mutex);
  5646. }
  5647. }
  5648. hash_table_free(dict_sys->table_hash);
  5649. /* The elements are the same instance as in dict_sys->table_hash,
  5650. therefore we don't delete the individual elements. */
  5651. hash_table_free(dict_sys->table_id_hash);
  5652. dict_ind_free();
  5653. mutex_free(&dict_sys->mutex);
  5654. rw_lock_free(&dict_operation_lock);
  5655. memset(&dict_operation_lock, 0x0, sizeof(dict_operation_lock));
  5656. if (!srv_read_only_mode) {
  5657. mutex_free(&dict_foreign_err_mutex);
  5658. }
  5659. delete dict_sys->autoinc_map;
  5660. mem_free(dict_sys);
  5661. dict_sys = NULL;
  5662. }
  5663. #ifdef UNIV_DEBUG
  5664. /**********************************************************************//**
  5665. Validate the dictionary table LRU list.
  5666. @return TRUE if valid */
  5667. static
  5668. ibool
  5669. dict_lru_validate(void)
  5670. /*===================*/
  5671. {
  5672. dict_table_t* table;
  5673. ut_ad(mutex_own(&dict_sys->mutex));
  5674. for (table = UT_LIST_GET_FIRST(dict_sys->table_LRU);
  5675. table != NULL;
  5676. table = UT_LIST_GET_NEXT(table_LRU, table)) {
  5677. ut_a(table->can_be_evicted);
  5678. }
  5679. for (table = UT_LIST_GET_FIRST(dict_sys->table_non_LRU);
  5680. table != NULL;
  5681. table = UT_LIST_GET_NEXT(table_LRU, table)) {
  5682. ut_a(!table->can_be_evicted);
  5683. }
  5684. return(TRUE);
  5685. }
  5686. /**********************************************************************//**
  5687. Check if a table exists in the dict table LRU list.
  5688. @return TRUE if table found in LRU list */
  5689. static
  5690. ibool
  5691. dict_lru_find_table(
  5692. /*================*/
  5693. const dict_table_t* find_table) /*!< in: table to find */
  5694. {
  5695. dict_table_t* table;
  5696. ut_ad(find_table != NULL);
  5697. ut_ad(mutex_own(&dict_sys->mutex));
  5698. for (table = UT_LIST_GET_FIRST(dict_sys->table_LRU);
  5699. table != NULL;
  5700. table = UT_LIST_GET_NEXT(table_LRU, table)) {
  5701. ut_a(table->can_be_evicted);
  5702. if (table == find_table) {
  5703. return(TRUE);
  5704. }
  5705. }
  5706. return(FALSE);
  5707. }
  5708. /**********************************************************************//**
  5709. Check if a table exists in the dict table non-LRU list.
  5710. @return TRUE if table found in non-LRU list */
  5711. static
  5712. ibool
  5713. dict_non_lru_find_table(
  5714. /*====================*/
  5715. const dict_table_t* find_table) /*!< in: table to find */
  5716. {
  5717. dict_table_t* table;
  5718. ut_ad(find_table != NULL);
  5719. ut_ad(mutex_own(&dict_sys->mutex));
  5720. for (table = UT_LIST_GET_FIRST(dict_sys->table_non_LRU);
  5721. table != NULL;
  5722. table = UT_LIST_GET_NEXT(table_LRU, table)) {
  5723. ut_a(!table->can_be_evicted);
  5724. if (table == find_table) {
  5725. return(TRUE);
  5726. }
  5727. }
  5728. return(FALSE);
  5729. }
  5730. #endif /* UNIV_DEBUG */
  5731. /*********************************************************************//**
  5732. Check an index to see whether its first fields are the columns in the array,
  5733. in the same order and is not marked for deletion and is not the same
  5734. as types_idx.
  5735. @return true if the index qualifies, otherwise false */
  5736. UNIV_INTERN
  5737. bool
  5738. dict_foreign_qualify_index(
  5739. /*=======================*/
  5740. const dict_table_t* table, /*!< in: table */
  5741. const char** col_names,
  5742. /*!< in: column names, or NULL
  5743. to use table->col_names */
  5744. const char** columns,/*!< in: array of column names */
  5745. ulint n_cols, /*!< in: number of columns */
  5746. const dict_index_t* index, /*!< in: index to check */
  5747. const dict_index_t* types_idx,
  5748. /*!< in: NULL or an index
  5749. whose types the column types
  5750. must match */
  5751. bool check_charsets,
  5752. /*!< in: whether to check
  5753. charsets. only has an effect
  5754. if types_idx != NULL */
  5755. ulint check_null,
  5756. /*!< in: nonzero if none of
  5757. the columns must be declared
  5758. NOT NULL */
  5759. ulint* error, /*!< out: error code */
  5760. ulint* err_col_no,
  5761. /*!< out: column number where error happened */
  5762. dict_index_t** err_index)
  5763. /*!< out: index where error happened */
  5764. {
  5765. if (dict_index_get_n_fields(index) < n_cols) {
  5766. return(false);
  5767. }
  5768. for (ulint i = 0; i < n_cols; i++) {
  5769. dict_field_t* field;
  5770. const char* col_name;
  5771. ulint col_no;
  5772. field = dict_index_get_nth_field(index, i);
  5773. col_no = dict_col_get_no(field->col);
  5774. if (field->prefix_len != 0) {
  5775. /* We do not accept column prefix
  5776. indexes here */
  5777. if (error && err_col_no && err_index) {
  5778. *error = DB_FOREIGN_KEY_IS_PREFIX_INDEX;
  5779. *err_col_no = i;
  5780. *err_index = (dict_index_t*)index;
  5781. }
  5782. return(false);
  5783. }
  5784. if (check_null
  5785. && (field->col->prtype & DATA_NOT_NULL)) {
  5786. if (error && err_col_no && err_index) {
  5787. *error = DB_FOREIGN_KEY_COL_NOT_NULL;
  5788. *err_col_no = i;
  5789. *err_index = (dict_index_t*)index;
  5790. }
  5791. return(false);
  5792. }
  5793. col_name = col_names
  5794. ? col_names[col_no]
  5795. : dict_table_get_col_name(table, col_no);
  5796. if (0 != innobase_strcasecmp(columns[i], col_name)) {
  5797. return(false);
  5798. }
  5799. if (types_idx && !cmp_cols_are_equal(
  5800. dict_index_get_nth_col(index, i),
  5801. dict_index_get_nth_col(types_idx, i),
  5802. check_charsets)) {
  5803. if (error && err_col_no && err_index) {
  5804. *error = DB_FOREIGN_KEY_COLS_NOT_EQUAL;
  5805. *err_col_no = i;
  5806. *err_index = (dict_index_t*)index;
  5807. }
  5808. return(false);
  5809. }
  5810. }
  5811. return(true);
  5812. }
  5813. /*********************************************************************//**
  5814. Update the state of compression failure padding heuristics. This is
  5815. called whenever a compression operation succeeds or fails.
  5816. The caller must be holding info->mutex */
  5817. static
  5818. void
  5819. dict_index_zip_pad_update(
  5820. /*======================*/
  5821. zip_pad_info_t* info, /*<! in/out: info to be updated */
  5822. ulint zip_threshold) /*<! in: zip threshold value */
  5823. {
  5824. ulint total;
  5825. ulint fail_pct;
  5826. ut_ad(info);
  5827. total = info->success + info->failure;
  5828. ut_ad(total > 0);
  5829. if(zip_threshold == 0) {
  5830. /* User has just disabled the padding. */
  5831. return;
  5832. }
  5833. if (total < ZIP_PAD_ROUND_LEN) {
  5834. /* We are in middle of a round. Do nothing. */
  5835. return;
  5836. }
  5837. /* We are at a 'round' boundary. Reset the values but first
  5838. calculate fail rate for our heuristic. */
  5839. fail_pct = (info->failure * 100) / total;
  5840. info->failure = 0;
  5841. info->success = 0;
  5842. if (fail_pct > zip_threshold) {
  5843. /* Compression failures are more then user defined
  5844. threshold. Increase the pad size to reduce chances of
  5845. compression failures. */
  5846. ut_ad(info->pad % ZIP_PAD_INCR == 0);
  5847. /* Only do increment if it won't increase padding
  5848. beyond max pad size. */
  5849. if (info->pad + ZIP_PAD_INCR
  5850. < (UNIV_PAGE_SIZE * zip_pad_max) / 100) {
  5851. #ifdef HAVE_ATOMIC_BUILTINS
  5852. /* Use atomics even though we have the mutex.
  5853. This is to ensure that we are able to read
  5854. info->pad atomically where atomics are
  5855. supported. */
  5856. os_atomic_increment_ulint(&info->pad, ZIP_PAD_INCR);
  5857. #else /* HAVE_ATOMIC_BUILTINS */
  5858. info->pad += ZIP_PAD_INCR;
  5859. #endif /* HAVE_ATOMIC_BUILTINS */
  5860. MONITOR_INC(MONITOR_PAD_INCREMENTS);
  5861. }
  5862. info->n_rounds = 0;
  5863. } else {
  5864. /* Failure rate was OK. Another successful round
  5865. completed. */
  5866. ++info->n_rounds;
  5867. /* If enough successful rounds are completed with
  5868. compression failure rate in control, decrease the
  5869. padding. */
  5870. if (info->n_rounds >= ZIP_PAD_SUCCESSFUL_ROUND_LIMIT
  5871. && info->pad > 0) {
  5872. ut_ad(info->pad % ZIP_PAD_INCR == 0);
  5873. #ifdef HAVE_ATOMIC_BUILTINS
  5874. /* Use atomics even though we have the mutex.
  5875. This is to ensure that we are able to read
  5876. info->pad atomically where atomics are
  5877. supported. */
  5878. os_atomic_decrement_ulint(&info->pad, ZIP_PAD_INCR);
  5879. #else /* HAVE_ATOMIC_BUILTINS */
  5880. info->pad -= ZIP_PAD_INCR;
  5881. #endif /* HAVE_ATOMIC_BUILTINS */
  5882. info->n_rounds = 0;
  5883. MONITOR_INC(MONITOR_PAD_DECREMENTS);
  5884. }
  5885. }
  5886. }
  5887. /*********************************************************************//**
  5888. This function should be called whenever a page is successfully
  5889. compressed. Updates the compression padding information. */
  5890. UNIV_INTERN
  5891. void
  5892. dict_index_zip_success(
  5893. /*===================*/
  5894. dict_index_t* index) /*!< in/out: index to be updated. */
  5895. {
  5896. ut_ad(index);
  5897. ulint zip_threshold = zip_failure_threshold_pct;
  5898. if (!zip_threshold) {
  5899. /* Disabled by user. */
  5900. return;
  5901. }
  5902. dict_index_zip_pad_lock(index);
  5903. ++index->zip_pad.success;
  5904. dict_index_zip_pad_update(&index->zip_pad, zip_threshold);
  5905. dict_index_zip_pad_unlock(index);
  5906. }
  5907. /*********************************************************************//**
  5908. This function should be called whenever a page compression attempt
  5909. fails. Updates the compression padding information. */
  5910. UNIV_INTERN
  5911. void
  5912. dict_index_zip_failure(
  5913. /*===================*/
  5914. dict_index_t* index) /*!< in/out: index to be updated. */
  5915. {
  5916. ut_ad(index);
  5917. ulint zip_threshold = zip_failure_threshold_pct;
  5918. if (!zip_threshold) {
  5919. /* Disabled by user. */
  5920. return;
  5921. }
  5922. dict_index_zip_pad_lock(index);
  5923. ++index->zip_pad.failure;
  5924. dict_index_zip_pad_update(&index->zip_pad, zip_threshold);
  5925. dict_index_zip_pad_unlock(index);
  5926. }
  5927. /*********************************************************************//**
  5928. Return the optimal page size, for which page will likely compress.
  5929. @return page size beyond which page might not compress */
  5930. UNIV_INTERN
  5931. ulint
  5932. dict_index_zip_pad_optimal_page_size(
  5933. /*=================================*/
  5934. dict_index_t* index) /*!< in: index for which page size
  5935. is requested */
  5936. {
  5937. ulint pad;
  5938. ulint min_sz;
  5939. ulint sz;
  5940. ut_ad(index);
  5941. if (!zip_failure_threshold_pct) {
  5942. /* Disabled by user. */
  5943. return(UNIV_PAGE_SIZE);
  5944. }
  5945. /* We use atomics to read index->zip_pad.pad. Here we use zero
  5946. as increment as are not changing the value of the 'pad'. On
  5947. platforms where atomics are not available we grab the mutex. */
  5948. #ifdef HAVE_ATOMIC_BUILTINS
  5949. pad = os_atomic_increment_ulint(&index->zip_pad.pad, 0);
  5950. #else /* HAVE_ATOMIC_BUILTINS */
  5951. dict_index_zip_pad_lock(index);
  5952. pad = index->zip_pad.pad;
  5953. dict_index_zip_pad_unlock(index);
  5954. #endif /* HAVE_ATOMIC_BUILTINS */
  5955. ut_ad(pad < UNIV_PAGE_SIZE);
  5956. sz = UNIV_PAGE_SIZE - pad;
  5957. /* Min size allowed by user. */
  5958. ut_ad(zip_pad_max < 100);
  5959. min_sz = (UNIV_PAGE_SIZE * (100 - zip_pad_max)) / 100;
  5960. return(ut_max(sz, min_sz));
  5961. }
  5962. /*************************************************************//**
  5963. Convert table flag to row format string.
  5964. @return row format name. */
  5965. UNIV_INTERN
  5966. const char*
  5967. dict_tf_to_row_format_string(
  5968. /*=========================*/
  5969. ulint table_flag) /*!< in: row format setting */
  5970. {
  5971. switch (dict_tf_get_rec_format(table_flag)) {
  5972. case REC_FORMAT_REDUNDANT:
  5973. return("ROW_TYPE_REDUNDANT");
  5974. case REC_FORMAT_COMPACT:
  5975. return("ROW_TYPE_COMPACT");
  5976. case REC_FORMAT_COMPRESSED:
  5977. return("ROW_TYPE_COMPRESSED");
  5978. case REC_FORMAT_DYNAMIC:
  5979. return("ROW_TYPE_DYNAMIC");
  5980. }
  5981. ut_error;
  5982. return(0);
  5983. }
  5984. #endif /* !UNIV_HOTBACKUP */
  5985. /** Calculate the used memory occupied by the data dictionary
  5986. table and index objects.
  5987. @return number of bytes occupied. */
  5988. UNIV_INTERN
  5989. ulint
  5990. dict_sys_get_size()
  5991. {
  5992. /* No mutex; this is a very crude approximation anyway */
  5993. ulint size = UT_LIST_GET_LEN(dict_sys->table_LRU)
  5994. + UT_LIST_GET_LEN(dict_sys->table_non_LRU);
  5995. size *= sizeof(dict_table_t)
  5996. + sizeof(dict_index_t) * 2
  5997. + (sizeof(dict_col_t) + sizeof(dict_field_t)) * 10
  5998. + sizeof(dict_field_t) * 5 /* total number of key fields */
  5999. + 200; /* arbitrary, covering names and overhead */
  6000. return size;
  6001. }