Block to Object Mapper (qblockb.sql)
What This Script Does
Section titled “What This Script Does”This script identifies which database object (table, index, partition, etc.) contains a specific block when given a file ID and block number. It’s essential for corruption analysis, block recovery operations, and understanding which objects are affected when Oracle reports block-level errors in alert logs or trace files.
Script
Section titled “Script”rem qblock.sqlremrem Queries the object name/type when supplied the file_id and block_id.remdefine fileid=&file_iddefine block_id=&bad_blockremset linesize 132remttitle 'Database Object'remcol owner format a12 heading 'OWNER'col segment_name format a30 heading 'SEGMENT NAME'col partition_name format a30 heading 'PARTITION NAME'col object_id format 99999 heading 'ID'col data_object_id format 99999 heading 'DATA|OBJECT ID'col relative_fno format 999999 heading 'REL|FILE|NO'col segment_type format a16 heading 'SEGMENT TYPE'col created format a9 heading 'CREATED'col modified format a9 heading 'MODIFIED'col status format a1 heading 'S'rembreak on owner skip 1remselect e.owner, e.segment_name, e.partition_name, e.segment_type from sys.dba_extents e where e.file_id=&fileid and &block_id between e.block_id and (e.block_id + e.blocks -1) order by 1, 2/undefine fileidundefine block_id
-- Run the script in SQL*Plus or SQLcl@qblockb.sql
-- When prompted, enter:-- file_id: The datafile ID number-- bad_block: The block number within that file
-- Example from alert log error:-- ORA-01578: ORACLE data block corrupted (file # 7, block # 12456)Enter value for file_id: 7Enter value for bad_block: 12456
Parameters
Section titled “Parameters”- &file_id: Datafile ID number (from alert log or v$datafile)
- &bad_block: Block number within the specified file
Required Privileges
Section titled “Required Privileges”- SELECT on SYS.DBA_EXTENTS
- Typically requires DBA role
Sample Output
Section titled “Sample Output”Database Object
OWNER SEGMENT NAME PARTITION NAME SEGMENT TYPE------------ ------------------------------ ------------------------------ ----------------HR EMPLOYEES TABLE EMP_SALARY_IDX INDEX
SALES ORDERS_2024_Q1 ORDERS_2024_Q1 TABLE PARTITION ORD_DATE_IDX ORDERS_2024_Q1 INDEX PARTITION
Key Output Columns
Section titled “Key Output Columns”- OWNER: Schema owner of the segment
- SEGMENT NAME: Name of the table, index, or other segment
- PARTITION NAME: Partition name (if partitioned object)
- SEGMENT TYPE: Type of segment (TABLE, INDEX, TABLE PARTITION, etc.)
Understanding Block Mapping
Section titled “Understanding Block Mapping”Block Location Process
Section titled “Block Location Process”- File identification: Uses file_id to locate datafile
- Extent search: Finds extent containing the block
- Segment mapping: Identifies object owning the extent
- Partition details: Shows partition if applicable
Common Segment Types
Section titled “Common Segment Types”- TABLE: Regular table data
- INDEX: Index structure
- TABLE PARTITION: Partitioned table segment
- INDEX PARTITION: Partitioned index segment
- LOBSEGMENT: LOB data segment
- LOBINDEX: LOB index segment
Common Use Cases
Section titled “Common Use Cases”Block Corruption Analysis
Section titled “Block Corruption Analysis”-- From alert log: ORA-01578 block corrupted@qblockb.sql-- Enter file_id and block number from error-- Identify affected object for recovery planning
RMAN Block Recovery
Section titled “RMAN Block Recovery”-- After identifying corrupted objectRMAN> RECOVER DATAFILE 7 BLOCK 12456;-- Or recover at object levelRMAN> RECOVER TABLE hr.employees;
Performance Issue Investigation
Section titled “Performance Issue Investigation”-- From buffer cache analysis showing hot blocks@qblockb.sql-- Identify which objects are causing contention-- Plan appropriate tuning strategies
Troubleshooting Block Issues
Section titled “Troubleshooting Block Issues”Block Corruption Recovery
Section titled “Block Corruption Recovery”-
Identify object impact:
@qblockb.sql-- Note the affected table/index -
Check object accessibility:
SELECT COUNT(*) FROM hr.employees;-- May fail if corruption affects data -
Recovery options:
-- RMAN block-level recoveryRMAN> RECOVER DATAFILE 7 BLOCK 12456;-- Or table-level recoveryRMAN> RECOVER TABLE hr.employees;
Multiple Block Analysis
Section titled “Multiple Block Analysis”-- For multiple corrupted blocksSELECT DISTINCT e.owner, e.segment_name, e.segment_typeFROM dba_extents eWHERE e.file_id = &file_idAND (&block1 BETWEEN e.block_id AND (e.block_id + e.blocks - 1) OR &block2 BETWEEN e.block_id AND (e.block_id + e.blocks - 1) OR &block3 BETWEEN e.block_id AND (e.block_id + e.blocks - 1));
Object Impact Assessment
Section titled “Object Impact Assessment”-- Determine table size and criticalitySELECT owner, segment_name, segment_type, ROUND(bytes/1024/1024, 2) size_mb, blocksFROM dba_segmentsWHERE owner = '&owner'AND segment_name = '&segment_name';
Advanced Analysis
Section titled “Advanced Analysis”Block Range Analysis
Section titled “Block Range Analysis”-- Find all objects in a block rangeSELECT e.owner, e.segment_name, e.segment_type, e.block_id start_block, e.block_id + e.blocks - 1 end_block, e.blocksFROM dba_extents eWHERE e.file_id = &file_idAND NOT (e.block_id + e.blocks - 1 < &start_block OR e.block_id > &end_block)ORDER BY e.block_id;
Datafile Block Mapping
Section titled “Datafile Block Mapping”-- Show all extents in a datafileSELECT e.owner, e.segment_name, e.segment_type, e.block_id, e.blocks, e.block_id + e.blocks - 1 end_blockFROM dba_extents eWHERE e.file_id = &file_idORDER BY e.block_id;
Cross-Reference with V$BH
Section titled “Cross-Reference with V$BH”-- Find blocks in buffer cacheSELECT obj object_id, dbarfil file_id, dbablk block_id, status, dirty, tempFROM v$bhWHERE dbarfil = &file_idAND dbablk = &block_id;
Partition-Specific Analysis
Section titled “Partition-Specific Analysis”-- Enhanced output for partitioned objectsSELECT e.owner, e.segment_name, e.partition_name, e.segment_type, e.block_id, e.blocks, p.high_value partition_boundFROM dba_extents e, dba_tab_partitions pWHERE e.file_id = &file_idAND &block_id BETWEEN e.block_id AND (e.block_id + e.blocks - 1)AND e.owner = p.table_owner(+)AND e.segment_name = p.table_name(+)AND e.partition_name = p.partition_name(+);
Related Scripts
Section titled “Related Scripts”- Database Files (../administration/dfile.md) - File information and mapping
- Extent Analysis (../administration/dextent.md) - Detailed extent information
- Segment Analysis (dsegment.md) - Segment space usage
- Object Dependencies (dobject.md) - Object relationship analysis
Recovery Planning
Section titled “Recovery Planning”RMAN Recovery Commands
Section titled “RMAN Recovery Commands”-- Block-level recoveryRMAN> RECOVER DATAFILE file_id BLOCK block_id;
-- Validate before recoveryRMAN> VALIDATE DATAFILE file_id BLOCK block_id;
-- Table recovery (if supported)RMAN> RECOVER TABLE schema.table_name;
Alternative Recovery Methods
Section titled “Alternative Recovery Methods”-- Export/Import for small tablesexpdp system/password tables=hr.employees directory=data_pump_dir
-- Tablespace point-in-time recoveryRMAN> RECOVER TABLESPACE users UNTIL TIME 'SYSDATE-1';