-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EFCore generate wrong sql when using UseRowNumberForPaging under concurrency requests #13935
Comments
@hbwang518 Could you please comment on issue #13959 and let us know why you are using RowNumberForPaging? |
I believe I may be running into the same problem - in certain high volume cases we start getting errors like: SQL 2008 R2 |
Do you have any news on this issue? I got the same error when testing my application with the UseRowNumberForPaging flag set to true. I need to use that flag because one of the installations of my application needs to target Sql Server 2008, and the OFFSET and FETCH keywords that EFCore otherwise generates are obviously not compatible with that version... |
I just discovered this issue after updating my apps to 2.2.3. |
@mailivore This issue is in the Backlog milestone. This means that it is not going to happen for the 3.0 release. We will re-assess the backlog following the 3.0 release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Also, I'm curious if you're planning to continue using SQL Server 2008 after it is out of support next month? |
@ajcvickers Sorry for jumping in, but as I said last month I have the same issue and I'm following the discussion.
In my case that's not in my hands, my application will be deployed at my client's premise. They have plans to migrate to a newer version of SQL Server but it won't happen before this fall in the best case (and I'm pretty sure it will take much more time), and in the meantime they are not able to use my application because of this bug... |
Not in my hands also. |
@aiscrim @mailivore Thanks for the info. |
RowNumberPagingExpressionVisitor in SqlServerQuerySqlGenerator's constructor leads to races in SelectExpression: if you have 2+ same "skip/take" queries executing in parallel at the same time, RowNumberPagingExpressionVisitor modifies the same SelectExpression object in 2+ threads which leads to an exception or invalid sql generated. I do not know reasons why RowNumberPagingExpressionVisitor was put in SqlServerQuerySqlGenerator but probably it would be better to move it to the query compilation stage as everything (except parameters?) related to queries should be immutable at the sql generation stage. As for now there is a simple fix for the issue: using a clone of SelectExpression to pass into a sql generator also RowNumberPagingExpressionVisitor in SqlServerQuerySqlGenerator leads to races in RelationalQueryCompilationContext (generation of unique table names) as it is unsafe to access SelectExpression's _queryCompilationContext field outside of the query compiler: |
@andreichuk If you have root-caused the issue, then would you consider submitting a pull request for the fix? |
@ajcvickers any chance to have a new version with the fix released before 3.0? I have tested my code with 2.2.5 version only |
@andreichuk This isn't currently planned for 3.0, but if we get a PR that fixes the issue then that could still make it in. |
This functionality hasn't been ported to 3.0 yet. |
@OpenSpacesAndPlaces |
@EzrealJ The only real mitigation, while keeping the same code in play, would be to prevent duplicate simultaneous requests by custom memory caching or something along those lines. IMO, probably the best fix if you don't have a heavy dependency on dynamics would be just to use like: raw SQL, Stored Procedures, or DB Functions. For small datasets, paging in-memory after generating the full data might be an option. TLDR: You're looking for a work-around that best fits your use-case. Hope this helps! |
@OpenSpacesAndPlaces |
Closing old issue as this is no longer something we intend to implement. |
The SQL EFCore generated is as below.
In concurrency case, the issue can be reproduced with high change. The sql EFCore generated are different for same query and many of them are wrong.
Steps to reproduce
Create dbContext with codes below.
Start two threads and generate new DbContext and call query in each thread. The query is as below.
Further technical details
EF Core version: (2.1.1) (The issue can be reproduced under 2.1.4)
Database Provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Operating system: Windows 10 Pro
IDE: (e.g. Visual Studio 2017 15.7.4)
The text was updated successfully, but these errors were encountered: