We're moving an existing DB to a new SQL Server 2019 instance.
If we change the compatibility level to 150, nothing works. Task manager shows that sql server service is consuming all available memory and all queries time out with an error that says
There is insufficient system memory in resource pool internal to run this query
The RAM is completely consumed until no query can be run (all end with the previous error I've mentioned). We've tried with 16, 32 and 64GB of RAM.
Can anyone give any tips on how to debug this?
Notice that it's not a query issue…just changing the compat level is enough to make SQL start exhausting the memory.
Even running a simple scalar function (which internally uses several Substring and cast calls – no table or view selects) is enough to make it consume all the available memory.
Best Answer
You're encountering a known bug with scalar UDF inlining. You can disable inlining using one of the methods here (or by using a lower compat level, as you've discovered yourself):
Disabling Scalar UDF Inlining without changing the compatibility level
Or install CU2 for the permanent fix.
Reading this part of your issue reminded me of this really interesting blog post from sqL_handLe:
SQL Server 2019 Scalar UDF inlining - OOM in some cases
He demonstrates a scalar UDF that doesn't do any data access, but uses up all the memory on the box (even one with almost a TB of RAM):
This should be fixed in SQL Server 2019 CU2, it looks like this is the KB for it:
FIX: Scalar UDF Inlining issues in SQL Server 2019
If you're unable to install CU2 for some reason, you can use the newer compatibility level as long as you disable scalar UDF inling at the database level like this:
Or at the query level, as described in the docs article I linked to earlier.