Skip to content

Commit f80de8e

Browse files
committed
blog: tweak pg rollback
1 parent 53a0c08 commit f80de8e

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

content/blog/postgres-rollback.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
---
2-
title: 'PostgreSQL Rollback'
2+
title: 'Postgres Rollback Explained'
33
author: Adela
44
updated_at: 2025/09/04 18:00
55
feature_image: /content/blog/postgres-rollback/cover.webp
66
tags: Explanation
7-
description: 'An engineering perspective to evaluate PostgreSQL rollback strategies'
7+
description: 'An engineering perspective to evaluate Postgres rollback strategies'
88
---
99

10-
Database integrity and recovery mechanisms are critical for any production system. PostgreSQL provides multiple rollback strategies: built-in transaction rollback with SAVEPOINTs, Point-in-Time Recovery (PITR), and modern cross-transaction DML rollback solutions with tools. Each serves different use cases with distinct limitations.
10+
Database integrity and recovery mechanisms are critical for any production system. Postgres provides multiple rollback strategies: built-in transaction rollback with SAVEPOINT, Point-in-Time Recovery (PITR), and modern cross-transaction DML rollback solutions with tools. Each serves different use cases with distinct limitations.
1111

12-
## Built-in Transaction Rollback and SAVEPOINTs
12+
## Built-in Transaction Rollback and SAVEPOINT
1313

14-
PostgreSQL transactions allow rolling back all changes within a transaction block. For granular control, `SAVEPOINT`s create markers within transactions, enabling partial rollbacks without affecting earlier operations.
14+
Postgres transactions allow rolling back all changes within a transaction block. For granular control, `SAVEPOINT` create markers within transactions, enabling partial rollbacks without affecting earlier operations.
1515

16-
### Using SAVEPOINTs
16+
### Using SAVEPOINT
1717

1818
Create a savepoint:
19+
1920
```sql
2021
SAVEPOINT my_savepoint;
2122
```
2223

2324
Roll back to it:
25+
2426
```sql
2527
ROLLBACK TO SAVEPOINT my_savepoint;
2628
```
2729

2830
**Practical pattern for risky operations:**
31+
2932
```sql
3033
BEGIN;
3134

@@ -56,16 +59,20 @@ The savepoint remains usable after rollback, but any savepoints created after it
5659

5760
## Point-In-Time Recovery (PITR)
5861

59-
PITR restores databases to specific points in time using continuous WAL archiving. PostgreSQL's Write-Ahead Log records every database change. PITR combines base backups with archived WAL files to replay changes up to any desired moment.
62+
PITR restores databases to specific points in time using continuous WAL archiving. Postgres's Write-Ahead Log records every database change. PITR combines base backups with archived WAL files to replay changes up to any desired moment.
6063

6164
### Cloud Provider Support
65+
6266
Major cloud providers offer one-click PITR experiences:
63-
- **AWS RDS for PostgreSQL**: Restore to point in time via Console/CLI/API
64-
- **Google Cloud SQL**: PITR from console interface
65-
- **Azure Database for PostgreSQL**: Portal "Restore" to latest or chosen restore point
67+
68+
- **AWS RDS for Postgres**: Restore to point in time via Console/CLI/API
69+
- **Google Cloud SQL**: PITR from console interface
70+
- **Azure Database for Postgres**: Portal "Restore" to latest or chosen restore point
6671

6772
### Named Restore Points
73+
6874
Create targeted recovery points for easier PITR:
75+
6976
```sql
7077
-- Before risky migration
7178
SELECT pg_create_restore_point('pre_migration_2025_09_04');
@@ -74,10 +81,12 @@ SELECT pg_create_restore_point('pre_migration_2025_09_04');
7481
Later recover using `recovery_target_name = 'pre_migration_2025_09_04'` instead of guessing timestamps.
7582

7683
### Advantages
84+
7785
- Handles any rollback scenario regardless of transaction commit status
7886
- Can recover from errors discovered hours or days later
7987

8088
### Limitations
89+
8190
- Operates at cluster level - rolls back entire database, not individual tables or rows
8291
- Heavyweight operation unsuitable for small, isolated changes
8392
- Rolling back one incorrect `UPDATE` also undoes all subsequent valid changes
@@ -89,11 +98,13 @@ After a bad `UPDATE`/`DELETE`/`INSERT` is committed, you need **compensating DML
8998
### Manual Compensating DML Example
9099

91100
Accidentally ran:
101+
92102
```sql
93103
UPDATE accounts SET status = 'inactive' WHERE org_id = 42;
94104
```
95105

96106
Compensate using audit/history table:
107+
97108
```sql
98109
-- Revert to last known status per row
99110
UPDATE accounts a
@@ -113,13 +124,13 @@ Real systems must handle sequences, cascades, triggers, and side-effects.
113124

114125
### Bytebase Solution
115126

116-
Bytebase provides point-and-click rollback through [Prior Backup](https://docs.bytebase.com/change-database/rollback-data-changes) functionality:
127+
Bytebase provides [point-and-click rollback](https://docs.bytebase.com/change-database/rollback-data-changes) functionality:
117128

118-
1. **Prior Backup**: Automatically captures affected rows before DML execution
119-
2. **Change Execution**: Stores backup in dedicated `bbdataarchive` schema
120-
3. **1-Click Rollback**: Generates and executes rollback scripts automatically
129+
1. **Prior Backup**: Automatically captures affected rows before DML execution and stores in the dedicated `bbdataarchive` schema
130+
1. **1-Click Rollback**: Generates and executes rollback scripts automatically
121131

122132
### Workflow Benefits
133+
123134
- Eliminates manual rollback script creation
124135
- Integrated review and approval process
125136
- Multi-task rollback across databases
@@ -129,23 +140,12 @@ Bytebase provides point-and-click rollback through [Prior Backup](https://docs.b
129140

130141
Now that you understand the three rollback approaches, here's how to choose the right one for your situation:
131142

132-
| Situation | Best Tool | Why |
133-
|-----------|-----------|-----|
134-
| Still in session, haven't committed | **Transaction rollback / SAVEPOINT** | Instant, lossless; keep good work, discard bad chunk |
135-
| Committed a small wrong UPDATE/DELETE | **Cross-transaction rollback (Bytebase)** | Surgical fix; no cluster restore |
136-
| Dropped table / mass data corruption | **PITR** | Ubiquitous, reliable; recovers to clean time point |
137-
| Need CREATE INDEX CONCURRENTLY | **Run outside explicit BEGIN** | PostgreSQL forbids it inside transaction block |
138-
| Need CREATE DATABASE | **Run autocommit / outside BEGIN** | Not allowed in transaction block |
139-
140-
## Caveats & Gotchas
141-
142-
- **Transaction abort state**: If a statement errors mid-transaction, the session is "aborted" until you `ROLLBACK`. Don't keep issuing statements hoping it heals.
143-
- **PITR blast radius**: It's cluster-level by design; plan to restore to a new server, verify state, then cut over or copy data back.
144-
- **Compensating DML complexity**: Manual rollback scripts are error-prone and must account for cascading effects, triggers, and referential integrity.
145-
146-
147-
## TL;DR
143+
| Situation | Best Tool | Why |
144+
| ------------------------------------- | ----------------------------------------- | ---------------------------------------------------- |
145+
| Still in session, haven't committed | **Transaction rollback / SAVEPOINT** | Instant, lossless; keep good work, discard bad chunk |
146+
| Committed a small wrong UPDATE/DELETE | **Cross-transaction rollback (Bytebase)** | Surgical fix; no cluster restore |
147+
| Dropped table / mass data corruption | **PITR** | Ubiquitous, reliable; recovers to clean time point |
148148

149149
- **Use transactions + SAVEPOINT** to avoid mistakes in the first place
150-
- **Use PITR** when blast radius is unclear or damage is large - it's ubiquitous and cloud-friendly
151-
- **Use compensating DML (or Bytebase's rollback workflow)** for small, precise fixes after commit - without PITR's weight
150+
- **Use PITR** when blast radius is unclear or damage is large - it's ubiquitous and cloud-friendly
151+
- **Use compensating DML (or Bytebase's rollback workflow)** for small, precise fixes after commit - without PITR's weight

0 commit comments

Comments
 (0)