Skip to content

analyzer/queries: avoid uint_numeric check failure on negative shares during upsert#1189

Merged
ptrus merged 1 commit intomainfrom
ptrus/fix/delegation-shares-negative
Nov 4, 2025
Merged

analyzer/queries: avoid uint_numeric check failure on negative shares during upsert#1189
ptrus merged 1 commit intomainfrom
ptrus/fix/delegation-shares-negative

Conversation

@ptrus
Copy link
Member

@ptrus ptrus commented Nov 4, 2025

Fixes

{"args":["sapphire","oasis1qpn09fsupnx720uv6zav8vzgkcl3mgfkas7wpth2","oasis1qpn83e8hm3gdhvpfv66xj3qsetkj3ulmkugmmxn3","-13398826426497"],"caller":"client.go:155","db":"nexusmainnet","err":"ERROR: value for domain uint_numeric violates check constraint \"uint_numeric_check\" (SQLSTATE 23514)","level":"error","module":"postgres","msg":"Query","pid":730080,"sql":"\n    INSERT INTO chain.runtime_accounts_delegations AS old (runtime, delegator, delegatee, shares)\n      VALUES ($1, $2, $3, $4)\n    ON CONFLICT (runtime, delegator, delegatee) DO UPDATE\n      SET shares = old.shares + $4","time":"3.914196ms","ts":"2025-11-04T10:56:31.366742875Z"}
{"analyzer":"sapphire","caller":"block.go:493","err":"query 520 &{\n    INSERT INTO chain.runtime_accounts_delegations AS old (runtime, delegator, delegatee, shares)\n      VALUES ($1, $2, $3, $4)\n    ON CONFLICT (runtime, delegator, delegatee) DO UPDATE\n      SET shares = old.shares + $4 [sapphire oasis1qpn09fsupnx720uv6zav8vzgkcl3mgfkas7wpth2 oasis1qpn83e8hm3gdhvpfv66xj3qsetkj3ulmkugmmxn3 -13398826426497]}: ERROR: value for domain uint_numeric violates check constraint \"uint_numeric_check\" (SQLSTATE 23514)","height":11322402,"level":"error","mode":"slow-sync","module":"analysis_service","msg":"failed to process block","ts":"2025-11-04T10:56:31.367852125Z"}

tested the fix with the exact same query that fails on mainnet db (with rollback of-course)

Future TODO:

  • update E2E regression test block ranges so that it covers at least one runtime undelegation

@ptrus ptrus requested a review from Copilot November 4, 2025 11:41
@ptrus ptrus force-pushed the ptrus/fix/delegation-shares-negative branch from cbc1db2 to 866c143 Compare November 4, 2025 11:42
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors two SQL upsert queries to use an "update-first" pattern instead of standard INSERT ... ON CONFLICT syntax. The change prevents premature type validation failures when handling negative deltas for share values in PostgreSQL's custom uint_numeric type.

Key Changes:

  • Replaced standard UPSERT with CTE-based update-first pattern for RuntimeConsensusAccountDelegationUpsert
  • Applied the same pattern to RuntimeConsensusAccountDebondingDelegationUpsert
  • Added explanatory comments describing why the pattern is necessary

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ptrus ptrus force-pushed the ptrus/fix/delegation-shares-negative branch from 866c143 to 48938e9 Compare November 4, 2025 11:46
@ptrus ptrus merged commit 0428c8a into main Nov 4, 2025
20 checks passed
@ptrus ptrus deleted the ptrus/fix/delegation-shares-negative branch November 4, 2025 11:58
@ptrus ptrus self-assigned this Nov 5, 2025
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