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

Source: https://sqlly.app/errors/clickhouse/table-is-read-only
Last updated: 2026-11-30
License: Text and data [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) (credit SQLly, sqlly.app); code and SQL snippets [MIT](https://opensource.org/license/mit). [Details](https://sqlly.app/license).
Note: Quoted error message text belongs to its database vendor.

- Error code: `242`
- Name: `TABLE_IS_READ_ONLY`
- Category: Resource limits

Error message:

```text
Table is in readonly mode
```

## What it means

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

## Common causes

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

## How to fix it

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

   ```sql
   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.

   ```sql
   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.

   ```sql
   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:

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

## Reproduce it safely

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

```sql
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.
```

## How SQLly helps

Nothing client-side clears the flag; keep SQLly on the reads you still need and point a second connection at another replica to see whether only one node refuses writes.

## Questions

### Is this the same as read-only user mode?

No. `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.

## Related errors

- [`243`](https://sqlly.app/errors/clickhouse/not-enough-space): NOT_ENOUGH_SPACE (code 243)
- [`164`](https://sqlly.app/errors/clickhouse/readonly): READONLY (code 164)

The code and message text are from the [ClickHouse documentation (system.errors)](https://clickhouse.com/docs/en/operations/system-tables/errors).
