pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #841: Vector Similarity Query Returns 0 Results at Certain LIMIT Values with Complex Filters
3+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #841: Vector Similarity Query Returns 0 Results at Certain LIMIT Values with Complex Filters
@ 2025-05-22 11:14 "bhagyajitjagdev (@bhagyajitjagdev)" <noreply+bhagyajitjagdev@github.com>
  0 siblings, 0 replies; 3+ messages in thread

From: bhagyajitjagdev (@bhagyajitjagdev) @ 2025-05-22 11:14 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

## Description  
While running `pgvector` in a Dockerized PostgreSQL instance on EC2, I observed unexpected behavior: vector similarity queries return **0 results** when using `LIMIT` values just below a certain threshold, but return expected results when the limit is slightly higher.

This happens only when combining vector similarity ordering with geographic distance calculations and additional filter conditions. I’m not entirely sure what’s causing it, but it seems like something in the query execution behaves differently based on the `LIMIT`.

---

## Environment

- **PostgreSQL Version:**  
  `17.4 (Debian 17.4-1.pgdg120+2)`  
  Compiled by: `gcc (Debian 12.2.0-14) 12.2.0`, 64-bit  
- **pgvector Version:** `0.8.0`  
- **Docker Image:** `pgvector/pgvector:pg17`  
- **Platform:** EC2 (x86_64 Linux)

---

## Issue Summary

- The query includes:
  - `ORDER BY embedding <=> vector`
  - Geographic filtering using the Haversine formula
  - Boolean and null checks
- Behavior:
  - `LIMIT 497` → **0 results**
  - `LIMIT 498` → **464 results** (expected)
- Removing `ORDER BY embedding <=> vector` makes both return results normally

---

## Example Queries

```sql
-- Returns 0 results
SELECT * FROM property_listings
WHERE is_active = true 
  AND embedding IS NOT NULL 
  AND (
      6371 * acos(
          cos(radians(25.1941288)) * 
          cos(radians(latitude)) * 
          cos(radians(longitude) - radians(55.2780264)) + 
          sin(radians(25.1941288)) * 
          sin(radians(latitude))
      )
  ) <= 1.0
ORDER BY embedding <=> '[384-dimension vector]'::vector, created_at DESC
LIMIT 497;

-- Returns 464 results
SELECT * FROM property_listings
WHERE is_active = true 
  AND embedding IS NOT NULL 
  AND (
      6371 * acos(
          cos(radians(25.1941288)) * 
          cos(radians(latitude)) * 
          cos(radians(longitude) - radians(55.2780264)) + 
          sin(radians(25.1941288)) * 
          sin(radians(latitude))
      )
  ) <= 1.0
ORDER BY embedding <=> '[same 384-dimension vector]'::vector, created_at DESC
LIMIT 498;
```

## Other Details
- ~6,846 rows with non-null embeddings
- Embeddings are 384-dimensional
- Geographic filter typically matches ~464 rows
- No errors — just an unexpected 0-row result at specific limits

## Expected Behavior
The query should return results consistently. Changing the LIMIT by one (from 498 to 497) shouldn’t result in zero rows when matching data exists.

## Question
Is this expected behavior when combining vector similarity search with complex filters like geographic distance? Or has anyone else seen something similar? Would love to know if this is a known edge case or if there are suggested ways to avoid it.


^ permalink  raw  [nested|flat] 3+ messages in thread

* Re: [pgvector/pgvector] issue #841: Vector Similarity Query Returns 0 Results at Certain LIMIT Values with Complex Filters
@ 2025-05-22 11:19 ` "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2025-05-22 11:19 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @bhagyajitjagdev, check out the [troubleshooting docs](https://github.com/pgvector/pgvector?tab=readme-ov-file#why-are-there-less-results-for-a-query-after...). You can use `EXPLAIN ANALYZE` to see how the query plans differ.

^ permalink  raw  [nested|flat] 3+ messages in thread

* Re: [pgvector/pgvector] issue #841: Vector Similarity Query Returns 0 Results at Certain LIMIT Values with Complex Filters
@ 2025-05-22 11:19 ` "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2025-05-22 11:19 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2025-05-22 11:19 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-05-22 11:14 [pgvector/pgvector] issue #841: Vector Similarity Query Returns 0 Results at Certain LIMIT Values with Complex Filters "bhagyajitjagdev (@bhagyajitjagdev)" <noreply+bhagyajitjagdev@github.com>
2025-05-22 11:19 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-05-22 11:19 ` "ankane (@ankane)" <noreply+ankane@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox