TRUNCATE

The TRUNCATE command removes all rows from a table.

Syntax

TRUNCATE [TABLE] <table_name>

Remarks

  • For rowstore tables, TRUNCATE is the preferred method (vs DELETE).

  • This command can be run on the master aggregator or a child aggregator (refer to Node Requirements for SingleStore Commands ).

  • Table memory can be freed when the TRUNCATE command is run. For information on when/how much table memory is freed when this command is run, see Memory Management.

  • The TRUNCATE command does not support temporary tables.

  • This command causes implicit commits. Refer to COMMIT for more information.

  • Refer to the Permissions Matrix for the required permissions.

Locking Behavior

SingleStore supports online TRUNCATE, which waits for DML queries that are already running on the target table to finish before it begins truncating the table. This allows any in-progress queries to complete before the truncate operation starts. New reads and writes on the table are blocked once the TRUNCATE command is run on the table. This blocking mechanism preserves the consistency of the results. Once the ongoing read and write operations are completed, TRUNCATE acquires a database-level lock on the target table and then truncates the table.

Because TRUNCATE and ALTER TABLE share the database-level DDL operation lock, a concurrent ALTER TABLE or TRUNCATE on another table in the same database waits while the currently running operation holds the lock. This includes the period when an ongoing TRUNCATE is waiting for DML on its target table.

TRUNCATE uses the same distributed DDL machinery as ALTER TABLE. On a given database, only one ALTER TABLE or TRUNCATE operation can be in the two-phase commit (2PC) or commit path at a time.

Additionally, concurrent ALTER TABLE and TRUNCATE operations are not guaranteed to complete in the order in which they were issued. Applications must not rely on FIFO ordering among ALTER TABLE and TRUNCATE statements waiting for the database-level DDL operation lock.

For online ALTER TABLE, data-population that occurs after the 2PC commit can overlap with other operations after the database-level commit lock is released. TRUNCATE does not have a long-running data-population phase.

Delays and Timeout

If TRUNCATE statements are frequently run on a table that has a lot of long-running queries, then the workload may experience some additional delay because the TRUNCATE operation blocks other queries from starting while it waits for completion of long-running queries.

The TRUNCATE command throws a timeout error if there are long running DML queries in your workload which extend the blocking period to several minutes.

Refer to Query Errors for resolving query timeout errors due to long running queries in a workload.

Example

TRUNCATE TABLE testTable;

Last modified:

Was this article helpful?

Verification instructions

Note: You must install cosign to verify the authenticity of the SingleStore file.

Use the following steps to verify the authenticity of singlestoredb-server, singlestoredb-toolbox, singlestoredb-studio, and singlestore-client SingleStore files that have been downloaded.

You may perform the following steps on any computer that can run cosign, such as the main deployment host of the cluster.

  1. (Optional) Run the following command to view the associated signature files.

    curl undefined
  2. Download the signature file from the SingleStore release server.

    • Option 1: Click the Download Signature button next to the SingleStore file.

    • Option 2: Copy and paste the following URL into the address bar of your browser and save the signature file.

    • Option 3: Run the following command to download the signature file.

      curl -O undefined
  3. After the signature file has been downloaded, run the following command to verify the authenticity of the SingleStore file.

    echo -n undefined |
    cosign verify-blob --certificate-oidc-issuer https://oidc.eks.us-east-1.amazonaws.com/id/CCDCDBA1379A5596AB5B2E46DCA385BC \
    --certificate-identity https://kubernetes.io/namespaces/freya-production/serviceaccounts/job-worker \
    --bundle undefined \
    --new-bundle-format -
    Verified OK

Try Out This Notebook to See What’s Possible in SingleStore

Get access to other groundbreaking datasets and engage with our community for expert advice.