11/* ****************************************************************************
22
33Copyright (c) 2017, 2025, Oracle and/or its affiliates.
4+ Copyright (c) 2025, buildup-db.
45
56This program is free software; you can redistribute it and/or modify it under
67the terms of the GNU General Public License, version 2.0, as published by the
@@ -399,7 +400,7 @@ int dd_table_open_on_dd_obj(THD *thd, dd::cache::Dictionary_client *client,
399400
400401 ut_ad (table_id != dd::INVALID_OBJECT_ID);
401402
402- dict_sys_mutex_enter ();
403+ dict_sys_s_lock ();
403404
404405 HASH_SEARCH (id_hash, dict_sys->table_id_hash , hash_value, dict_table_t *,
405406 table, ut_ad (table->cached ), table->id == table_id);
@@ -408,7 +409,7 @@ int dd_table_open_on_dd_obj(THD *thd, dd::cache::Dictionary_client *client,
408409 table->acquire ();
409410 }
410411
411- dict_sys_mutex_exit ();
412+ dict_sys_s_unlock ();
412413
413414 if (table != nullptr ) {
414415 return 0 ;
@@ -697,17 +698,19 @@ hold Shared MDL lock on it.
697698table_id was found) mdl=NULL if we are resurrecting table IX locks in recovery
698699@param[in] dict_locked dict_sys mutex is held
699700@param[in] check_corruption check if the table is corrupted or not.
701+ @param[in] for_modification if false, dict_locked means s-lock.
700702@return table
701703@retval NULL if the table does not exist or cannot be opened */
702704dict_table_t *dd_table_open_on_id (table_id_t table_id, THD *thd,
703705 MDL_ticket **mdl, bool dict_locked,
704- bool check_corruption) {
706+ bool check_corruption,
707+ bool for_modification) {
705708 dict_table_t *ib_table;
706709 const auto hash_value = ut::hash_uint64 (table_id);
707710 char full_name[MAX_FULL_NAME_LEN + 1 ];
708711
709712 if (!dict_locked) {
710- dict_sys_mutex_enter ();
713+ dict_sys_s_lock ();
711714 }
712715
713716 HASH_SEARCH (id_hash, dict_sys->table_id_hash , hash_value, dict_table_t *,
@@ -721,12 +724,12 @@ dict_table_t *dd_table_open_on_id(table_id_t table_id, THD *thd,
721724 space_id_t space_id = dict_sdi_get_space_id (table_id);
722725
723726 /* Create in-memory table object for SDI table */
724- dict_index_t *sdi_index =
725- dict_sdi_create_idx_in_mem ( space_id, false , 0 , false );
727+ dict_index_t *sdi_index = dict_sdi_create_idx_in_mem (
728+ space_id, false , 0 , false , dict_locked && for_modification );
726729
727730 if (sdi_index == nullptr ) {
728731 if (!dict_locked) {
729- dict_sys_mutex_exit ();
732+ dict_sys_s_unlock ();
730733 }
731734 return nullptr ;
732735 }
@@ -737,15 +740,24 @@ dict_table_t *dd_table_open_on_id(table_id_t table_id, THD *thd,
737740 ib_table->acquire ();
738741
739742 if (!dict_locked) {
743+ /* dict_sdi_create_idx_in_mem() re-obtains with x-lock. */
740744 dict_sys_mutex_exit ();
741745 }
742746 } else {
743- dict_sys_mutex_exit ();
747+ if (dict_locked && for_modification) {
748+ dict_sys_mutex_exit ();
749+ } else {
750+ dict_sys_s_unlock ();
751+ }
744752
745753 ib_table = dd_table_open_on_id_low (thd, mdl, table_id);
746754
747755 if (dict_locked) {
748- dict_sys_mutex_enter ();
756+ if (for_modification) {
757+ dict_sys_mutex_enter ();
758+ } else {
759+ dict_sys_s_lock ();
760+ }
749761 }
750762 }
751763#else /* !UNIV_HOTBACKUP */
@@ -762,7 +774,7 @@ dict_table_t *dd_table_open_on_id(table_id_t table_id, THD *thd,
762774 }
763775
764776 if (!dict_locked) {
765- dict_sys_mutex_exit ();
777+ dict_sys_s_unlock ();
766778 }
767779 } else {
768780 for (;;) {
@@ -778,12 +790,20 @@ dict_table_t *dd_table_open_on_id(table_id_t table_id, THD *thd,
778790
779791 ut_ad (!ib_table->is_temporary ());
780792
781- dict_sys_mutex_exit ();
793+ if (dict_locked && for_modification) {
794+ dict_sys_mutex_exit ();
795+ } else {
796+ dict_sys_s_unlock ();
797+ }
782798
783799#ifndef UNIV_HOTBACKUP
784800 if (db_str.empty () || tbl_str.empty ()) {
785801 if (dict_locked) {
786- dict_sys_mutex_enter ();
802+ if (for_modification) {
803+ dict_sys_mutex_enter ();
804+ } else {
805+ dict_sys_s_lock ();
806+ }
787807 }
788808 return nullptr ;
789809 }
@@ -797,7 +817,11 @@ dict_table_t *dd_table_open_on_id(table_id_t table_id, THD *thd,
797817#endif /* !UNIV_HOTBACKUP */
798818
799819 /* Re-lookup the table after acquiring MDL. */
800- dict_sys_mutex_enter ();
820+ if (dict_locked && for_modification) {
821+ dict_sys_mutex_enter ();
822+ } else {
823+ dict_sys_s_lock ();
824+ }
801825
802826 HASH_SEARCH (id_hash, dict_sys->table_id_hash , hash_value, dict_table_t *,
803827 ib_table, ut_ad (ib_table->cached ), ib_table->id == table_id);
@@ -832,7 +856,11 @@ dict_table_t *dd_table_open_on_id(table_id_t table_id, THD *thd,
832856 }
833857 }
834858
835- dict_sys_mutex_exit ();
859+ if (dict_locked && for_modification) {
860+ dict_sys_mutex_exit ();
861+ } else {
862+ dict_sys_s_unlock ();
863+ }
836864 break ;
837865 }
838866
@@ -857,11 +885,16 @@ dict_table_t *dd_table_open_on_id(table_id_t table_id, THD *thd,
857885#endif /* !UNIV_HOTBACKUP */
858886
859887 if (dict_locked) {
860- dict_sys_mutex_enter ();
888+ if (for_modification) {
889+ dict_sys_mutex_enter ();
890+ } else {
891+ dict_sys_s_lock ();
892+ }
861893 }
862894 }
863895
864- ut_ad (dict_locked == dict_sys_mutex_own ());
896+ ut_ad (dict_locked ==
897+ (for_modification ? dict_sys_mutex_own () : dict_sys_s_lock_own ()));
865898
866899 return ib_table;
867900}
0 commit comments