跪拜 Guibai
← All articles
Backend · Database

ksql's Session-Wide Output Controls Beat MySQL's Per-Statement Hacks

By 一只牛博 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Developers who spend hours in a database CLI need output controls that don't break their flow. ksql's session-wide toggles eliminate the repetitive boilerplate of MySQL's `\G` and `pager` tricks, and the built-in file redirection removes the need to exit the session or juggle separate tools just to save a result set.

Summary

Querying wide tables in a terminal is a mess of wrapped lines and lost column alignment. MySQL's answer is to append `\G` to individual statements, which gets tedious fast. KingbaseES's ksql client takes a different approach: meta-commands like `\x` and `\x auto` switch the entire session to vertical expanded display, so every subsequent query formats cleanly without extra typing.

Beyond display, ksql bundles `\timing` for wall-clock execution time on every statement, `\pset border` for three distinct table border styles (including a clean borderless mode for pasting into documents), and `\o` to redirect query output straight to a file without leaving the interactive session. The `\x auto` mode is particularly sharp—it decides whether to expand based on terminal width, not column count, so a single-row lookup gets the vertical treatment while a multi-row result stays in a compact table.

A practical warning accompanies `\o`: forgetting to close it with a bare `\o` silently swallows all subsequent output into the file, making the session appear unresponsive. And `\timing` measures client-side wall-clock time, not server execution time; real performance analysis still needs `EXPLAIN (ANALYZE, BUFFERS)`.

Takeaways
`\x` toggles expanded (vertical) display for the entire session, not just one query.
`\x auto` lets ksql decide when to expand based on terminal width—single rows expand, multi-row results stay tabular.
`\timing` appends a wall-clock execution time to every statement output; it is not a substitute for `EXPLAIN (ANALYZE, BUFFERS)`.
`\pset border` offers three styles: default with `|` separators, borderless for clean copy-paste, and a full boxed border.
`\o <file>` redirects all query output to a file inside the interactive session; a bare `\o` closes redirection.
Forgetting to close `\o` makes the session appear frozen because all output, including prompts, goes to the file.
Conclusions

MySQL's `\G` is a per-statement band-aid; ksql's `\x` treats output format as session state, which matches how people actually work—they don't want to decide formatting for every single query.

`\x auto` is the real quality-of-life feature. Basing the decision on terminal width rather than column count means resizing your window changes behavior, which is intuitive once you know the rule.

`\timing` is dangerously easy to misinterpret as a performance metric. It includes network latency and client processing, so two identical queries can show different times. The tool is for spotting outliers, not benchmarking.

`border 0` solves a mundane but persistent pain: copying terminal output into a doc or Slack message without spending 30 seconds cleaning up ASCII art borders.

In-session `\o` redirection is powerful but the silent-failure mode when you forget to close it is a footgun. A session that appears hung because output is going to a file will waste time for anyone who doesn't know the trick.

Concepts & terms
Expanded display (\x)
A ksql meta-command that switches query output from horizontal tables to a vertical key-value format where each column appears on its own line as `column_name | value`. It is a session-level toggle, not a per-statement modifier.
\x auto
A ksql mode where the client automatically chooses between horizontal and expanded display based on the current terminal width. Single-row results typically trigger expansion; multi-row results stay tabular if they fit.
\timing
A ksql meta-command that reports the wall-clock execution time of each SQL statement, measured on the client side. It includes network round-trip time and is not equivalent to server-side execution time from EXPLAIN ANALYZE.
\pset border
A ksql formatting command that controls table border rendering. `border 1` uses pipe separators, `border 0` removes all separators for clean text output, and `border 2` draws a full box around the result set.
\o (output redirection)
A ksql meta-command that redirects all subsequent query output to a specified file. A bare `\o` with no argument closes the redirection and restores terminal output. Forgetting to close it causes all output to silently go to the file.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗