Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Purge Database

...

In case the replication is running on the main site, and the database is set up for the purge feature, then, on the export stage, Replicator generates purge requests for the satellites where the purge is enabled.

The main site is never purged, and, therefore, satellite sites do not export purge requests. The main site is the only site that can export purge requests and send them to other sites (satellites). 

Replicator runs a separate replication phase called Purge database. This phase runs right after the import phase.  When the purge database is in progress, you can see in Replicator Manager what tables are purged and how many rows are deleted from each table:

...

Technical Details

Purge requests are exported at the very end of the procedure that exports data to a site. Replicator walks through the export tasks for a site once again after tasks export. It handles every table task where the new DataPurgedAfterDays or PKPurgeQuery field of the repl_ExportTasks view is set to a value other than null.

Those two fields suggest two different ways how you can to specify data to be purged from the database of a satellite site. The field DataPurgedAfterDays enables you to specify for a table a the number of days during which irrelevant data are stored on a satellite site till until the data are deleted. The field PKPurgeQuery enables you to specify your own purge rule in the form of a SQL query. A purge rule for a table is a SELECT query that returns a set of primary key values.

...

Let’s consider the meaning of the DataPurgedAfterDays field in detailsdetail.

After a table row no longer belongs to the result of the restricting query (PKRestrictionQuery, the partial replication) for a site, the value of the field LastExportTime of the distrib-table for this row and site is not updated (until the row belongs to the restricting query again). From this moment, the table row becomes irrelevant to that site. After some time, the difference in days between the value of LastExportTime and the current date exceeds the number of days specified in DataPurgedAfterDays. And that row must be deleted from the satellite site, and the corresponding row must be deleted from the distrib-table.

Example. Replicator The replicator is exporting data for site 50, DataPurgedAfterDays = 180 for the PW001P01 table. Replicator The replicator will determine rows to be deleted with the following query:

...

Similar to the first query, Replicator saves the result of this query into #_PW001P01 as well.

At the end of the current step, the Replicator checks the temporary table. If #_PW001P01 is not empty, Replicator saves its content to the PW001P01.da3 file. Such a file is a purge request. All formed purge requests (*.da3 files) are packed to a *.cab archive and sent to a certain satellite site where table rows with the corresponding values of the primary key will be deleted. Besides, Replicator cleans up the distrib-table:

Code Block
languagesql
DELETE FROM dbo.repl_Distrib_PW001P01

FROM dbo.repl_Distrib_PW001P01 AS dsb

INNER JOIN #pks_PW001P01 AS tmp

       ON dsb.PIN = tmp.PIN

WHERE dsb.SiteNr = Destination_Site_Number

Use Cases

Since the purge feature is an extension of the partial replication, the DataPurgedAfterDays, PKPurgeQuery and PKRestrictionQuery fields are related. Therefore, it is reasonable to store all these three fields in the same table (repl_Task or another one).

Below you can find two ways of how the DataPurgedAfterDays and PKPurgeQuery fields may be added to the repl_ExportTasks view.

Case A

Let us suggest using the same restricting queries for different sites of one client, then PKRestrictionQuery is just a field of the repl_Task table and a reference to this field is used in the repl_ExportTasks view. In this case, we could just add DataPurgedAfterDays and PKPurgeQuery fields to the repl_Task table and to the repl_ExportTasks view:

Code Block
ALTER TABLE dbo.repl_Task ADD DataPurgedAfterDays int NULL;

ALTER TABLE dbo.repl_Task ADD PurgeQuery varchar(8000);





ALTER VIEW [dbo].[repl_ExportTasks]

AS

SELECT …,

      DataPurgedAfterDays = CASE WHEN repl_Channel.IsPartial = 1 AND repl_Site.IsPurged = 1 THEN repl_Task.DataPurgedAfterDays ELSE NULL END,

      PKPurgeQuery = CASE WHEN repl_Channel.IsPartial = 1 AND repl_Site.IsPurged = 1 THEN repl_Task.PKPurgeQuery ELSE NULL END

FROM …

WHERE …


Case B

Let us suggest having a separate table with restricting queries that establishes relation to channels and tables. Such a table may be specified as:

Code Block
CREATE TABLE dbo.repl_Restrictions (

      ChannelNr int NOT NULL,

      TaskNr int NOT NULL,

      PKRestrictionQuery varchar (8000),

      CONSTRAINT PK_repl_Restrictions PRIMARY KEY (ChannelNr, TaskNr)

)

In this case, PKRestrictionQuery can be added to the repl_ExportTasks view in the following way:

code
ALTER VIEW [dbo].[repl_ExportTasks]

AS

SELECT …,

      PKRestrictionQuery = CASE WHEN repl_Channel.IsPartial = 1 THEN repl_Restrictions.PKRestrictionQuery ELSE NULL END

FROM …

LEFT OUTER JOIN

      dbo.repl_Restrictions ON repl_Task.TaskNr = repl_Restrictions.TaskNr AND repl_Channel.ChannelNr = repl_Restrictions.ChannelNr

WHERE …

Then, we could add the fields DataPurgedAfterDays and PKPurgeQuery to the repl_Restrictions table and references to them to the repl_ExportTasks view:

ALTER TABLE dbo.repl_Restrictions ADD DataPurgedAfterDays int;

ALTER TABLE dbo.repl_Restrictions ADD PurgeQuery varchar(8000);



ALTER VIEW [dbo].[repl_ExportTasks]

AS

SELECT …,

      DataPurgedAfterDays = CASE WHEN repl_Site.IsPurged = 1 THEN repl_Restrictions.DataPurgedAfterDays ELSE NULL END,

      PKPurgeQuery = CASE WHEN repl_Site.IsPurged = 1 THEN repl_Restrictions.PKPurgeQuery ELSE NULL END

FROM …

WHERE …

Housekeeping

Replicator enables you to benefit from the purge database functionality. For this, you have to set up the database for the purge feature. First, run APM and navigate to Setup > Adonis Replicator. Now, proceed as described below:

  1. Make sure you are on the Site Properties tab. Within the grid, in the Is Purge column, select the checkbox(es) for the site(s) for which you want to purge the database.

  2. Switch to the Replication Tasks tab under Views -1 to define the number of days to apply purging and PK Purge Query:

    • In the Data Purged After Days column, enter the number of days starting from today for purging to be applied.

    • In the PK Purge Query column, double-click the cell where a purge query is to be added. When the Browse button appears, click it to open the SQL Editor dialogdialogue:

      Image RemovedImage Added


      In the SQL Editor dialog dialogue, enter the SQL query or click Load to define the directory to the statement. Click Save to... to specify the save directory on your computer or Save&Close to apply the changes in the editor. 

...