pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #963: Question: Binary initialisation
9+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #963: Question: Binary initialisation
@ 2026-03-06 01:06 "Grunthos (@Grunthos)" <noreply+Grunthos@github.com>
  0 siblings, 0 replies; 9+ messages in thread

From: Grunthos (@Grunthos) @ 2026-03-06 01:06 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I have a need to create sparse vector from binary source. Specifically:

- ( indexes int[] , values int[] )   -- Using an int for values at each index
- ( indexes int[], values float[] )  -- Using a float for values at each index
- ( indexes int[], value )  -- Using a fixed value at each index (int/float)

I apologise if this has already been discussed, but would this be an acceptable/useful additional feature in your view? I am happy to do the leg work to make this happen.


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

* Re: [pgvector/pgvector] issue #963: Question: Binary initialisation
@ 2026-03-06 01:16 ` "ankane (@ankane)" <noreply+ankane@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: ankane (@ankane) @ 2026-03-06 01:16 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @Grunthos, the second case is already possible if you use the binary format ([relevant code](https://github.com/pgvector/pgvector/blob/v0.8.2/src/sparsevec.c#L504)). I don't think additional types of values are common enough to support right now.

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

* Re: [pgvector/pgvector] issue #963: Question: Binary initialisation
@ 2026-03-06 01:16 ` "ankane (@ankane)" <noreply+ankane@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: ankane (@ankane) @ 2026-03-06 01:16 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

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

* Re: [pgvector/pgvector] issue #963: Question: Binary initialisation
@ 2026-03-06 02:42 ` "Grunthos (@Grunthos)" <noreply+Grunthos@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: Grunthos (@Grunthos) @ 2026-03-06 02:42 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @ankane,

I think you may have misunderstood my (sparsely stated) intent (or I have misunderstood the answer). 

I meant those to be SQL functions; ie. something which I can use in queries. The link you posted was (I think) fow a wire protocol decoder?

Something that would support:
```
CREATE FUNCTION sparsevec_from_arrays(indices int[], values float4[])
RETURNS sparsevec
```

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

* Re: [pgvector/pgvector] issue #963: Question: Binary initialisation
@ 2026-03-06 03:29 ` "ankane (@ankane)" <noreply+ankane@github.com>
  7 siblings, 0 replies; 9+ messages in thread

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

Ah, gotcha. You can currently create a custom function for it:

```sql
CREATE FUNCTION sparsevec_from_arrays(indices int[], value float4[], dimensions int) RETURNS sparsevec AS $$
    SELECT ('{' || (SELECT string_agg(indices[i] || ':' || value[i], ',') FROM generate_series(1, array_length(indices, 1)) i) || '}/' || dimensions)::sparsevec;
$$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
```

```sql
SELECT sparsevec_from_arrays('{1,3,5}', '{2,4,6}', 6);
```

Can you share more about the use case, and why it's important to have the functionality in the database vs. the client libraries?

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

* Re: [pgvector/pgvector] issue #963: Question: Binary initialisation
@ 2026-03-06 04:49 ` "Grunthos (@Grunthos)" <noreply+Grunthos@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: Grunthos (@Grunthos) @ 2026-03-06 04:49 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @ankane ,

Thanks for the prompt replies!

The problem with the above approach is the creation and destruction of massive strings (in my case vectors with in the worst case up up to 8k values). Mostly they are small.

The use case is as follows:

- We build arrays of what are basically attribute lists for objects.
  - This is our int[] parameter.
  - Those attribute lists are of varying length but consist of a list of integer IDs.
- We also have "weights" for those attributes stored/derived elsewhere; many objects share attributes so we weight them in one place and can easily generate a per-object list of weights based on the attributes.
  - This is our float[] parameter
 - Next we have a 'check' object that wants to compute a score based on members of the 'check' list based on the weights in the first list.
   - this is the motivation for the sparse array with a single value for all entries, in this case '1'
   - We anticipate a sparse dot product will work nicely.
 - The problem is building the sparse arrays dynamically. This will be done inside a single query executing across many rows.
   - We currently do this with a simple C function. It's fast and efficient.
   - We are moving the database to AWS Aurora and `pgvector` is present, but C language is not.

To my eye this looks like a natural fit for anyone who wants to build sparse vectors efficiently and looks like a good analogue for the dense vector constructor and would seem to have the same justification: strings are bad  ;-).



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

* Re: [pgvector/pgvector] issue #963: Question: Binary initialisation
@ 2026-03-06 05:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: ankane (@ankane) @ 2026-03-06 05:25 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks.

The function will be slower than C, but still fairly fast, and I'd expect you'd want to materialize the sparse vectors in cases where performance is a concern.

Will keep an eye out for other use cases, but I don't think it makes sense to add for this.

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

* Re: [pgvector/pgvector] issue #963: Question: Binary initialisation
@ 2026-03-06 05:33 ` "Grunthos (@Grunthos)" <noreply+Grunthos@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: Grunthos (@Grunthos) @ 2026-03-06 05:33 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @ankane 

OK, thanks again for the replies.

I'm curious why the logic to add a non-string SQL initialiser for dense arrays does not apply to sparse arrays?
 

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

* Re: [pgvector/pgvector] issue #963: Question: Binary initialisation
@ 2026-03-08 11:03 ` "Grunthos (@Grunthos)" <noreply+Grunthos@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: Grunthos (@Grunthos) @ 2026-03-08 11:03 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

FWIW, I added a PR with tests etc, trying to keep the style of existing code. Compared to array->text->sparsevec it's 30-50 times faster

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


end of thread, other threads:[~2026-03-08 11:03 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-03-06 01:06 [pgvector/pgvector] issue #963: Question: Binary initialisation "Grunthos (@Grunthos)" <noreply+Grunthos@github.com>
2026-03-06 01:16 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-03-06 01:16 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-03-06 02:42 ` "Grunthos (@Grunthos)" <noreply+Grunthos@github.com>
2026-03-06 03:29 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-03-06 04:49 ` "Grunthos (@Grunthos)" <noreply+Grunthos@github.com>
2026-03-06 05:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-03-06 05:33 ` "Grunthos (@Grunthos)" <noreply+Grunthos@github.com>
2026-03-08 11:03 ` "Grunthos (@Grunthos)" <noreply+Grunthos@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