ACE Director Alum Daniel Morgan, founder of Morgan's Library, is scheduling
complimentary technical Workshops on Database Security for the first 30
Oracle Database customers located anywhere in North America, EMEA, LATAM, or
APAC that send an email to
asra_us@oracle.com. Request a Workshop for
your organization today.
Purpose
This package provides segment space information not currently available through the standard views
Note
For the object growth trend functions the following must be kept in mind to understand the output..
QUALITY: A value indicating how well the requested reporting interval matches the actual recording of statistics.
This information is useful because there is no guaranteed reporting interval for object size use statistics, and the actual reporting interval varies over time and from object to object.
The values of the QUALITY column are: GOOD: The value whenever the value of TIME is based on recorded statistics with a recorded timestamp within 10% of the INTERVAL specified in the input parameters.
INTERPOLATED: The value did not meet the criteria for GOOD, but was based on recorded statistics before and after the value of TIME.
Current in-memory statistics can be collected across all instances in a cluster and treated as the "recorded" value for the present time.
PROJECTION: The value of TIME is in the future as of the time the table was produced. In an Oracle Real Application Clusters environment, the rules for recording statistics allow each instance to choose independently which objects will be selected.
The output returned by these functions is an aggregation of values recorded across all instances in an Oracle RAC environment. Each value can be computed from a combination of GOOD and INTERPOLATED values.
The aggregate value returned is marked GOOD if at least 80% of that value was derived from GOOD instance values.
AUTHID
CURRENT_USER
Constants
Name
Data Type
Value
OBJECT_TYPE_TABLE
POSITIVE
1
OBJECT_TYPE_NESTED_TABLE
POSITIVE
2
OBJECT_TYPE_INDEX
POSITIVE
3
OBJECT_TYPE_CLUSTER
POSITIVE
4
OBJECT_TYPE_LOB_INDEX
POSITIVE
5
OBJECT_TYPE_LOBSEGMENT
POSITIVE
6
OBJECT_TYPE_TABLE_PARTITION
POSITIVE
7
OBJECT_TYPE_INDEX_PARTITION
POSITIVE
8
OBJECT_TYPE_TABLE_SUBPARTITION
POSITIVE
9
OBJECT_TYPE_INDEX_SUBPARTITION
POSITIVE
10
OBJECT_TYPE_LOB_PARTITION
POSITIVE
11
OBJECT_TYPE_LOB_SUBPARTITION
POSITIVE
12
OBJECT_TYPE_MV
POSITIVE
13
OBJECT_TYPE_MVLOG
POSITIVE
14
OBJECT_TYPE_ROLLBACK_SEGMENT
POSITIVE
15
SPACEUSAGE_EXACT (Computes space usage exhaustively)
POSITIVE
16
SPACEUSAGE_FAST (Retrieves values from in-memory statistics)
-- content of one row in dependent_segments table
TYPE object_dependent_segment IS RECORD (
segment_owner VARCHAR2(100),
segment_name VARCHAR2(100),
segment_type VARCHAR2(100),
tablespace_name VARCHAR2(100),
partition_name VARCHAR2(100),
lob_column_name VARCHAR2(100));
-- a table of dependent_segment records
TYPE dependent_segments_table IS TABLE OF object_dependent_segment;
-- object_growth_trend_row and object_growth_trend_table are used
-- by the object_growth_trend table function to describe its output
TYPE object_growth_trend_row IS RECORD (
timepoint TIMESTAMP,
space_usage NUMBER,
space_alloc NUMBER,
quality VARCHAR2(20));
TYPE object_growth_trend_table IS TABLE OF object_growth_trend_row;
/* object_growth_swrf_row, object_growth_swrf_table, object_growth_swrf_cursor, object_growth_trend_curtab,
and object_growth_trend_test_swrf are internal to the implementation of object_growth_trend but need to be declared here instead of in the private package body.
These internal types and procedures do not expose any internal information to the user. */
TYPE object_growth_swrf_cursor IS REF CURSOR RETURN object_growth_swrf_row;
TYPE object_growth_swrf_row IS RECORD (
timepoint TIMESTAMP,
delta_space_usage NUMBER,
delta_space_alloc NUMBER,
total_space_usage NUMBER,
total_space_alloc NUMBER,
instance_number NUMBER,
objn NUMBER);
TYPE object_growth_swrf_table IS TABLE OF object_growth_swrf_row;
-- type owned by SYS
CREATE TYPE create_table_cost_colinfo IS OBJECT (
col_type VARCHAR(200),
col_size NUMBER);
/
-- type owned by SYS
CREATE TYPE create_table_cost_columns IS VARRAY(50000) OF create_table_cost_colinfo;
/
-- type owned by SYS
CREATE TYPE tablespace_list IS VARRAY (64000) OF NUMBER;
/
TYPE verify_shrink_row IS RECORD(status NUMBER);
TYPE verify_shrink_table IS TABLE OF of verify_shrink_row;
TYPE object_space_usage_row IS RECORD (
space_used NUMBER,
space_allocated NUMBER,
chain_pcent NUMBER);
TYPE object_space_usage_table IS TABLE OF object_space_usage_row;
Returns recommendations / findings of segment advisor run automatically by the system or manually invoked by the user
dbms_space.asa_recommendations (
all_runs IN VARCHAR2 DEFAULT := TRUE,
show_manual IN VARCHAR2 DEFAULT := TRUE,
show_findings IN VARCHAR2 DEFAULT := 'FALSE')
RETURN asa_reco_row_tb PIPELINED;
set linesize 121
col tablespace_name format a20
col segment_owner format a20
col segment_name format a20
col segment_type format a15
col recommendations format a60
SELECT tablespace_name, segment_owner, segment_name, segment_type,
allocated_space, used_space, reclaimable_space
FROM (
SELECT *
FROM TABLE(dbms_space.asa_recommendations('TRUE','TRUE','TRUE')));
SELECT segment_owner, segment_name, recommendations
FROM (
SELECT *
FROM TABLE(dbms_space.asa_recommendations('TRUE','TRUE','TRUE')));
Determines the size of the table given various attributes
Overload 1
dbms_space.create_table_cost(
tablespace_name IN VARCHAR2,
avg_row_size IN NUMBER,
row_count IN NUMBER,
pct_free IN NUMBER,
used_bytes OUT NUMBER,
alloc_bytes OUT NUMBER);
set serveroutput on
DECLARE
ub NUMBER;
ab NUMBER;
BEGIN
dbms_space.create_table_cost('UWDATA',28,250000,0,ub,ab);
dbms_space.create_table_cost(
tablespace_name IN VARCHAR2,
colinfos IN CREATE_TABLE_COST_COLUMNS,
row_count IN NUMBER,
pct_free IN NUMBER,
used_bytes OUT NUMBER,
alloc_bytes OUT NUMBER);
set linesize 121
col type_owner format a20
col data_type format a20
SELECT argument_name, data_type, type_owner, type_name
FROM all_arguments
WHERE object_name = 'CREATE_TABLE_COST'
AND overload = 2;
SELECT text
FROM dba_source
WHERE name = 'CREATE_TABLE_COST_COLUMNS';
SELECT text
FROM dba_source
WHERE name = 'CREATE_TABLE_COST_COLINFO';
set serveroutput on
DECLARE
ub NUMBER;
ab NUMBER;
cl sys.create_table_cost_columns;
BEGIN
cl := sys.create_table_cost_columns(
sys.create_table_cost_colinfo('NUMBER',10),
sys.create_table_cost_colinfo('VARCHAR2',30),
sys.create_table_cost_colinfo('VARCHAR2',30),
sys.create_table_cost_colinfo('DATE',NULL));
Information about free blocks in a table, index, or cluster. Only work on tablespaces w/o ASSM. With ASSM use dbms_space.space_usage
dbms_space.free_blocks (
segment_owner IN VARCHAR2,
segment_name IN VARCHAR2,
segment_type IN VARCHAR2,
freelist_group_id IN NUMBER,
free_blks OUT NUMBER,
scan_limit IN NUMBER DEFAULT NULL,
partition_name IN VARCHAR2 DEFAULT NULL);
Segment Types
CLUSTER
INDEX
INDEX PARTITION
INDEX SUBPARTITION
LOB
LOB PARTITION
LOB SUBPARTITION
TABLE
TABLE PARTITION
TABLE SUBPARTITION
SELECT tablespace_name, segment_space_management
FROM dba_tablespaces;
Returns information to guide the decision as to whether to compress a heap table
dbms_space.get_compression_ratio(
scratchtbsname IN VARCHAR2,
ownname IN VARCHAR2,
objname IN VARCHAR2,
subobjname IN VARCHAR2,
comptype IN NUMBER,
blkcnt_cmp OUT PLS_INTEGER,
blkcnt_uncmp OUT PLS_INTEGER,
row_cmp OUT PLS_INTEGER,
row_uncmp OUT PLS_INTEGER,
cmp_ratio OUT NUMBER,
comptype_str OUT VARCHAR2);
Returns the list of segments that are associated with an object
dbms_space.object_dependent_segments(
objowner IN VARCHAR2,
objname IN VARCHAR2,
partname IN VARCHAR2,
objtype IN NUMBER)
RETURN dependent_segments_table PIPELINED;
set linesize 121
col segment_owner format a20
col segment_name format a30
col segment_type format a15
col tablespace_name format a15
col partition_name format a15
col lob_column_name format a10
set serveroutput on
SELECT segment_owner, segment_name, segment_type, tablespace_name
FROM (TABLE(dbms_space.object_dependent_segments('UWCLASS', 'SERV_INST', NULL, 1)));
Pipelined table function where each row describes the space usage of the object at a specific point in time
dbms_space.object_growth_trend (
object_owner IN VARCHAR2,
object_name IN VARCHAR2,
object_type IN VARCHAR2,
partition_name IN VARCHAR2 DEFAULT NULL,
start_time IN TIMESTAMP DEFAULT NULL,
end_time IN TIMESTAMP DEFAULT NULL,
interval IN DSINTERVAL_UNCONSTRAINED DEFAULT NULL,
skip_interpolated IN VARCHAR2 DEFAULT 'FALSE',
timeout_seconds IN NUMBER DEFAULT NULL,
single_datapoint_flag IN VARCHAR2 DEFAULT 'TRUE')
RETURN object_growth_trend_table PIPELINED;
set linesize 121
col timepoint format a40
SELECT *
FROM TABLE(dbms_space.object_growth_trend('SYS', 'TAB$', 'TABLE'));
dbms_space.object_growth_trend_curtab(
object_owner IN VARCHAR2,
object_name IN VARCHAR2,
object_type IN VARCHAR2,
partition_name IN VARCHAR2 DEFAULT NULL,
start_time IN TIMESTAMP DEFAULT NULL,
end_time IN TIMESTAMP DEFAULT NULL,
interval IN DSINTERVAL_UNCONSTRAINED DEFAULT NULL,
skip_interpolated IN VARCHAR2 DEFAULT 'FALSE',
timeout_seconds IN NUMBER DEFAULT NULL)
RETURN SYS_REFCURSOR;
Pipelined table function returning timestamp, change in space usage, change in space allocation, instance number, and object number
dbms_space.object_growth_trend_swrf (
object_owner IN VARCHAR2,
object_name IN VARCHAR2,
object_type IN VARCHAR2,
partition_name IN VARCHAR2 DEFAULT NULL)
RETURN object_growth_swrf_table PIPELINED;
set linesize 121
col timepoint format a40
SELECT timepoint,
delta_space_usage, delta_space_alloc, total_space_usage, total_space_alloc
FROM TABLE(dbms_space.object_growth_trend_swrf('SYS', 'TAB$', 'TABLE'))
ORDER BY 1;
Returns the space used, space allocated, and percentage of chained rows in a table
dbms_space.object_space_usage (
object_owner IN VARCHAR2,
object_name IN VARCHAR2,
object_type IN VARCHAR2,
sample_control IN NUMBER,
space_used OUT NUMBER,
space_allocated OUT NUMBER,
chain_pcent OUT NUMBER,
partition_name IN VARCHAR2 DEFAULT NULL,
preserve_result IN BOOLEAN DEFAULT TRUE,
timeout_seconds IN NUMBER DEFAULT NULL);
set serveroutput on
DECLARE
su NUMBER;
sa NUMBER;
cp NUMBER;
BEGIN
dbms_space.object_space_usage('UWCLASS', 'SERVERS', 'TABLE', NULL, su, sa, cp);
Pipelined table function returning space used, space allocated, and percentage of chained rows in a table
dbms_space.object_space_usage_tbf(
object_owner IN VARCHAR2,
object_name IN VARCHAR2,
object_type IN VARCHAR2,
sample_control IN NUMBER,
partition_name IN VARCHAR2 DEFAULT NULL,
preserve_result IN VARCHAR2 DEFAULT 'TRUE',
timeout_seconds IN NUMBER DEFAULT NULL)
RETURN object_space_usage_table pipelined;
SELECT *
FROM TABLE(dbms_space.object_space_usage_tbf('UWCLASS', 'SERVERS', 'TABLE', NULL));
Shows the space usage of data blocks
under the segment High Water Mark. The bitmap blocks, segment header, and
extent map blocks are not accounted for by this procedure. This procedure
can only be used on tablespaces that are created with auto segment space
management (ASSM).
Overload 1
dbms_space.space_usage(
segment_owner IN VARCHAR2,
segment_name IN VARCHAR2,
segment_type IN VARCHAR2,
unformatted_blocks OUT NUMBER,
unformatted_bytes OUT NUMBER,
fs1_blocks OUT NUMBER, -- blocks w/ 0-25% free space
fs1_bytes OUT NUMBER, -- bytes w/ 0-25% free space
fs2_blocks OUT NUMBER, -- blocks w/ 25-50% free space
fs2_bytes OUT NUMBER, -- bytes w/ 25-50% free space
fs3_blocks OUT NUMBER, -- blocks w/ 50-75% free space
fs3_bytes OUT NUMBER, -- bytes w/ 50-75% free space
fs4_blocks OUT NUMBER, -- blocks w/ 75-100% free space
fs4_bytes OUT NUMBER, -- bytes w/ 75-100% free space
full_blocks OUT NUMBER,
full_bytes OUT NUMBER,
partition_name IN VARCHAR2 DEFAULT NULL);
conn / as sysdba
SELECT blocks
FROM dba_segments
WHERE segment_name = 'SERVERS';
CREATE TABLE uwclass.servers_bak AS
SELECT * FROM uwclass.servers
WHERE srvr_id LIKE '%5%';
DELETE FROM uwclass.servers
WHERE srvr_id LIKE '%5%';
COMMIT;
INSERT /*+ APPEND */ INTO uwclass.servers
SELECT * FROM uwclass.servers_bak;
Returns the amount of space in blocks
being used by all the SECUREFILE LOBs in the LOB segment. The procedure
displays the space actively used by the LOB column, freed space that has
retention expired, and freed space that has retention unexpired.
Overload 2
I want to thank Dave Franks for catching the missing description of why
the demo at right returns an exception. The exception has now been
properly color highlighted and the exception added to the Exceptions
section above.
dbms_space.space_usage(
segment_owner IN VARCHAR2,
segment_name IN VARCHAR2,
segment_type IN VARCHAR2,
segment_size_blocks OUT NUMBER,
segment_size_bytes OUT NUMBER,
used_blocks OUT NUMBER,
used_bytes OUT NUMBER,
expired_blocks OUT NUMBER,
expired_bytes OUT NUMBER,
unexpired_blocks OUT NUMBER,
unexpired_bytes OUT NUMBER,
partition_name IN VARCHAR2 DEFAULT NULL);
DECLARE
*
ERROR at line 1:
ORA-03213: Invalid Lob Segment Name for DBMS_SPACE package
ORA-06512: at "SYS.DBMS_SPACE", line 234
ORA-06512: at line 11
This overload is officially
undocumented but note the suoption parameter and review the Space Usage
option constants above.
Overload 3
dbms_space.space_usage(
segment_owner IN VARCHAR2,
segment_name IN VARCHAR2,
segment_type IN VARCHAR2,
suoption IN NUMBER, -- see Constants: above
segment_size_blocks OUT NUMBER,
segment_size_bytes OUT NUMBER,
used_blocks OUT NUMBER,
used_bytes OUT NUMBER,
expired_blocks OUT NUMBER,
expired_bytes OUT NUMBER,
unexpired_blocks OUT NUMBER,
unexpired_bytes OUT NUMBER,
partition_name IN VARCHAR2 DEFAULT NULL);
dbms_output.put_line('segment size blocks: ' || TO_CHAR(sbl));
dbms_output.put_line('segment size bytes: ' || TO_CHAR(sby));
dbms_output.put_line('used blocks: ' || TO_CHAR(ubl));
dbms_output.put_line('used bytes: ' || TO_CHAR(uby));
dbms_output.put_line('expired blocks: ' || TO_CHAR(ebl));
dbms_output.put_line('expired bytes: ' || TO_CHAR(eby));
dbms_output.put_line('unexpired blocks: ' || TO_CHAR(xbl));
dbms_output.put_line('unexpired bytes: ' || TO_CHAR(xby));
END;
/
DECLARE
*
ERROR at line 1:
ORA-03213: Invalid Lob Segment Name for DBMS_SPACE package
ORA-06512: at "SYS.DBMS_SPACE", line 274
ORA-06512: at line 11
If you do not wish to use a long list of dbms_output statements try this technique I saw posted by Hazem Ameen in his "Oracle DBA in KSA" blog. I have made minor changes to it bu the essence is Hazem's.
Returns information about unused space in a table, index, or cluster
dbms_space.unused_space (
segment_owner IN VARCHAR2,
segment_name IN VARCHAR2,
segment_type IN VARCHAR2,
total_blocks OUT NUMBER,
total_bytes OUT NUMBER,
unused_blocks OUT NUMBER,
unused_bytes OUT NUMBER,
last_used_extent_file_id OUT NUMBER,
last_used_extent_block_id OUT NUMBER,
last_used_block OUT NUMBER,
partition_name IN VARCHAR2 DEFAULT NULL);
Determines whether a segment can be shrunk to the "shrink_target_bytes" value
dbms_space.verify_shrink_candidate(
segment_owner IN VARCHAR2,
segment_name IN VARCHAR2,
segment_type IN VARCHAR2,
shrink_target_bytes IN NUMBER,
partition_name IN VARCHAR2 DEFAULT NULL)
RETURN BOOLEAN;
BEGIN
IF (dbms_space.verify_shrink_candidate('UWCLASS', 'T', 'TABLE', 262144)) THEN
dbms_output.put_line('Shinkable');
ELSE
dbms_output.put_line('Not Shinkable');
END IF;
END;
/
Pipelined Table Function: Returns 1 if shrinkable, 0 if not shrinkable
dbms_space.verify_shrink_candidate_tbf(
segment_owner IN VARCHAR2,
segment_name IN VARCHAR2,
segment_type IN VARCHAR2,
shrink_target_bytes IN NUMBER,
partition_name IN VARCHAR2 DEFAULT NULL)
RETURN verify_shrink_table PIPELINED;
SELECT *
FROM TABLE(dbms_space.verify_shrink_candidate_tbf('UWCLASS', 'T', 'TABLE', 262144));
SELECT *
FROM TABLE(dbms_space.verify_shrink_candidate_tbf('UWCLASS', 'SERVERS', 'TABLE', 262144));