[Do Not Merge]5755-add long query commands #5800
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary (required)
This PR adds commands to manually check for and clear long running queries. I added logic to make sure that we are only killing queries in the read replica in prod.
Here's the dashboard for check_queries
If we use db.engine our logic routes to the default engine created by flask-sqlalchemy and points to SQLA_CONN. If we use db.session our logic goes through the follower logic, and IF the session is non-flushing the command will go to the read replica. We can test this on our locals by setting SQLA_FOLLOWERS. If you don't have SQLA_FOLLOWERS set, it will run against SQLA_CONN and if you don't have either set, it will run against cfdm_test.
Required reviewers
3 devs
Impacted areas of the application
General components of the application that this PR will affect:
How to test
Locally:
'export SQLA_FOLLOWERS=postgresql://:@/cfdm_test'
WARNING: Unsetting SQLA_CONN will be priority over exporting SQLA_FOLLOWERS.
'export SLACK_HOOK="slack hook here"'
check_long_queries (lines 160-171):
SQL = """
SELECT *
FROM pg_stat_activity
WHERE state = 'active'
and lower(query) like 'select %'
and lower(query) not like '%refresh%'
and lower(query) not like '%rollback%'
and (now() - pg_stat_activity.query_start) >= interval '{} minutes'
order by pg_stat_activity.query_start desc;
"""
clear_long_queries (lines 199-210):
SQL = """
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = 'active'
and lower(query) like 'select %'
and lower(query) not like '%refresh%'
and lower(query) not like '%rollback%'
and (now() - pg_stat_activity.query_start) >= interval '{} minutes'
order by pg_stat_activity.query_start desc;
"""
You should see the output in test-bot and in your terminal
You should see the output in test-bot
You can test running intervals lower than 2 (will create an error) or without an interval (will default to 5)
You can also run multiple long queries.
Deploy to a space: