Deduplicate stl_querytext rows, fixes #8867 (#9870)

* Deduplicate stl_querytext rows, fixes #8867

* Update queries.py

Filter down result-set by joining with queries

* Reduce line character length
This commit is contained in:
Noe Alejandro Perez Dominguez 2023-01-27 10:36:49 +01:00 committed by GitHub
parent 4a5676d617
commit d8674bd7f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,12 +31,19 @@ REDSHIFT_SQL_STATEMENT = textwrap.dedent(
AND starttime < '{end_time}' AND starttime < '{end_time}'
LIMIT {result_limit} LIMIT {result_limit}
), ),
deduped_querytext AS (
-- Sometimes rows are duplicated, causing LISTAGG to fail in the full_queries CTE.
SELECT DISTINCT qt.*
FROM pg_catalog.stl_querytext AS qt
INNER JOIN queries AS q
ON qt.query = q.query
),
full_queries AS ( full_queries AS (
SELECT SELECT
query, query,
LISTAGG(CASE WHEN LEN(RTRIM(text)) = 0 THEN text ELSE RTRIM(text) END, '') LISTAGG(CASE WHEN LEN(RTRIM(text)) = 0 THEN text ELSE RTRIM(text) END, '')
WITHIN GROUP (ORDER BY sequence) AS query_text WITHIN GROUP (ORDER BY sequence) AS query_text
FROM pg_catalog.stl_querytext FROM deduped_querytext
WHERE sequence < 327 -- each chunk contains up to 200, RS has a maximum str length of 65535. WHERE sequence < 327 -- each chunk contains up to 200, RS has a maximum str length of 65535.
GROUP BY query GROUP BY query
), ),