PR#2567: hub: use CTE for build_references

Merges #2567
https://pagure.io/koji/pull-request/2567

Fixes: #2535
https://pagure.io/koji/issue/2535
koji-gc trash traceback
This commit is contained in:
Tomas Kopecek 2020-11-24 13:45:28 +01:00
commit c39f6e9afa

View file

@ -8000,27 +8000,31 @@ def build_references(build_id, limit=None, lazy=False):
if build_rpm_ids:
# psql planner gots confused if buildroot table is large (>trillion)
# and len(rpm_ids) > ~500. In such case it switched to looped sequential scans
_dml("SET enable_hashjoin=off", {})
q = """SELECT MAX(create_event)
FROM standard_buildroot
WHERE buildroot_id IN (
SELECT buildroot_id
# using "SET enabled_hashjoin=off" improved it for some cases. CTE could be slower for
# simple cases but would improve complicated ones.
q = """WITH buildroot_ids as (
SELECT DISTINCT buildroot_id
FROM buildroot_listing
WHERE rpm_id IN %(rpm_ids)s
)"""
event_id = (_fetchSingle(q, {'rpm_ids': build_rpm_ids}) or (0,))[0] or 0
_dml("SET enable_hashjoin=on", {})
if build_archive_ids:
_dml("SET enable_hashjoin=off", {})
q = """SELECT MAX(create_event)
)
SELECT MAX(create_event)
FROM standard_buildroot
WHERE buildroot_id IN (
SELECT buildroot_id
SELECT buildroot_id FROM buildroot_ids
)"""
event_id = (_fetchSingle(q, {'rpm_ids': build_rpm_ids}) or (0,))[0] or 0
if build_archive_ids:
q = """WITH buildroot_ids as (
SELECT DISTINCT buildroot_id
FROM buildroot_archives
WHERE archive_id IN %(archive_ids)s
)
SELECT MAX(create_event)
FROM standard_buildroot
WHERE buildroot_id IN (
SELECT buildroot_id FROM buildroot_ids
)"""
event_id2 = (_fetchSingle(q, {'archive_ids': build_archive_ids}) or (0,))[0] or 0
_dml("SET enable_hashjoin=on", {})
event_id = max(event_id, event_id2)
if event_id:
q = """SELECT EXTRACT(EPOCH FROM get_event_time(%(event_id)i))"""