-- UNDO tablespace current usage / available space
SELECT a.TABLESPACE_NAME, SIZEMB, USAGEMB, (SIZEMB - USAGEMB) AS FREEMB
FROM (SELECT round(sum(BYTES) / 1e6) AS SIZEMB, b.TABLESPACE_NAME
FROM DBA_DATA_FILES a, DBA_TABLESPACES b
WHERE a.TABLESPACE_NAME = b.TABLESPACE_NAME AND b.CONTENTS LIKE '%UNDO%'
GROUP BY b.TABLESPACE_NAME) a,
(SELECT c.TABLESPACE_NAME, sum(BYTES) / 1e6 AS USAGEMB
FROM DBA_UNDO_EXTENTS c
WHERE STATUS <> 'EXPIRED'
GROUP BY c.TABLESPACE_NAME) b
WHERE a.TABLESPACE_NAME = b.TABLESPACE_NAME;
-- UNDO tablespace usage per user / session
SELECT s.SID, s.USERNAME, round(sum(ss.VALUE) / 1e6, 2) AS UNDO_SIZE_MB,
sql.ELAPSED_TIME, sql.SQL_TEXT
FROM V$SESSTAT ss JOIN V$SESSION s ON s.SID = ss.SID
JOIN V$STATNAME STAT ON STAT.STATISTIC# = ss.STATISTIC#
LEFT JOIN V$SQLAREA sql
ON s.SQL_ADDRESS = sql.ADDRESS AND s.SQL_HASH_VALUE = sql.HASH_VALUE
WHERE STAT.NAME = 'undo change vector size' AND s.TYPE <> 'BACKGROUND'
AND s.USERNAME IS NOT NULL AND ss.VALUE >= 0.01 * 1e6
GROUP BY s.SID, s.USERNAME, sql.ELAPSED_TIME, sql.SQL_TEXT
ORDER BY s.USERNAME, round(sum(ss.VALUE) / 1e6, 2);