Database Object from Block ID (qblocko.sql)
What This Script Does
Section titled “What This Script Does”This script identifies database objects from file and block coordinates by:
- Mapping file ID and block ID to specific database objects
- Finding which table, index, or other segment owns a particular block
- Providing object metadata including creation/modification dates
- Supporting partitioned objects with subobject identification
- Essential for corruption troubleshooting and block-level analysis
Script
Section titled “Script”rem qblock.sqlremrem Queries all rows from a block 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 object_name format a30 heading 'OBJECT NAME'col subobject_name format a15 heading 'SUBOBJECT 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 object_type format a13 heading 'OBJECT TYPE'col created format a9 heading 'CREATED'col modified format a9 heading 'MODIFIED'col status format a1 heading 'S'rembreak on owner skip 1remselect o.owner, o.object_name, o.subobject_name, o.object_type, o.data_object_id, o.created, decode( o.last_ddl_time, created, null, o.last_ddl_time ) modified, decode( o.status, 'VALID', null, 'INVALID', 'I', '?' ) status, e.relative_fno from sys.dba_objects o, sys.dba_extents e where e.file_id=&fileid and &block_id between e.block_id and (e.block_id + e.blocks -1) and o.object_type = e.segment_type and o.owner = e.owner and o.object_name = e.segment_name and nvl(o.subobject_name, 'x') = nvl(e.partition_name, 'x') order by owner, object_name;remundefine fileidundefine block_id
SQL> @qblocko.sqlEnter value for file_id: 4Enter value for bad_block: 12345
Parameters
Section titled “Parameters”- file_id: Database file identifier (from V$DATAFILE or DBA_DATA_FILES)
- bad_block: Block number within the file
Required Privileges
Section titled “Required Privileges”- SELECT on SYS.DBA_OBJECTS
- SELECT on SYS.DBA_EXTENTS
Sample Output
Section titled “Sample Output”Database Object
OWNER OBJECT NAME SUBOBJECT NAME OBJECT TYPE DATA REL CREATED MODIFIED S OBJECT ID FILE NO------------ ------------------------------ --------------- ------------- ------ ------- --------- --------- -HR EMPLOYEES TABLE 12345 4 15-MAR-24
SALES ORDER_ITEMS P_2024_Q1 TABLE 23456 4 01-JAN-24 15-MAR-24 ORDER_ITEMS_IDX P_2024_Q1 INDEX 23457 4 01-JAN-24 15-MAR-24
WAREHOUSE INVENTORY_HIST SYS_P2847 TABLE 34567 4 10-FEB-24
Key Output Columns
Section titled “Key Output Columns”Object Identification
Section titled “Object Identification”- OWNER: Schema that owns the object
- OBJECT NAME: Name of the table, index, or other segment
- SUBOBJECT NAME: Partition or subpartition name (if applicable)
- OBJECT TYPE: Type of database object (TABLE, INDEX, LOB, etc.)
Object Metadata
Section titled “Object Metadata”- DATA OBJECT ID: Internal Oracle object identifier
- REL FILE NO: Relative file number within tablespace
- CREATED: When the object was created
- MODIFIED: When the object was last modified (DDL)
- S: Status indicator (I = Invalid, blank = Valid)
Common Use Cases
Section titled “Common Use Cases”-
Corruption Troubleshooting
- Identify objects affected by ORA-01578 errors
- Map corrupt blocks to specific tables or indexes
- Plan recovery strategies based on object importance
-
Block-Level Analysis
- Understand data distribution across files
- Analyze space usage patterns
- Investigate I/O hotspots
-
Forensic Analysis
- Trace block-level issues to specific objects
- Understand impact of storage failures
- Plan targeted recovery operations
Finding File and Block Information
Section titled “Finding File and Block Information”From Oracle Error Messages
Section titled “From Oracle Error Messages”Oracle corruption errors typically provide file and block numbers:
ORA-01578: ORACLE data block corrupted (file # 4, block # 12345)
From Alert Log Analysis
Section titled “From Alert Log Analysis”-- Search alert log for corruption messagesSELECT message_text, originating_timestampFROM v$diag_alert_extWHERE message_text LIKE '%corrupt%'OR message_text LIKE '%ORA-01578%'ORDER BY originating_timestamp DESC;
From RMAN Validation
Section titled “From RMAN Validation”-- After RMAN validationSELECT file#, block#, blocks, corruption_typeFROM v$database_block_corruptionORDER BY file#, block#;
Advanced Analysis
Section titled “Advanced Analysis”Multiple Block Analysis
Section titled “Multiple Block Analysis”-- Check multiple blocks in a rangeSELECT DISTINCT o.owner, o.object_name, o.object_type, e.block_id start_block, e.block_id + e.blocks - 1 end_block, e.blocks total_blocksFROM dba_objects o, dba_extents eWHERE e.file_id = &file_idAND &start_block <= (e.block_id + e.blocks - 1)AND &end_block >= e.block_idAND o.object_type = e.segment_typeAND o.owner = e.ownerAND o.object_name = e.segment_nameAND NVL(o.subobject_name, 'x') = NVL(e.partition_name, 'x')ORDER BY o.owner, o.object_name, e.block_id;
Object Extent Mapping
Section titled “Object Extent Mapping”-- Show all extents for an objectSELECT extent_id, file_id, block_id, blocks, block_id + blocks - 1 end_blockFROM dba_extentsWHERE owner = 'HR'AND segment_name = 'EMPLOYEES'ORDER BY extent_id;
Free Space Analysis
Section titled “Free Space Analysis”-- Check if block is in free spaceSELECT file_id, block_id, blocks, 'FREE SPACE' object_typeFROM dba_free_spaceWHERE file_id = &file_idAND &block_id BETWEEN block_id AND (block_id + blocks - 1)
UNION ALL
SELECT e.file_id, e.block_id, e.blocks, o.object_typeFROM dba_extents e, dba_objects oWHERE e.file_id = &file_idAND &block_id BETWEEN e.block_id AND (e.block_id + e.blocks - 1)AND o.owner = e.ownerAND o.object_name = e.segment_nameAND o.object_type = e.segment_type;
Corruption Recovery Scenarios
Section titled “Corruption Recovery Scenarios”Table Corruption
Section titled “Table Corruption”If the block belongs to a table:
- Assess Impact: Determine affected rows
- Recovery Options: RMAN block recovery, table rebuild
- Business Impact: Evaluate criticality and downtime
Index Corruption
Section titled “Index Corruption”If the block belongs to an index:
- Drop and Rebuild: Often the simplest solution
- RMAN Recovery: For critical unique indexes
- Performance Impact: Consider during peak hours
System Object Corruption
Section titled “System Object Corruption”If the block belongs to a system object:
- Immediate Action: May require instance restart
- Recovery Planning: Coordinate with Oracle Support
- Impact Assessment: Could affect entire database
Block Recovery Commands
Section titled “Block Recovery Commands”RMAN Block Recovery
Section titled “RMAN Block Recovery”-- RMAN block-level recoveryRMAN> RECOVER DATAFILE 4 BLOCK 12345;
Index Rebuild
Section titled “Index Rebuild”-- For corrupted index blocksALTER INDEX hr.emp_name_idx REBUILD;
Table Analysis
Section titled “Table Analysis”-- Analyze table for corruptionANALYZE TABLE hr.employees VALIDATE STRUCTURE CASCADE;
Prevention and Monitoring
Section titled “Prevention and Monitoring”Regular Validation
Section titled “Regular Validation”-- Check for new corruptionsSELECT file#, block#, blocks, corruption_type, corruption_change#, marked_corruptFROM v$database_block_corruptionWHERE marked_corrupt = 'TRUE';
Proactive Monitoring
Section titled “Proactive Monitoring”-- Create monitoring for new corruptionsCREATE OR REPLACE VIEW corruption_monitor ASSELECT file#, block#, corruption_type, TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') check_timeFROM v$database_block_corruption;
RMAN Validation Scripts
Section titled “RMAN Validation Scripts”# Regular RMAN validationrman target / <<EOFVALIDATE DATABASE;VALIDATE ARCHIVELOG ALL;EOF
Integration with Recovery Tools
Section titled “Integration with Recovery Tools”DBVerify Integration
Section titled “DBVerify Integration”# Verify specific filedbv file=/path/to/datafile.dbf feedback=100 start=12340 end=12350
RMAN Integration
Section titled “RMAN Integration”-- List corruptions found by RMANLIST CORRUPTION;
-- Restore and recover specific blocksRESTORE DATAFILE 4 FROM TAG 'WEEKLY_BACKUP';RECOVER DATAFILE 4 BLOCK 12345;
Best Practices
Section titled “Best Practices”-
Immediate Response
- Document the file# and block# from error messages
- Run this script to identify affected objects
- Assess business impact before planning recovery
-
Recovery Planning
- Consider object criticality and size
- Plan recovery during maintenance windows
- Have rollback plans for recovery attempts
-
Preventive Measures
- Regular RMAN validations
- Monitor storage subsystem health
- Maintain current backups
Troubleshooting
Section titled “Troubleshooting”No Objects Found
Section titled “No Objects Found”If the script returns no rows:
- Free Space: Block might be in free space
- Incorrect Coordinates: Verify file# and block# values
- Dropped Objects: Object may have been dropped
Multiple Objects
Section titled “Multiple Objects”If multiple objects appear:
- Overlapping Extents: Possible data dictionary corruption
- Partition Confusion: Check partition mapping carefully