Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PgReader.GetTextReader with size > 65536 fails #5450

Closed
carlokok opened this issue Nov 24, 2023 · 1 comment · Fixed by #5454
Closed

PgReader.GetTextReader with size > 65536 fails #5450

carlokok opened this issue Nov 24, 2023 · 1 comment · Fixed by #5454
Assignees
Labels
Milestone

Comments

@carlokok
Copy link

Steps to reproduce

Query:

select d.data from pubdata.mt_doc_displaytype as d where ((d.data ->> 'Public' is not null and CAST(d.data ->> 'Public' as boolean) = True) and CAST(d.data ->> 'RoomId' as uuid) = 'c04785c8-6f3b-4446-9a78-3b9546e42273'
) order by CAST(d.data ->> 'SortOrder' as integer);

Code:

using var conn = new Npgsql.NpgsqlConnection("....");
conn.Open();
using var cmd = conn.CreateCommand();
cmd.CommandText = "select d.data from pubdata.mt_doc_displaytype as d where ((d.data ->> 'Public' is not null and CAST(d.data ->> 'Public' as boolean) = True) and CAST(d.data ->> 'RoomId' as uuid) = 'c04785c8-6f3b-4446-9a78-3b9546e42273'\r\n) order by CAST(d.data ->> 'SortOrder' as integer)";
using var read = cmd.ExecuteReader();
while (read.Read())
{
    read.GetTextReader(0);
}

against attached sql dump.
dump.zip

The issue

When using .GetTextReader() it calls PgReader.cs GetTextReader() which returns a streaming reader (StreamReader) if the column size is > 65536 (const int maxPreparedSize = 1024 * 64;) however EndRead fails if the FieldOffset does not exactly match the FieldSize:

		if (FieldOffset != FieldSize)
		{
			ThrowNotConsumedExactly();
		}

Even before returning from the outer GetTextReader

Exception:

>	Npgsql.dll!Npgsql.Internal.PgReader.ThrowNotConsumedExactly() Line 802	C#
 	Npgsql.dll!Npgsql.Internal.PgReader.EndRead() Line 492	C#
 	Npgsql.dll!Npgsql.NpgsqlDataReader.GetFieldValueCore<System.IO.TextReader>(int ordinal) Line 1629	C#
 	Npgsql.dll!Npgsql.NpgsqlDataReader.GetTextReader(int ordinal) Line 1521	C#
 	ConsoleApp6.dll!ConsoleApp6.Program.Main(string[] args) Line 14	C#

'```


### Further technical details

Npgsql version: 8.0.0
PostgreSQL version: 16.0
Operating system: Windows
@NinoFloris
Copy link
Member

Thanks for your report.

I was able to reproduce the issue, #5454 fixes the underlying problem and it will be part of Npgsql 8.0.1 shortly.

@NinoFloris NinoFloris self-assigned this Nov 24, 2023
@NinoFloris NinoFloris added the bug label Nov 24, 2023
@NinoFloris NinoFloris added this to the 8.0.1 milestone Nov 24, 2023
NinoFloris added a commit that referenced this issue Nov 28, 2023
JonasWestman pushed a commit to monitor-erp/npgsql that referenced this issue Dec 20, 2023
Fixes npgsql#5450

(cherry picked from commit 42ea28f)
Signed-off-by: monjowe <jonas.westman@monitor.se>
JonasWestman pushed a commit to monitor-erp/npgsql that referenced this issue Dec 21, 2023
Fixes npgsql#5450

(cherry picked from commit 42ea28f)
Signed-off-by: monjowe <jonas.westman@monitor.se>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants