DBAtlas · Database operations atlas

Add a database to an AG (automatic seeding) and monitor seeding progress

SQL ServerAlways On & HA

Command

-- Prerequisite (primary): FULL recovery and at least one FULL backup
SELECT name, recovery_model_desc FROM sys.databases WHERE name = N'Musteri';
BACKUP DATABASE [Musteri] TO DISK = N'D:\Backup\Musteri_pre_ag.bak' WITH COMPRESSION, CHECKSUM, INIT;

-- 1) Primary: switch the replica to automatic seeding
ALTER AVAILABILITY GROUP [AG_Musteri]
  MODIFY REPLICA ON N'SQL02' WITH (SEEDING_MODE = AUTOMATIC);

-- 2) On each secondary: allow the AG to create databases
ALTER AVAILABILITY GROUP [AG_Musteri] GRANT CREATE ANY DATABASE;

-- 3) Primary: add the database
ALTER AVAILABILITY GROUP [AG_Musteri] ADD DATABASE [Musteri];

-- 4) Progress (primary or secondary):
SELECT local_database_name, role_desc, internal_state_desc,
       transferred_size_bytes / 1048576         AS transferred_mb,
       database_size_bytes / 1048576            AS db_mb,
       transfer_rate_bytes_per_second / 1048576 AS mb_s,
       estimate_time_complete_utc, failure_message
FROM   sys.dm_hadr_physical_seeding_stats;

-- 5) Seeding history and failure reason:
SELECT s.start_time, s.completion_time, adc.database_name, ar.replica_server_name,
       s.current_state, s.failure_state_desc, s.error_code, s.number_of_attempts
FROM   sys.dm_hadr_automatic_seeding s
  JOIN sys.availability_databases_cluster adc ON adc.group_database_id = s.ag_db_id
  JOIN sys.availability_replicas ar           ON ar.replica_id = s.ag_remote_replica_id
ORDER BY s.start_time DESC;

What it does

Adds the database to the AG by streaming it to the secondaries over the endpoint (VDI) without a manual backup/restore; shows seeding throughput, estimated completion and failure reason.

When to use it

When adding a new database to an AG, adding a new replica, or rebuilding a broken secondary database.

Caution

Seeding saturates the endpoint link and is uncompressed by default; for very large databases or a weak WAN a manual backup/restore + JOIN is more predictable. The primary log is not truncated during seeding (log_reuse_wait_desc = AVAILABILITY_REPLICA). On 2016 the secondary needs the same data/log folder paths as the primary; on 2017+, if the primary files are in the instance default paths, the secondary uses its own default paths. For a TDE database the certificate must be restored on every secondary first. Without GRANT CREATE ANY DATABASE on the secondary, seeding stays pending.

Open in DBAtlas →

Related