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

Improve error message when query is canceled during streaming #507

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ext/pg_result.c
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,9 @@ pgresult_stream_any(VALUE self, int (*yielder)(VALUE, int, int, void*), void* da
if( pgresult == NULL )
rb_raise( rb_eNoResultError, "no result received - possibly an intersection with another query");

if( nfields != PQnfields(pgresult) && PQnfields(pgresult) == 0 )
rb_raise(rb_eInvalidChangeOfResultFields, "number of fields changed in single row mode to 0 - this is a sign that the query was canceled or timed out");

if( nfields != PQnfields(pgresult) )
rb_raise( rb_eInvalidChangeOfResultFields, "number of fields changed in single row mode from %d to %d - this is a sign for intersection with another query", nfields, PQnfields(pgresult));

Expand Down
14 changes: 13 additions & 1 deletion spec/pg/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@
}.to raise_error(PG::DivisionByZero)
end

it "raises an error if result number of rows change" do
it "raises an error if result number of fields change" do
@conn.send_query( "SELECT 1" )
@conn.set_single_row_mode
expect{
Expand All @@ -251,6 +251,18 @@
end
}.to raise_error(PG::InvalidChangeOfResultFields, /from 1 to 2 /)
end

it "raises an error if there is a timeout during streaming" do
@conn.exec( "SET local statement_timeout = 20" )

@conn.send_query( "SELECT 1, true UNION ALL SELECT 2, (pg_sleep(0.1) IS NULL)" )
@conn.set_single_row_mode
expect{
@conn.get_result.stream_each_row do |row|
# No-op
end
}.to raise_error(PG::InvalidChangeOfResultFields, /canceled or timed out/)
end
end

it "inserts nil AS NULL and return NULL as nil" do
Expand Down