Skip to content

Commit

Permalink
Codify why we don't commit directly after a read call is finished
Browse files Browse the repository at this point in the history
  • Loading branch information
NinoFloris committed Dec 2, 2023
1 parent 28927ed commit 511e038
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Npgsql/Internal/PgReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace Npgsql.Internal;

public class PgReader
{
// We don't want to add a ton of memory pressure for large strings.
internal const int MaxPreparedTextReaderSize = 1024 * 64;

readonly NpgsqlReadBuffer _buffer;

bool _resumable;
Expand Down Expand Up @@ -210,11 +213,8 @@ public ValueTask<TextReader> GetTextReaderAsync(Encoding encoding, CancellationT

async ValueTask<TextReader> GetTextReader(bool async, Encoding encoding, CancellationToken cancellationToken)
{
// We don't want to add a ton of memory pressure for large strings.
const int maxPreparedSize = 1024 * 64;

_requiresCleanup = true;
if (CurrentRemaining > _buffer.ReadBytesLeft || CurrentRemaining > maxPreparedSize)
if (CurrentRemaining > _buffer.ReadBytesLeft || CurrentRemaining > MaxPreparedTextReaderSize)
return new StreamReader(GetColumnStream(), encoding, detectEncodingFromByteOrderMarks: false);

if (_preparedTextReader is { IsDisposed: false })
Expand Down
15 changes: 15 additions & 0 deletions test/Npgsql.Tests/CopyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,21 @@ public async Task ReadMoreColumnsThanExist()
}
}

[Test]
public async Task StreamingRead()
{
if (IsMultiplexing)
Assert.Ignore("Multiplexing: fails");
using var conn = await OpenConnectionAsync();

var str = new string('a', PgReader.MaxPreparedTextReaderSize + 1);
var reader = conn.BeginBinaryExport($"""COPY (values ('{str}')) TO STDOUT BINARY""");
while (reader.StartRow() != -1)
{
using var _ = reader.Read<TextReader>(NpgsqlDbType.Text);
}
}

[Test, IssueLink("https://github.com/npgsql/npgsql/issues/2330")]
public async Task Wrong_format_binary_export()
{
Expand Down

0 comments on commit 511e038

Please sign in to comment.