Flashback Table
This topic describes the Flashback Table feature of PolarDB for PostgreSQL.
Scope of application
Flashback Table is supported in the following editions of PolarDB for PostgreSQL: PostgreSQL 11 with a minor engine version of 2.0.11.9.22.0 or later.
You can view the minor engine version in the console or by running the SHOW polardb_version; statement. If the minor engine version does not meet the requirements, upgrade the minor engine version。
Overview
Flashback Table
The Flashback Table feature periodically retains data page snapshots in flashback logs and transaction information in the fast recovery area. This allows you to restore table data as of a specific point in time to a new table.
Syntax
FLASHBACK TABLE
[ schema. ]table
TO TIMESTAMP expr;Parameters:
Parameter | Description |
[ schema. ]table | The name of the table to be flashed back. |
expr | The point in time to which the table data is to be flashed back. |
Examples
Prepare test data.
Create a table named
testand insert data.CREATE TABLE test(id int); INSERT INTO test select * FROM generate_series(1, 10000);Query the total number of rows in the
testtable.SELECT count(1) FROM test;The following result is returned:
count ------- 10000 (1 row)Calculate the sum of the
idvalues.SELECT sum(id) FROM test;The following result is returned:
sum ---------- 50005000 (1 row)
Wait for 10 seconds and delete data from the
testtable.SELECT pg_sleep(10); DELETE FROM test;Query the
testtable after deletion.SELECT * FROM test;The following result is returned:
id ---- (0 rows)Flash back the
testtable to 10 seconds ago.FLASHBACK TABLE test TO TIMESTAMP now() - interval'10s';The following result is returned:
NOTICE: Flashback the relation test to new relation polar_flashback_65566, please check the data FLASHBACK TABLEQuery data in the flashed-back table.
Query the total number of rows in the flashed-back table.
SELECT count(1) FROM polar_flashback_65566;The following result is returned:
count ------- 10000 (1 row)Calculate the sum of the
idvalues in the flashed-back table.SELECT sum(id) FROM polar_flashback_65566;The following result is returned:
sum ---------- 50005000 (1 row)
Usage guidelines
The Flashback Table feature depends on flashback logs and the fast recovery area. You must set the polar_enable_flashback_log and polar_enable_fast_recovery_area parameters and then restart the cluster. Other related parameters should also be adjusted as needed. We recommend that you modify all relevant parameters at once and restart the cluster during off-peak hours. Enabling Flashback Table increases memory and disk usage and causes some performance overhead. Evaluate the impact carefully before enabling this feature.
Memory usage
Enabling flashback logs requires additional shared memory equal to the sum of the following three items:
polar_flashback_log_buffers× 8 KBpolar_flashback_logindex_mem_sizeMBpolar_flashback_logindex_queue_buffersMB
Enabling the fast recovery area requires approximately 32 KB of additional shared memory. Evaluate the current cluster status before adjusting these parameters.
Disk usage
To flash back to a specific point in time, flashback logs, WAL logs, and their LogIndex files within that period must be retained. This increases disk usage. In general, the larger the value of polar_fast_recovery_area_rotation, the more disk space is consumed. For example, if polar_fast_recovery_area_rotation is set to 300, historical data of the last 5 hours is retained.
After flashback logs are enabled, flashback points are created periodically. A flashback point is a special type of checkpoint. When a checkpoint is triggered, the polar_flashback_point_segments and polar_flashback_point_timeout parameters are evaluated to determine whether the current checkpoint is a flashback point. We recommend that you configure these parameters as follows:
Set
polar_flashback_point_segmentsto a multiple ofmax_wal_size.Set
polar_flashback_point_timeoutto a multiple ofcheckpoint_timeout.
For example, if 20 GB of WAL logs are generated in 5 hours and the ratio of flashback logs to WAL logs is approximately 1:20, approximately 1 GB of flashback logs are generated. The ratio of flashback logs to WAL logs depends on the following two factors:
Workload pattern: the more write-intensive the workload, the more flashback logs are generated.
The larger the values of
polar_flashback_point_segmentsandpolar_flashback_point_timeout, the fewer flashback logs are generated.
Performance impact
The flashback log feature introduces two background processes that consume flashback logs, which increases CPU overhead. You can adjust the polar_flashback_log_bgwrite_delay and polar_flashback_log_insert_list_delay parameters to extend the interval between these background processes and reduce CPU consumption. However, this may cause some performance degradation. We recommend that you use the default values.
The flashback log feature must flush the corresponding flashback logs before dirty pages are flushed to disk to prevent flashback log loss. This may cause some performance degradation. In most scenarios, the performance degradation does not exceed 5%.
During a table flashback, the pages involved in the target table are swapped in and out of the shared memory pool. This may cause performance fluctuations in other database operations.
Usage limits
The Flashback Table feature restores the data of a target table to a new table named polar_flashback_<target table OID>. After you execute the FLASHBACK TABLE statement, the following NOTICE message is displayed:
flashback table test to timestamp now() - interval '1h'; NOTICE: Flashback the relation test to new relation polar_flashback_54986, please check the data FLASHBACK TABLEIn this example,
polar_flashback_54986is a temporary table generated by the flashback operation. Only the table data as of the target point in time is restored.Flashback Table supports only regular tables. The following database objects cannot be flashed back:
Indexes
TOAST tables
Materialized views
Partitioned tables
Table partitions
System tables
Foreign tables
Tables with TOAST child tables
A table cannot be flashed back if any of the following DDL operations have been performed on the table between the target point in time and the current time:
DROP TABLEALTER TABLE SET WITH OIDSALTER TABLE SET WITHOUT OIDSTRUNCATE TABLEColumn type modifications where the original and new types cannot be implicitly converted and no safe explicit cast is provided by using the
USINGclause.Changing the table to
UNLOGGEDorLOGGED.Adding an
IDENTITYcolumn.Adding a column whose type has constraints.
Adding a column whose default value expression contains volatile functions.
NoteIf
DROP TABLEhas been executed, you can use the Flashback Drop feature of PolarDB for PostgreSQL to restore the table.
Usage suggestions
If data is accidentally modified, we recommend that you use audit logs to locate the time when the accidental operation occurred and then flash back the target table to a point in time before that operation. During the table flashback, an exclusive lock is held on the target table, so only query operations can be performed on the target table. In addition, during the table flashback, the pages involved in the target table are swapped in and out of the shared memory pool, which may cause performance fluctuations in other database operations. Therefore, we recommend that you perform flashback operations during off-peak hours.
The speed of a flashback depends on the table size. For large tables, you can increase the value of polar_workers_per_flashback_table to increase the number of parallel flashback workers and reduce the flashback time.
After the table flashback is complete, you can query the data in the flashed-back table based on the NOTICE message and compare it with the data in the original table. The flashed-back table does not have any indexes. You can create indexes as needed. After the data comparison is complete, you can restore the missing data to the original table.
Parameters
Parameter | Description |
polar_enable_flashback_log | Specifies whether to enable the flashback log feature. Valid values:
Note This parameter takes effect after a |
polar_enable_fast_recovery_area | Specifies whether to enable the fast recovery area feature. Valid values:
Note This parameter takes effect after a |
polar_flashback_log_keep_segments | The number of flashback log files to retain. Valid values: 3 to 2147483647. Default value: 8. Note
|
polar_fast_recovery_area_rotation | The retention period of transaction information in the fast recovery area. Unit: minutes. Valid values: 1 to 14400. Default value: 180. Note This parameter takes effect after a |
polar_flashback_point_segments | The minimum number of WAL log files between two flashback points. Each WAL log file is 1 GB in size. Valid values: 1 to 2147483647. Default value: 16. Note This parameter takes effect after a |
polar_flashback_point_timeout | The minimum time interval between two flashback points. Unit: seconds. Valid values: 1 to 86400. Default value: 300. Note This parameter takes effect after a |
polar_flashback_log_buffers | The size of shared memory for flashback logs. Unit: KB. Valid values: 4 to 262144. Default value: 2048. Note This parameter takes effect after you modify the configuration file and restart the cluster. |
polar_flashback_logindex_mem_size | The size of shared memory for flashback log indexes. Unit: MB. Valid values: 3 to 1073741823. Default value: 64. Note This parameter takes effect after you modify the configuration file and restart the cluster. |
polar_flashback_logindex_bloom_blocks | The number of bloom filter pages for flashback log indexes. Valid values: 8 to 1073741823. Default value: 512. Note This parameter takes effect after you modify the configuration file and restart the cluster. |
polar_flashback_log_insert_locks | The number of flashback log insert locks. Valid values: 1 to 2147483647. Default value: 8. Note This parameter takes effect after you modify the configuration file and restart the cluster. |
polar_workers_per_flashback_table | The number of parallel workers for a table flashback. Valid values: 0 to 1024. Default value: 5. Note
|
polar_flashback_log_bgwrite_delay | The working interval of the flashback log bgwriter process. Unit: milliseconds. Valid values: 1 to 10000. Default value: 100. Note This parameter takes effect after a |
polar_flashback_log_flush_max_size | The maximum size of flashback logs flushed to disk by the bgwriter process at a time. Unit: KB. Valid values: 0 to 2097152. Default value: 5120. Note
|
polar_flashback_log_insert_list_delay | The working interval of the flashback log binserter process. Unit: milliseconds. Valid values: 1 to 10000. Default value: 10. Note This parameter takes effect after a |
polar_flashback_log_size_limit | The maximum size of flashback log space. Valid values: 0 to 2147483647. Default value: 20480. When the flashback log space exceeds this value, flashback log reclamation is triggered. A value of 0 indicates that the flashback log size is not limited. |