| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2014, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file ha_innopart.cc |
| 29 |
Code for native partitioning in InnoDB. |
| 30 |
|
| 31 |
Created Nov 22, 2013 Mattias Jonsson */ |
| 32 |
|
| 33 |
/* Include necessary SQL headers */ |
| 34 |
#include <debug_sync.h> |
| 35 |
#include <errno.h> |
| 36 |
#include <fcntl.h> |
| 37 |
#include <limits.h> |
| 38 |
#include <log.h> |
| 39 |
#include <my_check_opt.h> |
| 40 |
#include <mysqld.h> |
| 41 |
#include <sql_acl.h> |
| 42 |
#include <sql_backup_lock.h> |
| 43 |
#include <sql_class.h> |
| 44 |
#include <sql_show.h> |
| 45 |
#include <sql_table.h> |
| 46 |
#include <strfunc.h> |
| 47 |
#include <algorithm> |
| 48 |
#include <new> |
| 49 |
|
| 50 |
#include "dd/dd.h" |
| 51 |
#include "dd/dictionary.h" |
| 52 |
#include "dd/properties.h" |
| 53 |
#include "dd/types/partition.h" |
| 54 |
#include "dd/types/table.h" |
| 55 |
|
| 56 |
/* Include necessary InnoDB headers */ |
| 57 |
#include "btr0sea.h" |
| 58 |
#include "ddl0ddl.h" |
| 59 |
#include "dict0dd.h" |
| 60 |
#include "dict0dict.h" |
| 61 |
#include "dict0priv.h" |
| 62 |
#include "dict0stats.h" |
| 63 |
#include "fsp0sysspace.h" |
| 64 |
#include "ha_innodb.h" |
| 65 |
#include "ha_innopart.h" |
| 66 |
#include "handler0alter.h" //alter_stats_rebuild() |
| 67 |
#include "key.h" |
| 68 |
#include "lex_string.h" |
| 69 |
#include "lock0lock.h" |
| 70 |
#include "my_byteorder.h" |
| 71 |
#include "my_compiler.h" |
| 72 |
#include "my_dbug.h" |
| 73 |
#include "my_io.h" |
| 74 |
#include "my_macros.h" |
| 75 |
#include "mysql/plugin.h" |
| 76 |
#include "partition_info.h" |
| 77 |
#include "row0import.h" |
| 78 |
#include "row0ins.h" |
| 79 |
#include "row0mysql.h" |
| 80 |
#include "row0quiesce.h" |
| 81 |
#include "row0sel.h" |
| 82 |
#include "row0upd.h" |
| 83 |
#include "univ.i" |
| 84 |
#include "ut0ut.h" |
| 85 |
|
| 86 |
/* To be backwards compatible we also fold partition separator on windows. */ |
| 87 |
|
| 88 |
Ha_innopart_share::Ha_innopart_share(TABLE_SHARE *table_share) |
| 89 |
: Partition_share(), |
| 90 |
m_table_parts(), |
| 91 |
m_index_mapping(), |
| 92 |
m_tot_parts(), |
| 93 |
m_index_count(), |
| 94 |
m_ref_count(), |
| 95 |
m_table_share(table_share) {} |
| 96 |
|
| 97 |
Ha_innopart_share::~Ha_innopart_share() { |
| 98 |
ut_ad(m_ref_count == 0); |
| 99 |
if (m_table_parts != nullptr) { |
| 100 |
ut::free(m_table_parts); |
| 101 |
m_table_parts = nullptr; |
| 102 |
} |
| 103 |
if (m_index_mapping != nullptr) { |
| 104 |
ut::free(m_index_mapping); |
| 105 |
m_index_mapping = nullptr; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** Copy a cached MySQL row. |
| 110 |
If requested, also avoids overwriting non-read columns. |
| 111 |
@param[out] buf Row in MySQL format. |
| 112 |
@param[in] cached_row Which row to copy. */ |
| 113 |
inline void ha_innopart::copy_cached_row(uchar *buf, const uchar *cached_row) { |
| 114 |
if (m_prebuilt->keep_other_fields_on_keyread) { |
| 115 |
row_sel_copy_cached_fields_for_mysql(buf, cached_row, m_prebuilt); |
| 116 |
} else { |
| 117 |
memcpy(buf, cached_row, m_rec_length); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** Open one partition |
| 122 |
@param[in,out] client Data dictionary client |
| 123 |
@param[in] thd Thread THD |
| 124 |
@param[in] table MySQL table definition |
| 125 |
@param[in] dd_part dd::Partition |
| 126 |
@param[in] part_name Table name of this partition |
| 127 |
@param[out] part_dict_table InnoDB table for partition |
| 128 |
@retval false On success |
| 129 |
@retval true On failure */ |
| 130 |
bool Ha_innopart_share::open_one_table_part( |
| 131 |
dd::cache::Dictionary_client *client, THD *thd, const TABLE *table, |
| 132 |
const dd::Partition *dd_part, const char *part_name, |
| 133 |
dict_table_t **part_dict_table) { |
| 134 |
dict_table_t *part_table = nullptr; |
| 135 |
bool cached = false; |
| 136 |
|
| 137 |
dict_sys_mutex_enter(); |
| 138 |
part_table = dict_table_check_if_in_cache_low(part_name); |
| 139 |
if (part_table != nullptr) { |
| 140 |
cached = true; |
| 141 |
if (part_table->is_corrupted()) { |
| 142 |
if (part_table->get_ref_count() == 0) { |
| 143 |
dict_table_remove_from_cache(part_table); |
| 144 |
} |
| 145 |
part_table = nullptr; |
| 146 |
} else if (part_table->discard_after_ddl) { |
| 147 |
btr_drop_ahi_for_table(part_table); |
| 148 |
dict_table_remove_from_cache(part_table); |
| 149 |
part_table = nullptr; |
| 150 |
cached = false; |
| 151 |
} else { |
| 152 |
if (!dd_table_match(part_table, dd_part)) { |
| 153 |
dict_set_corrupted(part_table->first_index()); |
| 154 |
dict_table_remove_from_cache(part_table); |
| 155 |
part_table = nullptr; |
| 156 |
} else { |
| 157 |
part_table->acquire_with_lock(); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if (part_table != nullptr) { |
| 162 |
part_table->version = dd_get_version(&dd_part->table()); |
| 163 |
dict_table_ddl_release(part_table); |
| 164 |
} |
| 165 |
} |
| 166 |
dict_sys_mutex_exit(); |
| 167 |
|
| 168 |
if (!cached) { |
| 169 |
part_table = dd_open_table(client, table, part_name, dd_part, thd); |
| 170 |
} |
| 171 |
|
| 172 |
if (part_table != nullptr) { |
| 173 |
/* Set compression type like ha_innobase::open() does */ |
| 174 |
dberr_t err = |
| 175 |
dict_set_compression(part_table, table->s->compress.str, false); |
| 176 |
switch (err) { |
| 177 |
case DB_NOT_FOUND: |
| 178 |
case DB_UNSUPPORTED: |
| 179 |
/* We will do another check before the create |
| 180 |
table and push the error to the client there. */ |
| 181 |
break; |
| 182 |
|
| 183 |
case DB_IO_NO_PUNCH_HOLE_TABLESPACE: |
| 184 |
/* We did the check in the 'if' above. */ |
| 185 |
|
| 186 |
case DB_IO_NO_PUNCH_HOLE_FS: |
| 187 |
/* During open we can't check whether the FS supports |
| 188 |
punch hole or not, at least on Linux. */ |
| 189 |
break; |
| 190 |
|
| 191 |
default: |
| 192 |
ut_error; |
| 193 |
|
| 194 |
case DB_SUCCESS: |
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
*part_dict_table = part_table; |
| 200 |
return (part_table == nullptr); |
| 201 |
} |
| 202 |
|
| 203 |
/** Set up the virtual column template for partition table, and points |
| 204 |
all m_table_parts[]->vc_templ to it. |
| 205 |
@param[in] table MySQL TABLE object |
| 206 |
@param[in] ib_table InnoDB dict_table_t |
| 207 |
@param[in] name Table name (db/table_name) */ |
| 208 |
void Ha_innopart_share::set_v_templ(TABLE *table, dict_table_t *ib_table, |
| 209 |
const char *name) { |
| 210 |
ut_ad(dict_sys_mutex_own()); |
| 211 |
|
| 212 |
if (ib_table->n_v_cols > 0) { |
| 213 |
for (ulint i = 0; i < m_tot_parts; i++) { |
| 214 |
if (m_table_parts[i]->vc_templ == nullptr) { |
| 215 |
m_table_parts[i]->vc_templ = |
| 216 |
ut::new_withkey<dict_vcol_templ_t>(UT_NEW_THIS_FILE_PSI_KEY); |
| 217 |
m_table_parts[i]->vc_templ->vtempl = nullptr; |
| 218 |
} else if (m_table_parts[i]->get_ref_count() == 1) { |
| 219 |
/* Clean and refresh the template */ |
| 220 |
dict_free_vc_templ(m_table_parts[i]->vc_templ); |
| 221 |
m_table_parts[i]->vc_templ->vtempl = nullptr; |
| 222 |
} |
| 223 |
|
| 224 |
if (m_table_parts[i]->vc_templ->vtempl == nullptr) { |
| 225 |
innobase_build_v_templ(table, ib_table, m_table_parts[i]->vc_templ, |
| 226 |
nullptr, true, name); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/** Increment share and InnoDB tables reference counters. */ |
| 233 |
void Ha_innopart_share::increment_ref_counts() { |
| 234 |
#ifdef UNIV_DEBUG |
| 235 |
if (m_table_share->tmp_table == NO_TMP_TABLE) { |
| 236 |
mysql_mutex_assert_owner(&m_table_share->LOCK_ha_data); |
| 237 |
} |
| 238 |
#endif /* UNIV_DEBUG */ |
| 239 |
|
| 240 |
ut_ad(m_table_parts != nullptr); |
| 241 |
ut_ad(m_ref_count >= 1); |
| 242 |
ut_ad(m_tot_parts > 0); |
| 243 |
|
| 244 |
m_ref_count++; |
| 245 |
|
| 246 |
/* Increment dict_table_t reference count for all partitions */ |
| 247 |
dict_sys_mutex_enter(); |
| 248 |
for (uint i = 0; i < m_tot_parts; i++) { |
| 249 |
dict_table_t *table = m_table_parts[i]; |
| 250 |
table->acquire(); |
| 251 |
ut_ad(table->get_ref_count() >= m_ref_count); |
| 252 |
} |
| 253 |
dict_sys_mutex_exit(); |
| 254 |
} |
| 255 |
|
| 256 |
/** Open InnoDB tables for partitions and return them as array. |
| 257 |
@param[in,out] thd Thread context |
| 258 |
@param[in] table MySQL table definition |
| 259 |
@param[in] dd_table Global DD table object |
| 260 |
@param[in] part_info Partition info (partition names to use) |
| 261 |
@param[in] table_name Table name (db/table_name) |
| 262 |
@return Array on InnoDB tables on success else nullptr. */ |
| 263 |
dict_table_t **Ha_innopart_share::open_table_parts(THD *thd, const TABLE *table, |
| 264 |
const dd::Table *dd_table, |
| 265 |
partition_info *part_info, |
| 266 |
const char *table_name) { |
| 267 |
/* Code below might read from data-dictionary. In the process |
| 268 |
it will access SQL-layer's Table Cache and acquire lock associated |
| 269 |
with it. OTOH when closing tables we lock LOCK_ha_data while holding |
| 270 |
lock for Table Cache. So to avoid deadlocks we should not be holding |
| 271 |
LOCK_ha_data while trying to access data-dictionary. */ |
| 272 |
#ifdef UNIV_DEBUG |
| 273 |
if (table->s->tmp_table == NO_TMP_TABLE) { |
| 274 |
mysql_mutex_assert_not_owner(&table->s->LOCK_ha_data); |
| 275 |
} |
| 276 |
#endif /* UNIV_DEBUG */ |
| 277 |
|
| 278 |
uint tot_parts = part_info->get_tot_partitions(); |
| 279 |
size_t table_parts_size = sizeof(dict_table_t *) * tot_parts; |
| 280 |
dict_table_t **table_parts = static_cast<dict_table_t **>(ut::zalloc_withkey( |
| 281 |
ut::make_psi_memory_key(mem_key_partitioning), table_parts_size)); |
| 282 |
if (table_parts == nullptr) { |
| 283 |
return (nullptr); |
| 284 |
} |
| 285 |
|
| 286 |
dd::cache::Dictionary_client *client; |
| 287 |
client = dd::get_dd_client(thd); |
| 288 |
dd::cache::Dictionary_client::Auto_releaser releaser(client); |
| 289 |
uint i = 0; |
| 290 |
|
| 291 |
for (const auto dd_part : dd_table->leaf_partitions()) { |
| 292 |
std::string partition; |
| 293 |
/* Build the partition name. */ |
| 294 |
dict_name::build_partition(dd_part, partition); |
| 295 |
std::string part_table; |
| 296 |
/* Build the partitioned table name. */ |
| 297 |
dict_name::build_table("", table_name, partition, false, false, part_table); |
| 298 |
ut_ad(part_table.length() < FN_REFLEN); |
| 299 |
|
| 300 |
if (open_one_table_part(client, thd, table, dd_part, part_table.c_str(), |
| 301 |
&table_parts[i])) { |
| 302 |
ut_ad(table_parts[i] == nullptr); |
| 303 |
close_table_parts(table_parts, i); |
| 304 |
ut::free(table_parts); |
| 305 |
return (nullptr); |
| 306 |
} |
| 307 |
i++; |
| 308 |
} |
| 309 |
ut_ad(i == tot_parts); |
| 310 |
|
| 311 |
return (table_parts); |
| 312 |
} |
| 313 |
|
| 314 |
bool Ha_innopart_share::set_table_parts_and_indexes( |
| 315 |
partition_info *part_info, dict_table_t **table_parts) { |
| 316 |
uint ib_num_index; |
| 317 |
uint mysql_num_index; |
| 318 |
bool index_loaded = true; |
| 319 |
|
| 320 |
#ifdef UNIV_DEBUG |
| 321 |
if (m_table_share->tmp_table == NO_TMP_TABLE) { |
| 322 |
mysql_mutex_assert_owner(&m_table_share->LOCK_ha_data); |
| 323 |
} |
| 324 |
#endif /* UNIV_DEBUG */ |
| 325 |
|
| 326 |
m_ref_count++; |
| 327 |
|
| 328 |
/* Check if some other thread has managed to initialize share/open InnoDB |
| 329 |
tables for partitions concurrently, while LOCK_ha_data was free. |
| 330 |
In such a case table_parts array should point to same dict_table_t entries |
| 331 |
as one in share, so the array can be simply discarded. There is no need to |
| 332 |
increment reference counters for dict_table_t entries as this was already |
| 333 |
done during dd_open_table() call. */ |
| 334 |
if (m_table_parts != nullptr) { |
| 335 |
ut_ad(m_ref_count > 1); |
| 336 |
ut_ad(m_tot_parts == part_info->get_tot_partitions()); |
| 337 |
#ifdef UNIV_DEBUG |
| 338 |
for (uint i = 0; i < m_tot_parts; i++) { |
| 339 |
ut_ad(m_table_parts[i] == table_parts[i]); |
| 340 |
} |
| 341 |
#endif /* UNIV_DEBUG */ |
| 342 |
ut::free(table_parts); |
| 343 |
return (false); |
| 344 |
} |
| 345 |
|
| 346 |
ut_ad(m_ref_count == 1); |
| 347 |
|
| 348 |
m_tot_parts = part_info->get_tot_partitions(); |
| 349 |
m_table_parts = table_parts; |
| 350 |
|
| 351 |
/* Create the mapping of mysql index number to innodb indexes. */ |
| 352 |
|
| 353 |
ib_num_index = (uint)UT_LIST_GET_LEN(m_table_parts[0]->indexes); |
| 354 |
mysql_num_index = part_info->table->s->keys; |
| 355 |
|
| 356 |
/* If there exists inconsistency between MySQL and InnoDB dictionary |
| 357 |
(metadata) information, the number of index defined in MySQL |
| 358 |
could exceed that in InnoDB, do not build index translation |
| 359 |
table in such case. */ |
| 360 |
|
| 361 |
if (ib_num_index < mysql_num_index) { |
| 362 |
ut_d(ut_error); |
| 363 |
ut_o(goto err); |
| 364 |
} |
| 365 |
|
| 366 |
if (mysql_num_index != 0) { |
| 367 |
size_t alloc_size = |
| 368 |
mysql_num_index * m_tot_parts * sizeof(*m_index_mapping); |
| 369 |
m_index_mapping = static_cast<dict_index_t **>(ut::zalloc_withkey( |
| 370 |
ut::make_psi_memory_key(mem_key_partitioning), alloc_size)); |
| 371 |
if (m_index_mapping == nullptr) { |
| 372 |
/* Report an error if index_mapping continues to be |
| 373 |
NULL and mysql_num_index is a non-zero value. */ |
| 374 |
|
| 375 |
ib::error(ER_IB_MSG_582) << "Failed to allocate memory for" |
| 376 |
" index translation table. Number of" |
| 377 |
" Index:" |
| 378 |
<< mysql_num_index; |
| 379 |
goto err; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
/* For each index in the mysql key_info array, fetch its |
| 384 |
corresponding InnoDB index pointer into index_mapping |
| 385 |
array. */ |
| 386 |
|
| 387 |
for (ulint idx = 0; idx < mysql_num_index; idx++) { |
| 388 |
for (ulint part = 0; part < m_tot_parts; part++) { |
| 389 |
ulint count = part * mysql_num_index + idx; |
| 390 |
|
| 391 |
/* Fetch index pointers into index_mapping according |
| 392 |
to mysql index sequence. */ |
| 393 |
|
| 394 |
m_index_mapping[count] = dict_table_get_index_on_name( |
| 395 |
m_table_parts[part], part_info->table->key_info[idx].name); |
| 396 |
|
| 397 |
if (m_index_mapping[count] == nullptr) { |
| 398 |
ib::error(ER_IB_MSG_583) |
| 399 |
<< "Cannot find index `" << part_info->table->key_info[idx].name |
| 400 |
<< "` in InnoDB index dictionary" |
| 401 |
" partition `" |
| 402 |
<< get_partition_name(part) << "`."; |
| 403 |
index_loaded = false; |
| 404 |
break; |
| 405 |
} |
| 406 |
|
| 407 |
/* Double check fetched index has the same |
| 408 |
column info as those in mysql key_info. */ |
| 409 |
|
| 410 |
if (!innobase_match_index_columns(&part_info->table->key_info[idx], |
| 411 |
m_index_mapping[count])) { |
| 412 |
ib::error(ER_IB_MSG_584) |
| 413 |
<< "Found index `" << part_info->table->key_info[idx].name |
| 414 |
<< "` whose column info does not match" |
| 415 |
" that of MySQL."; |
| 416 |
index_loaded = false; |
| 417 |
break; |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
if (!index_loaded && m_index_mapping != nullptr) { |
| 422 |
ut::free(m_index_mapping); |
| 423 |
m_index_mapping = nullptr; |
| 424 |
} |
| 425 |
|
| 426 |
/* Successfully built the translation table. */ |
| 427 |
m_index_count = mysql_num_index; |
| 428 |
|
| 429 |
return (false); |
| 430 |
err: |
| 431 |
close_table_parts(); |
| 432 |
|
| 433 |
return (true); |
| 434 |
} |
| 435 |
|
| 436 |
/** Close InnoDB tables for partitions. |
| 437 |
@param[in] table_parts Array of InnoDB tables for partitions. |
| 438 |
@param[in] tot_parts Number of partitions. */ |
| 439 |
void Ha_innopart_share::close_table_parts(dict_table_t **table_parts, |
| 440 |
uint tot_parts) { |
| 441 |
for (uint i = 0; i < tot_parts; i++) { |
| 442 |
if (table_parts[i] != nullptr) { |
| 443 |
dd_table_close(table_parts[i], nullptr, nullptr, false); |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
/** Close the table partitions. |
| 449 |
If all instances are closed, also release the resources. */ |
| 450 |
void Ha_innopart_share::close_table_parts(void) { |
| 451 |
#ifdef UNIV_DEBUG |
| 452 |
if (m_table_share->tmp_table == NO_TMP_TABLE) { |
| 453 |
mysql_mutex_assert_owner(&m_table_share->LOCK_ha_data); |
| 454 |
} |
| 455 |
#endif /* UNIV_DEBUG */ |
| 456 |
m_ref_count--; |
| 457 |
if (m_ref_count != 0) { |
| 458 |
/* Decrement dict_table_t reference count for all partitions */ |
| 459 |
for (uint i = 0; i < m_tot_parts; i++) { |
| 460 |
dict_table_t *table = m_table_parts[i]; |
| 461 |
ut_d(uint64_t ref_count = table->get_ref_count()); |
| 462 |
table->release(); |
| 463 |
/* ref_count is got before release, so to minus 1 */ |
| 464 |
ut_ad(ref_count >= m_ref_count + 1); |
| 465 |
} |
| 466 |
|
| 467 |
return; |
| 468 |
} |
| 469 |
|
| 470 |
/* Last instance closed, close all table partitions and |
| 471 |
free the memory. */ |
| 472 |
|
| 473 |
if (m_table_parts != nullptr) { |
| 474 |
close_table_parts(m_table_parts, m_tot_parts); |
| 475 |
ut::free(m_table_parts); |
| 476 |
m_table_parts = nullptr; |
| 477 |
} |
| 478 |
|
| 479 |
if (m_index_mapping != nullptr) { |
| 480 |
ut::free(m_index_mapping); |
| 481 |
m_index_mapping = nullptr; |
| 482 |
} |
| 483 |
|
| 484 |
m_tot_parts = 0; |
| 485 |
m_index_count = 0; |
| 486 |
|
| 487 |
/* All table partitions have been closed, autoinc initialization |
| 488 |
should be done again. */ |
| 489 |
auto_inc_initialized = false; |
| 490 |
} |
| 491 |
|
| 492 |
/** Return innodb index for given partition and key number. |
| 493 |
@param[in] part_id Partition number. |
| 494 |
@param[in] keynr Key number. |
| 495 |
@return InnoDB index. */ |
| 496 |
inline dict_index_t *Ha_innopart_share::get_index(uint part_id, uint keynr) { |
| 497 |
if (part_id >= m_tot_parts) { |
| 498 |
/* purecov: begin inspected */ |
| 499 |
ut_d(ut_error); |
| 500 |
ut_o(return (nullptr)); |
| 501 |
/* purecov: end */ |
| 502 |
} |
| 503 |
|
| 504 |
ut_ad(keynr < m_index_count || keynr == MAX_KEY); |
| 505 |
if (m_index_mapping == nullptr || keynr >= m_index_count) { |
| 506 |
if (keynr == MAX_KEY) { |
| 507 |
return (get_table_part(part_id)->first_index()); |
| 508 |
} |
| 509 |
return (nullptr); |
| 510 |
} |
| 511 |
return (m_index_mapping[m_index_count * part_id + keynr]); |
| 512 |
} |
| 513 |
|
| 514 |
/** Get MySQL key number corresponding to InnoDB index. |
| 515 |
Calculates the key number used inside MySQL for an Innobase index. We will |
| 516 |
first check the "index translation table" for a match of the index to get |
| 517 |
the index number. If there does not exist an "index translation table", |
| 518 |
or not able to find the index in the translation table, then we will fall back |
| 519 |
to the traditional way of looping through dict_index_t list to find a |
| 520 |
match. In this case, we have to take into account if we generated a |
| 521 |
default clustered index for the table |
| 522 |
@param[in] part_id Partition the index belongs to. |
| 523 |
@param[in] index Index to return MySQL key number for. |
| 524 |
@return the key number used inside MySQL or UINT_MAX if key is not found. */ |
| 525 |
inline uint Ha_innopart_share::get_mysql_key(uint part_id, |
| 526 |
const dict_index_t *index) { |
| 527 |
ut_ad(index != nullptr); |
| 528 |
ut_ad(m_index_mapping != nullptr); |
| 529 |
ut_ad(m_tot_parts); |
| 530 |
|
| 531 |
if (index != nullptr && m_index_mapping != nullptr) { |
| 532 |
uint start; |
| 533 |
uint end; |
| 534 |
|
| 535 |
if (part_id < m_tot_parts) { |
| 536 |
start = part_id * m_index_count; |
| 537 |
end = start + m_index_count; |
| 538 |
} else { |
| 539 |
start = 0; |
| 540 |
end = m_tot_parts * m_index_count; |
| 541 |
} |
| 542 |
for (uint i = start; i < end; i++) { |
| 543 |
if (m_index_mapping[i] == index) { |
| 544 |
return (i % m_index_count); |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
/* Print an error message if we cannot find the index |
| 549 |
in the "index translation table". */ |
| 550 |
|
| 551 |
if (index->is_committed()) { |
| 552 |
ib::error(ER_IB_MSG_585) << "Cannot find index " << index->name |
| 553 |
<< " in InnoDB index translation table."; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
return (UINT_MAX); |
| 558 |
} |
| 559 |
|
| 560 |
/** Get explicit specified tablespace for one (sub)partition, checking |
| 561 |
from lowest level |
| 562 |
@param[in] tablespace table-level tablespace if specified |
| 563 |
@param[in] part Partition to check |
| 564 |
@param[in] sub_part Sub-partition to check, if no, just NULL |
| 565 |
@return Tablespace name, if nullptr or [0] = '\0' then nothing specified */ |
| 566 |
const char *partition_get_tablespace(const char *tablespace, |
| 567 |
const partition_element *part, |
| 568 |
const partition_element *sub_part) { |
| 569 |
if (sub_part != nullptr) { |
| 570 |
if (sub_part->tablespace_name != nullptr && |
| 571 |
sub_part->tablespace_name[0] != '\0') { |
| 572 |
return (sub_part->tablespace_name); |
| 573 |
} |
| 574 |
/* Once DATA DIRECTORY specified, it implies |
| 575 |
non-default tablespace, same as below */ |
| 576 |
if (sub_part->data_file_name != nullptr && |
| 577 |
sub_part->data_file_name[0] != '\0') { |
| 578 |
return (nullptr); |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
ut_ad(part != nullptr); |
| 583 |
if (part->tablespace_name != nullptr && part->tablespace_name[0] != '\0') { |
| 584 |
return (part->tablespace_name); |
| 585 |
} |
| 586 |
|
| 587 |
if (part->data_file_name != nullptr && part->data_file_name[0] != '\0') { |
| 588 |
return (nullptr); |
| 589 |
} |
| 590 |
|
| 591 |
return (tablespace); |
| 592 |
} |
| 593 |
|
| 594 |
/** Construct ha_innopart handler. |
| 595 |
@param[in] hton Handlerton. |
| 596 |
@param[in] table_arg MySQL Table. */ |
| 597 |
ha_innopart::ha_innopart(handlerton *hton, TABLE_SHARE *table_arg) |
| 598 |
: ha_innobase(hton, table_arg), |
| 599 |
Partition_helper(this), |
| 600 |
m_parts(), |
| 601 |
m_bitset(), |
| 602 |
m_sql_stat_start_parts(), |
| 603 |
m_pcur(), |
| 604 |
m_clust_pcur(), |
| 605 |
m_new_partitions() { |
| 606 |
m_int_table_flags &= ~(HA_INNOPART_DISABLED_TABLE_FLAGS); |
| 607 |
|
| 608 |
/* INNOBASE_SHARE is not used in ha_innopart. |
| 609 |
This also flags for ha_innobase that it is a partitioned table. |
| 610 |
And make it impossible to use legacy share functionality. */ |
| 611 |
|
| 612 |
m_share = nullptr; |
| 613 |
} |
| 614 |
|
| 615 |
/** Internally called for initializing auto increment value. |
| 616 |
Only called from ha_innobase::discard_or_import_table_space() |
| 617 |
and should not do anything, since it is ha_innopart will initialize |
| 618 |
it on first usage. */ |
| 619 |
int ha_innopart::innobase_initialize_autoinc() { |
| 620 |
ut_d(ut_error); |
| 621 |
ut_o(return (0)); |
| 622 |
} |
| 623 |
|
| 624 |
inline int ha_innopart::initialize_auto_increment(bool) { |
| 625 |
int error = 0; |
| 626 |
ulonglong auto_inc = 0; |
| 627 |
const Field *field = table->found_next_number_field; |
| 628 |
|
| 629 |
#ifdef UNIV_DEBUG |
| 630 |
if (table_share->tmp_table == NO_TMP_TABLE) { |
| 631 |
mysql_mutex_assert_owner(m_part_share->auto_inc_mutex); |
| 632 |
} |
| 633 |
#endif |
| 634 |
|
| 635 |
/* Since a table can already be "open" in InnoDB's internal |
| 636 |
data dictionary, we only init the autoinc counter once, the |
| 637 |
first time the table is loaded. We can safely reuse the |
| 638 |
autoinc value from a previous MySQL open. */ |
| 639 |
|
| 640 |
if (m_part_share->auto_inc_initialized) { |
| 641 |
/* Already initialized, nothing to do. */ |
| 642 |
return (0); |
| 643 |
} |
| 644 |
|
| 645 |
if (field == nullptr) { |
| 646 |
ib::info(ER_IB_MSG_586) << "Unable to determine the AUTOINC column name"; |
| 647 |
} |
| 648 |
|
| 649 |
if (srv_force_recovery >= SRV_FORCE_NO_IBUF_MERGE) { |
| 650 |
/* If the recovery level is set so high that writes |
| 651 |
are disabled we force the AUTOINC counter to 0 |
| 652 |
value effectively disabling writes to the table. |
| 653 |
Secondly, we avoid reading the table in case the read |
| 654 |
results in failure due to a corrupted table/index. |
| 655 |
|
| 656 |
We will not return an error to the client, so that the |
| 657 |
tables can be dumped with minimal hassle. If an error |
| 658 |
were returned in this case, the first attempt to read |
| 659 |
the table would fail and subsequent SELECTs would succeed. */ |
| 660 |
|
| 661 |
} else if (field == nullptr) { |
| 662 |
/* This is a far more serious error, best to avoid |
| 663 |
opening the table and return failure. */ |
| 664 |
|
| 665 |
my_error(ER_AUTOINC_READ_FAILED, MYF(0)); |
| 666 |
error = HA_ERR_AUTOINC_READ_FAILED; |
| 667 |
} else { |
| 668 |
dict_index_t *index; |
| 669 |
const char *col_name; |
| 670 |
uint64_t read_auto_inc; |
| 671 |
uint64_t persisted_auto_inc; |
| 672 |
uint64_t max_auto_inc = 0; |
| 673 |
ulint err; |
| 674 |
dict_table_t *ib_table; |
| 675 |
ulonglong col_max_value; |
| 676 |
|
| 677 |
col_max_value = field->get_max_int_value(); |
| 678 |
|
| 679 |
update_thd(ha_thd()); |
| 680 |
|
| 681 |
col_name = field->field_name; |
| 682 |
for (uint part = 0; part < m_tot_parts; part++) { |
| 683 |
ib_table = m_part_share->get_table_part(part); |
| 684 |
|
| 685 |
dict_table_autoinc_set_col_pos(ib_table, field->field_index()); |
| 686 |
|
| 687 |
dict_table_autoinc_lock(ib_table); |
| 688 |
read_auto_inc = dict_table_autoinc_read(ib_table); |
| 689 |
|
| 690 |
persisted_auto_inc = ib_table->autoinc_persisted; |
| 691 |
|
| 692 |
/* During startup, we may set both these two autoinc |
| 693 |
to same value after recovery of the counter. In this |
| 694 |
case, it's the first time we initialize the counter |
| 695 |
here, and we have to calculate the next counter. |
| 696 |
Otherwise, if they are not equal, we can use it |
| 697 |
directly. */ |
| 698 |
if (read_auto_inc != 0 && read_auto_inc != persisted_auto_inc) { |
| 699 |
/* Sometimes, such as after UPDATE, |
| 700 |
we may have the persisted counter bigger |
| 701 |
than the in-memory one, because UPDATE in |
| 702 |
partition tables still doesn't modify the |
| 703 |
in-memory counter while persisted one could |
| 704 |
be updated if it's updated to larger value. */ |
| 705 |
max_auto_inc = |
| 706 |
std::max(max_auto_inc, std::max(read_auto_inc, persisted_auto_inc)); |
| 707 |
dict_table_autoinc_unlock(ib_table); |
| 708 |
continue; |
| 709 |
} |
| 710 |
|
| 711 |
if (persisted_auto_inc == 0) { |
| 712 |
/* Execute SELECT MAX(col_name) FROM TABLE; */ |
| 713 |
index = m_part_share->get_index(part, table->s->next_number_index); |
| 714 |
err = row_search_max_autoinc(index, col_name, &read_auto_inc); |
| 715 |
} else { |
| 716 |
/* We have the persisted AUTOINC counter, |
| 717 |
have to calculate the next one. */ |
| 718 |
ut_ad(read_auto_inc == persisted_auto_inc); |
| 719 |
err = DB_SUCCESS; |
| 720 |
} |
| 721 |
|
| 722 |
switch (err) { |
| 723 |
case DB_SUCCESS: { |
| 724 |
/* At the this stage we do not know the |
| 725 |
increment nor the offset, |
| 726 |
so use a default increment of 1. */ |
| 727 |
|
| 728 |
auto_inc = |
| 729 |
innobase_next_autoinc(read_auto_inc, 1, 1, 0, col_max_value); |
| 730 |
max_auto_inc = std::max(max_auto_inc, uint64_t(auto_inc)); |
| 731 |
dict_table_autoinc_initialize(ib_table, auto_inc); |
| 732 |
break; |
| 733 |
} |
| 734 |
case DB_RECORD_NOT_FOUND: |
| 735 |
ib::error(ER_IB_MSG_587) |
| 736 |
<< "MySQL and InnoDB data" |
| 737 |
" dictionaries are out of sync. Unable" |
| 738 |
" to find the AUTOINC column " |
| 739 |
<< col_name << " in the InnoDB table " << ib_table->name |
| 740 |
<< ". We set the" |
| 741 |
" next AUTOINC column value to 0, in" |
| 742 |
" effect disabling the AUTOINC next" |
| 743 |
" value generation."; |
| 744 |
|
| 745 |
ib::info(ER_IB_MSG_588) << "You can either set the next" |
| 746 |
" AUTOINC value explicitly using ALTER" |
| 747 |
" TABLE or fix the data dictionary by" |
| 748 |
" recreating the table."; |
| 749 |
|
| 750 |
/* We want the open to succeed, so that the |
| 751 |
user can take corrective action. ie. reads |
| 752 |
should succeed but updates should fail. */ |
| 753 |
|
| 754 |
/* This will disable the AUTOINC generation. */ |
| 755 |
auto_inc = 0; |
| 756 |
goto done; |
| 757 |
default: |
| 758 |
/* row_search_max_autoinc() should only return |
| 759 |
one of DB_SUCCESS or DB_RECORD_NOT_FOUND. */ |
| 760 |
|
| 761 |
ut_error; |
| 762 |
} |
| 763 |
dict_table_autoinc_unlock(ib_table); |
| 764 |
} |
| 765 |
auto_inc = max_auto_inc; |
| 766 |
} |
| 767 |
|
| 768 |
done: |
| 769 |
m_part_share->next_auto_inc_val = auto_inc; |
| 770 |
m_part_share->auto_inc_initialized = true; |
| 771 |
return (error); |
| 772 |
} |
| 773 |
|
| 774 |
/** Open an InnoDB table. |
| 775 |
@param[in] name table name |
| 776 |
@param[in] mode access mode |
| 777 |
@param[in] test_if_locked test if the file to be opened is locked |
| 778 |
@param[in] table_def dd::Table describing table to be opened |
| 779 |
@retval 1 if error |
| 780 |
@retval 0 if success */ |
| 781 |
int ha_innopart::open(const char *name, int, uint, const dd::Table *table_def) { |
| 782 |
dict_table_t *ib_table; |
| 783 |
char norm_name[FN_REFLEN]; |
| 784 |
THD *thd; |
| 785 |
|
| 786 |
DBUG_TRACE; |
| 787 |
assert(table_share == table->s); |
| 788 |
|
| 789 |
if (m_part_info == nullptr) { |
| 790 |
/* Must be during ::clone()! */ |
| 791 |
ut_ad(table->part_info != nullptr); |
| 792 |
m_part_info = table->part_info; |
| 793 |
} |
| 794 |
thd = ha_thd(); |
| 795 |
|
| 796 |
if (!normalize_table_name(norm_name, name)) { |
| 797 |
/* purecov: begin inspected */ |
| 798 |
ut_d(ut_error); |
| 799 |
ut_o(return (HA_ERR_TOO_LONG_PATH)); |
| 800 |
/* purecov: end */ |
| 801 |
} |
| 802 |
|
| 803 |
m_user_thd = nullptr; |
| 804 |
|
| 805 |
/* Get the Ha_innopart_share from the TABLE_SHARE. */ |
| 806 |
lock_shared_ha_data(); |
| 807 |
|
| 808 |
m_part_share = static_cast<Ha_innopart_share *>(get_ha_share_ptr()); |
| 809 |
if (m_part_share == nullptr) { |
| 810 |
m_part_share = new (std::nothrow) Ha_innopart_share(table_share); |
| 811 |
if (m_part_share == nullptr) { |
| 812 |
share_error: |
| 813 |
unlock_shared_ha_data(); |
| 814 |
return HA_ERR_INTERNAL_ERROR; |
| 815 |
} |
| 816 |
set_ha_share_ptr(static_cast<Handler_share *>(m_part_share)); |
| 817 |
} |
| 818 |
|
| 819 |
if (m_part_share->has_table_parts()) { |
| 820 |
/* If share already has InnoDB tables open we just need to increment |
| 821 |
reference counters. */ |
| 822 |
m_part_share->increment_ref_counts(); |
| 823 |
} else { |
| 824 |
/* We need to open InnoDB tables and prepare index information. |
| 825 |
Since the former involves access to the data-dictionary we need |
| 826 |
to release TABLE_SHARE::LOCK_ha_data temporarily. */ |
| 827 |
unlock_shared_ha_data(); |
| 828 |
|
| 829 |
dict_table_t **table_parts = Ha_innopart_share::open_table_parts( |
| 830 |
thd, table, table_def, m_part_info, norm_name); |
| 831 |
|
| 832 |
if (table_parts == nullptr) { |
| 833 |
ib::warn(ER_IB_MSG_557) |
| 834 |
<< "Cannot open table " << norm_name << TROUBLESHOOTING_MSG; |
| 835 |
set_my_errno(ENOENT); |
| 836 |
|
| 837 |
return HA_ERR_NO_SUCH_TABLE; |
| 838 |
} |
| 839 |
|
| 840 |
/* Now acquire TABLE_SHARE::LOCK_ha_data again and assign table |
| 841 |
and index information. set_table_parts_and_indexes() will check |
| 842 |
if some other thread already has managed to do this concurrently, |
| 843 |
while lock was released. */ |
| 844 |
lock_shared_ha_data(); |
| 845 |
|
| 846 |
if (m_part_share->set_table_parts_and_indexes(m_part_info, table_parts)) { |
| 847 |
goto share_error; |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
if (m_part_share->populate_partition_name_hash(m_part_info)) { |
| 852 |
goto share_error; |
| 853 |
} |
| 854 |
|
| 855 |
if (m_part_share->auto_inc_mutex == nullptr && |
| 856 |
table->found_next_number_field != nullptr) { |
| 857 |
if (m_part_share->init_auto_inc_mutex(table_share)) { |
| 858 |
goto share_error; |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
unlock_shared_ha_data(); |
| 863 |
|
| 864 |
/* Will be allocated if it is needed in ::update_row(). */ |
| 865 |
m_upd_buf = nullptr; |
| 866 |
m_upd_buf_size = 0; |
| 867 |
|
| 868 |
m_prebuilt = nullptr; |
| 869 |
m_pcur_parts = nullptr; |
| 870 |
m_clust_pcur_parts = nullptr; |
| 871 |
m_pcur_map = nullptr; |
| 872 |
|
| 873 |
if (open_partitioning(m_part_share)) { |
| 874 |
close(); |
| 875 |
return HA_ERR_INITIALIZATION; |
| 876 |
} |
| 877 |
|
| 878 |
/* Currently we track statistics for all partitions, but for |
| 879 |
the secondary indexes we only use the biggest partition. */ |
| 880 |
|
| 881 |
for (uint part_id = 0; part_id < m_tot_parts; part_id++) { |
| 882 |
innobase_copy_frm_flags_from_table_share( |
| 883 |
m_part_share->get_table_part(part_id), table->s); |
| 884 |
dict_stats_init(m_part_share->get_table_part(part_id)); |
| 885 |
} |
| 886 |
|
| 887 |
MONITOR_INC(MONITOR_TABLE_OPEN); |
| 888 |
|
| 889 |
/* TODO: refactor this in ha_innobase so it can increase code reuse. */ |
| 890 |
|
| 891 |
for (uint part_id = 0; part_id < m_tot_parts; part_id++) { |
| 892 |
bool no_tablespace; |
| 893 |
ib_table = m_part_share->get_table_part(part_id); |
| 894 |
if (dict_table_is_discarded(ib_table)) { |
| 895 |
/* If the op is an IMPORT, open the space without this warning. */ |
| 896 |
if (thd_tablespace_op(thd) != Alter_info::ALTER_IMPORT_TABLESPACE) { |
| 897 |
ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_TABLESPACE_DISCARDED, |
| 898 |
table->s->table_name.str); |
| 899 |
} |
| 900 |
|
| 901 |
/* Allow an open because a proper DISCARD should have set |
| 902 |
all the flags and index root page numbers to FIL_NULL that |
| 903 |
should prevent any DML from running but it should allow DDL |
| 904 |
operations. */ |
| 905 |
no_tablespace = false; |
| 906 |
|
| 907 |
} else if (ib_table->ibd_file_missing) { |
| 908 |
ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_TABLESPACE_MISSING, norm_name); |
| 909 |
|
| 910 |
/* This means we have no idea what happened to the tablespace |
| 911 |
file, best to play it safe. */ |
| 912 |
|
| 913 |
no_tablespace = true; |
| 914 |
} else { |
| 915 |
no_tablespace = false; |
| 916 |
} |
| 917 |
|
| 918 |
if (!thd_tablespace_op(thd) && no_tablespace) { |
| 919 |
set_my_errno(ENOENT); |
| 920 |
close(); |
| 921 |
return HA_ERR_NO_SUCH_TABLE; |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
/* Get pointer to a table object in InnoDB dictionary cache. */ |
| 926 |
ib_table = m_part_share->get_table_part(0); |
| 927 |
|
| 928 |
m_prebuilt = row_create_prebuilt(ib_table, table->s->reclength); |
| 929 |
|
| 930 |
m_prebuilt->default_rec = table->s->default_values; |
| 931 |
ut_ad(m_prebuilt->default_rec); |
| 932 |
|
| 933 |
assert(table != nullptr); |
| 934 |
m_prebuilt->m_mysql_table = table; |
| 935 |
m_prebuilt->m_mysql_handler = this; |
| 936 |
|
| 937 |
if (ib_table->n_v_cols > 0) { |
| 938 |
dict_sys_mutex_enter(); |
| 939 |
m_part_share->set_v_templ(table, ib_table, name); |
| 940 |
dict_sys_mutex_exit(); |
| 941 |
} |
| 942 |
|
| 943 |
key_used_on_scan = table_share->primary_key; |
| 944 |
|
| 945 |
/* Allocate a buffer for a 'row reference'. A row reference is |
| 946 |
a string of bytes of length ref_length which uniquely specifies |
| 947 |
a row in our table. Note that MySQL may also compare two row |
| 948 |
references for equality by doing a simple memcmp on the strings |
| 949 |
of length ref_length! */ |
| 950 |
|
| 951 |
if (!row_table_got_default_clust_index(ib_table)) { |
| 952 |
m_prebuilt->clust_index_was_generated = false; |
| 953 |
|
| 954 |
if (table_share->is_missing_primary_key()) { |
| 955 |
table_name_t table_name; |
| 956 |
table_name.m_name = const_cast<char *>(name); |
| 957 |
ib::error(ER_IB_MSG_589) << "Table " << table_name |
| 958 |
<< " has a primary key in InnoDB data" |
| 959 |
" dictionary, but not in MySQL!"; |
| 960 |
|
| 961 |
/* This mismatch could cause further problems |
| 962 |
if not attended, bring this to the user's attention |
| 963 |
by printing a warning in addition to log a message |
| 964 |
in the errorlog. */ |
| 965 |
|
| 966 |
push_warning_printf(thd, Sql_condition::SL_WARNING, ER_NO_SUCH_INDEX, |
| 967 |
"Table %s has a" |
| 968 |
" primary key in InnoDB data" |
| 969 |
" dictionary, but not in" |
| 970 |
" MySQL!", |
| 971 |
name); |
| 972 |
|
| 973 |
/* If table_share->is_missing_primary_key(), |
| 974 |
the table_share->primary_key |
| 975 |
value could be out of bound if continue to index |
| 976 |
into key_info[] array. Find InnoDB primary index, |
| 977 |
and assign its key_length to ref_length. |
| 978 |
In addition, since MySQL indexes are sorted starting |
| 979 |
with primary index, unique index etc., initialize |
| 980 |
ref_length to the first index key length in |
| 981 |
case we fail to find InnoDB cluster index. |
| 982 |
|
| 983 |
Please note, this will not resolve the primary |
| 984 |
index mismatch problem, other side effects are |
| 985 |
possible if users continue to use the table. |
| 986 |
However, we allow this table to be opened so |
| 987 |
that user can adopt necessary measures for the |
| 988 |
mismatch while still being accessible to the table |
| 989 |
date. */ |
| 990 |
|
| 991 |
if (table->key_info == nullptr) { |
| 992 |
ut_ad(table->s->keys == 0); |
| 993 |
ref_length = 0; |
| 994 |
} else { |
| 995 |
ref_length = table->key_info[0].key_length; |
| 996 |
} |
| 997 |
|
| 998 |
/* Find corresponding cluster index |
| 999 |
key length in MySQL's key_info[] array. */ |
| 1000 |
|
| 1001 |
for (uint i = 0; i < table->s->keys; i++) { |
| 1002 |
dict_index_t *index; |
| 1003 |
index = innopart_get_index(0, i); |
| 1004 |
if (index->is_clustered()) { |
| 1005 |
ref_length = table->key_info[i].key_length; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
ut_ad(ref_length); |
| 1009 |
ref_length += PARTITION_BYTES_IN_POS; |
| 1010 |
} else { |
| 1011 |
/* MySQL allocates the buffer for ref. |
| 1012 |
key_info->key_length includes space for all key |
| 1013 |
columns + one byte for each column that may be |
| 1014 |
NULL. ref_length must be as exact as possible to |
| 1015 |
save space, because all row reference buffers are |
| 1016 |
allocated based on ref_length. */ |
| 1017 |
|
| 1018 |
ref_length = table->key_info[table_share->primary_key].key_length; |
| 1019 |
ref_length += PARTITION_BYTES_IN_POS; |
| 1020 |
} |
| 1021 |
} else { |
| 1022 |
if (!table_share->is_missing_primary_key()) { |
| 1023 |
table_name_t table_name; |
| 1024 |
table_name.m_name = const_cast<char *>(name); |
| 1025 |
ib::error(ER_IB_MSG_590) << "Table " << table_name |
| 1026 |
<< " has no primary key in InnoDB data" |
| 1027 |
" dictionary, but has one in MySQL! If you" |
| 1028 |
" created the table with a MySQL version <" |
| 1029 |
" 3.23.54 and did not define a primary key," |
| 1030 |
" but defined a unique key with all non-NULL" |
| 1031 |
" columns, then MySQL internally treats that" |
| 1032 |
" key as the primary key. You can fix this" |
| 1033 |
" error by dump + DROP + CREATE + reimport" |
| 1034 |
" of the table."; |
| 1035 |
|
| 1036 |
/* This mismatch could cause further problems |
| 1037 |
if not attended, bring this to the user attention |
| 1038 |
by printing a warning in addition to log a message |
| 1039 |
in the errorlog. */ |
| 1040 |
|
| 1041 |
push_warning_printf(thd, Sql_condition::SL_WARNING, ER_NO_SUCH_INDEX, |
| 1042 |
"InnoDB: Table %s has no" |
| 1043 |
" primary key in InnoDB data" |
| 1044 |
" dictionary, but has one in" |
| 1045 |
" MySQL!", |
| 1046 |
name); |
| 1047 |
} |
| 1048 |
|
| 1049 |
m_prebuilt->clust_index_was_generated = true; |
| 1050 |
|
| 1051 |
ref_length = DATA_ROW_ID_LEN; |
| 1052 |
ref_length += PARTITION_BYTES_IN_POS; |
| 1053 |
|
| 1054 |
/* If we automatically created the clustered index, then |
| 1055 |
MySQL does not know about it, and MySQL must NOT be aware |
| 1056 |
of the index used on scan, to make it avoid checking if we |
| 1057 |
update the column of the index. That is why we assert below |
| 1058 |
that key_used_on_scan is the undefined value MAX_KEY. |
| 1059 |
The column is the row id in the automatic generation case, |
| 1060 |
and it will never be updated anyway. */ |
| 1061 |
|
| 1062 |
if (key_used_on_scan != MAX_KEY) { |
| 1063 |
table_name_t table_name; |
| 1064 |
table_name.m_name = const_cast<char *>(name); |
| 1065 |
ib::warn(ER_IB_MSG_591) << "Table " << table_name |
| 1066 |
<< " key_used_on_scan is " << key_used_on_scan |
| 1067 |
<< " even though there is" |
| 1068 |
" no primary key inside InnoDB."; |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
/* Index block size in InnoDB: used by MySQL in query optimization. */ |
| 1073 |
stats.block_size = UNIV_PAGE_SIZE; |
| 1074 |
|
| 1075 |
/* Only if the table has an AUTOINC column. */ |
| 1076 |
if (m_prebuilt->table != nullptr && !m_prebuilt->table->ibd_file_missing && |
| 1077 |
table->found_next_number_field != nullptr) { |
| 1078 |
int error; |
| 1079 |
|
| 1080 |
/* Since a table can already be "open" in InnoDB's internal |
| 1081 |
data dictionary, we only init the autoinc counter once, the |
| 1082 |
first time the table is loaded, |
| 1083 |
see ha_innopart::initialize_auto_increment. |
| 1084 |
We can safely reuse the autoinc value from a previous MySQL |
| 1085 |
open. */ |
| 1086 |
|
| 1087 |
lock_auto_increment(); |
| 1088 |
error = initialize_auto_increment(false); |
| 1089 |
unlock_auto_increment(); |
| 1090 |
if (error != 0) { |
| 1091 |
close(); |
| 1092 |
return error; |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
#ifdef HA_INNOPART_SUPPORTS_FULLTEXT |
| 1097 |
/* Set plugin parser for fulltext index. */ |
| 1098 |
for (uint i = 0; i < table->s->keys; i++) { |
| 1099 |
if (table->key_info[i].flags & HA_USES_PARSER) { |
| 1100 |
dict_index_t *index = innobase_get_index(i); |
| 1101 |
plugin_ref parser = table->key_info[i].parser; |
| 1102 |
|
| 1103 |
ut_ad(index->type & DICT_FTS); |
| 1104 |
index->parser = |
| 1105 |
static_cast<st_mysql_ftparser *>(plugin_decl(parser)->info); |
| 1106 |
|
| 1107 |
DBUG_EXECUTE_IF("fts_instrument_use_default_parser", |
| 1108 |
index->parser = &fts_default_parser;); |
| 1109 |
} |
| 1110 |
} |
| 1111 |
#endif /* HA_INNOPART_SUPPORTS_FULLTEXT */ |
| 1112 |
|
| 1113 |
m_parts = ut::make_unique<saved_prebuilt_t[]>( |
| 1114 |
ut::make_psi_memory_key(mem_key_partitioning), m_tot_parts); |
| 1115 |
/* Verify that ut::new_arr_withkey performs value-initialization, which should |
| 1116 |
zero-initialize built-in types such as pointers and integers. */ |
| 1117 |
#ifdef UNIV_DEBUG |
| 1118 |
for (size_t i = 0; i < m_tot_parts; ++i) { |
| 1119 |
const auto &part{m_parts[i]}; |
| 1120 |
ut_a(part.m_ins_node == nullptr); |
| 1121 |
ut_a(part.m_upd_node == nullptr); |
| 1122 |
ut_a(part.m_row_read_type == 0); |
| 1123 |
ut_a(part.m_trx_id == 0); |
| 1124 |
ut_a(part.m_blob_heap == nullptr); |
| 1125 |
ut_a(0 == part.m_new_rec_lock.count()); |
| 1126 |
} |
| 1127 |
#endif |
| 1128 |
const size_t alloc_size = UT_BITS_IN_BYTES(m_tot_parts); |
| 1129 |
m_bitset = static_cast<byte *>(ut::zalloc_withkey( |
| 1130 |
ut::make_psi_memory_key(mem_key_partitioning), alloc_size)); |
| 1131 |
|
| 1132 |
if (m_parts == nullptr || m_bitset == nullptr) { |
| 1133 |
close(); // Frees all the above. |
| 1134 |
return HA_ERR_OUT_OF_MEM; |
| 1135 |
} |
| 1136 |
|
| 1137 |
m_sql_stat_start_parts = {m_bitset, UT_BITS_IN_BYTES(m_tot_parts)}; |
| 1138 |
m_reuse_mysql_template = false; |
| 1139 |
|
| 1140 |
info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST); |
| 1141 |
|
| 1142 |
return 0; |
| 1143 |
} |
| 1144 |
|
| 1145 |
/** Clone this handler, used when needing more than one cursor |
| 1146 |
to the same table. |
| 1147 |
@param[in] name Table name. |
| 1148 |
@param[in] mem_root mem_root to allocate from. |
| 1149 |
@retval Pointer to clone or NULL if error. */ |
| 1150 |
handler *ha_innopart::clone(const char *name, MEM_ROOT *mem_root) { |
| 1151 |
ha_innopart *new_handler; |
| 1152 |
|
| 1153 |
DBUG_TRACE; |
| 1154 |
|
| 1155 |
new_handler = dynamic_cast<ha_innopart *>(handler::clone(name, mem_root)); |
| 1156 |
if (new_handler != nullptr) { |
| 1157 |
ut_ad(new_handler->m_prebuilt != nullptr); |
| 1158 |
|
| 1159 |
new_handler->m_prebuilt->select_lock_type = m_prebuilt->select_lock_type; |
| 1160 |
new_handler->m_prebuilt->select_mode = m_prebuilt->select_mode; |
| 1161 |
} |
| 1162 |
|
| 1163 |
return new_handler; |
| 1164 |
} |
| 1165 |
|
| 1166 |
/** Clear used ins_nodes and upd_nodes. */ |
| 1167 |
void ha_innopart::clear_ins_upd_nodes() { |
| 1168 |
if (m_parts != nullptr) { |
| 1169 |
for (uint i = 0; i < m_tot_parts; i++) { |
| 1170 |
auto &part{m_parts[i]}; |
| 1171 |
/* Free memory from insert nodes. */ |
| 1172 |
if (part.m_ins_node != nullptr) { |
| 1173 |
ins_node_t *ins = part.m_ins_node; |
| 1174 |
if (ins->select != nullptr) { |
| 1175 |
que_graph_free_recursive(ins->select); |
| 1176 |
ins->select = nullptr; |
| 1177 |
} |
| 1178 |
|
| 1179 |
if (ins->entry_sys_heap != nullptr) { |
| 1180 |
mem_heap_free(ins->entry_sys_heap); |
| 1181 |
ins->entry_sys_heap = nullptr; |
| 1182 |
} |
| 1183 |
part.m_ins_node = nullptr; |
| 1184 |
} |
| 1185 |
|
| 1186 |
/* Free memory from update nodes. */ |
| 1187 |
if (part.m_upd_node != nullptr) { |
| 1188 |
upd_node_t *upd = part.m_upd_node; |
| 1189 |
if (upd->update) { |
| 1190 |
upd->update->free_per_stmt_heap(); |
| 1191 |
} |
| 1192 |
if (upd->cascade_heap) { |
| 1193 |
mem_heap_free(upd->cascade_heap); |
| 1194 |
upd->cascade_heap = nullptr; |
| 1195 |
} |
| 1196 |
if (upd->in_mysql_interface) { |
| 1197 |
btr_pcur_t::free_for_mysql(upd->pcur); |
| 1198 |
upd->in_mysql_interface = false; |
| 1199 |
} |
| 1200 |
|
| 1201 |
if (upd->select != nullptr) { |
| 1202 |
que_graph_free_recursive(upd->select); |
| 1203 |
upd->select = nullptr; |
| 1204 |
} |
| 1205 |
if (upd->heap != nullptr) { |
| 1206 |
mem_heap_free(upd->heap); |
| 1207 |
upd->heap = nullptr; |
| 1208 |
} |
| 1209 |
part.m_upd_node = nullptr; |
| 1210 |
} |
| 1211 |
} |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
/** Closes a handle to an InnoDB table. |
| 1216 |
@return 0 */ |
| 1217 |
int ha_innopart::close() { |
| 1218 |
DBUG_TRACE; |
| 1219 |
|
| 1220 |
ut_ad(m_pcur_parts == nullptr); |
| 1221 |
ut_ad(m_clust_pcur_parts == nullptr); |
| 1222 |
close_partitioning(); |
| 1223 |
|
| 1224 |
ut_ad(m_part_share != nullptr); |
| 1225 |
if (m_part_share != nullptr) { |
| 1226 |
lock_shared_ha_data(); |
| 1227 |
m_part_share->close_table_parts(); |
| 1228 |
unlock_shared_ha_data(); |
| 1229 |
m_part_share = nullptr; |
| 1230 |
} |
| 1231 |
clear_ins_upd_nodes(); |
| 1232 |
clear_blob_heaps(); |
| 1233 |
|
| 1234 |
/* Prevent double close of m_prebuilt->table. The real one was done |
| 1235 |
done in m_part_share->close_table_parts(). */ |
| 1236 |
if (m_prebuilt != nullptr) { |
| 1237 |
m_prebuilt->table = nullptr; |
| 1238 |
row_prebuilt_free(m_prebuilt, false); |
| 1239 |
} |
| 1240 |
|
| 1241 |
if (m_upd_buf != nullptr) { |
| 1242 |
ut_ad(m_upd_buf_size != 0); |
| 1243 |
/* Allocated with my_malloc! */ |
| 1244 |
my_free(m_upd_buf); |
| 1245 |
m_upd_buf = nullptr; |
| 1246 |
m_upd_buf_size = 0; |
| 1247 |
} |
| 1248 |
|
| 1249 |
m_parts = nullptr; |
| 1250 |
|
| 1251 |
ut::free(m_bitset); |
| 1252 |
m_bitset = nullptr; |
| 1253 |
|
| 1254 |
MONITOR_INC(MONITOR_TABLE_CLOSE); |
| 1255 |
|
| 1256 |
/* Tell InnoDB server that there might be work for |
| 1257 |
utility threads: */ |
| 1258 |
|
| 1259 |
srv_active_wake_master_thread(); |
| 1260 |
|
| 1261 |
return 0; |
| 1262 |
} |
| 1263 |
|
| 1264 |
/** Change active partition. |
| 1265 |
Copies needed info into m_prebuilt from the partition specific memory. |
| 1266 |
@param[in] part_id Partition to set as active. */ |
| 1267 |
void ha_innopart::set_partition(uint part_id) { |
| 1268 |
DBUG_TRACE; |
| 1269 |
|
| 1270 |
DBUG_PRINT("ha_innopart", ("partition id: %u", part_id)); |
| 1271 |
|
| 1272 |
if (part_id >= m_tot_parts) { |
| 1273 |
ut_d(ut_error); |
| 1274 |
ut_o(return); |
| 1275 |
} |
| 1276 |
if (m_pcur_parts != nullptr) { |
| 1277 |
m_prebuilt->pcur = &m_pcur_parts[m_pcur_map[part_id]]; |
| 1278 |
} |
| 1279 |
if (m_clust_pcur_parts != nullptr) { |
| 1280 |
m_prebuilt->clust_pcur = &m_clust_pcur_parts[m_pcur_map[part_id]]; |
| 1281 |
} |
| 1282 |
|
| 1283 |
/* Restore all fields stored in m_parts[part_id] to corresponding m_prebuilt's |
| 1284 |
fields, except for m_blob_heap for which we have a special case: */ |
| 1285 |
|
| 1286 |
const auto &part{m_parts[part_id]}; |
| 1287 |
m_prebuilt->ins_node = part.m_ins_node; |
| 1288 |
m_prebuilt->upd_node = part.m_upd_node; |
| 1289 |
|
| 1290 |
/* For unordered scan and table scan, use blob_heap from first |
| 1291 |
partition as we need exactly one blob. */ |
| 1292 |
m_prebuilt->blob_heap = m_parts[m_ordered ? part_id : 0].m_blob_heap; |
| 1293 |
|
| 1294 |
#ifdef UNIV_DEBUG |
| 1295 |
if (m_prebuilt->blob_heap != nullptr) { |
| 1296 |
DBUG_PRINT("ha_innopart", |
| 1297 |
("validating blob_heap: %p", m_prebuilt->blob_heap)); |
| 1298 |
mem_heap_validate(m_prebuilt->blob_heap); |
| 1299 |
} |
| 1300 |
#endif |
| 1301 |
|
| 1302 |
m_prebuilt->trx_id = part.m_trx_id; |
| 1303 |
m_prebuilt->row_read_type = part.m_row_read_type; |
| 1304 |
m_prebuilt->new_rec_lock = part.m_new_rec_lock; |
| 1305 |
|
| 1306 |
/* All fields from m_parts[part_id] should have been restored above. */ |
| 1307 |
|
| 1308 |
m_prebuilt->sql_stat_start = m_sql_stat_start_parts.test(part_id); |
| 1309 |
m_prebuilt->table = m_part_share->get_table_part(part_id); |
| 1310 |
m_prebuilt->index = innopart_get_index(part_id, active_index); |
| 1311 |
} |
| 1312 |
|
| 1313 |
/** Update active partition. |
| 1314 |
Copies needed info from m_prebuilt into the partition specific memory. |
| 1315 |
@param[in] part_id Partition to set as active. */ |
| 1316 |
void ha_innopart::update_partition(uint part_id) { |
| 1317 |
DBUG_TRACE; |
| 1318 |
DBUG_PRINT("ha_innopart", ("partition id: %u", part_id)); |
| 1319 |
|
| 1320 |
if (part_id >= m_tot_parts) { |
| 1321 |
ut_d(ut_error); |
| 1322 |
ut_o(return); |
| 1323 |
} |
| 1324 |
|
| 1325 |
/* Update all m_parts[part_id] fields with corresponding m_prebuilt's fields, |
| 1326 |
except for m_blob_heap for which we have a special case: */ |
| 1327 |
|
| 1328 |
auto &part{m_parts[part_id]}; |
| 1329 |
part.m_ins_node = m_prebuilt->ins_node; |
| 1330 |
part.m_upd_node = m_prebuilt->upd_node; |
| 1331 |
|
| 1332 |
#ifdef UNIV_DEBUG |
| 1333 |
if (m_prebuilt->blob_heap != nullptr) { |
| 1334 |
DBUG_PRINT("ha_innopart", |
| 1335 |
("validating blob_heap: %p", m_prebuilt->blob_heap)); |
| 1336 |
mem_heap_validate(m_prebuilt->blob_heap); |
| 1337 |
} |
| 1338 |
#endif |
| 1339 |
|
| 1340 |
/* For unordered scan and table scan, use blob_heap from first |
| 1341 |
partition as we need exactly one blob anytime. */ |
| 1342 |
m_parts[m_ordered ? part_id : 0].m_blob_heap = m_prebuilt->blob_heap; |
| 1343 |
|
| 1344 |
part.m_trx_id = m_prebuilt->trx_id; |
| 1345 |
part.m_row_read_type = m_prebuilt->row_read_type; |
| 1346 |
part.m_new_rec_lock = m_prebuilt->new_rec_lock; |
| 1347 |
|
| 1348 |
/* All fields of m_parts[part_id] should have been updated above. */ |
| 1349 |
|
| 1350 |
if (m_prebuilt->sql_stat_start == 0) { |
| 1351 |
m_sql_stat_start_parts.set(part_id, false); |
| 1352 |
m_reuse_mysql_template = true; |
| 1353 |
} |
| 1354 |
m_last_part = part_id; |
| 1355 |
} |
| 1356 |
|
| 1357 |
/** Save currently highest auto increment value. |
| 1358 |
@param[in] nr Auto increment value to save. */ |
| 1359 |
void ha_innopart::save_auto_increment(ulonglong nr) { |
| 1360 |
/* Store it in the shared dictionary of the partition. |
| 1361 |
TODO: When the new DD is done, store it in the table and make it |
| 1362 |
persistent! */ |
| 1363 |
|
| 1364 |
dict_table_autoinc_lock(m_prebuilt->table); |
| 1365 |
dict_table_autoinc_update_if_greater(m_prebuilt->table, nr + 1); |
| 1366 |
dict_table_autoinc_unlock(m_prebuilt->table); |
| 1367 |
} |
| 1368 |
|
| 1369 |
/** Was the last returned row semi consistent read. |
| 1370 |
In an UPDATE or DELETE, if the row under the cursor was locked by |
| 1371 |
another transaction, and the engine used an optimistic read of the last |
| 1372 |
committed row value under the cursor, then the engine returns 1 from |
| 1373 |
this function. MySQL must NOT try to update this optimistic value. If |
| 1374 |
the optimistic value does not match the WHERE condition, MySQL can |
| 1375 |
decide to skip over this row. This can be used to avoid unnecessary |
| 1376 |
lock waits. |
| 1377 |
|
| 1378 |
If this method returns true, it will also signal the storage |
| 1379 |
engine that the next read will be a locking re-read of the row. |
| 1380 |
@see handler.h and row0mysql.h |
| 1381 |
@return true if last read was semi consistent else false. */ |
| 1382 |
bool ha_innopart::was_semi_consistent_read() { |
| 1383 |
return m_parts[m_last_part].m_row_read_type == ROW_READ_DID_SEMI_CONSISTENT; |
| 1384 |
} |
| 1385 |
|
| 1386 |
/** Try semi consistent read. |
| 1387 |
Tell the engine whether it should avoid unnecessary lock waits. |
| 1388 |
If yes, in an UPDATE or DELETE, if the row under the cursor was locked |
| 1389 |
by another transaction, the engine may try an optimistic read of |
| 1390 |
the last committed row value under the cursor. |
| 1391 |
@see handler.h and row0mysql.h |
| 1392 |
@param[in] yes Should semi-consistent read be used. */ |
| 1393 |
void ha_innopart::try_semi_consistent_read(bool yes) { |
| 1394 |
ha_innobase::try_semi_consistent_read(yes); |
| 1395 |
for (uint i = m_part_info->get_first_used_partition(); i < m_tot_parts; |
| 1396 |
i = m_part_info->get_next_used_partition(i)) { |
| 1397 |
m_parts[i].m_row_read_type = m_prebuilt->row_read_type; |
| 1398 |
} |
| 1399 |
} |
| 1400 |
|
| 1401 |
/** Removes a lock on a row. |
| 1402 |
Removes a new lock set on a row, if it was not read optimistically. |
| 1403 |
This can be called after a row has been read in the processing of |
| 1404 |
an UPDATE or a DELETE query. @see ha_innobase::unlock_row(). */ |
| 1405 |
void ha_innopart::unlock_row() { |
| 1406 |
ut_ad(m_last_part < m_tot_parts); |
| 1407 |
set_partition(m_last_part); |
| 1408 |
ha_innobase::unlock_row(); |
| 1409 |
update_partition(m_last_part); |
| 1410 |
} |
| 1411 |
|
| 1412 |
/** Write a row in specific partition. |
| 1413 |
Stores a row in an InnoDB database, to the table specified in this |
| 1414 |
handle. |
| 1415 |
@param[in] part_id Partition to write to. |
| 1416 |
@param[in] record A row in MySQL format. |
| 1417 |
@return error code. */ |
| 1418 |
int ha_innopart::write_row_in_part(uint part_id, uchar *record) { |
| 1419 |
int error; |
| 1420 |
Field *saved_next_number_field = table->next_number_field; |
| 1421 |
DBUG_TRACE; |
| 1422 |
set_partition(part_id); |
| 1423 |
|
| 1424 |
/* Prevent update_auto_increment to be called |
| 1425 |
again in ha_innobase::write_row(). */ |
| 1426 |
|
| 1427 |
table->next_number_field = nullptr; |
| 1428 |
|
| 1429 |
/* TODO: try to avoid creating a new dtuple |
| 1430 |
(in row_get_prebuilt_insert_row()) for each partition). |
| 1431 |
Might be needed due to ins_node implementation. */ |
| 1432 |
|
| 1433 |
error = ha_innobase::write_row(record); |
| 1434 |
update_partition(part_id); |
| 1435 |
table->next_number_field = saved_next_number_field; |
| 1436 |
return error; |
| 1437 |
} |
| 1438 |
|
| 1439 |
/** Update a row in partition. |
| 1440 |
Updates a row given as a parameter to a new value. |
| 1441 |
@param[in] part_id Partition to update row in. |
| 1442 |
@param[in] old_row Old row in MySQL format. |
| 1443 |
@param[in] new_row New row in MySQL format. |
| 1444 |
@return 0 or error number. */ |
| 1445 |
int ha_innopart::update_row_in_part(uint part_id, const uchar *old_row, |
| 1446 |
uchar *new_row) { |
| 1447 |
int error; |
| 1448 |
DBUG_TRACE; |
| 1449 |
|
| 1450 |
set_partition(part_id); |
| 1451 |
error = ha_innobase::update_row(old_row, new_row); |
| 1452 |
update_partition(part_id); |
| 1453 |
return error; |
| 1454 |
} |
| 1455 |
|
| 1456 |
/** Deletes a row in partition. |
| 1457 |
@param[in] part_id Partition to delete from. |
| 1458 |
@param[in] record Row to delete in MySQL format. |
| 1459 |
@return 0 or error number. */ |
| 1460 |
int ha_innopart::delete_row_in_part(uint part_id, const uchar *record) { |
| 1461 |
int error; |
| 1462 |
DBUG_TRACE; |
| 1463 |
m_err_rec = nullptr; |
| 1464 |
|
| 1465 |
m_last_part = part_id; |
| 1466 |
set_partition(part_id); |
| 1467 |
error = ha_innobase::delete_row(record); |
| 1468 |
update_partition(part_id); |
| 1469 |
return error; |
| 1470 |
} |
| 1471 |
|
| 1472 |
/** Initializes a handle to use an index. |
| 1473 |
@param[in] keynr Key (index) number. |
| 1474 |
@param[in] sorted True if result MUST be sorted according to index. |
| 1475 |
@return 0 or error number. */ |
| 1476 |
int ha_innopart::index_init(uint keynr, bool sorted) { |
| 1477 |
int error; |
| 1478 |
uint part_id = m_part_info->get_first_used_partition(); |
| 1479 |
DBUG_TRACE; |
| 1480 |
|
| 1481 |
active_index = keynr; |
| 1482 |
if (part_id == MY_BIT_NONE) { |
| 1483 |
return 0; |
| 1484 |
} |
| 1485 |
|
| 1486 |
error = ph_index_init_setup(keynr, sorted); |
| 1487 |
if (error != 0) { |
| 1488 |
return error; |
| 1489 |
} |
| 1490 |
|
| 1491 |
if (sorted) { |
| 1492 |
error = init_record_priority_queue(); |
| 1493 |
if (error != 0) { |
| 1494 |
/* Needs cleanup in case it returns error. */ |
| 1495 |
destroy_record_priority_queue(); |
| 1496 |
return error; |
| 1497 |
} |
| 1498 |
/* Disable prefetch. |
| 1499 |
The prefetch buffer is not partitioning aware, so it may return |
| 1500 |
rows from a different partition if either the prefetch buffer is |
| 1501 |
full, or it is non-empty and the partition is exhausted. */ |
| 1502 |
m_prebuilt->m_no_prefetch = true; |
| 1503 |
} |
| 1504 |
|
| 1505 |
/* For scan across partitions, the keys needs to be materialized */ |
| 1506 |
m_prebuilt->m_read_virtual_key = true; |
| 1507 |
|
| 1508 |
error = change_active_index(part_id, keynr); |
| 1509 |
if (error != 0) { |
| 1510 |
destroy_record_priority_queue(); |
| 1511 |
return error; |
| 1512 |
} |
| 1513 |
|
| 1514 |
DBUG_EXECUTE_IF("partition_fail_index_init", { |
| 1515 |
destroy_record_priority_queue(); |
| 1516 |
return HA_ERR_NO_PARTITION_FOUND; |
| 1517 |
}); |
| 1518 |
|
| 1519 |
return 0; |
| 1520 |
} |
| 1521 |
|
| 1522 |
/** End index cursor. |
| 1523 |
@return 0 or error code. */ |
| 1524 |
int ha_innopart::index_end() { |
| 1525 |
uint part_id = m_part_info->get_first_used_partition(); |
| 1526 |
DBUG_TRACE; |
| 1527 |
|
| 1528 |
if (part_id == MY_BIT_NONE) { |
| 1529 |
/* Never initialized any index. */ |
| 1530 |
active_index = MAX_KEY; |
| 1531 |
return 0; |
| 1532 |
} |
| 1533 |
if (m_ordered) { |
| 1534 |
destroy_record_priority_queue(); |
| 1535 |
m_prebuilt->m_no_prefetch = false; |
| 1536 |
} |
| 1537 |
m_prebuilt->m_read_virtual_key = false; |
| 1538 |
|
| 1539 |
return ha_innobase::index_end(); |
| 1540 |
} |
| 1541 |
|
| 1542 |
/* Partitioning support functions. */ |
| 1543 |
|
| 1544 |
/** Setup the ordered record buffer and the priority queue. |
| 1545 |
@param[in] used_parts Number of used partitions in query. |
| 1546 |
@return false for success, else true. */ |
| 1547 |
int ha_innopart::init_record_priority_queue_for_parts(uint used_parts) { |
| 1548 |
size_t alloc_size; |
| 1549 |
void *buf; |
| 1550 |
|
| 1551 |
DBUG_TRACE; |
| 1552 |
ut_ad(used_parts >= 1); |
| 1553 |
/* TODO: Don't use this if only one partition is used! */ |
| 1554 |
// ut_ad(used_parts > 1); |
| 1555 |
|
| 1556 |
/* We could reuse current m_prebuilt->pcur/clust_pcur for the first |
| 1557 |
used partition, but it would complicate and affect performance, |
| 1558 |
so we trade some extra memory instead. */ |
| 1559 |
|
| 1560 |
m_pcur = m_prebuilt->pcur; |
| 1561 |
m_clust_pcur = m_prebuilt->clust_pcur; |
| 1562 |
|
| 1563 |
/* If we searching for secondary key or doing a write/update |
| 1564 |
we will need two pcur, one for the active (secondary) index and |
| 1565 |
one for the clustered index. */ |
| 1566 |
|
| 1567 |
bool need_clust_index = |
| 1568 |
m_curr_key_info[1] != nullptr || get_lock_type() != F_RDLCK; |
| 1569 |
|
| 1570 |
/* pcur and clust_pcur per partition. |
| 1571 |
By using zalloc, we do not need to initialize the pcur's! */ |
| 1572 |
|
| 1573 |
alloc_size = used_parts * sizeof(btr_pcur_t); |
| 1574 |
if (need_clust_index) { |
| 1575 |
alloc_size *= 2; |
| 1576 |
} |
| 1577 |
buf = ut::zalloc_withkey(ut::make_psi_memory_key(mem_key_partitioning), |
| 1578 |
alloc_size); |
| 1579 |
if (buf == nullptr) { |
| 1580 |
return true; |
| 1581 |
} |
| 1582 |
m_pcur_parts = static_cast<btr_pcur_t *>(buf); |
| 1583 |
if (need_clust_index) { |
| 1584 |
m_clust_pcur_parts = &m_pcur_parts[used_parts]; |
| 1585 |
} |
| 1586 |
/* mapping from part_id to pcur. */ |
| 1587 |
alloc_size = m_tot_parts * sizeof(*m_pcur_map); |
| 1588 |
buf = ut::zalloc_withkey(ut::make_psi_memory_key(mem_key_partitioning), |
| 1589 |
alloc_size); |
| 1590 |
if (buf == nullptr) { |
| 1591 |
return true; |
| 1592 |
} |
| 1593 |
m_pcur_map = static_cast<uint16_t *>(buf); |
| 1594 |
{ |
| 1595 |
uint16_t pcur_count = 0; |
| 1596 |
for (uint i = m_part_info->get_first_used_partition(); i < m_tot_parts; |
| 1597 |
i = m_part_info->get_next_used_partition(i)) { |
| 1598 |
m_pcur_map[i] = pcur_count++; |
| 1599 |
} |
| 1600 |
} |
| 1601 |
|
| 1602 |
return false; |
| 1603 |
} |
| 1604 |
|
| 1605 |
/** Destroy the ordered record buffer and the priority queue. */ |
| 1606 |
inline void ha_innopart::destroy_record_priority_queue_for_parts() { |
| 1607 |
DBUG_TRACE; |
| 1608 |
if (m_pcur_parts != nullptr) { |
| 1609 |
uint used_parts; |
| 1610 |
used_parts = bitmap_bits_set(&m_part_info->read_partitions); |
| 1611 |
for (uint i = 0; i < used_parts; i++) { |
| 1612 |
m_pcur_parts[i].free_rec_buf(); |
| 1613 |
if (m_clust_pcur_parts != nullptr) { |
| 1614 |
m_clust_pcur_parts[i].free_rec_buf(); |
| 1615 |
} |
| 1616 |
} |
| 1617 |
ut::free(m_pcur_parts); |
| 1618 |
m_clust_pcur_parts = nullptr; |
| 1619 |
m_pcur_parts = nullptr; |
| 1620 |
/* Reset the original m_prebuilt->pcur. */ |
| 1621 |
m_prebuilt->pcur = m_pcur; |
| 1622 |
m_prebuilt->clust_pcur = m_clust_pcur; |
| 1623 |
} |
| 1624 |
if (m_pcur_map != nullptr) { |
| 1625 |
ut::free(m_pcur_map); |
| 1626 |
m_pcur_map = nullptr; |
| 1627 |
} |
| 1628 |
} |
| 1629 |
|
| 1630 |
/** Print error information. |
| 1631 |
@param[in] error Error code (MySQL). |
| 1632 |
@param[in] errflag Flags. */ |
| 1633 |
void ha_innopart::print_error(int error, myf errflag) { |
| 1634 |
DBUG_TRACE; |
| 1635 |
if (print_partition_error(error)) { |
| 1636 |
ha_innobase::print_error(error, errflag); |
| 1637 |
} |
| 1638 |
} |
| 1639 |
|
| 1640 |
/** Can error be ignored. |
| 1641 |
@param[in] error Error code to check. |
| 1642 |
@return true if ignorable else false. */ |
| 1643 |
bool ha_innopart::is_ignorable_error(int error) { |
| 1644 |
if (ha_innobase::is_ignorable_error(error) || |
| 1645 |
error == HA_ERR_NO_PARTITION_FOUND || |
| 1646 |
error == HA_ERR_NOT_IN_LOCK_PARTITIONS) { |
| 1647 |
return (true); |
| 1648 |
} |
| 1649 |
return (false); |
| 1650 |
} |
| 1651 |
|
| 1652 |
/** Get the index for the current partition |
| 1653 |
@param[in] keynr MySQL index number. |
| 1654 |
@return InnoDB index or NULL. */ |
| 1655 |
inline dict_index_t *ha_innopart::innobase_get_index(uint keynr) { |
| 1656 |
uint part_id = m_last_part; |
| 1657 |
if (part_id >= m_tot_parts) { |
| 1658 |
part_id = 0; |
| 1659 |
ut_d(ut_error); |
| 1660 |
} |
| 1661 |
return (innopart_get_index(part_id, keynr)); |
| 1662 |
} |
| 1663 |
|
| 1664 |
/** Get the index for a handle. |
| 1665 |
Does not change active index. |
| 1666 |
@param[in] keynr Use this index; MAX_KEY means always clustered index, |
| 1667 |
even if it was internally generated by InnoDB. |
| 1668 |
@param[in] part_id From this partition. |
| 1669 |
@return NULL or index instance. */ |
| 1670 |
inline dict_index_t *ha_innopart::innopart_get_index(uint part_id, uint keynr) { |
| 1671 |
KEY *key = nullptr; |
| 1672 |
dict_index_t *index = nullptr; |
| 1673 |
|
| 1674 |
DBUG_TRACE; |
| 1675 |
|
| 1676 |
if (keynr != MAX_KEY && table->s->keys > 0) { |
| 1677 |
key = table->key_info + keynr; |
| 1678 |
|
| 1679 |
index = m_part_share->get_index(part_id, keynr); |
| 1680 |
|
| 1681 |
if (index != nullptr) { |
| 1682 |
ut_ad(ut_strcmp(index->name, key->name) == 0); |
| 1683 |
} else { |
| 1684 |
/* Can't find index with keynr in the translation |
| 1685 |
table. Only print message if the index translation |
| 1686 |
table exists. */ |
| 1687 |
|
| 1688 |
ib::warn(ER_IB_MSG_592) |
| 1689 |
<< "InnoDB could not find index " << (key ? key->name : "NULL") |
| 1690 |
<< " key no " << keynr << " for table " << m_prebuilt->table->name |
| 1691 |
<< " through its index translation table"; |
| 1692 |
|
| 1693 |
index = dict_table_get_index_on_name(m_prebuilt->table, key->name); |
| 1694 |
} |
| 1695 |
} else { |
| 1696 |
/* Get the generated index. */ |
| 1697 |
ut_ad(keynr == MAX_KEY); |
| 1698 |
index = m_part_share->get_table_part(part_id)->first_index(); |
| 1699 |
} |
| 1700 |
|
| 1701 |
if (index == nullptr) { |
| 1702 |
ib::error(ER_IB_MSG_593) |
| 1703 |
<< "InnoDB could not find key n:o " << keynr << " with name " |
| 1704 |
<< (key ? key->name : "NULL") << " from dict cache for table " |
| 1705 |
<< m_prebuilt->table->name << " partition n:o " << part_id; |
| 1706 |
} |
| 1707 |
|
| 1708 |
return index; |
| 1709 |
} |
| 1710 |
|
| 1711 |
/** Changes the active index of a handle. |
| 1712 |
@param[in] part_id Use this partition. |
| 1713 |
@param[in] keynr Use this index; MAX_KEY means always clustered index, |
| 1714 |
even if it was internally generated by InnoDB. |
| 1715 |
@return 0 or error number. */ |
| 1716 |
int ha_innopart::change_active_index(uint part_id, uint keynr) { |
| 1717 |
DBUG_TRACE; |
| 1718 |
|
| 1719 |
ut_ad(m_user_thd == ha_thd()); |
| 1720 |
ut_ad(m_prebuilt->trx == thd_to_trx(m_user_thd)); |
| 1721 |
|
| 1722 |
active_index = keynr; |
| 1723 |
set_partition(part_id); |
| 1724 |
|
| 1725 |
if (UNIV_UNLIKELY(m_prebuilt->index == nullptr)) { |
| 1726 |
ib::warn(ER_IB_MSG_594) |
| 1727 |
<< "change_active_index(" << part_id << "," << keynr << ") failed"; |
| 1728 |
m_prebuilt->index_usable = false; |
| 1729 |
return 1; |
| 1730 |
} |
| 1731 |
|
| 1732 |
m_prebuilt->index_usable = m_prebuilt->index->is_usable(m_prebuilt->trx); |
| 1733 |
|
| 1734 |
if (UNIV_UNLIKELY(!m_prebuilt->index_usable)) { |
| 1735 |
if (m_prebuilt->index->is_corrupted()) { |
| 1736 |
char table_name[MAX_FULL_NAME_LEN + 1]; |
| 1737 |
|
| 1738 |
innobase_format_name(table_name, sizeof table_name, |
| 1739 |
m_prebuilt->index->table->name.m_name); |
| 1740 |
|
| 1741 |
push_warning_printf(m_user_thd, Sql_condition::SL_WARNING, |
| 1742 |
HA_ERR_INDEX_CORRUPT, |
| 1743 |
"InnoDB: Index %s for table %s is" |
| 1744 |
" marked as corrupted" |
| 1745 |
" (partition %u)", |
| 1746 |
m_prebuilt->index->name(), table_name, part_id); |
| 1747 |
return HA_ERR_INDEX_CORRUPT; |
| 1748 |
} else { |
| 1749 |
push_warning_printf(m_user_thd, Sql_condition::SL_WARNING, |
| 1750 |
HA_ERR_TABLE_DEF_CHANGED, |
| 1751 |
"InnoDB: insufficient history for index %u", keynr); |
| 1752 |
} |
| 1753 |
|
| 1754 |
/* The caller seems to ignore this. Thus, we must check |
| 1755 |
this again in row_search_for_mysql(). */ |
| 1756 |
|
| 1757 |
return HA_ERR_TABLE_DEF_CHANGED; |
| 1758 |
} |
| 1759 |
|
| 1760 |
ut_a(m_prebuilt->search_tuple != nullptr); |
| 1761 |
ut_a(m_prebuilt->m_stop_tuple != nullptr); |
| 1762 |
|
| 1763 |
/* If too expensive, cache the keynr and only update search_tuple when |
| 1764 |
keynr changes. Remember that the clustered index is also used for |
| 1765 |
MAX_KEY. */ |
| 1766 |
m_prebuilt->init_search_tuples_types(); |
| 1767 |
|
| 1768 |
/* MySQL changes the active index for a handle also during some |
| 1769 |
queries, for example SELECT MAX(a), SUM(a) first retrieves the |
| 1770 |
MAX() and then calculates the sum. Previously we played safe |
| 1771 |
and used the flag ROW_MYSQL_WHOLE_ROW below, but that caused |
| 1772 |
unnecessary copying. Starting from MySQL-4.1 we use a more |
| 1773 |
efficient flag here. */ |
| 1774 |
|
| 1775 |
/* TODO: Is this really needed? |
| 1776 |
Will it not be built in index_read? */ |
| 1777 |
|
| 1778 |
build_template(false); |
| 1779 |
|
| 1780 |
return 0; |
| 1781 |
} |
| 1782 |
|
| 1783 |
/** Return first record in index from a partition. |
| 1784 |
@param[in] part Partition to read from. |
| 1785 |
@param[out] record First record in index in the partition. |
| 1786 |
@return error number or 0. */ |
| 1787 |
int ha_innopart::index_first_in_part(uint part, uchar *record) { |
| 1788 |
int error; |
| 1789 |
DBUG_TRACE; |
| 1790 |
|
| 1791 |
set_partition(part); |
| 1792 |
error = ha_innobase::index_first(record); |
| 1793 |
update_partition(part); |
| 1794 |
|
| 1795 |
return error; |
| 1796 |
} |
| 1797 |
|
| 1798 |
/** Return next record in index from a partition. |
| 1799 |
@param[in] part Partition to read from. |
| 1800 |
@param[out] record Last record in index in the partition. |
| 1801 |
@return error number or 0. */ |
| 1802 |
int ha_innopart::index_next_in_part(uint part, uchar *record) { |
| 1803 |
DBUG_TRACE; |
| 1804 |
|
| 1805 |
int error; |
| 1806 |
|
| 1807 |
set_partition(part); |
| 1808 |
error = ha_innobase::index_next(record); |
| 1809 |
update_partition(part); |
| 1810 |
|
| 1811 |
ut_ad(m_ordered_scan_ongoing || m_ordered_rec_buffer == nullptr || |
| 1812 |
m_prebuilt->used_in_HANDLER || |
| 1813 |
m_part_spec.start_part >= m_part_spec.end_part); |
| 1814 |
|
| 1815 |
return error; |
| 1816 |
} |
| 1817 |
|
| 1818 |
/** Return next same record in index from a partition. |
| 1819 |
This routine is used to read the next record, but only if the key is |
| 1820 |
the same as supplied in the call. |
| 1821 |
@param[in] part Partition to read from. |
| 1822 |
@param[out] record Last record in index in the partition. |
| 1823 |
@param[in] key Key to match. |
| 1824 |
@param[in] length Length of key. |
| 1825 |
@return error number or 0. */ |
| 1826 |
int ha_innopart::index_next_same_in_part(uint part, uchar *record, |
| 1827 |
const uchar *key, uint length) { |
| 1828 |
int error; |
| 1829 |
|
| 1830 |
set_partition(part); |
| 1831 |
error = ha_innobase::index_next_same(record, key, length); |
| 1832 |
update_partition(part); |
| 1833 |
return (error); |
| 1834 |
} |
| 1835 |
|
| 1836 |
/** Return last record in index from a partition. |
| 1837 |
@param[in] part Partition to read from. |
| 1838 |
@param[out] record Last record in index in the partition. |
| 1839 |
@return error number or 0. */ |
| 1840 |
int ha_innopart::index_last_in_part(uint part, uchar *record) { |
| 1841 |
int error; |
| 1842 |
|
| 1843 |
set_partition(part); |
| 1844 |
error = ha_innobase::index_last(record); |
| 1845 |
update_partition(part); |
| 1846 |
return (error); |
| 1847 |
} |
| 1848 |
|
| 1849 |
/** Return previous record in index from a partition. |
| 1850 |
@param[in] part Partition to read from. |
| 1851 |
@param[out] record Last record in index in the partition. |
| 1852 |
@return error number or 0. */ |
| 1853 |
int ha_innopart::index_prev_in_part(uint part, uchar *record) { |
| 1854 |
int error; |
| 1855 |
|
| 1856 |
set_partition(part); |
| 1857 |
error = ha_innobase::index_prev(record); |
| 1858 |
update_partition(part); |
| 1859 |
|
| 1860 |
ut_ad(m_ordered_scan_ongoing || m_ordered_rec_buffer == nullptr || |
| 1861 |
m_prebuilt->used_in_HANDLER || |
| 1862 |
m_part_spec.start_part >= m_part_spec.end_part); |
| 1863 |
|
| 1864 |
return (error); |
| 1865 |
} |
| 1866 |
|
| 1867 |
/** Start index scan and return first record from a partition. |
| 1868 |
This routine starts an index scan using a start key. The calling |
| 1869 |
function will check the end key on its own. |
| 1870 |
@param[in] part Partition to read from. |
| 1871 |
@param[out] record First matching record in index in the partition. |
| 1872 |
@param[in] key Key to match. |
| 1873 |
@param[in] keypart_map Which part of the key to use. |
| 1874 |
@param[in] find_flag Key condition/direction to use. |
| 1875 |
@return error number or 0. */ |
| 1876 |
int ha_innopart::index_read_map_in_part(uint part, uchar *record, |
| 1877 |
const uchar *key, |
| 1878 |
key_part_map keypart_map, |
| 1879 |
enum ha_rkey_function find_flag) { |
| 1880 |
int error; |
| 1881 |
|
| 1882 |
set_partition(part); |
| 1883 |
error = ha_innobase::index_read_map(record, key, keypart_map, find_flag); |
| 1884 |
update_partition(part); |
| 1885 |
return (error); |
| 1886 |
} |
| 1887 |
|
| 1888 |
/** Start index scan and return first record from a partition. |
| 1889 |
This routine starts an index scan using a start key. The calling |
| 1890 |
function will check the end key on its own. |
| 1891 |
@param[in] part Partition to read from. |
| 1892 |
@param[out] record First matching record in index in the partition. |
| 1893 |
@param[in] index Index to read from. |
| 1894 |
@param[in] key Key to match. |
| 1895 |
@param[in] keypart_map Which part of the key to use. |
| 1896 |
@param[in] find_flag Key condition/direction to use. |
| 1897 |
@return error number or 0. */ |
| 1898 |
int ha_innopart::index_read_idx_map_in_part(uint part, uchar *record, |
| 1899 |
uint index, const uchar *key, |
| 1900 |
key_part_map keypart_map, |
| 1901 |
enum ha_rkey_function find_flag) { |
| 1902 |
int error; |
| 1903 |
|
| 1904 |
set_partition(part); |
| 1905 |
error = ha_innobase::index_read_idx_map(record, index, key, keypart_map, |
| 1906 |
find_flag); |
| 1907 |
update_partition(part); |
| 1908 |
return (error); |
| 1909 |
} |
| 1910 |
|
| 1911 |
/** Return last matching record in index from a partition. |
| 1912 |
@param[in] part Partition to read from. |
| 1913 |
@param[out] record Last matching record in index in the partition. |
| 1914 |
@param[in] key Key to match. |
| 1915 |
@param[in] keypart_map Which part of the key to use. |
| 1916 |
@return error number or 0. */ |
| 1917 |
int ha_innopart::index_read_last_map_in_part(uint part, uchar *record, |
| 1918 |
const uchar *key, |
| 1919 |
key_part_map keypart_map) { |
| 1920 |
int error; |
| 1921 |
set_partition(part); |
| 1922 |
error = ha_innobase::index_read_last_map(record, key, keypart_map); |
| 1923 |
update_partition(part); |
| 1924 |
return (error); |
| 1925 |
} |
| 1926 |
|
| 1927 |
int ha_innopart::read_range_first_in_part(uint part, uchar *record, |
| 1928 |
const key_range *, const key_range *, |
| 1929 |
bool) { |
| 1930 |
int error; |
| 1931 |
uchar *read_record = record; |
| 1932 |
set_partition(part); |
| 1933 |
if (read_record == nullptr) { |
| 1934 |
read_record = table->record[0]; |
| 1935 |
} |
| 1936 |
if (m_start_key.key != nullptr) { |
| 1937 |
error = ha_innobase::index_read(read_record, m_start_key.key, |
| 1938 |
m_start_key.length, m_start_key.flag); |
| 1939 |
} else { |
| 1940 |
error = ha_innobase::index_first(read_record); |
| 1941 |
} |
| 1942 |
if (error == HA_ERR_KEY_NOT_FOUND) { |
| 1943 |
error = HA_ERR_END_OF_FILE; |
| 1944 |
} else if (error == 0 && !in_range_check_pushed_down) { |
| 1945 |
/* compare_key uses table->record[0], so we |
| 1946 |
need to copy the data if not already there. */ |
| 1947 |
|
| 1948 |
if (record != nullptr) { |
| 1949 |
copy_cached_row(table->record[0], read_record); |
| 1950 |
} |
| 1951 |
if (compare_key(end_range) > 0) { |
| 1952 |
/* must use ha_innobase:: due to set/update_partition |
| 1953 |
could overwrite states if ha_innopart::unlock_row() |
| 1954 |
was used. */ |
| 1955 |
ha_innobase::unlock_row(); |
| 1956 |
error = HA_ERR_END_OF_FILE; |
| 1957 |
} |
| 1958 |
} |
| 1959 |
update_partition(part); |
| 1960 |
return (error); |
| 1961 |
} |
| 1962 |
|
| 1963 |
/** Return next record in index range scan from a partition. |
| 1964 |
@param[in] part Partition to read from. |
| 1965 |
@param[in,out] record First matching record in index in the partition, |
| 1966 |
if NULL use table->record[0] as return buffer. |
| 1967 |
@return error number or 0. */ |
| 1968 |
int ha_innopart::read_range_next_in_part(uint part, uchar *record) { |
| 1969 |
int error; |
| 1970 |
uchar *read_record = record; |
| 1971 |
|
| 1972 |
set_partition(part); |
| 1973 |
if (read_record == nullptr) { |
| 1974 |
read_record = table->record[0]; |
| 1975 |
} |
| 1976 |
|
| 1977 |
/* TODO: Implement ha_innobase::read_range*? |
| 1978 |
So it will return HA_ERR_END_OF_FILE or |
| 1979 |
HA_ERR_KEY_NOT_FOUND when passing end_range. */ |
| 1980 |
|
| 1981 |
error = ha_innobase::index_next(read_record); |
| 1982 |
if (error == 0 && !in_range_check_pushed_down) { |
| 1983 |
/* compare_key uses table->record[0], so we |
| 1984 |
need to copy the data if not already there. */ |
| 1985 |
|
| 1986 |
if (record != nullptr) { |
| 1987 |
copy_cached_row(table->record[0], read_record); |
| 1988 |
} |
| 1989 |
if (compare_key(end_range) > 0) { |
| 1990 |
/* must use ha_innobase:: due to set/update_partition |
| 1991 |
could overwrite states if ha_innopart::unlock_row() |
| 1992 |
was used. */ |
| 1993 |
ha_innobase::unlock_row(); |
| 1994 |
error = HA_ERR_END_OF_FILE; |
| 1995 |
} |
| 1996 |
} |
| 1997 |
update_partition(part); |
| 1998 |
|
| 1999 |
return (error); |
| 2000 |
} |
| 2001 |
|
| 2002 |
int ha_innopart::sample_init(void *&scan_ctx, double sampling_percentage, |
| 2003 |
int sampling_seed, |
| 2004 |
enum_sampling_method sampling_method, |
| 2005 |
const bool tablesample) { |
| 2006 |
assert(table_share->is_missing_primary_key() == |
| 2007 |
(bool)m_prebuilt->clust_index_was_generated); |
| 2008 |
|
| 2009 |
ut_ad(sampling_percentage >= 0.0); |
| 2010 |
ut_ad(sampling_percentage <= 100.0); |
| 2011 |
ut_ad(sampling_method == enum_sampling_method::SYSTEM); |
| 2012 |
|
| 2013 |
if (sampling_percentage <= 0.0 || sampling_percentage > 100.0 || |
| 2014 |
sampling_method != enum_sampling_method::SYSTEM) { |
| 2015 |
return 0; |
| 2016 |
} |
| 2017 |
|
| 2018 |
trx_t *trx{nullptr}; |
| 2019 |
|
| 2020 |
if (tablesample) { |
| 2021 |
update_thd(ha_thd()); |
| 2022 |
|
| 2023 |
trx = m_prebuilt->trx; |
| 2024 |
trx_start_if_not_started_xa(trx, false, UT_LOCATION_HERE); |
| 2025 |
|
| 2026 |
if (trx->isolation_level > TRX_ISO_READ_UNCOMMITTED) { |
| 2027 |
trx_assign_read_view(trx); |
| 2028 |
} |
| 2029 |
} |
| 2030 |
|
| 2031 |
/* Parallel read is not currently supported for sampling. */ |
| 2032 |
size_t max_threads = Parallel_reader::available_threads(1, false); |
| 2033 |
|
| 2034 |
if (max_threads == 0) { |
| 2035 |
return HA_ERR_SAMPLING_INIT_FAILED; |
| 2036 |
} |
| 2037 |
|
| 2038 |
Histogram_sampler *sampler = ut::new_withkey<Histogram_sampler>( |
| 2039 |
UT_NEW_THIS_FILE_PSI_KEY, max_threads, sampling_seed, sampling_percentage, |
| 2040 |
sampling_method); |
| 2041 |
|
| 2042 |
if (sampler == nullptr) { |
| 2043 |
Parallel_reader::release_threads(max_threads); |
| 2044 |
return HA_ERR_OUT_OF_MEM; |
| 2045 |
} |
| 2046 |
|
| 2047 |
scan_ctx = sampler; |
| 2048 |
|
| 2049 |
const auto first_used_partition = m_part_info->get_first_used_partition(); |
| 2050 |
|
| 2051 |
for (auto i = first_used_partition; i < m_tot_parts; |
| 2052 |
i = m_part_info->get_next_used_partition(i)) { |
| 2053 |
set_partition(i); |
| 2054 |
|
| 2055 |
if (dict_table_is_discarded(m_prebuilt->table)) { |
| 2056 |
ib_senderrf(ha_thd(), IB_LOG_LEVEL_ERROR, ER_TABLESPACE_DISCARDED, |
| 2057 |
m_prebuilt->table->name.m_name); |
| 2058 |
|
| 2059 |
return (HA_ERR_NO_SUCH_TABLE); |
| 2060 |
} |
| 2061 |
|
| 2062 |
build_template(true); |
| 2063 |
|
| 2064 |
auto index = m_prebuilt->table->first_index(); |
| 2065 |
|
| 2066 |
auto success = sampler->init(trx, index, m_prebuilt); |
| 2067 |
|
| 2068 |
if (!success) { |
| 2069 |
return (HA_ERR_SAMPLING_INIT_FAILED); |
| 2070 |
} |
| 2071 |
} |
| 2072 |
|
| 2073 |
dberr_t db_err = sampler->run(); |
| 2074 |
|
| 2075 |
if (db_err != DB_SUCCESS) { |
| 2076 |
return (convert_error_code_to_mysql(db_err, 0, ha_thd())); |
| 2077 |
} |
| 2078 |
|
| 2079 |
return (0); |
| 2080 |
} |
| 2081 |
|
| 2082 |
int ha_innopart::sample_next(void *scan_ctx, uchar *buf) { |
| 2083 |
dberr_t err = DB_SUCCESS; |
| 2084 |
|
| 2085 |
auto sampler = static_cast<Histogram_sampler *>(scan_ctx); |
| 2086 |
|
| 2087 |
sampler->set(buf); |
| 2088 |
|
| 2089 |
/** Buffer rows one by one */ |
| 2090 |
err = sampler->buffer_next(); |
| 2091 |
|
| 2092 |
if (err == DB_END_OF_INDEX) { |
| 2093 |
return HA_ERR_END_OF_FILE; |
| 2094 |
} |
| 2095 |
|
| 2096 |
return (convert_error_code_to_mysql(err, 0, ha_thd())); |
| 2097 |
} |
| 2098 |
|
| 2099 |
int ha_innopart::sample_end(void *scan_ctx) { |
| 2100 |
auto sampler = static_cast<Histogram_sampler *>(scan_ctx); |
| 2101 |
ut::delete_(sampler); |
| 2102 |
|
| 2103 |
return 0; |
| 2104 |
} |
| 2105 |
|
| 2106 |
/** Initialize random read/scan of a specific partition. |
| 2107 |
@param[in] part_id Partition to initialize. |
| 2108 |
@param[in] scan True for scan else random access. |
| 2109 |
@return error number or 0. */ |
| 2110 |
int ha_innopart::rnd_init_in_part(uint part_id, bool scan) { |
| 2111 |
DBUG_TRACE; |
| 2112 |
assert(table_share->is_missing_primary_key() == |
| 2113 |
(bool)m_prebuilt->clust_index_was_generated); |
| 2114 |
|
| 2115 |
int err = change_active_index(part_id, table_share->primary_key); |
| 2116 |
|
| 2117 |
/* Don't use semi-consistent read in random row reads (by position). |
| 2118 |
This means we must disable semi_consistent_read if scan is false. */ |
| 2119 |
|
| 2120 |
if (!scan) { |
| 2121 |
m_prebuilt->row_read_type = ROW_READ_WITH_LOCKS; |
| 2122 |
} |
| 2123 |
|
| 2124 |
m_start_of_scan = true; |
| 2125 |
return err; |
| 2126 |
} |
| 2127 |
|
| 2128 |
int ha_innopart::rnd_end_in_part(uint, bool) { return (index_end()); } |
| 2129 |
|
| 2130 |
/** Get next row during scan of a specific partition. |
| 2131 |
Also used to read the FIRST row in a table scan. |
| 2132 |
@param[in] part_id Partition to read from. |
| 2133 |
@param[out] buf Next row. |
| 2134 |
@return error number or 0. */ |
| 2135 |
int ha_innopart::rnd_next_in_part(uint part_id, uchar *buf) { |
| 2136 |
int error; |
| 2137 |
|
| 2138 |
DBUG_TRACE; |
| 2139 |
|
| 2140 |
set_partition(part_id); |
| 2141 |
if (m_start_of_scan) { |
| 2142 |
error = ha_innobase::index_first(buf); |
| 2143 |
|
| 2144 |
if (error == HA_ERR_KEY_NOT_FOUND) { |
| 2145 |
error = HA_ERR_END_OF_FILE; |
| 2146 |
} |
| 2147 |
m_start_of_scan = false; |
| 2148 |
} else { |
| 2149 |
ha_statistic_increment(&System_status_var::ha_read_rnd_next_count); |
| 2150 |
error = ha_innobase::general_fetch(buf, ROW_SEL_NEXT, 0); |
| 2151 |
} |
| 2152 |
|
| 2153 |
update_partition(part_id); |
| 2154 |
return error; |
| 2155 |
} |
| 2156 |
|
| 2157 |
/** Get a row from a position. |
| 2158 |
Fetches a row from the table based on a row reference. |
| 2159 |
@param[out] buf Returns the row in this buffer, in MySQL format. |
| 2160 |
@param[in] pos Position, given as primary key value or DB_ROW_ID |
| 2161 |
(if no primary key) of the row in MySQL format. The length of data in pos has |
| 2162 |
to be ref_length. |
| 2163 |
@return 0, HA_ERR_KEY_NOT_FOUND or error code. */ |
| 2164 |
int ha_innopart::rnd_pos(uchar *buf, uchar *pos) { |
| 2165 |
int error; |
| 2166 |
uint part_id; |
| 2167 |
DBUG_TRACE; |
| 2168 |
static_assert(PARTITION_BYTES_IN_POS == 2); |
| 2169 |
DBUG_DUMP("pos", pos, ref_length); |
| 2170 |
|
| 2171 |
ha_statistic_increment(&System_status_var::ha_read_rnd_count); |
| 2172 |
|
| 2173 |
ut_ad(m_prebuilt->trx == thd_to_trx(ha_thd())); |
| 2174 |
|
| 2175 |
/* Restore used partition. */ |
| 2176 |
part_id = uint2korr(pos); |
| 2177 |
|
| 2178 |
set_partition(part_id); |
| 2179 |
|
| 2180 |
/* Note that we assume the length of the row reference is fixed |
| 2181 |
for the table, and it is == ref_length. */ |
| 2182 |
|
| 2183 |
error = ha_innobase::index_read(buf, pos + PARTITION_BYTES_IN_POS, |
| 2184 |
ref_length - PARTITION_BYTES_IN_POS, |
| 2185 |
HA_READ_KEY_EXACT); |
| 2186 |
DBUG_PRINT("info", ("part %u index_read returned %d", part_id, error)); |
| 2187 |
DBUG_DUMP("buf", buf, table_share->reclength); |
| 2188 |
|
| 2189 |
update_partition(part_id); |
| 2190 |
|
| 2191 |
return error; |
| 2192 |
} |
| 2193 |
|
| 2194 |
/** Return position for cursor in last used partition. |
| 2195 |
Stores a reference to the current row to 'ref' field of the handle. Note |
| 2196 |
that in the case where we have generated the clustered index for the |
| 2197 |
table, the function parameter is illogical: we MUST ASSUME that 'record' |
| 2198 |
is the current 'position' of the handle, because if row ref is actually |
| 2199 |
the row id internally generated in InnoDB, then 'record' does not contain |
| 2200 |
it. We just guess that the row id must be for the record where the handle |
| 2201 |
was positioned the last time. |
| 2202 |
@param[out] ref_arg Pointer to buffer where to write the position. |
| 2203 |
@param[in] record Record to position for. */ |
| 2204 |
void ha_innopart::position_in_last_part(uchar *ref_arg, const uchar *record) { |
| 2205 |
DBUG_TRACE; |
| 2206 |
assert(table_share->is_missing_primary_key() == |
| 2207 |
(bool)m_prebuilt->clust_index_was_generated); |
| 2208 |
|
| 2209 |
if (m_prebuilt->clust_index_was_generated) { |
| 2210 |
/* No primary key was defined for the table and we |
| 2211 |
generated the clustered index from row id: the |
| 2212 |
row reference will be the row id, not any key value |
| 2213 |
that MySQL knows of. */ |
| 2214 |
|
| 2215 |
memcpy(ref_arg, m_prebuilt->row_id, DATA_ROW_ID_LEN); |
| 2216 |
} else { |
| 2217 |
/* Copy primary key as the row reference */ |
| 2218 |
KEY *key_info = table->key_info + table_share->primary_key; |
| 2219 |
key_copy(ref_arg, (uchar *)record, key_info, key_info->key_length); |
| 2220 |
} |
| 2221 |
} |
| 2222 |
|
| 2223 |
/** Fill in data_dir_path and tablespace name from internal data dictionary. |
| 2224 |
@param[in,out] part_elem Partition element to fill. |
| 2225 |
@param[in] ib_table InnoDB table to copy from. |
| 2226 |
@param[in] display_tablespace Display tablespace name if set. */ |
| 2227 |
void ha_innopart::update_part_elem(partition_element *part_elem, |
| 2228 |
dict_table_t *ib_table, |
| 2229 |
bool display_tablespace) { |
| 2230 |
dd_get_and_save_data_dir_path<dd::Partition>(ib_table, nullptr, false); |
| 2231 |
if (ib_table->data_dir_path != nullptr) { |
| 2232 |
if (part_elem->data_file_name == nullptr || |
| 2233 |
strcmp(ib_table->data_dir_path, part_elem->data_file_name) != 0) { |
| 2234 |
/* Play safe and allocate memory from TABLE and copy |
| 2235 |
instead of expose the internal data dictionary. */ |
| 2236 |
part_elem->data_file_name = |
| 2237 |
strdup_root(&table->mem_root, ib_table->data_dir_path); |
| 2238 |
} |
| 2239 |
} else { |
| 2240 |
part_elem->data_file_name = nullptr; |
| 2241 |
} |
| 2242 |
|
| 2243 |
part_elem->index_file_name = nullptr; |
| 2244 |
dict_get_and_save_space_name(ib_table); |
| 2245 |
if (ib_table->tablespace != nullptr) { |
| 2246 |
ut_ad(part_elem->tablespace_name == nullptr || |
| 2247 |
0 == strcmp(part_elem->tablespace_name, ib_table->tablespace)); |
| 2248 |
if (part_elem->tablespace_name == nullptr || |
| 2249 |
strcmp(ib_table->tablespace, part_elem->tablespace_name) != 0) { |
| 2250 |
/* Play safe and allocate memory from TABLE and copy |
| 2251 |
instead of expose the internal data dictionary. */ |
| 2252 |
part_elem->tablespace_name = |
| 2253 |
strdup_root(&table->mem_root, ib_table->tablespace); |
| 2254 |
} |
| 2255 |
} else { |
| 2256 |
const char *tablespace_name = ib_table->space == 0 |
| 2257 |
? dict_sys_t::s_sys_space_name |
| 2258 |
: dict_sys_t::s_file_per_table_name; |
| 2259 |
|
| 2260 |
if (part_elem->tablespace_name != nullptr) { |
| 2261 |
if (0 != strcmp(part_elem->tablespace_name, tablespace_name)) { |
| 2262 |
/* Update part_elem tablespace to NULL same |
| 2263 |
as in innodb data dictionary ib_table. */ |
| 2264 |
part_elem->tablespace_name = nullptr; |
| 2265 |
} |
| 2266 |
} else if (display_tablespace) { |
| 2267 |
/* Update tablespace values so that SHOW CREATE TABLE |
| 2268 |
will display TABLESPACE=name for the partition when |
| 2269 |
appropriate, if it's not mentioned explicitly during |
| 2270 |
table creation. */ |
| 2271 |
part_elem->tablespace_name = |
| 2272 |
strdup_root(&table->mem_root, tablespace_name); |
| 2273 |
} |
| 2274 |
} |
| 2275 |
} |
| 2276 |
|
| 2277 |
/** Update create_info. |
| 2278 |
Used in SHOW CREATE TABLE et al. |
| 2279 |
@param[in,out] create_info Create info to update. */ |
| 2280 |
void ha_innopart::update_create_info(HA_CREATE_INFO *create_info) { |
| 2281 |
uint num_subparts = m_part_info->num_subparts; |
| 2282 |
uint num_parts; |
| 2283 |
uint part; |
| 2284 |
bool display_tablespace = false; |
| 2285 |
List_iterator<partition_element> part_it(m_part_info->partitions); |
| 2286 |
partition_element *part_elem; |
| 2287 |
partition_element *sub_elem; |
| 2288 |
DBUG_TRACE; |
| 2289 |
if ((create_info->used_fields & HA_CREATE_USED_AUTO) == 0) { |
| 2290 |
info(HA_STATUS_AUTO); |
| 2291 |
create_info->auto_increment_value = stats.auto_increment_value; |
| 2292 |
} |
| 2293 |
|
| 2294 |
num_parts = (num_subparts != 0) ? m_tot_parts / num_subparts : m_tot_parts; |
| 2295 |
|
| 2296 |
/* DATA/INDEX DIRECTORY are never applied to the whole partitioned |
| 2297 |
table, only to its parts. */ |
| 2298 |
|
| 2299 |
create_info->data_file_name = nullptr; |
| 2300 |
create_info->index_file_name = nullptr; |
| 2301 |
|
| 2302 |
/* Since update_create_info() can be called from |
| 2303 |
mysql_prepare_alter_table() when not all partitions are set up, |
| 2304 |
we look for that condition first. |
| 2305 |
If all partitions are not available then simply return, |
| 2306 |
since it does not need any updated partitioning info. */ |
| 2307 |
|
| 2308 |
if (!m_part_info->temp_partitions.is_empty()) { |
| 2309 |
return; |
| 2310 |
} |
| 2311 |
part = 0; |
| 2312 |
for (part_elem = part_it++; part_elem != nullptr; |
| 2313 |
part_elem = part_it++, part++) { |
| 2314 |
if (part >= num_parts) { |
| 2315 |
return; |
| 2316 |
} |
| 2317 |
if (m_part_info->is_sub_partitioned()) { |
| 2318 |
List_iterator<partition_element> subpart_it(part_elem->subpartitions); |
| 2319 |
uint subpart = 0; |
| 2320 |
for (sub_elem = subpart_it++; sub_elem != nullptr; |
| 2321 |
sub_elem = subpart_it++) { |
| 2322 |
if (subpart >= num_subparts) { |
| 2323 |
return; |
| 2324 |
} |
| 2325 |
auto table_part = |
| 2326 |
m_part_share->get_table_part(part * num_subparts + subpart); |
| 2327 |
|
| 2328 |
if (sub_elem->tablespace_name != nullptr || |
| 2329 |
table_part->tablespace != nullptr || table_part->space == 0) { |
| 2330 |
display_tablespace = true; |
| 2331 |
} |
| 2332 |
subpart++; |
| 2333 |
} |
| 2334 |
if (subpart != num_subparts) { |
| 2335 |
return; |
| 2336 |
} |
| 2337 |
} else { |
| 2338 |
auto table_part = m_part_share->get_table_part(part); |
| 2339 |
|
| 2340 |
if (table_part->space == 0 || table_part->tablespace != nullptr || |
| 2341 |
part_elem->tablespace_name != nullptr) { |
| 2342 |
display_tablespace = true; |
| 2343 |
} |
| 2344 |
} |
| 2345 |
} |
| 2346 |
if (part != num_parts) { |
| 2347 |
return; |
| 2348 |
} |
| 2349 |
|
| 2350 |
/* part_elem->data_file_name and tablespace_name should be correct from |
| 2351 |
the .frm, but may have been changed, so update from SYS_DATAFILES. |
| 2352 |
index_file_name is ignored, so remove it. */ |
| 2353 |
|
| 2354 |
part = 0; |
| 2355 |
part_it.rewind(); |
| 2356 |
for (part_elem = part_it++; part_elem != nullptr; part_elem = part_it++) { |
| 2357 |
if (m_part_info->is_sub_partitioned()) { |
| 2358 |
List_iterator<partition_element> subpart_it(part_elem->subpartitions); |
| 2359 |
for (sub_elem = subpart_it++; sub_elem != nullptr; |
| 2360 |
sub_elem = subpart_it++) { |
| 2361 |
auto table_part = m_part_share->get_table_part(part++); |
| 2362 |
update_part_elem(sub_elem, table_part, display_tablespace); |
| 2363 |
} |
| 2364 |
} else { |
| 2365 |
auto table_part = m_part_share->get_table_part(part++); |
| 2366 |
update_part_elem(part_elem, table_part, display_tablespace); |
| 2367 |
} |
| 2368 |
} |
| 2369 |
} |
| 2370 |
|
| 2371 |
/** Set flags and append '/' to remote path if necessary. */ |
| 2372 |
void create_table_info_t::set_remote_path_flags() { |
| 2373 |
if (m_remote_path[0] != '\0') { |
| 2374 |
ut_ad(DICT_TF_HAS_DATA_DIR(m_flags) != 0); |
| 2375 |
|
| 2376 |
/* os_file_make_remote_pathname will truncate |
| 2377 |
everything after the last '/', so append '/' |
| 2378 |
if it is not the last character. */ |
| 2379 |
|
| 2380 |
size_t len = strlen(m_remote_path); |
| 2381 |
if (m_remote_path[len - 1] != OS_PATH_SEPARATOR) { |
| 2382 |
m_remote_path[len] = OS_PATH_SEPARATOR; |
| 2383 |
m_remote_path[len + 1] = '\0'; |
| 2384 |
} |
| 2385 |
} else { |
| 2386 |
ut_ad(DICT_TF_HAS_DATA_DIR(m_flags) == 0); |
| 2387 |
} |
| 2388 |
} |
| 2389 |
|
| 2390 |
/** Creates a new table to an InnoDB database. |
| 2391 |
@param[in] name Table name (in filesystem charset). |
| 2392 |
@param[in] form MySQL Table containing information of |
| 2393 |
partitions, columns and indexes etc. |
| 2394 |
@param[in] create_info Additional create information, like |
| 2395 |
create statement string. |
| 2396 |
@param[in,out] table_def dd::Table object for table to be created. |
| 2397 |
Can be adjusted by this call. Changes to the table definition will be |
| 2398 |
persisted in the data-dictionary at statement commit time. |
| 2399 |
@return 0 or error number. */ |
| 2400 |
int ha_innopart::create(const char *name, TABLE *form, |
| 2401 |
HA_CREATE_INFO *create_info, dd::Table *table_def) { |
| 2402 |
int error; |
| 2403 |
/** {database}/{tablename} */ |
| 2404 |
char table_name[FN_REFLEN]; |
| 2405 |
/** absolute path of table */ |
| 2406 |
char remote_path[FN_REFLEN]; |
| 2407 |
char tablespace_name[NAME_LEN + 1]; |
| 2408 |
char table_data_file_name[FN_REFLEN]; |
| 2409 |
char table_level_tablespace_name[NAME_LEN + 1]; |
| 2410 |
const char *table_index_file_name; |
| 2411 |
uint created = 0; |
| 2412 |
THD *thd = ha_thd(); |
| 2413 |
trx_t *trx; |
| 2414 |
|
| 2415 |
if (thd_sql_command(thd) == SQLCOM_TRUNCATE) { |
| 2416 |
return (truncate_impl(name, form, table_def)); |
| 2417 |
} |
| 2418 |
|
| 2419 |
if (high_level_read_only) { |
| 2420 |
return (HA_ERR_INNODB_READ_ONLY); |
| 2421 |
} |
| 2422 |
|
| 2423 |
trx = check_trx_exists(thd); |
| 2424 |
|
| 2425 |
DBUG_TRACE; |
| 2426 |
|
| 2427 |
if (is_shared_tablespace(create_info->tablespace)) { |
| 2428 |
my_printf_error(ER_ILLEGAL_HA_CREATE_OPTION, PARTITION_IN_SHARED_TABLESPACE, |
| 2429 |
MYF(0)); |
| 2430 |
return HA_ERR_INTERNAL_ERROR; |
| 2431 |
} |
| 2432 |
|
| 2433 |
create_table_info_t info(thd, form, create_info, table_name, remote_path, |
| 2434 |
tablespace_name, srv_file_per_table, false, 0, 0, |
| 2435 |
true); |
| 2436 |
|
| 2437 |
ut_ad(create_info != nullptr); |
| 2438 |
ut_ad(m_part_info == form->part_info); |
| 2439 |
ut_ad(table_share != nullptr); |
| 2440 |
|
| 2441 |
/* Not allowed to create temporary partitioned tables. */ |
| 2442 |
if (create_info != nullptr && |
| 2443 |
(create_info->options & HA_LEX_CREATE_TMP_TABLE) != 0) { |
| 2444 |
my_error(ER_PARTITION_NO_TEMPORARY, MYF(0)); |
| 2445 |
ut_d(ut_error); // Can we support partitioned temporary tables? |
| 2446 |
ut_o(return HA_ERR_INTERNAL_ERROR); |
| 2447 |
} |
| 2448 |
|
| 2449 |
innobase_register_trx(ht, thd, trx); |
| 2450 |
|
| 2451 |
if (form->found_next_number_field) { |
| 2452 |
dd_set_autoinc(table_def->se_private_data(), |
| 2453 |
create_info->auto_increment_value); |
| 2454 |
} |
| 2455 |
|
| 2456 |
error = info.initialize(); |
| 2457 |
if (error != 0) { |
| 2458 |
return error; |
| 2459 |
} |
| 2460 |
|
| 2461 |
/* Setup and check table level options. */ |
| 2462 |
error = info.prepare_create_table(name); |
| 2463 |
if (error != 0) { |
| 2464 |
return error; |
| 2465 |
} |
| 2466 |
|
| 2467 |
/* Save the original table name before adding partition information. */ |
| 2468 |
const std::string saved_table_name(table_name); |
| 2469 |
|
| 2470 |
if (create_info->data_file_name != nullptr) { |
| 2471 |
/* Strip the tablename from the path. */ |
| 2472 |
strncpy(table_data_file_name, create_info->data_file_name, FN_REFLEN - 1); |
| 2473 |
table_data_file_name[FN_REFLEN - 1] = '\0'; |
| 2474 |
char *ptr = strrchr(table_data_file_name, OS_PATH_SEPARATOR); |
| 2475 |
ut_ad(ptr != nullptr); |
| 2476 |
if (ptr != nullptr) { |
| 2477 |
ptr++; |
| 2478 |
*ptr = '\0'; |
| 2479 |
create_info->data_file_name = table_data_file_name; |
| 2480 |
} |
| 2481 |
} else { |
| 2482 |
table_data_file_name[0] = '\0'; |
| 2483 |
} |
| 2484 |
table_index_file_name = create_info->index_file_name; |
| 2485 |
if (create_info->tablespace != nullptr) { |
| 2486 |
strcpy(table_level_tablespace_name, create_info->tablespace); |
| 2487 |
} else { |
| 2488 |
table_level_tablespace_name[0] = '\0'; |
| 2489 |
} |
| 2490 |
|
| 2491 |
/* It's also doable to get tablespace names by accessing |
| 2492 |
dd::Tablespace::name according to dd_part->tablespace_id(). |
| 2493 |
However, it costs more. So as long as partition_element contains |
| 2494 |
tablespace name, it's easier to check it */ |
| 2495 |
std::vector<const char *> tablespace_names; |
| 2496 |
List_iterator_fast<partition_element> part_it(form->part_info->partitions); |
| 2497 |
partition_element *part_elem; |
| 2498 |
for (part_elem = part_it++; part_elem != nullptr; part_elem = part_it++) { |
| 2499 |
const char *tablespace; |
| 2500 |
if (form->part_info->is_sub_partitioned()) { |
| 2501 |
List_iterator_fast<partition_element> sub_it(part_elem->subpartitions); |
| 2502 |
partition_element *sub_elem; |
| 2503 |
for (sub_elem = sub_it++; sub_elem != nullptr; sub_elem = sub_it++) { |
| 2504 |
tablespace = partition_get_tablespace(table_level_tablespace_name, |
| 2505 |
part_elem, sub_elem); |
| 2506 |
if (is_shared_tablespace(tablespace)) { |
| 2507 |
tablespace_names.clear(); |
| 2508 |
error = HA_ERR_INTERNAL_ERROR; |
| 2509 |
break; |
| 2510 |
} |
| 2511 |
tablespace_names.push_back(tablespace); |
| 2512 |
} |
| 2513 |
} else { |
| 2514 |
tablespace = partition_get_tablespace(table_level_tablespace_name, |
| 2515 |
part_elem, nullptr); |
| 2516 |
if (is_shared_tablespace(tablespace)) { |
| 2517 |
tablespace_names.clear(); |
| 2518 |
error = HA_ERR_INTERNAL_ERROR; |
| 2519 |
break; |
| 2520 |
} |
| 2521 |
tablespace_names.push_back(tablespace); |
| 2522 |
} |
| 2523 |
} |
| 2524 |
|
| 2525 |
if (error) { |
| 2526 |
my_printf_error(ER_ILLEGAL_HA_CREATE_OPTION, PARTITION_IN_SHARED_TABLESPACE, |
| 2527 |
MYF(0)); |
| 2528 |
return error; |
| 2529 |
} |
| 2530 |
|
| 2531 |
for (const auto dd_part : *table_def->leaf_partitions()) { |
| 2532 |
std::string partition; |
| 2533 |
/* Build the partition name. */ |
| 2534 |
dict_name::build_partition(dd_part, partition); |
| 2535 |
|
| 2536 |
std::string part_table; |
| 2537 |
/* Build the partitioned table name. */ |
| 2538 |
dict_name::build_table("", saved_table_name, partition, false, false, |
| 2539 |
part_table); |
| 2540 |
|
| 2541 |
if (part_table.length() + 1 >= FN_REFLEN - 1) { |
| 2542 |
error = HA_ERR_INTERNAL_ERROR; |
| 2543 |
my_error(ER_PATH_LENGTH, MYF(0), part_table.c_str()); |
| 2544 |
break; |
| 2545 |
} |
| 2546 |
|
| 2547 |
const dd::Properties &options = dd_part->options(); |
| 2548 |
dd::String_type index_file_name; |
| 2549 |
dd::String_type data_file_name; |
| 2550 |
const char *tablespace_name; |
| 2551 |
|
| 2552 |
if (options.exists(index_file_name_key)) |
| 2553 |
(void)options.get(index_file_name_key, &index_file_name); |
| 2554 |
if (options.exists(data_file_name_key)) |
| 2555 |
(void)options.get(data_file_name_key, &data_file_name); |
| 2556 |
ut_ad(created < tablespace_names.size()); |
| 2557 |
tablespace_name = tablespace_names[created]; |
| 2558 |
|
| 2559 |
if (!data_file_name.empty()) { |
| 2560 |
create_info->data_file_name = data_file_name.c_str(); |
| 2561 |
} |
| 2562 |
|
| 2563 |
if (!index_file_name.empty()) { |
| 2564 |
create_info->index_file_name = index_file_name.c_str(); |
| 2565 |
} |
| 2566 |
|
| 2567 |
if (!data_file_name.empty() && |
| 2568 |
dd_part->tablespace_id() == dd::INVALID_OBJECT_ID && |
| 2569 |
(tablespace_name == nullptr || |
| 2570 |
strcmp(tablespace_name, dict_sys_t::s_file_per_table_name) != 0)) { |
| 2571 |
create_info->tablespace = nullptr; |
| 2572 |
} else { |
| 2573 |
create_info->tablespace = tablespace_name; |
| 2574 |
} |
| 2575 |
info.flags_reset(); |
| 2576 |
info.flags2_reset(); |
| 2577 |
|
| 2578 |
if ((error = info.prepare_create_table(part_table.c_str())) != 0) { |
| 2579 |
break; |
| 2580 |
} |
| 2581 |
|
| 2582 |
info.set_remote_path_flags(); |
| 2583 |
|
| 2584 |
if ((error = info.create_table(&dd_part->table(), nullptr)) != 0) { |
| 2585 |
break; |
| 2586 |
} |
| 2587 |
|
| 2588 |
if ((error = info.create_table_update_global_dd<dd::Partition>( |
| 2589 |
const_cast<dd::Partition *>(dd_part))) != 0) { |
| 2590 |
break; |
| 2591 |
} |
| 2592 |
|
| 2593 |
if ((error = info.create_table_update_dict()) != 0) { |
| 2594 |
break; |
| 2595 |
} |
| 2596 |
|
| 2597 |
info.detach(); |
| 2598 |
|
| 2599 |
++created; |
| 2600 |
create_info->data_file_name = table_data_file_name; |
| 2601 |
create_info->index_file_name = table_index_file_name; |
| 2602 |
create_info->tablespace = table_level_tablespace_name; |
| 2603 |
} |
| 2604 |
|
| 2605 |
create_info->data_file_name = nullptr; |
| 2606 |
create_info->index_file_name = nullptr; |
| 2607 |
create_info->tablespace = nullptr; |
| 2608 |
|
| 2609 |
return error; |
| 2610 |
} |
| 2611 |
|
| 2612 |
/** Drop a table. |
| 2613 |
@param[in] name table name |
| 2614 |
@param[in,out] dd_table data dictionary table |
| 2615 |
@return error number |
| 2616 |
@retval 0 on success */ |
| 2617 |
int ha_innopart::delete_table(const char *name, const dd::Table *dd_table) { |
| 2618 |
THD *thd = ha_thd(); |
| 2619 |
trx_t *trx = check_trx_exists(thd); |
| 2620 |
char norm_name[FN_REFLEN]; |
| 2621 |
int error = 0; |
| 2622 |
|
| 2623 |
DBUG_TRACE; |
| 2624 |
|
| 2625 |
ut_ad(dd_table != nullptr); |
| 2626 |
ut_ad(dd_table_is_partitioned(*dd_table)); |
| 2627 |
ut_ad(dd_table->is_persistent()); |
| 2628 |
|
| 2629 |
if (high_level_read_only) { |
| 2630 |
return HA_ERR_TABLE_READONLY; |
| 2631 |
} |
| 2632 |
|
| 2633 |
if (!normalize_table_name(norm_name, name)) { |
| 2634 |
return (HA_ERR_TOO_LONG_PATH); |
| 2635 |
} |
| 2636 |
|
| 2637 |
innobase_register_trx(ht, thd, trx); |
| 2638 |
TrxInInnoDB trx_in_innodb(trx); |
| 2639 |
|
| 2640 |
dd::cache::Dictionary_client *client = dd::get_dd_client(thd); |
| 2641 |
dd::cache::Dictionary_client::Auto_releaser releaser(client); |
| 2642 |
|
| 2643 |
TABLE_SHARE ts; |
| 2644 |
TABLE td; |
| 2645 |
error = acquire_uncached_table(thd, client, dd_table, norm_name, &ts, &td); |
| 2646 |
if (error != 0) { |
| 2647 |
return (error); |
| 2648 |
} |
| 2649 |
|
| 2650 |
for (const dd::Partition *dd_part : dd_table->leaf_partitions()) { |
| 2651 |
std::string partition; |
| 2652 |
/* Build the partition name. */ |
| 2653 |
dict_name::build_partition(dd_part, partition); |
| 2654 |
|
| 2655 |
std::string part_table; |
| 2656 |
/* Build the partitioned table name. */ |
| 2657 |
dict_name::build_table("", name, partition, false, false, part_table); |
| 2658 |
|
| 2659 |
if (part_table.length() >= FN_REFLEN) { |
| 2660 |
ut_d(ut_error); |
| 2661 |
ut_o(release_uncached_table(&ts, &td)); |
| 2662 |
ut_o(return HA_ERR_INTERNAL_ERROR); |
| 2663 |
} |
| 2664 |
|
| 2665 |
const char *partition_name = part_table.c_str(); |
| 2666 |
|
| 2667 |
error = innobase_basic_ddl::delete_impl(thd, partition_name, dd_part, &td); |
| 2668 |
|
| 2669 |
if (error != 0) { |
| 2670 |
break; |
| 2671 |
} |
| 2672 |
} |
| 2673 |
release_uncached_table(&ts, &td); |
| 2674 |
return error; |
| 2675 |
} |
| 2676 |
|
| 2677 |
/** Rename a table. |
| 2678 |
@param[in] from table name before rename |
| 2679 |
@param[in] to table name after rename |
| 2680 |
@param[in] from_table data dictionary table before rename |
| 2681 |
@param[in,out] to_table data dictionary table after rename |
| 2682 |
@return error number |
| 2683 |
@retval 0 on success */ |
| 2684 |
int ha_innopart::rename_table(const char *from, const char *to, |
| 2685 |
const dd::Table *from_table, |
| 2686 |
dd::Table *to_table) { |
| 2687 |
THD *thd = ha_thd(); |
| 2688 |
char norm_from[FN_REFLEN]; |
| 2689 |
char norm_to[FN_REFLEN]; |
| 2690 |
int error = 0; |
| 2691 |
|
| 2692 |
DBUG_TRACE; |
| 2693 |
|
| 2694 |
ut_ad(from_table != nullptr); |
| 2695 |
ut_ad(to_table != nullptr); |
| 2696 |
ut_ad(from_table->se_private_id() == to_table->se_private_id()); |
| 2697 |
ut_ad(from_table->se_private_data().raw_string() == |
| 2698 |
to_table->se_private_data().raw_string()); |
| 2699 |
ut_ad(from_table->partition_type() == to_table->partition_type()); |
| 2700 |
|
| 2701 |
if (high_level_read_only) { |
| 2702 |
ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_READ_ONLY_MODE); |
| 2703 |
return HA_ERR_TABLE_READONLY; |
| 2704 |
} |
| 2705 |
|
| 2706 |
if (!normalize_table_name(norm_from, from) || |
| 2707 |
!normalize_table_name(norm_to, to)) { |
| 2708 |
return (HA_ERR_TOO_LONG_PATH); |
| 2709 |
} |
| 2710 |
|
| 2711 |
/* Get the transaction associated with the current thd, or create one |
| 2712 |
if not yet created */ |
| 2713 |
trx_t *trx = check_trx_exists(thd); |
| 2714 |
|
| 2715 |
trx_start_if_not_started(trx, false, UT_LOCATION_HERE); |
| 2716 |
innobase_register_trx(ht, thd, trx); |
| 2717 |
|
| 2718 |
TrxInInnoDB trx_in_innodb(trx); |
| 2719 |
dd::cache::Dictionary_client *client = dd::get_dd_client(thd); |
| 2720 |
dd::cache::Dictionary_client::Auto_releaser releaser(client); |
| 2721 |
|
| 2722 |
TABLE_SHARE ts; |
| 2723 |
TABLE td; |
| 2724 |
error = acquire_uncached_table(thd, client, from_table, norm_from, &ts, &td); |
| 2725 |
if (error != 0) { |
| 2726 |
return (error); |
| 2727 |
} |
| 2728 |
|
| 2729 |
auto to_part = to_table->leaf_partitions()->begin(); |
| 2730 |
|
| 2731 |
for (const auto from_part : from_table->leaf_partitions()) { |
| 2732 |
ut_ad((*to_part) != NULL); |
| 2733 |
|
| 2734 |
std::string partition; |
| 2735 |
/* Build the old partition name. */ |
| 2736 |
dict_name::build_partition(from_part, partition); |
| 2737 |
|
| 2738 |
/* Build the old partitioned table name. */ |
| 2739 |
std::string from_name; |
| 2740 |
dict_name::build_table("", from, partition, false, false, from_name); |
| 2741 |
|
| 2742 |
/* Build the new partition name. */ |
| 2743 |
dict_name::build_partition(*to_part, partition); |
| 2744 |
|
| 2745 |
/* Build the new partitioned table name. */ |
| 2746 |
std::string to_name; |
| 2747 |
dict_name::build_table("", to, partition, false, false, to_name); |
| 2748 |
|
| 2749 |
if (from_name.length() >= FN_REFLEN || to_name.length() >= FN_REFLEN) { |
| 2750 |
ut_d(ut_error); |
| 2751 |
ut_o(return HA_ERR_INTERNAL_ERROR); |
| 2752 |
} |
| 2753 |
error = innobase_basic_ddl::rename_impl( |
| 2754 |
thd, from_name.c_str(), to_name.c_str(), from_part, *to_part, &td); |
| 2755 |
if (error != 0) { |
| 2756 |
break; |
| 2757 |
} |
| 2758 |
|
| 2759 |
++to_part; |
| 2760 |
} |
| 2761 |
release_uncached_table(&ts, &td); |
| 2762 |
return error; |
| 2763 |
} |
| 2764 |
|
| 2765 |
/** Set DD discard attribute for tablespace. |
| 2766 |
@param[in] table_def dd table |
| 2767 |
@param[in] discard True if this table is discarded |
| 2768 |
@return 0 or error number. */ |
| 2769 |
int ha_innopart::set_dd_discard_attribute(dd::Table *table_def, bool discard) { |
| 2770 |
dict_table_t *table; |
| 2771 |
uint i = 0; |
| 2772 |
int error = 0; |
| 2773 |
|
| 2774 |
DBUG_TRACE; |
| 2775 |
|
| 2776 |
for (dd::Partition *dd_part : *table_def->leaf_partitions()) { |
| 2777 |
if (!m_part_info->is_partition_used(i++)) { |
| 2778 |
continue; |
| 2779 |
} |
| 2780 |
|
| 2781 |
table = m_part_share->get_table_part(i - 1); |
| 2782 |
|
| 2783 |
dd_part->set_se_private_id(table->id); |
| 2784 |
|
| 2785 |
/* Set the discard flag in the partition. */ |
| 2786 |
dd_set_discarded(*dd_part, discard); |
| 2787 |
|
| 2788 |
/* Set the discarded state in the dd::Tablespace */ |
| 2789 |
THD *thd = ha_thd(); |
| 2790 |
dd::Object_id dd_space_id = (*dd_part->indexes()->begin())->tablespace_id(); |
| 2791 |
std::string space_name(table->name.m_name); |
| 2792 |
dict_name::convert_to_space(space_name); |
| 2793 |
dd_space_states dd_state = |
| 2794 |
(discard ? DD_SPACE_STATE_DISCARDED : DD_SPACE_STATE_NORMAL); |
| 2795 |
dd_tablespace_set_state(thd, dd_space_id, space_name, dd_state); |
| 2796 |
|
| 2797 |
for (auto dd_index : *dd_part->indexes()) { |
| 2798 |
const dict_index_t *index = dd_find_index(table, dd_index); |
| 2799 |
ut_ad(index != nullptr); |
| 2800 |
|
| 2801 |
dd::Properties &p = dd_index->se_private_data(); |
| 2802 |
p.set(dd_index_key_strings[DD_INDEX_ROOT], index->page); |
| 2803 |
} |
| 2804 |
} |
| 2805 |
|
| 2806 |
/* Set new table id of the first partition to dd::Column::se_private_data */ |
| 2807 |
table = m_part_share->get_table_part(0); |
| 2808 |
|
| 2809 |
for (auto dd_column : *table_def->columns()) { |
| 2810 |
dd_column->se_private_data().set(dd_index_key_strings[DD_TABLE_ID], |
| 2811 |
table->id); |
| 2812 |
} |
| 2813 |
|
| 2814 |
return error; |
| 2815 |
} |
| 2816 |
|
| 2817 |
/** Discards or imports an InnoDB tablespace. |
| 2818 |
@param[in] discard True if discard, else import. |
| 2819 |
@param[in,out] table_def dd::Table describing table which |
| 2820 |
tablespaces are to be imported or discarded. Can be adjusted by SE, |
| 2821 |
the changes will be saved into the data-dictionary at statement |
| 2822 |
commit time. |
| 2823 |
@return 0 or error number. */ |
| 2824 |
int ha_innopart::discard_or_import_tablespace(bool discard, |
| 2825 |
dd::Table *table_def) { |
| 2826 |
int error = 0; |
| 2827 |
uint i; |
| 2828 |
|
| 2829 |
DBUG_TRACE; |
| 2830 |
|
| 2831 |
for (i = m_part_info->get_first_used_partition(); i < m_tot_parts; |
| 2832 |
i = m_part_info->get_next_used_partition(i)) { |
| 2833 |
m_prebuilt->table = m_part_share->get_table_part(i); |
| 2834 |
error = ha_innobase::discard_or_import_tablespace(discard, table_def); |
| 2835 |
if (error != 0) { |
| 2836 |
break; |
| 2837 |
} |
| 2838 |
} |
| 2839 |
|
| 2840 |
#ifdef UNIV_DEBUG |
| 2841 |
if (!discard && table_def->se_private_data().exists( |
| 2842 |
dd_table_key_strings[DD_TABLE_INSTANT_COLS])) { |
| 2843 |
ut_ad(dd_table_has_instant_cols(*table_def)); |
| 2844 |
} |
| 2845 |
#endif /* UNIV_DEBUG */ |
| 2846 |
|
| 2847 |
m_prebuilt->table = m_part_share->get_table_part(0); |
| 2848 |
|
| 2849 |
/* IMPORT/DISCARD also means resetting auto_increment. Make sure |
| 2850 |
that auto_increment initialization is done after all partitions |
| 2851 |
are imported. */ |
| 2852 |
if (table->found_next_number_field != nullptr) { |
| 2853 |
lock_auto_increment(); |
| 2854 |
m_part_share->next_auto_inc_val = 0; |
| 2855 |
m_part_share->auto_inc_initialized = false; |
| 2856 |
unlock_auto_increment(); |
| 2857 |
} |
| 2858 |
|
| 2859 |
/* Update dd partition for discard, since the table ids changed, |
| 2860 |
we need to change se_private_id accordingly. */ |
| 2861 |
if (error == 0) { |
| 2862 |
error = set_dd_discard_attribute(table_def, discard); |
| 2863 |
} |
| 2864 |
|
| 2865 |
return error; |
| 2866 |
} |
| 2867 |
|
| 2868 |
/** Compare key and rowid. |
| 2869 |
Helper function for sorting records in the priority queue. |
| 2870 |
a/b points to table->record[0] rows which must have the |
| 2871 |
key fields set. The bytes before a and b store the rowid. |
| 2872 |
This is used for comparing/sorting rows first according to |
| 2873 |
KEY and if same KEY, by rowid (ref). |
| 2874 |
@param[in] key_info Null terminated array of index information. |
| 2875 |
@param[in] a Pointer to record+ref in first record. |
| 2876 |
@param[in] b Pointer to record+ref in second record. |
| 2877 |
@return Return value is SIGN(first_rec - second_rec) |
| 2878 |
@retval 0 Keys are equal. |
| 2879 |
@retval -1 second_rec is greater than first_rec. |
| 2880 |
@retval +1 first_rec is greater than second_rec. */ |
| 2881 |
int ha_innopart::key_and_rowid_cmp(KEY **key_info, uchar *a, uchar *b) { |
| 2882 |
int cmp = key_rec_cmp(key_info, a, b); |
| 2883 |
if (cmp != 0) { |
| 2884 |
return (cmp); |
| 2885 |
} |
| 2886 |
|
| 2887 |
/* We must compare by rowid, which is added before the record, |
| 2888 |
in the priority queue. */ |
| 2889 |
|
| 2890 |
return (memcmp(a - DATA_ROW_ID_LEN, b - DATA_ROW_ID_LEN, DATA_ROW_ID_LEN)); |
| 2891 |
} |
| 2892 |
|
| 2893 |
/** Extra hints from MySQL. |
| 2894 |
@param[in] operation Operation hint. |
| 2895 |
@return 0 or error number. */ |
| 2896 |
int ha_innopart::extra(enum ha_extra_function operation) { |
| 2897 |
if (operation == HA_EXTRA_SECONDARY_SORT_ROWID) { |
| 2898 |
/* index_init(sorted=true) must have been called! */ |
| 2899 |
if (m_part_info->num_partitions_used() != 0) { |
| 2900 |
ut_ad(m_ordered); |
| 2901 |
ut_ad(m_ordered_rec_buffer != nullptr); |
| 2902 |
/* No index_read call must have been done! */ |
| 2903 |
ut_ad(m_queue->empty()); |
| 2904 |
|
| 2905 |
/* If not PK is set as secondary sort, do secondary sort by |
| 2906 |
rowid/ref. */ |
| 2907 |
|
| 2908 |
ut_ad(m_curr_key_info[1] != nullptr || |
| 2909 |
m_prebuilt->clust_index_was_generated != 0 || |
| 2910 |
m_curr_key_info[0] == table->key_info + table->s->primary_key); |
| 2911 |
|
| 2912 |
if (m_curr_key_info[1] == nullptr && |
| 2913 |
m_prebuilt->clust_index_was_generated) { |
| 2914 |
m_ref_usage = Partition_helper::REF_USED_FOR_SORT; |
| 2915 |
m_queue->m_fun = key_and_rowid_cmp; |
| 2916 |
} |
| 2917 |
} |
| 2918 |
return (0); |
| 2919 |
} |
| 2920 |
|
| 2921 |
/* In case of alter copy operation, set/unset the skip_undo_flag |
| 2922 |
for all partitions depends on the operation. */ |
| 2923 |
if (operation == HA_EXTRA_BEGIN_ALTER_COPY || |
| 2924 |
operation == HA_EXTRA_END_ALTER_COPY) { |
| 2925 |
for (uint i = m_part_info->get_first_used_partition(); i < m_tot_parts; |
| 2926 |
i = m_part_info->get_next_used_partition(i)) { |
| 2927 |
dict_table_t *table_part = m_part_share->get_table_part(i); |
| 2928 |
if (operation == HA_EXTRA_END_ALTER_COPY) { |
| 2929 |
alter_stats_rebuild(table_part, table_part->name.m_name, m_user_thd); |
| 2930 |
} |
| 2931 |
table_part->skip_alter_undo = (operation == HA_EXTRA_BEGIN_ALTER_COPY); |
| 2932 |
} |
| 2933 |
|
| 2934 |
return (0); |
| 2935 |
} |
| 2936 |
|
| 2937 |
return (ha_innobase::extra(operation)); |
| 2938 |
} |
| 2939 |
|
| 2940 |
/* Get partition row type |
| 2941 |
@param[in] partition_table partition table |
| 2942 |
@param[in] part_id Id of partition for which row type to be retrieved |
| 2943 |
@return Partition row type. */ |
| 2944 |
enum row_type ha_innopart::get_partition_row_type( |
| 2945 |
const dd::Table *partition_table, uint part_id) { |
| 2946 |
dd::Table::enum_row_format format; |
| 2947 |
row_type real_type = ROW_TYPE_NOT_USED; |
| 2948 |
|
| 2949 |
auto dd_table = partition_table->leaf_partitions().at(part_id); |
| 2950 |
const dd::Properties &part_p = dd_table->se_private_data(); |
| 2951 |
if (part_p.exists(dd_partition_key_strings[DD_PARTITION_ROW_FORMAT])) { |
| 2952 |
part_p.get(dd_partition_key_strings[DD_PARTITION_ROW_FORMAT], |
| 2953 |
reinterpret_cast<uint32_t *>(&format)); |
| 2954 |
switch (format) { |
| 2955 |
case dd::Table::RF_REDUNDANT: |
| 2956 |
real_type = ROW_TYPE_REDUNDANT; |
| 2957 |
break; |
| 2958 |
case dd::Table::RF_COMPACT: |
| 2959 |
real_type = ROW_TYPE_COMPACT; |
| 2960 |
break; |
| 2961 |
case dd::Table::RF_COMPRESSED: |
| 2962 |
real_type = ROW_TYPE_COMPRESSED; |
| 2963 |
break; |
| 2964 |
case dd::Table::RF_DYNAMIC: |
| 2965 |
real_type = ROW_TYPE_DYNAMIC; |
| 2966 |
break; |
| 2967 |
default: |
| 2968 |
/* purecov: begin inspected */ |
| 2969 |
real_type = ROW_TYPE_NOT_USED; |
| 2970 |
ut_d(ut_error); |
| 2971 |
/* purecov: end */ |
| 2972 |
} |
| 2973 |
} |
| 2974 |
if (real_type == ROW_TYPE_NOT_USED) { |
| 2975 |
return table_share->real_row_type; |
| 2976 |
} else { |
| 2977 |
return real_type; |
| 2978 |
} |
| 2979 |
} |
| 2980 |
|
| 2981 |
int ha_innopart::truncate_impl(const char *name, TABLE *form, |
| 2982 |
dd::Table *table_def) { |
| 2983 |
DBUG_TRACE; |
| 2984 |
|
| 2985 |
ut_ad(table_def != nullptr); |
| 2986 |
ut_ad(dd_table_is_partitioned(*table_def)); |
| 2987 |
ut_ad(table_def->is_persistent()); |
| 2988 |
|
| 2989 |
if (high_level_read_only) { |
| 2990 |
return HA_ERR_TABLE_READONLY; |
| 2991 |
} |
| 2992 |
|
| 2993 |
THD *thd = ha_thd(); |
| 2994 |
trx_t *trx = check_trx_exists(thd); |
| 2995 |
bool has_autoinc = false; |
| 2996 |
int error = 0; |
| 2997 |
|
| 2998 |
innobase_register_trx(ht, thd, trx); |
| 2999 |
|
| 3000 |
const bool is_instant = dd_table_has_instant_cols(*table_def); |
| 3001 |
|
| 3002 |
for (const auto dd_part : *table_def->leaf_partitions()) { |
| 3003 |
char norm_name[FN_REFLEN]; |
| 3004 |
dict_table_t *part_table = nullptr; |
| 3005 |
|
| 3006 |
std::string partition; |
| 3007 |
/* Build the partition name. */ |
| 3008 |
dict_name::build_partition(dd_part, partition); |
| 3009 |
|
| 3010 |
std::string partition_name; |
| 3011 |
/* Build the partitioned table name. */ |
| 3012 |
dict_name::build_table("", name, partition, false, false, partition_name); |
| 3013 |
ut_ad(partition_name.length() < FN_REFLEN); |
| 3014 |
|
| 3015 |
if (!normalize_table_name(norm_name, partition_name.c_str())) { |
| 3016 |
/* purecov: begin inspected */ |
| 3017 |
ut_d(ut_error); |
| 3018 |
ut_o(return (HA_ERR_TOO_LONG_PATH)); |
| 3019 |
/* purecov: end */ |
| 3020 |
} |
| 3021 |
|
| 3022 |
innobase_truncate<dd::Partition> truncator(thd, norm_name, form, dd_part, |
| 3023 |
false, true); |
| 3024 |
|
| 3025 |
error = truncator.open_table(part_table); |
| 3026 |
if (error != 0) { |
| 3027 |
return error; |
| 3028 |
} |
| 3029 |
|
| 3030 |
has_autoinc = dict_table_has_autoinc_col(part_table); |
| 3031 |
|
| 3032 |
if (dict_table_is_discarded(part_table)) { |
| 3033 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_TABLESPACE_DISCARDED, norm_name); |
| 3034 |
return HA_ERR_NO_SUCH_TABLE; |
| 3035 |
} else if (part_table->ibd_file_missing) { |
| 3036 |
return HA_ERR_TABLESPACE_MISSING; |
| 3037 |
} |
| 3038 |
|
| 3039 |
error = truncator.exec(); |
| 3040 |
|
| 3041 |
if (error != 0) { |
| 3042 |
return error; |
| 3043 |
} |
| 3044 |
} |
| 3045 |
|
| 3046 |
ut_ad(error == 0); |
| 3047 |
|
| 3048 |
if (has_autoinc) { |
| 3049 |
dd_set_autoinc(table_def->se_private_data(), 0); |
| 3050 |
} |
| 3051 |
|
| 3052 |
if (is_instant) { |
| 3053 |
for (dd::Partition *dd_part : *table_def->leaf_partitions()) { |
| 3054 |
if (dd_part_has_instant_cols(*dd_part)) { |
| 3055 |
dd_clear_instant_part(*dd_part); |
| 3056 |
} |
| 3057 |
} |
| 3058 |
|
| 3059 |
if (dd_clear_instant_table(*table_def, true) != DB_SUCCESS) { |
| 3060 |
error = HA_ERR_GENERIC; |
| 3061 |
} |
| 3062 |
} |
| 3063 |
|
| 3064 |
return error; |
| 3065 |
} |
| 3066 |
|
| 3067 |
/** Delete all rows in the requested partitions. |
| 3068 |
Done by deleting the partitions and recreate them again. |
| 3069 |
@param[in,out] dd_table dd::Table object for partitioned table |
| 3070 |
which partitions need to be truncated. Can be adjusted by this call. |
| 3071 |
Changes to the table definition will be persisted in the data-dictionary |
| 3072 |
at statement commit time. |
| 3073 |
@return 0 or error number. */ |
| 3074 |
int ha_innopart::truncate_partition_low(dd::Table *dd_table) { |
| 3075 |
int error = 0; |
| 3076 |
const char *table_name = table->s->normalized_path.str; |
| 3077 |
THD *thd = ha_thd(); |
| 3078 |
trx_t *trx = check_trx_exists(thd); |
| 3079 |
uint part_num = 0; |
| 3080 |
uint64_t autoinc = 0; |
| 3081 |
bool truncate_all = (m_part_info->num_partitions_used() == m_tot_parts); |
| 3082 |
|
| 3083 |
DBUG_TRACE; |
| 3084 |
|
| 3085 |
if (high_level_read_only) { |
| 3086 |
return HA_ERR_TABLE_READONLY; |
| 3087 |
} |
| 3088 |
|
| 3089 |
innobase_register_trx(ht, thd, trx); |
| 3090 |
|
| 3091 |
const bool is_versioned = dd_table_has_row_versions(*dd_table); |
| 3092 |
const bool is_instant = dd_table_is_upgraded_instant(*dd_table); |
| 3093 |
|
| 3094 |
for (const auto dd_part : *dd_table->leaf_partitions()) { |
| 3095 |
char norm_name[FN_REFLEN]; |
| 3096 |
dict_table_t *part_table = nullptr; |
| 3097 |
|
| 3098 |
std::string partition; |
| 3099 |
/* Build the partition name. */ |
| 3100 |
dict_name::build_partition(dd_part, partition); |
| 3101 |
|
| 3102 |
std::string partition_name; |
| 3103 |
/* Build the partitioned table name. */ |
| 3104 |
dict_name::build_table("", table_name, partition, false, false, |
| 3105 |
partition_name); |
| 3106 |
ut_ad(partition_name.length() < FN_REFLEN); |
| 3107 |
|
| 3108 |
if (!normalize_table_name(norm_name, partition_name.c_str())) { |
| 3109 |
/* purecov: begin inspected */ |
| 3110 |
ut_d(ut_error); |
| 3111 |
ut_o(return (HA_ERR_TOO_LONG_PATH)); |
| 3112 |
/* purecov: end */ |
| 3113 |
} |
| 3114 |
|
| 3115 |
innobase_truncate<dd::Partition> truncator(thd, norm_name, table, dd_part, |
| 3116 |
!truncate_all, truncate_all); |
| 3117 |
|
| 3118 |
error = truncator.open_table(part_table); |
| 3119 |
if (error != 0) { |
| 3120 |
return error; |
| 3121 |
} |
| 3122 |
|
| 3123 |
if (part_table->autoinc_persisted > autoinc) { |
| 3124 |
autoinc = part_table->autoinc_persisted; |
| 3125 |
} |
| 3126 |
|
| 3127 |
if (!m_part_info->is_partition_used(part_num++)) { |
| 3128 |
continue; |
| 3129 |
} |
| 3130 |
|
| 3131 |
if (dict_table_is_discarded(part_table)) { |
| 3132 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_TABLESPACE_DISCARDED, table_name); |
| 3133 |
return HA_ERR_NO_SUCH_TABLE; |
| 3134 |
} else if (part_table->ibd_file_missing) { |
| 3135 |
return HA_ERR_TABLESPACE_MISSING; |
| 3136 |
} |
| 3137 |
|
| 3138 |
error = truncator.exec(); |
| 3139 |
|
| 3140 |
if (error != 0) { |
| 3141 |
return error; |
| 3142 |
} |
| 3143 |
|
| 3144 |
if (is_instant && dd_part_has_instant_cols(*dd_part)) { |
| 3145 |
dd_clear_instant_part(*dd_part); |
| 3146 |
} |
| 3147 |
} |
| 3148 |
|
| 3149 |
ut_ad(error == 0); |
| 3150 |
|
| 3151 |
/* If it's TRUNCATE PARTITION ALL, reset the AUTOINC */ |
| 3152 |
if (table->found_next_number_field) { |
| 3153 |
dd_set_autoinc(dd_table->se_private_data(), |
| 3154 |
(truncate_all ? 0 : autoinc + 1)); |
| 3155 |
} |
| 3156 |
|
| 3157 |
if (is_instant || is_versioned) { |
| 3158 |
if (truncate_all) { |
| 3159 |
ut_ad(!dd_table_part_has_instant_cols(*dd_table)); |
| 3160 |
dd_clear_instant_table(*dd_table, is_versioned); |
| 3161 |
} else { |
| 3162 |
if (is_instant && !dd_table_part_has_instant_cols(*dd_table)) { |
| 3163 |
/* Not all partition truncate. Don't clear the versioned metadata. */ |
| 3164 |
if (dd_clear_instant_table(*dd_table, false) != DB_SUCCESS) { |
| 3165 |
error = HA_ERR_GENERIC; |
| 3166 |
} |
| 3167 |
} |
| 3168 |
} |
| 3169 |
} |
| 3170 |
|
| 3171 |
return error; |
| 3172 |
} |
| 3173 |
|
| 3174 |
/** Total number of rows in all used partitions. |
| 3175 |
Returns the exact number of records that this client can see using this |
| 3176 |
handler object. |
| 3177 |
@param[out] num_rows Number of rows. |
| 3178 |
@return 0 or error number. */ |
| 3179 |
int ha_innopart::records(ha_rows *num_rows) { |
| 3180 |
DBUG_TRACE; |
| 3181 |
|
| 3182 |
*num_rows = 0; |
| 3183 |
|
| 3184 |
auto trx = thd_to_trx(ha_thd()); |
| 3185 |
size_t n_threads = thd_parallel_read_threads(m_prebuilt->trx->mysql_thd); |
| 3186 |
|
| 3187 |
n_threads = Parallel_reader::available_threads(n_threads, false); |
| 3188 |
|
| 3189 |
if (n_threads > 1 && trx->isolation_level > TRX_ISO_READ_UNCOMMITTED && |
| 3190 |
m_prebuilt->select_lock_type == LOCK_NONE && |
| 3191 |
trx->mysql_n_tables_locked == 0 && !m_prebuilt->ins_sel_stmt) { |
| 3192 |
trx_start_if_not_started_xa(trx, false, UT_LOCATION_HERE); |
| 3193 |
trx_assign_read_view(trx); |
| 3194 |
|
| 3195 |
const auto first_used_partition = m_part_info->get_first_used_partition(); |
| 3196 |
|
| 3197 |
std::vector<dict_index_t *> indexes{}; |
| 3198 |
|
| 3199 |
for (auto i = first_used_partition; i < m_tot_parts; |
| 3200 |
i = m_part_info->get_next_used_partition(i)) { |
| 3201 |
set_partition(i); |
| 3202 |
|
| 3203 |
if (dict_table_is_discarded(m_prebuilt->table)) { |
| 3204 |
ib_senderrf(ha_thd(), IB_LOG_LEVEL_ERROR, ER_TABLESPACE_DISCARDED, |
| 3205 |
m_prebuilt->table->name.m_name); |
| 3206 |
|
| 3207 |
/* Restore the parallel read thread count if parallel read is not |
| 3208 |
executed */ |
| 3209 |
Parallel_reader::release_threads(n_threads); |
| 3210 |
|
| 3211 |
*num_rows = HA_POS_ERROR; |
| 3212 |
return (HA_ERR_NO_SUCH_TABLE); |
| 3213 |
} |
| 3214 |
|
| 3215 |
m_prebuilt->index_usable = m_prebuilt->index->is_usable(trx); |
| 3216 |
if (!m_prebuilt->index_usable) { |
| 3217 |
*num_rows = HA_POS_ERROR; |
| 3218 |
return HA_ERR_TABLE_DEF_CHANGED; |
| 3219 |
} |
| 3220 |
|
| 3221 |
build_template(true); |
| 3222 |
|
| 3223 |
indexes.push_back(m_prebuilt->table->first_index()); |
| 3224 |
} |
| 3225 |
|
| 3226 |
ulint n_rows{}; |
| 3227 |
|
| 3228 |
/* There is no need to restore the parallel read thread count after this |
| 3229 |
method call as it takes the ownership of n_threads. */ |
| 3230 |
auto err = |
| 3231 |
row_mysql_parallel_select_count_star(trx, indexes, n_threads, &n_rows); |
| 3232 |
|
| 3233 |
if (thd_killed(m_user_thd) || err == DB_INTERRUPTED) { |
| 3234 |
*num_rows = HA_POS_ERROR; |
| 3235 |
return HA_ERR_QUERY_INTERRUPTED; |
| 3236 |
} |
| 3237 |
|
| 3238 |
if (err == DB_SUCCESS) { |
| 3239 |
*num_rows = n_rows; |
| 3240 |
} else { |
| 3241 |
*num_rows = HA_POS_ERROR; |
| 3242 |
return convert_error_code_to_mysql(err, 0, m_user_thd); |
| 3243 |
} |
| 3244 |
} else { |
| 3245 |
/* The index scan is probably so expensive, so the overhead |
| 3246 |
of the rest of the function is neglectable for each partition. |
| 3247 |
So no current reason for optimizing this further. */ |
| 3248 |
|
| 3249 |
/* Restore the parallel read thread count if parallel read is not |
| 3250 |
executed */ |
| 3251 |
Parallel_reader::release_threads(n_threads); |
| 3252 |
|
| 3253 |
for (uint i = m_part_info->get_first_used_partition(); i < m_tot_parts; |
| 3254 |
i = m_part_info->get_next_used_partition(i)) { |
| 3255 |
set_partition(i); |
| 3256 |
|
| 3257 |
ha_rows n_rows{}; |
| 3258 |
|
| 3259 |
auto err = ha_innobase::records(&n_rows); |
| 3260 |
|
| 3261 |
update_partition(i); |
| 3262 |
|
| 3263 |
if (err != 0) { |
| 3264 |
*num_rows = HA_POS_ERROR; |
| 3265 |
return err; |
| 3266 |
} |
| 3267 |
*num_rows += n_rows; |
| 3268 |
} |
| 3269 |
} |
| 3270 |
return 0; |
| 3271 |
} |
| 3272 |
|
| 3273 |
/** Estimates the number of index records in a range. |
| 3274 |
@param[in] keynr Index number. |
| 3275 |
@param[in] min_key Start key value (or NULL). |
| 3276 |
@param[in] max_key End key value (or NULL). |
| 3277 |
@return estimated number of rows. */ |
| 3278 |
ha_rows ha_innopart::records_in_range(uint keynr, key_range *min_key, |
| 3279 |
key_range *max_key) { |
| 3280 |
KEY *key; |
| 3281 |
dict_index_t *index; |
| 3282 |
dtuple_t *range_start; |
| 3283 |
dtuple_t *range_end; |
| 3284 |
int64_t n_rows = 0; |
| 3285 |
page_cur_mode_t mode1; |
| 3286 |
page_cur_mode_t mode2; |
| 3287 |
mem_heap_t *heap; |
| 3288 |
uint part_id; |
| 3289 |
|
| 3290 |
DBUG_TRACE; |
| 3291 |
DBUG_PRINT("info", ("keynr %u min %p max %p", keynr, min_key, max_key)); |
| 3292 |
|
| 3293 |
ut_ad(m_prebuilt->trx == thd_to_trx(ha_thd())); |
| 3294 |
|
| 3295 |
m_prebuilt->trx->op_info = (char *)"estimating records in index range"; |
| 3296 |
|
| 3297 |
active_index = keynr; |
| 3298 |
|
| 3299 |
key = table->key_info + active_index; |
| 3300 |
|
| 3301 |
part_id = m_part_info->get_first_used_partition(); |
| 3302 |
if (part_id == MY_BIT_NONE) { |
| 3303 |
return 0; |
| 3304 |
} |
| 3305 |
/* This also sets m_prebuilt->index! */ |
| 3306 |
set_partition(part_id); |
| 3307 |
index = m_prebuilt->index; |
| 3308 |
|
| 3309 |
/* There exists possibility of not being able to find requested |
| 3310 |
index due to inconsistency between MySQL and InoDB dictionary info. |
| 3311 |
Necessary message should have been printed in innopart_get_index(). */ |
| 3312 |
if (index == nullptr || dict_table_is_discarded(m_prebuilt->table) || |
| 3313 |
!index->is_usable(m_prebuilt->trx)) { |
| 3314 |
n_rows = HA_POS_ERROR; |
| 3315 |
goto func_exit; |
| 3316 |
} |
| 3317 |
|
| 3318 |
heap = mem_heap_create( |
| 3319 |
2 * (key->actual_key_parts * sizeof(dfield_t) + sizeof(dtuple_t)), |
| 3320 |
UT_LOCATION_HERE); |
| 3321 |
|
| 3322 |
range_start = dtuple_create(heap, key->actual_key_parts); |
| 3323 |
dict_index_copy_types(range_start, index, key->actual_key_parts); |
| 3324 |
|
| 3325 |
range_end = dtuple_create(heap, key->actual_key_parts); |
| 3326 |
dict_index_copy_types(range_end, index, key->actual_key_parts); |
| 3327 |
|
| 3328 |
row_sel_convert_mysql_key_to_innobase( |
| 3329 |
range_start, m_prebuilt->srch_key_val1, m_prebuilt->srch_key_val_len, |
| 3330 |
index, (byte *)(min_key ? min_key->key : (const uchar *)nullptr), |
| 3331 |
(ulint)(min_key ? min_key->length : 0)); |
| 3332 |
|
| 3333 |
ut_ad(min_key != nullptr ? range_start->n_fields > 0 |
| 3334 |
: range_start->n_fields == 0); |
| 3335 |
|
| 3336 |
row_sel_convert_mysql_key_to_innobase( |
| 3337 |
range_end, m_prebuilt->srch_key_val2, m_prebuilt->srch_key_val_len, index, |
| 3338 |
(byte *)(max_key != nullptr ? max_key->key : (const uchar *)nullptr), |
| 3339 |
(ulint)(max_key != nullptr ? max_key->length : 0)); |
| 3340 |
|
| 3341 |
ut_ad(max_key != nullptr ? range_end->n_fields > 0 |
| 3342 |
: range_end->n_fields == 0); |
| 3343 |
|
| 3344 |
mode1 = convert_search_mode_to_innobase(min_key ? min_key->flag |
| 3345 |
: HA_READ_KEY_EXACT); |
| 3346 |
mode2 = convert_search_mode_to_innobase(max_key ? max_key->flag |
| 3347 |
: HA_READ_KEY_EXACT); |
| 3348 |
|
| 3349 |
if (mode1 != PAGE_CUR_UNSUPP && mode2 != PAGE_CUR_UNSUPP) { |
| 3350 |
n_rows = btr_estimate_n_rows_in_range(index, range_start, mode1, range_end, |
| 3351 |
mode2); |
| 3352 |
DBUG_PRINT("info", ("part_id %u rows %ld", part_id, (long int)n_rows)); |
| 3353 |
for (part_id = m_part_info->get_next_used_partition(part_id); |
| 3354 |
part_id < m_tot_parts; |
| 3355 |
part_id = m_part_info->get_next_used_partition(part_id)) { |
| 3356 |
index = m_part_share->get_index(part_id, keynr); |
| 3357 |
/* Individual partitions can be discarded |
| 3358 |
we need to check each partition */ |
| 3359 |
if (index == nullptr || dict_table_is_discarded(index->table) || |
| 3360 |
!index->is_usable(m_prebuilt->trx)) { |
| 3361 |
n_rows = HA_POS_ERROR; |
| 3362 |
mem_heap_free(heap); |
| 3363 |
goto func_exit; |
| 3364 |
} |
| 3365 |
int64_t n = btr_estimate_n_rows_in_range(index, range_start, mode1, |
| 3366 |
range_end, mode2); |
| 3367 |
n_rows += n; |
| 3368 |
DBUG_PRINT("info", ("part_id %u rows %ld (%ld)", part_id, (long int)n, |
| 3369 |
(long int)n_rows)); |
| 3370 |
} |
| 3371 |
} else { |
| 3372 |
n_rows = HA_POS_ERROR; |
| 3373 |
} |
| 3374 |
|
| 3375 |
mem_heap_free(heap); |
| 3376 |
|
| 3377 |
func_exit: |
| 3378 |
|
| 3379 |
m_prebuilt->trx->op_info = (char *)""; |
| 3380 |
|
| 3381 |
/* The MySQL optimizer seems to believe an estimate of 0 rows is |
| 3382 |
always accurate and may return the result 'Empty set' based on that. |
| 3383 |
The accuracy is not guaranteed, and even if it were, for a locking |
| 3384 |
read we should anyway perform the search to set the next-key lock. |
| 3385 |
Add 1 to the value to make sure MySQL does not make the assumption! */ |
| 3386 |
|
| 3387 |
if (n_rows == 0) { |
| 3388 |
n_rows = 1; |
| 3389 |
} |
| 3390 |
|
| 3391 |
return (ha_rows)n_rows; |
| 3392 |
} |
| 3393 |
|
| 3394 |
/** Gives an UPPER BOUND to the number of rows in a table. |
| 3395 |
This is used in filesort.cc. |
| 3396 |
@return upper bound of rows. */ |
| 3397 |
ha_rows ha_innopart::estimate_rows_upper_bound() { |
| 3398 |
const dict_index_t *index; |
| 3399 |
ulonglong estimate = 0; |
| 3400 |
ulonglong local_data_file_length; |
| 3401 |
ulint stat_n_leaf_pages; |
| 3402 |
|
| 3403 |
DBUG_TRACE; |
| 3404 |
|
| 3405 |
/* We do not know if MySQL can call this function before calling |
| 3406 |
external_lock(). To be safe, update the thd of the current table |
| 3407 |
handle. */ |
| 3408 |
|
| 3409 |
update_thd(ha_thd()); |
| 3410 |
|
| 3411 |
m_prebuilt->trx->op_info = "calculating upper bound for table rows"; |
| 3412 |
|
| 3413 |
for (uint i = m_part_info->get_first_used_partition(); i < m_tot_parts; |
| 3414 |
i = m_part_info->get_next_used_partition(i)) { |
| 3415 |
m_prebuilt->table = m_part_share->get_table_part(i); |
| 3416 |
index = m_prebuilt->table->first_index(); |
| 3417 |
|
| 3418 |
stat_n_leaf_pages = index->stat_n_leaf_pages; |
| 3419 |
|
| 3420 |
ut_ad(stat_n_leaf_pages > 0); |
| 3421 |
|
| 3422 |
local_data_file_length = ((ulonglong)stat_n_leaf_pages) * UNIV_PAGE_SIZE; |
| 3423 |
|
| 3424 |
/* Calculate a minimum length for a clustered index record |
| 3425 |
and from that an upper bound for the number of rows. |
| 3426 |
Since we only calculate new statistics in row0mysql.cc when a |
| 3427 |
table has grown by a threshold factor, |
| 3428 |
we must add a safety factor 2 in front of the formula below. */ |
| 3429 |
|
| 3430 |
estimate += 2 * local_data_file_length / dict_index_calc_min_rec_len(index); |
| 3431 |
} |
| 3432 |
|
| 3433 |
m_prebuilt->trx->op_info = ""; |
| 3434 |
|
| 3435 |
return (ha_rows)estimate; |
| 3436 |
} |
| 3437 |
|
| 3438 |
/** Time estimate for full table scan. |
| 3439 |
How many seeks it will take to read through the table. This is to be |
| 3440 |
comparable to the number returned by records_in_range so that we can |
| 3441 |
decide if we should scan the table or use keys. |
| 3442 |
@return estimated time measured in disk seeks. */ |
| 3443 |
double ha_innopart::scan_time() { |
| 3444 |
double scan_time = 0.0; |
| 3445 |
DBUG_TRACE; |
| 3446 |
|
| 3447 |
for (uint i = m_part_info->get_first_used_partition(); i < m_tot_parts; |
| 3448 |
i = m_part_info->get_next_used_partition(i)) { |
| 3449 |
m_prebuilt->table = m_part_share->get_table_part(i); |
| 3450 |
scan_time += ha_innobase::scan_time(); |
| 3451 |
} |
| 3452 |
return scan_time; |
| 3453 |
} |
| 3454 |
|
| 3455 |
/** Updates the statistics for one partition (table). |
| 3456 |
@param[in] table Table to update the statistics for. |
| 3457 |
@param[in] is_analyze True if called from "::analyze()". |
| 3458 |
@return error code. */ |
| 3459 |
static int update_table_stats(dict_table_t *table, bool is_analyze) { |
| 3460 |
dict_stats_upd_option_t opt; |
| 3461 |
dberr_t ret; |
| 3462 |
|
| 3463 |
if (dict_stats_is_persistent_enabled(table)) { |
| 3464 |
if (is_analyze) { |
| 3465 |
opt = DICT_STATS_RECALC_PERSISTENT; |
| 3466 |
} else { |
| 3467 |
/* This is e.g. 'SHOW INDEXES', |
| 3468 |
fetch the persistent stats from disk. */ |
| 3469 |
opt = DICT_STATS_FETCH_ONLY_IF_NOT_IN_MEMORY; |
| 3470 |
} |
| 3471 |
} else { |
| 3472 |
opt = DICT_STATS_RECALC_TRANSIENT; |
| 3473 |
} |
| 3474 |
|
| 3475 |
ut_ad(!dict_sys_mutex_own()); |
| 3476 |
ret = dict_stats_update(table, opt); |
| 3477 |
|
| 3478 |
if (ret != DB_SUCCESS) { |
| 3479 |
return (HA_ERR_GENERIC); |
| 3480 |
} |
| 3481 |
return (0); |
| 3482 |
} |
| 3483 |
|
| 3484 |
/** Updates and return statistics. |
| 3485 |
Returns statistics information of the table to the MySQL interpreter, |
| 3486 |
in various fields of the handle object. |
| 3487 |
@param[in] flag Flags for what to update and return. |
| 3488 |
@param[in] is_analyze True if called from "::analyze()". |
| 3489 |
@return HA_ERR_* error code or 0. */ |
| 3490 |
int ha_innopart::info_low(uint flag, bool is_analyze) { |
| 3491 |
dict_table_t *ib_table; |
| 3492 |
uint64_t max_rows = 0; |
| 3493 |
uint biggest_partition = 0; |
| 3494 |
int error = 0; |
| 3495 |
|
| 3496 |
DBUG_TRACE; |
| 3497 |
|
| 3498 |
/* If we are forcing recovery at a high level, we will suppress |
| 3499 |
statistics calculation on tables, because that may crash the |
| 3500 |
server if an index is badly corrupted. */ |
| 3501 |
|
| 3502 |
/* We do not know if MySQL can call this function before calling |
| 3503 |
external_lock(). To be safe, update the thd of the current table |
| 3504 |
handle. */ |
| 3505 |
|
| 3506 |
update_thd(ha_thd()); |
| 3507 |
|
| 3508 |
m_prebuilt->trx->op_info = (char *)"returning various info to MySQL"; |
| 3509 |
|
| 3510 |
ut_ad(m_part_share->get_table_part(0)->n_ref_count > 0); |
| 3511 |
|
| 3512 |
if ((flag & HA_STATUS_TIME) != 0) { |
| 3513 |
stats.update_time = 0; |
| 3514 |
|
| 3515 |
if (is_analyze) { |
| 3516 |
/* Only analyze the given partitions. */ |
| 3517 |
int error = set_altered_partitions(); |
| 3518 |
if (error != 0) { |
| 3519 |
/* Already checked in mysql_admin_table! */ |
| 3520 |
ut_d(ut_error); |
| 3521 |
ut_o(return error); |
| 3522 |
} |
| 3523 |
} |
| 3524 |
if (is_analyze || innobase_stats_on_metadata) { |
| 3525 |
m_prebuilt->trx->op_info = "updating table statistics"; |
| 3526 |
} |
| 3527 |
|
| 3528 |
/* TODO: Only analyze the PK for all partitions, |
| 3529 |
then the secondary indexes only for the largest partition! */ |
| 3530 |
for (uint i = m_part_info->get_first_used_partition(); i < m_tot_parts; |
| 3531 |
i = m_part_info->get_next_used_partition(i)) { |
| 3532 |
ib_table = m_part_share->get_table_part(i); |
| 3533 |
if (is_analyze || innobase_stats_on_metadata) { |
| 3534 |
error = update_table_stats(ib_table, is_analyze); |
| 3535 |
if (error != 0) { |
| 3536 |
m_prebuilt->trx->op_info = ""; |
| 3537 |
return error; |
| 3538 |
} |
| 3539 |
} |
| 3540 |
stats.update_time = std::max(stats.update_time, |
| 3541 |
ulong(std::chrono::system_clock::to_time_t( |
| 3542 |
ib_table->update_time.load()))); |
| 3543 |
} |
| 3544 |
|
| 3545 |
if (is_analyze || innobase_stats_on_metadata) { |
| 3546 |
m_prebuilt->trx->op_info = "returning various info to MySQL"; |
| 3547 |
} |
| 3548 |
} |
| 3549 |
|
| 3550 |
if ((flag & HA_STATUS_VARIABLE) != 0) { |
| 3551 |
/* TODO: If this is called after pruning, then we could |
| 3552 |
also update the statistics according to the non-pruned |
| 3553 |
partitions, by allocating new rec_per_key on the TABLE, |
| 3554 |
instead of using the info from the TABLE_SHARE. */ |
| 3555 |
ulint stat_clustered_index_size = 0; |
| 3556 |
ulint stat_sum_of_other_index_sizes = 0; |
| 3557 |
uint64_t n_rows = 0; |
| 3558 |
ulint avail_space = 0; |
| 3559 |
bool checked_sys_tablespace = false; |
| 3560 |
|
| 3561 |
if ((flag & HA_STATUS_VARIABLE_EXTRA) != 0) { |
| 3562 |
stats.delete_length = 0; |
| 3563 |
} |
| 3564 |
|
| 3565 |
for (uint i = m_part_info->get_first_used_partition(); i < m_tot_parts; |
| 3566 |
i = m_part_info->get_next_used_partition(i)) { |
| 3567 |
ib_table = m_part_share->get_table_part(i); |
| 3568 |
if ((flag & HA_STATUS_NO_LOCK) == 0) { |
| 3569 |
dict_table_stats_lock(ib_table, RW_S_LATCH); |
| 3570 |
} |
| 3571 |
|
| 3572 |
ut_ad(ib_table->stat_initialized); |
| 3573 |
|
| 3574 |
n_rows += ib_table->stat_n_rows; |
| 3575 |
if (ib_table->stat_n_rows > max_rows) { |
| 3576 |
max_rows = ib_table->stat_n_rows; |
| 3577 |
biggest_partition = i; |
| 3578 |
} |
| 3579 |
|
| 3580 |
stat_clustered_index_size += ib_table->stat_clustered_index_size; |
| 3581 |
|
| 3582 |
stat_sum_of_other_index_sizes += ib_table->stat_sum_of_other_index_sizes; |
| 3583 |
|
| 3584 |
if ((flag & HA_STATUS_NO_LOCK) == 0) { |
| 3585 |
dict_table_stats_unlock(ib_table, RW_S_LATCH); |
| 3586 |
} |
| 3587 |
|
| 3588 |
if ((flag & HA_STATUS_VARIABLE_EXTRA) != 0 && |
| 3589 |
(flag & HA_STATUS_NO_LOCK) == 0 && |
| 3590 |
srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE && |
| 3591 |
avail_space != ULINT_UNDEFINED) { |
| 3592 |
/* Only count system tablespace once! */ |
| 3593 |
if (fsp_is_system_or_temp_tablespace(ib_table->space)) { |
| 3594 |
if (checked_sys_tablespace) { |
| 3595 |
continue; |
| 3596 |
} |
| 3597 |
checked_sys_tablespace = true; |
| 3598 |
} |
| 3599 |
|
| 3600 |
uintmax_t space = |
| 3601 |
fsp_get_available_space_in_free_extents(ib_table->space); |
| 3602 |
if (space == UINTMAX_MAX) { |
| 3603 |
THD *thd = ha_thd(); |
| 3604 |
const char *table_name = ib_table->name.m_name; |
| 3605 |
|
| 3606 |
push_warning_printf(thd, Sql_condition::SL_WARNING, ER_CANT_GET_STAT, |
| 3607 |
"InnoDB: Trying to get the" |
| 3608 |
" free space for partition %s" |
| 3609 |
" but its tablespace has been" |
| 3610 |
" discarded or the .ibd file" |
| 3611 |
" is missing. Setting the free" |
| 3612 |
" space of the partition to" |
| 3613 |
" zero.", |
| 3614 |
ut_get_name(m_prebuilt->trx, table_name).c_str()); |
| 3615 |
} else { |
| 3616 |
avail_space += static_cast<ulint>(space); |
| 3617 |
} |
| 3618 |
} |
| 3619 |
} |
| 3620 |
|
| 3621 |
/* |
| 3622 |
The MySQL optimizer seems to assume in a left join that n_rows |
| 3623 |
is an accurate estimate if it is zero. Of course, it is not, |
| 3624 |
since we do not have any locks on the rows yet at this phase. |
| 3625 |
Since SHOW TABLE STATUS seems to call this function with the |
| 3626 |
HA_STATUS_TIME flag set, while the left join optimizer does not |
| 3627 |
set that flag, we add one to a zero value if the flag is not |
| 3628 |
set. That way SHOW TABLE STATUS will show the best estimate, |
| 3629 |
while the optimizer never sees the table empty. */ |
| 3630 |
|
| 3631 |
if (n_rows == 0 && (flag & HA_STATUS_TIME) == 0) { |
| 3632 |
n_rows++; |
| 3633 |
} |
| 3634 |
|
| 3635 |
/* Fix bug#40386: Not flushing query cache after truncate. |
| 3636 |
n_rows can not be 0 unless the table is empty, set to 1 |
| 3637 |
instead. The original problem of bug#29507 is actually |
| 3638 |
fixed in the server code. */ |
| 3639 |
if (thd_sql_command(m_user_thd) == SQLCOM_TRUNCATE) { |
| 3640 |
n_rows = 1; |
| 3641 |
|
| 3642 |
/* We need to reset the m_prebuilt value too, otherwise |
| 3643 |
checks for values greater than the last value written |
| 3644 |
to the table will fail and the autoinc counter will |
| 3645 |
not be updated. This will force write_row() into |
| 3646 |
attempting an update of the table's AUTOINC counter. */ |
| 3647 |
|
| 3648 |
m_prebuilt->autoinc_last_value = 0; |
| 3649 |
} |
| 3650 |
|
| 3651 |
/* Take page_size from first partition. */ |
| 3652 |
ib_table = m_part_share->get_table_part(0); |
| 3653 |
const page_size_t &page_size = dict_table_page_size(ib_table); |
| 3654 |
|
| 3655 |
stats.records = (ha_rows)n_rows; |
| 3656 |
stats.deleted = 0; |
| 3657 |
stats.data_file_length = |
| 3658 |
((ulonglong)stat_clustered_index_size) * page_size.physical(); |
| 3659 |
stats.index_file_length = |
| 3660 |
((ulonglong)stat_sum_of_other_index_sizes) * page_size.physical(); |
| 3661 |
|
| 3662 |
/* See ha_innobase::info_low() for comments! */ |
| 3663 |
if ((flag & HA_STATUS_NO_LOCK) == 0 && |
| 3664 |
(flag & HA_STATUS_VARIABLE_EXTRA) != 0 && |
| 3665 |
srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE) { |
| 3666 |
stats.delete_length = avail_space * 1024; |
| 3667 |
} |
| 3668 |
|
| 3669 |
stats.check_time = 0; |
| 3670 |
stats.mrr_length_per_rec = |
| 3671 |
ref_length + sizeof(void *) - PARTITION_BYTES_IN_POS; |
| 3672 |
|
| 3673 |
if (stats.records == 0) { |
| 3674 |
stats.mean_rec_length = 0; |
| 3675 |
} else { |
| 3676 |
stats.mean_rec_length = (ulong)(stats.data_file_length / stats.records); |
| 3677 |
} |
| 3678 |
} |
| 3679 |
|
| 3680 |
if ((flag & HA_STATUS_CONST) != 0) { |
| 3681 |
/* Find max rows and biggest partition. */ |
| 3682 |
for (uint i = 0; i < m_tot_parts; i++) { |
| 3683 |
/* Skip partitions from above. */ |
| 3684 |
if ((flag & HA_STATUS_VARIABLE) == 0 || |
| 3685 |
!bitmap_is_set(&(m_part_info->read_partitions), i)) { |
| 3686 |
ib_table = m_part_share->get_table_part(i); |
| 3687 |
if (ib_table->stat_n_rows > max_rows) { |
| 3688 |
max_rows = ib_table->stat_n_rows; |
| 3689 |
biggest_partition = i; |
| 3690 |
} |
| 3691 |
} |
| 3692 |
} |
| 3693 |
ib_table = m_part_share->get_table_part(biggest_partition); |
| 3694 |
/* Verify the number of index in InnoDB and MySQL |
| 3695 |
matches up. If m_prebuilt->clust_index_was_generated |
| 3696 |
holds, InnoDB defines GEN_CLUST_INDEX internally. */ |
| 3697 |
ulint num_innodb_index = UT_LIST_GET_LEN(ib_table->indexes) - |
| 3698 |
m_prebuilt->clust_index_was_generated; |
| 3699 |
if (table->s->keys < num_innodb_index) { |
| 3700 |
/* If there are too many indexes defined |
| 3701 |
inside InnoDB, ignore those that are being |
| 3702 |
created, because MySQL will only consider |
| 3703 |
the fully built indexes here. */ |
| 3704 |
|
| 3705 |
for (const dict_index_t *index : ib_table->indexes) { |
| 3706 |
/* First, online index creation is |
| 3707 |
completed inside InnoDB, and then |
| 3708 |
MySQL attempts to upgrade the |
| 3709 |
meta-data lock so that it can rebuild |
| 3710 |
the .frm file. If we get here in that |
| 3711 |
time frame, dict_index_is_online_ddl() |
| 3712 |
would not hold and the index would |
| 3713 |
still not be included in TABLE_SHARE. */ |
| 3714 |
if (!index->is_committed()) { |
| 3715 |
num_innodb_index--; |
| 3716 |
} |
| 3717 |
} |
| 3718 |
|
| 3719 |
if (table->s->keys < num_innodb_index && |
| 3720 |
(innobase_fts_check_doc_id_index(ib_table, nullptr, nullptr) == |
| 3721 |
FTS_EXIST_DOC_ID_INDEX)) { |
| 3722 |
num_innodb_index--; |
| 3723 |
} |
| 3724 |
} |
| 3725 |
|
| 3726 |
if (table->s->keys != num_innodb_index) { |
| 3727 |
ib::error(ER_IB_MSG_595) |
| 3728 |
<< "Table " << ib_table->name << " contains " << num_innodb_index |
| 3729 |
<< " indexes inside InnoDB, which" |
| 3730 |
" is different from the number of" |
| 3731 |
" indexes " |
| 3732 |
<< table->s->keys << " defined in the MySQL"; |
| 3733 |
} |
| 3734 |
|
| 3735 |
if ((flag & HA_STATUS_NO_LOCK) == 0) { |
| 3736 |
dict_table_stats_lock(ib_table, RW_S_LATCH); |
| 3737 |
} |
| 3738 |
|
| 3739 |
ut_ad(ib_table->stat_initialized); |
| 3740 |
|
| 3741 |
for (ulong i = 0; i < table->s->keys; i++) { |
| 3742 |
ulong j; |
| 3743 |
/* We could get index quickly through internal |
| 3744 |
index mapping with the index translation table. |
| 3745 |
The identity of index (match up index name with |
| 3746 |
that of table->key_info[i]) is already verified in |
| 3747 |
innopart_get_index(). */ |
| 3748 |
dict_index_t *index = innopart_get_index(biggest_partition, i); |
| 3749 |
|
| 3750 |
if (index == nullptr) { |
| 3751 |
ib::error(ER_IB_MSG_596) |
| 3752 |
<< "Table " << ib_table->name |
| 3753 |
<< " contains fewer indexes than expected." << TROUBLESHOOTING_MSG; |
| 3754 |
break; |
| 3755 |
} |
| 3756 |
|
| 3757 |
KEY *key = &table->key_info[i]; |
| 3758 |
for (j = 0; j < key->actual_key_parts; j++) { |
| 3759 |
if ((key->flags & HA_FULLTEXT) != 0) { |
| 3760 |
/* The whole concept has no validity |
| 3761 |
for FTS indexes. */ |
| 3762 |
key->set_records_per_key(j, 1.0f); |
| 3763 |
continue; |
| 3764 |
} |
| 3765 |
|
| 3766 |
if ((j + 1) > index->n_uniq) { |
| 3767 |
ib::error(ER_IB_MSG_597) |
| 3768 |
<< "Index " << index->name << " of " << ib_table->name << " has " |
| 3769 |
<< index->n_uniq |
| 3770 |
<< " columns unique inside" |
| 3771 |
" InnoDB, but MySQL is" |
| 3772 |
" asking statistics for " |
| 3773 |
<< j + 1 << " columns." << TROUBLESHOOTING_MSG; |
| 3774 |
break; |
| 3775 |
} |
| 3776 |
|
| 3777 |
/* innodb_rec_per_key() will use |
| 3778 |
index->stat_n_diff_key_vals[] and the value we |
| 3779 |
pass index->table->stat_n_rows. Both are |
| 3780 |
calculated by ANALYZE and by the background |
| 3781 |
stats gathering thread (which kicks in when too |
| 3782 |
much of the table has been changed). In |
| 3783 |
addition table->stat_n_rows is adjusted with |
| 3784 |
each DML (e.g. ++ on row insert). Those |
| 3785 |
adjustments are not MVCC'ed and not even |
| 3786 |
reversed on rollback. So, |
| 3787 |
index->stat_n_diff_key_vals[] and |
| 3788 |
index->table->stat_n_rows could have been |
| 3789 |
calculated at different time. This is |
| 3790 |
acceptable. */ |
| 3791 |
const rec_per_key_t rec_per_key = |
| 3792 |
innodb_rec_per_key(index, j, max_rows); |
| 3793 |
|
| 3794 |
key->set_records_per_key(j, rec_per_key); |
| 3795 |
} |
| 3796 |
} |
| 3797 |
|
| 3798 |
if ((flag & HA_STATUS_NO_LOCK) == 0) { |
| 3799 |
dict_table_stats_unlock(ib_table, RW_S_LATCH); |
| 3800 |
} |
| 3801 |
} |
| 3802 |
|
| 3803 |
if (srv_force_recovery >= SRV_FORCE_NO_IBUF_MERGE) { |
| 3804 |
goto func_exit; |
| 3805 |
} |
| 3806 |
|
| 3807 |
if ((flag & HA_STATUS_ERRKEY) != 0) { |
| 3808 |
const dict_index_t *err_index; |
| 3809 |
|
| 3810 |
ut_a(m_prebuilt->trx); |
| 3811 |
ut_a(m_prebuilt->trx->magic_n == TRX_MAGIC_N); |
| 3812 |
|
| 3813 |
err_index = trx_get_error_index(m_prebuilt->trx); |
| 3814 |
|
| 3815 |
if (err_index != nullptr) { |
| 3816 |
errkey = m_part_share->get_mysql_key(m_last_part, err_index); |
| 3817 |
} else { |
| 3818 |
errkey = |
| 3819 |
(unsigned int)((m_prebuilt->trx->error_key_num == ULINT_UNDEFINED) |
| 3820 |
? UINT_MAX |
| 3821 |
: m_prebuilt->trx->error_key_num); |
| 3822 |
} |
| 3823 |
} |
| 3824 |
|
| 3825 |
if ((flag & HA_STATUS_AUTO) != 0) { |
| 3826 |
/* auto_inc is only supported in first key for InnoDB! */ |
| 3827 |
ut_ad(table_share->next_number_keypart == 0); |
| 3828 |
DBUG_PRINT("info", ("HA_STATUS_AUTO")); |
| 3829 |
if (table->found_next_number_field == nullptr) { |
| 3830 |
stats.auto_increment_value = 0; |
| 3831 |
} else { |
| 3832 |
/* Lock to avoid two concurrent initializations. */ |
| 3833 |
lock_auto_increment(); |
| 3834 |
if (m_part_share->auto_inc_initialized) { |
| 3835 |
stats.auto_increment_value = m_part_share->next_auto_inc_val; |
| 3836 |
} else { |
| 3837 |
/* The auto-inc mutex in the table_share is |
| 3838 |
locked, so we do not need to have the handlers |
| 3839 |
locked. */ |
| 3840 |
|
| 3841 |
error = initialize_auto_increment((flag & HA_STATUS_NO_LOCK) != 0); |
| 3842 |
stats.auto_increment_value = m_part_share->next_auto_inc_val; |
| 3843 |
} |
| 3844 |
unlock_auto_increment(); |
| 3845 |
} |
| 3846 |
} |
| 3847 |
|
| 3848 |
func_exit: |
| 3849 |
m_prebuilt->trx->op_info = (char *)""; |
| 3850 |
|
| 3851 |
return error; |
| 3852 |
} |
| 3853 |
|
| 3854 |
int ha_innopart::optimize(THD *, HA_CHECK_OPT *) { |
| 3855 |
return (HA_ADMIN_TRY_ALTER); |
| 3856 |
} |
| 3857 |
|
| 3858 |
/** Checks a partitioned table. |
| 3859 |
Tries to check that an InnoDB table is not corrupted. If corruption is |
| 3860 |
noticed, prints to stderr information about it. In case of corruption |
| 3861 |
may also assert a failure and crash the server. Also checks for records |
| 3862 |
in wrong partition. |
| 3863 |
@param[in] thd MySQL THD object/thread handle. |
| 3864 |
@param[in] check_opt Check options. |
| 3865 |
@return HA_ADMIN_CORRUPT or HA_ADMIN_OK. */ |
| 3866 |
int ha_innopart::check(THD *thd, HA_CHECK_OPT *check_opt) { |
| 3867 |
uint error = HA_ADMIN_OK; |
| 3868 |
uint i; |
| 3869 |
|
| 3870 |
DBUG_TRACE; |
| 3871 |
/* TODO: Enhance this to: |
| 3872 |
- Every partition has the same structure. |
| 3873 |
- The names are correct (partition names checked in ::open()?) |
| 3874 |
Currently it only does normal InnoDB check of each partition. */ |
| 3875 |
|
| 3876 |
if (set_altered_partitions()) { |
| 3877 |
ut_d(ut_error); // Already checked by set_part_state()! |
| 3878 |
ut_o(return HA_ADMIN_INVALID); |
| 3879 |
} |
| 3880 |
for (i = m_part_info->get_first_used_partition(); i < m_tot_parts; |
| 3881 |
i = m_part_info->get_next_used_partition(i)) { |
| 3882 |
m_prebuilt->table = m_part_share->get_table_part(i); |
| 3883 |
error = ha_innobase::check(thd, check_opt); |
| 3884 |
if (error != 0) { |
| 3885 |
break; |
| 3886 |
} |
| 3887 |
if ((check_opt->flags & (T_MEDIUM | T_EXTEND)) != 0) { |
| 3888 |
error = Partition_helper::check_misplaced_rows(i, false); |
| 3889 |
if (error != 0) { |
| 3890 |
break; |
| 3891 |
} |
| 3892 |
} |
| 3893 |
} |
| 3894 |
if (error != 0) { |
| 3895 |
print_admin_msg(thd, 256, "error", table_share->db.str, table->alias, |
| 3896 |
"check", |
| 3897 |
m_is_sub_partitioned ? "Subpartition %s returned error" |
| 3898 |
: "Partition %s returned error", |
| 3899 |
m_part_share->get_partition_name(i)); |
| 3900 |
} |
| 3901 |
|
| 3902 |
return error; |
| 3903 |
} |
| 3904 |
|
| 3905 |
/** Repair a partitioned table. |
| 3906 |
Only repairs records in wrong partitions (moves them to the correct |
| 3907 |
partition or deletes them if not in any partition). |
| 3908 |
@param[in] thd MySQL THD object/thread handle. |
| 3909 |
@param[in] repair_opt Repair options. |
| 3910 |
@return 0 or error code. */ |
| 3911 |
int ha_innopart::repair(THD *thd, HA_CHECK_OPT *repair_opt) { |
| 3912 |
uint error = HA_ADMIN_OK; |
| 3913 |
|
| 3914 |
DBUG_TRACE; |
| 3915 |
|
| 3916 |
/* TODO: enable this warning to be clear about what is repaired. |
| 3917 |
Currently disabled to generate smaller test diffs. */ |
| 3918 |
#ifdef ADD_WARNING_FOR_REPAIR_ONLY_PARTITION |
| 3919 |
push_warning_printf(thd, Sql_condition::SL_WARNING, ER_ILLEGAL_HA, |
| 3920 |
"Only moving rows from wrong partition to correct" |
| 3921 |
" partition is supported," |
| 3922 |
" repairing InnoDB indexes is not yet supported!"); |
| 3923 |
#endif |
| 3924 |
|
| 3925 |
/* Only repair partitions for MEDIUM or EXTENDED options. */ |
| 3926 |
if ((repair_opt->flags & (T_MEDIUM | T_EXTEND)) == 0) { |
| 3927 |
return HA_ADMIN_OK; |
| 3928 |
} |
| 3929 |
if (set_altered_partitions()) { |
| 3930 |
ut_d(ut_error); // Already checked by set_part_state()! |
| 3931 |
ut_o(return HA_ADMIN_INVALID); |
| 3932 |
} |
| 3933 |
for (uint i = m_part_info->get_first_used_partition(); i < m_tot_parts; |
| 3934 |
i = m_part_info->get_next_used_partition(i)) { |
| 3935 |
/* TODO: Implement and use ha_innobase::repair()! */ |
| 3936 |
error = Partition_helper::check_misplaced_rows(i, true); |
| 3937 |
if (error != 0) { |
| 3938 |
print_admin_msg(thd, 256, "error", table_share->db.str, table->alias, |
| 3939 |
"repair", |
| 3940 |
m_is_sub_partitioned ? "Subpartition %s returned error" |
| 3941 |
: "Partition %s returned error", |
| 3942 |
m_part_share->get_partition_name(i)); |
| 3943 |
break; |
| 3944 |
} |
| 3945 |
} |
| 3946 |
|
| 3947 |
return error; |
| 3948 |
} |
| 3949 |
|
| 3950 |
/** Start statement. |
| 3951 |
MySQL calls this function at the start of each SQL statement inside LOCK |
| 3952 |
TABLES. Inside LOCK TABLES the "::external_lock" method does not work to |
| 3953 |
mark SQL statement borders. Note also a special case: if a temporary table |
| 3954 |
is created inside LOCK TABLES, MySQL has not called external_lock() at all |
| 3955 |
on that table. |
| 3956 |
MySQL-5.0 also calls this before each statement in an execution of a stored |
| 3957 |
procedure. To make the execution more deterministic for binlogging, MySQL-5.0 |
| 3958 |
locks all tables involved in a stored procedure with full explicit table |
| 3959 |
locks (thd_in_lock_tables(thd) holds in store_lock()) before executing the |
| 3960 |
procedure. |
| 3961 |
@param[in] thd Handle to the user thread. |
| 3962 |
@param[in] lock_type Lock type. |
| 3963 |
@return 0 or error code. */ |
| 3964 |
int ha_innopart::start_stmt(THD *thd, thr_lock_type lock_type) { |
| 3965 |
int error = 0; |
| 3966 |
|
| 3967 |
if (m_part_info->get_first_used_partition() == MY_BIT_NONE) { |
| 3968 |
/* All partitions pruned away, do nothing! */ |
| 3969 |
return (error); |
| 3970 |
} |
| 3971 |
|
| 3972 |
error = ha_innobase::start_stmt(thd, lock_type); |
| 3973 |
if (m_prebuilt->sql_stat_start) { |
| 3974 |
m_sql_stat_start_parts.set(); |
| 3975 |
} else { |
| 3976 |
m_sql_stat_start_parts.reset(); |
| 3977 |
} |
| 3978 |
m_reuse_mysql_template = false; |
| 3979 |
return (error); |
| 3980 |
} |
| 3981 |
|
| 3982 |
/** Function to store lock for all partitions in native partitioned table. Also |
| 3983 |
look at ha_innobase::store_lock for more details. |
| 3984 |
@param[in] thd user thread handle |
| 3985 |
@param[in] to pointer to the current element in an array of |
| 3986 |
pointers to lock structs |
| 3987 |
@param[in] lock_type lock type to store in 'lock'; this may also be |
| 3988 |
TL_IGNORE |
| 3989 |
@retval to pointer to the current element in the 'to' array */ |
| 3990 |
THR_LOCK_DATA **ha_innopart::store_lock(THD *thd, THR_LOCK_DATA **to, |
| 3991 |
thr_lock_type lock_type) { |
| 3992 |
trx_t *trx = m_prebuilt->trx; |
| 3993 |
const uint sql_command = thd_sql_command(thd); |
| 3994 |
|
| 3995 |
ha_innobase::store_lock(thd, to, lock_type); |
| 3996 |
|
| 3997 |
if (sql_command == SQLCOM_FLUSH && lock_type == TL_READ_NO_INSERT) { |
| 3998 |
for (uint i = 1; i < m_tot_parts; i++) { |
| 3999 |
dict_table_t *table = m_part_share->get_table_part(i); |
| 4000 |
|
| 4001 |
dberr_t err = row_quiesce_set_state(table, QUIESCE_START, trx); |
| 4002 |
ut_a(err == DB_SUCCESS || err == DB_UNSUPPORTED); |
| 4003 |
} |
| 4004 |
} |
| 4005 |
|
| 4006 |
return to; |
| 4007 |
} |
| 4008 |
|
| 4009 |
/** Lock/prepare to lock table. |
| 4010 |
As MySQL will execute an external lock for every new table it uses when it |
| 4011 |
starts to process an SQL statement (an exception is when MySQL calls |
| 4012 |
start_stmt for the handle) we can use this function to store the pointer to |
| 4013 |
the THD in the handle. We will also use this function to communicate |
| 4014 |
to InnoDB that a new SQL statement has started and that we must store a |
| 4015 |
savepoint to our transaction handle, so that we are able to roll back |
| 4016 |
the SQL statement in case of an error. |
| 4017 |
@param[in] thd Handle to the user thread. |
| 4018 |
@param[in] lock_type Lock type. |
| 4019 |
@return 0 or error number. */ |
| 4020 |
int ha_innopart::external_lock(THD *thd, int lock_type) { |
| 4021 |
int error = 0; |
| 4022 |
|
| 4023 |
if (m_part_info->get_first_used_partition() == MY_BIT_NONE && |
| 4024 |
!(m_mysql_has_locked && lock_type == F_UNLCK)) { |
| 4025 |
/* All partitions pruned away, do nothing! */ |
| 4026 |
ut_ad(!m_mysql_has_locked); |
| 4027 |
return (error); |
| 4028 |
} |
| 4029 |
ut_ad(m_mysql_has_locked || lock_type != F_UNLCK); |
| 4030 |
|
| 4031 |
if (m_prebuilt->table == nullptr) { |
| 4032 |
ut_ad(lock_type == F_UNLCK); |
| 4033 |
ut_ad(m_prebuilt->trx->n_mysql_tables_in_use > 0); |
| 4034 |
TrxInInnoDB::end_stmt(m_prebuilt->trx); |
| 4035 |
--m_prebuilt->trx->n_mysql_tables_in_use; |
| 4036 |
m_mysql_has_locked = false; |
| 4037 |
if (m_prebuilt->trx->n_mysql_tables_in_use == 0) { |
| 4038 |
m_prebuilt->trx->mysql_n_tables_locked = 0; |
| 4039 |
} |
| 4040 |
return (error); |
| 4041 |
} |
| 4042 |
|
| 4043 |
m_prebuilt->table = m_part_share->get_table_part(0); |
| 4044 |
error = ha_innobase::external_lock(thd, lock_type); |
| 4045 |
|
| 4046 |
for (uint i = 0; i < m_tot_parts; i++) { |
| 4047 |
dict_table_t *table = m_part_share->get_table_part(i); |
| 4048 |
|
| 4049 |
switch (table->quiesce) { |
| 4050 |
case QUIESCE_START: |
| 4051 |
/* Check for FLUSH TABLE t WITH READ LOCK */ |
| 4052 |
if (!srv_read_only_mode && thd_sql_command(thd) == SQLCOM_FLUSH && |
| 4053 |
lock_type == F_RDLCK) { |
| 4054 |
ut_ad(table->quiesce == QUIESCE_START); |
| 4055 |
|
| 4056 |
if (dict_table_is_discarded(table)) { |
| 4057 |
ib_senderrf(m_prebuilt->trx->mysql_thd, IB_LOG_LEVEL_ERROR, |
| 4058 |
ER_TABLESPACE_DISCARDED, table->name.m_name); |
| 4059 |
|
| 4060 |
return (HA_ERR_NO_SUCH_TABLE); |
| 4061 |
} |
| 4062 |
|
| 4063 |
row_quiesce_table_start(table, m_prebuilt->trx); |
| 4064 |
|
| 4065 |
/* Use the transaction instance to track |
| 4066 |
UNLOCK TABLES. It can be done via START |
| 4067 |
TRANSACTION; too implicitly. */ |
| 4068 |
|
| 4069 |
++m_prebuilt->trx->flush_tables; |
| 4070 |
} |
| 4071 |
break; |
| 4072 |
|
| 4073 |
case QUIESCE_COMPLETE: |
| 4074 |
/* Check for UNLOCK TABLES; implicit or explicit |
| 4075 |
or trx interruption. */ |
| 4076 |
if (m_prebuilt->trx->flush_tables > 0 && |
| 4077 |
(lock_type == F_UNLCK || trx_is_interrupted(m_prebuilt->trx))) { |
| 4078 |
ut_ad(table->quiesce == QUIESCE_COMPLETE); |
| 4079 |
row_quiesce_table_complete(table, m_prebuilt->trx); |
| 4080 |
|
| 4081 |
ut_a(m_prebuilt->trx->flush_tables > 0); |
| 4082 |
--m_prebuilt->trx->flush_tables; |
| 4083 |
} |
| 4084 |
break; |
| 4085 |
|
| 4086 |
case QUIESCE_NONE: |
| 4087 |
break; |
| 4088 |
|
| 4089 |
default: |
| 4090 |
ut_d(ut_error); |
| 4091 |
} |
| 4092 |
} |
| 4093 |
|
| 4094 |
ut_ad(!m_auto_increment_lock); |
| 4095 |
ut_ad(!m_auto_increment_safe_stmt_log_lock); |
| 4096 |
|
| 4097 |
if (m_prebuilt->sql_stat_start) { |
| 4098 |
m_sql_stat_start_parts.set(); |
| 4099 |
} else { |
| 4100 |
m_sql_stat_start_parts.reset(); |
| 4101 |
} |
| 4102 |
m_reuse_mysql_template = false; |
| 4103 |
return (error); |
| 4104 |
} |
| 4105 |
void ha_innopart::get_auto_increment(ulonglong, ulonglong increment, |
| 4106 |
ulonglong nb_desired_values, |
| 4107 |
ulonglong *first_value, |
| 4108 |
ulonglong *nb_reserved_values) { |
| 4109 |
DBUG_TRACE; |
| 4110 |
if (table_share->next_number_keypart != 0) { |
| 4111 |
/* Only first key part allowed as autoinc for InnoDB tables! */ |
| 4112 |
*first_value = ULLONG_MAX; |
| 4113 |
ut_d(ut_error); |
| 4114 |
ut_o(return); |
| 4115 |
} |
| 4116 |
get_auto_increment_first_field(increment, nb_desired_values, first_value, |
| 4117 |
nb_reserved_values); |
| 4118 |
} |
| 4119 |
|
| 4120 |
/** Compares two 'refs'. |
| 4121 |
A 'ref' is the (internal) primary key value of the row. |
| 4122 |
If there is no explicitly declared non-null unique key or a primary key, then |
| 4123 |
InnoDB internally uses the row id as the primary key. |
| 4124 |
It will use the partition id as secondary compare. |
| 4125 |
@param[in] ref1 An (internal) primary key value in the MySQL key value |
| 4126 |
format. |
| 4127 |
@param[in] ref2 Reference to compare with (same type as ref1). |
| 4128 |
@return < 0 if ref1 < ref2, 0 if equal, else > 0. */ |
| 4129 |
int ha_innopart::cmp_ref(const uchar *ref1, const uchar *ref2) const { |
| 4130 |
int cmp; |
| 4131 |
|
| 4132 |
cmp = ha_innobase::cmp_ref(ref1 + PARTITION_BYTES_IN_POS, |
| 4133 |
ref2 + PARTITION_BYTES_IN_POS); |
| 4134 |
|
| 4135 |
if (cmp != 0) { |
| 4136 |
return (cmp); |
| 4137 |
} |
| 4138 |
|
| 4139 |
cmp = static_cast<int>(uint2korr(ref1)) - static_cast<int>(uint2korr(ref2)); |
| 4140 |
|
| 4141 |
return (cmp); |
| 4142 |
} |
| 4143 |
|
| 4144 |
void ha_innopart::clear_blob_heaps() { |
| 4145 |
DBUG_TRACE; |
| 4146 |
if (m_parts == nullptr) { |
| 4147 |
return; |
| 4148 |
} |
| 4149 |
|
| 4150 |
for (uint i = 0; i < m_tot_parts; i++) { |
| 4151 |
auto &part{m_parts[i]}; |
| 4152 |
if (part.m_blob_heap != nullptr) { |
| 4153 |
DBUG_PRINT("ha_innopart", ("freeing blob_heap: %p", part.m_blob_heap)); |
| 4154 |
mem_heap_free(part.m_blob_heap); |
| 4155 |
part.m_blob_heap = nullptr; |
| 4156 |
} |
| 4157 |
} |
| 4158 |
|
| 4159 |
/* Reset blob_heap in m_prebuilt after freeing all heaps. It is set in |
| 4160 |
ha_innopart::set_partition to the blob heap of current partition. */ |
| 4161 |
m_prebuilt->blob_heap = nullptr; |
| 4162 |
} |
| 4163 |
|
| 4164 |
/** Reset state of file to after 'open'. This function is called |
| 4165 |
after every statement for all tables used by that statement. */ |
| 4166 |
int ha_innopart::reset() { |
| 4167 |
DBUG_TRACE; |
| 4168 |
|
| 4169 |
clear_blob_heaps(); |
| 4170 |
|
| 4171 |
return ha_innobase::reset(); |
| 4172 |
} |
| 4173 |
|
| 4174 |
/** Read row using position using given record to find. |
| 4175 |
This works as position()+rnd_pos() functions, but does some |
| 4176 |
extra work,calculating m_last_part - the partition to where |
| 4177 |
the 'record' should go. |
| 4178 |
Only useful when position is based on primary key |
| 4179 |
(HA_PRIMARY_KEY_REQUIRED_FOR_POSITION). |
| 4180 |
@param[in] record Current record in MySQL Row Format. |
| 4181 |
@return 0 for success else error code. */ |
| 4182 |
int ha_innopart::rnd_pos_by_record(uchar *record) { |
| 4183 |
int error; |
| 4184 |
DBUG_TRACE; |
| 4185 |
assert(ha_table_flags() & HA_PRIMARY_KEY_REQUIRED_FOR_POSITION); |
| 4186 |
/* TODO: Support HA_READ_BEFORE_WRITE_REMOVAL */ |
| 4187 |
/* Set m_last_part correctly. */ |
| 4188 |
if (unlikely(get_part_for_delete(record, m_table->record[0], m_part_info, |
| 4189 |
&m_last_part))) { |
| 4190 |
return HA_ERR_INTERNAL_ERROR; |
| 4191 |
} |
| 4192 |
|
| 4193 |
/* Init only the partition in which row resides */ |
| 4194 |
error = rnd_init_in_part(m_last_part, false); |
| 4195 |
if (error != 0) { |
| 4196 |
goto err; |
| 4197 |
} |
| 4198 |
|
| 4199 |
position(record); |
| 4200 |
error = handler::ha_rnd_pos(record, ref); |
| 4201 |
err: |
| 4202 |
rnd_end_in_part(m_last_part, false); |
| 4203 |
return error; |
| 4204 |
} |
| 4205 |
|
| 4206 |
/**************************************************************************** |
| 4207 |
DS-MRR implementation |
| 4208 |
***************************************************************************/ |
| 4209 |
|
| 4210 |
/* TODO: move the default implementations into the base handler class! */ |
| 4211 |
/* TODO: See if it could be optimized for partitioned tables? */ |
| 4212 |
/* Use default ha_innobase implementation for now... */ |