-- =====================================================================
-- Curatenie spatiu Oracle -- server 10.0.20.121:1521, PDB ROA
-- Motiv: ORA-65114 la INSERT in CONTAFIN_ORACLE.ISTORIC_CODURI_FISCALE
-- Diagnostic: docs\diagnostic-ora-65114-spatiu.md
--
-- EXECUTAT pe 01-02.08.2026. Scriptul e pastrat ca fisa de interventie
-- si ca sablon pentru urmatoarea curatenie. Contine corectiile aflate
-- la rulare (vezi notele "ATENTIE").
--
-- Rulare:
--   D:\ROA\instantclient_19_18\sqlplus.exe -L "sys/<parola>@ROA_CENTRAL as sysdba" @curatenie-spatiu-oracle.sql
--
-- ATENTIE GENERALA: liniile "prompt" NU trebuie sa se termine cu "---";
-- sqlplus interpreteaza "--" ca inceput de comentariu si inghite
-- instructiunea urmatoare (s-a intamplat: un truncate nu s-a executat,
-- desi iesirea parea normala). Foloseste "prompt >> text".
-- =====================================================================

set lines 220 pages 500 feedback on timing on serveroutput on


prompt >> STARE INITIALA
alter session set container=CDB$ROOT;
select con_id, name, open_mode, round(total_size/1024/1024/1024,2) gb_total from v$pdbs order by 1;
select con_id, property_value max_pdb_storage from cdb_properties where property_name='MAX_PDB_STORAGE' order by 1;


prompt >> PAS 1: ridicare plafon PDB ROA 5G -> 7G
-- XE 21c permite 12 GB date de utilizator pe intreg CDB-ul; erau 5401 MB
-- (ROA 3098 + ROA2 2298 + root 5). Plafoanele devin 7+5 = 12 G.
-- Fara acest pas PDB-ul nu poate extinde NICI un fisier, nici UNDO,
-- deci pasii urmatori ar putea esua.
alter pluggable database ROA storage (maxsize 7G);


alter session set container=ROA;
prompt >> liber in tablespace ROA inainte (MB)
select nvl(round(sum(bytes)/1024/1024,1),0) mb_liber from dba_free_space where tablespace_name='ROA';


prompt >> PAS 2: MARIUSM_AUTO.XSERVER_LOG
-- 1.752.814 randuri, toate din 2009-2011 (DATA_INCEPUT). Log mort, 152 MB.
truncate table mariusm_auto.xserver_log drop storage;
-- ATENTIE: "drop storage" NU a dezalocat extenturile (segmentul a ramas
-- 152 MB / 5 extenturi cu tabela goala). Pasul care elibereaza efectiv:
alter table mariusm_auto.xserver_log deallocate unused keep 0;


prompt >> PAS 3: SOFT.ERRORS
-- 678.430 randuri, 392 MB, nimic mai nou de 2023 (tabela nu mai e alimentata).
-- Decizie Marius: golire completa.
truncate table soft.errors drop storage;
alter table soft.errors deallocate unused keep 0;
--
-- Varianta cu pastrare partiala (daca la o rulare viitoare tabela e activa):
-- stergerea trebuie facuta IN LOTURI cu commit -- un DELETE monolitic pe
-- ~665.000 randuri cere ~450 MB undo, iar UNDOTBS1 avea 222 MB liberi.
-- declare
--   n_lot pls_integer; n_total pls_integer := 0;
-- begin
--   loop
--     delete from soft.errors where created_at < date '2020-01-01' and rownum <= 50000;
--     n_lot := sql%rowcount; commit; n_total := n_total + n_lot;
--     exit when n_lot = 0;
--   end loop;
--   dbms_output.put_line('sterse: ' || n_total);
-- end;
-- /
-- alter table soft.errors enable row movement;
-- alter table soft.errors shrink space cascade;
-- alter table soft.errors disable row movement;


prompt >> PAS 4: unified audit trail (SYSAUX)
-- 64.351 inregistrari, 09.2025-07.2026, 409 MB. Se pastreaza ultimele 90 zile.
-- ATENTIE: dbms_audit_mgmt.init_cleanup / is_cleanup_initialized dau
-- ORA-46250 pentru AUDIT_TRAIL_UNIFIED intr-un PDB. Nu sunt necesare:
-- set_last_archive_timestamp + clean_audit_trail functioneaza direct.
begin
  dbms_audit_mgmt.set_last_archive_timestamp(
    audit_trail_type  => dbms_audit_mgmt.audit_trail_unified,
    last_archive_time => systimestamp - 90);
  dbms_audit_mgmt.clean_audit_trail(
    audit_trail_type        => dbms_audit_mgmt.audit_trail_unified,
    use_last_arch_timestamp => true);
end;
/


prompt >> PAS 5: SQL Tuning Set automat SYS_AUTO_STS (SYSAUX)
-- 23.263 instructiuni capturate automat. Artefact de diagnostic; XE nu are
-- licenta Tuning Pack. Se goleste continutul, setul ramane.
begin
  dbms_sqltune.delete_sqlset(sqlset_name => 'SYS_AUTO_STS', sqlset_owner => 'SYS');
end;
/
-- ATENTIE: segmentele SYS.WRI$_SQLSET_* NU pot fi micsorate dintr-un PDB
--   alter table sys.wri$_sqlset_plan_lines shrink space  -> ORA-65040
--   alter table ... modify lob (other_xml) (shrink space) -> ORA-10662
-- Spatiul ramane alocat segmentului (reutilizabil de el), ~590 MB in SYSAUX.


prompt >> PAS 6 (de rulat la >30 min dupa stergerile mari): micsorare UNDO
-- Stergerile in loturi au umflat UNDOTBS1 de la 235 la 785 MB. Extenturile
-- devin EXPIRED abia dupa expirarea UNDO_RETENTION; pana atunci resize da
-- ORA-03297. Se reia periodic pana trece.
declare
  v_file varchar2(400);
begin
  select file_name into v_file from dba_data_files where tablespace_name='UNDOTBS1' and rownum=1;
  for m in 1..8 loop
    declare
      v_mb number := 800 - m*70;
    begin
      execute immediate 'alter database datafile ''' || v_file || ''' resize ' || v_mb || 'M';
      dbms_output.put_line('UNDO redimensionat la ' || v_mb || ' MB');
      exit;
    exception when others then
      dbms_output.put_line(v_mb || ' MB -> ' || substr(sqlerrm,1,60));
    end;
  end loop;
end;
/


prompt >> STARE FINALA
select d.tablespace_name, round(sum(d.bytes)/1024/1024,0) mb_alocat,
       nvl((select round(sum(f.bytes)/1024/1024,0) from dba_free_space f
             where f.tablespace_name=d.tablespace_name),0) mb_liber
  from dba_data_files d group by d.tablespace_name order by 2 desc;

alter session set container=CDB$ROOT;
select con_id, name, round(total_size/1024/1024/1024,2) gb_total from v$pdbs order by 1;
exit
