Skip to content

DB2 for z/OS — Tablespaces, Indexes, Buffer Pools, Stored Procedures and Query Optimization

DodaTech Updated 2026-06-22 9 min read

In this tutorial, you'll learn about DB2 for z/OS. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

DB2 for z/OS is IBM's relational database management system for the Mainframe — it powers the world's largest Transaction processing systems at banks, airlines, insurance companies, and government agencies, processing tens of thousands of SQL statements per second with 99.999% availability.

What You'll Learn

DB2 tablespace types (simple, segmented, partitioned), index strategies (clustering, non-clustering), buffer pool tuning with VPAR, BIND and REBIND for plan management, IRLM deadlock detection, and EXPLAIN for query optimization.

Why It Matters

Mainframes handle 68% of the world's production IT workloads, and DB2 is the primary database driving those systems. Unlike distributed databases, DB2 for z/OS is designed for mixed workloads — thousands of concurrent online transactions running alongside massive batch jobs — without compromising performance or data integrity.

Durga Antivirus Pro uses DB2-style buffer pool management for caching threat signatures, ensuring frequently accessed patterns are served from memory. DodaZIP applies DB2 partitioned tablespace concepts when processing multi-volume compressed archives.

Real-World Use

A bank's core banking system stores 50 million customer accounts in a DB2 for z/OS database. During peak hours, 10,000 teller transactions, 5,000 ATM requests, and 2,000 online banking queries hit the database simultaneously. DB2 processes all of them while nightly batch jobs calculate interest and generate statements — all against the same data.

Learning Path

flowchart LR
  A["Mainframe Basics"] --> B["COBOL Programming"]
  B --> C["JCL & CICS"]
  C --> D["DB2 for z/OS
You are here"] D --> E["Advanced DB2 Tuning"] style D fill:#f90,color:#fff

DB2 Objects Hierarchy

flowchart TB
  SUBSYSTEM["DB2 Subsystem"] --> DB["Database"]
  DB --> TS["Tablespace"]
  TS --> TBL["Table"]
  TBL --> IX["Index"]
  TBL --> COL["Columns"]
  TBL --> VIEW["View"]
  TBL --> SYN["Synonym"]
  • DB2 Subsystem: An instance of DB2 running on z/OS
  • Database: Logical grouping of tablespaces and related objects
  • Tablespace: Physical storage container for table data
  • Table: Logical structure of rows and columns
  • Index: Ordered pointer structure for fast data access
  • View: Virtual table based on a SELECT query

Creating Tables and Indexes

Let's create a banking database with DB2 SQL:

CREATE DATABASE BANKING
    STOGROUP SYSDEFLT
    BUFFERPOOL BP1;

CREATE TABLESPACE ACCTSPACE
    IN BANKING
    USING STOGROUP SYSDEFLT
    PRIQTY 1000
    SECQTY 200
    SEGSIZE 32
    BUFFERPOOL BP1;

CREATE TABLE BANKING.ACCOUNTS (
    ACCT_ID      CHAR(10)     NOT NULL,
    CUST_NAME    VARCHAR(50)  NOT NULL,
    BALANCE      DECIMAL(15,2) NOT NULL,
    ACCT_TYPE    CHAR(2)      NOT NULL,
    OPEN_DATE    DATE         NOT NULL,
    LAST_TXN     TIMESTAMP,
    CONSTRAINT PK_ACCT PRIMARY KEY (ACCT_ID)
) IN BANKING.ACCTSPACE;

CREATE UNIQUE INDEX BANKING.IX_ACCT_ID
    ON BANKING.ACCOUNTS (ACCT_ID)
    CLUSTER
    BUFFERPOOL BP2
    CLOSE NO;

Expected behavior: Creates the BANKING database, the ACCTSPACE tablespace, the ACCOUNTS table with seven columns and a primary key, and a unique clustering index on ACCT_ID.

Tablespace Types

DB2 for z/OS supports three types of tablespaces:

Type Description Best For
Simple Multiple tables share one tablespace Small tables, low concurrency
Segmented Each segment stores one table's data Most OLTP workloads (default)
Partitioned Table data split across physical partitions Large tables, rolling windows

Segmented Tablespace

The most common type for OLTP workloads. Data is organized into segments of 4, 8, 16, or 32 pages. Each segment holds data from one table.

CREATE TABLESPAGE SEGTSP
    IN BANKING
    SEGSIZE 32
    BUFFERPOOL BP1;

Partitioned Tablespace

Used for very large tables. Data is divided across partitions based on a Partitioning key. Each partition can be managed independently.

CREATE TABLESPACE PARTSP
    IN BANKING
    PARTITION BY RANGE (ACCT_ID)
    (PARTITION 1 VALUES LESS THAN ('50000'),
     PARTITION 2 VALUES LESS THAN ('99999'));

Buffer Pools

Buffer pools are memory areas in the DB2 buffer manager that cache table and index data, reducing physical I/O.

Buffer Pool Page Size Typical Use
BP0 4 KB System catalog, default
BP1 4 KB OLTP tables
BP2 4 KB Indexes
BP3 8 KB Larger rows
BP4 16 KB Very large rows
BP5 32 KB Large objects
BP6-BP49 4 KB User-defined

VPAR (Virtual Pool) allows tablespaces and indexes within a single buffer pool to be managed independently, preventing one workload from monopolizing the pool.

SQL Queries in DB2 for z/OS

Basic SELECT with DB2-specific features:

SELECT ACCT_ID, CUST_NAME, BALANCE,
       DECIMAL(BALANCE * 0.015, 15, 2) AS INTEREST
FROM BANKING.ACCOUNTS
WHERE BALANCE > 10000.00
  AND ACCT_TYPE = 'SA'
ORDER BY BALANCE DESC
FETCH FIRST 10 ROWS ONLY
WITH UR;
  • DECIMAL: DB2's decimal arithmetic for precise financial calculations
  • FETCH FIRST: Limits rows (DB2-specific, like LIMIT in other databases)
  • WITH UR: Uncommitted Read — allows reading without locking (for reporting queries)

Expected output: The top 10 savings accounts with balance over $10,000, sorted by balance descending, with calculated interest at 1.5%.

Inserting with Identity Columns

CREATE TABLE BANKING.TRANSACTIONS (
    TXN_ID      INTEGER GENERATED ALWAYS AS IDENTITY,
    ACCT_ID     CHAR(10)      NOT NULL,
    TXN_TYPE    CHAR(3)       NOT NULL,
    TXN_AMOUNT  DECIMAL(15,2) NOT NULL,
    TXN_DATE    TIMESTAMP     NOT NULL DEFAULT CURRENT TIMESTAMP
) IN BANKING.ACCTSPACE;

INSERT INTO BANKING.TRANSACTIONS
    (ACCT_ID, TXN_TYPE, TXN_AMOUNT)
VALUES
    ('00001', 'DEP', 500.00),
    ('00001', 'WTH', 100.00),
    ('00002', 'DEP', 1000.00);

Expected behavior: Inserts three Transaction records. The TXN_ID is auto-generated. The TXN_DATE is automatically set to the current timestamp.

Stored Procedures

DB2 for z/OS supports stored procedures written in COBOL, PL/I, or SQL:

CREATE PROCEDURE BANKING.CALC_INTEREST (
    IN  ACCT_TYPE   CHAR(2),
    IN  RATE        DECIMAL(5,4),
    OUT ROWS_UPDATED INTEGER
)
LANGUAGE COBOL
EXTERNAL NAME CALCINT
PARAMETER STYLE GENERAL;

CALL BANKING.CALC_INTEREST('SA', 0.015, :ROWS_UPDATED);

Stored procedures reduce network traffic by processing data where it lives — on the Mainframe.

BIND and REBIND

BIND is the process of preparing an SQL statement for execution. It creates an access path (plan or package) that DB2 uses at runtime.

-- In DSNTEP2 or SPUFI
BIND PLAN(BANKPLAN) MEMBER(BANKPROG) -
    ACT(REP) ISO(CS) CURRENTDATA(YES)

Key BIND parameters:

Parameter Options Meaning
ACT(REP) REP, NO Repeatable read or not
ISO(CS) CS, RR, RS, UR Isolation level (Cursor Stability, Repeatable Read, Read Stability, Uncommitted Read)
CURRENTDATA YES, NO Controls whether data must be current

IRLM Deadlock Detection

IRLM (Internal Resource Lock Manager) is DB2's lock manager. When two transactions wait for each other's locks, a deadlock occurs.

flowchart TD
  T1["Transaction 1"] -- Locks Account A --> A["Account A"]
  T2["Transaction 2"] -- Locks Account B --> B["Account B"]
  T1 -- Waits for Account B --> B
  T2 -- Waits for Account A --> A
  IRLM["IRLM detects deadlock"] --> T1
  IRLM --> T2
  T1 -- Rolled back --> T1END["T1 rolls back, releases lock"]
  T1END --> T2RES["T2 proceeds"]

IRLM selects one Transaction as the deadlock victim, rolls it back with SQLCODE -911, and lets the other Transaction continue.

EXPLAIN for Query Optimization

EXPLAIN shows the access path DB2 chooses for a query:

EXPLAIN PLAN SET QUERYNO = 1 FOR
SELECT ACCT_ID, CUST_NAME, BALANCE
FROM BANKING.ACCOUNTS
WHERE ACCT_ID = '00001';

After running EXPLAIN, query the plan table:

SELECT QBLOCKNO, METHOD, TNAME,
       ACCESSNAME, MATCHCOLS, PREFETCH
FROM PLAN_TABLE
WHERE QUERYNO = 1
ORDER BY QBLOCKNO, METHOD;

Expected output shows whether DB2 used index access or a tablespace scan, how many matching columns were used, and whether prefetch was requested.

Common Errors

1. SQLCODE -904 — Resource unavailable

The resource (tablespace, index) is in use or unavailable. Check the resource status in DB2I and retry.

2. SQLCODE -911 — Deadlock or timeout

Your Transaction was chosen as a deadlock victim. Retry the Transaction. Ensure transactions are short to minimize lock contention.

3. SQLCODE -117 — Same table used twice in SELECT

A correlated subquery or self-join requires table aliases. Always use unique aliases when referencing the same table multiple times.

4. Forgetting COMMIT in CICS-DB2 programs

In CICS, DB2 modifications are committed automatically at syncpoint. In batch, forgetting COMMIT leads to lock contention and large log files.

5. Selecting wrong buffer pool size

Using BP0 (4 KB) for tables with large rows causes excessive I/O. Match the buffer pool page size to your average row size.

Practice Questions

  1. What are the three types of DB2 tablespaces on z/OS? Simple (multiple tables per tablespace), Segmented (one table per segment), and Partitioned (large tables split across partitions).

  2. What does IRLM do? IRLM (Internal Resource Lock Manager) manages locks and detects deadlocks between DB2 transactions, rolling back one to resolve the conflict.

  3. What is the purpose of EXPLAIN in DB2? EXPLAIN shows the access path DB2 uses to execute a query — which indexes, join methods, and scan types are used.

  4. What is the difference between BIND and REBIND? BIND creates a new access path for the first time. REBIND regenerates the access path without recompiling the program, picking up changes in statistics.

Challenge: Create a DB2 database for a library system with tables for books, members, and loans. Create segmented tablespaces, clustering indexes, and a stored procedure that processes overdue fines. Use EXPLAIN to verify the query access paths.

Mini Project

Task: Design a DB2 for z/OS database for an ATM Transaction processing system.

Create the following objects:

  • A BANKING database with two tablespaces (segmented for active data, partitioned for historical data)
  • Tables: ACCOUNTS (account master), TRANSACTIONS (daily activity), ATM_AUDIT (audit log)
  • Indexes: clustering on ACCOUNT ID, non-clustering on Transaction date
  • A stored procedure that processes a withdrawal: checks balance, updates account, inserts Transaction record
  • Buffer pool assignments: BP1 for tables, BP2 for indexes

Write five SQL queries against these tables: balance inquiry, last 10 transactions, daily totals, high-value Transaction alert, and account statement. Run EXPLAIN on each to identify the access path.

What's Next

Tutorial What You'll Learn
CICS Transaction Processing Access DB2 from online CICS transactions
Mainframe Explained — Complete Guide Mainframe architecture and z/OS fundamentals
VSAM File Organization Compare VSAM files with DB2 tables for Mainframe storage

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro