其他所有查询均因语法错误而失败

时间:2020-01-16 02:23:22

标签: npgsql cockroachdb

我使用CockroachDb和Npgsql驱动程序。

我有一个简单的Users表。当我插入新记录时,其他所有查询都会因语法错误而失败,这对我来说似乎很奇怪。

CREATE TABLE Users (
    RequestIdentifier BYTEA NOT NULL UNIQUE,
    Identifier UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    Id INT8 NOT NULL DEFAULT unique_rowid(),
    Email BYTEA NOT NULL UNIQUE,
    Username BYTEA NOT NULL,
    PasswordHash BYTEA NOT NULL
);
var q = @"
    INSERT INTO Users (RequestIdentifier, Email, Username, PasswordHash)
    VALUES (@RequestIdentifier, @Email, @Username, @PasswordHash)
    ON CONFLICT (RequestIdentifier)
    DO NOTHING
    RETURNING Identifier
";

byte[] userIdentifier = null;
using (var cmd = new NpgsqlCommand(q, dbConn)) {
    cmd.Parameters.Add("RequestIdentifier", NpgsqlDbType.Bytea);
    cmd.Parameters.Add("Email", NpgsqlDbType.Bytea);
    cmd.Parameters.Add("Username", NpgsqlDbType.Bytea);
    cmd.Parameters.Add("PasswordHash", NpgsqlDbType.Bytea);

    await cmd.PrepareAsync();

    cmd.Parameters[0].Value = msg.RequestIdentifier;
    cmd.Parameters[1].Value = msg.Email;
    cmd.Parameters[2].Value = msg.Username;
    cmd.Parameters[3].Value = passwordHash;

    try {
        userIdentifier = ((Guid) await cmd.ExecuteScalarAsync()).ToByteArray();
    } catch (PostgresException e) when (e.SqlState == SqlErrorCodes.UniqueViolation) {
        logger.Information("Email {Email} already in use", UTF8.GetString(msg.Email));
    } catch (PostgresException e) {
        logger.Error("{Exception}", e);
        throw;
    }
}
Npgsql.PostgresException (0x80004005): 42601: at or near "close": syntax error
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
   at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteScalar(Boolean async, CancellationToken cancellationToken)
   at OwlAuthentication.Services.CockroachDbStorageService.CreateOrRetrieveExistingUser(SignUpMessage msg) in C:\Users\Che Khen Kho\Desktop\dotnet\OwlAuthentication\Services\CockroachDbStorageService.cs:line 94
  Exception data:
    Severity: ERROR
    SqlState: 42601
    MessageText: at or near "close": syntax error
    Detail: source SQL:
CLOSE ALL
^
    File: lexer.go
    Line: 175
    Routine: Error

例如,如果我尝试10个查询,则其中的5个会因该异常而失败,但实际上实际上会插入5个以上的行(有时是6个,有时是8个,等等)。

我也在PostgreSQL上进行了测试(使用uuid_generate_v4代替gen_random_uuid和BIGSERIAL作为ID列),并且一切正常。

1 个答案:

答案 0 :(得分:1)

我认为此问题(https://github.com/cockroachdb/cockroach/issues/45490)已解决,该问题已在CockroachDB v20.1中修复