Skip to content

Drain libpq results before throwing#531

Merged
staticlibs merged 1 commit into
duckdb:mainfrom
marknefedov:fix/undrained-state
Jul 25, 2026
Merged

Drain libpq results before throwing#531
staticlibs merged 1 commit into
duckdb:mainfrom
marknefedov:fix/undrained-state

Conversation

@marknefedov

@marknefedov marknefedov commented Jul 24, 2026

Copy link
Copy Markdown

PostgresConnection::ExecuteQueries threw a std::runtime_error as soon as it saw a result with an error status, while still inside the PQgetResult() polling loop. This left the connection's result queue undrained — libpq expects every result to be pulled with PQgetResult() until it returns NULL before the connection is reused. Bailing out early via an exception left stale/pending results on the connection, so the next query issued on that (pooled/reused) connection could get confused by leftover state.

In src/postgres_connection.cpp, the error path no longer throws immediately. Instead, it records the first error message and continues the loop so all remaining results are drained via PQgetResult(). Only after the loop exits (i.e. PQgetResult() returns NULL) is the preserved error thrown.

Signed-off-by: Mark Nefedov <mvnefedov@avito.ru>
@marknefedov
marknefedov marked this pull request as ready for review July 24, 2026 20:50

@staticlibs staticlibs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Looks good to me.

@staticlibs
staticlibs merged commit 46e2929 into duckdb:main Jul 25, 2026
7 checks passed
staticlibs pushed a commit that referenced this pull request Jul 25, 2026
This is a backport of the PR #531 to `v1.5-variegata` stable branch.

Signed-off-by: Mark Nefedov <mvnefedov@avito.ru>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants