Private
Public Access
Index Postgres search and harden SQLite search/concurrency (C5, C6)
Postgres recomputed a tsvector over every article's full text on every search. A migration now adds a stored, GIN-indexed search_vector column kept current by a trigger (so no ingest/update path can forget it), and the backend reads that column. Untested here: no Postgres in the dev sandbox, so the SQL is reviewed but not executed — worth one run of the (skipped-on-SQLite) Postgres test module against a real instance. SQLite search built one bound parameter per subscribed feed, so enough feeds would exceed the variable limit, and the OperationalError was swallowed into "no results". It now scopes through a subquery on Subscription and logs failures. SQLite connections also get WAL mode, a 20s busy timeout, and IMMEDIATE transactions so gunicorn, Celery, and the FTS triggers stop tripping over each other. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cb0ce5a336
commit
70b0d4f277
4 files changed
+104
-21
No files matched your search
@@ -0,0 +1,61 @@
|
||||
"""Stored, GIN-indexed tsvector for Postgres search (C5), kept current by a
|
||||
trigger so no ingestion/update code path can forget to maintain it.
|
||||
|
||||
No-ops entirely on SQLite — its FTS5 table (0001_sqlite_fts) already plays
|
||||
this role. The column is deliberately not a Django model field: a
|
||||
SearchVectorField would break SQLite, and nothing but the search backend
|
||||
ever reads it (apps/search/backends/postgres.py).
|
||||
"""
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
VECTOR_EXPR = (
|
||||
"setweight(to_tsvector('english', coalesce({row}title, '')), 'A') || "
|
||||
"setweight(to_tsvector('english', coalesce({row}content_html, '')), 'B')"
|
||||
)
|
||||
|
||||
CREATE = f"""
|
||||
ALTER TABLE articles_article ADD COLUMN IF NOT EXISTS search_vector tsvector;
|
||||
|
||||
UPDATE articles_article SET search_vector = {VECTOR_EXPR.format(row="")};
|
||||
|
||||
CREATE INDEX IF NOT EXISTS articles_article_search_vector_gin
|
||||
ON articles_article USING GIN (search_vector);
|
||||
|
||||
CREATE OR REPLACE FUNCTION articles_article_search_vector_update() RETURNS trigger AS $$
|
||||
BEGIN
|
||||
NEW.search_vector := {VECTOR_EXPR.format(row="NEW.")};
|
||||
RETURN NEW;
|
||||
END
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
DROP TRIGGER IF EXISTS articles_article_search_vector_trg ON articles_article;
|
||||
CREATE TRIGGER articles_article_search_vector_trg
|
||||
BEFORE INSERT OR UPDATE OF title, content_html ON articles_article
|
||||
FOR EACH ROW EXECUTE FUNCTION articles_article_search_vector_update();
|
||||
"""
|
||||
|
||||
DROP = """
|
||||
DROP TRIGGER IF EXISTS articles_article_search_vector_trg ON articles_article;
|
||||
DROP FUNCTION IF EXISTS articles_article_search_vector_update();
|
||||
DROP INDEX IF EXISTS articles_article_search_vector_gin;
|
||||
ALTER TABLE articles_article DROP COLUMN IF EXISTS search_vector;
|
||||
"""
|
||||
|
||||
|
||||
def create_vector(apps, schema_editor):
|
||||
if schema_editor.connection.vendor != "postgresql":
|
||||
return
|
||||
schema_editor.execute(CREATE)
|
||||
|
||||
|
||||
def drop_vector(apps, schema_editor):
|
||||
if schema_editor.connection.vendor != "postgresql":
|
||||
return
|
||||
schema_editor.execute(DROP)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [("search", "0001_sqlite_fts"), ("articles", "0001_initial")]
|
||||
|
||||
operations = [migrations.RunPython(create_vector, drop_vector)]
|
||||
Reference in new issue
Block a user