Manage transactions and concurrency

Where Should an Application Put BEGIN, COMMIT, and ROLLBACK?

See where to place PostgreSQL transaction boundaries for related updates, with a psql example that checks both commit and rollback paths.

Editorial illustration for Where Should an Application Put BEGIN, COMMIT, and ROLLBACK?

If an application must change two account balances together, put BEGIN before the first update and COMMIT only after both updates and the required business check succeed. If either update fails or the check rejects the result, use ROLLBACK for the whole unit. Otherwise, two separately committed statements can leave the operation half done.

Choose the boundary around the operation

Consider a transfer represented by two balance updates. The useful boundary is not “one transaction per SQL statement.” It is the point at which the application can either accept both changes or reject both. Without an explicit transaction block, PostgreSQL executes each statement in its own transaction and commits a successful statement. A successful debit could therefore remain even if the later credit fails. With a transaction block, other sessions cannot see the intermediate state before the related changes are complete. See the PostgreSQL BEGIN documentation.

Start the block immediately before the related changes. Finish it once the statements and the check needed to accept them have completed. Do not commit merely because the SQL statements ran: the application may still need to reject the proposed result.

Walk through both decisions in psql

The following is an illustrative PostgreSQL 18 example, not a report of an executed test. Run the setup once in a scratch database. Balances are whole units here to keep the arithmetic visible; the table intentionally does not enforce the example's nonnegative-balance rule.

CREATE TABLE accounts (
    account_id integer PRIMARY KEY,
    balance integer NOT NULL
);

INSERT INTO accounts (account_id, balance)
VALUES (1, 100), (2, 50);

Suppose the application wants to move 20 units from account 1 to account 2. In psql, run these statements, then inspect the SELECT result before choosing to commit:

BEGIN;

UPDATE accounts
SET balance = balance - 20
WHERE account_id = 1;

UPDATE accounts
SET balance = balance + 20
WHERE account_id = 2;

SELECT account_id, balance
FROM accounts
ORDER BY account_id;

Given the stated starting values, the check should show 80 for account 1 and 70 for account 2. If both updates succeeded and those values satisfy the intended operation, finish it and query again:

COMMIT;

SELECT account_id, balance
FROM accounts
ORDER BY account_id;

The post-commit query should still show 80 and 70. That is the check that both related changes were retained, rather than an assumption based on the first update alone.

Now consider a proposed transfer of 100 units from those post-commit balances. Run this as a new transaction and stop at the check:

BEGIN;

UPDATE accounts
SET balance = balance - 100
WHERE account_id = 1;

UPDATE accounts
SET balance = balance + 100
WHERE account_id = 2;

SELECT account_id, balance
FROM accounts
ORDER BY account_id;

The proposed balances are -20 and 170. If the application's rule rejects a negative balance, do not commit. End this transaction and check the stored values:

ROLLBACK;

SELECT account_id, balance
FROM accounts
ORDER BY account_id;

The final query should show the pre-transaction values, 80 and 70. ROLLBACK cancels the updates made in the open transaction; the earlier, committed transfer remains. This example makes the business decision explicit rather than pretending that the table has a constraint that performs it automatically. The PostgreSQL transaction tutorial likewise describes rolling back after noticing a negative balance.

Treat statement errors differently from a rejected check

The negative-balance check above is an application decision after successful statements. A SQL statement error is different: it can leave the transaction block aborted. If the failed statement is part of the required operation, roll back the entire unit rather than trying to commit the earlier update.

A savepoint is useful only when discarding a later, optional portion still leaves a valid operation. PostgreSQL documents ROLLBACK TO as a way to regain control after an error has aborted a transaction block, while retaining work before the savepoint. It is not a reason to keep half of a transfer that must succeed as one unit. See the PostgreSQL savepoint discussion.

Check what your client does

The psql walkthrough shows the SQL-level boundary. Before translating it into application code, check the selected client interface's documentation for whether it issues transaction commands automatically and how it starts and ends a block. PostgreSQL's transaction tutorial explicitly warns that some client libraries issue BEGIN and COMMIT automatically. The application still needs one deliberate accept-or-reject decision around the related work.

Find a note

Search by topic, title, or keyword.