titlesidebar_labeldescription
ALTER TABLE ATTACH PARTITION
ATTACH PARTITION
ATTACH PARTITION SQL keyword reference documentation.

Attaches one or more partitions to a table. This feature is designed to support attaching partitions from a backup or copying and attaching partitions across a network between QuestDB instances.

Before running an ATTACH PARTITION query, partitions must first be copied in the format <partition_name>.detached to the storage directory of the table to which they will be attached. Given a partition named 2020, it must be copied to the destination table as 2020.detached. Details of the naming format for detached partitions along with example commands for copying and attaching partitions can be found in the examples section below.

:::caution

This operation is only supported on tables which do not contain SYMBOL types

:::

Syntax

Flow chart showing the syntax of the ALTER TABLE keyword Flow chart showing the syntax of ALTER TABLE with ATTACH PARTITION keyword

Examples

Assuming a backup directory ~/backup/ exists for a table sensor_data which was partitioned by YEAR, the following example demonstrates how to attach the 2020 partition to a table named sensor_data:

  1. cp -r ~/backup/2021-01-01/sensor_data/2020 /path/to/questdb/db/sensor_data/2020.detached

The directory structure of the sensor_data table should look like the following:

  1. db
  2. └── sensor_data
  3. ├── 2020.detached
  4. ├── device_version.d
  5. ├── driver.d
  6. └── ...
  7. └── 2021
  8. ├── device_version.d
  9. ├── driver.d
  10. └── ...

The following SQL query will attach the 2020 partition to the sensor_data table:

  1. ALTER TABLE sensor_data ATTACH PARTITION LIST '2020';

The structure of the sensor_data table will then reflect that the 2020 partition has been successfully attached:

  1. db
  2. └── sensor_data
  3. ├── 2020
  4. └── ...
  5. └── 2021
  6. └── ...

:::info

Details of creating backups can be found on the BACKUP syntax reference page.

:::

Partitions may be referred to by YEAR, MONTH or DAY:

  1. --DAY
  2. ALTER TABLE sensor_data ATTACH PARTITION LIST '2019-05-18';
  3. --MONTH
  4. ALTER TABLE sensor_data ATTACH PARTITION LIST '2019-05';
  5. --YEAR
  6. ALTER TABLE sensor_data ATTACH PARTITION LIST '2019';

Multiple partitions may be copied and attached to a table:

  1. cp -r ~/backup/2021-01-01/sensor_data/2020 /path/to/questdb/db/sensor_data/2020.detached
  2. cp -r ~/backup/2021-01-01/sensor_data/2019 /path/to/questdb/db/sensor_data/2019.detached

The partitions are attached by providing a comma-separated list of partitions:

  1. ALTER TABLE sensor_data ATTACH PARTITION LIST '2019','2020';