How a secondary index is stored

Secondary index databases contain root segments only.

They are stored in a single VSAM KSDS if the key in the pointer segment is unique. If keys are not unique, an additional data set must be used (an ESDS) to store segments containing duplicate keys. (KSDS data sets do not allow duplicate keys.) Duplicate keys exist when, for example, a secondary index is used to retrieve courses based on student name. As shown in the following figure, several source segments could exist for each student.

Figure 1. Examples of source segments for each student
Three segments. First segment has a field with Math and a field with Adams. The second has a field with French and a field with Adams. The third has a field with Hist and a field with Adams.

Each pointer segment in a secondary index is stored in one logical record. A logical record containing a pointer segment is shown in the following figure.

Figure 2. Example of a logical record containing a pointer segment
The logical record contains a pointer field followed by the pointer segment, which contains a prefix and a data portion.

A HALDB secondary index record is shown in the following figure.

Figure 3. Secondary index entry for HALDB
The logical record contains a delete byte (1 byte) followed by an EPS-extended pointer set (28 bytes), then the key for the root of the target (1 to 255 bytes), and then the data (variable).

The format of the logical record is the same in both a KSDS and ESDS data set. The pointer field at the beginning of the logical record exists only when the key in the data portion of the segment is not unique. If keys are not unique, some pointer segments will contain duplicate keys. These pointer segments must be chained together, and this is done using the pointer field at the beginning of the logical record.

Pointer segments containing duplicate keys are stored in the ESDS in LIFO (last in, first out) sequence. When the first duplicate key segment is inserted, it is written to the ESDS, and the KSDS logical record containing the segment it is a duplicate of points to it. When the second duplicate is inserted, it is inserted into the ESDS in the next available location. The KSDS logical record is updated to point to the second duplicate. The effect of inserting duplicate pointer segments into the ESDS in LIFO sequence is that the original pointer segment (the one in the KSDS) is retrieved last. This retrieval sequence should not be a problem, because duplicates, by definition, have no special sequence.