Skip to content

'near' query in Postgres are giving inaccurate results compared to Mongo #14471

@alishahlakhani

Description

@alishahlakhani

Describe the Bug

When using the near operator to search for nearby locations, the actual search area is much smaller than the distance we provide in meters. Our 8-mile search only finds things within about 6 miles.

This issue only happens with the PostgreSQL adapter. We confirmed that MongoDB works correctly using the exact same query and data.

Link to the code that reproduces this issue

https://github.com/alishahlakhani/pg-bug

Reproduction Steps

  1. Use the Payload Postgres adapter.
  2. Seed with the following data: Create a collection with a point field and add two test items:
[
  {
    id: 1,
    name: 'Downtown Clinic - 3.5m',
    coordinates: [-74.0059, 40.7128 + 0.0505], // ~3.489 miles
    createdAt: '2025-10-20T10:20:00.000Z',
    updatedAt: '2025-10-20T10:20:00.000Z',
  },
  {
    id: 2,
    name: 'Downtown Clinic - 6m',
    coordinates: [-74.0059, 40.7128 + 0.0905], // ~6.253 miles
    createdAt: '2025-10-20T10:20:00.000Z',
    updatedAt: '2025-10-20T10:20:00.000Z',
  },
  {
    id: 3,
    name: 'Downtown Clinic - 10m',
    coordinates: [-74.0059 - 0.15818, 40.7128 + 0.0905], // ~10.375 miles
    createdAt: '2025-10-20T10:20:00.000Z',
    updatedAt: '2025-10-20T10:20:00.000Z',
  },
  {
    id: 4,
    name: 'Downtown Clinic - 21m',
    coordinates: [-74.0059 - 0.15818, 40.7128 + 0.2905], // ~21.707 miles
    createdAt: '2025-10-20T10:20:00.000Z',
    updatedAt: '2025-10-20T10:20:00.000Z',
  },
  {
    id: 5,
    name: 'Downtown Clinic - 56m',
    coordinates: [-74.0059 - 0.85818, 40.7128 + 0.4905], // ~56.16 miles
    createdAt: '2025-10-20T10:20:00.000Z',
    updatedAt: '2025-10-20T10:20:00.000Z',
  },
  {
    id: 6,
    name: 'Downtown Clinic - 85m',
    coordinates: [-74.0059 - 0.99818, 40.7128 + 0.9805], // ~85.33 miles
    createdAt: '2025-10-20T10:20:00.000Z',
    updatedAt: '2025-10-20T10:20:00.000Z',
  },
]
  1. Query: After hitting this API on postman via 8-mile radius (12,874.8 meters)
    http://localhost:3000/api/map?where[coordinates][near]=-74.0059,40.7128,12874.8

We should technically get 2 points(id:1 and id: 2) but the query only returns 1(3.5 miles) while 2(6.3 miles) is missing, even though it's well within the 8-mile search radius.

We tested this by using Mongodb adapter and it works flawlessly but for PG its failing. After checking with ✌️AI✌️ we found out that its a distance calculation issue. After we multiply the query's near value by 1.32 it works. Is this normal? We had to use beforeRead hook to sort this out for now but I believe CMS should solve this appropriately.

Which area(s) are affected? (Select all that apply)

db: postgres

Environment Info

Binaries:
  Node: 22.7.0
  npm: 10.8.2
  Yarn: N/A
  pnpm: 10.18.0
Relevant Packages:
  payload: 3.58.0
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.0.0: Wed Sep 17 21:41:45 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs-triagePossible bug which hasn't been reproduced yet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions