Skip to content

fix(db): use bigint for token counter columns in user_stats#3755

Merged
waleedlatif1 merged 1 commit intostagingfrom
waleedlatif1/fix-user-stats-int-overflow
Mar 25, 2026
Merged

fix(db): use bigint for token counter columns in user_stats#3755
waleedlatif1 merged 1 commit intostagingfrom
waleedlatif1/fix-user-stats-int-overflow

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • Changed total_tokens_used and total_copilot_tokens from integer to bigint in user_stats table
  • Fixes int4 overflow errors (22003) when updating user stats for heavy-usage accounts

Type of Change

  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@cursor
Copy link

cursor bot commented Mar 25, 2026

PR Summary

Medium Risk
Low-complexity schema migration, but it alters production column types and could impact long-running migrations or any code expecting 32-bit integers.

Overview
Prevents int4 overflow in usage tracking.

Adds a DB migration (0181_dazzling_the_leader.sql) that changes user_stats.total_tokens_used and user_stats.total_copilot_tokens from integer to bigint so large token counts can be stored without errors.

Written by Cursor Bugbot for commit c061ee1. Configure here.

@vercel
Copy link

vercel bot commented Mar 25, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview, Comment Mar 25, 2026 4:03am

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 25, 2026

Greptile Summary

This PR fixes an integer overflow (int4 overflow, error code 22003) in the user_stats table by widening total_tokens_used and total_copilot_tokens from integer (max ~2.1 billion) to bigint (max ~9.2 × 10^18) for heavy-usage accounts.

  • The migration (0181_dazzling_the_leader.sql) performs a non-destructive ALTER TABLE ... SET DATA TYPE bigint on both columns — no data loss risk.
  • schema.ts uses bigint('...', { mode: 'number' }), keeping the TypeScript type as number. This is consistent with the existing storageUsedBytes column in the same table and means no changes are needed in application code.
  • All call sites (logger.ts, update-cost/route.ts) use Drizzle sql template literals for increments, so they are unaffected by the type change.
  • The mode: 'number' mapping is safe for practical token volumes; JavaScript number supports safe integers up to ~9 quadrillion (2^53 - 1), well above any foreseeable token counter value.
  • The migration and journal are properly linked with the correct prevId and index.

Confidence Score: 5/5

  • This PR is safe to merge — it is a minimal, non-destructive type-widening fix for a real production overflow bug.
  • The change is a straightforward int4bigint widening on two columns. The migration is non-destructive, application code is unaffected (uses sql template increments), the mode: 'number' pattern is already established in the schema, and all four generated artifacts (SQL, snapshot, journal) are consistent with each other.
  • No files require special attention.

Important Files Changed

Filename Overview
packages/db/migrations/0181_dazzling_the_leader.sql Two-line ALTER TABLE migration that widens both token counter columns from int4 to bigint; non-destructive and correct.
packages/db/schema.ts Both totalTokensUsed and totalCopilotTokens updated to bigint(..., { mode: 'number' }), consistent with the existing storageUsedBytes bigint pattern in the same table.
packages/db/migrations/meta/_journal.json Migration 0181 properly registered in the journal with correct index and timestamp.
packages/db/migrations/meta/0181_snapshot.json Full schema snapshot updated to reflect bigint type for both token columns; looks auto-generated and consistent.

Sequence Diagram

sequenceDiagram
    participant App as Application Code
    participant Drizzle as Drizzle ORM
    participant PG as PostgreSQL user_stats

    App->>Drizzle: sql`total_tokens_used + ${tokens}`
    Drizzle->>PG: UPDATE user_stats SET total_tokens_used = total_tokens_used + N
    Note over PG: Previously int4 (max ~2.1B)<br/>Now bigint (max ~9.2 × 10^18)
    PG-->>Drizzle: bigint value returned
    Note over Drizzle: mode:'number' maps<br/>bigint → JS number<br/>(safe up to 2^53 - 1)
    Drizzle-->>App: TypeScript number type (unchanged)
Loading

Reviews (1): Last reviewed commit: "fix(db): use bigint for token counter co..." | Re-trigger Greptile

@waleedlatif1 waleedlatif1 merged commit 666dc67 into staging Mar 25, 2026
12 checks passed
@waleedlatif1 waleedlatif1 deleted the waleedlatif1/fix-user-stats-int-overflow branch March 25, 2026 04:08
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.

1 participant