跪拜 Guibai
← All articles
Backend

The ksql Meta-Commands Every MySQL DBA Needs on Day One

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

Teams migrating from MySQL to KingbaseES or PostgreSQL lose their muscle memory for basic database inspection. These six meta-commands replace the entire `show`/`describe` workflow and surface more metadata in one shot, but the `search_path` behavior around `\dt` is a recurring gotcha that hides tables silently.

Summary

A MySQL DBA connecting to KingbaseES for the first time hits an immediate wall: `show databases` and `use db` are gone. ksql handles the same inspection tasks through client-side meta-commands — `\l` for databases, `\c` to reconnect to a different database, `\dn` to list schemas, `\dt` for tables, `\d` for table structure, and `\du` for users. None of them need a semicolon, and none are sent to the server as SQL.

The payoff is density. `\l` returns owner, encoding, and collation alongside database names, where MySQL's `show databases` gives only names. `\d` bundles column definitions, indexes, constraints, and sequence references into a single output — replacing `describe` plus `show index` plus manual constraint queries. `\du` surfaces superuser and role attributes in one column, no `mysql.user` query needed.

A trap for MySQL users is `search_path`. `\dt` without a schema prefix only shows tables in schemas on the path, which defaults to `"$user", public`. Tables in a custom schema like `app_schema` stay invisible until you either prefix with `app_schema.*` or adjust `search_path` with `set search_path to app_schema, public;`. The setting is session-local and reverts on reconnect.

Takeaways
`\l` lists all databases with owner, encoding, collation, and access privileges — more than `show databases` provides.
`\c db_name` re-establishes the connection to switch databases, unlike MySQL's `use` which only changes context in-place.
`\dn` lists schemas within the current database; MySQL has no schema layer, so this is a new concept for MySQL DBAs.
`\dt` without a schema prefix only shows tables in schemas on `search_path`; use `\dt schema.*` to see tables outside the path.
`\d table_name` returns columns, indexes, constraints, and sequence references in one output, replacing `describe` and `show index`.
`\du` lists all users with their role attributes in a single column, no manual query against system tables needed.
`set search_path` changes `\dt` behavior immediately but only lasts for the current session; it reverts on reconnect.
Conclusions

ksql's meta-commands are client-side operations that query system catalogs, not SQL sent to the server — this means they work even when the database is in a restricted mode that blocks regular queries.

The `search_path` gotcha with `\dt` is the single biggest friction point for MySQL converts: tables exist but appear missing because the path doesn't include their schema. MySQL's flat database model never prepared anyone for this.

`\c` reconnecting rather than switching context in-place is a meaningful difference from `use`. It means connection pooling and session state (temp tables, prepared statements) don't survive a database switch the way they do in MySQL.

The prompt character changing from `#` to `>` based on superuser status is a small but effective safety cue — it won't stop you from running a dangerous command, but it makes privilege level visible at a glance without running any query.

Concepts & terms
Meta-command
A backslash-prefixed command in ksql (and psql) processed locally by the client, not sent to the database server as SQL. Examples include `\l`, `\dt`, and `\d`. They query system catalogs or control client behavior and do not require a semicolon.
search_path
A session-level setting in KingbaseES and PostgreSQL that determines which schemas are searched when an object name is used without a schema prefix. It directly affects which tables `\dt` displays and how SQL resolves unqualified table names.
bigserial
An auto-incrementing 64-bit integer type in KingbaseES and PostgreSQL. Unlike MySQL's `auto_increment` column attribute, `bigserial` creates an independent sequence object behind the scenes, which appears in `\d` output.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗