A search on Postgres starts from your feeds, not every feed's matches (#472)

After #459 a common word still took about 2 s: "apple" matched 61,674
items across every feed on the server, Postgres re-read each to confirm
the trigram match, and only then joined to the 1,151 in the searcher's
own feeds (EXPLAIN on production, 2026-11-17). The subquery
`e.feed_id IN (SELECT ... FROM subscriptions ...)` hid the feed list
from the planner, so the trigram index was always its way in.

Given as `e.feed_id = ANY(ARRAY(SELECT ...))`, the planner sees the
feeds and chooses per search: for "apple" this person's 20,152 items by
entries_seen, count 1.5 s to 0.38 s and the page of 50 1.1 s to 0.52 s;
for "kubernetes" still the trigram index, 21 ms; "the" 141 ms. Only for
a search, so ordinary lists keep their plans, and only on Postgres:
SQLite has no arrays and keeps the IN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 02:14:16 +00:00
parent 54bfa5faf6
commit 2ebe77b21e
2 changed files with 35 additions and 18 deletions

View File

@@ -32,7 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Searching All Subscriptions answers in a fraction of a second, not five: a search reads each item's title and text without its markup, indexed on Postgres, and still finds part of a word. It no longer matches words inside the HTML's tags, such as a link's address. Text past the first 4,000 characters of a very long post is not searched (#459).
- Searching All Subscriptions answers in a fraction of a second, not five: a search reads each item's title and text without its markup, indexed on Postgres, and still finds part of a word. It no longer matches words inside the HTML's tags, such as a link's address. Text past the first 4,000 characters of a very long post is not searched (#459, #472).
- Artwork that cannot be shown is asked for again a day later rather than every hour: one too large is refused on its stated size before it is read, and a refusal from the publisher or one ipx makes (too large, not an image) is remembered for a day; JPEG XL pictures sent without a type are shown (#448).
- `ipx reap --dry-run` says "would delete" of each file and of the total, rather than "deleted", word for word a real reap, of files it kept (#461).
- Subscribing to a feed someone here already reads puts its recent items to your Signals at once, rather than at the judge's next pass, up to five minutes later (#415).

View File

@@ -1315,6 +1315,9 @@ impl Args {
/// The FROM and WHERE that `entries_in` and `count_in` share: whose read state, which feeds
/// (one, or every feed the person subscribes to), the filter, and a case-insensitive search of
/// title and description.
// Each is one of the list's own dimensions, as entries_where takes them; a struct of them would be
// built only to be taken apart here.
#[allow(clippy::too_many_arguments)]
fn entries_from(
a: &mut Args,
user_id: i64,
@@ -1323,8 +1326,17 @@ fn entries_from(
search: Option<&str>,
guid: Option<&str>,
signal: Option<i64>,
pg: bool,
) -> String {
let user = a.p(user_id);
// A search on Postgres is given this person's feeds as an array, not a subquery: with the
// subquery it matched the words in every feed on the server and joined to theirs after, 61,674
// items re-read for 1,151 of theirs, 1.5 s for "apple"; with the array it picks per search the
// feeds' index or the words', 0.4 s and 21 ms (#472). SQLite has no arrays and keeps the IN.
let searching = search.is_some_and(|q| !q.trim().is_empty());
let mine = |sub: String| {
if pg && searching { format!("e.feed_id = ANY(ARRAY({sub}))") } else { format!("e.feed_id IN ({sub})") }
};
let mut scope = match feeds {
// One feed as before, so its page keeps its plan: an OR with the shows' list, or an IN
// over a subquery, took clarkesworld's from 10 ms to 26 ms or a second (#283).
@@ -1334,19 +1346,15 @@ fn entries_from(
// A Signal and All Subscriptions leave out what this person hid from them (#288, #289).
// An episode under way still shows in Currently Listening, which is All Subscriptions'
// in-progress filter.
None if signal.is_some() => {
format!(
"e.feed_id IN (SELECT m.feed_id FROM subscriptions m WHERE m.user_id = {user} AND {})",
None if signal.is_some() => mine(format!(
"SELECT m.feed_id FROM subscriptions m WHERE m.user_id = {user} AND {}",
shown_in("hide_from_signals", "m")
)
}
None if filter != Filter::InProgress => {
format!(
"e.feed_id IN (SELECT m.feed_id FROM subscriptions m WHERE m.user_id = {user} AND {})",
)),
None if filter != Filter::InProgress => mine(format!(
"SELECT m.feed_id FROM subscriptions m WHERE m.user_id = {user} AND {}",
shown_in("hide_from_all", "m")
)
}
None => format!("e.feed_id IN (SELECT feed_id FROM subscriptions WHERE user_id = {user})"),
)),
None => mine(format!("SELECT feed_id FROM subscriptions WHERE user_id = {user}")),
};
if let Some(id) = signal {
scope = format!(
@@ -1577,8 +1585,10 @@ impl Db {
pub async fn signal_count(&self, user_id: i64, signal: i64, filter: Filter, search: Option<&str>) -> Result<i64> {
let mut a = Args::default();
let sql =
format!("SELECT count(*) AS n {}", entries_from(&mut a, user_id, None, filter, search, None, Some(signal)));
let sql = format!(
"SELECT count(*) AS n {}",
entries_from(&mut a, user_id, None, filter, search, None, Some(signal), self.pg())
);
let r = self.rows(&sql, a.0).await?;
Ok(r.first().context("count(*) always returns a row")?.try_get("", "n")?)
}
@@ -1607,7 +1617,7 @@ impl Db {
order: &str,
) -> Result<Vec<EntryRow>> {
let mut a = Args::default();
let from = entries_from(&mut a, user_id, feeds, filter, search, guid, signal);
let from = entries_from(&mut a, user_id, feeds, filter, search, guid, signal, self.pg());
// The page's keys first, then its rows: the sort then needs only `entries_by_time`, not
// every item's row, and the show notes are read for the page alone (#188).
let (limit, offset) = (a.p(limit), a.p(offset));
@@ -1708,7 +1718,10 @@ impl Db {
unread_in_sub!()
)
} else {
format!("SELECT count(*) AS n {}", entries_from(&mut a, user_id, feeds, filter, search, None, None))
format!(
"SELECT count(*) AS n {}",
entries_from(&mut a, user_id, feeds, filter, search, None, None, self.pg())
)
};
let r = self.rows(&sql, a.0).await?;
Ok(r.first().context("count(*) always returns a row")?.try_get("", "n")?)
@@ -1825,6 +1838,10 @@ impl Db {
Ok(n)
}
fn pg(&self) -> bool {
self.orm.get_database_backend() == sea_orm::DbBackend::Postgres
}
async fn rows(&self, sql: &str, values: Vec<sea_orm::Value>) -> Result<Vec<sea_orm::QueryResult>> {
let (stmt, started) = (self.stmt(sql, values), std::time::Instant::now());
let rows = self.orm.query_all_raw(stmt).await?;