Skip to content
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

Write sql query detection for Postgres extremely slow #45234

Closed
urmomwx opened this issue Jun 1, 2022 · 3 comments · Fixed by #45237
Closed

Write sql query detection for Postgres extremely slow #45234

urmomwx opened this issue Jun 1, 2022 · 3 comments · Fixed by #45237

Comments

@urmomwx
Copy link

urmomwx commented Jun 1, 2022

Steps to reproduce

Feed reasonably large SQL (in my case ~ 600 lines) file with multiple query statements to ActiveRecord::Base.connection.execute while using Postgresql as database adapter and wait for results. For a long time. It is working ok in Rails 6.0.4.8 but when migrating to rails 7.0.3 one of my tests hangs for more than 5 minutes.

To check it's the issue:

Fetched READ_QUERY from debugger just before the line that hangs:
https://github.com/rails/rails/blob/main/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb#L30

read_query_regex = /\A(?:[(\s]|(?m-ix:(?:--.*\n)|\/\*(?:[^*]|\*[^\/])*\*\/))*(?-mix:(?i-mx:close)|(?i-mx:declare)|(?i-mx:fetch)|(?i-mx:move)|(?i-mx:set)|(?i-mx:show)|(?i-mx:begin)|(?i-mx:commit)|(?i-mx:explain)|(?i-mx:release)|(?i-mx:rollback)|(?i-mx:savepoint)|(?i-mx:select)|(?i-mx:with))/
sql = STDIN.read
puts read_query_regex.match?(sql)

Expected behavior

It should perform the match in reasonable time, even in 1 second it would be fine

Actual behavior

It does not finish in at least 5 minutes keeping CPU at 100%.

System configuration

Rails version: 7.0.3

Ruby version: 3.1.2

@fatkodima
Copy link
Member

I solved a similar problem for the query with comments - #45012. That was merged in 7.0.3, but seems like not solving you problem.
Can you paste your query? Or some other query that reproduces you problem?

@urmomwx
Copy link
Author

urmomwx commented Jun 1, 2022

Sure. Had to anonymize this old legacy script a bit. It still seems to be related to comments, for example removing first header comment makes it perform in low tens of milliseconds

0001_sample.sql.txt

@ghiculescu
Copy link
Member

Could you see if removing the m helps for you? Ref #45011 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants