PostgreSQL

Transactions, Commit, Rollback, and Savepoints

Group dependent statements into PostgreSQL transactions, choose commit or rollback deliberately, and use savepoints to recover part of a larger unit of work.

Intermediate14 min read
PostgreSQL lessonRelational data foundationsLearn

Group dependent statements into PostgreSQL transactions, choose commit or rollback deliberately, and use savepoints to recover part of a larger unit of work.

What you will be able to do

  • Distinguish transaction start from dependent changes in a realistic transactions, commit, rollback, and savepoints case.
  • Interpret the database evidence and boundary associated with commit decision.
  • Choose an appropriate action involving rollback decision without exceeding the named data scope.
  • Verify savepoint boundary through an observable database result and reproducible handoff.

01

Frame Transactions, Commit, Rollback, and Savepoints

Group dependent statements into PostgreSQL transactions, choose commit or rollback deliberately, and use savepoints to recover part of a larger unit of work.

A transfer moves training credits between two student accounts and records an audit row. Partial completion would leave the practice ledger inconsistent.

Keep the database target, stored state, input values, expected result, and allowed change scope separate. Begin in an isolated practice database, inspect before modifying, and preserve enough evidence to repeat the decision.

02

Transaction start

BEGIN starts an explicit transaction block for related statements. Within transactions, commit, rollback, and savepoints, this concept answers a separate data question and retains its own observable evidence.

Begin before changing either student balance or the audit table. Apply that action to the named database case before broadening the query or changing more stored state.

Respect this boundary: do not leave an interactive transaction open without a plan. The required result is specific: all transfer statements share one transaction boundary.

03

Dependent changes

Related writes should succeed or fail as one logical unit. Within transactions, commit, rollback, and savepoints, this concept answers a separate data question and retains its own observable evidence.

Update both accounts and insert the transfer record before deciding completion. Apply that action to the named database case before broadening the query or changing more stored state.

Respect this boundary: do not commit after only one side of the transfer. The required result is specific: the pending state contains a balanced transfer and audit row.

04

Commit decision

COMMIT makes the transaction's successful changes visible and durable. Within transactions, commit, rollback, and savepoints, this concept answers a separate data question and retains its own observable evidence.

Verify balances, constraints, and audit content before committing. Apply that action to the named database case before broadening the query or changing more stored state.

Respect this boundary: do not commit while any dependent result is uncertain. The required result is specific: a new session observes the complete approved transfer.

05

Rollback decision

ROLLBACK discards changes made by the current transaction. Within transactions, commit, rollback, and savepoints, this concept answers a separate data question and retains its own observable evidence.

Roll back when a balance or audit check fails. Apply that action to the named database case before broadening the query or changing more stored state.

Respect this boundary: do not expect rollback to undo separate already committed work. The required result is specific: both balances and the audit table return to their prior state.

06

Savepoint boundary

A savepoint permits rollback of later transaction work without ending the transaction. Within transactions, commit, rollback, and savepoints, this concept answers a separate data question and retains its own observable evidence.

Set a savepoint before an optional note or secondary update. Apply that action to the named database case before broadening the query or changing more stored state.

Respect this boundary: do not use savepoints to excuse an unclear atomic boundary. The required result is specific: a failed optional step can be reversed while core work remains pending.

07

Apply Transactions, Commit, Rollback, and Savepoints to One Case

Use the case as a bounded database task: A transfer moves training credits between two student accounts and records an audit row. Partial completion would leave the practice ledger inconsistent.

First, begin before changing either student balance or the audit table. Then, update both accounts and insert the transfer record before deciding completion. Keep both observations with the target database and expected result before choosing the next statement.

Next, verify balances, constraints, and audit content before committing. After that, roll back when a balance or audit check fails. Finish only after you set a savepoint before an optional note or secondary update.

08

Recap Before Practice and Prove

Transaction start: BEGIN starts an explicit transaction block for related statements. In the database case, begin before changing either student balance or the audit table. Preserve the boundary: do not leave an interactive transaction open without a plan.

Dependent changes: Related writes should succeed or fail as one logical unit. In the database case, update both accounts and insert the transfer record before deciding completion. Preserve the boundary: do not commit after only one side of the transfer.

Commit decision: COMMIT makes the transaction's successful changes visible and durable. In the database case, verify balances, constraints, and audit content before committing. Preserve the boundary: do not commit while any dependent result is uncertain.

Rollback decision: ROLLBACK discards changes made by the current transaction. In the database case, roll back when a balance or audit check fails. Preserve the boundary: do not expect rollback to undo separate already committed work.

Savepoint boundary: A savepoint permits rollback of later transaction work without ending the transaction. In the database case, set a savepoint before an optional note or secondary update. Preserve the boundary: do not use savepoints to excuse an unclear atomic boundary.

NEXT STEP

Turn reading into recall

Practice the concepts without a timer, with coaching and retry available after every answer.

Open guided practice