pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] PR #1038: Avoid full detoasting in vector_dims and subvector for vector and hal…
3+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] PR #1038: Avoid full detoasting in vector_dims and subvector for vector and hal…
@ 2026-10-04 13:20 "postsql (@postsql)" <noreply+postsql@github.com>
0 siblings, 0 replies; 3+ messages in thread
From: postsql (@postsql) @ 2026-10-04 13:20 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
## 2nd Try
I refactored the patch to be cleaner and hopefully easier to review/verify for brittleness.
The current main use case that will benefit from faster slice extraction is indexes on Matryoshka embedding.
The bigger benefits should be visible at index creation time.
## Summary
High-dimensional embeddings (e.g., `vector(1536)` at ~6.1 KB, `vector(3072)` at ~12.3 KB, or `halfvec` up to 16,000 dimensions at ~32 KB) exceed PostgreSQL's `TOAST_TUPLE_THRESHOLD` (~2 KB) and are stored out-of-line in TOAST tables (`STORAGE = external`).
Currently, `vector_dims()` and `subvector()` for both `vector` and `halfvec` call the 1-argument `PG_GETARG_VECTOR_P(0)` / `PG_GETARG_HALFVEC_P(0)` (`PG_DETOAST_DATUM`), which unconditionally fetches and reassembles **all** TOAST chunks of a vector—even when only the dimension count is needed or when extracting a small slice (such as in two-stage Matryoshka Representation Learning / MRL retrieval via `subvector(embedding, 1, 128)`).
This PR extends `PG_GETARG_VECTOR_P` / `DatumGetVector` and `PG_GETARG_HALFVEC_P` / `DatumGetHalfVector` with C99 variadic macro overloads that accept optional length or `(start, count)` slice arguments, encapsulating `toast_raw_datum_size()` and `PG_DETOAST_DATUM_SLICE()` inside the type accessors while keeping `vector_dims()` and `subvector()` concise.
## Changes
1. **Overloaded `PG_GETARG_VECTOR_P` / `DatumGetVector` and `PG_GETARG_HALFVEC_P` / `DatumGetHalfVector`** ([`src/vector.h`](file:///usr/local/google/home/hannuk/pgvector/src/vector.h#L14-L38), [`src/halfvec.h`](file:///usr/local/google/home/hannuk/pgvector/src/halfvec.h#L63-L87), [`src/vector.c`](file:///usr/local/google/home/hannuk/pgvector/src/vector.c#L143-L230), [`src/halfvec.c`](file:///usr/local/google/home/hannuk/pgvector/src/halfvec.c#L147-L234)):
- **1-Argument Form — `PG_GETARG_VECTOR_P(n)`**:
- Unchanged full detoast (`(Vector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(n))`).
- **2-Argument Form — `PG_GETARG_VECTOR_P(n, len)` (`DatumGetVectorPrefix`)**:
- Detoasts up to `len` prefix elements while preserving the original vector's `a->dim` in the header:
- When `len <= 0` (e.g. `PG_GETARG_VECTOR_P(0, 0)`): if the datum is already an inline 4-byte uncompressed varlena (`!VARATT_IS_EXTENDED`), returns the pointer directly with zero allocations; otherwise computes `dim = (toast_raw_datum_size(x) - offsetof(Vector, x)) / sizeof(float)` with **zero TOAST chunk fetches** and returns a header-only `Vector *` (`InitVector(0)` with `result->dim = dim`).
- When `len > 0`: calls `PG_DETOAST_DATUM_SLICE(x, 0, (offsetof(Vector, x) - VARHDRSZ) + mul_size(len, sizeof(float)))`, detoasting only the header and the first `Min(len, a->dim)` elements.
- **3-Argument Form — `PG_GETARG_VECTOR_P(n, start, count)` (`DatumGetVectorSlice`)**:
- Computes `a_dim` via `toast_raw_datum_size(x)`, validates the 1-indexed `(start, count)` bounds and overflow checks, and returns a newly constructed subvector of length `dim = end - start`:
- When `start == 1` (prefix slice), reuses `DatumGetVectorPrefix(x, dim)` and sets `result->dim = dim`, avoiding a second allocation and `memcpy`.
- When `start > 1`, fetches only `[sliceoffset, sliceoffset + slicelength)` via `PG_DETOAST_DATUM_SLICE(x, sliceoffset, slicelength)`.
2. **Concise `vector_dims` & `subvector` Call Sites** ([`src/vector.c`](file:///usr/local/google/home/hannuk/pgvector/src/vector.c#L847-L1083), [`src/halfvec.c`](file:///usr/local/google/home/hannuk/pgvector/src/halfvec.c#L783-L1039)):
```c
Datum
vector_dims(PG_FUNCTION_ARGS)
{
Vector *a = PG_GETARG_VECTOR_P(0, 0);
PG_RETURN_INT32(a->dim);
}
Datum
subvector(PG_FUNCTION_ARGS)
{
int32 start = PG_GETARG_INT32(1);
int32 count = PG_GETARG_INT32(2);
Vector *result = PG_GETARG_VECTOR_P(0, start, count);
PG_RETURN_VECTOR_P(result);
}
```
## Benchmark Results
Measured over 10 iterations on out-of-line toasted vectors (standard PostgreSQL TOAST):
| Workload | Before (Full Detoast) | After (Slice-Aware) | Speedup | TOAST Heap Chunks Read / Row |
| :--- | ---: | ---: | ---: | :--- |
| `vector_dims(emb)` (`1,000` rows, `vector(5376)`, `21.5 KB`/row) | 173.55 ms | **1.34 ms** | **129×** | `11` → **`0`** |
| `subvector(emb, 1, 128)` (`2,000` rows, `vector(1536)`, `6.1 KB`/row) | 168.22 ms | **105.54 ms** | **1.60×** | `4` → **`1`** |
| `subvector(emb, 1, 256)` (`1,000` rows, `vector(5376)`, `21.5 KB`/row) | 173.55 ms | **53.50 ms** | **3.24×** | `11` → **`1`** |
*(On PostgreSQL builds with Direct TOAST, the same `subvector` queries run **2.33×–3.72× faster** on `vector(1536)` and **6.62× faster** on `vector(5376)` by also eliminating the TOAST B-tree index probe.)*
## Testing
Extended [`test/sql/vector_type.sql`](file:///usr/local/google/home/hannuk/pgvector/test/sql/vector_type.sql#L143-L153) and [`test/sql/halfvec.sql`](file:///usr/local/google/home/hannuk/pgvector/test/sql/halfvec.sql#L137-L147) with table-backed test cases covering all three on-disk varlena layouts:
- Short 1-byte inline varlenas (`VARATT_IS_SHORT`, 5 dimensions),
- Multi-chunk uncompressed `STORAGE EXTERNAL` TOAST tuples (`1000`-dim `vector` and `2000`-dim `halfvec`) across prefix slices (`start == 1`), cross-chunk middle slices, suffix slices, negative start indices, and out-of-bounds error checks,
- Compressed `STORAGE EXTENDED` TOAST tuples (`VARATT_IS_COMPRESSED`).
inline patch 1: src/halfvec.c, src/halfvec.h, src/vector.c and 5 more (+284/-76 lines) download inline-1.patch
diff --git a/src/halfvec.c b/src/halfvec.c
index 47f5464c3..aa112fbd4 100644
--- a/src/halfvec.c
+++ b/src/halfvec.c
@@ -2,6 +2,7 @@
#include <math.h>
+#include "access/detoast.h"
#include "bitvec.h"
#include "catalog/pg_type.h"
#include "common/shortest_dec.h"
@@ -143,6 +144,96 @@ InitHalfVector(int dim)
return result;
}
+/*
+ * Detoast a half vector prefix (or header-only when len <= 0)
+ */
+HalfVector *
+DatumGetHalfVectorPrefix(Datum x, int32 len)
+{
+ if (len <= 0)
+ {
+ HalfVector *result;
+ Size raw_size;
+
+ if (!VARATT_IS_EXTENDED(DatumGetPointer(x)))
+ return (HalfVector *) DatumGetPointer(x);
+
+ raw_size = toast_raw_datum_size(x);
+ result = InitHalfVector(0);
+ result->dim = (int32) ((raw_size - offsetof(HalfVector, x)) / sizeof(half));
+ return result;
+ }
+
+ if (len > HALFVEC_MAX_DIM)
+ len = HALFVEC_MAX_DIM;
+
+ return (HalfVector *) PG_DETOAST_DATUM_SLICE(x, 0, (int32) ((offsetof(HalfVector, x) - VARHDRSZ) + mul_size(len, sizeof(half))));
+}
+
+/*
+ * Detoast a subvector slice of a half vector
+ */
+HalfVector *
+DatumGetHalfVectorSlice(Datum x, int32 start, int32 count)
+{
+ Size raw_size;
+ int32 a_dim;
+ int32 end;
+ HalfVector *result;
+ int32 dim;
+ int32 sliceoffset;
+ int32 slicelength;
+ struct varlena *sliced;
+
+ if (count < 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATA_EXCEPTION),
+ errmsg("halfvec must have at least 1 dimension")));
+
+ raw_size = toast_raw_datum_size(x);
+ a_dim = (int32) ((raw_size - offsetof(HalfVector, x)) / sizeof(half));
+
+ /*
+ * Check if (start + count > a_dim), avoiding integer overflow. a_dim
+ * and count are both positive, so a_dim - count won't overflow.
+ */
+ if (start > a_dim - count)
+ end = a_dim + 1;
+ else
+ end = start + count;
+
+ /* Indexing starts at 1, like substring */
+ if (start < 1)
+ start = 1;
+ else if (start > a_dim)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATA_EXCEPTION),
+ errmsg("halfvec must have at least 1 dimension")));
+
+ dim = end - start;
+ CheckDim(dim);
+
+ if (start == 1)
+ {
+ result = DatumGetHalfVectorPrefix(x, dim);
+ result->dim = dim;
+ return result;
+ }
+
+ result = InitHalfVector(dim);
+
+ sliceoffset = (offsetof(HalfVector, x) - VARHDRSZ) + (start - 1) * sizeof(half);
+ slicelength = dim * sizeof(half);
+ sliced = PG_DETOAST_DATUM_SLICE(x, sliceoffset, slicelength);
+
+ memcpy(result->x, VARDATA_ANY(sliced), slicelength);
+
+ if ((Pointer) sliced != DatumGetPointer(x))
+ pfree(sliced);
+
+ return result;
+}
+
#if PG_VERSION_NUM >= 170000
#define halfvec_isspace(ch) scanner_isspace(ch)
#else
@@ -692,7 +783,7 @@ FUNCTION_PREFIX PG_FUNCTION_INFO_V1(halfvec_vector_dims);
Datum
halfvec_vector_dims(PG_FUNCTION_ARGS)
{
- HalfVector *a = PG_GETARG_HALFVEC_P(0);
+ HalfVector *a = PG_GETARG_HALFVEC_P(0, 0);
PG_RETURN_INT32(a->dim);
}
@@ -940,44 +1031,11 @@ FUNCTION_PREFIX PG_FUNCTION_INFO_V1(halfvec_subvector);
Datum
halfvec_subvector(PG_FUNCTION_ARGS)
{
- HalfVector *a = PG_GETARG_HALFVEC_P(0);
int32 start = PG_GETARG_INT32(1);
int32 count = PG_GETARG_INT32(2);
- int32 end;
- half *ax = a->x;
- HalfVector *result;
- int32 dim;
+ HalfVector *result = PG_GETARG_HALFVEC_P(0, start, count);
- if (count < 1)
- ereport(ERROR,
- (errcode(ERRCODE_DATA_EXCEPTION),
- errmsg("halfvec must have at least 1 dimension")));
-
- /*
- * Check if (start + count > a->dim), avoiding integer overflow. a->dim
- * and count are both positive, so a->dim - count won't overflow.
- */
- if (start > a->dim - count)
- end = a->dim + 1;
- else
- end = start + count;
-
- /* Indexing starts at 1, like substring */
- if (start < 1)
- start = 1;
- else if (start > a->dim)
- ereport(ERROR,
- (errcode(ERRCODE_DATA_EXCEPTION),
- errmsg("halfvec must have at least 1 dimension")));
-
- dim = end - start;
- CheckDim(dim);
- result = InitHalfVector(dim);
-
- for (int i = 0; i < dim; i++)
- result->x[i] = ax[start - 1 + i];
-
- PG_RETURN_POINTER(result);
+ PG_RETURN_HALFVEC_P(result);
}
/*
diff --git a/src/halfvec.h b/src/halfvec.h
index 8ff2cbfbf..a47bc0b85 100644
--- a/src/halfvec.h
+++ b/src/halfvec.h
@@ -60,8 +60,18 @@
#define HALFVEC_MAX_DIM 16000
#define HALFVEC_SIZE(_dim) add_size(offsetof(HalfVector, x), mul_size(sizeof(half), _dim))
-#define DatumGetHalfVector(x) ((HalfVector *) PG_DETOAST_DATUM(x))
-#define PG_GETARG_HALFVEC_P(x) DatumGetHalfVector(PG_GETARG_DATUM(x))
+#define HALFVEC_EXPAND(x) x
+#define _DATUM_GET_HALFVEC_1(x) ((HalfVector *) PG_DETOAST_DATUM(x))
+#define _DATUM_GET_HALFVEC_2(x, len) DatumGetHalfVectorPrefix(x, len)
+#define _DATUM_GET_HALFVEC_3(x, start, count) DatumGetHalfVectorSlice(x, start, count)
+#define _DATUM_GET_HALFVEC_SELECT(_1, _2, _3, NAME, ...) NAME
+#define DatumGetHalfVector(...) \
+ HALFVEC_EXPAND(_DATUM_GET_HALFVEC_SELECT(__VA_ARGS__, _DATUM_GET_HALFVEC_3, _DATUM_GET_HALFVEC_2, _DATUM_GET_HALFVEC_1, 0)(__VA_ARGS__))
+#define _PG_GETARG_HALFVEC_P_1(n) _DATUM_GET_HALFVEC_1(PG_GETARG_DATUM(n))
+#define _PG_GETARG_HALFVEC_P_2(n, len) _DATUM_GET_HALFVEC_2(PG_GETARG_DATUM(n), len)
+#define _PG_GETARG_HALFVEC_P_3(n, start, count) _DATUM_GET_HALFVEC_3(PG_GETARG_DATUM(n), start, count)
+#define PG_GETARG_HALFVEC_P(...) \
+ HALFVEC_EXPAND(_DATUM_GET_HALFVEC_SELECT(__VA_ARGS__, _PG_GETARG_HALFVEC_P_3, _PG_GETARG_HALFVEC_P_2, _PG_GETARG_HALFVEC_P_1, 0)(__VA_ARGS__))
#define PG_RETURN_HALFVEC_P(x) PG_RETURN_POINTER(x)
typedef struct HalfVector
@@ -73,5 +83,7 @@ typedef struct HalfVector
} HalfVector;
HalfVector *InitHalfVector(int dim);
+HalfVector *DatumGetHalfVectorPrefix(Datum x, int32 len);
+HalfVector *DatumGetHalfVectorSlice(Datum x, int32 start, int32 count);
#endif
diff --git a/src/vector.c b/src/vector.c
index ebd78c14a..f09e603b1 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -2,6 +2,7 @@
#include <math.h>
+#include "access/detoast.h"
#include "bitutils.h"
#include "bitvec.h"
#include "catalog/pg_type.h"
@@ -139,6 +140,96 @@ InitVector(int dim)
return result;
}
+/*
+ * Detoast a vector prefix (or header-only when len <= 0)
+ */
+Vector *
+DatumGetVectorPrefix(Datum x, int32 len)
+{
+ if (len <= 0)
+ {
+ Vector *result;
+ Size raw_size;
+
+ if (!VARATT_IS_EXTENDED(DatumGetPointer(x)))
+ return (Vector *) DatumGetPointer(x);
+
+ raw_size = toast_raw_datum_size(x);
+ result = InitVector(0);
+ result->dim = (int32) ((raw_size - offsetof(Vector, x)) / sizeof(float));
+ return result;
+ }
+
+ if (len > VECTOR_MAX_DIM)
+ len = VECTOR_MAX_DIM;
+
+ return (Vector *) PG_DETOAST_DATUM_SLICE(x, 0, (int32) ((offsetof(Vector, x) - VARHDRSZ) + mul_size(len, sizeof(float))));
+}
+
+/*
+ * Detoast a subvector slice
+ */
+Vector *
+DatumGetVectorSlice(Datum x, int32 start, int32 count)
+{
+ Size raw_size;
+ int32 a_dim;
+ int32 end;
+ Vector *result;
+ int dim;
+ int32 sliceoffset;
+ int32 slicelength;
+ struct varlena *sliced;
+
+ if (count < 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATA_EXCEPTION),
+ errmsg("vector must have at least 1 dimension")));
+
+ raw_size = toast_raw_datum_size(x);
+ a_dim = (int32) ((raw_size - offsetof(Vector, x)) / sizeof(float));
+
+ /*
+ * Check if (start + count > a_dim), avoiding integer overflow. a_dim
+ * and count are both positive, so a_dim - count won't overflow.
+ */
+ if (start > a_dim - count)
+ end = a_dim + 1;
+ else
+ end = start + count;
+
+ /* Indexing starts at 1, like substring */
+ if (start < 1)
+ start = 1;
+ else if (start > a_dim)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATA_EXCEPTION),
+ errmsg("vector must have at least 1 dimension")));
+
+ dim = end - start;
+ CheckDim(dim);
+
+ if (start == 1)
+ {
+ result = DatumGetVectorPrefix(x, dim);
+ result->dim = dim;
+ return result;
+ }
+
+ result = InitVector(dim);
+
+ sliceoffset = (offsetof(Vector, x) - VARHDRSZ) + (start - 1) * sizeof(float);
+ slicelength = dim * sizeof(float);
+ sliced = PG_DETOAST_DATUM_SLICE(x, sliceoffset, slicelength);
+
+ memcpy(result->x, VARDATA_ANY(sliced), slicelength);
+
+ if ((Pointer) sliced != DatumGetPointer(x))
+ pfree(sliced);
+
+ return result;
+}
+
#if PG_VERSION_NUM >= 170000
#define vector_isspace(ch) scanner_isspace(ch)
#else
@@ -756,7 +847,7 @@ FUNCTION_PREFIX PG_FUNCTION_INFO_V1(vector_dims);
Datum
vector_dims(PG_FUNCTION_ARGS)
{
- Vector *a = PG_GETARG_VECTOR_P(0);
+ Vector *a = PG_GETARG_VECTOR_P(0, 0);
PG_RETURN_INT32(a->dim);
}
@@ -984,44 +1075,11 @@ FUNCTION_PREFIX PG_FUNCTION_INFO_V1(subvector);
Datum
subvector(PG_FUNCTION_ARGS)
{
- Vector *a = PG_GETARG_VECTOR_P(0);
int32 start = PG_GETARG_INT32(1);
int32 count = PG_GETARG_INT32(2);
- int32 end;
- float *ax = a->x;
- Vector *result;
- int dim;
+ Vector *result = PG_GETARG_VECTOR_P(0, start, count);
- if (count < 1)
- ereport(ERROR,
- (errcode(ERRCODE_DATA_EXCEPTION),
- errmsg("vector must have at least 1 dimension")));
-
- /*
- * Check if (start + count > a->dim), avoiding integer overflow. a->dim
- * and count are both positive, so a->dim - count won't overflow.
- */
- if (start > a->dim - count)
- end = a->dim + 1;
- else
- end = start + count;
-
- /* Indexing starts at 1, like substring */
- if (start < 1)
- start = 1;
- else if (start > a->dim)
- ereport(ERROR,
- (errcode(ERRCODE_DATA_EXCEPTION),
- errmsg("vector must have at least 1 dimension")));
-
- dim = end - start;
- CheckDim(dim);
- result = InitVector(dim);
-
- for (int i = 0; i < dim; i++)
- result->x[i] = ax[start - 1 + i];
-
- PG_RETURN_POINTER(result);
+ PG_RETURN_VECTOR_P(result);
}
/*
diff --git a/src/vector.h b/src/vector.h
index 20291715f..f2b1bd435 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -11,8 +11,18 @@
#define VECTOR_MAX_DIM 16000
#define VECTOR_SIZE(_dim) add_size(offsetof(Vector, x), mul_size(sizeof(float), _dim))
-#define DatumGetVector(x) ((Vector *) PG_DETOAST_DATUM(x))
-#define PG_GETARG_VECTOR_P(x) DatumGetVector(PG_GETARG_DATUM(x))
+#define VECTOR_EXPAND(x) x
+#define _DATUM_GET_VECTOR_1(x) ((Vector *) PG_DETOAST_DATUM(x))
+#define _DATUM_GET_VECTOR_2(x, len) DatumGetVectorPrefix(x, len)
+#define _DATUM_GET_VECTOR_3(x, start, count) DatumGetVectorSlice(x, start, count)
+#define _DATUM_GET_VECTOR_SELECT(_1, _2, _3, NAME, ...) NAME
+#define DatumGetVector(...) \
+ VECTOR_EXPAND(_DATUM_GET_VECTOR_SELECT(__VA_ARGS__, _DATUM_GET_VECTOR_3, _DATUM_GET_VECTOR_2, _DATUM_GET_VECTOR_1, 0)(__VA_ARGS__))
+#define _PG_GETARG_VECTOR_P_1(n) _DATUM_GET_VECTOR_1(PG_GETARG_DATUM(n))
+#define _PG_GETARG_VECTOR_P_2(n, len) _DATUM_GET_VECTOR_2(PG_GETARG_DATUM(n), len)
+#define _PG_GETARG_VECTOR_P_3(n, start, count) _DATUM_GET_VECTOR_3(PG_GETARG_DATUM(n), start, count)
+#define PG_GETARG_VECTOR_P(...) \
+ VECTOR_EXPAND(_DATUM_GET_VECTOR_SELECT(__VA_ARGS__, _PG_GETARG_VECTOR_P_3, _PG_GETARG_VECTOR_P_2, _PG_GETARG_VECTOR_P_1, 0)(__VA_ARGS__))
#define PG_RETURN_VECTOR_P(x) PG_RETURN_POINTER(x)
typedef struct Vector
@@ -24,6 +34,8 @@ typedef struct Vector
} Vector;
Vector *InitVector(int dim);
+Vector *DatumGetVectorPrefix(Datum x, int32 len);
+Vector *DatumGetVectorSlice(Datum x, int32 start, int32 count);
void PrintVector(char *msg, Vector * vector);
int vector_cmp_internal(Vector * a, Vector * b);
diff --git a/test/expected/halfvec.out b/test/expected/halfvec.out
index c7891508b..a5e136685 100644
--- a/test/expected/halfvec.out
+++ b/test/expected/halfvec.out
@@ -590,6 +590,29 @@ SELECT subvector('[1,2,3,4,5]'::halfvec, -2147483644, 2147483647);
[1,2]
(1 row)
+CREATE TABLE t (id int, val halfvec);
+INSERT INTO t (id, val) VALUES (1, '[1,2,3,4,5]');
+INSERT INTO t (id, val) SELECT 2, array_agg(n)::halfvec FROM generate_series(1, 2000) n;
+ALTER TABLE t ALTER COLUMN val SET STORAGE EXTENDED;
+INSERT INTO t (id, val) SELECT 3, array_agg(1)::halfvec FROM generate_series(1, 2000) n;
+SELECT id, vector_dims(val), subvector(val, 1, 3), subvector(val, 995, 4), subvector(val, 1998, 5), subvector(val, -1, 5) FROM t WHERE id >= 2 ORDER BY id;
+ id | vector_dims | subvector | subvector | subvector | subvector
+----+-------------+-----------+-------------------+------------------+-----------
+ 2 | 2000 | [1,2,3] | [995,996,997,998] | [1998,1999,2000] | [1,2,3]
+ 3 | 2000 | [1,1,1] | [1,1,1,1] | [1,1,1] | [1,1,1]
+(2 rows)
+
+SELECT vector_dims(val), subvector(val, 2, 3) FROM t WHERE id = 1;
+ vector_dims | subvector
+-------------+-----------
+ 5 | [2,3,4]
+(1 row)
+
+SELECT subvector(val, 1, 0) FROM t WHERE id = 2;
+ERROR: halfvec must have at least 1 dimension
+SELECT subvector(val, 2001, 1) FROM t WHERE id = 2;
+ERROR: halfvec must have at least 1 dimension
+DROP TABLE t;
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::halfvec, '[3,5,7]']) v;
avg
-----------
diff --git a/test/expected/vector_type.out b/test/expected/vector_type.out
index 38e388135..e8900221c 100644
--- a/test/expected/vector_type.out
+++ b/test/expected/vector_type.out
@@ -626,6 +626,29 @@ SELECT subvector('[1,2,3,4,5]'::vector, -2147483644, 2147483647);
[1,2]
(1 row)
+CREATE TABLE t (id int, val vector);
+INSERT INTO t (id, val) VALUES (1, '[1,2,3,4,5]');
+INSERT INTO t (id, val) SELECT 2, array_agg(n)::vector FROM generate_series(1, 1000) n;
+ALTER TABLE t ALTER COLUMN val SET STORAGE EXTENDED;
+INSERT INTO t (id, val) SELECT 3, array_agg(1)::vector FROM generate_series(1, 1000) n;
+SELECT id, vector_dims(val), subvector(val, 1, 3), subvector(val, 498, 4), subvector(val, 998, 5), subvector(val, -1, 5) FROM t WHERE id >= 2 ORDER BY id;
+ id | vector_dims | subvector | subvector | subvector | subvector
+----+-------------+-----------+-------------------+----------------+-----------
+ 2 | 1000 | [1,2,3] | [498,499,500,501] | [998,999,1000] | [1,2,3]
+ 3 | 1000 | [1,1,1] | [1,1,1,1] | [1,1,1] | [1,1,1]
+(2 rows)
+
+SELECT vector_dims(val), subvector(val, 2, 3) FROM t WHERE id = 1;
+ vector_dims | subvector
+-------------+-----------
+ 5 | [2,3,4]
+(1 row)
+
+SELECT subvector(val, 1, 0) FROM t WHERE id = 2;
+ERROR: vector must have at least 1 dimension
+SELECT subvector(val, 1001, 1) FROM t WHERE id = 2;
+ERROR: vector must have at least 1 dimension
+DROP TABLE t;
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]']) v;
avg
-----------
diff --git a/test/sql/halfvec.sql b/test/sql/halfvec.sql
index edfd10262..aef04f4bd 100644
--- a/test/sql/halfvec.sql
+++ b/test/sql/halfvec.sql
@@ -134,6 +134,17 @@ SELECT subvector('[1,2,3,4,5]'::halfvec, 2147483647, 10);
SELECT subvector('[1,2,3,4,5]'::halfvec, 3, 2147483647);
SELECT subvector('[1,2,3,4,5]'::halfvec, -2147483644, 2147483647);
+CREATE TABLE t (id int, val halfvec);
+INSERT INTO t (id, val) VALUES (1, '[1,2,3,4,5]');
+INSERT INTO t (id, val) SELECT 2, array_agg(n)::halfvec FROM generate_series(1, 2000) n;
+ALTER TABLE t ALTER COLUMN val SET STORAGE EXTENDED;
+INSERT INTO t (id, val) SELECT 3, array_agg(1)::halfvec FROM generate_series(1, 2000) n;
+SELECT id, vector_dims(val), subvector(val, 1, 3), subvector(val, 995, 4), subvector(val, 1998, 5), subvector(val, -1, 5) FROM t WHERE id >= 2 ORDER BY id;
+SELECT vector_dims(val), subvector(val, 2, 3) FROM t WHERE id = 1;
+SELECT subvector(val, 1, 0) FROM t WHERE id = 2;
+SELECT subvector(val, 2001, 1) FROM t WHERE id = 2;
+DROP TABLE t;
+
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::halfvec, '[3,5,7]']) v;
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::halfvec, '[3,5,7]', NULL]) v;
SELECT avg(v) FROM unnest(ARRAY[]::halfvec[]) v;
diff --git a/test/sql/vector_type.sql b/test/sql/vector_type.sql
index 565ec043b..d61c481e3 100644
--- a/test/sql/vector_type.sql
+++ b/test/sql/vector_type.sql
@@ -140,6 +140,17 @@ SELECT subvector('[1,2,3,4,5]'::vector, 2147483647, 10);
SELECT subvector('[1,2,3,4,5]'::vector, 3, 2147483647);
SELECT subvector('[1,2,3,4,5]'::vector, -2147483644, 2147483647);
+CREATE TABLE t (id int, val vector);
+INSERT INTO t (id, val) VALUES (1, '[1,2,3,4,5]');
+INSERT INTO t (id, val) SELECT 2, array_agg(n)::vector FROM generate_series(1, 1000) n;
+ALTER TABLE t ALTER COLUMN val SET STORAGE EXTENDED;
+INSERT INTO t (id, val) SELECT 3, array_agg(1)::vector FROM generate_series(1, 1000) n;
+SELECT id, vector_dims(val), subvector(val, 1, 3), subvector(val, 498, 4), subvector(val, 998, 5), subvector(val, -1, 5) FROM t WHERE id >= 2 ORDER BY id;
+SELECT vector_dims(val), subvector(val, 2, 3) FROM t WHERE id = 1;
+SELECT subvector(val, 1, 0) FROM t WHERE id = 2;
+SELECT subvector(val, 1001, 1) FROM t WHERE id = 2;
+DROP TABLE t;
+
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]']) v;
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]', NULL]) v;
SELECT avg(v) FROM unnest(ARRAY[]::vector[]) v;
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgvector/pgvector] PR #1038: Avoid full detoasting in vector_dims and subvector for vector and hal…
@ 2026-10-04 17:34 ` "ankane (@ankane)" <noreply+ankane@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: ankane (@ankane) @ 2026-10-04 17:34 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @postsql, I have the same thoughts as #1037.
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgvector/pgvector] PR #1038: Avoid full detoasting in vector_dims and subvector for vector and hal…
@ 2026-10-04 17:34 ` "ankane (@ankane)" <noreply+ankane@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: ankane (@ankane) @ 2026-10-04 17:34 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:[~2026-10-04 17:34 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-04 13:20 [pgvector/pgvector] PR #1038: Avoid full detoasting in vector_dims and subvector for vector and hal… "postsql (@postsql)" <noreply+postsql@github.com>
2026-10-04 17:34 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-04 17:34 ` "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