Merge Replication Identity Range Management - An Identity Disorder? Part 2
[sourcecode language="sql"] use pub Create table #tmp (col2 datetime) Insert into #tmp (col2) select getdate(); Go 400 select count(*) from #tmp 400 [/sourcecode] Let’s record the identity value of the table on publisher Dbcc checkident (tbl_pub , noreseed)
Checking identity information: current identity value '4602'The primary range is 4601-4701 & the secondary is 4701-4801 so adding 400 rows should override this Insert into pub.dbo.tbl_pub (col2) select * from #tmp Go Msg 548, Level 16, State 2, Line 1 The insert failed. It conflicted with an identity range check constraint in database 'Pub', replicated table 'dbo.tbl_pub', column 'col1'. If the identity column is automatically managed by replication, update the range as follows: for the Publisher, execute sp_adjustpublisheridentityrange; for the Subscriber, run the Distribution Agent or the Merge Agent. Dbcc checkident (tbl_pub , noreseed)
Checking identity information: current identity value '4802'The current identity pushed to the limit but no rows were inserted. Here is what happened:
- SQL Server began inserting the data and assigning new Identity values; since the trigger fires per statement, it will not fire until the batch completes.
- Because the batch has more rows than the available free Identity slots (in this case, fewer than 200) it fails because the constraint is violated. However, the next identity value is already greater than the max value defined by the constraint.
- Because the batch failed, the trigger never fired and never allocated a new range.
- Any subsequent processes, even if ONE single row, will fail because the next available Identity slot, 4802, is greater than the limit of the secondary range (4801). This is when the applications fail and return error.
[sourcecode language="sql"] select max(col1) +5 [reseed] from pub.dbo.tbl_pub where col1 between (select max(range_begin) from Distribution..MSmerge_identity_range_allocations where publication='pub1' and article= 'tbl_pub' and subscriber_db='Pub' and subscriber = 'MYINSTANCE') AND (select max(next_range_end) from Distribution..MSmerge_identity_range_allocations where publication='pub1' and article= 'tbl_pub' and subscriber_db='Pub' and subscriber = 'MYINSTANCE'); GO [/sourcecode] Both are manual workarounds, and this was not desired. Workarounds Few workarounds can work here, they all evolve around ensuring that the triggers fire to allocate new ranges including
- Increase the range assuming that a larger range can accommodate larger batches. Well,not quite true since the batches have varied sizes and user processes can reduce available identity slots to a number less than the batch rows count.
- Always assign new ranges when batches start. This creates gaps.
- Add some logic in the code that will check for the available Identity range VS batch row counts and insert if there is enough slots or assign new ranges if the rows count are more. We tried this but sometimes failed because some user processes added rows, exhausting range mid-way. It also created big gaps sometimes>If Current ID in the primary range then check the available ID count >>>> If the Available empty IDs > batch count then proceed >>>>>>>>>> If Available empty IDs If Current ID in the Secondary range then check the available ID count >>>> If the Available empty IDs > batch count then proceed >>>>>>>>>> If Available empty IDs < batch count then run sp_adjustpublisheridentityrange to assign new ranges
- Insert everything as single rows. This works, but it's very slow sometimes.
- Batch insert if the range permits, much faster
- Row by row if the range does NOT accommodate all the rows. We could have stretched it a bit more by batch inserting till the end of the range and insert the rest using row-by-row but that's complicating it more.
On this page
Share this
Share this
More resources
Learn more about Pythian by reading the following blogs and articles.
Pythian’s New Single Sign-on (SSO) Solution for Oracle E-Business Suite (EBS)—Part One
Pythian’s New Single Sign-on (SSO) Solution for Oracle E-Business Suite (EBS)—Part One
May 26, 2021 12:00:00 AM
2
min read
Data preparation with dbt and BigQuery
Data preparation with dbt and BigQuery
Jul 21, 2021 12:00:00 AM
8
min read
How to troubleshoot a failure to mount DBFS
How to troubleshoot a failure to mount DBFS
Apr 24, 2019 12:00:00 AM
12
min read
Ready to unlock value from your data?
With Pythian, you can accomplish your data transformation goals and more.