Case Study: Refreshing Db2 Test Environments with DSN1COPY

Modified on Mon, 30 Nov at 4:16 PM

Test data that is months old gives test results nobody can trust. Refreshing it with UNLOAD and LOAD takes days, and DSN1COPY, the fastest way to copy Db2 data, is too complex to set up for a whole environment. North Carolina Farm Bureau Mutual Insurance Company cut its test data refresh from days to hours with TUC, which automates DSN1COPY from the first matched dataset to the last copied object.

Results

From Days to Hours — Moving production data to a test environment used to take three to four days with UNLOAD and LOAD. With DSN1COPY automated by TUC, it takes hours.

Fresh Test Data — Test data used to be refreshed once or twice a year. Now that a refresh takes hours, the test environments can be refreshed as often as the developers need, and their tests reflect how the applications behave in production.

The Largest Objects in the Window — TUC copies in parallel, so even the largest objects are moved within the available window.

Consistent Environments — TUC finds every difference between the production and test definitions of an object and generates the DDL that corrects it.

Paid Back in Year One — The whole TUC implementation, which also automated REORG and RUNSTATS, returned its full cost in its first year, counting only the staff time it saved.

The Site

The customer runs 15 Db2 subsystems in four LPARs on an IBM z14 ZR1. Three subsystems serve production and twelve serve testing, so production data has to reach twelve test environments. A small database team supports all of them.

The Problem

The team built its test data refresh around UNLOAD and LOAD. Every refresh took three to four days and consumed DBA time and a great deal of CPU. Because of that cost, the test data was refreshed only once or twice a year.

Developers were therefore testing against data that could be many months behind production. A test that passes on old data may not show how the application will perform on the data it will meet in production. The team saw this as a risk it had to remove.

The team knew that DSN1COPY is the fastest way to move Db2 data from one subsystem to another. It copies the data at the dataset level, page by page, with no SQL, no sort and no index rebuild, and is typically about ten times faster than unloading and reloading. But DSN1COPY was so complex to use that the team rarely used it.

Why DSN1COPY Is Hard to Use

DSN1COPY works below Db2. It does not know which table a dataset holds or whether the target can receive it. Everything Db2 would normally check, the DBA has to check instead:

  • Pairing datasets with objects. Each source dataset, whether an image copy or a live dataset, has to be paired with its target tablespace, index or partition. Between production and test, database names, tablespace names and index names often differ, and implicitly created objects have generated names.
  • Translating internal ids. Each object has internal ids (DBID, PSID and OBID) that belong to its own subsystem. The same table has different ids in production and in test. DSN1COPY must be given the translation for every pair, or the copied data cannot be read.
  • Identical physical attributes. The source and target must have the same physical structure: page size, segment size, dataset size, number of partitions, row format, compression, number of columns and record length, among others. If they differ, the result of the copy is unpredictable.
  • Building the job. Each object needs its own step, with DD statements for the source and the target, plus steps to stop the objects, to delete and redefine the target dataset, and to start the objects again. A complete test environment produces a job with hundreds of steps that is hard to generate, to review and to restart.
  • Space. The target dataset must be large enough for the source data, or the copy fails partway through.
  • Partition boundaries. If the limit keys of a partitioned tablespace differ between source and target, the copy succeeds but some rows sit in partitions whose boundaries no longer match them, and queries do not find them.

What the Customer Did

The customer introduced TUC in two phases, starting in early 2021. The first phase automated REORG and RUNSTATS. The second phase replaced the UNLOAD and LOAD refresh with DSN1COPY, driven by TUC.

The customer's databases did not all follow the same naming conventions, so the matching of production datasets to test objects had to allow for them. TUC support adapted the setup to the site's conventions during the implementation, and the project was completed in May 2022.

TUC now finds the objects whose definitions differ between production and test, and generates the DDL that brings them back in line. The copy runs as parallel jobs, so the largest objects are moved within the available window. A refresh that took three to four days takes hours, and the developers work with current data.

How TUC Automates DSN1COPY

TUC takes over every task that makes DSN1COPY hard. The DBA names the source and the target; TUC works out the rest, checks it and runs it.

Matching Every Dataset to Its Target

The source can be the live datasets of the source objects or their image copies. When the source is a database name, TUC derives the dataset names of its objects by itself. When the source is a pattern of image copy names, TUC uses the most recent copy of each object. The source can be on the same subsystem or on another one at a remote location.

TUC pairs each source dataset with its target object in several steps, from the fastest to the most thorough:

  • By tablespace or indexspace name, when the names are the same.
  • By table name or index name, when the space names differ. This also covers implicitly created objects.
  • By the table an index belongs to, when the table has a single index.
  • By the ending of the indexspace names of all the indexes of the table, when the table has several indexes.

The result is a list of every source dataset with its target object and partition, and the method that matched them. Objects that could not be matched are listed with the reason, so nothing is missed silently.

Checking That Source and Target Are Compatible

Before anything is copied, TUC compares the physical attributes of each source and target pair. For tablespaces and tables it compares the page size, segment size, dataset size, number of partitions, number of datasets, page numbering, row format, compression, number of tables, maximum rows per page, number of columns, record length, encoding scheme, and edit and validation procedures. For indexes it compares the number of key columns, uniqueness, clustering, padding, the COPY attribute and the piece size.

An object with a difference is excluded from the copy automatically, so DSN1COPY never runs on an incompatible pair. TUC then offers three ways to bring it back in:

  • Alter the target. Many differences can be corrected in place: the dataset size and compression of a tablespace, the segment size of a partitioned tablespace, page numbering from absolute to relative, and the COPY, padding, clustering, compression and piece size attributes of an index. TUC generates the ALTER statements, with the REORG or REBUILD that puts the change into effect.
  • Recreate the target. TUC generates the DDL to recreate the target object with the definition of the source.
  • Move it the slow way. TUC prepares the incompatible tables for unload and reload, either by loading them directly from the source or by unloading them and loading the target, so that they are still part of the refresh.

For tablespaces partitioned by range, TUC also compares the limit keys of every partition. When they differ, for example because the source was rebalanced by a REORG, TUC generates the ALTER statements that adjust the target partitions before the copy, so that every row stays reachable.

Translating Internal Ids

For every matched pair, TUC builds the translation of the database, page set and table ids from the source subsystem to the target, and passes it to DSN1COPY. The DBA never has to look up an id. TUC also keeps the ids of objects that were dropped, so that a dropped object can be recreated and its data recovered from an old image copy.

Copying Only What Changed

Many tables do not change between two refreshes. TUC reads the time of the last data change from the Db2 real-time statistics, on the source and on the target, and compares it with the time of the last successful copy. A table that has not changed is left out, and so are its indexes. For a partitioned table, only the partitions that changed are copied, and a non-partitioned index is copied when at least one partition of its table changed. In large tables it is common that only a few partitions are active, so the saving is large.

Copying only what changed is what makes it practical to refresh test environments often. When a specific object must be copied regardless, the check can be switched off.

One Step for the Whole Environment

Instead of a job with a step per object, TUC runs the whole copy in a single step. For each object it executes, in order: stop the source and target objects, delete and redefine the target dataset, run DSN1COPY with the id translation, and start the objects again with their original access. The datasets are allocated as each object is copied and freed right after it, so the job needs no DD statements and the number of objects in one step has no limit.

No Space Failures

Before each copy, TUC adds candidate volumes to the target dataset, so that it can grow as far as the data needs. After a successful copy, TUC removes the candidate volumes that were not used, so the catalog does not fill with entries that serve no purpose. This is the same technique Db2 itself uses when it extends a dataset, and it takes very little time.

Restart Without Editing the Job

A failure on one object does not stop the copy. TUC still starts the objects again, so nothing is left stopped, and moves on to the next object. Each object is recorded as soon as it is copied. When the job is restarted, the objects already copied are skipped and only the failed ones are copied again, from the same consistent starting point. Nothing in the job has to be edited.

The usual flow is therefore one run that copies everything it can, a review of the objects that failed, a fix, and a restart that copies only what is left. The status of every object can be followed while the copy runs.

Refreshing Many Databases in One Run

For a complete environment, the DBA selects in the TUC policy the tablespaces to refresh, and one run of the TUC enforce process prepares the whole refresh. It places an order for each database that has at least one object to copy and produces consolidated reports:

  • The objects matched, the objects that could not be matched, and the attribute differences, across all databases at once.
  • The total space of the source and the target, and the difference between them, so the DBA can confirm that the target storage groups have enough free space and add volumes before the copy starts.

The orders are then processed automatically, the largest first. Profiles split into parts are copied by parallel jobs, and the number of concurrent orders can be limited to fit the window. TUC tracks the progress of the entire refresh and, at the end, reports that every object was copied or lists the ones that were not.

Other Uses

The same automation serves more than test environments. It can recover a dropped object from an image copy after the object has been recreated with new internal ids. It can also copy a periodic backup, such as a monthly image copy, into side tables, so the data can be examined as it was at that point in time.

Without and With TUC

Task Without TUC With TUC
Pair datasets with objects Looked up one by one, harder when names differ. Matched automatically, by name, table or index.
Internal ids Queried in both catalogs and coded for every object. Translation built for every pair.
Compatibility Checked object by object, or found when the data is unreadable. Compared before the copy; incompatible objects excluded, with ALTER or DDL to fix them.
Unchanged data Copied again every time. Skipped, down to the partition.
The job Hundreds of steps and DD statements. One step, no DD statements.
Space Estimated per dataset; a copy can run out partway. Total space reported in advance; candidate volumes added and removed.
Restart Edit the job to remove what was already copied. Resubmit as is; copied objects are skipped.
A whole environment Database by database. One run, parallel jobs, largest first, tracked to the end.

In short — TUC turns DSN1COPY from a tool the customer rarely dared to use into its everyday way of refreshing test data. A refresh that took three to four days now takes hours, so developers test against current production data instead of data that is months old.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article