Thu, Jul
0 New Articles

Practical RPG: Encapsulating Your Data with Extension Files, Part 1

  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Extension files allow you to extend the life of your database without having to completely re-engineer it.

As business programmers, we spend most of our time working with the database, one way or another. Unless we're lucky enough to be in on the ground floor of a new installation of a modern package, chances are that database will require modifications to support changing business requirements. Even if it's an entirely custom application suite built specifically for your business model, the database probably has become a bit long in the tooth and needs to be updated to take advantage of newer features. Rafael Victoria-Pereira has written several excellent articles on database modernization, and I invite you to spend some time with them. Today, though, I'm going to talk about extending your existing IBM i database without modernizing it.

But Isn't That Wrong?

Wrong is a strong word. How about "imperfect"? In a perfect world, we'd love to completely modernize our applications, but my very first mentor drilled one simple concept into my head: Don't let the perfect be the enemy of the good. That means sometimes making a conscious decision to take a less-than-perfect approach in order to provide a significant benefit to the business. Because remember, at the end of the day, we can best measure ourselves as business programmers not by how leading-edge our code is, but by how much better the business runs.

That's not to say the two can't coincide! We should try to find ways to incorporate modern techniques into our business solutions whenever possible. However, there are times when a more prosaic approach is required. The situation I have in mind is one we've all encountered: adding one or more data points to an existing file.

I'm in the process of doing this very thing for a number of files in our database. The primary files are part of a packaged ERP system, and much of the code is quite old. The beauty of our platform is that code can run literally for decades, migrating from hardware to hardware without changes. The downside of our platform is much the same; those old programs resist change, and things that were absolutely a necessity 10 (or 20 or 30) years ago now prevent us from moving forward.

The Genesis of the Extension File

Let's take a simple case: the customer master file. Customer processing requirements grow over time, whether through new regulations, new technology requirements, or just plain new business. One very simple case I encountered was the county. We needed to be able to identify the county for some tax reporting functions, and while our customer database file had traditional city, state, and postal code, even country code, it didn't have a place for county. (In case you're wondering, there is no foolproof way to go from ZIP code to county. I tried. ZIP codes span counties. In fact, several ZIP codes actually cross state boundaries. If you don't believe me, check out ZIP code 97635.) In any case, the decision was made to include the county as part of the customer definition. At that point, a decision had to be made: modify the customer master or extend?

We all know the difficulties encountered when modifying a highly used master file. One of the biggest benefits of the IBM midrange is the concept of the externally described file, but it did not come without a price: changing a database file has historically been the most difficult task in any development effort. If you change a file, you have to recompile every program that uses that file or else you encounter the deadly CPF4131, format level check. And I'm going to draw a line in the sand here; I don't even support the LVLCHK(*NO) workaround. It works in a subset of cases when used with extreme caution, but to me the potential for catastrophe far outweighs the short-term benefits.

In my case, the developers used as forward-thinking a technique as possible for the time: an extension file. An extension file is a file with exactly the same primary key as the master file it is extending. The record contains at a minimum the unqiue key fields from the master file and fields for the new data points that you need. When designing an extension file, you must address two main issues:

  • Do you always create an extension record even if none of the fields are populated?
  • Do you include audit fields in the extension file, or just piggyback on the extended record?

The answers aren’t critical to the general idea of extension files, but you should still consider them and then apply the answers you decide upon consistently throughout your organization. My personal preference is to always have an extension record but with no audit fields. For example, if you use a last-changed timestamp and user ID on your files, then have those fields on the master record only and update them even if you only update a field in the extension record. That's because I consider the fields in the extension file just to be more columns for the master record.

Master Files with No Unique Key

“But what if the file has no primary key?” you might ask. Excellent question! And my response is that you must absolutely get a unique key on that file. With today’s databases, you can even let the system generate the value for that field itself, although I’m just as happy with using a data area or some other global entity to hold the last key value. I can tell you though that I’m in the middle of exactly that situation. I'm currently dealing with a history file whose only “key” is the date and time the record was written, along with a small 3-digit suffix that I suppose was always unique back when the original code was written. For various reasons, though, that suffix is no longer unique, so I end up with transactions with literally no unique key.

So instead, I'm using a multi-step approach. First, I created an extension file. The primary key on the extension file uses a large numeric ID field. My file can contain roughly a billion records. For files of that size, I'd go with at least a 10- or 11-digit numeric ID field (this allows for plenty of growth). Then I do something really, really bad. Close your eyes while you read this part.

Seriously, it's a real kludge. Whenever I write a record to the history file, I use the relative record number as the key for the extension file. It's functional. When I'm getting data via SQL (something we'll talk about a lot more in future installments), I can do something like this:


FLDA and FLDB are fields from the original MYHIST file, where XFLDC is a field from the extension file MYHISTX. The JOIN uses the relative record number of the original file to join to the ID field (XID) of the extension file. As I said, it's not pretty. And if we ever reorganize the main file, we' re in deep trouble. This isn't anything new; in fact, it's rather ancient and, as noted, potentially very fragile. The only reason I can get away with it right now is that the main file is a transaction history file where records are not typically deleted.

Things can't stay this way. The master file needs a true unique key. So my next goal is to follow a simple strategy:

  1. Add a new index field to the original history file.
  2. Run a program that will populate that field.
  3. Update existing extension records to the new ID field.
  4. Change programs using the extension file to use the new ID field instead of RRN.

When I say simple, I mean straightforward but not necessarily easy. Since the primary file is used by literally hundreds of programs, I'll need to schedule a time when the system is completely down to perform some of these steps. But that's exactly the reason we want to use extension files; once I've made this modification, I won't have to make any more changes to the original file. Any changes will be to the extension file, and in a subsequent article we'll talk about how to minimize the impact of changes to that file as well.

Please stay tuned!

Joe Pluta

Joe Pluta is the founder and chief architect of Pluta Brothers Design, Inc. He has been extending the IBM midrange since the days of the IBM System/3. Joe uses WebSphere extensively, especially as the base for PSC/400, the only product that can move your legacy systems to the Web using simple green-screen commands. He has written several books, including Developing Web 2.0 Applications with EGL for IBM i, E-Deployment: The Fastest Path to the Web, Eclipse: Step by Step, and WDSC: Step by Step. Joe performs onsite mentoring and speaks at user groups around the country. You can reach him at This email address is being protected from spambots. You need JavaScript enabled to view it..

MC Press books written by Joe Pluta available now on the MC Press Bookstore.

Developing Web 2.0 Applications with EGL for IBM i Developing Web 2.0 Applications with EGL for IBM i
Joe Pluta introduces you to EGL Rich UI and IBM’s Rational Developer for the IBM i platform.
List Price $39.95

Now On Sale

WDSC: Step by Step WDSC: Step by Step
Discover incredibly powerful WDSC with this easy-to-understand yet thorough introduction.
List Price $74.95

Now On Sale

Eclipse: Step by Step Eclipse: Step by Step
Quickly get up to speed and productivity using Eclipse.
List Price $59.00

Now On Sale



Support MC Press Online


Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: