create table res ( res_id number not null , res_date date , hotel_id number(3), guest_id number ) partition by range (res_id) interval (100) store in (users) ( partition p1 values less than (101) );
create table res ( res_id number not null , res_date date , hotel_id number(3), guest_id number ) partition by range (res_id) interval (100) store in (users) ( partition p1 values less than (101) );
create table res ( res_id number not null , res_date date , hotel_id number(3), guest_id number ) partition by range (res_id) interval (100) store in (users) ( partition p1 values less than (101) );
create table res ( res_id number not null , res_date date , hotel_id number(3), guest_id number ) partition by range (res_id) interval (100) store in (users) ( partition p1 values less than (101) );
create table res ( res_id number not null , res_date date , hotel_id number(3), guest_id number ) partition by range (res_id) interval (100) store in (users) ( partition p1 values less than (101) );
create table res ( res_id number not null , res_date date , hotel_id number(3), guest_id number ) partition by range (res_id) interval (100) store in (users) ( partition p1 values less than (101) );
create table res ( res_id number not null , res_date date , hotel_id number(3), guest_id number ) partition by range (res_id) interval (100) store in (users) ( partition p1 values less than (101) );
create table res ( res_id number not null , res_date date , hotel_id number(3), guest_id number ) partition by range (res_id) interval (100) store in (users) ( partition p1 values less than (101) );
create table res ( res_id number not null , res_date date , hotel_id number(3), guest_id number ) partition by range (res_id) interval (100) store in (users) ( partition p1 values less than (101) );
create table res ( res_id number not null , res_date date , hotel_id number(3), guest_id number ) partition by range (res_id) interval (100) store in (users) ( partition p1 values less than (101) );
create table res ( res_id number not null , res_date date , hotel_id number(3), guest_id number ) partition by range (res_id) interval (100) store in (users) ( partition p1 values less than (101) );
- create table test_part (
- id number not null
- name varchar2 (100) not null ,
- owner varchar2 (100) not null ,
- type varchar2 (100) not null ,
- created date not null ,
- constraint test_part_pk
- primary key (id)
- )
- partition by range (id) (partition p1 values less than (10000));
- create or replace trigger tr_test_part
- before insert on test_part
- for each row
- when (mod ( NEW .id, 10000) = 6000)
- declare
- l_part_name number;
- l_maxvalue number;
- l_exist number;
- l_partition_exists exception ;
- PRAGMA EXCEPTION_INIT (l_partition_exists, -14074);
- PRAGMA AUTONOMOUS_TRANSACTION;
- begin
- l_part_name: = ceil (: NEW .ID / 10000) +1;
- BEGIN
- execute immediate alter table xtender.test_part add partition p ||| l_part_name || 'values less than (' || l_maxvalue || ')' ;
- EXCEPTION
- when l_partition_exists then null ;
- END ;
- end tr_test_part;
insert into xtender.test_part
select rownum, o.OBJECT_NAME, o.OWNER, o.OBJECT_TYPE, o.CREATED
from all_objects o
where rownum<1000;
when (mod (NEW.id, 10000) = 6000)on
when (mod (NEW.id, 10000) between 6000 and 6100)
exec dbms_system.ksdwrt (2, 'Test Alert Message');
sqlplus sys/<pwd> SQL> @$ORACLE_HOME/rdbms/admin/utlmail.sql SQL> @$ORACLE_HOME/rdbms/admin/prvtmail.plb
- select
- p. *,
- sys.pkg_partitions.get_penultimate_maxvalue (p.table_owner, p.table_name) pre_maxvalue
- from
- table (sys.pkg_partitions.get_maxvalued_partitions) p
* This source code was highlighted with Source Code Highlighter .
- create or replace package body pkg_partitions is
- / ** Function returning parameter for the penultimate section
- * @param i_table_name Table Name
- * @ return varchar2
- * /
- function get_penultimate_maxvalue (p_table_owner varchar2, p_table_name varchar2) return varchar2 is
- l_cursor integer default dbms_sql.open_cursor;
- l_ignore number;
- l_long_val varchar2 (4000);
- l_long_len number;
- l_buflen number: = 4000;
- l_curpos number: = 0;
- begin
- dbms_sql.parse (l_cursor,
- 'select p.high_value from all_tab_partitions p where p.table_owner like: o and p.table_name like: x and p.partition_position = (select max (p1.partition_position) -1 from all_tab_partitions p1 where p.table_owner like: o and p1. table_name like: x) '
- ,
- dbms_sql.native);
- dbms_sql.bind_variable (l_cursor, ': x' , p_table_name);
- dbms_sql.bind_variable (l_cursor, ': o' , p_table_owner);
- dbms_sql.define_column_long (l_cursor, 1);
- l_ignore: = dbms_sql. execute (l_cursor);
- if (dbms_sql.fetch_rows (l_cursor)> 0)
- then
- dbms_sql.column_value_long (l_cursor, 1, l_buflen, l_curpos,
- l_long_val, l_long_len);
- end if ;
- dbms_sql.close_cursor (l_cursor);
- return l_long_val;
- end ;
- / ** Function returning the names of tables and their owners, who have started filling out the last section
- * @ return
- * table_name varchar2 (4000),
- * table_owner varchar2 (4000),
- * partitions_count number,
- * partition_name varchar2 (4000));
- * /
- function get_maxvalued_partitions return tables_props_array pipelined is
- l_cursor integer default dbms_sql.open_cursor;
- l_count number;
- l_ignore integer ;
- l_data table_props;
- cursor l_partitions is
- select
- pl.table_owner,
- pl.table_name,
- count (1) cnt,
- max (pl.partition_name) keep (dense_rank last order by (pl.partition_position)) partition_name
- from dba_tab_partitions pl
- where pl.table_name not like 'BIN $%'
- group by pl.table_owner, pl.table_name
- having count (1)> 1;
- BEGIN
- FOR part in l_partitions
- LOOP
- dbms_sql.parse (l_cursor,
- 'select count (1) from' || part.table_owner || '.' || part.table_name
- || 'partition (' || part.partition_name || ')'
- || 'where rownum <2'
- ,
- dbms_sql.native);
- dbms_sql.define_column (l_cursor, 1, l_count);
- l_ignore: = dbms_sql.execute_and_fetch (l_cursor);
- dbms_sql.column_value (l_cursor, 1, l_count);
- if (l_count> 0) then
- l_data.table_name: = part.table_name;
- l_data.table_owner: = part.table_owner;
- l_data.partitions_count: = part.cnt;
- l_data.partition_name: = part.partition_name;
- pipe row (l_data);
- end if ;
- END LOOP;
- END ;
- / ** The function returns the names of the tables and their owners in the form of html, which began to fill the last section
- * @ return
- * table_name varchar2 (4000),
- * table_owner varchar2 (4000),
- * partitions_count number,
- * partition_name varchar2 (4000));
- * /
- function get_maxvalued_partitions_html return varchar2 is
- l_cursor integer default dbms_sql.open_cursor;
- l_count number;
- l_ignore integer ;
- l_data varchar2 (4000);
- cursor l_partitions is
- select
- pl.table_owner,
- pl.table_name,
- count (1) cnt,
- max (pl.partition_name) keep (dense_rank last order by (pl.partition_position)) partition_name
- from dba_tab_partitions pl
- where pl.table_name not like 'BIN $%'
- group by pl.table_owner, pl.table_name
- having count (1)> 1;
- BEGIN
- l_data: = '<html> <body> <table border = 1>'
- || '<tr> <th> Table name </ th>'
- || '<th> Table owner </ th>'
- || '<th> Partitions count </ th>'
- || '<th> Partition name </ th>'
- || '<th> Pre maxvalue </ th>' ;
- FOR part in l_partitions
- LOOP
- dbms_sql.parse (l_cursor,
- 'select count (1) from' || part.table_owner || '.' || part.table_name
- || 'partition (' || part.partition_name || ')'
- || 'where rownum <2'
- ,
- dbms_sql.native);
- dbms_sql.define_column (l_cursor, 1, l_count);
- l_ignore: = dbms_sql.execute_and_fetch (l_cursor);
- dbms_sql.column_value (l_cursor, 1, l_count);
- if (l_count> 0) then
- l_data: = l_data || '<tr> <td>'
- || part.table_name
- || '</ td> <td>'
- || part.table_owner
- || '</ td> <td>'
- || part.cnt
- || '</ td> <td>'
- || part.partition_name
- || '</ td> </ tr>' ;
- end if ;
- END LOOP;
- l_data: = l_data || '</ table> </ body> </ html>' ;
- return l_data;
- END ;
- / **
- * The procedure for sending a report with tables that have started to fill the last section
- * /
- procedure send_partitions_report (mail varchar2)
- is
- msg_body varchar2 (4000);
- BEGIN
- select pkg_partitions.get_maxvalued_partitions_html into msg_body from dual;
- - EXECUTE IMMEDIATE 'ALTER SESSION SET smtp_out_server =' 'our_mailserver' '' ;
- utl_mail.send (
- sender => 'oracleDBA@dbdomain.com' ,
- recipients => mail,
- subject => 'Maxvalued partitions Report' ,
- message => msg_body,
- mime_type => 'text / html' );
- END ;
- end pkg_partitions;
- select
- pl.table_owner,
- pl.table_name,
- count (1) cnt,
- max (pl.num_rows) keep (dense_rank last order by (pl.partition_position)) partition_rows,
- max (pl.partition_name) keep (dense_rank last order by (pl.partition_position)) partition_name
- from dba_tab_partitions pl
- where pl.table_name not like 'BIN $%'
- group by pl.table_owner, pl.table_name
- declare
- job binary_integer;
- begin
- dbms_job.submit (
- job,
- 'pkg_partitions.send_partitions_report (' 'dba@domain.ru' ');' ,
- sysdate
- 'trunc (sysdate) +1' );
- dbms_output.put_line (job);
- end ;
Source: https://habr.com/ru/post/105759/
All Articles