Skip to content

Fix progress bar flicker when piping large data output - #98993

Closed
Onyx2406 wants to merge 5 commits into
ClickHouse:masterfrom
Onyx2406:fix/progress-bar-flicker
Closed

Onyx2406 wants to merge 5 commits into
ClickHouse:masterfrom
Onyx2406:fix/progress-bar-flicker

Conversation

@Onyx2406

@Onyx2406 Onyx2406 commented Mar 8, 2026 •

Copy link
Copy Markdown
Contributor

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):
Fixed progress bar flickering when piping output through another command with --progress enabled.

Documentation entry for user-facing changes

  • Documentation is not required (bug fix)

What this PR does

When piping ClickHouse output through another process, the progress bar flickers rapidly:

# Flickers:
ch --progress -q "SELECT * FROM system.numbers" | ch --input-format TSV -q "SELECT count() FROM table"

Root cause

onData() clears the progress bar before every data block output (line 550). When progress is rendered on stderr but data goes to stdout (common when piping), they use different file descriptors and don't visually interfere. The unnecessary clear causes visible flicker as the progress bar is rapidly shown and hidden.

The code even has a comment acknowledging this: "If results are written INTO OUTFILE, we can avoid clearing progress to avoid flicker." — the same optimization was missing for the pipe case.

Fix (6 insertions, 1 file)

Skip the progress bar clear in onData() when tty_buf->getFD() != stdout_fd — meaning progress and data output use different file descriptors. When they share the same fd (interactive mode), the clear is still performed to prevent overlap.

Not changed

Other clearProgressOutput call sites (onLogData, onEndOfStream, onProfileEvents, etc.) are left untouched because:

  • onLogData: logs go to stderr where progress also lives — clear IS needed
  • End-of-stream/profile events: one-time cleanup, not high-frequency

Fixes #80056


Note

Low Risk
Low-risk client-side UI change that only adjusts when progress output is cleared based on file descriptors; main risk is minor progress rendering regressions in unusual tty/embedded setups.

Overview
Fixes progress bar/table flicker in ClientBase::onData() by skipping the per-block progress clear when progress output is on a different file descriptor than query results (e.g. progress on stderr while data streams to stdout during piping).

Clearing still happens when stdout is a TTY or when the progress buffer shares stdout_fd (embedded client), preserving overlap protection in interactive/shared-fd scenarios.

Written by Cursor Bugbot for commit 1ed7e4f. This will update automatically on new commits. Configure here.

When progress is rendered on a different file descriptor than data
output (e.g. progress on stderr, data on stdout), there is no need
to clear the progress bar before each data block. The clear causes
visible flicker when piping because the progress bar is rapidly
shown and hidden even though it doesn't interfere with the data
output on a separate fd.

Skip the progress bar clear in onData() when tty_buf's fd differs
from stdout_fd. This preserves the clear when both share the same
fd (interactive mode) where it's needed to prevent overlap.

Fixes ClickHouse#80056

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Comment thread src/Client/ClientBase.cpp Resolved
Onyx2406 added 4 commits March 8, 2026 15:47
The previous check compared tty_buf->getFD() with stdout_fd, but in
normal interactive mode tty_buf uses stderr_fd (preferred when
stderr_is_a_tty) while stdout_fd is a different number — even though
both point to the same terminal. This would cause progress and data
to overlap in interactive mode (regression).

Use stdout_is_a_tty instead: when stdout is a tty, progress and data
share the same terminal so clearing is needed. When stdout is piped
(not a tty), progress goes to a different terminal and clearing is
unnecessary.
When isEmbeeddedClient() is true and stdout_is_a_tty is false,
tty_buf still uses stdout_fd — progress and data share the same
output stream. The previous check (stdout_is_a_tty only) would
skip clearing in this case, causing garbled output.

New condition: clear progress when stdout is a tty OR when
tty_buf's fd matches stdout_fd (embedded client sharing stdout).
@alexey-milovidov

Copy link
Copy Markdown
Member

Fixed in #93298

@alexey-milovidov alexey-milovidov self-assigned this Jun 17, 2026
shevlfs added a commit to shevlfs/ClickHouse that referenced this pull request Aug 30, 2026
The client cleared the progress bar (rendered on the terminal via
`tty_buf`) before flushing every data block, even when the result was
written to a pipe or a file where it cannot interleave with the progress
rendering. For fast streaming queries such as
`clickhouse --progress -q 'SELECT * FROM system.numbers' | ...` this
erased and redrew the bar hundreds of times per second, so the bar was
invisible most of the time.

Skip the clearing in the pre-flush callback of `std_out_wrapper` when
the data cannot interleave with the progress rendering: keep it when
stdout is a terminal, when the output goes into a pager, or for a client
embedded into the server, where the data and the progress share one
stream.

Measured with a pty harness (stderr on a pty, stdout piped to /dev/null)
on `SELECT * FROM system.numbers LIMIT 30000000`: before, 459 erase
sequences for 471 progress redraws in one run; after, 1 erase sequence
(the final cleanup) for 465 redraws. When stdout is the terminal, the
clearing is preserved: one erase per flushed data block, as before.

A similar approach was earlier proposed in PR
ClickHouse#98993.

Closes: ClickHouse#80056
shevlfs added a commit to shevlfs/ClickHouse that referenced this pull request Aug 30, 2026
Follow-up to the previous commit for issue ClickHouse#80056. Skipping the clearing
of the progress bar before flushing a data block is only valid when the
progress is rendered on a real terminal while the data goes elsewhere.
With `--progress err ... 2>&1 | viewer` the progress goes into the same
pipe as the data even though stdout is not a terminal, and without the
clearing the first row of every flushed block is appended to a stale
progress-bar line: 19 garbled lines per 200000 rows (20 blocks) in a
harness, versus 0 on master. Extend the condition: skip the clearing
only when the progress target is a real terminal (`isatty` on the
`tty_buf` descriptor); `initTTYBuffer` runs at startup, so the
descriptor is available when the output format is initialized.

Why ClickHouse#93298 alone did not fix issue ClickHouse#80056: it moved the clearing into
the pre-flush callback of `std_out_wrapper`, so formats that buffer the
whole result stopped flickering (`Hash` and `Pretty`: 1 erase sequence
per query on master), but formats that flush every block (the streaming
formats with `setAutoFlush`) still cleared the bar once per block: 30
erase sequences for a 30-block `TSV` query on master, and 459 erase
sequences for 471 progress redraws in a 0.66 s run of
`SELECT * FROM system.numbers LIMIT 30000000` with stdout piped to
/dev/null. With this fix the same runs show 1 erase sequence (the final
cleanup) and a stable bar, while the merged-stream case above, the
terminal case (stdout on the terminal), and the pager case keep one
clear per flushed block exactly as master does.

Also align with the file style (`pager_cmd` instead of
`pager_cmd != nullptr`) and improve the test: trap-based cleanup of the
generated helper, a 60 s read timeout instead of 300 s, and a third
deterministic case pinning the merged-stream `--progress err 2>&1`
behavior (the assertions count erase sequences per flushed block, so
they do not depend on timing).

Related: ClickHouse#80056
Related: ClickHouse#93298
Related: ClickHouse#98993
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.

Progress bar flickers if a command line tool outputs a lot of data

2 participants