Microsoft SQL Server
Replicate Microsoft SQL Server from transaction log backups
Replicate SQL Server by reading transaction log backups (.trn files) from an SMB share, with supplemental logging for updates.
With this method, Artie reads changes from your transaction log backup (.trn) files as they land in a shared folder, instead of from the live database. To build those changes, Artie queries the database only for table metadata, off-row values, and updates on tables without supplemental logging. Backfills still read the tables from the database.
Latency is at least your log backup interval, plus up to 5 minutes, because Artie checks the folder every 5 minutes.
Prerequisites
- SQL Server 2019 or later
- Database recovery model set to
FULLorBULK_LOGGED - Log backups taken on a schedule to one folder on an SMB share, such as Amazon FSx for Windows File Server. See Backup folder and file names.
- Network access from Artie to the share on port 445
- An SMB user that can read the folder. Artie signs in with NTLM and requires SMB message signing.
- A primary key on each replicated table
- Supplemental logging on each replicated table
- A service account with
SELECTon the database, for table metadata and off-row values
Set up transaction log backups
Configure log backups
Write every log backup for the database to one folder. File names must end in
_YYYYMMDD_HHMMSS.trn, with a lowercase.trnextension. For example, Ola Hallengren’s maintenance solution writes names in this format.SQLEXECUTE dbo.DatabaseBackup @Databases = 'ANALYTICS', @Directory = '\\fs-0123456789abcdef0.corp.example.com\share\sqlbackups', @BackupType = 'LOG', @DirectoryStructure = NULL, @Compress = 'Y';Use backup compression only with the
MS_XPRESS(default) orZSTDalgorithm.Create a service account
Run this in the database as a database admin. It creates the login Artie connects with and grants it
SELECTon the database.SQLUSE [ANALYTICS]; CREATE LOGIN artie_transfer WITH PASSWORD = 'PASSWORD'; CREATE USER artie_transfer FOR LOGIN artie_transfer; ALTER LOGIN artie_transfer ENABLE; GRANT SELECT on DATABASE::[ANALYTICS] to artie_transfer; GOReplace
PASSWORDwith a password for the login.If a table’s clustered index is non-unique, or isn’t on the table’s leading columns, also grant
SELECTonsys.system_internals_partition_columns. Without it, Artie can’t decode that table’s rows.Enable supplemental logging
Follow Supplemental logging for each replicated table.
Add the SMB share in Artie
In Settings > Connectors, select New connector. Choose the environment your pipeline uses, then set Connector type to SMB share.
- UNC path: the folder your log backups go to, the same path as in
BACKUP LOG ... TO DISK. For example,\\fs-0123456789abcdef0.corp.example.com\share\sqlbackups. - Username and Password: a user that can read the folder, as
DOMAIN\useroruser@domain. - PrivateLink connection (optional): pick one if the share is only reachable privately. It must forward port 445. This field appears only when PrivateLink is available to your account.
Select Test connection. The button reads Able to connect when the check passes. Then select Save.
- UNC path: the folder your log backups go to, the same path as in
Select the replication method
When you create the pipeline, go to the Connect and verify step in the Source section and enter the database connection. Under Replication method, select Transaction log backups.
Under Where are the transaction log backups stored?, choose the share in the SMB share list. The list shows only shares in the pipeline’s environment, and on its data plane if the share has one.
If setup shows a Prepare source step, its script is for capture instances. Run its service account step, then follow Supplemental logging in place of its CDC steps, because that section also drops the CDC capture job.
To switch an existing pipeline, open the Source: Microsoft SQL Server tab on the Edit pipeline page. Replication method is locked while the pipeline is running.
Verify the connection
On the Connect and verify step, select Verify source. The check includes the SMB share, and the step shows Source verified when it passes.
Supplemental logging
By default, SQL Server logs only the bytes an UPDATE changed. For those tables, Artie looks the row up in the database when it reads the backup. The message then carries the row as it is at that point, not as of the update. An update to a row deleted since is lost.
An update that writes only off-row TEXT, NTEXT or IMAGE values is lost too, because SQL Server doesn’t log the row for it. With the table option large value types out of row on, the same applies to VARCHAR(MAX), NVARCHAR(MAX), VARBINARY(MAX) and XML values.
With supplemental logging, SQL Server logs the whole row for every update, and Artie reads updates straight from the backup, apart from off-row values. Enable it on each table in one of two ways:
Run this in the database. It enables change data capture (CDC) on each table, then drops the capture job.
USE [ANALYTICS];
GO
EXEC sys.sp_cdc_enable_db;
GO
-- For each table you want to replicate:
EXEC sys.sp_cdc_enable_table @source_schema = N'dbo', @source_name = N'orders', @role_name = NULL;
GO
-- Artie does not read the cdc change tables, so stop SQL Server from filling them.
EXEC sys.sp_cdc_drop_job @job_type = N'capture';
GOOn Amazon RDS, enable CDC on the database with EXEC msdb.dbo.rds_cdc_enable_db 'ANALYTICS'; instead of sys.sp_cdc_enable_db.
Use this if CDC isn’t an option. It requires a distributor and isn’t available on Amazon RDS.
-- Once per server, if no distributor is configured:
USE master;
GO
EXEC sp_adddistributor @distributor = @@SERVERNAME, @password = N'';
EXEC sp_adddistributiondb @database = N'distribution';
EXEC sp_adddistpublisher @publisher = @@SERVERNAME, @distribution_db = N'distribution';
EXEC sp_replicationdboption @dbname = N'ANALYTICS', @optname = N'publish', @value = N'true';
GO
USE [ANALYTICS];
GO
EXEC sp_addpublication @publication = N'artie', @status = N'active', @repl_freq = N'continuous';
-- For each table you want to replicate:
EXEC sp_addarticle @publication = N'artie', @article = N'orders', @source_owner = N'dbo', @source_object = N'orders', @type = N'logbased';
-- SQL Server only logs whole rows once the publication has a subscription.
EXEC sp_addsubscription @publication = N'artie', @subscriber = @@SERVERNAME, @destination_db = N'ANALYTICS_SUBSCRIBER',
@subscription_type = N'push', @sync_type = N'replication support only', @article = N'all';
GOThe subscriber database must already exist and can’t be master. No agents need to run.
Check supplemental logging
Run this query in the database. is_replicated is 1 for each table with supplemental logging.
SELECT s.name AS schema_name, t.name AS table_name, t.is_replicated
FROM sys.tables t
JOIN sys.schemas s ON t.schema_id = s.schema_id;When the pipeline starts, Artie logs a warning that names each configured table without supplemental logging.
Let the log truncate
Both options mark every transaction on these tables for replication. SQL Server keeps each one in the log until something marks it as distributed. Without the CDC capture job or a Log Reader Agent, log_reuse_wait_desc in sys.databases stays at REPLICATION. The log then keeps growing, even with regular log backups.
Artie reads your log backups, so marking everything as distributed on a schedule is safe. Each log backup still contains every change. Schedule this with SQL Server Agent:
USE msdb;
GO
EXEC sp_add_job @job_name = N'artie_mark_distributed';
EXEC sp_add_jobstep @job_name = N'artie_mark_distributed', @step_name = N'sp_repldone', @database_name = N'ANALYTICS',
@command = N'EXEC sp_repldone @xactid = NULL, @xact_seqno = NULL, @numtrans = 0, @time = 0, @reset = 1;';
EXEC sp_add_jobschedule @job_name = N'artie_mark_distributed', @name = N'every 15 minutes',
@freq_type = 4, @freq_interval = 1, @freq_subday_type = 4, @freq_subday_interval = 15;
EXEC sp_add_jobserver @job_name = N'artie_mark_distributed';
GOEvery 5 minutes, Artie checks whether the log is waiting on REPLICATION. It logs a warning once a database has been waiting for an hour or more.
Backup folder and file names
Artie lists one folder, without subfolders, and reads files whose names end in _YYYYMMDD_HHMMSS.trn. The match is case-sensitive, so Artie ignores a file that ends in .TRN. Artie reads files in file name order and skips any file whose name sorts before the last file it read.
- Use one folder per database, and one naming prefix. If backups land in subfolders, or different servers write different prefixes, Artie can skip files.
- Keep every log backup in the folder until Artie has read it. Artie doesn’t check that each backup starts where the previous one ended.
Protect the folder from cleanup jobs that delete files before Artie reads them.
Artie detects one case. If a transaction was still open when Artie last saved its position, Artie needs the file where that transaction began. If that file is gone, the pipeline stops with this error:
<file> is gone from the archive location, but transactions that were open or not all published at the offset began in itArtie treats a file as complete once the writer has closed it and its size is above zero and stays the same for two 3-second checks in a row.
For availability groups, see Log backups in an availability group.
How Artie reads log backups
- Every 5 minutes, Artie looks for new backup files in the folder.
- Artie parses each file’s backup container and log records, and builds row changes from the log records of the configured tables.
- Artie publishes only committed transactions, in commit order. It drops rolled-back transactions.
- A transaction that spans several log backups is held until it ends. Once its changes pass 4 MB, Artie writes them to a spool directory on disk.
- Artie saves its position (the last file and the commit LSN) only after changes are written to its Kafka buffer.
- After a restart, Artie rereads from the first file with a still-open transaction and skips changes it has already published.
- If the connection to the SMB share drops, Artie tries to reconnect for up to 2 minutes, then reads again from its saved position.
Open transactions over the spool limit
If open transactions need more than 10 GiB of spool, the pipeline stops with this error:
transactions open across log backups need more than trnCdcSettings.spoolMaxBytesEnd the transaction, or contact Artie to raise the limit.
Changes Artie can’t publish exactly
SQL Server doesn’t publish its log backup format. Artie checks each change against the table’s layout and stops instead of guessing. The pipeline stops with this error:
halted at a change that cannot be published exactly: <kind> at LSN <lsn>, table <database.schema.table>Nothing from that change on is published until it’s resolved. Contact Artie with the error. Resolving it usually means a backfill of the named table.
<kind> is one of these values:
| Kind | The change Artie stopped on |
|---|---|
update_without_row_image | An update logged without its whole row, on a table with supplemental logging |
update_row_layout_mismatch | An update whose row doesn’t match the table’s columns |
update_undecodable | An update whose row doesn’t decode with the table’s layout |
update_off_row_value_changed | An update that changes an off-row value Artie can’t read from the database, such as VARBINARY(MAX) or JSON |
update_off_row_key | An update of a row whose primary key column is stored off-row |
update_unstored_computed_column | An update on a table with a replicated computed column that isn’t persisted |
rolled_back_update_without_row_image | A rolled-back update whose original row isn’t in the log |
rolled_back_update_mismatch | A rolled-back update that doesn’t restore the whole row its update logged |
rolled_back_update_key_unknown | A rolled-back update where Artie can’t tell whether the primary key changed back |
rolled_back_change_not_kept | A rolled-back insert or update in a transaction too large for Artie to keep its changes, more than about a million rows or 64 MiB of inserted rows |
heap_forward_unverified | A heap row moved to another page, and Artie can’t confirm the move changed no values |
delete_insert_across_backups | A delete or insert that may be half of a row move split across log backups |
delete_insert_unprovable | A delete and insert of one row that Artie can’t prove is a single update |
transaction_aborted_without_undo | An aborted transaction that didn’t undo its changes |
A rolled_back_change_not_kept error also names the transaction. Each table with such a rolled-back change stops on its own, before any of that transaction’s changes to the table are replicated; the transaction’s other tables replicate as usual. The transaction has already ended, so once a table is backfilled, Artie skips all of that transaction’s changes to it. Another table with such a change stops the pipeline again, to be backfilled too.
Off-row columns
Values stored off-row, such as large VARCHAR(MAX) values, aren’t in the log with the rest of the row.
- Inserts: Artie reads off-row values from the database. If the row has been deleted by then, they’re published empty.
- Updates that leave an off-row value unchanged: the value stays as it is in the destination.
- Updates that change an
XML,VARCHARorNVARCHARvalue, or change the primary key of a row whose off-row values are all of these types: Artie reads the value from the database by primary key. This includesVARCHAR(n)andNVARCHAR(n)values that SQL Server moved off-row to keep the row within 8,060 bytes. The value is the row’s value when Artie reads the log backup, not its value at the time of the update. If the row was updated again in between, each of those updates is published with the latest value. If the row has been deleted by then, the destination keeps its current value until the delete arrives. - Every update of a row with a
TEXT,NTEXTorIMAGEvalue off-row: Artie reads the value from the database by primary key, the same way. The row holds the same pointer whether or not the update changed the value. This also applies toVARCHAR(MAX),NVARCHAR(MAX),VARBINARY(MAX)andXMLvalues written while the table optionlarge value types out of rowwas on. - Updates that change any other off-row value, or change the primary key of a row with one: the pipeline stops with
update_off_row_value_changed. This coversVARBINARY(MAX),VARBINARY(n)andJSON. Backfill the table, or exclude the column. - Computed columns that aren’t persisted: an update stops the pipeline with
update_unstored_computed_column. Persist the column, or exclude it.
Limitations
- Encrypted databases and backups: TDE-encrypted databases and encrypted backups aren’t supported.
- Backup compression:
QAT_DEFLATEisn’t supported. UseMS_XPRESSorZSTD. - Backup location: only SMB shares are supported. Azure Blob Storage isn’t supported.
- Accelerated database recovery and heap tables with row versioning: updates aren’t certified. Contact Artie before using them.
TRUNCATE TABLEisn’t allowed on tables tracked by CDC or published for replication.- Latency is at least your log backup interval. Take log backups every few minutes for lower latency.
Was this page helpful?
- Need help? Message us on Slack or email [email protected].
- Want a walkthrough? Book a demo.
- LLM? Read llms.txt.