Hook: Think of COMMIT like signing the receipt at checkout: until you sign, the store can still undo the cart.
Question: What does COMMIT do in SQL?
Answer: COMMIT ends the current transaction and makes all changes in that transaction permanent. After it succeeds, the changes are visible to other sessions, and the database can release the locks that the transaction was holding. If something goes wrong before the commit, you usually use ROLLBACK instead.
Interview-Ready Answer: I use COMMIT to finalize a transaction. It tells the database, “make everything I did since BEGIN permanent and visible.” The key idea is all-or-nothing: if the work is correct, I commit; if it fails or I need to undo it, I roll back. One detail interviewers like is that committing too often can hurt performance because the database may need to flush transaction logs to durable storage.
COMMIT really meansA transaction is a group of SQL statements that should succeed or fail together. COMMIT is the final step that says, “this group is complete.” A good mental model is a shopping basket: until you pay, the items are still in the cart; after payment, the store records them as final.
BEGIN or because your client is in manual transaction mode.INSERT, UPDATE, and DELETE statements. The database tracks the changes as part of that transaction, but they are not yet final.COMMIT. The database checks that the transaction is still valid and can be completed.Use COMMIT when you want a set of related changes to become permanent together. Classic examples are money transfers, order placement, booking inventory, or saving a parent row and its child rows. If the steps depend on each other, one commit at the end protects you from half-finished data.
Do not think of commit as something you sprinkle after every statement. If five statements are really one business action, committing after each one can leave the system in an impossible intermediate state if a later statement fails.
COMMIT vs alternatives| Concept | What it does | When to use |
|---|---|---|
| COMMIT | Makes changes permanent | Work succeeded |
| ROLLBACK | Undoes changes | Work failed or must be canceled |
| Autocommit | Each statement is its own transaction | Simple one-off statements |
If your database or client is in autocommit mode, each statement is committed immediately unless you explicitly start a transaction. That is convenient for simple scripts, but it is risky for multi-step business logic because you lose the safety of grouping statements together.
Under the hood, most databases use a transaction log. A transaction log is a durable record of changes that lets the database recover after a crash. When you commit, the database ensures the log is safely written before it tells you the transaction is final. That extra safety is why commit is not free.
For a rough performance model, the logical cost of committing is O(1) per transaction, but the physical cost depends on disk flushes, replication, and lock release. On a fast SSD-backed system, a commit may take fractions of a millisecond to a few milliseconds; in a busy replicated system, it can be longer. This is why batching 100 row changes into one commit is often much faster than doing 100 separate commits. A common real-world rule of thumb is that committing every row can be 10x to 100x slower than batching, depending on storage and workload.
One more important term: MVCC means Multi-Version Concurrency Control. It is a technique where the database keeps multiple row versions so readers and writers can work at the same time. In MVCC systems like PostgreSQL, committed changes become visible to new transactions, and old versions are cleaned up later.
ROLLBACK before you can do anything else. A later COMMIT will not rescue a broken transaction.SAVEPOINT is a named checkpoint inside a transaction. You can roll back part of the work and still commit the rest. This is useful when one small step is optional.Memory rule: if the work is one business event, commit once at the end; if the work is not safe to keep, roll it back.
Imagine a checkout service for an online store. When a customer clicks “Place order,” the app must create the order row, reserve inventory, and record payment intent. Those steps belong to one transaction. The service does the work, then COMMIT makes the whole order official.
What goes wrong when someone misunderstands COMMIT? A common bug is committing too early, for example after inserting the order but before reserving stock. In that case the database can show a paid order with no inventory reserved, which causes support tickets, duplicate shipments, and angry customers. Another failure mode is forgetting to commit at all in a manually managed session: the app may show success to the user, but the data disappears when the connection closes or the transaction is rolled back.
Symptoms usually look like this: order confirmations appear in the UI, but later queries cannot find matching rows; logs show long-running sessions with messages like “idle in transaction”; and the warehouse team sees stock levels drifting because reservations never finalized. In production, this is one of the fastest ways to get a “ghost order” outage.
-- PostgreSQL-flavored SQL script showing how COMMIT finalizes work.
-- Run this whole script in a SQL client connected to PostgreSQL.
DROP TABLE IF EXISTS accounts;
CREATE TABLE accounts (
id INT PRIMARY KEY,
owner TEXT NOT NULL,
balance NUMERIC(12,2) NOT NULL CHECK (balance >= 0)
);
INSERT INTO accounts (id, owner, balance) VALUES
(1, 'Alice', 1000.00),
(2, 'Bob', 500.00);
-- A transfer is one business action: both updates should stick together.
BEGIN;
UPDATE accounts
SET balance = balance - 200.00
WHERE id = 1;
UPDATE accounts
SET balance = balance + 200.00
WHERE id = 2;
COMMIT;
SELECT 'After COMMIT' AS stage, id, owner, balance
FROM accounts
ORDER BY id;
-- Edge case: partial work can be undone before COMMIT using a savepoint.
BEGIN;
SAVEPOINT before_bonus;
UPDATE accounts
SET balance = balance + 50.00
WHERE id = 1;
-- We decide not to keep that bonus. This rolls back only the inner step.
ROLLBACK TO SAVEPOINT before_bonus;
COMMIT;
SELECT 'After rollback-to-savepoint + COMMIT' AS stage, id, owner, balance
FROM accounts
ORDER BY id;
-- Failure path: if we change our mind, ROLLBACK discards the uncommitted change.
BEGIN;
UPDATE accounts
SET balance = balance - 25.00
WHERE id = 1;
ROLLBACK;
SELECT 'After ROLLBACK' AS stage, id, owner, balance
FROM accounts
ORDER BY id;
-- Final state shows that only the committed transaction changed the data permanently.Follow-up & Tricky Questions:
COMMIT and ROLLBACK? COMMIT makes the transaction permanent; ROLLBACK discards its changes. They are the two opposite outcomes of transaction control.COMMIT always make data visible immediately? Usually yes for transactions that become committed, but visibility still depends on isolation level and whether other sessions start after the commit. Existing transactions may continue to see their own snapshot.Common Mistakes:
Memory Hook: “Commit is the cashier’s final receipt.” Until the receipt prints, the basket can still change; once it prints, the sale is final.
Cheat Sheet:
COMMIT finalizes a transaction.ROLLBACK is the undo button.Practice Tasks: