SQL 89
BC Clean sysaux freepdb1 By allan on 23rd July 2026 12:48:53 PM
  1. SET linesize 120
  2. SET pagesize 100
  3. col ash form a30
  4. col retention form a30
  5. col snap form a30
  6.  
  7. COLUMN "Item" FORMAT A25
  8. COLUMN "Space Used (GB)" FORMAT 999.99
  9. COLUMN "Schema" FORMAT A25
  10. COLUMN "Move Procedure" FORMAT A40
  11.  
  12. SELECT  occupant_name "Item",
  13. space_usage_kbytes/1048576 "Space Used (GB)",
  14. schema_name "Schema",
  15. move_procedure "Move Procedure"
  16. FROM v$sysaux_occupants
  17. WHERE occupant_name = 'SM/AWR'
  18. ORDER BY 1
  19. /
  20.  
  21. SELECT 'alter index '||segment_name||' rebuild online parallel (degree 4);'
  22. FROM dba_segments
  23. WHERE tablespace_name= 'SYSAUX'
  24. AND segment_name LIKE 'WRH$_%'
  25. AND segment_type='INDEX'
  26. ORDER BY segment_name;
  27.  
  28. /
  29.  
  30. --- Move SYSAUX WRH$_% tables to reclaim space
  31. SELECT 'exec dbms_pdb.exec_as_oracle_script(''alter table '||segment_name||' move tablespace sysaux'');'
  32. FROM dba_segments
  33. WHERE tablespace_name= 'SYSAUX'
  34. AND segment_name LIKE 'WRH$_%'
  35. AND segment_type = 'TABLE'  
  36. ORDER BY segment_name;
  37.  
  38. /
  39.  
  40. SELECT TABLE_NAME, COUNT(*)
  41. FROM dba_tab_partitions
  42. WHERE TABLE_NAME LIKE 'WRH$%'
  43. AND table_owner = 'SYS'
  44. GROUP BY TABLE_NAME
  45. ORDER BY 1;
  46.  
  47. /
  48.  
  49. BEGIN
  50.  EXECUTE IMMEDIATE 'alter session set "_swrf_test_action" = 72';
  51. END;
  52. /
  53.  
  54. COLUMN min_id FORMAT 99999999999999999999
  55. COLUMN max_id FORMAT 99999999999999999999
  56.  
  57. SELECT MIN(sample_id) AS min_id,
  58.        MAX(sample_id) AS max_id
  59. FROM sys.wrh$_active_session_history;
  60. -- 80381470007  81701088731
  61.  
  62. EXEC dbms_workload_repository.drop_snapshot_range(low_snap_id=>80381470007, high_snap_id=>81701088731);
  63. /
  64.  
  65. /*
  66. To reduce a LOB segment in SYSAUX, first identify what owns it and whether it is a SecureFile or BasicFile LOB. The key difference is:
  67.  
  68. SecureFile LOBs can often be shrunk in place.
  69. BasicFile LOBs cannot really be shrunk in place; you usually have to move/rebuild the parent table or move the LOB to another tablespace.
  70. */
  71.  
  72. -- Run this first:
  73.  
  74. SELECT owner, TABLE_NAME, column_name, segment_name, tablespace_name, securefile
  75. FROM dba_lobs
  76. WHERE segment_name = 'SYS_LOB0000014105C00038$$';
  77.  
  78. SELECT segment_name, segment_type, bytes/1024/1024 AS mb
  79. FROM dba_segments
  80. WHERE segment_name = 'SYS_LOB0000014105C00038$$';
  81.  
  82.  
  83. -- If it is a SecureFile LOB, try:
  84. ALTER TABLE owner.table_name
  85.   MODIFY LOB (column_name) (SHRINK SPACE);
  86.  
  87. -- If you want a more aggressive cleanup, you can use:
  88. ALTER TABLE owner.table_name
  89.   MODIFY LOB (column_name) (SHRINK SPACE CASCADE);
  90.  
  91. --If it is a BasicFile LOB, shrink is not supported. In that case the usual options are:
  92. ALTER TABLE owner.table_name
  93.   MOVE LOB (column_name)
  94.   STORE AS (TABLESPACE <another_tablespace>);
  95.  
  96. /*
  97. or rebuild/move the parent table so Oracle recreates the LOB segment smaller.
  98.  
  99. For SYS-owned objects in SYSAUX, the better fix is often not manual shrinking but deleting the underlying data that filled it, then letting Oracle reuse the space. Common examples are optimizer stats history or advisor data. If your segment is one of the WRI$_OPTSTAT... or related SYS tables, purge old stats first:
  100. */
  101.  
  102. EXEC DBMS_STATS.PURGE_STATS(DBMS_STATS.PURGE_ALL);
  103.  
  104.  
  105.  
  106. SELECT 'alter table '||l.owner||'.'||l.table_name||' modify lob ('||l.column_name||') (shrink space cascade); --'||s.bytes/1024/1024 AS "Mb"
  107. FROM dba_lobs l
  108.   JOIN dba_segments s ON s.tablespace_name = l.tablespace_name
  109.     AND s.owner = l.owner
  110.     AND s.segment_type = 'LOBSEGMENT'
  111. WHERE l.owner = 'SYS'
  112. AND l.tablespace_name = 'SYSAUX'
  113. --and l.table_name like 'WR%'
  114. ;

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.