Cloud

Transactions

Redpanda SQL accepts transaction syntax for compatibility with tools that require it. However, queries execute immediately, and Redpanda SQL provides no transactional guarantees.

Commands

Use the following commands to manage transactions:

BEGIN

Initiates a new transaction by calling one of the following.

BEGIN
BEGIN;
BEGIN TRANSACTION
BEGIN TRANSACTION;

COMMIT

Saves the changes made in a transaction to the database and ends the transaction.

Call one of the following.

COMMIT
COMMIT;
END TRANSACTION
END TRANSACTION;

ROLLBACK

In Redpanda SQL, when you issue a ROLLBACK command, it does not undo changes made in the current transaction. It finishes the transaction without any rollback action.

ROLLBACK;

Examples

  1. Define a table named products with columns: product_name, price, and stock_quantity.

    CREATE TABLE productsnew(
        product_name TEXT,
        price INT,
        stock_quantity INT
    );

    Upon successful creation, you get the following output.

    CREATE
  2. Next, insert product data into the products table.

  • Transactions can only contain either multiple SELECT statements or a single non-SELECT one

  • The INSERT statement is executed immediately without waiting for the transaction to finish or a COMMIT to be issued

BEGIN;
INSERT INTO productsnew(product_name, price, stock_quantity) VALUES ('Tab', 8000, 20);

This returns the following output:

BEGIN
INSERT 0 1
  1. View the changes by displaying the products table:

    SELECT * FROM productsnew;
    COMMIT;

    The product data is now added to the table.

     product_name | price | stock_quantity
    --------------+-------+----------------
     Tab          |  8000 |             20
    (1 row)
    
    COMMIT