// ClickHouse error reference

242: TABLE_IS_READ_ONLY (code 242)

The table refuses writes because the server flagged it read-only; most often a ReplicatedMergeTree replica lost its Keeper session.

错误代码
242
名称
TABLE_IS_READ_ONLY
类别
Resource limits
错误信息
Table is in readonly mode

它的意思

TABLE_IS_READ_ONLY (242) is raised on INSERT or ALTER when the table engine put the table in read-only mode. For ReplicatedMergeTree the common trigger is a lost or expired session with ZooKeeper or ClickHouse Keeper: without a live session the replica cannot safely write. Plain MergeTree tables do not raise 242: a table with table_readonly = 1 fails INSERT with TABLE_IS_PERMANENTLY_READ_ONLY (774), and an unwritable data directory fails with code 1001 (std::exception). It is a table state, not a permission issue; that is ACCESS_DENIED (497), and a session-level limit is READONLY (164).

常见原因

  • A ReplicatedMergeTree replica lost its ZooKeeper or Keeper session, or cannot reinitialize it after a restart.
  • The filesystem holding the data path is mounted read-only or has no space left.
  • A data part failed the check at startup and the server marked the table read-only.

Find the cause

`system.replicas` (`is_readonly`, `is_session_expired`, `zookeeper_exception`) for replicated tables, `system.disks` for space and mount state, `system.parts` and `system.detached_parts` for part health, and the server log for the message that set read-only.

如何修复它

  1. See which tables are read-only and why: is_session_expired and the exception columns separate a Keeper problem from a local one.

    SELECT database, table, is_readonly, is_session_expired, total_replicas, active_replicas, zookeeper_exception, last_queue_update_exception FROM system.replicas WHERE is_readonly;
  2. For an expired Keeper session, restore connectivity to the Keeper cluster, then reinitialize the replica; it compares its state with Keeper and rebuilds its queue.

    SYSTEM RESTART REPLICA my_db.my_table;
  3. For a local cause, fix the disk or the damaged part first; system.detached_parts shows what the server set aside and why.

    SELECT name, reason FROM system.detached_parts WHERE database = 'my_db' AND table = 'my_table';
  4. When the cause is fixed but the server still refuses the table, the documented recovery is the force_restore_data flag: touch the flag file in the server's flags/ directory (or create the node under the table's flags/ path in Keeper), restart, then confirm the table is writable again:

    SELECT database, table, is_readonly, is_session_expired FROM system.replicas WHERE database = 'my_db' AND table = 'my_table';

Other ways to handle it

No client-side workaround clears the flag. Route writes to another replica meanwhile, and use force_restore_data only when the flagged parts are safe to lose.

安全地复现它

The replicated read-only state clears when the Keeper session reinitializes; nothing has to be deleted.

On a scratch server, run ALTER TABLE scratch_mt MODIFY SETTING table_readonly = 1 on a MergeTree table and INSERT fails with 774 (TABLE_IS_PERMANENTLY_READ_ONLY), not 242. Code 242 needs a ReplicatedMergeTree table, which needs a ClickHouse Keeper or ZooKeeper ensemble; the docs say replicated tables switch to read-only when Keeper is unavailable at startup.

问题

Is this the same as read-only user mode?

不。 READONLY (164) is a user session setting. 242 is the table itself refusing writes.

Does the table recover by itself?

A replica leaves read-only mode on its own once the Keeper session is back and its queue drains; for disk and part causes, fix the cause and restart.

相关错误

More resource limits errors

代码和消息文本来自 ClickHouse documentation (system.errors)。浏览完整的 ClickHouse error list 或者 搜索每个数据库错误.

// 错误更少,修复更快

Work on ClickHouse in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for ClickHouse and the other databases you use.

本页文字和数据: CC BY 4.0,请注明出处 SQLly (sqlly.app)。 代码和 SQL 片段: MIT. 许可详情 引用的错误消息文本归其数据库厂商所有。