The flashback table feature periodically records data snapshots to flashback logs and saves transaction information to the fast recovery area. You can use the flashback table feature to restore data at a point in time to a new table. If an accidental operation causes a data error, you can use flashback logs to quickly identify the error time and then restore the table data to a point before the error time.
Prerequisites
Before you can use the flashback table feature, enable both of the following parameters in the Parameters page of your PolarDB cluster:
| Parameter | Set to |
polar_enable_flashback_log | ON |
polar_enable_fast_recovery_area | ON |
These parameters enable flashback logs and the fast recovery area, respectively. Both are required for the flashback table feature to function.
Limitations
Supported table types
The flashback table feature supports only ordinary (regular) tables. The following database objects cannot be restored:
- Indexes
- TOAST tables
- Materialized views
- Partitioned tables
- Partitions of a partitioned table (child tables)
- System tables
- External tables
- Tables that contain child TOAST tables
DDL restrictions
You cannot flash back a table if any of the following DDL operations were performed on it between the target timestamp and the current time:
DROP TABLEALTER TABLE SET WITH OIDSALTER TABLE SET WITHOUT OIDSTRUNCATE TABLE- Modifying a column type where the source and target types cannot be implicitly converted, unless a
USINGclause is specified for explicit type conversion. - Setting the table to
UNLOGGEDorLOGGED. - Adding an identity column.
- Adding a column with a restricted data type.
- Adding a column whose default value expression contains volatile functions.
Parameters
PolarDB provides a set of parameters for fine-grained control over flashback table behavior. Modify all required parameters at the same time and restart the cluster during off-peak hours.
Core parameters
These parameters control the fundamental behavior of the flashback table feature.
| Parameter | Description | Valid values | Default | Unit |
polar_enable_flashback_log | Enables or disables flashback logs. The flashback table feature requires this parameter to be set to ON. | ON, OFF | -- | -- |
polar_enable_fast_recovery_area | Enables or disables the fast recovery area. The flashback table feature requires this parameter to be set to ON. | ON, OFF | -- | -- |
polar_fast_recovery_area_rotation | The retention period for transaction information in the fast recovery area. A larger value increases disk space consumption. | 1 -- 14400 | 180 | Minutes |
polar_flashback_log_keep_segments | The number of flashback log files to retain. Flashback log files are reusable, and each file is 256 MB. | 3 -- 2147483647 | 8 | Segments |
Flashback point parameters
These parameters control how frequently PolarDB creates flashback points in time, which determine the granularity of available restore targets.
| Parameter | Description | Valid values | Default | Unit |
polar_flashback_point_segments | The minimum number of WAL between two consecutive flashback points in time. Each WAL segment is 1 GB. We recommend setting this to a multiple of the max_wal_size value. | 1 -- 2147483647 | 16 | Segments |
polar_flashback_point_timeout | The minimum time interval between two consecutive flashback points in time. We recommend setting this to a multiple of the checkpoint_timeout value. | 1 -- 86400 | 300 | Seconds |
Memory parameters
These parameters configure the shared memory allocation for flashback log structures.
| Parameter | Description | Valid values | Default | Unit |
polar_flashback_log_buffers | The shared memory size allocated to flashback log buffers. | 4 -- 262144 | 2048 | KB |
polar_flashback_logindex_mem_size | The shared memory size allocated to flashback log indexes. | 3 -- 1073741823 | 64 | MB |
polar_flashback_logindex_bloom_blocks | The number of Bloom filter pages for flashback log indexes. Each data page is 8 KB. | 8 -- 1073741823 | 512 | Blocks |
Performance parameters
These parameters tune the parallelism and background process behavior for flashback operations.
| Parameter | Description | Valid values | Default | Unit |
polar_workers_per_flashback_table | The number of parallel workers for a flashback table operation. Set to 0 to disable parallel flashback. Increase this value for large tables to reduce flashback time. | 0 -- 1024 | 5 | Workers |
polar_flashback_log_insert_locks | The number of locks used for concurrent flashback log insertion. | 1 -- 2147483647 | 8 | Locks |
polar_flashback_log_bgwrite_delay | The interval at which the bgwriter process writes flashback logs to disk. | 1 -- 10000 | 100 | ms |
polar_flashback_log_flush_max_size | The maximum log size that the bgwriter process can flush to disk in a single cycle. Set to 0 for unlimited. | 0 -- 2097152 | 5120 | KB |
polar_flashback_log_insert_list_delay | The interval at which the bginserter process (a PolarDB-specific background process that manages flashback log insertion) operates. | 1 -- 10000 | 10 | ms |
Syntax
FLASHBACK TABLE [ schema. ]table TO TIMESTAMP expr;| Parameter | Description |
[ schema. ]table | The name of the table to flash back. Optionally specify the schema. |
expr | A timestamp expression representing the point in time to restore the table to. |
Example
The following example demonstrates a complete flashback table workflow: creating a table, inserting data, verifying the baseline, deleting data, performing a flashback, and verifying the restored data.
Step 1: Create a table and insert data
Create a test table and populate it with 10,000 rows.
CREATE TABLE test(id int);
INSERT INTO test select * FROM generate_series(1, 10000);Step 2: Verify the baseline data
Query the row count and sum to establish a baseline.
SELECT count(1) FROM test; count
-------
10000
(1 row)SELECT sum(id) FROM test; sum
----------
50005000
(1 row)Step 3: Simulate an accidental deletion
Wait 10 seconds and then delete all rows from the table.
SELECT pg_sleep(10);
DELETE FROM test;Verify that the table is now empty.
SELECT * FROM test; id
----
(0 rows)Step 4: Perform the flashback
Restore the table data to its state 10 seconds ago.
FLASHBACK TABLE test TO TIMESTAMP now() - interval'10s';PolarDB returns a NOTICE message indicating the name of the new table:
NOTICE: Flashback the relation test to new relation polar_flashback_65566, please check the data
FLASHBACK TABLEStep 5: Verify the restored data
Query the new table to confirm that the data has been restored correctly.
SELECT count(1) FROM polar_flashback_65566; count
-------
10000
(1 row)SELECT sum(id) FROM polar_flashback_65566; sum
----------
50005000
(1 row)The row count and sum match the baseline values from Step 2, confirming that the data has been fully restored.
After the flashback operation completes, you can query the data of the new table and compare it with the original table based on the NOTICE information. The missing data can be inserted back into the original table.
Usage notes
- Resource overhead: Enabling flashback logs and the fast recovery area increases shared memory and disk space consumption and causes some performance overhead. Assess these impacts before enabling the feature in production.
- Performance jitter during flashback: When a flashback table operation runs, table-related pages are swapped in and out of the shared memory pool. This may cause jitter in the access performance of other databases. We recommend performing flashback operations and restarting the cluster during off-peak hours.
- Parameter changes: We recommend modifying all required parameters at one time and restarting the cluster during off-peak hours.