The relative record number (RRN) is how you identify and access a record in a Relative Record Data Set (RRDS). The first record (or slot) is RRN 1, the second is RRN 2, and so on. There is no key and no byte-offset address like RBA; instead, the record is identified by its slot number. You set the RRN (e.g. in the COBOL RELATIVE KEY) and then READ, WRITE, REWRITE, or DELETE to operate on that slot. RRDS is designed for direct addressing by position: when you know the RRN, you can go straight to that record without scanning. This page explains what an RRN is, how it differs from RBA and key, how fixed-length and variable-length RRDS use RRN, and how to use RRN for direct access and slot reuse.
The relative record number is an ordinal: 1 for the first record (or slot), 2 for the second, 3 for the third, and so on up to the maximum number of slots or records in the dataset. It is "relative" because it is relative to the start of the dataset—the first position is 1, not 0 in the typical convention. So RRN is a slot index or record index. When you define an RRDS you specify (implicitly or explicitly) how many slots or how much space is available; each slot has a fixed RRN. In a fixed-length RRDS, the file is divided into fixed-size slots; slot 1 has RRN 1, slot 2 has RRN 2, and so on. A slot may be empty (no record) or contain one record. In a variable-length RRDS, there are no fixed slots on disk in the same way, but each record still has an RRN that identifies its logical position, and the index component maintains the mapping from RRN to record.
In an ESDS you address a record by its RBA (byte offset). In a KSDS you address by key. In an RRDS you address by RRN. So the three main addressing methods in VSAM are: key (KSDS), RBA (ESDS, and sometimes KSDS for positioning), and RRN (RRDS). RBA is a byte count—e.g. record at offset 4096. RRN is a slot count—e.g. record in slot 5. So RRN is simpler when you think in "record 1, record 2, record 3" and when the record length is fixed (each slot is the same size). Key is content-based (you search by the value in the key field); RRN is position-based (you specify the slot number).
| Type | Address | Notes |
|---|---|---|
| KSDS | Key | Primary key; index maps key to record |
| ESDS | RBA | Byte offset from start of data component |
| RRDS | RRN | Slot number (1, 2, 3, …); direct slot addressing |
In a fixed-length RRDS the data component is divided into slots. Each slot holds exactly one record of fixed length (e.g. 80 bytes). Slot 1 has RRN 1, slot 2 has RRN 2, and so on. When you WRITE a record, you specify the RRN (e.g. 5); the record is placed in slot 5. When you READ, you set the RRN to 5 and READ; you get the record in slot 5, or a "record not found" if the slot is empty. When you DELETE, you set the RRN and DELETE; the slot is freed. If the cluster is defined with REUSE, that slot can be used again for a new WRITE at the same RRN. So the RRN is permanent for the slot: slot 5 is always RRN 5, whether it is empty or full. The RRN does not change when you delete or insert; only the content of the slot changes.
In a variable-length RRDS (VRRDS) records can have different lengths. There is an index component that maps RRN to the record (similar in spirit to KSDS but keyed by RRN instead of key). Each record has a unique RRN; records are in ascending RRN order. When you delete a record, that RRN can be reused for a new record if the cluster has REUSE. So in VRRDS the RRN is the logical record number, and the index maintains the mapping from RRN to the actual record location. Direct access is still by RRN: set the RRN, READ or WRITE or DELETE.
| Operation | Description |
|---|---|
| READ | Set RRN (e.g. RELATIVE KEY), READ; returns record in that slot or record-not-found if empty. |
| WRITE | Set RRN, WRITE; places record in that slot (fixed RRDS) or assigns that RRN (variable RRDS). |
| REWRITE | After READ, REWRITE updates the record in the same slot; RRN unchanged. |
| DELETE | Set RRN, DELETE; frees the slot (fixed) or removes the record (variable); slot can be reused with REUSE. |
In COBOL you define the file with ORGANIZATION IS RELATIVE. The RELATIVE KEY is the data-name that holds the RRN. For random access you move the desired RRN to the RELATIVE KEY and then READ, WRITE, REWRITE, or DELETE. For sequential access you open the file and use READ NEXT (or equivalent); the order is by ascending RRN. Example:
1234567891011SELECT RRDSFILE ASSIGN TO RRDSFILE ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS WS-RRN FILE STATUS IS WS-STATUS. ... 01 WS-RRN PIC 9(8) COMP. ... MOVE 5 TO WS-RRN READ RRDSFILE *> Reads the record in slot (RRN) 5
The RELATIVE KEY must be large enough to hold the maximum RRN (typically a binary or numeric field). The value you put in it is the RRN of the slot or record you want to access.
The valid RRN values are from 1 to the maximum number of slots (fixed-length RRDS) or the maximum number of records (variable-length RRDS). That maximum is determined when you define the cluster—by specifying the number of records or the space (e.g. CYLINDERS or TRACKS) and the record size, from which the system can derive the number of slots. You cannot read or write at RRN 0 in the typical 1-based convention; 0 may be used as "not set" or invalid in your program. Check your documentation for the exact lower bound (1 or 0) and upper bound.
Because each RRN maps directly to a slot (fixed) or to an index entry (variable), direct access by RRN is fast. There is no key search and no sequential scan. You supply the RRN and the access method positions to that slot or uses the index to find the record. So RRDS is well suited when you have a natural "record number" or slot id (e.g. employee number 1–10000, or a hash that maps to a slot) and you want to read or update by that number without maintaining a key in the record.
Use RRDS when your application thinks in terms of slot numbers or record numbers and when direct access by that number is the main pattern. For example, a table where the row number (1 to N) is the natural identifier, or a cache where a numeric id maps to a slot. If you need key-based access (e.g. find by customer ID), KSDS is usually better. If you need append-only with stable byte addresses (RBA), ESDS is better. RRDS fits the niche of fixed or variable-length records with direct addressing by position number.
Imagine a row of mailboxes numbered 1, 2, 3, 4, 5. The number on the mailbox is the RRN. To get the mail in mailbox 3, you go to mailbox 3—you don't need to look at the mail to find it (that would be like a key). You just know "number 3." If the mailbox is empty, you can put something in it. If you take the mail out (delete), the mailbox is still number 3 and someone can put new mail in. RRN is that mailbox number.
1. What does RRN represent in an RRDS?
2. How do you read a specific record in an RRDS?
3. In a fixed-length RRDS with REUSE, what happens when you DELETE the record at RRN 5?