Repository navigation
[connectors] Retry a Postgres batch when a pooler ends the session - #8017
Conversation
| match error.parsed_severity() { | ||
| Some(severity) => matches!(severity, Severity::Fatal | Severity::Panic), | ||
| // Only the localizable severity is present: the sender is a server | ||
| // older than 9.6, or a pooler, and poolers do not localize. |
There was a problem hiding this comment.
what is 9.6? the postgres version or pgbouncer? if postgres, do we care about postgres<10?
There was a problem hiding this comment.
PostgreSQL 9.6, the version that added the V field (the severity that is not localized). We don't care about servers that old, but the branch is still needed: PgBouncer sends only S, C and M, so each PgBouncer error goes through it. The comment was misleading; it now names PgBouncer first (done in #8026, which touches the same file).
| // One attempt to have a connection for the next batch. If it | ||
| // fails, the worker connects again when a batch gives it rows. | ||
| if let Err(e) = self.retry_connecting() { | ||
| tracing::warn!( |
There was a problem hiding this comment.
| tracing::warn!( | |
| warn!( |
There was a problem hiding this comment.
That one doesn't compile as is: the file has no use tracing::warn, and every log call in it uses the tracing:: path. Kept it consistent with the rest of the file.
|
|
||
| - The PostgreSQL output connector now connects again and writes the batch | ||
| again when a connection pooler, such as PgBouncer, ends the session with | ||
| an error of its own (SQLSTATE `08P01`). Before, it dropped the batch. It |
There was a problem hiding this comment.
I'm hoping for a mode to retry infinite times?
| | The connection drops, or the server restarts or is not reachable. | Connects again. The wait between attempts grows to a maximum of 60 seconds. Then writes the batch again. | | ||
| | A connection pooler, such as PgBouncer, reports an error and ends the session. An example is a pooler that cannot log in to the server. | The same. | | ||
| | The same batch fails 10 times because the connection drops or the transaction cannot start. | 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. | |
There was a problem hiding this comment.
did we consider this to be fatal/panic?
There was a problem hiding this comment.
PostgreSQL sends these as FATAL, but the connect classifier makes them permanent by their SQLSTATE (28P01, 28000, 3D000, 42501), so the FATAL rule doesn't apply. For the pipeline it's a fatal connector error: the batch is dropped, the pipeline goes on, and the next batch tries to connect again. Stopping the pipeline on such an error is the last part of #8039; I'd keep it there.
|
Claude identified three potential issues. I don't think any of them is new in this PR, but they are all related to robust error handling, so worth addressing or tracking as separate issues. I asked claude for fix directions in each case, but I don't know that these are the best solutions possible. |
|
Thanks! My call:
Also rebased on main (changelog conflict only). |
PgBouncer reports each error of its own as FATAL with SQLSTATE 08P01, and closes the client session. The Postgres output connector classified these errors as permanent. It dropped the batch when the pooler could not log in to the server, and when the server connection died under a transaction. Classify a statement error by the state of the session. The error is transient if the connection is closed, if the error has no SQLSTATE, if the SQLSTATE is in class 08 or is 57P01, 57P02 or 57P03, or if the severity is FATAL or PANIC. A statement that the server rejects in a healthy session stays permanent. A transaction that cannot start no longer ends the batch. The worker marks the transaction as lost, skips the encode, and writes the batch when the batch ends, with at most 10 attempts. The batch start does not wait and does not connect again: the endpoint waits for each worker before it gives rows to any of them, so a wait there holds the transactions of the other workers, and it had no limit. The reconnect loop now reports whether it has a connection. A permanent reconnect failure stops the write at once, where the batch start looped without a wait. A worker that has no rows of a batch does not write the batch again when its transaction is lost. Before, it waited for a new connection with no limit, or it reported a dropped batch, although the other workers had committed all the rows. A stop of the pipeline that ends the wait of a worker is no longer reported as a fatal error of the endpoint. Unit tests cover the classifier. Four suite cases put a TCP proxy between the connector and Postgres. The proxy answers a chosen message as PgBouncer does: FATAL 08P01, then it closes the session. It can also end a session as the server does for the idle-in-transaction timeout (FATAL 25P03), and it can end the session of one worker only. Each case checks that the connector saw the SQLSTATE. Without the fix, the classifier tests fail and three of the suite cases lose all their rows. The fourth case fails without the change for a worker that has no rows. Document how the connector handles a lost connection, and add the fix to the changelog. The docs also say that a completion token completes when a connector drops a batch, and that the threads of the connector hold their transactions while they wait for each other: a pool with fewer server connections than threads stalls each batch, and a row lock between two threads needs a lock or statement timeout. Related: #8014 Signed-off-by: Xavier Bestel <xavier.bestel@feldera.com>
After a failover, a connection pooler can hold a session on the old primary, which is then a read-only replica. The server rejects each write with SQLSTATE 25006 and the session stays open. The Postgres output connector classified this error as permanent and dropped the batch, and it dropped each later batch for as long as the session stayed open. These changes correct this: - The classifier also treats these errors as temporary: 25006, 55P03 (lock not available), and the SQLSTATE classes 40 (serialization failure, deadlock), 53 (insufficient resources), 57 (operator intervention, which now includes a cancelled query), 58 (system error) and XX (internal error). The statement and its rows usually do not cause them. Where they do, the limit of 10 attempts bounds the cost. - Each new attempt to write a batch opens a new connection. Before, the worker kept a session that passed a validity check, and a read-only session passes it. The worker closes the old session before the new login, because the old session can hold the only connection slot of the role. It keeps the new client only together with its prepared statements. - The worker waits between the attempts: the second attempt starts at once, and each later attempt waits one second longer, 36 seconds in total for the 10 attempts. Before, the attempts on an open session ran with no wait. - A server that answers and does not prepare the statements after a new connection uses one of the 10 attempts. The wait with no limit stays for a server or a pooler that is not available: a failed login, and a connection error (class 08) of any severity, which ends the session also when a pooler sends it as ERROR (PgBouncer before 1.16). - The error for a batch that the worker gives up on states why the transaction was lost. The test proxy can now answer a statement as a server does that cannot run it, with the session left open. Four new suite cases use it. Each fails if the change that it covers is removed: the classifier, the new connection, the order of logout and login (with a role that has one connection slot), the wait, and the attempt limit for a failed prepare. Two more cases check that a failed login with severity ERROR and a pooler that sends ERROR 08P01 at the prepare do not use the attempts. A unit test covers the wait before each attempt. Document the new cases, and add the fix to the changelog. Related: #8014 Signed-off-by: Xavier Bestel <xavier.bestel@feldera.com>
|
Verdict: not this PR's fault (high confidence) — flaky pre-existing test on The PR only touches
The test asserts the OS page cache is fully evicted (0 pages) after Tracked as #8158, assigned to the test's author. Re-queueing should pass. |
Linked Issue
Related to #8014. This PR corrects the first problem of the issue: the Postgres output connector drops a batch when PgBouncer reports SQLSTATE
08P01. The second problem (the completion token reportscompletefor a dropped batch) is the intended behavior of a token: it reports the end of the processing, also when a connector failed. This PR documents it, and the way to find a dropped batch (the status of the connector).Stack: #8026 (read-only session) on this PR, and #8098 (
max_retries, no limit by default) on #8026. #8099 (updates asINSERT ... ON CONFLICT) is on main.What changes
PgBouncer reports each error of its own as FATAL with SQLSTATE
08P01, and closes the client session. The connector classified these errors as permanent and dropped the batch.08or is57P01,57P02,57P03, or the severity is FATAL or PANICA statement that the server rejects in a healthy session stays permanent.
One fact about the failure: when the
FATALerror and the end of the stream reach the Rustpostgresclient in the same read, the client reports "connection closed" and not the error, and a closed connection was retried before this PR too. So the batch was lost only when the client read the error first. The test proxy ends the session only after the next request of the client, so the suite cases always see08P01, and they assert it.Known and not changed here, because the behavior is in main before this PR:
08with severityERRORon a session that stays open (for example a bind mismatch) is written 10 times before it is dropped.batch_records_writtenkeeps the records of a failed attempt until the batch ends. The final counts are correct ([connectors] Postgres output live count batch_records_written keeps the records of a failed attempt #8027).query_wait_timeout, and a row lock between two threads hangs the batch without a lock or statement timeout ([connectors] Postgres output threads hold their transactions while they wait for each other #8096). The docs now give both, with the workaround.Describe Manual Test Plan
I ran a pipeline against PgBouncer 1.26.1 (transaction pooling) in front of PostgreSQL 15, stopped PostgreSQL while a batch of 3000 rows was pending, and started it again.
pg_batch_start,pg_exec,pg_batch_endwithpermanent: SqlState(E08P01)pg_batch_starterror withE08P01("the batch will be written again") and 3pg_conn_retryerrors while the server was down. Second run, after the review corrections, on the tree of #8026: the start failed with a closed connection, 6pg_conn_retryerrorsFATAL 08P01, at the transaction start and at the second statement of a batch) and as the server does for the idle-in-transaction timeout (FATAL 25P03)25P03case failsThe TLS cases of the Postgres suite were not run locally. The change for a pipeline stop has no test of its own.
Not changed: a pipeline that starts while PgBouncer cannot reach its server fails when the connector prepares its statements, because that path is the endpoint construction, not the batch path.
Docs added after the review: a completion token is
completealso for a dropped batch, the pool size rule, and the lock timeout.Checklist
Breaking Changes?
None. The connector now writes a batch again in cases where it dropped the batch before.