Find Block Segment Information (findblk.sql)
What This Script Does
Section titled “What This Script Does”This script identifies which database segment (table, index, partition, etc.) owns a specific file and block number. It’s essential for troubleshooting block corruption errors, analyzing specific block issues, and understanding storage allocation when Oracle reports file/block-related errors in alert logs or trace files.
Script
Section titled “Script”rem findblk.sqlremselect owner, segment_name, segment_type, tablespace_name, extent_id, file_id, block_id, blocks from sys.dba_extents where file_id = &file and &block between block_id and block_id + blocks - 1/
-- Run the script in SQL*Plus or SQLcl@findblk.sql
-- When prompted, enter:-- file: The datafile ID number-- block: The block number within that file
-- Example from corruption error:-- ORA-01578: ORACLE data block corrupted (file # 5, block # 12456)Enter value for file: 5Enter value for block: 12456
Parameters
Section titled “Parameters”- &file: Datafile ID number (from alert log or v$datafile)
- &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”OWNER SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME EXTENT_ID FILE_ID BLOCK_ID BLOCKS---------- ---------------------- ---------------- --------------- ------------ ---------- ---------- ----------HR EMPLOYEES TABLE USERS 15 5 12288 128
Key Output Columns
Section titled “Key Output Columns”- OWNER: Schema owner of the segment
- SEGMENT_NAME: Name of the table, index, or other segment
- SEGMENT_TYPE: Type of segment (TABLE, INDEX, etc.)
- TABLESPACE_NAME: Tablespace containing the segment
- EXTENT_ID: Extent number within the segment
- FILE_ID: Datafile ID (input parameter)
- BLOCK_ID: Starting block of the extent
- BLOCKS: Number of blocks in this extent
Understanding Block Allocation
Section titled “Understanding Block Allocation”Segment Types
Section titled “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
- CLUSTER: Cluster segment
Block Ownership
Section titled “Block Ownership”- Each block belongs to exactly one segment
- Blocks are allocated in extents (contiguous groups)
- Block numbers are unique within each datafile
Common Use Cases
Section titled “Common Use Cases”Block Corruption Analysis
Section titled “Block Corruption Analysis”-- From alert log: ORA-01578 block corrupted@findblk.sql-- Enter file_id and block number from error-- Identify affected object for recovery planning
Performance Investigation
Section titled “Performance Investigation”-- From buffer cache analysis showing hot blocks@findblk.sql-- Identify which objects are causing contention-- Plan appropriate tuning strategies
Storage Analysis
Section titled “Storage Analysis”-- Analyze specific block allocation@findblk.sql-- Understand extent structure-- Verify storage configuration
Troubleshooting Block Issues
Section titled “Troubleshooting Block Issues”Block Corruption Recovery
Section titled “Block Corruption Recovery”-
Identify object impact:
@findblk.sql-- Note the affected table/index -
Check object accessibility:
SELECT COUNT(*) FROM hr.employees;-- May fail if corruption affects critical blocks -
Recovery options:
-- RMAN block-level recoveryRMAN> RECOVER DATAFILE 5 BLOCK 12456;-- Or table-level recovery (if supported)RMAN> RECOVER TABLE hr.employees;
Extent Analysis
Section titled “Extent Analysis”-- Find all extents for the affected segmentSELECT 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;
Space Utilization
Section titled “Space Utilization”-- Analyze segment space usageSELECT owner, segment_name, segment_type, bytes/1024/1024 size_mb, blocks, extentsFROM dba_segmentsWHERE owner = 'HR'AND segment_name = 'EMPLOYEES';
Advanced Analysis
Section titled “Advanced Analysis”Multiple Block Analysis
Section titled “Multiple Block Analysis”-- For multiple corrupted blocks in same fileSELECT DISTINCT owner, segment_name, segment_type, tablespace_nameFROM dba_extentsWHERE file_id = &file_idAND (&block1 BETWEEN block_id AND block_id + blocks - 1 OR &block2 BETWEEN block_id AND block_id + blocks - 1 OR &block3 BETWEEN block_id AND block_id + blocks - 1);
Block Range Analysis
Section titled “Block Range Analysis”-- Find all segments in a block rangeSELECT owner, segment_name, segment_type, extent_id, block_id, blocks, block_id + blocks - 1 end_blockFROM dba_extentsWHERE file_id = &file_idAND NOT (block_id + blocks - 1 < &start_block OR block_id > &end_block)ORDER BY block_id;
Cross-Reference with Buffer Cache
Section titled “Cross-Reference with Buffer Cache”-- Find if block is currently in memorySELECT obj object_id, dbarfil file_id, dbablk block_id, status, dirty, tempFROM v$bhWHERE dbarfil = &file_idAND dbablk = &block_number;
Segment Header Analysis
Section titled “Segment Header Analysis”-- Find segment header blockSELECT owner, segment_name, header_file, header_blockFROM dba_segmentsWHERE owner = 'HR'AND segment_name = 'EMPLOYEES';
Recovery Planning
Section titled “Recovery Planning”RMAN Recovery Commands
Section titled “RMAN Recovery Commands”-- Validate block before recoveryRMAN> VALIDATE DATAFILE 5 BLOCK 12456;
-- Block-level recoveryRMAN> RECOVER DATAFILE 5 BLOCK 12456;
-- Preview recovery (no changes)RMAN> RECOVER DATAFILE 5 BLOCK 12456 PREVIEW;
Alternative Recovery Methods
Section titled “Alternative Recovery Methods”-- Export/Import for small tablesexpdp system/password tables=hr.employees directory=data_pump_dir
-- Flashback table (if available)FLASHBACK TABLE hr.employees TO TIMESTAMP (SYSTIMESTAMP - INTERVAL '1' HOUR);
-- Drop and recreate index (for index corruption)DROP INDEX hr.emp_name_idx;CREATE INDEX hr.emp_name_idx ON hr.employees(last_name, first_name);
Impact Assessment
Section titled “Impact Assessment”-- Check table size and importanceSELECT owner, table_name, num_rows, blocks, ROUND(avg_row_len * num_rows / 1024 / 1024, 2) estimated_mbFROM dba_tablesWHERE owner = 'HR'AND table_name = 'EMPLOYEES';
-- Check dependenciesSELECT owner, name, type, referenced_owner, referenced_nameFROM dba_dependenciesWHERE referenced_owner = 'HR'AND referenced_name = 'EMPLOYEES';
Related Scripts
Section titled “Related Scripts”- Block to Object Mapper (qblockb.sql) - Enhanced block analysis with more details
- Database Files (../administration/dfile.md) - File information and mapping
- Extent Analysis (../administration/dextent.md) - Detailed extent information
- Segment Analysis (dsegment.md) - Segment space usage
File and Block Context
Section titled “File and Block Context”Datafile Information
Section titled “Datafile Information”-- Get datafile detailsSELECT file_id, file_name, tablespace_name, bytes/1024/1024 size_mb, status, autoextensibleFROM dba_data_filesWHERE file_id = &file_id;
Block Size Information
Section titled “Block Size Information”-- Check database block sizeSELECT value block_size_bytesFROM v$parameterWHERE name = 'db_block_size';