Skip to content

.Net: Bug: Postgres upsert fails #11272

Closed
@dluc

Description

@dluc

The code below fails on UpsertAsync. Manual insert with SQL works fine instead.

This works:

INSERT INTO public."tests" ("id", "Count", "Vec")
VALUES ('1', 5, '[0.15, 0.25, 0.35]')
ON CONFLICT ("id")
DO UPDATE SET "Count"=5, "Vec"='[0.15, 0.25, 0.35]';

This fails:

public static async Task Run(string connString)
{
    var dataSource = NpgsqlDataSource.Create(connString);
    var db = new PostgresVectorStore(dataSource);
    var collection = db.GetCollection<string, Model>("tests") as PostgresVectorStoreRecordCollection<string, Model>;
    await collection.DeleteCollectionAsync().ConfigureAwait(false);
    await collection.CreateCollectionAsync().ConfigureAwait(false);

    // exception
    await collection.UpsertAsync(
        new Model { Id = "1", Count = 5, Vec = new ReadOnlyMemory<float>([0.15f, 0.25f, 0.35f]) });
}

public class Model
{
    [VectorStoreRecordKey(StoragePropertyName = "id")]
    public string Id { get; set; }

    [VectorStoreRecordVector(3)]
    public ReadOnlyMemory<float> Vec { get; set; }

    [VectorStoreRecordData(IsFilterable = true)]
    public int Count { get; set; }
}

Postgres: docker.io/pgvector/pgvector:pg17

Error:

Unhandled exception. Microsoft.Extensions.VectorData.VectorStoreOperationException: Call to vector store failed.
---> System.InvalidCastException: Writing values of 'Pgvector.Vector' is not supported for parameters having no NpgsqlDbType or DataTypeName. Try setting one of these values to the expected database type..
at Npgsql.Internal.AdoSerializerHelpers.g__ThrowWritingNotSupported|1_0(Type type, PgSerializerOptions options, Nullable1 pgTypeId, Nullable1 npgsqlDbType, Exception inner)
at Npgsql.Internal.AdoSerializerHelpers.GetTypeInfoForWriting(Type type, Nullable1 pgTypeId, PgSerializerOptions options, Nullable1 npgsqlDbType)
at Npgsql.NpgsqlParameter.ResolveTypeInfo(PgSerializerOptions options)
at Npgsql.NpgsqlParameterCollection.ProcessParameters(PgSerializerOptions options, Boolean validateValues, CommandType commandType)
at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
at Microsoft.SemanticKernel.Connectors.Postgres.PostgresVectorStoreDbClient.ExecuteNonQueryAsync(PostgresSqlCommandInfo commandInfo, CancellationToken cancellationToken) in ~/dotnet/src/Connectors/Connectors.Memory.Postgres/PostgresVectorStoreDbClient.cs:line 266
at Microsoft.SemanticKernel.Connectors.Postgres.PostgresVectorStoreDbClient.ExecuteNonQueryAsync(PostgresSqlCommandInfo commandInfo, CancellationToken cancellationToken) in ~/dotnet/src/Connectors/Connectors.Memory.Postgres/PostgresVectorStoreDbClient.cs:line 267
at Microsoft.SemanticKernel.Connectors.Postgres.PostgresVectorStoreDbClient.UpsertAsync(String tableName, Dictionary2 row, String keyColumn, CancellationToken cancellationToken) in ~/dotnet/src/Connectors/Connectors.Memory.Postgres/PostgresVectorStoreDbClient.cs:line 122 at Microsoft.SemanticKernel.Connectors.Postgres.PostgresVectorStoreRecordCollection2.<>c__DisplayClass14_0.<b__1>d.MoveNext() in ~/dotnet/src/Connectors/Connectors.Memory.Postgres/PostgresVectorStoreRecordCollection.cs:line 162
--- End of stack trace from previous location ---
at Microsoft.SemanticKernel.Connectors.Postgres.PostgresVectorStoreRecordCollection2.RunOperationAsync[T](String operationName, Func1 operation) in ~/dotnet/src/Connectors/Connectors.Memory.Postgres/PostgresVectorStoreRecordCollection.cs:line 346
--- End of inner exception stack trace ---
at Microsoft.SemanticKernel.Connectors.Postgres.PostgresVectorStoreRecordCollection2.RunOperationAsync[T](String operationName, Func1 operation) in ~/dotnet/src/Connectors/Connectors.Memory.Postgres/PostgresVectorStoreRecordCollection.cs:line 350

Metadata

Metadata

Assignees

Labels

.NETIssue or Pull requests regarding .NET codedocumentationmsft.ext.vectordataRelated to Microsoft.Extensions.VectorData

Type

No type

Projects

Status

Sprint: Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions