pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #985: Regression on PG 18+ in BitHammingDistanceDefault and BitJaccardDistanceDefault
3+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #985: Regression on PG 18+ in BitHammingDistanceDefault and BitJaccardDistanceDefault
@ 2026-06-11 00:11 "tureba (@tureba)" <noreply+tureba@github.com>
  0 siblings, 0 replies; 3+ messages in thread

From: tureba (@tureba) @ 2026-06-11 00:11 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

PostgreSQL 18 removed the macros `HAVE_LONG_INT_64` and `HAVE_LONG_LONG_INT_64` in [962da90](https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=962da900ac8f0927f1af2fd811ca67fa163c8...).

That means the logic in [bitutils.c:34](https://github.com/pgvector/pgvector/blob/master/src/bitutils.c#L34) resolves differently. This is the spot:

```c
/* Use built-ins when possible for inlining */
#if defined(HAVE__BUILTIN_POPCOUNT) && defined(HAVE_LONG_INT_64)
#define popcount64(x) __builtin_popcountl(x)
#elif defined(HAVE__BUILTIN_POPCOUNT) && defined(HAVE_LONG_LONG_INT_64)
#define popcount64(x) __builtin_popcountll(x)
#elif !defined(_MSC_VER)
/* Fails to resolve with MSVC */
#define popcount64(x) pg_popcount64(x)
#endif
```

In PG<18, it usually resolved to the builtins, but those `defined(HAVE_LONG*INT_64)` won't ever be true anymore. Now it generates code that falls back to the `pg_popcount64` function pointer, which is an indirect call per 64-bit word, that is much slower than the inlined alternatives.

The only case not affected by this is x86 with `AVX512-VPOPCNTDQ`, because of the specific implementations and dispatch. Otherwise, all architectures will see this regression of `<~>` and `<%>` in PG 18 when compared to PG<18.

From my testing, fixing this can be as simple as gating on `HAVE__BUILTIN_POPCOUNT` alone, and using `__builtin_popcountll`, which operates on unsigned long long, ≥64-bit by C99, fits uint64; correct on all ABIs and avoids the latent LLP64 truncation of `__builtin_popcountl`:

```c
#if defined(HAVE__BUILTIN_POPCOUNT)
#define popcount64(x) __builtin_popcountll(x)
#elif !defined(_MSC_VER)
#define popcount64(x) pg_popcount64(x)
#endif
```

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

* Re: [pgvector/pgvector] issue #985: Regression on PG 18+ in BitHammingDistanceDefault and BitJaccardDistanceDefault
@ 2026-06-11 02:56 ` "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2026-06-11 02:56 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

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

* Re: [pgvector/pgvector] issue #985: Regression on PG 18+ in BitHammingDistanceDefault and BitJaccardDistanceDefault
@ 2026-06-11 02:59 ` "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2026-06-11 02:59 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @tureba, thanks for reporting. Fixed in the commit above.

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


end of thread, other threads:[~2026-06-11 02:59 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-06-11 00:11 [pgvector/pgvector] issue #985: Regression on PG 18+ in BitHammingDistanceDefault and BitJaccardDistanceDefault "tureba (@tureba)" <noreply+tureba@github.com>
2026-06-11 02:56 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-06-11 02:59 ` "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