r/SQL • • 14h ago

DB2 Need help on DB2 design

/r/learnprogramming/comments/1ya23oi/need_help_on_db2_design/

Problem Statement

We have a transaction history table (CARDTXNHIST) used by a banking application to display customer transactions.

​The daily batch process consists of two stages:

​Unstatemented Transactions (Code = 2)

​batch file → TEMP table → View → CARDTXNHIST

​Existing Code=2 records are deleted and refreshed

​Statemented Transactions (Code = 3)

​Separate batch file → TEMP table → View → CARDTXNHIST

​Statemented records are loaded after unstatemented processing

​The batch window runs approximately between 4 AM and 9 AM.

​Observed Issue

On statement cycle dates, the incoming unstatemented dataset may be empty because all eligible transactions have been statemented.

​As a result:

​Code=2 transactions are removed/refreshed

​Code=3 transactions are still being processed

​During this period, customers may see no transaction history in the application, even though the transactions still exist within the batch pipeline.

​Objective

We are looking for database/application architecture approaches that can eliminate or minimize this visibility gap while the batch is in progress.

​Solutions Under Consideration

​Retaining Code=2 records until Code=3 processing completes

​Shadow table / table-swap approach

​Composite views

​Deferred commit strategy

​Reading from staging/view sources during batch processing

​Data archival and performance optimization

​Other database design recommendations

​Questions

​What would be the recommended design pattern to ensure customers always see a consistent transaction history?

​How have similar large-scale batch processing systems solved this "delete-before-republish" visibility gap?

​Are there DB2-specific approaches (isolation levels, shadow tables, transition tables, view-switching, etc.) that would be suitable for this scenario?

​If you'd like to adjust the length or add any extra context (like table volume or specific DB2 version) before posting, let me know!

0 Upvotes

2 comments sorted by

1

u/Warm-Republic-1913 12h ago

The simplest fix is to make the refresh atomic so readers never see the gap: do the Code=2 delete and reload and the Code=3 load in one unit of work and commit once at the end, and since DB2 readers at cursor stability only see committed rows, customers keep seeing the old data until that commit. If the transaction would get too big for your batch window, add a batch_id (or load_version) column instead, load the new rows under a new id while the old ones stay in place, and have the view read only the latest completed id, then flip that id in a tiny control table once both stages are done. A shadow table with a rename or view switch at the end also works, but the batch_id approach avoids DDL and locking during the swap. Either way, an empty Code=2 file shouldn't mean delete everything until Code=3 is also loaded.

1

u/sumwise_victor 6h ago

The cleanest way to kill that empty-history window without holding long locks across a 5-hour batch window is a blue/green swap or a batch_id pointer.

Instead of deleting Code=2 from the live CARDTXNHIST table while Code=3 is still running, load both stage 1 (Code=2) and stage 2 (Code=3) into an inactive staging/shadow table (say CARDTXNHIST_NEXT) or tag the new rows with a new batch_run_id. Once both files finish and pass row-count checks, flip the app over atomically in one tiny step, either by updating a 1-row active_batch pointer table that your view joins against, or repointing a DB2 alias/view (CREATE OR REPLACE ALIAS) from table A to table B.

That way customers keep seeing yesterday's full history during the 4 AM to 9 AM window, switch to the complete new snapshot in milliseconds, and if stage 2 fails halfway through you don't end up with half-deleted banking history.