top of page

Undoing a Bad UPDATE Without a Restore: Flashback Query, Version Query and Flashback Table on Exadata in Oracle Database@Google Cloud

OmerNarman
2 days ago
8 min read

Every DBA gets this call sooner or later. A release script ran an UPDATE with a WHERE clause that was too wide, the application kept running, and half an hour later the business notices that thousands of orders show the wrong status. The first idea is often a point-in-time restore of the whole database, which takes hours and throws away every good transaction since the mistake.


In most cases there is a faster way. The undo data that Oracle keeps for read consistency also holds the old values of the damaged rows, and Flashback Query can read them. This post shows how to use it on Exadata Database Service in Oracle Database@Google Cloud: check how far back undo reaches, save the old data, find the bad transaction, and repair only the rows it damaged, while the application keeps running.



The Use Case

A release script ran at 10:12 UTC. It should have cancelled a few hundred old EU orders, but its WHERE clause lost the date condition, so it set every EU order to CANCELLED. The same script was meant to delete expired promotion codes and deleted all of them instead. Both statements ran in one transaction with a single COMMIT. The problem was reported at 10:41, and the order service in Google Kubernetes Engine has been taking orders and updating them all the time.


Environment used in this runbook


  • Service: Exadata Database Service on Dedicated Infrastructure in Oracle Database@Google Cloud

  • Region: europe-west3 (Frankfurt)

  • Database: Oracle Database 19c (19.24), PDB SALESPDB, two-node RAC

  • Undo: local undo, one undo tablespace per instance, datafiles with autoextend

  • Time zone: database servers run in UTC, so the timestamps below are UTC

  • Licence: Flashback Query and Flashback Version Query are available in all editions; Flashback Table needs Enterprise Edition, which Exadata Database Service includes



How Flashback Query Works

  • Undo holds old values. Every change writes the previous value of the row to undo, so other sessions can still read consistent data.

  • AS OF reads the past. A query with AS OF TIMESTAMP or AS OF SCN rebuilds the rows as they were at that moment, from current blocks plus undo.

  • VERSIONS shows the history. A query with VERSIONS BETWEEN returns each committed version of a row, with the transaction ID and the time of the change.

  • The window is limited. Old undo is overwritten when space is needed. If the undo for your point in time is gone, the query fails with ORA-01555 or ORA-08180, and only a restore can help.



Step 1: Check How Far Back Undo Reaches

Before anything else, find out whether undo still covers the time of the mistake. The tuned undo retention is the number of seconds of undo that the database has actually been able to keep.


SQL

-- Connected to SALESPDB as a DBA

COLUMN tablespace_name FORMAT A15

COLUMN retention FORMAT A11

SELECT tablespace_name, retention

FROM dba_tablespaces

WHERE contents = 'UNDO';

SELECT inst_id, MIN(tuned_undoretention) AS tuned_secs

FROM gv$undostat

WHERE begin_time > SYSDATE - 1/24

GROUP BY inst_id

ORDER BY inst_id;

Expected output

TABLESPACE_NAME RETENTION
--------------- -----------
UNDOTBS1        NOGUARANTEE
UNDOTBS2        NOGUARANTEE

   INST_ID TUNED_SECS
---------- ----------
         1      14210
         2      13876

What to expect: Both instances have kept close to four hours of undo during the last hour, and the mistake happened about 30 minutes ago, so the old values are still there. NOGUARANTEE means that undo can still be overwritten under space pressure, so do not wait: the next step saves the data you need.



Step 2: Save the Before-Image First

Copy the affected rows as they were just before the script ran into a new table. This takes a minute and protects you if undo is overwritten while you investigate.


SQL

CREATE TABLE sales.orders_before_1012 AS

SELECT * FROM sales.orders AS OF TIMESTAMP TIMESTAMP '2026-12-03 10:11:00'

WHERE region = 'EU';

CREATE TABLE sales.promo_codes_before_1012 AS

SELECT * FROM sales.promo_codes AS OF TIMESTAMP TIMESTAMP '2026-12-03 10:11:00';

Expected output

Table created.

Table created.

What this does: The copies hold the EU orders and all promotion codes as of 10:11, one minute before the script. They are a safety net, not the final source for the repair: a few orders may have changed legitimately between 10:11 and 10:12, so the repair below reads the exact moment before the bad transaction instead.


Tip: Ask for a short hold on the release pipeline and on any further fix scripts until the repair is done. A second, well-meant script that runs on the damaged rows makes the analysis much harder.


Step 3: Find the Transaction That Did It

Flashback Version Query lists every committed change in a time range with its transaction ID. Normal application transactions change a few rows each, so the release script stands out by its size.


SQL

COLUMN xid FORMAT A16

COLUMN op FORMAT A2

COLUMN started FORMAT A8

SELECT RAWTOHEX(versions_xid) AS xid, versions_operation AS op,

TO_CHAR(MIN(versions_starttime), 'HH24:MI:SS') AS started,

COUNT(*) AS row_versions

FROM sales.orders

VERSIONS BETWEEN TIMESTAMP TIMESTAMP '2026-12-03 10:05:00' AND SYSTIMESTAMP

WHERE versions_xid IS NOT NULL

AND region = 'EU'

GROUP BY versions_xid, versions_operation

HAVING COUNT(*) > 1000

ORDER BY row_versions DESC;

Expected output

XID              OP STARTED  ROW_VERSIONS
---------------- -- -------- ------------
0A001F00B81C0000 U  10:12:07        41862

What to expect: One transaction updated 41,862 EU orders at 10:12:07. No application transaction comes close to that size, so this is the release script. Keep the transaction ID: it is the key for the next steps.



Step 4: Get the Exact SCN of the Mistake

Timestamps are mapped to SCNs with a precision of a few seconds. For the repair, use the SCN at which the bad transaction committed, and read the data at the SCN just before it.


SQL

SELECT MIN(versions_startscn) AS bad_scn

FROM sales.orders

VERSIONS BETWEEN TIMESTAMP TIMESTAMP '2026-12-03 10:05:00' AND SYSTIMESTAMP

WHERE versions_xid = HEXTORAW('0A001F00B81C0000');

Expected output

   BAD_SCN
----------
 918273645

What to expect: The transaction committed at SCN 918273645. Reading the tables AS OF SCN 918273644 shows them exactly as they were one change before the commit, with every earlier application transaction included and the bad one excluded.



Step 5: See Which Damaged Rows Changed Again

The application kept working after the mistake. Some orders that the script cancelled may have been changed again since, and those must not be overwritten blindly. Compare the state before the mistake with the current state.


SQL

COLUMN status_now FORMAT A10

SELECT o.status AS status_now, COUNT(*) AS orders

FROM sales.orders o

JOIN sales.orders AS OF SCN 918273644 b

ON b.order_id = o.order_id

WHERE b.region = 'EU'

AND b.status <> o.status

GROUP BY o.status

ORDER BY orders DESC;

Expected output

STATUS_NOW     ORDERS
---------- ----------
CANCELLED       41814
SHIPPED            31

What to expect: 41,814 orders are still CANCELLED and differ from their old status: these are the damaged rows. Another 31 orders have moved on to SHIPPED since the mistake, because customer service reopened and shipped them. Leave those 31 out of the automatic repair and give the list to the business to review.



Step 6: Repair Only the Damaged Rows

Write the old status back for the rows that the script cancelled and nobody has touched since. MERGE with a Flashback Query as its source does this in one statement, online, while the application keeps running.


SQL

MERGE INTO sales.orders o

USING (SELECT order_id, status

FROM sales.orders AS OF SCN 918273644

WHERE region = 'EU') b

ON (o.order_id = b.order_id)

WHEN MATCHED THEN UPDATE SET o.status = b.status

WHERE o.status = 'CANCELLED'

AND b.status <> 'CANCELLED';

COMMIT;

Expected output

41814 rows merged.

Commit complete.

What this does: It restores the previous status of exactly the 41,814 damaged orders, the same number that Step 5 found. Rows that were already cancelled before the script, and rows that changed after it, are not touched. This is a normal DML statement, so row triggers on ORDERS fire and the change is written to redo, which also repairs the Data Guard standby automatically.


Tip: If the table has a column such as LAST_UPDATED_BY, add it to the WHERE clause of the MERGE. It protects the rare order that a customer cancelled legitimately after 10:12, which would otherwise look the same as a damaged row.


Step 7: Flash Back the Table Nobody Else Touched

The promotion codes are different: only the release script changes that table. When no other transaction has touched a table since the mistake, Flashback Table puts the whole table back in one command. First confirm that the script was the only transaction.


SQL

SELECT COUNT(DISTINCT versions_xid) AS transactions

FROM sales.promo_codes

VERSIONS BETWEEN SCN 918273644 AND MAXVALUE

WHERE versions_xid IS NOT NULL;

ALTER TABLE sales.promo_codes ENABLE ROW MOVEMENT;

FLASHBACK TABLE sales.promo_codes TO SCN 918273644;

SELECT COUNT(*) AS promo_codes FROM sales.promo_codes;

Expected output

TRANSACTIONS
------------
           1

Table altered.

Flashback complete.

PROMO_CODES
-----------
       1284

What this does: Exactly one transaction, the release script, changed the table, so flashing it back loses nothing. Flashback Table needs row movement because the restored rows can get new rowids. It disables the table's triggers for the duration of the operation by default, and it needs the FLASHBACK privilege on the table, or FLASHBACK ANY TABLE, together with SELECT, INSERT, DELETE and ALTER.



Step 8: Verify the Result

Check the status distribution of the EU orders, and compare the promotion codes with the safety copy from Step 2.


SQL

COLUMN status FORMAT A10

SELECT status, COUNT(*) AS orders

FROM sales.orders

WHERE region = 'EU'

GROUP BY status

ORDER BY status;

SELECT COUNT(*) AS missing_codes

FROM (SELECT code FROM sales.promo_codes_before_1012

MINUS

SELECT code FROM sales.promo_codes);

Expected output

STATUS         ORDERS
---------- ----------
CANCELLED          23
NEW              2381
PAID             6204
SHIPPED         33187

MISSING_CODES
-------------
            0

What to expect: Only 23 EU orders are cancelled, the 17 that were cancelled before the release plus a few cancelled by customers since, and no promotion code is missing compared with the copy. The cancellation the release actually intended has not happened yet: run the corrected script once the repair is confirmed. When the business has confirmed the result and has reviewed the 31 shipped orders, drop the two copies so they do not stay behind as unmanaged data.



Step 9: Keep a Longer Window for Next Time

This repair worked because the mistake was noticed within the undo window. Give the database a clear target for how long to keep undo, sized for the time a problem usually takes to be noticed.


SQL

-- Connected to CDB$ROOT as SYSDBA

ALTER SYSTEM SET undo_retention = 21600 SCOPE = BOTH SID = '*';

COLUMN name FORMAT A20

COLUMN value FORMAT A10

SELECT inst_id, name, value

FROM gv$parameter

WHERE name = 'undo_retention'

ORDER BY inst_id;

Expected output

System altered.

   INST_ID NAME                 VALUE
---------- -------------------- ----------
         1 undo_retention       21600
         2 undo_retention       21600

What this does: The database now aims to keep six hours of undo. With autoextending undo datafiles it grows them when needed to reach that target; with fixed-size datafiles the real window still depends on the space available, so watch TUNED_UNDORETENTION after busy days. Avoid RETENTION GUARANTEE on a busy OLTP database, because DML fails when the undo tablespace is full. For tables that need history over days or months, use Flashback Time Travel (Flashback Data Archive), whose basic form is included in all editions.


Tip: Make every release script record the current SCN before it changes data, with SELECT current_scn FROM v$database. If something goes wrong, you start at Step 4 instead of searching for the transaction.


Summary

A wrong UPDATE or DELETE does not need a database restore when it is caught within the undo window. Check the tuned undo retention first, save the before-image into a copy table, and find the guilty transaction with Flashback Version Query. Read its commit SCN, compare the data before the mistake with the current data, and repair only the damaged rows with a MERGE from a Flashback Query, leaving rows that changed since for the business to review. Flash back whole tables only when nobody else has changed them, verify against the copies, and set an undo retention that matches how long mistakes take to be noticed.

Recent Posts

See All

Comments


bottom of page