Skip to content

Complete PgSqlHandle operations only after the last result - #77

Open
jwage wants to merge 1 commit into
amphp:2.xfrom
jwage:fix-pgsql-error-after-result
Open

jwage wants to merge 1 commit into
amphp:2.xfrom
jwage:fix-pgsql-error-after-result

Conversation

@jwage

@jwage jwage commented Nov 28, 2026

Copy link
Copy Markdown

The pgsql handle can report a write the server refused as successful.

Cause

The poll callback completes an operation with whatever results it can read without blocking, even while pg_connection_busy() is still true. A statement on its own is committed after its result is sent, so a deferred constraint or deferred trigger can still refuse it. When that error arrives in a later read, the operation has already completed with the successful result, and the next send() discards the error in its drain loop.

Reproduce

$connection->query('CREATE TABLE late (id int, pad text, UNIQUE (id) DEFERRABLE INITIALLY DEFERRED)');
$connection->query("INSERT INTO late VALUES (1, '')");

for ($length = 1; $length <= 20000; $length += 3) {
    try {
        // Always violates the unique constraint, so it must always throw.
        iterator_to_array($connection->execute("INSERT INTO late VALUES (1, '') RETURNING repeat('x', $length)"));
        echo "reported as successful at length $length\n";
    } catch (Amp\Sql\SqlQueryError) {
    }
}

With PHP 8.5 and PostgreSQL 15, up to 3 of the 6667 inserts returned successfully on 2.x. With this change none did, in repeated runs. It depends on how the reply is split, so the new test makes it deterministic: a deferred trigger raises a notice (which sends the result to the client), sleeps, then raises an exception.

Fix

The results are kept across reads, and the operation completes only once pg_get_result() has returned false.

Not covered

  • ext-pq has the same problem, and much more often: with the loop above in the default unbuffered mode, nearly every refused insert (6634 of 6667) was reported as successful, and the error surfaced later as an UnhandledFutureError. Fixing it means PqHandle holding the connection until the last result, which is a larger change.
  • COPY is unchanged. While looking at this loop I noticed that on 2.x COPY (SELECT 1) TO STDOUT keeps collecting results until PHP runs out of memory, as libpq returns the COPY result repeatedly. That is the same before and after this change.

Tests

The suite passes locally on the event and stream_select drivers, and Psalm and php-cs-fixer report nothing new. With the ev driver the pgsql tests are skipped or fail on the existing pecl-ev check, as on 2.x.

🤖 Generated with Claude Code

The poll callback completed an operation as soon as it had a result, also while libpq was still busy with the
rest of the reply. An error can follow a result: a statement on its own is committed after its result is sent, so
a deferred constraint or trigger may still refuse it. That error was then discarded by the next send, and the
refused write was reported as successful.

The callback now keeps the results across reads and completes the operation once pg_get_result() has returned
false.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant