Skip to content

Fix view-switch panic and add regression tests (issues #99, #116)#136

Merged
lesovsky merged 5 commits into
masterfrom
develop
May 18, 2026
Merged

Fix view-switch panic and add regression tests (issues #99, #116)#136
lesovsky merged 5 commits into
masterfrom
develop

Conversation

@lesovsky
Copy link
Copy Markdown
Owner

@lesovsky lesovsky commented May 18, 2026

Summary

Two independent fixes in `develop`:

Issue #99 — panic on `x` (pg_stat_statements view switch)

Root cause: after pressing `x`, the first stat batch may arrive with the OLD view's column count. `SetAlign` fires on it and populates `ColsWidth` for N columns (`Aligned = true`). The next batch has M ≠ N columns from the new view; `Aligned = true` skips `SetAlign`, so `ColsWidth[N..M-1]` returns 0 → `String[:-1]` → panic / "zero or negative width" error.

Fix: extracted `alignViewToResult()` from `printDbstat`. Re-runs `SetAlign` whenever `len(ColsWidth) != r.Ncols`, regardless of the `Aligned` flag.

Issue #116 — regression test for non-default schema in pg_tables_size

The original bug (v0.6.4 `::regclass` text cast) was already fixed in v0.9.x. Added `Test_StatSizesQueries_NonDefaultSchema` to protect against future regression.

Test plan

  • `Test_alignViewToResult` — 4 sub-cases including the original panic path (5→13 cols)
  • `Test_StatSizesQueries_NonDefaultSchema` — non-default schema in sizes query
  • `go test ./...` passes

Closes #99
Closes #116

Alexey Lesovsky and others added 4 commits May 18, 2026 18:18
Two structural problems in statements tests:

1. t.Skipf inside a bare for-loop caused the entire timing/report/wal
   suite to be skipped when the first PG version (9.5) was unavailable.
   Timing and report query integration tests never ran against PG 17/18.

2. TestSelectStatStatementsTimingQuery and TestSelectQueryReportQuery
   only asserted version routing up to PG 13, so a regression in the
   PG 17+ routing would go undetected.

Fixes:
- Split timing and WAL loops into per-version t.Run sub-tests; missing
  instances now skip only that version, not the entire suite.
- Extend WAL stats test to cover PG 13-18 (was hardcoded to PG 13).
- Add PG 14/16/17/18 cases to both unit routing tests, asserting that
  PG 17+ returns the blk_read_time-free PG17 query variants.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…version

Convention: PGxx suffix marks a query valid from PG xx (now superseded);
Default marks the query for the current/latest supported version.

Previously violated by two files:
- wal.go: PgStatWALDefault covered PG 14-17 (not latest);
  PgStatWALPG18 covered PG 18+ (latest).
- statements.go: PgStatStatementsTimingDefault / ReportQueryDefault
  covered PG 13-16; PG17 variants covered PG 17+ (latest).

Renames:
- PgStatWALDefault       → PgStatWALPG14
- PgStatWALPG18          → PgStatWALDefault
- PgStatStatementsTimingDefault      → PgStatStatementsTimingPG13
- PgStatStatementsTimingPG17         → PgStatStatementsTimingDefault
- PgStatStatementsReportQueryDefault → PgStatStatementsReportQueryPG13
- PgStatStatementsReportQueryPG17    → PgStatStatementsReportQueryDefault

Updated: selector functions, view.go initial templates, all test references.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ssue #122)

Commit cbfa0a4 added shared_preload_libraries to SelectCommonProperties,
increasing the metadata column count from 7 to 8. readMeta's strict
"Ncols != 7" check rejected all tar files recorded after that commit,
printing "invalid result" for every report row.

Fix: replace "!= 7" with "< 2" — readMeta only reads column 1
(version_num), so the minimum required is 2 columns. This accepts
both old (7-col) and current (8-col) metadata without breaking
forward compatibility for any future column additions.

Adds a regression test with 8-column metadata that previously failed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
In v0.6.4 the pg_tables_size query used:
  pg_total_relation_size((schemaname||'.'||relname)::regclass)

The ::regclass text cast failed with "relation does not exist" for tables
in non-default schemas when the name required quoting (mixed case, special
characters) or the schema was not in search_path.

The fix (OID-based lookup via s.relid) was applied in an earlier commit.
This test reproduces the original failure scenario — table in non-default
schema test_dbo — and confirms the OID-based query handles it correctly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Root cause: after switching pg_stat_statements views with 'x', the first
stat batch arriving at printDbstat may still carry the OLD view's column
count. SetAlign fires on it (Aligned=false) and populates ColsWidth for N
columns. The next batch has M != N columns from the new view; Aligned=true
skips SetAlign, so ColsWidth[N..M-1] returns 0. In printStatData the zero
width produces slice bounds [:-1] → panic, or "zero or negative width" error
after the symptom fix in 9adb560.

Fix: extract alignViewToResult() from printDbstat. It re-runs SetAlign
whenever len(ColsWidth) != r.Ncols, regardless of the Aligned flag. This
handles both first-time visits and the post-switch mismatch window.

Test: Test_alignViewToResult covers all four scenarios:
- first render (Aligned=false, empty ColsWidth)
- stable render (Aligned=true, matching column count — no recalc)
- MORE columns than ColsWidth had (the original panic path)
- FEWER columns than ColsWidth had

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@lesovsky lesovsky changed the title Add regression test for sizes query with non-default schema (issue #116) Fix view-switch panic and add regression tests (issues #99, #116) May 18, 2026
@lesovsky lesovsky merged commit f79c67b into master May 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ERROR "relation does not exist" after swich to context s (pg_tables_size) panic: runtime error: slice bounds out of range [:-1]

1 participant