# Waiting for metadata lock on 'table', when running insert/ when duplicate update

**URL:** <https://www.singlestore.com/forum/t/waiting-for-metadata-lock-on-table-when-running-insert-when-duplicate-update/7481>\
**Category:** Procedural SQL\
**Created:** [February 7, 2024, 2:16pm UTC](https://www.singlestore.com/forum/t/waiting-for-metadata-lock-on-table-when-running-insert-when-duplicate-update/7481 "2024-02-07T14:16:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![moralez.rodrigo](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@moralez.rodrigo](https://www.singlestore.com/forum/u/moralez.rodrigo)\
**Post date:** [February 7, 2024, 2:16pm UTC](https://www.singlestore.com/forum/t/waiting-for-metadata-lock-on-table-when-running-insert-when-duplicate-update/7481/1 "2024-02-07T14:16:47Z")

</div>

We are running an insert/when duplicate update over a rowstore table, it has in total 2M rows. We tried updating into batches of 100k, 10k, 4k, 1k (with start transaction as well), for all the cases it reaches a point on which the rest of the queries that are being done into the same table are locked with the message “waiting for metadata lock on ‘table’” and it got stuck for a minute until all the updates finish.

Do you know why this is doing a metadata lock? Is there a way to do this update without locking the whole table?

Memsql version 7.0.16.

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [February 9, 2024, 12:41am UTC](https://www.singlestore.com/forum/t/waiting-for-metadata-lock-on-table-when-running-insert-when-duplicate-update/7481/2 "2024-02-09T00:41:37Z")

</div>

I don’t have a direct answer for you. However, see

SELECT \* FROM information\_schema.MV\_BLOCKED\_QUERIES;

to learn what’s waiting for what.

I’d suggest trying smaller batches, like 200 instead of 1000.

7.0 was end of life December 09, 2021. You really should upgrade.

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [February 9, 2024, 1:37am UTC](https://www.singlestore.com/forum/t/waiting-for-metadata-lock-on-table-when-running-insert-when-duplicate-update/7481/3 "2024-02-09T01:37:37Z")

</div>

I checked with one of our dev experts, and he said:

" I think `waiting for metadata lock on` can only happen if they are running some kind of ddl (or very least compiling new queries)

so I don’t think it should happen if they only run queries as described"

If you have different batch sizes (different number of literal rows in your INSERT ON DUP KEY UPDATE commands) it can cause a compile. So make sure you have only a few standard sizes, like only 1 row and 200 rows. That will limit compiles. And make sure no DDL is going on (no ALTERs or CREATE TABLE etc.).

---

<div class="post-metadata">

**Author:** ![moralez.rodrigo](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@moralez.rodrigo](https://www.singlestore.com/forum/u/moralez.rodrigo)\
**Post date:** [February 15, 2024, 7:38pm UTC](https://www.singlestore.com/forum/t/waiting-for-metadata-lock-on-table-when-running-insert-when-duplicate-update/7481/4 "2024-02-15T19:38:45Z")

</div>

Thanks @hanson I also tried changing the batch size but still the same problem. Anyway I found something insteresting. Right before the metadata lock I’m seeing this running over the table with the problem:

USING `schema_name` SELECT ORDERED\_SERIALIZE(`is_value`), …  
FROM `table_that_I'm_updating` WITH (sample\_ratio = 0.719020464041) OPTION(INTERPRETER\_MODE=INTERPRET\_FIRST)  
/_stats\_collection\_query_/

Could it be due to the stats collection? I updating almost every row on the table. If this is the reason, is there a way to avoid it?

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [February 16, 2024, 8:38pm UTC](https://www.singlestore.com/forum/t/waiting-for-metadata-lock-on-table-when-running-insert-when-duplicate-update/7481/5 "2024-02-16T20:38:45Z")

</div>

Let me check. Meanwhile, did you switch to a newer version?

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [February 16, 2024, 11:46pm UTC](https://www.singlestore.com/forum/t/waiting-for-metadata-lock-on-table-when-running-insert-when-duplicate-update/7481/6 "2024-02-16T23:46:34Z")

</div>

That query is due to stats collection; it is internally generated.

But we don’t have an answer for you at this point. The same issue was raised – it appears that some connection is doing DDL. If that is not the case, I don’t have a good explanation.

---

<div class="post-metadata">

**Author:** ![moralez.rodrigo](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@moralez.rodrigo](https://www.singlestore.com/forum/u/moralez.rodrigo)\
**Post date:** [February 21, 2024, 4:19pm UTC](https://www.singlestore.com/forum/t/waiting-for-metadata-lock-on-table-when-running-insert-when-duplicate-update/7481/7 "2024-02-21T16:19:52Z")

</div>

Found it! It was that @hanson. Thanks for your help.  
I resolved it by setting: **ALTER**  **TABLE** mylockedtable AUTOSTATS\_ENABLED = OFF;

Looks like we are updating most of the rows on the table and the optimizer “thinks” that we added a lot of rows to that table (But we know is not like that).  
Then the optimizer invalidates all the plans starts collecting the stats of the table, and puts all new queries on hold until it has the statistics to create the execution plan.

I will have to collect the stats manually every few weeks, but it’s not a problem. All my locks disappeared after this change and it’s working perfectly.

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [February 27, 2024, 12:44am UTC](https://www.singlestore.com/forum/t/waiting-for-metadata-lock-on-table-when-running-insert-when-duplicate-update/7481/8 "2024-02-27T00:44:32Z")

</div>

@moralez.rodrigo Thanks so much for posting your solution!
