# COMMIT
`COMMIT` ends a transaction block and commits all changes if the transaction statements succeed.

> For the complete documentation index, see [llms.txt](https://preview.materialize.com/materialize/40269/llms.txt).


`COMMIT` ends the current [transaction](https://preview.materialize.com/materialize/40269/markdown-docs/sql/begin/index.md#details). Upon the `COMMIT`
statement:

- If all transaction statements succeed, all changes are committed.

- If an error occurs, all changes are discarded; i.e., rolled back.

## Syntax

```mzsql
COMMIT;
```

## Details

[`BEGIN`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/begin/index.md) starts a transaction block. Once a transaction is started:
- Statements within the transaction are executed sequentially.
- A transaction ends with either a [`COMMIT`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/commit/index.md) or a
  [`ROLLBACK`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/rollback/index.md) statement.
  - If all transaction statements succeed and a [`COMMIT`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/commit/index.md) is
  [issued](https://preview.materialize.com/materialize/40269/markdown-docs/sql/commit/index.md#details), all changes are saved.
  - If all transaction statements succeed and a [`ROLLBACK`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/rollback/index.md)
  is issued, all changes are discarded.
  - If an error occurs and either a [`COMMIT`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/commit/index.md) or a
  [`ROLLBACK`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/rollback/index.md) is issued, all changes are discarded.


Transactions in Materialize are **read-only** transactions, **write-only**
(more specifically, **insert-only**) transactions, or **DDL-only**
transactions.

For a [write-only (i.e., insert-only)
transaction](https://preview.materialize.com/materialize/40269/markdown-docs/sql/begin/index.md#write-only-transactions), all statements in the
transaction are committed at the same timestamp.

For a [DDL-only transaction](https://preview.materialize.com/materialize/40269/markdown-docs/sql/begin/index.md#ddl-only-transactions), all
statements in the transaction are committed at the same timestamp.

## Examples

### Commit a write-only transaction

In Materialize, write-only transactions are **insert-only** transactions.

An **insert-only** transaction block only contains [`INSERT`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/insert/index.md)
statements that insert into the **same** table.

On a successful [`COMMIT`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/commit/index.md), all statements from the
transaction are committed at the same timestamp.

```mzsql
BEGIN;
INSERT INTO orders VALUES (11,current_timestamp,'brownie',10);

-- Subsequent INSERTs must write to sales_items table only
-- Otherwise, the COMMIT will error and roll back the transaction.

INSERT INTO orders VALUES (11,current_timestamp,'chocolate cake',1);
INSERT INTO orders VALUES (11,current_timestamp,'chocolate chip cookie',20);
COMMIT;
```

If, within the transaction, a statement inserts into a table different from
that of the first statement, on [`COMMIT`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/commit/index.md), the transaction
encounters an **internal ERROR** and rolls back:

```none
ERROR:  internal error, wrong set of locks acquired
```


### Commit a read-only transaction

In Materialize, read-only transactions can be either:

- a `SELECT` only transaction that only contains [`SELECT`] statements or

- a `SUBSCRIBE`-based transactions that only contains a single[`DECLARE ...
  CURSOR FOR`] [`SUBSCRIBE`] statement followed by subsequent
  [`FETCH`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/fetch/index.md) statement(s).

For example:

```mzsql
BEGIN;
DECLARE c CURSOR FOR SUBSCRIBE (SELECT * FROM flippers);

-- Subsequent queries must only FETCH from the cursor

FETCH 10 c WITH (timeout='1s');
FETCH 20 c WITH (timeout='1s');
COMMIT;
```

During the first query, a timestamp is chosen that is valid for all of the
objects referenced in the query. This timestamp will be used for all other
queries in the transaction.

> **Note:** The transaction will additionally hold back normal compaction of the objects,
> potentially increasing memory usage for very long running transactions.


## See also

- [`BEGIN`]
- [`ROLLBACK`]

[`BEGIN`]: https://preview.materialize.com/materialize/40269/markdown-docs/sql/begin/index.md
[`ROLLBACK`]: https://preview.materialize.com/materialize/40269/markdown-docs/sql/rollback/index.md
[`COMMIT`]: https://preview.materialize.com/materialize/40269/markdown-docs/sql/commit/index.md
[`SELECT`]: https://preview.materialize.com/materialize/40269/markdown-docs/sql/select/index.md
[`SUBSCRIBE`]: https://preview.materialize.com/materialize/40269/markdown-docs/sql/subscribe/index.md
[`DECLARE ... CURSOR FOR`]: https://preview.materialize.com/materialize/40269/markdown-docs/sql/declare/index.md
[`INSERT`]: https://preview.materialize.com/materialize/40269/markdown-docs/sql/insert/index.md
