(somewhat) Social

Entries in replication (5)

Wednesday
Sep292010

CLR Errors – Follow Up

A few weeks ago I posted about an issue I was having with CLR failing on one of my production servers. While a reboot fixed the problem it was a temporary fix and happened on a couple of further occasions. Still not being able to find the root cause Microsoft recommended a complete removal and reinstall of the .NET framework.

One of the Microsoft support team advised running the following query which loads CLR and then traverses through all the registered assemblies. This quickly shows up any problems with CLR and SQL Server integration even if the problem is not showing elsewhere.

 

  1. select * from sys.assemblies

 

Seeing the following is not a good thing:

Msg 6512, Level 16, State 27, Line 1

Failed to initialize the Common Language Runtime (CLR) v2.0.50727 with HRESULT 0x80004005. You need to restart SQL Server to use CLR integration features.

 

Removing the .NET Frameworks

Firstly I downloaded the .NET Framework Cleanup Tool from http://blogs.msdn.com/b/astebner/archive/2008/08/28/8904493.aspx

With the tool installed on the server and all SQL services shut down and set to manual start I performed an uninstall of all of the .NET Frameworks in the order

  • 3.5
  • 3.0
  • 2.0

The removal complete a server restart was in order. Once restarted it was time to

 

Reinstall the .NET Frameworks

Here’s the interesting thing, there’s actually no need to go through and reinstall each individual Framework, in fact just download and install .NET Framework 3.5 SP1 from Microsoft.

Once the install was complete and the server restarted I brought the SQL services back online (and reset their services to start automatically)

 

Checking sys.assemblies again gave me positive results

name      principal_id           assembly_id          clr_name               permission_set     permission_set_desc          is_visible                create_date                modify_date         is_user_defined

Microsoft.SqlServer.Types 4              1              microsoft.sqlserver.types, version=10.0.0.0, culture=neutral, publickeytoken=89845dcd8080cc91, processorarchitecture=msil          3              UNSAFE_ACCESS  1              2008-07-09 16:20:00.070                2008-07-09 16:20:00.413 0

 

 

Since the reinstall I’ve not experienced further issues. It’s a shame that Microsoft were unable to give me a root cause to the problem, however it’s great to have it resolved.

Monday
Sep132010

T-SQL Tuesday #10 - Applying Indexes To Replicated Tables

It's T-SQL Tuesday time again! This month Michael J. Swart (blog|twitter) is hosting and has given us the topic of indexes.

I've previously written about how you can find indexes that are not being used and get their sizes. Not being allowed to reuse that meant that I had to come up with something else.

In my previous post on calculating replication schema options I documented a way of being able to change article options in transactional replication so that you could apply all the non-clustered indexes on your subscribing table that exist on the publishing one. But what if you are using replication to offload reporting and you need to create indexes that don't exist on the publishing table?

You could always manually apply these indexes any time that you perform a snapshot but this requires manual work and can easily be forgotten.

Fortunately there is another option, you can have replication automatically apply a post-snapshot SQL script which will create those indexes for you. Here's how...

 

Create a basic publication (snapshot or transactional) and add a table article. Perform the snapshot and compare indexes between the tables (in this example I am using the person.address table from AdventureWorks2008).

After the snapshot here's how the indexes stand

 

For this example we're going to create a new index on Admindb.person.address. To do this first we need to generate the index creation script

USE AdminDB
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[person].[address]') AND name = N'NCIX_Zip')
CREATE NONCLUSTERED INDEX [NCIX_Zip] ON [person].[address]
(
    [PostalCode] ASC
) WITH (ONLINE = ON, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

 

Save this script to C:\ReplicationScripts\CreateReportingIndex.sql

 

Now we change the existing publication so that whenever a snapshot is taken it will apply this script when applied to the subscriber. This change can be done in a couple of ways.

The quick and easy way is to use sp_changepublication and just pass in the publication name, property change and location of the script.

USE AdventureWorks2008
GO
EXEC sp_changepublication
    @publication = 'Post Snapshot Test'
  , @property = N'post_snapshot_script'
  , @value = 'C:\ReplicationScripts\CreateReportingIndex.sql'
  , @force_invalidate_snapshot = 1 --must invalidate existing snapshots for this to take effect
GO

 

This can also be done using the GUI (right click on the publication and select Properties)

 

 

When you next perform a snapshot then the script will be applied. We can check that this actually worked first by looking at replication monitor where it will tell you that the script was applied.

 

And then you can check the table itself and confirm that the index exists

 

 

There are a couple of final things to note.

Usually you will specify a unc path for the SQL script so that you just have to maintain a single copy. You just need to ensure that the subscriber is able to access the post-snapshot script location.

Post-snapshots are not limited to creating indexes, in fact you can do pretty much anything just so long as the Distribution Agent has the access;

  • Send yourself an email to let you know that the snapshot has been applied on the subscriber
  • Apply table permissions
  • Create views

 

If you use replication and find yourself manually reapplying indexes it's worth evaluating whether using post-snapshot scripts might save you some work.

Thursday
Sep092010

Calculating Replication Schema Options

I recently evaluated using page compression on some of my production data (see my post on estimating data compression). I'm using transactional replication to send this data out to a few other servers for reporting pusposes and I wanted to adjust replication so that the data compression settings on the tables would also be replicated.

Making life a little easier on this was an application change that was going to require me dropping and recreating replication so I figured it would be a simple case to change the replication options when recreating the publications.

Using the GUI I scripted out all of the existing publications and then dropped them.

Now I wanted to adjust the script so that the schema options I wanted replicated were applied. Here's the sp_addarticle for one of these tables (SQL 2008)

 

-- Adding the transactional articles
use [TestDB]
exec sp_addarticle
    @publication = N'TestDB_TestTable'
    , @article = N'TestTable'
    , @source_owner = N'dbo'
    , @source_object = N'TestTable'
    , @type = N'logbased'
    , @description = N''
    , @creation_script = N''
    , @pre_creation_cmd = N'drop'
    , @schema_option = 0x000000000803509F
    , @identityrangemanagementoption = N'none'
    , @destination_table = N'TestTable'
    , @destination_owner = N'dbo'
    , @status = 24
    , @vertical_partition = N'false'
    , @ins_cmd = N'CALL [dbo].[sp_MSins_dboTestTable]'
    , @del_cmd = N'CALL [dbo].[sp_MSdel_dboTestTable]'
    , @upd_cmd = N'SCALL [dbo].[sp_MSupd_dboTestTable]'
GO

 

Now this doesn't tell you a lot about what schema options are in place. The binary value @schema_option has all the information you need however it's not exactly user friendly. Books Online has the information available to be able to figure this out, it just takes a little calculation. Somewhat of a pain I've created a script to tell me what options are enabled.

All you need to do is pass in the @schema_option value and it will tell you what options are enabled (in this case  0x000000000803509F)

 

/* PROVIDES THE REPLICATION OPTIONS ENABLED FOR A GIVEN @SCHEMA_OPTION IN SYSARTICLES */
declare @schema_option varbinary(8) = 0x000000000803509F  --< PUT YOUR SCHEMA_OPTION HERE

set nocount on
declare @OptionTable table ( HexValue varbinary(8), IntValue as cast(HexValue as bigint), OptionDescription varchar(255))
insert into @OptionTable (HexValue, OptionDescription)
select 0x01 ,'Generates object creation script'
union all  select 0x02 ,'Generates procs that propogate changes for the article'
union all  select 0x04 ,'Identity columns are scripted using the IDENTITY property'
union all  select 0x08 ,'Replicate timestamp columns (if not set timestamps are replicated as binary)'
union all  select 0x10 ,'Generates corresponding clustered index'
union all  select 0x20 ,'Converts UDT to base data types'
union all  select 0x40 ,'Create corresponding nonclustered indexes'
union all  select 0x80 ,'Replicate pk constraints'
union all  select 0x100 ,'Replicates user triggers'
union all  select 0x200 ,'Replicates foreign key constraints'
union all  select 0x400 ,'Replicates check constraints'
union all  select 0x800  ,'Replicates defaults'
union all  select 0x1000 ,'Replicates column-level collation'
union all  select 0x2000 ,'Replicates extended properties'
union all  select 0x4000 ,'Replicates UNIQUE constraints'
union all  select 0x8000 ,'Not valid'
union all  select 0x10000 ,'Replicates CHECK constraints as NOT FOR REPLICATION so are not enforced during sync'
union all  select 0x20000 ,'Replicates FOREIGN KEY constraints as NOT FOR REPLICATION so are not enforced during sync'
union all  select 0x40000 ,'Replicates filegroups'
union all  select 0x80000 ,'Replicates partition scheme for partitioned table'
union all  select 0x100000 ,'Replicates partition scheme for partitioned index'
union all  select 0x200000 ,'Replicates table statistics'
union all  select 0x400000 ,'Default bindings'
union all  select 0x800000 ,'Rule bindings'
union all  select 0x1000000 ,'Full text index'
union all  select 0x2000000 ,'XML schema collections bound to xml columns not replicated'
union all  select 0x4000000 ,'Replicates indexes on xml columns'
union all  select 0x8000000 ,'Creates schemas not present on subscriber'
union all  select 0x10000000 ,'Converts xml columns to ntext'
union all  select 0x20000000 ,'Converts (max) data types to text/image'
union all  select 0x40000000 ,'Replicates permissions'
union all  select 0x80000000 ,'Drop dependencies to objects not part of publication'
union all  select 0x100000000 ,'Replicate FILESTREAM attribute (2008 only)'
union all  select 0x200000000 ,'Converts date & time data types to earlier versions'
union all  select 0x400000000 ,'Replicates compression option for data & indexes'
union all  select 0x800000000  ,'Store FILESTREAM data on its own filegroup at subscriber'
union all  select 0x1000000000 ,'Converts CLR UDTs larger than 8000 bytes to varbinary(max)'
union all  select 0x2000000000 ,'Converts hierarchyid to varbinary(max)'
union all  select 0x4000000000 ,'Replicates filtered indexes'
union all  select 0x8000000000 ,'Converts geography, geometry to varbinary(max)'
union all  select 0x10000000000 ,'Replicates geography, geometry indexes'
union all  select 0x20000000000 ,'Replicates SPARSE attribute '
                  
select HexValue,OptionDescription as 'Schema Options Enabled'
From @OptionTable where (cast(@schema_option as bigint) & cast(HexValue as bigint)) <> 0

 

This is really great and useful. In fact you can use this script to check out the schema options for any article, just plug in the schem_option value from sysarticles to get the data.

Now to take it a step further I wanted to know what the new value would need to be for @schema_option in order to replicate the data compression settings. For this I wrote another query. This time it's just a case of uncommenting the lines for the schema options that I want and running the select.

select cast(
  cast(0x01 AS BIGINT) --DEFAULT Generates object creation script
| cast(0x02 AS BIGINT) --DEFAULT Generates procs that propogate changes for the article
| cast(0x04 AS BIGINT) --Identity columns are scripted using the IDENTITY property
| cast(0x08 AS BIGINT) --DEFAULT Replicate timestamp columns (if not set timestamps are replicated as binary)
| cast(0x10 AS BIGINT) --DEFAULT Generates corresponding clustered index
--| cast(0x20 AS BIGINT) --Converts UDT to base data types
--| cast(0x40 AS BIGINT) --Create corresponding nonclustered indexes
| cast(0x80 AS BIGINT) --DEFAULT Replicate pk constraints
--| cast(0x100 AS BIGINT) --Replicates user triggers
--| cast(0x200 AS BIGINT) --Replicates foreign key constraints
--| cast(0x400 AS BIGINT) --Replicates check constraints
--| cast(0x800 AS BIGINT)  --Replicates defaults
| cast(0x1000 AS BIGINT) --DEFAULT Replicates column-level collation
--| cast(0x2000 AS BIGINT) --Replicates extended properties
| cast(0x4000 AS BIGINT) --DEFAULT Replicates UNIQUE constraints
--| cast(0x8000 AS BIGINT) --Not valid
| cast(0x10000 AS BIGINT) --DEFAULT Replicates CHECK constraints as NOT FOR REPLICATION so are not enforced during sync
| cast(0x20000 AS BIGINT) --DEFAULT Replicates FOREIGN KEY constraints as NOT FOR REPLICATION so are not enforced during sync
--| cast(0x40000 AS BIGINT) --Replicates filegroups (filegroups must already exist on subscriber)
--| cast(0x80000 AS BIGINT) --Replicates partition scheme for partitioned table
--| cast(0x100000 AS BIGINT) --Replicates partition scheme for partitioned index
--| cast(0x200000 AS BIGINT) --Replicates table statistics
--| cast(0x400000 AS BIGINT) --Default bindings
--| cast(0x800000 AS BIGINT) --Rule bindings
--| cast(0x1000000 AS BIGINT) --Full text index
--| cast(0x2000000 AS BIGINT) --XML schema collections bound to xml columns not replicated
--| cast(0x4000000 AS BIGINT) --Replicates indexes on xml columns
| cast(0x8000000 AS BIGINT) --DEFAULT Creates schemas not present on subscriber
--| cast(0x10000000 AS BIGINT) --Converts xml columns to ntext
--| cast(0x20000000 AS BIGINT) --Converts (max) data types to text/image
--| cast(0x40000000 AS BIGINT) --Replicates permissions
--| cast(0x80000000 AS BIGINT) --Drop dependencies to objects not part of publication
--| cast(0x100000000 AS BIGINT) --Replicate FILESTREAM attribute (2008 only)
--| cast(0x200000000 AS BIGINT) --Converts date & time data types to earlier versions
| cast(0x400000000 AS BIGINT) --Replicates compression option for data & indexes
--| cast(0x800000000 AS BIGINT)  --Store FILESTREAM data on its own filegroup at subscriber
--| cast(0x1000000000 AS BIGINT) --Converts CLR UDTs larger than 8000 bytes to varbinary(max)
--| cast(0x2000000000 AS BIGINT) --Converts hierarchyid to varbinary(max)
--| cast(0x4000000000 AS BIGINT) --Replicates filtered indexes
--| cast(0x8000000000 AS BIGINT) --Converts geography, geometry to varbinary(max)
--| cast(0x10000000000 AS BIGINT) --Replicates geography, geometry indexes
--| cast(0x20000000000 AS BIGINT) --Replicates SPARSE attribute
AS BINARY(8)) as Schema_Option

 

Plugging in that new value and executing the sp_addarticle script created the new article with all the old settings and now included replicating compression options.

 

Download the script Replication Schema Options.sql give them a try yourself and let me know if they help.

 

 

Monday
Aug302010

CLR Errors In Replication

Did you know that .NET components are used in replication? I sure didn't until I tried to add a new publication and perform a snapshot.

I had not troubles creating the publication or adding the subscribers but when I attempted to run the snapshot I got the following error:

Source: Replication
Exception Type: Microsoft.SqlServer.Replication.ReplicationAgentSqlException
Exception Message: Failed to initialize the Common Language Runtime (CLR) v2.0.50727 with HRESULT 0x800

Not a whole lot to go on there. There happens to be a DMV which will provide you with the status of CLR in SQL.

select * from sys.dm_clr_properties

I was expecting to see

name    value
-----------------------

directory    C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\
version    v2.0.50727
state    CLR is initialized

but instead I saw

name    value
-----------------------

directory  
version   
state    CLR initialization permanently failed

 

Time to open a support case with Microsoft.

After running through some troubleshooting steps with the MS engineers, including verifying the .NET install was good (see http://blogs.msdn.com/b/astebner/archive/2008/10/13/8999004.aspx) the decision was ultimately made to restart SQL to see if this resolved the problem.

After the restart everything was running smoothly and there were no issues performing snapshots. Sadly the restart eliminated any chance of getting a root cause for the problem. I've got a filtered dump setup to try and capture something in the event that it happens again. I kind of hope that it does just so we can find out exactly what the cause was.

Here are a couple of interesting notes that I found from this:

  • .NET Framework 3.5 is just a wrapper for .NET 2.0
  • You can log all .NET events to disk (see http://blogs.msdn.com/b/junfeng/archive/2004/02/14/72912.aspx)
  • In the event SQL encounters a .NET/CLR error then it will fail and set a flag internally. That flag will not be reset unless the SQL Instance is restarted and so any CLR functionality will fail
  • You want to have your SQL Instance configured to write dumps to a drive with more free disk space than you have memory on your server. By default dumps will be written to the log directory. You can change this by opening "Error and Usage Settings" from the start menu, going to options and changing the error reporting location. Note, you have to restart SQL for this to take effect as this information is read in at startup

 

Tuesday
Mar092010

Reinitializing Replication For A Single Subscriber

Interesting thing happened today, but never mind that. We had a server go down recently in our staging environment, wasn't any big deal, we had to have vendor support come out and replace the motherboard. It was stage, not a big deal. The thing was, the server was down for a couple of days. Actually it was down for a total of 4 days.

The server in question is a SQL 2008 subscriber to several published tables on the staging OLTP SQL 2008 Instance (using push subscriptions). Sadly, not thinking of this ahead of time, we neglected to adjust the maximum distribution retention period for transactional replication (which sits at 72 hours). Suffice to say, the server came back online, we restarted the distribution agents we got the dreaded "subscription marked as inactive" error requiring us to have to reinitialize all of the subscriptions to that server.

"There goes my afternoon" thought I. Not being a fan of the replication monitor in 2008 I dreaded having to work with it to go through and reinitialize each and every publication for that subscriber, especially given that there are around 80 different articles. There must be a better way I thought...and lo, there is!

Books online talks about sp_reinitsubscription and passing parameters for the publication, article, subscriber, destination database, for schema change, publisher, ignore distributor failover and invalidate snapshot.

What BOL is not clear about it that you can pass in just a subscriber, that's it, nothing else.

exec sp_reinitsubscription @subscriber = '<sql instance>';

Running this reinitialized all of the publications within the current database for that one subscriber. Very quick, very easy. From there I just had to kickstart the snapshot agents and we were back in business (sp_start_job in this case ftw).