fix: handle negative pg_class.reltuples in PostgreSQL Count functions#1186
Merged
zhenghaoz merged 1 commit intogorse-io:masterfrom Mar 4, 2026
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
zhenghaoz
requested changes
Mar 4, 2026
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.
c9f8271 to
bde0e94
Compare
zhenghaoz
approved these changes
Mar 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
makeslice: cap out of rangepanic when using PostgreSQL as data store on fresh/empty database.Occurs on first startup before
ANALYZEruns, becausepg_class.reltuplesreturns-1(float4) for unanalyzed tables.Root Cause
CountUsersandCountItemsinstorage/data/sql.goscanreltuplesdirectly intoint64, whileCountFeedbackcorrectly usesfloat64. The-1passes through tomake([]..., -1)→ panic.Fix
reltuplesasfloat64first, then convert (consistent withCountFeedback)0for all three Count functionsReproduction