Finding out how much space a library uses

Log in to save

The library total:

SELECT SUM(OBJSIZE) / 1024 / 1024 AS MB
  FROM TABLE(QSYS2.OBJECT_STATISTICS('PRODLIB', '*ALL'))

But the total is of little use: what matters is who is taking the space.

SELECT OBJNAME, OBJTYPE,
       OBJSIZE / 1024 / 1024 AS MB,
       LAST_USED_TIMESTAMP
  FROM TABLE(QSYS2.OBJECT_STATISTICS('PRODLIB', '*ALL'))
 ORDER BY OBJSIZE DESC
 FETCH FIRST 20 ROWS ONLY

The largest libraries on the system, when you do not know where to start:

SELECT OBJNAME AS LIBRARY, OBJSIZE / 1024 / 1024 AS MB
  FROM TABLE(QSYS2.OBJECT_STATISTICS('QSYS', '*LIB'))
 ORDER BY OBJSIZE DESC
 FETCH FIRST 20 ROWS ONLY

Example: the library that grows and nobody knows why

Knowing that PRODLIB takes 40 GB says nothing. Knowing it took 28 six months ago, and which objects took the 12 GB difference, says everything.

One snapshot a month, in a table created from the query itself:

CREATE TABLE MYLIB/SIZEHIST AS (
  SELECT CURRENT DATE AS TAKEN_AT, OBJNAME, OBJTYPE, OBJSIZE
    FROM TABLE(QSYS2.OBJECT_STATISTICS('PRODLIB', '*ALL'))
) WITH NO DATA

Then the comparison between two snapshots, which is the query that earns its keep:

SELECT o.OBJNAME, o.OBJTYPE,
       n.OBJSIZE - o.OBJSIZE AS GROWTH
  FROM MYLIB/SIZEHIST o
  JOIN MYLIB/SIZEHIST n
    ON n.OBJNAME = o.OBJNAME AND n.OBJTYPE = o.OBJTYPE
 WHERE o.TAKEN_AT = '2026-02-01'
   AND n.TAKEN_AT = '2026-08-01'
 ORDER BY GROWTH DESC
 FETCH FIRST 20 ROWS ONLY

The answer is usually an application log file nobody ever emptied, or a work table that was meant to be temporary.

Tip

LAST_USED_TIMESTAMP together with size is the pair that actually helps. A large object used yesterday stays; a large one unused for three years is the first candidate.


Releases. QSYS2.OBJECT_STATISTICS is available from 7.2; on 7.1 it came with a PTF. The LAST_USED_TIMESTAMP column was added after the function itself: if the query answers SQL0206 (column not found) drop it and the rest still works.

Sull'esempio: il confronto fra due fotografie non è stato eseguito; richiede due rilevazioni a distanza di mesi. Le colonne che usa sono le stesse già verificate.

← Back to blog

Comments

No comments yet. Be the first to comment!

You need an account to comment. Log in · Sign up