Skip to content

Back off the MaterializedPostgreSQL consumer after an exception - #127678

Merged
alexey-milovidov merged 8 commits into
masterfrom
fix/materialized-postgresql-consumer-backoff-on-exception
Oct 31, 2026
Merged

alexey-milovidov merged 8 commits into
masterfrom
fix/materialized-postgresql-consumer-backoff-on-exception

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

When MaterializedPostgreSQLConsumer::consume throws (for example, syncTables cannot flush a buffered table because a materialized view on the nested table throws), PostgreSQLReplicationHandler::consumerFunc rescheduled the consumer task immediately and ignored materialized_postgresql_backoff_min_ms. The task then ran back-to-back on the pool threads and re-acquired its exec_mutex right away. BackgroundSchedulePoolTaskInfo::deactivate, which DETACH TABLE, ATTACH TABLE and shutdown call, could starve on that mutex for minutes. The consumer now backs off after an exception, the same way consume already does for the errors it handles itself.

This is why test_postgresql_replica_database_engine/test_3.py::test_detach_table_with_pending_buffer is flaky: DETACH TABLE test_database.test_detach_stuck PERMANENTLY took 364 s under ASan, and the log showed thousands of Scheduling replication thread: now retries meanwhile. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=110493&sha=ca77771a0dba59ede9b97d7b289482f6e8aad138&name_0=PR&name_1=Integration%20tests%20(amd_asan_ubsan%2C%20flaky). The same hang was seen on #120273, which only touched tests.

The test test_detach_table_with_pending_buffer now uses a fixed backoff of 1000 ms and requires repeated sync failures to take at least 1.5 s. Locally, without the fix the failures arrived within 0.5 s and the test failed; with the fix it passes.

Related: #110493
Related: #120273

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

MaterializedPostgreSQL no longer retries a failing synchronization in a tight loop, which could make DETACH TABLE hang for minutes.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/127678]

Version info

  • Merged into: 26.11.1.980-master (included in 26.11 and later)

When `MaterializedPostgreSQLConsumer::consume` threw (for example, `syncTables`
failed to flush a buffered table because a materialized view on the nested table
throws), `PostgreSQLReplicationHandler::consumerFunc` rescheduled the consumer task
immediately, ignoring `materialized_postgresql_backoff_min_ms`. The task then ran
back-to-back on the pool threads, re-acquiring its `exec_mutex` right away, and
`BackgroundSchedulePoolTaskInfo::deactivate` (used by `DETACH TABLE`, `ATTACH TABLE`
and shutdown) could starve on that mutex for minutes.

This made `test_postgresql_replica_database_engine/test_3.py::test_detach_table_with_pending_buffer`
flaky: `DETACH TABLE ... PERMANENTLY` hung for 364 s under ASan.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=110493&sha=ca77771a0dba59ede9b97d7b289482f6e8aad138&name_0=PR&name_1=Integration%20tests%20(amd_asan_ubsan%2C%20flaky)
#110493

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6b49d95]

Summary: ✅


AI Review

Summary

PR #127678 routes exceptions escaping MaterializedPostgreSQLConsumer::consume through the existing backoff logic, preventing immediate retries from starving task deactivation during DETACH TABLE and ATTACH TABLE. The revised integration test checks repeated synchronization failures before detaching and verifies that replication continues for another table. No actionable findings in head 6b49d954cf5adbaaf9788728a3f7ef0405ebc9a8.

Missing context / blind spots
  • ⚠️ Integration tests were not run locally; this checkout has no build directory. The PR documents focused validation that fails without the fix and passes with it. Current-head CI was still running, with no targeted integration or bugfix-validation results available when reviewed; completed focused CI would provide independent runtime confirmation.
Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 24, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-postgresql PostgreSQL-specific integration (table engine/function, logical replication bridge). label Oct 24, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged the latest master. The only failing test in Upgrade check (amd_release) reports that use_query_condition_cache_for_time_conditions is missing from the settings changes history. This PR does not change it: the setting is wrong on master itself, and the open fix is #127687. The Finish Workflow new-tests check fails because this fix is covered by the existing integration test test_postgresql_replica_database_engine/test_3.py::test_detach_table_with_pending_buffer, which the PR does not modify.

@clickhouse-gh

clickhouse-gh Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6b49d954c with master 94d1a63f2 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 13 s compile time in total, 7 of them have a recent master baseline.

Job report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged the latest master again. It now includes #127687, which fixes the use_query_condition_cache_for_time_conditions settings-history failure in Upgrade check (amd_release). The Finish Workflow new-tests check is still expected to fail: this fix is covered by the existing integration test test_postgresql_replica_database_engine/test_3.py::test_detach_table_with_pending_buffer.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged the latest master, which includes #127781 — the fix for the 05218_text_index_substreams_opened_lazily failure with use_columns_cache = 1 in Stateless tests (arm_binary, sequential). That failure is unrelated to this PR. The Finish Workflow new-tests check is still expected to fail: the regression is covered by the existing integration test test_postgresql_replica_database_engine/test_3.py::test_detach_table_with_pending_buffer, which needs PostgreSQL and cannot be a stateless test.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 CI status of commit 44a4ccc: all remaining failures are unrelated to this PR (it only adds a backoff in PostgreSQLReplicationHandler::consumerFunc).

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged the latest master (commit 66af1a0). It includes #128067, which makes Build profile diff stop failing on the overloaded CI logs cluster. The Variant logical error in Stateless tests (arm_binary, AsyncInsert, s3 storage, parallel) still shows up across many PRs and on master. It is unrelated to this PR and is being fixed in #127867.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (adds 59f220c, which skips 05397_clickhouse_local_locked_path_untouched on macOS — the Fast test (arm_darwin) failure was unrelated to this PR).

The remaining red check is new_tests_check.py ("No new tests have been added"). The behavior is covered by the existing integration test test_postgresql_replica_database_engine/test_3.py::test_detach_table_with_pending_buffer, which was flaky for this exact reason. A dedicated regression test would have to assert that DETACH TABLE finishes within some wall-clock time against a real PostgreSQL, which is not deterministic and cannot be a stateless test. A human needs to decide whether to waive the check for this PR.

…with_pending_buffer`

The test already makes `syncTables` fail on every retry. Use a fixed backoff of
1000 ms and require that six push failures take at least 1.5 s. Without the fix
in `PostgreSQLReplicationHandler::consumerFunc`, the consumer retried
back-to-back and the failures appeared within 0.5 s. With the fix, the test
passes. The materialized view now gets a unique name, so a rerun of the test
cannot match log lines from an earlier run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=127678&sha=9c5723ce2c0ab6ef009c97bf3f65e4c1e487d85f&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20s3%20storage%2C%20DBReplicated%2C%20parallel%2C%202%2F2%29 (05453_webterminal_close_at_binary_format_question + Some queries hung: the query stays in system.processes after the WebSocket is closed at the binary format question) and provide a fix in a separate PR. If the fix is already in progress, link it here. It has failed on master and in about 20 PRs since 2026-10-28, when the test was added in #127912.

The No new tests have been added failure is addressed by 911f552, which makes test_detach_table_with_pending_buffer check the backoff.

@groeneai

Copy link
Copy Markdown
Collaborator

The fix is already in progress: #126995 (mine, open). @ PedroTadim merged master into it today; CI on that head is green, and it is waiting for a review.

This run has the same cause as the one I described in #125952 (comment). The server log of this job (server time):

  • the web terminal session of 05453 starts at 02:56:45.631;
  • 05394_executable_pool_does_not_keep_closed_connection_open runs in parallel, and its dictGet starts the executable_pool process /bin/sh (pid 122753) at 02:56:46.365;
  • the WebSocket closes at 02:56:46.660 (Closing server descriptors and waiting for client to finish), and that handler thread logs nothing after it: no Embedded client has finished.

pid 122753 inherited the pseudo-terminal master (openPty opens it without O_CLOEXEC), so closing it in the server does not hang up the slave, and the embedded client stays blocked in read() inside ask() (hung-query stack: ReadBufferFromFileDescriptor::readImpl <- readStringUntilNewlineInto <- DB::ask <- ClientBase::initOutputFormat <- ClientEmbeddedRunner::clientRoutine). #126995 opens both ends with O_CLOEXEC. This job has no ReadBuffer is canceled. Can't read from it. abort, so #128090 is not involved.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks @groeneai. Merged the latest master (6b49d95) to re-run CI. The Fast test failure of 05453_webterminal_close_at_binary_format_question is unrelated to this PR, and its fix is in #126995, which is still open. That failure dropped the integration Bugfix validation jobs, so the new_tests_check.py failure in Finish Workflow follows from it. The strengthened test_detach_table_with_pending_buffer (911f552) should validate the fix once Fast test passes.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Trivial. LGTM.

@alexey-milovidov alexey-milovidov self-assigned this Oct 31, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 31, 2026
Merged via the queue into master with commit 52529f0 Oct 31, 2026
180 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix/materialized-postgresql-consumer-backoff-on-exception branch October 31, 2026 20:08
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-postgresql PostgreSQL-specific integration (table engine/function, logical replication bridge). pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants