Skip to content

[connectors] Postgres output: do not write a part of a batch whose encode failed - #8200

Open
bestouff wants to merge 4 commits into
mainfrom
issue-8020
Open

bestouff wants to merge 4 commits into
mainfrom
issue-8020

Conversation

@bestouff

@bestouff bestouff commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Linked Issue

Fixes #8020. Found during the review of #8017 (related to #8014).

What changes

When the encode of a batch failed after some rows, the Postgres output connector could write a part of that batch. An encode failure is rare: it needs a value that the serializer rejects, or a failure to read the batch.

Case Before After
1. The first encode fails The transaction stayed open. The end of the batch flushed the buffers and committed: the rows before the failed row were in the table The worker rolls the transaction back and reports that it wrote none of its rows
2. The encode fails when the batch is written again after a lost transaction The rows that the worker had put in its buffers stayed there. The next batch wrote them and counted them as its own The buffers are empty when a batch ends

How:

  • A new transaction state, EncodeFailed. The end of the batch handles it as it handles Rejected (a statement that the server rejected): rollback, and a pg_batch_end error. The controller still reports the encode error itself.
  • The worker clears its buffers when a batch ends, in one place, so that no exit path of write_batch leaves rows for the next batch.

Not changed: with more than one thread, each worker has its own transaction. The worker whose encode failed writes nothing, and the other workers commit their parts.

This PR touches the same functions as #8098. The one that merges second needs a rebase.

Describe Manual Test Plan

Test Result
New suite case first_encode_rolls_back_partial_batch: 3 rows, one statement for each row, the encode fails at row 3. Then a normal batch Pass: the table is empty after the failed batch, and has only the rows of the second batch after it
Revert check: the encode failure does not set the new state The case fails: 2 rows of the failed batch are in the table
New suite case retry_encode_discards_buffered_rows: 2 rows, the session is ended before the commit, the encode fails at row 2 when the worker writes the batch again. Then a normal batch of 1 row Pass: the table has only the row of the second batch
Revert check: the buffers are not cleared when a batch ends The case fails: row 1 of the dropped batch is in the table with the row of the second batch
Postgres unit tests and the Postgres suite (not the 2 TLS cases), local PostgreSQL 15 126 unit tests pass. Suite: the 63 output cases pass. Not run with a correct setup: the 17 CDC input cases (my server had wal_level = replica) and the 2 TLS cases
Clippy with warnings as errors, all targets, dbsp_connectors and static-connectors Pass

Checklist

  • Unit tests added/updated
  • Integration tests added/updated
  • Documentation updated
  • Changelog updated

Breaking Changes?

None. Behavior change, in the changelog: a batch whose encode fails is now dropped as a whole by the thread that had it.

| The same batch fails in 10 attempts, the first one included, for one of the causes above. | Reports an error and drops the batch. |
| The server rejects the credentials, or the database does not exist. | Reports an error and drops the batch. |
| The server rejects a statement, for example because of a constraint. | Reports an error and drops the batch. A new connection cannot correct this failure. |
| The connector cannot encode a row of the batch. | Reports an error and drops the batch. It rolls back the rows of the batch that it wrote before the failure. |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

pitfall (medium): This row says the connector "drops the batch", but with threads > 1 only the thread whose encode failed rolls back. The other threads still commit their parts (the PR description says so too). The changelog entry has the same problem. Please name the per-thread scope, for example:

Suggested change
| The connector cannot encode a row of the batch. | Reports an error and drops the batch. It rolls back the rows of the batch that it wrote before the failure. |
| The connector cannot encode a row of the batch. | Reports an error. The writer thread that had the row rolls back its part of the batch. With `threads` > 1, the other threads still commit their parts. |

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 9f8c6ad, with your wording, and the changelog says the same.

@mythical-fred-oss

Copy link
Copy Markdown
Contributor

Review of the Postgres output encode-failure fix

The fix looks correct to me. push_batch_to_encoder always calls batch_end after an encode error, so the EncodeFailed rollback always runs. The retry path drops its transaction in restart_batch, and clear_buffers at BatchEnd covers every exit path of write_batch.

What I ran Result
cargo check --locked -p dbsp_connectors --tests Pass
rustfmt --check on the changed .rs files Pass
cargo test -p dbsp_connectors --lib postgres::output The tests that need no server pass. The tests that need a server fail because there is no PostgreSQL here. I could not run the 2 new suite cases.

missing (medium): cases the new tests do not cover

  1. threads: 2, and the encode fails in one worker. The test should check that the other worker commits its part and that the failing worker writes nothing. This is the rule the PR states, and no test checks it.
  2. An encode failure in cdc mode and on the upsert/delete paths of materialized mode with an index. Each of these paths has its own ? on serialization.
  3. An encode failure after a statement in the same batch was Rejected. The new state overwrites Rejected, so pg_batch_end reports the encode message instead of the rejection. The behaviour is acceptable, but no test pins it.

Gates: tests pass (integration cases plus revert checks), docs and changelog are updated (see the inline comment on the multi-thread wording), there is no new unsafe, and there are no dependency changes. I did not apply the SQL type coverage gate: the PR changes error handling, not the encoding.

@bestouff
bestouff marked this pull request as draft December 3, 2026 14:07
@bestouff

bestouff commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. In 9f8c6ad:

Point Answer
1. threads: 2, one worker fails Added other_worker_commits_after_encode_failure: the rows of the other worker are in the table, none of the failed part
2. cdc and the other paths Added first_encode_rolls_back_partial_cdc_batch. The upsert and delete paths end in the same EncodeFailed state, so I did not add one case per ?
3. Encode failure after Rejected Not pinned. Both errors are reported separately, only the pg_batch_end text changes

Without the fix, the two new cases and first_encode_rolls_back_partial_batch fail.

@blp blp added connectors Issues related to the adapters/connectors crate bug Something isn't working labels Dec 3, 2026
…code failed

When the encode of a batch failed after some rows, the Postgres output
connector could write a part of that batch:

- The first encode failed. The worker left its transaction open, and
  the end of the batch flushed the buffers and committed. The rows
  before the failed row were in the table, and the rows after it were
  not.
- The encode failed when the worker wrote the batch again after a lost
  transaction. The rows that it had put in the buffers stayed there,
  and the next batch wrote them and counted them as its own.

Give an encode failure a transaction state of its own, EncodeFailed.
At the end of the batch the worker rolls the transaction back and
reports that it wrote none of its rows, as it does for a statement
that the server rejected. The controller reports the encode error
itself, as before.

Clear the buffers of the worker when a batch ends, so that no exit
path of a batch leaves rows for the next batch.

With more than one thread, each worker has its own transaction: the
worker whose encode failed writes nothing, and the other workers
commit their parts. This rule is not new.

The two new suite cases use a wrapper around a batch that makes the
serializer fail at a chosen row of a chosen cursor.

Fixes: #8020
Signed-off-by: Xavier Bestel <xavier.bestel@feldera.com>
… failure

With more than one writer thread, only the thread whose encode failed
drops its part of the batch. The other threads commit their parts. Say
so in the docs and the changelog.

Add suite cases for this rule with two threads, and for an encode
failure in cdc mode. Both fail without the fix.

Signed-off-by: Xavier Bestel <xavier.bestel@feldera.com>
After an encode failure, the worker kept its transaction open until
the end of the batch. The endpoint waits for every worker to finish its
encode before it ends the batch. If the failed worker had already
written a row that another worker needs to lock (a trigger, a foreign
key, a second unique index), that worker could not finish its encode,
and the batch hung.

Roll back the transaction and clear the buffers before the reply to the
endpoint. The end of the batch then reports the dropped part as before,
with the rollback failure if there was one.

The new suite case gives the failing worker a lock that the other
worker needs. It ends in time with the fix, and runs into its time
limit without it.

The changelog now limits the promise to the transaction whose encode
failed: a commit whose reply was lost can already have written rows.

Signed-off-by: Xavier Bestel <xavier.bestel@feldera.com>
@bestouff
bestouff marked this pull request as ready for review December 4, 2026 16:49
@bestouff bestouff assigned swanandx and unassigned swanandx Dec 4, 2026
@bestouff
bestouff requested a review from swanandx December 4, 2026 16:55
Comment thread docs.feldera.com/docs/changelog.md Outdated
Comment on lines +30 to +33
- Connectors: when the PostgreSQL output connector cannot encode a row, it
now drops the batch. Before, it could write a part of that batch, or write
rows of that batch together with the next batch.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bug (docs, medium): This entry and the next one describe the same change, and they disagree. This one says the connector "drops the batch". The next one (and postgresql.md) says only the thread whose encode failed drops its part, and the other threads commit theirs. It looks like a leftover from the first commit. Delete this entry and keep the per-thread one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in c4cfc55: the first entry is removed. It was a duplicate from a rebase.

@mythical-fred-oss

Copy link
Copy Markdown
Contributor

What I ran (local PostgreSQL 16, wal_level=logical, at head 25a8b24)

Check Result
cargo check --locked -p dbsp_connectors --tests pass
connector_suites encode_failure (5 new cases) pass, 10/10 repeated runs (~0.75 s each)
connector_suites integrated::postgres::output 26/26 pass
connector_suites integrated::postgres 82 pass, 3 fail: simple_tls, input_tls, cdc_privileges::documented_recipe. All 3 need a TLS or role setup my local server does not have, and the PR does not touch their code
pre-commit run --files <changed> pass

Review. The fix is correct. One encode runs per batch (push_batch_to_encoder). The early rollback releases the transaction's locks before the endpoint waits on the other workers. EncodeFailed ends write_batch on the first attempt as a permanent error, so the worker does not write the batch again. The transaction.take() on the encode error path keeps the SAFETY invariant of the transmute in batch_start_inner. The lock test is deterministic: worker 1 finishes before worker 2 starts, and the 20 s limit only matters when the fix is missing. Gates: tests ✅, flakiness ✅, docs ✅, no new deps and no new unsafe. Full SQL type coverage does not apply: the change is about transaction handling on an error path and does not depend on column types.

Coverage gaps (medium):

  • The injected failure is only in serialize_val on the insert path. Nothing tests an encode failure on an upsert, or on a delete in materialized mode (serialize_key), or a failure to open the cursor (range.cursor() returning Err). All of them reach the same Err arm, so the risk is low. Making FailingCursor also fail serialize_key and adding one delete/upsert case would pin this down.
  • Nothing tests an encode failure after a statement in the same batch failed with a retryable error (Lost, then EncodeFailed overwrites it). The current behaviour, dropping the part with no retry, looks right, but no test fixes it.

Apart from the duplicate changelog entry (inline), this looks good.

@swanandx swanandx 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.

The fix is correct. Please make these changes:

  • changelog.md has two entries for this change. Remove the first one ("it now drops the batch"), because with more than one thread the other threads still commit.
  • If #8205 merges first, remove the third commit, because #8205 removes the wait that it works around.
  • If #8205 merges first, reset the EncodeFailed state at batch end. In #8205, a worker with no rows returns before start_batch(), so the state from the last batch stays.

The tests of an encode failure used only an insert whose value did not
serialize. Add cases for the other paths that reach the same handling:

- a delete in materialized mode, whose key does not serialize: all the
  rows stay in the table;
- an upsert: the rows keep their old values;
- a cursor that does not open: nothing is written, and the next batch
  is written;
- an encode failure after a statement of the same batch lost its
  transaction to a lock timeout: the part is dropped with no new
  attempt.

All four fail when an encode failure does not roll the transaction
back.

Remove a duplicate changelog entry that a rebase left.

Signed-off-by: Xavier Bestel <xavier.bestel@feldera.com>
@bestouff

bestouff commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Status of the review points, as of c4cfc55:

Coverage gap Status
Encode failure on a delete (serialize_key) Added: delete_encode_failure_keeps_rows
Encode failure on an upsert Added: upsert_encode_failure_keeps_old_values
Failure to open the cursor Added: cursor_open_failure_drops_batch
Encode failure after a statement lost its transaction (Lost, then EncodeFailed) Added: encode_failure_after_lock_timeout_drops_batch. The worker opens the cursor one time and writes no rows
Duplicate changelog entry Fixed, see the inline reply

The four new cases fail when an encode failure does not roll the transaction back. Local PostgreSQL 15: the 9 cases of the module pass.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working connectors Issues related to the adapters/connectors crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[connectors] Postgres output writes a part of a batch whose encode failed

3 participants