From noreply+gustavfridell@github.com Fri Sep 26 08:22:23 2025 Message-ID: From: "gustavfridell (@gustavfridell)" To: "pgvector/pgvector" Date: Fri, 26 Sep 2025 08:22:23 +0000 Subject: [pgvector/pgvector] issue #902: Optimizing distance + date queries List-Id: X-GitHub-Author-Id: 9382144 X-GitHub-Author-Login: gustavfridell X-GitHub-Issue: 902 X-GitHub-Repo: pgvector/pgvector X-GitHub-State: closed X-GitHub-Type: issue X-GitHub-Url: https://github.com/pgvector/pgvector/issues/902 Content-Type: text/plain; charset=utf-8 Hi, I have a use case where I need to retrieve all results within a certain distance threshold **and** order them by the `published_at` column. The challenge is that this prevents ordering by distance, which means I can't leverage an index to optimize these queries. One idea I had was to first filter by date (using an index) to reduce the number of rows from millions to a few thousand, and then let Postgres perform a regular scan to check which rows are within the distance threshold. In practice, though, this approach is too slow for real-world usage. Are there any practical strategies to make this more efficient using pgvector? From noreply+ankane@github.com Fri Sep 26 23:09:37 2025 Message-ID: From: "ankane (@ankane)" To: "pgvector/pgvector" Date: Fri, 26 Sep 2025 23:09:37 +0000 Subject: Re: [pgvector/pgvector] issue #902: Optimizing distance + date queries In-Reply-To: References: List-Id: X-GitHub-Author-Login: ankane X-GitHub-Comment-Id: 3340748189 X-GitHub-Comment-Type: issue_comment X-GitHub-Edited-At: 2025-09-26T23:12:17Z X-GitHub-Issue: 902 X-GitHub-Repo: pgvector/pgvector X-GitHub-Type: comment X-GitHub-Url: https://github.com/pgvector/pgvector/issues/902#issuecomment-3340748189 Content-Type: text/plain; charset=utf-8 Hi @gustavfridell, I'd expect the filter by date approach to perform well if only a few thousand rows match. What's the query and output of `EXPLAIN (ANALYZE, BUFFERS)`? Edit: The output of `\d ` in `psql` would be helpful as well. From noreply+gustavfridell@github.com Mon Sep 29 10:11:45 2025 Message-ID: From: "gustavfridell (@gustavfridell)" To: "pgvector/pgvector" Date: Mon, 29 Sep 2025 10:11:45 +0000 Subject: Re: [pgvector/pgvector] issue #902: Optimizing distance + date queries In-Reply-To: References: List-Id: X-GitHub-Author-Login: gustavfridell X-GitHub-Comment-Id: 3346119816 X-GitHub-Comment-Type: issue_comment X-GitHub-Edited-At: 2025-09-29T20:28:25Z X-GitHub-Issue: 902 X-GitHub-Repo: pgvector/pgvector X-GitHub-Type: comment X-GitHub-Url: https://github.com/pgvector/pgvector/issues/902#issuecomment-3346119816 Content-Type: text/plain; charset=utf-8 Thanks @ankane, I've managed to improve it by pre-filtering using four hash slices of bit(128) + hamming distance threshold, and then doing the full inner product distance threshold filtering on the halfvec(1536) column only on the remaining results. Still, it is borderline too slow, and will likely become slower as my database grows. Without the pre-filtering this operation timed out (consistently took more than 30s). I'm thinking it could also be related to my setup, running on Supabase with a 2-core ARM CPU + 2 GB RAM. Here's the EXPLAIN (ANALYZE, BUFFERS) output. I've removed the bit(128) / halfvec(1536) values for brevity. ``` ... ``` The query: ``` ... ``` From noreply+ankane@github.com Mon Sep 29 11:07:47 2025 Message-ID: From: "ankane (@ankane)" To: "pgvector/pgvector" Date: Mon, 29 Sep 2025 11:07:47 +0000 Subject: Re: [pgvector/pgvector] issue #902: Optimizing distance + date queries In-Reply-To: References: List-Id: X-GitHub-Author-Login: ankane X-GitHub-Comment-Id: 3346347699 X-GitHub-Comment-Type: issue_comment X-GitHub-Issue: 902 X-GitHub-Repo: pgvector/pgvector X-GitHub-Type: comment X-GitHub-Url: https://github.com/pgvector/pgvector/issues/902#issuecomment-3346347699 Content-Type: text/plain; charset=utf-8 I suspect increasing RAM will help, but a few more questions: 1. What's the output of `\d news` in `psql`? 2. What's the output of the query below in `psql`? ```sql EXPLAIN (ANALYZE, BUFFERS) SELECT id FROM news WHERE published_at >= '2025-08-30 09:56:00.126+00' AND published_at <= '2025-09-29 09:56:00.126+00' AND embedding_vector <#> '[add vector here]' < -0.31 ORDER BY published_at DESC LIMIT 10; ``` From noreply+gustavfridell@github.com Mon Sep 29 19:38:32 2025 Message-ID: From: "gustavfridell (@gustavfridell)" To: "pgvector/pgvector" Date: Mon, 29 Sep 2025 19:38:32 +0000 Subject: Re: [pgvector/pgvector] issue #902: Optimizing distance + date queries In-Reply-To: References: List-Id: X-GitHub-Author-Login: gustavfridell X-GitHub-Comment-Id: 3348758685 X-GitHub-Comment-Type: issue_comment X-GitHub-Edited-At: 2025-09-29T20:29:02Z X-GitHub-Issue: 902 X-GitHub-Repo: pgvector/pgvector X-GitHub-Type: comment X-GitHub-Url: https://github.com/pgvector/pgvector/issues/902#issuecomment-3348758685 Content-Type: text/plain; charset=utf-8 That query depends a lot on the specific vector I'm running and whether I've recently run the same one. A fresh one gives this, for example: ``` ... ``` This is the table definition: ``` ... ``` No index on the vector columns as I don't order by the distance. From noreply+ankane@github.com Mon Sep 29 20:25:02 2025 Message-ID: From: "ankane (@ankane)" To: "pgvector/pgvector" Date: Mon, 29 Sep 2025 20:25:02 +0000 Subject: Re: [pgvector/pgvector] issue #902: Optimizing distance + date queries In-Reply-To: References: List-Id: X-GitHub-Author-Login: ankane X-GitHub-Comment-Id: 3348920877 X-GitHub-Comment-Type: issue_comment X-GitHub-Issue: 902 X-GitHub-Repo: pgvector/pgvector X-GitHub-Type: comment X-GitHub-Url: https://github.com/pgvector/pgvector/issues/902#issuecomment-3348920877 Content-Type: text/plain; charset=utf-8 It looks like very few rows match the distance filter (it scanned 36758 rows to find 1 matching). If this is typical, you'll likely see better performance by partitioning the table by `published_at`, adding an HNSW index, and updating the query to use it. You'll likely also see better performance by adding more RAM (as buffers `read` indicates the query needed to read from disk). Finally, please use `psql` in the future to make the EXPLAIN output more readable and ensure the table definition is accurate. From noreply+ankane@github.com Mon Sep 29 20:25:02 2025 Message-ID: From: "ankane (@ankane)" To: "pgvector/pgvector" Date: Mon, 29 Sep 2025 20:25:02 +0000 Subject: Re: [pgvector/pgvector] issue #902: Optimizing distance + date queries In-Reply-To: References: List-Id: X-GitHub-Author-Login: ankane X-GitHub-Event: closed X-GitHub-Event-Id: 19992827164 X-GitHub-Issue: 902 X-GitHub-Repo: pgvector/pgvector X-GitHub-Type: timeline_event Content-Type: text/plain; charset=utf-8 @ankane closed this.