diff --git a/src/halfvec.c b/src/halfvec.c
index 47f5464c3..6adf49236 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"
@@ -692,9 +693,11 @@ FUNCTION_PREFIX PG_FUNCTION_INFO_V1(halfvec_vector_dims);
 Datum
 halfvec_vector_dims(PG_FUNCTION_ARGS)
 {
-	HalfVector *a = PG_GETARG_HALFVEC_P(0);
+	Datum		raw_datum = PG_GETARG_DATUM(0);
+	Size		raw_size = toast_raw_datum_size(raw_datum);
+	int32		dim = (int32) ((raw_size - offsetof(HalfVector, x)) / sizeof(half));
 
-	PG_RETURN_INT32(a->dim);
+	PG_RETURN_INT32(dim);
 }
 
 /*
@@ -940,13 +943,17 @@ FUNCTION_PREFIX PG_FUNCTION_INFO_V1(halfvec_subvector);
 Datum
 halfvec_subvector(PG_FUNCTION_ARGS)
 {
-	HalfVector *a = PG_GETARG_HALFVEC_P(0);
+	Datum		raw_datum = PG_GETARG_DATUM(0);
 	int32		start = PG_GETARG_INT32(1);
 	int32		count = PG_GETARG_INT32(2);
+	Size		raw_size = toast_raw_datum_size(raw_datum);
+	int32		a_dim = (int32) ((raw_size - offsetof(HalfVector, x)) / sizeof(half));
 	int32		end;
-	half	   *ax = a->x;
 	HalfVector *result;
 	int32		dim;
+	int32		sliceoffset;
+	int32		slicelength;
+	struct varlena *sliced;
 
 	if (count < 1)
 		ereport(ERROR,
@@ -954,18 +961,18 @@ halfvec_subvector(PG_FUNCTION_ARGS)
 				 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.
+	 * 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;
+	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)
+	else if (start > a_dim)
 		ereport(ERROR,
 				(errcode(ERRCODE_DATA_EXCEPTION),
 				 errmsg("halfvec must have at least 1 dimension")));
@@ -974,8 +981,14 @@ halfvec_subvector(PG_FUNCTION_ARGS)
 	CheckDim(dim);
 	result = InitHalfVector(dim);
 
-	for (int i = 0; i < dim; i++)
-		result->x[i] = ax[start - 1 + i];
+	sliceoffset = (offsetof(HalfVector, x) - VARHDRSZ) + (start - 1) * sizeof(half);
+	slicelength = dim * sizeof(half);
+	sliced = PG_DETOAST_DATUM_SLICE(raw_datum, sliceoffset, slicelength);
+
+	memcpy(result->x, VARDATA_ANY(sliced), slicelength);
+
+	if ((Pointer) sliced != DatumGetPointer(raw_datum))
+		pfree(sliced);
 
 	PG_RETURN_POINTER(result);
 }
diff --git a/src/vector.c b/src/vector.c
index ebd78c14a..139f885ac 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"
@@ -756,9 +757,11 @@ FUNCTION_PREFIX PG_FUNCTION_INFO_V1(vector_dims);
 Datum
 vector_dims(PG_FUNCTION_ARGS)
 {
-	Vector	   *a = PG_GETARG_VECTOR_P(0);
+	Datum		raw_datum = PG_GETARG_DATUM(0);
+	Size		raw_size = toast_raw_datum_size(raw_datum);
+	int32		dim = (int32) ((raw_size - offsetof(Vector, x)) / sizeof(float));
 
-	PG_RETURN_INT32(a->dim);
+	PG_RETURN_INT32(dim);
 }
 
 /*
@@ -984,13 +987,17 @@ FUNCTION_PREFIX PG_FUNCTION_INFO_V1(subvector);
 Datum
 subvector(PG_FUNCTION_ARGS)
 {
-	Vector	   *a = PG_GETARG_VECTOR_P(0);
+	Datum		raw_datum = PG_GETARG_DATUM(0);
 	int32		start = PG_GETARG_INT32(1);
 	int32		count = PG_GETARG_INT32(2);
+	Size		raw_size = toast_raw_datum_size(raw_datum);
+	int32		a_dim = (int32) ((raw_size - offsetof(Vector, x)) / sizeof(float));
 	int32		end;
-	float	   *ax = a->x;
 	Vector	   *result;
 	int			dim;
+	int32		sliceoffset;
+	int32		slicelength;
+	struct varlena *sliced;
 
 	if (count < 1)
 		ereport(ERROR,
@@ -998,18 +1005,18 @@ subvector(PG_FUNCTION_ARGS)
 				 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.
+	 * 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;
+	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)
+	else if (start > a_dim)
 		ereport(ERROR,
 				(errcode(ERRCODE_DATA_EXCEPTION),
 				 errmsg("vector must have at least 1 dimension")));
@@ -1018,8 +1025,14 @@ subvector(PG_FUNCTION_ARGS)
 	CheckDim(dim);
 	result = InitVector(dim);
 
-	for (int i = 0; i < dim; i++)
-		result->x[i] = ax[start - 1 + i];
+	sliceoffset = (offsetof(Vector, x) - VARHDRSZ) + (start - 1) * sizeof(float);
+	slicelength = dim * sizeof(float);
+	sliced = PG_DETOAST_DATUM_SLICE(raw_datum, sliceoffset, slicelength);
+
+	memcpy(result->x, VARDATA_ANY(sliced), slicelength);
+
+	if ((Pointer) sliced != DatumGetPointer(raw_datum))
+		pfree(sliced);
 
 	PG_RETURN_POINTER(result);
 }
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;
