Error: Unable to Retrieve Maximum Change Data Capture LSN Version
Issue
A SQL Server connection using change data capture (CDC) completes incremental syncs but processes no rows, even when source data changes are expected. When you attempt a historical sync, the connection may fail with the following error, which refers to the maximum CDC log sequence number (LSN):
Unable to retrieve maximum change data capture LSN version
This issue may occur even when CDC is enabled at the database and table levels.
Environment
- Connector: SQL Server
- Incremental sync method: Change data capture (CDC)
- SQL Server deployments that use SQL Server Agent for CDC capture
Resolution
To resolve this issue:
- Verify that CDC is enabled for the source database:An
SELECT DB_NAME() AS database_name, is_cdc_enabled FROM sys.databases WHERE database_id = DB_ID();is_cdc_enabledvalue of1confirms that CDC is enabled. For more information, see How Can I Check Whether I Have Enabled Change Data Capture (CDC) on Databases and Tables?. - Check whether SQL Server returns a valid maximum CDC LSN:If
SELECT sys.fn_varbintohexstr(sys.fn_cdc_get_max_lsn()) AS max_cdc_lsn;max_cdc_lsnisNULL, verify that SQL Server Agent and the CDC capture job are running, as described in the following steps. - Check the configured CDC jobs and confirm that the database has a capture job, such as
cdc.<database_name>_capture:EXEC sys.sp_cdc_help_jobs; - In SQL Server Management Studio (SSMS), go to SQL Server Agent > Jobs > cdc.<database_name>_capture and verify that the CDC capture job is running. If the job is stopped or failing, review its history and resolve the underlying error before restarting it.
If the CDC capture job is configured to run continuously, an
In Progressstatus is expected. - Verify that the CDC log scanner is processing the transaction log:In the results, confirm that:
SELECT TOP 20 session_id, start_time, end_time, error_count, start_lsn, current_lsn, end_lsn, tran_count, command_count FROM sys.dm_cdc_log_scan_sessions ORDER BY start_time DESC;- Recent scan sessions are present.
- LSN values advance when transactions are available to process.
error_countis0.
- Check for CDC processing errors. Resolve any errors the query returns:
SELECT TOP 20 entry_time, error_number, error_severity, error_state, error_message FROM sys.dm_cdc_errors ORDER BY entry_time DESC; - After the capture process resumes processing, check the maximum CDC LSN again:Confirm that SQL Server returns a valid LSN instead of
SELECT sys.fn_varbintohexstr(sys.fn_cdc_get_max_lsn()) AS max_cdc_lsn;NULL. - In Fivetran, go to your SQL Server connection page.
- In the top right, click Sync.
If we detect that a table's previous CDC LSN is no longer valid, we display a Resync Table warning with the following message:
LSN is currently invalid, it has been too long since the last incremental update
When this happens, we automatically schedule a historical sync for that table. No manual re-sync is required.
If we report a missing CDC cursor, see Error: CDC LSN Cursor Is Expected to Be Set in SqlServerImportTask.
Cause
This issue occurs when the SQL Server CDC capture job stops processing the transaction log. The capture job maintains the LSN information we require to track changes.
CDC can remain enabled at the database and table levels even when the capture job stops processing the transaction log. Therefore, confirming that CDC is enabled doesn't confirm that the CDC capture process is healthy.
When the CDC capture job stops processing the transaction log, SQL Server may return NULL for the maximum CDC LSN. Without a valid maximum LSN, we may be unable to establish the CDC replication position required for incremental or historical syncs.