Query Data from Specific Block (qblock.sql)
What This Script Does
Section titled “What This Script Does”This script provides comprehensive block-level analysis by:
- Step 1: Identifying which database object owns the specified block
- Step 2: Retrieving all rows stored in that specific block
- Using ROWID functions to precisely target the block contents
- Supporting partitioned objects with subobject identification
- Showing object metadata including creation and modification dates
- Providing detailed ROWID decomposition for each row
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' new_val ownercol object_name format a30 heading 'OBJECT NAME' new_val tablecol subobject_name format a15 heading 'SUBOBJECT NAME'col object_id format 99999 heading 'ID'col data_object_id format 99999 heading 'DATA|OBJECT ID' new_val data_object_nocol relative_fno format 999999 heading 'REL|FILE|NO' new_val rel_fnocol 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;remset feedback onremttitle 'Rows from File: &fileid, Block &block_id'rem-- define data_object_no=3450-- define rel_fno=411-- define block_id=3202define start_block_no=&block_iddefine end_block_no=&block_iddefine start_row_number=0define end_row_number=9999remdefine rowidtype=1define object_no=&data_object_nodefine relfno=&rel_fnodefine block_no=&block_iddefine startrow=&start_row_numberdefine endrow=&end_row_numberremcol data_object_id format 9999999col rel_fno format 9999col block_id format 9999999col row_no format 9999remrem define table=FET$select /*+ rowid(t) */rowid,dbms_rowid.rowid_object(rowid) data_object_id,dbms_rowid.rowid_relative_fno(rowid) rel_fno,dbms_rowid.rowid_block_number(rowid) block_id,dbms_rowid.rowid_row_number(rowid) row_no,t.*from &owner..&table twhere rowid between dbms_rowid.ROWID_CREATE(&rowidtype, &object_no, &relfno, &start_block_no, &startrow)and dbms_rowid.ROWID_CREATE(&rowidtype, &object_no, &relfno, &end_block_no, &endrow)/
SQL> @qblock.sqlEnter value for file_id: 4Enter value for bad_block: 12345
Parameters
Section titled “Parameters”- file_id: Absolute database file identifier (from V$DATAFILE)
- bad_block: Block number within the specified file
Required Privileges
Section titled “Required Privileges”- SELECT on SYS.DBA_OBJECTS
- SELECT on SYS.DBA_EXTENTS
- SELECT on the target table (determined dynamically)
Sample Output
Section titled “Sample Output”Object Identification (Step 1)
Section titled “Object Identification (Step 1)”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
Row Data Retrieval (Step 2)
Section titled “Row Data Retrieval (Step 2)”Rows from File: 4, Block 12345
ROWID DATA_OBJECT_ID REL_FNO BLOCK_ID ROW_NO EMPLOYEE_ID FIRST_NAME LAST_NAME------------------ -------------- ------- -------- ------ ----------- ---------- ----------AAAMcKAAEAAAADAAAa 12345 4 12345 0 1001 John SmithAAAMcKAAEAAAADAAAb 12345 4 12345 1 1002 Jane DoeAAAMcKAAEAAAADAABc 12345 4 12345 2 1003 Bob Johnson
Key Output Information
Section titled “Key Output Information”Object Details
Section titled “Object Details”- OWNER: Schema that owns the object
- OBJECT NAME: Table, index, or segment name
- SUBOBJECT NAME: Partition name (if applicable)
- OBJECT TYPE: Type of database object
- DATA OBJECT ID: Internal Oracle identifier
- REL FILE NO: Relative file number within tablespace
- CREATED/MODIFIED: Object timestamps
- S: Status (I = Invalid, blank = Valid)
Row Details
Section titled “Row Details”- ROWID: Oracle internal row identifier
- DATA_OBJECT_ID: Object identifier from ROWID
- REL_FNO: Relative file number from ROWID
- BLOCK_ID: Block number from ROWID
- ROW_NO: Row slot number within block
- Additional Columns: All table columns and data
Common Use Cases
Section titled “Common Use Cases”-
Corruption Analysis
- Investigate ORA-01578 errors
- Examine specific corrupt blocks
- Identify affected data rows
-
Block-Level Troubleshooting
- Analyze block contention issues
- Investigate buffer busy waits
- Examine hot block problems
-
Forensic Analysis
- Trace specific data modifications
- Investigate unexpected data changes
- Support recovery planning
Finding File and Block Numbers
Section titled “Finding File and Block Numbers”From Oracle Error Messages
Section titled “From Oracle Error Messages”ORA-01578: ORACLE data block corrupted (file # 4, block # 12345)ORA-08102: index key not found, obj# 12345, file 4, block 12345
From Alert Log Queries
Section titled “From Alert Log Queries”-- Search 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”-- Check corruption findingsSELECT file#, block#, blocks, corruption_typeFROM v$database_block_corruptionORDER BY file#, block#;
From Performance Views
Section titled “From Performance Views”-- Find hot blocksSELECT file#, block#, countFROM ( SELECT file#, block#, COUNT(*) count FROM v$bh WHERE status = 'xcur' GROUP BY file#, block# ORDER BY count DESC)WHERE ROWNUM <= 10;
Block Analysis Techniques
Section titled “Block Analysis Techniques”Multiple Block Range
Section titled “Multiple Block Range”To examine a range of blocks, modify the script:
-- Change these definitionsdefine start_block_no=12345define end_block_no=12350
Specific Row Range
Section titled “Specific Row Range”To examine specific rows within a block:
-- Change these definitionsdefine start_row_number=0define end_row_number=10
ROWID Analysis
Section titled “ROWID Analysis”-- Decompose ROWID componentsSELECT rowid, DBMS_ROWID.ROWID_OBJECT(rowid) object_id, DBMS_ROWID.ROWID_RELATIVE_FNO(rowid) rel_file, DBMS_ROWID.ROWID_BLOCK_NUMBER(rowid) block_num, DBMS_ROWID.ROWID_ROW_NUMBER(rowid) row_numFROM table_nameWHERE DBMS_ROWID.ROWID_BLOCK_NUMBER(rowid) = 12345;
Advanced Analysis
Section titled “Advanced Analysis”Block Utilization
Section titled “Block Utilization”-- Check block space usageSELECT file_id, block_id, COUNT(*) rows_in_block, AVG(vsize(column_list)) avg_row_sizeFROM ( -- Results from qblock script)GROUP BY file_id, block_id;
Cross-Block Analysis
Section titled “Cross-Block Analysis”-- Find related blocks for an objectSELECT extent_id, file_id, block_id, block_id + blocks - 1 end_block, blocksFROM dba_extentsWHERE owner = 'HR' AND segment_name = 'EMPLOYEES'ORDER BY extent_id;
Corruption Recovery Scenarios
Section titled “Corruption Recovery Scenarios”Table Block Corruption
Section titled “Table Block Corruption”- Identify affected rows using this script
- Assess data loss from row examination
- Plan recovery strategy based on importance
- Use RMAN block recovery or table rebuild
Index Block Corruption
Section titled “Index Block Corruption”- Identify index type from object information
- Drop and rebuild index (usually simplest)
- Use RMAN recovery for critical unique indexes
Partition-Specific Issues
Section titled “Partition-Specific Issues”- Identify affected partition from subobject name
- Consider partition-level recovery
- Plan minimal downtime approach
Recovery Commands
Section titled “Recovery Commands”RMAN Block Recovery
Section titled “RMAN Block Recovery”# RMAN block-level recoveryRMAN> RECOVER DATAFILE 4 BLOCK 12345;
Table Analysis
Section titled “Table Analysis”-- Validate table structureANALYZE TABLE hr.employees VALIDATE STRUCTURE CASCADE;
Index Rebuild
Section titled “Index Rebuild”-- Rebuild affected indexALTER INDEX hr.emp_name_idx REBUILD;
Monitoring and Prevention
Section titled “Monitoring and Prevention”Proactive Block Monitoring
Section titled “Proactive Block Monitoring”-- Create monitoring viewCREATE OR REPLACE VIEW hot_blocks ASSELECT file#, block#, count(*) access_count, MAX(timestamp) last_accessFROM v$bhGROUP BY file#, block#HAVING COUNT(*) > 10;
Regular Validation
Section titled “Regular Validation”# RMAN validation schedulerman target / <<EOFVALIDATE DATABASE;LIST CORRUPTION;EOF
Integration with Other Tools
Section titled “Integration with Other Tools”DBVerify Integration
Section titled “DBVerify Integration”# Verify specific file around problem blockdbv file=/path/to/datafile.dbf \ feedback=100 \ start=12340 \ end=12350
ASH Integration
Section titled “ASH Integration”-- Find sessions accessing this blockSELECT session_id, session_serial#, sql_id, eventFROM dba_hist_active_sess_historyWHERE current_file# = 4 AND current_block# = 12345 AND sample_time > SYSDATE - 1;
Best Practices
Section titled “Best Practices”-
Immediate Response
- Document file# and block# from errors
- Run this script to assess impact
- Plan recovery during maintenance window
-
Data Preservation
- Export good data before recovery attempts
- Document affected rows
- Plan rollback strategies
-
Root Cause Analysis
- Check storage subsystem logs
- Review recent changes
- Examine workload patterns
Troubleshooting
Section titled “Troubleshooting”No Object Found
Section titled “No Object Found”Possible Causes:
- Block is in free space
- Incorrect file# or block# values
- Object has been dropped
- System/temporary space
Solutions:
- Verify coordinates from error messages
- Check DBA_FREE_SPACE for free blocks
- Review alert log for context
Permission Errors
Section titled “Permission Errors”Possible Causes:
- Insufficient privileges on target table
- Object in restricted schema
Solutions:
- Grant SELECT privileges
- Run as SYSDBA if needed
- Check object accessibility