Details
-
Technical task
-
Status: Open (View Workflow)
-
Major
-
Resolution: Unresolved
-
None
-
None
-
None
Description
WITH BACKUP extension
RESTORE may include an optional WITH BACKUP clause:
RESTORE
|
{ TABLE table_name [, table_name] ... |
| DATABASE database_name } |
FOR SYSTEM_TIME AS OF as_of_value |
[WITH BACKUP] |
[WAIT wait_seconds | NOWAIT];
|
When WITH BACKUP is specified, RESTORE must create backup copies of all target tables before modifying them.
The backup names are generated using:
- @@system_versioning_backup_prefix
- @@system_versioning_backup_suffix
- @@system_versioning_backup_type
System variables
| Variable | Type | Valid values | Description |
|---|---|---|---|
| @@system_versioning_backup_prefix | String | Any valid identifier fragment | String prepended to a backup table or database name |
| @@system_versioning_backup_suffix | String | Any valid identifier fragment | String appended to a backup table or database name |
| @@system_versioning_backup_type | Enumeration | TABLE, DB, AUTO | Determines whether backup objects are created as renamed tables or in separate databases |
The effective backup name is constructed as follows:
backup_name =
|
@@system_versioning_backup_prefix
|
+ original_name
|
+ @@system_versioning_backup_suffix
|
Prefix and suffix values are used verbatim. Identifier quoting does not make a generated name valid if it exceeds the maximum identifier length.
TABLE mode
When @@system_versioning_backup_type is TABLE, backup tables are created in the same database as their source tables.
The backup table name is:
backup_table_name =
|
@@system_versioning_backup_prefix
|
+ original_table_name
|
+ @@system_versioning_backup_suffix
|
Example:
SET @@system_versioning_backup_type = 'TABLE'; |
SET @@system_versioning_backup_prefix = ''; |
SET @@system_versioning_backup_suffix = '_before_restore'; |
|
|
RESTORE TABLE accounting.invoice |
FOR SYSTEM_TIME AS OF TIMESTAMP '2026-09-28 10:30:00.000000' |
WITH BACKUP; |
This creates:
accounting.invoice_before_restore
|
For a table list, one backup table is created for every target table in its original database.
DB mode
When @@system_versioning_backup_type is DB, a separate backup database is created.
The backup database name is:
backup_database_name =
|
@@system_versioning_backup_prefix
|
+ original_database_name
|
+ @@system_versioning_backup_suffix
|
Backup tables retain their original table names.
Example:
SET @@system_versioning_backup_type = 'DB'; |
SET @@system_versioning_backup_prefix = ''; |
SET @@system_versioning_backup_suffix = '_before_restore'; |
|
|
RESTORE DATABASE accounting |
FOR SYSTEM_TIME AS OF TIMESTAMP '2026-09-28 10:30:00.000000' |
WITH BACKUP; |
This creates backup tables such as:
accounting_before_restore.invoice
|
accounting_before_restore.invoice_item
|
accounting_before_restore.payment
|
If RESTORE TABLE contains tables from multiple source databases, one backup database is created for each source database.
AUTO mode
When @@system_versioning_backup_type is AUTO, the effective mode depends on the RESTORE target:
| Statement | Effective mode |
|---|---|
| RESTORE TABLE | TABLE |
| RESTORE DATABASE | DB |
Example:
SET @@system_versioning_backup_type = 'AUTO'; |
SET @@system_versioning_backup_suffix = '_before_restore'; |
|
|
RESTORE TABLE accounting.invoice |
FOR SYSTEM_TIME AS OF TIMESTAMP '2026-09-28 10:30:00.000000' |
WITH BACKUP; |
The backup is created as:
accounting.invoice_before_restore
|
For a database restore:
RESTORE DATABASE accounting |
FOR SYSTEM_TIME AS OF TIMESTAMP '2026-09-28 10:30:00.000000' |
WITH BACKUP; |
The backup database is:
accounting_before_restore
|
Backup contents
Each backup table must represent the source table exactly as it exists immediately before RESTORE starts.
The backup must include:
- The current rows
- Existing historical row versions
- System-versioning period definitions
- Columns, indexes, and table options required to restore the original table
- The table state before any RESTORE-generated changes
A single statement-level snapshot must be used for all backup tables.
Validation and failure handling
Before creating or modifying any objects, RESTORE WITH BACKUP must validate:
- All generated database and table names are valid
- Generated names do not exceed identifier-length limits
- No generated backup table or database already exists
- The user has privileges to create all required backup objects
- Sufficient historical data exists for the requested restore point
- All target tables can be restored
- The selected backup type is valid for the RESTORE target
If validation or backup creation fails, RESTORE must not modify any target table.
Existing backup objects must not be overwritten, truncated, or reused implicitly. A naming collision must produce an error identifying the conflicting object.
If restoration fails after all backups have been created, the restore changes must be rolled back. Successfully created backup objects should remain available for recovery and diagnostics.
Privileges
RESTORE WITH BACKUP requires:
- The privileges normally required by RESTORE
- CREATE privilege for every generated backup database
- CREATE privilege for every generated backup table
- Required privileges to copy the complete source-table definition and data
All privilege checks must complete before backup creation or target-table modification.
Replication
Backup creation must be binary logged together with RESTORE.
The source table list, resolved AS OF value, effective backup mode, and generated object names must be deterministic on replicas. Replicas must not independently evaluate different prefix, suffix, or backup-type values.
Additional acceptance criteria
- RESTORE without WITH BACKUP does not create backup objects.
- TABLE mode creates backup tables in each source table's database.
- TABLE mode applies the configured prefix and suffix to table names.
- DB mode creates backup databases using the configured prefix and suffix.
- DB mode preserves original table names inside backup databases.
- AUTO selects TABLE mode for RESTORE TABLE.
- AUTO selects DB mode for RESTORE DATABASE.
- A multi-database RESTORE TABLE in DB mode creates one backup database per source database.
- Backup tables contain the complete pre-restore state.
- Backup creation completes before target tables are modified.
- Existing backup-object names cause an error without modifying target tables.
- Invalid or overlong generated identifiers cause an error.
- Failure to create any backup prevents the restore.
- Backup naming and creation are deterministic under replication.