Skip to content

fix: handle negative pg_class.reltuples in PostgreSQL Count functions#1186

Merged
zhenghaoz merged 1 commit intogorse-io:masterfrom
legenduck:fix/postgres-count-negative-reltuples
Mar 4, 2026
Merged

fix: handle negative pg_class.reltuples in PostgreSQL Count functions#1186
zhenghaoz merged 1 commit intogorse-io:masterfrom
legenduck:fix/postgres-count-negative-reltuples

Conversation

@legenduck
Copy link
Contributor

Problem

makeslice: cap out of range panic when using PostgreSQL as data store on fresh/empty database.

Occurs on first startup before ANALYZE runs, because pg_class.reltuples returns -1 (float4) for unanalyzed tables.

Root Cause

CountUsers and CountItems in storage/data/sql.go scan reltuples directly into int64, while CountFeedback correctly uses float64. The -1 passes through to make([]..., -1) → panic.

Fix

  • Scan reltuples as float64 first, then convert (consistent with CountFeedback)
  • Clamp negative values to 0 for all three Count functions

Reproduction

  1. Configure Gorse with PostgreSQL + Redis Stack
  2. Start with empty database (no prior ANALYZE)
  3. Master panics at "load dataset" step:

@codecov
Copy link

codecov bot commented Mar 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.71%. Comparing base (4ed9467) to head (bde0e94).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1186      +/-   ##
==========================================
- Coverage   73.08%   72.71%   -0.37%     
==========================================
  Files          88       88              
  Lines       16254    16258       +4     
==========================================
- Hits        11879    11822      -57     
- Misses       3202     3235      +33     
- Partials     1173     1201      +28     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

PostgreSQL returns reltuples=-1 (float4) for tables that have not been
ANALYZEd yet. CountUsers and CountItems scanned this value directly
into int64, causing `makeslice: cap out of range` panic when the
negative count was used as a slice capacity hint.

CountFeedback already handled this correctly by scanning into float64
first. This commit applies the same pattern to CountUsers and
CountItems, and adds a negative-to-zero clamp for all three functions
as a defensive measure.

Reproduces on fresh PostgreSQL + Redis Stack setup with empty database.
@legenduck legenduck force-pushed the fix/postgres-count-negative-reltuples branch from c9f8271 to bde0e94 Compare March 4, 2026 07:39
@zhenghaoz zhenghaoz merged commit adc3ed9 into gorse-io:master Mar 4, 2026
33 of 37 checks 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.

2 participants