// 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.

Fehlercode
242
Name
TABLE_IS_READ_ONLY
Kategorie
Resource limits
Fehlermeldung
Table is in readonly mode

Was es bedeutet

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).

Häufige Ursachen

  • 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.

Wie du es behebst

  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.

Sicher reproduzieren

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.

Fragen

Is this the same as read-only user mode?

Nein. 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.

Zugehörige Fehler

More resource limits errors

Der Code und der Meldungstext stammen aus der ClickHouse documentation (system.errors). Durchstöbere die vollständige ClickHouse error list oder Jeden Datenbankfehler durchsuchen.

// weniger Fehler, schnellere Korrekturen

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.

Text und Daten auf dieser Seite: CC BY 4.0, Quelle: SQLly (sqlly.app). Code und SQL-Snippets: MIT. Lizenzdetails Zitierte Fehlermeldungstexte gehören dem jeweiligen Datenbankhersteller.