VSAM RRDS Relative Record Number (RRN)

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.

What Is a Relative Record Number?

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.

RRN vs RBA vs Key

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).

Primary addressing by VSAM dataset type
TypeAddressNotes
KSDSKeyPrimary key; index maps key to record
ESDSRBAByte offset from start of data component
RRDSRRNSlot number (1, 2, 3, …); direct slot addressing

Fixed-Length RRDS: Slots and RRN

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.

Variable-Length RRDS and RRN

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.

Operations Using RRN

RRDS operations by RRN
OperationDescription
READSet RRN (e.g. RELATIVE KEY), READ; returns record in that slot or record-not-found if empty.
WRITESet RRN, WRITE; places record in that slot (fixed RRDS) or assigns that RRN (variable RRDS).
REWRITEAfter READ, REWRITE updates the record in the same slot; RRN unchanged.
DELETESet RRN, DELETE; frees the slot (fixed) or removes the record (variable); slot can be reused with REUSE.

COBOL: RELATIVE KEY

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:

cobol
1
2
3
4
5
6
7
8
9
10
11
SELECT 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.

Valid RRN Range

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.

Direct Access and Performance

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.

When to Use RRN (RRDS)

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.

Key Takeaways

  • RRN (relative record number) is the slot or record number in an RRDS: 1 for the first, 2 for the second, and so on.
  • You access by RRN: set the RELATIVE KEY (or equivalent) to the RRN and use READ, WRITE, REWRITE, or DELETE.
  • In fixed-length RRDS each slot has a fixed RRN; the slot can be empty or full; with REUSE, a deleted slot can be reused for the same RRN.
  • In variable-length RRDS the index maps RRN to record; RRN is the logical record number.
  • RRN is position-based (slot number); it differs from RBA (byte offset) and key (content-based).

Explain Like I'm Five

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.

Test Your Knowledge

Test Your Knowledge

1. What does RRN represent in an RRDS?

  • Byte offset
  • Key value
  • Slot number (position) for the record
  • Control interval number

2. How do you read a specific record in an RRDS?

  • By key
  • By RBA
  • By RRN (set RELATIVE KEY to the RRN, then READ)
  • By CI number

3. In a fixed-length RRDS with REUSE, what happens when you DELETE the record at RRN 5?

  • RRN 5 is removed and all RRNs shift
  • Slot 5 becomes empty and can be reused for a new WRITE at RRN 5
  • The file shrinks
  • RRN 5 becomes 0
Published
Updated
Read time4 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: IBM z/OS 2.5 documentationSources: IBM DFSMS Access Method Services, z/OS VSAM documentationApplies to: z/OS 2.5