pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feedFrom: Mirza-Samad-Ahmed-Baig (@Mirza-Samad-Ahmed-Baig) <noreply+Mirza-Samad-Ahmed-Baig@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] PR #871: feat: Improve error messages and add robust overflow checks
Date: Thu, 03 Jul 2025 13:34:05 +0000
Message-ID: <gh-pgvector-pgvector-871@github.com> (raw)
This pull request introduces the following updates:
src/vector.c
Enhanced error messages for invalid input in vector_in.
Added more robust overflow checks in vector_accum and vector_combine to safeguard against issues with large sums.
These improvements aim to enhance the robustness and user-friendliness of the pgvector extension.
inline patch 1: src/vector.c (+20/-8 lines) download inline-1.patch
diff --git a/src/vector.c b/src/vector.c
index d2312e042..212076dac 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -184,10 +184,16 @@ vector_in(PG_FUNCTION_ARGS)
pt++;
if (*pt == ']')
- ereport(ERROR,
+ erepart(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("vector must have at least 1 dimension")));
+ if (*pt == '\0')
+ erepart(ERROR,
+ (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+ errmsg("invalid input syntax for type vector: \"%s\"", lit),
+ errdetail("Empty string is not a valid vector representation.")));
+
for (;;)
{
float val;
@@ -203,9 +209,10 @@ vector_in(PG_FUNCTION_ARGS)
/* Check for empty string like float4in */
if (*pt == '\0')
- ereport(ERROR,
- (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
- errmsg("invalid input syntax for type vector: \"%s\"", lit)));
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+ errmsg("invalid input syntax for type vector: \"%s\"", lit),
+ errdetail("Empty string is not a valid vector representation.")));
errno = 0;
@@ -1157,12 +1164,14 @@ vector_accum(PG_FUNCTION_ARGS)
{
for (int i = 0; i < dim; i++)
{
- double v = statevalues[i + 1] + x[i];
+ double v = statevalues[i + 1];
/* Check for overflow */
- if (isinf(v))
+ if ((x[i] > 0 && v > DBL_MAX - x[i]) || (x[i] < 0 && v < DBL_MIN - x[i]))
float_overflow_error();
+ v += x[i];
+
statedatums[i + 1] = Float8GetDatum(v);
}
}
@@ -1227,12 +1236,15 @@ vector_combine(PG_FUNCTION_ARGS)
statedatums = CreateStateDatums(dim);
for (int i = 1; i <= dim; i++)
{
- double v = statevalues1[i] + statevalues2[i];
+ double v1 = statevalues1[i];
+ double v2 = statevalues2[i];
/* Check for overflow */
- if (isinf(v))
+ if ((v2 > 0 && v1 > DBL_MAX - v2) || (v2 < 0 && v1 < DBL_MIN - v2))
float_overflow_error();
+ v = v1 + v2;
+
statedatums[i] = Float8GetDatum(v);
}
}
view thread (3+ messages) latest in thread
Message-ID: <gh-pgvector-pgvector-871@github.com>
Permalink: ../gh-pgvector-pgvector-871@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