> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to rotate the credentials of an S3Queue table without reprocessing files

> Replace the access key of an S3Queue table by creating a second table with the new credentials on the same keeper_path, exchanging the two tables and dropping the old one.

The credentials of an [`S3Queue`](https://clickhouse.com/docs/reference/engines/table-engines/integrations/s3queue) table are engine arguments, and `ALTER TABLE` can change only the settings of the table. To switch to new credentials, create a second `S3Queue` table with the new credentials on the same `keeper_path`, pause the old table, swap the two tables with [`EXCHANGE TABLES`](https://clickhouse.com/docs/reference/statements/exchange), and drop the old one. The record of which files are processed is stored in ClickHouse Keeper under `keeper_path`, so the new table continues where the old one stopped and skips the files that are recorded there as processed.

## Starting point

The steps below assume a queue, a target table and a materialized view like these:

```sql theme={null}
CREATE TABLE s3_events_queue
(
    id UInt64,
    value UInt64
)
ENGINE = S3Queue(
    'https://<bucket>.s3.<region>.amazonaws.com/events/*.csv',
    '<old-access-key-id>',
    '<old-secret-access-key>',
    'CSV')
SETTINGS mode = 'unordered';

CREATE TABLE events
(
    id UInt64,
    value UInt64
)
ENGINE = MergeTree
ORDER BY id;

CREATE MATERIALIZED VIEW s3_events_mv TO events AS
SELECT
    id,
    value
FROM s3_events_queue;
```

## Steps

1. Find the Keeper path of the current table:

```sql theme={null}
SELECT value
FROM system.s3_queue_settings
WHERE database = currentDatabase() AND table = 's3_events_queue' AND name = 'keeper_path';
```

```text theme={null}
/clickhouse/s3queue/<database-uuid>/<table-uuid>
```

The query returns the path that the table uses, also when `keeper_path` was not set in `CREATE TABLE`. In that case the path consists of the `s3queue_default_zookeeper_path` setting (`/clickhouse/s3queue/` by default), the UUID of the database and the UUID of the table. `system.s3_queue_settings` is available from ClickHouse 24.10. On older versions, run `SHOW CREATE TABLE s3_events_queue`: if it contains an explicit `keeper_path`, use that value. Otherwise, construct the default path from the `uuid` columns of `system.databases` and `system.tables`.

2. Create the new credentials in your cloud provider. Keep the old ones valid until the last step.

3. Create a second table with the new credentials and the path from step 1. Copy everything else from `SHOW CREATE TABLE s3_events_queue`:

```sql theme={null}
CREATE TABLE s3_events_queue_new
(
    id UInt64,
    value UInt64
)
ENGINE = S3Queue(
    'https://<bucket>.s3.<region>.amazonaws.com/events/*.csv',
    '<new-access-key-id>',
    '<new-secret-access-key>',
    'CSV')
SETTINGS
    mode = 'unordered',
    keeper_path = '<keeper-path-from-step-1>';
```

The new table reads nothing yet, because no materialized view selects from it.

4. Pause the table with the old credentials:

```sql theme={null}
SYSTEM PAUSE s3_events_queue;
```

The table finishes the batch of files that it is processing and starts no new one. Do not skip this step. A table that is working through a backlog does not notice that the next step takes its materialized view away: it keeps processing files, their rows are inserted nowhere, and the files are still recorded as processed, so the new table never reads them. [`SYSTEM PAUSE`](https://clickhouse.com/docs/reference/statements/system#pause-background) works for `S3Queue` tables from ClickHouse 26.8. On an earlier version, run the next step only while the queue is idle: stop the uploads to the bucket and wait until every file is ingested.

5. Swap the names of the two tables:

```sql theme={null}
EXCHANGE TABLES s3_events_queue AND s3_events_queue_new;
```

The materialized view refers to its source by name, so it now reads from the table with the new credentials, which starts to process files. The table with the old credentials is now called `s3_events_queue_new`, has no view attached and stays paused. A batch that it started before the exchange still goes through the view.

6. Drop the table with the old credentials:

```sql theme={null}
DROP TABLE s3_events_queue_new;
```

If the table is still in the middle of its last batch, `DROP TABLE` interrupts it, and the new table reads the files that the batch did not record as processed, from their beginning. The state in Keeper is kept, because another table is still registered on the path. ClickHouse removes it only when the last table that uses the path is dropped.

7. Revoke the old credentials.

To check the result, run `SHOW CREATE TABLE s3_events_queue`: it shows the new access key ID and the explicit `keeper_path`. For the next rotation, reuse the same `keeper_path` value.

## What must match

Both tables describe the same queue, so ClickHouse compares the new definition with the one stored in Keeper. The URL (endpoint, bucket and path), the format, the columns, `mode`, `after_processing`, `tracked_files_limit`, `tracked_file_ttl_sec` and, in `ordered` mode, `buckets` must be the same. The credentials are not compared. If something differs, `CREATE TABLE` fails, for example:

```text theme={null}
Code: 342. DB::Exception: Existing table metadata in ZooKeeper differs in engine mode. Stored in ZooKeeper: unordered, local: ordered. (METADATA_MISMATCH)
```

`CREATE TABLE` does not compare the other settings, but do not use the new table to change them. Some settings, such as `loading_retries` and `processing_threads_num`, belong to the queue and not to the table: two tables on the same server share them, so the new table keeps the values of the existing one, although `SHOW CREATE TABLE` shows what you wrote. `system.s3_queue_settings` shows the values in effect. Copy the settings unchanged and change them after the rotation, for example with `ALTER TABLE s3_events_queue MODIFY SETTING loading_retries = 5`.

## Things to know

* The procedure is the same for the `ordered` mode.
* `SYSTEM PAUSE` acts only on the server where you run it. If several servers have the table on the same `keeper_path`, pause it on each of them before the exchange.
* The rotation does not change which files are tracked. In the `unordered` mode, ClickHouse Keeper keeps the records of at most `tracked_files_limit` processed files (1000 by default) and, if `tracked_file_ttl_sec` is set, only for that many seconds. A file that is still in the bucket when its record is removed is read again, with or without a rotation.
* Do the exchange while the old credentials still work. Failed attempts to read a file are counted in Keeper under the shared path, and a file that fails `loading_retries` times is recorded as failed and skipped, also by the new table.
* `EXCHANGE TABLES` works only in databases whose engine supports it, such as `Atomic`. See the [`EXCHANGE` statement](https://clickhouse.com/docs/reference/statements/exchange).
* For an `S3Queue` table, `ALTER TABLE` accepts only `MODIFY SETTING` and `RESET SETTING`. Any other `ALTER` fails with `Only MODIFY/RESET SETTING alter is allowed for S3Queue`, and the credentials are not a setting.

## Related pages

* [`S3Queue` table engine](https://clickhouse.com/docs/reference/engines/table-engines/integrations/s3queue), including the [`keeper_path`](https://clickhouse.com/docs/reference/engines/table-engines/integrations/s3queue#keeper_path) setting
* [`EXCHANGE` statement](https://clickhouse.com/docs/reference/statements/exchange)
* [`system.s3_queue_settings`](https://clickhouse.com/docs/reference/system-tables/s3_queue_settings)
