Skip to content

NpgsqlBinaryExporter function throws exception when reading BigInteger. #6383

@SoftStoneDevelop

Description

@SoftStoneDevelop
Table

CREATE TABLE public.bigintegernumeric
(
    value numeric NOT NULL
);

Import method

        private static async Task ImportAsync(NpgsqlDataSource npgsqlDataSource, IEnumerable<BigNumberModel> collection)
        {
            await using var connection = await npgsqlDataSource.OpenConnectionAsync();
            NpgsqlBinaryImporter import = null;
            try
            {
                import = connection.BeginBinaryImport(@"

COPY public.bigintegernumeric
(
value
) 
FROM STDIN (FORMAT BINARY)

");

                foreach (var item in collection)
                {
                    import.StartRow();
                    import.Write(item.BigInteger, NpgsqlTypes.NpgsqlDbType.Numeric);
                }
                import.Complete();
                import.Dispose();
                import = null;
            }
            finally
            {
                if (import != null)
                {
                    try
                    {
                        import.Close();
                    }
                    catch { /* ignore */ }

                    import.Dispose();
                }
            }
        }

Export method

        private static async Task<List<BigNumberModel>> ExportAsync(NpgsqlDataSource npgsqlDataSource)
        {
            await using var connection = await npgsqlDataSource.OpenConnectionAsync();
            NpgsqlBinaryExporter export = null;
            try
            {
                export = await connection.BeginBinaryExportAsync(@"

COPY public.bigintegernumeric
(
    value
) TO STDOUT (FORMAT BINARY)

");
                var list = new List<BigNumberModel>(); 
                while (await export.StartRowAsync() != -1)
                {
                    var item = new BigNumberModel();
                    item.BigInteger = await export.ReadAsync<System.Numerics.BigInteger>(NpgsqlTypes.NpgsqlDbType.Numeric);

                    list.Add(item);
                }

                await export.DisposeAsync();
                export = null;

                return list;
            }
            finally
            {
                if (export != null)
                {
                    try
                    {
                        await export.CancelAsync();
                    }
                    catch { /* ignore */ }

                    await export.DisposeAsync();
                }
            }
        }

An exception occurs when attempting to execute export:

   at Npgsql.ThrowHelper.ThrowArgumentOutOfRangeException(String paramName, String message)
   at Npgsql.Internal.PgReader.<ShouldBuffer>g__ShouldBufferSlow|115_0(Int32 byteCount)
   at Npgsql.Internal.PgReader.ShouldBuffer(Int32 byteCount)
   at Npgsql.Internal.Converters.NumericConverter.<ReadAsync>d__2.MoveNext()
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   at Npgsql.Internal.Converters.BigIntegerNumericConverter.<<ReadAsync>g__AsyncCore|2_0>d.MoveNext()
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   at Npgsql.NpgsqlBinaryExporter.<ReadAsync>d__30`1.MoveNext()
   at Npgsql.NpgsqlBinaryExporter.<ReadAsync>d__30`1.MoveNext()
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
   at NpgsqlBenchmark.Program.<ExportAsync>d__2.MoveNext() in Program.cs:line 145
   ...
Image

Nothing to have changed in the default type mapping for BigInteger. What am I doing wrong?

Info:
"Npgsql" Version="10.0.1"
TargetFramework: net10.0

Project to reproduce error
https://github.com/SoftStoneDevelop/NpgsqlBinaryExporter/blob/main/Src/NpgsqlBenchmark/Program.cs

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions