I have a SSIS package that is truncating and then inserting a whole table from serverA to serverB, however I am trying to view the progress of the job by performing a select with a NOLOCK hint on serverB but it gets lock by the “BULK INSERT” performed by the SSIS package.
I have review the “sys.dm_tran_locks” and found that the “BULK INSERT” is holding a “Sch-M” lock on the table, and I don’t understand why.
Please can you help me with this?
Thanks.
Truncate requires SCH-M for deallocating the table allocation units. Among other, this lock serves the very purpose of protecting against concurrent dirty reads. Had the truncate succeed in deallocating the table while dirty reads scanner are in it those scanner would all of a sudden find themselves reading pages that belong to no one (or worse, were reallocated to some other AU). As with any write lock, the 2PL protocol requires to be held until end of transaction. Subsequent dirty scanners will block behind it, as they should.
If you do not have the TRUNCATE/BULK INSERT wrapped in a transaction then there are other scenarios that could cause BULK INSERT itself to go SCH-M instead of X (I think table lock hint on bulk insert on an empty table (no AU root page) will cause this).
Too peek at the progress of the BULK INSERT check how many pages are allocated to your target in
sys.allocation_units. This will give you a rough estimate of percent complete.