Skip to main content

Replace Tables Atomically

VeloDB Cloud supports atomic replacement between two OLAP tables. A single ALTER TABLE statement performs a table-name swap or a full-table replacement, so newly planned queries resolve either the old or the replacement table. Load and validate the new data before switching names.

Choose a replacement strategy​

When a workload needs to rewrite all data in a table without exposing an intermediate "empty data" state, use atomic table replacement.

ScenarioRecommended approach
Full-table rewrite (such as offline full backfill)Use CREATE TABLE LIKE to create a new table, load the new data, and atomically replace the old table
Replace only a specific partitionSee Temporary Partition
Keep the old table for rollbackUse swap = true. The old table data is moved under the new table name
No need to keep the old table after replacementUse swap = false. The old table leaves the active catalog

Replace a table​

Take "replacing the old table tbl1 with new data" as an example. The standard procedure is:

  1. Create a new table based on the schema of the old table:

    CREATE TABLE tbl2 LIKE tbl1;
  2. Load the new data into the new table tbl2.

  3. Run the atomic replacement:

    ALTER TABLE tbl1 REPLACE WITH TABLE tbl2
    PROPERTIES('swap' = 'true');
  4. Confirm the result based on the swap parameter (see below for details).

Write the replacement statement​

ALTER TABLE [db.]tbl1 REPLACE WITH TABLE tbl2
[PROPERTIES('swap' = 'true')] [FORCE];

Meaning: replace table tbl1 with table tbl2. The swap parameter controls how the old table is handled after replacement.

Choose how to retain the old table​

swap valueData of tbl1 after replacementState of tbl2 after replacementTypical use
true (default)The original data of tbl2Retained, and contains the original data of tbl1 (the two tables are swapped)Keep the old data for rollback or comparison
falseThe original data of tbl2The name tbl2 is removed. The old tbl1 is recycled unless FORCE is usedDiscard the old active table

Understand the metadata switch​

Atomic replacement merges several DDL steps into one atomic operation. The catalog exposes the name change as one metadata switch.

  • When swap = true:

    1. Rename table B to table A.
    2. Rename table A to table B.
  • When swap = false:

    1. Drop table A.
    2. Rename table B to table A.

Because these steps run as an atomic transaction at the metadata layer, external queries do not observe any intermediate state.

Check replacement constraints​

  • Replacement is supported only between two OLAP tables. Other table types are not supported.
  • The system does not validate that the two tables have identical schemas. You must ensure schema compatibility yourself.
  • When swap = false, the old table is recycled unless FORCE is specified. Recovery depends on retention and permissions. Recover it under a different name to avoid colliding with the replacement table. Use swap = true when you need an immediate rollback path.
  • Check that the account has the required privileges for the replacement. Existing grants on a table name continue to apply to that name. Recheck access and dependent queries after replacement.

Resolve common questions​

Coordinate running queries

Replacement switches metadata atomically, so new queries do not see an intermediate missing-table state. Coordinate concurrent writes and validate long-running workloads before production replacement. Atomic metadata replacement alone is not a guarantee that every in-flight query succeeds.

Validate compatible schemas

Yes. The system does not validate schema consistency, but you should keep schemas compatible. Otherwise, downstream queries and applications may fail because of column-name or type changes.

Retain a recovery path

With swap = true, the original data remains under tbl2, and you can swap the tables again. With swap = false, the old table is eligible for recycle-bin recovery unless FORCE was used. See RECOVER.

Replace individual partitions

Yes. Use the Temporary Partition feature for partition-level atomic overwrite.

  • Temporary Partition: partition-level atomic overwrite
  • CREATE TABLE LIKE: quickly create a new table based on an existing table schema

Ask AI