pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #850: LIMIT clause interferes with ORDER BY on vector distance operations
4+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #850: LIMIT clause interferes with ORDER BY on vector distance operations
@ 2025-06-04 10:06  "bennomeyer (@bennomeyer)" <noreply+bennomeyer@github.com>
  0 siblings, 0 replies; 4+ messages in thread

From: bennomeyer (@bennomeyer) @ 2025-06-04 10:06 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

## Summary

When using LIMIT with ORDER BY on pgvector distance operations (e.g., embedding <=> ?), the query returns incorrect results that don't respect the distance-based ordering.

## Environment

- pgvector version: 0.7.4
- PostgreSQL version: 17.0
- Database: PostgreSQL with pgvector extension

## Expected Behavior

The query should return the N closest vectors based on the distance calculation, properly ordered from smallest to largest distance.

## Actual Behavior

The query returns incorrect results when LIMIT is used with ORDER BY on vector distance operations. In some cases, it returns 0 results when it should return multiple results, or returns results that are not the closest vectors.

## Reproduction Steps

1. Create a table with vector embeddings:
`CREATE TABLE embeddings (
    id SERIAL PRIMARY KEY,
    embedding vector(1536)
);`
2. Insert multiple rows with vector data
3. Execute a query with both ORDER BY and LIMIT:
`SELECT id, embedding <=> '[0.1,0.2,...]' as distance 
FROM embeddings 
ORDER BY embedding <=> '[0.1,0.2,...]' 
LIMIT 3;`
4. Compare with the same query without LIMIT:
`SELECT id, embedding <=> '[0.1,0.2,...]' as distance 
FROM embeddings 
ORDER BY embedding <=> '[0.1,0.2,...]';`

## Observed Issue

- **Without LIMIT**: Returns all rows correctly ordered by distance
- **With LIMIT**: Returns incorrect subset or sometimes 0 results, not respecting the distance-based ordering

## Workaround

Fetch all results without LIMIT and manually limit the results in application code.

## Impact

This bug severely affects vector similarity search applications, as they rely on getting the most similar vectors. Applications may return irrelevant results instead of the closest matches.

## Additional Notes

- The issue appears to be related to query planning where LIMIT is applied before the distance-based ordering
- Various SQL workarounds (subqueries, OFFSET 0, alias ordering) do not resolve the issue
- The bug is reproducible across different vector dimensions and distance operators


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

* Re: [pgvector/pgvector] issue #850: LIMIT clause interferes with ORDER BY on vector distance operations
@ 2025-06-04 10:42  "bennomeyer (@bennomeyer)" <noreply+bennomeyer@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: bennomeyer (@bennomeyer) @ 2025-06-04 10:42 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

See https://github.com/pgvector/pgvector?tab=readme-ov-file#why-are-there-less-results-for-a-query-after... 

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

* Re: [pgvector/pgvector] issue #850: LIMIT clause interferes with ORDER BY on vector distance operations
@ 2025-06-04 10:42  "bennomeyer (@bennomeyer)" <noreply+bennomeyer@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: bennomeyer (@bennomeyer) @ 2025-06-04 10:42 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@bennomeyer closed this.

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

* Re: [pgvector/pgvector] issue #850: LIMIT clause interferes with ORDER BY on vector distance operations
@ 2025-06-04 13:51  "jkatz (@jkatz)" <noreply+jkatz@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: jkatz (@jkatz) @ 2025-06-04 13:51 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

> When using LIMIT with ORDER BY on pgvector distance operations (e.g., embedding <=> ?), the query returns incorrect results that don't respect the distance-based ordering.

This is fundamentally how approximate nearest neighbor (ANN) search works in any vector search system. If PostgreSQL opts use one of the indexes offered through pgvector (IVFFlat or HNSW), you may not get the best possible results for the query (as measured by recall). However, you can still get an exact nearest-neighbor (aka "K-NN") search with pgvector when you have an `ORDER BY` and `LIMIT` if you don't add any of these indexes to the table, or if PostgreSQL choose to use a different search path (which pgvector v0.8.0 greatly simplifies - I noticed you're using v0.7.4).


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


end of thread, other threads:[~2025-06-04 13:51 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-06-04 10:06 [pgvector/pgvector] issue #850: LIMIT clause interferes with ORDER BY on vector distance operations "bennomeyer (@bennomeyer)" <noreply+bennomeyer@github.com>
2025-06-04 10:42 ` "bennomeyer (@bennomeyer)" <noreply+bennomeyer@github.com>
2025-06-04 10:42 ` "bennomeyer (@bennomeyer)" <noreply+bennomeyer@github.com>
2025-06-04 13:51 ` "jkatz (@jkatz)" <noreply+jkatz@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