cockroachdb / cockroachdb/cockroach
sql: SHOW ZONE CONFIG returns incorrect subzone_id for index/partition zone configs
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
## Summary
`SHOW ZONE CONFIGURATION FOR INDEX` and `SHOW ZONE CONFIGURATION FOR PARTITION` return an incorrect `subzone_id` column value. The value is always `len(zone.Subzones)` regardless of which subzone is being displayed.
## Root Cause
In `pkg/sql/show_zone_config.go:135-139`, the code attempts to find the subzone index using a **pointer comparison**:
```go
for i := range zone.Subzones {
subZoneIdx++
if subzone == &zone.Subzones[i] {
break
}
}
```
However, `GetSubzone()` in `pkg/config/zonepb/zone.go:1211-1212` returns a pointer to a **stack-allocated copy**:
```go
copySubzone := s
return ©Subzone
```
The pointer comparison always fails, so the loop runs to completion.
## Reproduction
```sql
CREATE TABLE t (id INT PRIMARY KEY, INDEX idx (id))
PARTITION BY LIST (id) (PARTITION p1 VALUES IN (1,2,3), PARTITION p2 VALUES IN (4,5,6));
ALTER INDEX t@idx CONFIGURE ZONE USING gc.ttlseconds = 100;
ALTER PARTITION p1 OF TABLE t CONFIGURE ZONE USING gc.ttlseconds = 200;
-- All return subzone_id=3 (wrong); crdb_internal.zones correctly shows 1, 2, 3
SELECT subzone_id FROM [SHOW ZONE CONFIGURATION FOR INDEX t@idx];
SELECT subzone_id FROM [SHOW ZONE CONFIGURATION FOR PARTITION p1 OF TABLE t];
```
## Suggested Fix
Replace the pointer comparison with a field-based match:
```go
for i, s := range zone.Subzones {
if s.IndexID == subzone.IndexID && s.PartitionName == subzone.PartitionName {
subZoneIdx = uint32(i + 1)
break
}
}
```
_This issue was found via automated deep static analysis._
Jira issue: CRDB-62037
Contributor guide
Assessment
This issue has not been assessed yet.