From fdd004c50de6131be193044ae40424d691425a81 Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 18:28:16 +0000 Subject: db, watch: two N+1s and a version rule with one owner MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit m-17. `watch::staleness::dependents_of` walked the index and asked for each package's recipe by name — one query per package, on the path that runs after every publish, with N equal to the size of the index. `db::index_recipes` returns the whole table in one query now. While there: a recipe that fails to parse is no longer treated as "does not depend on this package". An unreadable srcinfo used to hide a dependent from staleness by returning `None` into an `unwrap_or(false)`; now it is a reason to look. `db::builds::requeue_blocked` asked "is this name in the index?" once per dependency of every blocked build, inside the dispatcher's hot loop. The set of in-index names is read once per pass and tested in memory. Same answer, one query. m-18. `approved_but_unbuilt` assembled the version a human reads with two `CASE WHEN epoch='0'` expressions in SQL — a second, independent copy of the `epoch:pkgver-pkgrel` rule that `pkgver::full_version` owns. Two copies of a formatting rule is how the Attention page ends up describing versions differently from every other page. The query returns the parts; Rust formats them. A guard test fails if version-string concatenation reappears in SQL. --- src/db/queries.rs | 81 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 21 deletions(-) (limited to 'src/db/queries.rs') diff --git a/src/db/queries.rs b/src/db/queries.rs index 66bab73..62c1045 100644 --- a/src/db/queries.rs +++ b/src/db/queries.rs @@ -928,18 +928,26 @@ pub fn served_vs_offered_pkgvers(db: &Db) -> Result Result> { +/// Every in-index package's recipe JSON, as (name, srcinfo_json). +/// +/// One query, not one per package (m-17). `watch::staleness::dependents_of` +/// wants the whole table: asking for each package by name was an N+1 whose N is +/// the size of the index, on a code path that runs after every publish. +pub fn index_recipes(db: &Db) -> Result> { db.read(|c| { - Ok(c.query_row( - "SELECT s.srcinfo_json FROM package pk + let mut stmt = c.prepare( + "SELECT pk.name, s.srcinfo_json FROM package pk JOIN release r ON r.id = pk.release_id JOIN candidate cnd ON cnd.id = r.candidate_id JOIN snapshot s ON s.id = cnd.snapshot_id - WHERE pk.name = ?1 AND pk.state='in_index'", - params![name], - |r| r.get::<_, String>(0), - ) - .optional()?) + WHERE pk.state='in_index'", + )?; + let rows = stmt.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) }) } @@ -1566,16 +1574,20 @@ pub struct Unbuilt { pub fn approved_but_unbuilt(db: &Db) -> Result> { db.read(|c| { + // The version strings are assembled in Rust by `pkgver::full_version`, + // not by a CASE expression here (m-18): the epoch-omitted form is a rule + // of ours, and a second copy of it in SQL is a second rule that can + // disagree with the first. let mut stmt = c.prepare( "SELECT p.name, - (SELECT (CASE WHEN c.epoch='0' THEN '' ELSE c.epoch||':' END) || c.pkgver || '-' || c.pkgrel - FROM candidate c WHERE c.id = b.candidate_id), - COALESCE((SELECT (CASE WHEN r.epoch='0' THEN '' ELSE r.epoch||':' END) || r.pkgver || '-' || r.pkgrel - FROM release r JOIN pkgbase pb ON pb.current_accepted_release_id = r.id - WHERE pb.id = p.id AND r.state='published'), ''), + cnd.epoch, cnd.pkgver, cnd.pkgrel, + ser.epoch, ser.pkgver, ser.pkgrel, COALESCE(b.error, ''), b.id FROM build b JOIN pkgbase p ON p.id = b.pkgbase_id + LEFT JOIN candidate cnd ON cnd.id = b.candidate_id + LEFT JOIN release ser ON ser.id = p.current_accepted_release_id + AND ser.state='published' WHERE b.state = 'failed' AND b.candidate_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM release r WHERE r.candidate_id = b.candidate_id) @@ -1585,17 +1597,44 @@ pub fn approved_but_unbuilt(db: &Db) -> Result> { ORDER BY p.name", )?; let rows = stmt.query_map([], |r| { - Ok(Unbuilt { - pkgbase: r.get(0)?, - approved: r.get(1)?, - serving: r.get(2)?, - error: r.get(3)?, - build_id: r.get(4)?, - }) + Ok(( + r.get::<_, Option>(0)?, + r.get::<_, Option>(1)?, + r.get::<_, Option>(2)?, + r.get::<_, Option>(3)?, + r.get::<_, Option>(4)?, + r.get::<_, Option>(5)?, + r.get::<_, Option>(6)?, + r.get::<_, String>(7)?, + r.get::<_, String>(8)?, + )) })?; let mut out = Vec::new(); for r in rows { - out.push(r?); + let (pkgbase, ae, av, ar, se, sv, sr, error, build_id) = r?; + let (pkgbase, av, ar) = match (pkgbase, av, ar) { + (Some(p), Some(v), Some(rel)) => (p, v, rel), + // The join guarantees a pkgbase, and a failed build with a + // candidate always has one; a missing version means the schema + // was violated, so say so rather than show a blank. + _ => { + return Err(crate::error::Error::Db( + "failed build row without a candidate version".into(), + )) + } + }; + out.push(Unbuilt { + pkgbase, + approved: crate::pkgver::full_version(&ae.unwrap_or_default(), &av, &ar), + serving: match (sv, sr) { + (Some(v), Some(rel)) => { + crate::pkgver::full_version(&se.unwrap_or_default(), &v, &rel) + } + _ => String::new(), + }, + error, + build_id, + }); } Ok(out) }) -- cgit v1.2.3