Search code examples
sql-serverdatabase-replicationtransactional-replication

It is possible to alter SQL Server replication filter without delivering an entire publication snapshot?


I am constantly asked to change the filters on my companies SQL Server Transactional Publications which contain several hundred tables and roughly 400GBs of row data.

Each time I am required to alter a filter, I have to completely re-snapshot the entire publication and deliver it to the subscriber, a process which takes nearly an entire day to complete.

My question: It is possible to alter SQL Server replication filter without delivering an entire publication snapshot?


Solution

  • You have to drop the table (article) from the publication and re-add it with a new filter. The trick is that if you remove the subscription to the article before removing the article from the publication, you will not be required to deliver an entire snapshot for all article – but only for the single table (and it’s new filter).

    --Drop existing subscription:
    EXEC sp_dropsubscription
                    @publication='<pub_name',
                    @article='<article_name',
                    @subscriber='<sub_name',
                    @destination_db='<db_name>',
                    @ignore_distributor=0
    
    --Drop the article from the publication:
    EXEC sp_droparticle
                    @publication='<pub_name',
                    @article='<article_name',
                    @ignore_distributor=0,
                    @force_invalidate_snapshot=1
    

    Now, the easiest way to add the article back to the subscription is through the replication publication GUI, you can add the article, add the filter then click ok. When you run the snapshot job, it will only generate a snapshot for the single table. This is known as a mini-snapshot.

    If you want to manually re-add the article and its filter to the publication then you'll need to do the following to get it back into the subscription.

    --Re-add the subscription to the article.
    EXEC sp_addsubscription
                    @publication = @publication='<pub_name',
                    @article = @article='<article_name',
                    @subscriber =  @subscriber='<sub_name',
                    @destination_db='<db_name>',
                    @sync_type =  'automatic ',
                    @subscription_type = 'push',
                    @update_mode =  'read only'
    

    --You will now need to manually add any new columns to the destination table at the subscriber, re-run the snapshot agent which will run a mini-snapshot... then kick off the distributor.