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;
